This Page

mmf.async.utils

IFormatter()
ProtocolError Malformed message.
MySet(*v, **kw) A set-like object that allows for unhashable entries.
MyCondition([lock, verbose])
IntStringFormatter Formatter for messages of the format “%i.%s”(len(msg),msg)
MsgQueue([maxsize]) Implements a threadsafe message queue that allows you to add data and then yields parsed and complete messages.
hasargs(decorator) Meta-decorator to allow a decorator to accept kwargs or no arg (but use the default values).

Inheritance diagram for mmf.async.utils:

Inheritance diagram of mmf.async.utils

Some useful non-twisted dependent utilities.

interface mmf.async.utils.IFormatter[source]

Bases: zope.interface.Interface

split(data)[source]

Return (msg, tail) where msg is the first complete message encoded in data, and tail is the remaining data. If there is no message the msg == None (which is different from an empty message).

raises ProtocolError if the message is malformed.

format(msg)[source]

Return the formatted message msg.

class mmf.async.utils.ProtocolError[source]

Bases: exceptions.Exception

Malformed message.

__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

class mmf.async.utils.MySet(*v, **kw)[source]

Bases: list

A set-like object that allows for unhashable entries.

Methods

add(item)
append L.append(object) – append object to end
count L.count(value) -> integer – return number of occurrences of value
extend L.extend(iterable) – extend list by appending elements from the iterable
index L.index(value, [start, [stop]]) -> integer – return first index of value.
insert L.insert(index, object) – insert object before index
pop L.pop([index]) -> item – remove and return item at index (default last).
remove(item[, throw])
reverse L.reverse() – reverse IN PLACE
sort L.sort(cmp=None, key=None, reverse=False) – stable sort IN PLACE;
update(iterable)
__init__(*v, **kw)[source]
add(item)[source]
remove(item, throw=True)[source]
update(iterable)[source]
class mmf.async.utils.MyCondition(lock=None, verbose=None)[source]

Bases: threading._Condition

Methods

notify([n])
notifyAll()
notify_all()
wait([timeout]) Like regular wait, but raises and exception upon timeout.
__init__(lock=None, verbose=None)
wait(timeout=None)[source]

Like regular wait, but raises and exception upon timeout.

class mmf.async.utils.IntStringFormatter[source]

Bases: object

Formatter for messages of the format “%i.%s”(len(msg),msg)

>>> F = IntStringFormatter
>>> msg = F.format("Hello")
>>> msg
'5.Hello'
>>> F.split(msg)
('Hello', '')
>>> F.split("2.Hi5.There")
('Hi', '5.There')

Invalid messages will raise a ProtocolError >>> F.split(“1j.Hello”) Traceback (most recent call last):

...

ProtocolError: Received non-digit ‘j’ in message prefix ‘1j’

__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

static format(msg)[source]

Return the formatted message.

static split(data)[source]

Return (msg, tail) where msg is the first complete message encoded in data, and tail is the remaining data. If there is no message the msg == None (which is different from an empty message).

raises ProtocolError if the message is malformed.

class mmf.async.utils.MsgQueue(maxsize=0)[source]

Bases: Queue.Queue

Implements a threadsafe message queue that allows you to add data and then yields parsed and complete messages.

Methods

Formatter
empty() Return True if the queue is empty, False otherwise (not reliable!).
full() Return True if the queue is full, False otherwise (not reliable!).
get([block, timeout]) Remove and return an item from the queue.
get_nowait() Remove and return an item from the queue without blocking.
join() Blocks until all items in the Queue have been gotten and processed.
put(item[, block, timeout]) Put an item into the queue.
put_nowait(item) Put an item into the queue without blocking.
qsize() Return the approximate size of the queue (not reliable!).
task_done() Indicate that a formerly enqueued task is complete.
Formatter

alias of IntStringFormatter

mmf.async.utils.hasargs(decorator)[source]

Meta-decorator to allow a decorator to accept kwargs or no arg (but use the default values). Also applies wraps.

>>> @hasargs
... def decorator_with_args(f, m=2, b=1):
...     return lambda x: m*f(x) + b
>>> @decorator_with_args
... def f(x): return x**2
>>> f(2.0)
9.0
>>> @decorator_with_args(m=3, b=2)
... def f(x): return x**2
>>> f(2.0)
14.0