-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp005.py
69 lines (58 loc) · 2.64 KB
/
cp005.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
import pandas as pd
def create_queries_for_ruleitems_df_cp005(ruleitems_df_cp005):
"""
Updates 'other_template_query' column in the DataFrame by replacing placeholders
with specific values based on column values or predefined expressions.
Args:
ruleitems_df_cp005: Input Pandas DataFrame with rule items.
Returns:
Updated Pandas DataFrame.
"""
# Helper function to replace placeholder if the column `col_name` is not null.
def replace_placeholder(df, col_name, placeholder):
for index, row in df.iterrows():
if pd.notnull(row[col_name]):
df.at[index, 'other_template_query'] = row['other_template_query'].replace(
f"<{placeholder}>", str(row[col_name])
)
return df
# Define a dictionary for column-to-placeholder mapping
column_replacements = {
'JOIN_CONDITION': 'JOIN_CONDITION',
'RULE_EXPRESSION': 'RULE_EXPRESSION',
'LIST_OF_VALUES': 'LIST_OF_VALUES',
'RANGE_EXPRESSION': 'RANGE_EXPRESSION',
'DIMENSION_KEY': 'DIMENSION_KEY',
'BUSINESS_GROUP_LOCATION': 'BUSINESS_GROUP_LOCATION',
'DATA_FILTER_EXPRESSION': 'DATA_FILTER_EXPRESSION',
'DQ_COUNT_FILTER_EXPRESSION1': 'DQ_COUNT_FILTER_EXPRESSION1',
'FILTER_OUT_CONDITION1': 'FILTER_OUT_CONDITION1'
}
# Apply placeholder replacements iteratively
for col_name, placeholder in column_replacements.items():
ruleitems_df_cp005 = replace_placeholder(ruleitems_df_cp005, col_name, placeholder)
# Define static replacements for placeholders
static_replacements = {
'<BUSINESS_DATE_FILTER>': "BUSINESS_DATE = :",
'<PARALLEL_FILTER>': "PARALLEL_ID = :",
'<CURR_BUSINESS_DATE_FILTER>': "BUSINESS_DATE = :",
'<TABLE_NAME>': "TABLE_NAME",
'<ATTRIBUTE_NAME>': "ATTRIBUTE_NAME",
'<PREVIOUS_DATE_FILTER>': "business_date = prev_date"
}
# Apply static replacements
for placeholder, replacement in static_replacements.items():
ruleitems_df_cp005['other_template_query'] = ruleitems_df_cp005['other_template_query'].str.replace(
placeholder, replacement, regex=False
)
return ruleitems_df_cp005
# Example usage with a sample DataFrame
data = {
'other_template_query': ['<JOIN_CONDITION> is applied', '<RULE_EXPRESSION> check', '<BUSINESS_DATE_FILTER>'],
'JOIN_CONDITION': ['join_condition_value', None, None],
'RULE_EXPRESSION': [None, 'rule_expression_value', None],
}
ruleitems_df_cp005 = pd.DataFrame(data)
# Call the function
updated_df = create_queries_for_ruleitems_df_cp005(ruleitems_df_cp005)
print(updated_df)