An object has a list of dictionaries:
self.aggregator = []
self.aggregator.append({'type': 'Log', 'entry': logline1})
self.aggregator.append({'type': 'Log', 'entry': logline2})
print self.aggregator
Everything is OK so far as seen:
[{'type': 'Log', 'entry': 'Content of logline 1'}, {'type': 'Log', 'entry': 'Content of logline 2'}]
Then I try to reset the contents of this list using different approaches:
self.aggregator = []
del self.aggregator
Now when I print the list it is empty:
[]
But when I check the length of the list, it still has its old size:
>> print len(self.aggregator)
2
Why is this possible, did I miss anything?
=== Update
Complete demo code below. I figured out what causes the error. It's the line with "del self.aggregator". When I delete this line, everything works as expected. Tested this behaviour on OS X with Python 2.7.10 and Python 2.7.11 (32bit) on Windows 7.
class Checker(object):
aggregator = []
aggregator_max = 10
def __init__(self):
pass
def finalizeAggregator(self):
string = "\n".join([attribute['string'] for attribute in self.aggregator])
# Check for a match on all the aggregator content
match_result = self.checkString(string,"")
if match_result:
# If match has been found, check the aggregator contents one by one
for element in self.aggregator:
self.checkString(element["string"],
element["module"],
use_aggregator=False)
# Clear aggregator
print self.aggregator
self.aggregator = None
self.aggregator = []
del self.aggregator
print self.aggregator
print len(self.aggregator)
def checkString(self, string, module, use_aggregator=False):
# If aggregator should be used
if use_aggregator:
print "USING aggregator"
# As long as the aggregator is not full
if len(self.aggregator) <= self.aggregator_max:
#if string not in self.aggregator:
# Add element to aggregator
self.aggregator.append({"string": string,
"module": module})
# Process aggregator if full
if len(self.aggregator) >= self.aggregator_max:
self.finalizeAggregator()
# Otherwise return
else:
return False
else:
print "NOT using aggregator"
if "evil" in string:
print "WARNING!!!"
if __name__ == '__main__':
checker = Checker()
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is evil', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)