mdbq 0.2.6__tar.gz → 0.2.8__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 (43) hide show
  1. {mdbq-0.2.6 → mdbq-0.2.8}/PKG-INFO +1 -1
  2. mdbq-0.2.8/mdbq/aggregation/optimize_data.py +22 -0
  3. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/aggregation/query_data.py +4 -1
  4. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/mysql/data_types.py +10 -4
  5. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/mysql/mysql.py +44 -8
  6. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq.egg-info/PKG-INFO +1 -1
  7. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq.egg-info/SOURCES.txt +1 -0
  8. {mdbq-0.2.6 → mdbq-0.2.8}/setup.py +1 -1
  9. {mdbq-0.2.6 → mdbq-0.2.8}/README.txt +0 -0
  10. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/__init__.py +0 -0
  11. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/__version__.py +0 -0
  12. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/aggregation/__init__.py +0 -0
  13. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/aggregation/aggregation.py +0 -0
  14. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/bdup/__init__.py +0 -0
  15. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/bdup/bdup.py +0 -0
  16. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/clean/__init__.py +0 -0
  17. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/clean/data_clean.py +0 -0
  18. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/company/__init__.py +0 -0
  19. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/company/copysh.py +0 -0
  20. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/config/__init__.py +0 -0
  21. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/config/get_myconf.py +0 -0
  22. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/config/products.py +0 -0
  23. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/config/update_conf.py +0 -0
  24. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/dataframe/__init__.py +0 -0
  25. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/dataframe/converter.py +0 -0
  26. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/log/__init__.py +0 -0
  27. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/log/mylogger.py +0 -0
  28. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/mongo/__init__.py +0 -0
  29. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/mongo/mongo.py +0 -0
  30. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/mysql/__init__.py +0 -0
  31. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/mysql/s_query.py +0 -0
  32. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/mysql/year_month_day.py +0 -0
  33. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/other/__init__.py +0 -0
  34. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/other/porxy.py +0 -0
  35. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/other/pov_city.py +0 -0
  36. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/other/ua_sj.py +0 -0
  37. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/pbix/__init__.py +0 -0
  38. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/pbix/pbix_refresh.py +0 -0
  39. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/pbix/refresh_all.py +0 -0
  40. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq/spider/__init__.py +0 -0
  41. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq.egg-info/dependency_links.txt +0 -0
  42. {mdbq-0.2.6 → mdbq-0.2.8}/mdbq.egg-info/top_level.txt +0 -0
  43. {mdbq-0.2.6 → mdbq-0.2.8}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mdbq
3
- Version: 0.2.6
3
+ Version: 0.2.8
4
4
  Home-page: https://pypi.org/project/mdbsql
5
5
  Author: xigua,
6
6
  Author-email: 2587125111@qq.com
@@ -0,0 +1,22 @@
1
+ # -*- coding: UTF-8 –*-
2
+ from mdbq.mysql import mysql
3
+ from mdbq.config import get_myconf
4
+ """
5
+ 对指定数据库所有冗余数据进行清理
6
+ """
7
+
8
+
9
+ def op_data(service_databases, days: int = 63):
10
+ for service_database in service_databases:
11
+ for service_name, database in service_database.items():
12
+ username, password, host, port = get_myconf.select_config_values(target_service=service_name, database=database)
13
+ s = mysql.OptimizeDatas(username=username, password=password, host=host, port=port)
14
+ s.db_name_lists = [
15
+ '聚合数据',
16
+ ]
17
+ s.days = days
18
+ s.optimize_list()
19
+
20
+
21
+ if __name__ == '__main__':
22
+ op_data(service_databases=[{'home_lx': 'mysql'}], days=3650)
@@ -2,6 +2,7 @@
2
2
  from mdbq.mongo import mongo
3
3
  from mdbq.mysql import mysql
4
4
  from mdbq.mysql import s_query
5
+ from mdbq.aggregation import optimize_data
5
6
  from mdbq.config import get_myconf
6
7
  import datetime
7
8
  from dateutil.relativedelta import relativedelta
@@ -511,6 +512,8 @@ def data_aggregation(service_databases=[{}]):
511
512
  res = g.performance() # 盈亏表,依赖其他表,单独做
512
513
  m.df_to_mysql(df=res, db_name='聚合数据', tabel_name='销售盈亏')
513
514
 
515
+ optimize_data.op_data(service_databases=service_databases, days=3650) # 立即启动对聚合数据的清理工作
516
+
514
517
 
515
518
  if __name__ == '__main__':
516
- data_aggregation(service_databases=[{'company': 'mysql'}])
519
+ data_aggregation(service_databases=[{'home_lx': 'mysql'}])
@@ -155,13 +155,13 @@ class DataTypes:
155
155
  if collection_name in list(self.datas[cl][db_name].keys()):
156
156
  return self.datas[cl][db_name][collection_name]
157
157
  else:
158
- print(f'不存在的集合名信息: {collection_name}')
158
+ print(f'不存在的集合名信息: {collection_name}, 文件位置: {json_file}')
159
159
  return {}
160
160
  else:
161
- print(f'不存在的数据库信息: {db_name}')
161
+ print(f'不存在的数据库信息: {db_name}, 文件位置: {json_file}')
162
162
  return {}
163
163
  else:
164
- print(f'不存在的数据分类: {cl}')
164
+ print(f'不存在的数据分类: {cl}, 文件位置: {json_file}')
165
165
  return {}
166
166
 
167
167
 
@@ -252,4 +252,10 @@ def main():
252
252
 
253
253
  if __name__ == '__main__':
254
254
  # main()
255
- mysql_all_dtypes()
255
+ mysql_all_dtypes() # 更新 mysql 中所有数据库的 dtypes 信息到本地 json
256
+
257
+ # path = '/Users/xigua/数据中心/自动0备份/py/数据更新/support'
258
+ # d = DataTypes()
259
+ # # 从本地文件中读取 dtype 信息
260
+ # dtypes = d.load_dtypes(cl='mysql', db_name='生意经2', collection_name='店铺指标', path=path)
261
+ # print(dtypes)
@@ -13,6 +13,7 @@ import os
13
13
  import calendar
14
14
  from mdbq.config import get_myconf
15
15
  from mdbq.dataframe import converter
16
+ from mdbq.mysql import data_types
16
17
 
17
18
  warnings.filterwarnings('ignore')
18
19
 
@@ -107,12 +108,15 @@ class MysqlUpload:
107
108
  cursor.execute(sql)
108
109
  print(f'创建 mysql 表: {tabel_name}')
109
110
 
110
- cols = df.columns.tolist()
111
- dtypes = df.dtypes.apply(str).to_dict() # 将 dataframe 数据类型转为字典形式
112
- # 转换为 mysql 的数据类型
113
- dtypes.update({col: self.convert_dtype_to_sql(df=df, col=col, dtype=dtypes[col]) for col in cols})
111
+ # # 2. 列数据类型转换
112
+ # cols = df.columns.tolist()
113
+ # dtypes = df.dtypes.apply(str).to_dict() # 将 dataframe 数据类型转为字典形式
114
+ # # 转换为 mysql 的数据类型
115
+ # dtypes.update({col: self.convert_dtype_to_sql(df=df, col=col, dtype=dtypes[col]) for col in cols})
116
+ dtypes = self.convert_dtypes(df=df, db_name=db_name, tabel_name=tabel_name)
114
117
 
115
- # 2. 检查列, 不存在则添加新列
118
+ # 3. 检查列, 不存在则添加新列
119
+ cols = df.columns.tolist()
116
120
  for col in cols:
117
121
  sql = ('SELECT 1 FROM information_schema.columns WHERE table_schema = %s AND table_name = %s AND '
118
122
  'column_name = %s')
@@ -134,7 +138,7 @@ class MysqlUpload:
134
138
  cursor.execute(f"CREATE INDEX index_name ON {tabel_name}({col})")
135
139
  connection.commit() # 提交事务
136
140
 
137
- # # 3. 移除指定日期范围内的数据, 避免重复插入
141
+ # # 4. 移除指定日期范围内的数据, 避免重复插入
138
142
  # dates = df['日期'].values.tolist()
139
143
  # start_date = pd.to_datetime(min(dates)).strftime('%Y-%m-%d')
140
144
  # end_date = (pd.to_datetime(max(dates)) + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
@@ -142,7 +146,7 @@ class MysqlUpload:
142
146
  # cursor.execute(sql)
143
147
  # connection.commit()
144
148
 
145
- # 4. 更新插入数据
149
+ # 5. 更新插入数据
146
150
  now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S ")
147
151
  print(f'{now}正在更新 mysql ({self.host}:{self.port}) {db_name}/{tabel_name}')
148
152
  if str(self.host) == '192.168.1.100': # 群晖服务器
@@ -176,8 +180,40 @@ class MysqlUpload:
176
180
  finally:
177
181
  connection.close()
178
182
 
183
+ def convert_dtypes(self, df, db_name, tabel_name):
184
+ """
185
+ 根据本地已经存在的记录着 mysql dtypes 的 json 文件转换 df 的类型为 mysql 专有的数据类型
186
+ 允许通过 json 文件指定列的数据类型
187
+ 以下两种情况已经兼容:
188
+ 1. 可能不存在本地 json 文件 (利用 convert_dtype_to_sql 函数按指定规则转换全部列)
189
+ 2. json 文件中没有或者缺失部分列信息(利用 convert_dtype_to_sql 函数按指定规则转换缺失列)
190
+ """
191
+ cols = df.columns.tolist()
192
+ path = '/Users/xigua/数据中心/自动0备份/py/数据更新/support'
193
+ # json_file = os.path.join(path, 'df_dtypes.json')
194
+ # if os.path.isfile(json_file):
195
+ d = data_types.DataTypes()
196
+ # 从本地文件中读取 dtype 信息
197
+ dtypes = d.load_dtypes(cl='mysql', db_name=db_name, collection_name=tabel_name, path=path)
198
+ # 可能会因为没有 json 文件, 返回 None
199
+ if dtypes:
200
+ # 按照文件记录更新 dtypes
201
+ dtypes.update({col: dtypes[col] for col in cols if col in dtypes.keys()})
202
+ # 可能存在部分列不在文件记录中
203
+ col_not_exist = [col for col in cols if col not in dtypes.keys()]
204
+ # 这些列不存在于 df 中, 必须移除
205
+ [dtypes.pop(col) for col in list(dtypes.keys()) if col not in cols]
206
+ else:
207
+ dtypes = df.dtypes.apply(str).to_dict() # 将 dataframe 数据类型转为字典形式
208
+ col_not_exist = cols
209
+ # 对文件不存在的列信息进行数据类型转换(按指定规则)
210
+ dtypes.update({col: self.convert_dtype_to_sql(df=df, col=col, dtype=df[col].dtype) for col in col_not_exist})
211
+ # 至此 df 中全部列类型已经转换完成
212
+ # 返回结果, 示例: {'上市年份': 'mediumtext', '商品id': 'mediumtext', '平台': 'mediumtext'}
213
+ return dtypes
214
+
179
215
  def convert_dtype_to_sql(self, df, col, dtype):
180
- """ 转换DataFrame列的数据类型为SQL数据类型 """
216
+ """ 按照以下规则转换DataFrame列的数据类型为 MYSQL 专有的数据类型 """
181
217
  # 最优先处理 ID 类型, 在 mysql 里面, 有些列数字过长不能存储为 int 类型
182
218
  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
219
  return 'mediumtext'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mdbq
3
- Version: 0.2.6
3
+ Version: 0.2.8
4
4
  Home-page: https://pypi.org/project/mdbsql
5
5
  Author: xigua,
6
6
  Author-email: 2587125111@qq.com
@@ -8,6 +8,7 @@ mdbq.egg-info/dependency_links.txt
8
8
  mdbq.egg-info/top_level.txt
9
9
  mdbq/aggregation/__init__.py
10
10
  mdbq/aggregation/aggregation.py
11
+ mdbq/aggregation/optimize_data.py
11
12
  mdbq/aggregation/query_data.py
12
13
  mdbq/bdup/__init__.py
13
14
  mdbq/bdup/bdup.py
@@ -3,7 +3,7 @@
3
3
  from setuptools import setup, find_packages
4
4
 
5
5
  setup(name='mdbq',
6
- version='0.2.6',
6
+ version='0.2.8',
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