grepros 1.2.2
grep for ROS bag files and live topics
Loading...
Searching...
No Matches
grepros.common Namespace Reference

Common utilities. More...

Classes

class  ArgumentUtil
 Namespace for program argument handling. More...
 
class  ConsolePrinter
 Prints to console, supports color output. More...
 
class  Decompressor
 Decompresses zstandard archives. More...
 
class  LenIterable
 Wrapper for iterable value with specified fixed length. More...
 
class  MatchMarkers
 Highlight markers for matches in message values. More...
 
class  ProgressBar
 A simple ASCII progress bar with a ticker thread. More...
 
class  TextWrapper
 TextWrapper that supports custom substring widths in line width calculation. More...
 

Functions

 drop_zeros (v, replace="")
 Drops trailing zeros and empty decimal separator, if any.
 
 ellipsize (text, limit, ellipsis="..")
 Returns text ellipsized if beyond limit.
 
 ensure_namespace (val, defaults=None, dashify=("WRITE_OPTIONS",), **kwargs)
 Returns a copy of value as argparse.Namespace, with all keys uppercase.
 
 filter_dict (dct, keys=(), values=(), reverse=False)
 Filters string dictionary by keys and values, supporting * wildcards.
 
 find_files (names=(), paths=(), suffixes=(), skip_suffixes=(), recurse=False)
 Yields filenames from current directory or given paths.
 
 format_bytes (size, precision=2, inter=" ", strip=True)
 Returns a formatted byte size (like 421.40 MB), trailing zeros optionally removed.
 
 format_stamp (stamp)
 Returns ISO datetime from UNIX timestamp.
 
 format_timedelta (delta)
 Formats the datetime.timedelta as "3d 40h 23min 23.1sec".
 
 get_name (obj)
 Returns the fully namespaced name for a Python module, class, function or object.
 
 has_arg (func, name)
 Returns whether function supports taking specified argument by name.
 
 import_item (name)
 Returns imported module, or identifier from imported namespace; raises on error.
 
 is_iterable (value)
 Returns whether value is iterable.
 
 is_stream (value)
 Returns whether value is a file-like object.
 
 makedirs (path)
 Creates directory structure for path if not already existing.
 
 memoize (func)
 Returns a results-caching wrapper for the function, cache used if arguments hashable.
 
 merge_dicts (d1, d2)
 Merges d2 into d1, recursively for nested dicts, returns d1.
 
 merge_spans (spans, join_blanks=False)
 Returns a sorted list of (start, end) spans with overlapping spans merged.
 
 parse_datetime (text)
 Returns datetime object from ISO datetime string (may be partial).
 
 parse_number (value, suffixes=None)
 Returns an integer parsed from text, raises on error.
 
 path_to_regex (text, sep=".", wildcard="*", end=False, intify=False)
 Returns re.Pattern for matching path strings with optional integer indexes.
 
 plural (word, items=None, numbers=True, single="1", sep=",", pref="", suf="")
 Returns the word as 'count words', or '1 word' if count is 1, or 'words' if count omitted.
 
 structcopy (value)
 Returns a deep copy of a standard data structure (dict, list, set, tuple), other object types reused instead of copied.
 
 unique_path (pathname, empty_ok=False)
 Returns a unique version of the path.
 
 verify_io (f, mode)
 Returns whether stream or file path can be read from and/or written to as binary.
 
 wildcard_to_regex (text, end=False)
 Returns plain wildcard like "foo*bar" as re.Pattern("foo.*bar", re.I).
 

Variables

tuple PATH_TYPES = (six.binary_type, six.text_type)
 Python types for filesystem paths.
 
tuple STRING_TYPES = (six.binary_type, six.text_type)
 Python types for both byte strings and text strings.
 
tuple TEXT_TYPES = (six.binary_type, six.text_type) if six.PY2 else (six.text_type, )
 Python types for text strings.
 

Detailed Description

Common utilities.

Function Documentation

◆ drop_zeros()

drop_zeros (   v,
  replace = "" 
)

Drops trailing zeros and empty decimal separator, if any.

Definition at line 854 of file common.py.

◆ ellipsize()

ellipsize (   text,
  limit,
  ellipsis = ".." 
)

Returns text ellipsized if beyond limit.

Definition at line 860 of file common.py.

◆ ensure_namespace()

ensure_namespace (   val,
  defaults = None,
  dashify = ("WRITE_OPTIONS", ),
**  kwargs 
)

Returns a copy of value as argparse.Namespace, with all keys uppercase.

Arguments with list/tuple values in defaults are ensured to have list/tuple values.

Parameters
valargparse.Namespace or dictionary or None
defaultsadditional arguments to set to namespace if missing
dashifynames of dictionary arguments where to replace the first underscore in string keys with a dash
kwargsany and all argument overrides as keyword overrides

Definition at line 878 of file common.py.

◆ filter_dict()

filter_dict (   dct,
  keys = (),
  values = (),
  reverse = False 
)

Filters string dictionary by keys and values, supporting * wildcards.

Dictionary values may be additional lists; keys with emptied lists are dropped.

Retains only entries that find a match (supports * wildcards); if reverse, retains only entries that do not find a match.

Definition at line 905 of file common.py.

◆ find_files()

find_files (   names = (),
  paths = (),
  suffixes = (),
  skip_suffixes = (),
  recurse = False 
)

Yields filenames from current directory or given paths.

Seeks only files with given extensions if names not given. Logs errors for names and paths not found.

Parameters
nameslist of specific files to return (supports * wildcards)
pathslist of paths to look under, if not using current directory
suffixeslist of suffixes to select if no wilcarded names, as (".ext1", ..)
skip_suffixeslist of suffixes to skip if no wildcarded names, as (".ext1", ..)
recursewhether to recurse into subdirectories

Definition at line 936 of file common.py.

◆ format_bytes()

format_bytes (   size,
  precision = 2,
  inter = " ",
  strip = True 
)

Returns a formatted byte size (like 421.40 MB), trailing zeros optionally removed.

Definition at line 987 of file common.py.

◆ format_stamp()

format_stamp (   stamp)

Returns ISO datetime from UNIX timestamp.

Definition at line 1000 of file common.py.

◆ format_timedelta()

format_timedelta (   delta)

Formats the datetime.timedelta as "3d 40h 23min 23.1sec".

Definition at line 975 of file common.py.

◆ get_name()

get_name (   obj)

Returns the fully namespaced name for a Python module, class, function or object.

E.g. "my.thing" or "my.module.MyCls" or "my.module.MyCls.my_method" or "my.module.MyCls<0x1234abcd>" or "my.module.MyCls<0x1234abcd>.my_method".

Definition at line 1011 of file common.py.

◆ has_arg()

has_arg (   func,
  name 
)

Returns whether function supports taking specified argument by name.

Definition at line 1028 of file common.py.

◆ import_item()

import_item (   name)

Returns imported module, or identifier from imported namespace; raises on error.

Parameters
namePython module name like "my.module" or module namespace identifier like "my.module.Class"

Definition at line 1040 of file common.py.

◆ is_iterable()

is_iterable (   value)

Returns whether value is iterable.

Definition at line 1054 of file common.py.

◆ is_stream()

is_stream (   value)

Returns whether value is a file-like object.

Definition at line 1061 of file common.py.

◆ makedirs()

makedirs (   path)

Creates directory structure for path if not already existing.

Definition at line 1067 of file common.py.

◆ memoize()

memoize (   func)

Returns a results-caching wrapper for the function, cache used if arguments hashable.

Definition at line 1092 of file common.py.

◆ merge_dicts()

merge_dicts (   d1,
  d2 
)

Merges d2 into d1, recursively for nested dicts, returns d1.

Definition at line 1105 of file common.py.

◆ merge_spans()

merge_spans (   spans,
  join_blanks = False 
)

Returns a sorted list of (start, end) spans with overlapping spans merged.

Parameters
join_blankswhether to merge consecutive zero-length spans, e.g. [(0, 0), (1, 1)] -> [(0, 1)]

Definition at line 1120 of file common.py.

◆ parse_datetime()

parse_datetime (   text)

Returns datetime object from ISO datetime string (may be partial).

Raises if invalid.

Definition at line 1141 of file common.py.

◆ parse_number()

parse_number (   value,
  suffixes = None 
)

Returns an integer parsed from text, raises on error.

Parameters
valuetext or binary string to parse, may contain abbrevations like "12K"
suffixesa dictionary of multipliers like {"K": 1024}, case-insensitive

Definition at line 1156 of file common.py.

◆ path_to_regex()

path_to_regex (   text,
  sep = ".",
  wildcard = "*",
  end = False,
  intify = False 
)

Returns re.Pattern for matching path strings with optional integer indexes.

Parameters
textseparated wildcarded path pattern like "foo*.bar"
seppath parts separator, optional
wildcardsimple wildcard to convert to Python wildcard pattern, optional
endwhether pattern should match until end (terminates with $)
intifywhether path should match optional integer index between parts, like "foo.bar" as "foo(\.\d+)?\.bar"

Definition at line 1173 of file common.py.

◆ plural()

plural (   word,
  items = None,
  numbers = True,
  single = "1",
  sep = ",",
  pref = "",
  suf = "" 
)

Returns the word as 'count words', or '1 word' if count is 1, or 'words' if count omitted.

Parameters
itemsitem collection or count, or None to get just the plural of the word
numbersif False, count is omitted from final result
singleprefix to use for word if count is 1, e.g. "a"
septhousand-separator to use for count
prefprefix to prepend to count, e.g. "~150"
sufsuffix to append to count, e.g. "150+"

Definition at line 1193 of file common.py.

◆ structcopy()

structcopy (   value)

Returns a deep copy of a standard data structure (dict, list, set, tuple), other object types reused instead of copied.

Definition at line 1081 of file common.py.

◆ unique_path()

unique_path (   pathname,
  empty_ok = False 
)

Returns a unique version of the path.

If a file or directory with the same name already exists, returns a unique version (e.g. "/tmp/my.2.file" if ""/tmp/my.file" already exists).

Parameters
empty_okwhether to ignore existence if file is empty

Definition at line 1224 of file common.py.

◆ verify_io()

verify_io (   f,
  mode 
)

Returns whether stream or file path can be read from and/or written to as binary.

Prints or raises error if not.

Tries to open file in append mode if verifying path writability, auto-creating missing directories if any, will delete any file or directory created.

Parameters
ffile path, or stream
mode"r" for readable, "w" for writable, "a" for readable and writable

Definition at line 1258 of file common.py.

◆ wildcard_to_regex()

wildcard_to_regex (   text,
  end = False 
)

Returns plain wildcard like "foo*bar" as re.Pattern("foo.*bar", re.I).

Parameters
endwhether pattern should match until end (adds $)

Definition at line 1315 of file common.py.

Variable Documentation

◆ PATH_TYPES

tuple PATH_TYPES = (six.binary_type, six.text_type)

Python types for filesystem paths.

Definition at line 42 of file common.py.

◆ STRING_TYPES

tuple STRING_TYPES = (six.binary_type, six.text_type)

Python types for both byte strings and text strings.

Definition at line 45 of file common.py.

◆ TEXT_TYPES

tuple TEXT_TYPES = (six.binary_type, six.text_type) if six.PY2 else (six.text_type, )

Python types for text strings.

Definition at line 47 of file common.py.