recursive method in __init__

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paul koelle

    #1

    recursive method in __init__

    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('Inv alid IP address.')
    else:
    self._ip_addr = ip
    self._mac_addr = mac
    self.collector = collector
    # traffic counter
    self._minutes = []
    self._additiona l_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_ad dr)
    if data:
    self.minutes.ap pend(data)
    time.sleep(60)
    self.update()
Working...