text stringlengths 0 828 |
|---|
) |
return response |
return wrapper |
return decorator" |
4697,"def extract_version(path): |
"""""" |
Reads the file at the specified path and returns the version contained in it. |
This is meant for reading the __init__.py file inside a package, and so it |
expects a version field like: |
__version__ = '1.0.0' |
:param path: path to the Python file |
:return: the version inside the file |
"""""" |
# Regular expression for the version |
_version_re = re.compile(r'__version__\s+=\s+(.*)') |
with open(path + '__init__.py', 'r', encoding='utf-8') as f: |
version = f.read() |
if version: |
version = _version_re.search(version) |
if version: |
version = version.group(1) |
version = str(ast.literal_eval(version.rstrip())) |
extracted = version |
else: |
extracted = None |
else: |
extracted = None |
return extracted" |
4698,"def _make_connect(module, args, kwargs): |
"""""" |
Returns a function capable of making connections with a particular |
driver given the supplied credentials. |
"""""" |
# pylint: disable-msg=W0142 |
return functools.partial(module.connect, *args, **kwargs)" |
4699,"def connect(module, *args, **kwargs): |
"""""" |
Connect to a database using the given DB-API driver module. Returns |
a database context representing that connection. Any arguments or |
keyword arguments are passed the module's :py:func:`connect` function. |
"""""" |
mdr = SingleConnectionMediator( |
module, _make_connect(module, args, kwargs)) |
return Context(module, mdr)" |
4700,"def create_pool(module, max_conns, *args, **kwargs): |
"""""" |
Create a connection pool appropriate to the driver module's capabilities. |
"""""" |
if not hasattr(module, 'threadsafety'): |
raise NotSupported(""Cannot determine driver threadsafety."") |
if max_conns < 1: |
raise ValueError(""Minimum number of connections is 1."") |
if module.threadsafety >= 2: |
return Pool(module, max_conns, *args, **kwargs) |
if module.threadsafety >= 1: |
return DummyPool(module, *args, **kwargs) |
raise ValueError(""Bad threadsafety level: %d"" % module.threadsafety)" |
4701,"def transactional(wrapped): |
"""""" |
A decorator to denote that the content of the decorated function or |
method is to be ran in a transaction. |
The following code is equivalent to the example for |
:py:func:`dbkit.transaction`:: |
import sqlite3 |
import sys |
from dbkit import connect, transactional, query_value, execute |
# ...do some stuff... |
with connect(sqlite3, '/path/to/my.db') as ctx: |
try: |
change_ownership(page_id, new_owner_id) |
catch ctx.IntegrityError: |
print >> sys.stderr, ""Naughty!"" |
@transactional |
def change_ownership(page_id, new_owner_id): |
old_owner_id = query_value( |
""SELECT owner_id FROM pages WHERE page_id = ?"", |
(page_id,)) |
execute( |
""UPDATE users SET owned = owned - 1 WHERE id = ?"", |
(old_owner_id,)) |
execute( |
""UPDATE users SET owned = owned + 1 WHERE id = ?"", |
(new_owner_id,)) |
execute( |
""UPDATE pages SET owner_id = ? WHERE page_id = ?"", |
(new_owner_id, page_id)) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.