~reducedmodelling/fluidity/ROM_Non-intrusive-ann

« back to all changes in this revision

Viewing changes to libspud/diamond/build/lib.linux-x86_64-2.7/diamond/plist.py

  • Committer: fangf at ac
  • Date: 2012-11-06 12:21:31 UTC
  • mto: This revision was merged to the branch mainline in revision 3989.
  • Revision ID: fangf@imperial.ac.uk-20121106122131-u2zvt7fxc1r3zeou
updated

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
#    This file is part of Diamond.
 
4
#
 
5
#    Diamond is free software: you can redistribute it and/or modify
 
6
#    it under the terms of the GNU General Public License as published by
 
7
#    the Free Software Foundation, either version 3 of the License, or
 
8
#    (at your option) any later version.
 
9
#
 
10
#    Diamond is distributed in the hope that it will be useful,
 
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#    GNU General Public License for more details.
 
14
#
 
15
#    You should have received a copy of the GNU General Public License
 
16
#    along with Diamond.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
class List:
 
19
  def __init__(self, datatype, cardinality=''):
 
20
    self.datatype = datatype
 
21
    self.cardinality = cardinality
 
22
 
 
23
  # The input to call is a string containing a list of elements seperated by "," or " ". It
 
24
  # returns a string, containing the elements in val seperated by " ".
 
25
  def __call__(self, val):
 
26
    val = val.strip()
 
27
    if "," in val:
 
28
      x = val.split(",")
 
29
    else:
 
30
      x = val.split(" ")
 
31
 
 
32
    # Perform some checks on the list cardinality, for oneOrMore elements ('+')
 
33
    # and compulsory elements ('').
 
34
    if self.cardinality == '+':
 
35
      assert len(x) > 0
 
36
    elif self.cardinality == '':
 
37
      assert len(x) == 1
 
38
 
 
39
    # Check the list cardinality (as an integer) matches up with the number of elements
 
40
    # in the seperated val string.
 
41
    try:
 
42
      assert len(x) == int(self.cardinality)
 
43
    except ValueError:
 
44
      pass # The int conversion may fail (if cardinality is '+' or ''), so just ignore it.
 
45
 
 
46
    # Make sure each element can be converted to type 'self.datatype'. An exception will be
 
47
    # thrown if this is not possible.
 
48
    for y in x:
 
49
      z = self.datatype(y)
 
50
 
 
51
    # Return a string of the elements in val, seperated by " ".
 
52
    return " ".join(x)
 
53
 
 
54
  def __str__(self):
 
55
    return "list of " + str(self.datatype) + " of cardinality: " + self.cardinality
 
56
 
 
57
  def __repr__(self):
 
58
    return "list of " + str(self.datatype) + " of cardinality: " + self.cardinality