pymt package

Subpackages

Submodules

pymt.babel module

Utility functions for working with babel projects.

exception pymt.babel.BabelConfigError(prog)[source]

Bases: Exception

pymt.babel.contains_identifiers(template)[source]
pymt.babel.prepend_env_path(var, path, sep=':')[source]

Prepend a path to an environment variable.

pymt.babel.query_config_all(config='csdms-config')[source]
pymt.babel.query_config_var(var, config='csdms-config', interpolate=True)[source]

Get a configuration variable from a babel project.

pymt.babel.recursive_substitute(template, **kwds)[source]
pymt.babel.setup_babel_environ()[source]

Set up environment variables to load babelized components.

pymt.errors module

exception pymt.errors.BmiError(fname, status)[source]

Bases: pymt.errors.PymtError

exception pymt.errors.PymtError[source]

Bases: Exception

pymt.models module

pymt.timeline module

Execute events along a timeline.

Examples

>>> timeline = Timeline()
>>> timeline.add_recurring_event('event 1', 1.)
>>> timeline.add_recurring_event('event 2', .3)
>>> timeline.pop()
'event 2'
>>> timeline.pop()
'event 2'
>>> timeline.pop()
'event 2'
>>> timeline.pop()
'event 1'
>>> timeline.time
1.0
>>> for event in timeline.iter_until(2.0): print(event)
event 2
event 2
event 2
event 1
>>> timeline.pop()
'event 2'

When events occur at the same time, events are popped in as first-in, first-out.

>>> timeline = Timeline([('event 1', 1.), ('event 2', .5)])
>>> for event in timeline.iter_until(1.0): print(event)
event 2
event 1
event 2
>>> timeline = Timeline([('event 2', .5), ('event 1', 1.)])
>>> timeline.pop_until(1.05)
['event 2', 'event 1', 'event 2']
>>> timeline.time
1.05
>>> for event in timeline.iter_until(1.05): print(event)
>>> timeline.time
1.05
>>> for event in timeline.iter_until(1.06): print(event)
>>> timeline.time
1.06

The event key can be any object, even a tuple.

>>> timeline = Timeline([(('event', 2), .5), (('event', 0), 1.)])
>>> for event in timeline.iter_until(1.05): print(event)
('event', 2)
('event', 0)
('event', 2)

Events do not have to be recurring.

>>> timeline = Timeline([(('event', 2), .5), (('event', 0), 1.)])
>>> timeline.add_one_time_event('one-timer', .1)
>>> for event in timeline.iter_until(1.05): print(event)
one-timer
('event', 2)
('event', 0)
('event', 2)
class pymt.timeline.Timeline(events=None, start=0.0)[source]

Bases: object

Create a timeline of events.

Parameters:
  • events (dict-like) – Events as event-object/repeat interval pairs.
  • start (float, optional) – Start time for the timeline.

Examples

Create a timeline with two recurring events. Events can be any old object. In this case they are two strings.

>>> timeline = Timeline([('hello', 1.), ('world', 1.5)], start=3.)
>>> sorted(timeline.events) # The events are returned as a set.
['hello', 'world']

The first events will not occur at the starting time.

>>> timeline.time
3.0
>>> timeline.next_event
'hello'
>>> timeline.time_of_next_event
4.0

To advance the timeline forward to the next event, use the pop method.

>>> timeline.pop()
'hello'
>>> timeline.time
4.0
>>> timeline.pop()
'world'
>>> timeline.time
4.5

The timeline keeps track of objects, without making copies. The objects don’t even need to be hashable.

>>> hello = ['hello', 'world']
>>> timeline = Timeline([(hello, 1.)])
>>> event = timeline.pop()
>>> event is hello
True
>>> event.append('!')
>>> hello
['hello', 'world', '!']
add_one_time_event(event, time)[source]

Add an event that will only happen once.

Parameters:
  • event (event-like) – Event to add to the timeline.
  • time (float) – Time for the event to execute.

Examples

>>> timeline = Timeline()
>>> timeline.add_one_time_event('say hello', 1.)
>>> timeline.time_of_next_event
1.0
>>> timeline.pop()
'say hello'
>>> timeline.time_of_next_event  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
IndexError: empty timeline
add_recurring_event(event, interval)[source]

Add a recurring event to the timeline.

Adds a single recurring event to the timeline. event is the event object, and interval is recurrence interval.

Parameters:
  • event (event-like) – Event to add to the timeline.
  • interval (float) – Recurrence interval of the event.

Examples

>>> timeline = Timeline()
>>> timeline.add_recurring_event('say hello', 1.)
>>> timeline.events == {'say hello'}
True
add_recurring_events(events)[source]

Add a series of recurring events to the timeline.

Adds recurring events to the timeline. events is a list where each element is tuple that gives the event object followed by the event recurrence interval.

Parameters:events (dict-like) – Events object/interval pairs to add to the timeline.

Examples

>>> timeline = Timeline()
>>> len(timeline.events)
0
>>> timeline.add_recurring_events([('hello', 1.), ('world', 1.)])
>>> sorted(timeline.events)
['hello', 'world']

Events pop as first-in, first-out.

>>> timeline.pop()
'hello'
>>> timeline.pop()
'world'

The same event can be added multiple times.

>>> timeline = Timeline()
>>> timeline.add_recurring_events([('hello', 1.), ('world', 1.),
...     ('hello', 1.)])
>>> sorted(timeline.events)
['hello', 'world']
>>> timeline.pop_until(2.)
['hello', 'world', 'hello', 'hello', 'world', 'hello']
events

All of the event objects in the timeline.

Examples

>>> timeline = Timeline([('an event', 1.), ('another event', 1.)])
>>> events = timeline.events
>>> isinstance(events, set)
True
>>> sorted(events)
['an event', 'another event']
iter_until(stop)[source]

Iterate the timeline until a given time.

Iterate the timeline until stop, popping events along the way.

Parameters:stop (float) – Time to iterate until.
Returns:The next event object as the timeline advances to stop.
Return type:event
next_event

Next event object.

Return the next event object but don’t advance the timeline forward in time.

Examples

>>> timeline = Timeline([('an event', 1.)])
>>> timeline.next_event
'an event'
>>> timeline.time
0.0
pop()[source]

Pop the next event from the timeline.

Examples

>>> timeline = Timeline(dict(hello=1.))
>>> timeline.pop()
'hello'
pop_until(stop)[source]

Advance the timeline, popping events along the way.

Advance the timeline to stop, popping events along the way. Returns a list of the event objects that were popped to advance the timeline to stop.

Parameters:stop (float) – Time to iterate until.
Returns:The events popped to get to the stop time.
Return type:list

Examples

>>> timeline = Timeline([('a', 1.), ('b', 1.), ('c', 1.)])
>>> timeline.pop_until(2.)
['a', 'b', 'c', 'a', 'b', 'c']
time

Current time along the timeline.

Examples

>>> timeline = Timeline(start=0)
>>> timeline.time
0.0
>>> timeline = Timeline(start=2)
>>> timeline.time
2.0
time_of_next_event

Time when the next event will happen.

Examples

>>> timeline = Timeline([('an event', 1.)])
>>> timeline.time_of_next_event
1.0

pymt.units module

Module contents