psycodict.utils

Helpers shared across psycodict: DelayCommit for batching many writes into one transaction, the whitelist parser that admits limited raw arithmetic into queries (filter_sql_injection()), identifier wrapping with array slicers (IdentifierWrapper()), the exceptions raised during query parsing and locking, and small formatting utilities.

class psycodict.utils.DelayCommit(obj, final_commit=True, silence=None, active=True)[source]

Bases: object

Used to set default behavior for whether to commit changes to the database connection.

Entering this context in a with statement will cause _execute calls to not commit by default. When the final DelayCommit is exited, the connection will commit.

Setting active=False disables the DelayCommit completely, which can be helpful since it’s often used in a with context and conditionally entering that context is annoying to write with if statements.

psycodict.utils.IdentifierWrapper(name, convert=True)[source]

Returns a composable representing an SQL identifier.

This is wrapper for psycopg.sql.Identifier that supports ARRAY slicers and converts them (if desired) from the Python format to SQL, as SQL starts at 1, and it is inclusive at the end

EXAMPLES:

>>> IdentifierWrapper('name')
Identifier('name')
>>> print(IdentifierWrapper('name[:10]').as_string(db.conn))
"name"[:10]
>>> print(IdentifierWrapper('name[1:10]').as_string(db.conn))
"name"[2:10]
>>> print(IdentifierWrapper('name[1:10]', convert=False).as_string(db.conn))
"name"[1:10]
>>> print(IdentifierWrapper('name[1:10:3]').as_string(db.conn))
"name"[2:10:3]
>>> print(IdentifierWrapper('name[1:10:3][0:2]').as_string(db.conn))
"name"[2:10:3][1:2]
>>> print(IdentifierWrapper('name[1:10:3][0::1]').as_string(db.conn))
"name"[2:10:3][1::1]
>>> print(IdentifierWrapper('name[1:10:3][0]').as_string(db.conn))
"name"[2:10:3][1]
exception psycodict.utils.LockError[source]

Bases: RuntimeError

Raised when a data-changing operation cannot proceed because another operation holds a conflicting lock on the table (for example, a reload in progress, or a staged _tmp copy awaiting its swap).

exception psycodict.utils.SearchParsingError(*args, **kwargs)[source]

Bases: ValueError

Used for errors raised when parsing search boxes