元编程学习(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
posted @ 2018-03-11 22:54  绿色的麦田  阅读(273)  评论(0编辑  收藏  举报