Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format code with black and isort #34

Merged
merged 4 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions atpbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"disable",
"__version__",
]
from .main import atpbar
from .funcs import find_reporter, register_reporter, flush, disable

from .__about__ import __version__
from .funcs import disable, find_reporter, flush, register_reporter
from .main import atpbar
23 changes: 9 additions & 14 deletions atpbar/funcs.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# Tai Sakuma <tai.sakuma@gmail.com>
import atexit
import multiprocessing
import contextlib
import multiprocessing

from .machine import StateMachine

##__________________________________________________________________||
_machine = StateMachine()

##__________________________________________________________________||

def find_reporter():
"""returns the progress reporter

Expand All @@ -24,7 +22,7 @@ def find_reporter():
"""
return _machine.find_reporter()

##__________________________________________________________________||

def register_reporter(reporter):
"""registers a reporter

Expand All @@ -45,7 +43,7 @@ def register_reporter(reporter):
"""
_machine.register_reporter(reporter)

##__________________________________________________________________||

def flush():
"""flushes progress bars

Expand All @@ -59,7 +57,7 @@ def flush():
"""
_machine.flush()

##__________________________________________________________________||

def disable():
"""disables progress bars

Expand All @@ -73,9 +71,7 @@ def disable():
"""
_machine.disable()

##__________________________________________________________________||

##__________________________________________________________________||
def shutdown():
"""shutdowns the progress bars

Expand All @@ -87,14 +83,13 @@ def shutdown():
_machine.shutdown()


import multiprocessing.queues # This import prevents the issue
# https://github.com/alphatwirl/atpbar/issues/4
# This import prevents the issue
# https://github.com/alphatwirl/atpbar/issues/4
import multiprocessing.queues # noqa: E402 F401

atexit.register(shutdown)

##__________________________________________________________________||

@contextlib.contextmanager
def fetch_reporter():
yield from _machine.fetch_reporter()

##__________________________________________________________________||
45 changes: 25 additions & 20 deletions atpbar/machine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Tai Sakuma <tai.sakuma@gmail.com>
import threading
import multiprocessing
import threading

from .progressreport.reporter import ProgressReporter
from .presentation.create import create_presentation
from .progressreport.pickup import ProgressReportPickup
from .progressreport.reporter import ProgressReporter
from .stream import StreamRedirection, register_stream_queue
from .presentation.create import create_presentation

##__________________________________________________________________||

class StateMachine:
def __init__(self):
self.lock = threading.Lock()
Expand Down Expand Up @@ -37,10 +36,10 @@ def fetch_reporter(self):
self.state = self.state.prepare_reporter()
yield from self.state.fetch_reporter(lock=self.lock)

##__________________________________________________________________||

class State:
"""The base class of the states
"""
"""The base class of the states"""

def prepare_reporter(self):
return self

Expand All @@ -59,7 +58,7 @@ def flush(self):
def shutdown(self):
return self

##__________________________________________________________________||

class Initial(State):
"""Initial state

Expand All @@ -78,16 +77,22 @@ def fetch_reporter(self, lock):
def flush(self):
return Active()


class Active(State):
"""Active state

The pickup started and is running, typically, in the main process
"""
def __init__(self,):

def __init__(
self,
):

self.queue = multiprocessing.Queue()
self.reporter = ProgressReporter(queue=self.queue)
self.reporter.notices_from_sub_processes = self.notices_from_sub_processes = multiprocessing.Queue()
self.reporter.notices_from_sub_processes = self.notices_from_sub_processes = (
multiprocessing.Queue()
)

self.reporter.stream_queue = self.stream_queue = multiprocessing.Queue()

Expand All @@ -103,7 +108,9 @@ def _start_pickup(self):
presentation = create_presentation()
self.pickup = ProgressReportPickup(self.queue, presentation)

self.stream_redirection = StreamRedirection(queue=self.stream_queue, presentation=presentation)
self.stream_redirection = StreamRedirection(
queue=self.stream_queue, presentation=presentation
)
self.stream_redirection.start()

def _end_pickup(self):
Expand Down Expand Up @@ -156,7 +163,7 @@ def shutdown(self):
self._end_pickup()
return Initial()

##__________________________________________________________________||

class Registered(State):
"""Registered state

Expand All @@ -179,16 +186,14 @@ def fetch_reporter(self, lock):
self.reporter.notices_from_sub_processes.put(True)
yield self.reporter


class Disabled(State):
"""Disabled state
"""
"""Disabled state"""

def __init__(self):
self.reporter = None

##__________________________________________________________________||

def in_main_thread():
"""test if in the main thread
"""
"""test if in the main thread"""
return threading.current_thread() == threading.main_thread()

##__________________________________________________________________||
27 changes: 11 additions & 16 deletions atpbar/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Tai Sakuma <tai.sakuma@gmail.com>
import uuid
import contextlib
import logging
import time

import contextlib
import uuid

from .funcs import fetch_reporter

##__________________________________________________________________||

def atpbar(iterable, name=None, time_track=False):
"""returns an instance of `Atpbar`

Expand All @@ -29,16 +27,16 @@
len_ = len(iterable)
except TypeError:
logger = logging.getLogger(__name__)
logging.warning('length is unknown: {!r}'.format(iterable))
logging.warning('atpbar is turned off')
logging.warning("length is unknown: {!r}".format(iterable))
logging.warning("atpbar is turned off")
return iterable

if name is None:
name = repr(iterable)

return Atpbar(iterable, name=name, len_=len_, time_track=time_track)

##__________________________________________________________________||

class Atpbar:
"""Progress bar

Expand Down Expand Up @@ -70,7 +68,7 @@
self.loop_complete = False
self._report_start()
with report_last(pbar=self):
for i, e in enumerate(self. iterable):
for i, e in enumerate(self.iterable):
yield e
self._report_progress(i)
else:
Expand All @@ -80,11 +78,9 @@
if self.reporter is None:
return
try:
report = dict(
taskid=self.id_, name=self.name,
done=0, total=self.len_)
report = dict(taskid=self.id_, name=self.name, done=0, total=self.len_)
if self.time_track:
report['start_time'] = start_time=time.time()
report["start_time"] = start_time = time.time()

Check warning on line 83 in atpbar/main.py

View check run for this annotation

Codecov / codecov/patch

atpbar/main.py#L83

Added line #L83 was not covered by tests
self.reporter.report(report)
except:
pass
Expand All @@ -93,11 +89,12 @@
if self.reporter is None:
return
try:
report = dict(taskid=self.id_, done=(i+1))
report = dict(taskid=self.id_, done=(i + 1))
self.reporter.report(report)
except:
pass


@contextlib.contextmanager
def report_last(pbar):
"""send a last report
Expand All @@ -119,5 +116,3 @@
pbar.reporter.report(report)
except:
pass

##__________________________________________________________________||
Loading
Loading