Skip to content

Commit

Permalink
Re-arrange utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bpiwowar committed Feb 28, 2024
1 parent 3c44a75 commit f13f521
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
9 changes: 7 additions & 2 deletions src/xpmir/utils/iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ def __init__(self, iterator: Iterator[T], maxsize=100):
self.maxsize = maxsize
self.iterator = iterator

def __next__(self):
# (1) Start a process if needed
def start(self):
"""Start the iterator process"""
if self.process is None:
self.queue = mp.Queue(self.maxsize)
self.process = mp.Process(
Expand All @@ -346,6 +346,11 @@ def __next__(self):
# Start, and register a kill switch
self.process.start()
atexit.register(self.kill_subprocess)
return self

def __next__(self):
# Start a process if needed
self.start()

# Get the next element
element = self.queue.get()
Expand Down
29 changes: 29 additions & 0 deletions src/xpmir/utils/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import logging
from functools import cached_property


def easylog():
"""
Returns a logger with the caller's __name__
"""
import inspect

try:
frame = inspect.stack()[1] # caller
module = inspect.getmodule(frame[0])
return logging.getLogger(module.__name__)
except IndexError:
return logging.getLogger("UNKNOWN")


class EasyLogger:
@cached_property
def logger(self):
clsdict = self.__class__.__dict__

logger = clsdict.get("__LOGGER__", None)
if logger is None:
logger = logging.getLogger(self.__class__.__qualname__)
self.__class__.__LOGGER__ = logger

return logger
29 changes: 1 addition & 28 deletions src/xpmir/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
)
import inspect
import logging
from functools import cached_property
import os
from pathlib import Path
import re
Expand All @@ -21,6 +20,7 @@
from experimaestro import SubmitHook, Job, Launcher
from threading import Thread
from xpmir.utils.functools import cache
from xpmir.utils.logging import easylog, EasyLogger # noqa: F401

T = TypeVar("T")

Expand Down Expand Up @@ -143,33 +143,6 @@ def batchiter(batchsize: int, iter: Iterator[T], keeppartial=True) -> Iterator[L
yield samples


def easylog():
"""
Returns a logger with the caller's __name__
"""
import inspect

try:
frame = inspect.stack()[1] # caller
module = inspect.getmodule(frame[0])
return logging.getLogger(module.__name__)
except IndexError:
return logging.getLogger("UNKNOWN")


class EasyLogger:
@cached_property
def logger(self):
clsdict = self.__class__.__dict__

logger = clsdict.get("__LOGGER__", None)
if logger is None:
logger = logging.getLogger(self.__class__.__qualname__)
self.__class__.__LOGGER__ = logger

return logger


@cache
def find_java_home(min_version: int = 6) -> str:
"""Find JAVA HOME"""
Expand Down

0 comments on commit f13f521

Please sign in to comment.