Guest User

How py.path ate my files

a guest
May 21st, 2016
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. # How py.path ate my files
  2. """
  3. Yesterday I realized, the `Pictures` folder had vanished from my hard drive. It wasn't corrupted or anything, it was just gone. I wasted some time trying to recover lost files and checking the hard drive's health, but nothing came up. Apart from the missing files, everything seemed to work. A mystery that left me with a bad feeling about my computer's integrity.
  4. I continued my work and moved on. Finally last night, it struck me: on the evening of the day before the loss I was trying to write a test for a piece of code that reads small text files from the hard drive. Easy enough with `pytest`, use `tempdir` in the test's signature, write the file and call the target with the file path.
  5. """
  6.  
  7. def test_target_read_from_file(tempdir):
  8.   sourcefile = tempdir.join("testfile.txt").write("Hello World!")
  9.   assert target.read(sourcefile) == "Hello World!"
  10.  
  11. """
  12. Now, above test raises a `TypeError: coercing to Unicode: need string or buffer, LocalPath found`. Oh?
  13.  
  14. It turns out `tempdir` resolves to a `py.path.local` object and the target code works with the `os.path` module. So off, to read the `py.path` documentation (http://py.readthedocs.io/en/latest/path.html#reference-documentation), there surely will be a method to get the file path from the `sourcefile`-object. Hmm...which one will it be?
  15. http://py.readthedocs.io/en/latest/path.html#py._path.local.LocalPath.purebasename goes the right direction:
  16. """
  17. import py.path
  18. p = py.path.local()
  19. print p
  20. # >>> local('/home/user')
  21. print p.purebasename
  22. # >>> 'user'
  23.  
  24. """
  25. Well, let's look again. http://py.readthedocs.io/en/latest/path.html#py._path.local.LocalPath.parts returns a list of `py.path.local` objects, so that is definitely worse...
  26. After spending too much time reading the docs and desperately trying, I was ready to do apply brute force. After all, I am a programmer and the computer has to serve me. Having sailed all sorts of heavy weathers, my time had come to outsmart the (obviously) terrible library:
  27. """
  28.  
  29. from operator import methodcaller
  30.  
  31. for m in [m for m in dir(p) if not p.startswith('_')]:
  32.   try:
  33.     print methodcalller(m)(p), m
  34.   except:
  35.     print ":(", m
  36.  
  37. # NOTE: there is an error in the loop as a safeguard. You do not want to run that code!!!
  38.  
  39. """
  40. I hit enter. After roughly two seconds, I cancelled the loop. It did not feel right anymore...I raise my head and disturb my coworker's zen to ask:
  41.  
  42. - "I feel, that I'm stupid, but how the hell do I get the plain path from a `py.path.local` object?"
  43. - "Ah, it may not be the most intuitive, but: just create a string from the object :)"
  44.  
  45. Phew. Mystery solved, test finished. Finally: beer!
  46.  
  47. ...and humiliation: after realizing what happened, I must conclude that this was probably the stupidest thing I did on a computer in a long time.
  48.  
  49. TLDR; don't randomly run all functions of a python object just because you can! Especially so, when dealing with the file system.
  50. """
Advertisement
Add Comment
Please, Sign In to add comment