quanttp2/quanttp/data/qng_wrapper_windows.py

77 lines
2.6 KiB
Python
Raw Normal View History

2020-05-21 21:05:12 +07:00
# Copyright (c) 2020 Andika Wasisto
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2020-08-17 05:03:36 +07:00
class QngWrapperWindows:
2020-05-21 21:05:12 +07:00
def __init__(self):
2020-08-17 05:03:36 +07:00
import win32com.client
2020-09-07 21:11:39 +07:00
self._qng = win32com.client.Dispatch("QWQNG.QNG")
2020-05-21 21:05:12 +07:00
def deviceId(self):
try:
return self._qng.DeviceId
except:
self._qng.Reset()
return self._qng.DeviceId
2020-05-21 21:05:12 +07:00
def randint32(self):
2020-05-24 07:33:43 +07:00
try:
2020-09-07 21:11:39 +07:00
return self._qng.RandInt32
2020-05-24 07:33:43 +07:00
except:
2020-09-07 21:11:39 +07:00
self._qng.Reset()
return self._qng.RandInt32
2020-05-21 21:05:12 +07:00
def randuniform(self):
2020-05-24 07:33:43 +07:00
try:
2020-09-07 21:11:39 +07:00
return self._qng.RandUniform
2020-05-24 07:33:43 +07:00
except:
2020-09-07 21:11:39 +07:00
self._qng.Reset()
return self._qng.RandUniform
2020-05-21 21:05:12 +07:00
def randnormal(self):
2020-05-24 07:33:43 +07:00
try:
2020-09-07 21:11:39 +07:00
return self._qng.RandNormal
2020-05-24 07:33:43 +07:00
except:
2020-09-07 21:11:39 +07:00
self._qng.Reset()
return self._qng.RandNormal
2020-05-21 21:05:12 +07:00
def randbytes(self, length):
2020-05-24 07:33:43 +07:00
try:
return self._randbytes_arbitrary_length(length)
2020-05-24 07:33:43 +07:00
except:
2020-09-07 21:11:39 +07:00
self._qng.Reset()
return self._randbytes_arbitrary_length(length)
def _randbytes_arbitrary_length(self, length):
if length <= 8192:
2020-09-07 21:11:39 +07:00
return bytes(self._qng.RandBytes(length))
else:
data = bytearray()
for x in range(length // 8192):
2020-09-07 21:11:39 +07:00
data.extend(bytearray(self._qng.RandBytes(8192)))
bytes_needed = length % 8192
if bytes_needed != 0:
2020-09-07 21:11:39 +07:00
data.extend(bytearray(self._qng.RandBytes(bytes_needed)))
return bytes(data)
2020-05-25 12:40:13 +07:00
def clear(self):
2020-09-07 21:11:39 +07:00
self._qng.Clear()