anyconfig.utils
¶
Misc utility routines for anyconfig module.
-
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
(path_or_stream)¶ Is given object path_or_stream a file path?
Parameters: path_or_stream – file path or stream, file/file-like object Returns: True if path_or_stream is a file path
-
anyconfig.utils.
get_path_from_stream
(maybe_stream)¶ Try to get file path from given stream stream.
Parameters: maybe_stream – A file or file-like object Returns: Path of given file or file-like object or None >>> __file__ == get_path_from_stream(__file__) True >>> __file__ == get_path_from_stream(open(__file__, 'r')) True >>> strm = anyconfig.compat.StringIO() >>> get_path_from_stream(strm) is None True
-
anyconfig.utils.
are_same_file_types
(paths)¶ Are given (maybe) file paths same type (extension) ?
Parameters: paths – 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.
norm_paths
(paths, marker='*')¶ Parameters: - paths – A glob path pattern string, or a list consists of path strings or glob path pattern strings or file objects
- marker – Glob marker character or string, e.g. ‘*’
Returns: List of path strings
>>> norm_paths([]) [] >>> norm_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 norm_paths(paths_s) == ref >>> ref = ["/etc/a.conf"] + ref >>> assert norm_paths(["/etc/a.conf", paths_s]) == ref >>> strm = anyconfig.compat.StringIO() >>> assert norm_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)) {}