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

2005-03-09 Thread Martin MOKREJŠ
Steve Holden wrote:
Martin MOKREJŠ wrote:
Steve Holden wrote:
[...]
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)


   1
   2
   
   44
   sdjahd
   qew
   



I still don't really see why you have to store this thing as objects, 
but I appreciate that you can only give limited information and still 
retain the validity of a thesis.
The project is not published yet. When it is, I'll make it free. I'm a 
biologist,
and most biologists care only about the conte

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

2005-03-08 Thread Brian Roberts
> 
> Am I so deperately fighting the language? No-one here on the list needs to 
> set hundreds
> variables at once somewhere in their code? I still don't get why:
> 

I once (and only once) needed hundreds of variables in a program.  It
was to simplify creation of unit tests, not for production use.  The
variable names and data (representing a graph with named nodes) was
stored in a text file, I read that file and used setattr() to create
each variable.  This was in a module that did nothing else, and was
imported by unit test code that benefited from the names when setting
up easily readable test cases.

Background if you're new to Python: importing a module *executes* it;
for most modules the only important stuff executing is the class and
def statements.  However, you can execute more stuff when (rarely)
necessary -- reading a file in my case.  This is very different than,
say, C or C++, which has separate include and execute steps.

In general, you want to either use the built-in lists and dicts, or
create classes/objects to represent hundreds of things.

Brian.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Steve Holden
Martin MOKREJŠ wrote:
Steve Holden wrote:
[...]
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)

   1
   2
   
   44
   sdjahd
   qew
   



I still don't really see why you have to store this thing as objects, 
but I appreciate that you can only give limited information and still 
retain the validity of a thesis.

My own usual approach in such situations is to use metadata to describe 
the structure and required processing of the data, as it's often much 
easier to wri

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

2005-03-08 Thread Peter Hansen
Martin MOKREJŠ wrote:
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 ...).
This sounds very much like an opportunity for some code
that is "data-driven".  Rather than dealing with individual
variables, you define data structures (or even just input
files specialized for your situation, if that seems best)
which describe the sorts of tests you talk about above.
You would include the names of the various fields, in
order to match them up with their respective descriptions,
and then have generic code that dealt with each field in
the appropriate manner, driven by what was in the above
data structures.
As trivialized example, to show what I mean:
import types
NonEmpty = object()
Unique = object()
info = {
'field1': (NonEmpty, r'\w+', types.int),
'field2': (None, r'[a-zA-Z_][a-za-Z0-9]*', types.str),
'foo': (Unique, r'\d+', types.float),
}
# inside some routine that loads the data:
for name, text in readMyData():
try:
flag, pattern, type = info[name]
except KeyError:
print 'ignoring unrecognized field', name
else:
if flag == NonEmpty:
if text == '':
raise ValueError('field %s cannot be empty' % name)
value = convertToType(text, type)
storeMyData(name, value)
and so forth
I think that gives the idea.  Generally if I had
hundreds of fields, I wouldn't even bother with
the hardcoded dict as above but would just define my
own file format to describe all this, and parse it
first into some internal representation before trying
to read the real data.
-Peter
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Martin MOKREJŠ
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 fo

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

2005-03-08 Thread Steve Holden
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 
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?

regards
 Steve
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Martin MOKREJŠ
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.
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Peter Hansen
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.
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
want to do it by assigning and then referencing hundreds of
variables.
-Peter
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Steven Bethard
Martin MOKREJŠ wrote:
The data passed to an instance come from sql tables. I have the idea
every table will be represented by an object.
Have you looked at SQLObject?  I've never used it, but it does seem like 
this is the sort of thing it was designed for:

http://sqlobject.org/
STeVe
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Martin MOKREJŠ
Hi to everyone who has repsonded. I'll try to clarify my problem in more detail.
I believe I have the answers how to assign to self. in superclasses. In case
you would know of yet another way, let me know. ;)
Steve Holden wrote:
Martin MOKREJŠ wrote:
Diez B. Roggisch wrote:
See my post on Mar 2 about "automating assignment of class variables".
I got no answers, maybe I wasn't clear enough ... :(

class Foo:
def __init__(self, a, b, .):
 self.a = a
 self.b = b
 
If that is what you want, then this might help you: Put all the 
values in a
The data passed to an instance come from sql tables. I have the idea
every table will be represented by an object. At the most upper level
of abstraction, I work with, partly overlapping set of tables.
imagine two objects, X and Y. X refers to table T1, T2, T3 while
Y refers to T1, T3, T4, T5.
let's say:
T1 has columns T1C1, T1C2, T1C3
T2 has columns T2C1, T2C2, T2C3, ...
and so on
object t1 is something like:
class t1:
   def __init__(self, T1C1, T1C2, T1C3=None):
   self.T1C1 = T1C1
   self.T1C2 = T1C2
   self.T1C3 = T1C3
similarly in case of t2 or any other table. The column names/types are 
different,
also requirements for non-NULL values differ (T1C3 above is allowed to be 
empty).
T1C2 is ENUM type, for example which must be equal ether to "a" or "b" or "d" 
... etc.
object X is something like:
class x:
   def __init__(self, t1object, t2object, t3object):
   self.t1object = t1object
   self.t2object = t2object
   self.t3object = t3object
   self.xml_root = cElementTree.Element("xml root")
   # and now the boring stuff comes
   self.xml_t1 = cElementTree.SubElement(self.xml_root, "table 1")
   self.xml_t1_T1C1 = cElementTree.SubElement(self.xml_T1C1, "name of T1C1 
comlumn")
   self.xml_t1_T1C1.text = self.t1object.T1C1
   self.xml_t1_T1C2 = cElementTree.SubElement(self.xml_T1C2, "name of T1C2 
comlumn")
   self.xml_t1_T1C2.text = self.t1object.T1C2
   # ... and more or less the same for tables t2 and t3
   def set_more_data_from_optional_tables(self, t6object, t7object):
   # I'd do (self, anyobject) on the line above, but the objects represent 
different
   #   sql tables, so different variable names apply. I believe the best I 
could do
   #   is to walk their __dict__ so I could assign the data under 
self.xml_root ...
   #   can any of teh XML: libraries represent an object in XML?
   self.xml_t6_T6C1 = cElementTree.SubElement(self.xml_T6C1, "name of T6C1 
comlumn")
   self.xml_t6_T6C1.text = self.t6object.T1C1
   self.xml_t6_T6C2 = cElementTree.SubElement(self.xml_T6C2, "name of T6C2 
comlumn")
   self.xml_t6_T6C2.text = self.t6object.T1C2
   # ... and more or less the same for tables t7
class y:
   def __init__(self, t1object, t3object, t4object, t5object=None):
   self.t1object = t1object
   self.t3object = t3object
   self.t4object = t4object
   if t5object:
   self.t5object = t5object
   self.xml_root = cElementTree.Element("xml root")
   # now the code from x.__init__() to table t1 and t3 would be cut&pasted,
   #or now I can say any of the two or three approaches suggested in 
this
   #thread will be used

Good guess! ;)
dictionary - like this:
my_vals = {"a": 1, "b" : 2, }
There are plenty of other ways to create such a dictionary, but I won't
digress on that here.
Now in your class, you pass than dict to your constructor and then 
simply
update the instance's __dict__ so that the keys-value-pairs in my_vals
become attributes:

class Foo:
def __init__(self, my_vals):
 self.__dict__.update(my_vals)
foo = Foo(my_vals)
print foo.a
-> 1
Hope this helps,

Sure. Thanks! Would you prefer exactly for this method over the method 
posted by Kent Johnson
few minutes ago?

Am I so deperately fighting the language? No-one here on the list 
needs to set hundreds
variables at once somewhere in their code? I still don't get why:

Well, consider that you haven't actually made any kind of a case for 
using variables!
OK, imagine I want to read the data from sql into memory and check the
values (there're some conditions they have to fullfill). Therefore, once
I get the classes defined, I'll implement methods to check every table
t1 to tx for it's contents. ^H^H^H^H^H^H^H^H^H^H^H^H Actually, the checks
have to be based on X or Y instance. The check differs between X.t1
and Y.t1. :(
If you ask me why do I have such a mess, my answer is that I don't want
to store same datatype into two different tables. So the location is only
one, but the value has to be filled in only when X and is unused when Y.
Similarly it happens when some foreign keys are/aren't defined.

If the names of these things are dynamic then why would you want to put 
them in an object's namespace, when you could just as easily include

self.insDict = {}
in your __init__() method and then simply update self.insDict when

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

2005-03-08 Thread Steve Holden
Martin MOKREJŠ wrote:
Diez B. Roggisch wrote:
See my post on Mar 2 about "automating assignment of class variables".
I got no answers, maybe I wasn't clear enough ... :(

Seems so - I for example didn't understand it.

I need to define lots of variables. The variable names are often
identical. The problem is that if I put such a code into a function ...
no, I'm not going to pass anything to a function to get it returned 
back.
I just want to get lots of variables assigned, that all. If I put them
into module, it get's exectued only once unless I do reload. And I'd 
have
to use: "from some import *", because mainly I'm interrested in 
assigning
to self: self.x = "blah"
self.y = "uhm"

Okay, I try and guess: From your two posts I infer that you want to set
variables in instances. But you've got lots of these and you don't 
want to
write code like this:

class Foo:
def __init__(self, a, b, .):
 self.a = a
 self.b = b
 
If that is what you want, then this might help you: Put all the values 
in a

Good guess! ;)
dictionary - like this:
my_vals = {"a": 1, "b" : 2, }
There are plenty of other ways to create such a dictionary, but I won't
digress on that here.
Now in your class, you pass than dict to your constructor and then simply
update the instance's __dict__ so that the keys-value-pairs in my_vals
become attributes:
class Foo:
def __init__(self, my_vals):
 self.__dict__.update(my_vals)
foo = Foo(my_vals)
print foo.a
-> 1
Hope this helps,

Sure. Thanks! Would you prefer exactly for this method over the method 
posted by Kent Johnson
few minutes ago?

Am I so deperately fighting the language? No-one here on the list needs 
to set hundreds
variables at once somewhere in their code? I still don't get why:

Well, consider that you haven't actually made any kind of a case for 
using variables!

If the names of these things are dynamic then why would you want to put 
them in an object's namespace, when you could just as easily include

self.insDict = {}
in your __init__() method and then simply update self.insDict whenever 
you want.

If you "set hundreds variables", and their names aren't predictable, 
then presumably you will have to go through similar contortions to 
access them.

So it is generally simpler just to use a dictionary to hold the values 
whose names aren't known in advance. Techniques for inserting names into 
namespaces are known (as you have discovered), but when a beginner wants 
 to know about them it's usually considered a sign of "fighting the 
language".

So, think about this a while and then tell us exactly *why* it's so 
important that these values are stored in variables.


"include somefile.py" would be that non-pythonic so that it's not 
available, but
I have already two choices how to proceed anyway. Thanks. ;)

Now have to figure out how to assign them easily into the XML tree.
martin
You do know there are lots of Python libraries that support XML, right?
regards
 Steve
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Duncan Booth
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? I
> still don't get why: 

I've certainly never needed to set hundreds of variables at once in my 
code. I might have hundreds of values, but if so they go into a list or a 
dictionary or some other suitable data structure.

There isn't any point setting hundreds of variables unless you also 
have hundreds of different expressions accessing hundreds of variables. If 
they are all accessed in a similar way then this indicates they shouldn't 
be separate variables. If they are all accessed differently then you have 
some very complex code and hard to maintain code which can probably be 
simplified.

Can you give a use case where you think 'hundreds of variables' would be 
needed?
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Martin MOKREJŠ
Diez B. Roggisch wrote:
See my post on Mar 2 about "automating assignment of class variables".
I got no answers, maybe I wasn't clear enough ... :(

Seems so - I for example didn't understand it.

I need to define lots of variables. The variable names are often
identical. The problem is that if I put such a code into a function ...
no, I'm not going to pass anything to a function to get it returned back.
I just want to get lots of variables assigned, that all. If I put them
into module, it get's exectued only once unless I do reload. And I'd have
to use: "from some import *", because mainly I'm interrested in assigning
to self: self.x = "blah"
self.y = "uhm"

Okay, I try and guess: From your two posts I infer that you want to set
variables in instances. But you've got lots of these and you don't want to
write code like this:
class Foo:
def __init__(self, a, b, .):
 self.a = a
 self.b = b
 
If that is what you want, then this might help you: Put all the values in a
Good guess! ;)
dictionary - like this:
my_vals = {"a": 1, "b" : 2, }
There are plenty of other ways to create such a dictionary, but I won't
digress on that here.
Now in your class, you pass than dict to your constructor and then simply
update the instance's __dict__ so that the keys-value-pairs in my_vals
become attributes:
class Foo:
def __init__(self, my_vals):
 self.__dict__.update(my_vals)
foo = Foo(my_vals)
print foo.a
-> 1
Hope this helps,
Sure. Thanks! Would you prefer exactly for this method over the method 
posted by Kent Johnson
few minutes ago?
Am I so deperately fighting the language? No-one here on the list needs to set 
hundreds
variables at once somewhere in their code? I still don't get why:
"include somefile.py" would be that non-pythonic so that it's not available, but
I have already two choices how to proceed anyway. Thanks. ;)
Now have to figure out how to assign them easily into the XML tree.
martin
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Martin MOKREJŠ
Kent Johnson wrote:
Martin MOKREJŠ wrote:
Hi,
 I'm looking for some easy way to do something like include in c or PHP.
Imagine I would like to have:
cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py
def a():
   include somefile
   postprocess(a)
def b():
   include somefile
   postprocess(a, b, c)
class klass():
   def __init__(self, a, b, c, d):
   include somefile2

You can do this with module-level variables and a base class for klass:
cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
class base:
def __init__(self, a, b, c, d):
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py
import somefile, somefile2
def a():
   postprocess(somefile.a)
def b():
   postprocess(somefile.a, somefile.b, somefile.c)
class klass(somefile2.base):
   def __init__(self, a, b, c, d):
   somefile2.base.__init__(self, a, b, c, d)
Oh, I've picked up not the best example. I wanted to set the variables
not under __init__, but under some other method. So this is actually
what I really wanted.
class klass(somefile2.base):
  def __init__():
  pass
  def set_them(self, a, b, c, d):
  somefile2.base.__init__(self, a, b, c, d)
Thanks!
Martin
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Diez B. Roggisch
> See my post on Mar 2 about "automating assignment of class variables".
> I got no answers, maybe I wasn't clear enough ... :(

Seems so - I for example didn't understand it.

> 
> I need to define lots of variables. The variable names are often
> identical. The problem is that if I put such a code into a function ...
> no, I'm not going to pass anything to a function to get it returned back.
> I just want to get lots of variables assigned, that all. If I put them
> into module, it get's exectued only once unless I do reload. And I'd have
> to use: "from some import *", because mainly I'm interrested in assigning
> to self: self.x = "blah"
> self.y = "uhm"

Okay, I try and guess: From your two posts I infer that you want to set
variables in instances. But you've got lots of these and you don't want to
write code like this:

class Foo:
def __init__(self, a, b, .):
 self.a = a
 self.b = b
 


If that is what you want, then this might help you: Put all the values in a
dictionary - like this:

my_vals = {"a": 1, "b" : 2, }

There are plenty of other ways to create such a dictionary, but I won't
digress on that here.

Now in your class, you pass than dict to your constructor and then simply
update the instance's __dict__ so that the keys-value-pairs in my_vals
become attributes:

class Foo:

def __init__(self, my_vals):
 self.__dict__.update(my_vals)

foo = Foo(my_vals)

print foo.a

-> 1

Hope this helps,


-- 
Regards,

Diez B. Roggisch
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Martin MOKREJŠ
Scott David Daniels wrote:
Martin MOKREJŠ wrote:
Hi,
 I'm looking for some easy way to do something like include in c or PHP.
Imagine I would like to have:   
 I know about module imports and reloads, but am not sure if this is 
the right way to go. Mainly, I want to assign to multiple object 
instances some self bound variables. Their values will be different, 
so I can't 
 > use global variables.
Someone will, no doubt, find a way to code this.  I suggest you are
fighting the language here -- learn to use it instead.  Decide what
you really want to do, not how you want to do it.  Then try to figure
out how to accomplish your real goal in the normal flow of the language.
See my post on Mar 2 about "automating assignment of class variables".
I got no answers, maybe I wasn't clear enough ... :(
I need to define lots of variables. The variable names are often identical.
The problem is that if I put such a code into a function ... no, I'm not going
to pass anything to a function to get it returned back. I just want to get
lots of variables assigned, that all. If I put them into module, it get's
exectued only once unless I do reload. And I'd have to use:
"from some import *", because mainly I'm interrested in assigning to self:
self.x = "blah"
self.y = "uhm"
I'm newbie, sure.
M.
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Kent Johnson
Martin MOKREJŠ wrote:
Hi,
 I'm looking for some easy way to do something like include in c or PHP.
Imagine I would like to have:
cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py
def a():
   include somefile
   postprocess(a)
def b():
   include somefile
   postprocess(a, b, c)
class klass():
   def __init__(self, a, b, c, d):
   include somefile2
You can do this with module-level variables and a base class for klass:
cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
class base:
def __init__(self, a, b, c, d):
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py
import somefile, somefile2
def a():
   postprocess(somefile.a)
def b():
   postprocess(somefile.a, somefile.b, somefile.c)
class klass(somefile2.base):
   def __init__(self, a, b, c, d):
   somefile2.base.__init__(self, a, b, c, d)
Kent

 I know about module imports and reloads, but am not sure if this is the 
right
way to go. Mainly, I want to assign to multiple object instances some 
self bound
variables. Their values will be different, so I can't use global variables.

Martin
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-03-08 Thread Scott David Daniels
Martin MOKREJŠ wrote:
Hi,
 I'm looking for some easy way to do something like include in c or PHP.
Imagine I would like to have:   
 I know about module imports and reloads, but am not sure if this is the 
right way to go. Mainly, I want to assign to multiple object instances 
some self bound variables. Their values will be different, so I can't 
> use global variables.
Someone will, no doubt, find a way to code this.  I suggest you are
fighting the language here -- learn to use it instead.  Decide what
you really want to do, not how you want to do it.  Then try to figure
out how to accomplish your real goal in the normal flow of the language.
-Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list