當前位置: 首頁>>代碼示例>>Python>>正文


Python jsonmerge.merge方法代碼示例

本文整理匯總了Python中jsonmerge.merge方法的典型用法代碼示例。如果您正苦於以下問題:Python jsonmerge.merge方法的具體用法?Python jsonmerge.merge怎麽用?Python jsonmerge.merge使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在jsonmerge的用法示例。


在下文中一共展示了jsonmerge.merge方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_version_meta

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_version_meta(self):

        schema = {'mergeStrategy': 'version'}

        merger = jsonmerge.Merger(schema)

        base = None
        base = merger.merge(base, "a", merge_options={
                'version': {'metadata': {'uri': 'http://example.com/a'}}})
        base = merger.merge(base, "b", merge_options={
                'version': {'metadata': {'uri': 'http://example.com/b'}}})

        self.assertEqual(base, [
            {'value': "a",
             'uri': 'http://example.com/a'},
            {'value': "b",
             'uri': 'http://example.com/b'}]) 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:19,代碼來源:test_jsonmerge.py

示例2: test_internal_refs

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_internal_refs(self):

        schema = {
            'id': 'http://example.com/schema_1.json',
            'properties': {
                'a': {'$ref': "#/definitions/a"},
            },
            'definitions': {
                "a": {
                    "properties": {
                        "b": {'mergeStrategy': 'version'},
                    }
                },
            }
        }

        merger = jsonmerge.Merger(schema)

        base = None
        base = merger.merge(base, {"a": {"b": "c"}})
        base = merger.merge(base, {"a": {"b": "d"}})

        self.assertEqual(base, {"a": {"b": [{"value": "c"}, {"value": "d"}]}}) 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:25,代碼來源:test_jsonmerge.py

示例3: test_oneof_overwrite_toplevel

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_oneof_overwrite_toplevel(self):
        schema = {
            'mergeStrategy': 'overwrite',
            'oneOf': [
                {
                    'type': 'array'
                },
                {
                    'type': 'string'
                },
            ]
        }

        merger = jsonmerge.Merger(schema)

        self.assertEqual(merger.merge([2, 3, 4], 'a'), 'a')
        self.assertEqual(merger.merge('a', [2, 3, 4]), [2, 3, 4]) 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:19,代碼來源:test_jsonmerge.py

示例4: test_anyof_overwrite_toplevel

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_anyof_overwrite_toplevel(self):
        schema = {
            'mergeStrategy': 'overwrite',
            'anyOf': [
                {
                    'type': 'array'
                },
                {
                    'type': 'string'
                },
            ]
        }

        merger = jsonmerge.Merger(schema)

        self.assertEqual(merger.merge([2, 3, 4], 'a'), 'a')
        self.assertEqual(merger.merge('a', [2, 3, 4]), [2, 3, 4]) 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:19,代碼來源:test_jsonmerge.py

示例5: test_custom_strategy

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_custom_strategy(self):

        schema = {'mergeStrategy': 'myStrategy'}

        class MyStrategy(jsonmerge.strategies.Strategy):
            def merge(self, walk, base, head, schema, meta, **kwargs):
                if base is None:
                    ref = ""
                else:
                    ref = base.ref

                return JSONValue("foo", ref)

        merger = jsonmerge.Merger(schema=schema,
                                  strategies={'myStrategy': MyStrategy()})

        base = None
        base = merger.merge(base, {'a': 1})

        self.assertEqual(base, "foo") 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:22,代碼來源:test_jsonmerge.py

示例6: test_merge_by_id_no_key

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_merge_by_id_no_key(self):
        schema = {
            "mergeStrategy": "arrayMergeById",
        }

        a = [
            {"id": "A", "field": 1},
        ]

        b = [
            {'field': 2}
        ]

        merger = jsonmerge.Merger(schema)

        base = None
        base = merger.merge(base, a)
        base = merger.merge(base, b)

        # it should ignore array elements that do not have the id
        self.assertEqual(base, a) 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:23,代碼來源:test_jsonmerge.py

示例7: test_merge_by_id_only_integers

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_merge_by_id_only_integers(self):

        # arrayMergeById strategy can be used to treat simple arrays of
        # integers as Python sets by setting idRef to root (i.e. pointing to
        # the array element itself)
        #
        # https://github.com/avian2/jsonmerge/issues/24

        schema = {
            "mergeStrategy": "arrayMergeById",
            "mergeOptions": {"idRef": "/"},
        }

        base = [ 1, 2 ]
        head = [ 2, 3 ]

        expected = [ 1, 2, 3]

        merger = jsonmerge.Merger(schema)

        base = merger.merge(base, head)

        self.assertEqual(base, expected) 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:25,代碼來源:test_jsonmerge.py

示例8: test_merge_by_id_non_unique_base

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_merge_by_id_non_unique_base(self):
        schema = {
            "mergeStrategy": "arrayMergeById",
        }

        base = [
            {'id': 'a'},
            {'id': 'a'}
        ]

        head = [
            {'id': 'a',
             'foo': 1}
        ]

        merger = jsonmerge.Merger(schema)

        with self.assertRaises(BaseInstanceError) as cm:
            merger.merge(base, head)

        self.assertEqual(cm.exception.value.ref, '#/1') 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:23,代碼來源:test_jsonmerge.py

示例9: test_append_with_unique

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_append_with_unique(self):

        schema = {
            "mergeStrategy": "append",
            "uniqueItems": True,
        }

        merger = jsonmerge.Merger(schema)

        head = ["a"]
        base = None

        base = merger.merge(base, head)
        base = merger.merge(base, head)

        schema2 = merger.get_schema()

        jsonschema.validate(head, schema2)
        jsonschema.validate(base, schema2) 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:21,代碼來源:test_jsonmerge.py

示例10: test_discard_null_keep

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_discard_null_keep(self):

        schema = {
                'properties': {
                    'a': {
                        'mergeStrategy': 'discard',
                        'mergeOptions': {
                            'keepIfUndef': True
                        }
                    }
                } }

        base = {}
        head = {'a': 1}

        base = jsonmerge.merge(base, head, schema)
        self.assertEqual(base, {'a': 1})

        head = {'a': 2}

        base = jsonmerge.merge(base, head, schema)
        self.assertEqual(base, {'a': 1}) 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:24,代碼來源:test_jsonmerge.py

示例11: test_merge_by_index_name_in_exception

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def test_merge_by_index_name_in_exception(self):
        schema = {
            'properties': {
                'a': {
                    'mergeStrategy': 'arrayMergeByIndex'
                }
            }
        }

        head = {'a': {}}
        base = {'a': []}

        merger = jsonmerge.Merger(schema)
        with self.assertRaises(HeadInstanceError) as cm:
            merger.merge(base, head)

        self.assertIn('arrayMergeByIndex', str(cm.exception)) 
開發者ID:avian2,項目名稱:jsonmerge,代碼行數:19,代碼來源:test_jsonmerge.py

示例12: restoreConfig

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def restoreConfig(ldifFolder, newLdif, ldifModFolder):
    logging.info('Comparing old LDAP data and creating `modify` files.')
    ignoreList = ['objectClass', 'ou', 'oxAuthJwks', 'oxAuthConfWebKeys']
    current_config_dns = getDns(newLdif)
    oldDnMap = getOldEntryMap(ldifFolder)
    for dn in oldDnMap.keys():
        old_entry = getEntry("%s/%s" % (ldifFolder, oldDnMap[dn]), dn)
        if dn not in current_config_dns:
            addEntry(dn, old_entry, ldifModFolder)
            continue
        new_entry = getEntry(newLdif, dn)
        for attr in old_entry.keys():
            # Note: Prefixing with part length helps in inserting base
            # before leaf entries in the LDAP
            # Filename = DN_part_length + unique random string
            new_fn = str(len(dn.split(','))) + '_' + str(uuid.uuid4())
            filename = '%s/%s.ldif' % (ldifModFolder, new_fn)

            if attr in ignoreList:
                continue

            if attr not in new_entry:
                writeMod(dn, attr, old_entry[attr], filename, True)
                logging.debug("Adding attr %s to %s", attr, dn)
            elif old_entry[attr] != new_entry[attr]:
                mod_list = None
                if len(old_entry[attr]) == 1:
                    try:
                        logging.debug("Merging json value for %s", attr)
                        old_json = json.loads(old_entry[attr][0])
                        new_json = json.loads(new_entry[attr][0])
                        new_json = merge(new_json, old_json)
                        mod_list = [json.dumps(new_json)]
                    except:
                        mod_list = old_entry[attr]
                        logging.debug("Keeping old value for %s", attr)
                else:
                    mod_list = old_entry[attr]
                    logging.debug("Keeping multiple old value for %s", attr)
                writeMod(dn, attr, mod_list, filename) 
開發者ID:GluuFederation,項目名稱:community-edition-setup,代碼行數:42,代碼來源:import24.py

示例13: restoreConfig

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def restoreConfig(ldifFolder, newLdif, ldifModFolder):
    ignoreList = ['objectClass', 'ou']
    current_config_dns = getDns(newLdif)
    oldDnMap = getOldEntryMap(ldifFolder)
    for dn in oldDnMap.keys():
        old_entry = getEntry("%s/%s" % (ldifFolder, oldDnMap[dn]), dn)
        if dn not in current_config_dns:
            addEntry(dn, old_entry, ldifModFolder)
            continue
        new_entry = getEntry(newLdif, dn)
        for attr in old_entry.keys():
            if attr in ignoreList:
                continue
            if not new_entry.has_key(attr):
                writeMod(dn, attr, old_entry[attr], '%s/%s.ldif' % (ldifModFolder, str(uuid.uuid4())), True)
                logIt("Adding attr %s to %s" % (attr, dn))
            else:
                mod_list = None
                if old_entry[attr] != new_entry[attr]:
                    if len(old_entry[attr]) == 1:
                        try:
                            logIt("Merging json value for %s " % attr)
                            old_json = json.loads(old_entry[attr][0])
                            new_json = json.loads(new_entry[attr][0])
                            new_json = merge(new_json, old_json)
                            mod_list = [json.dumps(new_json)]
                        except:
                            mod_list = old_entry[attr]
                    else:
                        mod_list = old_entry[attr]
                        logIt("Keeping multiple old values for %s" % attr)
                else:
                    continue
                writeMod(dn, attr, mod_list, '%s/%s.ldif' % (ldifModFolder, str(uuid.uuid4()))) 
開發者ID:GluuFederation,項目名稱:community-edition-setup,代碼行數:36,代碼來源:import23.py

示例14: record_histogram

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def record_histogram(self, key, val, name=None):
        if str(key) not in self._histogram_ds:
            with tf.name_scope(name, "record_histogram"):
                self._histogram_ds[str(key)] = tf.Variable(val)
            self._histogram_summary_op.append(
                tf.summary.histogram(str(key), self._histogram_ds[str(key)]))
            self._histogram_summary_op_merge = tf.summary.merge(
                self._histogram_summary_op)

        self._feed[self._histogram_ds[str(key)]] = val 
開發者ID:rlworkgroup,項目名稱:gym-sawyer,代碼行數:12,代碼來源:tensorboard_output.py

示例15: record_histogram_by_type

# 需要導入模塊: import jsonmerge [as 別名]
# 或者: from jsonmerge import merge [as 別名]
def record_histogram_by_type(self,
                                 histogram_type,
                                 key=None,
                                 shape=[1000],
                                 name=None,
                                 **kwargs):
        '''
        distribution type and args:
            normal: mean, stddev
            gamma: alpha
            poisson: lam
            uniform: maxval

        example:
            $ python examples/example_tensorboard_logger.py
        '''
        if histogram_type not in self._histogram_distribute_list:
            raise Exception('histogram type error %s' % histogram_type,
                            'builtin type', self._histogram_distribute_list)

        if str(key) not in self._histogram_ds:
            self._histogram_ds[str(key)] = self._get_histogram_var_by_type(
                histogram_type, shape, name, **kwargs)
            self._histogram_summary_op.append(
                tf.summary.histogram(
                    str(key), self._histogram_ds[str(key)][0]))
            self._histogram_summary_op_merge = tf.summary.merge(
                self._histogram_summary_op)

        key_list = self._histogram_ds[str(key)][1]
        val_list = self._get_histogram_val_by_type(histogram_type, **kwargs)

        for key, val in zip(key_list, val_list):
            self._feed[key] = val 
開發者ID:rlworkgroup,項目名稱:gym-sawyer,代碼行數:36,代碼來源:tensorboard_output.py


注:本文中的jsonmerge.merge方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。