ailuoge 1.0.7__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.
ailuoge-1.0.7/PKG-INFO ADDED
@@ -0,0 +1,17 @@
1
+ Metadata-Version: 2.1
2
+ Name: ailuoge
3
+ Version: 1.0.7
4
+ Summary: ailuoge
5
+ Author: ailuoge
6
+ Author-email:
7
+ Keywords: python,ailuoge
8
+ Classifier: Development Status :: 1 - Planning
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: Unix
12
+ Classifier: Operating System :: MacOS :: MacOS X
13
+ Classifier: Operating System :: Microsoft :: Windows
14
+ Description-Content-Type: text/markdown
15
+
16
+
17
+ # ilog
@@ -0,0 +1,795 @@
1
+ import hashlib
2
+ import json
3
+ import math
4
+ import openpyxl
5
+ import os
6
+ import pymssql
7
+ import pymysql
8
+ import re
9
+ import requests
10
+ import sqlalchemy
11
+ import time
12
+ import zipfile
13
+
14
+ import pandas as pd
15
+ import numpy as np
16
+
17
+ from openpyxl.utils import get_column_letter
18
+ from tqdm import tqdm
19
+
20
+
21
+ class GetEcData:
22
+
23
+ def __init__(self, user_name, user_token):
24
+ self.wsdl = 'http://openapi-web.eccang.com/openApi/api/unity'
25
+ self.headers = {
26
+ 'ContentType': 'application/json'
27
+ }
28
+
29
+ self.user_name = user_name
30
+ self.user_token = user_token
31
+
32
+ def __concat_params(self, biz_content: dict, interface_method: str):
33
+ """
34
+ 处理post data, 生成key
35
+ :param biz_content: 详细调用参数
36
+ :param interface_method: 易仓的信息 getStockOrderList
37
+ :return:
38
+ """
39
+ post_data = {
40
+ "app_key": self.user_name,
41
+ "biz_content": json.dumps(biz_content),
42
+ "charset": "UTF-8",
43
+ "interface_method": interface_method,
44
+ "nonce_str": "113456",
45
+ "service_id": "E7HPYV",
46
+ "sign_type": "MD5",
47
+ "timestamp": int(time.time() * 1000),
48
+ "version": "v1.0.0"
49
+ }
50
+
51
+ # 将字典转化为易仓需要的加密形式
52
+ post_data_str = ''
53
+ for one_key, one_value in zip(post_data.keys(), post_data.values()):
54
+ if type(one_value) == dict:
55
+ one_value = json.dumps(one_value).replace(': ', ':')
56
+ post_data_str += one_key
57
+ post_data_str += '='
58
+ post_data_str += str(one_value)
59
+ post_data_str += '&'
60
+
61
+ post_data_str = post_data_str[:-1]
62
+ post_data_str += self.user_token
63
+
64
+ # 对组合后的信息进行加密md5
65
+ post_data['sign'] = hashlib.md5(bytes(post_data_str, encoding='utf-8')).hexdigest()
66
+
67
+ return post_data
68
+
69
+ def __get_data(self, biz_content: dict, interface_method: str, key_word: str = 'data'):
70
+ """
71
+ 获取单页数据,把传入的参数转成json格式,向api请求,提取response.text里的data数据
72
+ :param biz_content:
73
+ :param interface_method:
74
+ :param key_word: 要返回的关键词
75
+ :return:
76
+ """
77
+ concated_params = self.__concat_params(biz_content, interface_method)
78
+
79
+ # 获取response
80
+ res = requests.post(self.wsdl, json=concated_params, headers=self.headers)
81
+ # 把response的text的json格式转换成字典格式
82
+ pg_info = json.loads(res.text)
83
+ print(pg_info['message']) if pg_info['message'] != 'Success' else None # 打印异常信息
84
+
85
+ # 判断是否超时
86
+ if pg_info['message'] == '系统异常':
87
+ print('系统异常,易仓可能超时')
88
+ page_data = ''
89
+ else:
90
+ # 根据传入的键返回值
91
+ page_data = json.loads(pg_info.get('biz_content')).get(key_word)
92
+
93
+ return page_data
94
+
95
+ def get_data(self, biz_content: dict, interface_method: str, special_param: str = None):
96
+ """
97
+ https://open.eccang.com/#/documentCenter?docId=1287&catId=0-225-225,0-177
98
+ 获取请求的数据
99
+ :param biz_content:
100
+ :param interface_method:
101
+ :param special_param: 特殊参数,传入该参数后不会尝试获取数据的最大行数,而是遍历biz_content中的该参数列表
102
+ :return:
103
+ """
104
+ # 0 参数设置
105
+ # 默认页数
106
+ if not biz_content.get('page_size'):
107
+ biz_content['page_size'] = 20
108
+
109
+ list_df = []
110
+ if not special_param:
111
+ # 1 获取最大页数
112
+ record_rows = self.__get_data(biz_content, interface_method, 'total_count')
113
+ if not record_rows:
114
+ record_rows = self.__get_data(biz_content, interface_method, 'total')
115
+ # 向上取整
116
+ max_page = math.ceil(int(record_rows) / biz_content.get('page_size'))
117
+
118
+ # 2 按页获取数据
119
+ print('按页获取数据')
120
+ for i in tqdm(range(1, max_page + 1)):
121
+ # 2.1 调整键值对
122
+ biz_content['page'] = i
123
+ # 2.2 获取对应页数的数据
124
+ pg_data = self.__get_data(biz_content, interface_method)
125
+ if pg_data:
126
+ list_df.append(pd.DataFrame(pg_data))
127
+ else:
128
+ list_param = biz_content[special_param] # 参数列表,比如订单号
129
+ lens = len(list_param)
130
+ # 1 遍历special_param,每次1个
131
+ print(f'根据{special_param},每次获取1个数据')
132
+ for i in tqdm(range(0, lens, 1)):
133
+ # 2.2 获取对应页数的数据
134
+ biz_content[special_param] = list_param[i: i + 1]
135
+ pg_data = self.__get_data(biz_content, interface_method)
136
+ if pg_data:
137
+ list_df.append(pd.DataFrame(pg_data))
138
+
139
+ return list_df
140
+
141
+
142
+ class SQLServer:
143
+
144
+ def __init__(self, server, user, password, database):
145
+ self.conn = pymssql.connect(
146
+ server=server,
147
+ user=user,
148
+ password=password,
149
+ database=database,
150
+ autocommit=True
151
+ )
152
+ self.cur = self.conn.cursor()
153
+
154
+ def exec_query(self, sql):
155
+ self.cur.execute(sql)
156
+ self.conn.commit()
157
+ self.conn.close()
158
+
159
+
160
+ class Mysql:
161
+
162
+ def __init__(self, host, user, password, database):
163
+ self.conn = pymysql.connect(host=host, user=user, password=password, database=database, charset='utf8')
164
+ self.cur = self.conn.cursor()
165
+
166
+ def exec_query(self, sql):
167
+ self.cur.execute(sql)
168
+ self.conn.commit()
169
+ result = self.cur.fetchall()
170
+ for row in result:
171
+ print(row)
172
+ self.conn.close()
173
+
174
+
175
+ def account_perriod(start_date=None, day='first') -> pd.Timestamp:
176
+ """
177
+ 根据输入的月份格式返回日期格式
178
+ :param start_date: 起始日期,如果有的话,返回这个日期对应的月的最后一天,比如传入'2023-01-01',返回'2023-01-31'
179
+ :param day: first、last,返回月初或者月末日期,默认为月初
180
+ :return: "%Y-%m-01"
181
+ """
182
+ # 传入的日期格式固定为%Y-%m-%d
183
+
184
+ # 判断有无输入日期
185
+ if start_date:
186
+ # 有输入日期,说明现在要求结尾日期,需要返回这个输入日期的当月最后一天。输入的日期加31天变成下个月日期,再减去天数变成当月最后一天
187
+ default = (pd.to_datetime(start_date, format='%Y-%m-%d') + pd.Timedelta(31, 'D'))
188
+ default = default - pd.Timedelta(default.day, 'D')
189
+ else:
190
+ # startDate为空,说明是求起始日期,默认上个月的第一天
191
+ default = pd.Timestamp.now() - pd.Timedelta(pd.Timestamp.now().day + 1, 'D') # 默认上个月
192
+ default = default - pd.Timedelta(default.day - 1, 'D')
193
+
194
+ month = input(f"格式为:2000-01,(直接回车就是这个账期):{default.strftime('%Y-%m-%d')[:7]}\n")
195
+
196
+ # 如果输入不为空
197
+ if month:
198
+ # 输入的格式是%Y-%m,先把它改成月份的第一天
199
+ month = pd.to_datetime(month, format='%Y-%m')
200
+ # 如果要返回月初,等于输入月份的第一天
201
+ if day == 'first':
202
+ date = month
203
+ # 如果要返回月末,等于月份最后一天
204
+ else:
205
+ date = (month + pd.Timedelta(31, 'D'))
206
+ date = (date - pd.Timedelta(date.day, 'D'))
207
+ # 为空的话返回默认值
208
+ else:
209
+ date = default
210
+ return date
211
+
212
+
213
+ def amz_used_item_regex() -> str:
214
+ """
215
+ 从二手sku里获取sku的正则表达式
216
+ """
217
+ used_sku_regex = r'([A-Z]{1,2}\d?(-)?\d{0,2}[A-Z]{1,2}\d{2,6}[A-Z]{0,2})|(W\d{5})'
218
+ return used_sku_regex
219
+
220
+
221
+ def any_files(folder_path: str) -> bool:
222
+ """
223
+ 给文件地址,判断里面有没有文件,有的话返回True,反之False
224
+ :param folder_path:
225
+ :return:
226
+ """
227
+ for root, dirs, files in os.walk(folder_path):
228
+ if files:
229
+ return True
230
+ return False
231
+
232
+
233
+ def cover_table(df, table_name, conn, server, user, password, database):
234
+ """
235
+ :feature: 清空table_name,添加df至《table_name》
236
+ :return:
237
+ """
238
+ print(f'清空{table_name}')
239
+ sql = 'delete from ' + table_name
240
+ SQLServer(server, user, password, database).exec_query(sql)
241
+ # 导入数据库
242
+ print(f'导入至{table_name}')
243
+ df.to_sql(table_name, conn, if_exists='append', index=False, chunksize=1000)
244
+ print('导入完毕')
245
+
246
+
247
+ def create_dict_agg(df, merge_cols: list, func: str = 'sum'):
248
+ """
249
+ 返回除了merge_cols之外其他列的聚合字典,默认sum
250
+ :param df:
251
+ :param merge_cols:
252
+ :param func:
253
+ :return:
254
+ """
255
+ dict_agg_temp = {}
256
+ for col in df.columns:
257
+ if col not in merge_cols:
258
+ dict_agg_temp[col] = func
259
+ return dict_agg_temp
260
+
261
+
262
+ def excel_process(file_path):
263
+ """
264
+ 调整列宽,冻结首行,添加筛选
265
+ freeze title, adjust width of columns, open filter
266
+ :param file_path: path of file
267
+ :return:
268
+ """
269
+ print('调整列宽,冻结首行,添加筛选')
270
+ # 修改下述参数即可使用,Excel名称及Sheet名称即可
271
+ work_book = openpyxl.load_workbook(file_path)
272
+ for sheet in work_book.sheetnames:
273
+ work_book[sheet].freeze_panes = 'A2'
274
+ work_sheet = work_book[sheet]
275
+ # 设置一个字典用于保存列宽数据
276
+ dim_cols = {}
277
+ # 遍历表格数据,获取自适应列宽数据
278
+ for row in work_sheet.rows:
279
+ for cell in row:
280
+ if cell.value:
281
+ # 遍历整个表格,把该列所有的单元格文本进行长度对比,找出最长的单元格
282
+ # 在对比单元格文本时需要将中文字符识别为1.7个长度,英文字符识别为1个,这里只需要将文本长度直接加上中文字符数量即可
283
+ # re.findall('([\u4e00-\u9fa5])', cell.value)能够识别大部分中文字符
284
+ cell_len = 0.5 * len(re.findall('([\u4e00-\u9fa5])', str(cell.value))) + len(str(cell.value))
285
+ dim_cols[cell.column] = max((dim_cols.get(cell.column, 0), cell_len))
286
+ for col, value in dim_cols.items():
287
+ # 设置列宽,get_column_letter用于获取数字列号对应的字母列号,最后值+2是用来调整最终效果的,限制最小宽度10, 最大宽度为30
288
+ if value > 28:
289
+ work_sheet.column_dim_colensions[get_column_letter(col)].width = 30
290
+ elif value < 8:
291
+ work_sheet.column_dim_colensions[get_column_letter(col)].width = 10
292
+ else:
293
+ work_sheet.column_dim_colensions[get_column_letter(col)].width = value + 2
294
+ dict_num_to_alphabet = {
295
+ 1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J',
296
+ 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T',
297
+ 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z',
298
+ }
299
+ # 获取第一行
300
+ rows = work_sheet.iter_rows(max_row=1, values_only=True)
301
+ max_col = 0
302
+ for cell in rows:
303
+ max_col = len(cell)
304
+ # 对第一行添加过滤功能
305
+ filters = work_sheet.auto_filter
306
+ filters.ref = 'A:' + dict_num_to_alphabet.get(max_col)
307
+ work_book.save(file_path)
308
+ print('end')
309
+ print('$' * 20)
310
+
311
+
312
+ def group_by(df, by, func, dropna: bool = False, as_index: bool = False):
313
+ """
314
+ 聚合
315
+ param: func 可接受格式:
316
+ '账期'
317
+ ['账期', '花费']
318
+ ['账期: max', '花费']
319
+ 默认sum
320
+ """
321
+ func_dict = {}
322
+ if type(func) == str:
323
+ func_dict[func] = 'sum'
324
+ else:
325
+ for unit in func:
326
+ if len(re.split(': ', unit)) == 2:
327
+ func_dict[re.split(': ', unit)[0]] = re.split(': ', unit)[1]
328
+ else:
329
+ func_dict[unit] = 'sum'
330
+
331
+ return df.groupby(by, as_index=as_index, dropna=dropna).agg(func_dict)
332
+
333
+
334
+ def get_eu_sales_rate(
335
+ file_path,
336
+ include_uk=False,
337
+ only_amz=True,
338
+ include_platform=True,
339
+ is_chuangyi=False
340
+ ):
341
+ """
342
+ 从业务数据中获取非英国的欧洲小组在事业部内销售额占比
343
+ :param file_path: 业务数据的文件地址
344
+ :param include_uk: 是否包含英国,默认不包含
345
+ :param only_amz: 是否只取亚马逊数据,默认是
346
+ :param include_platform: 是否包含平台维度,默认是
347
+ :param is_chuangyi: 是否是创意的版本,默认否
348
+ :return : [['平台', '小组', '事业部', 'rate']]
349
+ """
350
+ print('从业务数据中获取欧洲小组在事业部内非英国的销售额占比')
351
+
352
+ if is_chuangyi:
353
+ regex = '.*'
354
+ else:
355
+ regex = '.*-(DE|BE|FR|ES|IT|NL|PL|SE|TR)$'
356
+ if include_uk:
357
+ regex = regex[:-2] + '|UK' + regex[-2:]
358
+ if only_amz:
359
+ regex = '^AMAZON-' + regex
360
+
361
+ # 第一次聚合的维度
362
+ group_cols_1 = ['小组', '事业部', '账期']
363
+ if include_platform:
364
+ group_cols_1 += ['平台', '平台_true']
365
+
366
+ # 第二次聚合的维度
367
+ group_cols_2 = ['账期', '事业部']
368
+ if include_platform:
369
+ group_cols_2 += ['平台']
370
+
371
+ # 小组过滤条件
372
+ regex_group = '欧'
373
+ if is_chuangyi:
374
+ regex_group += '|创'
375
+
376
+ df = pd.read_excel(file_path, usecols=['小组', '销售_原币', '兑美元汇率', '平台', '账期'])
377
+
378
+ df = (
379
+ df
380
+ .loc[lambda d:
381
+ d['平台'].str.contains(regex, regex=True, flags=re.IGNORECASE)
382
+ & d['小组'].str.contains(regex_group, regex=True)
383
+ & ~d['小组'].str.contains('综合') # 去掉综合组
384
+ ]
385
+ .assign(
386
+ sales=lambda d: d['销售_原币'] * d['兑美元汇率'], # 原币销售额转成相同币种
387
+ 事业部=lambda d: np.select(
388
+ [
389
+ d['小组'].str.contains('视'),
390
+ d['小组'].str.contains('办'),
391
+ d['小组'].str.contains('(家.+)|照', regex=True),
392
+ d['小组'].str.contains('创'),
393
+ ],
394
+ [
395
+ '视听',
396
+ '办公',
397
+ '家居',
398
+ '创意',
399
+ ],
400
+ '其他'
401
+ ), # 添加事业部
402
+ 平台_true=lambda d: d['平台'], # 实际的平台
403
+ 平台=lambda d: d['平台'].str[:-2] + 'DE', # 欧洲平台都变成德国
404
+ )
405
+ )
406
+
407
+ df = (
408
+ df
409
+ .groupby(group_cols_1, as_index=False)
410
+ .agg({'sales': 'sum'})
411
+ .assign(
412
+ rate=lambda d: d.groupby(group_cols_2)['sales'].transform(lambda x: x / x.sum())
413
+ )
414
+ .astype({'账期': 'str'})
415
+ .sort_values(group_cols_2)
416
+ [group_cols_1 + ['rate']]
417
+ )
418
+
419
+ return df
420
+
421
+
422
+ def incremental_update(
423
+ df: pd.DataFrame,
424
+ table_name: str,
425
+ merge_cols: list,
426
+ conn: sqlalchemy.engine,
427
+ server,
428
+ user,
429
+ password,
430
+ database,
431
+ dtype: dict = None,
432
+ calculate_cols=None,
433
+ dim_col: str = None
434
+ ):
435
+ """
436
+ 增量更新df至数据库的table_name中
437
+ :param df: 需要被传入的DataFrame
438
+ :param table_name: 需要更新的数据库表名
439
+ :param merge_cols: 匹配的字段列表
440
+ :param conn: 数据库连接
441
+ :param server: 服务器地址
442
+ :param user: 用户名
443
+ :param password: 密码
444
+ :param database: db名
445
+ :param dtype: 特殊字段类型,默认空
446
+ :param calculate_cols: 需要计算的字段,默认空,可接受字符串(单列)、列表(多列)
447
+ :param dim_col: 维度列表,如果存在此参数,则不会根据merge_cols来判断本地与数据库的重复数据,而是通过此参数判断
448
+ """
449
+ # 实例化sqlserver
450
+ sql_server = SQLServer(server, user, password, database)
451
+ # dtype设置为空白字典
452
+ if dtype is None:
453
+ dtype = {}
454
+
455
+ # 获取当前系统用户名
456
+ user = os.getlogin()
457
+ # 重置索引,不然匹配时
458
+ df = df.reset_index(drop=True)
459
+ # 检测数据库中重复记录,有计算列的话会取计算列
460
+ sql = f"select {', '.join(merge_cols)}" + ", id" if merge_cols else f"select {dim_col}" + ", id"
461
+ if calculate_cols:
462
+ if type(calculate_cols) == str:
463
+ sql += f', {calculate_cols}'
464
+ elif type(calculate_cols) == list:
465
+ sql = sql + ', ' + ', '.join(calculate_cols)
466
+ sql += f' from {table_name}'
467
+ df_db = pd.read_sql(sql, conn, dtype=dtype)
468
+
469
+ # 本地文件和数据库都有的数据
470
+ if merge_cols:
471
+ df_inner = df_db.merge(df, 'inner', merge_cols, )
472
+ else:
473
+ df_inner = df_db.loc[lambda d: d['account_perriod'].isin(tuple(df[dim_col].drop_duplicates().values))]
474
+ # 只有本地文件有的数据
475
+ if merge_cols:
476
+ df_not_inner = df.merge(df_inner[merge_cols].assign(mark=1), 'left', merge_cols, ).loc[
477
+ lambda d: d['mark'].isnull()].drop('mark', axis=1)
478
+ else:
479
+ df_not_inner = pd.DataFrame({})
480
+
481
+ nums_db = df_db.shape[0]
482
+ nums_df = df.shape[0]
483
+ nums_inner = df_inner.shape[0]
484
+ nums_only_df = df_not_inner.shape[0]
485
+ print('-' * 50)
486
+ print(f'''数据库中“{table_name}”数据:{nums_db}条''')
487
+ print('-' * 50)
488
+ print(f'''本地数据:{nums_df}条''')
489
+ print('其中:')
490
+ print(f'''\t其中两者都有的:{nums_inner}条''') if nums_inner > 0 else None
491
+ print(f'''\t本地新的:{nums_only_df}条''') if nums_only_df > 0 else None
492
+
493
+ # 有计算列的额外提示语句
494
+ add_info = ''
495
+ c1, c2 = '', ''
496
+ if calculate_cols is not None:
497
+ if type(calculate_cols) == str:
498
+ # c1是数据库的金额,c2是本地文件的金额,如果有比较列,生成有差异的df
499
+ c1, c2 = calculate_cols + '_x', calculate_cols + '_y'
500
+ df_diff = df_db.merge(df, 'outer', merge_cols).loc[lambda d: (d[c1] != d[c2]) & (d[c2].notnull())]
501
+ num_diff = df_diff.shape[0] - nums_inner
502
+ add_info += f'两者都有的但“{calculate_cols}”数值不一致的:{num_diff}条' if num_diff > 0 else ''
503
+ elif type(calculate_cols) == list:
504
+ for calculate_col in calculate_cols:
505
+ c1, c2 = calculate_col + '_x', calculate_col + '_y'
506
+ df_diff = df_db.merge(df, 'outer', merge_cols).loc[lambda d: (d[c1] != d[c2]) & (d[c2].notnull())]
507
+ num_diff = df_diff.shape[0]
508
+ add_info += f'两者都有的但“{calculate_col}”数值不一致的:{num_diff}条\n\t' if num_diff > 0 else ''
509
+
510
+ print(f'''\t{add_info}''') if add_info else None
511
+ print('-' * 50)
512
+
513
+ # 打印出重复信息
514
+ if nums_inner > 5:
515
+ df_print = df_inner.sample(1).reset_index()
516
+ elif nums_inner == 0:
517
+ df_print = pd.DataFrame({})
518
+ else:
519
+ df_print = df_inner.reset_index()
520
+ for index in df_print.index:
521
+ print(df_print.loc[index])
522
+
523
+ info = ('请输入指令以继续(直接回车可跳过):'
524
+ '\n1:清空数据库,再导入本地数据(此操作会清空历史数据,请慎重选择!)'
525
+ '\n2: 只上传非重复记录(只添加新的)'
526
+ '\n3: 删除数据不一致的,再上传非重复记录(更新老的,添加新的)'
527
+ '\n\n')
528
+
529
+ if add_info:
530
+ info += '本地的数据跟数据库的相比,数值有变化,建议选:3'
531
+ else:
532
+ if nums_df == nums_inner:
533
+ info += '本地的数据在数据库都有,建议回车跳过'
534
+ elif nums_df == nums_only_df:
535
+ info += '本地的数据在数据库都没有,建议选:2'
536
+ else:
537
+ info += '本地的数据有部分和数据库重合,建议选:3'
538
+ info += '\n'
539
+ select = input(info)
540
+
541
+ # 要删除的id
542
+ ids_to_be_deleted = df_inner['id'].to_list() if select == '3' else []
543
+ # 要导入的数据,1和3都要导入所有本地数据
544
+ df_upload = df_not_inner if select == '2' else df
545
+
546
+ # 先删除
547
+ if select == '1':
548
+ print(f'清空{table_name}')
549
+ sql_server.exec_query(f'truncate table {table_name}')
550
+ elif select == '3':
551
+ ids_len = len(ids_to_be_deleted)
552
+ if ids_len > 0:
553
+ # 删掉数据库中这些数据
554
+ print(f'删除{table_name}中{ids_len}条记录')
555
+ for i in tqdm(range(0, ids_len, 1000)):
556
+ sql_server.exec_query(f'delete {table_name} where id in {tuple(ids_to_be_deleted[i: i + 999])}')
557
+ print(f'\n{user}删除了{table_name}的{ids_len}条记录')
558
+
559
+ if select != '':
560
+ # 导入数据库
561
+ upload_records = df_upload.shape[0]
562
+ # 再重置一次索引
563
+ df_upload = df_upload.reset_index(drop=True)
564
+ print(f'{upload_records}条记录等待被导入至{table_name}')
565
+ for i in tqdm(range(0, upload_records, 1000)):
566
+ df_upload.loc[i: i + 999].to_sql(f'{table_name}', conn, index=False, if_exists='append')
567
+ print(f'{user}导入了{upload_records}条记录至{table_name}')
568
+ else:
569
+ print('跳过')
570
+
571
+
572
+ def input_password(conn, mark: str = None):
573
+ """输入密码,错误就退出程序"""
574
+ # 返回小组列表
575
+ df_password = pd.read_sql('''select password, group_name, authority from jybb_group_password''', conn)
576
+ while True:
577
+ # 返回小组列表,密码错误返回空列表
578
+ password = input('请输入密码:')
579
+ df_filtered = df_password.loc[lambda d: d['password'] == password][['group_name', 'authority']]
580
+
581
+ if df_filtered.size == 0:
582
+ print('密码错误!请重新输入,如不清楚请联系信息部人员获得密码')
583
+ continue
584
+ else:
585
+ regex = df_filtered.values[0][0]
586
+ print(f'您的小组是:{regex}')
587
+ if mark == 'authority':
588
+ authority = df_filtered.values[0][1]
589
+ print(f'您的权限是:{authority}')
590
+ return regex
591
+
592
+
593
+ def lambda_f(n: str, sep: str = ','):
594
+ """
595
+ 根据输入的关键词返回对应的匿名函数
596
+ :param n: lam_multi_to_unique_single, s: lam_multi_to_single
597
+ :param sep: 分隔符,默认逗号
598
+ :return lambda function
599
+ """
600
+ lam_multi_to_unique_single = lambda x: re.sub('^,|,$', '', sep.join(set(x))) # 多行转唯一一行
601
+ lam_multi_to_single = lambda x: re.sub('^,|,$', '', sep.join(list(x))) # 多行转一行
602
+ return {'us': lam_multi_to_unique_single, 's': lam_multi_to_single}.get(n)
603
+
604
+
605
+ def order_files_exist(path) -> bool:
606
+ """传入文件夹路径,判断里面的非“历史订单”文件夹中是否有文件,有的话返回True,否则返回False"""
607
+ while True:
608
+ for dir_path, _, file_list in os.walk(path):
609
+ if dir_path[-4:] != '历史订单' and file_list != []: # 订单文件夹里文件非空
610
+ return True # 有的话直接返回True
611
+ return False # 检测一遍后没有则返回False
612
+
613
+
614
+ def print_mapping_string(df: pd.DataFrame, duplicated_cols: list, key_word: str) -> None:
615
+ """
616
+ 打印无映射的信息
617
+ :param df: DataFrame
618
+ :param duplicated_cols: 需要去重及打印的字段列表
619
+ :param key_word: 关键词,告知什么东西没有映射
620
+ """
621
+ df = df.drop_duplicates(duplicated_cols).reset_index(drop=True) # 去重后的df
622
+ lens = df.shape[0]
623
+ if lens > 0:
624
+ print(f'以下记录无{key_word}:')
625
+ # 打印要输出的文本
626
+ for i in range(lens):
627
+ # 循环拼接要打印的字符串
628
+ word_list = []
629
+ for k in range(len(duplicated_cols)):
630
+ # df的第0列的第i行
631
+ word_list.append(f'{duplicated_cols[k]}:{df[duplicated_cols[k]].loc[i]}')
632
+ print(','.join(word_list))
633
+
634
+
635
+ def remove_files():
636
+ """移除除了历史文件以外的其他文件,订单、结算单之类"""
637
+ file_list = []
638
+ for path, dirName, fileNames in os.walk('../../raw_data'):
639
+ for fileName in fileNames:
640
+ # print(path)
641
+ if not re.search('history', path):
642
+ file_list.append(os.path.abspath(path + '\\' + fileName).replace('\\', '/'))
643
+
644
+ if not file_list:
645
+ print('无可删除文件')
646
+ else:
647
+ print('这些文件将被删除:')
648
+ for j in file_list:
649
+ # 展示绝对路径
650
+ print(j)
651
+ answer = input('情确认是否删除这些文件?(y/n)')
652
+ if answer == 'y':
653
+ for j in file_list:
654
+ print('从file_list中删除%s' % j)
655
+ os.remove(j)
656
+
657
+
658
+ def save_file(df, file_path, file_name, is_multi_df=False, dict_dfs=None):
659
+ """
660
+ 经营报表第三步保存文件
661
+ :param df:
662
+ :param file_path:
663
+ :param file_name:
664
+ :param is_multi_df: is it multi df step, default False
665
+ :param dict_dfs: map of workbook name to df, default null dict
666
+ """
667
+ file_path = file_path.replace('中间', '结果') + rf'\{file_name}.xlsx'
668
+ # 判断是否是多df类型
669
+ if is_multi_df:
670
+ while True:
671
+ try:
672
+ with pd.ExcelWriter(file_path) as writer:
673
+ for workbook_name in dict_dfs:
674
+ print(f'写入{workbook_name}')
675
+ try:
676
+ dict_dfs[workbook_name].to_excel(writer, index=False, sheet_name=workbook_name)
677
+ except NotImplementedError:
678
+ dict_dfs[workbook_name].to_excel(writer, sheet_name=workbook_name)
679
+ break
680
+ except PermissionError:
681
+ input('文件打开了,请关闭文件后按回车继续保存')
682
+ print('继续')
683
+ continue
684
+
685
+ else:
686
+ if df.shape[0] > 0:
687
+ print(f'保存文件至:{file_path}')
688
+ while True:
689
+ try:
690
+ df.to_excel(file_path, index=False)
691
+ print('导出完毕')
692
+ break
693
+ except PermissionError:
694
+ input('文件打开了,请关闭文件后按回车继续保存')
695
+ print('继续')
696
+ continue
697
+ else:
698
+ print(f'{file_name}无有效数据,跳过导出')
699
+
700
+
701
+ def share_multisku_fees(
702
+ df: pd.DataFrame,
703
+ groupby_cols: list,
704
+ sku_raw_col: str,
705
+ calculate_cols: list,
706
+ conn_152: sqlalchemy.engine
707
+ ) -> pd.DataFrame:
708
+ """
709
+ 用成本价来计算单品sku占组合的比例,继而分摊费用
710
+ :param df:
711
+ :param groupby_cols: 聚合维度,必须包含sku_raw_col
712
+ :param sku_raw_col: sku原始列名,例如[品号-归属]
713
+ :param calculate_cols: 费用列
714
+ :param conn_152:
715
+ """
716
+ print('拆分组合sku,根据单品成本价在组合件内占比分摊费用')
717
+ # 1 从数据库获取sku成本价
718
+ mapping_cost = pd.read_sql(
719
+ '''
720
+ select sku, 出厂价, location
721
+ from 参数2_成本头程关税
722
+ ''', conn_152
723
+ )
724
+
725
+ # 2 生成agg的字典
726
+ dict_agg = {}
727
+ for col in calculate_cols:
728
+ dict_agg[col] = 'sum'
729
+
730
+ # 3 拆分组合件,根据成本价计算sku占比,分摊费用
731
+ df = (
732
+ df
733
+ .groupby(groupby_cols, as_index=False, dropna=False)
734
+ .agg(dict_agg)
735
+ .assign(
736
+ sku=lambda d: d[sku_raw_col].str.split('[+]')
737
+ )
738
+ .explode('sku')
739
+ .assign(
740
+ qty=lambda d: d['sku'].str.split('x').str[1].fillna(1).astype('int'),
741
+ sku=lambda d: d['sku'].str.split('x').str[0],
742
+ location=lambda d: np.where(d['sku'].str[-2:] == 'UK', 'uk', 'normal')
743
+ )
744
+ .merge(mapping_cost, 'left', ['sku', 'location'])
745
+ .assign(
746
+ # 出厂价填充空值为1,乘以数量
747
+ 出厂价=lambda d: d['出厂价'].fillna(1) * d['qty'],
748
+ # 没有sku的话,要填充为1,不然费用没了
749
+ rate=lambda d: d.groupby(groupby_cols)['出厂价'].transform(lambda x: x / x.sum()).fillna(1),
750
+ )
751
+ )
752
+
753
+ # 4 要分摊的字段乘以比例
754
+ for col in calculate_cols:
755
+ df[col] = df[col] * df['rate']
756
+
757
+ # 5 聚合
758
+ groupby_cols.remove(sku_raw_col) # 删掉原sku名字
759
+ groupby_cols.append('sku') # 加上sku作为维度
760
+ df = (
761
+ df
762
+ # 删掉原来的sku名称,加上sku
763
+ .groupby(groupby_cols, dropna=False)
764
+ .agg(dict_agg)
765
+ )
766
+
767
+ return df
768
+
769
+
770
+ def unzip_file(zip_file_path, dest_path):
771
+ with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
772
+ zip_ref.extractall(dest_path)
773
+
774
+
775
+ def update_exchange_rate(server, user, password, database):
776
+ """
777
+ 复制最新汇率为上月汇率
778
+ """
779
+ sql = '''
780
+ declare @last_month date, @current_month date
781
+ set @last_month = cast(dateadd(mm, -1, dateadd(dd,-day(getdate())+1,getdate())) as date)
782
+ set @current_month = cast(dateadd(dd,-day(getdate())+1,getdate()) as date)
783
+ delete 汇率_月 where 月份 = @current_month --删除当月汇率
784
+ insert into 汇率_月
785
+ select
786
+ 币种, 兑美元汇率, 月份=@current_month, 兑人民币汇率,
787
+ 货币键=left(货币键, len(货币键)-6) + cast(year(@current_month)*100+month(@current_month) as varchar(6)),
788
+ 国家, 排序
789
+ from 汇率_月
790
+ where 月份 = @last_month
791
+ '''
792
+ sql_server = SQLServer(server, user, password, database)
793
+ sql_server.exec_query(sql)
794
+
795
+
@@ -0,0 +1,17 @@
1
+ Metadata-Version: 2.1
2
+ Name: ailuoge
3
+ Version: 1.0.7
4
+ Summary: ailuoge
5
+ Author: ailuoge
6
+ Author-email:
7
+ Keywords: python,ailuoge
8
+ Classifier: Development Status :: 1 - Planning
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: Unix
12
+ Classifier: Operating System :: MacOS :: MacOS X
13
+ Classifier: Operating System :: Microsoft :: Windows
14
+ Description-Content-Type: text/markdown
15
+
16
+
17
+ # ilog
@@ -0,0 +1,6 @@
1
+ setup.py
2
+ ailuoge/__init__.py
3
+ ailuoge.egg-info/PKG-INFO
4
+ ailuoge.egg-info/SOURCES.txt
5
+ ailuoge.egg-info/dependency_links.txt
6
+ ailuoge.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ ailuoge
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
ailuoge-1.0.7/setup.py ADDED
@@ -0,0 +1,36 @@
1
+ import codecs
2
+ import os
3
+ from setuptools import setup, find_packages
4
+
5
+ # these things are needed for the README.md show on pypi
6
+ here = os.path.abspath(os.path.dirname(__file__))
7
+
8
+ with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh:
9
+ long_description = "\n" + fh.read()
10
+
11
+
12
+ VERSION = '1.0.7'
13
+ DESCRIPTION = 'ailuoge'
14
+ LONG_DESCRIPTION = 'fixed the page of get ec data'
15
+
16
+ # Setting up
17
+ setup(
18
+ name="ailuoge",
19
+ version=VERSION,
20
+ author="ailuoge",
21
+ author_email="",
22
+ description=DESCRIPTION,
23
+ long_description_content_type="text/markdown",
24
+ long_description=long_description,
25
+ packages=find_packages(),
26
+ install_requires=[],
27
+ keywords=['python','ailuoge'],
28
+ classifiers=[
29
+ "Development Status :: 1 - Planning",
30
+ "Intended Audience :: Developers",
31
+ "Programming Language :: Python :: 3",
32
+ "Operating System :: Unix",
33
+ "Operating System :: MacOS :: MacOS X",
34
+ "Operating System :: Microsoft :: Windows",
35
+ ]
36
+ )