properly added new files to git and added test
This commit is contained in:
parent
7b6616ce55
commit
87a92a2e9e
|
@ -0,0 +1,139 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
pint.babel
|
||||||
|
~~~~~~~~~~
|
||||||
|
|
||||||
|
:copyright: 2016 by Pint Authors, see AUTHORS for more details.
|
||||||
|
:license: BSD, see LICENSE for more details.
|
||||||
|
"""
|
||||||
|
|
||||||
|
babel_units = dict(
|
||||||
|
standard_gravity='acceleration-g-force',
|
||||||
|
millibar='pressure-millibar',
|
||||||
|
metric_ton='mass-metric-ton',
|
||||||
|
megawatt='power-megawatt',
|
||||||
|
degF='temperature-fahrenheit',
|
||||||
|
dietary_calorie='energy-foodcalorie',
|
||||||
|
millisecond='duration-millisecond',
|
||||||
|
mph='speed-mile-per-hour',
|
||||||
|
acre_foot='volume-acre-foot',
|
||||||
|
mebibit='digital-megabit',
|
||||||
|
gibibit='digital-gigabit',
|
||||||
|
tebibit='digital-terabit',
|
||||||
|
mebibyte='digital-megabyte',
|
||||||
|
kibibyte='digital-kilobyte',
|
||||||
|
mm_Hg='pressure-millimeter-of-mercury',
|
||||||
|
month='duration-month',
|
||||||
|
kilocalorie='energy-kilocalorie',
|
||||||
|
cubic_mile='volume-cubic-mile',
|
||||||
|
arcsecond='angle-arc-second',
|
||||||
|
byte='digital-byte',
|
||||||
|
metric_cup='volume-cup-metric',
|
||||||
|
kilojoule='energy-kilojoule',
|
||||||
|
meter_per_second_squared='acceleration-meter-per-second-squared',
|
||||||
|
pint='volume-pint',
|
||||||
|
square_centimeter='area-square-centimeter',
|
||||||
|
in_Hg='pressure-inch-hg',
|
||||||
|
milliampere='electric-milliampere',
|
||||||
|
arcminute='angle-arc-minute',
|
||||||
|
MPG='consumption-mile-per-gallon',
|
||||||
|
hertz='frequency-hertz',
|
||||||
|
day='duration-day',
|
||||||
|
mps='speed-meter-per-second',
|
||||||
|
kilometer='length-kilometer',
|
||||||
|
square_yard='area-square-yard',
|
||||||
|
kelvin='temperature-kelvin',
|
||||||
|
kilogram='mass-kilogram',
|
||||||
|
kilohertz='frequency-kilohertz',
|
||||||
|
megahertz='frequency-megahertz',
|
||||||
|
meter='length-meter',
|
||||||
|
cubic_inch='volume-cubic-inch',
|
||||||
|
kilowatt_hour='energy-kilowatt-hour',
|
||||||
|
second='duration-second',
|
||||||
|
yard='length-yard',
|
||||||
|
light_year='length-light-year',
|
||||||
|
millimeter='length-millimeter',
|
||||||
|
metric_horsepower='power-horsepower',
|
||||||
|
gibibyte='digital-gigabyte',
|
||||||
|
## 'temperature-generic',
|
||||||
|
liter='volume-liter',
|
||||||
|
turn='angle-revolution',
|
||||||
|
microsecond='duration-microsecond',
|
||||||
|
pound='mass-pound',
|
||||||
|
ounce='mass-ounce',
|
||||||
|
calorie='energy-calorie',
|
||||||
|
centimeter='length-centimeter',
|
||||||
|
inch='length-inch',
|
||||||
|
centiliter='volume-centiliter',
|
||||||
|
troy_ounce='mass-ounce-troy',
|
||||||
|
gream='mass-gram',
|
||||||
|
kilowatt='power-kilowatt',
|
||||||
|
knot='speed-knot',
|
||||||
|
lux='light-lux',
|
||||||
|
hectoliter='volume-hectoliter',
|
||||||
|
microgram='mass-microgram',
|
||||||
|
degC='temperature-celsius',
|
||||||
|
tablespoon='volume-tablespoon',
|
||||||
|
cubic_yard='volume-cubic-yard',
|
||||||
|
square_foot='area-square-foot',
|
||||||
|
tebibyte='digital-terabyte',
|
||||||
|
square_inch='area-square-inch',
|
||||||
|
carat='mass-carat',
|
||||||
|
hectopascal='pressure-hectopascal',
|
||||||
|
gigawatt='power-gigawatt',
|
||||||
|
watt='power-watt',
|
||||||
|
micrometer='length-micrometer',
|
||||||
|
volt='electric-volt',
|
||||||
|
bit='digital-bit',
|
||||||
|
gigahertz='frequency-gigahertz',
|
||||||
|
teaspoon='volume-teaspoon',
|
||||||
|
ohm='electric-ohm',
|
||||||
|
joule='energy-joule',
|
||||||
|
cup='volume-cup',
|
||||||
|
square_mile='area-square-mile',
|
||||||
|
nautical_mile='length-nautical-mile',
|
||||||
|
square_meter='area-square-meter',
|
||||||
|
mile='length-mile',
|
||||||
|
acre='area-acre',
|
||||||
|
nanometer='length-nanometer',
|
||||||
|
hour='duration-hour',
|
||||||
|
astronomical_unit='length-astronomical-unit',
|
||||||
|
liter_per_100kilometers ='consumption-liter-per-100kilometers',
|
||||||
|
megaliter='volume-megaliter',
|
||||||
|
ton='mass-ton',
|
||||||
|
hectare='area-hectare',
|
||||||
|
square_kilometer='area-square-kilometer',
|
||||||
|
kibibit='digital-kilobit',
|
||||||
|
mile_scandinavian='length-mile-scandinavian',
|
||||||
|
liter_per_kilometer='consumption-liter-per-kilometer',
|
||||||
|
century='duration-century',
|
||||||
|
cubic_foot='volume-cubic-foot',
|
||||||
|
deciliter='volume-deciliter',
|
||||||
|
##pint='volume-pint-metric',
|
||||||
|
cubic_meter='volume-cubic-meter',
|
||||||
|
cubic_kilometer='volume-cubic-kilometer',
|
||||||
|
quart='volume-quart',
|
||||||
|
cc='volume-cubic-centimeter',
|
||||||
|
pound_force_per_square_inch='pressure-pound-per-square-inch',
|
||||||
|
milligram='mass-milligram',
|
||||||
|
kph='speed-kilometer-per-hour',
|
||||||
|
minute='duration-minute',
|
||||||
|
parsec='length-parsec',
|
||||||
|
picometer='length-picometer',
|
||||||
|
degree='angle-degree',
|
||||||
|
milliwatt='power-milliwatt',
|
||||||
|
week='duration-week',
|
||||||
|
ampere='electric-ampere',
|
||||||
|
milliliter='volume-milliliter',
|
||||||
|
decimeter='length-decimeter',
|
||||||
|
fluid_ounce='volume-fluid-ounce',
|
||||||
|
nanosecond='duration-nanosecond',
|
||||||
|
foot='length-foot',
|
||||||
|
karat='proportion-karat',
|
||||||
|
year='duration-year',
|
||||||
|
gallon='volume-gallon',
|
||||||
|
radian='angle-radian',
|
||||||
|
)
|
||||||
|
|
||||||
|
babel_forms = ['narrow', 'short', 'long']
|
||||||
|
|
|
@ -127,3 +127,12 @@ except ImportError:
|
||||||
ufloat = None
|
ufloat = None
|
||||||
HAS_UNCERTAINTIES = False
|
HAS_UNCERTAINTIES = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
from babel import Locale as Loc
|
||||||
|
HAS_BABEL = True
|
||||||
|
HAS_PROPER_BABEL = hasattr(Loc, 'unit_patterns')
|
||||||
|
except ImportError:
|
||||||
|
HAS_PROPER_BABEL = HAS_BABEL = False
|
||||||
|
|
||||||
|
if not HAS_PROPER_BABEL:
|
||||||
|
Loc = None
|
||||||
|
|
|
@ -114,6 +114,9 @@ def formatter(items, as_ratio=True, single_denominator=False,
|
||||||
:param division_fmt: the format used for division.
|
:param division_fmt: the format used for division.
|
||||||
:param power_fmt: the format used for exponentiation.
|
:param power_fmt: the format used for exponentiation.
|
||||||
:param parentheses_fmt: the format used for parenthesis.
|
:param parentheses_fmt: the format used for parenthesis.
|
||||||
|
:param locale: the locale object as defined in babel.
|
||||||
|
:param form: the length of the translated unit, as defined in babel cldr.
|
||||||
|
:param plural_form: the plural form, calculated as defined in babel.
|
||||||
|
|
||||||
:return: the formula as a string.
|
:return: the formula as a string.
|
||||||
"""
|
"""
|
||||||
|
@ -129,7 +132,7 @@ def formatter(items, as_ratio=True, single_denominator=False,
|
||||||
pos_terms, neg_terms = [], []
|
pos_terms, neg_terms = [], []
|
||||||
|
|
||||||
for key, value in sorted(items):
|
for key, value in sorted(items):
|
||||||
if locale and form and plural_form:
|
if locale and form and plural_form and hasattr(locale, 'unit_patterns'):
|
||||||
_key = babel_units.get(key)
|
_key = babel_units.get(key)
|
||||||
if _key:
|
if _key:
|
||||||
patterns = locale.unit_patterns
|
patterns = locale.unit_patterns
|
||||||
|
|
|
@ -7,7 +7,7 @@ import doctest
|
||||||
from distutils.version import StrictVersion
|
from distutils.version import StrictVersion
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from pint.compat import unittest, HAS_NUMPY, HAS_UNCERTAINTIES, NUMPY_VER, PYTHON3
|
from pint.compat import unittest, HAS_NUMPY, HAS_PROPER_BABEL, HAS_UNCERTAINTIES, NUMPY_VER, PYTHON3
|
||||||
|
|
||||||
|
|
||||||
def requires_numpy18():
|
def requires_numpy18():
|
||||||
|
@ -30,6 +30,10 @@ def requires_not_numpy():
|
||||||
return unittest.skipIf(HAS_NUMPY, 'Requires NumPy is not installed.')
|
return unittest.skipIf(HAS_NUMPY, 'Requires NumPy is not installed.')
|
||||||
|
|
||||||
|
|
||||||
|
def requires_proper_babel():
|
||||||
|
return unittest.skipUnless(HAS_PROPER_BABEL, 'Requires Babel with units support')
|
||||||
|
|
||||||
|
|
||||||
def requires_uncertainties():
|
def requires_uncertainties():
|
||||||
return unittest.skipUnless(HAS_UNCERTAINTIES, 'Requires Uncertainties')
|
return unittest.skipUnless(HAS_UNCERTAINTIES, 'Requires Uncertainties')
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import division, unicode_literals, print_function, absolute_import
|
||||||
|
|
||||||
|
from pint.compat import Loc
|
||||||
|
from pint.testsuite import helpers, BaseTestCase
|
||||||
|
from pint import UnitRegistry
|
||||||
|
|
||||||
|
class TestBabel(BaseTestCase):
|
||||||
|
|
||||||
|
@helpers.requires_proper_babel()
|
||||||
|
def test_babel(self):
|
||||||
|
ureg = UnitRegistry()
|
||||||
|
ureg.load_definitions('../xtranslated.txt')
|
||||||
|
locale = Loc('fr', 'FR')
|
||||||
|
|
||||||
|
distance = 24.0 * ureg.meter
|
||||||
|
self.assertEqual(
|
||||||
|
distance.format_babel(locale=locale, form='long'),
|
||||||
|
u'24.0 mètres'
|
||||||
|
)
|
||||||
|
time = 8.0 * ureg.second
|
||||||
|
self.assertEqual(
|
||||||
|
time.format_babel(locale=locale, form='long'),
|
||||||
|
u'8.0 secondes'
|
||||||
|
)
|
||||||
|
velocity = distance / time ** 2
|
||||||
|
self.assertEqual(
|
||||||
|
velocity.format_babel(locale=locale, form='long'),
|
||||||
|
u'0.375 mètre par seconde²'
|
||||||
|
)
|
|
@ -0,0 +1,26 @@
|
||||||
|
|
||||||
|
# a few unit definitions added to use the translations by unicode cldr
|
||||||
|
|
||||||
|
dietary_calorie = 1000 * calorie = Calorie
|
||||||
|
metric_cup = liter / 4
|
||||||
|
mps = meter / second
|
||||||
|
square_inch = inch ** 2 = sq_in
|
||||||
|
square_mile = mile ** 2 = sq_mile
|
||||||
|
square_meter = kilometer ** 2 = sq_m
|
||||||
|
square_kilometer = kilometer ** 2 = sq_km
|
||||||
|
mile_scandinavian = 10000 * meter
|
||||||
|
century = 100 * year
|
||||||
|
cubic_mile = 1 * mile ** 3 = cu_mile = cubic_miles
|
||||||
|
cubic_yard = 1 * yard ** 3 = cu_yd = cubic_yards
|
||||||
|
cubic_foot = 1 * foot ** 3 = cu_ft = cubic_feet
|
||||||
|
cubic_inch = 1 * inch ** 3 = cu_in = cubic_inches
|
||||||
|
cubic_meter = 1 * meter ** 3 = cu_m
|
||||||
|
cubic_kilometer = 1 * kilometer ** 3 = cu_km
|
||||||
|
karat = [purity] = Karat
|
||||||
|
|
||||||
|
[consumption] = [volume] / [length]
|
||||||
|
liter_per_kilometer = liter / kilometer
|
||||||
|
liter_per_100kilometers = liter / (100 * kilometers)
|
||||||
|
|
||||||
|
[US_consumption] = [length] / [volume]
|
||||||
|
MPG = mile / gallon
|
Loading…
Reference in New Issue