forked from ni/nimi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.py
More file actions
1817 lines (1270 loc) · 63.7 KB
/
session.py
File metadata and controls
1817 lines (1270 loc) · 63.7 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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# This file was generated
import array # noqa: F401
# Used by @ivi_synchronized
from functools import wraps
import nifake._attributes as _attributes
import nifake._converters as _converters
import nifake._library_interpreter as _library_interpreter
import nifake.enums as enums
import nifake.errors as errors
import nifake.custom_struct as custom_struct # noqa: F401
import nifake.custom_struct_typedef as custom_struct_typedef # noqa: F401
import nifake.custom_struct_nested_typedef as custom_struct_nested_typedef # noqa: F401
import hightime
import nitclk
# Used for __repr__
import pprint
pp = pprint.PrettyPrinter(indent=4)
class _Acquisition(object):
def __init__(self, session):
self._session = session
self._session._initiate()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self._session.abort()
# From https://stackoverflow.com/questions/5929107/decorators-with-parameters
def ivi_synchronized(f):
@wraps(f)
def aux(*xs, **kws):
session = xs[0] # parameter 0 is 'self' which is the session object
with session.lock():
return f(*xs, **kws)
return aux
class _Lock(object):
def __init__(self, session):
self._session = session
def __enter__(self):
# _lock_session is called from the lock() function, not here
return self
def __exit__(self, exc_type, exc_value, traceback):
self._session.unlock()
class _RepeatedCapabilities(object):
def __init__(self, session, prefix, current_repeated_capability_list):
self._session = session
self._prefix = prefix
# We need at least one element. If we get an empty list, make the one element an empty string
self._current_repeated_capability_list = current_repeated_capability_list if len(current_repeated_capability_list) > 0 else ['']
# Now we know there is at lease one entry, so we look if it is an empty string or not
self._separator = '/' if len(self._current_repeated_capability_list[0]) > 0 else ''
def __getitem__(self, repeated_capability):
'''Set/get properties or call methods with a repeated capability (i.e. channels)'''
rep_caps_list = _converters.convert_repeated_capabilities(repeated_capability, self._prefix)
complete_rep_cap_list = [current_rep_cap + self._separator + rep_cap for current_rep_cap in self._current_repeated_capability_list for rep_cap in rep_caps_list]
return _SessionBase(
repeated_capability_list=complete_rep_cap_list,
all_channels_in_session=self._session._all_channels_in_session,
interpreter=self._session._interpreter,
freeze_it=True
)
# This is a very simple context manager we can use when we need to set/get attributes
# or call functions from _SessionBase that require no channels. It is tied to the specific
# implementation of _SessionBase and how repeated capabilities are handled.
class _NoChannel(object):
def __init__(self, session):
self._session = session
def __enter__(self):
self._repeated_capability_cache = self._session._repeated_capability
self._session._repeated_capability = ''
def __exit__(self, exc_type, exc_value, traceback):
self._session._repeated_capability = self._repeated_capability_cache
class _SessionBase(object):
'''Base class for all NI-FAKE sessions.'''
# This is needed during __init__. Without it, __setattr__ raises an exception
_is_frozen = False
float_enum = _attributes.AttributeEnum(_attributes.AttributeViReal64, enums.FloatEnum, 1000005)
'''Type: enums.FloatEnum
A property with an enum that is also a float
'''
read_write_bool = _attributes.AttributeViBoolean(1000000)
'''Type: bool
A property of type bool with read/write access.
'''
read_write_color = _attributes.AttributeEnum(_attributes.AttributeViInt32, enums.Color, 1000003)
'''Type: enums.Color
A property of type Color with read/write access.
'''
read_write_comma_separated_string = _attributes.AttributeViStringCommaSeparated(1000015)
'''Type: str
A property of type comma separated string with read/write access.
'''
read_write_double = _attributes.AttributeViReal64(1000001)
'''Type: float
A property of type float with read/write access.
'''
read_write_double_with_converter = _attributes.AttributeViReal64TimeDeltaSeconds(1000007)
'''Type: hightime.timedelta, datetime.timedelta, or float in seconds
Property in seconds
'''
read_write_double_with_repeated_capability = _attributes.AttributeViReal64(1000009)
'''Type: float
Tip:
This property can be set/get on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset.
Example: :py:attr:`my_session.channels[ ... ].read_write_double_with_repeated_capability`
To set/get on all channels, you can call the property directly on the :py:class:`nifake.Session`.
Example: :py:attr:`my_session.read_write_double_with_repeated_capability`
'''
read_write_enum_with_converter = _attributes.AttributeEnumWithConverter(_attributes.AttributeEnum(_attributes.AttributeViInt32, enums.EnumWithConverter, 1000011), _converters.convert_from_enum_with_converter_enum, _converters.convert_to_enum_with_converter_enum)
read_write_int64 = _attributes.AttributeViInt64(1000006)
'''Type: int
A property of type 64-bit integer with read/write access.
'''
read_write_integer = _attributes.AttributeViInt32(1000004)
'''Type: int
A property of type integer with read/write access.
'''
read_write_integer_with_converter = _attributes.AttributeViInt32TimeDeltaMilliseconds(1000008)
'''Type: hightime.timedelta, datetime.timedelta, or int in milliseconds
Property in milliseconds
'''
read_write_integer_with_month_converter = _attributes.AttributeViInt32TimeDeltaMonths(1000014)
'''Type: hightime.timedelta, datetime.timedelta, or int in months
Property in months
'''
read_write_string = _attributes.AttributeViString(1000002)
'''Type: str
A property of type string with read/write access.
'''
read_write_string_repeated_capability = _attributes.AttributeViStringRepeatedCapability(1000010)
'''Type: Any repeated capability type, as defined in nimi-python:
- str
- str - Comma delimited list
- str - Range (using '-' or ':')
- int
- Basic sequence types (list, tuple, range, slice) of other supported types
A property with read/write access, that represents a repeated capability
Tip:
This property can be set/get on specific instruments within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container instruments to specify a subset.
Example: :py:attr:`my_session.instruments[ ... ].read_write_string_repeated_capability`
To set/get on all instruments, you can call the property directly on the :py:class:`nifake.Session`.
Example: :py:attr:`my_session.read_write_string_repeated_capability`
'''
sample_count = _attributes.AttributeViInt32(1000012)
sample_interval = _attributes.AttributeViReal64(1000013)
def __init__(self, repeated_capability_list, all_channels_in_session, interpreter, freeze_it=False):
self._repeated_capability_list = repeated_capability_list
self._repeated_capability = ','.join(repeated_capability_list)
self._all_channels_in_session = all_channels_in_session
self._interpreter = interpreter
# Store the parameter list for later printing in __repr__
param_list = []
param_list.append("repeated_capability_list=" + pp.pformat(repeated_capability_list))
param_list.append("interpreter=" + pp.pformat(interpreter))
self._param_list = ', '.join(param_list)
# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '', repeated_capability_list)
self.sites = _RepeatedCapabilities(self, 'site', repeated_capability_list)
self.instruments = _RepeatedCapabilities(self, '', repeated_capability_list)
# Finally, set _is_frozen to True which is used to prevent clients from accidentally adding
# members when trying to set a property with a typo.
self._is_frozen = freeze_it
def __repr__(self):
return '{0}.{1}({2})'.format('nifake', self.__class__.__name__, self._param_list)
def __setattr__(self, key, value):
if self._is_frozen and key not in dir(self):
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, key))
object.__setattr__(self, key, value)
''' These are code-generated '''
@ivi_synchronized
def function_with_repeated_capability_type(self):
r'''function_with_repeated_capability_type
A method with a parameter that specifies repeated_capability_type.
Tip:
This method can be called on specific sites within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container sites to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.sites[ ... ].function_with_repeated_capability_type`
To call the method on all sites, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session.function_with_repeated_capability_type`
'''
self._interpreter.function_with_repeated_capability_type(self._repeated_capability)
@ivi_synchronized
def _get_attribute_vi_boolean(self, attribute_id):
r'''_get_attribute_vi_boolean
Queries the value of a ViBoolean property.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ]._get_attribute_vi_boolean`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session._get_attribute_vi_boolean`
Args:
attribute_id (int): Pass the ID of a property.
Returns:
attribute_value (bool): Returns the value of the property.
'''
attribute_value = self._interpreter.get_attribute_vi_boolean(self._repeated_capability, attribute_id)
return attribute_value
@ivi_synchronized
def _get_attribute_vi_int32(self, attribute_id):
r'''_get_attribute_vi_int32
Queries the value of a ViInt32 property.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ]._get_attribute_vi_int32`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session._get_attribute_vi_int32`
Args:
attribute_id (int): Pass the ID of a property.
Returns:
attribute_value (int): Returns the value of the property.
'''
attribute_value = self._interpreter.get_attribute_vi_int32(self._repeated_capability, attribute_id)
return attribute_value
@ivi_synchronized
def _get_attribute_vi_int64(self, attribute_id):
r'''_get_attribute_vi_int64
Queries the value of a ViInt64 property.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ]._get_attribute_vi_int64`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session._get_attribute_vi_int64`
Args:
attribute_id (int): Pass the ID of a property.
Returns:
attribute_value (int): Returns the value of the property.
'''
attribute_value = self._interpreter.get_attribute_vi_int64(self._repeated_capability, attribute_id)
return attribute_value
@ivi_synchronized
def _get_attribute_vi_real64(self, attribute_id):
r'''_get_attribute_vi_real64
Queries the value of a ViReal property.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ]._get_attribute_vi_real64`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session._get_attribute_vi_real64`
Args:
attribute_id (int): Pass the ID of a property.
Returns:
attribute_value (float): Returns the value of the property.
'''
attribute_value = self._interpreter.get_attribute_vi_real64(self._repeated_capability, attribute_id)
return attribute_value
@ivi_synchronized
def _get_attribute_vi_string(self, attribute_id):
r'''_get_attribute_vi_string
Queries the value of a ViBoolean property.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ]._get_attribute_vi_string`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session._get_attribute_vi_string`
Args:
attribute_id (int): Pass the ID of a property.
Returns:
attribute_value (str): Returns the value of the property.
'''
attribute_value = self._interpreter.get_attribute_vi_string(self._repeated_capability, attribute_id)
return attribute_value
@ivi_synchronized
def _get_channel_names(self, indices):
r'''_get_channel_names
Returns a list of channel names for the given channel indices.
Args:
indices (basic sequence types or str or int): Index list for the channels in the session. Valid values are from zero to the total number of channels in the session minus one. The index string can be one of the following formats:
- A comma-separated list—for example, "0,2,3,1"
- A range using a hyphen—for example, "0-3"
- A range using a colon—for example, "0:3 "
You can combine comma-separated lists and ranges that use a hyphen or colon. Both out-of-order and repeated indices are supported ("2,3,0," "1,2,2,3"). White space characters, including spaces, tabs, feeds, and carriage returns, are allowed between characters. Ranges can be incrementing or decrementing.
Returns:
names (list of str): The channel name(s) at the specified indices.
'''
indices = _converters.convert_repeated_capabilities_without_prefix(indices)
names = self._interpreter.get_channel_names(indices)
return _converters.convert_comma_separated_string_to_list(names)
def lock(self):
'''lock
Obtains a multithread lock on the device session. Before doing so, the
software waits until all other execution threads release their locks
on the device session.
Other threads may have obtained a lock on this session for the
following reasons:
- The application called the lock method.
- A call to NI-FAKE locked the session.
- After a call to the lock method returns
successfully, no other threads can access the device session until
you call the unlock method or exit out of the with block when using
lock context manager.
- Use the lock method and the
unlock method around a sequence of calls to
instrument driver methods if you require that the device retain its
settings through the end of the sequence.
You can safely make nested calls to the lock method
within the same thread. To completely unlock the session, you must
balance each call to the lock method with a call to
the unlock method.
Returns:
lock (context manager): When used in a with statement, nifake.Session.lock acts as
a context manager and unlock will be called when the with block is exited
'''
self._interpreter.lock() # We do not call this in the context manager so that this function can
# act standalone as well and let the client call unlock() explicitly. If they do use the context manager,
# that will handle the unlock for them
return _Lock(self)
@ivi_synchronized
def get_channel_names(self, indices):
'''get_channel_names
Returns a list of channel names for the given channel indices.
Args:
indices (basic sequence types or str or int): Index list for the channels in the session. Valid values are from zero to the total number of channels in the session minus one. The index string can be one of the following formats:
- A comma-separated list—for example, "0,2,3,1"
- A range using a hyphen—for example, "0-3"
- A range using a colon—for example, "0:3 "
You can combine comma-separated lists and ranges that use a hyphen or colon. Both out-of-order and repeated indices are supported ("2,3,0," "1,2,2,3"). White space characters, including spaces, tabs, feeds, and carriage returns, are allowed between characters. Ranges can be incrementing or decrementing.
Returns:
names (list of str): The channel name(s) at the specified indices.
'''
return self._get_channel_names(indices)
@ivi_synchronized
def read_from_channel(self, maximum_time):
r'''read_from_channel
Acquires a single measurement and returns the measured value.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ].read_from_channel`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session.read_from_channel`
Args:
maximum_time (hightime.timedelta): Specifies the **maximum_time** allowed in milliseconds.
Returns:
reading (float): The measured value.
'''
maximum_time = _converters.convert_timedelta_to_milliseconds_int32(maximum_time)
reading = self._interpreter.read_from_channel(self._repeated_capability, maximum_time)
return reading
@ivi_synchronized
def _set_attribute_vi_boolean(self, attribute_id, attribute_value):
r'''_set_attribute_vi_boolean
This method sets the value of a ViBoolean property.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ]._set_attribute_vi_boolean`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session._set_attribute_vi_boolean`
Args:
attribute_id (int): Pass the ID of a property.
attribute_value (bool): Pass the value that you want to set the property to.
'''
self._interpreter.set_attribute_vi_boolean(self._repeated_capability, attribute_id, attribute_value)
@ivi_synchronized
def _set_attribute_vi_int32(self, attribute_id, attribute_value):
r'''_set_attribute_vi_int32
This method sets the value of a ViInt32 property.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ]._set_attribute_vi_int32`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session._set_attribute_vi_int32`
Args:
attribute_id (int): Pass the ID of a property.
attribute_value (int): Pass the value that you want to set the property to.
'''
self._interpreter.set_attribute_vi_int32(self._repeated_capability, attribute_id, attribute_value)
@ivi_synchronized
def _set_attribute_vi_int64(self, attribute_id, attribute_value):
r'''_set_attribute_vi_int64
This method sets the value of a ViInt64 property.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ]._set_attribute_vi_int64`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session._set_attribute_vi_int64`
Args:
attribute_id (int): Pass the ID of a property.
attribute_value (int): Pass the value that you want to set the property to.
'''
self._interpreter.set_attribute_vi_int64(self._repeated_capability, attribute_id, attribute_value)
@ivi_synchronized
def _set_attribute_vi_real64(self, attribute_id, attribute_value):
r'''_set_attribute_vi_real64
This method sets the value of a ViReal64 property.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ]._set_attribute_vi_real64`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session._set_attribute_vi_real64`
Args:
attribute_id (int): Pass the ID of a property.
attribute_value (float): Pass the value that you want to set the property to.
'''
self._interpreter.set_attribute_vi_real64(self._repeated_capability, attribute_id, attribute_value)
@ivi_synchronized
def _set_attribute_vi_string(self, attribute_id, attribute_value):
r'''_set_attribute_vi_string
This method sets the value of a ViString property.
Tip:
This method can be called on specific channels within your :py:class:`nifake.Session` instance.
Use Python index notation on the repeated capabilities container channels to specify a subset,
and then call this method on the result.
Example: :py:meth:`my_session.channels[ ... ]._set_attribute_vi_string`
To call the method on all channels, you can call it directly on the :py:class:`nifake.Session`.
Example: :py:meth:`my_session._set_attribute_vi_string`
Args:
attribute_id (int): Pass the ID of a property.
attribute_value (str): Pass the value that you want to set the property to.
'''
self._interpreter.set_attribute_vi_string(self._repeated_capability, attribute_id, attribute_value)
def unlock(self):
'''unlock
Releases a lock that you acquired on an device session using
lock. Refer to lock for additional
information on session locks.
'''
self._interpreter.unlock()
def _error_message(self, error_code):
r'''_error_message
Takes the errorCode returned by a functiona and returns it as a user-readable string.
Args:
error_code (int): The errorCode returned from the instrument.
Returns:
error_message (str): The error information formatted into a string.
'''
error_message = self._interpreter.error_message(error_code)
return error_message
class Session(_SessionBase):
'''An NI-FAKE session to a fake MI driver whose sole purpose is to test nimi-python code generation'''
def __init__(self, resource_name, options={}, id_query=False, reset_device=False, *, grpc_options=None):
r'''An NI-FAKE session to a fake MI driver whose sole purpose is to test nimi-python code generation
Creates a new IVI instrument driver session.
Args:
resource_name (str): Caution: This is just some string.
Contains the **resource_name** of the device to initialize.
options (dict): Specifies the initial value of certain properties for the session. The
syntax for **options** is a dictionary of properties with an assigned
value. For example:
{ 'simulate': False }
You do not have to specify a value for all the properties. If you do not
specify a value for a property, the default value is used.
Advanced Example:
{ 'simulate': True, 'driver_setup': { 'Model': '<model number>', 'BoardType': '<type>' } }
+-------------------------+---------+
| Property | Default |
+=========================+=========+
| range_check | True |
+-------------------------+---------+
| query_instrument_status | False |
+-------------------------+---------+
| cache | True |
+-------------------------+---------+
| simulate | False |
+-------------------------+---------+
| record_value_coersions | False |
+-------------------------+---------+
| driver_setup | {} |
+-------------------------+---------+
id_query (bool): NI-FAKE is probably not needed.
+----------------+---+------------------+
| True (default) | 1 | Perform ID Query |
+----------------+---+------------------+
| False | 0 | Skip ID Query |
+----------------+---+------------------+
reset_device (bool): Specifies whether to reset
+----------------+---+--------------+
| True (default) | 1 | Reset Device |
+----------------+---+--------------+
| False | 0 | Don't Reset |
+----------------+---+--------------+
grpc_options (nifake.grpc_session_options.GrpcSessionOptions): MeasurementLink gRPC session options
Returns:
session (nifake.Session): A session object representing the device.
'''
if grpc_options:
import nifake._grpc_stub_interpreter as _grpc_stub_interpreter
interpreter = _grpc_stub_interpreter.GrpcStubInterpreter(grpc_options)
else:
interpreter = _library_interpreter.LibraryInterpreter(encoding='windows-1251')
# Initialize the superclass with default values first, populate them later
super(Session, self).__init__(
repeated_capability_list=[],
interpreter=interpreter,
freeze_it=False,
all_channels_in_session=None
)
options = _converters.convert_init_with_options_dictionary(options)
# Call specified init function
# Note that _interpreter default-initializes the session handle in its constructor, so that
# if _init_with_options fails, the error handler can reference it.
# And then here, once _init_with_options succeeds, we call set_session_handle
# with the actual session handle.
self._interpreter.set_session_handle(self._init_with_options(resource_name, options, id_query, reset_device))
# NI-TClk does not work over NI gRPC Device Server
if not grpc_options:
self.tclk = nitclk.SessionReference(self._interpreter.get_session_handle())
# Store the parameter list for later printing in __repr__
param_list = []
param_list.append("resource_name=" + pp.pformat(resource_name))
param_list.append("options=" + pp.pformat(options))
param_list.append("reset_device=" + pp.pformat(reset_device))
self._param_list = ', '.join(param_list)
# Store the list of channels in the Session which is needed by some nimi-python modules.
# Use try/except because not all the modules support channels.
# self.get_channel_names() and self.channel_count can only be called after the session
# handle is set
try:
self._all_channels_in_session = self.get_channel_names(range(self.channel_count))
except AttributeError:
self._all_channels_in_session = None
# Finally, set _is_frozen to True which is used to prevent clients from accidentally adding
# members when trying to set a property with a typo.
self._is_frozen = True
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
if self._interpreter._close_on_exit:
self.close()
def initiate(self):
'''initiate
Initiates a thingie.
Note:
This method will return a Python context manager that will initiate on entering and abort on exit.
'''
return _Acquisition(self)
def close(self):
'''close
Closes the specified session and deallocates resources that it reserved.
Note:
This method is not needed when using the session context manager
'''
try:
self._close()
except errors.DriverError:
self._interpreter.set_session_handle()
raise
self._interpreter.set_session_handle()
''' These are code-generated '''
@ivi_synchronized
def abort(self):
r'''abort
Aborts a previously initiated thingie.
'''
self._interpreter.abort()
@ivi_synchronized
def accept_list_of_durations_in_seconds(self, delays):
r'''accept_list_of_durations_in_seconds
Accepts list of hightime.timedelta or datetime.timedelta or float instances representing time delays.
Args:
delays (hightime.timedelta, datetime.timedelta, or float in seconds): A collection of time delay values.
'''
delays = _converters.convert_timedeltas_to_seconds_real64(delays)
self._interpreter.accept_list_of_durations_in_seconds(delays)
@ivi_synchronized
def bool_array_output_function(self, number_of_elements):
r'''bool_array_output_function
This method returns an array of booleans.
Args:
number_of_elements (int): Number of elements in the array.
Returns:
an_array (list of bool): Contains an array of booleans
'''
an_array = self._interpreter.bool_array_output_function(number_of_elements)
return an_array
@ivi_synchronized
def configure_abc(self):
r'''configure_abc
TBD
'''
self._interpreter.configure_abc()
@ivi_synchronized
def custom_nested_struct_roundtrip(self, nested_custom_type_in):
r'''custom_nested_struct_roundtrip
TBD
Args:
nested_custom_type_in (CustomStructNestedTypedef):
Returns:
nested_custom_type_out (CustomStructNestedTypedef):
'''
nested_custom_type_out = self._interpreter.custom_nested_struct_roundtrip(nested_custom_type_in)
return nested_custom_type_out
@ivi_synchronized
def double_all_the_nums(self, numbers):
r'''double_all_the_nums
Test for buffer with converter
Args:
numbers (list of float): numbers is an array of numbers we want to double.
'''
numbers = _converters.convert_double_each_element(numbers)
self._interpreter.double_all_the_nums(numbers)
@ivi_synchronized
def enum_array_output_function(self, number_of_elements):
r'''enum_array_output_function
This method returns an array of enums, stored as 16 bit integers under the hood.
Args:
number_of_elements (int): Number of elements in the array.
Returns:
an_array (list of enums.Turtle): Contains an array of enums, stored as 16 bit integers under the hood
'''
an_array = self._interpreter.enum_array_output_function(number_of_elements)
return an_array
@ivi_synchronized
def enum_input_function_with_defaults(self, a_turtle=enums.Turtle.LEONARDO):
r'''enum_input_function_with_defaults
This method takes one parameter other than the session, which happens to be an enum and has a default value.
Args:
a_turtle (enums.Turtle): Indicates a ninja turtle
+---+---------------+
| 0 | Leonardo |
+---+---------------+
| 1 | Donatello |
+---+---------------+
| 2 | Raphael |
+---+---------------+
| 3 | Mich elangelo |
+---+---------------+
'''
if type(a_turtle) is not enums.Turtle:
raise TypeError('Parameter a_turtle must be of type ' + str(enums.Turtle))
self._interpreter.enum_input_function_with_defaults(a_turtle)
@ivi_synchronized
def export_attribute_configuration_buffer(self):
r'''export_attribute_configuration_buffer
Export configuration buffer.
Returns:
configuration (bytes):
'''
configuration = self._interpreter.export_attribute_configuration_buffer()
return _converters.convert_to_bytes(configuration)
@ivi_synchronized
def fetch_waveform(self, number_of_samples):
r'''fetch_waveform
Returns waveform data.
Args:
number_of_samples (int): Number of samples to return
Returns:
waveform_data (array.array("d")): Samples fetched from the device. Array should be numberOfSamples big.
'''
waveform_data = self._interpreter.fetch_waveform(number_of_samples)
return waveform_data
@ivi_synchronized
def fetch_waveform_into(self, waveform_data):
r'''fetch_waveform_into
Returns waveform data.
Args:
waveform_data (numpy.array(dtype=numpy.float64)): Samples fetched from the device. Array should be numberOfSamples big.
'''
import numpy
if type(waveform_data) is not numpy.ndarray:
raise TypeError('waveform_data must be {0}, is {1}'.format(numpy.ndarray, type(waveform_data)))
if numpy.isfortran(waveform_data) is True:
raise TypeError('waveform_data must be in C-order')
if waveform_data.dtype is not numpy.dtype('float64'):
raise TypeError('waveform_data must be numpy.ndarray of dtype=float64, is ' + str(waveform_data.dtype))
self._interpreter.fetch_waveform_into(waveform_data)
def function_with_3d_numpy_array_of_numpy_complex128_input_parameter(self, multidimensional_array):
r'''function_with_3d_numpy_array_of_numpy_complex128_input_parameter
Method that takes a 3D numpy array of numpy complex128 as an input parameter.
Args:
multidimensional_array (numpy.array(dtype=numpy.complex128)): Specifies the 3D array of numpy complex numbers to write.
'''
import numpy
if type(multidimensional_array) is not numpy.ndarray:
raise TypeError('multidimensional_array must be {0}, is {1}'.format(numpy.ndarray, type(multidimensional_array)))
if numpy.isfortran(multidimensional_array) is True:
raise TypeError('multidimensional_array must be in C-order')
if multidimensional_array.dtype is not numpy.dtype('complex128'):
raise TypeError('multidimensional_array must be numpy.ndarray of dtype=complex128, is ' + str(multidimensional_array.dtype))
if multidimensional_array.ndim != 3:
raise TypeError('multidimensional_array must be numpy.ndarray of dimension=3, is ' + str(multidimensional_array.ndim))
self._interpreter.function_with_3d_numpy_array_of_numpy_complex128_input_parameter(multidimensional_array)
@ivi_synchronized
def function_with_intflag_parameter(self, flag):
r'''function_with_intflag_parameter
Calls a method that takes a flag parameter which can be OR'd from multiple enum values.
Args:
flag (enums.IntFlagEnum): A flag parameter that can be a combination (bitwise OR) of IntFlagEnum values.
'''
if type(flag) is not enums.IntFlagEnum:
raise TypeError('Parameter flag must be of type ' + str(enums.IntFlagEnum))
self._interpreter.function_with_intflag_parameter(flag)
@ivi_synchronized
def get_a_boolean(self):
r'''get_a_boolean
Returns a boolean.
Note: This method rules!