-
Notifications
You must be signed in to change notification settings - Fork 4
/
sku-gen.py
163 lines (110 loc) · 6.57 KB
/
sku-gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import pandas as pd
import os
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) # Identifying path
colors = pd.read_csv(os.path.join(__location__, 'CHARTS/colors.csv')) # Importing Chart of Colors
models = pd.read_csv(os.path.join(__location__, 'CHARTS/categories-subcategories.csv')) # Importing Chart of Models
def colorist(file): # Function to switch Colors to Color Codes
fake_id_parts = pd.DataFrame(file, columns = ['U_COLOR'])
l = len(fake_id_parts)
i = 0
for color_list in fake_id_parts['U_COLOR']:
coded_colors = []
#print(color_list)
#print(type(color_list))
for color in str(color_list).split(','):
if i < l:
#print(color)
st = str.strip(color)
#print(color)
c = colors.loc[colors['Name'] == st]
if len(c) > 0:
a = str(c.iloc[0,0])
coded_colors.append(a)
else:
coded_colors.append('')
coded_colors = str(coded_colors)
coded_colors = coded_colors.replace('\'','')
coded_colors = coded_colors.replace('[','')
coded_colors = coded_colors.replace(']','')
print(coded_colors)
#print(z)
file.at[i, 'COLOR'] = coded_colors
print('=====================')
print('ItemCode ' + str(file.at[i, 'ItemCode']) + ' |COLOR CODE: ' + coded_colors)
#print(z)
i += 1
file.to_csv((os.path.join(__location__,'Results/COLORIST_result.csv'))) #save results
print('============================================================')
print(str(i) + ' ITEMS COLORS CODED' )
print('COLORIST FUNCTION DONE CODING COLORS!')
print('RESULT SAVED AS COLORIST_result.csv')
print('============================================================')
def sku_gen(column, file, sep): #main function for SKU Generation
data_column = pd.DataFrame(file, columns= [column], dtype = str) #taking one column from file to use as main reference for SKU Generation
index = 0
print('============================================================')
print(str(len(data_column)) + ' ITEMS HAS BEEN RECEIVED. ' + column + ' GENERATION IS COMMENCING')
print('============================================================')
for column_name, item in data_column.iteritems(): #iteration through data columns (2 columns: index and variables)
i = - 1 #index for new items
add_counter = 0 #counter for cases when SKU can be rewritten in the same row instead of copying
for variables in item.tolist(): #iteration through variables column
variable_as_str = str(variables)
variables_list = variable_as_str.split(',') #split variables
if len(variables_list) > 1 and ( file.at[index, 'ItemCode'] != 'USED'): # Check if the variable is a list of variables
for var in variables_list : #if yes make a copy of the row and add variable in the column instead of the list of variables
#if '.' in var:
#var = var.split('.')[0]
print(var)
file.loc[i] = file.iloc[index]
file.at[i, 'ItemCode'] = str.strip(str(file.at[i, 'ItemCode']))+sep+str.strip(var)
file.at[i, column] = str.strip(var)
print(file.at[i, 'ItemCode'])
print(column + ' GENERATED')
print('===============================================')
i -= 1
file.at[index, 'ItemCode'] = 'USED'
elif (variables_list[0] != 'nan') and ( file.at[index, 'ItemCode'] != 'USED'): #Check is there a variable
if '.' in variable_as_str:
variable_as_str = variable_as_str.split('.')[0]
file.at[index, 'ItemCode'] = str.strip(str(file.at[index, 'ItemCode']))+sep+str.strip(variable_as_str)
add_counter = add_counter + 1 #if yes add value from column to ItemCode
print('--------------------')
print(file.at[index, 'ItemCode'])
print(column+' ADDED')
index += 1
amount = int(str(i)[1:]) - 1
am = str(amount)
print('-----------------------------------------------------------------------------------')
print(column+' GENERATED: ' + am + ' SKUs')
print(column+' ADDED: ' + str(add_counter) + ' SKUs')
print('-----------------------------------------------------------------------------------')
file.to_csv((os.path.join(__location__,'Results/'+column+'_result.csv')))
file_str = 'sku_gen_template.csv'
#description_edit(immport_file)
immport_file = pd.read_csv(os.path.join(__location__, file_str))
#immport_file = pd.read_csv(os.path.join(__location__, 'MODELIST_result.csv'))
colorist(immport_file)
immport_file = pd.read_csv(os.path.join(__location__, file_str))
sku_gen('SupplierCatalogNo', immport_file , '')
immport_file = pd.read_csv(os.path.join(__location__, 'Results/SupplierCatalogNo_result.csv'))
sku_gen('U_SIZE', immport_file, '-' )
immport_file = pd.read_csv(os.path.join(__location__, 'Results/U_SIZE_result.csv'))
sku_gen('U_VARIABLE', immport_file, '-' )
immport_file = pd.read_csv(os.path.join(__location__, 'Results/U_VARIABLE_result.csv'))
sku_gen('OC', immport_file, '-' )
immport_file = pd.read_csv(os.path.join(__location__, 'Results/OC_result.csv'))
sku_gen('U_LOGO', immport_file,'-' )
immport_file = pd.read_csv(os.path.join(__location__, 'Results/U_LOGO_result.csv'))
immport_file = immport_file[immport_file['ItemCode'] != 'USED'] #delete parent items
immport_file.to_csv('Results/final'+'_result.csv') #save results
print('===================================')
print('GENERATION IS COMPLETED')
print('===================================')
immport_file = pd.read_csv(os.path.join(__location__, 'Results/final_result.csv'))
for col in immport_file:
#print(col)
if 'Unnamed' in str(col):
#print('c')
immport_file.drop(col,inplace=True, axis=1)
immport_file.to_csv(file_str[:-4] + '_result.csv')