Monday, December 17, 2012

Recover music files from iPod (Windows)

Problem: The computer with the original music is out of operation, leaving the only copy of music on the iPod. How to get the music back

On windows:
  1. Connect the iPod
  2. Double click on the iPod icon to open the folder
  3. It will list four folders none of which contain music. The music folder is hidden
  4. Press ALT+T to expose the tools menu
  5. Click on tools and show hidden files
  6. Enter the iPod_Controller folder
  7. Copy all the subfolders under this into a temporary directory (say /OldMusic) on the new computer (the music files are here)
  8. Select all music directories under  /OldMusic and uncheck hidden (the folders are hidden)
  9. Open up iTunes
  10. Click ALT to bring up the menu bar
  11. Click File->Add folder to add the folders to the library

Monday, December 10, 2012

Python: re (regexp): index of matches: difference between findall and finditer

  • using the findall method of a regexp will return all matches to the regular experession as a list
  • using finditer will return an iterator which you can use to interate through the matches
  • if m is the result of iterator.next() then m.span() will give you the start and end indexes of the match and m.group() gives you the match
  • If you have groups in the regexp e.g r"a(.*?)b" then m.group() will return the WHOLE matched expression and m.span() will return the corresponding start and stop indexes. To get just the particular group, for the expression given above, you have to do m.span(1) and m.group(1)

Sunday, December 9, 2012

Python: using a regexp to do pattern matching on a byte stream

Python regular expressions are a very concise and efficient way of performing pattern matching on strings. Many computing problems involves a similar kind of pattern matching, but on arbitrary data. For my particular application I have a long sequence of one byte digital codes that indicate a sequence of runs for an experiment. Each run starts with the codes 9,9,9 followed by some codes telling me what happened during the experiment and ends with 18,18,18. I need to split up this long sequence of codes into runs and then parse the events in each run.

In the past I would have written a state machine to do this, but I thought, that's a waste: the regexp module already implements logic of this kind. So I came up with the following:

  ec = array.array('B', [ev & 0xff for ev in event_sequence]).tostring()
  separate = re.compile(r"\x09\x09\x09(.*?)\x12\x12\x12")
  trials = separate.findall(ec)

array.array converts the sequence of bytes into a fake string and the regexp does its usual magic.

Friday, December 7, 2012

apsw + cifs (samba) on ubuntu

Problem: a python program using apsw on MacOSX runs fine and fast using c.executemany to do inserts. When the same program is run on Ubuntu 12 the program runs very slow.

Solution:  Use transactions explicitly. Use a 'BEGIN' and 'END' statements to bracket the list of inserts. For some reason on Mac OS X this does not make a difference, but in Ubuntu it does

Problem: On Ubuntu a python program using apsw can create a database just fine on a local drive but on a samba (smb) network drive it gives the error: apsw.BusyError: BusyError: database is locked

Solution: use the nobrl option to mount. eg. in the /etc/fstab the line should look like

//server/path   /mnt/LabServer cifs nobrl,credentials=/my/home/credfile,iocharset=utf8,file,uid=1000,gid=1000