Re: newbie write to file question

2005-12-04 Thread Rob E
> I'm not sure what I'm
> missing so I'd appreciate some advice.
You question is pretty general and I'm not going to go over this in any
great detail, but I will make a few comments. 

*  In your if section use if ... else constructs not all the strange if 
   and then not if blocks.  Also get rid of all those unneeded if's with the 
   pass in them.  They do nothing.
*  You may want to put the heart of the code in a separate function or if 
   you need persistance, use a class.  Optional and depends on how complex
   your analysis code is going to be.  Generally one function should not
   be two deep in terms of block nesting for readabilily
   and maintainability.
*  the table initialization i.e. table = {} was outside of your main file
   scan loop, that seemed strange to me since I think you were doing this
   file by file.
*  your log writing code was indented below the the if sub_three is None:
   if block which means that it's inside that block -- that's probably not
   what you want.  Remember python defines blocks by indentation.  The
   indentation is a nice feature because python blocking is in fact like
   it looks (unlike C++).  
*  if your parsing XML and maybe SGML the python library has some special 
   tools for this.  You might want to look at the lib or search the net.

Take care,
Rob

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


Re: Efficient lookup in list of dictionaries

2005-12-04 Thread Rob E
> Hi. I like working with lists of dictionaries since order is preserved 
> in a list when I want order and the dictionaries make it explicit what 
> I have got inside them. I find this combination very useful for storing 
> constants especially. Generally I find myself either needing to 
> retrieve the values of constants in an iterative way (as in my 
> contrived example below). Perhaps even more frequent is given one value 
> is to look up the matching value in a dict (contained in the list) and 
> then obtain the value of another element in the same dictionary.

Instead of doing this in an iterative way -- create a dictionary that
points to the correct dictionary, i.e., who's key is the search key and
whose value is the dictionary (or list of dicts if more than one).  This
approach is basically accelerating the lookup by creating a special index.

Another way of doing this is to create a class that works like an
"ordered" dictionary.  Once can do this with a little programming -- for
example by putting your data in a list and creating a dict that indexes 
the records of the list -- i.e. the key is the index and the value is the
index of the list.  This is pretty much how databases work.

Rob


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


Re: Strange behavior of int()

2006-01-29 Thread Rob E
> Why is int(r/k), where r = 0.5 and k = 0.5 = 0?  Shouldn't it be 1?
> And why is the last one = 4 and not 5?

I dont' know why the differences in your exact case.  However, please
realise that Regardless of the programming language good programming 
practice is to never rely on the int of a floating point division 
-- or even the int of a floating point constant to be exactly 
the integer value you expect it to be.  This is because both 
constant representations and math operations have rounding 
error.  Instead do less precise conversions by rounding.  For 
example:

a=b/c
if (a >= 0.0):
   d = int(a+0.5)
else:
   d = int(a-0.5)

If you don't do this sort of thing your programs generally 
are  senstivie to the details of foating point rounding -- 
which is generally dependent on processor, compilier and
in pythons case probably the runtime system.

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


Re: List append

2007-09-15 Thread Rob E
On Sat, 15 Sep 2007 03:25:27 +, mouseit wrote:

> I'm trying to add an element to a list which is a property of an
> object, stored in an array. When I append to one element, all of the
> lists are appended!
> 
> Example Code:
> 
> class Test:
> array = []
> 
> myTests = [Test() , Test() , Test()]
> print len(myTests[1].array)
> myTests[0].array.append( 5 )
> print len(myTests[1].array)
> 
> prints:
> 0
> 1
> 
> This is probably a really easy question (I'm very new to python), so
> thanks in advance for your help!

Yes, that's easy:

class myclass:
   var1 = []

means that var1 is associated with the class.  If you want an attribute:

class myclass:
def __init__ (self):
self.var1 = []

is the correct way.

Rob

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


Re: scope of variables

2006-05-03 Thread Rob E
> is the code below correct?
> 
> b = 3
> def adding(a)
> print a + b
> 
> it seams not to see the up-level scope where b is defined.

Yes except for the missing : at the end of the "def" line.

Rob

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