diff --git a/README.md b/README.md index dc47d76..81a2d35 100644 --- a/README.md +++ b/README.md @@ -51,28 +51,14 @@ 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 @@ -80,13 +66,7 @@ sounds_periodically = sounds_by_description("periodically") 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