psycodict.encoding¶
This module provides functions for encoding data for storage in Postgres and decoding the results.
- psycodict.encoding.numeric_precision(value)[source]¶
The bit precision needed to faithfully represent a decimal string: log(10)/log(2) bits per significant digit, where the sign, the decimal point and leading zeros carry no information. (Counting every character, as this function’s predecessor did, manufactured phantom digits whenever the value was printed at full precision.) At least 2, the smallest precision a RealField accepts.
INPUT:
value– a string representing a decimal number, as Postgres delivers the numeric type
EXAMPLES:
>>> numeric_precision("0.00459244230167") # 12 significant digits 40
- psycodict.encoding.numeric_converter(value, cur=None)[source]¶
Used for converting numeric values from Postgres to Python.
INPUT:
value– a string representing a decimal number.cur– a cursor, unused
OUTPUT:
either a sage integer (if there is no decimal point) or a real number whose precision depends on the number of significant digits in value.
- class psycodict.encoding.Array(seq)[source]¶
Bases:
objectSince we use Json by default for lists, this class lets us get back the original behavior of encoding as a Postgres array when needed.
- class psycodict.encoding.ArrayDumper(cls: type, context: AdaptContext | None = None)[source]¶
Bases:
DumperDumps an Array wrapper as a Postgres array literal with unknown oid.
- class psycodict.encoding.Json(obj)[source]¶
Bases:
objectA wrapper marking a value for storage as json/jsonb, encoded with psycodict’s extended encoding (Sage types etc.).
With psycopg2 this subclassed
psycopg2.extras.Json; with psycopg3 adaptation happens throughJsonWrapperDumperbelow instead. The wrapped value is available as both.obj(psycopg3 convention) and.adapted(psycopg2 convention, kept for backward compatibility).- classmethod dumps(obj)[source]¶
Serialize
objto json text using the extended encoding (seeprep()).
- classmethod loads(s)[source]¶
Parse json text and decode the extended encoding back to Python and Sage objects (see
extract()).
- class psycodict.encoding.JsonWrapperDumper(cls: type, context: AdaptContext | None = None)[source]¶
Bases:
DumperDumps a
Jsonwrapper as its json text. We leave the oid unknown (0) rather than declaring json/jsonb, matching psycopg2’s behavior of interpolating an untyped quoted literal so that the server casts by context (this works for both json and jsonb columns).
- class psycodict.encoding.DictJsonDumper(cls: type, context: AdaptContext | None = None)[source]¶
Bases:
DumperDumps a plain dict as json text with unknown oid (psycopg2 behavior via
register_adapter(dict, Json)).
- psycodict.encoding.check_copy_sep(sep)[source]¶
Raise a ValueError if
sepcannot be used as the column separator for a COPY (text format) file written bycopy_dumps().
- psycodict.encoding.copy_dumps(inp, typ, recursing=False, *, sep='|')[source]¶
Output a string formatted as needed for loading by Postgres’ COPY FROM.
INPUT:
inp– a Python or Sage object that directly translates to a postgres type (e.g. Integer, RealLiteral, dict…typ– the Postgres type of the column in which this data is being stored.recursing– used internally to format the elements of an array. The value is rendered as an array member (quoted and escaped for the array parser as needed) rather than as a complete COPY field, andsepplays no role: the separator is escaped in a single pass over the assembled field by the top-level call.sep– (keyword only) the column separator that the resulting file will be loaded with (default"|"). Occurrences of this character anywhere in the formatted field – inside text values, but also in dates and negative numbers forsep="-", times forsep=":", JSON text and the braces, commas and quotes of array literals – are backslash-escaped, matching what Postgres’ COPY (text format) does on output, so that they are not mistaken for column boundaries on the way back in. This must agree with the separator eventually passed to COPY FROM. Separators that cannot work (seecheck_copy_sep()) raise a ValueError.