In my previous post, I disussesed with the reader the concept of metaclass and its internals. and in following post one usage of the python metaclass is discussed. Today another use of the metaclass will be discussed.
As a note, this article is inspired from this original post from stackoverflow.
Problem with Python and Enumeration
python does not have internal keyword for enumeration, however, with the help of metaclass and dictionary, you can create your own enuemration.
let's see two impl, one is with a enum method by which you can further create any enumeration class.
First impl: enum meta method to create enumeration classes
The impl
# define the __metaclass__ function
def M_add_class_attr(attribs):
def foo(name, bases, dict_):
for v, k in attribs:
dict_[k] = v
return type(name, bases, dict_)
return foo
def enum(names):
class Foo(object):
__metaclass__ = M_add_class_attr(enumerate(names))
def __setattr__(self, name, value): # this make it readonly
raise NotImplementedError
return Foo()
the test code
import unittest
from MetaClass.EnumImpl1 import M_add_class_attr, enum
class Test(unittest.TestCase):
def test_enumImpl1_usage_example(self):
Animal = enum(['DOG', 'CAT'])
self.assertEqual(0, Animal.DOG)
self.assertEqual(1, Animal.CAT)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.test_enumImpl1_usage_example']
unittest.main()
Second impl: Enumeration Class with Symbol name
In this example, we are still going to create a animal enumeration class, and we want Animal to be a symbol rather than a variable where the client is able to reassign to.
the impl
class Animal(object):
values = ["Horse", "Dog", "Cat"]
# this is a very neat implementation
# where you define a __metaclass__ method that
# has the __getattr__ override to return the index of the constants
# that you have defined in class instance 'values'
class __metaclass__(type):
def __getattr__(self, name):
return self.values.index(name)
# the above is equivalent as writting as follow.
# class Animal_Metaclass(type):
# def __getattr__(self, name):
# return self.values.index(name)
# __metaclass__ = Animal_Metaclass
the test code
import unittest
from MetaClass.EnumImpl2 import Animal
class Test(unittest.TestCase):
# though the syntax is not well recognized by the Eclispe IDE, the function works
# perfectly well.
def test_enumImpl2_usage(self):
# print Animal.Horse
self.assertEqual(0, Animal.Horse)
self.assertEqual(1, Animal.Dog)
self.assertEqual(2, Animal.Cat)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.test_enumImpl2_usage']
unittest.main()
分享到:
相关推荐
资源分类:Python库 所属语言:Python 资源全名:django-enumeration-0.1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
linux-smart-enumeration-master.zip
赋值枚举python 最低成本k-best完美匹配。 分配问题代码是从借来的,因此受其影响。参考
首先,几个有用的one...linux-smart-enumeration 用于渗透测试和CTFLinux枚举工具 该项目的灵感来自并使用了许多测试。 不像LINENUM, lse尝试gradualy揭露根据其重要性从一个privesc点的信息。 它是什么? 该She
小小的列举进程程序可以把他列举出来,这个是源码
Possible use of ''xxx'' before definition -------------------在定义之前就使用了xxx(警告) Possibly incorrect assignment ----------------赋值可能不正确 Redeclaration of ''xxx'' -------------------...
Access-based Enumeration Windows Server 2003安装包
扫描和枚举自动化工具SEA安装打开终端通过输入命令来创建虚拟环境:python3 -m venv SEA 通过输入以下命令激活虚拟环境:source SEA / bin / activate 使用以下命令安装python库:pip3 install -r requirements.txt ...
PEP 487: Simpler customization of class creation PEP 487: Descriptor Protocol Enhancements PEP 519: Adding a file system path protocol PEP 495: Local Time Disambiguation PEP 529: Change Windows ...
信息收集工作整合成自动化脚本。该工具也是纯bash脚本,通用性较好,与LinPEAS类似,但收集的信息不如LinPEAS全面,但会关注一些LinPEAS没有关注到的信息,二者可结合使用,保证提权信息的全面性,防止遗漏。
Students are introduced to Python and the basics of programming in the context of such computational concepts and techniques as exhaustive enumeration, bisection search, and efficient approximation ...
进程、线程枚举,窗体组件调用例程的功能
网站: : 怎么跑用法nosqli-user-pass-enum.py [-h] [-u URL] [-up parameter] [-pp parameter] [-op parameters] [-ep parameter] [-sc character] [-m Method]例子python nosqli-user-pass-enum.py -u ...
如果要枚举'struct_enum.in'文件中指定的所有可能的结构(可在输入文件夹中找到其示例),请使用以下命令: enumeration-py -enum枚举子集如果系统中的布置总数很大,则可能不希望一一列举。 在这种情况下,可以...
Java 实例 - 使用 Enumeration 遍历 HashTable源代码+详细指导教程.zip
Intel(R)64 Architecture Processor Topology Enumeration
Slack Enumeration and Extraction Tool - extract sensitive information from a Slack Workspace
Ref: "Serial Comm: Use P/Invoke to Develop a .NET Base Class Library for Serial Device Communications" John Hind, MSDN Magazine, Oct 2002. V1.3 February 2004. LIBRARY CommBase.dll The library ...