parent
1df42a3991
commit
8e1a0242d9
@ -0,0 +1,35 @@
|
|||||||
|
from typing import Callable, Any
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def block(
|
||||||
|
function: Callable[..., Any],
|
||||||
|
device: str = None,
|
||||||
|
verbose: bool = False,
|
||||||
|
) -> Callable[..., Any]:
|
||||||
|
"""
|
||||||
|
A decorator that transforms a function into a block.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
function (Callable[..., Any]): The function to transform.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Callable[..., Any]: The transformed function.
|
||||||
|
"""
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
# Here you can add code to execute the function on various hardwares
|
||||||
|
# For now, we'll just call the function normally
|
||||||
|
try:
|
||||||
|
return function(*args, **kwargs)
|
||||||
|
except Exception as error:
|
||||||
|
logger.error(f"Error in {function.__name__}: {error}")
|
||||||
|
raise error
|
||||||
|
|
||||||
|
# Set the wrapper function's name and docstring to those of the original function
|
||||||
|
wrapper.__name__ = function.__name__
|
||||||
|
wrapper.__doc__ = function.__doc__
|
||||||
|
|
||||||
|
return wrapper
|
@ -0,0 +1,19 @@
|
|||||||
|
from tracemalloc import start
|
||||||
|
from typing import Any, Callable, Dict, List, Union
|
||||||
|
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BlockDevice:
|
||||||
|
device: str
|
||||||
|
cluster: str
|
||||||
|
description: str
|
||||||
|
|
||||||
|
def __init__(self, device: str, cluster: str, description: str):
|
||||||
|
self.device = device
|
||||||
|
self.cluster = cluster
|
||||||
|
self.description = description
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"BlockDevice(device={self.device}, cluster={self.cluster}, description={self.description})"
|
||||||
|
|
@ -0,0 +1,65 @@
|
|||||||
|
from typing import (
|
||||||
|
Any,
|
||||||
|
Dict,
|
||||||
|
Optional,
|
||||||
|
)
|
||||||
|
|
||||||
|
from swarms.structs.base import BaseStructure
|
||||||
|
|
||||||
|
|
||||||
|
class BlocksDict(BaseStructure):
|
||||||
|
"""
|
||||||
|
A class representing a dictionary of blocks.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name (str): The name of the blocks dictionary.
|
||||||
|
description (str): The description of the blocks dictionary.
|
||||||
|
blocks (Dict[str, Any]): The dictionary of blocks.
|
||||||
|
parent (Optional[Any], optional): The parent of the blocks dictionary. Defaults to None.
|
||||||
|
**kwargs: Additional keyword arguments.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
parent (Optional[Any]): The parent of the blocks dictionary.
|
||||||
|
blocks (Dict[str, Any]): The dictionary of blocks.
|
||||||
|
|
||||||
|
Methods:
|
||||||
|
add(key: str, block: Any): Add a block to the dictionary.
|
||||||
|
remove(key: str): Remove a block from the dictionary.
|
||||||
|
get(key: str): Get a block from the dictionary.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str,
|
||||||
|
description: str,
|
||||||
|
blocks: Dict[str, Any],
|
||||||
|
parent: Optional[Any] = None,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
super().__init__(name=name, description=description, **kwargs)
|
||||||
|
self.parent = parent
|
||||||
|
self.blocks = blocks
|
||||||
|
|
||||||
|
def add(self, key: str, block: Any):
|
||||||
|
self.blocks[key] = block
|
||||||
|
|
||||||
|
def remove(self, key: str):
|
||||||
|
del self.blocks[key]
|
||||||
|
|
||||||
|
def get(self, key: str):
|
||||||
|
return self.blocks.get(key)
|
||||||
|
|
||||||
|
def update(self, key: str, block: Any):
|
||||||
|
self.blocks[key] = block
|
||||||
|
|
||||||
|
def keys(self):
|
||||||
|
return list(self.blocks.keys())
|
||||||
|
|
||||||
|
def values(self):
|
||||||
|
return list(self.blocks.values())
|
||||||
|
|
||||||
|
def items(self):
|
||||||
|
return list(self.blocks.items())
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
self.blocks.clear()
|
@ -0,0 +1,138 @@
|
|||||||
|
from typing import (
|
||||||
|
Any,
|
||||||
|
List,
|
||||||
|
Optional,
|
||||||
|
)
|
||||||
|
|
||||||
|
from swarms.structs.base import BaseStructure
|
||||||
|
|
||||||
|
|
||||||
|
class BlocksList(BaseStructure):
|
||||||
|
"""
|
||||||
|
A class representing a list of blocks.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name (str): The name of the blocks list.
|
||||||
|
description (str): The description of the blocks list.
|
||||||
|
blocks (List[Any]): The list of blocks.
|
||||||
|
parent (Optional[Any], optional): The parent of the blocks list. Defaults to None.
|
||||||
|
**kwargs: Additional keyword arguments.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
parent (Optional[Any]): The parent of the blocks list.
|
||||||
|
blocks (List[Any]): The list of blocks.
|
||||||
|
|
||||||
|
Methods:
|
||||||
|
add(block: Any): Add a block to the list.
|
||||||
|
remove(block: Any): Remove a block from the list.
|
||||||
|
update(block: Any): Update a block in the list.
|
||||||
|
get(index: int): Get a block at the specified index.
|
||||||
|
get_all(): Get all blocks in the list.
|
||||||
|
get_by_name(name: str): Get blocks by name.
|
||||||
|
get_by_type(type: str): Get blocks by type.
|
||||||
|
get_by_id(id: str): Get blocks by ID.
|
||||||
|
get_by_parent(parent: str): Get blocks by parent.
|
||||||
|
get_by_parent_id(parent_id: str): Get blocks by parent ID.
|
||||||
|
get_by_parent_name(parent_name: str): Get blocks by parent name.
|
||||||
|
get_by_parent_type(parent_type: str): Get blocks by parent type.
|
||||||
|
get_by_parent_description(parent_description: str): Get blocks by parent description.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str,
|
||||||
|
description: str,
|
||||||
|
blocks: List[Any],
|
||||||
|
parent: Optional[Any] = None,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
super().__init__(name=name, description=description, **kwargs)
|
||||||
|
self.parent = parent
|
||||||
|
self.blocks = blocks
|
||||||
|
|
||||||
|
def add(self, block: Any):
|
||||||
|
self.blocks.append(block)
|
||||||
|
|
||||||
|
def remove(self, block: Any):
|
||||||
|
self.blocks.remove(block)
|
||||||
|
|
||||||
|
def update(self, block: Any):
|
||||||
|
self.blocks[self.blocks.index(block)] = block
|
||||||
|
|
||||||
|
def get(self, index: int):
|
||||||
|
return self.blocks[index]
|
||||||
|
|
||||||
|
def get_all(self):
|
||||||
|
return self.blocks
|
||||||
|
|
||||||
|
def get_by_name(self, name: str):
|
||||||
|
return [block for block in self.blocks if block.name == name]
|
||||||
|
|
||||||
|
def get_by_type(self, type: str):
|
||||||
|
return [block for block in self.blocks if block.type == type]
|
||||||
|
|
||||||
|
def get_by_id(self, id: str):
|
||||||
|
return [block for block in self.blocks if block.id == id]
|
||||||
|
|
||||||
|
def get_by_parent(self, parent: str):
|
||||||
|
return [
|
||||||
|
block for block in self.blocks if block.parent == parent
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_by_parent_id(self, parent_id: str):
|
||||||
|
return [
|
||||||
|
block
|
||||||
|
for block in self.blocks
|
||||||
|
if block.parent_id == parent_id
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_by_parent_name(self, parent_name: str):
|
||||||
|
return [
|
||||||
|
block
|
||||||
|
for block in self.blocks
|
||||||
|
if block.parent_name == parent_name
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_by_parent_type(self, parent_type: str):
|
||||||
|
return [
|
||||||
|
block
|
||||||
|
for block in self.blocks
|
||||||
|
if block.parent_type == parent_type
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_by_parent_description(self, parent_description: str):
|
||||||
|
return [
|
||||||
|
block
|
||||||
|
for block in self.blocks
|
||||||
|
if block.parent_description == parent_description
|
||||||
|
]
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.blocks)
|
||||||
|
|
||||||
|
def __getitem__(self, index):
|
||||||
|
return self.blocks[index]
|
||||||
|
|
||||||
|
def __setitem__(self, index, value):
|
||||||
|
self.blocks[index] = value
|
||||||
|
|
||||||
|
def __delitem__(self, index):
|
||||||
|
del self.blocks[index]
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self.blocks)
|
||||||
|
|
||||||
|
def __reversed__(self):
|
||||||
|
return reversed(self.blocks)
|
||||||
|
|
||||||
|
def __contains__(self, item):
|
||||||
|
return item in self.blocks
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.name}({self.blocks})"
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"{self.name}({self.blocks})"
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.blocks == other.blocks
|
Loading…
Reference in new issue