From 4efb13954c2037126cb6d73757f080fd568e8c67 Mon Sep 17 00:00:00 2001 From: L-Nafaryus Date: Wed, 11 Aug 2021 12:46:35 +0500 Subject: [PATCH] Mod: few changes with imports --- anisotropy/__init__.py | 2 +- anisotropy/core/main.py | 52 ++--------------- anisotropy/core/utils.py | 89 ++++++++++++++++++------------ anisotropy/samples/__init__.py | 6 ++ anisotropy/samples/bodyCentered.py | 4 +- anisotropy/samples/faceCentered.py | 4 +- anisotropy/samples/simple.py | 4 +- tests/__init__.py | 0 tests/test_anisotropy.py | 5 ++ 9 files changed, 79 insertions(+), 87 deletions(-) create mode 100644 tests/__init__.py diff --git a/anisotropy/__init__.py b/anisotropy/__init__.py index ceaa1f1..212c6cc 100644 --- a/anisotropy/__init__.py +++ b/anisotropy/__init__.py @@ -10,4 +10,4 @@ __version__ = "1.1.0" __author__ = __maintainer = "George Kusayko" __email__ = "gkusayko@gmail.com" -from anisotropy.core.main import main +#from anisotropy.core.main import main diff --git a/anisotropy/core/main.py b/anisotropy/core/main.py index 231e9c9..bb52307 100644 --- a/anisotropy/core/main.py +++ b/anisotropy/core/main.py @@ -15,12 +15,12 @@ from anisotropy.core.utils import struct, deepupdate # Environment variables and config ## env = { "ROOT": os.path.abspath(".") } -env.update(dict( +env.update( BUILD = os.path.join(env["ROOT"], "build"), LOG = os.path.join(env["ROOT"], "logs"), DEFAULT_CONFIG = os.path.join(env["ROOT"], "anisotropy/config/default.toml"), CONFIG = os.path.join(env["ROOT"], "conf/config.toml") -)) +) env["db_path"] = env["BUILD"] env["salome_port"] = 2810 @@ -44,60 +44,20 @@ env["salome_port"] = 2810 ### # Logger ## +from anisotropy.core.utils import setupLogger logger_env = env.get("logger", {}) -class CustomFormatter(logging.Formatter): - grey = "\x1b[38;21m" - yellow = "\x1b[33;21m" - red = "\x1b[31;21m" - bold_red = "\x1b[31;1m" - reset = "\x1b[0m" - format = logger_env.get("format", "[ %(asctime)s ] [ %(levelname)s ] %(message)s") - - FORMATS = { - logging.DEBUG: grey + format + reset, - logging.INFO: grey + format + reset, - logging.WARNING: yellow + format + reset, - logging.ERROR: red + format + reset, - logging.CRITICAL: bold_red + format + reset - } - - def format(self, record): - log_fmt = self.FORMATS.get(record.levelno) - formatter = logging.Formatter(log_fmt) - - return formatter.format(record) - logger = logging.getLogger(logger_env.get("name", "anisotropy")) -logger.setLevel(logging.INFO) - -sh = logging.StreamHandler() -sh.setLevel(logging.INFO) -sh.setFormatter(CustomFormatter()) - -fh = logging.FileHandler(os.path.join(env["LOG"], logger_env.get("name", "anisotropy"))) -fh.setLevel(logging.DEBUG) -fh.setFormatter(CustomFormatter()) - -logger.addHandler(sh) -logger.addHandler(fh) +setupLogger(logger, logging.INFO) peeweeLogger = logging.getLogger("peewee") peeweeLogger.setLevel(logging.INFO) -def timer(func): - def inner(*args, **kwargs): - start = time.monotonic() - ret = func(*args, **kwargs) - elapsed = time.monotonic() - start - - return ret, elapsed - - return inner - +from anisotropy.core.utils import timer from anisotropy import __version__ from anisotropy import salomepl from anisotropy import openfoam +from anisotropy.samples import Simple, FaceCentered, BodyCentered from math import sqrt from peewee import JOIN diff --git a/anisotropy/core/utils.py b/anisotropy/core/utils.py index 4666bb4..b80dfd3 100644 --- a/anisotropy/core/utils.py +++ b/anisotropy/core/utils.py @@ -2,7 +2,48 @@ import logging from multiprocessing import Queue, Process, cpu_count import socket +import copy +import time +from types import FunctionType +import os +class CustomFormatter(logging.Formatter): + grey = "\x1b[38;21m" + yellow = "\x1b[33;21m" + red = "\x1b[31;21m" + bold_red = "\x1b[31;1m" + reset = "\x1b[0m" + format = "[ %(asctime)s ] [ %(levelname)s ] %(message)s" + + FORMATS = { + logging.DEBUG: grey + format + reset, + logging.INFO: grey + format + reset, + logging.WARNING: yellow + format + reset, + logging.ERROR: red + format + reset, + logging.CRITICAL: bold_red + format + reset + } + + def format(self, record): + log_fmt = self.FORMATS.get(record.levelno) + formatter = logging.Formatter(log_fmt) + + return formatter.format(record) + +def setupLogger(logger, level: int): + logger.setLevel(level) + + sh = logging.StreamHandler() + sh.setLevel(level) + sh.setFormatter(CustomFormatter()) + + fh = logging.FileHandler(os.path.join("logs", logger.name)) + fh.setLevel(level) + fh.setFormatter(CustomFormatter()) + + logger.addHandler(sh) + logger.addHandler(fh) + + return logger class struct: def __init__(self, *args, **kwargs): @@ -42,7 +83,6 @@ class struct: def __repr__(self): return str(self) -import copy def deepupdate(target, src): for k, v in src.items(): @@ -64,44 +104,25 @@ def deepupdate(target, src): else: target[k] = copy.copy(v) -class Logger: - def __init__(self, name, logpath): - logging.basicConfig( - level = logging.INFO, - format = "%(levelname)s: %(message)s", - handlers = [ - logging.StreamHandler(), - logging.FileHandler(logpath) - ] - ) - self.logger = logging.getLogger(name) - self.warnings = 0 - self.errors = 0 - self.criticals = 0 - self.exceptions = 0 - - def info(self, *args): - self.logger.info(*args) - def warning(self, *args): - self.warnings += 1 - self.logger.warning(*args) +def timer(func: FunctionType) -> (tuple, float): + """(Decorator) Returns output of inner function and execution time + + :param func: inner function + :type: FunctionType - def error(self, *args): - self.errors += 1 - self.logger.error(*args) + :return: output, elapsed time + :rtype: tuple(tuple, float) + """ + def inner(*args, **kwargs): + start = time.monotonic() + ret = func(*args, **kwargs) + elapsed = time.monotonic() - start - def critical(self, *args): - self.criticals += 1 - self.logger.critical(*args) + return ret, elapsed - def exception(self, *args): - self.exceptions += 1 - self.logger.exception(*args) - - def fancyline(self): - self.logger.info("-" * 80) + return inner diff --git a/anisotropy/samples/__init__.py b/anisotropy/samples/__init__.py index e69de29..b522cae 100644 --- a/anisotropy/samples/__init__.py +++ b/anisotropy/samples/__init__.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from anisotropy.samples.simple import Simple +from anisotropy.samples.bodyCentered import BodyCentered +from anisotropy.samples.faceCentered import FaceCentered diff --git a/anisotropy/samples/bodyCentered.py b/anisotropy/samples/bodyCentered.py index fea760f..c74698f 100644 --- a/anisotropy/samples/bodyCentered.py +++ b/anisotropy/samples/bodyCentered.py @@ -1,4 +1,5 @@ from math import pi, sqrt +from anisotropy.salomepl import geometry class BodyCentered(object): def __init__(self, **kwargs): @@ -13,9 +14,8 @@ class BodyCentered(object): def build(self): - import salomepl - geompy = salomepl.geometry.getGeom() + geompy = geometry.getGeom() ### # Pore Cell diff --git a/anisotropy/samples/faceCentered.py b/anisotropy/samples/faceCentered.py index 74611f0..222537f 100644 --- a/anisotropy/samples/faceCentered.py +++ b/anisotropy/samples/faceCentered.py @@ -1,4 +1,5 @@ from math import pi, sqrt +from anisotropy.salomepl import geometry class FaceCentered(object): def __init__(self, **kwargs): @@ -13,9 +14,8 @@ class FaceCentered(object): def build(self): - import salomepl - geompy = salomepl.geometry.getGeom() + geompy = geometry.getGeom() ### # Pore Cell diff --git a/anisotropy/samples/simple.py b/anisotropy/samples/simple.py index 7cc2495..d72d3e1 100644 --- a/anisotropy/samples/simple.py +++ b/anisotropy/samples/simple.py @@ -1,4 +1,5 @@ from math import pi, sqrt +from anisotropy.salomepl import geometry class Simple(object): def __init__(self, **kwargs): @@ -13,9 +14,8 @@ class Simple(object): def build(self): - import salomepl - geompy = salomepl.geometry.getGeom() + geompy = geometry.getGeom() ### # Pore Cell diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_anisotropy.py b/tests/test_anisotropy.py index c49d175..784e56e 100644 --- a/tests/test_anisotropy.py +++ b/tests/test_anisotropy.py @@ -26,6 +26,7 @@ class TestAnisotropy(unittest.TestCase): except Exception as e: passed = False + print(e) self.assertTrue(passed) @@ -35,6 +36,10 @@ class TestAnisotropy(unittest.TestCase): self.assertEqual(self.model.params["structure"]["type"], "simple") + def tearDown(self): + #os.removedirs(self.model.env["BUILD"]) + #os.removedirs(self.model.env["LOG"]) + pass if __name__ == "__main__": unittest.main()