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

Classes

class  ArgumentUtil
 
class  ConsolePrinter
 
class  Decompressor
 
class  LenIterable
 
class  MatchMarkers
 
class  ProgressBar
 
class  TextWrapper
 

Functions

 drop_zeros (v, replace="")
 
 ellipsize (text, limit, ellipsis="..")
 
 ensure_namespace (val, defaults=None, dashify=("WRITE_OPTIONS",), **kwargs)
 
 filter_dict (dct, keys=(), values=(), reverse=False)
 
 find_files (names=(), paths=(), suffixes=(), skip_suffixes=(), recurse=False)
 
 format_bytes (size, precision=2, inter=" ", strip=True)
 
 format_stamp (stamp)
 
 format_timedelta (delta)
 
 get_name (obj)
 
 has_arg (func, name)
 
 import_item (name)
 
 is_iterable (value)
 
 is_stream (value)
 
 makedirs (path)
 
 memoize (func)
 
 merge_dicts (d1, d2)
 
 merge_spans (spans, join_blanks=False)
 
 parse_datetime (text)
 
 parse_number (value, suffixes=None)
 
 path_to_regex (text, sep=".", wildcard="*", end=False, intify=False)
 
 plural (word, items=None, numbers=True, single="1", sep=",", pref="", suf="")
 
 structcopy (value)
 
 unique_path (pathname, empty_ok=False)
 
 verify_io (f, mode)
 
 wildcard_to_regex (text, end=False)
 

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.

------------------------------------------------------------------------------
This file is part of grepros - grep for ROS1 bag files and live topics.
Released under the BSD License.

@author      Erki Suurjaak
@created     23.10.2021
@modified    06.05.2024
------------------------------------------------------------------------------

Function Documentation

◆ drop_zeros()

drop_zeros (   v,
  replace = "" 
)
Drops trailing zeros and empty decimal separator, if any.

Definition at line 837 of file common.py.

◆ ellipsize()

ellipsize (   text,
  limit,
  ellipsis = ".." 
)
Returns text ellipsized if beyond limit.

Definition at line 843 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.

@param  val       `argparse.Namespace` or dictionary or `None`
@param  defaults  additional arguments to set to namespace if missing
@param  dashify   names of dictionary arguments where to replace
                  the first underscore in string keys with a dash
@param  kwargs    any and all argument overrides as keyword overrides

Definition at line 850 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 881 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.

@param   names          list of specific files to return (supports * wildcards)
@param   paths          list of paths to look under, if not using current directory
@param   suffixes       list of suffixes to select if no wilcarded names, as (".ext1", ..)
@param   skip_suffixes  list of suffixes to skip if no wildcarded names, as (".ext1", ..)
@param   recurse        whether to recurse into subdirectories

Definition at line 907 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 970 of file common.py.

◆ format_stamp()

format_stamp (   stamp)
Returns ISO datetime from UNIX timestamp.

Definition at line 983 of file common.py.

◆ format_timedelta()

format_timedelta (   delta)
Formats the datetime.timedelta as "3d 40h 23min 23.1sec".

Definition at line 958 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 988 of file common.py.

◆ has_arg()

has_arg (   func,
  name 
)
Returns whether function supports taking specified argument by name.

Definition at line 1011 of file common.py.

◆ import_item()

import_item (   name)
Returns imported module, or identifier from imported namespace; raises on error.

@param   name  Python module name like "my.module"
               or module namespace identifier like "my.module.Class"

Definition at line 1018 of file common.py.

◆ is_iterable()

is_iterable (   value)
Returns whether value is iterable.

Definition at line 1038 of file common.py.

◆ is_stream()

is_stream (   value)
Returns whether value is a file-like object.

Definition at line 1045 of file common.py.

◆ makedirs()

makedirs (   path)
Creates directory structure for path if not already existing.

Definition at line 1051 of file common.py.

◆ memoize()

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

Definition at line 1076 of file common.py.

◆ merge_dicts()

merge_dicts (   d1,
  d2 
)
Merges d2 into d1, recursively for nested dicts, returns d1.

Definition at line 1089 of file common.py.

◆ merge_spans()

merge_spans (   spans,
  join_blanks = False 
)
Returns a sorted list of (start, end) spans with overlapping spans merged.

@param   join_blanks  whether to merge consecutive zero-length spans,
                      e.g. [(0, 0), (1, 1)] -> [(0, 1)]

Definition at line 1099 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 1126 of file common.py.

◆ parse_number()

parse_number (   value,
  suffixes = None 
)
Returns an integer parsed from text, raises on error.

@param   value     text or binary string to parse, may contain abbrevations like "12K"
@param   suffixes  a dictionary of multipliers like {"K": 1024}, case-insensitive

Definition at line 1135 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.

@param   text      separated wildcarded path pattern like "foo*.bar"
@param   sep       path parts separator, optional
@param   wildcard  simple wildcard to convert to Python wildcard pattern, optional
@param   end       whether pattern should match until end (terminates with $)
@param   intify    whether path should match optional integer index between parts,
                   like "foo.bar" as "foo(\.\d+)?\.bar"

Definition at line 1149 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.

@param   items      item collection or count,
                    or None to get just the plural of the word
@param   numbers    if False, count is omitted from final result
@param   single     prefix to use for word if count is 1, e.g. "a"
@param   sep        thousand-separator to use for count
@param   pref       prefix to prepend to count, e.g. "~150"
@param   suf        suffix to append to count, e.g. "150+"

Definition at line 1167 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 1061 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).

@param   empty_ok  whether to ignore existence if file is empty

Definition at line 1202 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.

@param   f     file path, or stream
@param   mode  "r" for readable, "w" for writable, "a" for readable and writable

Definition at line 1233 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).

@param   end  whether pattern should match until end (adds $)

Definition at line 1296 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.