zoukankan      html  css  js  c++  java
  • 元编程学习(1)

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from urllib.request import urlopen
    import warnings
    import os
    import json
    URL = 'http://bangth.com:8080/osconfeed.json'
    JSON = '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:
                local.write(remote.read())
        with open(JSON) as fp:
            return json.load(fp)
        
    feed = load()
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from collections import abc
    
    class FrozenJSON:
        """一个只读接口,使用属性表示法访问JSON类对象
        """
        def __init__(self, mapping):
            self.__data = dict(mapping)
            
        def __getattr__(self, name):
            if hasattr(self.__data, name):
                return getattr(self.__data, name)
            else:
                return FrozenJSON.build(self.__data[name])
            
        @classmethod
        def build(cls, obj):
            if isinstance(obj, abc.Mapping):
                return cls(obj)
            elif isinstance(obj, abc.MutableSequence):
                return [cls.build(item) for item in obj]
            else:
                return obj
  • 相关阅读:
    c-复习基础
    java-根据起止IP获取IP段集合
    java-随机数
    java-数组
    TypeSafe Config使用
    日志手段
    git 常用命令
    看门狗
    容器HashSet原理(学习)
    容器Vector原理(学习)
  • 原文地址:https://www.cnblogs.com/luhouxiang/p/8546874.html
Copyright © 2011-2022 走看看