Skip to content

Commit

Permalink
Grouped utilities into the utils.py package
Browse files Browse the repository at this point in the history
  • Loading branch information
NikosDelijohn committed Sep 16, 2024
1 parent f9ba6cf commit 048a1c0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 60 deletions.
15 changes: 1 addition & 14 deletions src/a0.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# SPDX-License-Identifier: MIT

import os
import sys
import logging
import pathlib
import shutil
import subprocess
Expand All @@ -15,18 +13,7 @@
import csv
import time

# TEMPORARY
log = logging.getLogger("testcrush logger")
log.setLevel(logging.DEBUG)
log_stream = logging.StreamHandler(stream=sys.stdout)
log_stream.setLevel(logging.INFO)
log_stream.setFormatter(logging.Formatter('[%(levelname)s]: %(message)s'))
log_file = logging.FileHandler(filename="debug.log", mode='w')
log_file.setLevel(logging.DEBUG)
log_file.setFormatter(logging.Formatter(
'%(lineno)d:[%(levelname)s|%(module)s|%(funcName)s]: %(message)s'))
log.addHandler(log_stream)
log.addHandler(log_file)
from utils import log


def compile_assembly(*instructions, exit_on_error: bool = False) -> bool:
Expand Down
32 changes: 1 addition & 31 deletions src/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,14 @@
# SPDX-License-Identifier: MIT

import pathlib
import logging
import sys
import re
import tempfile
import random
import shutil

from utils import Singleton, log
from dataclasses import dataclass

# TEMPORARY
log = logging.getLogger("testcrush logger")
log.setLevel(logging.DEBUG)
log_stream = logging.StreamHandler(stream=sys.stdout)
log_stream.setLevel(logging.INFO)
log_stream.setFormatter(logging.Formatter('[%(levelname)s]: %(message)s'))
log_file = logging.FileHandler(filename="debug.log", mode='w')
log_file.setLevel(logging.DEBUG)
log_file.setFormatter(logging.Formatter(
'%(lineno)d:[%(levelname)s|%(module)s|%(funcName)s]: %(message)s'))
log.addHandler(log_stream)
log.addHandler(log_file)


@dataclass
class Codeline:
Expand Down Expand Up @@ -113,22 +99,6 @@ def __eq__(self, other: 'Codeline|int') -> bool:
raise TypeError(f"Unsupported type for ==: {type(other)}")


class Singleton(type):

_instances = {}

def __call__(cls, *args, **kwargs):

if cls not in cls._instances:

log.debug(f"Singleton {cls.__name__} created!")

instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance

return cls._instances[cls]


class ISA(metaclass=Singleton):
"""**Singleton** class to provide utilities for the considered ISA."""

Expand Down
61 changes: 61 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python3
# SPDX-License-Identifier: MIT

import time
import logging
import sys

log = logging.getLogger("testcrush logger")
log.setLevel(logging.DEBUG)

log_stream = logging.StreamHandler(stream=sys.stdout)
log_stream.setLevel(logging.INFO)
log_stream.setFormatter(logging.Formatter('[%(levelname)s]: %(message)s'))

log_file = logging.FileHandler(filename="testcrush_debug.log", mode='w')
log_file.setLevel(logging.DEBUG)
log_file.setFormatter(logging.Formatter('%(lineno)d:[%(levelname)s|%(module)s|%(funcName)s]: %(message)s'))

log.addHandler(log_stream)
log.addHandler(log_file)


class Timer():
"""
Context manager style timer. To be used as: ``with Timer():``
"""

def __enter__(self):

self.start = time.perf_counter()
return self

def __exit__(self, *args):

self.end = time.perf_counter()
self.interval = self.end - self.start
print(f"Execution time: {self.format_time(self.interval)}")

def format_time(self, seconds):

days, remainder = divmod(seconds, 86_400) # 86400 seconds in a day
hours, remainder = divmod(remainder, 3_600) # 3600 seconds in an hour
minutes, seconds = divmod(remainder, 60) # 60 seconds in a minute
return f"{int(days)}d {int(hours)}h {int(minutes)}m {seconds:.2f}s"


class Singleton(type):
"""
Singleton design pattern. To be used as a metaclass: ``class A(metaclass = Singleton)``
"""

_instances = {}

def __call__(cls, *args, **kwargs):

if cls not in cls._instances:

instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance

return cls._instances[cls]
16 changes: 1 addition & 15 deletions src/zoix.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
#!/usr/bin/python3
# SPDX-License-Identifier: MIT

import sys
import subprocess
import logging
import re
import enum
import pathlib
import csv
from utils import log
from typing import Any

# TEMPORARY
log = logging.getLogger("testcrush logger")
log.setLevel(logging.DEBUG)
log_stream = logging.StreamHandler(stream=sys.stdout)
log_stream.setLevel(logging.INFO)
log_stream.setFormatter(logging.Formatter('[%(levelname)s]: %(message)s'))
log_file = logging.FileHandler(filename="debug.log", mode='w')
log_file.setLevel(logging.DEBUG)
log_file.setFormatter(logging.Formatter(
'%(lineno)d:[%(levelname)s|%(module)s|%(funcName)s]: %(message)s'))
log.addHandler(log_stream)
log.addHandler(log_file)


class Compilation(enum.Enum):
"""Statuses for the VCS compilation of HDL sources."""
Expand Down

0 comments on commit 048a1c0

Please sign in to comment.