How to get all variables of some module in that module

A

Alex Gusarov

Hello, I have a following module and in its end I want to initalize
collection of tables:

Module:

from sqlalchemy import *

metadata = MetaData()

calendars_table = Table('calendars', metadata,
Column('id', Integer, primary_key=True),
Column('title', Unicode(50)),
Column('color', Unicode(6)),
)

events_table = Table('events', metadata,
Column('id', Integer, primary_key=True),
Column('calendar', Integer, ForeignKey('calendars.id')),
Column('date', Date),
Column('title', Unicode(50)),
Column('description', Unicode(1000)),
Column('color', Unicode(6)),
)

tables_collection = {}

Here I want to get all Table instances of current module and put them
into dictionary by names, but I don't know how I can get all variables
of current module in the end of this module. Please, give me a hint.
 
S

Steven D'Aprano

Here I want to get all Table instances of current module and put them
into dictionary by names, but I don't know how I can get all variables
of current module in the end of this module. Please, give me a hint.
['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2',
'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod',
'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow',
'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']


Note that dir() gives you a list of "interesting" names. You can also use
math.__dict__.keys() or the inspect module.

Once you have a key, you can get the actual object you want with getattr:
<built-in function acos>
 
P

Peter Otten

Alex said:
Hello, I have a following module and in its end I want to initalize
collection of tables:

Module:

from sqlalchemy import *

metadata = MetaData()

calendars_table = Table('calendars', metadata,
Column('id', Integer, primary_key=True),
Column('title', Unicode(50)),
Column('color', Unicode(6)),
)

events_table = Table('events', metadata,
Column('id', Integer, primary_key=True),
Column('calendar', Integer, ForeignKey('calendars.id')),
Column('date', Date),
Column('title', Unicode(50)),
Column('description', Unicode(1000)),
Column('color', Unicode(6)),
)

tables_collection = {}

Here I want to get all Table instances of current module and put them
into dictionary by names, but I don't know how I can get all variables
of current module in the end of this module. Please, give me a hint.

tables_collection = dict((k, v) for k, v in globals().iteritems()
if isinstance(v, Table))

Alternatively you may consider using metadata.tables. The keys will then
be "calendars" and "events" rather than the variable names.

Peter
 
A

Alex Gusarov

Steven, Peter, thanks a lot from a Python newcomer. Problem solved
with you help.
Still, Python community is best I ever met :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top