Changeset 25455 for issm/trunk-jpl/src/m/qmu/helpers.py
- Timestamp:
- 08/25/20 00:32:13 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/qmu/helpers.py
r25065 r25455 6 6 7 7 class struct(object): 8 '''An empty struct that can be assigned arbitrary attributes'''8 """An empty struct that can be assigned arbitrary attributes""" 9 9 pass 10 10 11 11 12 12 class Lstruct(list): 13 '''13 """ 14 14 An empty struct that can be assigned arbitrary attributes but can also be 15 15 accesed as a list. Eg. x.y = 'hello', x[:] = ['w', 'o', 'r', 'l', 'd'] … … 46 46 Sources: 47 47 -https://github.com/Vectorized/Python-Attribute-List 48 '''48 """ 49 49 50 50 def __new__(self, *args, **kwargs): … … 64 64 65 65 class OrderedStruct(object): 66 '''66 """ 67 67 A form of dictionary-like structure that maintains the ordering in which 68 68 its fields/attributes and their corresponding values were added. … … 113 113 Note: to access internal fields use dir(x) (input fields will be included, 114 114 but are not technically internals) 115 '''115 """ 116 116 117 117 def __init__(self, *args): 118 '''118 """ 119 119 Provided either nothing or a series of strings, construct a structure 120 120 that will, when accessed as a list, return its fields in the same order 121 121 in which they were provided 122 '''122 """ 123 123 124 124 # keys and values … … 187 187 188 188 def __copy__(self): 189 '''189 """ 190 190 shallow copy, hard copies of trivial attributes, 191 191 references to structures like lists/OrderedDicts 192 192 unless redefined as an entirely different structure 193 '''193 """ 194 194 newInstance = type(self)() 195 195 for k, v in list(self.items()): … … 198 198 199 199 def __deepcopy__(self, memo=None): 200 '''200 """ 201 201 hard copy of all attributes 202 202 same thing but call deepcopy recursively … … 204 204 (see https://docs.python.org/2/library/copy.html #copy.deepcopy ) 205 205 but will generally work in this case 206 '''206 """ 207 207 newInstance = type(self)() 208 208 for k, v in list(self.items()): … … 232 232 233 233 def isempty(x): 234 '''234 """ 235 235 returns true if object is +/-infinity, NaN, None, '', has length 0, or is 236 236 an array/matrix composed only of such components (includes mixtures of 237 237 "empty" types) 238 '''238 """ 239 239 240 240 if type(x) in [list, np.ndarray, tuple]: … … 270 270 271 271 def fieldnames(x, ignore_internals=True): 272 '''272 """ 273 273 returns a list of fields of x 274 274 ignore_internals ignores all fieldnames starting with '_' and is True by 275 275 default 276 '''276 """ 277 277 result = list(vars(x).keys()) 278 278 … … 284 284 285 285 def isfield(x, y, ignore_internals=True): 286 '''286 """ 287 287 is y is a field of x? 288 288 ignore_internals ignores all fieldnames starting with '_' and is True by 289 289 default 290 '''290 """ 291 291 return str(y) in fieldnames(x, ignore_internals) 292 292 293 293 294 294 def fileparts(x): 295 '''295 """ 296 296 given: "path/path/.../file_name.ext" 297 297 returns: [path, file_name, ext] (list of strings) 298 '''298 """ 299 299 try: 300 300 a = x[:x.rindex('/')] #path … … 311 311 312 312 def fullfile(*args): 313 '''313 """ 314 314 usage: 315 315 fullfile(path, path, ... , file_name + ext) … … 322 322 as final arguments ('file.doc') or ('file' + '.doc') will work 323 323 ('final', '.doc'), and the like, will not (you'd get 'final/.doc') 324 '''324 """ 325 325 result = str(args[0]) 326 326 for i in range(len(args[1:])): … … 334 334 335 335 def findline(fidi, s): 336 '''336 """ 337 337 returns full first line containing s (as a string), or None 338 338 … … 340 340 use str(findline(f, s)).strip() to remove these, str() in case result is 341 341 None 342 '''342 """ 343 343 for line in fidi: 344 344 if s in line: … … 348 348 349 349 def empty_nd_list(shape, filler=0., as_numpy_ndarray=False): 350 '''350 """ 351 351 returns a python list of the size/shape given (shape must be int or tuple) 352 352 the list will be filled with the optional second argument … … 363 363 empty_nd_list((5, 5), 0.0) # returns a 5x5 matrix of 0.0's 364 364 empty_nd_list(5, None) # returns a 5 long array of NaN 365 '''365 """ 366 366 result = np.empty(shape) 367 367 result.fill(filler)
Note:
See TracChangeset
for help on using the changeset viewer.