嵌套类的简单介绍

在看 idarling 源码中,经常出现如下代码:

import sys

import ida_funcs

import ida_kernwin

from PyQt5.QtCore import ( # noqa: I202

QAbstractItemModel,

QModelIndex,

QObject,

Qt,

)

from PyQt5.QtGui import QColor

from PyQt5.QtWidgets import QStyledItemDelegate, QWidget

import sip

from .widget import StatusWidget

if sys.version_info > (3,):

long = int

class Painter(QObject):

class ProxyItemDelegate(QStyledItemDelegate):

def __init__(self, delegate, model, parent=None):

super(Painter.ProxyItemDelegate, self).__init__(parent)

self._delegate = delegate

self._model = model

def paint(self, painter, option, index):

index = self._model.index(index.row(), index.column())

self._delegate.paint(painter, option, index)

class ProxyItemModel(QAbstractItemModel):

def __init__(self, model, plugin, parent=None):

super(Painter.ProxyItemModel, self).__init__(parent)

self._model = model

self._plugin = plugin

def index(self, row, column, parent=QModelIndex()):

return self.createIndex(row, column)

def parent(self, index):

index = self._model.index(index.row(), index.column())

return self._model.parent(index)

def rowCount(self): # noqa: N802

return self._model.rowCount()

def columnCount(self): # noqa: N802

return self._model.columnCount()

def data(self, index, role=Qt.DisplayRole):

# Check if disabled by the user

cursors = self._plugin.config["cursors"]

if role == Qt.BackgroundRole and cursors["funcs"]:

func_ea = int(index.sibling(index.row(), 2).data(), 16)

func = ida_funcs.get_func(func_ea)

for user in self._plugin.core.get_users().values():

if ida_funcs.func_contains(func, user["ea"]):

r, g, b = StatusWidget.ida_to_python(user["color"])

return QColor(StatusWidget.python_to_qt(r, g, b))

index = self._model.index(index.row(), index.column())

return self._model.data(index, role)

def __init__(self, plugin):

super(Painter, self).__init__()

self._plugin = plugin

self._ida_nav_colorizer = None

self._nbytes = 0

def nav_colorizer(self, ea, nbytes):

"""This is the custom nav colorizer used by the painter."""

self._nbytes = nbytes

# There is a bug in IDA: with a huge number of segments, all the navbar

# is colored with the user color. This will be resolved in IDA 7.2.

cursors = self._plugin.config["cursors"]

if cursors["navbar"]:

for user in self._plugin.core.get_users().values():

# Cursor color

if ea - nbytes * 2 <= user["ea"] <= ea + nbytes * 2:

return long(user["color"])

# Cursor borders

if ea - nbytes * 4 <= user["ea"] <= ea + nbytes * 4:

return long(0)

orig = ida_kernwin.call_nav_colorizer(

self._ida_nav_colorizer, ea, nbytes

)

return long(orig)

def ready_to_run(self):

# The default nav colorized can only be recovered once!

ida_nav_colorizer = ida_kernwin.set_nav_colorizer(self.nav_colorizer)

if ida_nav_colorizer is not None:

self._ida_nav_colorizer = ida_nav_colorizer

self.refresh()

def get_ea_hint(self, ea):

cursors = self._plugin.config["cursors"]

if not cursors["navbar"]:

return None

for name, user in self._plugin.core.get_users().items():

start_ea = user["ea"] - self._nbytes * 4

end_ea = user["ea"] + self._nbytes * 4

# Check if the navbar range contains the user's address

if start_ea <= ea <= end_ea:

return str(name)

def get_bg_color(self, ea):

# Check if disabled by the user

cursors = self._plugin.config["cursors"]

if not cursors["disasm"]:

return None

for user in self._plugin.core.get_users().values():

if ea == user["ea"]:

return user["color"]

return None

def widget_visible(self, twidget):

widget = sip.wrapinstance(long(twidget), QWidget)

if widget.windowTitle() != "Functions window":

return

table = widget.layout().itemAt(0).widget()

# Replace the table's item delegate

model = Painter.ProxyItemModel(table.model(), self._plugin, self)

old_deleg = table.itemDelegate()

new_deleg = Painter.ProxyItemDelegate(old_deleg, model, self)

table.setItemDelegate(new_deleg)

def refresh(self):

ida_kernwin.refresh_navband(True)

ida_kernwin.request_refresh(ida_kernwin.IWID_DISASMS)

ida_kernwin.request_refresh(ida_kernwin.IWID_FUNCS)

能够看到 类中 又定义了 类 ,这种情况我们称之为嵌套类 。给一个简单 demo 来认识嵌套类 。

#!/usr/bin/env python

import os, sys

class parent:

def __init__(self):

self.name = 'parent'

def getName(self):

print self.name

class child:

def __init__(self):

self.name = 'child'

def getName(self):

print self.name

if __name__ == '__main__':

child = parent.child()

child.getName()

zhangji16@zhangji16vm:/tmp$ python tmp2.py

child

python中类的嵌套_python 中的嵌套类相关推荐

  1. python中类怎么理解_Python中的列表理解

    python中类怎么理解 In order to create a list, a most obvious and remembered solution is to use a for-loop. ...

  2. python写一个类方法_python中如何写类

    类的定义 Python中,定义类是通过class关键字,例如我们定义一个存储学生信息的类: class Student(object): pass class后面紧接着是类名,即Student,类名通 ...

  3. python中类的嵌套_python中的嵌套类 | 学步园

    在.NET和JAVA语言中看到过嵌套类的实现,作为外部类一个局部工具还是很有用的,今天在python也看到了很不错支持一下.动态语言中很好的嵌套类的实现,应该说嵌套类解决设计问题同时简化了程序,值得学 ...

  4. python中类的用法_Python中的类和方法使用举例

    成员变量 对象的创建 创建对象的过程称之为实例化,当一个对象被创建后,包含三个方面的特性对象聚丙属性和方法, 句柄用于区分不同的对象, 对象的属性和方法,与类中的成员变量和成员函数对应, obj = ...

  5. python中类的构成_Python中类型关系和继承关系实例详解

    本文详细介绍了Python中类型关系和继承关系.分享给大家供大家参考.具体分析如下: 如果一个对象A持有另一个对象B的ID,那么检索到A之后就可以检索到B,我们就说存在一个A到B的导航.这种导航关系使 ...

  6. python中类的使用_Python第九课-使用 类(class)创造新的数据类型!

    前言 之前几课里边我们接触到了很多内置数据类型,字符串.列表.字典等等. 难道一个编程语言里只能使用定义好的数据类型吗?如果他们并不非常适合现有数据的表达呢?我们可以创造一个新的数据类型来使用吗? 当 ...

  7. python算法和数据结构_Python中的数据结构和算法

    python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...

  8. python嵌套列表操作方法_python中多层嵌套列表的拆分方法

    场景:有一个多层嵌套的列表如:[[23],[3,3],[22,22],1,123,[[123,a],2]] 拆分成: def splitlist(list): ''' 现有一个列表,里面元素包括 数字 ...

  9. python if else 嵌套格式_python中if嵌套命令实例讲解

    一.嵌套命令计算机执行的顺序 缩进相同的命令处于同一个等级,第一步,计算机就要按顺序一条一条地执行命令. 1.先给score赋值: 2.因为if和else是只能留一个的互斥关系,if和else下的代码 ...

最新文章

  1. Python之目录结构
  2. Spoken English(021)
  3. Android eclipse导入项目后出现Unable to resolve target #39;android-17#39;解决方法
  4. 基本数据类型及其包装类(一)
  5. Oracle 11g EM安全证书问题无法访问的解决办法
  6. python提取身份证信息_Python3 简单抓取身份证地址信息
  7. (产品分析)KFC肯德基APP分析报告
  8. linux中iptable和firewalld详解
  9. autojs开发的ip地址查询工具开源代码免费分享
  10. 微信公众号编辑模式下推送消息
  11. CICD概念 k8s DevOps
  12. Windows 10调整色温显示器
  13. vlc搭建流媒体转发服务器
  14. 联机系统的服务器,远程联机服务器系统
  15. 典型相关分析(Matlab实现函数)
  16. git 拉取远程分支报错(fatal: ''xxx is not a commit and a branch '' cannot be created from it)
  17. IT经理的两条职业路做管理还是管理咨询
  18. .net 脱壳及编译
  19. 网络新宠儿EyeOS和YouOS Web版操作系统
  20. php简转繁体,使用PHP实现繁体与简体互转

热门文章

  1. java spring 多数据源_Spring多数据源解决方案
  2. ACM-ICPC 2018 南京赛区网络预赛 B. The writing on the wall
  3. linux 运行springboot 项目 (后台运行,并且打印实时日志)
  4. JDK8新特性(七)之Stream流的count()、filter()、limit()、skip()方法
  5. java arraylist与数组转换_java中String,数组,ArrayList三者之间的转换
  6. django项目转pyc_Python自动化运维系列:Django搭建小博客
  7. linux 重定向_Linux视频教程分享,零基础在家你也可以学的会
  8. 快速批量一个半小时插入1亿条数据到mysql
  9. redis3.0搭建分布式集群
  10. 湖南师范大学数学与计算机学院郭水霞,湖南师范大学数学与计算机科学学院2013备考手册...