dandi.utils¶
- dandi.utils.Parallel(**kwargs: Any) Any[source]¶
Adapter for joblib.Parallel so we could if desired, centralize control
- class dandi.utils.ServerInfo(*, version: str, services: ServerServices, cli_minimal_version: str, cli_bad_versions: List[str])[source]¶
- cli_bad_versions: List[str]¶
- cli_minimal_version: str¶
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [
ConfigDict][pydantic.config.ConfigDict].
- services: ServerServices¶
- version: str¶
- class dandi.utils.ServerServices(*, api: ServiceURL, webui: ServiceURL | None = None, jupyterhub: ServiceURL | None = None)[source]¶
- api: ServiceURL¶
- jupyterhub: ServiceURL | None¶
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [
ConfigDict][pydantic.config.ConfigDict].
- webui: ServiceURL | None¶
- class dandi.utils.ServiceURL(*, url: str)[source]¶
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [
ConfigDict][pydantic.config.ConfigDict].
- url: str¶
- class dandi.utils.StrEnum(value)[source]¶
A variation of Enum that is also a subclass of str, akin to IntEnum
Member instances of a subclass of this class assigned to
auto()will have their own name as their value.
- dandi.utils.abbrev_prompt(msg: str, *options: str) str[source]¶
Prompt the user to input one of several options, which can be entered as either a whole word or the first letter of a word. All input is handled case-insensitively. Returns the complete word corresponding to the input, lowercased.
For example,
abbrev_prompt("Delete assets?", "yes", "no", "list")prompts the user with the messageDelete assets? ([y]es/[n]o/[l]ist): `` and accepts as input ``y`, ``yes,n,no,l, andlist.
- dandi.utils.delayed(*args, **kwargs)[source]¶
Adapter for joblib.delayed so we could if desired, centralize control
- dandi.utils.ensure_datetime(t: datetime | int | float | str, strip_tzinfo: bool = False, tz: tzinfo | None = None) datetime[source]¶
Ensures that time is a datetime
strip_tzinfo applies only to str records passed in
epoch time assumed to be local (not utc)
- dandi.utils.ensure_strtime(t: str | int | float | datetime, isoformat: bool = True) str[source]¶
Ensures that time is a string in iso format
Note: if
tis already a string, no conversion of any kind is done.epoch time assumed to be local (not utc)
- Parameters:
isoformat (bool, optional) – If True, use .isoformat() and otherwise str(). With .isoformat() there is no space but T to separate date from time.
- dandi.utils.exclude_from_zarr(path: Path) bool[source]¶
Returns
Trueif thepathis a file or directory that should be excluded from consideration when located in a Zarr
- dandi.utils.find_files(regex: str, paths: str | Path | Iterable[str | Path] = '.', exclude: str | None = None, exclude_dotfiles: bool = True, exclude_dotdirs: bool = True, exclude_vcs: bool = True, exclude_datalad: bool = False, dirs: bool = False, dirs_avoid: str | None = None) Iterator[str][source]¶
Generator to find files matching regex
- Parameters:
regex (string) – Regex to search target files. Is not applied to filter out directories
paths (string or list, optional) – Directories or files to search among (directories are searched recursively)
exclude (string, optional) – Matches to exclude
exclude_vcs – If True, excludes commonly known VCS subdirectories. If string, used as regex to exclude those files (regex:
%r)exclude_dotdirs – If True, does not descend into directories starting with “.”.
exclude_datalad – If True, excludes files known to be datalad meta-data files (e.g. under .datalad/ subdirectory) (regex:
%r)dirs (bool, optional) – Whether to match directories as well as files
dirs_avoid (string, optional) – Regex for directories to not rercurse under (they might still be reported if
dirs=True)
- dandi.utils.find_parent_directory_containing(filename: str | Path, path: str | Path | None = None) Path | None[source]¶
Find a directory, on the path to ‘path’ containing filename
if no ‘path’ - path from cwd. If ‘path’ is not absolute, absolute path is taken assuming relative to cwd.
Returns None if no such found, pathlib’s Path (absolute) to the directory if found.
- dandi.utils.flatten(it: Iterable) Iterator[source]¶
Yield items flattened if list, tuple or a generator
- dandi.utils.flattened(it: Iterable) list[source]¶
Return list with items flattened if list, tuple or a generator
- dandi.utils.get_instance(dandi_instance_id: str | DandiInstance) DandiInstance[source]¶
- dandi.utils.get_mime_type(filename: str, strict: bool = False) str[source]¶
Like
mimetypes.guess_type(), except that if the file is compressed, the MIME type for the compression is returned. Also, the default return value is now'application/octet-stream'instead ofNone.
- dandi.utils.get_module_version(module: str | ModuleType) str | None[source]¶
Return version of the module
Return module’s
__version__if present, or use importlib to get version.- Return type:
object
- dandi.utils.get_retry_after(response: Response) int | None[source]¶
If provided and parsed ok, returns duration in seconds to sleep before retry.
If not provided in the response header
Retry-After, would return None. If parsing fails, or provided date/sleep does not make sense since either too far in the past (over 2 seconds) or in the future (over a week), would return None.
- dandi.utils.get_utcnow_datetime(microseconds: bool = True) datetime[source]¶
Return current time as datetime with time zone information.
Microseconds are stripped away.
If string representation is desired, just “apply” .isoformat()
- dandi.utils.is_page2_url(page1: str, page2: str) bool[source]¶
Tests whether the URL
page2is the same aspage1but with thepagequery parameter set to2
- dandi.utils.is_same_time(*times: datetime | int | float | str, tolerance: float = 1e-06, strip_tzinfo: bool = False) bool[source]¶
Helper to do comparison between time points
Time zone information gets stripped Does it by first normalizing all times to datetime, and then comparing to the first entry
- Parameters:
tolerance (float, optional) – Seconds of difference between times to tolerate. By default difference up to a microsecond is ok
- dandi.utils.is_url(s: str) bool[source]¶
Determines whether the input string
sis a valid URL (standard URL or DANDI URL).
- dandi.utils.joinurl(base: str, path: str) str[source]¶
Append a slash-separated
pathto a base HTTP(S) URLbase. The two components are separated by a single slash, removing any excess slashes that would be present after naïve concatenation.If
pathis already an absolute HTTP(S) URL, it is returned unchanged.Note that this function differs from
urllib.parse.urljoin()when the path portion ofbaseis nonempty and does not end in a slash.
- dandi.utils.list_paths(dirpath: str | Path, dirs: bool = False, exclude_vcs: bool = True) list[Path][source]¶
- dandi.utils.path_is_subpath(path: str, prefix: str) bool[source]¶
Return True if path is a subpath of prefix
It will return False if path == prefix.
- Parameters:
path (str)
prefix (str)
- dandi.utils.setup_exceptionhook(ipython: bool = False) None[source]¶
Overloads default sys.excepthook with our exceptionhook handler.
If interactive, our exceptionhook handler will invoke pdb.post_mortem; if not interactive, then invokes default handler.
- dandi.utils.under_paths(paths: Iterable[str | PurePath], filter_paths: Iterable[str | PurePath]) Iterator[PurePosixPath][source]¶
Return all elements of
paths(converted toPurePosixPathinstances) that are equal to or under/start with one or more paths infilter_paths. The elements of both iterables must be relative & normalized.Based on
get_filtered_paths_from datalad’sdatalad/support/path.py
- dandi.utils.yaml_dump(rec: Any) str[source]¶
Consistent dump into yaml
Of primary importance is default_flow_style=False to assure proper formatting on versions of pyyaml before 5.1: https://github.com/yaml/pyyaml/pull/256
- dandi.utils.yaml_load(f: str | IO[str], typ: str | None = None) Any[source]¶
Load YAML source from a file or string.
- Parameters:
f (str or IO[str]) – The YAML source to load
typ (str, optional) – The value of
typto pass toruamel.yaml.YAML. May be “rt” (default), “safe”, “unsafe”, or “base”
- Returns:
The parsed YAML value
- Return type:
Any