Miscellaneous API#

wget()#

planetary_coverage.misc.wget(url, fout, skip=False, force=False)[source]#

Web download.

Parameters:
  • url (str) – URL to download

  • fout (str or pathlib.Path) – Output file.

  • skip (bool, optional) – Skip download if the output file already exists (default: False). Has the priority over force.

  • force (bool, optional) – Force download even if the file exists (default: False).

Returns:

Downloaded file path.

Return type:

pathlib.Path

Raises:
  • ValueError – If the URL provided is not starting with http[s]://.

  • FileExistsError – If the file already exists and force is False (default).

Note

The missing sub-directories will be created.

By default, logging is set at INFO level. Use debug_download() function to increase or disable the logging output.

Segment#

class planetary_coverage.misc.Segment(arr)[source]#

Bases: object

Segments object on conditional list.

Parameters:

arr (numpy.ndarray) – Conditional list.

property starts#

Get segment starts.

property stops#

Get segment stops.

property singles#

Get segment isolated single points.

property inside#

Get segment inside points.

property outside#

Get segment outside points.

property istarts#

Segment starts indexes.

property istops#

Segment stops indexes.

property slices#

Segment slices.

rindex()#

planetary_coverage.misc.rindex(lst, value)[source]#

Search the last index reference of a value in a list.

Solution from Stackoverflow: https://stackoverflow.com/a/63834895

Cache#

@cached_property#

@planetary_coverage.misc.cached_property(method=None, *, parent=None)[source]#

Cached class property decorator attribute.

Can be used in the following forms:

  • @cached_property

  • @cached_property()

  • @cached_property(parent='my_parent')

  • @cached_property(parent=('my_parent_1', ...))

  • @cached_property(parent=['my_parent_1', ...])

  • @cached_property(parent={'my_parent_1', ...})

Parameters:
  • method (callable, optional) – Method to memoizes.

  • parent (str or set or tuple or list) – List of parents cached properties, for garbage collection.

Note

To remove a single cached attributed, use the delattr() function or the del keyword. In that case, all the child cached property will also be cleared.

To clear the cache globally on this object, use the clear_cache() method.

Warning

This caching strategy is not thread-safe contrary to the functools method.

Depreciation#

@depreciated#

@planetary_coverage.misc.depreciated(version, new_name=None)[source]#

Depreciation class decorator.

DepreciationHelper#

class planetary_coverage.misc.DepreciationHelper(old_name, new_name, new_target)[source]#

Bases: object

Depreciation helper.

Parameters:
  • old_name (str) – Original object name.

  • new_name (str) – New object name.

  • new_target (object) – New object target.

Env variable and dotenv file#

getenv()#

planetary_coverage.misc.getenv(key: str, default: str = None, max_parents: int = None, dotenv: str = '.env') str[source]#

Get environnement variables from dotenv file or globally.

Parameters:
  • key (str) – Key to query on the environment

  • default (str, optional) – Optional value if not found.

  • max_parents (int, optional) – Max number of parent to check recursively for a .env file. To search only the current parent set max_parents=0. Default: None.

  • dotenv (str, optional) – Dotenv file to search (default: '.env'). To load only the global variables set dotenv=None.

Returns:

Environment variable value. None, if not found.

Return type:

str

Note

The function first search for a .env file in the current working directory, then in its parents up to the root. If a .env file is found, the search is stopped and the file is parsed for key-values. If not present, the function will search globally if the value is present.

find_dotenv()#

planetary_coverage.misc.find_dotenv(max_parents: int = None, fname: str = '.env') Path[source]#

Search for .env file in the working directory and its parents.

Parameters:
  • max_parents (int, optional) – Max number of parent to check recursively for a .env file. To search only the current parent set max_parents=0. Default: None.

  • fname (str, optional) – Dotenv file to search (default: '.env').

Returns:

File path object.

Return type:

pathlib.Path or None

Note

Only the first parent with a .env if returned.

%load_ext planetary_coverage#

planetary_coverage.load_ipython_extension(ipython)[source]#

Print the list of installed packages and kernels directories.

IPython/Jupyter magic line: %load_ext planetary_coverage

File size#

file_size()#

planetary_coverage.misc.file_size(*fname, fmt=',.0f', skip=False) str[source]#

Get filesize as human string.

Parameters:
  • *fname (str or pathlib.Path) – Filename(s) to measure.

  • fmt (str, optional) – Size format. Default: ',.0f'.

  • skip (bool, optional) – Skip error handling and return zero-size if the file does not exists. Default: False.

Returns:

Filesize human string. If multiple files are provided, it returns the sum of their combined sizes.

Return type:

str

Raises:

FileNotFoundError – If the file does not exist.

get_size()#

planetary_coverage.misc.filesize.get_size(fname, skip=False) int[source]#

Get filename size.

Parameters:
  • fname (str or pathlib.Path) – File name to measure.

  • skip (bool, optional) – Skip error handling and return zero-size if the file does not exists. Default: False.

Returns:

Filesize as bytes.

Return type:

int

Raises:

FileNotFoundError – If the file does not exist.

as_bytes()#

planetary_coverage.misc.filesize.as_bytes(size, fmt=',.0f')[source]#

Convert filesize as compressed bytes units.

Parameters:
  • size (int or float) – File size to convert.

  • fmt (str, optional) – Size format to adjust the precision. Default: ',.0f'.

Returns:

File size human string.

Return type:

str