Re: [Tutor] perplexing error with shelve REVISED

2007-10-31 Thread Orest Kozyar
 It appears you have a cyclic reference in your doc object.  
 Try adding doc.unlink() before you add it to your shelf.

That fixed the problem.  I did not realize that XML documents could have
cyclic references.  Thanks for all your help!

Orest

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] perplexing error with shelve REVISED

2007-10-31 Thread Orest Kozyar

 It appears you have a cyclic reference in your doc object.  
 Try adding doc.unlink() before you add it to your shelf.

Actually, I just realized that doc.unlink() seems to delete the entire XML
content, which kind of defeats the purpose of caching it.  I'll check on
xml-sig and the comp.lang.python group to see if there are any suggestions.

Thanks again for your help!
Orest

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] perplexing error with shelve

2007-10-30 Thread Orest Kozyar
I have a program which queries an online database (Medline) for XML data.
It caches all data using shelve to avoid hitting the database too many
times.  For some reason, I keep getting a RuntimeError: maximum recursion
depth exceeded when attempting to add a certain record.  This program has
successfully handled over 40,000 records from Medline, but for whatever
reason, this particular record (PMID: 16842422) produces this error.  If I
set maximum recursion depth to a larger value such as 5,000, I get a
segfault.  

Below is a script that should reproduce the problem I am having.  Any
guidance in solving this problem would greatly be appreciated.  

Thank you,
Orest

### START SCRIPT ###
import urllib, shelve
from xml.dom import minidom

baseurl = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?'

params = {
'db':   'pubmed',
'retmode':  'xml',
'rettype':  'medline'
}

badkey = '16842422'
goodkey = '16842423' #or just about any other ID

data = shelve.open('data.tmp', writeback=True)

try:
params['id'] = goodkey
url = baseurl + urllib.urlencode(params)
doc = minidom.parseString(urllib.urlopen(url).read())
print 'Successfully retrieved and parsed XML document with ID %s' %
goodkey
data[goodkey] = doc
print 'Successfully shelved XML document with ID %s' % goodkey
except RuntimeError:
print 'Should not see this error message!'

try:
params['id'] = badkey
url = baseurl + urllib.urlencode(params)
doc = minidom.parseString(urllib.urlopen(url).read())
print 'Successfully retrieved and parsed XML document with ID %s' %
badkey
data[badkey] = doc
#Should not get this message!
print 'Successfully shelved XML document with ID %s' % badkey
except RuntimeError, e:
print 'Error shelving XML document with ID %s' % badkey
print e
### END SCRIPT ###

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] perplexing error with shelve REVISED

2007-10-30 Thread Orest Kozyar
 Please post the entire traceback (omitting duplicate lines). 

Sorry, I should have included the traceback.  I've revised the sample script
so that it generates the traceback when run.  The sample script is at the
very bottom of this email.

### SCRIPT OUTPUT ###
[kozyar]:~$ python example.py 

Successfully retrieved and parsed XML document with ID 16842423
Successfully shelved XML document with ID 16842423
Successfully retrieved and parsed XML document with ID 16842422

Traceback (most recent call last):
  File example.py, line 30, in module
data[badkey] = doc
  File /usr/lib64/python2.5/shelve.py, line 123, in __setitem__
p.dump(value)
RuntimeError: maximum recursion depth exceeded

Exception exceptions.RuntimeError: 'maximum recursion depth exceeded' in
bound method DbfilenameShelf.__del__ of {'16842423':
xml.dom.minidom.Document instance at 0x96f290} ignored


### START SCRIPT ###
import urllib, shelve
from xml.dom import minidom

baseurl = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?'

params = {
'db':   'pubmed',
'retmode':  'xml',
'rettype':  'medline'
}

badkey = '16842422'
goodkey = '16842423' #or just about any other ID

data = shelve.open('data.tmp', writeback=True)

params['id'] = goodkey
url = baseurl + urllib.urlencode(params)
doc = minidom.parseString(urllib.urlopen(url).read())
print 'Successfully retrieved and parsed XML document with ID %s' % goodkey
data[goodkey] = doc
print 'Successfully shelved XML document with ID %s' % goodkey

params['id'] = badkey
url = baseurl + urllib.urlencode(params)
doc = minidom.parseString(urllib.urlopen(url).read())
print 'Successfully retrieved and parsed XML document with ID %s' % badkey
data[badkey] = doc
#Will fail on the above line
print 'Successfully shelved XML document with ID %s' % badkey

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] evaluating AND

2007-09-13 Thread Orest Kozyar
Given a variable x that can either be None or a tuple of two floats [i.e.
(0.32, 4.2)], which syntax is considered most appropriate under Python
coding standards?

if x and x[0]  0:
pass

=OR=

if x:
if x[0]  0:
pass


In the first, I'm obviously making the assumption that if the first
condition evaluates to false, then the second condition won't be evaluated.
But, is this a good/valid assumption to make?  Is there a more appropriate
design pattern in Python?

Thanks!
Orest

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Metaclass programming

2007-09-04 Thread Orest Kozyar

 This could be written
kwargs.update(zip(argnames, args))

Nice trick!  Thanks for the pointer.  

  return type.__call__(cls, kwargs)
 
 You are passing kwargs as a positional argument to __call__(); i.e. 
 passing the dict as an ordinary parameter. To use kwargs as 
 the keyword dict for the call, use the syntax
type.__call__(cls, **kwargs)

Great, I did not realize that ** was part of the syntax so couldn't figure
out what the issue was.  It works smoothly now.  

 I think the problem is elsewhere in your code.

You're right.  I just figured out that for some reason, when I use the
SQLAlchemy mapper() function to map my classes to the corresponding table
object, it seems to affect inspect.getargspec().  

For example:

from sqlalchemy.orm import mapper
from sqlalchemy import Table, MetaData
import inspect

class Foo:
def __init__(self, x, y):
pass

print inspect.getargspec(Foo.__init__)

 (['self', 'x', 'y'], None, None, None)

metadata = MetaData()
foo_table = Table('foo', metadata)
mapper(Foo, foo_table)

print inspect.getargspec(Foo.__init__)

 (['instance'], 'args', 'kwargs', None)

I'm not sure why this would be the case, but is a bit frustrating since I do
need the names of the positional arguments sometimes.  

 Why are you doing this?

Partially as an exercise to help me better understand Python inspection as
well as metaclass programming.  I also am using it in a EntitySingleton
metaclass that I adapted from the SQLAlchemy wiki
(http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject).  Some of my
classes have unique constraints, and I want the metaclass to check these
unique constraints and return an object from the database if an object
meeting these constraints already exists.  

For example:

class User:
__unique__ = ['firstname', 'lastname']
__metaclass__ = EntitySingleton

def __init__(self, firstname, lastname, password):
pass

The metaclass knows what the unique constraints are based on the
__unique__ list, but needs to use inspect.getargspec() to get the variable
names and match them up with the *args list that EntitySingleton.__call__
recieves.  At least, that's how I'm trying to do it, but I expect this might
not be the best way to do it?  Either case, when the class is mapped to a
database table, I lose all this information, so will need to figure out a
different way of approaching this.

Orest

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Metaclass programming

2007-09-03 Thread Orest Kozyar
I have the following code:

class meta(type):

def __call__(cls, *args, **kwargs):
argnames = inspect.getargspec(cls.__init__)[0]
for i, value in enumerate(args):
kwargs[argnames[i]] = value
return type.__call__(cls, kwargs)

class test(object):

__metaclass__ = meta

def __init__(self, x, y, **kwargs):
pass

However, inspect.getargspec(cls.__init__) appears to return only the
arguments of the meta __init__ rather than the test __init__.  Is there any
way I can get access to the test __init__ function from the metaclass?

Orest

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Variable scope for class?

2007-08-30 Thread Orest Kozyar
I'm trying to follow the example listed in the wiki at
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject regarding the
use of a metaclass.  

What I don't understand is how the metaclass (EntitySingleton) has access to
the variable ctx which is instantinated outside the scope of the class
definition.  Could anyone point me in the right direction please?

Thanks,
Orest

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Accesing column of a 2D list

2007-08-20 Thread Orest Kozyar
I've got a 2D list (essentially a list of lists where all sublists are of
the same length).  The sublists are polymorphic.  One 2D list I commonly
work with is:

[ [datetime object, float, int, float],
  [datetime object, float, int, float],
  [datetime object, float, int, float] ]

I'd like to be able to quickly accumulate the datetime column into a list.
Is there a simple/straightforward syntax (such as list[:][0]) to do this, or
do we need to use a for loop?  I expect what I have in mind is similar to
the Python array type, except it would be polymorphic.  

Thanks,
Orest

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor