Code Monkey home page Code Monkey logo

Comments (2)

yoyonel avatar yoyonel commented on August 15, 2024 1

A simpler (better) approach with simple custom type to indicate dotted ignore key: feature/Issue#107_oo

Custom type in utils.py:

class DottedIgnoreKey(str):
    """Custom type to specify dotted ignore key.

    This custom type help to desactivate dotted interpretation.
    """

    pass

Using in diff function in init.py:

def _process_ignore_value(value):
            if isinstance(value, int):
                return value,
            elif isinstance(value, list):
                return tuple(value)
            elif isinstance(value, DottedIgnoreKey):
                return str(value),
            return value

and finally in unit test test_dictdiffer.py:

def test_ignore_dotted_ignore_key(self):
        key_to_ignore = u'nifi.zookeeper.session.timeout'
        config_dict = OrderedDict(
            [('address', 'devops011-slv-01.gvs.ggn'),
             (key_to_ignore, '3 secs')])

        ref_dict = OrderedDict(
            [('address', 'devops011-slv-01.gvs.ggn'),
             (key_to_ignore, '4 secs')])

        assert len(
            list(diff(config_dict, ref_dict,
                      ignore=[DottedIgnoreKey(key_to_ignore)]))) == 0

Work fine with minimal modifications impact in code base :-D

from dictdiffer.

yoyonel avatar yoyonel commented on August 15, 2024

I write a fix/patch to workaround this problem: feature/Issue#107

My solution is to provide a function to generate raw ignored keys.

I define a RAW_IGNORE_KEY_TOKEN to generate (prefix) raw ignored keys (in utils.py).
I use this functionnality in diff function for updating elements in ignore list keys:

if isinstance(ignore, Iterable):
        def _unmake_raw_ignore_key(key):
            return key[len(RAW_IGNORE_KEY_TOKEN):]

        def _is_raw_ignore_key(key):
            return key.startswith(RAW_IGNORE_KEY_TOKEN)

        def _process_ignore_value(value):
            if isinstance(value, int):
                return value,
            elif isinstance(value, list):
                return tuple(value)
            elif isinstance(value, string_types) and _is_raw_ignore_key(value):
                return _unmake_raw_ignore_key(value),
            return value

        ignore = type(ignore)(_process_ignore_value(value) for value in ignore)

Usage in unittest:

def test_ignore_dotted_key_disable(self):
        key_to_ignore = u'nifi.zookeeper.session.timeout'
        config_dict = OrderedDict(
            [('address', 'devops011-slv-01.gvs.ggn'),
             (key_to_ignore, '3 secs')])

        ref_dict = OrderedDict(
            [('address', 'devops011-slv-01.gvs.ggn'),
             (key_to_ignore, '4 secs')])

        assert len(
            list(diff(config_dict, ref_dict,
                      ignore=[make_raw_ignore_key(key_to_ignore)]))) == 0

@jirikuncar Sound good for you ? (if yes, i can PR my branch)

from dictdiffer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.