-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScopePy_utilities.py
More file actions
975 lines (723 loc) · 23.4 KB
/
ScopePy_utilities.py
File metadata and controls
975 lines (723 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 27 07:14:32 2014
@author: john
"""
#==============================================================================
#%% License
#==============================================================================
"""
Copyright 2015 John Bainbridge
This file is part of ScopePy.
ScopePy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ScopePy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ScopePy. If not, see <http://www.gnu.org/licenses/>.
"""
#==============================================================================
#%% Imports
#==============================================================================
import os
import importlib
import numpy as np
import logging
import platform
#==============================================================================
#%% Logger
#==============================================================================
# create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Add do nothing handler
logger.addHandler(logging.NullHandler())
# create console handler and set level to debug
con = logging.StreamHandler()
con.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('[%(asctime)s:%(name)s:%(levelname)s]: %(message)s')
# add formatter to ch
con.setFormatter(formatter)
# add ch to logger
logger.addHandler(con)
#=========================================================================
#%% Range list <-> string conversion functions
#=========================================================================
def str2RangeList(inString):
""" Converts a string to a list of numbers.
The string contains only numbers,commas and dashes
e.g. "1", "1,2,3", "1,3-5,6-10"
Usage examples:
---------------
>>> str2RangeList("1")
[1]
>>> str2RangeList("1,2,3")
[1, 2, 3]
>>> str2RangeList("1,3-5,8,9,12-15")
[1, 3, 4, 5, 8, 9, 12, 13, 14, 15]
>>> str2RangeList("")
[]
"""
# Check for empty string
if inString == "":
return []
# Check for comma separated list
if "," in inString:
# split the list
strList = inString.split(",")
else:
# Make single element list
strList = [inString]
# Go through each item in strList
# if the item is a number then convert it
# if it's a range then convert to a list of numbers
range_list = []
for item in strList:
# Is it a range? i.e. contains a dash "-"
if "-" in item:
# Split item around the dash
strNums = item.split("-")
if len(strNums) == 2:
# Add range to list
range_list.extend(range(int(strNums[0]),int(strNums[1])+1))
else:
# It's a single number, convert and add to list
range_list.append(int(item))
return range_list
def rangeList2str(listOfNumbers):
"""Converts a list of numbers into a string, with the numbers
separated by commas
Usage examples
---------------
>>> rangeList2str([1,2,3])
'1,2,3'
"""
return ','.join(str(e) for e in listOfNumbers)
def filterList(listOfNumbers,minValue,maxValue):
"""Filter the values in a numeric list and return only
values between two limits
Inputs
--------
listOfNumbers = list e.g. [1,2,3,4]
minValue = lowest allowed value
maxValue = highest allowed value
Output
------
filteredList = list of numbers with outliers removed
Usage examples:
----------------
>>> filterList([1,2,3,4,5,6],2,5)
[2, 3, 4, 5]
>>> filterList([],0,10)
[]
"""
# Check for empty lists
if not listOfNumbers:
return []
filteredList = []
for num in listOfNumbers:
if minValue <= num <= maxValue:
filteredList.append(num)
return filteredList
#=========================================================================
#%% Array conversions
#=========================================================================
def listOfDict2Recarray(listOfDicts):
"""
Convert list of dictionaries into a numpy recarray
This is mainly for reading in .CSV files using DictReader and converting
to numpy arrays
Inputs:
-----------
listOfDicts : list of dictionaries
dictionaries are of the form:
{'xdata':0,'y1data':1,'y2data':3 ....}
Outputs
----------
recarray : numpy recarray
array with columns labelled 'xdata', 'y1data', 'y2data' ...
i.e. accessing columns col = recarray['xdata']
Individual elements recarray['y1data'][3]
"""
# Get column names
# -----------------------
header = list(listOfDicts[0].keys())
# Create output array
# ------------------------
# make a numpy data type with all floating point formats
dtype = np.dtype({'names':header,'formats':[float]*len(header)})
array = np.zeros(len(listOfDicts),dtype)
# Populate the array
# --------------------------
for iRow,row in enumerate(listOfDicts):
for col in row:
array[col][iRow] = row[col]
return array
#=========================================================================
#%% DebugPrintout
#=========================================================================
class DebugPrint():
"""
Debug printout controller.
Allows printing out of comments for certain tags
Example usage
--------------
Create the printout controller
>>> dp = DebugPrint(['all','brief','verbose'])
In the code the following can be used to make a printout
>>> dp.dbg("Function call","The value is wrong","brief")
If more than one tag is required to make a printout, eg in a function
then use a list of tags
Example
>>> dp.dbg("myFunction)","doing something",tag=['brief','myFunction'])
"""
def __init__(self,initialList = []):
# List of tags that are accepted
self.tagList = initialList
# Flags
# -----------
# disable all printing
self.disablePrinting = False
# Print everything
self.printAll = False
def __call__(self,*args,**kwargs):
self.dbg(*args,**kwargs)
def addTag(self,newTag):
"""
Add new tag to the list
"""
self.tagList.append(newTag)
def removeTag(self,tag):
"""
Remove a tag from the list
"""
if tag in self.tagList:
self.tagList.pop(self.tagList.index(tag))
def dbg(self,stage,comment,tag='all',disable=False):
"""
Do a printout of the form:
<stage> : <comment>
If the specified tag is in the list then the comment will be printed,
if not then it won't
Inputs
------
stage: str or any object
First string to be printed, if this is an object then the class
name will be printed
comment : str
second string to be printed
tag : str or list of str
tag or list of tags that are required to force a printout.
If a list of tags are given then all must be in the tagList.
disable : bool
local override, set to True to stop printing
"""
# Check the override
# ----------------------------
if self.disablePrinting or disable:
return
# Check the stage
# ------------------------
# if stage is an object get the class name
if not isinstance(stage,str):
stage = stage.__class__.__name__
# Check if a single tag or a list has been entered
# -------------------------------------------------
if not isinstance(tag,list):
tag = [tag]
# Check if any of specified tags are in the list
# ----------------------------------------------
inList = []
for item in tag:
if item in self.tagList:
inList.append(True)
else:
inList.append(False)
# Printout if ALL tags are in the main list
# -------------------------------------------
if all(inList) or self.printAll:
print("> %s : %s" % (stage,comment))
#=========================================================================
#%% Number formats
#=========================================================================
def formatNumber(number):
"""
Format a number into a string using sensible criteria
Input
--------
number : float
Output
--------
numberText : str
string with number in a format suitable for displaying.
"""
# Break number into exponent and fraction
# ------------------------------------------
# get absolute value
abs_value = abs(number)
exponent = np.floor(np.log10(abs_value))
#fraction = number / np.power(10, exponent)
# Select format to return
# ----------------------------
if abs(exponent) > 3:
return "%+.3g" % number
if abs_value > 100:
return "%+.0f" % round(number,0)
elif abs_value > 10:
return "%+.1f" % round(number,1)
elif abs_value > 1:
return "%+.2f" % round(number,2)
else:
return "%+.3f" % round(number,4)
def getNumberFormat(number):
"""
Return a format string for the magnitude of the number specified
Inputs
---------
number : float
Output
---------
format_st : str
format string for the number specified
"""
# Break number into exponent and fraction
# ------------------------------------------
# get absolute value
abs_value = abs(number)
exponent = np.floor(np.log10(abs_value))
#fraction = number / np.power(10, exponent)
# Select format to return
# ----------------------------
if abs(exponent) > 3:
return "%.3g"
if abs_value > 100:
return "%.0f"
elif abs_value > 10:
return "%.1f"
elif abs_value > 1:
return "%.2f"
else:
return "%.3f"
#=========================================================================
#%% Minidir
#=========================================================================
class minidir:
def __init__(self,startdir = 'minidir'):
self.cdir = '/'+startdir
self.dirs = []
self.sdir = startdir
self.files = {}
@property
def pwd(self):
return self.cdir
def createfile(self,filename,value):
if '/'+self.sdir in filename:
self.files[filename]=value
else:
self.files[self.cdir+'/'+filename]=value
def getfile(self,filename):
if '/'+self.sdir in filename:
return self.files[filename]
else:
return self.files[self.cdir+'/'+filename]
def createdir(self,dirname):
if dirname in self.dirs:
pass
else:
if '/'+self.sdir in dirname:
self._create(dirname)
else:
dirname = self.sdir+'/'+dirname
self._create(dirname)
def _create(self,dirname):
d = dirname.split('/')
dr = []
for i in d:
if i == '':
pass
else:
dr.append(i)
for i in dr:
if i in self.dirs:
pass
else:
self.dirs.append(i)
def cd(self,dirname):
if dirname in self.dirs:
if '/'+self.sdir in dirname:
self.cdir = dirname
else:
self.cdir = self.cdir+'/'+dirname
return True
else:
return False
def find(self,finddir):
rl = []
for i in self.dirs:
if finddir in i:
rl.append(i)
for i in self.files:
if finddir in i:
rl.append(i)
return rl
def home(self):
self.cdir = '/'+self.sdir
def __repr__(self):
return self.cdir
def __getitem__(self,fileordir):
return self.getfile(fileordir)
def __setitem__(self,fileordir,value):
if value == '':
self.createdir(fileordir)
else:
self.createfile(fileordir,value)
#=============================================================================
#%% Importer functions
#=============================================================================
#
# Functions for dynamically importing panels
# Notes:
# For testing which python version:
# import platform
# version = platform.python_version()
def import_module_from_file(full_path_to_module):
"""
Import module given full path/filename
Select how this is done according to version of Python
"""
# Get version of Python and convert to number with one decimal place
version = float(platform.python_version()[:3])
# Select the function to use
if version >= 3.4:
return import_module_from_file_py34(full_path_to_module)
else:
return import_module_from_file_py32(full_path_to_module)
def import_module_from_file_py34(full_path_to_module):
"""
Import a module given the full path/filename of the .py file
Python 3.4
"""
module = None
try:
# Get module name and path from full path
module_dir, module_file = os.path.split(full_path_to_module)
module_name, module_ext = os.path.splitext(module_file)
# Get module "spec" from filename
spec = importlib.util.spec_from_file_location(module_name,full_path_to_module)
module = spec.loader.load_module()
except Exception as ec:
logger.error("import error: message as follows:")
print(ec)
finally:
return module
"""
Module importer for Python 3.2
"""
def import_module_from_file_py32(path):
"""
Import module from a path/filename string
Python 3.2 (Raspberry Pi)
"""
# Only import this if we need it
import imp
module = None
# Get module name and path
# ==========================
module_dir,module_file = os.path.split(path)
module_name,ext = os.path.splitext(module_file)
try:
# Look for module
module_info=imp.find_module(module_name,[module_dir])
# Load module
module = imp.load_module(module_name,*module_info)
except Exception as ec:
print(ec)
finally:
return module
#=========================================================
## Connections
#=========================================================
class FUtilError(Exception): pass
class Connector(object):
'''
Can connect to connectables
'''
def start(self,name="Unknown"):
'''
Setup the connections
'''
self.connections = {}
self.name = name
def _check(self):
try:
a = self.name
return
except AttributeError:
pass
raise FUtilError('Start the connectable!')
def addConnection(self,var,func,*args,**kwargs):
'''
var:
Connectable object
func:
Function used to get data to be put in connactable
*args:
args for func
**kwargs:
kwargs for func
'''
self._check()
self.connections[var]=(func,args,kwargs)
def connect(self):
"""
Updates the connections.
"""
self._check()
k = list(self.connections.items())
for var,fak in k:
#var = i
r = False
func = fak[0]
args = fak[1]
kwargs = fak[2]
try:
var.getconnect(func(*args,**kwargs),self.name)
except AttributeError:
r = True
if r:
raise FUtilError('%s Type is not a connectable.' % str(type(var)))
class Connectable(object):
'''
Can be connected to by connectors
'''
def start(self,connectable):
self.connectable = connectable
self.name = connectable.name
def _check(self):
try:
a = self.name
return
except AttributeError:
pass
raise FUtilError('Start the connectable!')
def getconnect(self,new_data,name):
self._check()
if name == self.name:
self.setData(new_data)
def setData(self,new_data):
pass
def deconnect(self):
self.name = None
def getData(self):
self._check()
c = self.connectable
func = c.connections[self]
fun = func[0]
args = func[1]
kwargs = func[2]
data = fun(*args,**kwargs)
return data
class Connection(Connector,Connectable):
'''
Can be connected to, and connect to other connectables
'''
def start(self,name='Unknown'):
Connector.start(self,name)
Connectable.start(self,self)
class DataStorage(Connection):
'''
Ordered dict
'''
def __init__(self,iterable=None,name='DataStorage'):
self.start('DataStorage')
if iterable:
self.update(iterable)
else:
self._items = []
def __getitem__(self,item):
return self._get(item)
def __setitem__(self,item,value):
if self._get(item):
self.delete(item)
self._set(item,value)
#self.update()
def index(self,index):
return self._items[int(index)]
def getIndexOf(self,item):
for e, i in enumerate(self._items):
if i[0] == item:
return e
def _get(self,item):
for i in self._items:
if i[0] == item:
return i[1]
def checkFor(self,item,default):
a = _get(item)
if a == None:
return default
else:
return item
def _set(self,item,value):
self._items.append([item,value])
self.connect()
def delete(self,item):
index = self.getIndexOf(item)
self._items.pop(index)
self.connect()
def __len__(self):
return len(self._items)
def __str__(self):
return self.__repr__()
def insert(self,index,item,value):
self._items.insert(index,[item,value])
self.connect()
def __repr__(self):
s = '{\n'
for i in self._items:
s += "%s:%s,\n" % (i[0],i[1])
s += '\n}'
return s
def keys(self):
el = []
for i in self._items:
el.append(i[0])
cd = connectedData(el,self)
self.addConnection(cd,self.keys)
return cd
def values(self):
el = []
for i in self._items:
el.append(i[1])
cd = connectedData(el,self)
self.addConnection(cd,self.items)
return cd
def items(self):
el = []
for i in self._items:
el.append((i[0],i[1]))
cd = connectedData(el,self)
self.addConnection(cd,self.both)
return cd
def getAll(self):
return self._items
def fromList(self,l):
for i in l:
r = False
try:
self._set(i[0],i[1])
except TypeError:
r = True
if r:
raise FUtilError('%s is not in any readable format.' % str(l))
def toList(self):
return self._itmes
def update(self,d):
'''
Takes a dict or a list of lists, or list of tuples, or list of any iterables
'''
if type(d) != dict:
self.fromList(d)
return
for i in d:
self._set(i,d[i])
self.connect()
def __iter__(self):
return self.items().__iter__()
def clear(self):
self._items = []
self.connect()
def setData(self,data):
#print('Getting Data!')
self.clear()
self.update(data)
def get(self,item,default=None):
i = self._get(item)
if i == None:
return default
return i
#import pickle
class connectedData(Connectable):
def __init__(self,l,parent):
self.start(parent)
#self.name = connectable.name
self.data = l
self.on = 0
def __iter__(self):
return self
def __repr__(self):
return 'connectedData(%s)' % str(self.data)
def setData(self,new_data):
self.data = list(new_data)
def __next__(self):
try:
rt = self.data[self.on]
except IndexError:
raise StopIteration
self.on += 1
return rt
class itemHolder(Connection):
'''
DataStorage with fixed items
Options:
Names of all 'items' to be accepted
'''
def __init__(self,*options,name='itemHolder'):
self.start(name)
self.options = options
self._data = DataStorage()
for i in options:
self._data[i]=[]
def __getitem__(self,key):
if type(key) == slice:
k = key
key = k.start
l = True
else:
l = False
if key in self.options:
if l:
index = k.stop
else:
index = len(self._data[key])-1
try:
return self._data[key][index]
except IndexError:
ras = True
if ras:
raise FUtilError('Index Out Of Range!')
else:
raise FUtilError('No Key Named %s' % key)
def __setitem__(self,key,value):
if key in self.options:
self._data[key].append(value)
else:
raise FUtilError('No Key Named %s' % key)
def __repr__(self):
sq = 'FUtil.itemHolder:\n'
for i in self.options:
sq += '%s: %s\n' % (i,self._data[i])
return sq
class ConnectedChannel(Connectable):
def __init__(self,connector):
self.start(connector)
@property
def x(self):
return self.getData().x
@property
def y(self):
return self.getData().y
#if __name__ == '__main__':
# ds = DataStorage()
# ds['name'] = 'Finn'
# ds['age'] = 10
#=========================================================================
#%% Execute doctests if run
#=========================================================================
if __name__ == "__main__":
import doctest
doctest.testmod()