-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to Python 3 only and include a setup.py script
- Loading branch information
Showing
17 changed files
with
130 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
*.swp | ||
*.swo | ||
.DS_Store | ||
build/ | ||
dist/ | ||
pyqn.egg-info/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# pyvalem | ||
A Python package for handling physical units and quantities. | ||
|
||
A very quick overview: | ||
|
||
In [1]: from pyqn.units import Units | ||
|
||
In [2]: u1 = Units('km') | ||
|
||
In [3]: u2 = Units('hr') | ||
|
||
In [4]: u3 = u1/u2 | ||
|
||
In [5]: print(u3) | ||
km.hr-1 | ||
|
||
In [6]: u4 = Units('m/s') | ||
|
||
In [7]: u3.conversion(u4) # OK: can convert from km/hr to m/s | ||
Out[7]: 0.2777777777777778 | ||
|
||
In [8]: u3.conversion(u2) # Oops: can't convert from km/hr to m! | ||
... | ||
UnitsError: Failure in units conversion: units km.hr-1[L.T-1] and hr[T] have | ||
different dimensions | ||
|
||
For more information and examples, see http://christianhill.co.uk/projects/pyqn |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
# -*- coding: utf-8 -*- | ||
# base_unit.py | ||
# A class representing a "base unit", identified as a single unit with no | ||
# prefix or exponent, such as 'g', 'hr', 'bar', 's'. | ||
# | ||
# Copyright (C) 2012-2015 Christian Hill | ||
# Copyright (C) 2012-2016 Christian Hill | ||
# Department of Physics and Astronomy, University College London | ||
# [email protected] | ||
# | ||
|
@@ -22,7 +21,7 @@ | |
# You should have received a copy of the GNU General Public License | ||
# along with PyQn. If not, see <http://www.gnu.org/licenses/> | ||
|
||
from dimensions import * | ||
from .dimensions import * | ||
|
||
class BaseUnit(object): | ||
""" | ||
|
@@ -83,7 +82,7 @@ def __str__(self): | |
BaseUnit('Pa', 'pascal', 'pressure', 1., '', 'Pa', d_pressure), | ||
BaseUnit('C', 'coulomb', 'charge', 1., '', 'C', d_charge), | ||
BaseUnit('V', 'volt', 'voltage', 1., '', 'V', d_voltage), | ||
BaseUnit(u'Ω', 'ohm', 'electric resistance', 1, '', r'\Omega', | ||
BaseUnit('Ω', 'ohm', 'electric resistance', 1, '', r'\Omega', | ||
d_voltage / d_current), | ||
BaseUnit('F', 'farad', 'capacitance', 1., '', 'F', d_charge / d_voltage), | ||
BaseUnit('Wb', 'weber', 'magnetic flux', 1, '', 'Wb', d_magnetic_flux), | ||
|
@@ -137,15 +136,15 @@ def __str__(self): | |
]), | ||
|
||
('Non-SI mass units', [ | ||
BaseUnit('u', 'atomic mass unit', 'mass', 1.660538921e-27, '', 'u', d_mass), | ||
BaseUnit('amu', 'atomic mass unit', 'mass', 1.660538921e-27, '', 'amu', d_mass), | ||
BaseUnit('u', 'atomic mass unit', 'mass', 1.660538921e-27, '', '', d_mass), | ||
BaseUnit('amu', 'atomic mass unit', 'mass', 1.660538921e-27, '', 'am', d_mass), | ||
BaseUnit('Da', 'dalton', 'mass', 1.660538921e-27, '', 'Da', d_mass), | ||
BaseUnit('m_e', 'electron mass', 'mass', 9.10938291e-31, '', 'm_e', d_mass), | ||
]), | ||
|
||
('Non-SI units of length, area and volume', [ | ||
# Non-SI length units | ||
BaseUnit(u'Å', 'angstrom', 'length', 1.e-10, '', '\AA', d_length), | ||
BaseUnit('Å', 'angstrom', 'length', 1.e-10, '', '\AA', d_length), | ||
BaseUnit('a0', 'bohr', 'length', 5.2917721092e-11, '', 'a_0', d_length), | ||
# Non-SI area units | ||
BaseUnit('b', 'barn', 'area', 1.e-28, '', 'b', d_area), | ||
|
@@ -200,7 +199,9 @@ def __str__(self): | |
|
||
('Miscellaneous units', [ | ||
BaseUnit('Td', 'townsend', 'reduced electric field', 1.e-21, '', 'Td', | ||
d_voltage * d_area) | ||
d_voltage * d_area), | ||
BaseUnit('Jy', 'jansky', 'spectral flux density', 1.e-26, '', 'Jy', | ||
d_energy / d_area) # W.m-2.s-1 | ||
]), | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
# dimensions.py | ||
# A class representing the dimensions of a physical quantity's units, in | ||
# terms of powers of length (L), mass (M), time (T), temperature (Theta), | ||
# amount of substance (Q), current (C) and luminous intensity (I). | ||
# | ||
# Copyright (C) 2012 Christian Hill | ||
# Copyright (C) 2012-2016 Christian Hill | ||
# Department of Physics and Astronomy, University College London | ||
# [email protected] | ||
# | ||
|
@@ -40,7 +39,7 @@ def __init__(self, dims=None, **kwargs): | |
if not kwargs: | ||
self.dims = dims | ||
else: | ||
print 'bad initialisation of Dimensions object' | ||
print('bad initialisation of Dimensions object') | ||
sys.exit(1) | ||
else: | ||
# initialize by keyword arguments | ||
|
@@ -53,7 +52,7 @@ def __mul__(self, other): | |
new_dims.append(dim + other.dims[i]) | ||
return Dimensions(tuple(new_dims)) | ||
|
||
def __div__(self, other): | ||
def __truediv__(self, other): | ||
new_dims = [] | ||
for i, dim in enumerate(self.dims): | ||
new_dims.append(dim - other.dims[i]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
# quantity.py | ||
# A class representing physical quantity, with name, units and uncertainty. | ||
# | ||
# Copyright (C) 2012 Christian Hill | ||
# Copyright (C) 2012-2016 Christian Hill | ||
# Department of Physics and Astronomy, University College London | ||
# [email protected] | ||
# | ||
|
@@ -23,8 +22,8 @@ | |
|
||
import re | ||
import math | ||
from symbol import Symbol | ||
from units import Units, UnitsError | ||
from .symbol import Symbol | ||
from .units import Units, UnitsError | ||
|
||
class QuantityError(Exception): | ||
""" | ||
|
@@ -231,7 +230,7 @@ def __mul__(self, other): | |
units = self.units * other.units | ||
return Quantity(value=value, units=units, sd=sd) | ||
|
||
def __div__(self, other): | ||
def __truediv__(self, other): | ||
""" | ||
Divide this quantity by a number or another quantity. Errors are | ||
propagated, but assumed to be uncorrelated. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
# si.py | ||
# A class representing the SI prefixes (SIPrefix) and a list of the SI | ||
# base units (si_unit_stems): length (L), mass (M), time (T), temperature | ||
# (Theta), amount of substance (Q), current (C) and luminous intensity (I). | ||
# | ||
# Copyright (C) 2012 Christian Hill | ||
# Copyright (C) 2012-2016 Christian Hill | ||
# Department of Physics and Astronomy, University College London | ||
# [email protected] | ||
# | ||
|
@@ -32,27 +31,27 @@ def __init__(self, prefix, name, power): | |
self.fac = 10**power | ||
|
||
# Here are the SI prefixes that we recognise. | ||
si_prefixes = { u'y': SIPrefix(u'y', 'yocto', -24), | ||
u'z': SIPrefix(u'z', 'zepto', -21), | ||
u'a': SIPrefix(u'a', 'atto', -18), | ||
u'f': SIPrefix(u'f', 'femto', -15), | ||
u'p': SIPrefix(u'p', 'pico', -12), | ||
u'n': SIPrefix(u'n', 'nano', -9), | ||
u'μ': SIPrefix(u'μ', 'micro', -6), | ||
u'm': SIPrefix(u'm', 'milli', -3), | ||
u'c': SIPrefix(u'c', 'centi', -2), | ||
u'd': SIPrefix(u'd', 'deci', -1), | ||
u'k': SIPrefix(u'k', 'kilo', 3), | ||
u'M': SIPrefix(u'M', 'mega', 6), | ||
u'G': SIPrefix(u'G', 'giga', 9), | ||
u'T': SIPrefix(u'T', 'tera', 12), | ||
u'P': SIPrefix(u'P', 'peta', 15), | ||
u'E': SIPrefix(u'E', 'exa', 18), | ||
u'Z': SIPrefix(u'Z', 'zetta', 21), | ||
u'Y': SIPrefix(u'Y', 'yotta', 24), | ||
si_prefixes = { 'y': SIPrefix('y', 'yocto', -24), | ||
'z': SIPrefix('z', 'zepto', -21), | ||
'a': SIPrefix('a', 'atto', -18), | ||
'f': SIPrefix('f', 'femto', -15), | ||
'p': SIPrefix('p', 'pico', -12), | ||
'n': SIPrefix('n', 'nano', -9), | ||
'μ': SIPrefix('μ', 'micro', -6), | ||
'm': SIPrefix('m', 'milli', -3), | ||
'c': SIPrefix('c', 'centi', -2), | ||
'd': SIPrefix('d', 'deci', -1), | ||
'k': SIPrefix('k', 'kilo', 3), | ||
'M': SIPrefix('M', 'mega', 6), | ||
'G': SIPrefix('G', 'giga', 9), | ||
'T': SIPrefix('T', 'tera', 12), | ||
'P': SIPrefix('P', 'peta', 15), | ||
'E': SIPrefix('E', 'exa', 18), | ||
'Z': SIPrefix('Z', 'zetta', 21), | ||
'Y': SIPrefix('Y', 'yotta', 24), | ||
} | ||
|
||
# The base SI unit stems for length, time, mass, amount of substance, | ||
# thermodynamic temperature, luminous intenstiy and current respectively: | ||
si_unit_stems = (u'm', u's', u'g', u'mol', u'K', u'cd', u'A') | ||
si_unit_stems = ('m', 's', 'g', 'mol', 'K', 'cd', 'A') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
# -*- coding: utf-8 -*- | ||
# symbol.py | ||
# A class representing a symbol (perhaps the label for a physical quantity | ||
# represented as a Quantity object) with a name in text, LaTeX and HTML. | ||
# | ||
# Copyright (C) 2012 Christian Hill | ||
# Copyright (C) 2012-2016 Christian Hill | ||
# Department of Physics and Astronomy, University College London | ||
# [email protected] | ||
# | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.