Enumeration of strings and export of the constants

L

lnenov

Hi,

I need to enumerate a couple hundred strings to constants and export
them to another module(s) globals.
In the mean time I want to keep my module's namespace as clear as I can
and I don't want to use "import *" later.

Here is the code I intend to use:

test.py
1 class Apinamespace():
2 @staticmethod
3 def import_to(destination):
4
5 for name, number in Apinamespace.__dict__.items():
6 if (name.startswith('__') and name.endswith('__')) or
name == "import_to": # :)
7 pass
8 else:
9 setattr(destination, name, number)
10
11 class Enumerate():
12 def __init__(self, names, start = 0, step = 1):
13 enumerate_to = Apinamespace
14
15 for number, name in self.enumerate(names, start, step):
16 setattr(enumerate_to, name, number)
17
18 def enumerate(self, names, start = 0, step = 1):
19 for index in range(len(names)):
20 yield (start, names[index])
21 start += step
22
23 example_names = ['Foo','Bar','Free', 'Beer']
24 example_names2 = ['Foo_me_too', 'Bar_me_too', 'Free_me_too',
'Beer_me_too']
25 Enumerate(example_names)
26 Enumerate(example_names2, 4, 2)

This works like a charm:
>>> import sys, test
>>> thismodule = sys.modules[__name__]
>>> test.Apinamespace.import_to(thismodule)
>>> Beer 3
>>> Beer_me_too
10

Is there a better and more common way to do this?
Do you know of a way to see the module in/from which a function was
called without using traceback and frames? (I want to remove the
argument from Apinamespace.import_me)
And can line 6 be replaced by something less evil.

Thanks for any suggestions.

Lyudmil
 
I

Ian

Is there a better and more common way to do this?

from itertools import count, izip

class APINamespace(object):

def __init__(self):
self._named_values = []

def enumerate(self, names, start=0, step=1):
self._named_values.extend(izip(names, count(start, step)))

def import_to(self, destination):
for name, number in self._named_values:
setattr(destination, name, number)

Note the "step" parameter of itertools.count requires Python 2.7 or
3.1.
And can line 6 be replaced by something less evil.

Yes, by putting the names to be imported into their own container as
I've done above, instead of polluting the class dictionary with them.

Cheers,
Ian
 
L

lnenov

Is there a better and more common way to do this?
from itertools import count, izip

class APINamespace(object):

def __init__(self):
self._named_values = []

def enumerate(self, names, start=0, step=1):
self._named_values.extend(izip(names, count(start, step)))

def import_to(self, destination):
for name, number in self._named_values:
setattr(destination, name, number)

Note the "step" parameter of itertools.count requires Python 2.7 or
3.1.
And can line 6 be replaced by something less evil.
Yes, by putting the names to be imported into their own container as
I've done above, instead of polluting the class dictionary with them.

Thanks a lot.

It was my initial idea to pull something like that but for some
mysterious reason i failed miserably.

Thanks again.

~Lyudmil
 

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

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top