brynq-sdk-brynq 1.0.1__tar.gz → 1.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of brynq-sdk-brynq might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.0
2
2
  Name: brynq_sdk_brynq
3
- Version: 1.0.1
3
+ Version: 1.1.0
4
4
  Summary: BrynQ SDK for the BrynQ.com platform
5
5
  Home-page: UNKNOWN
6
6
  Author: BrynQ
@@ -2,7 +2,7 @@ import os
2
2
  import requests
3
3
  import json
4
4
  import pandas as pd
5
- from typing import Union
5
+ from typing import Union, Literal
6
6
 
7
7
 
8
8
  class BrynQ:
@@ -26,55 +26,61 @@ class BrynQ:
26
26
  'Domain': self.subdomain
27
27
  }
28
28
 
29
- def _get_mappings(self, task_id: int) -> dict:
29
+ def _get_mappings(self, data_interface_id: int) -> dict:
30
30
  """
31
31
  Get the mappings from the task in BrynQ
32
- :param task_id: The id of the task in BrynQ. this does not have to be the task id of the current task
32
+ :param data_interface_id: The id of the data_interface in BrynQ. this does not have to be the task id of the current task
33
33
  :return: A dictionary with the following structure: {mapping_title: {tuple(input): output}}
34
34
  """
35
- response = requests.get(url=f'{self.url}interfaces/{task_id}/data-mapping', headers=self._get_headers())
36
-
35
+ response = requests.get(url=f'{self.url}interfaces/{data_interface_id}/data-mapping', headers=self._get_headers())
37
36
  if response.status_code != 200:
38
37
  raise ValueError(f'Error occurred while fetching mappings: {response.text}. Please always check if you have added BRYNQ_SUBDOMAIN to your .env file')
39
38
 
40
39
  return response.json()
41
40
 
42
- def get_mapping(self, task_id: int, mapping: str, multiple_values: str = 'raise'):
41
+ def get_mapping(self, data_interface_id: int, mapping: str, return_format: Literal['input_as_key', 'columns_names_as_keys', 'nested_input_output'] = 'input_as_key') -> dict:
43
42
  """
44
43
  Get the mapping json from the mappings
45
- :param task_id: The id of the task in BrynQ. this does not have to be the task id of the current task
44
+ :param data_interface_id: The id of the task in BrynQ. this does not have to be the task id of the current task
46
45
  :param mapping: The name of the mapping
47
- :param multiple_values: A string to indicate how to handle multiple input or output values. Options are 'raise' and 'concat'
46
+ :param return_format: Determines how the mapping should be returned. Options are 'input_as_key' (Default, the input column is the key, the output columns are the values), 'columns_names_as_keys', 'nested_input_output'
48
47
  :return: The json of the mapping
49
48
  """
50
49
  # Find the mapping for the given sheet name
51
- mappings = self._get_mappings(task_id=task_id)
50
+ mappings = self._get_mappings(data_interface_id=data_interface_id)
52
51
  mapping_data = next((item for item in mappings if item['name'] == mapping), None)
53
52
  if not mapping_data:
54
53
  raise ValueError(f"Mapping named '{mapping}' not found")
55
54
 
56
- result_mapping = {}
57
- for value in mapping_data['values']:
58
- if multiple_values == 'raise' and (len(value['input'].items()) > 1 or len(value['output'].items()) > 1):
59
- raise ValueError("Multiple input or output keys found when 'raise' strategy is specified.")
60
-
61
- input_values = []
62
- output_values = []
63
- for _, val in value['input'].items():
64
- input_values.append(val)
65
- for _, val in value['output'].items():
66
- output_values.append(val)
67
-
68
- if multiple_values == 'concat':
69
- concatenated_input = ','.join(input_values)
70
- concatenated_output = ','.join(output_values)
71
- result_mapping[concatenated_input] = concatenated_output
72
- else: # Default to assuming there's only one key-value pair if not concatenating
73
- result_mapping[input_values[0]] = output_values[0]
74
-
75
- return result_mapping
76
-
77
- def get_mapping_as_dataframe(self, task_id: int, mapping: str, prefix: bool = False):
55
+ # If the user want to get the column names back as keys, transform the data accordingly and return
56
+ if return_format == 'columns_names_as_keys':
57
+ final_mapping = []
58
+ for row in mapping_data['values']:
59
+ combined_dict = {}
60
+ combined_dict.update(row['input'])
61
+ combined_dict.update(row['output'])
62
+ final_mapping.append(combined_dict)
63
+ elif return_format == 'nested_input_output':
64
+ final_mapping = mapping_data
65
+ else:
66
+ final_mapping = {}
67
+ for value in mapping_data['values']:
68
+ input_values = []
69
+ output_values = []
70
+ for _, val in value['input'].items():
71
+ input_values.append(val)
72
+ for _, val in value['output'].items():
73
+ output_values.append(val)
74
+ # Detect if there are multiple input or output columns and concatenate them
75
+ if len(value['input'].items()) > 1 or len(value['output'].items()) > 1:
76
+ concatenated_input = ','.join(input_values)
77
+ concatenated_output = ','.join(output_values)
78
+ final_mapping[concatenated_input] = concatenated_output
79
+ else: # Default to assuming there's only one key-value pair if not concatenating
80
+ final_mapping[input_values[0]] = output_values[0]
81
+ return final_mapping
82
+
83
+ def get_mapping_as_dataframe(self, data_interface_id: int, mapping: str, prefix: bool = False):
78
84
  """
79
85
  Get the mapping dataframe from the mappings
80
86
  :param mapping: The name of the mapping
@@ -82,7 +88,7 @@ class BrynQ:
82
88
  :return: The dataframe of the mapping
83
89
  """
84
90
  # Find the mapping for the given sheet name
85
- mappings = self._get_mappings(task_id=task_id)
91
+ mappings = self._get_mappings(data_interface_id=data_interface_id)
86
92
  mapping_data = next((item for item in mappings if item['name'] == mapping), None)
87
93
  if not mapping_data:
88
94
  raise ValueError(f"Mapping named '{mapping}' not found")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.0
2
2
  Name: brynq-sdk-brynq
3
- Version: 1.0.1
3
+ Version: 1.1.0
4
4
  Summary: BrynQ SDK for the BrynQ.com platform
5
5
  Home-page: UNKNOWN
6
6
  Author: BrynQ
@@ -3,7 +3,7 @@ from setuptools import setup
3
3
 
4
4
  setup(
5
5
  name='brynq_sdk_brynq',
6
- version='1.0.1',
6
+ version='1.1.0',
7
7
  description='BrynQ SDK for the BrynQ.com platform',
8
8
  long_description='BrynQ SDK for the BrynQ.com platform',
9
9
  author='BrynQ',