123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- # from . import DataHandler #此时是导入文件
- from .DataStream import DataReadStream, day_stamp,open_hdf5
- from .DataStream import EMchPosmap as pos_map
- import numpy as np
- import re
- from collections import defaultdict
- __all__ = ['MerchantReader']
- import logging
- log = logging.getLogger('reader')
- class MerchantReader(DataReadStream):
- def __init__(self):
- file = '/var/www/html/data/stdata/merchant.hdf5'
- hfive = open_hdf5(file,False)
- super(MerchantReader,self).__init__(hfive)
- def __del__(self):
- self.close()
- super(MerchantReader, self).__del__()
- pass
- def tuple_path(self, day: int, mchids: set = None, card_types: set = None, spec: int = None):
- def parse(path):
- items = re.split(r'/', path)
- (_prifix, _version, today, mchid, card_type, spec) = items
- return int(mchid), int(card_type), int(spec)
- tuples = defaultdict(list)
- pathes = self.datasets(day)
- for path in pathes:
- _mchid, _card_type, _spec = parse(path)
- tuples[_mchid].append((_card_type, _spec))
- def name_filter(tuples, mchids):
- all = True if mchids is None or len(mchids) == 0 else False
- if all:
- return tuples
- else:
- result = defaultdict(list)
- for mchid, tup in tuples.items():
- if mchid in mchids:
- result[mchid] = tup
- return result
- tuples = name_filter(tuples, mchids)
- def typespec_filter(tuples, card_types, spec):
- result = defaultdict(list)
- for name, ls in tuples.items():
- for tup in ls:
- _card_type, _spec = tup
- if _card_type in card_types:
- if spec is None or _spec == spec:
- result[name].append((_card_type, _spec))
- return result
- tuples = typespec_filter(tuples,card_types,spec)
- return tuples
- def many_tuple_path(self, days: list, mchids: set = None, card_types: set = None, spec: int = None):
- def merge(l,r):
- for mchid,ls in l.items():
- if mchid in r:
- r[mchid].union(set(ls))
- else:
- r[mchid] = set(ls)
- return r
- all = defaultdict(set)
- for day in days:
- tuples = self.tuple_path(day, mchids, card_types, spec)
- all = merge(tuples,all)
- return all
- def init_data(self, days):
- dim = pos_map.dim()
- return np.zeros((dim, 86400 * days))
- def read(self, day_stamp, mchid, card_type, spec):
- path = f'/{self._version}/{day_stamp}/{mchid}/{card_type}/{spec}'
- hfive = self.file
- if path in hfive:
- return hfive[path]
- else:
- return None
- pass
|