2011-07-06 242 views
1

下面的代碼真的很煩我我已經看到了stackoverflow和谷歌還沒有找到任何東西,我是一個很不錯的pyton程序員,還沒有到目前爲止,直到現在,發現了一個我無法處理的錯誤。我已經嘗試了一切,但這代碼的和平給我的IndentationError:意外的unindent這是奇怪的,因爲正常的錯誤是「undexpected indent」,這意味着多次發佈其間距,我如何隔開它,所以我經歷了整個代碼和nada同樣的錯誤,我正確地放入了四個空格,一切仍然...沒有。幫幫我?這段代碼爲什麼給我一個「IndentationError:unexpected unindent」

from bottle import Bottle, run, route, static_file, debug 
from mako.template import Template as temp 
from mako.lookup import TemplateLookup 

lookup = TemplateLookup(directories=[base+'/templates']) 
application = Bottle() 

if __name__ == '__main__': 
    @route('/') 
else: 
    @application.route('/') 
def index(): 
    index_temp = lookup.get_template('index.html') 
    return index_temp.render(site=site, corperate=corperate, copyright=copyright) 
+5

我不認爲你可以在裏面放一個裝飾器*這樣的if語句。這可能是問題嗎? –

+3

具體來說,它需要'if'塊內的函數定義,所以'else'是*意外地不縮進*。 –

回答

9

我覺得你的想法是有不同的裝飾適用於根據該模塊是否被直接運行,或導入功能。不幸的是,這不會按照你的方式工作,因爲裝飾器調用需要緊跟其後的函數。但是,你可以做這樣的:

if __name__ != '__main__': 
    route = application.route 

@route('/') 
def index(): 
    index_temp = lookup.get_template('index.html') 
    return index_temp.render(site=site, corperate=corperate, copyright=copyright) 

或者,假設你要導入application的地方,你可以只from application import route開始用,然後你就不需要任何if聲明。

+1

+1以獲得乾淨的解決方案 –

1

我很困惑與正在使用的裝飾方式,檢查了這一點:http://www.python.org/dev/peps/pep-0318/

我相當肯定你的一個裝飾的使用必須在同一水平壓痕作爲函數定義本身。另外,第一個裝飾器(@route())在它之後沒有功能。

+1

我更新到完整的代碼,我不知道我是否實際上能夠把和如果聲明周圍裝飾的一個...... @路由是在這個例子中路由的URL地址喜歡@路由( '/')轉到本地主機的索引/ – gabeio

+0

不,我不認爲你可以。它期望在你的第一個裝飾器之後有一個函數定義,看到'else:'(它不像它期望的函數定義那樣縮進),並拋出異常。 – kcbanner

2

syntax of the def statement不允許你試圖做什麼。做到這一點,而不是:

def index(): 
    index_temp = lookup.get_template('index.html') 
    return index_temp.render(site=site, corperate=corperate, copyright=copyright) 

if __name__ == '__main__': 
    index = route('/')(index) 
else: 
    index = application.route('/')(index) 
+0

因此,如果陳述中不可能放置裝飾器? – gabeio

+0

@C Genius Go:不是沒有他們附加的功能。你可以隨時在一個if調用中設置裝飾器函數,然後用它作爲裝飾器。 –

+0

將if語句放入您自己的裝飾器中!編寫裝飾器函數並不難。 – pfctdayelise

0

這種方式不能從裝飾函數中分離裝飾器。

你仍然可以計算你的裝飾,然後應用它:

if condition: 
    deco = route('/') 
else: 
    deco = application.route('/') 
@deco 
def foo(...): 
0

如果不需要路線爲別的你可以做這樣的事情

route_decorator = route if __name__ == '__main__' else application.route 

@route_decorator('/') 
def index(): 
    index_temp = lookup.get_template('index.html') 
    return index_temp.render(site=site, corperate=corperate, copyright=copyright) 

,你可以只說

if __name__ != '__main__': 
    route = application.route 

@route('/') 
def index(): 
    index_temp = lookup.get_template('index.html') 
    return index_temp.render(site=site, corperate=corperate, copyright=copyright) 
3

另一個可能的解決方案接近你有什麼:

def decorate(func): 
    if __name__ == '__main__': 
     @route('/') 
     def f(): 
      func() 
    else: 
     @application.route('/') 
     def f(): 
      func() 

    return f 

@decorate 
def index(): 
    index_temp = lookup.get_template('index.html') 
    return index_temp.render(site=site, corperate=corperate, copyright=copyright) 
相關問題