How to import ...

14 views
Skip to first unread message

weheh

unread,
Sep 6, 2010, 11:17:01 AM9/6/10
to web2py-users
I've written my own serializer, which I've stored locally under
modules.

I've imported the serializer in my controller with the statement

module=local_import('serializer') # note: the v3 doc does not
include quotes around the name of the module

So far, so good. However, the serializer uses regular expressions, so
I do an import re within the serializer:

def my_dummy_serializer(text,tag=None,attr={}):
import re # this is throwing a ticket!
if tag==None: return re.sub('\s+',' ',text)
if tag=='br': return '\n\n'
if tag=='h1': return text
if tag=='h2': return text
if tag=='h3': return text
if tag=='h4': return text
if tag=='p': return text
if tag=='b' or tag=='strong': return text
if tag=='em' or tag=='i': return text
if tag=='tt' or tag=='code': return text
if tag=='a': return text
if tag=='img': return text
return text

The import re throws a ticket:

NameError: global name 're' is not defined

Suggestions?

mdipierro

unread,
Sep 6, 2010, 11:20:14 AM9/6/10
to web2py-users
This is a python thing. Not sure why it works the way it does. Put
import re outside the function and it will work.

weheh

unread,
Sep 6, 2010, 11:28:22 AM9/6/10
to web2py-users
I already tried that and got the same ticket: NameError: global name
're' is not defined

I also put import re in the controller that imports the serializer. No
joy.

mdipierro

unread,
Sep 6, 2010, 11:48:38 AM9/6/10
to web2py-users
which web2py version? source or binary?

weheh

unread,
Sep 6, 2010, 12:04:04 PM9/6/10
to web2py-users
web2py Version 1.83.2 (2010-08-15 08:16:30)
I think it's the binary version. How can I tell?

weheh

unread,
Sep 7, 2010, 3:40:00 PM9/7/10
to web2py-users
Reply all
Reply to author
Forward
0 new messages