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

[no subject]



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


--- 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:
> 
&gt; <a  rel="nofollow" href="http://docs.python.org/lib/module-smtplib.html";>http://docs.python.org/lib/module-smtplib.html</a>
&gt; 
&gt; 
&gt; 5265762e204a6f686e6e79204865616c6579
&gt; 
&gt; _______________________________________________
&gt; Ale mailing list
&gt; Ale at ale.org
&gt; <a  rel="nofollow" href="http://www.ale.org/mailman/listinfo/ale";>http://www.ale.org/mailman/listinfo/ale</a>


</pre>
<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<hr>
<ul><li><strong>Follow-Ups</strong>:
<ul>
<li><strong><a name="01267" href="msg01267.html">[ale] Py[h]hon syntax (sic)</a></strong>
<ul><li><em>From:</em> jknapka at kneuro.net (Joe Knapka)</li></ul></li>
<li><strong><a name="01304" href="msg01304.html">[ale] Py[h]hon syntax (sic)</a></strong>
<ul><li><em>From:</em> fletch at phydeaux.org (Fletch)</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>
</ul></li></ul>
<!--X-References-End-->
<!--X-BotPNI-->
<ul>
<li>Prev by Date:
<strong><a href="msg01232.html">[ale] Py[h]hon syntax (sic)</a></strong>
</li>
<li>Next by Date:
<strong><a href="msg01234.html">[ale] Getting Linux OS to boot</a></strong>
</li>
<li>Previous by thread:
<strong><a href="msg01232.html">[ale] Py[h]hon syntax (sic)</a></strong>
</li>
<li>Next by thread:
<strong><a href="msg01267.html">[ale] Py[h]hon syntax (sic)</a></strong>
</li>
<li>Index(es):
<ul>
<li><a href="maillist.html#01233"><strong>Date</strong></a></li>
<li><a href="threads.html#01233"><strong>Thread</strong></a></li>
</ul>
</li>
</ul>

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