[13019] | 1 | #Module import
|
---|
[21759] | 2 | import numpy as np
|
---|
[13019] | 3 | from math import isnan
|
---|
[17480] | 4 | import MatlabFuncs as m
|
---|
[13019] | 5 |
|
---|
| 6 | def fielddisplay(md,name,comment):
|
---|
[13125] | 7 | """
|
---|
| 8 | FIELDDISPLAY - display model field
|
---|
[13019] | 9 |
|
---|
[13125] | 10 | Usage:
|
---|
| 11 | fielddisplay(md,name,comment)
|
---|
| 12 | """
|
---|
| 13 |
|
---|
[13019] | 14 | #get field
|
---|
| 15 | field=getattr(md,name)
|
---|
| 16 |
|
---|
| 17 | #disp corresponding line as a function of field type (offset set as 9 spaces)
|
---|
| 18 | return parsedisplay(" ",name,field,comment);
|
---|
| 19 |
|
---|
[13125] | 20 | def parsedisplay(offset,name,field,comment): # {{{
|
---|
[13019] | 21 |
|
---|
| 22 | #string
|
---|
[13098] | 23 | if isinstance(field,(str,unicode)):
|
---|
[13125] | 24 |
|
---|
[13019] | 25 | if len(field)>30:
|
---|
| 26 | string=displayunit(offset,name,"not displayed",comment)
|
---|
| 27 | else:
|
---|
[13125] | 28 | string=displayunit(offset,name,"'%s'" % field,comment)
|
---|
[13019] | 29 |
|
---|
| 30 | #numeric
|
---|
[13125] | 31 | elif isinstance(field,(int,long,float)):
|
---|
| 32 | string=displayunit(offset,name,str(field),comment)
|
---|
[13019] | 33 |
|
---|
[13125] | 34 | #matrix
|
---|
[21759] | 35 | elif isinstance(field,np.ndarray):
|
---|
[13125] | 36 | string=displayunit(offset,name,str(field.shape),comment)
|
---|
[13019] | 37 |
|
---|
| 38 | #logical
|
---|
| 39 | elif isinstance(field,bool):
|
---|
| 40 | if field:
|
---|
[13125] | 41 | string=displayunit(offset,name,"True",comment)
|
---|
[13019] | 42 | else:
|
---|
[13125] | 43 | string=displayunit(offset,name,"False",comment)
|
---|
[13019] | 44 |
|
---|
[13125] | 45 | #dictionary
|
---|
[13019] | 46 | elif isinstance(field,dict):
|
---|
[13125] | 47 | string=dict_display(offset,name,field,comment)
|
---|
[13019] | 48 |
|
---|
[13125] | 49 | #list or tuple
|
---|
| 50 | elif isinstance(field,(list,tuple)):
|
---|
| 51 | string=list_display(offset,name,field,comment)
|
---|
[13019] | 52 |
|
---|
[13965] | 53 | #None
|
---|
| 54 | elif field is None:
|
---|
| 55 | string=displayunit(offset,name,"None",comment)
|
---|
| 56 |
|
---|
[13019] | 57 | else:
|
---|
| 58 | string=displayunit(offset,name,"not displayed",comment)
|
---|
| 59 |
|
---|
| 60 | return string
|
---|
[13125] | 61 | # }}}
|
---|
[13019] | 62 |
|
---|
[13125] | 63 | def dict_display(offset,name,field,comment): # {{{
|
---|
[13019] | 64 |
|
---|
[13125] | 65 | if field:
|
---|
| 66 | string =displayunit(offset,name,'{dictionary}',comment)+'\n'
|
---|
| 67 | offset+=' '
|
---|
| 68 |
|
---|
| 69 | for structure_field,sfield in field.iteritems():
|
---|
| 70 | string+=parsedisplay(offset,str(structure_field),sfield,'')+'\n'
|
---|
| 71 |
|
---|
| 72 | if string and string[-1]=='\n':
|
---|
| 73 | string=string[:-1]
|
---|
| 74 |
|
---|
| 75 | else:
|
---|
| 76 | string=displayunit(offset,name,'N/A',comment)
|
---|
| 77 |
|
---|
| 78 | return string
|
---|
| 79 | # }}}
|
---|
| 80 |
|
---|
| 81 | def list_display(offset,name,field,comment): # {{{
|
---|
| 82 |
|
---|
| 83 | #initialization
|
---|
[13150] | 84 | if isinstance(field,list):
|
---|
| 85 | sbeg='['
|
---|
| 86 | send=']'
|
---|
| 87 | elif isinstance(field,tuple):
|
---|
[13125] | 88 | sbeg='('
|
---|
| 89 | send=')'
|
---|
| 90 | string=sbeg
|
---|
| 91 |
|
---|
| 92 | #go through the cell and fill string
|
---|
| 93 | if len(field)<5:
|
---|
| 94 | for fieldi in field:
|
---|
| 95 | if isinstance(fieldi,(str,unicode)):
|
---|
| 96 | string+="'%s'," % fieldi
|
---|
| 97 | elif isinstance(fieldi,(bool,int,long,float)):
|
---|
| 98 | string+="%s," % str(fieldi)
|
---|
| 99 | else:
|
---|
| 100 | string=sbeg
|
---|
| 101 | break
|
---|
| 102 |
|
---|
[17480] | 103 | if m.strcmp(string,sbeg):
|
---|
[13125] | 104 | string="%s%dx1%s" % (sbeg,len(field),send)
|
---|
| 105 | else:
|
---|
| 106 | string=string[:-1]+send
|
---|
| 107 |
|
---|
| 108 | #call displayunit
|
---|
| 109 | return displayunit(offset,name,string,comment)
|
---|
| 110 | # }}}
|
---|
| 111 |
|
---|
| 112 | def displayunit(offset,name,characterization,comment): # {{{
|
---|
| 113 |
|
---|
[13019] | 114 | #take care of name
|
---|
| 115 | if len(name)>23:
|
---|
[13125] | 116 | name="%s..." % name[:20]
|
---|
[13019] | 117 |
|
---|
| 118 | #take care of characterization
|
---|
[17480] | 119 | if m.strcmp(characterization,"''") or m.strcmp(characterization,'""') or m.strcmpi(characterization,'nan'):
|
---|
[13019] | 120 | characterization="N/A"
|
---|
| 121 |
|
---|
| 122 | if len(characterization)>15:
|
---|
[13125] | 123 | characterization="%s..." % characterization[:12]
|
---|
[13019] | 124 |
|
---|
| 125 | #print
|
---|
| 126 | if not comment:
|
---|
| 127 | string="%s%-23s: %-15s" % (offset,name,characterization)
|
---|
| 128 | else:
|
---|
[13125] | 129 | if isinstance(comment,(str,unicode)):
|
---|
[13019] | 130 | string="%s%-23s: %-15s -- %s" % (offset,name,characterization,comment)
|
---|
| 131 | elif isinstance(comment,list):
|
---|
| 132 | string="%s%-23s: %-15s -- %s" % (offset,name,characterization,comment[0])
|
---|
[13125] | 133 | for commenti in comment:
|
---|
[13423] | 134 | string+="\n%s%-23s %-15s %s" % (offset,'','',commenti)
|
---|
[13019] | 135 | else:
|
---|
[13125] | 136 | raise RuntimeError("fielddisplay error message: format for comment not supported yet")
|
---|
[13019] | 137 |
|
---|
| 138 | return string
|
---|
[13125] | 139 | # }}}
|
---|
| 140 |
|
---|