skip to navigation
skip to content

hiro 0.1.2

time manipulation utilities for python

Latest Version: 0.2


—Hiro Nakamura

Hiro context manager

Timeline context

The hiro.Timeline context manager hijacks a few commonly used time functions to allow time manipulation within its context. Specifically time.sleep, time.time, time.gmtime,, datetime.utcnow and behave according the configuration of the context.

The context provides the following manipulation options:

  • rewind: accepts seconds as an integer or a timedelta object.
  • forward: accepts seconds as an integer or a timedelta object.
  • freeze: accepts a floating point time since epoch or datetime or date object to freeze the time at.
  • unfreeze: resumes time from the point it was frozen at.
  • scale: accepts a floating point to accelerate/decelerate time by. > 1 = acceleration,  < 1 = deceleration
  • reset: resets all time alterations.
import hiro
from datetime import timedelta, datetime
import time
# OUT: '2013-12-01T06:55:41.706060'
with hiro.Timeline() as timeline:

    # forward by an hour
    # OUT: '2013-12-01T07:55:41.707383'

    # jump forward by 10 minutes
    # OUT: '2013-12-01T08:05:41.707425'

    # jump to yesterday and freeze
    timeline.freeze( - timedelta(hours=24))
    # OUT: '2013-11-30T09:15:41'

    timeline.scale(5) # scale time by 5x
    time.sleep(5) # this will effectively only sleep for 1 second

    # since time is frozen the sleep has no effect
    # OUT: '2013-11-30T09:15:41'

    # OUT: '2012-11-30T09:15:41'

To reduce the amount of statements inside the context, certain timeline setup tasks can be done via the constructor and/or by using the fluent interface.

import hiro
import time
from datetime import timedelta, datetime

start_point = datetime(2012,12,12,0,0,0)
my_timeline = hiro.Timeline(scale=5).forward(60*60).freeze()
with my_timeline as timeline:
    # OUT: '2012-12-12 01:00:00.000315'
    time.sleep(5) # effectively 1 second
    # no effect as time is frozen
    # OUT: '2012-12-12 01:00:00.000315'
    # back to starting point
    # OUT: '2012-12-12 01:00:00.000317'
    time.sleep(5) # effectively 1 second
    # takes effect (+5 seconds)
    # OUT: '2012-12-12 01:00:05.003100'

Timeline can additionally be used as a decorator

import hiro
import time, datetime

def sleeper():
    # OUT: '2013-11-30 14:27:43.409291'
    time.sleep(60*60) # effectively 72 ms
    # OUT: '2013-11-30 15:28:36.240675'

Hiro executors

In order to execute certain callables within a Timeline context, two shortcut functions are provided.

  • run_sync(factor=1, callable, *args, **kwargs)
  • run_async(factor=1, callable, *args, **kwargs)

Both functions return a ScaledRunner object which provides the following methods

  • get_execution_time: The actual execution time of the callable
  • get_response (will either return the actual return value of callable or raise the exception that was thrown)

run_async returns a derived class of ScaledRunner that additionally provides the following methods

  • is_running: True/False depending on whether the callable has completed execution
  • join: blocks until the callable completes execution


import hiro
import time

def _slow_function(n):
    if n > 10:
        raise RuntimeError()
    return n

runner = hiro.run_sync(10, _slow_function, 10)
# OUT: 10

# due to the scale factor 10 it only took 1s to execute
# OUT: 1.1052658557891846

runner = hiro.run_async(10, _slow_function, 11)
# OUT: True
# OUT: 1.1052658557891846
# OUT: Traceback (most recent call last):
# ....
# OUT:   File "<input>", line 4, in _slow_function
# OUT: RuntimeError
File Type Py Version Uploaded on Size
hiro-0.1.2-py2.7.egg (md5) Python Egg 2.7 2014-02-20 17KB
hiro-0.1.2.tar.gz (md5) Source 2014-02-20 14KB
  • Downloads (All Versions):
  • 2 downloads in the last day
  • 383 downloads in the last week
  • 2278 downloads in the last month