I need to parse some text file, create objects for various entities encountered in the text, and put them in some data structure (e.g., a list) for further processing. Example of the text:
laptop
17" dell, weight: 12 lb
desktop
24" hp
I know in advance which entities may exist in the text, and what attributes they are supposed to have. In this example, I would already have classes laptop and desktop defined (probably subclasses of class computer). The parser would just need to create objects laptop('dell', 17, 12), and dekstop('hp', 24).
If I follow this route, I would need to retrieve class names from strings, and create objects of those classes. Is it the Pythonic way of doing things? If so, what's the best approach (using Python 3.1)? If not, what should I do instead?
Thanks!
What
If the classes are defined in
computers.py
, say, you can doto instantiate a
computers.Laptop
. If they are defined in the same file that you are running the code in (so that they are global variables), you can dobut this is less elegant; it would be nicer to put them in a separate scope.
Alternatively, if you want a more powerful mapping (say you want "Nettop", "Lapbook", and "Laptop" all to instantiate
Laptop
), you could maintain a mapping of strings to their corresponding constructor and use that:Technically, what you're asking for (or at least the way everyone is interpreting it) just isn't very good practice, especially if you might be taking input from an untrusted source (remember, any source other than yourself should generally be considered untrusted!). You should whitelist these things explicitly, because someone might trigger the execution of a function or creation of an object you didn't intend with properties you really don't want...
What you can do instead is something like this (this is of course wildly incomplete, but should give you the general idea):
Then just add the mapping for each new kind of object to the possible_classes dict.
you can create instances of a class by it's name using the following code
where
classname
is a stringNot sure about the syntax of python code but is this what you want?
I think a inspection-based method would potentially be quite fragile and resistant to change. What if you want to use classes from other modules?
Why not a object factory? It could be a simple function or a class.
Example: