[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



Christopher Fowler <cfowler at outpostsentinel.com> writes:

> 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?

Yes. Check the module index in the Python docs.

>  I think in Perl threads are not really threads
> as they would be in C.  
> 
> chop up this code and tell me how it can be better:

It could be a lot shorter:
 
--- Cut Here --- Cut Here ----------------------------------------------

#!/usr/bin/env python

import MySQLdb;
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 "UP"
  return "DOWN"

db = MySQLdb.connect(host="127.0.0.1", user="cms", passwd="cms",db="AC_OUTPOST");
c = db.cursor();
c.execute("select * from ens");
row = c.fetchone();
while row:
  id,dummy1,name,dummy2,dummy3,ip = row
  print "ID:",id,"Name:",name,ping(ip)
  row = c.fetchone()
--- Cut Here --- Cut Here ----------------------------------------------

IMO, "do the simplest thing that can possibly work" is nearly always
the right thing :-) While not explicitly OO, the code above is much
shorter and equally encapsulated: there is only a single line of code
that knows the structure of a DB row, and that knowledge was all that
was really being abstracted in the original code.
 
> 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?

class Ens:
  def __init__(self,row):
    self.id = row[0]
    self.ip = row[5]
    self.name = row[2]

But OO is definitely overkill in this example.

> 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?

Run it. The interpreter will generate a .pyc.

>  This is one of my
> biggest problems with Perl.

You worry about OTHER people reading and understanding your
Perl code???

-- Joe

-- 
No sig for you today.
--
pub  1024D/BA496D2B 2004-05-14 Joseph A Knapka
     Key fingerprint = 3BA2 FE72 3CBA D4C2 21E4  C9B4 3230 94D7 BA49 6D2B
If you really want to get my attention, send mail to
jknapka .at. kneuro .dot. net.