anyconfig.utils

Misc utility routines for anyconfig module.

anyconfig.utils.groupby(itr, key_fn=None)

An wrapper function around itertools.groupby to sort each results.

Parameters:
  • itr – Iterable object, a list/tuple/genrator, etc.
  • key_fn – Key function to sort itr.
>>> import operator
>>> itr = [("a", 1), ("b", -1), ("c", 1)]
>>> res = groupby(itr, operator.itemgetter(1))
>>> [(key, tuple(grp)) for key, grp in res]
[(-1, (('b', -1),)), (1, (('a', 1), ('c', 1)))]
anyconfig.utils.get_file_extension(file_path)
>>> get_file_extension("/a/b/c")
''
>>> get_file_extension("/a/b.txt")
'txt'
>>> get_file_extension("/a/b/c.tar.xz")
'xz'
anyconfig.utils.sglob(files_pattern)

glob.glob alternative of which results sorted always.

anyconfig.utils.is_iterable(obj)
>>> is_iterable([])
True
>>> is_iterable(())
True
>>> is_iterable([x for x in range(10)])
True
>>> is_iterable((1, 2, 3))
True
>>> g = (x for x in range(10))
>>> is_iterable(g)
True
>>> is_iterable("abc")
False
>>> is_iterable(0)
False
>>> is_iterable({})
False
anyconfig.utils.concat(xss)

Concatenates a list of lists.

>>> concat([[]])
[]
>>> concat((()))
[]
>>> concat([[1,2,3],[4,5]])
[1, 2, 3, 4, 5]
>>> concat([[1,2,3],[4,5,[6,7]]])
[1, 2, 3, 4, 5, [6, 7]]
>>> concat(((1,2,3),(4,5,[6,7])))
[1, 2, 3, 4, 5, [6, 7]]
>>> concat(((1,2,3),(4,5,[6,7])))
[1, 2, 3, 4, 5, [6, 7]]
>>> concat((i, i*2) for i in range(3))
[0, 0, 1, 2, 2, 4]
anyconfig.utils.normpath(path)

Normalize path.

  • eliminating double slashes, etc. (os.path.normpath)
  • ensure paths contain ~[user]/ expanded.
Parameters:path – Path string :: str
anyconfig.utils.is_path(obj)

Is given object obj a file path?

Parameters:obj – file path or something
Returns:True if obj is a file path
anyconfig.utils.is_path_obj(obj)

Is given object input a pathlib.Path object?

Parameters:obj – a pathlib.Path object or something
Returns:True if obj is a pathlib.Path object
>>> from anyconfig.compat import pathlib
>>> if pathlib is not None:
...      obj = pathlib.Path(__file__)
...      assert is_path_obj(obj)
>>>
>>> assert not is_path_obj(__file__)
anyconfig.utils.is_file_stream(obj)

Is given object input a file stream (file/file-like object)?

Parameters:obj – a file / file-like (stream) object or something
Returns:True if obj is a file stream
>>> assert is_file_stream(open(__file__))
>>> assert not is_file_stream(__file__)
anyconfig.utils.is_ioinfo(obj, keys=None)
Returns:True if given obj is a ‘IOInfo’ namedtuple object.
>>> assert not is_ioinfo(1)
>>> assert not is_ioinfo("aaa")
>>> assert not is_ioinfo({})
>>> assert not is_ioinfo(('a', 1, {}))
>>> inp = anyconfig.globals.IOInfo("/etc/hosts", "path", "/etc/hosts",
...                                None, open)
>>> assert is_ioinfo(inp)
anyconfig.utils.is_stream_ioinfo(obj)
Parameters:obj – IOInfo object or something
Returns:True if given IOInfo object obj is of file / file-like object
>>> ioi = anyconfig.globals.IOInfo(None, anyconfig.globals.IOI_STREAM,
...                                None, None, None)
>>> assert is_stream_ioinfo(ioi)
>>> assert not is_stream_ioinfo(__file__)
anyconfig.utils.is_path_like_object(obj, marker='*')

Is given object obj a path string, a pathlib.Path, a file / file-like (stream) or IOInfo namedtuple object?

Parameters:obj – a path string, pathlib.Path object, a file / file-like or ‘IOInfo’ object
Returns:True if obj is a path string or a pathlib.Path object or a file (stream) object
>>> assert is_path_like_object(__file__)
>>> assert not is_path_like_object("/a/b/c/*.json", '*')
>>> from anyconfig.compat import pathlib
>>> if pathlib is not None:
...      assert is_path_like_object(pathlib.Path("a.ini"))
...      assert not is_path_like_object(pathlib.Path("x.ini"), 'x')
>>> assert is_path_like_object(open(__file__))
anyconfig.utils.is_paths(maybe_paths, marker='*')

Does given object maybe_paths consist of path or path pattern strings?

anyconfig.utils.get_path_from_stream(strm)

Try to get file path from given file or file-like object strm.

Parameters:strm – A file or file-like object
Returns:Path of given file or file-like object or None
Raises:ValueError
>>> assert __file__ == get_path_from_stream(open(__file__, 'r'))
>>> assert get_path_from_stream(anyconfig.compat.StringIO()) is None
>>> get_path_from_stream(__file__)  
Traceback (most recent call last):
    ...
ValueError: ...
anyconfig.utils.are_same_file_types(objs)

Are given (maybe) file objs same type (extension) ?

Parameters:objs – A list of file path or file(-like) objects
>>> are_same_file_types([])
False
>>> are_same_file_types(["a.conf"])
True
>>> are_same_file_types(["a.conf", "b.conf"])
True
>>> are_same_file_types(["a.yml", "b.yml"])
True
>>> are_same_file_types(["a.yml", "b.json"])
False
>>> strm = anyconfig.compat.StringIO()
>>> are_same_file_types(["a.yml", "b.yml", strm])
False
anyconfig.utils.expand_paths(paths, marker='*')
Parameters:
  • paths – A glob path pattern string or pathlib.Path object holding such path, or a list consists of path strings or glob path pattern strings or pathlib.Path object holding such ones, or file objects
  • marker – Glob marker character or string, e.g. ‘*’
Returns:

List of path strings

>>> expand_paths([])
[]
>>> expand_paths("/usr/lib/a/b.conf /etc/a/b.conf /run/a/b.conf".split())
['/usr/lib/a/b.conf', '/etc/a/b.conf', '/run/a/b.conf']
>>> paths_s = os.path.join(os.path.dirname(__file__), "u*.py")
>>> ref = sglob(paths_s)
>>> assert expand_paths(paths_s) == ref
>>> ref = ["/etc/a.conf"] + ref
>>> assert expand_paths(["/etc/a.conf", paths_s]) == ref
>>> strm = anyconfig.compat.StringIO()
>>> assert expand_paths(["/etc/a.conf", strm]) == ["/etc/a.conf", strm]
anyconfig.utils.noop(val, *args, **kwargs)

A function does nothing.

>>> noop(1)
1
anyconfig.utils.is_dict_like(obj)
Parameters:obj – Any object behaves like a dict.
>>> is_dict_like("a string")
False
>>> is_dict_like({})
True
>>> is_dict_like(anyconfig.compat.OrderedDict((('a', 1), ('b', 2))))
True
anyconfig.utils.is_namedtuple(obj)
>>> p0 = collections.namedtuple("Point", "x y")(1, 2)
>>> is_namedtuple(p0)
True
>>> is_namedtuple(tuple(p0))
False
anyconfig.utils.is_list_like(obj)
>>> is_list_like([])
True
>>> is_list_like(())
True
>>> is_list_like([x for x in range(10)])
True
>>> is_list_like((1, 2, 3))
True
>>> g = (x for x in range(10))
>>> is_list_like(g)
True
>>> is_list_like("abc")
False
>>> is_list_like(0)
False
>>> is_list_like({})
False
anyconfig.utils.filter_options(keys, options)

Filter options with given keys.

Parameters:
  • keys – key names of optional keyword arguments
  • options – optional keyword arguments to filter with keys
>>> filter_options(("aaa", ), dict(aaa=1, bbb=2))
{'aaa': 1}
>>> filter_options(("aaa", ), dict(bbb=2))
{}