A module to aggregate config parser (loader/dumper) backends.
>>> fst((0, 1))
0
>>> snd((0, 1))
1
An wrapper function around itertools.groupby
Parameters: |
|
---|
>>> itr = [("a", 1), ("b", -1), ("c", 1)]
>>> res = groupby_key(itr, operator.itemgetter(1))
>>> [(key, tuple(grp)) for key, grp in res]
[(-1, (('b', -1),)), (1, (('a', 1), ('c', 1)))]
sorted + uniq
Note
sorted(set(iterable), key=iterable.index) cannot be used for any iterables (generator, a list of dicts, etc.), I guess.
Parameters: |
|
---|---|
Returns: | a sorted list of items in iterable |
>>> uniq([1, 2, 3, 1, 2])
[1, 2, 3]
>>> uniq((i for i in (2, 10, 3, 2, 5, 1, 7, 3)))
[1, 2, 3, 5, 7, 10]
>>> uniq(({str(i): i} for i in (2, 10, 3, 2, 5, 1, 7, 3)),
... key=lambda d: int(list(d.keys())[0]))
[{'1': 1}, {'2': 2}, {'3': 3}, {'5': 5}, {'7': 7}, {'10': 10}]
Returns: | True if given obj is an instance of parser. |
---|
>>> is_parser("ini")
False
>>> is_parser(anyconfig.backend.base.Parser)
False
>>> is_parser(anyconfig.backend.base.Parser())
True
Returns: | List (generator) of (config_type, [config_parser]) |
---|
Returns: | List (generator) of (config_ext, [config_parser]) |
---|
Find config parser by the extension of file path_or_stream, file path or stream (a file or file-like objects).
Parameters: |
|
---|---|
Returns: | Config Parser class found |
>>> find_by_file("a.missing_cnf_ext") is None
True
>>> strm = anyconfig.compat.StringIO()
>>> find_by_file(strm) is None
True
>>> find_by_file("a.json")
<class 'anyconfig.backend.json.Parser'>
>>> find_by_file("a.json", is_path_=True)
<class 'anyconfig.backend.json.Parser'>
Find config parser by file’s extension.
Parameters: |
|
---|---|
Returns: | Config Parser class found |
>>> find_by_type("missing_type") is None
True
Find out config parser object appropriate to load from a file of given path or file/file-like object.
Parameters: |
|
---|---|
Returns: | A tuple of (Parser class or None, “” or error message) |
>>> find_parser(None)
Traceback (most recent call last):
ValueError: path_or_stream or forced_type must be some value
>>> find_parser(None, "ini")
(<class 'anyconfig.backend.ini.Parser'>, '')
>>> find_parser(None, "type_not_exist")
(None, 'No parser found for given type: type_not_exist')
>>> find_parser("cnf.json")
(<class 'anyconfig.backend.json.Parser'>, '')
>>> find_parser("cnf.json", is_path_=True)
(<class 'anyconfig.backend.json.Parser'>, '')
>>> find_parser("cnf.ext_not_found")
(None, 'No parser found for given file: cnf.ext_not_found')
List available config types.