当前位置: 首页>>代码示例>>Python>>正文


Python extras.Json方法代码示例

本文整理汇总了Python中psycopg2.extras.Json方法的典型用法代码示例。如果您正苦于以下问题:Python extras.Json方法的具体用法?Python extras.Json怎么用?Python extras.Json使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在psycopg2.extras的用法示例。


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

示例1: _serialize_event

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def _serialize_event(self, event):
        metadata = event.metadata
        doc = {'machine_serial_number': metadata.machine_serial_number,
               'event_type': event.event_type,
               'uuid': metadata.uuid,
               'index': metadata.index,
               'created_at': metadata.created_at}
        if metadata.request is not None:
            doc['user_agent'] = metadata.request.user_agent
            doc['ip'] = metadata.request.ip
            user = metadata.request.user
            if user:
                doc['user'] = Json(user.serialize())
        else:
            doc['user_agent'] = None
            doc['ip'] = None
            doc['user'] = None
        doc['payload'] = Json(event.payload)
        return doc 
开发者ID:zentralopensource,项目名称:zentral,代码行数:21,代码来源:postgres.py

示例2: test_adapt_subclass

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def test_adapt_subclass(self):
        from psycopg2.extras import json, Json

        class DecimalEncoder(json.JSONEncoder):
            def default(self, obj):
                if isinstance(obj, Decimal):
                    return float(obj)
                return json.JSONEncoder.default(self, obj)

        class MyJson(Json):
            def dumps(self, obj):
                return json.dumps(obj, cls=DecimalEncoder)

        curs = self.conn.cursor()
        obj = Decimal('123.45')
        self.assertEqual(curs.mogrify("%s", (MyJson(obj),)),
            b("'123.45'")) 
开发者ID:synthetichealth,项目名称:syntheticmass,代码行数:19,代码来源:test_types_extras.py

示例3: test_json

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def test_json(make_connection):
    conn = await make_connection()
    data = {'a': 1, 'b': 'str'}
    cur = await conn.cursor()
    try:
        await cur.execute("DROP TABLE IF EXISTS tbl")
        await cur.execute("""CREATE TABLE tbl (
                              id SERIAL,
                              val JSON)""")
        await cur.execute(
            "INSERT INTO tbl (val) VALUES (%s)", [Json(data)])
        await cur.execute("SELECT * FROM tbl")
        item = await cur.fetchone()
        assert (1, {'b': 'str', 'a': 1}) == item
    finally:
        cur.close() 
开发者ID:aio-libs,项目名称:aiopg,代码行数:18,代码来源:test_extended_types.py

示例4: contains

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def contains(self, other):
        clone = self.as_json(True)
        if isinstance(other, (list, dict)):
            return Expression(clone, OP.JSONB_CONTAINS, Json(other))
        return Expression(clone, OP.JSONB_EXISTS, other) 
开发者ID:danielecook,项目名称:Quiver-alfred,代码行数:7,代码来源:postgres_ext.py

示例5: __init__

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def __init__(self, dumps=None, *args, **kwargs):
        if Json is None:
            raise Exception('Your version of psycopg2 does not support JSON.')

        self.dumps = dumps
        super(JSONField, self).__init__(*args, **kwargs) 
开发者ID:danielecook,项目名称:Quiver-alfred,代码行数:8,代码来源:postgres_ext.py

示例6: db_value

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def db_value(self, value):
        if value is None:
            return value
        if not isinstance(value, Json):
            return Json(value, dumps=self.dumps)
        return value 
开发者ID:danielecook,项目名称:Quiver-alfred,代码行数:8,代码来源:postgres_ext.py

示例7: contained_by

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def contained_by(self, other):
        return Expression(self, OP.JSONB_CONTAINED_BY, Json(other)) 
开发者ID:danielecook,项目名称:Quiver-alfred,代码行数:4,代码来源:postgres_ext.py

示例8: update_metadata

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def update_metadata(cls, table, column, srid, scale_x, scale_y, scale_z,
                        offset_x, offset_y, offset_z):
        '''
        Add an entry to the lopocs metadata tables to use.
        To be used after a fresh pc table creation.
        '''
        pcid = cls.query("""
            select pcid from pointcloud_columns
            where "schema" = %s and "table" = %s and "column" = %s
            """, (table.split('.')[0], table.split('.')[1], column)
        )[0][0]

        bbox = cls.compute_boundingbox(table, column)
        # compute bbox with offset and scale applied
        bbox_scaled = [0] * 6
        bbox_scaled[0] = (bbox['xmin'] - offset_x) / scale_x
        bbox_scaled[1] = (bbox['ymin'] - offset_y) / scale_y
        bbox_scaled[2] = (bbox['zmin'] - offset_z) / scale_z
        bbox_scaled[3] = (bbox['xmax'] - offset_x) / scale_x
        bbox_scaled[4] = (bbox['ymax'] - offset_y) / scale_y
        bbox_scaled[5] = (bbox['zmax'] - offset_z) / scale_z

        res = cls.query("""
            delete from pointcloud_lopocs where schematable = %s and "column" = %s;
            insert into pointcloud_lopocs (schematable, "column", srid, bbox)
            values (%s, %s, %s, %s) returning id
            """, (table, column, table, column, srid, bbox))
        plid = res[0][0]

        scales = scale_x, scale_y, scale_z
        offsets = offset_x, offset_y, offset_z

        json_schema = cls.patch2greyhoundschema(table, column)

        cls.execute("""
            insert into pointcloud_lopocs_outputs
            (id, pcid, scales, offsets, stored, bbox, point_schema)
            values (%s, %s, %s, %s, True, %s, %s)
        """, (
            plid, pcid, iterable2pgarray(scales), iterable2pgarray(offsets),
            iterable2pgarray(bbox_scaled), Json(json_schema))) 
开发者ID:Oslandia,项目名称:lopocs,代码行数:43,代码来源:database.py

示例9: parse_committee_metadata

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def parse_committee_metadata(committee_metadata):
    id_ = committee_metadata['id']
    state = committee_metadata['state']
    chamber = committee_metadata['chamber']
    committee = committee_metadata['committee']
    subcommittee = committee_metadata['subcommittee']
    if len(committee_metadata['members']) > 0:
        members = Json(committee_metadata['members'][0])
    else: 
        members = None
    sources = committee_metadata['sources'][0]['url']
    parent_id = committee_metadata['parent_id']
    created_at = committee_metadata['created_at']
    updated_at = committee_metadata['updated_at']
    if len(committee_metadata['all_ids']) > 0:
        all_ids = committee_metadata['all_ids'][0]
    else:
        all_ids = None
    if 'level' in committee_metadata:
        level = committee_metadata['level']
    else:
        level = None

    return((id_, state, chamber, committee, subcommittee, members,
        sources, parent_id, created_at, updated_at, all_ids, level))



# GRAB COMMITTEE METADATA FROM FILES AND PUSH TO DATABASE 
开发者ID:dssg,项目名称:policy_diffusion,代码行数:31,代码来源:committee_metadata.py

示例10: get_prep_value

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def get_prep_value(self, value):
        if value is not None:
            return Json(value)
        return value 
开发者ID:drexly,项目名称:openhgsenti,代码行数:6,代码来源:jsonb.py

示例11: get_prep_lookup

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def get_prep_lookup(self, lookup_type, value):
        if lookup_type in ('has_key', 'has_keys', 'has_any_keys'):
            return value
        if isinstance(value, (dict, list)):
            return Json(value)
        return super(JSONField, self).get_prep_lookup(lookup_type, value) 
开发者ID:drexly,项目名称:openhgsenti,代码行数:8,代码来源:jsonb.py

示例12: cleanup_tags

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def cleanup_tags(tags):
        """
        Delete tags because they have low accuracy or because they are in the
        blacklist. If no change is made, return None.
        :return: A SQL fragment if an update is required or None
        """
        update_required = False
        tag_output = []
        if not tags:
            return None
        for tag in tags:
            below_threshold = False
            if 'accuracy' in tag and tag['accuracy'] < TAG_MIN_CONFIDENCE:
                below_threshold = True
            if 'name' in tag:
                lower_tag = tag['name'].lower()
                should_filter = _tag_blacklisted(lower_tag) or below_threshold
            else:
                log.warning(f'Filtering malformed tag "{tag}" in "{tags}"')
                should_filter = True
            if should_filter:
                update_required = True
            else:
                tag_output.append(tag)

        if update_required:
            fragment = Json(tag_output)
            return fragment
        else:
            return None


# Define which tables, sources, and fields require cleanup. Map the field
# to a cleanup function that returns either a cleaned version of the field
# or 'None' to signal that no update is required. 
开发者ID:creativecommons,项目名称:cccatalog-api,代码行数:37,代码来源:cleanup.py

示例13: test_tag_blacklist

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def test_tag_blacklist():
        tags = [
            {
                'name': 'cc0'
            },
            {
                'name': ' cc0'
            },
            {
                'name': 'valid',
                'accuracy': 0.99
            },
            {
                'name': 'valid_no_accuracy'
            },
            {
                'name': 'garbage:=metacrap',
            }
        ]
        result = str(CleanupFunctions.cleanup_tags(tags))
        expected = str(Json([
            {'name': 'valid', 'accuracy': 0.99},
            {'name': 'valid_no_accuracy'}
        ]))

        assert result == expected 
开发者ID:creativecommons,项目名称:cccatalog-api,代码行数:28,代码来源:unit_tests.py

示例14: test_accuracy_filter

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def test_accuracy_filter():
        tags = [
            {
                'name': 'inaccurate',
                'accuracy': 0.5
            },
            {
                'name': 'accurate',
                'accuracy': 0.999
            }
        ]
        result = str(CleanupFunctions.cleanup_tags(tags))
        expected = str(Json([{'name': 'accurate', 'accuracy': 0.999}]))
        assert result == expected 
开发者ID:creativecommons,项目名称:cccatalog-api,代码行数:16,代码来源:unit_tests.py

示例15: test_module_not_available

# 需要导入模块: from psycopg2 import extras [as 别名]
# 或者: from psycopg2.extras import Json [as 别名]
def test_module_not_available(self):
        from psycopg2.extras import Json
        self.assertRaises(ImportError, Json(None).getquoted) 
开发者ID:synthetichealth,项目名称:syntheticmass,代码行数:5,代码来源:test_types_extras.py


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