Changeset 144
- Timestamp:
- 08/31/2009 06:38:30 PM (13 years ago)
- Location:
- SHX/trunk/src/SeismicHandler
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
SHX/trunk/src/SeismicHandler/core/error.py
r143 r144 85 85 def __str__(self): 86 86 return """internal variables are unchangeable!""" 87 88 class VarsSymbolNotFoundError(Error): 89 def __init__(self, name): 90 Error.__init__(self) 91 self.name = name 92 93 def __str__(self): 94 return """symbol '%s' not found!""" % self.name 95 96 class VarsSymbolGlobalLocalError(Error): 97 def __init__(self, name): 98 Error.__init__(self) 99 self.name = name 100 101 def __str__(self): 102 return """symbol '%s' already defined in local scope!""" % self.name -
SHX/trunk/src/SeismicHandler/core/modules/Trace.py
r142 r144 61 61 def __len__(self): 62 62 """return length of trace in milliseconds""" 63 63 64 64 return self.delta * self.length * 1000 65 65 … … 191 191 192 192 def __getitem__(self, number): 193 return self.tracedata[self.traces[number-1]] 193 if number >= len(self.traces): 194 raise IndexError("trace list end reached") 195 196 return self.tracedata[self.traces[number]] 194 197 195 198 def update(self, event=None): … … 258 261 return len(self.traces) 259 262 263 def get(self, number): 264 """return trace by index using natural count (start from one) 265 """ 266 return self.traces[number-1] 267 260 268 if __name__ == "__main__": 261 269 t = Traces() -
SHX/trunk/src/SeismicHandler/core/modules/Variables.py
r143 r144 26 26 from SeismicHandler.core.shlib import shlib 27 27 from SeismicHandler.core.log import logging 28 from SeismicHandler.core.error import VarsSystemReadOnlyError 28 from SeismicHandler.core.error import VarsSystemReadOnlyError, VarsSymbolNotFoundError, VarsSymbolGlobalLocalError 29 29 import ctypes as C 30 30 31 31 class accessSystem(object): 32 _logger = None 33 _sh = None 34 32 35 def __init__(self): 33 self._logger = logging.getLogger("modules.variables") 34 self._sh = shlib().sh 35 36 if not self._logger: 37 self._logger = logging.getLogger("modules.variables") 38 39 if not self._sh: 40 self._sh = shlib().sh 41 36 42 def __getattr__(self, name): 37 43 if name in ["_logger", "_sh"]: 38 44 return self.__dict__[name] 39 45 40 46 res = C.create_string_buffer(1024) 41 47 status = C.c_int() 42 48 49 self._logger.debug("system variable access: %s" % name.upper()) 43 50 self._sh.tr_intern(name.upper(), "", len(res), C.byref(res), C.byref(status)) 44 51 45 52 if not status: 46 53 return res.value 47 54 else: 48 55 return False 49 56 50 57 def __setattr__(self, name, value): 51 58 if name in ["_logger", "_sh"]: … … 55 62 56 63 class accessSymbols(object): 64 _logger = None 65 _sh = None 66 _types = {} 67 57 68 def __init__(self): 58 pass 59 69 if not self._logger: 70 self._logger = logging.getLogger("modules.variables") 71 72 if not self._sh: 73 self._sh = shlib().sh 74 60 75 def __getattr__(self, name): 61 pass 62 63 def __setattr__(self, name, value): 64 pass 76 if name in ["_logger", "_sh", "_types"]: 77 return self.__dict__[name] 65 78 66 SystemVars = accessSystem() 67 SymbolVars = accessSymbols() 79 name = name.upper() 80 81 inlocal = self._sh.ss_exist(0, name) 82 inglobal = self._sh.ss_exist(1, name) 83 84 # not found at all 85 if not inlocal | inglobal: 86 raise VarsSymbolNotFoundError(name) 87 88 symbolset = inglobal and 1 or 0 89 90 status = C.c_int(0) 91 value = C.create_string_buffer(512) 92 93 self._sh.ss_getval(symbolset, name, len(value), C.byref(value), C.byref(status)) 94 95 if not status: 96 try: 97 return self._types[name](value.value) 98 except KeyError: 99 self._types[name] = str 100 return value.value 101 else: 102 self._logger.warning("error accessing symbol: %s" % name.upper()) 103 104 def __setattr__(self, name, value, globalscope = 0): 105 if name in ["_logger", "_sh", "_types"]: 106 self.__dict__[name] = value 107 return 108 109 name = name.upper() 110 status = C.c_int(0) 111 112 inlocal = self._sh.ss_exist(0, name) 113 inglobal = self._sh.ss_exist(1, name) 114 115 if not inlocal | inglobal: 116 # symbol does not exist at all 117 if value != None: 118 # setting value to None will delete symbol 119 # since is does not exist, we just have to handle the opposite 120 121 # save type for later reconversion (SH saves everything as string) 122 self._types[name] = type(value) 123 124 self._logger.debug("creating %s symbol: %s, value '%s', %s" % (globalscope and "global" or "local", name, str(value), type(value))) 125 self._sh.ss_define(globalscope, name, str(value), C.byref(status)) 126 127 else: 128 # symbol exists already 129 # use detected scope 130 globalscope = inglobal and 1 or 0 131 132 if value == None: 133 # setting value to None deletes symbol by definition 134 self._logger.debug("deleting %s symbol: %s" % (globalscope and "global" or "local", name)) 135 self._sh.ss_delete(globalscope, name, C.byref(status)) 136 del self._types[name] 137 else: 138 self._logger.debug("changing %s symbol value: %s to '%s', %s" % (globalscope and "global" or "local", name, str(value), type(value))) 139 self._sh.ss_change(globalscope, name, str(value), C.byref(status)) 140 self._types[name]= type(value) 141 142 if status: 143 self._logger.warning("error accessing symbol: %s" % name) 144 145 def createGlobal(self, name, value = ""): 146 # check if symbol was defined in other scope 147 name = name.upper() 148 149 if name in self._types: 150 raise VarsSymbolGlobalLocalError(name) 151 152 self.__setattr__(name, value, 1) 153 154 System = accessSystem() 155 Symbol = accessSymbols() 68 156 69 157 if __name__ == "__main__": 70 print SystemVars.pi 158 # print System.pi 159 Symbol.lala = 10 160 Symbol.createGlobal("lala", 1) 161 # print Symbol.lala 162 # Symbol.lala = None 163 # Symbol.lala = 1 164 # print Symbol.lala 165 # Symbol.createGlobal("a", 2.1) 166 # print Symbol.a 167 # Symbol.lulu = "test" 168 # Symbol.i = 1j 169 170 # print Symbol._types 171 172 # print type(Symbol.i) -
SHX/trunk/src/SeismicHandler/startup.py
r137 r144 35 35 # finally import modules and commands for instant usage 36 36 from SeismicHandler.core.log import logging 37 from SeismicHandler.core.commands import Run, Quit 37 from SeismicHandler.core.commands import * 38 from SeismicHandler.core.modules.Variables import System, Symbol 38 39 39 40 # just init adopted cmd line interface -
SHX/trunk/src/SeismicHandler/tests/test_commands.py
r143 r144 3 3 from SeismicHandler.core.modules.Events import Event, EventManager 4 4 from SeismicHandler.core.modules.Trace import Traces 5 from SeismicHandler.core.modules.Variables import Symbol, System 5 6 from SeismicHandler.core.commands import Run 6 7 … … 15 16 16 17 def testAm(self): 17 # create synth 100 seconds length18 # maximum amplitude 113 at position 1518 # Create synthetic trace of 100 seconds length. 19 # Maximum amplitude 113 at position 15. 19 20 Run("create sharp 0.05 100 113. 15. .1 .5") 21 22 # create symbols (we want to set the result type from python 23 Symbol.min = 0. 24 Symbol.minpos = 0. 25 Symbol.max = 0. 26 Symbol.maxpos = 0. 27 Run("am 1 0. 100. &min &max &minpos &maxpos") 28 29 # test values taken from SH command line version 30 self.assertAlmostEqual(Symbol.min, -60.2811) 31 self.assertAlmostEqual(Symbol.minpos, 24.05) 32 self.assertAlmostEqual(Symbol.max, 113.00) 33 # please note that the maximum position differs from input (15) 34 self.assertAlmostEqual(Symbol.maxpos, 17.75) 35 36 def testAppend(self): 37 pass 20 38 21 39 def testSum(self): -
SHX/trunk/src/SeismicHandler/tests/test_shlib.py
r139 r144 4 4 from SeismicHandler.config.options import config 5 5 from SeismicHandler.core.commands import Run 6 from SeismicHandler.core.modules.Variables import System 6 7 7 8 import unittest … … 23 24 24 25 Run("cresharp") 25 # xxx should check number of traces 26 # Run("echo $tottrcs") 26 self.assertEqual(int(System.dsptrcs), 1) 27 27 28 28 def suite(): -
SHX/trunk/src/SeismicHandler/tests/test_traces.py
r143 r144 13 13 self.traces = Traces() 14 14 # count starts from one! 15 self.trace = self.traces[ 1]15 self.trace = self.traces[0] 16 16 17 17 def tearDown(self):
Note: See TracChangeset
for help on using the changeset viewer.