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

[no subject]



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

Python has threads.

&gt; 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
&gt; 
&gt; --- Cut Here --- Cut Here
----------------------------------------------
&gt; 
&gt; #!/usr/bin/env python
&gt; 
&gt; import sys;
&gt; import MySQLdb;
&gt; import os;
&gt; import re;
&gt; 
&gt; class Ens:
&gt;   def set_name(self, name):
&gt;     self.name = name;
&gt;   def set_id(self, id):
&gt;     self.id = id;
&gt;   def set_ip(self, ip):
&gt;     self.ip = ip;
&gt;   def get_name(self):
&gt;     return self.name;
&gt;   def get_id(self):
&gt;     return self.id;
&gt;   def get_ip(self):
&gt;     return self.ip;
&gt; 
&gt; 
&gt; def ping(ip, tries = 2):
&gt;   my_re = re.compile('(\w|\W)+ (\d) received, (\w|\W)+')
&gt;   for attempt in range(tries):
&gt;     ping_in, ping_out = os.popen2(&quot;ping -c 1 %s&quot; % ip)
&gt;     for line in ping_out:
&gt;       rec_match = my_re.match(line)
&gt;       if not rec_match: continue
&gt;       received = int(rec_match.groups()[1])
&gt;       if not received: continue
&gt;       return attempt + 1
&gt; 
&gt;   return False
&gt; 
&gt; 
&gt; class Application:
&gt;   def main(self):
&gt;     db = MySQLdb.connect(host=&quot;127.0.0.1&quot;, user=&quot;cms&quot;, passwd=&quot;cms&quot;,
&gt; db=&quot;AC_OUTPOST&quot;);
&gt;     c = db.cursor();
&gt;     c.execute(&quot;select * from ens&quot;);
&gt;     ENS = [ ];
&gt; 
&gt;     while True:
&gt;       row = c.fetchone();
&gt;       if not row: break;
&gt;       e = Ens();
&gt;       e.set_name(row[2]);
&gt;       e.set_id(row[0]);
&gt;       e.set_ip(row[5]);
&gt;       ENS.append(e);
&gt; 
&gt;     for tmp in ENS:
&gt;      result = ping(tmp.get_ip());
&gt;      if not result:
&gt;        print &quot;ID:&quot;,tmp.get_id(),&quot; Name:&quot;,tmp.get_name(),&quot; DOWN!&quot;;
&gt;     else:
&gt;        print &quot;ID:&quot;,tmp.get_id(),&quot; Name:&quot;,tmp.get_name(),&quot; UP!&quot;;
&gt; 
&gt; 
&gt; program = Application();
&gt; program.main();
&gt; 
&gt; # vi: set ts=2 sw=2: #
&gt; 
&gt; --- Cut Here --- Cut Here
----------------------------------------------
&gt; 
&gt; How do I set the object constructor so that I can simply create an Ens
&gt; object with all the information needed?  Is it possible to simply pass
&gt; the whole row into the constructor?
&gt; 
&gt; I read in the O'reilly book &quot;Learning Python&quot; that the code can be
&gt; compiled into a .pyc file.  How do I compile into .pyc so that I can
&gt; distribute the code without the source being seen?  This is one of my
&gt; biggest problems with Perl.
&gt; 
&gt; Thanks,
&gt; Chris
&gt; 
&gt; 
&gt; On Thu, 2005-01-27 at 14:49, John P. Healey wrote:
&gt; &gt; the code for pinging would look something like this:
&gt; &gt;
&gt; &gt; import os
&gt; &gt; import re
&gt; &gt;
&gt; &gt; def ping(ip, tries = 2):
&gt; &gt;     my_re = re.compile('(\w|\W)+ (\d) received, (\w|\W)+')
&gt; &gt;     for attempt in range(tries):
&gt; &gt;         ping_in, ping_out = os.popen2(&quot;ping -c 1 %s&quot; % ip)
&gt; &gt;         for line in ping_out:
&gt; &gt;             rec_match = my_re.match(line)
&gt; &gt;             if not rec_match: continue
&gt; &gt;             received = int(rec_match.groups()[1])
&gt; &gt;             if not received: continue
&gt; &gt;             return attempt + 1
&gt; &gt;     return False
&gt; &gt;
&gt; &gt; it takes an optional second argument (# of tries) and returns the
number
&gt; of
&gt; &gt; tries needed when successful, False otherwise.  the SMTP stuff can
be
&gt; handled
&gt; &gt; with smtplib.  it's part of the standard library.  the main python
site
&gt; has
&gt; &gt; pretty good documentation on this, including example code:
&gt; &gt;
&gt; &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; &gt;
&gt; &gt; 5265762e204a6f686e6e79204865616c6579
&gt; &gt;
&gt; &gt; _______________________________________________
&gt; &gt; Ale mailing list
&gt; &gt; Ale at ale.org
&gt; &gt; <a  rel="nofollow" href="http://www.ale.org/mailman/listinfo/ale";>http://www.ale.org/mailman/listinfo/ale</a>
&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="01264" href="msg01264.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="01266" href="msg01266.html">[ale] Py[h]hon syntax (sic)</a></strong>
<ul><li><em>From:</em> scherrey at proteus-tech.com (Benjamin Scherrey)</li></ul></li>
</ul></li></ul>
<!--X-Follow-Ups-End-->
<!--X-References-->
<!--X-References-End-->
<!--X-BotPNI-->
<ul>
<li>Prev by Date:
<strong><a href="msg01262.html">[ale] Getting Linux OS to boot</a></strong>
</li>
<li>Next by Date:
<strong><a href="msg01264.html">[ale] Py[h]hon syntax (sic)</a></strong>
</li>
<li>Previous by thread:
<strong><a href="msg01276.html">[ale] brain damaged perl DBI</a></strong>
</li>
<li>Next by thread:
<strong><a href="msg01264.html">[ale] Py[h]hon syntax (sic)</a></strong>
</li>
<li>Index(es):
<ul>
<li><a href="maillist.html#01263"><strong>Date</strong></a></li>
<li><a href="threads.html#01263"><strong>Thread</strong></a></li>
</ul>
</li>
</ul>

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