|
From: <zk...@us...> - 2008-12-16 10:48:20
|
Revision: 588
http://pyphant.svn.sourceforge.net/pyphant/?rev=588&view=rev
Author: zklaus
Date: 2008-12-16 10:48:16 +0000 (Tue, 16 Dec 2008)
Log Message:
-----------
Refactured function config2tables of FMFLoader in order to parse item types more easily.
Function config2tables also parses quantities without symbol and booleans.
Modified Paths:
--------------
trunk/doc/demo/example.fmf
trunk/src/workers/fmfile/fmfile/FMFLoader.py
Modified: trunk/doc/demo/example.fmf
===================================================================
--- trunk/doc/demo/example.fmf 2008-12-11 10:14:14 UTC (rev 587)
+++ trunk/doc/demo/example.fmf 2008-12-16 10:48:16 UTC (rev 588)
@@ -32,6 +32,14 @@
Complex number with zero real part : 2J
Complex number with zero imaginary part: 1+0J
list of complex: 1+2j, 1+2J
+[Boolean values]
+true1: True
+true2: true
+true3: TRUE
+false1: False
+false2: false
+false3: FALSE
+list of booleans: True, true, TRUE, False, false, FALSE
[Physical Quantities]
number: N = 2
voltage: U = 1V
@@ -50,6 +58,23 @@
trailing amplitude, relative error: \omega = (1 +- 1%) 2 ohm
estimated parameter: p = 1 \pm 0.1
another estimated parameter: p = 1 \pm 1%
+list of quantities: number: N = 2, U = 1V, T_C = 22 degC
+[Simplified quantities]
+voltage: 1V
+temperature in Celsius: 22 degC
+resistance: 2.0 kg*m^2*A^-2*s^-3
+[Simplified Quantities with error]
+voltage: 1 V +- 1 mV
+current: 1 A +/- 0.001 A
+energy: 1 J \pm 1 mJ
+work: 1 W +- 0.1%
+voltage in brackets: (1 +- 0.001) V
+current in brackets: (1 +- 0.1%) A
+trailing amplitude: (1 +- 0.01) 2 ohm
+trailing amplitude, relative error: (1 +- 1%) 2 ohm
+estimated parameter: 1 \pm 0.1
+another estimated parameter: 1 \pm 1%
+list of quantities: 1V, 22 degC,(1 +- 1%) 2 ohm
[*table definitions]
table: T
mixed: M
Modified: trunk/src/workers/fmfile/fmfile/FMFLoader.py
===================================================================
--- trunk/src/workers/fmfile/fmfile/FMFLoader.py 2008-12-11 10:14:14 UTC (rev 587)
+++ trunk/src/workers/fmfile/fmfile/FMFLoader.py 2008-12-16 10:48:16 UTC (rev 588)
@@ -240,6 +240,49 @@
return config2tables(preParsedData, config)
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 = [ parseVariable,
+ parseQuantity,
+ int,
+ float,
+ complex,
+ parseBool,
+ ]
+
def str2unit(unit):
if unit.startswith('.'):
unit = '0'+unit
@@ -252,54 +295,18 @@
return unit
def item2value(section, key):
- pm = re.compile(ur"(?:\\pm|\+-|\+/-)")
oldVal = section[key]
- try:
- shortname, value = tuple([s.strip() for s in oldVal.split('=')])
+ if type(oldVal)==type([]):
+ for c in converters:
+ try:
+ return map(c,oldVal)
+ except:
+ pass
+ for c in converters:
try:
- value, error = [s.strip() for s in pm.split(value)]
+ return c(oldVal)
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 (shortname, value, error)
- except:
- if type(oldVal)==type([]):
- try:
- return map(int,oldVal)
- except:
- try:
- return map(float,oldVal)
- except:
- try:
- return map(complex,oldVal)
- except:
- pass
- else:
- try:
- return int(oldVal)
- except:
- try:
- return float(oldVal)
- except:
- try:
- return complex(oldVal)
- except:
- pass
-
+ pass
return oldVal
if config.has_key('*table definitions'):
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|