Re: dynamically creating classes from text

2012-01-24 Thread Visgean Skeloru
As I assume the text you want to process has some source you might want to look at http://docs.python.org/library/pickle.html ... 2012/1/24 T H > I’m new to python, sorry if my question is a bit naive, I was > wondering if it is possible to parse some text (ie. from a text file > or say html) an

Re: dynamically creating classes from text

2012-01-24 Thread Marco Nawijn
On Jan 24, 6:22 pm, T H wrote: > I’m new to python, sorry if my question is a bit naive, I was > wondering if it is possible to parse some text (ie. from a text file > or say html) and then dynamically create a class? > > for example lets say the contents of the text file is: > >      functionName

Re: dynamically creating classes from text

2012-01-24 Thread Chris Angelico
On Wed, Jan 25, 2012 at 4:22 AM, T H wrote: > I’m new to python, sorry if my question is a bit naive, I was > wondering if it is possible to parse some text (ie. from a text file > or say html) and then dynamically create a class? Presuming that your class name comes from somewhere (eg the name o

dynamically creating classes from text

2012-01-24 Thread T H
I’m new to python, sorry if my question is a bit naive, I was wondering if it is possible to parse some text (ie. from a text file or say html) and then dynamically create a class? for example lets say the contents of the text file is: functionName: bark arg1: numberBarks functionName:

Re: Creating Classes

2009-12-21 Thread Steven D'Aprano
On Sun, 20 Dec 2009 11:11:54 -0500, Steve Holden wrote: > Dave Angel wrote: > [...] >> We were talking about 2.x And I explicitly mentioned 3.x because if >> one develops code that depends on old-style classes, they'll be in >> trouble with 3.x, which has no way to specify old-style classes. In

Re: Creating Classes

2009-12-20 Thread Steve Holden
Dave Angel wrote: [...] > We were talking about 2.x And I explicitly mentioned 3.x because if > one develops code that depends on old-style classes, they'll be in > trouble with 3.x, which has no way to specify old-style classes. In > 3.x, all classes are new-style. And although it'll no longer

Re: Creating Classes

2009-12-19 Thread Dave Angel
Steve Holden wrote: Dave Angel wrote: seafoid wrote: Hey Guys, I have started to read over classes as a brief respite from my parsing problem. When a class is defined, how does the class access the data upon which the class should act? Example: class Seq:

Re: Creating Classes

2009-12-19 Thread Steve Holden
Dave Angel wrote: > seafoid wrote: >> Hey Guys, >> >> I have started to read over classes as a brief respite from my parsing >> problem. >> >> When a class is defined, how does the class access the data upon which >> the >> class should act? >> >> Example: >> >> class Seq:

Re: Creating Classes

2009-12-19 Thread Emeka
Hello Dave > > There are more complex things that can go on, like creating "bound" > function objects, but I think this should get you pretty far. > > Could explain the complex things for me? Regards, Janus -- http://mail.python.org/mailman/listinfo/python-list

Re: Creating Classes

2009-12-18 Thread Dave Angel
Alf P. Steinbach wrote: * Dave Angel -> seafoid: One other point: you should always derive a class from some other class, or 'object' by default. So you should being the class definition by: class Seq(object): Why? It mainly has to do with super(). But in any case if you omit the 'o

Re: Creating Classes

2009-12-18 Thread Alf P. Steinbach
* Dave Angel -> seafoid: One other point: you should always derive a class from some other class, or 'object' by default. So you should being the class definition by: class Seq(object): Why? It mainly has to do with super(). But in any case if you omit the 'object' it's an "old style"

Re: Creating Classes

2009-12-18 Thread Dave Angel
seafoid wrote: Hey Guys, I have started to read over classes as a brief respite from my parsing problem. When a class is defined, how does the class access the data upon which the class should act? Example: class Seq: def _

Re: Creating Classes

2009-12-18 Thread seafoid
Steve, that has indeed clarified matters! Thanks! -- View this message in context: http://old.nabble.com/Creating-Classes-tp26848375p26849864.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list

Re: Creating Classes

2009-12-18 Thread Steve Holden
seafoid wrote: > Hey Guys, > > I have started to read over classes as a brief respite from my parsing > problem. > > When a class is defined, how does the class access the data upon which the > class should act? > > Example: > > class Seq:

Creating Classes

2009-12-18 Thread seafoid
. -- View this message in context: http://old.nabble.com/Creating-Classes-tp26848375p26848375.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list

Re: creating classes with mix-ins

2009-05-12 Thread Carl Banks
On May 12, 4:45 am, samwyse wrote: > Unfortunately, 'boilerplate()' uses the handlers that I provide when > MetaBlog is instantiated. In that case, the handler functions should be attributes of the instance, not of the class. Do something like this: class MetaBlog(object): def __init__(self

Re: creating classes with mix-ins

2009-05-12 Thread samwyse
On May 11, 9:01 pm, Carl Banks wrote: > On May 11, 11:16 am, samwyse wrote: > > > Should I use a class decorator, or a metaclass? > > Here's the thing: unless you have advance knowledge of the methods > defined by self.blog, you can't get the attr_list at class definition > time, which means neit

Re: creating classes with mix-ins

2009-05-11 Thread Carl Banks
On May 11, 11:16 am, samwyse wrote: > I'm writing a class that derives it's functionality from mix-ins. > Here's the code: > >     def boilerplate(what):   # This used to be a decorator, but all of > the >         ##what = f.__name__  # function bodies turned out to be > 'pass'. >         'Validat

Re: creating classes with mix-ins

2009-05-11 Thread samwyse
On May 11, 1:16 pm, samwyse wrote: > I'm writing a class that derives it's functionality from mix-ins. While waiting, I gave a try at using class decorators. Here's what I came up with: def add_methods(*m_list, **kwds): def wrapper(klass): for m_name in m_list: def templ

creating classes with mix-ins

2009-05-11 Thread samwyse
I'm writing a class that derives it's functionality from mix-ins. Here's the code: def boilerplate(what): # This used to be a decorator, but all of the ##what = f.__name__ # function bodies turned out to be 'pass'. 'Validate the user, then call the appropriate plug-in.'

Re: Creating classes and objects more than once?

2008-12-01 Thread Gabriel Genellina
En Sat, 29 Nov 2008 04:51:59 -0200, Carl Banks <[EMAIL PROTECTED]> escribió: On Nov 28, 11:51 pm, Carl Banks Absolute versus relative imports don't have anything to do with the issue here.  PEP 328 concerns itself with imports relative to the executing module in package space.  It has nothing

Re: Creating classes and objects more than once?

2008-11-28 Thread Carl Banks
On Nov 28, 11:51 pm, Carl Banks > Absolute versus relative imports don't have anything to do with the > issue here.  PEP 328 concerns itself with imports relative to the > executing module in package space.  It has nothing to do with imports > relative to the current directory in filename space.

Re: Creating classes and objects more than once?

2008-11-28 Thread Carl Banks
On Nov 28, 2:59 pm, Ben Finney <[EMAIL PROTECTED]> wrote: > Carl Banks <[EMAIL PROTECTED]> writes: > > On Nov 28, 3:15 am, Ben Finney <[EMAIL PROTECTED]> > > wrote: > > > This is resolved in the Python 2.x series by implementing PEP 328 > > > http://www.python.org/dev/peps/pep-0328/>, such that the

Re: Creating classes and objects more than once?

2008-11-28 Thread Ben Finney
Carl Banks <[EMAIL PROTECTED]> writes: > On Nov 28, 3:15 am, Ben Finney <[EMAIL PROTECTED]> > wrote: > > This is resolved in the Python 2.x series by implementing PEP 328 > > http://www.python.org/dev/peps/pep-0328/>, such that the > > search path for ‘import’ does *not* contain the current direct

Re: Creating classes and objects more than once?

2008-11-28 Thread Carl Banks
On Nov 28, 3:24 am, Viktor Kerkez <[EMAIL PROTECTED]> wrote: > On Nov 28, 9:35 am, Carl Banks <[EMAIL PROTECTED]> wrote: > > > However, I'm not so sure the effect of os.chdir() on the import path > > is a good idea. > > I'm not actually using os.chidir(), I just used it here to create a > clearer e

Re: Creating classes and objects more than once?

2008-11-28 Thread Carl Banks
On Nov 28, 3:15 am, Ben Finney <[EMAIL PROTECTED]> wrote: > Carl Banks <[EMAIL PROTECTED]> writes: > > I like to think that "import abc" always does the same thing > > regardless of any seemingly unrelated state changes of my program, > > especially since, as the OP pointed out, import is used as a

Re: Creating classes and objects more than once?

2008-11-28 Thread Viktor Kerkez
On Nov 28, 9:35 am, Carl Banks <[EMAIL PROTECTED]> wrote: > However, I'm not so sure the effect of os.chdir() on the import path > is a good idea. I'm not actually using os.chidir(), I just used it here to create a clearer example. Here is the simplest representation of the problem: http://www.n

Re: Creating classes and objects more than once?

2008-11-28 Thread Ben Finney
Carl Banks <[EMAIL PROTECTED]> writes: > I like to think that "import abc" always does the same thing > regardless of any seemingly unrelated state changes of my program, > especially since, as the OP pointed out, import is used as a means > to ensure singleness. Thus, if I were designing the lang

Re: Creating classes and objects more than once?

2008-11-28 Thread Carl Banks
On Nov 27, 11:42 am, Viktor Kerkez <[EMAIL PROTECTED]> wrote: > Here is the situation: > > $ ls > test > $ cd test > $ ls > __init__.py data.py > $ cat __init__.py > > $ cat data.py > DATA = {} > > $ cd .. > $ python>>> import os > >>> from test.data import DATA > >>> DATA['something'] = 33 > >>> o

Re: Creating classes and objects more than once?

2008-11-27 Thread Viktor Kerkez
On Nov 28, 12:32 am, "Chris Rebert" <[EMAIL PROTECTED]> wrote: > The Python position on singletons is generally to just use a module > instead (preferred), or apply the Borg > pattern:http://code.activestate.com/recipes/66531/ The same problem appears if I use the module (as I pointed in the firs

Re: Creating classes and objects more than once?

2008-11-27 Thread Chris Rebert
On Thu, Nov 27, 2008 at 2:36 PM, Viktor Kerkez <[EMAIL PROTECTED]> wrote: > A better way to do this was http://pastebin.com/m1130d1fe :) > -- > http://mail.python.org/mailman/listinfo/python-list > The Python position on singletons is generally to just use a module instead (preferred), or apply th

Re: Creating classes and objects more than once?

2008-11-27 Thread Viktor Kerkez
A better way to do this was http://pastebin.com/m1130d1fe :) -- http://mail.python.org/mailman/listinfo/python-list

Re: Creating classes and objects more than once?

2008-11-27 Thread Viktor Kerkez
But this means that there is no way to create a safe Singleton in python, because the classes are also created twice. This is the problem that I encountered. I created a complex implementation of a Singleton pattern using metaclasses because I needed the __init__ method to be called just once and

Re: Creating classes and objects more than once?

2008-11-27 Thread Arnaud Delobelle
Viktor Kerkez <[EMAIL PROTECTED]> writes: > Here is the situation: > > $ ls > test > $ cd test > $ ls > __init__.py data.py > $ cat __init__.py > > $ cat data.py > DATA = {} > > $ cd .. > $ python import os from test.data import DATA DATA['something'] = 33 os.chdir('test')

Re: Creating classes and objects more than once?

2008-11-27 Thread Harold Fellermann
On Nov 27, 6:42 pm, Viktor Kerkez <[EMAIL PROTECTED]> wrote: > Is this a bug? It is not a bug: the dictionaries are different because they are loaded from different modules. >>> import os >>> import test.data >>> test.data >>> os.chdir('test') >>> import data >>> data >>> test.data is data Fals

Re: Creating classes and objects more than once?

2008-11-27 Thread Diez B. Roggisch
Viktor Kerkez wrote: > Here is the situation: > > $ ls > test > $ cd test > $ ls > __init__.py data.py > $ cat __init__.py > > $ cat data.py > DATA = {} > > $ cd .. > $ python import os from test.data import DATA DATA['something'] = 33 os.chdir('test') from data import

Creating classes and objects more than once?

2008-11-27 Thread Viktor Kerkez
Here is the situation: $ ls test $ cd test $ ls __init__.py data.py $ cat __init__.py $ cat data.py DATA = {} $ cd .. $ python >>> import os >>> from test.data import DATA >>> DATA['something'] = 33 >>> os.chdir('test') >>> from data import DATA as NEW_DATA >>> DATA {'something': 33} >>> NEW_DAT