Skip to content

Commit

Permalink
style: formatted everything with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Jun 30, 2020
1 parent 0444be3 commit eda4ff6
Show file tree
Hide file tree
Showing 42 changed files with 948 additions and 429 deletions.
10 changes: 8 additions & 2 deletions aw_core/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
# https://github.com/pypa/pipfile/blob/master/pipfile/__about__.py

__all__ = [
"__title__", "__summary__", "__uri__", "__version__", "__author__",
"__email__", "__license__", "__copyright__",
"__title__",
"__summary__",
"__uri__",
"__version__",
"__author__",
"__email__",
"__license__",
"__copyright__",
]

__title__ = "aw-core"
Expand Down
4 changes: 2 additions & 2 deletions aw_core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def load_config(appname, default_config):

# Override defaults from existing config file
if os.path.isfile(config_file_path):
with open(config_file_path, 'r') as f:
with open(config_file_path, "r") as f:
config.read_file(f)

# Overwrite current config file (necessary in case new default would be added)
Expand All @@ -31,5 +31,5 @@ def load_config(appname, default_config):
def save_config(appname, config):
config_dir = dirs.get_config_dir(appname)
config_file_path = os.path.join(config_dir, "{}.ini".format(appname))
with open(config_file_path, 'w') as f:
with open(config_file_path, "w") as f:
config.write(f)
21 changes: 16 additions & 5 deletions aw_core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ def g(*args, **kwargs):
# TODO: Use logging module instead?
nonlocal warned_for
if not warned_for:
warnings.simplefilter('always', DeprecationWarning) # turn off filter
warnings.warn("Call to deprecated function {}, this warning will only show once per function.".format(f.__name__), category=DeprecationWarning, stacklevel=2)
warnings.simplefilter('default', DeprecationWarning) # reset filter
warnings.simplefilter("always", DeprecationWarning) # turn off filter
warnings.warn(
"Call to deprecated function {}, this warning will only show once per function.".format(
f.__name__
),
category=DeprecationWarning,
stacklevel=2,
)
warnings.simplefilter("default", DeprecationWarning) # reset filter
warned_for = True
return f(*args, **kwargs)

Expand All @@ -37,7 +43,12 @@ def g(*args, **kwargs):
f(*args, **kwargs)
except exception as e:
# TODO: Use warnings module instead?
logging.error("{} crashed due to exception, restarting.".format(f.__name__))
logging.error(
"{} crashed due to exception, restarting.".format(f.__name__)
)
logging.error(e)
time.sleep(delay) # To prevent extremely fast restarts in case of bad state.
time.sleep(
delay
) # To prevent extremely fast restarts in case of bad state.

return g
1 change: 1 addition & 0 deletions aw_core/dirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def wrapper(subpath: Optional[str]) -> str:
path = f(subpath)
ensure_path_exists(path)
return path

return wrapper


Expand Down
54 changes: 36 additions & 18 deletions aw_core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,36 @@ def get_log_file_path() -> Optional[str]: # pragma: no cover
return log_file_path


def setup_logging(name: str, testing=False, verbose=False,
log_stderr=True, log_file=False, log_file_json=False): # pragma: no cover
def setup_logging(
name: str,
testing=False,
verbose=False,
log_stderr=True,
log_file=False,
log_file_json=False,
): # pragma: no cover
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG if verbose else logging.INFO)
root_logger.handlers = []

if log_stderr:
root_logger.addHandler(_create_stderr_handler())
if log_file:
root_logger.addHandler(_create_file_handler(name, testing=testing, log_json=log_file_json))
root_logger.addHandler(
_create_file_handler(name, testing=testing, log_json=log_file_json)
)


def _get_latest_log_files(name, testing=False) -> List[str]: # pragma: no cover
"""Returns a list with the paths of all available logfiles for `name` sorted by latest first."""
log_dir = dirs.get_log_dir(name)
files = filter(lambda filename: name in filename, os.listdir(log_dir))
files = filter(lambda filename: "testing" in filename if testing else "testing" not in filename, files)
files = filter(
lambda filename: "testing" in filename
if testing
else "testing" not in filename,
files,
)
return [os.path.join(log_dir, filename) for filename in sorted(files, reverse=True)]


Expand All @@ -54,7 +67,9 @@ def _create_stderr_handler() -> logging.Handler: # pragma: no cover
return stderr_handler


def _create_file_handler(name, testing=False, log_json=False) -> logging.Handler: # pragma: no cover
def _create_file_handler(
name, testing=False, log_json=False
) -> logging.Handler: # pragma: no cover
log_dir = dirs.get_log_dir(name)

# Set logfile path and name
Expand All @@ -67,7 +82,7 @@ def _create_file_handler(name, testing=False, log_json=False) -> logging.Handler
log_name = name + "_" + ("testing_" if testing else "") + now_str + file_ext
log_file_path = os.path.join(log_dir, log_name)

fh = logging.FileHandler(log_file_path, mode='w')
fh = logging.FileHandler(log_file_path, mode="w")
if log_json:
fh.setFormatter(_create_json_formatter())
else:
Expand All @@ -77,23 +92,26 @@ def _create_file_handler(name, testing=False, log_json=False) -> logging.Handler


def _create_human_formatter() -> logging.Formatter: # pragma: no cover
return logging.Formatter('%(asctime)s [%(levelname)-5s]: %(message)s (%(name)s:%(lineno)s)', '%Y-%m-%d %H:%M:%S')
return logging.Formatter(
"%(asctime)s [%(levelname)-5s]: %(message)s (%(name)s:%(lineno)s)",
"%Y-%m-%d %H:%M:%S",
)


def _create_json_formatter() -> logging.Formatter: # pragma: no cover
supported_keys = [
'asctime',
"asctime",
# 'created',
'filename',
'funcName',
'levelname',
"filename",
"funcName",
"levelname",
# 'levelno',
'lineno',
'module',
"lineno",
"module",
# 'msecs',
'message',
'name',
'pathname',
"message",
"name",
"pathname",
# 'process',
# 'processName',
# 'relativeCreated',
Expand All @@ -103,8 +121,8 @@ def _create_json_formatter() -> logging.Formatter: # pragma: no cover

def log_format(x):
"""Used to give JsonFormatter proper parameter format"""
return ['%({0:s})'.format(i) for i in x]
return ["%({0:s})".format(i) for i in x]

custom_format = ' '.join(log_format(supported_keys))
custom_format = " ".join(log_format(supported_keys))

return jsonlogger.JsonFormatter(custom_format)
35 changes: 27 additions & 8 deletions aw_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ class Event(dict):
Used to represents an event.
"""

def __init__(self, id: Id = None, timestamp: ConvertableTimestamp = None,
duration: Duration = 0, data: Data = dict()) -> None:
def __init__(
self,
id: Id = None,
timestamp: ConvertableTimestamp = None,
duration: Duration = 0,
data: Data = dict(),
) -> None:
self.id = id
if timestamp is None:
logger.warning("Event initializer did not receive a timestamp argument, using now as timestamp")
logger.warning(
"Event initializer did not receive a timestamp argument, using now as timestamp"
)
# FIXME: The typing.cast here was required for mypy to shut up, weird...
self.timestamp = datetime.now(typing.cast(timezone, timezone.utc))
else:
Expand All @@ -55,17 +62,27 @@ def __init__(self, id: Id = None, timestamp: ConvertableTimestamp = None,

def __eq__(self, other: object) -> bool:
if isinstance(other, Event):
return self.timestamp == other.timestamp \
and self.duration == other.duration \
return (
self.timestamp == other.timestamp
and self.duration == other.duration
and self.data == other.data
)
else:
raise TypeError("operator not supported between instances of '{}' and '{}'".format(type(self), type(other)))
raise TypeError(
"operator not supported between instances of '{}' and '{}'".format(
type(self), type(other)
)
)

def __lt__(self, other: object) -> bool:
if isinstance(other, Event):
return self.timestamp < other.timestamp
else:
raise TypeError("operator not supported between instances of '{}' and '{}'".format(type(self), type(other)))
raise TypeError(
"operator not supported between instances of '{}' and '{}'".format(
type(self), type(other)
)
)

def to_json_dict(self) -> dict:
"""Useful when sending data over the wire.
Expand Down Expand Up @@ -119,4 +136,6 @@ def duration(self, duration: Duration) -> None:
elif isinstance(duration, numbers.Real):
self["duration"] = timedelta(seconds=duration) # type: ignore
else:
raise TypeError("Couldn't parse duration of invalid type {}".format(type(duration)))
raise TypeError(
"Couldn't parse duration of invalid type {}".format(type(duration))
)
12 changes: 9 additions & 3 deletions aw_core/timeperiod.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ def duration(self) -> timedelta:

def overlaps(self, other: "TimePeriod") -> bool:
"""Checks if this timeperiod is overlapping partially or entirely with another timeperiod"""
return self.start <= other.start < self.end \
or self.start < other.end <= self.end \
return (
self.start <= other.start < self.end
or self.start < other.end <= self.end
or self in other
)

def intersects(self, other: "TimePeriod") -> bool:
"""Alias for overlaps"""
Expand Down Expand Up @@ -49,7 +51,11 @@ def __lt__(self, other: object) -> bool:
if isinstance(other, TimePeriod):
return self.start < other.start
else:
raise TypeError("operator not supported between instaces of '{}' and '{}'".format(type(self), type(other)))
raise TypeError(
"operator not supported between instaces of '{}' and '{}'".format(
type(self), type(other)
)
)

def intersection(self, other: "TimePeriod") -> Optional["TimePeriod"]:
"""Returns the timeperiod contained in both periods"""
Expand Down
12 changes: 7 additions & 5 deletions aw_core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ class VersionException(Exception):


def _version_info_tuple() -> Tuple[int, int, int]: # pragma: no cover
return (sys.version_info.major,
sys.version_info.minor,
sys.version_info.micro)
return (sys.version_info.major, sys.version_info.minor, sys.version_info.micro)


def assert_version(required_version: Tuple[int, ...] = (3, 5)): # pragma: no cover
actual_version = _version_info_tuple()
if actual_version <= required_version:
raise VersionException(("Python version {} not supported, you need to upgrade your Python" +
" version to at least {}.").format(required_version))
raise VersionException(
(
"Python version {} not supported, you need to upgrade your Python"
+ " version to at least {}."
).format(required_version)
)
logger.debug("Python version: {}".format(_version_info_tuple()))
2 changes: 2 additions & 0 deletions aw_datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# like ellipsises. See here: https://github.com/python/typing/issues/259
def get_storage_methods() -> Dict[str, Callable[[Any], storages.AbstractStorage]]:
from .storages import MemoryStorage, MongoDBStorage, PeeweeStorage, SqliteStorage

methods: Dict[str, Callable[[Any], storages.AbstractStorage]] = {
PeeweeStorage.sid: PeeweeStorage,
MemoryStorage.sid: MemoryStorage,
Expand All @@ -20,6 +21,7 @@ def get_storage_methods() -> Dict[str, Callable[[Any], storages.AbstractStorage]
if _platform.system() == "Linux": # pragma: no branch
try:
import pymongo

methods[MongoDBStorage.sid] = MongoDBStorage
except ImportError: # pragma: no cover
pass
Expand Down
10 changes: 6 additions & 4 deletions aw_datastore/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def create_test_events(n):

events = []
for i in range(n):
events.append(Event(timestamp=now + i * timedelta(seconds=1), data={"label": "asd"}))
events.append(
Event(timestamp=now + i * timedelta(seconds=1), data={"label": "asd"})
)

return events

Expand Down Expand Up @@ -49,14 +51,14 @@ def benchmark(storage: Callable[..., AbstractStorage]):

num_single_events = 50
num_replace_events = 50
num_bulk_events = 2 * 10**3
num_bulk_events = 2 * 10 ** 3
num_events = num_single_events + num_replace_events + num_bulk_events + 1
num_final_events = num_single_events + num_bulk_events + 1

events = create_test_events(num_events)
single_events = events[:num_single_events]
replace_events = events[num_single_events:num_single_events+num_replace_events]
bulk_events = events[num_single_events+num_replace_events:-1]
replace_events = events[num_single_events : num_single_events + num_replace_events]
bulk_events = events[num_single_events + num_replace_events : -1]

print(storage.__name__)

Expand Down
Loading

0 comments on commit eda4ff6

Please sign in to comment.