misc module¶
This module contains miscellaneous utility functions that can be applied in a variety of circumstances; there are context managers, membership functions (test if an argument is of a given type), numerical functions and string functions
Context managers¶
-
putil.misc.
ignored
(*exceptions)¶ Executes commands and selectively ignores exceptions (Inspired by “Transforming Code into Beautiful, Idiomatic Python” talk at PyCon US 2013 by Raymond Hettinger)
Parameters: exceptions (Exception object, i.e. RuntimeError, OSError, etc.) – Exception type(s) to ignore For example:
# misc_example_1.py from __future__ import print_function import os, putil.misc def ignored_example(): fname = 'somefile.tmp' open(fname, 'w').close() print('File {0} exists? {1}'.format( fname, os.path.isfile(fname) )) with putil.misc.ignored(OSError): os.remove(fname) print('File {0} exists? {1}'.format( fname, os.path.isfile(fname) )) with putil.misc.ignored(OSError): os.remove(fname) print('No exception trying to remove a file that does not exists') try: with putil.misc.ignored(RuntimeError): os.remove(fname) except: print('Got an exception')
>>> import docs.support.misc_example_1 >>> docs.support.misc_example_1.ignored_example() File somefile.tmp exists? True File somefile.tmp exists? False No exception trying to remove a file that does not exists Got an exception
-
class
putil.misc.
Timer
(verbose=False)¶ Bases: object
Profiles blocks of code by calculating elapsed time between the context manager entry and exit time points. Inspired by Huy Nguyen’s blog
Parameters: verbose (boolean) – Flag that indicates whether the elapsed time is printed upon exit (True) or not (False) Returns: putil.misc.Timer Raises: RuntimeError (Argument `verbose` is not valid) For example:
# misc_example_2.py from __future__ import print_function import putil.misc def timer(num_tries, fpointer): with putil.misc.Timer() as tobj: for _ in range(num_tries): fpointer() print('Time per call: {0} seconds'.format( tobj.elapsed_time/(2.0*num_tries) )) def sample_func(): count = 0 for num in range(0, count): count += num
>>> from docs.support.misc_example_2 import * >>> timer(100, sample_func) Time per call: ... seconds
-
elapsed_time
¶ Returns elapsed time (in seconds) between context manager entry and exit time points
Return type: float
-
-
class
putil.misc.
TmpFile
(fpointer=None)¶ Bases: object
Creates a temporary file and optionally sets up hooks for a function to write data to it
Parameters: fpointer (function object or None) – Pointer to a function that writes data to file. If the argument is not None the function pointed to receives exactly one argument, a file-like object Returns: temporary file name Raises: RuntimeError (Argument `fpointer` is not valid) Warning
The file name returned uses the forward slash (
/
) as the path separator regardless of the platform. This avoids problems with escape sequences or mistaken Unicode character encodings (\\user
for example). Many functions in the os module of the standard library ( os.path.normpath() and others) can change this path separator to the operating system path separator if neededFor example:
# misc_example_3.py from __future__ import print_function import putil.misc def write_data(file_handle): file_handle.write('Hello world!') def show_tmpfile(): with putil.misc.TmpFile(write_data) as fname: with open(fname, 'r') as fobj: lines = fobj.readlines() print('\n'.join(lines))
>>> from docs.support.misc_example_3 import * >>> show_tmpfile() Hello world!
File¶
-
putil.misc.
make_dir
(fname)¶ Creates the directory of a fully qualified file name if it does not exist
Parameters: fname (string) – File name Equivalent to these Bash shell commands:
$ dir=$(dirname ${fname}) $ mkdir -p ${dir}
Parameters: fname (string) – Fully qualified file name
-
putil.misc.
normalize_windows_fname
(fname)¶ Fix potential problems with a Microsoft Windows file name. Superfluous backslashes are removed and unintended escape sequences are converted to their equivalent (presumably correct and intended) representation, for example
r'