hirokiky/dripper

Adding dot notation?

Closed this issue · 2 comments

I've build much the same code in http://janschulz.github.io/working_with_dicts.html and one thing I like about my way is making it work with "dot notation" like field1.field2.name (e.g. ("field1","field2","name") as declaration in the dripper way) . Would you accept a patch to make the ValueDripper accept such a declaration?

The patch is mostly:

class ValueDripper(object):
    [...]
    def __call__(self, converting):
        try:
            dug_in = dig_in(converting, self.source_root)
        except DrippingError:
            if self.default is not None:
                return self.default
            else:
                if not (len(self.source_root) == 1 and "." in self.source_root):
                    raise
                source = self.source_root.split(".")
                def convert_int(x):
                    try:
                        return int(x)
                    except:
                        return x
                source = [convert_int(x) for x in source]
                dug_in = dig_in(converting, source)
        [...]

Another way could be a convert_dot kwarg in dripper_factory (would probably be faster as the conversion is cached and also in the default path...).

Thank you! its really nice proposal. 👍

we neee some tests.
and I think dig_in function shoud have this processes.

Tests are no problem :-)

Regarding where it is:

The deeper it is, the more hard it is to set the behavior to one "mode": declarations which failed might work afterwards (aka if there is a data structure with both 'some.key':'...' and {"some":{"key": ...}}

The deeper it is, the worse it is to cache the right "path", which might result in a lot more computations, depending on your data structure:

  • in dripper_factory, the transformation is run once (if '.' in declaration and convert_dot: declaration = ...) and not runtime overhead is generated.
  • in ValueDripper it can be cached but it is still a "is the original key available in the structure" lookup for each call
  • in dig_in it can't be cached and will run for every dig of that key