Various helper modules

Plugin loading

Plugin loader module

Plugin loader can load Python modules as well as snippets stored in arbitrary files. The latter enables storing configuration as a Python code.

Note that typically it’s a better idea to store configuration in INI, YAML or JSON files than in Python code, because it’s easier to generate and process in other tools and even languages.

class seismometer.plugin.PluginLoader

Plugin loader. It can load module from sys.path or a code snippet from outside.

close()

Clean up temporary directory. This function is also called on object destruction, so there’s no need (but no harm, either) to call it separately.

load(name, file=None)
Parameters:
  • name – name of the module to load
  • file – module’s file name
Returns:

imported module’s handle

Load specified module and return its handle. Module can be loaded from outside of sys.path (e.g. from /etc) by providing its file name. (In such case, no *.pyc is stored along the original file.)

NOTE: Specifying a name under non-existent hierarchy may cause a warning to be issued. Better stick to a name that exists except for the last component, e.g. seismometer.dumbprobe.__config__.

Rate limiter for logging and other operations

RateLimit is useful for limiting number of consequent log entries, connection retries and so on. It can also track custom information, like number of messages dropped so far.

Small example of use:

limit = RateLimit(dropped_messages = 0)
# ...
if is_dropped():
    limit.dropped_messages += 1
if limit.dropped_messages > 0 and limit.should_fire():
    log.info("dropped: %d", limit.dropped_messages)
    limit.dropped_messages = 0
    limit.fired()
class seismometer.rate_limit.RateLimit(interval=30, **kwargs)

Rate limiter with some metadata storage.

Limiter object can have custom values set, so it can track some additional data than merely when the operation was fired recently.

Parameters:
  • interval – minimum interval between operations
  • kwargs – default values for additional fields
fired()

Mark the newest execution of the rate-limited operation.

reset()

Reset the rate-limiting timer. Next should_fire() call will return True.

should_fire()
Returns:Boolean

Check if the rate-limited operation should be fired now.

Input descriptors polling

class seismometer.poll.Poll

Poll filehandles for input.

This is convenience wrapper around select module to work with filehandles instead of file descriptors.

add(handle)
Parameters:handle – file handle (e.g. file object, but anything with fileno() method)

Add a handle to poll list. If handle.fileno() returns None, the handle is not added. The same stands for objects that already were added (check is based on file descriptor).

count()

Count the descriptors added to the poll.

empty()

Check if the poll is empty (no descriptors).

poll(timeout=100)
Parameters:timeout – timeout in milliseconds for poll operation
Returns:list of file handles added with add() method

Check whether any data arrives on descriptors. File handles (handles, not descriptors) that are ready for reading are returned as a list.

Method works around calls interrupted by signals (terminates early instead of throwing an exception).

remove(handle)
Parameters:handle – file handle, the same as for add()

Remove file handle from poll list. Handle must still be the same object as passed to add() method, but may be closed.

Priority queue with random access updates

class seismometer.prio_queue.PrioQueue(make_key=None)

Priority queue that supports updating priority of arbitrary elements and removing arbitrary elements.

Entry with lowest priority value is returned first.

Mutation operations (set(), pop(), remove(), and update()) have complexity of O(log(n)). Read operations (length() and peek()) have complexity of O(1).

Parameters:make_key – element-to-hashable converter function

If make_key is left unspecified, an identity function is used (which means that the queue can only hold hashable objects).

__contains__(entry)
Parameters:entry – entry to check
Returns:True if entry is in queue, False otherwise

Check whether the queue contains an entry.

__delitem__(entry)
Parameters:entry – entry to remove

Remove an entry from the queue.

__getitem__(entry)
Parameters:entry – entry to get priority of
Returns:priority
Throws :KeyError if entry is not in the queue

Get priority of an entry.

__init__(make_key=None)
Parameters:make_key – element-to-hashable converter function

If make_key is left unspecified, an identity function is used (which means that the queue can only hold hashable objects).

__iter__()
Returns:iterator

Iterate over the entries in the queue.

Order of the entries is unspecified.

__len__()
Returns:queue length

Return length of the queue.

__setitem__(entry, priority)
Parameters:
  • entry – entry to add/update
  • priority – entry’s priority

Set priority for an entry, either by adding a new or updating an existing one.

entries()
Returns:list of entries

Retrieve list of entries stored in the queue.

Order of the entries is unspecified.

iterentries()
Returns:iterator

Iterate over the entries in the queue.

Order of the entries is unspecified.

length()
Returns:queue length

Return length of the queue.

peek()
Returns:tuple (priority, entry)
Throws :IndexError when the queue is empty

Return the entry with lowest priority value. The entry is not removed from the queue.

pop()
Returns:tuple (priority, entry)
Throws :IndexError when the queue is empty

Return the entry with lowest priority value. The entry is immediately removed from the queue.

remove(entry)
Returns:priority of entry or None when entry was not found

Remove an arbitrary entry from the queue.

set(entry, priority)
Parameters:
  • entry – entry to add/update
  • priority – entry’s priority

Set priority for an entry, either by adding a new or updating an existing one.

update(entry, priority)
Parameters:
  • entry – entry to update
  • priority – entry’s new priority
Returns:

old priority of the entry

Throws :

KeyError if entry is not in the queue

Update priority of an arbitrary entry.