← Back to team overview

oship-dev team mailing list archive

Re: adl2py fundamentals

 

Hi Roberto,

On Thu, 2009-04-23 at 21:55 +0200, Roberto Siqueira wrote:
>  In any case, I 
> suppose that the small "helper class" described in: 
> http://effbot.org/zone/python-code-generator.htm will have some utility 
> here, as handling indentation can be very cumbersome, sometimes.
> 

Very true.

This has given me the idea of re-implementing this as a generator so
that it will have one function: indent().  We can then send it a +1 to
increase indent, -1 to decrease indent and 0 to reset the level back to
no indentation.  A generator keeps track of where it is internally. 

>   Now, let me explain my approach a little more. [Without any 
> engagement, of course; just "brainstorming", OK?] 

This is what this list is for. :-)

> Well: when the 
> adl_1_4.py parser finishes, we have a series of Python objects (classes 
> etc) "living" in program memory, right? 

Kind of.  We get back a pyparsing object, not an archetype object.

> Presently, these objects are 
> made permanent by storing them on a ZODB database. 

Nope.

The following is (should be) of interest to everyone.

A picture (or at least the output in your terminal window) is worth a thousand words.

I have added some print output so that everyone can see what we are working with turning the ADL into pyparsing output and then into Archetypes.

Check out Rev 234. into a fresh environment following all of the install instructions.
You may want to look at the code in oship/src/oship/openehr/atbldr.py  Pyparsing does it's thing and returns it's output at line 63.

I've put some print statements in at lines 64  - 66.
As Setup is executing, switch to your (full screen)  terminal window.

First notice that I print the raw parsed_adl object which is a list of nested lists. 
This list comes from pyparsing with named sections so you'll see in the code things like parsed_adl.ontology, parsed_adl.definition, etc.
This gives us sectional access but we still have nested lists to deal with.  

See the next print of parsed_adl.definition.

There is a function at the bottom of atbldr.py called flatten.  It is an unattributed open source donation that I found. It's original 
task was to take nested lists and turn them into one list. I added two things to this operation.  One is to decode non-ascii characters 
and the other was to replace all of the ['s and ]'s with a space. Those ['s and ]'s confuse Python into thinking you are defining a list object
where you are really working with unicode strings.
Of course we could have used an escape and kept them but in the long run they do not serve any purpose for us so it's easier to just 
remove them entirely. 

So we flatten the section named parse outputs and start creating openEHR objects based on what we have from what was originally the ADL.

Hopefully this helps.

Cheers,
Tim


Attachment: signature.asc
Description: This is a digitally signed message part


References