aboutsummaryrefslogtreecommitdiffstats
path: root/acispy
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2017-02-19 18:56:26 +0800
committerAaron LI <aaronly.me@outlook.com>2017-02-19 18:56:26 +0800
commitfee526538937585ceef57c9e4000de0b00be1ce7 (patch)
treeb8efd9d1aa293809cd36650e4ff9dc0bcdfd1b11 /acispy
parent2e5b01590e90f302151a50b2d11144a94f925ec6 (diff)
downloadchandra-acis-analysis-fee526538937585ceef57c9e4000de0b00be1ce7.tar.bz2
acispy/manifest.py: Split out 'parse_value_single()'
Diffstat (limited to 'acispy')
-rw-r--r--acispy/manifest.py60
1 files changed, 34 insertions, 26 deletions
diff --git a/acispy/manifest.py b/acispy/manifest.py
index ae013ec..79fbdd5 100644
--- a/acispy/manifest.py
+++ b/acispy/manifest.py
@@ -174,36 +174,43 @@ class Manifest:
del self.manifest[key]
self.save()
+ @classmethod
+ def parse_value(self, value):
+ """
+ Parse the given (list of) value(s) from string.
+
+ If the value list only has length of 1, then just return
+ the only element without list enclosure.
+ """
+ if isinstance(value, list):
+ pv = [self.parse_value_single(v) for v in value]
+ if len(pv) == 1:
+ return pv[0]
+ else:
+ return pv
+ else:
+ return self.parse_value_single(value)
+
@staticmethod
- def parse_value(values):
+ def parse_value_single(value):
"""
- Try to parse the given (list of) value(s) from string to
- integer or float.
+ Try to parse the given value from string to integer, float, boolean
+ or string.
"""
- if not isinstance(values, list):
- values = [values]
- #
- parsed_values = []
- for value in values:
+ try:
+ v = int(value)
+ except ValueError:
try:
- v = int(value)
+ v = float(value)
except ValueError:
- try:
- v = float(value)
- except ValueError:
- # string/boolean
- if value.lower() in ["true", "yes"]:
- v = True
- elif value.lower() in ["false", "no"]:
- v = False
- else:
- v = value # string
- parsed_values.append(v)
- #
- if len(parsed_values) == 1:
- return parsed_values[0]
- else:
- return parsed_values
+ # string/boolean
+ if value.lower() in ["true", "yes"]:
+ v = True
+ elif value.lower() in ["false", "no"]:
+ v = False
+ else:
+ v = value # string
+ return v
def find_manifest(filename="manifest.yaml", startdir=os.getcwd()):
@@ -275,7 +282,8 @@ def cmd_get(args, manifest):
if args.field:
print(value[args.field-1])
else:
- print(args.separator.join(value))
+ vs = [str(v) for v in value]
+ print(args.separator.join(vs))
else:
print(value)