Interesting Python Packages that Deserve Our Attention

Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0


Help researchers by exploring cool packages written in Python.


Learn how to write functions that handle units.


Roy Hyunjin Han


When applying mathematics to the real world, it is important to be mindful of the units for each quantity.


20170605-2000 - 20170605-2100: 1 hour estimated

20170605-2000 - 20170605-2130: 1.5 hours actual


  1. Write an expression that uses the pint package.
  2. Write a function that uses the pint package.
  3. Write a tool that uses the pint package.


20170605-2000 - 20170605-2030: 30 minutes

In [ ]:
import pip
pip.main(['install', 'pint'])
+ Make sure the `pint` package is installed

I would really like to use Jupyter with Vim key bindings next time.

Katy Huff mentioned the pint package in her keynote at PyCon 2017.

I took some time to work through the documentation. The package is written by Hernan E. Grecco, who is a professor and researcher at the University of Buenos Aires in Argentina.

20170605-2030 - 20170605-2100: 30 minutes

Let's look at why this package is cool.

In [ ]:
from pint import UnitRegistry
u = UnitRegistry()

It comes predefined with a lot of units.

In [ ]:
' '.join(dir(u))
In [ ]:
distance = 10 * u.kilometer
print(distance + 500 * u.meter)
print(distance * distance)
In [ ]:

Use ito for in-place conversions.

In [ ]:

Parse strings.

In [ ]:
Q = u.Quantity
In [ ]:
Q('1 km/s').to(u.mile / u.hour)

Format strings.

In [ ]:
x = Q('100 km/hr ** 2').to(u.mile / u.hour ** 2)
print('{:P}'.format(x))  # Pretty Print
print('{:L}'.format(x))  # LaTeX
print('{:H}'.format(x))  # HTML

Use arrays.

In [ ]:
[[2, 3], [4, 5]] * + [[6, 7], [8, 9]] *

Define functions.

In [ ]:
In [ ]:
@u.wraps(u.kilowatt_hour, u.hour)
def compute_kwh(time_in_hours):
    return time_in_hours * 10

compute_kwh(Q('180 minutes'))
In [ ]:
@u.wraps(u.meter / u.second, (u.meter, u.meter, u.second))
def compute_velocity(distance1, distance2, time_interval):
    return (distance2 - distance1) / time_interval

compute_velocity(Q('5 meters'), Q('1 mile'), Q('1 minute'))
+ List which aspects of the package we would like to feature
+ Write an expression
+ Write a function    

20170605-2100 - 20170605-2130: 30 minutes

The documentation notes that one cannot combine objects that are initialized using different UnitRegistry instances.

ValueError: Cannot operate with Quantity and Quantity of different registries.

Thus it is important to use a single UnitRegistry for an entire application.

Use set_application_registry when unpickling unit-aware objects to ensure that all objects are instantiated from the same UnitRegistry class.

In [ ]:
from pint import set_application_registry

I think we can design a simple CrossCompute tool that converts units. What is a unit conversion that I do frequently? Well for now, let's just convert temperatures.

+ Write a tool

We made a rough and fun tool. We should eventually let people deploy the tool directly.

What are some exercises that people could do to practice using this package?

In [ ]:
# Write an expression that adds three different units of time
In [ ]:
# Write a function that takes the number of years and returns the number of hours
In [ ]:
# Add a decorator using u.wraps that sets the following units:
# initial_velocity in meters per second
# final_velocity in meters per second
# time_interval in seconds
# acceleration in meters per second squared

def compute_acceleration(
        initial_velocity, final_velocity, time_interval):
    return (final_velocity - initial_velocity) / time_interval

x = compute_acceleration(
    Q('0.5 km/hr'), Q('5 mile/second'), Q('7 microseconds'))
# assert x.magnitude == 1149511587.3015873
+ Create some exercises so that people can practice