python Programming Glossary: contextlib
should I call close() after urllib.urlopen()? http://stackoverflow.com/questions/1522636/should-i-call-close-after-urllib-urlopen best approach instead of x urllib.urlopen u etc use import contextlib with contextlib.closing urllib.urlopen u as x ...use x at will.. of x urllib.urlopen u etc use import contextlib with contextlib.closing urllib.urlopen u as x ...use x at will here... The with..
copy files to nework path or Drive using python http://stackoverflow.com/questions/2625877/copy-files-to-nework-path-or-drive-using-python context manager if you are using Python 2.5 or later from contextlib import contextmanager @contextmanager def network_share_auth..
Silence the stdout of a function in python without trashing sys.stdout and restoring each function call http://stackoverflow.com/questions/2828953/silence-the-stdout-of-a-function-in-python-without-trashing-sys-stdout-and-resto save_stdout for elegance a context is best e.g import contextlib import sys import cStringIO @contextlib.contextmanager def nostdout.. is best e.g import contextlib import sys import cStringIO @contextlib.contextmanager def nostdout save_stdout sys.stdout sys.stdout.. object that has a no op write method. For example import contextlib import sys class DummyFile object def write self x pass @contextlib.contextmanager..
Pretty-printing of numpy.array http://stackoverflow.com/questions/2891790/pretty-printing-of-numpy-array you could use a contextmanager import numpy as np import contextlib @contextlib.contextmanager def printoptions args kwargs original.. use a contextmanager import numpy as np import contextlib @contextlib.contextmanager def printoptions args kwargs original np.get_printoptions.. as np import numpy.core.arrayprint as arrayprint import contextlib @contextlib.contextmanager def printoptions strip_zeros True..
How do I zip the contents of a folder using python (version 2.5)? http://stackoverflow.com/questions/296499/how-do-i-zip-the-contents-of-a-folder-using-python-version-2-5 bin env python from __future__ import with_statement from contextlib import closing from zipfile import ZipFile ZIP_DEFLATED import..
What is the python “with” statement designed for? [closed] http://stackoverflow.com/questions/3012488/what-is-the-python-with-statement-designed-for context managers using the contextmanager decorator from contextlib . For instance I often use this when I have to change the current.. directory temporarily and then return to where I was from contextlib import contextmanager import os @contextmanager def working_directory.. to some other file handle and restores them later from contextlib import contextmanager import sys @contextmanager def redirected..
How do you set up a Flask application with SQLAlchemy for testing? http://stackoverflow.com/questions/5025720/how-do-you-set-up-a-flask-application-with-sqlalchemy-for-testing with_statement from sqlite3 import dbapi2 as sqlite3 from contextlib import closing from flask import Flask request session g redirect..
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 avoid leaking file descriptors import os import sys from contextlib import contextmanager @contextmanager def stdout_redirected..
Is it possible to generate and return a ZIP file with App Engine? http://stackoverflow.com/questions/583791/is-it-possible-to-generate-and-return-a-zip-file-with-app-engine available at appengine and reworked example follows from contextlib import closing from zipfile import ZipFile ZIP_DEFLATED from..
Frequently repeated try/except in Python http://stackoverflow.com/questions/7108193/frequently-repeated-try-except-in-python abstract exception handling is with a context manager from contextlib import contextmanager @contextmanager def common_handling try..
Use numpy array in shared memory for multiprocessing http://stackoverflow.com/questions/7894791/use-numpy-array-in-shared-memory-for-multiprocessing ctypes import logging import multiprocessing as mp from contextlib import closing import numpy as np info mp.get_logger .info def..
Getting all visible text from a webpage using Selenium http://stackoverflow.com/questions/7947579/getting-all-visible-text-from-a-webpage-using-selenium Using lxml you might try something like this import contextlib import selenium.webdriver as webdriver import lxml.html as LH.. www.yahoo.com ignore_tags 'script' 'noscript' 'style' with contextlib.closing webdriver.Firefox as browser browser.get url # Load..
Multiple variables in Python 'with' statement http://stackoverflow.com/questions/893333/multiple-variables-in-python-with-statement python with statement share improve this question contextlib.nested supports this import contextlib with contextlib.nested.. this question contextlib.nested supports this import contextlib with contextlib.nested open out.txt wt open in.txt as file_out.. contextlib.nested supports this import contextlib with contextlib.nested open out.txt wt open in.txt as file_out file_in ... Update..
Get page generated with Javascript in Python http://stackoverflow.com/questions/8960288/get-page-generated-with-javascript-in-python You could use Selenium Webdriver # usr bin env python from contextlib import closing from selenium.webdriver import Firefox # pip..
Returning a lower case ASCII string from a (possibly encoded) string fetched using urllib2 or BeautifulSoup http://stackoverflow.com/questions/9012607/returning-a-lower-case-ascii-string-from-a-possibly-encoded-string-fetched-usi or tag names # usr bin env python import urllib2 from contextlib import closing import regex # pip install regex from BeautifulSoup..
Python Selenium (waiting for frame, element lookups) http://stackoverflow.com/questions/9823272/python-selenium-waiting-for-frame-element-lookups improve this question You could use WebDriverWait from contextlib import closing from selenium.webdriver import Chrome as Browser..
|