Skip to content

Commit

Permalink
Replaced python code by XPATH queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
evanmiltenburg committed Oct 19, 2015
1 parent 3db7596 commit 5dca0a4
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,22 @@ bitdepth_24 = root.xpath('./sound[@bitdepth="24"]')
Here is how to find all sounds with a 'bang' in them.

```python
def sounds_by_crowd_tag(tag):
"Find sounds by original tags."
for sound in root.iterfind('sound'):
tags = [raw.attrib['label'] for raw in sound.xpath('./crowd-tags/tag/raw')]
if tag in tags:
yield sound

bang_sounds = sounds_by_crowd_tag('bang')
bang_sounds = root.xpath('./sound[crowd-tags/tag/raw[@label="bang"]]')
```
##### By their description

We can also use the metadata of the sound to find the recordings you're after. Here is some code to get all the sounds that have a particular word in their description (e.g. 'periodically'):
We can also use the metadata of the sound to find the recordings you're after. Here is some code to get all the sounds that have a particular word in their description (e.g. 'synth'):

```python
def sounds_by_description(word):
"Find sounds by their description."
for sound in root.iterfind('sound'):
description = sound.find('description')
if word in description.text:
yield sound

sounds_periodically = sounds_by_description("periodically")
sounds_synth = root.xpath('./sound[description[contains(.,"synth")]]')
```

##### By original tag

Let's look for sounds that the original author tagged 'vintage':

```python
def sounds_by_author_tag(tag):
"Find sounds by original tags."
for sound in root.iterfind('sound'):
if sound.xpath('./author-tags/tag[@label="' + tag + '"]'):
yield sound

vintage_sounds = sounds_by_author_tag('vintage')
vintage_sounds = root.xpath('./sound[author-tags/tag[@label="vintage"]]')
```

### Code & Replication
Expand Down

0 comments on commit 5dca0a4

Please sign in to comment.