From: <zk...@us...> - 2009-01-20 15:23:36
|
Revision: 601 http://pyphant.svn.sourceforge.net/pyphant/?rev=601&view=rev Author: zklaus Date: 2009-01-20 15:23:29 +0000 (Tue, 20 Jan 2009) Log Message: ----------- Refactured quantities parsers of FMFLoader into new module of the Pyphant core. Modified Paths: -------------- trunk/src/workers/fmfile/fmfile/FMFLoader.py Added Paths: ----------- trunk/src/pyphant/pyphant/quantities/ParseQuantities.py Added: trunk/src/pyphant/pyphant/quantities/ParseQuantities.py =================================================================== --- trunk/src/pyphant/pyphant/quantities/ParseQuantities.py (rev 0) +++ trunk/src/pyphant/pyphant/quantities/ParseQuantities.py 2009-01-20 15:23:29 UTC (rev 601) @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2008, Rectorate of the University of Freiburg +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the Freiburg Materials Research Center, +# University of Freiburg nor the names of its contributors may be used to +# endorse or promote products derived from this software without specific +# prior written permission. +# +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER +# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +u""" +""" + +__id__ = "$Id$" +__author__ = "$Author$" +__version__ = "$Revision$" +# $Source$ + +from PhysicalQuantities import PhysicalQuantity + +def str2unit(unit): + if unit.startswith('.'): + unit = '0'+unit + elif unit.endswith('%'): + unit = float(unit[:-1])/100.0 + elif unit.endswith('a.u.'): + try: + unit = float(unit[:-4]) + except: + unit = 1.0 + elif not (unit[0].isdigit() or unit[0]=='-'): + unit = '1'+unit + try: + unit = unit.replace('^', '**') + unit = PhysicalQuantity(unit.encode('utf-8')) + except: + unit = float(unit) + return unit + +def parseQuantity(value): + import re + pm = re.compile(ur"(?:\\pm|\+-|\+/-)") + try: + value, error = [s.strip() for s in pm.split(value)] + except: + error = None + if value.startswith('('): + value = float(value[1:]) + error, unit = [s.strip() for s in error.split(')')] + unit = str2unit(unit) + value *= unit + else: + value = str2unit(value) + if error != None: + if error.endswith('%'): + error = value*float(error[:-1])/100.0 + else: + try: + error = float(error)*unit + except: + error = str2unit(error) + return value, error + +def parseVariable(oldVal): + shortname, value = tuple([s.strip() for s in oldVal.split('=')]) + value, error = parseQuantity(value) + return (shortname, value, error) Modified: trunk/src/workers/fmfile/fmfile/FMFLoader.py =================================================================== --- trunk/src/workers/fmfile/fmfile/FMFLoader.py 2009-01-20 12:59:31 UTC (rev 600) +++ trunk/src/workers/fmfile/fmfile/FMFLoader.py 2009-01-20 15:23:29 UTC (rev 601) @@ -41,6 +41,7 @@ from pyphant.core import (Worker, Connectors, Param, DataContainer) from pyphant.quantities.PhysicalQuantities import PhysicalQuantity,isPhysicalUnit,isPhysicalQuantity +from pyphant.quantities.ParseQuantities import parseQuantity, parseVariable, str2unit import mx.DateTime.ISO import logging _logger = logging.getLogger("pyphant") @@ -240,61 +241,14 @@ config = FMFConfigObj(d.encode('utf-8').splitlines(), encoding='utf-8') return config2tables(preParsedData, config) -def str2unit(unit): - if unit.startswith('.'): - unit = '0'+unit - elif unit == '%': - unit = 0.01 - elif unit.endswith('a.u.'): - try: - unit = float(unit[:-4]) - except: - unit = 1.0 - elif not (unit[0].isdigit() or unit[0]=='-'): - unit = '1'+unit - try: - unit = unit.replace('^', '**') - unit = PhysicalQuantity(unit.encode('utf-8')) - except: - unit = float(unit) - return unit +def parseBool(value): + if value.lower() == 'true': + return True + elif value.lower() == 'false': + return False + raise AttributeError def config2tables(preParsedData, config): - def parseVariable(oldVal): - shortname, value = tuple([s.strip() for s in oldVal.split('=')]) - value, error = parseQuantity(value) - return (shortname, value, error) - - def parseQuantity(value): - pm = re.compile(ur"(?:\\pm|\+-|\+/-)") - try: - value, error = [s.strip() for s in pm.split(value)] - except: - error = None - if value.startswith('('): - value = float(value[1:]) - error, unit = [s.strip() for s in error.split(')')] - unit = str2unit(unit) - value *= unit - else: - value = str2unit(value) - if error != None: - if error.endswith('%'): - error = value*float(error[:-1])/100.0 - else: - try: - error = float(error)*unit - except: - error = str2unit(error) - return value, error - - def parseBool(value): - if value.lower() == 'true': - return True - elif value.lower() == 'false': - return False - raise AttributeError - converters = [ int, float, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |