From 5559d37e57fe443cc3c532de703f6065a4729c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Wed, 18 May 2022 15:20:58 +0200 Subject: [PATCH] version 0.20.4 --- CHANGELOG.md | 4 ++++ soil/VERSION | 2 +- soil/agents/__init__.py | 7 +++++-- soil/environment.py | 15 +++++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb7a34f..9023162 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.20.4] +### Added +* Agents can now be given any kwargs, which will be used to set their state +* Environments have a default logger `self.logger` and a log method, just like agents ## [0.20.3] ### Fixed * Default state values are now deepcopied again. diff --git a/soil/VERSION b/soil/VERSION index 3428dd4..a4e543e 100644 --- a/soil/VERSION +++ b/soil/VERSION @@ -1 +1 @@ -0.20.3 \ No newline at end of file +0.20.4 \ No newline at end of file diff --git a/soil/agents/__init__.py b/soil/agents/__init__.py index e7c3b6c..0602d32 100644 --- a/soil/agents/__init__.py +++ b/soil/agents/__init__.py @@ -35,7 +35,9 @@ class BaseAgent(Agent): unique_id, model, name=None, - interval=None): + interval=None, + **kwargs + ): # Check for REQUIRED arguments # Initialize agent parameters if isinstance(unique_id, Agent): @@ -52,7 +54,8 @@ class BaseAgent(Agent): if hasattr(self, 'level'): self.logger.setLevel(self.level) - + for (k, v) in kwargs.items(): + setattr(self, k, v) # TODO: refactor to clean up mesa compatibility @property diff --git a/soil/environment.py b/soil/environment.py index 6df44be..25d8338 100644 --- a/soil/environment.py +++ b/soil/environment.py @@ -5,6 +5,7 @@ import math import random import yaml import tempfile +import logging import pandas as pd from time import time as current_time from copy import deepcopy @@ -101,6 +102,8 @@ class Environment(Model): environment_agents = agents._convert_agent_types(distro) self.environment_agents = environment_agents + self.logger = utils.logger.getChild(self.name) + @property def now(self): if self.schedule: @@ -198,6 +201,18 @@ class Environment(Model): start = start or self.now return self.G.add_edge(agent1, agent2, **attrs) + def log(self, message, *args, level=logging.INFO, **kwargs): + if not self.logger.isEnabledFor(level): + return + message = message + " ".join(str(i) for i in args) + message = " @{:>3}: {}".format(self.now, message) + for k, v in kwargs: + message += " {k}={v} ".format(k, v) + extra = {} + extra['now'] = self.now + extra['unique_id'] = self.name + return self.logger.log(level, message, extra=extra) + def step(self): super().step() self.schedule.step()