[Tutor] Dict question
Roeland Rengelink
r.b.rigilink@chello.nl
Thu, 14 Jun 2001 06:19:31 +0200
Hi Nate,
As far as I can tell your code is not wrong (Is this everything there
is), so given this I don't understand the error.
However, it is somewhat unconventional, which may hide the error from
me.
The idiom for what you're trying to do with updating your dict is:
l = error_dict.get(error, []) # if error in dict get list, else return
empty list
l.append(error_ip)
error_dict[error] = l # put list (back) in dict
Hope this helps,
Roeland
> Nate Custer wrote:
>
> Hi all,
>
> I am calling a dict (format should be {string: [list]} ). It fails as:
>
> File "guardian_sum.py", line 45, in scan
> l.append(error_ip)
> AttributeError: 'string' object has no attribute 'append'
>
> Why does the error_dict.get() return a string?
>
> Thanks in advance,
>
> Nate Custer
>
> ====================================Begin Code
> ==========================
> error_dict = {}
>
> def scan():
> guardian = open("/var/log/guardian/guardian.log", 'r')
> ipchains = open("/home/nsc/ipchains.log", 'w')
> odd = open("/home/nsc/odd.log", 'w')
>
> nums = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ]
> days = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
> blocked_host = []
>
> while 1:
> # read the line
> line = guardian.readline()
> if not line: break
>
> if line[:3] in days:
>
> word = string.split (line)
> for w in word:
> if 'IDS' == w[:3]:
> error = w
> if w[0] in nums:
> error_ip = w
>
This is unconventional:
> if error_dict.has_key(error):
> l = error_dict.get(error)
> l.append(error_ip)
> append = {error: l}
> error_dict.update (append)
> else:
> x = [`error_ip`]
> append = {error: x}
> error_dict.update (append)
--
r.b.rigilink@chello.nl
"Half of what I say is nonsense. Unfortunately I don't know which half"