Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/olemb/lib
Various useful Python functions
https://github.com/olemb/lib
Last synced: about 6 hours ago
JSON representation
Various useful Python functions
- Host: GitHub
- URL: https://github.com/olemb/lib
- Owner: olemb
- License: mit
- Created: 2011-08-27T12:47:23.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2013-06-27T15:01:43.000Z (over 11 years ago)
- Last Synced: 2023-03-24T06:25:27.749Z (over 1 year ago)
- Language: Python
- Homepage:
- Size: 117 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
lib.py
======A collection of useful Python functions.
Feel free to cut and paste into your own projects.All functions are written to work with Python 2 and 3.
Examples
--------Contextmanagers tmpdir() and chdir()::
>>> import os
>>> from lib import tmpdir, chdir
>>>
>>> with tmpdir() as t:
... with chdir(t):
... print(t)
/tmp/tmpdRzm2V
... print(os.getcwd())
/tmp/tmpdRzm2V
... os.mkdir('these')
... os.mkdir('temporary')
... os.mkdir('dirs')
... os.mkdir('will')
... os.mkdir('be')
... os.mkdir('deleted')
...
>>>
>>> os.chdir('/tmp/tmpdRzm2V')
Traceback (most recent call last):
File "", line 1, in
OSError: [Errno 2] No such file or directory: '/tmp/tmpdRzm2V'inpipe() and stripped()::
>>> import os
>>> from lib import inpipe, stripped
>>>
>>> for line in stripped(inpipe(['ls'])):
... print(repr(line))
...
'LICENSE'
'olelib.py'
'olelib.pyc'
'README.rst'
>>>outpipe()::
>>> from lib import outpipe
>>>
>>> args = ['vorbiscomment', '-w', '-c', '-', '.ogg']
>>> with outpipe(args, nl=True) as write:
... write('artist=Kepa Junkera')
... write('album=Bilbao 00:00h')
... write('title=Arin Qubec')
... write('tracknumber=1')
...
Inpipe and outpipe will encode/decode the data. The default encoding
is 'utf-8', but this can be overriden by calling inpipe(args,
encoding='latin1') etc.env()::
>>> from lib import env
>>>
>>> env('$USER lives in $HOME')
'olemb lives in /home/olemb'
>>>Author: Ole Martin Bjørndalen - [email protected] - http://nerdly.info/ole/
License: MIT