Re: using names before they're defined
Hiya, you might be interested in this alternative config parsing program: http://www.voidspace.org.uk/python/configobj.html Yes, I know it. But I don't like it. Either a simple ini file do the trick, or I need a full blown app-specific DSL - which can be as simple as a Python file with dicts, lists, etc !-) What do you mean? I don't really understand. I had a blast with configobj and seems to be ok - quite straightfoward for the type of ini file I'm using. configparser prob the same... it's kind of 'much over muchness' as they say Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: using names before they're defined
do you mean 'configparser'? Yes. Does it generate objects from the config file automatically? It generates a representation of the config file as a Python object composed of sections and options. The documentation should get you started. Hiya, you might be interested in this alternative config parsing program: http://www.voidspace.org.uk/python/configobj.html seems to offer a bit more than configparser Dave -- http://mail.python.org/mailman/listinfo/python-list
extender method
'Learning Python' by Lutz and Ascher (excellent book by the way) explains that a subclass can call its superclass constructor as follows: class Super: def method(self): # do stuff class Extender(Super): def method(self): Super.method(self) # call the method in super # do more stuff - additional stuff here I'm trying to use this for a superclass called 'component' in the constructor. I have different types of component (let's say for arguments sake resistor, capacitor etc). When I instantiate a new resistor, say, I want the constructor to call the constructor within the component superclass, and then add some resistor-specific stuff. Now, this is fine using the above code. Where I'm struggling is with argument passing. The following, for example, doesn't seem to work: class Super: def __init__(self, **kargs): self.data = kargs class Extender(Super): def __init__(self, **kargs): Super.__init__(self, kargs) # call the constructor method in Super # do additional extender-specific stuff here What am I doing wrong? I get: TypeError: __init__() takes exactly 1 argument (2 given) WARNING: Failure executing file: main.py Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: using names before they're defined
First case is a little shorter but then you have to use a parser for it There's one builtin. do you mean 'configparser'? I'm just trying to figure out how this works. Does it generate objects from the config file automatically? Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: using names before they're defined
Hi Also, I gave the example using Python code as 'config' format, but any structured enough text format could do, ie JSON, XML, or even ini-like: # schema.ini objects = turbine1, frobnicator2 [turbine1] class=Turbine upstream=frobnicator2 downstream= yes, I like the idea of using .ini type file format or XML, very much. There are parser available which will automatically building python objects from this, won't they (like configparser)? I'll have to get reading over the weekend... def get_class_by_name(name): return globals()[name] QD way to retrieve the class object (Python's classes are themselves objects) known by it's name (as a string). ok, so it actually returns the class object itself. One thing that confuses me is that objects have a name (self.name) but object instances also have a name (e.g. myspecialturbine = turbine(...) how do I discover the name 'myspecialturbine' ?). And object instances have a class name too ('turbine'). Aaargh, too many names! what if just want to know what the name of the instance is (in this case 'myspecialturbine'?) Right. I'll have a go at pulling all this together over the weekend hopefully. Hurrah! Thanks for all the help, to everyone. Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: using names before they're defined
Paddy, thanks for your mail. In Digital electronics we have what are called netlists, (and also component lists) yes, years back I did a 3rd year project on a 'logic simulator' which used the kind of thing you are talking about. I think spice does as well. Fortunately my problem is a little simpler, phew. [by the way, as an aside, check out modelia/dymola for some really powerful simulation stuff http://www.dynasim.se/ - it uses powerful symoblic algebra algorithms to derive system equations and the solve them numerically] With a bit more effort you can create component and link factories that will name instances with the variable they are assigned to without having to put that information in twice. sorry - could you explain a bit more? sounds interesting and also brings me onto another question that has been bugging me, which is, if I want to create components (as object instances) at run time (rather than through a python code imported in), how do I do this? i.e. if I hardcoded something like turbine1 = turbine(...) then python knows that turbine1 is a turbine. but if I had say some kind of user interface and I want to create turbine1 (or suchlike) on the fly, how do I actually do that? how do I create objects after run time? Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: using names before they're defined
Hiya Could you just talk me through this... is it: schema = {'turbine1': {'class': 'Turbine', 'upstream' : ('frobnicator2',), 'downstream' : () # nothing, }, 'frobnicator2' : {'class' : 'Frobnicator', 'upstream' : (), 'downstream' : ('frobnicator2',), }, } ok, so schema is a dictionary of different components, defining what class they are and what they're connected to. def get_class_by_name(name): return globals()[name] what does this function do exactly? def chain_from_schema(schema): objects = {} # so objects is our list of objects ? for name, desc in schema: # can you step through a dictionary like this? klass = get_class_by_name(desc['class']) # does this create an object called klass? objects[name] = klass() sorry for being dim.. Dave -- http://mail.python.org/mailman/listinfo/python-list
using names before they're defined
I have a problem. I'm writing a simulation program with a number of mechanical components represented as objects. When I create instances of objects, I need to reference (link) each object to the objects upstream and downstream of it, i.e. supply = supply() compressor = compressor(downstream=combustor, upstream=supply) combuster = combuster(downstream=turbine, upstream=compressor) etc. the problem with this is that I reference 'combustor' before is it created. If I swap the 2nd and 3rd lines I get the same problem (compressor is referenced before creation). aargh!!! any ideas on getting around this? Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: using names before they're defined
Iain, thanks - very helpful. Really I'm trying to write a simulation program that goes through a number of objects that are linked to one another and does calculations at each object. The calculations might be backwards or fowards (i.e. starting at the supply or demand ends of the system and then working through the objects). And also, I might have multiple objects linked to a single object (upstream or downstream) - e.g. compressor -- multiple combusters - turbine I like your idea of using something like a setStreams method to establish the linking. The streams do reflect each other, although having many-to-one and vice versa will complicate that. I have not quite got my head around having multiple links. In C++ I would be thinking about something like a linked-list but I'm not sure that's the right approach here. Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: using names before they're defined
Bruno, Thanks. An issue is that I need to be able to link multiple objects to a single object etc. Say for example using the previous wording, I might have compressor - multiple combustors - turbine this complicates things slightly. my current thought is to do a two stage initialisation 1. create the objects compressor = compressor() combuster1 = combuster() combuster2 = combuster() etc 2. link them compressor.link(downstream = [combuster1, combuster2]) combuster1.link(upstream = compressor) etc. h I need to give it some more though, particularly how I solve all the linked objects (which is the point) Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: using names before they're defined
Even if you need to do something during attachment of components it is more Pythonic to use properties. So you will write a method in your class name something like _set_up(self,upstream_obj) an _get_up(self). And then at the end of your class put up=property(_get_up, _set_up). You can still use the compr.up=... format. sorry, I don't quite follow. what are properties? Also, you might want to re-think your OO design. It seem that all of your components do a lot of things in common already. For one they all are connected to other components like themselves, they also propably will have method to do some computing, perhaps send or receive stuff from other components, or they all will implement somekind of an event model. In that case you could create a generic component class and sublass the specific implementations from it. yes, I already do this - I have a component class and then the other components inherit from it. Dave -- http://mail.python.org/mailman/listinfo/python-list