dynamic - python : read a line, then instantiate a class named in the line -
I'm sorry, if I was not enough in the title, do not hesitate to fix it if you
I have a file where there are class names, that is:
classa classc classc classc classc classc
Then I line it line I want to read and make that class dynamic. I will do something like this in php:
while (! Eof ()) {$ class = fread (..) $ tab [] = new $ square (); }
How would you do this in dragon (if this is possible)?
Thanks a lot!
EDIT: After reading the answer, what I'm planning to do is to be more precise: I'm not planning to do such simple stuff. It would be very complicated: I want a user who does not know programming to edit a simple text file and copy / paste some announcements and change its assets and relaunch a kind of parser That would be the reuser to show the results of batch and complex operations. Simplified Example of a file:
Car: (red, 4_wheels, 4_places) bike: (blue, 2_wheels, 1_place)
< P> Then the user will change it in: Car: (red, 4_wheels, 4_places) Car: (yellow, 4_wheels, 2_places) bike: (blue, 2_wheels, 1_place) bike: (green) , 2_wheels, 2_places)
And then with Python I will read this file, class class and class examples of> bike is thus a user who does not know / understand the dragon D will be able to edit the file without touching a line. I think this is the right way, if you have any other suggestions for this code, you are welcome!
Olivier Pan
As you are using YAML, Consider experimenting with serialized classes obtained from code> yaml.YAMLObject metaclass or register your own representative.
From the PYAML document:
class monster (yaml.YAMLObject): yaml_tag = u '! Monsters' def __init __ (self, name, HP, AC, attacks): self. Name = name self.hp = hp self. Ac = ac self.attacks = attack def __repr __ (self): return "% s (name =% r, hp =% r, ac =% r, attacks =% r) "% (Self .__ class __.__ name__, self name, self., HP, self., Self.tex) print yaml.load (" "--- demon name: cave spider hp: [2,6 ] # 2D6 AC: 16 attacks: [byte, heart] "" ")
print monster (name = 'cave spider', hp = [2, 6] , AC = 16, attacks = ['byte', 'heart'])
This way you can leave several codes to handle the error (e.g. And you have a system that is stronger than malicious configuration files. As an extra bonus, you are able to dump objects from your program into YAML file.