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

[no subject]



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


</pre>
<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<hr>
<ul><li><strong>Follow-Ups</strong>:
<ul>
<li><strong><a name="01268" href="msg01268.html">[ale] Py[h]hon syntax (sic)</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Christopher Fowler)</li></ul></li>
</ul></li></ul>
<!--X-Follow-Ups-End-->
<!--X-References-->
<ul><li><strong>References</strong>:
<ul>
<li><strong><a name="01121" href="msg01121.html">[ale] Pyhon syntax</a></strong>
<ul><li><em>From:</em> fletch at phydeaux.org (fletch at phydeaux.org)</li></ul></li>
<li><strong><a name="01129" href="msg01129.html">[ale] Pyhon syntax</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Christopher Fowler)</li></ul></li>
<li><strong><a name="01133" href="msg01133.html">[ale] Py[h]hon syntax (sic)</a></strong>
<ul><li><em>From:</em> scherrey at proteus-tech.com (Benjamin Scherrey)</li></ul></li>
<li><strong><a name="01207" href="msg01207.html">[ale] Py[h]hon syntax (sic)</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Christopher Fowler)</li></ul></li>
<li><strong><a name="01215" href="msg01215.html">[ale] Py[h]hon syntax (sic)</a></strong>
<ul><li><em>From:</em> jpheale at LearnLink.Emory.Edu (John P. Healey)</li></ul></li>
<li><strong><a name="01233" href="msg01233.html">[ale] Py[h]hon syntax (sic)</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Christopher Fowler)</li></ul></li>
</ul></li></ul>
<!--X-References-End-->
<!--X-BotPNI-->
<ul>
<li>Prev by Date:
<strong><a href="msg01266.html">[ale] Py[h]hon syntax (sic)</a></strong>
</li>
<li>Next by Date:
<strong><a href="msg01268.html">[ale] Py[h]hon syntax (sic)</a></strong>
</li>
<li>Previous by thread:
<strong><a href="msg01233.html">[ale] Py[h]hon syntax (sic)</a></strong>
</li>
<li>Next by thread:
<strong><a href="msg01268.html">[ale] Py[h]hon syntax (sic)</a></strong>
</li>
<li>Index(es):
<ul>
<li><a href="maillist.html#01267"><strong>Date</strong></a></li>
<li><a href="threads.html#01267"><strong>Thread</strong></a></li>
</ul>
</li>
</ul>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
</body>
</html>