0

私はスクリプトを書いていますが、スクリプトには次の関数があります。

def insert_image(cursor, object_id, sku):
    product_obj = core.Object.get(object_id)
    string_sku = str(sku)
    folder = string_sku[0] + string_sku[1] + string_sku[2]
    found_url = False
    # KLUDGE This is ugly and redundant, however putting this in an if elif elif else throws error when url not found
    # try this url first
    try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
        urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
        found_url = True
    except:
        found_url = False
    # If that one didn't work try this one
    if found_url == False:
        try urllib.urlopen("http://<path to images>/%s/%sPK-PT,PM.jpg" % (folder, sku)):
            urllib.URLopener().retrieve("http://<path to images>/%s/%sPK-PT,PM.jpg" % (folder, sku), "%sPK-PT,PM.jpg" % (sku))
            found_url = True
        except:
            found_url = False
    # If still nothing, one last attempt
    if found_url == False:
        try urllib.urlopen("http://<path to images>/%s/%sCC-PT,IM.jpg" % (folder, sku)):
            urllib.URLopener().retrieve("http://<path to images>/%s/%sCC-PT,IM.jpg" % (folder, sku), "%sCC-PT,IM.jpg" % (sku))
            found_url = True
        except:
            found_url = False
    # We failed to find an image for this product, it will have to be done manually
    if found_url == False:
        log.info("Could not find the image on notions")
        return False

    # Hey we found something! Open the image....
    send_image = open('%sPK-PT,PM.jpg' % sku, 'r')
    # ...and send it for processing
    if product_obj.set_image(send_image, 5, 1) == False:
        return False
    else:
        log.debug("Inserted Image")
        return True

これは、try キャッチを追加するまでうまくいきました。if、elif、関数は問題なく実行されました。これが私の呼び出しとその直前に実行されるコードです。

   if rollback == False:
        # Nah -- it's all good SAVE IT!
        count += 1
        log.debug("INSERT %s" % count)
        conn.commit()
    else:
        # Yeah something went wrong, errors reported why, roll it back
        conn.rollback()
        log.debug("skipped %s" % skip_count)

  # Insert images
        if rollback == False:
            sku = row[0]
            if insert_image(cursor, object_id, sku) == False:
                log.error("Could not get the image inserted for product: %s" % object_id)
                conn.rollback()
            else:
                conn.commit()

私のエラーは次のとおりです。

16:33:46,153 DEBUG [pylons-admin] Inserted Description
16:33:46,164 DEBUG [pylons-admin] Inserted Attributes
16:33:46,164 DEBUG [pylons-admin] INSERT 1
Traceback (most recent call last):
File "<console>", line 47, in <module>
NameError: name 'insert_image' is not defined

呼び出しが 2101 行目にあるため、47 行目が何を意味するのかわかりません。また、try を追加する前に、関数が正常に検出されました。また、最初のコミットを、insert_image を呼び出した後でコミットする前に、今見たように try を追加したときに、insert_image 呼び出しの前に切り替えました。インデント、スペース、タブをチェックしましたが、役に立ちませんでした。

私は TextMate を使用しています。TextMate からスクリプトを実行すると、次のような構文エラーが発生します。

 try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):

(フォルダの ( を指しています... しかし、どこに構文エラーがあるのか​​わかりません。助けてください。私はこのスクリプトに数週間取り組んできましたが、これが最後の実行になるはずでしたテストして完了と呼びます:(

4

2 に答える 2

3

関数に構文エラーがあります:

try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
        urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
        found_url = True
    except:
        found_url = False

そのはず:

try:
    urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
    urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
    found_url = True
except:
    found_url = False

これらの SyntaxErrors をキャッチしてエラーを非表示にするいくつかの広範なキャッチもありますが、insert_image はこのように定義されていません。単独except:で使用しないでください。キャッチしたい例外の名前を常に入力してください。そうしないと、SyntaxError などもキャッチされ、非常に危険です。

于 2010-03-03T14:22:08.923 に答える
0

メソッドの前に空白の問題があるようです。不適切な空白により、通常のコード パスの外に移動されました。クラス内にある場合は、クラス内に存在しないように見える場合があります。

于 2010-03-03T14:24:03.280 に答える