开发者

Why is this code giving me an "IndentationError: unexpected unindent"

The code below is really annoying me I have looked on stackoverflow and google yet have not found anything and I am a pretty good pyton programmer and have not as of yet, well untill now, found an error that I could 开发者_如何学JAVAnot deal with. I have tried everything but this peace of code is giving me the IndentationError: unexpected unindent which is weird because the normal error is "undexpected indent" which means as posted many times that its the spacing and how I spaced it so I went through the whole code and nada same error and I put in four spaces correctly and everything still ... nothing. help?

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)


I think your idea is to have a different decorator apply to a function depending on whether the module is being run directly, or imported. Unfortunately this won't work the way you have it, because the decorator call needs the function to follow immediately after it. However, you could do it like this:

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)

Or, assuming you're importing application somewhere, you could just from application import route to begin with, and then you wouldn't need any if statements.


Another possible solution close to what you have:

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)


The syntax of the def statement doesn't allow what your trying to do. Do this instead:

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)


I'm confused with the way you are using decorators, check this out: http://www.python.org/dev/peps/pep-0318/

I'm fairly sure your use of a decorator needs to be at the same level of indentation as the function definition itself. Also, the first decorator (@route()), has no function following it.


You cannot detach a decorator from the decorated function this way.

Still you can compute your decorator and then apply it:

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


You could do something like this

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 route is not needed for anything else, you could just say

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜