Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # How py.path ate my files
- """
- 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.
- 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.
- """
- def test_target_read_from_file(tempdir):
- sourcefile = tempdir.join("testfile.txt").write("Hello World!")
- assert target.read(sourcefile) == "Hello World!"
- """
- Now, above test raises a `TypeError: coercing to Unicode: need string or buffer, LocalPath found`. Oh?
- 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?
- http://py.readthedocs.io/en/latest/path.html#py._path.local.LocalPath.purebasename goes the right direction:
- """
- import py.path
- p = py.path.local()
- print p
- # >>> local('/home/user')
- print p.purebasename
- # >>> 'user'
- """
- 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...
- 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:
- """
- from operator import methodcaller
- for m in [m for m in dir(p) if not p.startswith('_')]:
- try:
- print methodcalller(m)(p), m
- except:
- print ":(", m
- # NOTE: there is an error in the loop as a safeguard. You do not want to run that code!!!
- """
- 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:
- - "I feel, that I'm stupid, but how the hell do I get the plain path from a `py.path.local` object?"
- - "Ah, it may not be the most intuitive, but: just create a string from the object :)"
- Phew. Mystery solved, test finished. Finally: beer!
- ...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.
- TLDR; don't randomly run all functions of a python object just because you can! Especially so, when dealing with the file system.
- """
Advertisement
Add Comment
Please, Sign In to add comment