Enumeration of strings and export of the constants

Discussion in 'Python' started by lnenov, Nov 10, 2010.

  1. lnenov

    lnenov Guest

    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
    lnenov, Nov 10, 2010
    #1
    1. Advertising

  2. lnenov

    Ian Guest

    On Nov 10, 6:12 am, lnenov <> wrote:
    > 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
    Ian, Nov 10, 2010
    #2
    1. Advertising

  3. lnenov

    lnenov Guest

    On 11/11/2010 01:30 AM, Ian wrote:
    > On Nov 10, 6:12 am, lnenov<> wrote:
    >> 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

    > Cheers,
    > Ian
    lnenov, Nov 11, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    740
    Malcolm
    Jun 24, 2006
  2. kenkahn

    Accessing enumeration strings in macros

    kenkahn, Mar 28, 2007, in forum: C Programming
    Replies:
    3
    Views:
    603
    Eric Sosman
    Mar 29, 2007
  3. puvit82
    Replies:
    4
    Views:
    749
    puvit82
    Feb 1, 2008
  4. Steve Allan
    Replies:
    5
    Views:
    718
    Steve Allan
    Jul 29, 2003
  5. kj
    Replies:
    5
    Views:
    116
    Anno Siegel
    Feb 12, 2004
Loading...

Share This Page