【问题标题】:Subclassing ctypes - Python子类化 ctypes - Python
【发布时间】:2009-05-13 03:36:33
【问题描述】:

这是我在互联网上找到的一些代码。我不确定它是如何使用的。我只是用枚举键/值填充 members 并且它可以工作,但我很好奇这个元类的全部内容。我假设它与 ctypes 有关,但我找不到太多关于子类化 ctypes 的信息。我知道 EnumerationType 并没有像我使用 Enumeration 那样做任何事情。

from ctypes import *

class EnumerationType(type(c_uint)):  
    def __new__(metacls, name, bases, dict):  
        if not "_members_" in dict:  
            _members_ = {}  
            for key,value in dict.items():  
                if not key.startswith("_"):  
                    _members_[key] = value  
            dict["_members_"] = _members_  
        cls = type(c_uint).__new__(metacls, name, bases, dict)  
        for key,value in cls._members_.items():  
            globals()[key] = value  
        return cls  

    def __contains__(self, value):
        return value in self._members_.values()

    def __repr__(self):
        return "<Enumeration %s>" % self.__name__

class Enumeration(c_uint):
    __metaclass__ = EnumerationType
    _members_ = {}
    def __init__(self, value):
        for k,v in self._members_.items():
            if v == value:
                self.name = k
                break
        else:
            raise ValueError("No enumeration member with value %r" % value)
        c_uint.__init__(self, value)


    @classmethod
    def from_param(cls, param):
        if isinstance(param, Enumeration):
            if param.__class__ != cls:
                raise ValueError("Cannot mix enumeration members")
            else:
                return param
        else:
            return cls(param)

    def __repr__(self):
        return "<member %s=%d of %r>" % (self.name, self.value, self.__class__)

And an enumeration probably done the wrong way.  

class TOKEN(Enumeration):
    _members_ = {'T_UNDEF':0, 'T_NAME':1, 'T_NUMBER':2, 'T_STRING':3, 'T_OPERATOR':4, 'T_VARIABLE':5, 'T_FUNCTION':6}

【问题讨论】:

  • 我认为您想添加更多缩进,以便您的代码将被格式化为代码。试试文本区域上方的“101/010”按钮。

标签: python ctypes


【解决方案1】:

元类是用于创建类的类。可以这样想:所有对象都有一个类,一个类也是一个对象,因此,一个类可以有一个类是有道理的。

http://www.ibm.com/developerworks/linux/library/l-pymeta.html

要了解这是做什么的,您可以查看代码中的几个点。

 _members_ = {'T_UNDEF':0, 'T_NAME':1, 'T_NUMBER':2, 'T_STRING':3, 'T_OPERATOR':4, 'T_VARIABLE':5, 'T_FUNCTION':6}

globals()[key] = value

这里它获取字典中定义的每个键:“T_UNDEF”“T_NUMBER”,并使其在全局字典中可用。

def __init__(self, value):
    for k,v in self._members_.items():
        if v == value:
            self.name = k
            break

每当您创建枚举的实例时,它都会在您初始化类时检查“值”是否在允许的枚举名称列表中。找到值后,将字符串名称设置为 self.name。

c_uint.__init__(self, value)

这是将“ctypes value”设置为实际 c 无符号整数的实际行。

【讨论】:

  • 谢谢。那篇文章解释了一切。我猜你每天都会学到一些新东西。
【解决方案2】:

这确实是一个奇怪的类。

您使用它的方式是正确的,虽然另一种方式是:

class TOKEN(Enumeration):
    T_UNDEF    = 0
    T_NAME     = 1
    T_NUMBER   = 2
    T_STRING   = 3
    T_OPERATOR = 4
    T_VARIABLE = 5
    T_FUNCTION = 6

(这就是__new__ 中的前 6 行)

那么你可以这样使用它:

>>> TOKEN
<Enumeration TOKEN>
>>> TOKEN(T_NAME)
<member T_NAME=1 of <Enumeration TOKEN>>
>>> T_NAME in TOKEN
True
>>> TOKEN(1).name
'T_NAME'

from_param 方法似乎是为了方便,用于编写接受 int 或 Enumeration 对象的方法。不确定这是否真的是它的目的。

我认为这个类应该在使用 c 样式枚举的外部 API 时使用,但它看起来需要做很多工作却收效甚微。

【讨论】:

  • 嗯,它为我的 LCD 项目获得了一个快速表达式评估器,其中包含一堆与 Linux 相关的插件。 :) ctypes 模块没有明确提供表示枚举的方法,因此代码。如果没有我找到的元类,也可以这样做。那只是为了提供全局变量。我使用 Python 已经 5 年多了,但我从来不知道元类。当我看到它时,我以为它是特定于 ctypes 的。
猜你喜欢
  • 2010-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
相关资源
最近更新 更多