commonFunc.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from sqlalchemy import create_engine
  2. from urllib import parse
  3. import pandas as pd
  4. import yaml
  5. with open('config/config.yaml', mode='r', encoding='utf-8') as f:
  6. config = yaml.load(f.read(), Loader=yaml.FullLoader)
  7. def update_dict(x, y):
  8. # 用字典y,来更新x
  9. # key 存在则更新,不存在则新增
  10. x.update(y)
  11. return x
  12. def get_db_engine(db_info):
  13. db_con_str = 'mysql+pymysql://%s:%s@%s:%d/%s' % \
  14. (db_info['username'],
  15. parse.quote_plus(db_info['password']),
  16. db_info['host'],
  17. db_info['port'],
  18. db_info['database'])
  19. engine = create_engine(db_con_str, connect_args={'charset': 'utf8'})
  20. return engine
  21. def is_contains_region(val1, val2):
  22. """
  23. level1
  24. region long[] 可选 地域 传值为[]表示不限;传递上一级ID时,childrenID可以不传;不允许同时传parentID和childrenID;
  25. 仅计划的campaign_type为5时,支持设置三级地域(例:山西-大同-左云,左云是三级地域)
  26. :param val1:
  27. :param val2:
  28. :return:
  29. """
  30. is_contains = False
  31. val = None
  32. city_lst = []
  33. sql = """
  34. select t1.city_name, t2.level, t2.region_id from ctop_kuaishou_city_level t1
  35. left join ctop_kuaishou_region_list_parent t2
  36. on t1.city_name = t2.name
  37. where t1.city_level in %s
  38. """ % (tuple(val1.split('|')),)
  39. city_df = pd.read_sql(sql, None)
  40. # level1
  41. if city_df[city_df.level == 1].region_id.values in eval(val2):
  42. val.extend(city_df[city_df.level == 1].region_id.values)
  43. else:
  44. return False, -1
  45. # level2
  46. while not is_contains:
  47. pass
  48. # level3
  49. pass
  50. def is_contains_gender(val1, val2):
  51. """
  52. :param val1: ctop_ai_kuaishou_signature_recommended_target_combine 中的 gender 字段: '男'、'女'、'不限'、None
  53. :param val2: ctop_ai_kuaishou_advertiser_strategy 中的 gender 字段: 1:女性, 2:男性,0表示不限
  54. :return: val1 是否为 val2的子集,以及传递给快手后台的值
  55. """
  56. is_contains = False
  57. val = None
  58. if val1 == '男' and val2 in (2, 0):
  59. is_contains = True
  60. val = val1
  61. if val1 == '女' and val2 in (1, 0):
  62. is_contains = True
  63. val = val1
  64. if val1 == '不限' and val2 == 0:
  65. is_contains = True
  66. val = val2
  67. # 如果val1为空,表示该字段没有参与定向组合运算,就直接取客户投放策略里面的值
  68. if val1 is None:
  69. is_contains = True
  70. val = val2
  71. return is_contains, {'gender': val}
  72. def is_contains_platform_os(val1, val2):
  73. """
  74. :param val1: ctop_ai_kuaishou_signature_recommended_target_combine 中的 client 字段: '男'、'女'、'不限'、None
  75. :param val2:
  76. :return:
  77. """
  78. pass
  79. def is_contains_age(val1, val2):
  80. pass
  81. def is_contains_business_interest(val1, val2):
  82. pass