[ale] Py[h]hon syntax (sic)

Michael Hirsch mhirsch at nubridges.com
Fri Jan 28 10:19:00 EST 2005



> -----Original Message-----
> From: ale-bounces at ale.org [mailto:ale-bounces at ale.org] On Behalf Of
> Christopher Fowler
> Sent: Thursday, January 27, 2005 6:07 PM
> To: Atlanta Linux Enthusiasts
> Subject: Re: [ale] Py[h]hon syntax (sic)
> 
> Here is my WIP code.   I'm converting an agent I wrote in perl that
> checks the database for our embedded devices in the field.  It then
does
> a ping to see if they are available.  The perl agent does more in the
> fact that it will email the admin and also store states in the db. So
it
> will check every 5 minutes on a host and then email the admin every 60
> minutes until the host is back up.  The thing I hate about the perl
> agent is that it round robins the list.  That is no good so I will
> either convert it to do a fork() on each object or I'll use threads.
Is
> there threads in python?  I think in Perl threads are not really
threads
> as they would be in C.

Python has threads.

> chop up this code and tell me how it can be better:

No real comments, just a relevant question to the pythonistas out there.
I notice in this code that there are accessor methods for all the
variables.  This is technically unnecessary since python doesn't have
private fields like java or C++.  However, perhaps it is good style--in
Java it would be.  So, my question is whether this is the preferred way
to expose fields, or is it considered okay to just access the fields
directly?

Michael
> 
> --- Cut Here --- Cut Here
----------------------------------------------
> 
> #!/usr/bin/env python
> 
> import sys;
> import MySQLdb;
> import os;
> import re;
> 
> class Ens:
>   def set_name(self, name):
>     self.name = name;
>   def set_id(self, id):
>     self.id = id;
>   def set_ip(self, ip):
>     self.ip = ip;
>   def get_name(self):
>     return self.name;
>   def get_id(self):
>     return self.id;
>   def get_ip(self):
>     return self.ip;
> 
> 
> def ping(ip, tries = 2):
>   my_re = re.compile('(\w|\W)+ (\d) received, (\w|\W)+')
>   for attempt in range(tries):
>     ping_in, ping_out = os.popen2("ping -c 1 %s" % ip)
>     for line in ping_out:
>       rec_match = my_re.match(line)
>       if not rec_match: continue
>       received = int(rec_match.groups()[1])
>       if not received: continue
>       return attempt + 1
> 
>   return False
> 
> 
> class Application:
>   def main(self):
>     db = MySQLdb.connect(host="127.0.0.1", user="cms", passwd="cms",
> db="AC_OUTPOST");
>     c = db.cursor();
>     c.execute("select * from ens");
>     ENS = [ ];
> 
>     while True:
>       row = c.fetchone();
>       if not row: break;
>       e = Ens();
>       e.set_name(row[2]);
>       e.set_id(row[0]);
>       e.set_ip(row[5]);
>       ENS.append(e);
> 
>     for tmp in ENS:
>      result = ping(tmp.get_ip());
>      if not result:
>        print "ID:",tmp.get_id()," Name:",tmp.get_name()," DOWN!";
>     else:
>        print "ID:",tmp.get_id()," Name:",tmp.get_name()," UP!";
> 
> 
> program = Application();
> program.main();
> 
> # vi: set ts=2 sw=2: #
> 
> --- Cut Here --- Cut Here
----------------------------------------------
> 
> How do I set the object constructor so that I can simply create an Ens
> object with all the information needed?  Is it possible to simply pass
> the whole row into the constructor?
> 
> I read in the O'reilly book "Learning Python" that the code can be
> compiled into a .pyc file.  How do I compile into .pyc so that I can
> distribute the code without the source being seen?  This is one of my
> biggest problems with Perl.
> 
> Thanks,
> Chris
> 
> 
> On Thu, 2005-01-27 at 14:49, John P. Healey wrote:
> > the code for pinging would look something like this:
> >
> > import os
> > import re
> >
> > def ping(ip, tries = 2):
> >     my_re = re.compile('(\w|\W)+ (\d) received, (\w|\W)+')
> >     for attempt in range(tries):
> >         ping_in, ping_out = os.popen2("ping -c 1 %s" % ip)
> >         for line in ping_out:
> >             rec_match = my_re.match(line)
> >             if not rec_match: continue
> >             received = int(rec_match.groups()[1])
> >             if not received: continue
> >             return attempt + 1
> >     return False
> >
> > it takes an optional second argument (# of tries) and returns the
number
> of
> > tries needed when successful, False otherwise.  the SMTP stuff can
be
> handled
> > with smtplib.  it's part of the standard library.  the main python
site
> has
> > pretty good documentation on this, including example code:
> >
> > http://docs.python.org/lib/module-smtplib.html
> >
> >
> > 5265762e204a6f686e6e79204865616c6579
> >
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://www.ale.org/mailman/listinfo/ale
> 
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale




More information about the Ale mailing list