Changeset 24254 for issm/trunk-jpl/src/m/qmu/helpers.py
- Timestamp:
- 10/18/19 00:13:27 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/qmu/helpers.py
r24213 r24254 24 24 Example uses: 25 25 26 x = Lstruct(1, 2, 3, 4) - 26 x = Lstruct(1, 2, 3, 4) -> [1, 2, 3, 4] 27 27 x.a = 'hello' 28 len(x) - 28 len(x) -> 4 29 29 x.append(5) 30 len(x) - 31 x[2] - 32 x.a - 33 print x - 34 x.__dict__ - > {'a':'hello'}30 len(x) -> 5 31 x[2] -> 3 32 x.a -> 'hello' 33 print x -> [1, 2, 3, 4, 5] 34 x.__dict__ -> {'a':'hello'} 35 35 x.b = [6, 7, 8, 9] 36 x.b[-1] - 37 len(x.b) - 36 x.b[-1] -> 9 37 len(x.b) -> 4 38 38 39 39 Other valid constructors: 40 x = Lstruct(1, 2, 3, a = 'hello') - > x.a - > 'hello', x -> [1, 2, 3]40 x = Lstruct(1, 2, 3, a = 'hello') -> x.a -> 'hello', x -> [1, 2, 3] 41 41 x = Lstruct(1, 2, 3)(a = 'hello') 42 42 x = Lstruct([1, 2, 3], x = 'hello') 43 43 x = Lstruct((1, 2, 3), a = 'hello') 44 44 45 Credit: https: / / github.com / Vectorized / Python -Attribute-List45 Credit: https://github.com/Vectorized/Python-Attribute-List 46 46 ''' 47 47 … … 63 63 class OrderedStruct(object): 64 64 ''' 65 A form of dictionary -like structure that maintains the65 A form of dictionary-like structure that maintains the 66 66 ordering in which its fields / attributes and their 67 67 corresponding values were added. … … 70 70 can be used as an "ordered struct / class" giving 71 71 it much more flexibility in practice. It is 72 also easier to work with fixed valued keys in -code.72 also easier to work with fixed valued keys in-code. 73 73 74 74 Eg: … … 79 79 x.y = 5 80 80 OR 81 x['y'] = 5 # supports OrderedDict -style usage82 83 Supports: len(x), str(x), for -loop iteration.81 x['y'] = 5 # supports OrderedDict-style usage 82 83 Supports: len(x), str(x), for-loop iteration. 84 84 Has methods: x.keys(), x.values(), x.items(), x.iterkeys() 85 85 … … 94 94 # in the same order as the inputs 95 95 96 x.keys() - 97 x.values() - 98 x.items() - 99 x.__dict__ - 100 vars(x) - 101 102 x.y - 103 x['y'] - 104 x.z - 105 x['z'] - 96 x.keys() -> ['y', 'z'] 97 x.values() -> [5, 6] 98 x.items() -> [('y', 6), ('z', 6)] 99 x.__dict__ -> [('y', 6), ('z', 6)] 100 vars(x) -> [('y', 6), ('z', 6)] 101 102 x.y -> 5 103 x['y'] -> 5 104 x.z -> 6 105 x['z'] -> 6 106 106 107 107 for i in x: # same as x.items() 108 108 print i 109 - 109 -> 110 110 ('x', 5) 111 111 ('y', 6) … … 197 197 # same thing but call deepcopy recursively 198 198 # technically not how it should be done, 199 # (see https: / / docs.python.org / 2 / library /copy.html #copy.deepcopy )199 # (see https://docs.python.org/2/library/copy.html #copy.deepcopy ) 200 200 # but will generally work in this case 201 201 newInstance = type(self)() … … 227 227 def isempty(x): 228 228 ''' 229 returns true if object is + - 230 array /matrix composed only of such components (includes mixtures of "empty" types)'''229 returns true if object is + -infinity, NaN, None, '', has length 0, or is an 230 array/matrix composed only of such components (includes mixtures of "empty" types)''' 231 231 232 232 if type(x) in [list, np.ndarray, tuple]: … … 247 247 if x is None: 248 248 return True 249 if type(x) == str and x.lower() in ['', 'nan', 'none', 'inf', 'infinity', ' - inf', ' -infinity']:249 if type(x) == str and x.lower() in ['', 'nan', 'none', 'inf', 'infinity', '-inf', '-infinity']: 250 250 return True 251 251 … … 280 280 def fileparts(x): 281 281 ''' 282 given: "path / path / ... /file_name.ext"282 given: "path/path/.../file_name.ext" 283 283 returns: [path, file_name, ext] (list of strings)''' 284 284 try: … … 300 300 301 301 fullfile(path, path, ... , file_name + ext) 302 returns: "path / path / ... /file_name.ext"303 304 with all arguments as strings with no " /"s302 returns: "path/path/.../file_name.ext" 303 304 with all arguments as strings with no "/"s 305 305 306 306 regarding extensions and the '.': … … 331 331 def empty_nd_list(shape, filler=0., as_numpy_ndarray=False): 332 332 ''' 333 returns a python list of the size /shape given (shape must be int or tuple)333 returns a python list of the size/shape given (shape must be int or tuple) 334 334 the list will be filled with the optional second argument 335 335 … … 338 338 as_numpy_ndarray will return the result as a numpy.ndarray and is False by default 339 339 340 Note: the filler must be either None / np.nan / float('NaN'), float /double, or int341 other numpy and float values such as + /- np.inf will also work340 Note: the filler must be either None/np.nan/float('NaN'), float/double, or int 341 other numpy and float values such as +/- np.inf will also work 342 342 343 343 use:
Note:
See TracChangeset
for help on using the changeset viewer.