P
paul koelle
hi all,
I thought it would be nice to have instances that can "live" somehow in
the "background" and do some tasks without being told so. The problem in
the code below is: calling new_host = Host(ip, mac, collector) never
"returns" because the the self.update() method gets called recursively.
Im pretty sure I have to restructure the code and call the update()
method from outside but I ask out of curiosity and to get some ideas how
to do such things elegantly.
thanks
Paul
class Host(object):
def __init__(self, ip, mac, collector):
'''ip and mac are strings, collector is a Collector() class
instance from the loganalyze module'''
re_ip = re.compile ('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
print "New host instance created"
if not re_ip.match(ip):
raise ValueError('Invalid IP address.')
else:
self._ip_addr = ip
self._mac_addr = mac
self.collector = collector
# traffic counter
self._minutes = []
self._additional_ips = []
self.update()
def update(self):
'''adds a dict with traffic for this host every minute'''
print "Method update of class Host called"
data = self.collector.get(self._ip_addr)
if data:
self.minutes.append(data)
time.sleep(60)
self.update()
I thought it would be nice to have instances that can "live" somehow in
the "background" and do some tasks without being told so. The problem in
the code below is: calling new_host = Host(ip, mac, collector) never
"returns" because the the self.update() method gets called recursively.
Im pretty sure I have to restructure the code and call the update()
method from outside but I ask out of curiosity and to get some ideas how
to do such things elegantly.
thanks
Paul
class Host(object):
def __init__(self, ip, mac, collector):
'''ip and mac are strings, collector is a Collector() class
instance from the loganalyze module'''
re_ip = re.compile ('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
print "New host instance created"
if not re_ip.match(ip):
raise ValueError('Invalid IP address.')
else:
self._ip_addr = ip
self._mac_addr = mac
self.collector = collector
# traffic counter
self._minutes = []
self._additional_ips = []
self.update()
def update(self):
'''adds a dict with traffic for this host every minute'''
print "Method update of class Host called"
data = self.collector.get(self._ip_addr)
if data:
self.minutes.append(data)
time.sleep(60)
self.update()