1

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 not 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)
3
  • 5
    I don't think you can put a decorator inside an if statement like that. Could that be the problem? Jul 6, 2011 at 0:19
  • 3
    Specifically, it's expecting a function definition inside the if block, so the else is unexpectedly unindented.
    – Thomas K
    Jul 6, 2011 at 0:23
  • "unexpected unindent which is weird because the normal error is "undexpected indent"" - why would that be strange? The code needs to contain both indents and unindents in certain places; therefore either could be in the wrong places, and thus unexpected. Dec 14, 2023 at 11:35

6 Answers 6

9

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.

0
3

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

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)
4
  • so is it not possible to put decorators inside if statements?
    – gabeio
    Jul 6, 2011 at 0:23
  • @C Genius Go: Not without the function they're attached to. You could always set the decorator function within an if call, then use it as a decorator.
    – Thomas K
    Jul 6, 2011 at 0:26
  • Put the if statement inside your own decorator! It's not hard to write a decorator function. Jul 6, 2011 at 0:26
  • The @-syntax requires that what follows the @ is a dotted_name and optionally an argument list enclosed by parens. It cannot be an expression or a statement. Jul 6, 2011 at 0:30
1

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.

2
  • 1
    I updated to the full code and I don't know if I'm actually able to put and if statement around a decorated one... the @route is where the route of the url goes to like in this example @route('/') goes to the index of localhost/
    – gabeio
    Jul 6, 2011 at 0:22
  • No, I don't think you can. It is expecting a function definition after your first decorator, sees the else: (which is not indented like the function definition it expects), and throws the exception.
    – kcbanner
    Jul 6, 2011 at 0:29
0

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(...):
0

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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.