mdbq 0.1.7__tar.gz → 0.1.9__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. {mdbq-0.1.7 → mdbq-0.1.9}/PKG-INFO +1 -1
  2. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/aggregation/aggregation.py +43 -3
  3. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/aggregation/query_data.py +47 -45
  4. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/mysql/mysql.py +15 -1
  5. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq.egg-info/PKG-INFO +1 -1
  6. {mdbq-0.1.7 → mdbq-0.1.9}/setup.py +1 -1
  7. {mdbq-0.1.7 → mdbq-0.1.9}/README.txt +0 -0
  8. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/__init__.py +0 -0
  9. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/__version__.py +0 -0
  10. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/aggregation/__init__.py +0 -0
  11. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/bdup/__init__.py +0 -0
  12. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/bdup/bdup.py +0 -0
  13. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/clean/__init__.py +0 -0
  14. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/clean/data_clean.py +0 -0
  15. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/company/__init__.py +0 -0
  16. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/company/copysh.py +0 -0
  17. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/config/__init__.py +0 -0
  18. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/config/get_myconf.py +0 -0
  19. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/config/update_conf.py +0 -0
  20. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/dataframe/__init__.py +0 -0
  21. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/dataframe/converter.py +0 -0
  22. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/log/__init__.py +0 -0
  23. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/log/mylogger.py +0 -0
  24. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/mongo/__init__.py +0 -0
  25. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/mongo/mongo.py +0 -0
  26. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/mysql/__init__.py +0 -0
  27. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/mysql/s_query.py +0 -0
  28. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/mysql/year_month_day.py +0 -0
  29. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/other/__init__.py +0 -0
  30. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/other/porxy.py +0 -0
  31. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/other/pov_city.py +0 -0
  32. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/other/ua_sj.py +0 -0
  33. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/pbix/__init__.py +0 -0
  34. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/pbix/pbix_refresh.py +0 -0
  35. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/pbix/refresh_all.py +0 -0
  36. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq/spider/__init__.py +0 -0
  37. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq.egg-info/SOURCES.txt +0 -0
  38. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq.egg-info/dependency_links.txt +0 -0
  39. {mdbq-0.1.7 → mdbq-0.1.9}/mdbq.egg-info/top_level.txt +0 -0
  40. {mdbq-0.1.7 → mdbq-0.1.9}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mdbq
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Home-page: https://pypi.org/project/mdbsql
5
5
  Author: xigua,
6
6
  Author-email: 2587125111@qq.com
@@ -136,6 +136,7 @@ class DatabaseUpdate:
136
136
  def __init__(self, path):
137
137
  self.path = path # 数据所在目录, 即: 下载文件夹
138
138
  self.datas: list = [] # 带更新进数据库的数据集合
139
+ self.start_date = '2022-01-01' # 日期表的起始日期
139
140
 
140
141
  def cleaning(self, is_move=True):
141
142
  """
@@ -740,6 +741,15 @@ class DatabaseUpdate:
740
741
  os.remove(os.path.join(root, name))
741
742
  json_data.dtypes_to_file() # 写入 json 文件, 包含数据的 dtypes 信息
742
743
 
744
+ df = self.date_table() # 创建一个日期表
745
+ self.datas.append(
746
+ {
747
+ '数据库名': '聚合数据',
748
+ '集合名称': '日期表',
749
+ '数据主体': df,
750
+ }
751
+ )
752
+
743
753
  def upload_df(self, service_databases=[{}]):
744
754
  """
745
755
  将清洗后的 df 上传数据库
@@ -896,6 +906,32 @@ class DatabaseUpdate:
896
906
  encod = chardet.detect(f1).get('encoding')
897
907
  return encod
898
908
 
909
+ def date_table(self):
910
+ """
911
+ 生成 pbix使用的日期表
912
+ """
913
+ yesterday = time.strftime('%Y-%m-%d', time.localtime(time.time() - 86400))
914
+ dic = pd.date_range(start=self.start_date, end=yesterday)
915
+ df = pd.DataFrame(dic, columns=['日期'])
916
+ df.sort_values('日期', ascending=True, ignore_index=True, inplace=True)
917
+ df.reset_index(inplace=True)
918
+ # inplace 添加索引到 df
919
+ p = df.pop('index')
920
+ df['月2'] = df['日期']
921
+ df['月2'] = df['月2'].dt.month
922
+ df['日期'] = df['日期'].dt.date # 日期格式保留年月日,去掉时分秒
923
+ df['年'] = df['日期'].apply(lambda x: str(x).split('-')[0] + '年')
924
+ df['月'] = df['月2'].apply(lambda x: str(x) + '月')
925
+ # df.drop('月2', axis=1, inplace=True)
926
+ mon = df.pop('月2')
927
+ df['日'] = df['日期'].apply(lambda x: str(x).split('-')[2])
928
+ df['年月'] = df.apply(lambda x: x['年'] + x['月'], axis=1)
929
+ df['月日'] = df.apply(lambda x: x['月'] + x['日'] + '日', axis=1)
930
+ df['第n周'] = df['日期'].apply(lambda x: x.strftime('第%W周'))
931
+ df['索引'] = p
932
+ df['月索引'] = mon
933
+ df.sort_values('日期', ascending=False, ignore_index=True, inplace=True)
934
+ return df
899
935
 
900
936
  def update_dtypte():
901
937
  """ 更新一个文件的 dtype 信息到 json 文件 """
@@ -991,7 +1027,11 @@ def main():
991
1027
  d = DatabaseUpdate(path='/Users/xigua/Downloads')
992
1028
  d.new_unzip(is_move=True)
993
1029
  d.cleaning(is_move=False)
994
- d.upload_df(service_databases=[{'home_lx': 'mongodb'}, {'home_lx': 'mysql'}])
1030
+ d.upload_df(service_databases=[
1031
+ # {'home_lx': 'mongodb'},
1032
+ {'home_lx': 'mysql'}
1033
+ ]
1034
+ )
995
1035
  # print(d.datas)
996
1036
 
997
1037
 
@@ -999,5 +1039,5 @@ if __name__ == '__main__':
999
1039
  # username, password, host, port = get_myconf.select_config_values(target_service='nas', database='mysql')
1000
1040
  # print(username, password, host, port)
1001
1041
 
1002
- # main()
1003
- upload()
1042
+ main()
1043
+ # upload()
@@ -396,57 +396,59 @@ class GroupBy:
396
396
  index=index, header=header, engine=engine, freeze_panes=freeze_panes)
397
397
 
398
398
 
399
- def data_aggregation():
399
+ def data_aggregation(service_databases=[{}]):
400
400
  """
401
401
  1. 从数据库中读取数据
402
402
  2. 数据聚合清洗
403
403
  3. 统一回传数据库: <聚合数据> (不再导出为文件)
404
404
  """
405
- sdq = MysqlDatasQuery(target_service='company') # 实例化数据处理类
406
- sdq.months = 0 # 设置数据周期
407
- g = GroupBy() # 实例化数据聚合类
408
- # 实例化数据库连接
409
- username, password, host, port = get_myconf.select_config_values(target_service='company', database='mysql')
410
- m = mysql.MysqlUpload(username=username, password=password, host=host, port=port)
405
+ for service_database in service_databases:
406
+ for service_name, database in service_database.items():
407
+ sdq = MysqlDatasQuery(target_service=service_name) # 实例化数据处理类
408
+ sdq.months = 0 # 设置数据周期
409
+ g = GroupBy() # 实例化数据聚合类
410
+ # 实例化数据库连接
411
+ username, password, host, port = get_myconf.select_config_values(target_service=service_name, database=database)
412
+ m = mysql.MysqlUpload(username=username, password=password, host=host, port=port)
411
413
 
412
- data_dict = [
413
- {
414
- '数据库名': '聚合数据',
415
- '集合名': '推广数据_宝贝主体报表',
416
- '数据主体': sdq.tg_wxt(),
417
- },
418
- {
419
- '数据库名': '聚合数据',
420
- '集合名': '天猫生意经_宝贝指标',
421
- '数据主体': sdq.syj(),
422
- },
423
- {
424
- '数据库名': '聚合数据',
425
- '集合名': '天猫_店铺来源_日数据',
426
- '数据主体': sdq.dplyd(),
427
- },
428
- {
429
- '数据库名': '聚合数据',
430
- '集合名': '商品id编码表',
431
- '数据主体': sdq.idbm(),
432
- },
433
- {
434
- '数据库名': '聚合数据',
435
- '集合名': '商品id图片对照表',
436
- '数据主体': sdq.sp_picture(),
437
- },
438
- {
439
- '数据库名': '聚合数据',
440
- '集合名': '商品成本',
441
- '数据主体': sdq.sp_cost(),
442
- },
443
- ]
444
- for items in data_dict:
445
- db_name, tabel_name, df = items['数据库名'], items['集合名'], items['数据主体']
446
- df = g.groupby(df=df, tabel_name=tabel_name, is_maximize=True) # 2. 聚合数据
447
- # g.as_csv(df=df, filename=tabel_name + '.csv')
448
- m.df_to_mysql(df=df, db_name=db_name, tabel_name=tabel_name) # 3. 回传数据库
414
+ data_dict = [
415
+ {
416
+ '数据库名': '聚合数据',
417
+ '集合名': '推广数据_宝贝主体报表',
418
+ '数据主体': sdq.tg_wxt(),
419
+ },
420
+ {
421
+ '数据库名': '聚合数据',
422
+ '集合名': '天猫生意经_宝贝指标',
423
+ '数据主体': sdq.syj(),
424
+ },
425
+ {
426
+ '数据库名': '聚合数据',
427
+ '集合名': '天猫_店铺来源_日数据',
428
+ '数据主体': sdq.dplyd(),
429
+ },
430
+ {
431
+ '数据库名': '聚合数据',
432
+ '集合名': '商品id编码表',
433
+ '数据主体': sdq.idbm(),
434
+ },
435
+ {
436
+ '数据库名': '聚合数据',
437
+ '集合名': '商品id图片对照表',
438
+ '数据主体': sdq.sp_picture(),
439
+ },
440
+ {
441
+ '数据库名': '聚合数据',
442
+ '集合名': '商品成本',
443
+ '数据主体': sdq.sp_cost(),
444
+ },
445
+ ]
446
+ for items in data_dict:
447
+ db_name, tabel_name, df = items['数据库名'], items['集合名'], items['数据主体']
448
+ df = g.groupby(df=df, tabel_name=tabel_name, is_maximize=True) # 2. 聚合数据
449
+ # g.as_csv(df=df, filename=tabel_name + '.csv')
450
+ m.df_to_mysql(df=df, db_name=db_name, tabel_name=tabel_name) # 3. 回传数据库
449
451
 
450
452
 
451
453
  if __name__ == '__main__':
452
- data_aggregation()
454
+ data_aggregation(service_databases=[{'company': 'mysql'}])
@@ -181,6 +181,8 @@ class MysqlUpload:
181
181
  # 最优先处理 ID 类型, 在 mysql 里面, 有些列数字过长不能存储为 int 类型
182
182
  if 'id' in col or 'ID' in col or 'Id' in col or '摘要' in col or '商家编码' in col or '单号' in col or '款号' in col:
183
183
  return 'mediumtext'
184
+ if '商品编码' in col: # 京东sku/spu商品信息
185
+ return 'mediumtext'
184
186
  if '文件大小' in col: # bw 程序
185
187
  return 'mediumtext'
186
188
  elif '日期' in col or '时间' in col:
@@ -192,8 +194,12 @@ class MysqlUpload:
192
194
  elif dtype == 'datetime64[ns]': # 日期可能显示为数字, 因为放在判断 int 的前面
193
195
  return 'DATE'
194
196
  elif dtype == 'int32':
197
+ if len(str(max(df[col].tolist()))) >= 10: # 数值长度超限转为 mediumtext
198
+ return 'mediumtext'
195
199
  return 'INT'
196
200
  elif dtype == 'int64':
201
+ if len(str(max(df[col].tolist()))) >= 10:
202
+ return 'mediumtext'
197
203
  return 'INT'
198
204
  elif dtype == 'float64':
199
205
  return 'FLOAT'
@@ -391,11 +397,19 @@ class OptimizeDatas:
391
397
  self.config.update({'database': self.db_name}) # 添加更新 config 字段
392
398
  self.connection = pymysql.connect(**self.config)
393
399
  with self.connection.cursor() as cursor:
400
+ sql = f"SELECT 1 FROM {table_name} LIMIT 1"
401
+ cursor.execute(sql)
402
+ result = cursor.fetchone()
403
+ if not result:
404
+ now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S ")
405
+ print(f'{now}数据表: {table_name}, 数据长度为 0')
406
+ continue # 检查数据表是否为空
407
+
394
408
  cursor.execute(f"SHOW FULL COLUMNS FROM {table_name}") # 查询数据表的列信息
395
409
  columns = cursor.fetchall()
396
410
  date_exist = False
397
411
  for col in columns: # 遍历列信息,检查是否存在类型为日期的列
398
- if col['Field'] == '日期' and col['Type'] == 'date' or col['Type'].startswith('datetime'):
412
+ if col['Field'] == '日期' and (col['Type'] == 'date' or col['Type'].startswith('datetime')):
399
413
  date_exist = True
400
414
  break
401
415
  if date_exist: # 存在日期列
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mdbq
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Home-page: https://pypi.org/project/mdbsql
5
5
  Author: xigua,
6
6
  Author-email: 2587125111@qq.com
@@ -3,7 +3,7 @@
3
3
  from setuptools import setup, find_packages
4
4
 
5
5
  setup(name='mdbq',
6
- version='0.1.7',
6
+ version='0.1.9',
7
7
  author='xigua, ',
8
8
  author_email="2587125111@qq.com",
9
9
  url='https://pypi.org/project/mdbsql',
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes