Base
Base classes, mixins, and utilities for exporting data.
Each exporter should inherit from the BaseExporter
class and implement the export
method. The export
method should return the exported content as an instance of the ExporterReturnType
typed dictionary.
Bases: TypedDict
Exporter Return Type.
Attributes:
Name | Type | Description |
---|---|---|
content | Optional[str] | The exported content. |
imports | Optional[List[Tuple[str, ImportPosition]]] | The additional imports required for the exported content. |
environment_variables | Optional[List[Tuple[str, str]]] | The environment variables to set. |
before_export | Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]] | The exported content before the main export and its position. |
after_export | Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]] | The exported content after the main export and its position. |
Bases: ABC
Base exporter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args | Any | The positional arguments. | () |
**kwargs | Any | The keyword arguments. | {} |
Source code in waldiez/exporting/base/base_exporter.py
export() -> ExporterReturnType
abstractmethod
¶
generate() -> Optional[str]
¶
get_after_export() -> Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]]
¶
Generate the content after the main export.
Returns:
Type | Description |
---|---|
Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]] | The exported content after the main export and its position. |
Source code in waldiez/exporting/base/base_exporter.py
get_before_export() -> Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]]
¶
Generate the content before the main export.
Returns:
Type | Description |
---|---|
Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]] | The exported content before the main export and its position. |
Source code in waldiez/exporting/base/base_exporter.py
get_environment_variables() -> Optional[List[Tuple[str, str]]]
¶
get_imports() -> Optional[List[Tuple[str, ImportPosition]]]
¶
"Generate the imports string for the exporter.
Returns:
Type | Description |
---|---|
Optional[Tuple[str, ImportPosition]] | The exported imports and the position of the imports. |
Source code in waldiez/exporting/base/base_exporter.py
Export position enum.
ExportPosition(position: ExportPositions, order: int = 0)
dataclass
¶
Export position.
Optionally, the order can be provided to sort the exported content.
ExportPositions
¶
Bases: Enum
Export position.
Attributes:
Name | Type | Description |
---|---|---|
TOP | int | The top of the export (name, comments etc.) |
IMPORTS | int | The imports section. |
MODELS | int | The models section (define the llm_configs). |
SKILLS | int | The skills section (generate the skill files, and import them) |
AGENTS | int | The agents section. |
CHATS | int | The chats section (e.g. agent.initiate_chat, or initiate_chats) |
BOTTOM | int | The bottom part of the export (like the main function and calling it). |
Agent position generation.
AgentPosition(agent: Optional[WaldiezAgent], position: AgentPositions, order: int = 0)
dataclass
¶
Agent position.
Attributes:
Name | Type | Description |
---|---|---|
agent | Optional[WaldiezAgent] | The agent. |
position | AgentPositions | The position. |
order | int | The order of the agent position. |
Raises:
Type | Description |
---|---|
ValueError | If the position is not "BEFORE_ALL" or "AFTER_ALL" and the agent is not provided. |
The base exporter mixin.
ExporterMixin
¶
Static methods to be used by the exporters.
comment(for_notebook: bool, hashtags: int = 1) -> str
staticmethod
¶
Comment the text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
for_notebook | bool | Whether the comment is for a notebook or not. | required |
hashtags | int | The number of hashtags (for notebooks), by default 1. | 1 |
Returns:
Type | Description |
---|---|
str | The commented text. |
Source code in waldiez/exporting/base/mixin.py
get_comment(key: CommentKey, for_notebook: bool) -> str
staticmethod
¶
Get the comment string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key | CommentKey | The comment key. | required |
for_notebook | bool | Whether the comment is for a notebook or not. | required |
Returns:
Type | Description |
---|---|
str | The comment string. |
Source code in waldiez/exporting/base/mixin.py
get_valid_instance_name(instance: Tuple[str, str], current_names: Dict[str, str], prefix: str = 'w', max_length: int = 64) -> Dict[str, str]
staticmethod
¶
Get a valid instance name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance | Tuple[str, str] | The instance id and possible name. | required |
current_names | Dict[str, str] | The current names. | required |
prefix | str | The prefix for the instance name, by default "w". | 'w' |
max_length | int | The maximum length of the variable name, by default 64 | 64 |
Returns:
Type | Description |
---|---|
Dict[str, str] | The updated dictionary of current names. |
Source code in waldiez/exporting/base/mixin.py
path_resolver(path: str) -> str
staticmethod
¶
serializer(item: Any, tabs: int = 1) -> str
staticmethod
¶
Get the string representation of an item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item | Any | The item. | required |
tabs | int | The number of tabs for indentation, by default 1. | 1 |
Returns:
Type | Description |
---|---|
str | The string representation of the item. |
Source code in waldiez/exporting/base/mixin.py
Generic utils to be used for exporting.
CommentKey = Literal['agents', 'imports', 'skills', 'models', 'nested', 'run', 'logging']
module-attribute
¶
Possible keys for comments.
comment(for_notebook: bool, hashtags: int = 1) -> str
¶
Get the comment string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
for_notebook | bool | Whether the comment is for a notebook or not. | required |
hashtags | int | The number of hashtags (for notebooks), by default 1. | 1 |
Returns:
Type | Description |
---|---|
str | The comment string. |
Source code in waldiez/exporting/base/utils/comments.py
comments
¶
CommentKey = Literal['agents', 'imports', 'skills', 'models', 'nested', 'run', 'logging']
module-attribute
¶
Possible keys for comments.
comment(for_notebook: bool, hashtags: int = 1) -> str
¶
Get the comment string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
for_notebook | bool | Whether the comment is for a notebook or not. | required |
hashtags | int | The number of hashtags (for notebooks), by default 1. | 1 |
Returns:
Type | Description |
---|---|
str | The comment string. |
Source code in waldiez/exporting/base/utils/comments.py
get_comment(key: CommentKey, for_notebook: bool) -> str
¶
Get a comment string for some common keys.
The key is a heading (in a notebook) or just a comment (in a script).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key | agents | imports | skills | models | nested | run | logging | The key. | required |
for_notebook | bool | Whether the comment is for a notebook. | required |
Returns:
Type | Description |
---|---|
str | The comment string. |
Source code in waldiez/exporting/base/utils/comments.py
get_comment(key: CommentKey, for_notebook: bool) -> str
¶
Get a comment string for some common keys.
The key is a heading (in a notebook) or just a comment (in a script).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key | agents | imports | skills | models | nested | run | logging | The key. | required |
for_notebook | bool | Whether the comment is for a notebook. | required |
Returns:
Type | Description |
---|---|
str | The comment string. |
Source code in waldiez/exporting/base/utils/comments.py
get_escaped_string(string: str) -> str
¶
get_item_string(item: Any, tabs: int = 1) -> str
¶
Convert an item to a formatted string with given indentation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item | Any | The item to convert. | required |
tabs | int | The number of tabs, by default 1. | 1 |
Returns:
Type | Description |
---|---|
str | The formatted string. |
Example
Source code in waldiez/exporting/base/utils/to_string.py
get_path_string(path: str) -> str
¶
get_valid_instance_name(instance: Tuple[str, str], current_names: Dict[str, str], prefix: str = 'w', max_length: int = MAX_VARIABLE_LENGTH) -> Dict[str, str]
¶
Get a valid instance name.
If the instance id is already in the current names nothing is done. If the name already exists in the current names, the name is updated (with an index suffix).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance | Tuple[str, str] | The instance id and possible name. | required |
current_names | Dict[str, str] | The current names. | required |
prefix | str | The prefix to use if the name starts with a digit, if the name is already in the current names, or if the name is already in the current names with an index suffix. | 'w' |
max_length | int | The maximum length of the variable name. | MAX_VARIABLE_LENGTH |
Returns:
Type | Description |
---|---|
Dict[str, str] | The updated names. |
Source code in waldiez/exporting/base/utils/naming.py
naming
¶
Utilities for naming.
Functions:
Name | Description |
---|---|
get_valid_python_variable_name | Make sure a string is a valid Python variable name. |
get_valid_instance_name | Get a valid instance name. |
get_escaped_string | Get a string with escaped quotes and newlines. |
get_escaped_string(string: str) -> str
¶
get_valid_instance_name(instance: Tuple[str, str], current_names: Dict[str, str], prefix: str = 'w', max_length: int = MAX_VARIABLE_LENGTH) -> Dict[str, str]
¶
Get a valid instance name.
If the instance id is already in the current names nothing is done. If the name already exists in the current names, the name is updated (with an index suffix).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance | Tuple[str, str] | The instance id and possible name. | required |
current_names | Dict[str, str] | The current names. | required |
prefix | str | The prefix to use if the name starts with a digit, if the name is already in the current names, or if the name is already in the current names with an index suffix. | 'w' |
max_length | int | The maximum length of the variable name. | MAX_VARIABLE_LENGTH |
Returns:
Type | Description |
---|---|
Dict[str, str] | The updated names. |
Source code in waldiez/exporting/base/utils/naming.py
get_valid_python_variable_name(possible: str, prefix: str = 'w', max_length: int = MAX_VARIABLE_LENGTH) -> str
¶
Get a valid Python variable name from a possible name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
possible | str | The possible name. | required |
prefix | str | The prefix to use if the name starts with a digit or special character | 'w' |
max_length | int | The maximum length of the variable name. | MAX_VARIABLE_LENGTH |
Returns:
Type | Description |
---|---|
str | The valid Python variable name. |
Source code in waldiez/exporting/base/utils/naming.py
path_check
¶
to_string
¶
Utilities for converting items to strings.
To be used with dicts and/or lists.
get_item_string(item: Any, tabs: int = 1) -> str
¶
Convert an item to a formatted string with given indentation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item | Any | The item to convert. | required |
tabs | int | The number of tabs, by default 1. | 1 |
Returns:
Type | Description |
---|---|
str | The formatted string. |
Utilities for comments.
comment Get a comment string. get_comment Get a comment string for some common keys (notebook headings).