Classes | Functions | Variables
grepros.common Namespace Reference

Classes

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

Functions

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

Variables

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

Function Documentation

◆ drop_zeros()

def grepros.common.drop_zeros (   v,
  replace = "" 
)
Drops or replaces trailing zeros and empty decimal separator, if any.

Definition at line 646 of file common.py.

◆ ellipsize()

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

Definition at line 651 of file common.py.

◆ ensure_namespace()

def grepros.common.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 658 of file common.py.

◆ filter_dict()

def grepros.common.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 689 of file common.py.

◆ find_files()

def grepros.common.find_files (   names = (),
  paths = (),
  extensions = (),
  skip_extensions = (),
  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   extensions       list of extensions to select if not using names, as (".ext1", ..)
@param   skip_extensions  list of extensions to skip if not using names, as (".ext1", ..)
@param   recurse          whether to recurse into subdirectories

Definition at line 715 of file common.py.

◆ format_bytes()

def grepros.common.format_bytes (   size,
  precision = 2,
  inter = " ",
  strip = True 
)
Returns a formatted byte size (like 421.40 MB), trailing zeros optionally removed.

Definition at line 779 of file common.py.

◆ format_stamp()

def grepros.common.format_stamp (   stamp)
Returns ISO datetime from UNIX timestamp.

Definition at line 791 of file common.py.

◆ format_timedelta()

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

Definition at line 767 of file common.py.

◆ get_name()

def grepros.common.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 796 of file common.py.

◆ has_arg()

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

Definition at line 816 of file common.py.

◆ import_item()

def grepros.common.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 823 of file common.py.

◆ is_iterable()

def grepros.common.is_iterable (   value)
Returns whether value is iterable.

Definition at line 843 of file common.py.

◆ is_stream()

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

Definition at line 850 of file common.py.

◆ makedirs()

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

Definition at line 856 of file common.py.

◆ memoize()

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

Definition at line 881 of file common.py.

◆ merge_dicts()

def grepros.common.merge_dicts (   d1,
  d2 
)
Merges d2 into d1, recursively for nested dicts.

Definition at line 894 of file common.py.

◆ merge_spans()

def grepros.common.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 903 of file common.py.

◆ parse_datetime()

def grepros.common.parse_datetime (   text)
Returns datetime object from ISO datetime string (may be partial). Raises if invalid.

Definition at line 930 of file common.py.

◆ parse_number()

def grepros.common.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 939 of file common.py.

◆ plural()

def grepros.common.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 953 of file common.py.

◆ structcopy()

def grepros.common.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 866 of file common.py.

◆ unique_path()

def grepros.common.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 988 of file common.py.

◆ verify_io()

def grepros.common.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 1019 of file common.py.

◆ wildcard_to_regex()

def grepros.common.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 1081 of file common.py.

Variable Documentation

◆ __all__

list grepros.common.__all__
private
Initial value:
1 = [
2  "PATH_TYPES", "ConsolePrinter", "Decompressor", "MatchMarkers", "ProgressBar", "TextWrapper",
3  "drop_zeros", "ellipsize", "ensure_namespace", "filter_dict", "find_files",
4  "format_bytes", "format_stamp", "format_timedelta", "get_name", "has_arg", "import_item",
5  "is_iterable", "is_stream", "makedirs", "memoize", "merge_dicts", "merge_spans",
6  "parse_datetime", "parse_number", "plural", "unique_path", "verify_io", "wildcard_to_regex",
7 ]

Definition at line 1091 of file common.py.

◆ curses

grepros.common.curses

Definition at line 34 of file common.py.

◆ PATH_TYPES

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

Python types for filesystem paths.

Definition at line 42 of file common.py.

◆ STRING_TYPES

tuple grepros.common.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 grepros.common.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.

◆ zstandard

grepros.common.zstandard

Definition at line 38 of file common.py.



grepros
Author(s): Erki Suurjaak
autogenerated on Sat Jan 6 2024 03:11:30