-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhancements, efficiencies and examples (#7)
* feat: add/update examples * feat: make use of data attribute in overriden methods * fix: make use of private methods * fix: check for isatty prior to iteration * fix: index fill and return logic * feat: add more examples * doc: add module attribute and method documentation * doc: update example sections * fix: unit test mocks * docs: update images --------- Signed-off-by: Emilio Reyes <soda480@gmail.com>
- Loading branch information
Showing
20 changed files
with
316 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import time | ||
import random | ||
from faker import Faker | ||
from list2term import Lines | ||
from mock import patch | ||
|
||
def main(): | ||
print('Generating random sentences...') | ||
docgen = Faker() | ||
with Lines(size=15, show_x_axis=True, max_chars=100) as lines: | ||
for _ in range(200): | ||
index = random.randint(0, len(lines) - 1) | ||
lines[index] = docgen.sentence() | ||
time.sleep(.02) | ||
|
||
if __name__ == '__main__': | ||
with patch('sys.stderr.isatty', return_value=False): | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import asyncio | ||
import random | ||
from faker import Faker | ||
from list2term import Lines | ||
from mock import patch | ||
|
||
async def do_work(worker, lines): | ||
total = random.randint(10, 65) | ||
for _ in range(total): | ||
# mimic an IO-bound process | ||
await asyncio.sleep(random.choice([.05, .1, .15])) | ||
lines[worker] = f'processed {Faker().name()}' | ||
return total | ||
|
||
async def run(workers): | ||
with Lines(size=workers) as lines: | ||
return await asyncio.gather(*(do_work(worker, lines) for worker in range(workers))) | ||
|
||
def main(): | ||
workers = 12 | ||
print(f'Total of {workers} workers working concurrently') | ||
results = asyncio.run(run(workers)) | ||
print(f'The {workers} workers processed a total of {sum(results)} items') | ||
|
||
if __name__ == '__main__': | ||
with patch('sys.stderr.isatty', return_value=False): | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,25 @@ | ||
import asyncio | ||
import random | ||
import uuid | ||
from faker import Faker | ||
from list2term import Lines | ||
|
||
async def do_work(worker, logger=None): | ||
logger.write(f'{worker}->worker is {worker}') | ||
async def do_work(worker, lines): | ||
total = random.randint(10, 65) | ||
logger.write(f'{worker}->{worker}processing total of {total} items') | ||
for _ in range(total): | ||
# mimic an IO-bound process | ||
await asyncio.sleep(random.choice([.05, .1, .15])) | ||
logger.write(f'{worker}->processed {Faker().name()}') | ||
await asyncio.sleep(random.choice([.05, .1, .025])) | ||
lines[worker] = f'processed {Faker().name()}' | ||
return total | ||
|
||
async def run(workers): | ||
with Lines(lookup=workers, use_color=True) as logger: | ||
doers = (do_work(worker, logger=logger) for worker in workers) | ||
return await asyncio.gather(*doers) | ||
with Lines(size=workers) as lines: | ||
return await asyncio.gather(*(do_work(worker, lines) for worker in range(workers))) | ||
|
||
def main(): | ||
workers = [Faker().user_name() for _ in range(12)] | ||
print(f'Total of {len(workers)} workers working concurrently') | ||
workers = 15 | ||
print(f'Total of {workers} workers working concurrently') | ||
results = asyncio.run(run(workers)) | ||
print(f'The {len(workers)} workers processed a total of {sum(results)} items') | ||
print(f'The {workers} workers processed a total of {sum(results)} items') | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import time | ||
from list2term import Lines | ||
from list2term.multiprocessing import pool_map | ||
from list2term.multiprocessing import CONCURRENCY | ||
|
||
def is_prime(num): | ||
if num == 1: | ||
return False | ||
for i in range(2, num): | ||
if (num % i) == 0: | ||
return False | ||
else: | ||
return True | ||
|
||
def count_primes(start, stop, logger): | ||
worker_id = f'{start}:{stop}' | ||
primes = 0 | ||
for number in range(start, stop): | ||
if is_prime(number): | ||
primes += 1 | ||
logger.write(f'{worker_id}->{worker_id} {number} is prime') | ||
logger.write(f'{worker_id}->{worker_id} processing complete') | ||
return primes | ||
|
||
def main(number): | ||
step = int(number / CONCURRENCY) | ||
print(f"Distributing {int(number / step)} ranges across {CONCURRENCY} workers running concurrently") | ||
iterable = [(index, index + step) for index in range(0, number, step)] | ||
# do not use Lines context and do not print messages from processes to stderr | ||
results = pool_map(count_primes, iterable, print_status=False) | ||
return sum(results.get()) | ||
|
||
if __name__ == '__main__': | ||
start = time.perf_counter() | ||
number = 100_000 | ||
result = main(number) | ||
stop = time.perf_counter() | ||
print(f"Finished in {round(stop - start, 2)} seconds\nTotal number of primes between 0-{number}: {result}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import time | ||
from list2term import Lines | ||
from list2term.multiprocessing import pool_map | ||
from list2term.multiprocessing import CONCURRENCY | ||
|
||
def is_prime(num): | ||
if num == 1: | ||
return False | ||
for i in range(2, num): | ||
if (num % i) == 0: | ||
return False | ||
else: | ||
return True | ||
|
||
def count_primes(start, stop, logger): | ||
worker_id = f'{start}:{stop}' | ||
primes = 0 | ||
for number in range(start, stop): | ||
if is_prime(number): | ||
primes += 1 | ||
logger.write(f'{worker_id}->{worker_id} {number} is prime') | ||
logger.write(f'{worker_id}->{worker_id} processing complete') | ||
return primes | ||
|
||
def main(number): | ||
step = int(number / CONCURRENCY) | ||
print(f"Distributing {int(number / step)} ranges across {CONCURRENCY} workers running concurrently") | ||
iterable = [(index, index + step) for index in range(0, number, step)] | ||
# do not use Lines context but print messages from processes to stderr | ||
results = pool_map(count_primes, iterable) | ||
return sum(results.get()) | ||
|
||
if __name__ == '__main__': | ||
start = time.perf_counter() | ||
number = 100_000 | ||
result = main(number) | ||
stop = time.perf_counter() | ||
print(f"Finished in {round(stop - start, 2)} seconds\nTotal number of primes between 0-{number}: {result}") |
Oops, something went wrong.