jina.types.querylang.queryset.dunderkey

Originally from https://github.com/naiquevin/lookupy

The library is provided as-is under the MIT License

Copyright (c) 2013 Vineet Naik (naikvin@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

jina.types.querylang.queryset.dunderkey.dunderkey(*args)[source]

Produces a nested key from multiple args separated by double underscore

>>> dunderkey('a', 'b', 'c')
>>> 'a__b__c'
Parameters

args (str) – the multiple strings

Return type

str

Return:

the joined key

jina.types.querylang.queryset.dunderkey.dunder_partition(key)[source]

Split a dunderkey into 2 parts.

The first part is everything before the final double underscore The second part is after the final double underscore

>>> dunder_partition('a__b__c')
>>> ('a__b', 'c')

:param key : the dunder string :rtype: Tuple[str, Optional[str]] :return: the two parts

jina.types.querylang.queryset.dunderkey.dunder_init(key)[source]

Returns the initial part of the dunder key

>>> dunder_init('a__b__c')
>>> 'a__b'

:param key : the dunder string :rtype: str :return: the first part (None if invalid dunder str)

jina.types.querylang.queryset.dunderkey.dunder_last(key)[source]

Returns the last part of the dunder key

>>> dunder_last('a__b__c')
>>> 'c'

:param key : the dunder string :rtype: Optional[str] :return: the last part (None if invalid dunder string)

jina.types.querylang.queryset.dunderkey.dunder_get(_dict, key)[source]

Returns value for a specified dunderkey

A “dunderkey” is just a fieldname that may or may not contain double underscores (dunderscores!) for referencing nested keys in a dict. eg:

>>> data = {'a': {'b': 1}}
>>> dunder_get(data, 'a__b')
1

key ‘b’ can be referrenced as ‘a__b’

:param _dict : (dict, list, struct or object) which we want to index into :param key : (str) that represents a first level or nested key in the dict :rtype: Any :return: (mixed) value corresponding to the key

jina.types.querylang.queryset.dunderkey.undunder_keys(_dict)[source]

Returns dict with the dunder keys converted back to nested dicts

eg:

>>> undunder_keys({'a': 'hello', 'b__c': 'world'})
{'a': 'hello', 'b': {'c': 'world'}}

:param _dict : (dict) flat dict :rtype: Dict :return: (dict) nested dict

jina.types.querylang.queryset.dunderkey.dunder_truncate(_dict)[source]

Returns dict with dunder keys truncated to only the last part

In other words, replaces the dunder keys with just last part of it. In case many identical last parts are encountered, they are not truncated further

eg:

>>> dunder_truncate({'a__p': 3, 'b__c': 'no'})
{'c': 'no', 'p': 3}
>>> dunder_truncate({'a__p': 'yay', 'b__p': 'no', 'c__z': 'dunno'})
{'a__p': 'yay', 'b__p': 'no', 'z': 'dunno'}

:param _dict : (dict) to flatten :rtype: Dict :return: (dict) flattened result