Skip to content

Commit

Permalink
Refactor units into eeml.unit
Browse files Browse the repository at this point in the history
closes #21
  • Loading branch information
petervizi committed Jan 30, 2013
1 parent 235deab commit aec9299
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 172 deletions.
174 changes: 5 additions & 169 deletions eeml/__init__.py
Expand Up @@ -8,51 +8,15 @@
Look at the test directory.
"""

from datetime import date, datetime

try:
from lxml import etree
except ImportError: # If lxml is not there try python standard lib
from xml.etree import ElementTree as etree

from eeml.datastream import Cosm, CosmError, Pachube

__authors__ = "Peter Vizi"
__license__ = "GPLv3"
__docformat__ = "restructuredtext en"

EEML_SCHEMA_VERSION = '0.5.1'
EEML_NAMESPACE = 'http://www.eeml.org/xsd/{}'.format(EEML_SCHEMA_VERSION)
XSI_NAMESPACE = 'http://www.w3.org/2001/XMLSchema-instance'
SCHEMA_LOCATION = ('{{{}}}schemaLocation'.format(XSI_NAMESPACE),
EEML_NAMESPACE +
' http://www.eeml.org/xsd/{0}/{0}.xsd'
.format(EEML_SCHEMA_VERSION))
NSMAP = {None: EEML_NAMESPACE,
'xsi': XSI_NAMESPACE}

def _elem(name):
"""
Create an element in the EEML namespace
"""
return etree.Element("{{{}}}{}".format(EEML_NAMESPACE, name), nsmap=NSMAP)


def _addE(env, attr, name):
"""
Helper method to add child if not None
"""
if attr is not None:
tmp = _elem(name)
tmp.text = str(attr)
env.append(tmp)
from datetime import date, datetime

def _addA(env, attr, name):
"""
Helper method to add attribute if not None
"""
if attr is not None:
env.attrib[name] = str(attr)
from eeml.namespace import EEML_SCHEMA_VERSION, SCHEMA_LOCATION
from eeml.unit import Unit
from eeml.util import _elem, _addE, _addA

class Environment(object):
"""
Expand Down Expand Up @@ -364,6 +328,7 @@ def toeeml(self):

return data


class DataPoints(object):
"""
The DataPoints element of the document
Expand Down Expand Up @@ -397,135 +362,6 @@ def toeeml(self):

return data


class Unit(object):
"""
This class represents a unit element in the EEML document.
"""

__valid_types = ['basicSI', 'derivedSI', 'conversionBasedUnits',
'derivedUnits', 'contextDependentUnits']

def __init__(self, name, type_=None, symbol=None):
"""
:raise Exception: is sg is wrong
:param name: the name of this unit (eg. meter, Celsius)
:type name: `str`
:param type_: the type of this unit (``basicSI``, ``derivedSI``, ``conversionBasedUnits``, ``derivedUnits``, ``contextDependentUnits``)
:type type: `str`
:param symbol: the symbol of this unit (eg. m, C)
:type symbol: `str`
"""

self._name = name
if type_ is not None and not type_ in self.__valid_types:
raise ValueError("type must be {}, got '{}'".format(
", ".join(['%s'%s for s in self.__valid_types]), type_))
self._type = type_
self._symbol = symbol

def toeeml(self):
"""
Convert this object into a DOM element.
:return: the unit element
:rtype: `Element`
"""

unit = _elem('unit')

_addA(unit, self._type, 'type')
_addA(unit, self._symbol, 'symbol')

unit.text = self._name

return unit


class Celsius(Unit):
"""
Degree Celsius unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Celsius.
"""
Unit.__init__(self, 'Celsius', 'derivedSI', u'\xb0C')

class Degree(Unit):
"""
Degree of arc unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Degree.
"""
Unit.__init__(self, 'Degree', 'basicSI', u'\xb0')


class Fahrenheit(Unit):
"""
Degree Fahrenheit unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Fahrenheit.
"""
Unit.__init__(self, 'Fahrenheit', 'derivedSI', u'\xb0F')


class hPa(Unit):
"""
hPa unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with hPa.
"""
Unit.__init__(self, 'hPa', 'derivedSI', 'hPa')


class Knots(Unit):
"""
Knots class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Knots.
"""
Unit.__init__(self, 'Knots', 'conversionBasedUnits', u'kts')


class RH(Unit):
"""
Relative Humidity unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Relative Humidity.
"""
Unit.__init__(self, 'Relative Humidity', 'derivedUnits', '%RH')


class Watt(Unit):
"""
Watt unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Watt.
"""
Unit.__init__(self, 'Watt', 'derivedSI', 'W')


def create_eeml(env, loc, data):
"""
Create an `EEML` document from the parameters.
Expand Down
4 changes: 2 additions & 2 deletions eeml/datastream.py
Expand Up @@ -7,7 +7,7 @@
import re
from lxml import etree

url_pattern = re.compile("/v[12]/feeds/\d+\.xml")
URLPATTERN = re.compile("/v[12]/feeds/\d+\.xml")

class CosmError(Exception):
"""
Expand All @@ -33,7 +33,7 @@ def __init__(self, url, key, env=None, loc=None, dat=list(),
if not env:
env = eeml.Environment()
if str(url) == url:
if(url_pattern.match(url)):
if(URLPATTERN.match(url)):
self._url = url
else:
raise ValueError("The url argument has to be in the form "
Expand Down
14 changes: 14 additions & 0 deletions eeml/namespace.py
@@ -0,0 +1,14 @@
"""
XML namespace definitions
"""

EEML_SCHEMA_VERSION = '0.5.1'
EEML_NAMESPACE = 'http://www.eeml.org/xsd/{}'.format(EEML_SCHEMA_VERSION)
XSI_NAMESPACE = 'http://www.w3.org/2001/XMLSchema-instance'
SCHEMA_LOCATION = ('{{{}}}schemaLocation'.format(XSI_NAMESPACE),
EEML_NAMESPACE +
' http://www.eeml.org/xsd/{0}/{0}.xsd'
.format(EEML_SCHEMA_VERSION))
NSMAP = {None: EEML_NAMESPACE,
'xsi': XSI_NAMESPACE}

133 changes: 133 additions & 0 deletions eeml/unit.py
@@ -0,0 +1,133 @@
"""
This package stores all the available implementations of Unit
"""

from eeml.util import _elem, _addA

class Unit(object):
"""
This class represents a unit element in the EEML document.
"""

__valid_types = ['basicSI', 'derivedSI', 'conversionBasedUnits',
'derivedUnits', 'contextDependentUnits']

def __init__(self, name, type_=None, symbol=None):
"""
:raise Exception: is sg is wrong
:param name: the name of this unit (eg. meter, Celsius)
:type name: `str`
:param type_: the type of this unit (``basicSI``, ``derivedSI``, ``conversionBasedUnits``, ``derivedUnits``, ``contextDependentUnits``)
:type type: `str`
:param symbol: the symbol of this unit (eg. m, C)
:type symbol: `str`
"""

self._name = name
if type_ is not None and not type_ in self.__valid_types:
raise ValueError("type must be {}, got '{}'".format(
", ".join(['%s'%s for s in self.__valid_types]), type_))
self._type = type_
self._symbol = symbol

def toeeml(self):
"""
Convert this object into a DOM element.
:return: the unit element
:rtype: `Element`
"""

unit = _elem('unit')

_addA(unit, self._type, 'type')
_addA(unit, self._symbol, 'symbol')

unit.text = self._name

return unit


class Celsius(Unit):
"""
Degree Celsius unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Celsius.
"""
Unit.__init__(self, 'Celsius', 'derivedSI', u'\xb0C')

class Degree(Unit):
"""
Degree of arc unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Degree.
"""
Unit.__init__(self, 'Degree', 'basicSI', u'\xb0')


class Fahrenheit(Unit):
"""
Degree Fahrenheit unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Fahrenheit.
"""
Unit.__init__(self, 'Fahrenheit', 'derivedSI', u'\xb0F')


class hPa(Unit):
"""
hPa unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with hPa.
"""
Unit.__init__(self, 'hPa', 'derivedSI', 'hPa')


class Knots(Unit):
"""
Knots class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Knots.
"""
Unit.__init__(self, 'Knots', 'conversionBasedUnits', u'kts')


class RH(Unit):
"""
Relative Humidity unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Relative Humidity.
"""
Unit.__init__(self, 'Relative Humidity', 'derivedUnits', '%RH')


class Watt(Unit):
"""
Watt unit class.
"""

def __init__(self):
"""
Initialize the `Unit` parameters with Watt.
"""
Unit.__init__(self, 'Watt', 'derivedSI', 'W')

0 comments on commit aec9299

Please sign in to comment.