Python源码示例:ctypes.byref()
示例1
def windowsRam(self):
"""
Uses Windows API to check RAM
"""
kernel32 = ctypes.windll.kernel32
c_ulong = ctypes.c_ulong
class MEMORYSTATUS(ctypes.Structure):
_fields_ = [
("dwLength", c_ulong),
("dwMemoryLoad", c_ulong),
("dwTotalPhys", c_ulong),
("dwAvailPhys", c_ulong),
("dwTotalPageFile", c_ulong),
("dwAvailPageFile", c_ulong),
("dwTotalVirtual", c_ulong),
("dwAvailVirtual", c_ulong)
]
memoryStatus = MEMORYSTATUS()
memoryStatus.dwLength = ctypes.sizeof(MEMORYSTATUS)
kernel32.GlobalMemoryStatus(ctypes.byref(memoryStatus))
return int(memoryStatus.dwTotalPhys / 1024 ** 2)
示例2
def hamming_klcs(seqs):
"""
Implementation of k-LCS as described in Christian Blichmann's thesis "Automatisierte Signaturgenerierung fuer Malware-Staemme" on page 52.
This algorithm will not forcibly find THE longest common subsequence among all sequences, as the subsequence returned by the 2-LCS algorithm
might not be the optimal one from the set of longest common subsequences.
:see: https://static.googleusercontent.com/media/www.zynamics.com/en//downloads/blichmann-christian--diplomarbeit--final.pdf
:param seqs: List of sequences
:return: A shared subsequence between the input sequences. Not necessarily the longest one, and only one of several that might exist.
"""
c_seqs_type = c_char_p * len(seqs)
c_seqs = c_seqs_type()
c_seqs[:] = seqs
c_lens_type = c_size_t * len(seqs)
c_lens = c_lens_type()
c_lens[:] = [len(seq) for seq in seqs]
result = create_string_buffer("\0" * min(len(seq) for seq in seqs))
result_len = c_size_t(len(result))
ret = _lib.hamming_klcs_c(c_seqs, c_lens, len(seqs), result, byref(result_len))
if ret == 0:
return result[:result_len.value]
else:
raise RuntimeError("lcs returned error code %d" % ret)
示例3
def try_enable_ansi():
"""Try enabling ANSI colors
https://stackoverflow.com/questions/44482505/setconsolemode-returning-false-when-enabling-ansi-color-under-windows-10"""
lpMode = wintypes.DWORD()
handle = WINAPI._GetStdHandle(WINAPI._STDOUT)
if WINAPI._GetConsoleMode(handle, ctypes.byref(lpMode)):
if not WINAPI._SetConsoleMode(handle, lpMode.value | WINAPI._ENABLE_VIRTUAL_TERMINAL_PROCESSING):
return False
else:
return False
lpMode = wintypes.DWORD()
handle = WINAPI._GetStdHandle(WINAPI._STDERR)
if WINAPI._GetConsoleMode(handle, ctypes.byref(lpMode)):
if not WINAPI._SetConsoleMode(handle, lpMode.value | WINAPI._ENABLE_VIRTUAL_TERMINAL_PROCESSING):
return False
else:
return False
return True
示例4
def lcs(s, t):
"""
Calculate the longest common subsequence between two sequences in O(min(len(x), len(y))) space and O(len(x) * len(y)) time.
Implemented in C++.
Since only one instance from the set of longest common subsequences is returned,
the algorithm has the unpleasing property of not being commutative (i.e., changing
the input vectors changes the result).
:see: https://en.wikipedia.org/wiki/Hirschberg%27s_algorithm
:param x: First input sequence.
:param y: Second input sequence.
:return: LCS(x, y)
"""
result = create_string_buffer("\0" * min(len(s), len(t)))
result_len = c_size_t(len(result))
if isinstance(s, list):
s = "".join(s)
if isinstance(t, list):
t = "".join(t)
ret = _lib.hirschberg_lcs(s, len(s), t, len(t), result, byref(result_len))
if ret == 0:
return result[:result_len.value]
else:
raise RuntimeError("lcs returned error code %d" % ret)
示例5
def Put(self, name, val, flavor):
prototype = ctypes.WINFUNCTYPE(HRESULT,
wintypes.LPCWSTR,
ctypes.POINTER(winapi.VARIANT),
ctypes.c_long)
paramflags = ((_In_, 'wszName'),
(_In_, 'pVal'),
(_In_, 'lFlavor'),
)
_Put = prototype(IWbemQualifierSet_Put_Idx,
'Put',
paramflags)
_Put.errcheck = winapi.RAISE_NON_ZERO_ERR
_Put(self.this,
name,
ctypes.byref(val) if val else None,
flavor
)
示例6
def Put(self, name, flags, val, type_param):
prototype = ctypes.WINFUNCTYPE(HRESULT,
wintypes.LPCWSTR,
ctypes.c_long,
ctypes.POINTER(winapi.VARIANT),
CIMTYPE)
paramflags = ((_In_, 'wszName'),
(_In_, 'lFlags'),
(_In_, 'pVal'),
(_In_, 'Type'),
)
_Put = prototype(IWbemClassObject_Put_Idx,
'Put',
paramflags)
_Put.errcheck = winapi.RAISE_NON_ZERO_ERR
_Put(self.this,
name,
flags,
ctypes.byref(val) if val else None,
type_param
)
示例7
def GetNames(self, qualifier_name, flags, qualifier_val):
prototype = ctypes.WINFUNCTYPE(HRESULT,
wintypes.LPCWSTR,
ctypes.c_long,
ctypes.POINTER(winapi.VARIANT),
ctypes.POINTER(wintypes.LPVOID))
paramflags = ((_In_, 'wszQualifierName'),
(_In_, 'lFlags'),
(_In_, 'pQualifierVal'),
(_Out_, 'pNames'),
)
_GetNames = prototype(IWbemClassObject_GetNames_Idx,
'GetNames',
paramflags)
_GetNames.errcheck = winapi.RAISE_NON_ZERO_ERR
return_obj = _GetNames(self.this,
qualifier_name,
flags,
ctypes.byref(qualifier_val) if qualifier_val else None
)
return_obj = ctypes.cast(wintypes.LPVOID(return_obj), ctypes.POINTER(winapi.SAFEARRAY))
return return_obj
示例8
def SetValue(self, name, flags, value):
prototype = ctypes.WINFUNCTYPE(HRESULT,
wintypes.LPCWSTR,
ctypes.c_long,
ctypes.POINTER(winapi.VARIANT))
paramflags = ((_In_, 'wszName'),
(_In_, 'lFlags'),
(_In_, 'pValue'),
)
_SetValue = prototype(IWbemContext_SetValue_Idx,
'SetValue',
paramflags)
_SetValue.errcheck = winapi.RAISE_NON_ZERO_ERR
_SetValue(self.this,
name,
flags,
ctypes.byref(value) if value else None
)
示例9
def QueryInterface(self, riid):
prototype = ctypes.WINFUNCTYPE(HRESULT,
ctypes.POINTER(winapi.GUID),
ctypes.POINTER(wintypes.LPVOID))
paramflags = ((_In_, 'riid'),
(_Out_, 'ppvObject', ctypes.pointer(wintypes.LPVOID(None)))
)
_QueryInterface = prototype(IUnknown_QueryInterface_Idx,
'QueryInterface',
paramflags)
_QueryInterface.errcheck = winapi.RAISE_NON_ZERO_ERR
return_ptr = _QueryInterface(self.this,
ctypes.byref(riid))
return IUnknown(return_ptr.contents)
示例10
def imdecode(str_img, flag=1):
"""Decode image from str buffer.
Wrapper for cv2.imdecode that uses mx.nd.NDArray
Parameters
----------
str_img : str
str buffer read from image file
flag : int
same as flag for cv2.imdecode
Returns
-------
img : NDArray
decoded image in (width, height, channels)
with BGR color channel order
"""
hdl = NDArrayHandle()
check_call(_LIB.MXCVImdecode(ctypes.c_char_p(str_img),
mx_uint(len(str_img)),
flag, ctypes.byref(hdl)))
return mx.nd.NDArray(hdl)
示例11
def resize(src, size, interpolation=cv2.INTER_LINEAR):
"""Decode image from str buffer.
Wrapper for cv2.imresize that uses mx.nd.NDArray
Parameters
----------
src : NDArray
image in (width, height, channels)
size : tuple
target size in (width, height)
interpolation : int
same as interpolation for cv2.imresize
Returns
-------
img : NDArray
resized image
"""
hdl = NDArrayHandle()
check_call(_LIB.MXCVResize(src.handle, mx_uint(size[0]), mx_uint(size[1]),
interpolation, ctypes.byref(hdl)))
return mx.nd.NDArray(hdl)
示例12
def copyMakeBorder(src, top, bot, left, right, border_type=cv2.BORDER_CONSTANT, value=0):
"""Pad image border
Wrapper for cv2.copyMakeBorder that uses mx.nd.NDArray
Parameters
----------
src : NDArray
Image in (width, height, channels).
Others are the same with cv2.copyMakeBorder
Returns
-------
img : NDArray
padded image
"""
hdl = NDArrayHandle()
check_call(_LIB.MXCVcopyMakeBorder(src.handle, ctypes.c_int(top), ctypes.c_int(bot),
ctypes.c_int(left), ctypes.c_int(right),
ctypes.c_int(border_type), ctypes.c_double(value),
ctypes.byref(hdl)))
return mx.nd.NDArray(hdl)
示例13
def set_bulk_size(size):
"""Set size limit on bulk execution.
Bulk execution bundles many operators to run together.
This can improve performance when running a lot of small
operators sequentially.
Parameters
----------
size : int
Maximum number of operators that can be bundled in a bulk.
Returns
-------
int
Previous bulk size.
"""
prev = ctypes.c_int()
check_call(_LIB.MXEngineSetBulkSize(
ctypes.c_int(size), ctypes.byref(prev)))
return prev.value
示例14
def tell(self):
"""Returns the current position of write head.
Example usage:
----------
>>> record = mx.recordio.MXIndexedRecordIO('tmp.idx', 'tmp.rec', 'w')
>>> print(record.tell())
0
>>> for i in range(5):
... record.write_idx(i, 'record_%d'%i)
... print(record.tell())
16
32
48
64
80
"""
assert self.writable
pos = ctypes.c_size_t()
check_call(_LIB.MXRecordIOWriterTell(self.handle, ctypes.byref(pos)))
return pos.value
示例15
def num_gpus():
"""Query CUDA for the number of GPUs present.
Raises
------
Will raise an exception on any CUDA error.
Returns
-------
count : int
The number of GPUs.
"""
count = ctypes.c_int()
check_call(_LIB.MXGetGPUCount(ctypes.byref(count)))
return count.value
示例16
def _new_alloc_handle(shape, ctx, delay_alloc, dtype=mx_real_t):
"""Return a new handle with specified shape and context.
Empty handle is only used to hold results.
Returns
-------
handle
A new empty `NDArray` handle.
"""
hdl = NDArrayHandle()
check_call(_LIB.MXNDArrayCreateEx(
c_array_buf(mx_uint, native_array('I', shape)),
mx_uint(len(shape)),
ctypes.c_int(ctx.device_typeid),
ctypes.c_int(ctx.device_id),
ctypes.c_int(int(delay_alloc)),
ctypes.c_int(int(_DTYPE_NP_TO_MX[np.dtype(dtype).type])),
ctypes.byref(hdl)))
return hdl
示例17
def shape(self):
"""Tuple of array dimensions.
Examples
--------
>>> x = mx.nd.array([1, 2, 3, 4])
>>> x.shape
(4L,)
>>> y = mx.nd.zeros((2, 3, 4))
>>> y.shape
(2L, 3L, 4L)
"""
ndim = mx_uint()
pdata = ctypes.POINTER(mx_uint)()
check_call(_LIB.MXNDArrayGetShape(
self.handle, ctypes.byref(ndim), ctypes.byref(pdata)))
return tuple(pdata[:ndim.value]) # pylint: disable=invalid-slice-index
示例18
def context(self):
"""Device context of the array.
Examples
--------
>>> x = mx.nd.array([1, 2, 3, 4])
>>> x.context
cpu(0)
>>> type(x.context)
<class 'mxnet.context.Context'>
>>> y = mx.nd.zeros((2,3), mx.gpu(0))
>>> y.context
gpu(0)
"""
dev_typeid = ctypes.c_int()
dev_id = ctypes.c_int()
check_call(_LIB.MXNDArrayGetContext(
self.handle, ctypes.byref(dev_typeid), ctypes.byref(dev_id)))
return Context(Context.devtype2str[dev_typeid.value], dev_id.value)
示例19
def dtype(self):
"""Data-type of the array's elements.
Returns
-------
numpy.dtype
This NDArray's data type.
Examples
--------
>>> x = mx.nd.zeros((2,3))
>>> x.dtype
<type 'numpy.float32'>
>>> y = mx.nd.zeros((2,3), dtype='int32')
>>> y.dtype
<type 'numpy.int32'>
"""
mx_dtype = ctypes.c_int()
check_call(_LIB.MXNDArrayGetDType(
self.handle, ctypes.byref(mx_dtype)))
return _DTYPE_MX_TO_NP[mx_dtype.value]
示例20
def __deepcopy__(self, _):
"""Returns a deep copy of the input object.
This function returns a deep copy of the input object including the current state
of all its parameters such as weights, biases, etc.
Any changes made to the deep copy do not reflect in the original object.
Example
-------
>>> import copy
>>> data = mx.sym.Variable('data')
>>> data_1 = copy.deepcopy(data)
>>> data_1 = 2*data
>>> data_1.tojson()
>>> data_1 is data # Data got modified
False
"""
handle = SymbolHandle()
check_call(_LIB.MXSymbolCopy(self.handle,
ctypes.byref(handle)))
return Symbol(handle)
示例21
def name(self):
"""Gets name string from the symbol, this function only works for non-grouped symbol.
Returns
-------
value : str
The name of this symbol, returns ``None`` for grouped symbol.
"""
ret = ctypes.c_char_p()
success = ctypes.c_int()
check_call(_LIB.MXSymbolGetName(
self.handle, ctypes.byref(ret), ctypes.byref(success)))
if success.value != 0:
return py_str(ret.value)
else:
return None
示例22
def list_attr(self, recursive=False):
"""Gets all attributes from the symbol.
Example
-------
>>> data = mx.sym.Variable('data', attr={'mood': 'angry'})
>>> data.list_attr()
{'mood': 'angry'}
Returns
-------
ret : Dict of str to str
A dictionary mapping attribute keys to values.
"""
if recursive:
raise DeprecationWarning("Symbol.list_attr with recursive=True has been deprecated. "
"Please use attr_dict instead.")
size = mx_uint()
pairs = ctypes.POINTER(ctypes.c_char_p)()
f_handle = _LIB.MXSymbolListAttrShallow
check_call(f_handle(self.handle, ctypes.byref(size), ctypes.byref(pairs)))
return {py_str(pairs[i * 2]): py_str(pairs[i * 2 + 1]) for i in range(size.value)}
示例23
def list_arguments(self):
"""Lists all the arguments in the symbol.
Example
-------
>>> a = mx.sym.var('a')
>>> b = mx.sym.var('b')
>>> c = a + b
>>> c.list_arguments
['a', 'b']
Returns
-------
args : list of string
List containing the names of all the arguments required to compute the symbol.
"""
size = ctypes.c_uint()
sarr = ctypes.POINTER(ctypes.c_char_p)()
check_call(_LIB.MXSymbolListArguments(
self.handle, ctypes.byref(size), ctypes.byref(sarr)))
return [py_str(sarr[i]) for i in range(size.value)]
示例24
def __len__(self):
"""Get number of outputs for the symbol.
Example
-------
>>> a = mx.sym.var('a')
>>> b = mx.sym.var('b')
>>> c = a + b
>>> len(c)
Returns
-------
len(self): Number of outputs
Number of outputs
"""
output_count = mx_uint()
check_call(_LIB.MXSymbolGetNumOutputs(self.handle, ctypes.byref(output_count)))
return output_count.value
示例25
def list_inputs(self):
"""Lists all arguments and auxiliary states of this Symbol.
Returns
-------
inputs : list of str
List of all inputs.
Examples
--------
>>> bn = mx.sym.BatchNorm(name='bn')
>>> bn.list_arguments()
['bn_data', 'bn_gamma', 'bn_beta']
>>> bn.list_auxiliary_states()
['bn_moving_mean', 'bn_moving_var']
>>> bn.list_inputs()
['bn_data', 'bn_gamma', 'bn_beta', 'bn_moving_mean', 'bn_moving_var']
"""
size = ctypes.c_uint()
sarr = ctypes.POINTER(ctypes.c_char_p)()
check_call(_LIB.NNSymbolListInputNames(
self.handle, 0, ctypes.byref(size), ctypes.byref(sarr)))
return [py_str(sarr[i]) for i in range(size.value)]
示例26
def get_backend_symbol(self, backend):
"""Return symbol for target backend.
Parameters
----------
backend : str
The backend names.
Returns
-------
out : Symbol
The created Symbol for target backend.
"""
out = SymbolHandle()
check_call(_LIB.MXGenBackendSubgraph(self.handle, c_str(backend), ctypes.byref(out)))
return Symbol(out)
示例27
def winapi_test():
def _winapi_test(handle):
csbi = CONSOLE_SCREEN_BUFFER_INFO()
success = WINAPI._GetConsoleScreenBufferInfo(
handle, byref(csbi))
return bool(success)
return any(_winapi_test(h) for h in
(WINAPI._GetStdHandle(WINAPI._STDOUT), WINAPI._GetStdHandle(WINAPI._STDERR)))
示例28
def _terminal_size(handle):
csbi = CONSOLE_SCREEN_BUFFER_INFO()
if not WINAPI._GetConsoleScreenBufferInfo(handle, byref(csbi)):
raise ctypes.WinError() # Subclass of OSError.
else:
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1
return columns, rows
示例29
def get_font():
handle = WINAPI._GetStdHandle(WINAPI._STDOUT)
font = CONSOLE_FONT_INFOEX()
font.cbSize = sizeof(CONSOLE_FONT_INFOEX)
if not WINAPI._GetCurrentConsoleFontEx(handle, False, byref(font)):
return None
else:
return font.FaceName
示例30
def __capture(self, data, _sdr):
timestamp = time.time()
dst = ctypes.byref(self._capture, 0)
ctypes.memmove(dst, data, len(data))
iq = self.__stream_to_complex(self._capture)
l, f = psd(iq, BINS, SAMPLE_RATE,
scale_by_freq=False,
noverlap=-SAMPLES / 64)
f /= 1e6
f += self._freq
event = Event(Events.SCAN_DATA, timestamp=timestamp, l=l, f=f)
post_event(self._eventHandler, event)