1from TemplateManager
import TemplateManager
2from ErrorHandler
import *
4from pandas
import DataFrame, to_datetime
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".
16 def ftypes(self, df, ftype, Format, filename):
18 Maps the columns from almost any template to otto template
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)
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]")
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
47 df_otto[
"full_type"] = df_otto.apply(
lambda Row: TemplateManager.full_type(Row[
"type"]), axis=1)
52 Gives a value to each currency
55 currency_col (pandas Series): column assigned to currency
56 exchange (dict): it contains all exchange rates for current report
57 Returns: exchange (float)
60 if item[
"currency"] == currency_col:
62 return item[
"period"], item[
"value"]
67 Adds currency_exception column to current dataframe iff it is indicate in the input
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)
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]
79 period = item[
"period"]
80 return period, currency_value
84 Adds exchange_rate and total_local columns to current dataframe
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)
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))
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"]
101 return "There are currencies not found in your file, please check currencies"
105 Makes a discount for selected templates
108 df (pandas dataframe): usage report loaded as dataframe
109 Returns: df (pandas dataframe)
111 df[
"total_local"] = df[
"total_local"]-(df[
"total_local"]*0.015)
118 base_currency = args[3]
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
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"] ]
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"] ]
ftypes(self, df, ftype, Format, filename)
currency_type(self, currency_col, currency)
add_exchange(self, df, exchange, currency_exception=None)
add_info_columns(self, *args)
add_currency_exception(self, service_col, currency_col, currency_exceptions)