Metadata
Summary
The following is a short reminder of what was previously described in the Getting Started part.
The Agent Class features various metadata (attributes), used to identify and describe him.
By default, an Agent has 4 attributes, specified in the file base.py
.
NAME
: Agent NameACTIVATED
: Whether this agent is enabled or not,False
by defaultVERSION
: Version, used for Agent versionningMISSION
: Short description for its mission, its usage
Every Agent inherits the attributes of the Base
Class. You can override these attributes by redefining them within the Agent Class.
class MyParserAgent(Base):
NAME = "MyParser"
ACTIVATED = True
VERSION = "1.0"
MISSION = "Yet another parser to parse awesome stuff"
Adding Metadata
To add metadata (attribute), you have two options: globally or within the Agent. This is about attribute's Scope.
- Global Scope
- Agent Scope
To add an attribute globally, with repercussions in every Agent, add it to the Base
Class, located on agents/base.py
.
class Base(ABC):
NAME = "Base Agent"
ACTIVATED = False
VERSION = "0.0"
MISSION = "No description"
FILE = None
YOUR_ATTRIBUTE = "HELLO WORLD!" # New attribute
Access the attribute using self.YOUR_ATTRIBUTE
in your Agent.
As seen previously, you don't need to override an attribute if you just want its value.
For instance, you can access self.YOUR_ATTRIBUTE
within your Agent without declaring it in its attributes.
class MyParserAgent(Base):
NAME = "MyParser"
ACTIVATED = True
VERSION = "1.0"
MISSION = "Yet another parser to parse awesome stuff"
FILE = None
def mission(self):
self.YOUR_ATTRIBUTE # You can access YOUR_ATTRIBUTE here, declared only in Base Class
You can however declare it within the Agent Class to override the value in the Base
Class.
To add an attribute within an Agent, simply define it in its Class.
class MyParserAgent(Base):
NAME = "MyParser"
ACTIVATED = True
VERSION = "1.0"
MISSION = "Yet another parser to parse awesome stuff"
FILE = None
YOUR_ATTRIBUTE = "HELLO WORLD!" # New attribute
Access the new attribute using self.YOUR_ATTRIBUTE
.
Real-World Example
In this real-world example, we will create a global attribute called EXTENSIONS
.
To introduce only concept at a time, we will filter within the Agent mission
using the attribute.
Please refer to Captain Filtering Agents to optimize the filtering of Agents based on their attributes: Captain should do the filtering.
Add the attribute to the Base
Class.
class Base(ABC):
NAME = "Base Agent"
ACTIVATED = False
VERSION = "0.0"
MISSION = "No description"
EXTENSIONS = None
By default, Agent will inherits the attribute EXTENSIONS
with value None
, meaning they can parse any file type.
If an Agent can only process specific file type, we will override the EXTENSIONS
attribute from within Agent.
- Use default value
- Override attribute value
If your Agent can process every file type, don't redeclare EXTENSIONS
attribute.
class MyParserAgent(Base):
NAME = "MyParser"
ACTIVATED = True
VERSION = "1.0"
MISSION = "Yet another parser to parse awesome stuff"
FILE = None
For instance, if your Agent can only process zip
and tar
files, override the default None
value within Agent Class.
class MyParserAgent(Base):
NAME = "MyParser"
ACTIVATED = True
VERSION = "1.0"
MISSION = "Yet another parser to parse awesome stuff"
FILE = None
EXTENSIONS = ["zip", "tar"]
You are now able to use EXTENSIONS
attribute in Agent's mission
function to have a different behavior based on the value.
For instance with pseudo-code:
def mission(self):
if self.EXTENSIONS:
if not any(ext == self.FILE.split('.')[-1] for ext in self.EXTENSIONS): # If file extension is not any of EXTENSIONS element
print(f'Cannot process this type of file')