convert.py (13663:9b64aeabf9a5) convert.py (13696:b303ca652433)
1# Copyright (c) 2005 The Regents of The University of Michigan
2# Copyright (c) 2010 Advanced Micro Devices, Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;

--- 79 unchanged lines hidden (view full) ---

88}
89
90def assertStr(value):
91 if not isinstance(value, str):
92 raise TypeError("wrong type '%s' should be str" % type(value))
93
94
95# memory size configuration stuff
1# Copyright (c) 2005 The Regents of The University of Michigan
2# Copyright (c) 2010 Advanced Micro Devices, Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;

--- 79 unchanged lines hidden (view full) ---

88}
89
90def assertStr(value):
91 if not isinstance(value, str):
92 raise TypeError("wrong type '%s' should be str" % type(value))
93
94
95# memory size configuration stuff
96def toFloat(value, target_type='float', units=None, prefixes=[]):
96def toNum(value, target_type, units, prefixes, converter):
97 assertStr(value)
98
97 assertStr(value)
98
99 def convert(val):
100 try:
101 return converter(val)
102 except ValueError:
103 raise ValueError(
104 "cannot convert '%s' to %s" % (value, target_type))
105
99 if units and not value.endswith(units):
100 units = None
101 if not units:
106 if units and not value.endswith(units):
107 units = None
108 if not units:
102 try:
103 return float(value)
104 except ValueError:
105 raise ValueError("cannot convert '%s' to %s" % \
106 (value, target_type))
109 return convert(value)
107
108 value = value[:-len(units)]
109
110 prefix = next((p for p in prefixes.keys() if value.endswith(p)), None)
111 if not prefix:
110
111 value = value[:-len(units)]
112
113 prefix = next((p for p in prefixes.keys() if value.endswith(p)), None)
114 if not prefix:
112 return float(value)
115 return convert(value)
113 value = value[:-len(prefix)]
114
116 value = value[:-len(prefix)]
117
115 return float(value) * prefixes[prefix]
118 return convert(value) * prefixes[prefix]
116
119
120def toFloat(value, target_type='float', units=None, prefixes=[]):
121 return toNum(value, target_type, units, prefixes, float)
122
117def toMetricFloat(value, target_type='float', units=None):
118 return toFloat(value, target_type, units, metric_prefixes)
119
120def toBinaryFloat(value, target_type='float', units=None):
121 return toFloat(value, target_type, units, binary_prefixes)
122
123def toInteger(value, target_type='integer', units=None, prefixes=[]):
123def toMetricFloat(value, target_type='float', units=None):
124 return toFloat(value, target_type, units, metric_prefixes)
125
126def toBinaryFloat(value, target_type='float', units=None):
127 return toFloat(value, target_type, units, binary_prefixes)
128
129def toInteger(value, target_type='integer', units=None, prefixes=[]):
124 value = toFloat(value, target_type, units, prefixes)
125 result = long(value)
126 if value != result:
127 raise ValueError("cannot convert '%s' to integer %s" % \
128 (value, target_type))
130 intifier = lambda x: int(x, 0)
131 return toNum(value, target_type, units, prefixes, intifier)
129
132
130 return result
131
132def toMetricInteger(value, target_type='integer', units=None):
133 return toInteger(value, target_type, units, metric_prefixes)
134
135def toBinaryInteger(value, target_type='integer', units=None):
136 return toInteger(value, target_type, units, binary_prefixes)
137
138def toBool(value):
139 assertStr(value)

--- 108 unchanged lines hidden ---
133def toMetricInteger(value, target_type='integer', units=None):
134 return toInteger(value, target_type, units, metric_prefixes)
135
136def toBinaryInteger(value, target_type='integer', units=None):
137 return toInteger(value, target_type, units, binary_prefixes)
138
139def toBool(value):
140 assertStr(value)

--- 108 unchanged lines hidden ---