Closed hgrecco closed 10 years ago
I hope you will define the relation as wavelength = speed_of_light / frequency
instead... ;-)
It could be useful also to be able to parametrize the context. For example, the speed of light depends on the refractive index of the material. For such situation, I propose something like:
@context(n = 1) spectroscopy = sp
m = c / n / Hz
then we could use it like
q.to('Hz', ctx='spectroscopy', n=1.444)
and to
would be defined like
def to(self, other=None, ctx=None, **kwargs):
Off course, parameters would be optionals, and have default values.
It would also be useful to have a way to define a default context. For example:
ur = UnitRegistry()
ur.context('spectroscopy', n=1.444)
I cannot really define wavelength = speed_of_light / frequency
because pint
is about units and dimensions. It knows about meter
and second
, about [length]
, [time]
, [frequency]
but not about wavelength. You measure wavelengths in meters which and they have length dimension.
I understand that as a user this might not be the most straight forward way to declare. m = c * s
looks strange. Maybe [length] = c * [time]
. I am open to ideas.
I really like the idea of parametrization of the context. We just need find a (pythonic) way to indicate that n
is a parameter, no something to take from the registry.
I also like the idea of a default context. I am actually looking into the idea of setting several defaults including format and base units. Context should be included as well.
Another proposition for definition of context could be something like:
@context spectroscopy = sp:
@parameter n = []; default: 1 # unitless, with default value
speed_of_light = 299792458 / n * meter / second = c # redefinition of a constant inside the context
meter = c / hertz # wavelength <-> frequency
I think I like your original proposal of parametrization:
@context(n = 1) spectroscopy = sp
m = c / n / Hz
and:
q.to('Hz', ctx='spectroscopy', n=1.444)
Let's try this and see how it works.
Implemented Context Parsing 4aba2db47707cffd4c7f49cfe0d423f32f16de4c
`If you work in certain topics, you usually need to convert from certain units to others based some pre-established relationships.
For example, you are working with EM fields in vacuum and you need to transform wavelength to frequency. If you try this:
You need to use the relation
wavelength = speed_of_light * frequency
.The plan is to provide a context (abbreviated ctx) to facilitate this:
or alternative as a context manager:
Some context will be predefined in the definitions file. The proposed syntax is:
which then can be used as:
or with the abbreviated form:
You can define multiple relations in a single context (one per line in the definition file or in an tuple when you define it in situ)
Open points:
ctx
are:context
,using
[length] = c * [time]