Entries in python (1)

Tuesday
May242011

triggerGreppy

For the longest time, I have been meaning to brush up on Python. Unfortunately, * I haven't had much need for scripts lately. * When I have needed a script, I've needed it pronto! No time to learn a new language.

The problem is I'm lazy and I don't really want to read a book or website on it. I just want to solve some little problem I might have that needed scripting. You know, just take a sample script and play with it to do something useful to me.

So... after lamenting about this, my friend Mike sent me a sample python script to play with. The sample code he sent me was basically a simple grep clone.

Aha!

I have always wanted a grep-like tool that gave me more context. I know that you can tell grep itself to provide more context, but as far as I know, you can only specify the number of lines before and after your match you want included. This is realistically all one might need for most cases, but I wanted more.

So that is what me new script does. (You can find a copy of my script here) You provide three search terms:

  • trigger: the main term you are looking for.
  • begin: something pattern that describes the beginning of the block you want to print out.
  • end: The end pattern for the block.

For example: I exported an xml file from my iTunes library that describes all of my media. I can now search for the name of an author for example and get The full dictionary entry for the song. (output listed below.)

I hope this is useful to some one else or at least serves as a starting point for your own exploration of python.

% triggerGreppy Tacuba "<dict>" "</dict>" Library.xml

    <dict>
        <key>Track ID</key><integer>7587</integer>
        <key>Name</key><string>Bar Tacuba</string>
        <key>Artist</key><string>Café Tacuba</string>
        <key>Album Artist</key><string>Café Tacuba</string>
        <key>Album</key><string>Café Tacuba</string>
        <key>Genre</key><string>Alternativo &#38; Rock Latino</string>
        <key>Kind</key><string>MPEG audio file</string>
        <key>Size</key><integer>4703278</integer>
        <key>Total Time</key><integer>233221</integer>
        <key>Track Number</key><integer>13</integer>
        <key>Year</key><integer>1992</integer>
        <key>Date Modified</key><date>2010-09-27T20:44:49Z</date>
        <key>Date Added</key><date>2010-08-03T04:30:10Z</date>
        <key>Bit Rate</key><integer>160</integer>
        <key>Sample Rate</key><integer>44100</integer>
        <key>Normalization</key><integer>2381</integer>
        <key>Artwork Count</key><integer>1</integer>
        <key>Persistent ID</key><string>E467DC7506CA7184</string>
        <key>Track Type</key><string>File</string>
        <key>Location</key><string>file://localhost/Volumes/Attic/Music/iTunes/iTunes%20Music/Cafe%CC%81%20Tacuba/Cafe%CC%81%20Tacuba/13%20Bar%20Tacuba.mp3</string>
        <key>File Folder Count</key><integer>4</integer>
        <key>Library Folder Count</key><integer>1</integer>
    </dict>

---------------------
    <dict>
        <key>Track ID</key><integer>7753</integer>
        <key>Name</key><string>Las Flores- Cafe Tacuba</string>
        <key>Artist</key><string>Various Artists</string>
        <key>Album</key><string>Hace Calor  Pop &#38; Rock</string>
        <key>Genre</key><string>General Rock</string>
        <key>Kind</key><string>MPEG audio file</string>
        <key>Size</key><integer>2752097</integer>
        <key>Total Time</key><integer>136881</integer>
        <key>Track Number</key><integer>3</integer>
        <key>Date Modified</key><date>2010-08-19T05:00:31Z</date>
        <key>Date Added</key><date>2010-08-19T04:58:27Z</date>
        <key>Bit Rate</key><integer>160</integer>
        <key>Sample Rate</key><integer>44100</integer>
        <key>Play Count</key><integer>1</integer>
        <key>Play Date</key><integer>3370023481</integer>
        <key>Play Date UTC</key><date>2010-10-16T01:38:01Z</date>
        <key>Normalization</key><integer>2518</integer>
        <key>Persistent ID</key><string>52FAE1944A13F3EE</string>
        <key>Track Type</key><string>File</string>
        <key>Location</key><string>file://localhost/Volumes/Attic/Music/iTunes/iTunes%20Music/Various%20Artists/Hace%20Calor%20%20Pop%20&#38;%20Rock/03%20Las%20Flores-%20Cafe%20Tacuba.mp3</string>
        <key>File Folder Count</key><integer>4</integer>
        <key>Library Folder Count</key><integer>1</integer>
    </dict>

---------------------

[I removed furthere matches for brevity]