type-checking support in Python?

Arnaud Delobelle arnodel at googlemail.com
Sat Oct 11 06:42:24 EDT 2008


On Oct 7, 9:52 pm, Erik Max Francis <m... at alcyone.com> wrote:

> I have a unit system module (unity) in progress as well (it's working
> but it's as yet unreleased), but which takes a different approach which
> I think has more promise.  Instead of having multiple united types
> derived from different fundamental types, it has its own type Quantity
> which takes two arguments: the amount and the units (which are further
> data structures).  That way, the amount and the units are kept
> separately and the unit system doesn't know anything specific about the
> amount and only deals with the unit system.  Operations between
> quantities enforce the type system (either combining the units for
> operations like multiplication, or requiring that they be compatible for
> operations like addition or comparison), and simply perform the
> appropriate operations on the amount without enforcing any type.  This
> allows you to have united operations on integers, floating point
> numbers, decimals, vectors, matrices, quaternions -- whatever values you
> may want to support units.
>
> Mine is more of a dimensional analysis tool (as well as a general unit
> converter) since it allows you to define a basis of units, then define
> units within that basis and manipulate them -- thus it remembers the
> definitions of all the units it has encountered.  You can define whole
> new unit systems (e.g., c.g.s. vs. SI) and have them all relate to each
> other, or even define brand new unit systems (e.g., man-day-widgets for
> questions like, "If it takes one man three days to make two widgets, how
> many widgets can five men make in two weeks?").
>
> --
> Erik Max Francis && m... at alcyone.com &&http://www.alcyone.com/max/
>   San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
>    Those who forget the past are condemned to repeat it.
>     -- George Santayana

It's funny how many people have had the need for this!  I have written
a module that seems to be very similar.  It contains:

* A BasicDimension class whose objects are basis vectors in the
dimensions vector space.
* A Dimension class which is simply a vector in the dimension vector
space, which optionally can be named.

E.g.

distance = new_basic_dim('distance', 'metre')
mass = new_basic_dim('mass', 'gram')
time = new_basic_dim('time', 'second')

speed = new_dim('speed', distance/time)
accel = new_dim('accel', speed/time)

area = new_dim('area', distance**2)
volume = new_dim('volume', distance**3)

density = new_dim('density', mass/volume)

force = new_dim('force', mass*accel)

Then:

>>> volume/area
>>> distance

>>> speed*time
>>> distance

* A Units class whose objects can registered with a dimension

* DObject class whose objects are just objects with a dimension.  You
can do arithmetic with DObjects provided that you can do arithmetic
with their underlying objects.

>>> s = Units(time, 1)
>>> ms = Units(time, 0.001)
>>> s.symbol = 's'
>>> print DObject(2, s) + DObject(19, ms)
2.019 s
>>> m = Units(distance, 1)
>>> print DObject(3.0, m) / DObject(2.0, s)
1.5 [units distance*time**-1 1]
>>> Units(distance/time, 1).symbol = 'm/s'
>>> print DObject(3.0, m) / DObject(2.0, s)
1.5 m/s
>>> print DObject(23.0, s).convert_to(ms)
23000 ms

* You can also have separate 'Dimensions systems', e.g. you can have
speed, force, time as basic dimensions instead of distance, mass, time
(IOW you have a different basis for the dimensions vector space).
Then you can convert DObjects between systems.  Although this feature
is not working to my satisfaction.

I have designed this as part of a program to mark automatically online
maths/science worksheets, so speed is not really a consideration in my
implementation.

I have always thought of this as just a tool for my program, but I
suppose it can be seen as a sort of dynamic type checking system!

--
Arnaud




More information about the Python-list mailing list