Question

Full script: https://gist.github.com/4476526

The specific code in question is

# Cloud Files username & API key
username = ''
key = ''

# Source and destination container names
originContainerName = ''
targetContainerName = ''

...

def cloudConnect():
    global originContainer
    global targetContainer
    global connection
    print "Creating connection"
    connection = cloudfiles.get_connection(username,key,servicenet=True)
    print "-- [DONE]"
    print "Accessing containers"
    originContainer = connection.create_container(originContainerName)
    targetContainer = connection.create_container(targetContainerName)
    print "-- [DONE]"
    return

The script works perfectly fine, however I've read in multiple places that global variables should be used with hesitation and that there is almost always a better way to do the same thing without them. Is this true? And if so, how exactly should I fix this script? To me it seems much easier just to use global connection and container variables instead of passing those objects around as arguments in multiple functions.

Was it helpful?

Solution

You should create a class (called something like CloudContainer) that includes all of those global variables as members, and rewrite it as (just as a start):

class CloudContainers(object):
    def __init__(self, username, key, originContainerName, targetContainerName):
        self.username = username
        self.key = key     
        self.originContainerName = originContainerName
        self.targetContainerName = targetContainerName

    def cloudConnect(self):
        print "Creating connection"
        self.connection = cloudfiles.get_connection(self.username,self.key,servicenet=True)
        print "-- [DONE]"
        print "Accessing containers"
        self.originContainer = connection.create_container(self.originContainerName)
        self.targetContainer = connection.create_container(self.targetContainerName)
        print "-- [DONE]"
        return

    def uploadImg(self, new_name):
        new_obj = self.targetContainer.create_object(new_name)
        new_obj.content_type = 'image/jpeg'
        new_obj.load_from_filename("up/"+new_name)

    def getImg(name):
        obj = self.originContainer.get_object(name)
        obj.save_to_filename("down/"+name)

Thus, any function that uses these global variables (like getImg and uploadImg above) would be included as a method of the class.

OTHER TIPS

Easier, yes, but it means that it's very hard to tell when and why those variables get changed. I think the best answer is the one you have given in your question - pass them around as an object. E.g:

def cloud_connect(origin_container_name, target_container_name):
    print "Creating connection"
    connection = cloudfiles.get_connection(username, key, servicenet=True)
    print "-- [DONE]"
    print "Accessing containers"
    origin_container = connection.create_container(origin_container_name)
    target_container = connection.create_container(target_container_name)
    print "-- [DONE]"
    return connection, origin_container, target_container

Then simply pass that tuple around.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top