dynamically creating classes from text

T

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: run arg1: howFast arg2: howLong

and then from the text dynamically create a class like the one below
(including the functions and its implementation)

class Dog:
def bark(self, numberBarks, myArg):
print(‘numberBarks: ‘ + numberBarks)
print(‘myArg’ + myArg)
return
def run(self, howFast, howLong, myArg):
print(‘howFast: ‘ + howFast)
print(‘howLong’ + howLong)
print(‘myArg’ + myArg)
return

I know my question is a bit far fetched. Anyhow if it is possible how
is it done?
Thanks so much for any help!
 
C

Chris Angelico

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 of
the text file), yes, you can do that!

What I'd do is parse the file and write out another text file
containing the Python code. If you then need that in the current
session, you could import or eval it. That's likely to be the easiest
solution, I think.

This is a fairly straightforward text processing question. Personally,
I would be inclined to simplify the input file's format; perhaps
something like this:

bark numberBarks
run howFast howLong

Less clutter, just the content you need. Either way, though, all you
need to do is iterate over the lines in the file, parse them to get
what you need, and write out a function definition for each one -
something like this:

out = open("output.py","wt") # you'll want a better file name
out.write("class Dog:\n") # or whatever class name you need
for line in open("input.txt","t"): # use 'with' if you want to learn
about that feature
words=line.split(" ")
out.write("\tdef "+words[0]+"(self, "+", ".join(words[1:])+", myArg):\n")
for word in words[1:]:
out.write("\t\tprint('"+word+": '+"word+")\n")
out.write("\t\treturn\n")

This will more or less do what you want, though with my simpler file
format. Play around with text parsing functions in the standard
library to make it read another format.

Python's pretty good at manipulating text. Whatever you want to do,
you can do fairly easily.

ChrisA
 
M

Marco Nawijn

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: run    arg1: howFast  arg2: howLong

and then from the text dynamically create a class like the one below
(including the functions and its implementation)

class Dog:
        def bark(self, numberBarks, myArg):
                print(‘numberBarks: ‘ + numberBarks)
                print(‘myArg’ + myArg)
                return
        def run(self, howFast, howLong, myArg):
                print(‘howFast: ‘ + howFast)
                print(‘howLong’ + howLong)
                print(‘myArg’ + myArg)
                return

I know my question is a bit far fetched. Anyhow if it is possible how
is it done?
Thanks so much for any help!

Hi,

You could also take a look at the 'type' builtin. It can be used to
dynamically create a class. It allows you to specify the class name,
the bases and a dictionary containing the attributes. A simple
example:
10

The methods might be a little more difficult to attach to the class,
but I don't fully understand your problem. In particular, where does
the implementation of your methods come from?

Marco
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top