-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Saving runtime configuration in root directory automatically (#23)
- Loading branch information
Showing
5 changed files
with
108 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Manage the id for each runtime""" | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
from agentscope.utils.tools import _get_timestamp | ||
from agentscope.utils.tools import _generate_random_code | ||
|
||
_RUNTIME_ID_FORMAT = "run_%Y%m%d-%H%M%S_{}" | ||
_RUNTIME_TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M:%S" | ||
|
||
|
||
class Runtime: | ||
"""Used to record the runtime information.""" | ||
class _Runtime: | ||
"""A singleton class used to record the runtime information, which will | ||
be initialized when the package is imported.""" | ||
|
||
project: str = None | ||
"""The project name, which is used to identify the project.""" | ||
|
||
name: str = None | ||
"""The name for runtime, which is used to identify this runtime.""" | ||
|
||
runtime_id: str = _get_timestamp(_RUNTIME_ID_FORMAT).format( | ||
_generate_random_code(), | ||
) | ||
runtime_id: str = None | ||
"""The id for runtime, which is used to identify the this runtime and | ||
name the saving directory.""" | ||
name the saving directory.""" | ||
|
||
_timestamp: datetime = datetime.now() | ||
"""The timestamp of when the runtime is initialized.""" | ||
|
||
_instance = None | ||
|
||
def __new__(cls, *args: Any, **kwargs: Any) -> Any: | ||
"""Create a singleton instance.""" | ||
if not cls._instance: | ||
cls._instance = super(_Runtime, cls).__new__( | ||
cls, | ||
*args, | ||
**kwargs, | ||
) | ||
return cls._instance | ||
|
||
def __init__(self) -> None: | ||
"""Generate random project name, runtime name and default | ||
runtime_id when the package is initialized. After that, user can set | ||
them by calling `agentscope.init(project="xxx", name="xxx", | ||
runtime_id="xxx")`.""" | ||
|
||
self.project = _generate_random_code() | ||
self.name = _generate_random_code(uppercase=False) | ||
|
||
# Obtain time from timestamp in string format, and then turn it into | ||
# runtime ID format | ||
self.runtime_id = _get_timestamp( | ||
_RUNTIME_ID_FORMAT, | ||
self._timestamp, | ||
).format(self.name) | ||
|
||
@property | ||
def timestamp(self) -> str: | ||
"""Get the current timestamp in specific format.""" | ||
return self._timestamp.strftime(_RUNTIME_TIMESTAMP_FORMAT) | ||
|
||
|
||
_runtime = _Runtime() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters