8339a3712739c903f06b8b9066500a63

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.

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 !

64798a399a79ed8d34fa83ba0e61c1ac

ielectric.myopenid.com

February 5, 2009, February 05, 2009 21:58, permalink

No rating. Login to rate!

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.

8339a3712739c903f06b8b9066500a63

rem7.myopenid.com

February 5, 2009, February 05, 2009 22:10, permalink

No rating. Login to rate!

yeah, that's pretty much it. And if the elements are not in the toLoad list then unload them. I'll checkout sets. Thanks

8339a3712739c903f06b8b9066500a63

rem7.myopenid.com

February 5, 2009, February 05, 2009 22:54, permalink

No rating. Login to rate!

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']) #
64798a399a79ed8d34fa83ba0e61c1ac

ielectric.myopenid.com

February 5, 2009, February 05, 2009 23:16, permalink

No rating. Login to rate!

yep, that's about it. you can just return toLoad instead of history, otherwise it's as simple as it gets.

Your refactoring





Format Copy from initial code

or Cancel