• 技术文章 >Python技术 >Python高级

    Python如何利用动态属性处理JSON数据源

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-06-29 10:52:38原创2608

    利用动态属性处理JSON数据源

    属性:在Python中,数据的属性和处理数据的方法统称属性。

    元编程:用元类进行编程,元类→类→对象,元类比类更抽象,生成类的类。

    1、使用动态属性访问JSON类数据

    第一版:利用json.load(fp)审查数据

    from urllib.request import urlopen
    import warnings
    import os
    import json
     
    URL = 'http://www.oreilly.com/pub/sc/osconfeed'
    JSON = 'data/osconfeed.json'
     
    def load():
      if not os.path.exists(JSON):
        msg = 'downloading {} to {}'.format(URL, JSON)
        warnings.warn(msg) #如果需要下载就发出提醒。
        with urlopen(URL) as remote, open(JSON, 'wb') as local: #在with语句中使用两个上下文管理器分别用于读取和保存远程文件。
          local.write(remote.read())
      with open(JSON) as fp:
        return json.load(fp)#json.load函数解析JSON文件,返回Python原生对象。

    第二版:使用动态属性访问JSON类数据

    第一版查阅深层数据的格式比较冗长,例如feed'Schedule'40,我们希望在读取属性上采用feed.Schedule.events[40].name这类方式来改进。并且第二版的类能递归,自动处理嵌套的映射和列表。

    from collections import abc
     
    class FronenJSON():
      def __init__(self,mapping):
        self.__data=dict(mapping)#创建副本,同时确保处理的是字典。
         
      def __getattr__(self, name):#仅当没有指定名称的属性才调用__getattr__方法。
        if hasattr(self,name):
          return getattr(self.__data,name)
        else:
          return FronenJSON.build(self.__data[name])
       
      @classmethod 
      def __build__(cls,obj):
        if isinstance(obj,abc.Mapping):#判断obj是否是映射。
          return cls(obj)#创建FrozenJSON对象。
        elif isinstance(obj,abc.MutableSequence):
          return [cls.build(item) for item in obj]#递归调用.build()方法,构建一个列表。
        else:#既不是字典也不是列表,则返回元素本身。
          return obj

    分析: FronenJSON类的关键是__getattr__方法。仅当无法使用常规的方式获取属性(即在实例、类或超类中找不到指定的属性),解释器才会调用特殊的__getattr__方法。

    相关推荐:《Python视频教程

    2、处理无效属性名

    在Python中,由于关键字被保留,名称为关键字的属性是无效的。因此需要对第二版中的__init__进行改进:

    def __init__(self,mapping):
      self.__data={}
      for key,value in mapping.items():
        if keyword.iskeyword(key):
          key+='_'#与Python关键字重复的key在尾部加上下划线。
        self.__data[key]=value

    3、使用特殊方法__new__

    第三版:使用__new__构造方法把一个类转换成一个灵活的对象工厂函数。

    from collections import abc
     
    class FronenJSON():
      def __new__(cls, arg): # __new__是类方法,第一个参数是类本身cls。
        if isinstance(arg, abc.Mapping):
          return super().__new__(cls) #委托给超类object基类的__new__方法处理。
        elif isinstance(arg, abc.MutableSequence): # 余下方法与原先的build方法一致。
          return [cls(item) for item in arg]
        else:
          return arg
      
       def __init__(self,mapping):
        self.__data={}
        for key,value in mapping.items():
          if keyword.iskeyword(key):
            key+='_'
          self.__data[key]=value 
     
      def __getattr__(self, name):
        if hasattr(self,name):
          return getattr(self.__data,name)
        else:
          return FronenJSON(self.__data[name])
    专题推荐:python json 动态属性
    品易云
    上一篇:九步就可入门Python装饰器 下一篇:Python中处理属性的重要属性和函数是什么

    相关文章推荐

    • 什么是Python中的闭包• python中for循环的底层实现• 九步就可入门Python装饰器

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网