1"""! Module for parsing param files. """
223 if isinstance(file, str):
224 with open(file)
as f:
225 firstLine = f.readline()
226 fl = firstLine.split()
228 raise Exception(
'This is not a valid parameter file.')
230 self.
label = fl[0][:-1]
247 line = file.readline()
257 raise Exception(
'Invalid syntax for a Composite element')
258 elif l[0][-1] ==
'[':
263 p =
Array(label, file,
None)
269 for i
in range(1, len(l)-1):
271 p =
Array(l[0][:-1],
None, val)
273 for i
in range(1,len(l)):
275 p =
Array(l[0][:-1], file, val)
276 elif l[0][-1] ==
'(':
283 raise Exception(
'Invalid syntax for Matrix parameter')
290 raise Exception(
'Invalid parameter file line')
300 for i
in range(1, len(l)):
305 line = file.readline()
322 if (label ==
'label')
or (label ==
'_children'):
323 raise Exception(
'Illegal parameter file label')
326 if not isinstance(current, list):
371 if attr ==
'_children' :
376 if isinstance(self.
_children[attr], list):
412 s = depth + self.
label +
'{' +
'\n'
414 if isinstance(item, list):
415 for i
in range(len(item)):
418 s += item.getString(depth+
' ')
434 with open(filename,
'w')
as f:
462 if isinstance(val, list):
476 if isinstance(val, list):
478 raise Exception(
'Not valid input for Parameter.')
480 for i
in range(len(val)):
506 if isinstance(self.
val, list):
507 s += depth + f
'{self.label:40}'
508 s += f
'{self.val[0]:>6}'
509 for i
in range(1, len(self.
val)):
510 s += f
'{self.val[i]:>7}'
513 s += depth + f
'{self.label:20}'
514 if isinstance(self.
val, float):
515 v = f
'{self.val:.12e}'
518 s += f
'{self.val:>20}\n'
576 line = file.readline()
586 for i
in range(len(l)):
590 line = file.readline()
616 s += depth + self.
label +
'[' +
'\n'
617 if not isinstance(self.
val[0], list):
619 for i
in range(len(self.
val)):
620 if isinstance(self.
val[i], float):
621 v = f
'{self.val[i]:.12e}'
622 s += depth + f
'{v:>40}\n'
624 s += depth + f
'{self.val[i]:>40}\n'
627 for i
in range(len(self.
val)):
630 for j
in range(len(self.
val[i])):
631 if isinstance(self.
val[i][j], int):
632 s += f
'{self.val[i][j]:>8}'
633 elif isinstance(self.
val[i][j], float):
634 v = f
'{self.val[i][j]:.11e}'
637 s += f
'{self.val[i][j]:>22}'
658 if not isinstance(val, list):
659 raise Exception(
'Value of an Array must be a list')
662 if isinstance(val[0], list):
664 for i
in range(len(val)):
669 for i
in range(len(val)):
672 for i
in range(len(val)):
674 for j
in range(len(val[i])):
675 v[i].append(
getValue(str(val[i][j])))
677 for i
in range(len(val)):
718 line = file.readline()
722 line = file.readline()
726 for i
in range(1, len(att)):
727 if att[i][0] > rowMax:
729 if att[i][1] > colMax:
731 size = int(max(rowMax, colMax))+1
732 for i
in range(0, size):
734 for j
in range(0, size):
736 for i
in range(0, len(att)):
737 self.
val[int(att[i][0])][int(att[i][1])] =
getValue(att[i][2])
738 self.
val[int(att[i][1])][int(att[i][0])] =
getValue(att[i][2])
761 s += depth + self.
label +
'(\n'
762 for i
in range(len(self.
val)):
764 s += depth + f
'{i:>24}{j:>5} ' + f
'{self.val[i][j]:.12e}\n'
783 if not isinstance(val, list):
784 raise TypeError(
'This is not a valid value for Matrix.')
786 if isinstance(val[0], list):
788 if len(val) != len(val[0]):
789 raise Exception(
'Input Matrix should be square')
790 for i
in range(len(val)):
792 if val[i][j] != val[j][i]:
793 raise Exception(
'This is not a symmetric Matrix.')
794 for i
in range(len(val)):
796 raise Exception(
'The diagonal of the Matrix should all be zero.')
798 for i
in range(len(val)):
800 for j
in range(len(val[0])):
805 raise Exception(
'Not valid input format for Matrix modification.')
806 elif (type(val[0]) != int)
or (type(val[1]) != int)
or ((type(val[-1]) != int)
and (type(val[-1]) != float)):
807 raise Exception(
'Not valid input format for Matrix modification.')
827 if (v.isdigit() ==
True)
or (v[0] ==
'-' and v[1:].isdigit() ==
True):
Container for data of an array in a param file.
returnData(self)
Return the value of this Array, as a list of element values.
getString(self, depth='')
Indented string representation of this Array object.
read(self, file)
Read the elements of this Array from a file.
__init__(self, label, file, val=None)
Constructor.
setValue(self, val)
Set the new value to the Array object.
__str__(self)
String representation of this Array object.
Container for data of a Composite in a param file.
addChild(self, child)
Add a single child to this Composite.
__str__(self)
Return an indented string representation of this Composite.
returnData(self)
Return the Composite object itself.
getString(self, depth='')
Get an indented string representation of this Composite.
getChildren(self)
Get the dictionary of child items.
write(self, filename)
Write the indented param file string for this to a file.
read(self, file)
Read and parse a parameter block from a file.
__init__(self, file=None, label=None)
Constructor.
__setattr__(self, label, val)
Set a new value for a specified child or attribute.
__getattr__(self, attr)
Return the value of one child of this Composite.
A Matrix represents a matrix-valued parameter in parameter file.
returnData(self)
Return the value of this Matrix as a list of lists.
__init__(self, label, file)
Constructor.
read(self, file)
Read the a parameter block from a file.
setValue(self, val)
Set a new value for this Matrix.
__str__(self)
String representation of this Matrix object.
getString(self, depth='')
Indented string representation of this Matrix object.
A Parameter represents a single parameter in a parameter file.
__str__(self)
Unindented string representation of this Parameter object.
returnData(self)
Return the stored value.
setValue(self, val)
Set the new value to the Parameter object.
getString(self, depth='')
Indented string for the Parameter object.
__init__(self, label, val)
Constructor.
getValue(v)
Distinguish the type of a value from its string representation.