edsl 0.1.49__py3-none-any.whl → 0.1.51__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/__init__.py +124 -53
- edsl/__version__.py +1 -1
- edsl/agents/agent.py +21 -21
- edsl/agents/agent_list.py +2 -5
- edsl/agents/exceptions.py +119 -5
- edsl/base/__init__.py +10 -35
- edsl/base/base_class.py +71 -36
- edsl/base/base_exception.py +204 -0
- edsl/base/data_transfer_models.py +1 -1
- edsl/base/exceptions.py +94 -0
- edsl/buckets/__init__.py +15 -1
- edsl/buckets/bucket_collection.py +3 -4
- edsl/buckets/exceptions.py +107 -0
- edsl/buckets/model_buckets.py +1 -2
- edsl/buckets/token_bucket.py +11 -6
- edsl/buckets/token_bucket_api.py +27 -12
- edsl/buckets/token_bucket_client.py +9 -7
- edsl/caching/cache.py +12 -4
- edsl/caching/cache_entry.py +10 -9
- edsl/caching/exceptions.py +113 -7
- edsl/caching/remote_cache_sync.py +6 -7
- edsl/caching/sql_dict.py +20 -14
- edsl/cli.py +43 -0
- edsl/config/__init__.py +1 -1
- edsl/config/config_class.py +32 -6
- edsl/conversation/Conversation.py +8 -4
- edsl/conversation/car_buying.py +1 -3
- edsl/conversation/exceptions.py +58 -0
- edsl/conversation/mug_negotiation.py +2 -8
- edsl/coop/__init__.py +28 -6
- edsl/coop/coop.py +120 -29
- edsl/coop/coop_functions.py +1 -1
- edsl/coop/ep_key_handling.py +1 -1
- edsl/coop/exceptions.py +188 -9
- edsl/coop/price_fetcher.py +5 -8
- edsl/coop/utils.py +4 -6
- edsl/dataset/__init__.py +5 -4
- edsl/dataset/dataset.py +177 -86
- edsl/dataset/dataset_operations_mixin.py +98 -76
- edsl/dataset/dataset_tree.py +11 -7
- edsl/dataset/display/table_display.py +0 -2
- edsl/dataset/display/table_renderers.py +6 -4
- edsl/dataset/exceptions.py +125 -0
- edsl/dataset/file_exports.py +18 -11
- edsl/dataset/r/ggplot.py +13 -6
- edsl/display/__init__.py +27 -0
- edsl/display/core.py +147 -0
- edsl/display/plugin.py +189 -0
- edsl/display/utils.py +52 -0
- edsl/inference_services/__init__.py +9 -1
- edsl/inference_services/available_model_cache_handler.py +1 -1
- edsl/inference_services/available_model_fetcher.py +5 -6
- edsl/inference_services/data_structures.py +10 -7
- edsl/inference_services/exceptions.py +132 -1
- edsl/inference_services/inference_service_abc.py +2 -2
- edsl/inference_services/inference_services_collection.py +2 -6
- edsl/inference_services/registry.py +4 -3
- edsl/inference_services/service_availability.py +4 -3
- edsl/inference_services/services/anthropic_service.py +4 -1
- edsl/inference_services/services/aws_bedrock.py +13 -12
- edsl/inference_services/services/azure_ai.py +12 -10
- edsl/inference_services/services/deep_infra_service.py +1 -4
- edsl/inference_services/services/deep_seek_service.py +1 -5
- edsl/inference_services/services/google_service.py +7 -3
- edsl/inference_services/services/groq_service.py +1 -1
- edsl/inference_services/services/mistral_ai_service.py +4 -2
- edsl/inference_services/services/ollama_service.py +1 -1
- edsl/inference_services/services/open_ai_service.py +7 -5
- edsl/inference_services/services/perplexity_service.py +6 -2
- edsl/inference_services/services/test_service.py +8 -7
- edsl/inference_services/services/together_ai_service.py +2 -3
- edsl/inference_services/services/xai_service.py +1 -1
- edsl/instructions/__init__.py +1 -1
- edsl/instructions/change_instruction.py +7 -5
- edsl/instructions/exceptions.py +61 -0
- edsl/instructions/instruction.py +6 -2
- edsl/instructions/instruction_collection.py +6 -4
- edsl/instructions/instruction_handler.py +12 -15
- edsl/interviews/ReportErrors.py +0 -3
- edsl/interviews/__init__.py +9 -2
- edsl/interviews/answering_function.py +11 -13
- edsl/interviews/exception_tracking.py +15 -8
- edsl/interviews/exceptions.py +79 -0
- edsl/interviews/interview.py +33 -30
- edsl/interviews/interview_status_dictionary.py +4 -2
- edsl/interviews/interview_status_log.py +2 -1
- edsl/interviews/interview_task_manager.py +5 -5
- edsl/interviews/request_token_estimator.py +5 -2
- edsl/interviews/statistics.py +3 -4
- edsl/invigilators/__init__.py +7 -1
- edsl/invigilators/exceptions.py +79 -0
- edsl/invigilators/invigilator_base.py +0 -1
- edsl/invigilators/invigilators.py +9 -13
- edsl/invigilators/prompt_constructor.py +1 -5
- edsl/invigilators/prompt_helpers.py +8 -4
- edsl/invigilators/question_instructions_prompt_builder.py +1 -1
- edsl/invigilators/question_option_processor.py +9 -5
- edsl/invigilators/question_template_replacements_builder.py +3 -2
- edsl/jobs/__init__.py +42 -5
- edsl/jobs/async_interview_runner.py +25 -23
- edsl/jobs/check_survey_scenario_compatibility.py +11 -10
- edsl/jobs/data_structures.py +8 -5
- edsl/jobs/exceptions.py +177 -8
- edsl/jobs/fetch_invigilator.py +1 -1
- edsl/jobs/jobs.py +74 -69
- edsl/jobs/jobs_checks.py +6 -7
- edsl/jobs/jobs_component_constructor.py +4 -4
- edsl/jobs/jobs_pricing_estimation.py +4 -3
- edsl/jobs/jobs_remote_inference_logger.py +5 -4
- edsl/jobs/jobs_runner_asyncio.py +3 -4
- edsl/jobs/jobs_runner_status.py +8 -9
- edsl/jobs/remote_inference.py +27 -24
- edsl/jobs/results_exceptions_handler.py +10 -7
- edsl/key_management/__init__.py +3 -1
- edsl/key_management/exceptions.py +62 -0
- edsl/key_management/key_lookup.py +1 -1
- edsl/key_management/key_lookup_builder.py +37 -14
- edsl/key_management/key_lookup_collection.py +2 -0
- edsl/language_models/__init__.py +1 -1
- edsl/language_models/exceptions.py +302 -14
- edsl/language_models/language_model.py +9 -8
- edsl/language_models/model.py +4 -4
- edsl/language_models/model_list.py +1 -1
- edsl/language_models/price_manager.py +1 -1
- edsl/language_models/raw_response_handler.py +14 -9
- edsl/language_models/registry.py +17 -21
- edsl/language_models/repair.py +0 -6
- edsl/language_models/unused/fake_openai_service.py +0 -1
- edsl/load_plugins.py +69 -0
- edsl/logger.py +146 -0
- edsl/notebooks/__init__.py +24 -1
- edsl/notebooks/exceptions.py +82 -0
- edsl/notebooks/notebook.py +7 -3
- edsl/notebooks/notebook_to_latex.py +1 -2
- edsl/plugins/__init__.py +63 -0
- edsl/plugins/built_in/export_example.py +50 -0
- edsl/plugins/built_in/pig_latin.py +67 -0
- edsl/plugins/cli.py +372 -0
- edsl/plugins/cli_typer.py +283 -0
- edsl/plugins/exceptions.py +31 -0
- edsl/plugins/hookspec.py +51 -0
- edsl/plugins/plugin_host.py +128 -0
- edsl/plugins/plugin_manager.py +633 -0
- edsl/plugins/plugins_registry.py +168 -0
- edsl/prompts/__init__.py +24 -1
- edsl/prompts/exceptions.py +107 -5
- edsl/prompts/prompt.py +15 -7
- edsl/questions/HTMLQuestion.py +5 -11
- edsl/questions/Quick.py +0 -1
- edsl/questions/__init__.py +6 -4
- edsl/questions/answer_validator_mixin.py +318 -323
- edsl/questions/compose_questions.py +3 -3
- edsl/questions/descriptors.py +11 -50
- edsl/questions/exceptions.py +278 -22
- edsl/questions/loop_processor.py +7 -5
- edsl/questions/prompt_templates/question_list.jinja +3 -0
- edsl/questions/question_base.py +46 -19
- edsl/questions/question_base_gen_mixin.py +2 -2
- edsl/questions/question_base_prompts_mixin.py +13 -7
- edsl/questions/question_budget.py +503 -98
- edsl/questions/question_check_box.py +660 -160
- edsl/questions/question_dict.py +345 -194
- edsl/questions/question_extract.py +401 -61
- edsl/questions/question_free_text.py +80 -14
- edsl/questions/question_functional.py +119 -9
- edsl/questions/{derived/question_likert_five.py → question_likert_five.py} +2 -2
- edsl/questions/{derived/question_linear_scale.py → question_linear_scale.py} +3 -4
- edsl/questions/question_list.py +275 -28
- edsl/questions/question_matrix.py +643 -96
- edsl/questions/question_multiple_choice.py +219 -51
- edsl/questions/question_numerical.py +361 -32
- edsl/questions/question_rank.py +401 -124
- edsl/questions/question_registry.py +7 -5
- edsl/questions/{derived/question_top_k.py → question_top_k.py} +3 -3
- edsl/questions/{derived/question_yes_no.py → question_yes_no.py} +3 -4
- edsl/questions/register_questions_meta.py +2 -2
- edsl/questions/response_validator_abc.py +13 -15
- edsl/questions/response_validator_factory.py +10 -12
- edsl/questions/templates/dict/answering_instructions.jinja +1 -0
- edsl/questions/templates/rank/question_presentation.jinja +1 -1
- edsl/results/__init__.py +1 -1
- edsl/results/exceptions.py +141 -7
- edsl/results/report.py +1 -2
- edsl/results/result.py +11 -9
- edsl/results/results.py +480 -321
- edsl/results/results_selector.py +8 -4
- edsl/scenarios/PdfExtractor.py +2 -2
- edsl/scenarios/construct_download_link.py +69 -35
- edsl/scenarios/directory_scanner.py +33 -14
- edsl/scenarios/document_chunker.py +1 -1
- edsl/scenarios/exceptions.py +238 -14
- edsl/scenarios/file_methods.py +1 -1
- edsl/scenarios/file_store.py +7 -3
- edsl/scenarios/handlers/__init__.py +17 -0
- edsl/scenarios/handlers/docx_file_store.py +0 -5
- edsl/scenarios/handlers/pdf_file_store.py +0 -1
- edsl/scenarios/handlers/pptx_file_store.py +0 -5
- edsl/scenarios/handlers/py_file_store.py +0 -1
- edsl/scenarios/handlers/sql_file_store.py +1 -4
- edsl/scenarios/handlers/sqlite_file_store.py +0 -1
- edsl/scenarios/handlers/txt_file_store.py +1 -1
- edsl/scenarios/scenario.py +1 -3
- edsl/scenarios/scenario_list.py +179 -27
- edsl/scenarios/scenario_list_pdf_tools.py +1 -0
- edsl/scenarios/scenario_selector.py +0 -1
- edsl/surveys/__init__.py +3 -4
- edsl/surveys/dag/__init__.py +4 -2
- edsl/surveys/descriptors.py +1 -1
- edsl/surveys/edit_survey.py +1 -0
- edsl/surveys/exceptions.py +165 -9
- edsl/surveys/memory/__init__.py +5 -3
- edsl/surveys/memory/memory_management.py +1 -0
- edsl/surveys/memory/memory_plan.py +6 -15
- edsl/surveys/rules/__init__.py +5 -3
- edsl/surveys/rules/rule.py +1 -2
- edsl/surveys/rules/rule_collection.py +1 -1
- edsl/surveys/survey.py +12 -24
- edsl/surveys/survey_css.py +3 -3
- edsl/surveys/survey_export.py +6 -3
- edsl/surveys/survey_flow_visualization.py +10 -1
- edsl/surveys/survey_simulator.py +2 -1
- edsl/tasks/__init__.py +23 -1
- edsl/tasks/exceptions.py +72 -0
- edsl/tasks/question_task_creator.py +3 -3
- edsl/tasks/task_creators.py +1 -3
- edsl/tasks/task_history.py +8 -10
- edsl/tasks/task_status_log.py +1 -2
- edsl/tokens/__init__.py +29 -1
- edsl/tokens/exceptions.py +37 -0
- edsl/tokens/interview_token_usage.py +3 -2
- edsl/tokens/token_usage.py +4 -3
- edsl/utilities/__init__.py +21 -1
- edsl/utilities/decorators.py +1 -2
- edsl/utilities/markdown_to_docx.py +2 -2
- edsl/utilities/markdown_to_pdf.py +1 -1
- edsl/utilities/repair_functions.py +0 -1
- edsl/utilities/restricted_python.py +0 -1
- edsl/utilities/template_loader.py +2 -3
- edsl/utilities/utilities.py +8 -29
- {edsl-0.1.49.dist-info → edsl-0.1.51.dist-info}/METADATA +32 -2
- edsl-0.1.51.dist-info/RECORD +365 -0
- edsl-0.1.51.dist-info/entry_points.txt +3 -0
- edsl/dataset/smart_objects.py +0 -96
- edsl/exceptions/BaseException.py +0 -21
- edsl/exceptions/__init__.py +0 -54
- edsl/exceptions/configuration.py +0 -16
- edsl/exceptions/general.py +0 -34
- edsl/questions/derived/__init__.py +0 -0
- edsl/study/ObjectEntry.py +0 -173
- edsl/study/ProofOfWork.py +0 -113
- edsl/study/SnapShot.py +0 -80
- edsl/study/Study.py +0 -520
- edsl/study/__init__.py +0 -6
- edsl/utilities/interface.py +0 -135
- edsl-0.1.49.dist-info/RECORD +0 -347
- {edsl-0.1.49.dist-info → edsl-0.1.51.dist-info}/LICENSE +0 -0
- {edsl-0.1.49.dist-info → edsl-0.1.51.dist-info}/WHEEL +0 -0
edsl/dataset/smart_objects.py
DELETED
@@ -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 ..utilities.markdown_to_pdf 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 ..utilities.markdown_to_docx 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
|
edsl/exceptions/BaseException.py
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
class BaseException(Exception):
|
2
|
-
relevant_doc = "https://docs.expectedparrot.com/"
|
3
|
-
|
4
|
-
def __init__(self, message, *, show_docs=True):
|
5
|
-
# Format main error message
|
6
|
-
formatted_message = [message.strip()]
|
7
|
-
|
8
|
-
# Add documentation links if requested
|
9
|
-
if show_docs:
|
10
|
-
if hasattr(self, "relevant_doc"):
|
11
|
-
formatted_message.append(
|
12
|
-
f"\nFor more information, see:\n{self.relevant_doc}"
|
13
|
-
)
|
14
|
-
if hasattr(self, "relevant_notebook"):
|
15
|
-
formatted_message.append(
|
16
|
-
f"\nFor a usage example, see:\n{self.relevant_notebook}"
|
17
|
-
)
|
18
|
-
|
19
|
-
# Join with double newlines for clear separation
|
20
|
-
final_message = "\n\n".join(formatted_message)
|
21
|
-
super().__init__(final_message)
|
edsl/exceptions/__init__.py
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
# from .agents import (
|
2
|
-
# # AgentAttributeLookupCallbackError,
|
3
|
-
# AgentCombinationError,
|
4
|
-
# # AgentLacksLLMError,
|
5
|
-
# # AgentRespondedWithBadJSONError,
|
6
|
-
# )
|
7
|
-
# from .configuration import (
|
8
|
-
# InvalidEnvironmentVariableError,
|
9
|
-
# MissingEnvironmentVariableError,
|
10
|
-
# )
|
11
|
-
# from .data import (
|
12
|
-
# DatabaseConnectionError,
|
13
|
-
# DatabaseCRUDError,
|
14
|
-
# DatabaseIntegrityError,
|
15
|
-
# )
|
16
|
-
|
17
|
-
# from .scenarios import (
|
18
|
-
# ScenarioError,
|
19
|
-
# )
|
20
|
-
|
21
|
-
# from .general import MissingAPIKeyError
|
22
|
-
|
23
|
-
# from .jobs import JobsRunError, InterviewErrorPriorTaskCanceled, InterviewTimeoutError
|
24
|
-
|
25
|
-
# from .language_models import (
|
26
|
-
# LanguageModelResponseNotJSONError,
|
27
|
-
# LanguageModelMissingAttributeError,
|
28
|
-
# LanguageModelAttributeTypeError,
|
29
|
-
# LanguageModelDoNotAddError,
|
30
|
-
# )
|
31
|
-
# from .questions import (
|
32
|
-
# QuestionAnswerValidationError,
|
33
|
-
# QuestionAttributeMissing,
|
34
|
-
# QuestionCreationValidationError,
|
35
|
-
# QuestionResponseValidationError,
|
36
|
-
# QuestionSerializationError,
|
37
|
-
# QuestionScenarioRenderError,
|
38
|
-
# )
|
39
|
-
# from .results import (
|
40
|
-
# ResultsBadMutationstringError,
|
41
|
-
# ResultsColumnNotFoundError,
|
42
|
-
# ResultsInvalidNameError,
|
43
|
-
# ResultsMutateError,
|
44
|
-
# )
|
45
|
-
# from .surveys import (
|
46
|
-
# SurveyCreationError,
|
47
|
-
# SurveyHasNoRulesError,
|
48
|
-
# SurveyRuleCannotEvaluateError,
|
49
|
-
# SurveyRuleCollectionHasNoRulesAtNodeError,
|
50
|
-
# SurveyRuleReferenceInRuleToUnknownQuestionError,
|
51
|
-
# SurveyRuleRefersToFutureStateError,
|
52
|
-
# SurveyRuleSendsYouBackwardsError,
|
53
|
-
# SurveyRuleSkipLogicSyntaxError,
|
54
|
-
# )
|
edsl/exceptions/configuration.py
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
class ConfigurationError(Exception):
|
2
|
-
"""Base exception for errors."""
|
3
|
-
|
4
|
-
pass
|
5
|
-
|
6
|
-
|
7
|
-
class InvalidEnvironmentVariableError(ConfigurationError):
|
8
|
-
"""Raised when an environment variable is invalid."""
|
9
|
-
|
10
|
-
pass
|
11
|
-
|
12
|
-
|
13
|
-
class MissingEnvironmentVariableError(ConfigurationError):
|
14
|
-
"""Raised when an expected environment variable is missing."""
|
15
|
-
|
16
|
-
pass
|
edsl/exceptions/general.py
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
import re
|
2
|
-
from textwrap import dedent
|
3
|
-
|
4
|
-
|
5
|
-
class GeneralErrors(Exception):
|
6
|
-
def __init__(self, message):
|
7
|
-
super().__init__(message)
|
8
|
-
self.message = message
|
9
|
-
|
10
|
-
def __str__(self):
|
11
|
-
return self.make_urls_clickable(self.message)
|
12
|
-
|
13
|
-
@staticmethod
|
14
|
-
def make_urls_clickable(text):
|
15
|
-
url_pattern = r"https?://[^\s]+"
|
16
|
-
urls = re.findall(url_pattern, text)
|
17
|
-
for url in urls:
|
18
|
-
clickable_url = f"\033]8;;{url}\007{url}\033]8;;\007"
|
19
|
-
text = text.replace(url, clickable_url)
|
20
|
-
return text
|
21
|
-
|
22
|
-
|
23
|
-
class MissingAPIKeyError(GeneralErrors):
|
24
|
-
def __init__(self, full_message=None, model_name=None, inference_service=None):
|
25
|
-
if model_name and inference_service:
|
26
|
-
full_message = dedent(
|
27
|
-
f"""
|
28
|
-
An API Key for model `{model_name}` is missing from the .env file.
|
29
|
-
This key is associated with the inference service `{inference_service}`.
|
30
|
-
Please see https://docs.expectedparrot.com/en/latest/api_keys.html for more information.
|
31
|
-
"""
|
32
|
-
)
|
33
|
-
|
34
|
-
super().__init__(full_message)
|
File without changes
|
edsl/study/ObjectEntry.py
DELETED
@@ -1,173 +0,0 @@
|
|
1
|
-
import time
|
2
|
-
import webbrowser
|
3
|
-
from typing import Any, Dict, Optional, Type
|
4
|
-
from edsl.questions import QuestionBase
|
5
|
-
from edsl.base import RegisterSubclassesMeta
|
6
|
-
|
7
|
-
|
8
|
-
class ObjectEntry:
|
9
|
-
def __init__(
|
10
|
-
self,
|
11
|
-
variable_name: str,
|
12
|
-
object: Any,
|
13
|
-
description: str,
|
14
|
-
coop_info: Optional[Dict[str, Any]] = None,
|
15
|
-
created_at: Optional[float] = None,
|
16
|
-
edsl_class_name: Optional[str] = None,
|
17
|
-
):
|
18
|
-
"""
|
19
|
-
Initialize an ObjectEntry instance.
|
20
|
-
|
21
|
-
:param variable_name: The name of the variable.
|
22
|
-
:param object: The object being wrapped.
|
23
|
-
:param description: A description of the object.
|
24
|
-
:param coop_info: Optional Coop information dictionary.
|
25
|
-
:param created_at: Optional creation timestamp. Defaults to current time.
|
26
|
-
:param edsl_class_name: Optional EDSL class name. Defaults to object's class name.
|
27
|
-
"""
|
28
|
-
self.created_at = created_at or time.time()
|
29
|
-
self.variable_name = variable_name
|
30
|
-
self.object = object
|
31
|
-
self.edsl_class_name = edsl_class_name or object.__class__.__name__
|
32
|
-
self.description = description
|
33
|
-
self.coop_info = coop_info
|
34
|
-
|
35
|
-
@classmethod
|
36
|
-
def _get_class(cls, object_dict: Dict[str, Any]) -> Type:
|
37
|
-
"""
|
38
|
-
Get the class of an object from its dictionary representation.
|
39
|
-
|
40
|
-
:param object_dict: The dictionary representation of the object.
|
41
|
-
:return: The class of the object.
|
42
|
-
"""
|
43
|
-
class_name = object_dict["edsl_class_name"]
|
44
|
-
if class_name == "QuestionBase":
|
45
|
-
return QuestionBase
|
46
|
-
else:
|
47
|
-
return RegisterSubclassesMeta._registry[class_name]
|
48
|
-
|
49
|
-
def __repr__(self) -> str:
|
50
|
-
"""
|
51
|
-
Return a string representation of the ObjectEntry instance.
|
52
|
-
|
53
|
-
:return: A string representation of the ObjectEntry instance.
|
54
|
-
"""
|
55
|
-
return f"ObjectEntry(variable_name='{self.variable_name}', object={self.object!r}, description='{self.description}', coop_info={self.coop_info}, created_at={self.created_at}, edsl_class_name='{self.edsl_class_name}')"
|
56
|
-
|
57
|
-
def to_dict(self) -> Dict[str, Any]:
|
58
|
-
"""
|
59
|
-
Convert the ObjectEntry instance to a dictionary.
|
60
|
-
|
61
|
-
:return: A dictionary representation of the ObjectEntry instance.
|
62
|
-
"""
|
63
|
-
return {
|
64
|
-
"created_at": self.created_at,
|
65
|
-
"variable_name": self.variable_name,
|
66
|
-
"object": self.object.to_dict(),
|
67
|
-
"edsl_class_name": self.edsl_class_name,
|
68
|
-
"description": self.description,
|
69
|
-
"coop_info": self.coop_info,
|
70
|
-
}
|
71
|
-
|
72
|
-
@classmethod
|
73
|
-
def from_dict(cls, d: Dict[str, Any]) -> "ObjectEntry":
|
74
|
-
"""
|
75
|
-
Create an ObjectEntry instance from a dictionary.
|
76
|
-
|
77
|
-
:param d: The dictionary representation of the ObjectEntry instance.
|
78
|
-
:return: An ObjectEntry instance.
|
79
|
-
"""
|
80
|
-
d["object"] = cls._get_class(d["object"]).from_dict(d["object"])
|
81
|
-
return cls(**d)
|
82
|
-
|
83
|
-
@property
|
84
|
-
def hash(self) -> str:
|
85
|
-
"""
|
86
|
-
Compute the hash of the object.
|
87
|
-
|
88
|
-
:return: The hash of the object as a string.
|
89
|
-
"""
|
90
|
-
return str(hash(self.object))
|
91
|
-
|
92
|
-
def add_to_namespace(self) -> None:
|
93
|
-
"""
|
94
|
-
Add the object to the global namespace using its variable name.
|
95
|
-
"""
|
96
|
-
globals()[self.variable_name] = self.object
|
97
|
-
|
98
|
-
@property
|
99
|
-
def coop_info(self) -> Optional[Dict[str, Any]]:
|
100
|
-
"""
|
101
|
-
Get the Coop information for the object.
|
102
|
-
|
103
|
-
:return: The Coop information dictionary, if available.
|
104
|
-
"""
|
105
|
-
return self._coop_info
|
106
|
-
|
107
|
-
@coop_info.setter
|
108
|
-
def coop_info(self, coop_info: Optional[Dict[str, Any]]) -> None:
|
109
|
-
"""
|
110
|
-
Set the Coop information for the object.
|
111
|
-
|
112
|
-
:param coop_info: The Coop information dictionary.
|
113
|
-
"""
|
114
|
-
self._coop_info = coop_info
|
115
|
-
|
116
|
-
def view_on_coop(self) -> None:
|
117
|
-
"""
|
118
|
-
Open the object's Coop URL in a web browser.
|
119
|
-
"""
|
120
|
-
if self.coop_info is None:
|
121
|
-
print("Object not pushed to coop")
|
122
|
-
return
|
123
|
-
url = self.coop_info.get("url")
|
124
|
-
webbrowser.open(url)
|
125
|
-
|
126
|
-
def push(self, refresh: Optional[bool] = False) -> Dict[str, Any]:
|
127
|
-
"""
|
128
|
-
Push the object to the Coop.
|
129
|
-
|
130
|
-
:param refresh: Whether to refresh the Coop entry for the object.
|
131
|
-
:return: The Coop info dictionary.
|
132
|
-
"""
|
133
|
-
if self.coop_info is None or refresh:
|
134
|
-
self.coop_info = self.object.push(description=self.description)
|
135
|
-
print(
|
136
|
-
f"Object {self.variable_name} pushed to coop with info: {self._coop_info}"
|
137
|
-
)
|
138
|
-
else:
|
139
|
-
print(
|
140
|
-
f"Object {self.variable_name} already pushed to coop with info: {self._coop_info}"
|
141
|
-
)
|
142
|
-
|
143
|
-
def __eq__(self, other: "ObjectEntry") -> bool:
|
144
|
-
"""
|
145
|
-
Check if two ObjectEntry instances are equal.
|
146
|
-
|
147
|
-
:param other: The other ObjectEntry instance.
|
148
|
-
:return: True if the two instances are equal, False otherwise.
|
149
|
-
"""
|
150
|
-
# if the other item is not "ObjectEntry" type, return False
|
151
|
-
if not isinstance(other, ObjectEntry):
|
152
|
-
return False
|
153
|
-
|
154
|
-
return (
|
155
|
-
self.variable_name == other.variable_name
|
156
|
-
and self.object == other.object
|
157
|
-
and self.description == other.description
|
158
|
-
and self.coop_info == other.coop_info
|
159
|
-
and self.created_at == other.created_at
|
160
|
-
and self.edsl_class_name == other.edsl_class_name
|
161
|
-
)
|
162
|
-
|
163
|
-
|
164
|
-
if __name__ == "__main__":
|
165
|
-
from edsl import QuestionFreeText
|
166
|
-
from edsl.study import ObjectEntry
|
167
|
-
|
168
|
-
q = QuestionFreeText.example()
|
169
|
-
|
170
|
-
oe = ObjectEntry("q", q, "This is a question")
|
171
|
-
d = oe.to_dict()
|
172
|
-
new_oe = ObjectEntry.from_dict(d)
|
173
|
-
new_oe == oe
|
edsl/study/ProofOfWork.py
DELETED
@@ -1,113 +0,0 @@
|
|
1
|
-
import hashlib
|
2
|
-
import time
|
3
|
-
from typing import Any, Dict, List, Optional
|
4
|
-
|
5
|
-
|
6
|
-
class ProofOfWork:
|
7
|
-
def __init__(
|
8
|
-
self,
|
9
|
-
input_data: Optional[Any] = None,
|
10
|
-
proof: Optional[Dict[int, List[Dict[str, Any]]]] = None,
|
11
|
-
):
|
12
|
-
self.input_data = input_data
|
13
|
-
self.proof = proof or {}
|
14
|
-
|
15
|
-
def add_input_data(self, input_data: Any) -> None:
|
16
|
-
self.input_data = input_data
|
17
|
-
|
18
|
-
def to_dict(self) -> Dict[str, Any]:
|
19
|
-
return {
|
20
|
-
"input_data": self.input_data,
|
21
|
-
"proof": self.proof,
|
22
|
-
}
|
23
|
-
|
24
|
-
@classmethod
|
25
|
-
def from_dict(cls, data: Dict[str, Any]) -> "ProofOfWork":
|
26
|
-
return cls(data["input_data"], data["proof"])
|
27
|
-
|
28
|
-
def __repr__(self) -> str:
|
29
|
-
return f"ProofOfWork(input_data={self.input_data}, proof={self.proof})"
|
30
|
-
|
31
|
-
def to_hash(self, nonce: int) -> str:
|
32
|
-
"""
|
33
|
-
Hash the input data combined with the nonce.
|
34
|
-
|
35
|
-
Returns:
|
36
|
-
str: The resulting hash.
|
37
|
-
"""
|
38
|
-
hash_input = self.input_data + str(nonce)
|
39
|
-
return hashlib.md5(hash_input.encode()).hexdigest()
|
40
|
-
|
41
|
-
def verify_work(self) -> bool:
|
42
|
-
for difficulty in self.proof:
|
43
|
-
for proof in self.proof[difficulty]:
|
44
|
-
nonce = proof["nonce"]
|
45
|
-
hash_result = self.to_hash(nonce)
|
46
|
-
prefix = "0" * difficulty
|
47
|
-
if not hash_result.startswith(prefix):
|
48
|
-
return False
|
49
|
-
if hash_result != proof["hash"]:
|
50
|
-
return False
|
51
|
-
return True
|
52
|
-
|
53
|
-
def add_proof(self, difficulty: int, starting_nonce: Optional[int] = None) -> None:
|
54
|
-
"""
|
55
|
-
Find a nonce that results in a hash with `difficulty` number of leading zeros.
|
56
|
-
|
57
|
-
Returns:
|
58
|
-
int, str: The nonce that solves the proof of work and the resulting hash.
|
59
|
-
"""
|
60
|
-
# Convert the difficulty into a string of zeros for comparison
|
61
|
-
prefix = "0" * difficulty
|
62
|
-
if not starting_nonce:
|
63
|
-
import random
|
64
|
-
|
65
|
-
starting_nonce = random.randint(0, 1000000)
|
66
|
-
nonce = starting_nonce
|
67
|
-
start = time.time()
|
68
|
-
while True:
|
69
|
-
# Combine the input data with the nonce and hash it
|
70
|
-
hash_result = self.to_hash(nonce)
|
71
|
-
|
72
|
-
# Check if the hash meets the difficulty requirement
|
73
|
-
if hash_result.startswith(prefix):
|
74
|
-
cycles = nonce - starting_nonce
|
75
|
-
end = time.time()
|
76
|
-
if difficulty in self.proof:
|
77
|
-
self.proof[difficulty].append(
|
78
|
-
{
|
79
|
-
"nonce": nonce,
|
80
|
-
"hash": hash_result,
|
81
|
-
"time": end - start,
|
82
|
-
"cycles": cycles,
|
83
|
-
}
|
84
|
-
)
|
85
|
-
else:
|
86
|
-
self.proof[difficulty] = [
|
87
|
-
{
|
88
|
-
"nonce": nonce,
|
89
|
-
"hash": hash_result,
|
90
|
-
"time": end - start,
|
91
|
-
"cycles": cycles,
|
92
|
-
}
|
93
|
-
]
|
94
|
-
return
|
95
|
-
|
96
|
-
nonce += 1
|
97
|
-
|
98
|
-
|
99
|
-
if __name__ == "__main__":
|
100
|
-
from edsl.study import ProofOfWork
|
101
|
-
|
102
|
-
p = ProofOfWork("hello world")
|
103
|
-
p.add_proof(3)
|
104
|
-
print(p)
|
105
|
-
p.add_proof(6)
|
106
|
-
print(p)
|
107
|
-
|
108
|
-
# Takes about a minute to run
|
109
|
-
p.add_proof(7)
|
110
|
-
print(p)
|
111
|
-
|
112
|
-
ok = p.verify_work()
|
113
|
-
print(ok)
|
edsl/study/SnapShot.py
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
import inspect
|
2
|
-
from typing import Generator, List, Optional
|
3
|
-
|
4
|
-
|
5
|
-
class SnapShot:
|
6
|
-
def __init__(self, namespace, exclude: Optional[List] = None):
|
7
|
-
self.namespace = namespace
|
8
|
-
self.exclude = exclude or []
|
9
|
-
self.edsl_objects = dict(self._get_edsl_objects(namespace=self.namespace))
|
10
|
-
self.edsl_classes = dict(self._get_edsl_classes(namespace=self.namespace))
|
11
|
-
|
12
|
-
def _all_object_keys(self):
|
13
|
-
return self.namespace.keys()
|
14
|
-
|
15
|
-
def __repr__(self):
|
16
|
-
return f"SnapShot(edsl_objects={self.edsl_objects}, edsl_classes={self.edsl_objects})"
|
17
|
-
|
18
|
-
def _get_edsl_classes(
|
19
|
-
self, namespace: dict
|
20
|
-
) -> Generator[tuple[str, type], None, None]:
|
21
|
-
"""Get all EDSL classes in the namespace.
|
22
|
-
|
23
|
-
:param namespace: The namespace to search for EDSL classes. The default is the global namespace.
|
24
|
-
|
25
|
-
>>> sn = SnapShot(namespace = {})
|
26
|
-
>>> sn.edsl_classes
|
27
|
-
{}
|
28
|
-
|
29
|
-
>>> from edsl.caching import Cache
|
30
|
-
>>> sn = SnapShot(namespace = globals())
|
31
|
-
>>> sn.edsl_classes
|
32
|
-
{'Cache': <class 'edsl.caching.cache.Cache'>}
|
33
|
-
"""
|
34
|
-
from ..base import RegisterSubclassesMeta
|
35
|
-
from ..questions import QuestionBase
|
36
|
-
|
37
|
-
all_edsl_objects = RegisterSubclassesMeta.get_registry()
|
38
|
-
|
39
|
-
for name, value in namespace.items():
|
40
|
-
if (
|
41
|
-
inspect.isclass(value)
|
42
|
-
and name in all_edsl_objects
|
43
|
-
and value != RegisterSubclassesMeta
|
44
|
-
):
|
45
|
-
yield name, value
|
46
|
-
if inspect.isclass(value) and issubclass(value, QuestionBase):
|
47
|
-
yield name, value
|
48
|
-
|
49
|
-
def _get_edsl_objects(self, namespace) -> Generator[tuple[str, type], None, None]:
|
50
|
-
"""Get all EDSL objects in the global namespace.
|
51
|
-
|
52
|
-
>>> sn = SnapShot(namespace = globals())
|
53
|
-
>>> sn.edsl_objects
|
54
|
-
{}
|
55
|
-
|
56
|
-
"""
|
57
|
-
from edsl.base import Base
|
58
|
-
from edsl.study.Study import Study
|
59
|
-
|
60
|
-
def is_edsl_object(obj):
|
61
|
-
package_name = "edsl"
|
62
|
-
cls = obj.__class__
|
63
|
-
module_name = cls.__module__
|
64
|
-
return module_name.startswith(package_name)
|
65
|
-
|
66
|
-
for name, value in namespace.items():
|
67
|
-
# TODO check this code logic (if there are other objects with to_dict method that are not from edsl)
|
68
|
-
if (
|
69
|
-
is_edsl_object(value)
|
70
|
-
and hasattr(value, "to_dict")
|
71
|
-
and not inspect.isclass(value)
|
72
|
-
and value.__class__ not in [o.__class__ for o in self.exclude]
|
73
|
-
):
|
74
|
-
yield name, value
|
75
|
-
|
76
|
-
|
77
|
-
if __name__ == "__main__":
|
78
|
-
import doctest
|
79
|
-
|
80
|
-
doctest.testmod(optionflags=doctest.ELLIPSIS)
|