Skip to content

Commit fcc347e

Browse files
committed
refactoring Translator to remove pandas library dependency
1 parent a9b319f commit fcc347e

File tree

6 files changed

+47
-178
lines changed

6 files changed

+47
-178
lines changed

docs/releases.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
This page gathered the changes made between version of the launchpy module.\
44
This has been started after the 0.3.0 release.\
55

6+
## 0.4.3
7+
* Refactor `Translator` class for removing `pandas` dependency
8+
69
## 0.4.2
710
* Supporting the Oauth Token V2 authentication
811

docs/translator.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Also it is possible to use it not only between 2 properties but between 2 Organi
99
## Starting with translator class
1010

1111
The instantiation of the translator class is very basic as you would just need to call for it.
12-
What is really required is the base property extensions and rules. The id of the elements from your mother property.
12+
What is really required is the base property extensions and rules. The id of the elements from your "mother" property.
1313
This is an assumption that I have when I created this facilitator, you would have a blueprint that you will use to copy elements from.
1414
The translator class provide 2 methods to add base rule and base extension ids:
1515

@@ -25,7 +25,7 @@ The translator class provide 2 methods to add base rule and base extension ids:
2525

2626
## Set Target Rule and Extensions ids
2727

28-
Once you have loaded the base Extension and Rules ids, the method will start to expand the table into multiple columns for matching. Underlying is the use of pandas dataframe to realize that matching.
28+
Once you have loaded the base Extension and Rules ids, the method will start to expand the table into multiple columns for matching.
2929
You will need to be consistent with the naming of your target in order for the method to work properly.
3030
The methods that allow extension of your table are the following:
3131

launchpy/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.4.2-1"
1+
__version__ = "0.4.3"

launchpy/launchpy.py

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -410,56 +410,42 @@ class Translator:
410410
"""
411411

412412
def __init__(self):
413-
self.rules = pd.DataFrame()
414-
self.extensions = pd.DataFrame()
413+
self.rules = {}
414+
self.baseRuleIdName = {}
415+
self.extensions = {}
416+
self.baseExtensionIdName = {}
415417

416-
def setBaseExtensions(self, extensions: list=None, property_name: str=None)->None:
418+
def setBaseExtensions(self, base_property_extensions: list, property_name: str):
417419
"""
418420
Pass all the extensions from the base property to start building the table.
419421
Arguments:
420-
extensions_base : REQUIRED : list of all extensions retrieve through getExtensions method
422+
base_property_extensions : REQUIRED : list of all extensions retrieve through getExtensions method
421423
property_name : REQUIRED : name of your base property.
422424
"""
423-
if extensions is None or type(extensions)!= list:
424-
raise ValueError("Require a list of extensions to be loaded")
425-
if property_name is None:
426-
raise ValueError("Require the main property name")
427-
df = pd.DataFrame(extensionsInfo(extensions)).T
428-
df = pd.DataFrame(df['id'])
429-
df.columns = [property_name]
430-
self.extensions = df
425+
self.baseExtensionIdName = {ext['id'] : ext['attributes']['name'] for ext in base_property_extensions}
426+
self.extensions = {ext['attributes']['name']:{property_name:ext['id']} for ext in base_property_extensions}
431427

432-
def extendExtensions(self, extensions: list=None, property_name: str=None)-> None:
428+
def extendExtensions(self, new_property_extensions: list, new_prop_name: str)-> None:
433429
"""
434430
Add the extensions id from a target property.
435431
Arguments:
436-
extensions: REQUIRED : the extension list from your target property.
437-
property_name : REQUIRED : target property name.
432+
new_property_extensions: REQUIRED : the extension list from your target property.
433+
new_prop_name : REQUIRED : target property name.
438434
"""
439-
if extensions is None or type(extensions)!= list:
440-
raise ValueError("Require a list of extensions to be loaded")
441-
if property_name is None:
442-
raise ValueError("Require the main property name")
443-
df = pd.DataFrame(extensionsInfo(extensions)).T
444-
df = pd.DataFrame(df['id'])
445-
self.extensions[property_name] = df
435+
for ext in new_property_extensions:
436+
if ext['attributes']['name'] in list(self.extensions.keys()):
437+
self.extensions[ext['attributes']['name']][new_prop_name] = ext['id']
446438
return self.extensions
447439

448-
def setBaseRules(self, rules: list=None, property_name: str=None)->None:
440+
def setBaseRules(self, base_property_rules: list, property_name: str):
449441
"""
450442
Pass all the rules from the base property to start building the table.
451443
Arguments:
452-
rules : REQUIRED : list of all rules retrieve through getExtensions method
444+
base_property : REQUIRED : list of all rules retrieve through getExtensions method
453445
property_name : REQUIRED : name of your base property.
454446
"""
455-
if rules is None or type(rules)!= list:
456-
raise ValueError("Require a list of rules to be loaded")
457-
if property_name is None:
458-
raise ValueError("Require the main property name")
459-
df = pd.DataFrame(rulesInfo(rules)).T
460-
df = pd.DataFrame(df['id'])
461-
df.columns = [property_name]
462-
self.rules = df
447+
self.baseRuleIdName = {rule['id']:rule['attributes']['name'] for rule in base_property_rules}
448+
self.rules = {rule['attributes']['name']:{property_name:rule['id']} for rule in base_property_rules}
463449

464450
def extendBaseRules(self,ruleName:str=None,ruleId:str=None,property_name: str=None)->None:
465451
"""
@@ -474,20 +460,20 @@ def extendBaseRules(self,ruleName:str=None,ruleId:str=None,property_name: str=No
474460
raise ValueError("Require a rule name to be loaded")
475461
if property_name is None:
476462
raise ValueError("Require the main property name")
477-
temp_df = pd.DataFrame.from_dict({property_name:{ruleName:ruleId}})
478-
self.rules = self.rules.append(temp_df)
463+
self.baseRuleIdName[ruleId] = ruleName
464+
self.rules[ruleName][property_name] = ruleId
479465

480466

481-
def extendRules(self, rules: list=None, property_name: str=None):
467+
def extendRules(self, new_property_rules: list, new_prop_name: str):
482468
"""
483-
Add the rule id from a target property.
469+
Add the extensions id from a target property.
484470
Arguments:
485-
rules: REQUIRED : the rules list from your target property.
486-
property_name : REQUIRED : target property name.
471+
new_property_rules: REQUIRED : the rules list from your target property.
472+
new_prop_name : REQUIRED : target property name.
487473
"""
488-
df = pd.DataFrame(rulesInfo(rules)).T
489-
df = pd.DataFrame(df['id'])
490-
self.rules[property_name] = df
474+
for rule in new_property_rules:
475+
if rule['attributes']['name'] in list(self.rules.keys()):
476+
self.rules[rule['attributes']['name']][new_prop_name] = rule['id']
491477
return self.rules
492478

493479
def extendTargetRules(self,ruleName:str=None,ruleId:str=None,property_name: str=None)->None:
@@ -504,10 +490,9 @@ def extendTargetRules(self,ruleName:str=None,ruleId:str=None,property_name: str=
504490
raise ValueError("Require a rule name to be loaded")
505491
if property_name is None:
506492
raise ValueError("Require the main property name")
507-
temp_df = pd.DataFrame.from_dict({property_name:{ruleName:ruleId}})
508-
self.rules.update(temp_df)
493+
self.rules[ruleName][property_name] = ruleId
509494

510-
def translate(self, target_property: str=None, data_element: dict = None, rule_component: dict = None)->dict:
495+
def translate(self, target_property: str, data_element: dict = None, rule_component: dict = None)->dict:
511496
"""
512497
change the id from the base element to the new property.
513498
Pre checked should be done beforehands (updating Extension & Rules elements)
@@ -516,39 +501,32 @@ def translate(self, target_property: str=None, data_element: dict = None, rule_c
516501
data_element : OPTIONAL : if the elements passed are data elements
517502
rule_component : OPTIONAL : if the elements passed are rule components
518503
"""
519-
if target_property is None:
520-
raise ValueError("Require the target property name")
521-
if data_element is None and rule_component is None:
522-
raise AttributeError("Require at least data element or rule component to be passed")
523-
if self.extensions.empty == True:
504+
if len(self.extensions) == 0:
524505
raise AttributeError(
525506
"You didn't import the base extensions or the target extensions")
526507
if data_element is not None:
527508
new_de = deepcopy(data_element)
528509
base_id = new_de['extension']['id']
529-
row = self.extensions[self.extensions.iloc[:, 0]
530-
== base_id].index.values[0]
531-
new_value = self.extensions.loc[row, target_property]
532-
new_de['extension']['id'] = new_value
510+
based_ext_name = self.baseExtensionIdName[base_id]
511+
new_extension_id = self.extensions[based_ext_name][target_property]
512+
new_de['extension']['id'] = new_extension_id
533513
return new_de
534514
elif rule_component is not None:
535-
if self.rules.empty == True:
515+
if len(self.rules) == 0:
536516
raise AttributeError(
537517
"The rules have not been imported, the rule id needs to be changed")
538518
new_rc = deepcopy(rule_component)
539519
base_id = new_rc['extension']['id']
540-
row = self.extensions[self.extensions.eq(
541-
base_id).any(1)].index.values[0]
542-
new_value = self.extensions.loc[row, target_property]
543-
new_rc['extension']['id'] = new_value
544-
if self.rules.empty == False:
520+
based_ext_name = self.baseExtensionIdName[base_id]
521+
new_extension_id = self.extensions[based_ext_name][target_property]
522+
new_rc['extension']['id'] = new_extension_id
523+
if len(self.rules) > 0:
545524
new_rc['rule_setting'] = {
546525
'data': [{
547-
'id': self.rules.loc[rule_component['rule_name'], target_property],
526+
'id': self.rules[rule_component['rule_name']][target_property],
548527
'type':'rules'}
549528
]}
550-
new_rc['rule_id'] = self.rules.loc[rule_component['rule_name'],
551-
target_property]
529+
new_rc['rule_id'] = self.rules[rule_component['rule_name']][target_property]
552530
else:
553531
print(
554532
"You didn't load the rules. Please use setExtensions and setRules, and extendExtensions and extendRules")

launchpy/property.py

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,118 +1492,6 @@ def copySettings(data: object)->object:
14921492
return obj
14931493

14941494

1495-
class Translator:
1496-
"""
1497-
A class to store the translator dataframe for extensions ids.
1498-
It has multiple methods, you should set the Extensions and the Rules.
1499-
1. setBaseExtensions
1500-
2. extendExtensions
1501-
--> You can use the translate method to translate Data Element settings
1502-
3. setBaseRules
1503-
4. extendRules
1504-
--> You can use the translate method to translate Rule Components settings
1505-
"""
1506-
1507-
def __init__(self):
1508-
self.rules = pd.DataFrame()
1509-
self.extensions = pd.DataFrame()
1510-
1511-
def setBaseExtensions(self, base_property_extensions: object, property_name: str):
1512-
"""
1513-
Pass all the extensions from the base property to start building the table.
1514-
Arguments:
1515-
base_property : REQUIRED : list of all extensions retrieve through getExtensions method
1516-
property_name : REQUIRED : name of your base property.
1517-
"""
1518-
df = pd.DataFrame(extensionsInfo(base_property_extensions)).T
1519-
df = pd.DataFrame(df['id'])
1520-
df.columns = [property_name]
1521-
self.extensions = df
1522-
1523-
def extendExtensions(self, new_property_extensions: object, new_prop_name: str)-> None:
1524-
"""
1525-
Add the extensions id from a target property.
1526-
Arguments:
1527-
new_property_extensions: REQUIRED : the extension list from your target property.
1528-
new_prop_name : REQUIRED : target property name.
1529-
"""
1530-
df = pd.DataFrame(extensionsInfo(new_property_extensions)).T
1531-
df = pd.DataFrame(df['id'])
1532-
self.extensions[new_prop_name] = df
1533-
return self.extensions
1534-
1535-
def setBaseRules(self, base_property_rules: object, property_name: str):
1536-
"""
1537-
Pass all the rules from the base property to start building the table.
1538-
Arguments:
1539-
base_property : REQUIRED : list of all rules retrieve through getExtensions method
1540-
property_name : REQUIRED : name of your base property.
1541-
"""
1542-
df = pd.DataFrame(rulesInfo(base_property_rules)).T
1543-
df = pd.DataFrame(df['id'])
1544-
df.columns = [property_name]
1545-
self.rules = df
1546-
1547-
def extendRules(self, new_property_rules: object, new_prop_name: str):
1548-
"""
1549-
Add the extensions id from a target property.
1550-
Arguments:
1551-
new_property_rules: REQUIRED : the rules list from your target property.
1552-
new_prop_name : REQUIRED : target property name.
1553-
"""
1554-
df = pd.DataFrame(rulesInfo(new_property_rules)).T
1555-
df = pd.DataFrame(df['id'])
1556-
self.rules[new_prop_name] = df
1557-
return self.rules
1558-
1559-
def translate(self, target_property: str, data_element: dict = None, rule_component: dict = None)->dict:
1560-
"""
1561-
change the id from the base element to the new property.
1562-
Pre checked should be done beforehands (updating Extension & Rules elements)
1563-
Arguments:
1564-
target_property : REQUIRED : property that is targeted to translate the element to
1565-
data_element : OPTIONAL : if the elements passed are data elements
1566-
rule_component : OPTIONAL : if the elements passed are rule components
1567-
"""
1568-
if self.extensions.empty == True:
1569-
raise AttributeError(
1570-
"You didn't import the base extensions or the target extensions")
1571-
if data_element is not None:
1572-
new_de = deepcopy(data_element)
1573-
base_id = new_de['extension']['id']
1574-
row = self.extensions[self.extensions.iloc[:, 0]
1575-
== base_id].index.values[0]
1576-
new_value = self.extensions.loc[row, target_property]
1577-
new_de['extension']['id'] = new_value
1578-
return new_de
1579-
elif rule_component is not None:
1580-
if self.rules.empty == True:
1581-
print(
1582-
"The rules have not been imported, the rule id needs to be changed")
1583-
new_rc = deepcopy(rule_component)
1584-
base_id = new_rc['extension']['id']
1585-
row = self.extensions[self.extensions.eq(
1586-
base_id).any(1)].index.values[0]
1587-
new_value = self.extensions.loc[row, target_property]
1588-
# print(f"name : {rule_component['rule_name']}")
1589-
# print(f"old_id : {base_id}")
1590-
# print(f"new_id : {new_value}")
1591-
new_rc['extension']['id'] = new_value
1592-
if self.rules.empty == False:
1593-
new_rc['rule_setting'] = {
1594-
'data': [{
1595-
'id': self.rules.loc[rule_component['rule_name'], target_property],
1596-
'type':'rules'}
1597-
]}
1598-
new_rc['rule_id'] = self.rules.loc[rule_component['rule_name'],
1599-
target_property]
1600-
else:
1601-
print(
1602-
"You didn't load the rules. Please use setExtensions and setRules, and extendExtensions and extendRules")
1603-
del new_rc['rules']
1604-
return new_rc
1605-
1606-
16071495
def extractAnalyticsCode(rcSettings: str, save: bool = False, filename: str = None,encoding:str='utf-8')->None:
16081496
"""
16091497
Extract the custom code of the rule and save it in a file.

launchpy/synchronizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def syncComponent(self,componentName:str=None,componentId:str=None,publishedVers
159159
if len(self.target_configs.get(target,{}).get('inclComponents',[]))==0 or flagAllowList:
160160
translatedComponent = self.translator.translate(target,data_element=cmp_baseDict['copy'])
161161
## if it does not exist
162-
if cmp_baseDict['name'] not in [de['attributes']['name'] for de in self.targets[target]['dataElements']]:
162+
if cmp_baseDict['name'] not in [de.get('attributes',{}).get('name') for de in self.targets[target]['dataElements']]:
163163
comp = self.targets[target]['api'].createDataElement(
164164
name=cmp_baseDict['name'],
165165
descriptor= translatedComponent['descriptor'],

0 commit comments

Comments
 (0)