1
\$\begingroup\$

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:

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

(Note that pickle itself does not work on the echonest LocalAudioFiles, which have their own save() method.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 2, 2015 at 17:12
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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))
answered Sep 10, 2015 at 9:32
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.