edsl 0.1.38__py3-none-any.whl → 0.1.38.dev1__py3-none-any.whl
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.
- edsl/Base.py +34 -63
- edsl/BaseDiff.py +7 -7
- edsl/__init__.py +1 -2
- edsl/__version__.py +1 -1
- edsl/agents/Agent.py +11 -23
- edsl/agents/AgentList.py +23 -86
- edsl/agents/Invigilator.py +7 -18
- edsl/agents/InvigilatorBase.py +19 -0
- edsl/agents/PromptConstructor.py +4 -5
- edsl/auto/SurveyCreatorPipeline.py +1 -1
- edsl/auto/utilities.py +1 -1
- edsl/base/Base.py +13 -3
- edsl/config.py +0 -8
- edsl/conjure/AgentConstructionMixin.py +160 -0
- edsl/conjure/Conjure.py +62 -0
- edsl/conjure/InputData.py +659 -0
- edsl/conjure/InputDataCSV.py +48 -0
- edsl/conjure/InputDataMixinQuestionStats.py +182 -0
- edsl/conjure/InputDataPyRead.py +91 -0
- edsl/conjure/InputDataSPSS.py +8 -0
- edsl/conjure/InputDataStata.py +8 -0
- edsl/conjure/QuestionOptionMixin.py +76 -0
- edsl/conjure/QuestionTypeMixin.py +23 -0
- edsl/conjure/RawQuestion.py +65 -0
- edsl/conjure/SurveyResponses.py +7 -0
- edsl/conjure/__init__.py +9 -0
- edsl/conjure/examples/placeholder.txt +0 -0
- edsl/{utilities → conjure}/naming_utilities.py +1 -1
- edsl/conjure/utilities.py +201 -0
- edsl/coop/coop.py +7 -77
- edsl/data/Cache.py +17 -45
- edsl/data/CacheEntry.py +3 -8
- edsl/data/RemoteCacheSync.py +19 -0
- edsl/enums.py +0 -2
- edsl/exceptions/agents.py +0 -4
- edsl/inference_services/GoogleService.py +15 -7
- edsl/inference_services/registry.py +0 -2
- edsl/jobs/Jobs.py +559 -110
- edsl/jobs/buckets/TokenBucket.py +0 -3
- edsl/jobs/interviews/Interview.py +7 -7
- edsl/jobs/runners/JobsRunnerAsyncio.py +28 -156
- edsl/jobs/runners/JobsRunnerStatus.py +196 -194
- edsl/jobs/tasks/TaskHistory.py +19 -27
- edsl/language_models/LanguageModel.py +90 -52
- edsl/language_models/ModelList.py +14 -67
- edsl/language_models/registry.py +4 -57
- edsl/notebooks/Notebook.py +8 -7
- edsl/prompts/Prompt.py +3 -8
- edsl/questions/QuestionBase.py +30 -38
- edsl/questions/QuestionBaseGenMixin.py +1 -1
- edsl/questions/QuestionBasePromptsMixin.py +17 -0
- edsl/questions/QuestionExtract.py +4 -3
- edsl/questions/QuestionFunctional.py +3 -10
- edsl/questions/derived/QuestionTopK.py +0 -2
- edsl/questions/question_registry.py +6 -36
- edsl/results/Dataset.py +15 -146
- edsl/results/DatasetExportMixin.py +217 -231
- edsl/results/DatasetTree.py +4 -134
- edsl/results/Result.py +16 -31
- edsl/results/Results.py +65 -159
- edsl/scenarios/FileStore.py +13 -187
- edsl/scenarios/Scenario.py +18 -73
- edsl/scenarios/ScenarioList.py +76 -251
- edsl/surveys/MemoryPlan.py +1 -1
- edsl/surveys/Rule.py +5 -1
- edsl/surveys/RuleCollection.py +1 -1
- edsl/surveys/Survey.py +19 -25
- edsl/surveys/SurveyFlowVisualizationMixin.py +9 -67
- edsl/surveys/instructions/ChangeInstruction.py +7 -9
- edsl/surveys/instructions/Instruction.py +7 -21
- edsl/templates/error_reporting/interview_details.html +3 -3
- edsl/templates/error_reporting/interviews.html +9 -18
- edsl/utilities/utilities.py +0 -15
- {edsl-0.1.38.dist-info → edsl-0.1.38.dev1.dist-info}/METADATA +1 -2
- {edsl-0.1.38.dist-info → edsl-0.1.38.dev1.dist-info}/RECORD +77 -71
- edsl/exceptions/cache.py +0 -5
- edsl/inference_services/PerplexityService.py +0 -163
- edsl/jobs/JobsChecks.py +0 -147
- edsl/jobs/JobsPrompts.py +0 -268
- edsl/jobs/JobsRemoteInferenceHandler.py +0 -239
- edsl/results/CSSParameterizer.py +0 -108
- edsl/results/TableDisplay.py +0 -198
- edsl/results/table_display.css +0 -78
- edsl/scenarios/ScenarioJoin.py +0 -127
- {edsl-0.1.38.dist-info → edsl-0.1.38.dev1.dist-info}/LICENSE +0 -0
- {edsl-0.1.38.dist-info → edsl-0.1.38.dev1.dist-info}/WHEEL +0 -0
edsl/results/CSSParameterizer.py
DELETED
@@ -1,108 +0,0 @@
|
|
1
|
-
import re
|
2
|
-
from typing import Dict, Set, Optional
|
3
|
-
|
4
|
-
|
5
|
-
class CSSParameterizer:
|
6
|
-
"""A utility class to parameterize CSS with custom properties (variables)."""
|
7
|
-
|
8
|
-
def __init__(self, css_content: str):
|
9
|
-
"""
|
10
|
-
Initialize with CSS content to be parameterized.
|
11
|
-
|
12
|
-
Args:
|
13
|
-
css_content (str): The CSS content containing var() declarations
|
14
|
-
"""
|
15
|
-
self.css_content = css_content
|
16
|
-
self._extract_variables()
|
17
|
-
|
18
|
-
def _extract_variables(self) -> None:
|
19
|
-
"""Extract all CSS custom properties (variables) from the CSS content."""
|
20
|
-
# Find all var(...) declarations in the CSS
|
21
|
-
var_pattern = r"var\((--[a-zA-Z0-9-]+)\)"
|
22
|
-
self.variables = set(re.findall(var_pattern, self.css_content))
|
23
|
-
|
24
|
-
def _validate_parameters(self, parameters: Dict[str, str]) -> Set[str]:
|
25
|
-
"""
|
26
|
-
Validate the provided parameters against the CSS variables.
|
27
|
-
|
28
|
-
Args:
|
29
|
-
parameters (Dict[str, str]): Dictionary of variable names and their values
|
30
|
-
|
31
|
-
Returns:
|
32
|
-
Set[str]: Set of missing variables
|
33
|
-
"""
|
34
|
-
# Convert parameter keys to CSS variable format if they don't already have --
|
35
|
-
formatted_params = {
|
36
|
-
f"--{k}" if not k.startswith("--") else k for k in parameters.keys()
|
37
|
-
}
|
38
|
-
|
39
|
-
# print("Variables from CSS:", self.variables)
|
40
|
-
# print("Formatted parameters:", formatted_params)
|
41
|
-
|
42
|
-
# Find missing and extra variables
|
43
|
-
missing_vars = self.variables - formatted_params
|
44
|
-
extra_vars = formatted_params - self.variables
|
45
|
-
|
46
|
-
if extra_vars:
|
47
|
-
print(f"Warning: Found unused parameters: {extra_vars}")
|
48
|
-
|
49
|
-
return missing_vars
|
50
|
-
|
51
|
-
def generate_root(self, **parameters: str) -> Optional[str]:
|
52
|
-
"""
|
53
|
-
Generate a :root block with the provided parameters.
|
54
|
-
|
55
|
-
Args:
|
56
|
-
**parameters: Keyword arguments where keys are variable names and values are their values
|
57
|
-
|
58
|
-
Returns:
|
59
|
-
str: Generated :root block with variables, or None if validation fails
|
60
|
-
|
61
|
-
Example:
|
62
|
-
>>> css = "body { height: var(--bodyHeight); }"
|
63
|
-
>>> parameterizer = CSSParameterizer(css)
|
64
|
-
>>> parameterizer.apply_parameters({'bodyHeight':"100vh"})
|
65
|
-
':root {\\n --bodyHeight: 100vh;\\n}\\n\\nbody { height: var(--bodyHeight); }'
|
66
|
-
"""
|
67
|
-
missing_vars = self._validate_parameters(parameters)
|
68
|
-
|
69
|
-
if missing_vars:
|
70
|
-
print(f"Error: Missing required variables: {missing_vars}")
|
71
|
-
return None
|
72
|
-
|
73
|
-
# Format parameters with -- prefix if not present
|
74
|
-
formatted_params = {
|
75
|
-
f"--{k}" if not k.startswith("--") else k: v for k, v in parameters.items()
|
76
|
-
}
|
77
|
-
|
78
|
-
# Generate the :root block
|
79
|
-
root_block = [":root {"]
|
80
|
-
for var_name, value in sorted(formatted_params.items()):
|
81
|
-
if var_name in self.variables:
|
82
|
-
root_block.append(f" {var_name}: {value};")
|
83
|
-
root_block.append("}")
|
84
|
-
|
85
|
-
return "\n".join(root_block)
|
86
|
-
|
87
|
-
def apply_parameters(self, parameters: dict) -> Optional[str]:
|
88
|
-
"""
|
89
|
-
Generate the complete CSS with the :root block and original CSS content.
|
90
|
-
|
91
|
-
Args:
|
92
|
-
**parameters: Keyword arguments where keys are variable names and values are their values
|
93
|
-
|
94
|
-
Returns:
|
95
|
-
str: Complete CSS with :root block and original content, or None if validation fails
|
96
|
-
"""
|
97
|
-
root_block = self.generate_root(**parameters)
|
98
|
-
if root_block is None:
|
99
|
-
return None
|
100
|
-
|
101
|
-
return f"{root_block}\n\n{self.css_content}"
|
102
|
-
|
103
|
-
|
104
|
-
# Example usage
|
105
|
-
if __name__ == "__main__":
|
106
|
-
import doctest
|
107
|
-
|
108
|
-
doctest.testmod()
|
edsl/results/TableDisplay.py
DELETED
@@ -1,198 +0,0 @@
|
|
1
|
-
from tabulate import tabulate
|
2
|
-
from pathlib import Path
|
3
|
-
|
4
|
-
from edsl.results.CSSParameterizer import CSSParameterizer
|
5
|
-
|
6
|
-
|
7
|
-
class TableDisplay:
|
8
|
-
max_height = 400
|
9
|
-
min_height = 200
|
10
|
-
|
11
|
-
@classmethod
|
12
|
-
def get_css(cls):
|
13
|
-
"""Load CSS content from the file next to this module"""
|
14
|
-
css_path = Path(__file__).parent / "table_display.css"
|
15
|
-
return css_path.read_text()
|
16
|
-
|
17
|
-
def __init__(self, headers, data, tablefmt=None, raw_data_set=None):
|
18
|
-
self.headers = headers
|
19
|
-
self.data = data
|
20
|
-
self.tablefmt = tablefmt
|
21
|
-
self.raw_data_set = raw_data_set
|
22
|
-
|
23
|
-
if hasattr(raw_data_set, "print_parameters"):
|
24
|
-
if raw_data_set.print_parameters:
|
25
|
-
self.printing_parameters = raw_data_set.print_parameters
|
26
|
-
else:
|
27
|
-
self.printing_parameters = {}
|
28
|
-
else:
|
29
|
-
self.printing_parameters = {}
|
30
|
-
|
31
|
-
def to_csv(self, filename: str):
|
32
|
-
self.raw_data_set.to_csv(filename)
|
33
|
-
|
34
|
-
def write(self, filename: str):
|
35
|
-
if self.tablefmt is None:
|
36
|
-
table = tabulate(self.data, headers=self.headers, tablefmt="simple")
|
37
|
-
else:
|
38
|
-
table = tabulate(self.data, headers=self.headers, tablefmt=self.tablefmt)
|
39
|
-
|
40
|
-
with open(filename, "w") as file:
|
41
|
-
print("Writing table to", filename)
|
42
|
-
file.write(table)
|
43
|
-
|
44
|
-
def to_pandas(self):
|
45
|
-
return self.raw_data_set.to_pandas()
|
46
|
-
|
47
|
-
def to_list(self):
|
48
|
-
return self.raw_data_set.to_list()
|
49
|
-
|
50
|
-
def __repr__(self):
|
51
|
-
from tabulate import tabulate
|
52
|
-
|
53
|
-
if self.tablefmt is None:
|
54
|
-
return tabulate(self.data, headers=self.headers, tablefmt="simple")
|
55
|
-
else:
|
56
|
-
return tabulate(self.data, headers=self.headers, tablefmt=self.tablefmt)
|
57
|
-
|
58
|
-
def long(self):
|
59
|
-
new_header = ["row", "key", "value"]
|
60
|
-
new_data = []
|
61
|
-
for index, row in enumerate(self.data):
|
62
|
-
new_data.extend([[index, k, v] for k, v in zip(self.headers, row)])
|
63
|
-
return TableDisplay(new_header, new_data)
|
64
|
-
|
65
|
-
def _repr_html_(self):
|
66
|
-
if self.tablefmt is not None:
|
67
|
-
return (
|
68
|
-
"<pre>"
|
69
|
-
+ tabulate(self.data, headers=self.headers, tablefmt=self.tablefmt)
|
70
|
-
+ "</pre>"
|
71
|
-
)
|
72
|
-
|
73
|
-
num_rows = len(self.data)
|
74
|
-
height = min(
|
75
|
-
num_rows * 30 + 50, self.max_height
|
76
|
-
) # Added extra space for header
|
77
|
-
|
78
|
-
if height < self.min_height:
|
79
|
-
height = self.min_height
|
80
|
-
|
81
|
-
html_template = """
|
82
|
-
<style>
|
83
|
-
{css}
|
84
|
-
</style>
|
85
|
-
<div class="table-container">
|
86
|
-
<div class="scroll-table-wrapper">
|
87
|
-
{table}
|
88
|
-
</div>
|
89
|
-
</div>
|
90
|
-
"""
|
91
|
-
|
92
|
-
html_content = tabulate(self.data, headers=self.headers, tablefmt="html")
|
93
|
-
html_content = html_content.replace("<table>", '<table class="scroll-table">')
|
94
|
-
|
95
|
-
height_string = f"{height}px"
|
96
|
-
parameters = {"containerHeight": height_string, "headerColor": "blue"}
|
97
|
-
parameters.update(self.printing_parameters)
|
98
|
-
rendered_css = CSSParameterizer(self.get_css()).apply_parameters(parameters)
|
99
|
-
|
100
|
-
return html_template.format(table=html_content, css=rendered_css)
|
101
|
-
|
102
|
-
@classmethod
|
103
|
-
def example(
|
104
|
-
cls,
|
105
|
-
headers=None,
|
106
|
-
data=None,
|
107
|
-
filename: str = "table_example.html",
|
108
|
-
auto_open: bool = True,
|
109
|
-
):
|
110
|
-
"""
|
111
|
-
Creates a standalone HTML file with an example table in an iframe and optionally opens it in a new tab.
|
112
|
-
|
113
|
-
Args:
|
114
|
-
cls: The class itself
|
115
|
-
headers (list): List of column headers. If None, uses example headers
|
116
|
-
data (list): List of data rows. If None, uses example data
|
117
|
-
filename (str): The name of the HTML file to create. Defaults to "table_example.html"
|
118
|
-
auto_open (bool): Whether to automatically open the file in the default web browser. Defaults to True
|
119
|
-
|
120
|
-
Returns:
|
121
|
-
str: The path to the created HTML file
|
122
|
-
"""
|
123
|
-
import os
|
124
|
-
import webbrowser
|
125
|
-
|
126
|
-
# Use example data if none provided
|
127
|
-
if headers is None:
|
128
|
-
headers = ["Name", "Age", "City", "Occupation"]
|
129
|
-
if data is None:
|
130
|
-
data = [
|
131
|
-
[
|
132
|
-
"John Doe",
|
133
|
-
30,
|
134
|
-
"New York",
|
135
|
-
"""cls: The class itself
|
136
|
-
headers (list): List of column headers. If None, uses example headers
|
137
|
-
data (list): List of data rows. If None, uses example data
|
138
|
-
filename (str): The name of the HTML file to create. Defaults to "table_example.html"
|
139
|
-
auto_open (bool): Whether to automatically open the file in the default web browser. Defaults to True
|
140
|
-
""",
|
141
|
-
],
|
142
|
-
["Jane Smith", 28, "San Francisco", "Designer"],
|
143
|
-
["Bob Johnson", 35, "Chicago", "Manager"],
|
144
|
-
["Alice Brown", 25, "Boston", "Developer"],
|
145
|
-
["Charlie Wilson", 40, "Seattle", "Architect"],
|
146
|
-
]
|
147
|
-
|
148
|
-
# Create instance with the data
|
149
|
-
instance = cls(headers=headers, data=data)
|
150
|
-
|
151
|
-
# Get the table HTML content
|
152
|
-
table_html = instance._repr_html_()
|
153
|
-
|
154
|
-
# Calculate the appropriate iframe height
|
155
|
-
num_rows = len(data)
|
156
|
-
iframe_height = min(num_rows * 140 + 50, cls.max_height)
|
157
|
-
print(f"Table height: {iframe_height}px")
|
158
|
-
|
159
|
-
# Create the full HTML document
|
160
|
-
html_content = f"""
|
161
|
-
<!DOCTYPE html>
|
162
|
-
<html>
|
163
|
-
<head>
|
164
|
-
<title>Table Display Example</title>
|
165
|
-
<style>
|
166
|
-
body {{
|
167
|
-
margin: 0;
|
168
|
-
padding: 20px;
|
169
|
-
font-family: Arial, sans-serif;
|
170
|
-
}}
|
171
|
-
iframe {{
|
172
|
-
width: 100%;
|
173
|
-
height: {iframe_height}px;
|
174
|
-
border: none;
|
175
|
-
overflow: hidden;
|
176
|
-
}}
|
177
|
-
</style>
|
178
|
-
</head>
|
179
|
-
<body>
|
180
|
-
<iframe srcdoc='{table_html}'></iframe>
|
181
|
-
</body>
|
182
|
-
</html>
|
183
|
-
"""
|
184
|
-
|
185
|
-
# Write the HTML file
|
186
|
-
abs_path = os.path.abspath(filename)
|
187
|
-
with open(filename, "w", encoding="utf-8") as f:
|
188
|
-
f.write(html_content)
|
189
|
-
|
190
|
-
# Open in browser if requested
|
191
|
-
if auto_open:
|
192
|
-
webbrowser.open("file://" + abs_path, new=2)
|
193
|
-
|
194
|
-
return abs_path
|
195
|
-
|
196
|
-
|
197
|
-
if __name__ == "__main__":
|
198
|
-
TableDisplay.example()
|
edsl/results/table_display.css
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
.table-container {
|
2
|
-
height: var(--containerHeight) !important;
|
3
|
-
width: 100%;
|
4
|
-
overflow: auto; /* This enables both horizontal and vertical scrolling */
|
5
|
-
border: 1px solid #d4d4d4;
|
6
|
-
background: transparent;
|
7
|
-
position: relative; /* Create stacking context for sticky header */
|
8
|
-
}
|
9
|
-
|
10
|
-
.scroll-table {
|
11
|
-
/* Remove width: 100% to prevent table from being constrained */
|
12
|
-
/* min-width: 100% ensures table takes at least full container width */
|
13
|
-
min-width: 100%;
|
14
|
-
border-collapse: separate;
|
15
|
-
border-spacing: 4px;
|
16
|
-
background: transparent;
|
17
|
-
table-layout: auto; /* Allow table to size based on content */
|
18
|
-
}
|
19
|
-
|
20
|
-
.scroll-table th {
|
21
|
-
background: transparent; /* Semi-transparent background to ensure text readability */
|
22
|
-
position: sticky;
|
23
|
-
top: 0;
|
24
|
-
z-index: 1;
|
25
|
-
text-align: left !important;
|
26
|
-
padding: 8px;
|
27
|
-
font-weight: bold;
|
28
|
-
white-space: nowrap; /* Prevent header text from wrapping */
|
29
|
-
min-width: 100px; /* Ensure minimum column width */
|
30
|
-
backdrop-filter: blur(8px); /* Optional: adds extra clarity */
|
31
|
-
color: var(--headerColor);
|
32
|
-
}
|
33
|
-
|
34
|
-
.scroll-table td {
|
35
|
-
padding: 8px;
|
36
|
-
text-align: left !important;
|
37
|
-
white-space: pre-wrap;
|
38
|
-
word-wrap: break-word;
|
39
|
-
vertical-align: top;
|
40
|
-
color: inherit;
|
41
|
-
border-bottom: none;
|
42
|
-
background: transparent;
|
43
|
-
min-width: 100px; /* Match header minimum width */
|
44
|
-
}
|
45
|
-
|
46
|
-
.scroll-table tbody tr:hover {
|
47
|
-
background: transparent;
|
48
|
-
}
|
49
|
-
|
50
|
-
/* Additional rule to ensure header background is truly transparent */
|
51
|
-
.scroll-table thead tr {
|
52
|
-
background: transparent !important;
|
53
|
-
}
|
54
|
-
|
55
|
-
/* Add shadow to indicate scrollable content */
|
56
|
-
.table-container::after {
|
57
|
-
content: '';
|
58
|
-
position: absolute;
|
59
|
-
top: 0;
|
60
|
-
right: 0;
|
61
|
-
bottom: 0;
|
62
|
-
width: 5px;
|
63
|
-
background: linear-gradient(to right, transparent, rgba(242, 6, 6, 0.1));
|
64
|
-
pointer-events: none;
|
65
|
-
opacity: 0;
|
66
|
-
transition: opacity 0.3s;
|
67
|
-
}
|
68
|
-
|
69
|
-
.table-container:hover::after {
|
70
|
-
opacity: 1;
|
71
|
-
}
|
72
|
-
|
73
|
-
/* Handle Jupyter notebook specific styling */
|
74
|
-
.jp-OutputArea-output .table-container {
|
75
|
-
max-width: 100%;
|
76
|
-
margin: 0;
|
77
|
-
overflow-x: auto;
|
78
|
-
}
|
edsl/scenarios/ScenarioJoin.py
DELETED
@@ -1,127 +0,0 @@
|
|
1
|
-
from __future__ import annotations
|
2
|
-
from typing import Union, TYPE_CHECKING
|
3
|
-
|
4
|
-
# if TYPE_CHECKING:
|
5
|
-
from edsl.scenarios.ScenarioList import ScenarioList
|
6
|
-
from edsl.scenarios.Scenario import Scenario
|
7
|
-
|
8
|
-
|
9
|
-
class ScenarioJoin:
|
10
|
-
"""Handles join operations between two ScenarioLists.
|
11
|
-
|
12
|
-
This class encapsulates all join-related logic, making it easier to maintain
|
13
|
-
and extend with other join types (inner, right, full) in the future.
|
14
|
-
"""
|
15
|
-
|
16
|
-
def __init__(self, left: "ScenarioList", right: "ScenarioList"):
|
17
|
-
"""Initialize join operation with two ScenarioLists.
|
18
|
-
|
19
|
-
Args:
|
20
|
-
left: The left ScenarioList
|
21
|
-
right: The right ScenarioList
|
22
|
-
"""
|
23
|
-
self.left = left
|
24
|
-
self.right = right
|
25
|
-
|
26
|
-
def left_join(self, by: Union[str, list[str]]) -> ScenarioList:
|
27
|
-
"""Perform a left join between the two ScenarioLists.
|
28
|
-
|
29
|
-
Args:
|
30
|
-
by: String or list of strings representing the key(s) to join on. Cannot be empty.
|
31
|
-
|
32
|
-
Returns:
|
33
|
-
A new ScenarioList containing the joined scenarios
|
34
|
-
|
35
|
-
Raises:
|
36
|
-
ValueError: If by is empty or if any join keys don't exist in both ScenarioLists
|
37
|
-
"""
|
38
|
-
self._validate_join_keys(by)
|
39
|
-
by_keys = [by] if isinstance(by, str) else by
|
40
|
-
|
41
|
-
other_dict = self._create_lookup_dict(self.right, by_keys)
|
42
|
-
all_keys = self._get_all_keys()
|
43
|
-
|
44
|
-
return ScenarioList(
|
45
|
-
self._create_joined_scenarios(by_keys, other_dict, all_keys)
|
46
|
-
)
|
47
|
-
|
48
|
-
def _validate_join_keys(self, by: Union[str, list[str]]) -> None:
|
49
|
-
"""Validate join keys exist in both ScenarioLists."""
|
50
|
-
if not by:
|
51
|
-
raise ValueError(
|
52
|
-
"Join keys cannot be empty. Please specify at least one key to join on."
|
53
|
-
)
|
54
|
-
|
55
|
-
by_keys = [by] if isinstance(by, str) else by
|
56
|
-
left_keys = set(next(iter(self.left)).keys()) if self.left else set()
|
57
|
-
right_keys = set(next(iter(self.right)).keys()) if self.right else set()
|
58
|
-
|
59
|
-
missing_left = set(by_keys) - left_keys
|
60
|
-
missing_right = set(by_keys) - right_keys
|
61
|
-
if missing_left or missing_right:
|
62
|
-
missing = missing_left | missing_right
|
63
|
-
raise ValueError(f"Join key(s) {missing} not found in both ScenarioLists")
|
64
|
-
|
65
|
-
@staticmethod
|
66
|
-
def _get_key_tuple(scenario: Scenario, keys: list[str]) -> tuple:
|
67
|
-
"""Create a tuple of values for the join keys."""
|
68
|
-
return tuple(scenario[k] for k in keys)
|
69
|
-
|
70
|
-
def _create_lookup_dict(self, scenarios: ScenarioList, by_keys: list[str]) -> dict:
|
71
|
-
"""Create a lookup dictionary for the right scenarios."""
|
72
|
-
return {
|
73
|
-
self._get_key_tuple(scenario, by_keys): scenario for scenario in scenarios
|
74
|
-
}
|
75
|
-
|
76
|
-
def _get_all_keys(self) -> set:
|
77
|
-
"""Get all unique keys from both ScenarioLists."""
|
78
|
-
all_keys = set()
|
79
|
-
for scenario in self.left:
|
80
|
-
all_keys.update(scenario.keys())
|
81
|
-
for scenario in self.right:
|
82
|
-
all_keys.update(scenario.keys())
|
83
|
-
return all_keys
|
84
|
-
|
85
|
-
def _create_joined_scenarios(
|
86
|
-
self, by_keys: list[str], other_dict: dict, all_keys: set
|
87
|
-
) -> list[Scenario]:
|
88
|
-
"""Create the joined scenarios."""
|
89
|
-
new_scenarios = []
|
90
|
-
|
91
|
-
for scenario in self.left:
|
92
|
-
new_scenario = {key: None for key in all_keys}
|
93
|
-
new_scenario.update(scenario)
|
94
|
-
|
95
|
-
key_tuple = self._get_key_tuple(scenario, by_keys)
|
96
|
-
if matching_scenario := other_dict.get(key_tuple):
|
97
|
-
self._handle_matching_scenario(
|
98
|
-
new_scenario, scenario, matching_scenario, by_keys
|
99
|
-
)
|
100
|
-
|
101
|
-
new_scenarios.append(Scenario(new_scenario))
|
102
|
-
|
103
|
-
return new_scenarios
|
104
|
-
|
105
|
-
def _handle_matching_scenario(
|
106
|
-
self,
|
107
|
-
new_scenario: dict,
|
108
|
-
left_scenario: Scenario,
|
109
|
-
right_scenario: Scenario,
|
110
|
-
by_keys: list[str],
|
111
|
-
) -> None:
|
112
|
-
"""Handle merging of matching scenarios and conflict warnings."""
|
113
|
-
overlapping_keys = set(left_scenario.keys()) & set(right_scenario.keys())
|
114
|
-
|
115
|
-
for key in overlapping_keys:
|
116
|
-
if key not in by_keys and left_scenario[key] != right_scenario[key]:
|
117
|
-
join_conditions = [f"{k}='{left_scenario[k]}'" for k in by_keys]
|
118
|
-
print(
|
119
|
-
f"Warning: Conflicting values for key '{key}' where "
|
120
|
-
f"{' AND '.join(join_conditions)}. "
|
121
|
-
f"Keeping left value: {left_scenario[key]} "
|
122
|
-
f"(discarding: {right_scenario[key]})"
|
123
|
-
)
|
124
|
-
|
125
|
-
# Only update with non-overlapping keys from matching scenario
|
126
|
-
new_keys = set(right_scenario.keys()) - set(left_scenario.keys())
|
127
|
-
new_scenario.update({k: right_scenario[k] for k in new_keys})
|
File without changes
|
File without changes
|