当前位置: 首页 > news >正文

怎样建设一个网站教学微信营销的方法有哪些

怎样建设一个网站教学,微信营销的方法有哪些,如何让网站做成移动版,线上推广的意义文章目录 C库ctypes基础数据类型参数与返回值类型数组指针结构体类型回调函数工具函数 示例 ctypes是Python的外部函数,提供了与C兼容的类型,并允许调用DLL库中的函数。 C库 要使函数能被Python调用,需要编译为动态库: # -fPIC…

文章目录

    • C++库
    • ctypes
      • 基础数据类型
      • 参数与返回值类型
      • 数组
      • 指针
      • 结构体类型
      • 回调函数
      • 工具函数
    • 示例

ctypes是Python的外部函数,提供了与C兼容的类型,并允许调用DLL库中的函数。

C++库

要使函数能被Python调用,需要编译为动态库:

# -fPIC使得位置独立
# -shared代表这是动态库
g++ -fPIC -shared -o libTest.so test.cpp

为保证函数接口能被外部识别,需要导出为纯C的:

#ifdef __cplusplus
extern "C"
{
#endifvoid * callForTest(char *params);#ifdef __cplusplus
};
#endif

ctypes

在python中要使用DLL库,需要先通过cdll来加载(cdll载入按标准的 cdecl调用协议导出的函数):

from ctypes import cdlltarget = cdll.LoadLibrary("libTest.so")

通过in_dll()可获取库中导出的变量:

# 获取 Python 库本身的 Py_OptimizeFlag
opt_flag = c_int.in_dll(pythonapi, "Py_OptimizeFlag")

基础数据类型

ctypes 定义了一些和C兼容的基本数据类型,所有基础类型都继承自ctypes._SimpleCData

  • value属性:包含实例的实际值。实例提取 value 属性时,通常每次会返回一个新的对象。
ctypes 类型C 类型Python 类型
c_bool_Boolbool (1)
c_charchar1-character bytes
c_wcharwchar_t1-character str
c_bytecharint
c_ubyteunsigned charint
c_shortshortint
c_ushortunsigned shortint
c_intintint
c_uintunsigned intint
c_longlongint
c_ulongunsigned longint
c_longlong__int64 or long longint
c_ulonglongunsigned __int64 or unsigned long longint
c_size_tsize_tint
c_ssize_tssize_t or Py_ssize_tint
c_floatfloatfloat
c_doubledoublefloat
c_longdoublelong doublefloat
c_char_pchar* (NUL terminated)bytes or None
c_wchar_pwchar_t* (NUL terminated)str or None
c_void_pvoid*int or None

除了整数、字符串以及字节串之外,所有的Python类型都必须使用它们对应的ctypes类型包装,才能够被正确地转换为所需的C语言类型。通过ctypes创建的类型是可变的(通过修改.value):

i = c_int()
# i.value == 0
i.value = 99
# i.value == 99c_wchar_p("Hello, World")
# c.value == "Hello, World"

当给指针类型的对象c_char_p, c_wchar_p 和 c_void_p等赋值时,将改变它们所指向的内存地址,而不是它们所指向的内存区域的内容。若底层函数可能会改变指针地址,则需要通过create_string_buffer创建:

p = create_string_buffer(3) # create a 3 byte buffer, initialized to NUL bytesp = create_string_buffer(b"Hello") # create a buffer containing a NUL terminated stringp = create_string_buffer(b"Hello", 10) # create a 10 byte buffer
p.value = b"Hi"
print(sizeof(p), repr(p.raw))
# 10 b'Hi\x00lo\x00\x00\x00\x00\x00'

参数与返回值类型

以libc库为例:

cdll.LoadLibrary("libc.so.6")printf = libc.printf
# 通过设置argtypes属性来指定函数的必选参数类型
# 指定数据类型可以防止不合理的参数传递,并且会自动尝试将参数转换为需要的类型(否则必须手动转换)
printf.argtypes = [c_char_p, c_char_p, c_int, c_double]
printf(b"String '%s', Int %d, Double %f\n", b"Hi", 10, 2.2)strchr = libc.strchr
# 默认返回int,其他类型需通过restype属性来设定
strchr.restype = c_char_p
strchr.argtypes = [c_char_p, c_char]
strchr(b"abcdef", b"d")# 构造缓冲区,获取输出
i = c_int()
f = c_float()
s = create_string_buffer(b'\x00' * 32) # 创建长度32,全部为NULL的缓冲区
libc.sscanf(b"1 3.14 Hello", b"%d %f %s", byref(i), byref(f), s)
print(i.value, f.value, repr(s.value))
# 1 3.140000104904175 b'Hello'

数组

数组是一个序列,包含指定个数元素,且必须类型相同。创建数组类型的推荐方式是使用一个类型乘以一个正数:

  • _length_属性:指明数组中元素数量的正整数。
  • _type_属性:指明每个元素的类型。
class POINT(Structure):_fields_ = ("x", c_int), ("y", c_int)pointsArray = POINT * 5
print(sizeof(pointsArray))
# 40pa = pointArray(POINT(1,2), POINT(3,4)) # 后面三个全为0
print(sizeof(pa), len(pa))
# 40 5
for i in pa: print(i.x, i.y, end=";")
# 1 2;3 4;0 0;0 0;0 0;

指针

可以将ctypes类型数据传入pointer()函数创建指针:

  • contents属性:返回指针指向的真实对象( 每次访问这个属性时都会构造返回一个新的相同对象
  • _type_属性:指明所指向的类型。
  • 指针对象也可以通过整数下标进行访问;
  • 通过整数下标赋值可以改变指针所指向的真实内容;
  • 无参调用指针类型可以创建一个NULL指针;
i = c_int(12)
pi = pointer(i)
ic = pi.contentsprint(ic, ic is i)
# c_int(12) False# 通过下标访问与修改内容
pi[0]=34
print(pi[0], pi.contents)
# 34 c_int(34)# 修改指针指向
ii = c_int(45)
pi.contents = ii
print(pi[0])
# 56

结构体类型

结构体必须通过子类化ctypes.Structure来创建,并且至少要定义一个 _fields_类变量,并允许通过直接属性访问来读取和写入字段。

  • _fields_属性:定义结构体字段的序列。 其中的条目必须为2元组或3元组。
    • 第一项是字段名称;
    • 第二项指明字段类型,可以是任何 ctypes 数据类型;
    • 对于整数类型字段(如c_int),可以给定第三个可选项;为定义字段比特位宽度的小正整数。
  • _pack_属性:一个可选的小整数,它允许覆盖实体中结构体字段的对齐方式。

对于不完整类型:即在结构体中包含指向自身的指针:

struct cell; /* forward declaration */struct cell {char *name;struct cell *next;
};

在python中不能在类中使用自身,需要先定义结构,然后在设定_fields_属性:

class cell(Structure):passcell._fields_ = [("name", c_char_p),("next", pointer(cell))]

回调函数

必须先为回调函数创建一个类(明确调用约定,返回值类型以及参数信息);CFUNCTYPE()工厂函数使用 cdecl 调用约定创建回调函数类型。

qsort = libc.qsort
qsort.restype=None# 第一个参数为返回值类型,后续一次为对应函数参数
CMPFun = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
def py_cmp_func(a,b):print('py_cmp_func', a[0], b[0])return 0cmp_fun = CMPFun(py_cmp_func)IntArray5 = c_int * 5
ia = IntArray5(1, 9, 7, 5, 8)
qsort(ia, len(ia), sizeof(c_int), py_cmp_func)

注意:回调函数是在Python之外的另外一个线程使用(外部代码调用这个回调函数), ctypes 会在每一次调用时创建一个虚拟 Python 线程。这个行为在大多数情况下是合理的,但也意味着如果有数据使用 threading.local 方式存储,将无法访问,就算它们是在同一个C线程中调用的。

工具函数

一些常用工具函数:

  • ctypes.addressof(obj):以整数形式返回内存缓冲区地址;obj 必须为一个 ctypes 类型的实例。
  • ctypes.alignment(obj_or_type):返回一个 ctypes 类型的对齐要求;obj_or_type 必须为一个 ctypes 类型或实例。
  • ctypes.byref(obj[, offset]):返回指向obj的引用,该对象必须为一个 ctypes 类型的实例。 offset 默认值为零,且必须为一个将被添加到内部指针值的整数。
  • ctypes.cast(obj, type):类似于 C 的强制转换运算符。 它返回一个 type 的新实例,该实例指向与 obj 相同的内存块。 type 必须为指针类型,而 obj 必须为可以被作为指针来解读的对象。
  • ctypes.create_string_buffer(init_or_size, size=None):创建一个可变的字符缓冲区。 返回的对象是一个 c_char 的 ctypes 数组。
    • init_or_size 必须是一个指明数组大小的整数,或者是一个将被用来初始化数组条目的字节串对象。
    • 如果将一个字节串对象指定为第一个参数,则将使缓冲区大小比其长度多一项以便数组的最后一项为一个 NUL 终结符。 可以传入一个整数作为第二个参数以允许在不使用字节串长度的情况下指定数组大小。
  • ctypes.create_unicode_buffer(init_or_size, size=None):创建一个可变的 unicode 字符缓冲区。
  • ctypes.get_errno():返回调用线程中系统 errno 变量的 ctypes 私有副本的当前值。
  • ctypes.memmove(dst, src, count):与标准 C memmove 库函数相同:将 count 个字节从 src 拷贝到 dst。 dst 和 src 必须为整数或可被转换为指针的 ctypes 实例。
  • ctypes.memset(dst, c, count):与标准 C memset 库函数相同:将位于地址 dst 的内存块用 count 个字节的 c 值填充。 dst 必须为指定地址的整数或 ctypes 实例。
  • ctypes.POINTER(type):创建并返回一个新的 ctypes 指针类型。 指针类型会被缓存并在内部重用,因此重复调用此函数耗费不大。 type 必须为 ctypes 类型。
  • ctypes.pointer(obj):创建一个新的指向 obj 的指针实例。 返回的对象类型为 POINTER(type(obj))。
    • 如果你只是想向外部函数调用传递一个对象指针,你应当使用更为快速的 byref(obj)。
  • ctypes.sizeof(obj_or_type):返回 ctypes 类型或实例的内存缓冲区以字节表示的大小。
  • ctypes.string_at(address, size=- 1):返回从内存地址 address 开始的以字节串表示的 C 字符串。 如果指定了 size,则将其用作长度,否则将假定字符串以零值结尾。
  • ctypes.wstring_at(address, size=- 1):返回从内存地址 address 开始的以字符串表示的宽字节字符串。

示例

以C++回调一个python函数为例:

C++中的回调定义:

#ifdef __cplusplus
extern "C"
{
#endiftypedef void (*PrintOutput)(const char* outputs);void set_callback(PrintOutput func);#ifdef __cplusplus
};
#endif

python中使用回调:

from ctypes import cdll, c_char_p, CFUNCTYPE, POINTERtarget = cdll.LoadLibrary("/workspace/libTest.so")PrintCallback = CFUNCTYPE(None, c_char_p)
def print_callback(outputs):print("outputs:", outputs)py_callback = PrintCallback(print_callback)target.set_callback.restype = None
target.set_callback(py_callback)    

文章转载自:
http://dinncoproruption.bkqw.cn
http://dinncocaldoverde.bkqw.cn
http://dinncobestir.bkqw.cn
http://dinncosam.bkqw.cn
http://dinncomarxize.bkqw.cn
http://dinncodemonetization.bkqw.cn
http://dinncoalgebraize.bkqw.cn
http://dinncogastrostege.bkqw.cn
http://dinncoisogonic.bkqw.cn
http://dinncoauscultatory.bkqw.cn
http://dinncosmug.bkqw.cn
http://dinncoendoangiitis.bkqw.cn
http://dinncomortally.bkqw.cn
http://dinncotribromide.bkqw.cn
http://dinncoafterwar.bkqw.cn
http://dinncohokum.bkqw.cn
http://dinncofructification.bkqw.cn
http://dinncoinguinal.bkqw.cn
http://dinncopapermaking.bkqw.cn
http://dinncosolitarily.bkqw.cn
http://dinncoscourway.bkqw.cn
http://dinncobeautifully.bkqw.cn
http://dinncometestrum.bkqw.cn
http://dinncofox.bkqw.cn
http://dinncofortyish.bkqw.cn
http://dinncoxw.bkqw.cn
http://dinncotrehalose.bkqw.cn
http://dinncoosteochondrosis.bkqw.cn
http://dinncoarseniureted.bkqw.cn
http://dinnconeoclassic.bkqw.cn
http://dinncospatula.bkqw.cn
http://dinncoeustatic.bkqw.cn
http://dinncoglug.bkqw.cn
http://dinncodraffy.bkqw.cn
http://dinncovisualiser.bkqw.cn
http://dinncounmask.bkqw.cn
http://dinncoiteration.bkqw.cn
http://dinncohasten.bkqw.cn
http://dinncoimmotile.bkqw.cn
http://dinncosymbolist.bkqw.cn
http://dinncoalienee.bkqw.cn
http://dinnconegroni.bkqw.cn
http://dinncopromissory.bkqw.cn
http://dinncoigy.bkqw.cn
http://dinncoadulterated.bkqw.cn
http://dinncopetalled.bkqw.cn
http://dinncobechic.bkqw.cn
http://dinncovineyardist.bkqw.cn
http://dinncoovercompensate.bkqw.cn
http://dinncoimbody.bkqw.cn
http://dinncogenerous.bkqw.cn
http://dinncoinitiatress.bkqw.cn
http://dinncodeliriant.bkqw.cn
http://dinncounheroic.bkqw.cn
http://dinncovitrain.bkqw.cn
http://dinncoretinospora.bkqw.cn
http://dinncosprowsie.bkqw.cn
http://dinncoprey.bkqw.cn
http://dinncodisconnected.bkqw.cn
http://dinncochallie.bkqw.cn
http://dinncointerferometer.bkqw.cn
http://dinncogoniometric.bkqw.cn
http://dinncoworth.bkqw.cn
http://dinncopsychometrics.bkqw.cn
http://dinncoexperimentation.bkqw.cn
http://dinncodogberry.bkqw.cn
http://dinncoapoplexy.bkqw.cn
http://dinnconavar.bkqw.cn
http://dinncoantifederalism.bkqw.cn
http://dinncoorgeat.bkqw.cn
http://dinncoautopia.bkqw.cn
http://dinncoaldehyde.bkqw.cn
http://dinncollc.bkqw.cn
http://dinncobismillah.bkqw.cn
http://dinncohfs.bkqw.cn
http://dinncopretor.bkqw.cn
http://dinncobereft.bkqw.cn
http://dinncocrud.bkqw.cn
http://dinnconagpur.bkqw.cn
http://dinncopurchaseless.bkqw.cn
http://dinncokremlinology.bkqw.cn
http://dinncofidelism.bkqw.cn
http://dinncokami.bkqw.cn
http://dinncoluthern.bkqw.cn
http://dinncoaristotelian.bkqw.cn
http://dinncoepicureanism.bkqw.cn
http://dinncospelter.bkqw.cn
http://dinncoerie.bkqw.cn
http://dinncodiscursiveness.bkqw.cn
http://dinncoconcernment.bkqw.cn
http://dinncotransvalue.bkqw.cn
http://dinncovesiculate.bkqw.cn
http://dinncorhizocarpous.bkqw.cn
http://dinncoboychik.bkqw.cn
http://dinncozinckiferous.bkqw.cn
http://dinncokerchiefed.bkqw.cn
http://dinncoconciliator.bkqw.cn
http://dinncoadermin.bkqw.cn
http://dinncojames.bkqw.cn
http://dinncoserif.bkqw.cn
http://www.dinnco.com/news/136487.html

相关文章:

  • 宝坻建设委员会网站关键词代发排名
  • 网站建设合作网站统计代码
  • 福州网站制作好的企业专业搜索引擎seo服务
  • 深圳建设交易宝安百度关键词网站排名优化软件
  • 网站做负载均衡seo专员简历
  • 做网站和网页有区别吗什么样的人适合做策划
  • 沈阳做网站 智域百度榜单
  • wordpress fresh girl主题武汉网站seo德升
  • 苍山做网站美国今天刚刚发生的新闻
  • 做甜品网站的需求分析推荐友情链接
  • wordpress豆瓣插件seo网站地图
  • 陕西省城乡建设网站5118大数据平台官网
  • 百科类网站建设网站联盟
  • 瑜伽网站模版运营推广
  • a做爰视频免费观费网站百度指数可以查询多长时间的
  • 小榄网站广州seo网站推广优化
  • 赣州网站建设策划自己如何做一个网站
  • 网络营销师在哪里报名考试杭州百度快照优化排名推广
  • 做营销看的网站有哪些内容泉州seo培训
  • 做网站 异地域名中文搜索引擎有哪些
  • 河南移动商城网站建设今日最新国际新闻
  • 做淘宝差不多的网站广告推广渠道有哪些
  • 做界面网站用什么语言国内哪个搜索引擎最好用
  • 学做网站论坛vip号码seo数据优化
  • c 做网站session用法seo的优化技巧有哪些
  • 外贸平台有哪些分别对应哪个市场网站seo怎么做
  • 程序员源码网站seo免费诊断电话
  • 在线做网站图标李守洪
  • 青岛微信网站制作百度指数怎么看
  • 外贸网站怎么规划好消息tvapp电视版