Reload pickled objects into dict - assigned to variable name from file name I'm doing a bunch of work with echonest audio objects (as returned by the echonest API) in the Python CLI and rather than having to recreate them each time, they can be saved.

This bit of code reloads them all, referenced via a dict:

def get_saved(directory = 'audio/', extension = 'en'):
    return glob.glob(directory + '*.' + extension)

def file_queue(files):
    """
    Get list of files, add them to a queue and return the queue.
    """
    q = Queue() 

    for f in files:
        q.put(f)

    return q

def lazarus(filename):
    """
    Get pickled (*.en) filename (path) and return echonest analysis object
    """
    with open(filename) as f:
        return pickle.load(f)

def lazarus_queue(q):
    container = {}
    while not q.empty():
        track = q.get() 
        filename = track.replace('.mp3.analysis.en', '')
        filename = filename.replace('audio/', '')
        container[filename] = lazarus(track)
    return container

def resurrect():
    files = get_saved()
    q = file_queue(files)
    return lazarus_queue(q)


It's part of a little helper module:

&gt;&gt;&gt; import mtu
&gt;&gt;&gt; audio_objs = mtu.resurrect()
&gt;&gt;&gt; audio_objs
{'claudia': &lt;echonest.remix.audio.LocalAudioFile object at 0x10712e910&gt;, 'GlitchBit_BJanoff': &lt;echonest.remix.audio.LocalAudioFile object at 0x10712e690&gt;, 'livingstonleo': &lt;echonest.remix.audio.LocalAudioFile object at 0x10f52e0d0&gt;, 'amandathorpe': &lt;echonest.remix.audio.LocalAudioFile object at 0x10712ef90&gt;, 'cucumbers': &lt;echonest.remix.audio.LocalAudioFile object at 0x10712e990&gt;}


(Note that pickle itself does not work on the echonest LocalAudioFiles, which have their own save() method.
Here's a couple pieces of style advice, sourced from Python's style guide (PEP0008).

Keyword arguments shouldn't have space on either side of the equals sign:

def get_saved(directory='audio/', extension='en'):


Good that you're including a docstring, but one line docstrings should be on one line.

"""Get list of files, add them to a queue and return the queue."""


Also there may be a reason, but why not use the os module's os.path.join function instead of concatenation when making paths? It checks the OS you're on to ensure it's using the right syntax. It's generally a safer way than concatenation.

return glob.glob(os.path.join(directory, '*.' + extension))