1 def openPossiblyCompressedFile(baseFilename):
3 Tries to return an object with file semantics for the given path, or a path
4 closely related as described below.
6 When talking about `Plain` and `BZ2` instances below, what is meant is:
7 - For `python2`, `Plain` corresponds to the type `file`, and `BZ2`
8 corresponds to the type `bz2.BZ2File`.
9 - For `python3`, `Plain` and `BZ2` both correspond to `_io.TextIOWrapper`.
11 If `baseFilename` ends in the string `.bz2`, a `BZ2` instance
12 is attempted to be returned in `rt` mode for that file path.
13 Otherwise, if the concatenation of `baseFilename` and the string `.bz2`
14 resolves to a file, this function tries to open it in `rt` mode and return
15 the resulting `BZ2` instance.
16 Otherwise, if `baseFilename` resolves to a file, `open` is called on it
17 with an open mode of `rt`, and that object is returned.
18 Finally, if none of the above applies, an instance of `ValueError` is
22 Throws if a viable candidate is found, as described above, but the
23 file could not be opened.
25 Throws if no viable candidate file can be found.
27 @param[in] baseFilename
28 A path to use to open a file, as described above.
35 if sys.version_info[0] < 3:
37 return bz2.BZ2File(path,
"r")
40 return bz2.open(path,
"rt")
42 if baseFilename[-4:] ==
'.bz2':
43 return openBZ2(baseFilename)
45 if os.path.isfile(baseFilename +
'.bz2'):
46 return openBZ2(baseFilename +
'.bz2')
48 if os.path.isfile(baseFilename):
49 return open(baseFilename,
"rt")
51 raise ValueError(
"No such file: " + baseFilename)
54 def readValuePairsFromFile(filename, minXValue = None):
56 Returns a `collections.OrderedDict`, containing key-value-pairs read from
59 The given `filename` is supplied to `openPossiblyCompressedFile`. Then,
60 each line is read. If it consists of exactly two whitespace-separated
61 sub-strings, and each can be interpreted as a `float`, let the left column,
62 interpreted as a `float`, be the key, and the right column, interpreted as
63 a `float` be the value; otherwise, the line is ignored.
64 Then, if `minXValue` is `None`, or if the key is smaller than `minXValue`
65 (as determined by the `<` operator), the key-value-pair is added to the
66 dictionary to be returned; otherwise, the line is ignored.
68 Finally, after treating all lines in the file as described, the dictionary
69 is sorted by key and returned.
72 See `openPossiblyCompressedFile`.
74 See `openPossiblyCompressedFile`.
76 Throws if a key appears twice in the file (not counting ignored
80 from collections
import OrderedDict
84 f = openPossiblyCompressedFile(filename)
86 columns = line.split()
96 msg =
"A point has been given twice in readValuePairsFromFile: "
100 if minXValue
is not None:
106 return OrderedDict(sorted(data.items()))
108 def getConfigValueAndCheckConstistency(config, valueKey, knownValue):
110 Given the configuration instance `config`, fetches the value associated to
111 `valueKey` and compares it to `knownValue`.
113 If `knownValue` is not `None`, it is compared to the given config's value.
114 If it does not match, an instance of `ValueError` is raised.
117 Throws if any argument is of the wrong type.
119 Throws if the given `valueKey` does not exist in the given `config`.
121 Throws if `knownValue` is not `None`, but does not equal the given
122 config's value for the given `valueKey`.
125 An instance of `MPCDAnalysis.Configuration.Configuration`.
127 The configuration element to consider, as a `str` instance.
128 @param[in] knownValue
129 The known value for the given `valueKey`, or `None` if unknown.
131 @return Returns the given config's value for the `valueKey`.
134 from .Configuration
import Configuration
137 if not isinstance(config, Configuration):
139 if not isinstance(valueKey, str):
143 currentValue = config[valueKey]
145 if knownValue
is None:
148 if knownValue != currentValue:
149 msg =
"Key " + valueKey +
" yielded value "
150 msg += str(currentValue)
151 msg +=
" rather than the expeded value "
152 msg += str(knownValue)
153 raise ValueError(msg)
158 def getConsistentConfigValue(rundirs, valueKey):
160 For the given `valueKey`, gets the configuration value for all the given
161 `rundirs` if it is consistent, and throws otherwise.
164 Throws if any argument is of the wrong type.
166 Throws if not all configurations agree on the value, or the value
167 does not exist in at least one configuration.
170 A `list` of `str` objects, each representing a directory from
171 which to load the file `config.txt` in an instance of
172 `MPCDAnalysis.Configuration.Configuration`.
174 The configuration key to consider, as a `str`.
176 @return Returns the common value.
179 from .Configuration
import Configuration
182 if not isinstance(rundirs, list):
184 for rundir
in rundirs:
185 if not isinstance(rundir, str):
187 if not isinstance(valueKey, str):
194 for rundir
in rundirs:
195 config = Configuration(rundir +
"/config.txt")
196 value = getConfigValueAndCheckConstistency(config, valueKey, value)
203 def getNumberOfArgumentsFromCallable(f):
205 Returns the number of arguments for the given callable `f`.
208 The callable to inspect.
214 if sys.version_info[0] < 3:
215 args = inspect.getargspec(f).args
217 args = inspect.signature(f).parameters