|
From: Guy K. K. <g....@ma...> - 2010-09-15 21:56:01
|
Thanks a lot. This code looks *much* more Pythonic already.
On Thu, 16 Sep 2010 06:59:14 Bruce Sherwood wrote:
> Here's another successful test. I replaced the import statements at
> the start of the example program gas.py with the following import
> statements, and deleted the old Numeric code in a try structure:
>
> from visual.cvisual import (vector, mag, mag2, norm, dot, cross, rotate,
> comp, proj, diff_angle, rate, waitclose)
> from visual.primitives import (arrow, cylinder, cone, sphere, box, ring,
> label, frame, pyramid, ellipsoid, curve,
> faces, convex, helix,
> points, text, distant_light, local_light)
Another possibility would be to just say
from visual import primitives
And then in the code you could create e. g. a cone using primitives.cone.
Much cleaner than trying to pull in *all* the individual objects into the
local name space.
> from visual.ui import display
> import visual.crayola as color
> import visual.materials as materials
Do one like this here:
from visual import materials
> import visual.site_settings
> import atexit as _atexit
> _atexit.register(waitclose)
> from math import pi, sin, cos, exp
> from numpy import (sqrt, array, newaxis, add, less_equal, identity,
> sort, nonzero, greater_equal, arange)
> from visual.graph import (gdisplay, gcurve, ghistogram)
> from random import random
>
> This could have been more selective, since gas.py uses only a few of
> the vector and primitive features. Also, gas.py already has a scene =
> display(....) statement.
>
> A subtle point that one should be aware of: In visual's __init__.py
> there is some complex machinery to deal with the fact that when, for
> example, you're taking the square root of a scalar you want to use the
> math module's sqrt, because it is much faster than using numpy's sqrt.
> On the other hand, you need to use numpy's sqrt if you're taking the
> square roots of all the elements of a numpy array. For functions such
> as sqrt you would want to do something like this:
>
> from math import sqrt as msqrt
> from numpy import sqrt as nsqrt
Then why "rename" these things? Just import math and numpy directly and use
numpy.sqrt() or math.sqrt()
This is *much* more clear ("explicit is better than implicit"), as otherwise
users might be digging through the Python/NumPy docs to look for the
description of msqrt or nsqrt.
Guy
--
Guy K. Kloss
Institute of Information and Mathematical Sciences
Te Kura Pūtaiao o Mōhiohio me Pāngarau
Massey University, Albany (North Shore City, Auckland)
473 State Highway 17, Gate 1, Mailroom, Quad B Building
voice: +64 9 414-0800 ext. 9266 fax: +64 9 441-8181
G....@ma... http://www.massey.ac.nz/~gkloss
|