Creating A Pythonic API Binding
Lawrence D’Oliveiro
lawrencedo99 at gmail.com
Tue Jun 7 22:31:29 EDT 2016
The wrong way:
ldo at theon:~> python2
Python 2.7.11+ (default, May 9 2016, 15:54:33)
[GCC 5.3.1 20160429] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymtp
>>> mtp = pymtp.MTP()
>>> mtp.connect()
Device 0 (VID=04e8 and PID=6860) is a blah blah blah
>>> topdirs = dict((t.name, t) for t in mtp.get_parent_folders())
>>> alldirs = mtp.get_folder_list()
>>> photos_root = topdirs["DCIM"]
>>> photos_dir = list(f for f in alldirs.values() if f.name == "Camera" and f.parent_id == photos_root.folder_id)[0]
>>> print(list(f for f in mtp.get_filelisting() if f.parent_id == photos_dir.folder_id))
[IMG_20150502_141333.jpg (5184), IMG_20150502_141336.jpg (5185), etc]
>>> mtp.disconnect()
>>>^D
The right way <https://github.com/ldo/mtpy>:
ldo at theon:~> python3
Python 3.5.1+ (default, May 9 2016, 11:00:17)
[GCC 5.3.1 20160429] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mtpy
>>> dev = mtpy.get_raw_devices()[0].open()
Device 0 (VID=04e8 and PID=6860) is a blah blah blah
>>> p = dev.get_descendant_by_path("/DCIM/Camera")
>>> p.get_children()
[<File “/DCIM/Camera/IMG_20150801_162923.jpg”>, <File “/DCIM/Camera/IMG_20150731_153318.jpg”>, etc]
>>> dev.close()
>>>^D
More information about the Python-list
mailing list