xtras

module fastcore.xtras in fastcore

Classes

builtins.object
    EventTimer
    IterLen
        ReindexCollection(fastcore.basics.GetAttr, IterLen)
fastcore.basics.GetAttr(builtins.object)
    ContextManagers
    ReindexCollection(fastcore.basics.GetAttr, IterLen)
string.Formatter(builtins.object)
    PartialFormatter

ContextManagers

class ContextManagers(fastcore.basics.GetAttr)
 |  ContextManagers(mgrs)
 |  
 |  Wrapper for `contextlib.ExitStack` which enters a collection of context managers
 |  
 |  Method resolution order:
 |      ContextManagers
 |      fastcore.basics.GetAttr
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  

ContextManagers.__enter__

 |  
 |  __enter__(self)
 |  
 |  

ContextManagers.__exit__

 |  
 |  __exit__(self, *args, **kwargs)
 |  
 |  

ContextManagers.__init__

 |  
 |  __init__(self, mgrs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from fastcore.basics.GetAttr:
 |  
 |  

GetAttr.__dir__

 |  
 |  __dir__(self)
 |      Default dir() implementation.
 |  
 |  

GetAttr.__getattr__

 |  
 |  __getattr__(self, k)
 |  
 |  

GetAttr.__setstate__

 |  
 |  __setstate__(self, data)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from fastcore.basics.GetAttr:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

EventTimer

class EventTimer(builtins.object)
 |  EventTimer(store=5, span=60)
 |  
 |  An event timer with history of `store` items of time `span`
 |  
 |  Methods defined here:
 |  
 |  

EventTimer.__init__

 |  
 |  __init__(self, store=5, span=60)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  

EventTimer.add

 |  
 |  add(self, n=1)
 |      Record `n` events
 |  
 |  ----------------------------------------------------------------------
 |  Readonly properties defined here:
 |  
 |  duration
 |  
 |  freq
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

IterLen

class IterLen(builtins.object)
 |  Base class to add iteration to anything supporting `__len__` and `__getitem__`
 |  
 |  Methods defined here:
 |  
 |  

IterLen.__iter__

 |  
 |  __iter__(self)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

PartialFormatter

class PartialFormatter(string.Formatter)
 |  A `string.Formatter` that doesn't error on missing fields, and tracks missing fields and unused args
 |  
 |  Method resolution order:
 |      PartialFormatter
 |      string.Formatter
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  

PartialFormatter.__init__

 |  
 |  __init__(self)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  

PartialFormatter.check_unused_args

 |  
 |  check_unused_args(self, used, args, kwargs)
 |  
 |  

PartialFormatter.get_field

 |  
 |  get_field(self, nm, args, kwargs)
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from string.Formatter:
 |  
 |  

Formatter.convert_field

 |  
 |  convert_field(self, value, conversion)
 |  
 |  

Formatter.format

 |  
 |  format(self, format_string, /, *args, **kwargs)
 |  
 |  

Formatter.format_field

 |  
 |  format_field(self, value, format_spec)
 |  
 |  

Formatter.get_value

 |  
 |  get_value(self, key, args, kwargs)
 |  
 |  

Formatter.parse

 |  
 |  parse(self, format_string)
 |      # returns an iterable that contains tuples of the form:
 |      # (literal_text, field_name, format_spec, conversion)
 |      # literal_text can be zero length
 |      # field_name can be None, in which case there's no
 |      #  object to format and output
 |      # if field_name is not None, it is looked up, formatted
 |      #  with format_spec and conversion and then used
 |  
 |  

Formatter.vformat

 |  
 |  vformat(self, format_string, args, kwargs)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from string.Formatter:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

ReindexCollection

class ReindexCollection(fastcore.basics.GetAttr, IterLen)
 |  ReindexCollection(coll, idxs=None, cache=None, tfm=<function noop at 0x10c634ee0>)
 |  
 |  Reindexes collection `coll` with indices `idxs` and optional LRU cache of size `cache`
 |  
 |  Method resolution order:
 |      ReindexCollection
 |      fastcore.basics.GetAttr
 |      IterLen
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  

ReindexCollection.__getitem__

 |  
 |  __getitem__(self, i)
 |  
 |  

ReindexCollection.__getstate__

 |  
 |  __getstate__(self)
 |  
 |  

ReindexCollection.__init__

 |  
 |  __init__(self, coll, idxs=None, cache=None, tfm=<function noop at 0x10c634ee0>)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  

ReindexCollection.__len__

 |  
 |  __len__(self)
 |  
 |  

ReindexCollection.__setstate__

 |  
 |  __setstate__(self, s)
 |  
 |  

ReindexCollection.cache_clear

 |  
 |  cache_clear(self)
 |      Clear LRU cache
 |  
 |  

ReindexCollection.reindex

 |  
 |  reindex(self, idxs)
 |      Replace `self.idxs` with idxs
 |  
 |  

ReindexCollection.shuffle

 |  
 |  shuffle(self)
 |      Randomly shuffle indices
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from fastcore.basics.GetAttr:
 |  
 |  

GetAttr.__dir__

 |  
 |  __dir__(self)
 |      Default dir() implementation.
 |  
 |  

GetAttr.__getattr__

 |  
 |  __getattr__(self, k)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from fastcore.basics.GetAttr:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from IterLen:
 |  
 |  

IterLen.__iter__

 |  
 |  __iter__(self)

Functions

autostart

autostart(g)
    Decorator that automatically starts a generator

bunzip

bunzip(fn)
    bunzip `fn`, raising exception if output already exists

console_help

console_help(libname: 'str')
    Show help for all console scripts from `libname`

dict2obj

dict2obj(d, list_func=, dict_func=)
    Convert (possibly nested) dicts (or lists of dicts) to `AttrDict`

dumps

dumps(obj, **kw)
    Same as `json.dumps`, but uses `ujson` if available

globtastic

globtastic(path: 'Path | str', recursive: 'bool' = True, symlinks: 'bool' = True, file_glob: 'str' = None, file_re: 'str' = None, folder_re: 'str' = None, skip_file_glob: 'str' = None, skip_file_re: 'str' = None, skip_folder_re: 'str' = None, func: 'callable' = , ret_folders: 'bool' = False) -> 'L'
    A more powerful `glob`, including regex matches, symlink handling, and skip parameters

image_size

image_size(fn)
    Tuple of (w,h) for png, gif, or jpg; `None` otherwise

is_listy

is_listy(x)
    `isinstance(x, (tuple,list,L,slice,Generator))`

join_path_file

join_path_file(file, path, ext='')
    Return `path/file` if file is a string or a `Path`, file otherwise

load_pickle

load_pickle(fn)
    Load a pickle file from a file name or opened file

loads

loads(s, **kw)
    Same as `json.loads`, but handles `None`

loads_multi

loads_multi(s: 'str')
    Generator of >=0 decoded json dicts, possibly with non-json ignored text at start and end

local2utc

local2utc(dt: 'datetime') -> 'datetime'
    Convert `dt` from local to UTC time

mapped

mapped(f, it)
    map `f` over `it`, unless it's not listy, in which case return `f(it)`

maybe_open

maybe_open(f, mode='r', **kwargs)
    Context manager: open `f` if it is a path (and close on exit)

mkdir

mkdir(path, exist_ok=False, parents=False, overwrite=False, **kwargs)
    Creates and returns a directory defined by `path`, optionally removing previous existing directory if `overwrite` is `True`

modified_env

modified_env(*delete, **replace)
    Context manager temporarily modifying `os.environ` by deleting `delete` and replacing `replace`

modify_exception

modify_exception(e: 'Exception', msg: 'str' = None, replace: 'bool' = False) -> 'Exception'
    Modifies `e` with a custom message attached

obj2dict

obj2dict(d)
    Convert (possibly nested) AttrDicts (or lists of AttrDicts) to `dict`

open_file

open_file(fn, mode='r', **kwargs)
    Open a file, with optional compression if gz or bz2 suffix

partial_format

partial_format(s: 'str', **kwargs)
    string format `s`, ignoring missing field errors, returning missing and extra fields

repo_details

repo_details(url)
    Tuple of `owner,name` from ssh or https git repo `url`

repr_dict

repr_dict(d)
    Print nested dicts and lists, such as returned by `dict2obj`

round_multiple

round_multiple(x, mult, round_down=False)
    Round `x` to nearest multiple of `mult`

run

run(cmd, *rest, same_in_win=False, ignore_ex=False, as_bytes=False, stderr=False)
    Pass `cmd` (splitting with `shlex` if string) to `subprocess.run`; return `stdout`; raise `IOError` if fails

save_pickle

save_pickle(fn, o)
    Save a pickle file, to a file name or opened file

set_num_threads

set_num_threads(nt)
    Get numpy (and others) to use `nt` threads

shufflish

shufflish(x, pct=0.04)
    Randomly relocate items of `x` up to `pct` of `len(x)` from their starting location

sparkline

sparkline(data, mn=None, mx=None, empty_zero=False)
    Sparkline for `data`, with `None`s (and zero, if `empty_zero`) shown as empty column

stringfmt_names

stringfmt_names(s: 'str') -> 'list'
    Unique brace-delimited names in `s`

trace

trace(f)
    Add `set_trace` to an existing function `f`

truncstr

truncstr(s: 'str', maxlen: 'int', suf: 'str' = '…', space='') -> 'str'
    Truncate `s` to length `maxlen`, adding suffix `suf` if truncated

untar_dir

untar_dir(fname, dest, rename=False, overwrite=False)
    untar `file` into `dest`, creating a directory if the root contains more than one item

utc2local

utc2local(dt: 'datetime') -> 'datetime'
    Convert `dt` from UTC to local time

walk

walk(path: 'Path | str', symlinks: 'bool' = True, keep_file: 'callable' = , keep_folder: 'callable' = , skip_folder: 'callable' = , func: 'callable' = , ret_folders: 'bool' = False)
    Generator version of `os.walk`, using functions to filter files and folders