toLoad = ['one', 'two', 'three']
history = ['two']
def processLoading(toLoad, history):
oldHistory = history
history = []
for x in toLoad:
if x in oldHistory:
history.append(x)
print "Adding % s to history again" % x
else:
history.append(x)
print "Loading %s, it was not in history" % x
for x in oldHistory:
if x not in toLoad:
print "Unloading %s as it was not in toLoad" % x
toLoad = []
return history
history = processLoading(toLoad, history)
print history
Refactorings
No refactoring yet !
ielectric.myopenid.com
February 5, 2009, February 05, 2009 21:58, permalink
What is your goal? To load all elements that weren't in the history and then update history?
anyway, you should check out python sets.
rem7.myopenid.com
February 5, 2009, February 05, 2009 22:10, permalink
yeah, that's pretty much it. And if the elements are not in the toLoad list then unload them. I'll checkout sets. Thanks
rem7.myopenid.com
February 5, 2009, February 05, 2009 22:54, permalink
interesting... this is what I came up with using sets:
toLoad = set(['two', 'three', 'four', 'one'])
history = set(['two', 'twenty', 'five'])
def processLoadingSets(toLoad, history):
load = toLoad - history
unload = history - toLoad
for x in load:
print "loading %s" % x
for x in unload:
print "unloading %s" % x
history = toLoad
return history
history = processLoadingSets(toLoad, history)
# output ########
loading four
loading three
loading one
unloading twenty
unloading five
print history
# Result: set(['four', 'one', 'two', 'three']) #
ielectric.myopenid.com
February 5, 2009, February 05, 2009 23:16, permalink
yep, that's about it. you can just return toLoad instead of history, otherwise it's as simple as it gets.
I have to load and unload elements based on two lists:
toLoad contains what I have to load.
history contains what is already loaded.
The list toLoad gets constantly updated so I call processLoading through out my entire program. Maybe you guys can find a more efficient way to do this.