123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- # coding=utf-8
- """ Ecpm 底价配置文件 """
- import io
- import ast
- from common.country_code_utils import CountryCodeUtils
- def _is_country_line(line):
- """ 判断一行是否是国家
- 用 逗号 分隔出来的个数进行判断
- :param line: string
- :return: bool
- """
- length = len(line.split(","))
- if length == 5 or length == 8:
- return False
- if line == "":
- return False
- return True
- def _is_price_line(line):
- """ 判断一行是否是价格行
- 用 逗号 分隔出来的个数进行判断
- :param line: string
- :return: bool
- """
- length = len(line.split(","))
- if length == 5 or length == 8:
- return True
- return False
- #
- # 格式解析文本文件 至 字典
- #
- #
- # 文本文件格式
- # ---------------------------
- # 日本
- # 38, 63, 127, 205, 337
- # 牙买加
- # 10, 18, 40, 67, 112
- # ---------------------------
- #
- # 生成字典 -> dic {
- # '日本': [38, 95, 205, 337], '牙买加': [10, 29, 67, 112]
- # }
- def _parse_country_price_text(config_txt_path, isBanner):
- """ 解析文本配置文件
- 如果是Banner的话,需要用平均数合并(2,3 | 3,4)
- 文本格式
- 日本
- 38, 63, 127, 205, 337
- 牙买加
- 10, 18, 40, 67, 112
- :param config_txt_path: string
- :param isBanner: bool 是否banner
- :return: dic key:国家名 value: ecpm(100*美金)
- """
- dic = {}
- with io.open(config_txt_path, "r") as f:
- lines = f.readlines()
- for i in range(0, len(lines)):
- line = lines[i]
- line = line.strip()
- line = line.encode(encoding='utf-8')
- if _is_country_line(line):
- # 正常读到一个国家
- country = line
- # 下面一行应该紧跟的是该国家的价格配置
- price_list_line = lines[i + 1]
- price_list_line = price_list_line.strip()
- if _is_price_line(price_list_line):
- price_list = ast.literal_eval("[{}]".format(price_list_line))
- if isBanner:
- # 特殊处理,第2个和第3个取平均数
- price1 = (price_list[1] + price_list[2]) / 2
- price2 = (price_list[2] + price_list[3]) / 2
- price_list.pop(1)
- price_list.pop(1)
- price_list.pop(1)
- price_list.insert(1, price1)
- price_list.insert(2, price2)
- dic[country] = price_list
- return dic
- class AdEcpmConfigTxt(object):
- """ Ecpm 底价配置文件
- country_name_dic: 国家名 配置内容
- country_code_dic: 国家码 配置内容
- country_name_arr: 国家名
- country_code_arr: 国家码
- country_codes_arr: 总国家总个数
- price_index_count: 价格总个数
- """
- def __init__(self):
- self.country_name_dic = {}
- self.country_code_dic = {}
- self.country_name_arr = []
- self.country_code_arr = []
- self.country_count = 0
- self.price_index_count = 0
- def read(self, path, is_banner=False):
- self.country_name_dic = _parse_country_price_text(path, is_banner)
- self.country_count = len(self.country_name_dic)
- for country_name in self.country_name_dic:
- price_arr = self.country_name_dic[country_name]
- # 价格是100倍的美金存储的,转一下
- price_arr = [p / 100.0 for p in price_arr]
- if not self.country_name_arr.__contains__(country_name):
- self.country_name_arr.append(country_name)
- country_code = CountryCodeUtils.get_country_code(country_name)
- if not self.country_code_arr.__contains__(country_code):
- self.country_code_arr.append(country_code)
- self.country_name_dic[country_name] = price_arr
- self.country_code_dic[country_code] = price_arr
- self.price_index_count = len(self.country_name_dic[self.country_name_arr[0]])
- def get_price(self, country_code, index):
- if not self.country_code_dic.has_key(country_code):
- return None
- if index < len(self.country_code_dic[country_code]):
- return self.country_code_dic[country_code][index]
- else:
- return None
- def get_country_name_index_arr(self):
- """ 按国家名重新组织数据
- 把price按index分成数组
- :return: arr [{'美国': 1.0}, {'美国': 2.0}]
- """
- arr = []
- for index in range(0, self.price_index_count):
- dic = {}
- for country_name in self.country_name_dic:
- price_arr = self.country_name_dic[country_name]
- dic[country_name] = price_arr[index]
- arr.append(dic)
- return arr
- if __name__ == '__main__':
- ad_worth_config_txt_path = "/Users/zhuge/Temp/20201222/FruitHeroes-点消-IOS_inter_summary.txt"
- ad_worth_config_txt = AdEcpmConfigTxt()
- ad_worth_config_txt.read(ad_worth_config_txt_path)
- print ad_worth_config_txt.country_name_arr
- print ad_worth_config_txt.country_code_arr
- print ad_worth_config_txt.get_price("US", 7)
- print "总国家数: {}".format(ad_worth_config_txt.country_count)
- print "总价格数: {}".format(ad_worth_config_txt.price_index_count)
- print "第一层价格: {}".format(ad_worth_config_txt.get_country_name_index_arr()[7]["美国"])
|