Je suis en train d'écrire un script et dans mon script je cette fonction:non définie mais il est vraiment défini
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
Cela a bien fonctionné jusqu'à ce que j'ajouté les prises d'essai. J'ai eu le si, elif, la fonction fonctionnait très bien. Voici mon appel et le peice de code qui passe juste avant qu'il:
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()
Mon erreur est:
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
Je ne sais pas ce que la ligne 47 des moyens parce que l'appel est en ligne 2101, encore une fois avant d'ajouter les trys, il a trouvé la fonction très bien. J'ai également commuté le premier commit à être avant l'appel insert_image quand j'ai ajouté le trys comme vous voyez maintenant, avant que le commit soit après que nous ayons appelé insert_image. J'ai vérifié les retraits, les espaces et les onglets sans résultat.
J'utilise TextMate, quand je lance le script de TextMate, je reçois une erreur de syntaxe ici:
try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
Il pointe vers le (sur (dossier ... Mais je ne vois pas où je une erreur de syntaxe Aidez-moi, je travaille sur ce script depuis quelques semaines, c'était censé être la dernière exécution à tester et l'appeler fini :(
-1: Wow c'est beaucoup de code. Pourriez-vous réduire cela à juste assez de code pour montrer le problème? –