#!/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

相关文章:

  • 2021-08-27
  • 2022-03-11
  • 2022-12-23
  • 2021-05-10
  • 2021-12-11
  • 2021-11-18
  • 2021-11-12
  • 2021-12-29
猜你喜欢
  • 2021-12-03
  • 2021-12-30
  • 2021-12-11
  • 2021-07-13
  • 2021-12-01
  • 2021-07-27
相关资源
相似解决方案