Utils Module¶
- class AWSHandler(*args, **kwargs)¶
Bases:
object
Responsible for handling communication with Amazon AWS services.
- download_file_from_s3(bucket_name: str, file_name: str, desired_path: str = '') None ¶
Downloads a file from an S3 bucket to a local path.
- Parameters:
bucket_name (str) – The name of the S3 bucket.
file_name (str) – The key/path of the file in the S3 bucket.
desired_path (str, optional) – The local path where the file will be saved. If not provided, the file will be downloaded to the current working directory with the original filename.
- Raises:
RuntimeError – If the download operation fails.
- upload_buffer_to_s3(bucket_name: str, buffer: StringIO, desired_name: str) None ¶
Attempts to upload buffer as file body directly to S3 Amazon bucket.
- Parameters:
bucket_name (str) – String denoting bucket name.
buffer (io.StringIO) – Buffer containing data that should be directly written to bucket.
desired_name (str) – Desired name to be given to the file after being uploaded.
- Raises:
RuntimeError – If approached problem during file uploading.
- upload_file_to_s3(bucket_name: str, file_path: str, desired_name: str = '') None ¶
Attempts to upload local file specified by path to S3 Amazon bucket.
- Parameters:
bucket_name (str) – String denoting bucket name.
file_path (str) – String representing file to the path that should be uploaded.
desired_name (str) – Desired name to be given to the file after being uploaded. If left unspecified, name does not change.
- Raises:
RuntimeError – If approached problem during file uploading.
- class DynamicFromStringConverter(*args, **kwargs)¶
Bases:
object
Implements a dynamic class converter that allows for the conversion of class names from strings to actual class handles. It builds a tree structure of packages and allows for the retrieval of class handles based on their names.
- get_class_handle(class_name: str) type ¶
Retrieves the class handle for a given class name from the registered packages.
- Parameters:
class_name (str) – The name of the class to retrieve.
- Raises:
ValueError – If the class is not found in the registered packages.
- Returns:
The class type if found.
- Return type:
(type)
- register_packages(packages_to_get_registered: list[str]) None ¶
Registers a list of packages by building their package trees.
- Parameters:
packages_to_get_registered (list[str]) – A list of package names to register.
- class PackageNode(name: str, package: module, parent: Optional[PackageNode] = None)¶
Bases:
Node
Implements a node in the package tree structure. Each node represents a package and contains its name, the package itself, and a reference to its parent node.
- property ancestors¶
All parent nodes and their parent nodes.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> udo.ancestors () >>> marc.ancestors (Node('/Udo'),) >>> lian.ancestors (Node('/Udo'), Node('/Udo/Marc'))
- property anchestors¶
All parent nodes and their parent nodes - see
ancestors
.The attribute anchestors is just a typo of ancestors. Please use ancestors. This attribute will be removed in the 3.0.0 release.
- property children¶
All child nodes.
>>> from anytree import Node >>> n = Node("n") >>> a = Node("a", parent=n) >>> b = Node("b", parent=n) >>> c = Node("c", parent=n) >>> n.children (Node('/n/a'), Node('/n/b'), Node('/n/c'))
Modifying the children attribute modifies the tree.
Detach
The children attribute can be updated by setting to an iterable.
>>> n.children = [a, b] >>> n.children (Node('/n/a'), Node('/n/b'))
Node c is removed from the tree. In case of an existing reference, the node c does not vanish and is the root of its own tree.
>>> c Node('/c')
Attach
>>> d = Node("d") >>> d Node('/d') >>> n.children = [a, b, d] >>> n.children (Node('/n/a'), Node('/n/b'), Node('/n/d')) >>> d Node('/n/d')
Duplicate
A node can just be the children once. Duplicates cause a
TreeError
:>>> n.children = [a, b, d, a] Traceback (most recent call last): ... anytree.node.exceptions.TreeError: Cannot add node Node('/n/a') multiple times as child.
- property depth¶
Number of edges to the root Node.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> udo.depth 0 >>> marc.depth 1 >>> lian.depth 2
- property descendants¶
All child nodes and all their child nodes.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> loui = Node("Loui", parent=marc) >>> soe = Node("Soe", parent=lian) >>> udo.descendants (Node('/Udo/Marc'), Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Lian/Soe'), Node('/Udo/Marc/Loui')) >>> marc.descendants (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Lian/Soe'), Node('/Udo/Marc/Loui')) >>> lian.descendants (Node('/Udo/Marc/Lian/Soe'),)
- property height¶
Number of edges on the longest path to a leaf Node.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> udo.height 2 >>> marc.height 1 >>> lian.height 0
- property is_leaf¶
Node has no children (External Node).
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> udo.is_leaf False >>> marc.is_leaf False >>> lian.is_leaf True
- property is_root¶
Node is tree root.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> udo.is_root True >>> marc.is_root False >>> lian.is_root False
- iter_path_reverse()¶
Iterate up the tree from the current node to the root node.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> for node in udo.iter_path_reverse(): ... print(node) Node('/Udo') >>> for node in marc.iter_path_reverse(): ... print(node) Node('/Udo/Marc') Node('/Udo') >>> for node in lian.iter_path_reverse(): ... print(node) Node('/Udo/Marc/Lian') Node('/Udo/Marc') Node('/Udo')
- property leaves¶
Tuple of all leaf nodes.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> loui = Node("Loui", parent=marc) >>> lazy = Node("Lazy", parent=marc) >>> udo.leaves (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Loui'), Node('/Udo/Marc/Lazy')) >>> marc.leaves (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Loui'), Node('/Udo/Marc/Lazy'))
- property parent¶
Parent Node.
On set, the node is detached from any previous parent node and attached to the new node.
>>> from anytree import Node, RenderTree >>> udo = Node("Udo") >>> marc = Node("Marc") >>> lian = Node("Lian", parent=marc) >>> print(RenderTree(udo)) Node('/Udo') >>> print(RenderTree(marc)) Node('/Marc') └── Node('/Marc/Lian')
Attach
>>> marc.parent = udo >>> print(RenderTree(udo)) Node('/Udo') └── Node('/Udo/Marc') └── Node('/Udo/Marc/Lian')
Detach
To make a node to a root node, just set this attribute to None.
>>> marc.is_root False >>> marc.parent = None >>> marc.is_root True
- property path¶
Path from root node down to this Node.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> udo.path (Node('/Udo'),) >>> marc.path (Node('/Udo'), Node('/Udo/Marc')) >>> lian.path (Node('/Udo'), Node('/Udo/Marc'), Node('/Udo/Marc/Lian'))
- property root¶
Tree Root Node.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> udo.root Node('/Udo') >>> marc.root Node('/Udo') >>> lian.root Node('/Udo')
- separator = '/'¶
- property siblings¶
Tuple of nodes with the same parent.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> loui = Node("Loui", parent=marc) >>> lazy = Node("Lazy", parent=marc) >>> udo.siblings () >>> marc.siblings () >>> lian.siblings (Node('/Udo/Marc/Loui'), Node('/Udo/Marc/Lazy')) >>> loui.siblings (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Lazy'))
- property size¶
Tree size — the number of nodes in tree starting at this node.
>>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> loui = Node("Loui", parent=marc) >>> soe = Node("Soe", parent=lian) >>> udo.size 5 >>> marc.size 4 >>> lian.size 2 >>> loui.size 1
- class GradientHandler(*args, **kwargs)¶
Bases:
object
Responsible for communication and management of Paperspace Gradient services.
- create_notebook(command_to_invoke: str, github_repository_url: Optional[str] = None, notebook_name: str = datetime.datetime(2025, 8, 6, 16, 43, 15, 607437), machine_types: list = ['Free-P5000'], timeout: int = 6, environment_dict: dict = {}) str ¶
Attempts to create notebook basing on certain github repository, starting command and sets needed environmental parameters (e.g. variables, machine type, etc.).
- Parameters:
command_to_invoke (str) – Command to be invoked after notebook is started.
github_repository_url (str) – URL to repository that should be downloaded to notebook.
notebook_name (str) – Name that should be given to notebook.
machine_types (list) – List of demanded machine’s types that notebook should be attempted to be created on. The first successful creation stops attempts to create notebook for machine types that are further in the list. Therefore, machine’s types should be ordered from the most wanted to the least one.
timeout (int) – Number of hours that notebook should be active for. For free instances maximal value for this parameter is 6.
environment_dict (dict) – Dictionary containing defined environmental variables.
- Raises:
RuntimeError – If approached problem during notebook creation.
- delete_notebook(notebook_id: str) None ¶
Deletes notebook with the given ID from Paperspace Gradient.
This method attempts to remove a previously created notebook from the Paperspace Gradient platform.
- Parameters:
notebook_id (str) – ID of the notebook to be deleted.
- Raises:
RuntimeError – If approached problem during notebook deletion.
- class Granularity(value)¶
Bases:
Enum
Enum representing possible values of time granularity that coinbase API can handle. Values assigned to particular options represent number of seconds that certain option denotes.
- FIFTEEN_MINUTES = 900¶
- FIVE_MINUTES = 300¶
- ONE_DAY = 86400¶
- ONE_HOUR = 3600¶
- ONE_MINUTE = 60¶
- SIX_HOURS = 21600¶
- THIRTY_MINUTES = 1800¶
- classmethod from_string(granularity_str: str) Granularity ¶
Converts string representation of granularity to Granularity enum.
- Parameters:
granularity_str (str) – String representation of granularity.
- Returns:
Corresponding Granularity enum.
- Return type:
- class SingletonMeta(name: str, bases: tuple, attrs: dict)¶
Bases:
type
Metaclass for defining singleton classes.
- mro()¶
Return a type’s method resolution order.
- class LoggingOut¶
Bases:
object
Implements a custom output stream that redirects stdout to the logging module. It captures text written to stdout and logs it as info messages.
- flush() None ¶
Flushes the internal buffer by logging all captured text. If the buffer contains new lines, it splits the text into lines and logs each line.
- write(text) None ¶
Writes text to the internal buffer.
- Parameters:
text (str) – The text to write to the buffer.
- redirect_stdout_to_logging()¶
Context manager that redirects stdout to a custom logging stream.