rules from an xml file
I have a python code that manages some parameters using some variable rules that may change from day to day. I'd like that the code will self-modify according to rules parsed from a xml file: example: rules rule01 if or lessthan par_1=glicemyAtMorning par_2=80/ lessthan par_1=glicemyAtNight par_2=80/ and greaterthan par_1=glicemyAtMorning par_2=0/ or equalto par_1=urine par_2=0/ equalto par_1=urine par_2=+/-/ equalto par_1=urine par_2=+/ /or /and /or /if then sendmessageSomething is wrong!/sendmessage /then /rule01 rule02 ... /rule02 rule03 ... /rule03 /rules Due to the fact that rules may change, I have to manage the python program, parsing the whole xml file, and self-modify the python code according to these variable rules. How can I do? -- http://mail.python.org/mailman/listinfo/python-list
Re: rules from an xml file
jonny wrote: I have a python code that manages some parameters using some variable rules that may change from day to day. I'd like that the code will self-modify according to rules parsed from a xml file: example: rules rule01 if or lessthan par_1=glicemyAtMorning par_2=80/ lessthan par_1=glicemyAtNight par_2=80/ and greaterthan par_1=glicemyAtMorning par_2=0/ or equalto par_1=urine par_2=0/ equalto par_1=urine par_2=+/-/ equalto par_1=urine par_2=+/ /or /and /or /if then sendmessageSomething is wrong!/sendmessage /then /rule01 rule02 ... /rule02 rule03 ... /rule03 /rules Due to the fact that rules may change, I have to manage the python program, parsing the whole xml file, and self-modify the python code according to these variable rules. How can I do? So, essentially you are creating a dynamic language? But you already have one - python! Who's responsible for creating these rules, and what do they allow to be done? Because I'd rather use python snippets to define them, that you compile using the exec-function. If you insist on using XML, use some module like lxml to parse and create your own language constructs. Just a note: the use of par_X as attributes is unfortunate, to say the least. It doesn't allow for easy argument swapping, can cause troubles because you delete one attribute and miss renaming the others, and in general it's not good design to have arbitrary numbers of parameters. Instead, you better go with a nested param-tag. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: rules from an xml file
jonny wrote: I have a python code that manages some parameters using some variable rules that may change from day to day. I'd like that the code will self-modify according to rules parsed from a xml file: example: rules rule01 if or lessthan par_1=glicemyAtMorning par_2=80/ lessthan par_1=glicemyAtNight par_2=80/ and greaterthan par_1=glicemyAtMorning par_2=0/ or equalto par_1=urine par_2=0/ equalto par_1=urine par_2=+/-/ equalto par_1=urine par_2=+/ /or /and /or /if then sendmessageSomething is wrong!/sendmessage /then /rule01 rule02 ... /rule02 rule03 ... /rule03 /rules Due to the fact that rules may change, I have to manage the python program, parsing the whole xml file, and self-modify the python code according to these variable rules. How can I do? A beautiful solution could be to implement a namespace for these elements in lxml.etree (even if it's not an XML namespace with a URI). You would give each of the elements its own subclass of lxml.etree.BaseElement, maybe split into rule elements and expression elements, and then give each of them an evaluate() method that returns the result of the evaluation, such as def evaluate(self): for child in self: if child.evaluate(): return True return False for the or element or def evaluate(self): if self[0].evaluate(): return self.getnext().evaluate() else: return self.getnext().getnext().evaluate() for an if-then-else statement as in your example above (plus special casing if then or else are not there). Then you define a mapping from the tag names to your element classes and let lxml.etree instantiate the decision tree for you. Here is the documentation on this feature: http://codespeak.net/lxml/dev/element_classes.html http://codespeak.net/lxml/dev/element_classes.html#id1 Especially these two class lookup schemes might be handy: http://codespeak.net/lxml/dev/element_classes.html#custom-element-class-lookup http://codespeak.net/lxml/dev/element_classes.html#namespace-class-lookup And here is an example: http://codespeak.net/svn/lxml/trunk/src/lxml/html/__init__.py Have fun, Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: rules from an xml file
On Mon, 24 Sep 2007 13:42:05 +0200, Diez B. Roggisch wrote: jonny wrote: rules rule01 if or lessthan par_1=glicemyAtMorning par_2=80/ lessthan par_1=glicemyAtNight par_2=80/ and greaterthan par_1=glicemyAtMorning par_2=0/ or equalto par_1=urine par_2=0/ equalto par_1=urine par_2=+/-/ equalto par_1=urine par_2=+/ /or /and /or /if then sendmessageSomething is wrong!/sendmessage /then /rule01 rule02 ... /rule02 rule03 ... /rule03 /rules […] […] Just a note: the use of par_X as attributes is unfortunate, to say the least. It doesn't allow for easy argument swapping, can cause troubles because you delete one attribute and miss renaming the others, and in general it's not good design to have arbitrary numbers of parameters. Quite the same is true for numbers in tag names. If you (the OP) need to number the rules better use an attribute for the numbers. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: rules from an xml file
Marc 'BlackJack' Rintsch wrote: On Mon, 24 Sep 2007 13:42:05 +0200, Diez B. Roggisch wrote: jonny wrote: rules rule01 if or lessthan par_1=glicemyAtMorning par_2=80/ lessthan par_1=glicemyAtNight par_2=80/ and greaterthan par_1=glicemyAtMorning par_2=0/ or equalto par_1=urine par_2=0/ equalto par_1=urine par_2=+/-/ equalto par_1=urine par_2=+/ /or /and /or /if then sendmessageSomething is wrong!/sendmessage /then /rule01 rule02 ... /rule02 rule03 ... /rule03 /rules […] […] Just a note: the use of par_X as attributes is unfortunate, to say the least. It doesn't allow for easy argument swapping, can cause troubles because you delete one attribute and miss renaming the others, and in general it's not good design to have arbitrary numbers of parameters. Quite the same is true for numbers in tag names. If you (the OP) need to number the rules better use an attribute for the numbers. I meant of course equalto param0/param param+/param /equalto Diez -- http://mail.python.org/mailman/listinfo/python-list