Glider
Loading...
Searching...
No Matches
ColumnsManager.py
Go to the documentation of this file.
1from TemplateManager import TemplateManager
2from ErrorHandler import *
3# from copy import deepcopy
4from pandas import DataFrame, to_datetime
5
7 """
8 Builds a homogenized dataframe according to ottomapping field of each format in out database
9 Checks each ftype and applies different instructions depending of ftype:
10 ftype = header: Creates a new columns using field "name".
11 ftype = fixed_value: Creates a new column which contains only field "value".
12 ftype = custom_code: Creates a new column using the column given by format_type.
13 ftype = ignore: Creates a empty column using field "name".
14 """
15
16 def ftypes(self, df, ftype, Format, filename):
17 """
18 Maps the columns from almost any template to otto template
19
20 Args:
21 df (pandas dataframe): usage report loaded as dataframe
22 ftype (list): a list of each column info
23 Format (str): template format
24 filename (str): current filename
25 Returns: df_otto (pandas dataframe)
26 """
27 df_otto = DataFrame()
28 for i in ftype:
29 print(i)
30 if i["ftype"] == "header":
31 if i["name"] == "date" and df[i["value"].lower()].dtype == int:
32 df[i["value"].lower()] = to_datetime(df[i["value"].lower()], format='%Y%m%d')
33 df_otto[i["name"]] = df[i["value"].lower()].astype(i["dtype"])
34 elif i["ftype"] == "fixed_value":
35 df_otto[i["name"]] = i["value"]
36 elif i["ftype"] == "custom_code":
37 if i["name"] == "date":
38 date = TemplateManager.getDate(df=df, type_format=Format, filename=filename)
39 df_otto[i["name"]] = date
40 df_otto[i["name"]] = df_otto[i["name"]].astype("datetime64[ns]")
41 else:
42 TemplateManager.getType(df=df, type_format=Format, filename=filename)
43 df_otto[i["name"]] = df[i["value"].lower()]
44 elif i["ftype"] == "ignore":
45 df_otto[i["name"]] = None
46 # TemplateManager.set_full_type(df_otto)
47 df_otto["full_type"] = df_otto.apply(lambda Row: TemplateManager.full_type(Row["type"]), axis=1)
48 return df_otto
49
50 def currency_type(self, currency_col, currency):
51 """
52 Gives a value to each currency
53
54 Args:
55 currency_col (pandas Series): column assigned to currency
56 exchange (dict): it contains all exchange rates for current report
57 Returns: exchange (float)
58 """
59 for item in currency:
60 if item["currency"] == currency_col:
61 # values.append(item["value"])
62 return item["period"], item["value"]
63 # return exchange[currency_col]
64
65 def add_currency_exception(self, service_col, currency_col, currency_exceptions):
66 """
67 Adds currency_exception column to current dataframe iff it is indicate in the input
68
69 Args:
70 service_col (pandas Series): column assigned to service/dsp
71 currency_col (pandas Series): column assigned to currency
72 currency_exceptions (dict): it contains all currency exceptions for current report
73 Returns: period (str), currency_value(float)
74 """
75 for item in currency_exceptions:
76 if item["dsp"].lower() == service_col:
77 currency_value = [currency["value"] for currency in item["currencies"] if currency["currency"] == currency_col][0]
78 # print(item["period"], currency_value)
79 period = item["period"]
80 return period, currency_value
81
82 def add_exchange(self, df, exchange, currency_exception=None):
83 """
84 Adds exchange_rate and total_local columns to current dataframe
85
86 Args:
87 df (pandas dataframe): usage report loaded as dataframe
88 exchange (dict): it contains all exchange rates for current report
89 currency_exception (dict): it contains all currency exception rates for current report (if it needs them)
90 Returns: df (pandas dataframe)
91 """
92 try:
93 if currency_exception:
94 dsp = [i["dsp"].lower() for i in currency_exception]
95 df['currency_period'], df['exchange_rate'] = zip(*df.apply(lambda Row: self.add_currency_exception(Row['service_id'].lower(), Row['currency'], currency_exception) if Row['service_id'].lower() in dsp else self.currency_type(Row['currency'], exchange), axis=1))
96 else:
97 df['currency_period'], df["exchange_rate"] = zip(*df.apply(lambda Row: self.currency_type(Row['currency'], exchange), axis=1))
98 df["total_local"] = df["exchange_rate"]*df["total_foreign"]
99 return df
100 except Exception:
101 return "There are currencies not found in your file, please check currencies"
102
103 def discount(self, df):
104 """
105 Makes a discount for selected templates
106
107 Args:
108 df (pandas dataframe): usage report loaded as dataframe
109 Returns: df (pandas dataframe)
110 """
111 df["total_local"] = df["total_local"]-(df["total_local"]*0.015)
112 return df
113
114 def add_info_columns(self, *args):
115 df = args[0]
116 filename = args[1]
117 client_id = args[2]
118 base_currency = args[3]
119
120 from datetime import date
121 df["accounting_date"] = date.today()
122 df["accounting_date"] = df["accounting_date"].astype("datetime64[ns]")
123 df["full_total_foreign"] = df["total_local"]
124 df["file"] = filename
125 df["client_id"] = client_id
126 df["foreign_currency"] = df["currency"]
127 df["local_currency"] = base_currency
128 # df["line"] = list(df.index)
129
130 if "total_gross" in df.columns and "total_net" in df.columns:
131 df = df[["release_id","isrc_id", "quantity", "total_foreign", "territory_code", "date", "release_title", "track_title", "label_id", "artists",
132 "currency", "type", "service_id", "file", "accounting_date", "exchange_rate", "currency_period", "total_local", "total_gross", "total_net", "client_id"] ]
133 else:
134 df = df[["release_id","isrc_id", "quantity", "total_foreign", "territory_code", "date", "release_title", "track_title", "label_id", "artists",
135 "currency", "type", "full_type", "service_id", "file", "accounting_date", "full_total_foreign", "exchange_rate", "local_currency",
136 "foreign_currency", "currency_period", "total_local", "client_id"] ]
137 return df
ftypes(self, df, ftype, Format, filename)
currency_type(self, currency_col, currency)
add_exchange(self, df, exchange, currency_exception=None)
add_currency_exception(self, service_col, currency_col, currency_exceptions)