• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python extensions.new_type函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中psycopg2.extensions.new_type函数的典型用法代码示例。如果您正苦于以下问题:Python new_type函数的具体用法?Python new_type怎么用?Python new_type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了new_type函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: register_uuid

    def register_uuid(oids=None, conn_or_curs=None):
        """Create the UUID type and an uuid.UUID adapter."""
        if not oids:
            oid1 = 2950
            oid2 = 2951
        elif type(oids) == list:
            oid1, oid2 = oids
        else:
            oid1 = oids
            oid2 = 2951

        def parseUUIDARRAY(data, cursor):
            if data is None:
                return None
            elif data == "{}":
                return []
            else:
                return [((len(x) > 0 and x != "NULL") and uuid.UUID(x) or None) for x in data[1:-1].split(",")]

        _ext.UUID = _ext.new_type((oid1,), "UUID", lambda data, cursor: data and uuid.UUID(data) or None)
        _ext.UUIDARRAY = _ext.new_type((oid2,), "UUID[]", parseUUIDARRAY)

        _ext.register_type(_ext.UUID, conn_or_curs)
        _ext.register_type(_ext.UUIDARRAY, conn_or_curs)
        _ext.register_adapter(uuid.UUID, UUID_adapter)

        return _ext.UUID
开发者ID:pombredanne,项目名称:Vixen,代码行数:27,代码来源:extras.py


示例2: _make_casters

def _make_casters():
    inet = new_type((869,), 'INET', cast_interface)
    ainet = new_array_type((1041,), 'INET[]', inet)

    cidr = new_type((650,), 'CIDR', cast_network)
    acidr = new_array_type((651,), 'CIDR[]', cidr)

    return [inet, ainet, cidr, acidr]
开发者ID:Oakafee,项目名称:Interpretation-of-New-Jersey,代码行数:8,代码来源:_ipaddress.py


示例3: register_inet

def register_inet(oid=None, conn_or_curs=None):
    """Create the INET type and an Inet adapter."""
    if not oid:
        oid = 869
    _ext.INET = _ext.new_type((oid,), "INET", lambda data, cursor: data and Inet(data) or None)
    _ext.register_type(_ext.INET, conn_or_curs)
    return _ext.INET
开发者ID:pombredanne,项目名称:Vixen,代码行数:7,代码来源:extras.py


示例4: register_extensions

def register_extensions(con):
    pgext.register_adapter(Polygon, adapt_wkt)
    pgext.register_adapter(Point, adapt_wkt)
    pgext.register_adapter(LineString, adapt_wkt)

    with con.cursor() as c:
        c.execute('select null::point, null::polygon, null::geometry')
        point_oid, poly_oid = c.description[0][1], c.description[1][1]
        geom_oid = c.description[2][1]

    point = pgext.new_type((point_oid,), 'POINT', cast_wkb)
    polygon = pgext.new_type((poly_oid,), 'POLYGON', cast_wkb)
    geom = pgext.new_type((geom_oid,), 'GEOMETRY', cast_wkb)
    pgext.register_type(point)
    pgext.register_type(polygon)
    pgext.register_type(geom)
开发者ID:GeograpHack,项目名称:afg,代码行数:16,代码来源:geo.py


示例5: register_ltree

def register_ltree(conn):
    oid = _get_ltree_oids(conn)
    if not oid[0]:
        return False
    else:
        array_oid = oid[1]
        oid = oid[0]

    if isinstance(oid, int):
        oid = (oid,)

    if array_oid is not None:
        if isinstance(array_oid, int):
            array_oid = (array_oid,)
        else:
            array_oid = tuple([x for x in array_oid if x])

    ltree = extensions.new_type(oid, "LTREE", _cast_fn)
    extensions.register_type(ltree, None)

    if array_oid:
        ltree_array = extensions.new_array_type(array_oid, "LTREEARRAY", ltree)
        extensions.register_type(ltree_array, None)

    return True
开发者ID:MaxPresman,项目名称:peewee,代码行数:25,代码来源:postgres_ext.py


示例6: register_ltree

def register_ltree(conn_or_curs, globally=False, oid=None, array_oid=None):
    """Register the ltree adapter and typecaster on the connection or cursor.
    """
    register_adapter()

    conn, curs, conn_or_curs = _solve_conn_curs(conn_or_curs)

    if oid is None:
        oid = get_oids(conn_or_curs, 'ltree')
        if oid is None or not oid[0]:
            raise psycopg2.ProgrammingError(
                "ltree type not found in the database."
            )
        else:
            array_oid = oid[1]
            oid = oid[0]

    if isinstance(oid, int):
        oid = (oid,)

    if array_oid is not None:
        if isinstance(array_oid, int):
            array_oid = (array_oid,)
        else:
            array_oid = tuple([x for x in array_oid if x])

    # create and register the typecaster
    LTREE = ext.new_type(oid, "LTREE", cast_ltree)
    ext.register_type(LTREE, not globally and conn_or_curs or None)

    if array_oid:
        LTREEARRAY = ext.new_array_type(array_oid, "LTREEARRAY", LTREE)
        ext.register_type(LTREEARRAY, not globally and conn_or_curs or None)
开发者ID:dvarrazzo,项目名称:py-ltree,代码行数:33,代码来源:pg.py


示例7: register_cidr

def register_cidr(oid=None, conn_or_curs=None):
    """Create the CIDR type and an Cidr adapter.

    :param oid: oid for the PostgreSQL :sql:`cidr` type, or 2-items sequence
        with oids of the type and the array. If not specified, use PostgreSQL
        standard oids.
    :param conn_or_curs: where to register the typecaster. If not specified,
        register it globally.
    """
    if not oid:
        oid1 = 650
        oid2 = 651
    elif isinstance(oid, (list, tuple)):
        oid1, oid2 = oid
    else:
        oid1 = oid
        oid2 = 651

    _ext.CIDR = _ext.new_type((oid1, ), "CIDR",
            lambda data, cursor: data and Cidr(data) or None)
    _ext.CIDRARRAY = _ext.new_array_type((oid2, ), "CIDRARRAY", _ext.CIDR)

    _ext.register_type(_ext.CIDR, conn_or_curs)
    _ext.register_type(_ext.CIDRARRAY, conn_or_curs)

    return _ext.CIDR
开发者ID:trentclainor,项目名称:psycopg2,代码行数:26,代码来源:extras.py


示例8: register_uuid

def register_uuid(oids=None, conn_or_curs=None):
    """Create the UUID type and an uuid.UUID adapter.

    :param oids: oid for the PostgreSQL :sql:`uuid` type, or 2-items sequence
        with oids of the type and the array. If not specified, use PostgreSQL
        standard oids.
    :param conn_or_curs: where to register the typecaster. If not specified,
        register it globally.
    """

    import uuid

    if not oids:
        oid1 = 2950
        oid2 = 2951
    elif isinstance(oids, (list, tuple)):
        oid1, oid2 = oids
    else:
        oid1 = oids
        oid2 = 2951

    _ext.UUID = _ext.new_type((oid1, ), "UUID",
            lambda data, cursor: data and uuid.UUID(data) or None)
    _ext.UUIDARRAY = _ext.new_array_type((oid2,), "UUID[]", _ext.UUID)

    _ext.register_type(_ext.UUID, conn_or_curs)
    _ext.register_type(_ext.UUIDARRAY, conn_or_curs)
    _ext.register_adapter(uuid.UUID, UUID_adapter)

    return _ext.UUID
开发者ID:gopal-neosoft,项目名称:Assignment,代码行数:30,代码来源:extras.py


示例9: register_tstz_w_secs

def register_tstz_w_secs(oids=None, conn_or_curs=None):
    """Register alternate type caster for TIMESTAMP WITH TIME ZONE.

    The Python datetime module cannot handle time zones with
    seconds in the UTC offset. There are, however, historical
    "time zones" which contain such offsets, eg. "Asia/Calcutta".
    In many cases those offsets represent true local time.

    If you encounter "unable to parse time" on a perfectly valid
    timestamp you likely want to try this type caster. It truncates
    the seconds from the time zone data and retries casting
    the timestamp. Note that this will generate timestamps
    which are INACCURATE by the number of seconds truncated
    (unless the seconds were 00).

    <oids>
            which OIDs to use this type caster for,
            defaults to TIMESTAMP WITH TIME ZONE
    <conn_or_curs>
            a cursor or connection if you want to attach
            this type caster to that only, defaults to
            None meaning all connections and cursors
    """
    if oids is None:
        oids = (1184,)  # hardcoded from PostgreSQL headers

    _ext.TSTZ_W_SECS = _ext.new_type(oids, "TSTZ_W_SECS", _convert_tstz_w_secs)
    _ext.register_type(TSTZ_W_SECS, conn_or_curs)

    return _ext.TSTZ_W_SECS
开发者ID:pombredanne,项目名称:Vixen,代码行数:30,代码来源:extras.py


示例10: register_inet

def register_inet(oid=None, conn_or_curs=None):
    """Create the INET type and an Inet adapter.

    :param oid: oid for the PostgreSQL :sql:`inet` type, or 2-items sequence
        with oids of the type and the array. If not specified, use PostgreSQL
        standard oids.
    :param conn_or_curs: where to register the typecaster. If not specified,
        register it globally.
    """
    import warnings
    warnings.warn(
        "the inet adapter is deprecated, it's not very useful",
        DeprecationWarning)

    if not oid:
        oid1 = 869
        oid2 = 1041
    elif isinstance(oid, (list, tuple)):
        oid1, oid2 = oid
    else:
        oid1 = oid
        oid2 = 1041

    _ext.INET = _ext.new_type((oid1, ), "INET",
            lambda data, cursor: data and Inet(data) or None)
    _ext.INETARRAY = _ext.new_array_type((oid2, ), "INETARRAY", _ext.INET)

    _ext.register_type(_ext.INET, conn_or_curs)
    _ext.register_type(_ext.INETARRAY, conn_or_curs)

    return _ext.INET
开发者ID:gopal-neosoft,项目名称:Assignment,代码行数:31,代码来源:extras.py


示例11: __init__

    def __init__(self, name, oid, attrs):
        self.name = name
        self.oid = oid

        self.attnames = [ a[0] for a in attrs ]
        self.atttypes = [ a[1] for a in attrs ]
        self._create_type(name, self.attnames)
        self.typecaster = _ext.new_type((oid,), name, self.parse)
开发者ID:erochest,项目名称:sshPostGIS,代码行数:8,代码来源:extras.py


示例12: _register_inet

def _register_inet(oid=None, conn_or_curs=None):
    """Create the INET type and an Inet adapter."""
    from psycopg2 import extensions as _ext
    if not oid: oid = 869
    _ext.INET = _ext.new_type((oid, ), "INET",
            lambda data, cursor: data and Inet(data) or None)
    _ext.register_type(_ext.INET, conn_or_curs)
    return _ext.INET
开发者ID:Cougar,项目名称:NIPAP,代码行数:8,代码来源:convert.py


示例13: register_cast

    def register_cast(cls, connection):
        cast_function = CAST_MAPPER[cls.type_name()]
        cursor = connection.cursor()
        cursor.execute(cls.sql_for_oid())
        oid = cursor.description[0][1]
        cursor.close()

        PGTYPE = new_type((oid,), cls.type_name().upper(), cast_function)
        register_type(PGTYPE)
开发者ID:berrytj,项目名称:django-orm-extensions,代码行数:9,代码来源:objects.py


示例14: register_range_caster

def register_range_caster(pgrange, pyrange, oid, subtype_oid, array_oid, scope=None):
    # Create an adapter for this range type
    adapter = partial(cast_raw_range_string, pyrange, subtype_oid=subtype_oid)

    # Create types using adapter
    range_type = new_type((oid,), pgrange, adapter)
    range_array_type = new_array_type((array_oid,), pgrange, range_type)

    register_type(range_type, scope)
    register_type(range_array_type, scope)
开发者ID:runfalk,项目名称:psycospans,代码行数:10,代码来源:_utils.py


示例15: register_citext_type

def register_citext_type(dbapi_con, connection_record):
    def cast_citext(in_str, cursor):
        if in_str == None:
            return None
        return unicode(in_str, cursor.connection.encoding)
    with closing(dbapi_con.cursor()) as c:
        c.execute(b"SELECT pg_type.oid FROM pg_type WHERE typname = 'citext'")
        citext_oid = c.fetchone()
        if citext_oid != None:
            citext_type = new_type(citext_oid, b"CITEXT", cast_citext)
            register_type(citext_type)
开发者ID:themylogin,项目名称:last.fm.thelogin.ru,代码行数:11,代码来源:db.py


示例16: __init__

    def __init__(self, pgrange, pyrange, oid, subtype_oid, array_oid=None):
        self.subtype_oid = subtype_oid
        self._create_ranges(pgrange, pyrange)

        name = self.adapter.name or self.adapter.__class__.__name__

        self.typecaster = new_type((oid,), name, self.parse)

        if array_oid is not None:
            self.array_typecaster = new_array_type((array_oid,), name + "ARRAY", self.typecaster)
        else:
            self.array_typecaster = None
开发者ID:psycopg,项目名称:psycopg2,代码行数:12,代码来源:_range.py


示例17: register_macaddr_type

def register_macaddr_type():
    from psycopg2.extensions import register_adapter, new_type, register_type, new_array_type
    import psycopg2

    oid = get_type_oid("NULL::macaddr")
    PGTYPE = new_type((oid,), "macaddr", cast_macaddr)
    register_type(PGTYPE)
    register_adapter(MacAddr, adapt_macaddr)

    mac_array_oid = get_type_oid("'{}'::macaddr[]")
    array_of_mac = new_array_type((mac_array_oid, ), 'macaddr', psycopg2.STRING)
    psycopg2.extensions.register_type(array_of_mac)
开发者ID:nwp90,项目名称:djorm-ext-pgarray,代码行数:12,代码来源:tests.py


示例18: register_hstore

def register_hstore(conn_or_curs, globally=False, unicode=False, oid=None):
    """Register adapter and typecaster for `!dict`\-\ |hstore| conversions.

    :param conn_or_curs: a connection or cursor: the typecaster will be
        registered only on this object unless *globally* is set to `!True`
    :param globally: register the adapter globally, not only on *conn_or_curs*
    :param unicode: if `!True`, keys and values returned from the database
        will be `!unicode` instead of `!str`. The option is not available on
        Python 3
    :param oid: the OID of the |hstore| type if known. If not, it will be
        queried on *conn_or_curs*

    The connection or cursor passed to the function will be used to query the
    database and look for the OID of the |hstore| type (which may be different
    across databases). If querying is not desirable (e.g. with
    :ref:`asynchronous connections <async-support>`) you may specify it in the
    *oid* parameter (it can be found using a query such as :sql:`SELECT
    'hstore'::regtype::oid;`).

    Note that, when passing a dictionary from Python to the database, both
    strings and unicode keys and values are supported. Dictionaries returned
    from the database have keys/values according to the *unicode* parameter.

    The |hstore| contrib module must be already installed in the database
    (executing the ``hstore.sql`` script in your ``contrib`` directory).
    Raise `~psycopg2.ProgrammingError` if the type is not found.

    .. versionchanged:: 2.4
        added the *oid* parameter. If not specified, the typecaster is
        installed also if |hstore| is not installed in the :sql:`public`
        schema.
    """
    if oid is None:
        oid = HstoreAdapter.get_oids(conn_or_curs)
        if oid is None or not oid[0]:
            raise psycopg2.ProgrammingError(
                "hstore type not found in the database. "
                "please install it from your 'contrib/hstore.sql' file")
        else:
            oid = oid[0]  # for the moment we don't have a HSTOREARRAY

    if isinstance(oid, int):
        oid = (oid,)

    # create and register the typecaster
    if sys.version_info[0] < 3 and unicode:
        cast = HstoreAdapter.parse_unicode
    else:
        cast = HstoreAdapter.parse

    HSTORE = _ext.new_type(oid, "HSTORE", cast)
    _ext.register_type(HSTORE, not globally and conn_or_curs or None)
    _ext.register_adapter(dict, HstoreAdapter)
开发者ID:erochest,项目名称:sshPostGIS,代码行数:53,代码来源:extras.py


示例19: register_bitstring_types

def register_bitstring_types(connection):
    """Register the BIT and VARBIT casters on the provided connection.

    This ensures that BIT and VARBIT instances returned from the database will
    be represented in Python as ``bitstring.Bits`` instances.
    """
    with closing(connection.cursor()) as cur:
        cur.execute("SELECT NULL::BIT")
        bit_oid = cur.description[0].type_code
        cur.execute("SELECT NULL::VARBIT")
        varbit_oid = cur.description[0].type_code
    bit_caster = ext.new_type((bit_oid, varbit_oid), 'BIT', cast_bits)
开发者ID:SergeyKubrak,项目名称:django-postgres,代码行数:12,代码来源:bitstrings.py


示例20: _bind_type

def _bind_type(conn, sql_type_name, cast):
    """
    Binds a `cast` function to the SQL type in the connection `conn`
    given by `sql_type_name`. `cast` must be a function with two
    parameters: the SQL value and a cursor object. It should return the
    appropriate Python object.

    Note that `sql_type_name` is not escaped.
    """
    with Tx(conn) as c:
        c.execute('SELECT NULL::%s' % sql_type_name)
        typ = new_type((c.description[0].type_code,), sql_type_name, cast)
        register_type(typ)
开发者ID:alphaManimal,项目名称:nfldb,代码行数:13,代码来源:db.py



注:本文中的psycopg2.extensions.new_type函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python extensions.register_adapter函数代码示例发布时间:2022-05-25
下一篇:
Python extensions.new_array_type函数代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap