本文整理汇总了Python中ctypes.c_short方法的典型用法代码示例。如果您正苦于以下问题:Python ctypes.c_short方法的具体用法?Python ctypes.c_short怎么用?Python ctypes.c_short使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ctypes
的用法示例。
在下文中一共展示了ctypes.c_short方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_int_pointers
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def test_int_pointers(self):
from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer
LPINT = POINTER(c_int)
## p = pointer(c_int(42))
## x = LPINT.from_param(p)
x = LPINT.from_param(pointer(c_int(42)))
self.assertEqual(x.contents.value, 42)
self.assertEqual(LPINT(c_int(42)).contents.value, 42)
self.assertEqual(LPINT.from_param(None), None)
if c_int != c_long:
self.assertRaises(TypeError, LPINT.from_param, pointer(c_long(42)))
self.assertRaises(TypeError, LPINT.from_param, pointer(c_uint(42)))
self.assertRaises(TypeError, LPINT.from_param, pointer(c_short(42)))
示例2: test_array_pointers
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def test_array_pointers(self):
from ctypes import c_short, c_uint, c_int, c_long, POINTER
INTARRAY = c_int * 3
ia = INTARRAY()
self.assertEqual(len(ia), 3)
self.assertEqual([ia[i] for i in range(3)], [0, 0, 0])
# Pointers are only compatible with arrays containing items of
# the same type!
LPINT = POINTER(c_int)
LPINT.from_param((c_int*3)())
self.assertRaises(TypeError, LPINT.from_param, c_short*3)
self.assertRaises(TypeError, LPINT.from_param, c_long*3)
self.assertRaises(TypeError, LPINT.from_param, c_uint*3)
## def test_performance(self):
## check_perf()
示例3: _generate_data
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def _generate_data(self, bytes, offset):
if self._bytes_per_sample == 1:
start = offset
samples = bytes
bias = 127
amplitude = 127
data = (ctypes.c_ubyte * samples)()
else:
start = offset >> 1
samples = bytes >> 1
bias = 0
amplitude = 32767
data = (ctypes.c_short * samples)()
step = self.frequency * (math.pi * 2) / self.audio_format.sample_rate
for i in range(samples):
data[i] = int(math.sin(step * (i + start)) * amplitude + bias)
return data
示例4: dataSources
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def dataSources():
"""Return a list with [name, descrition]"""
dsn = create_buffer(1024)
desc = create_buffer(1024)
dsn_len = c_short()
desc_len = c_short()
dsn_list = {}
try:
lock.acquire()
if shared_env_h is None:
AllocateEnv()
finally:
lock.release()
while 1:
ret = ODBC_API.SQLDataSources(shared_env_h, SQL_FETCH_NEXT, \
dsn, len(dsn), ADDR(dsn_len), desc, len(desc), ADDR(desc_len))
if ret == SQL_NO_DATA_FOUND:
break
elif not ret in (SQL_SUCCESS, SQL_SUCCESS_WITH_INFO):
ctrl_err(SQL_HANDLE_ENV, shared_env_h, ret)
else:
dsn_list[dsn.value] = desc.value
return dsn_list
示例5: _generate_data
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def _generate_data(self, num_bytes):
samples = num_bytes >> 1
value = 0
maximum = 32767
minimum = -32768
data = (ctypes.c_short * samples)()
step = (maximum - minimum) * 2 * self.frequency / self.audio_format.sample_rate
envelope = self._envelope_generator
for i in range(samples):
value += step
if value > maximum:
value = maximum - (value - maximum)
step = -step
if value < minimum:
value = minimum - (value - minimum)
step = -step
data[i] = int(value * next(envelope))
return data
示例6: shmem_as_ndarray
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def shmem_as_ndarray(raw_array):
_ctypes_to_numpy = {
ctypes.c_char: np.int8,
ctypes.c_wchar: np.int16,
ctypes.c_byte: np.int8,
ctypes.c_ubyte: np.uint8,
ctypes.c_short: np.int16,
ctypes.c_ushort: np.uint16,
ctypes.c_int: np.int32,
ctypes.c_uint: np.int32,
ctypes.c_long: np.int32,
ctypes.c_ulong: np.int32,
ctypes.c_float: np.float32,
ctypes.c_double: np.float64
}
dtype = _ctypes_to_numpy[raw_array._type_]
# The following works too, but occasionally raises
# RuntimeWarning: Item size computed from the PEP 3118 buffer format string does not match the actual item size.
# and appears to be slower.
# return np.ctypeslib.as_array(raw_array)
return np.frombuffer(raw_array, dtype=dtype)
示例7: is_ipv6
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def is_ipv6(ip):
try:
if os.name == "nt":
class sockaddr(ctypes.Structure):
_fields_ = [("sa_family", ctypes.c_short),
("__pad1", ctypes.c_ushort),
("ipv4_addr", ctypes.c_byte * 4),
("ipv6_addr", ctypes.c_byte * 16),
("__pad2", ctypes.c_ulong)]
WSAStringToAddressA = ctypes.windll.ws2_32.WSAStringToAddressA
addr = sockaddr()
addr.sa_family = socket.AF_INET6
addr_size = ctypes.c_int(ctypes.sizeof(addr))
if WSAStringToAddressA(ip, socket.AF_INET6, None, ctypes.byref(addr), ctypes.byref(addr_size)) != 0:
raise socket.error(ctypes.FormatError())
return ctypes.string_at(addr.ipv6_addr, 16)
else:
return socket.inet_pton(socket.AF_INET6, ip)
except:
return False
示例8: cf_number_to_number
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def cf_number_to_number(value):
"""
Converts a CFNumber object to a python float or integer
:param value:
The CFNumber object
:return:
A python number (float or integer)
"""
type_ = CoreFoundation.CFNumberGetType(_cast_pointer_p(value))
c_type = {
1: c_byte, # kCFNumberSInt8Type
2: ctypes.c_short, # kCFNumberSInt16Type
3: ctypes.c_int32, # kCFNumberSInt32Type
4: ctypes.c_int64, # kCFNumberSInt64Type
5: ctypes.c_float, # kCFNumberFloat32Type
6: ctypes.c_double, # kCFNumberFloat64Type
7: c_byte, # kCFNumberCharType
8: ctypes.c_short, # kCFNumberShortType
9: ctypes.c_int, # kCFNumberIntType
10: c_long, # kCFNumberLongType
11: ctypes.c_longlong, # kCFNumberLongLongType
12: ctypes.c_float, # kCFNumberFloatType
13: ctypes.c_double, # kCFNumberDoubleType
14: c_long, # kCFNumberCFIndexType
15: ctypes.c_int, # kCFNumberNSIntegerType
16: ctypes.c_double, # kCFNumberCGFloatType
}[type_]
output = c_type(0)
CoreFoundation.CFNumberGetValue(_cast_pointer_p(value), type_, byref(output))
return output.value
示例9: shape_as
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def shape_as(self, obj):
"""
Return the shape tuple as an array of some other c-types
type. For example: ``self.shape_as(ctypes.c_short)``.
"""
if self._zerod:
return None
return (obj*self._arr.ndim)(*self._arr.shape)
示例10: getShort
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def getShort(self,data, index):
# return two bytes from data as a signed 16-bit value
return c_short((data[index+1] << 8) + data[index]).value
示例11: test_byref_pointer
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def test_byref_pointer(self):
# The from_param class method of POINTER(typ) classes accepts what is
# returned by byref(obj), it type(obj) == typ
from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref
LPINT = POINTER(c_int)
LPINT.from_param(byref(c_int(42)))
self.assertRaises(TypeError, LPINT.from_param, byref(c_short(22)))
if c_int != c_long:
self.assertRaises(TypeError, LPINT.from_param, byref(c_long(22)))
self.assertRaises(TypeError, LPINT.from_param, byref(c_uint(22)))
示例12: test_byref_pointerpointer
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def test_byref_pointerpointer(self):
# See above
from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref
LPLPINT = POINTER(POINTER(c_int))
LPLPINT.from_param(byref(pointer(c_int(42))))
self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_short(22))))
if c_int != c_long:
self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_long(22))))
self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_uint(22))))
示例13: test_array_pointers
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import c_short [as 别名]
def test_array_pointers(self):
from ctypes import c_short, c_uint, c_int, c_long, POINTER
INTARRAY = c_int * 3
ia = INTARRAY()
self.assertEqual(len(ia), 3)
self.assertEqual([ia[i] for i in range(3)], [0, 0, 0])
# Pointers are only compatible with arrays containing items of
# the same type!
LPINT = POINTER(c_int)
LPINT.from_param((c_int*3)())
self.assertRaises(TypeError, LPINT.from_param, c_short*3)
self.assertRaises(TypeError, LPINT.from_param, c_long*3)
self.assertRaises(TypeError, LPINT.from_param, c_uint*3)