tparams.py - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
 (HTM) git clone git://src.adamsgaard.dk/pism
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       tparams.py (2624B)
       ---
            1 ############################################################################
            2 #
            3 #  This file is a part of siple.
            4 #
            5 #  Copyright 2010, 2014 David Maxwell
            6 #
            7 #  siple is free software: you can redistribute it and/or modify
            8 #  it under the terms of the GNU General Public License as published by
            9 #  the Free Software Foundation, either version 2 of the License, or
           10 #  (at your option) any later version.
           11 # 
           12 ############################################################################
           13 
           14 # The following was copied from matplotlib, which copied a python recipe.
           15 class Bunch:
           16     """
           17     Often we want to just collect a bunch of stuff together, naming each
           18     item of the bunch; a dictionary's OK for that, but a small do- nothing
           19     class is even handier, and prettier to use.  Whenever you want to
           20     group a few variables:
           21 
           22       >>> point = Bunch(datum=2, squared=4, coord=12)
           23       >>> point.datum
           24       By: Alex Martelli
           25       From: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308
           26     """
           27     def __init__(self, **kwds):
           28         self.__dict__.update(kwds)
           29 
           30     def has_key(self,k):
           31       return k in self.__dict__
           32       
           33     def update(self,**kwds):
           34       self.__dict__.update(**kwds)
           35 
           36     def __repr__(self):
           37         keys = list(self.__dict__.keys())
           38         return 'Bunch(%s)'%', '.join(['%s=%s'%(k,self.__dict__[k]) for k in keys])
           39 
           40 class Parameters(Bunch):
           41   """This is a quick and dirty replacement for the dolfin.Parameters class
           42   that was previously used in 'sipl'."""
           43   def __init__(self, *args, **kwds):
           44       Bunch.__init__(self,**kwds)
           45       if len(args) > 0:
           46         self.name = args[0]
           47 
           48   def __repr__(self):
           49       keys = list(self.__dict__.keys())
           50       return 'Parameters(%s): [%s]'% (self.name,','.join(['%s=%s'%(k,self.__dict__[k]) for k in keys]))
           51 
           52   def add(self,*args):
           53     if len(args)==0 or len(args)>2:
           54       'add expects a single Parameters argument or a key/value pair'
           55     if (len(args)==1):
           56       otherParams = args[0]
           57       if (isinstance(otherParams,Parameters)):
           58         self.__dict__[otherParams.name] = otherParams.copy()
           59       else:
           60         raise ValueError('add with one argument expects a Parameters, found %s' % otherParams)
           61     else:
           62       name = args[0]
           63       value = args[1]
           64       self.__dict__[name] = value
           65         
           66   def rename(self,name):
           67     self.name = name
           68 
           69   def update(self,other):
           70     if isinstance(other,dict):
           71       self.__dict__.update(other)
           72     elif isinstance(other,Parameters):
           73       self.__dict__.update(other.__dict__)
           74     else:
           75       raise ValueError()
           76 
           77   def copy(self):
           78     p=Parameters(self.name)
           79     p.__dict__.update(self.__dict__)
           80     return p