python Programming Glossary: contextmanager
Make Python stop emitting a carriage return when writing newlines to sys.stdout http://stackoverflow.com/questions/10020325/make-python-stop-emitting-a-carriage-return-when-writing-newlines-to-sys-stdout write yourself a wrapper import sys from contextlib import contextmanager @contextmanager def binary_mode f if sys.platform win32 yield.. wrapper import sys from contextlib import contextmanager @contextmanager def binary_mode f if sys.platform win32 yield return import..
looking for a more pythonic way to access the database http://stackoverflow.com/questions/1107297/looking-for-a-more-pythonic-way-to-access-the-database http jessenoller.com 2009 02 03 get with the program as contextmanager completely different Also the python documentation has a sample..
Alter each for-loop in a function to have error handling executed automatically after each failed iteration http://stackoverflow.com/questions/13648200/alter-each-for-loop-in-a-function-to-have-error-handling-executed-automatically the function becomes as follows from contextlib import contextmanager @contextmanager def ErrorManaged try yield except Exception.. becomes as follows from contextlib import contextmanager @contextmanager def ErrorManaged try yield except Exception as e log_exception..
python libraries for ssh handling http://stackoverflow.com/questions/1939107/python-libraries-for-ssh-handling usr bin env python import paramiko from contextlib import contextmanager host '192.168.10.142' username 'slacker' password 'insecure'@contextmanager.. '192.168.10.142' username 'slacker' password 'insecure'@contextmanager def create_ssh host host username username password password..
How to define a decimal class holding 1000 digits in python? http://stackoverflow.com/questions/19980840/how-to-define-a-decimal-class-holding-1000-digits-in-python You can also use the context locally via the localcontext contextmanager context decimal.Context prec 1000 with decimal.localcontext..
Add columns to CSV while writing the CSV http://stackoverflow.com/questions/20224912/add-columns-to-csv-while-writing-the-csv replacing a file a little easier from contextlib import contextmanager import io import os @contextmanager def inplace filename mode.. from contextlib import contextmanager import io import os @contextmanager def inplace filename mode 'r' buffering 1 encoding None errors..
copy files to nework path or Drive using python http://stackoverflow.com/questions/2625877/copy-files-to-nework-path-or-drive-using-python you are using Python 2.5 or later from contextlib import contextmanager @contextmanager def network_share_auth share username None password.. Python 2.5 or later from contextlib import contextmanager @contextmanager def network_share_auth share username None password None drive_letter..
Pretty-printing of numpy.array http://stackoverflow.com/questions/2891790/pretty-printing-of-numpy-array options. To apply print options locally you could use a contextmanager import numpy as np import contextlib @contextlib.contextmanager.. import numpy as np import contextlib @contextlib.contextmanager def printoptions args kwargs original np.get_printoptions np.set_printoptions.. as arrayprint import contextlib @contextlib.contextmanager def printoptions strip_zeros True kwargs origcall arrayprint.FloatFormat.__call__..
What is the python “with” statement designed for? [closed] http://stackoverflow.com/questions/3012488/what-is-the-python-with-statement-designed-for You can also construct your own context managers using the contextmanager decorator from contextlib . For instance I often use this when.. and then return to where I was from contextlib import contextmanager import os @contextmanager def working_directory path current_dir.. I was from contextlib import contextmanager import os @contextmanager def working_directory path current_dir os.getcwd os.chdir path..
python: create a “with” block on several context managers http://stackoverflow.com/questions/3024925/python-create-a-with-block-on-several-context-managers I should suggest it in a pep P python with statement contextmanager share improve this question In Python 2.6 and below you..
How can I send python multiprocessing Process output to a Tkinter gui http://stackoverflow.com/questions/4227808/how-can-i-send-python-multiprocessing-process-output-to-a-tkinter-gui from code import InteractiveConsole from contextlib import contextmanager from multiprocessing import Process Pipe @contextmanager def.. contextmanager from multiprocessing import Process Pipe @contextmanager def std_redirector stdin sys.stdin stdout sys.stdin stderr sys.stderr..
Having a console in a single-threaded Python script http://stackoverflow.com/questions/4241234/having-a-console-in-a-single-threaded-python-script from code import InteractiveConsole from contextlib import contextmanager __all__ 'Interpreter' @contextmanager def std_redirector stdin.. contextlib import contextmanager __all__ 'Interpreter' @contextmanager def std_redirector stdin sys.stdin stdout sys.stdin stderr sys.stderr..
How do I prevent a C shared library to print on stdout in python? http://stackoverflow.com/questions/5081657/how-do-i-prevent-a-c-shared-library-to-print-on-stdout-in-python descriptors import os import sys from contextlib import contextmanager @contextmanager def stdout_redirected to os.devnull ''' import.. os import sys from contextlib import contextmanager @contextmanager def stdout_redirected to os.devnull ''' import os with stdout_redirected..
Python: Temporarily Redirect stdout/stderr http://stackoverflow.com/questions/6796492/python-temporarily-redirect-stdout-stderr question You can also put the redirection logic in a contextmanager. import os import sys class RedirectStdStreams object def __init__..
Frequently repeated try/except in Python http://stackoverflow.com/questions/7108193/frequently-repeated-try-except-in-python handling is with a context manager from contextlib import contextmanager @contextmanager def common_handling try yield finally # whatever.. a context manager from contextlib import contextmanager @contextmanager def common_handling try yield finally # whatever your common..
|