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 v = f
'{self.val[i]:.12e}'
621 s += depth + f
'{v:>40}\n'
624 if isinstance(self.
val[0][0], int) & (len(self.
val[0]) == 2):
625 for i
in range(len(self.
val)):
626 v = f
'{self.val[i][1]:.12e}'
627 s += depth + f
'{self.val[i][0]:>41}{v:>22}\n'
629 for i
in range(len(self.
val)):
630 s += depth + f
'{self.val[i][0]:^20}'
631 for j
in range(1, len(self.
val[0])):
632 if j == (len(self.
val[0])-1):
633 if self.
val[i][j] < 0:
634 v = f
'{self.val[i][j]:.11e}'
636 v = f
'{self.val[i][j]:.12e}'
639 s += f
'{self.val[i][j]}'
641 s += f
'{self.val[i][j]:>5}'
660 if not isinstance(val, list):
661 raise Exception(
'Value of an Array must be a list')
664 if isinstance(val[0], list):
666 for i
in range(len(val)):
671 for i
in range(len(val)):
674 for i
in range(len(val)):
676 for j
in range(len(val[i])):
677 v[i].append(
getValue(str(val[i][j])))
679 for i
in range(len(val)):
720 line = file.readline()
724 line = file.readline()
728 for i
in range(1, len(att)):
729 if att[i][0] > rowMax:
731 if att[i][1] > colMax:
733 size = int(max(rowMax, colMax))+1
734 for i
in range(0, size):
736 for j
in range(0, size):
738 for i
in range(0, len(att)):
739 self.
val[int(att[i][0])][int(att[i][1])] =
getValue(att[i][2])
740 self.
val[int(att[i][1])][int(att[i][0])] =
getValue(att[i][2])
763 s += depth + self.
label +
'(\n'
764 for i
in range(len(self.
val)):
766 s += depth + f
'{i:>24}{j:>5} ' + f
'{self.val[i][j]:.12e}\n'
785 if not isinstance(val, list):
786 raise TypeError(
'This is not a valid value for Matrix.')
788 if isinstance(val[0], list):
790 if len(val) != len(val[0]):
791 raise Exception(
'Input Matrix should be square')
792 for i
in range(len(val)):
794 if val[i][j] != val[j][i]:
795 raise Exception(
'This is not a symmetric Matrix.')
796 for i
in range(len(val)):
798 raise Exception(
'The diagonal of the Matrix should all be zero.')
800 for i
in range(len(val)):
802 for j
in range(len(val[0])):
807 raise Exception(
'Not valid input format for Matrix modification.')
808 elif (type(val[0]) != int)
or (type(val[1]) != int)
or ((type(val[-1]) != int)
and (type(val[-1]) != float)):
809 raise Exception(
'Not valid input format for Matrix modification.')
829 if (v.isdigit() ==
True)
or (v[0] ==
'-' and v[1:].isdigit() ==
True):
Container for data of an array in a param file.
def __str__(self)
String representation of this Array object.
def read(self, file)
Read the elements of this Array from a file.
def getString(self, depth='')
Indented string representation of this Array object.
def __init__(self, label, file, val=None)
Constructor.
def returnData(self)
Return the value of this Array, as a list of element values.
def setValue(self, val)
Set the new value to the Array object.
Container for data of a Composite in a param file.
def __str__(self)
Return an indented string representation of this Composite.
def __getattr__(self, attr)
Return the value of one child of this Composite.
def addChild(self, child)
Add a single child to this Composite.
def __setattr__(self, label, val)
Set a new value for a specified child or attribute.
def read(self, file)
Read and parse a parameter block from a file.
def write(self, filename)
Write the indented param file string for this to a file.
def getString(self, depth='')
Get an indented string representation of this Composite.
def returnData(self)
Return the Composite object itself.
def __init__(self, file=None, label=None)
Constructor.
def getChildren(self)
Get the dictionary of child items.
A Matrix represents a matrix-valued parameter in parameter file.
def __init__(self, label, file)
Constructor.
def read(self, file)
Read the a parameter block from a file.
def __str__(self)
String representation of this Matrix object.
def setValue(self, val)
Set a new value for this Matrix.
def getString(self, depth='')
Indented string representation of this Matrix object.
def returnData(self)
Return the value of this Matrix as a list of lists.
A Parameter represents a single parameter in a parameter file.
def returnData(self)
Return the stored value.
def getString(self, depth='')
Indented string for the Parameter object.
def __init__(self, label, val)
Constructor.
def __str__(self)
Unindented string representation of this Parameter object.
def setValue(self, val)
Set the new value to the Parameter object.
def getValue(v)
Distinguish the type of a value from its string representation.