title: how to set an attribute in a nested object in Python?
body: <p>I'm making my first attempts at Python.</p>
  
  <p>I need to loop over a log, parse log entries and then update an object, which includes nested objects for machines listed in the log. </p>
  
  <p>This is what I have:</p>
  
  <pre><code>import re
  format_pat= re.compile( 
      r"(?P&lt;host&gt;(?:[\d\.]|[\da-fA-F:])+)\s" 
      r"(?P&lt;identity&gt;\S*)\s" 
      r"(?P&lt;user&gt;\S*)\s"
      r"\[(?P&lt;time&gt;.*?)\]\s"
      r'"(?P&lt;request&gt;.*?)"\s'
      r"(?P&lt;status&gt;\d+)\s"
      r"(?P&lt;bytes&gt;\S*)\s"
      r'"(?P&lt;referer&gt;.*?)"\s'
      r'"(?P&lt;user_agent&gt;.*?)"\s*' 
  )
  
  from json import JSONEncoder
  class MyEncoder(JSONEncoder):
      def default(self, o):
        return o.__dict__ 
  
  # JSON response object
  class ResponseObject(object):
      def __init__(self, dict):
        self.__dict__ = dict
  
  
  # check for JSON response object
  try:
      obj
  except NameError:
      obj = ResponseObject({})
  
  test = ['2001:470:1f14:169:15f3:824f:8a61:7b59 - SOFTINST [14/Nov/2012:09:32:31 +0100] "POST /setComputer HTTP/1.1" 200 4 "-" "-" 102356']
  
  # log loop
  for line in test:
    try:
      # try to create object from log entry
      m = format_pat.match(line)
      if m:
        res = m.groupdict()
        res["status"] = int(res["status"])
  
        # register machine if not done
        if not hasattr(obj, res["user"]):
          setattr(obj, res["user"], {"downtime":"0","flag":"false","downstart":"0","init":res["time"],"last":"","uptime":"","downtime":"","totaltime":""})
  
  
        machine = getattr(obj, res["user"])
        flag = machine["flag"]
        start = machine["downstart"]
        down = machine["downtime"]
        last = machine["last"]
  
        print "done"
        # set last
        last = res["time"]
  
        # PROBLEM this does not work
        setattr(machine, last, res["time"])
        print machine
  
      else:
        print "nope"
    except:
        print "nope base"
  
  print MyEncoder().encode(obj)
  </code></pre>
  
  <p>The error I'm getting when trying to <code>setattr()</code> is </p>
  
  <pre><code>AttributeError: 'dict' object has no attribute ''
  </code></pre>
  
  <p>but I was afraid it was not as easy as this...</p>
  
  <p><strong>Question:</strong><br>
  How do I update the <code>last</code> value in my nested object using 'setattr'? Or is there another way to update nested object attributes? </p>