Skip to content

Commit

Permalink
docstrings and pylint cleanuo
Browse files Browse the repository at this point in the history
  • Loading branch information
semiversus committed Sep 13, 2018
1 parent 75d0ab8 commit 4757c92
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
6 changes: 6 additions & 0 deletions broqer/hub/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def __init__(self, hub: 'Hub', # pylint: disable=unused-argument
Publisher.__init__(self)
self._subject = None # type: Publisher
self._path = path
self._hub = hub
self.assignment_future = None
self._pre_assign_emit = None # type: list

Expand Down Expand Up @@ -203,6 +204,11 @@ def path(self) -> str:
""" topic path used as key in the hub """
return self._path

@property
def hub(self) -> 'Hub':
""" reference to hub """
return self._hub


class MetaTopic(Topic):
""" MetaTopic is adding a meta dictionary to each topic """
Expand Down
6 changes: 3 additions & 3 deletions broqer/op/debounce.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ class Debounce(Operator):
:param error_callback: error callback to be registered
:param loop: asyncio loop to be used
"""
def __init__(self, publisher: Publisher, duetime: float,
def __init__(self, # pylint: disable=too-many-arguments
publisher: Publisher, duetime: float,
retrigger_value: Any = NONE,
error_callback=default_error_handler,
loop=None) -> None:
error_callback=default_error_handler, loop=None) -> None:
assert duetime >= 0, 'duetime has to be positive'

Operator.__init__(self, publisher)
Expand Down
37 changes: 12 additions & 25 deletions broqer/op/subscribers/trace.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
from functools import partial
from typing import Any, Callable, Optional
""" Implements Trace subscriber """
from broqer import Publisher
from broqer.op import Sink

from broqer import Publisher, Subscriber


class Trace(Subscriber):
def __init__(self, # pylint: disable=keyword-arg-before-vararg
callback: Optional[Callable[..., None]] = None,
*args, unpack=False, **kwargs) -> None:
if callback is None:
self._callback = None # type: Callable
elif args or kwargs:
self._callback = \
partial(callback, *args, **kwargs) # type: Callable
else:
self._callback = callback # type: Callable

self._unpack = unpack

def emit(self, value: Any, who: Publisher):
if self._callback:
if self._unpack:
self._callback(*value)
else:
self._callback(value)

class Trace(Sink):
""" Trace is a subscriber used for debugging purpose. On subscription
it will use the prepend flag to be the first callback called when the
publisher of interest is emitting.
:param callback: optional function to call
:param /*args: arguments used additionally when calling callback
:param /**kwargs: keyword arguments used when calling callback
:param unpack: value from emits will be unpacked as (*value)
"""
def __call__(self, publisher: Publisher):
return publisher.subscribe(self, prepend=True)
1 change: 1 addition & 0 deletions broqer/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
""" Module with utilities for broqer """
from .datatype_check import DTRegistry

__all__ = ['DTRegistry']
26 changes: 17 additions & 9 deletions broqer/utils/datatype_check.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
""" Implements DTRegistry and common DTs """
import asyncio
from typing import Any

Expand All @@ -6,6 +7,7 @@


def resolve_meta_key(hub, key, meta):
""" resolve a value when it's a string and starts with '>' """
if key not in meta:
return None
value = meta[key]
Expand All @@ -18,10 +20,13 @@ def resolve_meta_key(hub, key, meta):


class DT:
""" base class for a datatype """
def cast(self, topic, value): # pylint: disable=no-self-use,W0613
""" casting a string to the appropriate datatype """
return value

def check(self, topic, value): # pylint: disable=no-self-use,W0613
""" checking the value if it fits into the given specification """
pass


Expand All @@ -44,6 +49,7 @@ def check(self, topic, value):


class IntegerDT(NumberDT):
""" Datatype to represant integers """
def cast(self, topic, value):
return int(value)

Expand All @@ -54,6 +60,7 @@ def check(self, topic, value):


class FloatDT(NumberDT):
""" Datatype for floating point numbers """
def cast(self, topic, value):
return float(value)

Expand All @@ -64,24 +71,25 @@ def check(self, topic, value):


class DTTopic(MetaTopic):
""" Topic with additional datatype check functionality """
def __init__(self, hub: Hub, path: str) -> None:
MetaTopic.__init__(self, hub, path)
self._hub = hub

def cast(self, value):
'''Will cast value to the given datatype. It will not check the
"""Will cast value to the given datatype. It will not check the
value.
'''
"""
return self._hub.topic_factory.cast(self, value)

def check(self, value):
'''Check the value against the datatype and limits defined in meta
"""Check the value against the datatype and limits defined in meta
dictionary. The value has to be in the appropriate datatype (may use
cast before)
'''
"""
self._hub.topic_factory.check(self, value)

def checked_emit(self, value: Any) -> asyncio.Future:
""" casting and checking in one call """

assert isinstance(self._subject, Subscriber), \
'Topic has to be a subscriber'
Expand All @@ -90,12 +98,9 @@ def checked_emit(self, value: Any) -> asyncio.Future:
self.check(value)
return self._subject.emit(value, who=self)

@property
def hub(self):
return self._hub


class DTRegistry:
""" Registry used as topic factory for hub """
def __init__(self):
self._datatypes = {
'none': DT(),
Expand All @@ -105,12 +110,14 @@ def __init__(self):
}

def add_datatype(self, name: str, datatype: DT):
""" register the datatype with it's name """
self._datatypes[name] = datatype

def __call__(self, hub: Hub, path: str) -> DTTopic:
return DTTopic(hub, path)

def cast(self, topic, value):
""" cast a string to the value based on the datatype """
datatype_key = topic.meta.get('datatype', 'none')
result = self._datatypes[datatype_key].cast(topic, value)
validate_dt = topic.meta.get('validate', None)
Expand All @@ -119,6 +126,7 @@ def cast(self, topic, value):
return result

def check(self, topic, value):
""" checking the value if it fits into the given specification """
datatype_key = topic.meta.get('datatype', 'none')
self._datatypes[datatype_key].check(topic, value)
validate_dt = topic.meta.get('validate', None)
Expand Down

0 comments on commit 4757c92

Please sign in to comment.