Skip to content

Commit

Permalink
Merge pull request #28 from soda480/0.3.0
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
soda480 authored Apr 1, 2021
2 parents 32120e0 + 2ca9bdf commit 22a26d0
Show file tree
Hide file tree
Showing 16 changed files with 748 additions and 389 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The main features are:
* Display tables
* Display lists

Refer to documentation here: https://soda480.github.io/mpcurses/

### Installation ###
```bash
pip install mpcurses
Expand All @@ -40,13 +42,13 @@ from mpcurses import MPcurses
import namegenerator, time, logging
logger = logging.getLogger(__name__)

def run(*args):
def do_something(*args):
for _ in range(0, 600):
logger.debug(f'processing item "{namegenerator.gen()}"')
time.sleep(.01)

MPcurses(
function=run,
function=do_something,
screen_layout={
'display_item': {
'position': (1, 1),
Expand All @@ -62,21 +64,21 @@ MPcurses(
Executing the code above results in the following:
![example](https://raw.githubusercontent.com/soda480/mpcurses/master/docs/images/example.gif)

To scale execution of the function across multiple processes, we make a few simple updates:
To scale execution of the function across multiple processes; we set the `process_data` parameter to a list whose total number of elements represent the number of processes to execute, and each list element represent the data to be passed to each respective process:

```python
from mpcurses import MPcurses
import namegenerator, time, logging
logger = logging.getLogger(__name__)

def run(*args):
def do_something(*args):
group = args[0].get('group', 0)
for _ in range(0, 600):
logger.debug(f'processing item "[{group}]: {namegenerator.gen()}"')
time.sleep(.01)

MPcurses(
function=run,
function=do_something,
process_data=[{'group': 1}, {'group': 2}, {'group': 3}],
screen_layout={
'display_item': {
Expand Down
2 changes: 1 addition & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
authors = [Author('Emilio Reyes', 'emilio.reyes@intel.com')]
summary = 'Mpcurses is an abstraction of the Python curses and multiprocessing libraries providing function execution and runtime visualization capabilities'
url = 'https://github.com/soda480/mpcurses'
version = '0.2.5'
version = '0.3.0'
default_task = [
'clean',
'analyze',
Expand Down
47 changes: 47 additions & 0 deletions examples/example_counter1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# counter example

import random
import namegenerator
import logging
from time import sleep

from mpcurses import MPcurses
from screen_layouts.example_counter import get_screen_layout


logger = logging.getLogger(__name__)


def get_items():
items = []
for count in range(0, 1000):
items.append(namegenerator.gen())
return items


def do_work(*args):
items = get_items()
logger.debug(f'{len(items)} total items')
for item in items:
logger.debug(f'processing item "{item}"')
# simulate work being done
sleep(random.choice([0.015]))
if item.count('e') > 4:
# simulate warning
logger.debug(f'warning processing item "{item}"')
elif item.count('g') > 3:
# simulate error
logger.debug(f'error processing item "{item}"')
else:
# simulate success
logger.debug(f'item "{item}" was processed')
logger.debug('processing complete')


def main():
screen_layout = get_screen_layout()
MPcurses(function=do_work, screen_layout=screen_layout).execute()


if __name__ == '__main__':
main()
15 changes: 15 additions & 0 deletions examples/example_counter2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# counter example - with modulus
from example_counter1 import do_work
from mpcurses import MPcurses
from screen_layouts.example_counter import get_screen_layout


def main():
screen_layout = get_screen_layout()
screen_layout['_counter_']['modulus'] = 20
screen_layout['_counter_']['color'] = 7
MPcurses(function=do_work, screen_layout=screen_layout).execute()


if __name__ == '__main__':
main()
16 changes: 16 additions & 0 deletions examples/example_counter3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# counter example - with progress bar
from example_counter1 import do_work
from mpcurses import MPcurses
from screen_layouts.example_counter import get_screen_layout


def main():
screen_layout = get_screen_layout()
screen_layout['_counter_']['modulus'] = 20
screen_layout['_counter_']['color'] = 7
screen_layout['_counter_']['regex'] = r'^(?P<value>\d+) total items$'
MPcurses(function=do_work, screen_layout=screen_layout).execute()


if __name__ == '__main__':
main()
8 changes: 4 additions & 4 deletions examples/screen_layouts/example6_sl.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_screen_layout():
'text': '-------',
'text_color': 244,
'color': 244,
'width': 7,
'length': 7,
'right_justify': True,
'regex': r'^item .* head is at (?P<value>.*)$',
'table': True
Expand All @@ -53,23 +53,23 @@ def get_screen_layout():
'position': (1, 18),
'text': '',
'color': 244,
'width': 28,
'length': 28,
'regex': r"^'item' is '(?P<value>.*)'$",
'table': True
},
'item_processing': {
'position': (1, 18),
'text': '',
'color': 14,
'width': 28,
'length': 28,
'regex': r'^processing item (?P<value>.*)$',
'table': True
},
'item_processed': {
'position': (1, 18),
'text': '',
'color': 7,
'width': 28,
'length': 28,
'regex': r'^processed item (?P<value>.*)$',
'table': True
}
Expand Down
59 changes: 59 additions & 0 deletions examples/screen_layouts/example_counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

def get_screen_layout():
return {
'total': {
'position': (1, 9),
'text': 'Total: 0',
'text_color': 0,
'color': 15,
'regex': r'^(?P<value>\d+) total items$'
},
'successful': {
'position': (2, 4),
'text': 'Successful: -',
'text_color': 0,
'color': 2,
'keep_count': True,
'regex': r'^item ".*" was processed$'
},
'warnings': {
'position': (3, 6),
'text': 'Warnings: -',
'text_color': 0,
'color': 3, # 232,
'keep_count': True,
'regex': r'^warning processing item ".*"$'
},
'errors': {
'position': (4, 8),
'text': 'Errors: -',
'text_color': 0,
'color': 1, # 237,
'keep_count': True,
'regex': r'^error processing item ".*"$'
},
'processing': {
'position': (5, 4),
'text': 'Processing: -',
'text_color': 0,
'color': 14,
'clear': True,
'regex': r'^processing item "(?P<value>.*)"$'
},
'processing_done': {
'position': (5, 4),
'replace_text': ' ',
'clear': True,
'regex': r'^processing complete$'
},
'_counter_': {
'position': (6, 0),
'categories': [
'successful',
'warnings',
'errors'
],
'counter_text': '|',
'width': 100
}
}
1 change: 1 addition & 0 deletions src/main/python/mpcurses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@

from .handler import queue_handler
from .mpcurses import MPcurses
from .mpcontroller import MPcontroller
1 change: 1 addition & 0 deletions src/main/python/mpcurses/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def _queue_handler(*args, **kwargs):
result_queue.put({
offset: result
})
logger.debug(f'execution of {function.__name__} offset {offset} ended')
# log control message that method completed
logger.debug('DONE')
if message_queue:
Expand Down
Loading

0 comments on commit 22a26d0

Please sign in to comment.