2010-03-03 4 views
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 

이 잘했다. 나는 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() 

내 오류는 다음과 같습니다 : 여기 내 전화 및 바로 전에 실행되는 코드의의 peice는

전화 라인 2101가 뭔지 라인 47 의미하기 때문에 나도 몰라
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 

, trys를 추가하기 전에 다시 함수를 찾았습니다. insert_image를 호출 한 후 커밋이 발생하기 전에 지금 보았던 것처럼 trys를 추가 할 때 insert_image 호출 전에 첫 번째 커밋을 전환했다. 들여 쓰기와 공백, 탭을 확인했습니다.

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

그것은 가리키는 (폴더 (에 ... 그러나 나는이 위치를 확인하지 않습니다

내가 TextMate를를 사용하는 내가 TextMate를에서 스크립트를 실행할 때, 내가 여기에 구문 오류가

구문 오류가 발생했습니다. 제발 도와주세요. 나는이 스크립트에 대해 2 주 동안 작업 해왔다. 테스트를 마친 후 마지막으로 테스트를 마쳤다. (

+1

-1 : 와우는 많은 코드입니다. 문제를 보여주기에 충분한 코드로 줄였습니까? –

답변

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 같은 것을 잡아낼 수 있으며 이것은 매우 위험합니다.

+0

나는 그것이 명백한 무엇인가 알고 있었다. 고맙습니다. 나는 PHP에서 왔고 지금까지는 실제로 사용해 본 적이 없다. 내 예외가 무엇인지 어떻게 알 수 있습니까? 나는 그것이 URL이 발견되지 않을 것이라는 것을 알고 있지만, 어떻게 예외를 두어야 하는지를 어떻게 알 수 있습니까? – KacieHouser

+1

공정하게 말하자면 런타임에 'SyntaxError'를 거의 접할 수 없습니다. 'try' 블록이 실행되기 훨씬 전에 컴파일 타임에 생성됩니다. –

+0

아아 이것이 기능을 찾지 못했기 때문에 기능을 찾았을 때 오류가 발견되어 그것을 제외 시켰기 때문에 기능을 찾지 못했습니다. 우욱. – KacieHouser

0

방법 전에 공백 문제가있는 것 같습니다. 부정확 한 공백 문자가 정상적인 코드 경로 바깥으로 옮겼습니다. 클래스에있는 경우 클래스에 더 이상 표시되지 않을 수 있습니다.