[Tutor] Clash of the Titans and Mundane Matters

2005-01-19 Thread Michael Powe
Clash of the Titans

>From "Dive into Python":

__init__ is called immediately after an instance of the class is
created. It would be tempting but incorrect to call this the
constructor of the class. It's tempting, because it looks like a
constructor (by convention, __init__ is the first method defined for
the class), acts like one (it's the first piece of code executed in a
newly created instance of the class), and even sounds like one ("init"
certainly suggests a constructor-ish nature). Incorrect, because the
object has already been constructed by the time __init__ is called,
and you already have a valid reference to the new instance of the
class. But __init__ is the closest thing you're going to get to a
constructor in Python, and it fills much the same role.

>From Alan's book "Learning to Program":

One of the methods of this class is called __init__ and it is a
special method called a constructor. The reason for the name is that
it is called when a new object instance is created or constructed. Any
variables assigned (and hence created in Python) inside this method
will be unique to the new instance. There are a number of special
methods like this in Python, nearly all distinguished by the __xxx__
naming format.


Mundane Matters

I'm having a hard time with classes in python, but it's coming
slowly.  One thing that I think is generally difficult is to parse a
task into "objects."  Here's an example:  in Java, I wrote an
application to track my travelling expenses (I'm a consultant; this
tracking of expenses is the itch I am constantly scratching.  ;-)
I've also written this application in a perl/CGI web application as
well.)  It's easy to see the outline of this task:  create an abstract
class for expense and then extend it for the particular types of
expenses -- travel, food, transportation, lodging and so forth.  In
python, I guess I'd create a class and then "subclass" it.  But
... what are reading/writing to files and printing?  Of course, I
create a "file object" in order to accomplish these tasks -- but how
is this object fit into the application design?  Do I just create
methods within the expense class to accomplish these parts of the
task?  When I tried this on, it seemed hacky.  The other option seemed
to be creating an I/O class and passing the expense objects to it.
But, should that class be an interface or an object?  The one thing
you don't see in "how to program" java books is an implementation of
I/O in the context of an application.

A similar problem occurs with my HTML-parsing routine that I brought
to the list recently.  Use of HTMLParser was suggested.  I've looked
into this and usage means subclassing HTMLParser in order to implement
the methods in the way that will accomplish my task.  Conceptually,
I'm having a hard time with the "object" here.  (The fairly poor
documentation for HTMLParser doesn't help.)  Apparently, I'm creating
a "parser" object and feeding it data.  At least, that's the closest I
can get to understanding this process.  How I'm actually feeding data
to the "parser" object and retrieving the results are matters open to
discussion.  I'll be working on that when I get another chance.

Finally, in terms of "understanding python," the question I keep
coming up against is:  why do we have both functions and methods?
What is the rationale for making join() a string method and a os.path
function?

Thanks for your time.  It's late.  ;-)  Sometimes, I just have to get
these things off my chest.

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


Re: [Tutor] Fw: Please submit to tutor list: dictionary update prob

2005-01-19 Thread Kent Johnson
Jacob S. wrote:
sorry to send this to you but if you may, kindly send to tutor list as im
no longer subscribed.  my problem is in the update dict portion: it just
doesnt update regardless how many contacts i add. kindly advise where
my mistake is or code gone wrong. the rest of the options i will do on my
own so just hold off the helps for now. appreciate all your good help.

def update_dict(d, f):
   ''' update the saved dictionary file '''
   read = open(f, 'rb')
   newdic = cPickle.load(read)
   newdic.update(d)
   read.close()
You don't do anything with newdic. My guess is you want to dump it back to 
the file so it is saved.
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] Unexpected result from decimal

2005-01-19 Thread Tony Meyer
> >>> import decimal
> >>> decimal.getcontext().prec = 2
> >>> a = decimal.Decimal(2)
> >>> b = decimal.Decimal(3)
> >>> 100*a/b
> Decimal("67")
> >>> print 100*a/b

This prints "67".

> try - 
> 
> a=decimal.Decimal(2.0)

This will not work.  You can't convert a float directly to a decimal.Decimal
(I believe this is so that you are forced to understand that there are
precision issues involved).  'a = decimal.Decimal("2.0")' will do what you
meant, though.

> b = decimal.Decimal(3)
> print 100*a/b

However, this will print out "67", just as the above did.  The reason is the
one that Tim outlined: precision isn't the number of digits after the
decimal place - when I was at school the latter was called "decimal places"
and precision was "significant digits".

> Jacob- one slight flaw/quirk in Python is if you want floating point
> computations you have to specify a floating point.
[...]
> Same as writing 100/3.0 as opposed to 100/3. Try it. 

Note that you can do 'from __future__ import division' and 100/3 will be the
same as 100/3.0 (and 100//3 will give you 3).

=Tony.Meyer

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


Re: [Tutor] Unexpected result from decimal

2005-01-19 Thread Orri Ganel




Jacob S. wrote:
Hi all,
  
  
   I'm having a problem that is ticking me off. (to put it lightly)
  
Why does decimal do this --  I thought that getcontext().prec was
number of decimal places?
  
  
  

  import decimal

decimal.getcontext().prec = 2

a = decimal.Decimal(2)

b = decimal.Decimal(3)

100*a/b

  

  
Decimal("67")
  
  

  print 100*a/b

  

  
67
  
  

  
  

  
  
Why does it do this?
  
It's really, really, messing things up for me because results are not
interpreted in my programs correctly.
  
Jacob 
___
  
Tutor maillist  -  Tutor@python.org
  
http://mail.python.org/mailman/listinfo/tutor
  
  

Well, from looking at the documentation, decimal.getcontext().prec
*tells* you the precision, so assigning to it is meaningless. Let's see
what you have to do to actually change it:

>>> a = decimal.Decimal(2)
>>> b = decimal.Decimal(3)
>>> 100*a/b
Decimal("66.67")
>>> decimal.BasicContext
Context(prec=9, rounding=ROUND_HALF_UP, Emin=-9,
Emax=9, capitals=1, flags=[], traps=[Clamped, DivisionByZero,
InvalidOperation, Overflow, Underflow])
>>> decimal.DefaultContext
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-9,
Emax=9, capitals=1, flags=[], traps=[DivisionByZero,
InvalidOperation, Overflow])
>>> decimal.ExtendedContext
Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-9,
Emax=9, capitals=1, flags=[], traps=[])
>>> decimal.setcontext(decimal.ExtendedContext)
>>> decimal.getcontext().prec
9
>>> decimal.setcontext(decimal.Context(prec=50,
rounding=decimal.ROUND_HALF_EVEN, Emin=-9, Emax=9,
capitals=1, flags=[], traps=[decimal.DivisionByZero,
decimal.InvalidOperation, decimal.Overflow]))
>>> decimal.getcontext().prec
50
>>> 100*a/b
Decimal("66.6667")

Hold on a second . . . it looks like prec
doesn't refer to how many digits there will be after the decimal point,
but how many total digits . . . bit of an odd way to do things, but
that explains your problem. Scratch what i said before about assigning
to decimal.getcontext().prec, by the way, it looks like that works
after all:

>>> decimal.getcontext().prec = 4
>>> 100 * a/b
Decimal("66.67")

HTH,
Orri
-- 
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.


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


Re: [Tutor] Unexpected result from decimal

2005-01-19 Thread Liam Clarke
Jacob- one slight flaw/quirk in Python is if you want floating point
computations you have to specify a floating point.

>>> import decimal
>>> decimal.getcontext().prec = 2
>>> a = decimal.Decimal(2)
>>> b = decimal.Decimal(3)
>>> 100*a/b
Decimal("67")
>>> print 100*a/b


try - 

a=decimal.Decimal(2.0)
b = decimal.Decimal(3)
print 100*a/b

Same as writing 100/3.0 as opposed to 100/3. Try it. 

On Wed, 19 Jan 2005 22:07:35 -0500, Tim Peters <[EMAIL PROTECTED]> wrote:
> [Jacob S.]
> >I'm having a problem that is ticking me off. (to put it lightly)
> > Why does decimal do this --  I thought that getcontext().prec
> > was number of decimal places?
> 
> It's unclear what you mean by "decimal places".  From context, you
> _appear_ to mean "number of decimal digits after the radix point".  In
> that case, no, that's not what precision means.  decimal is a
> floating-point type, not a fixed-point type, and precision is the
> total number of significant digits; the location of the radix point is
> irrelevant.  The rules are spelled out in great detail here:
> 
> http://www2.hursley.ibm.com/decimal/
> 
> > >>> import decimal
> > >>> decimal.getcontext().prec = 2
> > >>> a = decimal.Decimal(2)
> > >>> b = decimal.Decimal(3)
> > >>> 100*a/b
> > Decimal("67")
> > >>> print 100*a/b
> > 67
> > >>>
> >
> > Why does it do this?
> 
> It's doing what you told it to do.  It would have helped if you had
> been specific about what you wanted it to do.  For example, did you
> want 66.67, or what?
> 
> > It's really, really, messing things up for me because results are
> > not interpreted in my programs correctly.
> 
> Change your code .  Perhaps this is what you wanted?
> 
> >>> import decimal
> >>> pennies = decimal.Decimal("0.01")
> >>> a = decimal.Decimal(2)
> >>> b = decimal.Decimal(3)
> >>> print 100*a/b
> 66.67
> >>> print (100*a/b).quantize(pennies)
> 66.67
> >>>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fw: Please submit to tutor list: dictionary update prob

2005-01-19 Thread Liam Clarke
 update_dict(data_holder, filename)
>else:   # file no exist
>write_book(data_holder, filename)
>break
>else:
>d.clear()   #clear dict
>break


There's two elses, but no inital if:
There's a place to start, right there.


On Wed, 19 Jan 2005 21:27:24 -0500, Jacob S. <[EMAIL PROTECTED]> wrote:
> Hi everyone, sent this on to the list as told to.
> cc to eri to verify my sending to list...
> ;-) Jacob
> 
> > dear jacob,
> >
> > sorry to send this to you but if you may, kindly send to tutor list as im
> > no longer subscribed.  my problem is in the update dict portion: it just
> > doesnt update regardless how many contacts i add. kindly advise where
> > my mistake is or code gone wrong. the rest of the options i will do on my
> > own so just hold off the helps for now. appreciate all your good help.
> >
> > please cc to this account.
> >
> > --
> > regards,
> > erimendz
> >
> >
> > #!/usr/bin/env python
> >
> > import cPickle, os, sys, re, stat
> > #import string
> >
> > ## Global variables
> >
> >
> > home = '~'
> > filename = os.path.join(os.path.expanduser(home), 'abook.dat')
> > data_holder = {}
> >
> >
> >
> > ## Functions
> > ##
> >
> >
> > def about_program():
> >print
> >print '\t'*2, 'A simple address book written in Python'
> >raw_input('\nPress  to continue...')
> >
> >
> > ## add_contact ##
> > def add_contact(d):
> >while True:
> >name = add_name()
> >email = add_email()
> >d[name] = email
> >print 'Add another contact? '
> >ans = ask_yes_no()
> >if ans == 0:# no
> >print 'Save to address book? '
> >get_ans = ask_yes_no()
> >if get_ans == 1:# yes
> >#collected = d
> >check = if_file_exist(filename)
> >if check is True:
> >  update_dict(data_holder, filename)
> >else:   # file no exist
> >write_book(data_holder, filename)
> >break
> >else:
> >d.clear()   #clear dict
> >break
> >
> > def add_name():
> >msg = 'Enter contact name: '
> >while True:
> >try:
> >name = raw_input(msg)
> >if len(name) != 0:
> >if len(name) <= 20:
> >return name
> >else:
> >print 'Name too long: please limit to 20 characters'
> >else:
> >print 'Try again: blank not allowed!'
> >except EOFError:# catch ^C-D keypress
> >print
> >
> > def add_email():
> >msg = 'Enter email address: '
> >while True:
> >try:
> >email = raw_input(msg)
> >if len(email) == 0:
> >print 'Blank not allowed!'
> >else:
> >valid_format =
> > r'[EMAIL PROTECTED](\.[-a-z0-9]+)*\.(com$|\
> >edu$|net$|gov$|mil$|org$|int$|aero$|biz$|coop$|
> > museum$|pro$|info$)'
> >valid_email = re.compile(valid_format)
> >if valid_email.match(email):
> >return email
> >else:
> >print '%s is not a valid address: try again!' % email
> >except EOFError:
> >print
> >
> >
> > def ask_yes_no():
> >try:
> > ask = raw_input('Yes or No? [y|N] ')
> > if ask.lower() in ['y', 'ye', 'yes', 'yep', 'ok']:
> > return 1# yes
> > else:
> > return 0# no
> >except EOFError:
> > print
> >
> >
> > def if_file_exist(f):
> >''' test if file exists; returns boolean '''
> >
> >return os.path.exists(os.path.join(os.path.expanduser('~'), f))
> >
> > def get_filesize(f):
> >''' check file size '''
> >
> >return os.stat(os.path.join(os.path.expanduser('~'), f))[stat.ST_SIZE]
> >
> >
> > def write_book(d, f):
> >write = open(f, 'wb')
> >cPickle.dump(d, write)
> >write.close()
> >
> > def update_dict(d, f):
> >''' update the saved dictionary file '''
> >
> >read = open(f, 'rb')
> >newdic = cPickle.load(read)
> >newdic.update(d)
> >read.close()
> >
> >
> >
> >
> > ## view_abook() ##
> > def view_abook(d, f):
> >check = if_file_exist(f)
> >if check is True:
> ># load file and pretty print
> >read = open(f, 'rb')
> >d = cPickle.load(read)
> >for k, v in d.iteritems():
> >print '%s\t%s' % (k, v)
> >read.close()
> >else:
> >print 'no contacts listed!'
> >
> >
> >
> > ## function tester, sort of ##
> > def ifaccessible(f):
> >''' test if file is accessible by user; returns boolean '''
> >
> >return os.access(os.path.join(os.path.expanduser('~'), f), os.F_OK)
> >
> >
> >
> > def main():
> >while True:
> >select = main_menu()
> >   

Re: [Tutor] Unexpected result from decimal

2005-01-19 Thread Tim Peters
[Jacob S.]
>I'm having a problem that is ticking me off. (to put it lightly)
> Why does decimal do this --  I thought that getcontext().prec
> was number of decimal places?

It's unclear what you mean by "decimal places".  From context, you
_appear_ to mean "number of decimal digits after the radix point".  In
that case, no, that's not what precision means.  decimal is a
floating-point type, not a fixed-point type, and precision is the
total number of significant digits; the location of the radix point is
irrelevant.  The rules are spelled out in great detail here:

http://www2.hursley.ibm.com/decimal/

> >>> import decimal
> >>> decimal.getcontext().prec = 2
> >>> a = decimal.Decimal(2)
> >>> b = decimal.Decimal(3)
> >>> 100*a/b
> Decimal("67")
> >>> print 100*a/b
> 67
> >>>
>
> Why does it do this?

It's doing what you told it to do.  It would have helped if you had
been specific about what you wanted it to do.  For example, did you
want 66.67, or what?

> It's really, really, messing things up for me because results are
> not interpreted in my programs correctly.

Change your code .  Perhaps this is what you wanted?

>>> import decimal
>>> pennies = decimal.Decimal("0.01")
>>> a = decimal.Decimal(2)
>>> b = decimal.Decimal(3)
>>> print 100*a/b
66.67
>>> print (100*a/b).quantize(pennies)
66.67
>>>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Unexpected result from decimal

2005-01-19 Thread Jacob S.
Hi all,
   I'm having a problem that is ticking me off. (to put it lightly)
Why does decimal do this --  I thought that getcontext().prec was number of 
decimal places?

import decimal
decimal.getcontext().prec = 2
a = decimal.Decimal(2)
b = decimal.Decimal(3)
100*a/b
Decimal("67")
print 100*a/b
67

Why does it do this?
It's really, really, messing things up for me because results are not 
interpreted in my programs correctly.
Jacob 

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


[Tutor] Fw: Please submit to tutor list: dictionary update prob

2005-01-19 Thread Jacob S.
Hi everyone, sent this on to the list as told to.
cc to eri to verify my sending to list...
;-) Jacob
dear jacob,
sorry to send this to you but if you may, kindly send to tutor list as im
no longer subscribed.  my problem is in the update dict portion: it just
doesnt update regardless how many contacts i add. kindly advise where
my mistake is or code gone wrong. the rest of the options i will do on my
own so just hold off the helps for now. appreciate all your good help.
please cc to this account.
--
regards,
erimendz
#!/usr/bin/env python
import cPickle, os, sys, re, stat
#import string
## Global variables
home = '~'
filename = os.path.join(os.path.expanduser(home), 'abook.dat')
data_holder = {}

## Functions
##
def about_program():
   print
   print '\t'*2, 'A simple address book written in Python'
   raw_input('\nPress  to continue...')
## add_contact ##
def add_contact(d):
   while True:
   name = add_name()
   email = add_email()
   d[name] = email
   print 'Add another contact? '
   ans = ask_yes_no()
   if ans == 0:# no
   print 'Save to address book? '
   get_ans = ask_yes_no()
   if get_ans == 1:# yes
   #collected = d
   check = if_file_exist(filename)
   if check is True:
 update_dict(data_holder, filename)
   else:   # file no exist
   write_book(data_holder, filename)
   break
   else:
   d.clear()   #clear dict
   break
def add_name():
   msg = 'Enter contact name: '
   while True:
   try:
   name = raw_input(msg)
   if len(name) != 0:
   if len(name) <= 20:
   return name
   else:
   print 'Name too long: please limit to 20 characters'
   else:
   print 'Try again: blank not allowed!'
   except EOFError:# catch ^C-D keypress
   print
def add_email():
   msg = 'Enter email address: '
   while True:
   try:
   email = raw_input(msg)
   if len(email) == 0:
   print 'Blank not allowed!'
   else:
   valid_format = 
r'[EMAIL PROTECTED](\.[-a-z0-9]+)*\.(com$|\
   edu$|net$|gov$|mil$|org$|int$|aero$|biz$|coop$|
museum$|pro$|info$)'
   valid_email = re.compile(valid_format)
   if valid_email.match(email):
   return email
   else:
   print '%s is not a valid address: try again!' % email
   except EOFError:
   print

def ask_yes_no():
   try:
ask = raw_input('Yes or No? [y|N] ')
if ask.lower() in ['y', 'ye', 'yes', 'yep', 'ok']:
return 1# yes
else:
return 0# no
   except EOFError:
print
def if_file_exist(f):
   ''' test if file exists; returns boolean '''
   return os.path.exists(os.path.join(os.path.expanduser('~'), f))
def get_filesize(f):
   ''' check file size '''
   return os.stat(os.path.join(os.path.expanduser('~'), f))[stat.ST_SIZE]
def write_book(d, f):
   write = open(f, 'wb')
   cPickle.dump(d, write)
   write.close()
def update_dict(d, f):
   ''' update the saved dictionary file '''
   read = open(f, 'rb')
   newdic = cPickle.load(read)
   newdic.update(d)
   read.close()

## view_abook() ##
def view_abook(d, f):
   check = if_file_exist(f)
   if check is True:
   # load file and pretty print
   read = open(f, 'rb')
   d = cPickle.load(read)
   for k, v in d.iteritems():
   print '%s\t%s' % (k, v)
   read.close()
   else:
   print 'no contacts listed!'

## function tester, sort of ##
def ifaccessible(f):
   ''' test if file is accessible by user; returns boolean '''
   return os.access(os.path.join(os.path.expanduser('~'), f), os.F_OK)

def main():
   while True:
   select = main_menu()
   while True:
   if select in [ '1', 'p']:
   about_program()
   break
   elif select in [ '2', 'a']:
   add_contact(data_holder)
   break
   elif select in [ '3', 's']:
   print "save_changes()"
   break
   elif select in [ '4', 'v']:
   view_abook(data_holder, filename)
   break
   elif select in [ '5', 'f']:
   print "find_contact()"
   break
   elif select in [ '6', 'e']:
   print "edit_contact()"
   break
   elif select in [ '7', 'r']:
   print "remove_contact()"
   break
   elif select in [ '0', 't', 'T']:
   #print if_file_exist(filename)
   #print get_filesize(filename)
   try:
   print get_filesize(filename)
   except OSError:
   print '%s not found!' % filename
   break
   elif select in [ '8', 'q']:
  

Re: [Tutor] A somewhat easier way to parse XML

2005-01-19 Thread Kent Johnson
Kent Johnson wrote:
On Jan 19, 2005, at 03:58, David Rock wrote:
Indeed. The problem is, even if I know what I'm looking for, the  
problem remains that given the following document,


baz

If I want to get "baz", the command is ...

I'll try to find the time to write up a full example using ElementTree, 
Amara and dom4j. Meanwhile see http://www.oreillynet.com/pub/wlg/6225 
and http://www.oreillynet.com/pub/wlg/6239

OK, here is code to print 'baz' from a simple XML string using three different XML toolkits. (I 
added another level to the XML to make it a little more challenging.)

This is pretty much a tie - it's three lines of code in each toolkit. The main difference is between 
the XPath access used by ElementTree and dom4j and the attribute access used by amara. Personally I 
find dom4j's full XPath support to be very handy - it essentially gives you a query engine built in 
to your data model. But it is a matter of taste, and amara has XPath support also.

I put each example inside 'except ImportError' so I could have them all in one file - it's not 
something you would normally do. The ElementTree and amara examples are for CPython; the dom4j 
example is for Jython.

Of course you need the corresponding toolkit to be correctly installed...
Kent
docText = '''


baz


'''
# ElementTree
try:
from elementtree import ElementTree
doc = ElementTree.XML(docText)
# Note: doc represents the top-level ('doc') element
print 'ElementTree'
print doc.findtext('foo/bar')
except ImportError:
print 'No ElementTree'
print
# amara
try:
from amara import binderytools
root = binderytools.bind_string(docText)
# root is the 'root' element - the parent of the 'doc' element
print 'amara'
print root.doc.foo.bar
except ImportError:
print 'No amara'
print
# dom4j
try:
import org.dom4j as dom
root = dom.DocumentHelper.parseText(docText)
print 'dom4j'
print root.valueOf('doc/foo/bar')
except ImportError:
print 'No dom4j'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sockets, files, threads

2005-01-19 Thread Marilyn Davis
On Wed, 19 Jan 2005, Danny Yoo wrote:

> On Wed, 19 Jan 2005, Marilyn Davis wrote:
> 
> > class Exim:
> >  def __init__(self):
> >  self.fdin = None
> >  self.err_flush = []
> >  self.stdout, self.stdin, self.stderr  = popen2.popen3('%s -t' % 
> > MAILER)
> >  self.fdin = self.stdin.fileno()
> >  self.fdout = self.stdout.fileno()
> >  self.fderr = self.stderr.fileno()
> 
> 
> Hi Marilyn,
> 
> You probably don't need to explicitly use the file descriptors here. I see
> that you're using them because of the use of select() later on:

Yippee!  This stopped the error.  Whew!  Odd that it didn't happen on
2.3.

Whatever.

One wonders, how many times do you guys have to tell me to stop mixing
file descriptors and file objects?  I guess this was old code, before
you told me that <-- weak excuse.

Having the __del__ doesn't seem to hurt anything, which is a relief.
There are times when I want to call close_up() and not __del__ in my
real code.

> 
> 
> > Are you well yet?  A lot of sick people around these days!
> 
> 
> Feeling better.
> 
> 

Good.  Take care.

And, thank you some more.

Marilyn



-- 

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


Re: [Tutor] need advice on streamlining code...

2005-01-19 Thread Eric L. Howard
At a certain time, now past [Jan.17.2005-01:48:34PM -0500], 
elh@outreachnetworks.com spake thusly:
> The following block of code works, and provides the necessary output I'm
> looking for...but I have a feeling that it's working through sheer brute
> force and could be better:
> 
> insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf")
> insideipgrep = insideipgrepfd.readlines()

Thanks to all who replied.  I got some good ideas from the exchange.

~elh

-- 
Eric L. Howard   e l h @ o u t r e a c h n e t w o r k s . c o m

www.OutreachNetworks.com313.297.9900

JabberID: [EMAIL PROTECTED] Advocate of the Theocratic Rule
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sockets, files, threads

2005-01-19 Thread Danny Yoo


On Wed, 19 Jan 2005, Marilyn Davis wrote:

> class Exim:
>  def __init__(self):
>  self.fdin = None
>  self.err_flush = []
>  self.stdout, self.stdin, self.stderr  = popen2.popen3('%s -t' % 
> MAILER)
>  self.fdin = self.stdin.fileno()
>  self.fdout = self.stdout.fileno()
>  self.fderr = self.stderr.fileno()


Hi Marilyn,

You probably don't need to explicitly use the file descriptors here. I see
that you're using them because of the use of select() later on:

###
sel = select.select(self.outlist, [], [], 1)
###


but, although select.select() can use file descriptor numbers, it can also
take file-like objects.  See:

http://www.python.org/doc/lib/module-select.html


So you can simplify this code to:

###
sel = select.select([self.stdout, self.stderr], [], [], 1)
###

The upside to this is that you don't need to use the low-level os.read()
or os.close() functions at all.


I suspect that some silliness with file descriptors is causing your bug,
as the following:

###
>>> os.close(42)
Traceback (most recent call last):
  File "", line 1, in ?
OSError: [Errno 9] Bad file descriptor
###

shows that if we feed os.close() a nonsense file descriptor, it'll throw
out a error message that you may be familiar with.


The error can happen if we close the same file descriptor twice:

###
>>> myin, myout, myerr = popen2.popen3('cat')
>>> os.close(myin.fileno())
>>> os.close(myin.fileno())
Traceback (most recent call last):
  File "", line 1, in ?
OSError: [Errno 9] Bad file descriptor
###


Here is another particular case that might really be relevant:

###
>>> myin, myout, myerr = popen2.popen3('cat')
>>> os.close(myin.fileno())
>>> myin.close()
Traceback (most recent call last):
  File "", line 1, in ?
IOError: [Errno 9] Bad file descriptor
###

We're getting an IOError here for exactly the same reasons: the high-level
'myin' object has no knowledge that its underlying file descriptor was
closed.

This is exactly why we've been saying not to mix file-descriptor stuff
with high-level file-like object manipulation: it breaks the assumptions
that the API expects to see.



> However, all that said, I do dimly remember that poplib perhaps had some
> extra processing that maybe is not needed -- but I could be wrong.

Ok; I'll try to remember to look into poplib later and see if there's
anything silly in there.



> if __name__ == '__main__':
> msg = '''To: %s
>
>  xx''' % TO_WHOM
>
>  p = Exim()
>  p.write(msg)
>  del p


Instead of using __del__ implicitely, drop the __del__ method and try
calling Exim.close_up()  explicitely:

###
p = Exim()
p.write(msg)
p.close_up()
###



> Are you well yet?  A lot of sick people around these days!


Feeling better.

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


Re: [Tutor] sockets, files, threads

2005-01-19 Thread Marilyn Davis
Thank you Kent.

On Wed, 19 Jan 2005, Kent Johnson wrote:

> Marilyn Davis wrote:
> >>few lines up, right where 'client_socket' is initialized.  Like this:
> >>
> >>###
> >>try:
> >>client_socket, client_addr = self.server_socket.accept()
> >>Spawn(client_socket).start()
> >>except socket.error, msg:
> >>time.sleep(.5)
> >>except (EOFError, KeyboardInterrupt):
> >>self.close_up()
> >>###
> >>
> >>Not only does this make it more clear where 'client_socket' is being used,
> >>but it ends up making the code shorter.  I've dropped the 'continue'
> >>statement, as it becomes superfluous when the Spawn() moves into the try's
> >>body.
> > 
> > 
> > But but but, that wraps the whole thread in one try/except clause.
> > That Spawn() call starts the thread.  If a client_socket generates an
> > error, we don't want the main thread to sleep.  All the socket
> > operations in Spawn.start() are also wrapped in their own (hopefully)
> > intelligent try/excepts.  I thought it was good practice to -not- put
> > extra stuff in a try/except.
> 
> Use try: except: else:
> The else clause is only executed if no exception happens, so the code is 
> correct; the else is 
> outside the scope of the try, so you don't catch unexpected exceptions.
> 
>  try:
>  client_socket, client_addr = self.server_socket.accept()
>  except socket.error, msg:
>  time.sleep(.5)
>  except (EOFError, KeyboardInterrupt):
>  self.close_up()
>  else:
>  Spawn(client_socket).start()


That makes good sense.  But I'm not threading anymore and my call to
server_socket.accept now blocks so I'm not sleeping.  The only
exceptions I want to catch now are those keyboard things, which make
me stop.  The socket.error I was catching was that there was no
connections waiting right now.  So my situation is much simpler.

Marilyn



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

-- 


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


Re: [Tutor] sockets, files, threads

2005-01-19 Thread Marilyn Davis
> 
> > What about when I do an explicit call to a close inside a __del__.  Is
> > that a bad idea?
> 
> I usually prefer to add a close() method to my objects that releases
> resources, rather than __del__(), because it's more visible.
> 

OK Danny!  I found it!  When I was almost asleep last night, I knew
what to do and this morning I whittled it down to this situation.
Maybe it can be whittled further but I'm holding up our project and I
can't take any more time right now.

Note that the two variables TO_WHOM and MAILER, I think, can be safely
changed to suit your environment, indeed MAILER='mail' should work for
others.

It happens on 2.4 but not on 2.3.  Either 2.3 masks my error, or 2.4
has an error?

I wouldn't be surprised if this is responsible for my thread problem
but we're not going to find out.


#!/usr/bin/env python2.4
'''On my python2.4, this produces:
./test_exim.py
close failed: [Errno 9] Bad file descriptor
close failed: [Errno 9] Bad file descriptor
close failed: [Errno 9] Bad file descriptor

but it runs just fine.

but if I specify python2.3, it runs cleanly.

'''

TO_WHOM = 'marilyn'
MAILER = 'exim'

import popen2
import select
import os

class Exim:
 def __init__(self):
 self.fdin = None
 self.err_flush = []
 self.stdout, self.stdin, self.stderr  = popen2.popen3('%s -t' % MAILER)
 self.fdin = self.stdin.fileno()
 self.fdout = self.stdout.fileno()
 self.fderr = self.stderr.fileno()
 self.outlist = [self.fdout, self.fderr]
 self.inlist = [self.fdin]

 def __del__(self):
 self.close_up()

 def close_up(self):
 if not self.fdin:
 return
 count = 0
 in_status = os.close(self.fdin)
 while 1:
 sel = select.select(self.outlist, [], [], 1)
 if sel[0] == []:
 break
 else:
 for fd in sel[0]:
 r = os.read(fd, 16384)
 if r:
 self.err_flush += [r]
 count = 0
 count += 1
 if count >= 5:
 break
 else:
 continue
 break
 self.err_flush = ''.join(self.err_flush)
 out_status = os.close(self.fdout)
 err_status = os.close(self.fderr)
 self.fdin = None
 if not in_status:
 return
 raise RuntimeError, self.err_flush

 def write(self, stuff):
 while 1:
 sel = select.select(self.outlist, self.inlist, [], 1)
 if sel[1] != []:
 os.write(sel[1][0], stuff)
 return


if __name__ == '__main__':
 msg = '''To: %s

xx''' % TO_WHOM

 p = Exim()
 p.write(msg)
 del p



On Tue, 18 Jan 2005, Danny Yoo wrote:
> 
> [About using the Standard Library]
> 
> > And since then, please don't shoot me, but I don't immediately trust the
> > modules.  I read them and see how many times they loop through the data,
> > and how many copies of the data they put into memory -- and usually
> > decide to write the simple things I need myself, looping zero times and
> > keeping only one block in memory.
> 
> Hmm.. . Do you remember which Standard Library modules you were looking at
> earlier?  Perhaps there was some funky stuff happening, in which case we
> should try to fix it, so that no one else runs into the same problems.


Yes.  poplib and socket.  But, it's not fair to assume that something
funky is happening in there.  A poplib should provide a set of tools
for making a pop client and I'm not making a pop client, just pumping
the message from a pop server through my python/mysql magic and into
exim.  Similarly with socket.  It does buffering and prepares things
that I don't need, but probably lots of people do.  So when I say I
don't "trust" stdlib modules, I mean I don't trust them to be simple
enough for my situation.

However, all that said, I do dimly remember that poplib perhaps had
some extra processing that maybe is not needed -- but I could be
wrong.

> In a similar vein, in Spawn.run(), it might be a good idea to explicitely
> call write_and_close() on our FileSocket instance.  For example, we can
> add a try/finally in the body of the revised start()  method:
> 
> ###
> def start(self):
> '''Given the command, provides the function to call.'''
> global TESTING
> function =  { 'acl_rcpt.py' : calls.acl_rcpt,
>   'route_mail.py' : calls.route_mail,
>   'route_errors.py' : calls.route_errors,
>   'doorman.py' : calls.doorman,
>   'doorman_errors.py' : calls.doorman_errors,
>   'is_known_to.py' : is_known_to
>   }
> if log.level & log.calls:
> log.it('fd%d: %s: %s

RE: [Tutor] Popen and sending output to a file

2005-01-19 Thread Ertl, John
That is too easy.  I was looking at the examples of how to replace the old
popen and I just did not get it but the page you sent is great.  

John 

-Original Message-
From: Danny Yoo [mailto:[EMAIL PROTECTED]
Sent: Wednesday, January 19, 2005 10:34
To: Ertl, John
Cc: tutor@python.org
Subject: Re: [Tutor] Popen and sending output to a file


On Wed, 19 Jan 2005, Ertl, John wrote:

> I am using the subprocess.Popen from 2.4.  I can launch a job from
> python and the output from the job goes to the screen but now I would
> like to have the output go to a file.  I could do the crude
>
> subprocess.Popen("dtg | cat > job.out", shell=True)
>
> But I would think there is a better way built into Popen but I could not
> figure out the documentation.

Hi John,


According to:

http://www.python.org/doc/lib/node227.html

we can redirect standard input, output, and error by calling Popen with
the 'stdin', 'stdout', or 'stderr' keyword arguments.

We should be able to do something like:

###
job_output = open("job.out", "w")
subprocess.Popen("dtg", shell=True, stdout=job_output)
###


Best of wishes to you!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Popen and sending output to a file

2005-01-19 Thread Danny Yoo


On Wed, 19 Jan 2005, Ertl, John wrote:

> I am using the subprocess.Popen from 2.4.  I can launch a job from
> python and the output from the job goes to the screen but now I would
> like to have the output go to a file.  I could do the crude
>
> subprocess.Popen("dtg | cat > job.out", shell=True)
>
> But I would think there is a better way built into Popen but I could not
> figure out the documentation.

Hi John,


According to:

http://www.python.org/doc/lib/node227.html

we can redirect standard input, output, and error by calling Popen with
the 'stdin', 'stdout', or 'stderr' keyword arguments.

We should be able to do something like:

###
job_output = open("job.out", "w")
subprocess.Popen("dtg", shell=True, stdout=job_output)
###


Best of wishes to you!

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


[Tutor] Popen and sending output to a file

2005-01-19 Thread Ertl, John
I am using the subprocess.Popen from 2.4.  I can launch a job from python
and the output from the job goes to the screen but now I would like to have
the output go to a file.  I could do the crude 

subprocess.Popen("dtg | cat > job.out", shell=True)

But I would think there is a better way built into Popen but I could not
figure out the documentation.

Any help would be appreciated.

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


Re: [Tutor] A somewhat easier way to parse XML

2005-01-19 Thread Kent Johnson
David Rock wrote:
* Max Noel <[EMAIL PROTECTED]> [2005-01-19 11:48]:
On Jan 19, 2005, at 03:58, David Rock wrote:

For me, it seems that the way you are supposed to interact with an XML
DOM is to already know what you are looking for, and in theory, you
_should_ know ;-)
	Indeed. The problem is, even if I know what I'm looking for, the  
problem remains that given the following document,


baz

	If I want to get "baz", the command is (assuming a DOM object has 
	been  created):

doc.documentElement.getElementsByTagName("bar")[0].childNodes[0].nodeVal 
ue

	Quoting from memory there, it may not be entirely correct. However,  
the command has more characters than the document itself. Somehow I  
feel it'd be a bit more elegant to use:

doc["bar"]
(or depending on the implementation, doc["foo"]["bar"])
	Don't you think?

Absolutely. That is exactly what I was hoping for, too. ElementTree
comes close, but even that can be a bit unwieldy because of the
multi-dimentional array you end up with. Still, if you know the data,
doc[0][0] is a lot easier than doc.documentElement...nodeValue
Use the XPath support in ElementTree. Something like
doc.find('foo/bar')
If I understand correctly Amara allows something like
doc.foo.bar
I'll try to find the time to write up a full example using ElementTree, Amara and dom4j. Meanwhile 
see http://www.oreillynet.com/pub/wlg/6225 and http://www.oreillynet.com/pub/wlg/6239

Kent


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


Re: [Tutor] A somewhat easier way to parse XML

2005-01-19 Thread David Rock
* Max Noel <[EMAIL PROTECTED]> [2005-01-19 11:48]:
> 
> On Jan 19, 2005, at 03:58, David Rock wrote:
> 
> >For me, it seems that the way you are supposed to interact with an XML
> >DOM is to already know what you are looking for, and in theory, you
> >_should_ know ;-)
> 
>   Indeed. The problem is, even if I know what I'm looking for, the  
> problem remains that given the following document,
> 
> 
>   baz
> 
> 
>   If I want to get "baz", the command is (assuming a DOM object has 
>   been  created):
> 
> doc.documentElement.getElementsByTagName("bar")[0].childNodes[0].nodeVal 
> ue
> 
>   Quoting from memory there, it may not be entirely correct. However,  
> the command has more characters than the document itself. Somehow I  
> feel it'd be a bit more elegant to use:
> 
> doc["bar"]
> 
> (or depending on the implementation, doc["foo"]["bar"])
> 
>   Don't you think?

Absolutely. That is exactly what I was hoping for, too. ElementTree
comes close, but even that can be a bit unwieldy because of the
multi-dimentional array you end up with. Still, if you know the data,

doc[0][0] is a lot easier than doc.documentElement...nodeValue

-- 
David Rock
[EMAIL PROTECTED]


pgp6q0sQrJqbe.pgp
Description: PGP signature
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Selecting text

2005-01-19 Thread Karl Pflästerer
On 19 Jan 2005, [EMAIL PROTECTED] wrote:

> I have two lists:
>
> 1. Lseq:
>
 len(Lseq)
> 30673
 Lseq[20:25]
> ['NM_025164', 'NM_025164', 'NM_012384', 'NM_006380',
> 'NM_007032','NM_014332']
>
>
> 2. refseq:
 len(refseq)
> 1080945
 refseq[0:25]
> ['>gi|10047089|ref|NM_014332.1| Homo sapiens small
> muscle protein, X-linked (SMPX), mRNA',
> 'GTTCTCAATACCGGGAGAGGCACAGAGCTATTTCAGCCACATGGCATCGGAATTGAGATCGCAGCT',
> 'CAGAGGACACCGGGCGTTCCACCTTCCAAGGAGCTTTGTATTCTTGCATCTGGCTGCCTGGGACTT',
[...]
> 'ACTTTGTATGAGTTCAAATAAATATTTGACTAAATGTTGTGA',
> '>gi|10047091|ref|NM_013259.1| Homo sapiens neuronal
> protein (NP25), mRNA',
[...]

> If Lseq[i] is present in refseq[k], then I am
> interested in printing starting from refseq[k] until
> the element that starts with '>' sign. 
>
> my Lseq has NM_014332 element and this is also present
> in second list refseq. I want to print starting from
> element where NM_014332 is present until next element
> that starts with '>' sign.

> I could not think of any smart way to do this,
> although I have tried like this:

I give you the same answer I think you got the last times you asked such
a question: use a dictionary if you want to search items.

So how to do it?
You could build a dictionary from refseq where the elements that can
match the elemenst from Lseq are the keys.

Then you iterate over Lseq, look if you find a key in your dictionary
and if yes print the matching elemnt from the list.

The next function creates a dictionary.  The keys are the
NM_... entries the values are the start and end indice of the
corresponding entries.

def build_dic (seq):
keys = []
indice = []
for ind, entry in enumerate(seq):
if entry.startswith('>'):
key = entry.split('|')[3]
keys.append(key)
indice.append(ind)
indice.append(-1)
return dict(zip(keys, zip(indice, indice[1:])))

With that function you search for matching keys and if a match is found
use the start and end index to extract the right elements from the list.


def find_matching (rseq, lseq):
d = build_dic(rseq)
for key in lseq:
if key in d:
start, end = d[key]
print rseq[start:end]




   Karl
-- 
Please do *not* send copies of replies to me.
I read the list
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Threaded Script Runs Find in IDLE, Never Terminates from Windows Command Prompt

2005-01-19 Thread Gooch, John
I have a threaded python ( Python 2.3.4 ) script that runs perfectly on
Windows 2000 Server SP4 when it is executed from IDLE ( i.e. press 'F5' from
the editor ), the threads do their work, halt, and the 'join' command picks
them up. When I run the same script from windows command line ( cmd.exe ),
it appears to work normally until the last thread is 'joined'. What happens
at that point is it does not execute the any lines after the join command,
and will stay in the position indefinitely. 

Do I need to alter the script to run from the command line, or perhaps add
some parameters to have it terminate successfully? Once this issue is
resolved, the script will run daily via a scheduling program we use in our
company, and the exit code of the script tells the scheduler whether the
script completed its task successfully or not. 

Here is the 'code block' of my script that clean up  the threads via .join:

for workerThread in ThreadGroup:
#timeout will difference between ( start time + 4 hours ) and
current time
#unless current time is greater than that, in which case the
#thread gets one second to join
#For example, if the current time if 30 minutes less than the
endTime
#time, then the thread has a timeout to 'join' of 30 minutes 
#timeout = endTime - time.time()
#if ( timeout < 0 ):
#timeout = 1
if debug:
print "Joining "+workerThread.getName()+".\n"
workerThread.join()
if debug:
print "Thread "+workerThread.getName()+" successfully joined."
 

I run it with 3 threads, from IDLE it will close all 3 and exit normally,
from the command prompt it prints the "successfully joined" for the first
two, but hangs after printing "Joining Thread 3". The command line syntax is
"c:"\python23\python.exe test.py", I have also tried
"c:\python23\pythonw.exe test.py", but that just adds an entry in Windows
task manager and sits there forever also. 

Thank You, 

 

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


[Tutor] Python and IRC

2005-01-19 Thread Mark Kels
Hi all,

I want to make an IRC bot with python...
The problem is that I cant find any good information about this topic
(not even documentation for the irclib module).
Does anyone here have some experience with python programming for IRC
(clients, bots, etc) and can give me some simple source code or a good
tutorial (name of book will be fine too) ?

Thanks.
-- 
1. The day Microsoft makes something that doesn't suck is probably the
day they start making vacuum cleaners.
2. Unix is user friendly - it's just picky about it's friends.
3. Documentation is like sex: when it is good, it is very, very good.
And when it is bad, it is better than nothing. - Dick Brandon
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Selecting text

2005-01-19 Thread Kent Johnson
Kumar,
You should look for a way to solve this with dictionaries or sets. If you look for each element of 
Lseq in each element of refseq, that is 33,155,825,985 lookups. That is a lot!

Sets have a fast test for membership, look for ways to use them!
In this case, the target string in refseq seems to be pretty easy to pick out with a regular 
expression. So you can do this:
- put Lseq into a set
- iterate through refseq looking for lines starting with >
- use a regular expression search to pick out the NM_ code
- look up the code in the Lseq set
- if it is found, print out the sequence

Here is an implementation that uses this idea. To print the entire group, I keep a flag that turns 
printing on and off.

Kumar, please take the time to understand the basics about sets, dictionaries and iteration. You ask 
similar questions over and over and you don't seem to learn from the answers. You repeatedly make 
the same mistakes with indexing. If you don't understand the solutions, ask more questions. But 
please learn enough so you don't keep asking the same questions and making the same mistakes.

Kent
import re
# Regular expression to pick out the NM_xx codes from refseq
refseq_re = re.compile(r'(NM_\d+)\.1')
Lseq = ['NM_025164', 'NM_025164', 'NM_012384', 'NM_006380',
'NM_007032','NM_014332']
refseq = ['>gi|10047089|ref|NM_014332.1| Homo sapiens small muscle protein, 
X-linked (SMPX), mRNA',
'GTTCTCAATACCGGGAGAGGCACAGAGCTATTTCAGCCACATGGCATCGGAATTGAGATCGCAGCT',
'CAGAGGACACCGGGCGTTCCACCTTCCAAGGAGCTTTGTATTCTTGCATCTGGCTGCCTGGGACTT',
'CCCTTAGGCAGTAAACAAATACATAAAGCAGGGATAAGACTGCATGAATATGTCGAAACAGCCAGTTTCC',
'AATGTTAGAGCCATCCAGGCAAATATCAATATTCCAATGGGAGCCTTTCGGCCAGGAGCAGGTCAA',
'CCAGAAGGAATGTACTCCTGAAGTGGAGGAGGGTGTTCCTCCCACCTCGGATGAGGAGAAGAAGCC',
'AATTCCAGGAGCGAAGAAACTTCCAGGACCTGCAGTCAATCTATCGGAAATCCAGAATATTGTGAA',
'CTTATGTAAAGCTGAACAGTAGTAGGAAGAAGGATTGATGTGAAGAAATAAAGAGGCA',
'GAAGATGGATTCAATAGCTCACTATATATTTGTATGATGATTGTGAACCTCCTGAATGCCTG',
'AGACTCTAGCAGAAATGGCCTGTTTGTACATTTATATCTCTTCCTTCTAGTTGGCTGTATTTCTTACTTT',
'ATCTTCATGGCACCTCACAGAACAAATTAGCCCATAAATTCAACACCTGGAGGGTGTGGGAG',
'GAGGGATATGAATGGAGAATGATATGGCAATGTGCCTAACGAGATGGTTTCCCAAGCT',
'ACTTCCTACAGTAGGTCAATATTTGGAATGCGAGTTCTTCACCAAATTATGTCACTAA',
'ACTTTGTATGAGTTCAAATAAATATTTGACTAAATGTTGTGA',
'>gi|10047091|ref|NM_013259.1| Homo sapiens neuronal protein (NP25), mRNA',
'TGTGCTGCTATTGTGTGGATGCCGCGCGTGTCTTCTCTTCTTTCCAGAGATGGCTAACACCCGAGC',
'TATGGCTTAAGCCGAGAGGTGCAGGAGAAGATCGAGCAGAAGTATGATGCGGACCTGGAGAACAAGCTGG',
'TGGACTGGATCATCCTGCAGTGCGCCGAGGACATAGAGCACCCGCCGGCAGGGCCCACAGAA',
'ATGGTTAATGGACGGGACGGTCCTGTGCAAGCTGATAAATAGTTTATACCCACCAGGACAAGAGCCCATA',
'CCCAAGATCTCAGAGTCAAAGATGGCAAGCAGATGGAGCAAATCTCCCAGTTCCTGCTGCGG',
'AGACCTATGGTGTCAGAACCACCGACATCTTTCAGACGGTGGATCTATGGGAAGGGAAGGACATGGCAGC',
'TGTGCAGAGGACCCTGATGGCTTTAGGCAGCGTTGCAGTCACCAAGGATGATGGCTGCTATCAGAG',
'CCATCCTGGTTTCACAGGAAAGCCCAGCAGAATCGGAGAGGCCCGAGGAGCAGCTTCGCCAGGGAC',
'AGAACGTAATAGGCCTGCAGATGGGCAGCAACAAGGGAGCCTCCCAGGCGGGCATGACAGGGTACGGGAT',
'GCCCAGGCAGATCATGTTAGGACGCGGCATCCTGTGGTAGAGAGGACGAATGTTCCACACCATGGT']
# Turn Lseq into a set so we can search it efficiently
Lseq_set = set(Lseq)
printing = False# Flag whether to print lines or not (are we in a target 
sequence?)
for l in refseq:
if l.startswith('>'):
printing = False# Reset at start of a group
# Look for an interesting group
# First extract the code
m = refseq_re.search(l)
if m:
nm_code = m.group(1) # This is the NM code
if nm_code in Lseq_set:
# We found a good one; turn on printing
printing = True
else:
# This is an error - a line starting with > didn't match the re
print " Error - couldn't match line:"
print "", l
if printing:
print l
kumar s wrote:
Dear group:
I have two lists:
1. Lseq:

len(Lseq)
30673
Lseq[20:25]
['NM_025164', 'NM_025164', 'NM_012384', 'NM_006380',
'NM_007032','NM_014332']
2. refseq:
len(refseq)
1080945
refseq[0:25]
['>gi|10047089|ref|NM_014332.1| Homo sapiens small
muscle protein, X-linked (SMPX), mRNA',
'GTTCTCAATACCGGGAGAGGCACAGAGCTATTTCAGCCACATGGCATCGGAATTGAGATCGCAGCT',
'CAGAGGACACCGGGCGTTCCACCTTCCAAGGAGCTTTGTATTCTTGCATCTGGCTGCCTGGGACTT',
'CCCTTAGGCAGTAAACAAATACATAAAGCAGGGATAAGACTGCATGAATATGTCGAAACAGCCAGTTTCC',
'AATGTTAGAGCCATCCAGGCAAATATCAATATTCCAATGGGAGCCTTTCGGCCAGGAGCAGGTCAA',
'CCAGAAGGAATGTACTCCTGAAGTGGAGGAGGGTGTTCCTCCCACCTCGGATGAGGAGAAGAAGCC',
'AATTCCAGGAGCGAAGAAACTTCCAGGACCTGCAGTCAATCTATCGGAAATCCAGAATATTGTGAA',
'CTTATGTAAAGCTGAACAGTAGTAGGAAGAAGGATTGATGTGAAGAAATAAAGAGGCA',
'GAAGATGGATTCAATAGCTCACTATATATTTGTATGATGATTGTGAACCTCCTGAATGCCTG',
'AGACTCTAGCAGAAATGGCCTGTTTGTACATTTATATCTCTTCCTTCTAGTTGGCTGTATTTCTTACTTT',
'ATCTTCATGGCACCTCACAGAACAAATTAGCCCATAAATTCAACACCTGGAGGGTGTGGGAG',
'GAGGGATATGAATGGAGAATGA

Re: [Tutor] A somewhat easier way to parse XML

2005-01-19 Thread Max Noel
On Jan 19, 2005, at 03:58, David Rock wrote:
For me, it seems that the way you are supposed to interact with an XML
DOM is to already know what you are looking for, and in theory, you
_should_ know ;-)
	Indeed. The problem is, even if I know what I'm looking for, the  
problem remains that given the following document,


baz

	If I want to get "baz", the command is (assuming a DOM object has been  
created):

doc.documentElement.getElementsByTagName("bar")[0].childNodes[0].nodeVal 
ue

	Quoting from memory there, it may not be entirely correct. However,  
the command has more characters than the document itself. Somehow I  
feel it'd be a bit more elegant to use:

doc["bar"]
(or depending on the implementation, doc["foo"]["bar"])
Don't you think?
Still, I can't help wishing I had a simple way to create a dict from a
DOM. From a Python perspective, that seems more "Pythonic" to me as
well. I guess it's just a different way of looking at it.
	I can't help but think that from the perspective of any other  
language, that would feel more [language]-ic as well ;)

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting  
and sweating as you run through my corridors... How can you challenge a  
perfect, immortal machine?"

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


Re: [Tutor] sockets, files, threads

2005-01-19 Thread Kent Johnson
Marilyn Davis wrote:
few lines up, right where 'client_socket' is initialized.  Like this:
###
   try:
   client_socket, client_addr = self.server_socket.accept()
   Spawn(client_socket).start()
   except socket.error, msg:
   time.sleep(.5)
   except (EOFError, KeyboardInterrupt):
   self.close_up()
###
Not only does this make it more clear where 'client_socket' is being used,
but it ends up making the code shorter.  I've dropped the 'continue'
statement, as it becomes superfluous when the Spawn() moves into the try's
body.

But but but, that wraps the whole thread in one try/except clause.
That Spawn() call starts the thread.  If a client_socket generates an
error, we don't want the main thread to sleep.  All the socket
operations in Spawn.start() are also wrapped in their own (hopefully)
intelligent try/excepts.  I thought it was good practice to -not- put
extra stuff in a try/except.
Use try: except: else:
The else clause is only executed if no exception happens, so the code is correct; the else is 
outside the scope of the try, so you don't catch unexpected exceptions.

try:
client_socket, client_addr = self.server_socket.accept()
except socket.error, msg:
time.sleep(.5)
except (EOFError, KeyboardInterrupt):
self.close_up()
else:
Spawn(client_socket).start()
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Selecting text

2005-01-19 Thread Ewald Ertl
Hi kumar

If I unterstood your question correctly, you have to iterate over the refseq 
until 
you get the next entry, which starts with a '>'

on Tue, 18 Jan 2005 20:12:32 -0800 (PST)  kumar s <[EMAIL PROTECTED]> wrote :
-


kumar s > I could not think of any smart way to do this,
kumar s > although I have tried like this:
kumar s > 
kumar s > >>> for ele1 in Lseq:
kumar s >   for ele2 in refseq:
kumar s >   if ele1 in ele2:
kumar s >   k = ele2
kumar s >   s = refseq[ele2].startswith('>')

Here you index the list with an element, but the index must be an integer

kumar s >   print k,s
kumar s > 
kumar s >   
kumar s > 
kumar s > Traceback (most recent call last):
kumar s >   File "", line 5, in -toplevel-
kumar s > s = refseq[ele2].startswith('>')
kumar s > TypeError: list indices must be integers
kumar s > 
kumar s > 
kumar s > I do not know how to dictate to python to select lines
kumar s > between two > symbols. 
kumar s > 


--- end --
Perhaps this is a possible solution: 

Iteration of Lseq an then looping over all elements in refseq. 
if an element of refseq contains the ele1 the list-entry is printed. 
The nested while-loop print's the refseq -elements as long as the element
does not start with a '>'

for ele1 in Lseq:
i=0
while i < len(refseq):
if ele1 in refseq[i]:
print refseq[i]
i +=1
while not refseq[i].startswith('>'):
print refseq[i]
i+=1 # increment until the next entry starting 
with '>' is found
i +=1 # if search should continue if another element is present

HTH Ewald



-- 
Ing. Ewald Ertl HartterGruppe   Phone : 
+43-3352-33085-558
trinomic Projektmanagement & Informationstechnik GmbH   Fax   : 
+43-3352-33085-600
Wiener Straße 41mailto:[EMAIL PROTECTED]
A-7400 Oberwart http://www.trinomic.com mailto:[EMAIL PROTECTED]

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


Re: [Tutor] A somewhat easier way to parse XML

2005-01-19 Thread Danny Yoo


On Wed, 19 Jan 2005, Max Noel wrote:

>   I've just spent the last few hours learning how to use the DOM XML
> API (to be more precise, the one that's in PyXML), instead of revising
> for my exams :p. My conclusion so far: it sucks (and so does SAX because
> I can't see a way to use it for OOP or "recursive" XML trees).

Hi Max,

You are not alone in this restless feeling.

In fact, Uche Ogbuji, one of the lead developers of 4Suite and Amara
(which Kent mentioned earlier), just wrote a blog entry about his
malcontent with the DOM.  Here, these may interest you:

http://www.oreillynet.com/pub/wlg/6224
http://www.oreillynet.com/pub/wlg/6225


> In fact, I find it appalling that none of the "standard" XML parsers
> (DOM, SAX) provides an easy way to do that (yeah, I know that's what
> more or less what the shelve module does, but I want a
> language-independent way).

For simple applications, the 'xmlrpclib' has two functions (dumps() and
loads()) that we can use:

http://www.python.org/doc/lib/node541.html


For example:

###
>>> s = xmlrpclib.dumps(({'hello': 'world'},))
>>> print s




hello
world




>>>
>>>
>>> xmlrpclib.loads(s)
(({'hello': 'world'},), None)
###

A little bit silly, but it does work.  The nice thing about this that
xmlrpc is pretty much a platform-independent standard, so if we're dealing
with simple values like strings, integers, lists, and dictionaries, we're
all set.  It is a bit verbose, though.

Amara looks really interesting, especially because they have tools for
doing data-binding in a Python-friendly way.


Best of wishes to you!

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