PostgreSQL binary type I/O routines
Typical is a project providing PostgreSQL binary type I/O routines for Python. At the core, pstruct, it provides the transformation routine. The outer layers, oidmap and stdio, it provides dictionaries that associate the PostgreSQL type oid with the transformation routine.
package postgresql.protocol.typical
Typical PostgreSQL type I/O package space
module postgresql.protocol.typical.oidmaps
Map PostgreSQL type Oids to routines that pack and unpack raw data. stdio: The primary map.
module postgresql.protocol.typical.pstruct
PostgreSQL data type protocol--packing and unpacking functions. This module provides functions that pack and unpack many standard PostgreSQL types. These functions are completely unassociated with normally corresponding type Oids; that mapping is handled by the `postgresql.protocol.typical.oidmaps` module and the `postgresql.protocol.typical.stdio` module(The latter doesn't normally map to these functions, rather a higher level transformation that uses these functions). The name of the function describes what type the function is intended to be used on. Normally, the fucntions return a structured form of the serialized data to be used as a parameter to the creation of a higher level instance. In particular, most of the functions that deal with time return a pair for representing the relative offset: (seconds, microseconds). For times, this provides an abstraction for quad-word based times used by some configurations of PostgreSQL.
functions
def mk_pack(x):
Create a pair, (pack, unpack) for the given `struct` format.
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def <lambda>(y):
def mktimetuple(ts):
make a pair of (seconds, microseconds) out of the given double
def mktimetuple64(ts):
make a pair of (seconds, microseconds) out of the given long
def mktime((seconds, ms)):
make a double out of the pair of (seconds, microseconds)
def mktime64((seconds, ms)):
make an integer out of the pair of (seconds, microseconds)
def path_pack(data):
Given a sequence of point data, pack it into a path's serialized form.
[px1, py1, px2, py2, ...]
Must be an even number of numbers.
def path_pack(data):
Given a sequence of point data, pack it into a path's serialized form.
[px1, py1, px2, py2, ...]
Must be an even number of numbers.
def path_unpack(data):
Unpack a path's serialized form into a sequence of point data:
[px1, py1, px2, py2, ...]
Should be an even number of numbers.
def path_unpack(data):
Unpack a path's serialized form into a sequence of point data:
[px1, py1, px2, py2, ...]
Should be an even number of numbers.
def time_pack(timetup):
Given a pair, (seconds, microseconds), serialize it into a double.
def time_unpack(data):
Given a serialized double, return the pai (seconds, microseconds).
def interval_pack((month, day, timetup)):
Given a triple, (month, day, (seconds, microseconds)), serialize it for transport.
def interval_unpack(data):
Given a serialized interval, '{month}{day}{time}', yield the triple:
(month, day, (seconds, microseconds))
def interval_noday_pack((month, day, timetup)):
Given a triple, (month, day, (seconds, microseconds)), return the serialized form that does not have an individual day component. There is no day component, so if day is non-zero, it will be converted to seconds and subsequently added to the seconds.
def interval_noday_unpack(data):
Given a serialized interval without a day component, return the triple:
(month, day(always zero), (seconds, microseconds))
def time64_pack(timetup):
Pack a (seconds, microseconds) tuple into a quad-word.
def time64_unpack(data):
Given a quad-word, unpack it into a pair:
(seconds, microseconds)
def interval64_pack((month, day, timetup)):
Given a triple, (month, day, (seconds, microseconds)), return the serialized data using a quad-word for the (seconds, microseconds) tuple.
def interval64_unpack(data):
Unpack an interval containing a quad-word into a triple:
(month, day, (seconds, microseconds))
def interval64_noday_pack((month, day, timetup)):
Pack an interval without a day component and using a quad-word for second representation. There is no day component, so if day is non-zero, it will be converted to seconds and subsequently added to the seconds.
def interval64_noday_unpack(data):
Unpack a ``noday`` quad-word based interval. Returns a triple:
(month, day(always zero), (seconds, microseconds))
def timetz_pack((timetup, tz_offset)):
Pack a time; offset from beginning of the day and timezone offset. Given a pair, ((seconds, microseconds), timezone_offset), pack it into its serialized form: "!dl".
def timetz_unpack(data):
Given serialized time data, unpack it into a pair:
((seconds, microseconds), timezone_offset).
def timetz64_pack((timetup, tz_offset)):
Pack a time; offset from beginning of the day and timezone offset. Given a pair, ((seconds, microseconds), timezone_offset), pack it into its serialized form using a long long: "!ql".
def timetz64_unpack(data):
Given "long long" serialized time data, "ql", unpack it into a pair:
((seconds, microseconds), timezone_offset)
def oidvector_pack(seq):
Given a sequence of Oids, pack them into the serialized form. An oidvector is a type used by the PostgreSQL catalog.
def oidvector_unpack(data):
Given a serialized oidvector(32 longs), unpack it into a list of unsigned integers. An int2vector is a type used by the PostgreSQL catalog.
def int2vector_pack(seq):
Given a sequence of integers, pack them into the serialized form. An int2vector is a type used by the PostgreSQL catalog.
def int2vector_unpack(data):
Given a serialized int2vector, unpack it into a list of integers. An int2vector is a type used by the PostgreSQL catalog.
def varbit_pack((bits, data)):
Given a pair, serialize the varbit. >>> varbit_pack((1, '\x00')) '\x00\x00\x00\x01\x00'
def varbit_unpack(data):
Given ``varbit`` data, unpack it into a pair:
(bits, data)
Where
def cidr_pack((family, mask, data)):
Given a triple, yield the serialized form for transport. Prepends the ``family``, ``mask`` and implicit ``is_cidr`` fields.
def cidr_unpack(data):
Given serialized cidr data, return a tuple:
(family, mask, data)
def inet_pack(data):
Given serialized inet data; add the necessary family mask for transport.
def inet_unpack(data):
Unpack serialized inet data into mere address data. Mostly, this strips off the explicitly family specification as that can be determined from the length of the string.
def record_unpack(data):
Given serialized record data, return a tuple of tuples of type Oids and attributes.
def record_pack(seq):
pack a record given an iterable of (type_oid, data) pairs.
def elements_pack(elements):
Pack the elements for containment within a serialized array. This is used by array_pack.
def array_pack((flags, typid, dlb, elements)):
Pack a raw array. A raw array consists of flags, type oid, sequence of lower
and upper bounds, and an iterable of already serialized element data:
(0, element type oid, (lower bounds, upper bounds, ...), iterable of element_data)
The lower bounds and upper bounds specifies boundaries of the dimension. So the length
of the boundaries sequence is two times the number of dimensions that the array has.
def elements_unpack(data, offset):
Unpack the serialized elements of an array into a list. This is used by array_unpack.
def array_unpack(data):
Given a serialized array, unpack it into a tuple:
(flags, typid, (lower bounds, upper bounds, ...), [elements])
data
BBBB_unpack =
<built-in method unpack of Struct object at 0x1136fe0>
bitdata_to_bool =
{'\x00\x00\x00\x01\x00': False, '\x00\x00\x00\x01\x01': True}bool_to_bitdata =
{False: '\x00\x00\x00\x01\x00', True: '\x00\x00\x00\x01\x01'}bool_to_data =
{False: '\x00', True: '\x01'}circle_unpack =
<built-in method unpack of Struct object at 0x1136ce0>
data_to_bool =
{'\x01': True, '\x00': False}dddd_unpack =
<built-in method unpack of Struct object at 0x1136d60>
double_pack =
<built-in method pack of Struct object at 0x1136b60>
float_pack =
<built-in method pack of Struct object at 0x1136be0>
longlong_pack =
<built-in method pack of Struct object at 0x11368e0>
lseg_unpack =
<built-in method unpack of Struct object at 0x1136d60>
null_sequence =
'\xff\xff\xff\xff'
point_unpack =
<built-in method unpack of Struct object at 0x1136c60>
short_pack =
<built-in method pack of Struct object at 0x1136a60>
ulong_pack =
<built-in method pack of Struct object at 0x11369e0>
ushort_pack =
<built-in method pack of Struct object at 0x1136ae0>
module postgresql.protocol.typical.stdio
Typical PostgreSQL type I/O routines I/O maps for converting wire data into standard Python objects.
functions
def timestamp_pack(x):
Create a (seconds, microseconds) pair from a `datetime.datetime` instance.
def timestamp_unpack(seconds):
Create a `datetime.datetime` instance from a (seconds, microseconds) pair.
def time_pack(x):
Create a (seconds, microseconds) pair from a `datetime.time` instance.
def time_unpack(seconds_ms):
Create a `datetime.time` instance from a (seconds, microseconds) pair. Seconds being offset from epoch.
def interval_pack(x):
Create a (months, days, (seconds, microseconds)) tuple from a `datetime.timedelta` instance.
def interval_unpack(mds):
Given a (months, days, (seconds, microseconds)) tuple, create a `datetime.timedelta` instance.
def timetz_pack(x):
Create a ((seconds, microseconds), timezone) tuple from a `datetime.time` instance.
def timetz_unpack(tstz):
Create a `datetime.time` instance from a ((seconds, microseconds), timezone) tuple.
def circle_unpack(x):
Given raw circle data, (x, y, radius), make a circle instance using `postgresql.types.circle`.
def cidr_unpack(x):
Given serialized cidr data, make a `postgresql.types.cidr` instance.
def two_pair(x):
Make a pair of pairs out of a sequence of four objects
data
pg_date_offset =
730120
pg_epoch_date =
datetime.date(2000, 1, 1)
pg_epoch_datetime =
datetime.datetime(2000, 1, 1, 0, 0)
pg_time_days =
10957