Implements Better Tests and Logging

This commit is contained in:
2023-03-01 02:49:28 -03:00
parent 32ca95086a
commit aea34bf214
17 changed files with 1147 additions and 72 deletions

View File

@@ -1,9 +1,8 @@
from dataclasses import dataclass, field, fields
from dataclasses import dataclass, field, fields, asdict
@dataclass
class Config:
mem: int
level: str
n_atoms: int
@@ -31,13 +30,11 @@ class Config:
f'Memory must be a integer greater than 0.'
)
if self.level is None:
raise ValueError(
f'Invalid value for level. Level must not be none.'
)
if self.n_atoms is None or self.n_atoms <= 0:
raise ValueError(
f'Invalid value for n_atoms: {self.mem},'
f'Number of Atoms must be a integer greater than 0.'
)
def to_dict(self):
return asdict(self)

View File

@@ -18,6 +18,7 @@ class Atom:
ry: float,
rz: float,
na: int = None,
chg: float = None,
symbol: str = None,
) -> None:
@@ -42,5 +43,5 @@ class Atom:
self.rx = rx
self.ry = ry
self.rz = rz
self.chg = None
self.chg = chg
self.mass = atom_mass[self.na]

View File

@@ -2,4 +2,4 @@ import time
def weekday_date_time():
return time.strftime("%A, %d %b %Y at %H:%M:%S")
return time.strftime("%A, %d %b %Y at %H:%M:%S")

View File

@@ -1,45 +1,49 @@
import sys
from crystalpol.shared.config import Config
from crystalpol.shared.utils import weekday_date_time
import logging
from crystalpol.shared.utils import weekday_date_time
import sys
class Log:
@staticmethod
def make_header(version: str, config_dict: dict):
logging.info(
def make_header(version: str, config_dict: dict, logger=None):
if logger is None:
logger = logging.getLogger()
logger.info(
f"##########################################################################################\n"
f"############## Welcome to CRYSTALPOL version {version} ##############\n"
f"##########################################################################################\n"
)
logging.info(f"Your python version is {sys.version}\n")
logging.info(f"Program started on {weekday_date_time()}\n")
logger.info(f"Your python version is {sys.version}\n")
logger.info(f"Program started on {weekday_date_time()}\n")
logging.info("------------------------------------------------------------------------------------------")
logging.info(" CRYSTALPOL variables being used in this run: ")
logging.info("------------------------------------------------------------------------------------------\n")
logger.info("------------------------------------------------------------------------------------------")
logger.info(" CRYSTALPOL variables being used in this run: ")
logger.info("------------------------------------------------------------------------------------------\n")
for key, value in config_dict.items():
logging.info(f"\t{key} = {(key if key else 'Not set')}")
logger.info(f"\t{key} = {(value if value else 'Not set')}")
logging.info("------------------------------------------------------------------------------------------")
logging.info(f" RUN Results: ")
logging.info("------------------------------------------------------------------------------------------\n")
logger.info("------------------------------------------------------------------------------------------")
logger.info(f" RUN Results: ")
logger.info("------------------------------------------------------------------------------------------\n")
@staticmethod
def make_run(cycle, max_charge_diff, charge_diff, crystal):
logging.info(f"cycle: {cycle}")
logging.info(f"\nMax charge diff: {max_charge_diff}")
logging.info(f"Charge Diff: {charge_diff}\n")
def make_run(cycle, max_charge_diff, charge_diff, crystal, logger=None):
logging.info("------------------------------------------------------------------------------------------")
logging.info(f" S rx ry rz chg ")
logging.info("------------------------------------------------------------------------------------------")
if logger is None:
logger = logging.getLogger()
logger.info(f"cycle: {cycle}")
logger.info(f"\nMax charge diff: {max_charge_diff:.5f}")
logger.info(f"Charge Diff: {charge_diff}\n")
logger.info(f"------------------------------------------------------------------------------------------")
logger.info(f" S rx ry rz chg ")
logger.info(f"------------------------------------------------------------------------------------------")
for atom in crystal[0][0]:
logging.info(f" {atom.symbol} {atom.rx} {atom.ry} {atom.rz} {atom.chg} ")
logging.info("\n------------------------------------------------------------------------------------------\n")
logger.info(
f" {atom.symbol.rjust(2)} {float(atom.rx):.6f} {float(atom.ry):.6f} {float(atom.rz):.6f} {float(atom.chg):.6f} ")
logger.info("\n------------------------------------------------------------------------------------------\n")