data_logic

“”"
Created on Tue Jul 2 14:18:04 2019

@author: zoe
“”"

import time

class Quantity:
def init(self, detail_list):
self.component = detail_list[6]
self.vendor = detail_list[0]
self.config = detail_list[1]
self.OEM_PN = detail_list[2]
self.Color = detail_list[3]
self.wifi = detail_list[4]
self.cell = detail_list[5]
# self.cell = detail_list[7]
self.bonded = ‘True’ if detail_list[7] else ‘False’
self.QOH = detail_list[8] if not isinstance(detail_list[7], str) else 0.0
self.ETAs = detail_list[9]
try:
self.Build_Date = detail_list[10]
except:
self.Build_Date = ‘’
self.qty = self.QOH

def Date_format_transform(self,date):
    """将 月/日/年 类格式的日期转换成 20190506 类的int格式"""
    date = date.strip(' ')
    date_list = date.split('/')
    month = date_list[0]
    try:
        day = date_list[1]
    except:
        return
    if len(month) == 1:
        month = '0' + month
    if len(day) == 1:
        day = '0' + day

    date_str = str(date_list[2])+'/'+str(month)+'/'+str(day)
        # int(date_list[2] + month + day)
    return date_str

def Date_comparison(self,date1,date2):
    if date1 == date2:
        return '='
    elif date1 > date2:
        return '>'
    else:
        return '<'

def calculate_quantity(self,date):
    # print('start',self.qty)
    ETAs_Quantity = 0
    if self.ETAs:
        for i in self.ETAs.keys():
            # print(date,i)
            if not self.Date_comparison(date,i) == '<':
                ETAs_Quantity += self.ETAs[i]
    Build_Date_Quantity = 0

    if self.Build_Date:
        for i in self.Build_Date.keys():
            date_value = str(self.Date_format_transform(i))
           # print(date,date_value)
            if not self.Date_comparison(date,date_value) == '<':
                Build_Date_Quantity += self.Build_Date[i]

    self.qty = self.QOH + ETAs_Quantity - Build_Date_Quantity
    # dict_aggregate = {'Component':{},'Vendor':{},'Config':{},'OEM_PN':{},'Color':{},'WIFI':{},'CELL':{},'Quantity':{}}

class Dict_object_manage:
“”“基础属性:data(数据分析后的数据),date(更新日期),filter_dict(要查询的数据),
内置属性:(仅可查看)base_object(所有基础数据),Relevant_attributes(筛选后的相关属性) ,quantity(筛选后的数量)filter_object(筛选后的对象),
函数:object_create 根据数据分析的数据创建数据对象组 ,filter_data filter_dict赋值后根据filter_data查询符合条件的数据”""

def __getattr__(self,attr):
    if attr == '__date':
        return None
    elif attr == '__base_object' or attr=='__filter_object' or attr=='__data':
        return []
    elif attr == '__dict_aggregate' or attr=='__Relevant_attributes' or attr=='__dict_key' or attr=='__filter_dict':
        return {}
    elif attr == '__quantity':
        return 0
    else:
        return attr

@property
def data(self):
    if self.__data == '_Dict_object_manage__data':
        return []
    else:
        return self.__data

@data.setter
def data(self,data):
    # if self.__data == '_Dict_object_manage__data':
    #     self.__data = data
    #     self.__object_create
    #     self.__base_object_dict = {str(i): Quantity(self.__data[i]) for i in range(len(self.__data))}
    # elif self.__data != data:
    self.__data = data
    self.__object_create
    self.__base_object_dict = {str(i): Quantity(self.__data[i]) for i in range(len(self.__data))}
    self.__component_quantity_cal
    self.__component_object




@property
def date(self):
    if self.__date == '_Dict_object_manage__date':
        return None
    else:
        return self.__date

@date.setter
def date(self,date):
    if self.__data == '_Dict_object_manage__data':
        error_imformation = '请先导入data基础数据,不然未有对应数据导入date也无法运算,date格式如:2019/05/07'
        print(error_imformation)
        return error_imformation
    # if self.__date == '_Dict_object_manage__date':
    #     self.__date = date
    #     self.__filter_data

    # elif self.__date != date:
    self.__date = date
    for key,value in self.__base_object_dict.items():
        if not key =='0':
            self.__base_object_dict[key].calculate_quantity(self.__date)
    self.__object_create
    self.__filter_data
    self.__component_quantity_cal
    self.__component_object


@property
def filter_dict(self):
    if self.__filter_dict == '_Dict_object_manage__filter_dict':
        return []
    else:
        return self.__filter_dict

@filter_dict.setter
def filter_dict(self,filter_dict):
    if self.__data == '_Dict_object_manage__data':
        error_imformation = '请先导入data基础数据,基于基础数据运算产生其他数据,否则其他数据也为空'
        print(error_imformation)
        return error_imformation
    if  self.__date == '_Dict_object_manage__date':
        print('请先导入相应日期,否则只能统计其QOH项的数量而不能统计其相应的ETA和Build Date项的数据,导入方法 object.date = date')

    # if self.__filter_dict == '_Dict_object_manage__filter_dict':
    #     if 'Bonded' in filter_dict:
    #         if filter_dict['Bonded']:
    #             filter_dict['Bonded'] = 'True'
    #         else:
    #             filter_dict['Bonded'] = 'False'
    #     self.__filter_dict = filter_dict
    #     self.__filter_data
    # if self.__filter_dict != filter_dict or self.__filter_dict == '_Dict_object_manage__filter_dict':
    if 'Bonded' in filter_dict:
        if filter_dict['Bonded']:
            filter_dict['Bonded'] = 'True'
        else:
            filter_dict['Bonded'] = 'False'
    self.__filter_dict = filter_dict
    self.__filter_data




@property
def base_object(self):
    if self.__base_object == '_Dict_object_manage__base_object':
        return []
    else:
        return self.__base_object

@property
def dict_aggregate(self):
    if self.__dict_aggregate == '_Dict_object_manage__dict_aggregate':
        return {}
    else:
        return self.__dict_aggregate

@property
def Relevant_attributes(self):
    if self.__Relevant_attributes == '_Dict_object_manage__Relevant_attributes':
        return {}
    else:
        return self.__Relevant_attributes

@property
def quantity(self):
    if self.__quantity == '_Dict_object_manage__quantity':
        return 0
    else:
        return self.__quantity

@property
def filter_object(self):
    if self.__filter_object == '_Dict_object_manage__filter_object':
        return []
    else:
        return self.__filter_object

@property
def dict_key(self):
    if self.__dict_key == '_Dict_object_manage__dict_key':
        return {}
    else:
        return self.__dict_key

@property
def component_quantity(self):
    if self.__component_quantity == '_Dict_object_manage__component_quantity':
        return {}
    else:
        return self.__component_quantity

@property
def component_object_atttribute(self):
    if self.__component_object_atttribute == '_Dict_object_manage__component_object_atttribute':
        return {}
    else:
        return self.__component_object_atttribute

@property
def __component_quantity_cal(self):
    self.__component_quantity = {}
    for i in ['True','False']:
        dict_component = {}
        for key in self.__dict_key['Component']:
            filter_dict = {'Bonded':i,'Component':key}
            self.__filter_dict = filter_dict
            self.__filter_data
            if self.__Relevant_attributes:
                quantity = self.__Relevant_attributes['quantity']
            else:
                quantity = 0
            dict_component[key] = quantity
        if i == 'True':
            self.__component_quantity['Bonded'] = dict_component
        else:
            self.__component_quantity['NonBonded'] = dict_component


@property
def __component_object(self):
    self.__component_object_atttribute = {}
    for bonded_value in ['True','False']:
        dict_component = {}
        for component_value in self.__dict_key['Component']:
            self.__filter_dict = {'Bonded':bonded_value,'Component':component_value}
            self.__filter_data
            dict_component.update({component_value:self.__filter_object})
        if bonded_value == 'True':
            self.__component_object_atttribute.update({'Bonded':dict_component})
        else:
            self.__component_object_atttribute.update({'NonBonded': dict_component})





@property
def __filter_data(self):
    """导入的字典如{'Component':'housing','Vendor':'RT','Config:'RP1-CCF-1-DOE2-ID'}这种格式"""
    self.__filter_object = []
    if len(self.__filter_dict) == 1:
        self.__filter_object = self.__dict_aggregate.get(list(self.filter_dict.keys())[0]).get(list(self.filter_dict.values())[0])
    elif len(self.__filter_dict) > 1:
        for key,value in self.__filter_dict.items():
            if self.__filter_object:
                self.__filter_object = list(set(self.__filter_object) & set(self.__dict_aggregate[key][value]))
            else:
                self.__filter_object = self.__dict_aggregate[key][value]

    component_list,vendor_list,config_list,OEM_PN_list,Color_list,wifi_list,cell_list,bonded_list = [],[],[],[],[],[],[],[]
    self.__quantity = 0
    if self.__filter_object:
        for i in self.__filter_object:
            component_list.append(i.component)
            vendor_list.append(i.vendor)
            config_list.append(i.config)
            OEM_PN_list.append(i.OEM_PN)
            Color_list.append(i.Color)
            wifi_list.append(i.wifi)
            cell_list.append(i.cell)
            bonded_list.append(i.bonded)
            if i.qty:
                self.__quantity+=i.qty

        self.__Relevant_attributes = {'Component':[i for i in set(component_list) if i],
                                      'Vendor':[i for i in set(vendor_list) if i],
                                      'Config':[i for i in set(config_list) if i],
                                      'OEM PN':[i for i in set(OEM_PN_list) if i],
                                      'Color':[i for i in set(Color_list) if i],
                                      'Wifi Per':[i for i in set(wifi_list) if i],
                                      'Cell Per':[i for i in set(cell_list) if i],
                                      'Bonded':[i for i in set(bonded_list) if i],
                                      'quantity':self.__quantity}
    else:
        self.__Relevant_attributes = {}


def __columns_with_dict(self,judge_str,data_columns,base_dict,dict_aggregate):
    list_del = list(set(data_columns))
    dict_aggregate_branch = {i:[] for i in list_del}
    # if judge_str == 'Bonded':
    #     dict_aggregate_branch = {'False':[],'True':[]}
    if base_dict['0'].component == 'Component':
        index_m = 1
    else:
        index_m = 0

    for index,value in enumerate(data_columns):
        a=index
        index += index_m
        print('component',base_dict[str(index)].component)
        if judge_str == 'OEM PN' :
            if a==0:
                dict_aggregate_branch[value].append(base_dict[str(index)])
        elif judge_str == 'Cell Per' or judge_str == 'Wifi Per':
            if isinstance(value,float):
                if value>0:
                    dict_aggregate_branch[value].append(base_dict[str(index)])

        # elif judge_str == 'Bonded':
        #     # if value:
        #     if value != base_dict[str(index)].bonded:
        #     dict_aggregate_branch[value].append(base_dict[str(index)])
            # else:
            #     dict_aggregate_branch['False'].append(base_dict[str(index)])


        else:
            if value:
                dict_aggregate_branch[value].append(base_dict[str(index)])

    dict_aggregate_branch = {key:value for key,value in dict_aggregate_branch.items() if value}
    dict_aggregate[judge_str]=dict_aggregate_branch



    return dict_aggregate

@property
def __object_create(self):
    """根据导入的数据初始化数据模块"""
    title = self.__data.pop(0)
    data_t = list(map(list,zip(*self.__data)))

    if self.__base_object_dict == '_Dict_object_manage__base_object_dict':
        self.__base_object_dict = {str(i): Quantity(self.__data[i]) for i in range(len(self.__data))}
    self.__base_object = list(self.__base_object_dict.values())
    self.__dict_aggregate = {}
    for index,value in enumerate(title[:8]):
        if value != 'Bonded':
            self.__dict_aggregate = self.__columns_with_dict(value, data_t[index], self.__base_object_dict,
                                                             self.__dict_aggregate)
        else:
            dict_aggregate_branch = {'False': [], 'True': []}
            for i in self.__base_object_dict.values():
                dict_aggregate_branch[i.bonded].append(i)
            self.__dict_aggregate['Bonded'] = dict_aggregate_branch



    self.__dict_key = {i:list(self.__dict_aggregate[i].keys()) for i in self.__dict_aggregate.keys()}

    self.__data.insert(0,title)

def object_transform_dict(self, dict_object):
    dict_json = {key:[] for key,value in dict_object.items() if value}
    for key,value in dict_object.items():
        if value:
            for i in value:
                # print(i)
                if isinstance(i.wifi,float):
                    if i.wifi > 0:
                        wifi = True
                    else:
                        wifi = False
                else:
                    wifi=False
                if isinstance(i.cell,float):
                    if i.cell > 0:
                        cell = True
                    else:
                        cell = False
                else:
                    cell = False

                if (wifi and cell)or not(wifi or cell):
                    wifi_or_cell = 'ALL'
                elif wifi and not cell:
                    wifi_or_cell = 'WIFI'
                elif not wifi and cell:
                    wifi_or_cell = 'CELL'
                if i.bonded == 'False':
                    Bonded = False
                else:
                    Bonded =True
                dict_json[key].append({'bonded':Bonded,
                                       'Conponent':i.component,
                                       'name':i.component,
                                       'Vendor':i.vendor,
                                       'Config':i.config,
                                       'Color':i.Color,
                                       'WIFI_OR_CELL':wifi_or_cell,
                                       'Quantity':i.qty,
                                       'ETA_dict':i.ETAs,
                                       'Build_date_dict':i.Build_Date
                                       })
    return dict_json
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值