edsl 0.1.39__py3-none-any.whl → 0.1.39.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.
Files changed (212) hide show
  1. edsl/Base.py +116 -197
  2. edsl/__init__.py +7 -15
  3. edsl/__version__.py +1 -1
  4. edsl/agents/Agent.py +147 -351
  5. edsl/agents/AgentList.py +73 -211
  6. edsl/agents/Invigilator.py +50 -101
  7. edsl/agents/InvigilatorBase.py +70 -62
  8. edsl/agents/PromptConstructor.py +225 -143
  9. edsl/agents/__init__.py +1 -0
  10. edsl/agents/prompt_helpers.py +3 -3
  11. edsl/auto/AutoStudy.py +5 -18
  12. edsl/auto/StageBase.py +40 -53
  13. edsl/auto/StageQuestions.py +1 -2
  14. edsl/auto/utilities.py +6 -0
  15. edsl/config.py +2 -22
  16. edsl/conversation/car_buying.py +1 -2
  17. edsl/coop/PriceFetcher.py +1 -1
  18. edsl/coop/coop.py +47 -125
  19. edsl/coop/utils.py +14 -14
  20. edsl/data/Cache.py +27 -45
  21. edsl/data/CacheEntry.py +15 -12
  22. edsl/data/CacheHandler.py +12 -31
  23. edsl/data/RemoteCacheSync.py +46 -154
  24. edsl/data/__init__.py +3 -4
  25. edsl/data_transfer_models.py +1 -2
  26. edsl/enums.py +0 -27
  27. edsl/exceptions/__init__.py +50 -50
  28. edsl/exceptions/agents.py +0 -12
  29. edsl/exceptions/questions.py +6 -24
  30. edsl/exceptions/scenarios.py +0 -7
  31. edsl/inference_services/AnthropicService.py +19 -38
  32. edsl/inference_services/AwsBedrock.py +2 -0
  33. edsl/inference_services/AzureAI.py +2 -0
  34. edsl/inference_services/GoogleService.py +12 -7
  35. edsl/inference_services/InferenceServiceABC.py +85 -18
  36. edsl/inference_services/InferenceServicesCollection.py +79 -120
  37. edsl/inference_services/MistralAIService.py +3 -0
  38. edsl/inference_services/OpenAIService.py +35 -47
  39. edsl/inference_services/PerplexityService.py +3 -0
  40. edsl/inference_services/TestService.py +10 -11
  41. edsl/inference_services/TogetherAIService.py +3 -5
  42. edsl/jobs/Answers.py +14 -1
  43. edsl/jobs/Jobs.py +431 -356
  44. edsl/jobs/JobsChecks.py +10 -35
  45. edsl/jobs/JobsPrompts.py +4 -6
  46. edsl/jobs/JobsRemoteInferenceHandler.py +133 -205
  47. edsl/jobs/buckets/BucketCollection.py +3 -44
  48. edsl/jobs/buckets/TokenBucket.py +21 -53
  49. edsl/jobs/interviews/Interview.py +408 -143
  50. edsl/jobs/runners/JobsRunnerAsyncio.py +403 -88
  51. edsl/jobs/runners/JobsRunnerStatus.py +165 -133
  52. edsl/jobs/tasks/QuestionTaskCreator.py +19 -21
  53. edsl/jobs/tasks/TaskHistory.py +18 -38
  54. edsl/jobs/tasks/task_status_enum.py +2 -0
  55. edsl/language_models/KeyLookup.py +30 -0
  56. edsl/language_models/LanguageModel.py +236 -194
  57. edsl/language_models/ModelList.py +19 -28
  58. edsl/language_models/__init__.py +2 -1
  59. edsl/language_models/registry.py +190 -0
  60. edsl/language_models/repair.py +2 -2
  61. edsl/language_models/unused/ReplicateBase.py +83 -0
  62. edsl/language_models/utilities.py +4 -5
  63. edsl/notebooks/Notebook.py +14 -19
  64. edsl/prompts/Prompt.py +39 -29
  65. edsl/questions/{answer_validator_mixin.py → AnswerValidatorMixin.py} +2 -47
  66. edsl/questions/QuestionBase.py +214 -68
  67. edsl/questions/{question_base_gen_mixin.py → QuestionBaseGenMixin.py} +50 -57
  68. edsl/questions/QuestionBasePromptsMixin.py +3 -7
  69. edsl/questions/QuestionBudget.py +1 -1
  70. edsl/questions/QuestionCheckBox.py +3 -3
  71. edsl/questions/QuestionExtract.py +7 -5
  72. edsl/questions/QuestionFreeText.py +3 -2
  73. edsl/questions/QuestionList.py +18 -10
  74. edsl/questions/QuestionMultipleChoice.py +23 -67
  75. edsl/questions/QuestionNumerical.py +4 -2
  76. edsl/questions/QuestionRank.py +17 -7
  77. edsl/questions/{response_validator_abc.py → ResponseValidatorABC.py} +26 -40
  78. edsl/questions/SimpleAskMixin.py +3 -4
  79. edsl/questions/__init__.py +1 -2
  80. edsl/questions/derived/QuestionLinearScale.py +3 -6
  81. edsl/questions/derived/QuestionTopK.py +1 -1
  82. edsl/questions/descriptors.py +3 -17
  83. edsl/questions/question_registry.py +1 -1
  84. edsl/results/CSSParameterizer.py +1 -1
  85. edsl/results/Dataset.py +7 -170
  86. edsl/results/DatasetExportMixin.py +305 -168
  87. edsl/results/DatasetTree.py +8 -28
  88. edsl/results/Result.py +206 -298
  89. edsl/results/Results.py +131 -149
  90. edsl/results/ResultsDBMixin.py +238 -0
  91. edsl/results/ResultsExportMixin.py +0 -2
  92. edsl/results/{results_selector.py → Selector.py} +13 -23
  93. edsl/results/TableDisplay.py +171 -98
  94. edsl/results/__init__.py +1 -1
  95. edsl/scenarios/FileStore.py +239 -150
  96. edsl/scenarios/Scenario.py +193 -90
  97. edsl/scenarios/ScenarioHtmlMixin.py +3 -4
  98. edsl/scenarios/{scenario_join.py → ScenarioJoin.py} +6 -10
  99. edsl/scenarios/ScenarioList.py +244 -415
  100. edsl/scenarios/ScenarioListExportMixin.py +7 -0
  101. edsl/scenarios/ScenarioListPdfMixin.py +37 -15
  102. edsl/scenarios/__init__.py +2 -1
  103. edsl/study/ObjectEntry.py +1 -1
  104. edsl/study/SnapShot.py +1 -1
  105. edsl/study/Study.py +12 -5
  106. edsl/surveys/Rule.py +4 -5
  107. edsl/surveys/RuleCollection.py +27 -25
  108. edsl/surveys/Survey.py +791 -270
  109. edsl/surveys/SurveyCSS.py +8 -20
  110. edsl/surveys/{SurveyFlowVisualization.py → SurveyFlowVisualizationMixin.py} +9 -11
  111. edsl/surveys/__init__.py +2 -4
  112. edsl/surveys/descriptors.py +2 -6
  113. edsl/surveys/instructions/ChangeInstruction.py +2 -1
  114. edsl/surveys/instructions/Instruction.py +13 -4
  115. edsl/surveys/instructions/InstructionCollection.py +6 -11
  116. edsl/templates/error_reporting/interview_details.html +1 -1
  117. edsl/templates/error_reporting/report.html +1 -1
  118. edsl/tools/plotting.py +1 -1
  119. edsl/utilities/utilities.py +23 -35
  120. {edsl-0.1.39.dist-info → edsl-0.1.39.dev1.dist-info}/METADATA +10 -12
  121. edsl-0.1.39.dev1.dist-info/RECORD +277 -0
  122. {edsl-0.1.39.dist-info → edsl-0.1.39.dev1.dist-info}/WHEEL +1 -1
  123. edsl/agents/QuestionInstructionPromptBuilder.py +0 -128
  124. edsl/agents/QuestionTemplateReplacementsBuilder.py +0 -137
  125. edsl/agents/question_option_processor.py +0 -172
  126. edsl/coop/CoopFunctionsMixin.py +0 -15
  127. edsl/coop/ExpectedParrotKeyHandler.py +0 -125
  128. edsl/exceptions/inference_services.py +0 -5
  129. edsl/inference_services/AvailableModelCacheHandler.py +0 -184
  130. edsl/inference_services/AvailableModelFetcher.py +0 -215
  131. edsl/inference_services/ServiceAvailability.py +0 -135
  132. edsl/inference_services/data_structures.py +0 -134
  133. edsl/jobs/AnswerQuestionFunctionConstructor.py +0 -223
  134. edsl/jobs/FetchInvigilator.py +0 -47
  135. edsl/jobs/InterviewTaskManager.py +0 -98
  136. edsl/jobs/InterviewsConstructor.py +0 -50
  137. edsl/jobs/JobsComponentConstructor.py +0 -189
  138. edsl/jobs/JobsRemoteInferenceLogger.py +0 -239
  139. edsl/jobs/RequestTokenEstimator.py +0 -30
  140. edsl/jobs/async_interview_runner.py +0 -138
  141. edsl/jobs/buckets/TokenBucketAPI.py +0 -211
  142. edsl/jobs/buckets/TokenBucketClient.py +0 -191
  143. edsl/jobs/check_survey_scenario_compatibility.py +0 -85
  144. edsl/jobs/data_structures.py +0 -120
  145. edsl/jobs/decorators.py +0 -35
  146. edsl/jobs/jobs_status_enums.py +0 -9
  147. edsl/jobs/loggers/HTMLTableJobLogger.py +0 -304
  148. edsl/jobs/results_exceptions_handler.py +0 -98
  149. edsl/language_models/ComputeCost.py +0 -63
  150. edsl/language_models/PriceManager.py +0 -127
  151. edsl/language_models/RawResponseHandler.py +0 -106
  152. edsl/language_models/ServiceDataSources.py +0 -0
  153. edsl/language_models/key_management/KeyLookup.py +0 -63
  154. edsl/language_models/key_management/KeyLookupBuilder.py +0 -273
  155. edsl/language_models/key_management/KeyLookupCollection.py +0 -38
  156. edsl/language_models/key_management/__init__.py +0 -0
  157. edsl/language_models/key_management/models.py +0 -131
  158. edsl/language_models/model.py +0 -256
  159. edsl/notebooks/NotebookToLaTeX.py +0 -142
  160. edsl/questions/ExceptionExplainer.py +0 -77
  161. edsl/questions/HTMLQuestion.py +0 -103
  162. edsl/questions/QuestionMatrix.py +0 -265
  163. edsl/questions/data_structures.py +0 -20
  164. edsl/questions/loop_processor.py +0 -149
  165. edsl/questions/response_validator_factory.py +0 -34
  166. edsl/questions/templates/matrix/__init__.py +0 -1
  167. edsl/questions/templates/matrix/answering_instructions.jinja +0 -5
  168. edsl/questions/templates/matrix/question_presentation.jinja +0 -20
  169. edsl/results/MarkdownToDocx.py +0 -122
  170. edsl/results/MarkdownToPDF.py +0 -111
  171. edsl/results/TextEditor.py +0 -50
  172. edsl/results/file_exports.py +0 -252
  173. edsl/results/smart_objects.py +0 -96
  174. edsl/results/table_data_class.py +0 -12
  175. edsl/results/table_renderers.py +0 -118
  176. edsl/scenarios/ConstructDownloadLink.py +0 -109
  177. edsl/scenarios/DocumentChunker.py +0 -102
  178. edsl/scenarios/DocxScenario.py +0 -16
  179. edsl/scenarios/PdfExtractor.py +0 -40
  180. edsl/scenarios/directory_scanner.py +0 -96
  181. edsl/scenarios/file_methods.py +0 -85
  182. edsl/scenarios/handlers/__init__.py +0 -13
  183. edsl/scenarios/handlers/csv.py +0 -49
  184. edsl/scenarios/handlers/docx.py +0 -76
  185. edsl/scenarios/handlers/html.py +0 -37
  186. edsl/scenarios/handlers/json.py +0 -111
  187. edsl/scenarios/handlers/latex.py +0 -5
  188. edsl/scenarios/handlers/md.py +0 -51
  189. edsl/scenarios/handlers/pdf.py +0 -68
  190. edsl/scenarios/handlers/png.py +0 -39
  191. edsl/scenarios/handlers/pptx.py +0 -105
  192. edsl/scenarios/handlers/py.py +0 -294
  193. edsl/scenarios/handlers/sql.py +0 -313
  194. edsl/scenarios/handlers/sqlite.py +0 -149
  195. edsl/scenarios/handlers/txt.py +0 -33
  196. edsl/scenarios/scenario_selector.py +0 -156
  197. edsl/surveys/ConstructDAG.py +0 -92
  198. edsl/surveys/EditSurvey.py +0 -221
  199. edsl/surveys/InstructionHandler.py +0 -100
  200. edsl/surveys/MemoryManagement.py +0 -72
  201. edsl/surveys/RuleManager.py +0 -172
  202. edsl/surveys/Simulator.py +0 -75
  203. edsl/surveys/SurveyToApp.py +0 -141
  204. edsl/utilities/PrettyList.py +0 -56
  205. edsl/utilities/is_notebook.py +0 -18
  206. edsl/utilities/is_valid_variable_name.py +0 -11
  207. edsl/utilities/remove_edsl_version.py +0 -24
  208. edsl-0.1.39.dist-info/RECORD +0 -358
  209. /edsl/questions/{register_questions_meta.py → RegisterQuestionsMeta.py} +0 -0
  210. /edsl/results/{results_fetch_mixin.py → ResultsFetchMixin.py} +0 -0
  211. /edsl/results/{results_tools_mixin.py → ResultsToolsMixin.py} +0 -0
  212. {edsl-0.1.39.dist-info → edsl-0.1.39.dev1.dist-info}/LICENSE +0 -0
@@ -1,122 +0,0 @@
1
- from typing import Optional
2
- import subprocess
3
- import os
4
- from pathlib import Path
5
- import tempfile
6
-
7
-
8
- class MarkdownToDocx:
9
- def __init__(self, markdown_content: str, filename: Optional[str] = None):
10
- """
11
- Initialize the converter with markdown content.
12
-
13
- Args:
14
- markdown_content (str): The markdown content to be converted
15
- """
16
- self.markdown_content = markdown_content
17
- self.filename = filename
18
- self._check_pandoc()
19
-
20
- def _check_pandoc(self):
21
- """Check if pandoc is installed and accessible."""
22
- try:
23
- subprocess.run(["pandoc", "--version"], capture_output=True, check=True)
24
- except (subprocess.CalledProcessError, FileNotFoundError):
25
- raise RuntimeError(
26
- "Pandoc is not installed or not found in PATH. "
27
- "Please install pandoc before using this converter."
28
- )
29
-
30
- def convert(self, output_path: str, **options) -> bool:
31
- """
32
- Convert the markdown content to DOCX.
33
-
34
- Args:
35
- output_path (str): Path where the DOCX should be saved
36
- **options: Additional conversion options
37
- reference_doc (str): Path to reference docx for styling
38
- toc (bool): Include table of contents (default: False)
39
- number_sections (bool): Number sections (default: False)
40
- highlight_style (str): Code highlighting style (default: "tango")
41
-
42
- Returns:
43
- bool: True if conversion was successful, False otherwise
44
- """
45
- # Ensure output directory exists
46
- output_path = Path(output_path)
47
- output_path.parent.mkdir(parents=True, exist_ok=True)
48
-
49
- # Build pandoc command
50
- cmd = ["pandoc", "-f", "markdown", "-t", "docx", "-o", str(output_path)]
51
-
52
- # Add reference doc if provided
53
- if "reference_doc" in options:
54
- ref_doc = Path(options["reference_doc"])
55
- if ref_doc.exists():
56
- cmd.extend(["--reference-doc", str(ref_doc)])
57
- else:
58
- print(f"Warning: Reference document {ref_doc} not found")
59
-
60
- # Add optional parameters
61
- if options.get("toc", False):
62
- cmd.append("--toc")
63
-
64
- if options.get("number_sections", False):
65
- cmd.append("--number-sections")
66
-
67
- if "highlight_style" in options:
68
- cmd.extend(["--highlight-style", options["highlight_style"]])
69
-
70
- try:
71
- # Run pandoc command
72
- result = subprocess.run(
73
- cmd,
74
- input=self.markdown_content,
75
- text=True,
76
- capture_output=True,
77
- check=True,
78
- )
79
- return True
80
- except subprocess.CalledProcessError as e:
81
- print(f"Error converting markdown to DOCX: {e.stderr}")
82
- return False
83
-
84
- def preview(self) -> str:
85
- """
86
- Generate a temporary DOCX and return its path.
87
-
88
- Returns:
89
- str: Path to the temporary DOCX file
90
- """
91
- temp_dir = tempfile.mkdtemp()
92
- if self.filename:
93
- temp_docx = os.path.join(temp_dir, self.filename)
94
- else:
95
- temp_docx = os.path.join(temp_dir, "preview.docx")
96
-
97
- if self.convert(temp_docx):
98
- from edsl.scenarios.FileStore import FileStore
99
-
100
- return FileStore(path=temp_docx)
101
-
102
- return None
103
-
104
- def create_template(self, output_path: str) -> bool:
105
- """
106
- Create a reference DOCX template that can be modified for styling.
107
-
108
- Args:
109
- output_path (str): Path where the template should be saved
110
-
111
- Returns:
112
- bool: True if template was created successfully, False otherwise
113
- """
114
- try:
115
- cmd = ["pandoc", "--print-default-data-file", "reference.docx"]
116
-
117
- with open(output_path, "wb") as f:
118
- result = subprocess.run(cmd, stdout=f, check=True)
119
- return True
120
- except subprocess.CalledProcessError as e:
121
- print(f"Error creating template: {e.stderr}")
122
- return False
@@ -1,111 +0,0 @@
1
- from typing import Optional
2
- import subprocess
3
- import os
4
- from pathlib import Path
5
- import tempfile
6
-
7
-
8
- class MarkdownToPDF:
9
- def __init__(self, markdown_content: str, filename: Optional[str] = None):
10
- """
11
- Initialize the converter with markdown content.
12
-
13
- Args:
14
- markdown_content (str): The markdown content to be converted
15
- """
16
- self.markdown_content = markdown_content
17
- self.filename = filename
18
- self._check_pandoc()
19
- # self.convert()
20
-
21
- def _check_pandoc(self):
22
- """Check if pandoc is installed and accessible."""
23
- try:
24
- subprocess.run(["pandoc", "--version"], capture_output=True, check=True)
25
- except (subprocess.CalledProcessError, FileNotFoundError):
26
- raise RuntimeError(
27
- "Pandoc is not installed or not found in PATH. "
28
- "Please install pandoc before using this converter."
29
- )
30
-
31
- def convert(self, output_path: str, **options) -> bool:
32
- """
33
- Convert the markdown content to PDF.
34
-
35
- Args:
36
- output_path (str): Path where the PDF should be saved
37
- **options: Additional conversion options
38
- margin (str): Page margin (default: "1in")
39
- font_size (str): Font size (default: "12pt")
40
- toc (bool): Include table of contents (default: False)
41
- number_sections (bool): Number sections (default: False)
42
- highlight_style (str): Code highlighting style (default: "tango")
43
-
44
- Returns:
45
- bool: True if conversion was successful, False otherwise
46
- """
47
- # Ensure output directory exists
48
- output_path = Path(output_path)
49
- output_path.parent.mkdir(parents=True, exist_ok=True)
50
-
51
- # Build pandoc command with default options
52
- cmd = [
53
- "pandoc",
54
- "-f",
55
- "markdown",
56
- "-o",
57
- str(output_path),
58
- "--pdf-engine=xelatex",
59
- "--variable",
60
- f'geometry:margin={options.get("margin", "1in")}',
61
- "--variable",
62
- f'fontsize={options.get("font_size", "12pt")}',
63
- ]
64
-
65
- # Add font only if specifically provided
66
- if "font" in options:
67
- cmd.extend(["--variable", f'mainfont={options["font"]}'])
68
-
69
- # Add optional parameters
70
- if options.get("toc", False):
71
- cmd.append("--toc")
72
-
73
- if options.get("number_sections", False):
74
- cmd.append("--number-sections")
75
-
76
- if "highlight_style" in options:
77
- cmd.extend(["--highlight-style", options["highlight_style"]])
78
-
79
- try:
80
- # Run pandoc command
81
- result = subprocess.run(
82
- cmd,
83
- input=self.markdown_content,
84
- text=True,
85
- capture_output=True,
86
- check=True,
87
- )
88
- return True
89
- except subprocess.CalledProcessError as e:
90
- print(f"Error converting markdown to PDF: {e.stderr}")
91
- return False
92
-
93
- def preview(self) -> str:
94
- """
95
- Generate a temporary PDF and return its path.
96
-
97
- Returns:
98
- str: Path to the temporary PDF file
99
- """
100
- temp_dir = tempfile.mkdtemp()
101
- if self.filename:
102
- temp_pdf = os.path.join(temp_dir, f"{self.filename}.pdf")
103
- else:
104
- temp_pdf = os.path.join(temp_dir, "preview.pdf")
105
-
106
- if self.convert(temp_pdf):
107
- from edsl.scenarios.FileStore import FileStore
108
-
109
- return FileStore(temp_pdf)
110
-
111
- return None
@@ -1,50 +0,0 @@
1
- try:
2
- import gradio as gr
3
- except ImportError:
4
- print("Gradio is not installed. Please install it using `pip install gradio`")
5
-
6
- import time
7
-
8
-
9
- class TextEditor:
10
- def __init__(self, initial_text=""):
11
- self.text = initial_text
12
- self._text_saved = False
13
-
14
- def save_text(self, new_text):
15
- self.text = new_text
16
- self._text_saved = True
17
- return "Text saved successfully!"
18
-
19
- def edit_gui(self):
20
- js_code = """
21
- async (text) => {
22
- await navigator.clipboard.writeText(text);
23
- return "Copied to clipboard!";
24
- }
25
- """
26
-
27
- with gr.Blocks() as interface:
28
- text_area = gr.Textbox(
29
- value=self.text, lines=10, label="Edit Text", placeholder="Type here..."
30
- )
31
-
32
- with gr.Row():
33
- save_btn = gr.Button("Save")
34
- copy_btn = gr.Button("Copy to Clipboard")
35
-
36
- output = gr.Textbox(label="Status")
37
-
38
- save_btn.click(fn=self.save_text, inputs=[text_area], outputs=[output])
39
-
40
- # Add copy functionality
41
- copy_btn.click(
42
- fn=None, inputs=text_area, outputs=output, api_name=False, js=js_code
43
- )
44
-
45
- interface.launch(share=False, prevent_thread_lock=True)
46
-
47
- while not self._text_saved:
48
- time.sleep(0.1)
49
-
50
- return self.text
@@ -1,252 +0,0 @@
1
- from abc import ABC, abstractmethod
2
- import io
3
- import csv
4
- import base64
5
- from typing import Optional, Union, Tuple, List, Any, Dict
6
- from openpyxl import Workbook
7
-
8
- from edsl.scenarios.FileStore import FileStore
9
-
10
-
11
- class FileExport(ABC):
12
- def __init__(
13
- self,
14
- data: Any,
15
- filename: Optional[str] = None,
16
- remove_prefix: bool = False,
17
- pretty_labels: Optional[Dict[str, str]] = None,
18
- ):
19
- self.data = data
20
- self.filename = filename # or self._get_default_filename()
21
- self.remove_prefix = remove_prefix
22
- self.pretty_labels = pretty_labels
23
-
24
- @property
25
- def mime_type(self) -> str:
26
- """Return the MIME type for this export format."""
27
- return self.__class__.mime_type
28
-
29
- @property
30
- def suffix(self) -> str:
31
- """Return the file suffix for this format."""
32
- return self.__class__.suffix
33
-
34
- @property
35
- def is_binary(self) -> bool:
36
- """Whether the format is binary or text-based."""
37
- return self.__class__.is_binary
38
-
39
- def _get_default_filename(self) -> str:
40
- """Generate default filename for this format."""
41
- return f"results.{self.suffix}"
42
-
43
- def _create_filestore(self, data: Union[str, bytes]) -> "FileStore":
44
- """Create a FileStore instance with encoded data."""
45
- if isinstance(data, str):
46
- base64_string = base64.b64encode(data.encode()).decode()
47
- else:
48
- base64_string = base64.b64encode(data).decode()
49
-
50
- from edsl.scenarios.FileStore import FileStore
51
-
52
- path = self.filename or self._get_default_filename()
53
-
54
- fs = FileStore(
55
- path=path,
56
- mime_type=self.mime_type,
57
- binary=self.is_binary,
58
- suffix=self.suffix,
59
- base64_string=base64_string,
60
- )
61
-
62
- if self.filename is not None:
63
- fs.write(self.filename)
64
- return None
65
- return fs
66
-
67
- @abstractmethod
68
- def format_data(self) -> Union[str, bytes]:
69
- """Convert the input data to the target format."""
70
- pass
71
-
72
- def export(self) -> Optional["FileStore"]:
73
- """Export the data to a FileStore instance."""
74
- formatted_data = self.format_data()
75
- return self._create_filestore(formatted_data)
76
-
77
-
78
- class JSONLExport(FileExport):
79
- mime_type = "application/jsonl"
80
- suffix = "jsonl"
81
- is_binary = False
82
-
83
- def format_data(self) -> str:
84
- output = io.StringIO()
85
- for entry in self.data:
86
- key, values = list(entry.items())[0]
87
- output.write(f'{{"{key}": {values}}}\n')
88
- return output.getvalue()
89
-
90
-
91
- class TabularExport(FileExport, ABC):
92
- """Base class for exports that use tabular data."""
93
-
94
- def __init__(self, *args, **kwargs):
95
- super().__init__(*args, **kwargs)
96
- self.header, self.rows = self.data._get_tabular_data(
97
- remove_prefix=self.remove_prefix, pretty_labels=self.pretty_labels
98
- )
99
-
100
-
101
- class CSVExport(TabularExport):
102
- mime_type = "text/csv"
103
- suffix = "csv"
104
- is_binary = False
105
-
106
- def format_data(self) -> str:
107
- output = io.StringIO()
108
- writer = csv.writer(output)
109
- writer.writerow(self.header)
110
- writer.writerows(self.rows)
111
- return output.getvalue()
112
-
113
-
114
- class ExcelExport(TabularExport):
115
- mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
116
- suffix = "xlsx"
117
- is_binary = True
118
-
119
- def __init__(self, *args, sheet_name: Optional[str] = None, **kwargs):
120
- super().__init__(*args, **kwargs)
121
- self.sheet_name = sheet_name or "Results"
122
-
123
- def format_data(self) -> bytes:
124
- wb = Workbook()
125
- ws = wb.active
126
- ws.title = self.sheet_name
127
-
128
- # Write header
129
- for col, value in enumerate(self.header, 1):
130
- ws.cell(row=1, column=col, value=value)
131
-
132
- # Write data rows
133
- for row_idx, row_data in enumerate(self.rows, 2):
134
- for col, value in enumerate(row_data, 1):
135
- ws.cell(row=row_idx, column=col, value=value)
136
-
137
- # Save to bytes buffer
138
- buffer = io.BytesIO()
139
- wb.save(buffer)
140
- buffer.seek(0)
141
- return buffer.getvalue()
142
-
143
-
144
- import sqlite3
145
- from typing import Any
146
-
147
-
148
- class SQLiteExport(TabularExport):
149
- mime_type = "application/x-sqlite3"
150
- suffix = "db"
151
- is_binary = True
152
-
153
- def __init__(
154
- self, *args, table_name: str = "results", if_exists: str = "replace", **kwargs
155
- ):
156
- """
157
- Initialize SQLite export.
158
-
159
- Args:
160
- table_name: Name of the table to create
161
- if_exists: How to handle existing table ('fail', 'replace', or 'append')
162
- """
163
- super().__init__(*args, **kwargs)
164
- self.table_name = table_name
165
- self.if_exists = if_exists
166
-
167
- def _get_column_types(self) -> list[tuple[str, str]]:
168
- """Infer SQL column types from the data."""
169
- column_types = []
170
-
171
- # Check first row of data for types
172
- if self.rows:
173
- first_row = self.rows[0]
174
- for header, value in zip(self.header, first_row):
175
- if isinstance(value, bool):
176
- sql_type = "BOOLEAN"
177
- elif isinstance(value, int):
178
- sql_type = "INTEGER"
179
- elif isinstance(value, float):
180
- sql_type = "REAL"
181
- else:
182
- sql_type = "TEXT"
183
- column_types.append((header, sql_type))
184
- else:
185
- # If no data, default to TEXT
186
- column_types = [(header, "TEXT") for header in self.header]
187
-
188
- return column_types
189
-
190
- def _create_table(self, cursor: sqlite3.Cursor) -> None:
191
- """Create the table with appropriate schema."""
192
- column_types = self._get_column_types()
193
-
194
- # Drop existing table if replace mode
195
- if self.if_exists == "replace":
196
- cursor.execute(f"DROP TABLE IF EXISTS {self.table_name}")
197
- elif self.if_exists == "fail":
198
- cursor.execute(
199
- f"SELECT name FROM sqlite_master WHERE type='table' AND name=?",
200
- (self.table_name,),
201
- )
202
- if cursor.fetchone():
203
- raise ValueError(f"Table {self.table_name} already exists")
204
-
205
- # Create table
206
- columns = ", ".join(f'"{col}" {dtype}' for col, dtype in column_types)
207
- create_table_sql = f"""
208
- CREATE TABLE IF NOT EXISTS {self.table_name} (
209
- {columns}
210
- )
211
- """
212
- cursor.execute(create_table_sql)
213
-
214
- def format_data(self) -> bytes:
215
- """Convert the data to a SQLite database file."""
216
- buffer = io.BytesIO()
217
-
218
- # Create in-memory database
219
- conn = sqlite3.connect(":memory:")
220
- cursor = conn.cursor()
221
-
222
- # Create table and insert data
223
- self._create_table(cursor)
224
-
225
- # Prepare placeholders for INSERT
226
- placeholders = ",".join(["?" for _ in self.header])
227
- insert_sql = f"INSERT INTO {self.table_name} ({','.join(self.header)}) VALUES ({placeholders})"
228
-
229
- # Insert data
230
- cursor.executemany(insert_sql, self.rows)
231
- conn.commit()
232
-
233
- # Save to file buffer
234
- conn.backup(sqlite3.connect(buffer))
235
- conn.close()
236
-
237
- buffer.seek(0)
238
- return buffer.getvalue()
239
-
240
- def _validate_params(self) -> None:
241
- """Validate initialization parameters."""
242
- valid_if_exists = {"fail", "replace", "append"}
243
- if self.if_exists not in valid_if_exists:
244
- raise ValueError(
245
- f"if_exists must be one of {valid_if_exists}, got {self.if_exists}"
246
- )
247
-
248
- # Validate table name (basic SQLite identifier validation)
249
- if not self.table_name.isalnum() and not all(c in "_" for c in self.table_name):
250
- raise ValueError(
251
- f"Invalid table name: {self.table_name}. Must contain only alphanumeric characters and underscores."
252
- )
@@ -1,96 +0,0 @@
1
- from typing import Optional
2
-
3
-
4
- class SmartInt(int):
5
- pass
6
-
7
-
8
- class SmartFloat(float):
9
- pass
10
-
11
-
12
- class SmartStr(str):
13
- def clipboard(self) -> None:
14
- try:
15
- import pyperclip
16
- except ImportError:
17
- print(
18
- "pyperclip is not installed. Run `pip install pyperclip` to install it."
19
- )
20
- return None
21
-
22
- pyperclip.copy(self)
23
- print("Text copied to clipboard.")
24
-
25
- def write(self, filename: str):
26
- with open(filename, "w") as f:
27
- f.write(str(self))
28
- return None
29
-
30
- def _repr_html_(self):
31
- pass
32
-
33
- def markdown(self):
34
- return SmartMarkdown(self)
35
-
36
- def pdf(self, filename: Optional[str] = None): # Markdown will have this as well
37
- # renders the markdown as a pdf that can be downloaded
38
- from edsl.results.MarkdownToPDF import MarkdownToPDF
39
-
40
- return MarkdownToPDF(self, filename).preview()
41
-
42
- def docx(self, filename: Optional[str] = None):
43
- # renders the markdown as a docx that can be downloaded
44
- from edsl.results.MarkdownToDocx import MarkdownToDocx
45
-
46
- return MarkdownToDocx(self, filename).preview()
47
-
48
- def edit(self):
49
- from edsl.results.TextEditor import TextEditor
50
-
51
- editor = TextEditor(self)
52
- self = self.__class__(editor.edit_gui())
53
- # print(f"Updated text: {self}")
54
-
55
-
56
- class SmartMarkdown(SmartStr):
57
- def _repr_markdown_(self):
58
- return self
59
-
60
- def _repr_html_(self):
61
- from IPython.display import Markdown, display
62
-
63
- display(Markdown(self))
64
-
65
-
66
- class SmartLaTeX(SmartStr):
67
- def _repr_html_(self):
68
- print(self)
69
-
70
- def pdf(self, filename: Optional[str] = None):
71
- from edsl.results.LaTeXToPDF import LaTeXToPDF
72
-
73
- return LaTeXToPDF(self, filename).preview()
74
-
75
- def docx(self, filename: Optional[str] = None):
76
- from edsl.results.LaTeXToDocx import LaTeXToDocx
77
-
78
- return LaTeXToDocx(self, filename).preview()
79
-
80
- def edit(self):
81
- from edsl.results.TextEditor import TextEditor
82
-
83
- editor = TextEditor(self)
84
- self = self.__class__(editor.edit_gui())
85
- # print(f"Updated LaTeX: {self}")
86
-
87
-
88
- class FirstObject:
89
- def __new__(self, value):
90
- if isinstance(value, int):
91
- return SmartInt(value)
92
- if isinstance(value, float):
93
- return SmartFloat(value)
94
- if isinstance(value, str):
95
- return SmartStr(value)
96
- return value
@@ -1,12 +0,0 @@
1
- from dataclasses import dataclass
2
- from typing import Any, List
3
-
4
-
5
- @dataclass
6
- class TableData:
7
- """Simple data class to hold table information"""
8
-
9
- headers: List[str]
10
- data: List[List[Any]]
11
- parameters: dict = None
12
- raw_data_set: Any = None