looking for way to include many times some .py code from anotherpython code

Martin MOKREJŠ mmokrejs at ribosome.natur.cuni.cz
Tue Mar 8 16:18:25 EST 2005


Steve Holden wrote:
> Martin MOKREJŠ wrote:
> 
>> Peter Hansen wrote:
>>
>>> Martin MOKREJŠ wrote:
>>>
>>>> Am I so deperately fighting the language? No-one here on the list 
>>>> needs to set hundreds variables at once somewhere in their code? 
>>>
>>>
>>>
>>>
>>> Nobody needs to do that.  As others have pointed out, creating variables
>>> implies wanting to access them distinctly, not as a whole group.  If
>>> you are just going to access them as a group, use contain objects such
>>> as lists or dicts or a custom class, not individual variables.
>>
>>
>>
>> I understand, but this is unfortunately not my case now. It's really
>> about assigning data from mysql to some variables and getting them later
>> checked. The checks will be different for most variables and will even
>> differ if I read from sql or alternatively I instantiate first new data
>> obtained from web interface and write subsequently into sql (for example,
>> all row ID's won't be known while instantiating the objects).
>>
>>>> Now have to figure out how to assign them easily into the XML tree.
>>>
>>>
>>>
>>>
>>> This might be the hint that others were hoping for, about your
>>> real requirements.  Do you mean to say that the whole reason
>>> you have for assigning hundreds of variables is to go and
>>> shove the values right back into another data structure such
>>> as an XML document?  If so, trust us, you very likely don't
>>
>>
>>
>> No, the xml is another reason why I wanted to walk over the __dict__
>> of some object and let something magically constrcut the XML tree for me.
>> But this is really another, distinct problem from teh one I posted 
>> originally.
>>
>>> want to do it by assigning and then referencing hundreds of
>>> variables.
>>
>>
>>
>> I need to test almost every for it's content. Some tests
>> are just that the value is non-empty (few cases), but in most cases
>> a lot more checks (only certain values allowed, or only int type allowed,
>> or only \w is allowed ...).
>>
>> FYI: The program/database runs at the moment under php + mysql.
>>
> I will be *very* surprised if you can't get a much better (i.e. easier 
> and more efficient) solution by stepping back from the programming 

Hmm, I'm not convinced, but I'll put few more words here then. ;)

> details for a moment and explaining what it is you are actually trying 
> to achieve in user-space.
> 
> Can you describe the problem you are trying to solve, rather than the 
> solution you are hoping to adopt?

User inputs data through html forms, data has to be quality-checked
and strored into mysql. Later, data is read from mysql and presented
through web. There's nothing special in it, just that the tables
describe a lot of specific experimental parameters. Tables do reflect
type of informations, so they group the data into logical units - so
tablename reflects the data contained.

In general, there are two types of data, hence my X and Y objects.
The underlaying data at some time point go into sql tables. Before that
happens, the data == variable contents are checked that they contain
expected values (in some cases enumerated values, in some cases integers,
sometime chars and only about 6 blobs). I spent a year developing the
database schema and php code, the schema is nearly optimal. I got bored
by the php code, as it was partly developed by a lazy guy (lazier than I'm).
I went fot python - to have better error handling, have not only web app,
but reusable code for standalone application (html forms can be replaced
by any tcl/tk widget for M$ Windows). Sql transaction I have added to
the php code, but anyway it sucks to work with it further.

My idea is to check some of the values while instantiating, as I get it for
free (assigning either to a default value or raising an exception when
variable is empty). In most cases this is not enough, and I have to type in
the allowed values.
1. In case of enumerated types, I hope to find a tool
able to read sql files and able to extract column definitions. In this
particular case, the program would dynamically read allowed ENUM values,
so whenever sql table is altered, the program will recognize new value
allowed.
2. In most other cases, the values are simply some kind of string, and
.find() et al. will suffice.
3. In case data was read from mysql, I can verify that foreign keys refer
to what they should refer.

OK, I get the data written to mysql. I can fetch it back, and want to dump
it into xml and present on web/(local gui).

I have the claases corresponding to all tables as superclasses of X and Y
as necessary. I went to ask on this list how to assign the variables easily
because parts of the code are almost identical. I believe this has been
answered quite well.

I believe the approach using classes corresponding to every single table
is right, when using them as superclasses for those two, practically
used objects: X and Y.

To print the output on web or any gui, I think I'll use the xml output
and just parse it. I need xml anyway for testing, and definitely want
to be able to construct the html/GUI output from the xml input - again,
for testing. So the objects will more or less exist only to get the
necessary checks done for those myriads of variables, which must be
evaluated in current context. I'd get crazy if I'd store things into
bsbdb -- I'm not going to remember that a[0] is table1, a[1] is table2,
a[0][0] is the primary key called blah, a[0][22] is allowed to be 
equal only to "foo" or "bar" ... and that if a[2][4] is defined (actually number),
the number is the key to search in c[key]. Simply, that's for what I use mysql
I don't want to invent the database schema in bsddb in python. ;)
It's simply data, it must be read into variables in some objects, those
object are groupped into just two superobjects. The superobjects define
check-methods, define how to dump the it's data into xml, how
to write (in which order) the values into mysql.

I'm sorry not to send in the sql schema + the code, but this is my phd thesis. ;)

I'm very glad there's so many people interrested to help - not only - me.
Thanks! Now I'm really looking forward how would you rework this thing.
It's simple, easy, it's just sometime tedious as having 250 columns in 20 tables
simply makes you bored to type the code, after while.

The only think where I think I need help is, how to dump easily into xml say object
X, having variables a, b, c, where c is a ref. to object B, containing variables p, q, r.

>>> B = obj()
>>> setattr(B, p, 44)
>>> setattr(B, q, "sdjahd")
>>> setattr(B, r, "qew")
>>> X = obj()
>>> setattr(X, a, 1)
>>> setattr(X, a, 2)
>>> setattr(X, a, B)

>>> print do_magick(X)
<X>
    <a>1</a>
    <b>2</b>
    <B>
        <p>44</p>
        <q>sdjahd</q>
        <r>qew</r>
    </B>
</X>
>>>

Martin



More information about the Python-list mailing list