Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: Publish to PyPI

on:
workflow_call:
workflow_dispatch:
inputs:
tag_name:
required: true
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ lgbt/dist
lgbt/lgbt.egg-info
lgbt/build
__pycache__
.github
.github
.pytest_cache
Binary file modified lgbt/lgbt/__pycache__/lgbt.cpython-310.pyc
Binary file not shown.
Binary file modified lgbt/lgbt/__pycache__/lgbt.cpython-38.pyc
Binary file not shown.
37 changes: 30 additions & 7 deletions lgbt/lgbt/lgbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ def __c__(str, color, background=None):
return f"{COLOURS[color]}{str}{RESET}"


def translate_time(sec):
total_seconds = int(sec)
if total_seconds > 3600:
hours = total_seconds // 3600
remaining_seconds = total_seconds % 3600
minutes = remaining_seconds // 60
seconds = remaining_seconds % 60
return f'{hours}:{minutes:02}:{seconds:02}'
else:
seconds = total_seconds % 60
minutes = total_seconds // 60
return f'{minutes:02}:{seconds:02}'

def translate_iter(iter):
if iter > 1000000:
return f'{iter/1000000:.0f}Mit/s'
if iter > 1000:
return f'{iter/1000:.0f}Kit/s'
return f'{iter:.0f}it/s'

class screenplay():
def __init__(self, country='default'):
Expand Down Expand Up @@ -101,7 +120,7 @@ def __len__(self):
return len(self.bar)

class lgbt():
def __init__(self, iterable=None, desc=" ", miniters=2500, hero='rainbow', total=None, mode='default'):
def __init__(self, iterable=None, desc=" ", miniters=2500, minintervals=0.1, hero='rainbow', total=None, mode='default'):
self.iterable = iterable
self.total = total
if inspect.isgenerator(self.iterable):
Expand All @@ -111,6 +130,7 @@ def __init__(self, iterable=None, desc=" ", miniters=2500, hero='rainbow', total
if self.total == None:
self.total = len(self.iterable)
self.miniters = miniters
self.minintervals = minintervals
self.hero = hero
self.desc = desc
self.screenplay = screenplay(country=mode)
Expand Down Expand Up @@ -145,14 +165,14 @@ def _fill_bars(self):
self.bars.append(simb)

def _draw(self):
end = time.perf_counter() - self.start
speed = self.iterations / end
total_time = time.perf_counter() - self.start
speed = self.iterations / total_time
remaining = (self.total - self.iterations) / speed
filled = round(self.iterations / self.total * (self.bar_width-1))
percent = (self.iterations / self.total) * 100

sys.stdout.write(
f"\r{self.desc}{percent:03.0f}% {self.bars[filled]} {self.iterations}/{self.total} [{end:03.2f}s{self.anim[filled%4]}{remaining:03.2f}s, {speed:.2f}it/s]{CLEAN}")
f"\r{self.desc}{percent:03.0f}% {self.bars[filled]} {self.iterations}/{self.total} [{translate_time(total_time)}{self.anim[int(total_time)%4]}{translate_time(remaining)}, {translate_iter(speed)}]{CLEAN}")
sys.stdout.flush()

def _desc_prep(self):
Expand Down Expand Up @@ -184,11 +204,14 @@ def __iter__(self):
#colection = iterable if not inspect.isgenerator(iterable) else range(total)

self.start = time.perf_counter()
last_update = self.start

for self.iterations, data in enumerate(self.iterable, 1):
yield data
interval = time.perf_counter() - last_update

if self.iterations % self.miniters == 0:
if self.iterations % self.miniters == 0 or interval >= self.minintervals:
self._draw()

sys.stdout.write("\n")
last_update = time.perf_counter()
sys.stdout.write("\n")

49 changes: 49 additions & 0 deletions lgbt/tests/test_arguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest
import lgbt

def my_gen(max):
current = 0
while current < max:
yield current
current += 1

def test_init_gen_without_total():
try:
temp = lgbt.lgbt(my_gen(100))
except (ValueError):
assert True
else:
assert False

def test_init_gen():
temp = lgbt.lgbt(my_gen(100), total=100)
assert temp.total == 100

def test_init_list():
temp = lgbt.lgbt([0,1,2,3,4,5,6,7,8,9])
assert temp.total == 10

def test_gen_run():
temp = lgbt.lgbt(my_gen(100), total=100)
print("Running with generator:")
for i in temp:
pass

def test_list_run():
temp = lgbt.lgbt([0,1,2,3,4,5,6,7,8,9])
print("Running with list:")
for i in temp:
pass

def test_gen_run_miniters():
temp = lgbt.lgbt([0,1,2,3,4,5,6,7,8,9], miniters=100)
print("Running with miniters:")
for i in temp:
pass

def test_gen_run_hero():
temp = lgbt.lgbt([0,1,2,3,4,5,6,7,8,9], hero='tralalero')
print("Running with miniters:")
for i in temp:
pass

6 changes: 6 additions & 0 deletions lgbt/tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest
import lgbt

def test_init_class():
temp = lgbt.lgbt(total=1)
assert temp.__class__.__name__ == "lgbt"
21 changes: 17 additions & 4 deletions lgbt/tests/test_update.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import lgbt.lgbt
import pytest
import lgbt

def test_addition():
assert 1 + 1 == 2
def test_without_total():
try:
temp = lgbt.lgbt()
except TypeError:
assert True
else:
assert False


def test_subtraction():
assert 5 - 3 == 2
def test_with_total():
try:
temp = lgbt.lgbt(total=100)
except TypeError:
assert False
else:
assert True