edsl 0.1.27.dev1__py3-none-any.whl → 0.1.27.dev3__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 CHANGED
@@ -35,4 +35,5 @@ from edsl.data.CacheEntry import CacheEntry
35
35
  from edsl.data.CacheHandler import set_session_cache, unset_session_cache
36
36
  from edsl.shared import shared_globals
37
37
  from edsl.jobs import Jobs
38
+ from edsl.notebooks import Notebook
38
39
  from edsl.coop.coop import Coop
edsl/__version__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.1.27.dev1"
1
+ __version__ = "0.1.27.dev3"
edsl/coop/utils.py CHANGED
@@ -1,4 +1,5 @@
1
1
  from edsl import Agent, AgentList, Cache, Jobs, Results, Scenario, ScenarioList, Survey
2
+ from edsl.notebooks import Notebook
2
3
  from edsl.questions import QuestionBase
3
4
  from typing import Literal, Type, Union
4
5
 
@@ -7,6 +8,7 @@ EDSLObject = Union[
7
8
  AgentList,
8
9
  Cache,
9
10
  Jobs,
11
+ Notebook,
10
12
  Type[QuestionBase],
11
13
  Results,
12
14
  Scenario,
@@ -20,6 +22,7 @@ ObjectType = Literal[
20
22
  "cache",
21
23
  "job",
22
24
  "question",
25
+ "notebook",
23
26
  "results",
24
27
  "scenario",
25
28
  "scenario_list",
@@ -31,6 +34,7 @@ ObjectPage = Literal[
31
34
  "agentlists",
32
35
  "caches",
33
36
  "jobs",
37
+ "notebooks",
34
38
  "questions",
35
39
  "results",
36
40
  "scenarios",
@@ -76,6 +80,11 @@ class ObjectRegistry:
76
80
  "edsl_class": QuestionBase,
77
81
  "object_page": "questions",
78
82
  },
83
+ {
84
+ "object_type": "notebook",
85
+ "edsl_class": Notebook,
86
+ "object_page": "notebooks",
87
+ },
79
88
  {
80
89
  "object_type": "results",
81
90
  "edsl_class": Results,
@@ -0,0 +1,167 @@
1
+ """A Notebook is ...."""
2
+
3
+ import json
4
+ import nbformat
5
+
6
+ from typing import Dict, List, Optional
7
+ from rich.table import Table
8
+ from edsl.Base import Base
9
+ from edsl.utilities.decorators import (
10
+ add_edsl_version,
11
+ remove_edsl_version,
12
+ )
13
+
14
+
15
+ class Notebook(Base):
16
+ """
17
+ A Notebook is a utility class that allows you to easily share/pull ipynbs from Coop.
18
+ """
19
+
20
+ default_name = "notebook"
21
+
22
+ def __init__(
23
+ self,
24
+ data: Optional[Dict] = None,
25
+ path: Optional[str] = None,
26
+ name: Optional[str] = None,
27
+ ):
28
+ """
29
+ Initialize a new Notebook.
30
+ - if a path is provided, try to load the notebook from that path.
31
+ - if no path is provided, assume this code is run in a notebook and try to load the current notebook.
32
+ """
33
+ if data is not None:
34
+ nbformat.validate(data)
35
+ self.data = data
36
+ elif path is not None:
37
+ # TO BE IMPLEMENTED
38
+ # store in this var the data from the notebook
39
+ with open(path, mode="r", encoding="utf-8") as f:
40
+ data = nbformat.read(f, as_version=4)
41
+ self.data = json.loads(json.dumps(data))
42
+ else:
43
+ # TO BE IMPLEMENTED
44
+ # 1. Check you're in a notebook ...
45
+ # 2. get its info and store it in self.data
46
+ # RI: Working on this
47
+ self.data = {"some": "data"}
48
+
49
+ # deprioritize - perhaps add sanity check function
50
+ # 1. could check if the notebook is a valid notebook
51
+ # 2. could check notebook uses EDSL
52
+ # ....
53
+
54
+ self.name = name or self.default_name
55
+
56
+ def __eq__(self, other):
57
+ """
58
+ Check if two Notebooks are equal.
59
+ This should maybe only check the cells and not the metadata/nbformat?
60
+ """
61
+ return self.data == other.data
62
+
63
+ @add_edsl_version
64
+ def to_dict(self) -> dict:
65
+ """
66
+ Convert to a dictionary.
67
+ AF: here you will create a dict from which self.from_dict can recreate the object.
68
+ AF: the decorator will add the edsl_version to the dict.
69
+ """
70
+ return {"name": self.name, "data": self.data}
71
+
72
+ @classmethod
73
+ @remove_edsl_version
74
+ def from_dict(cls, d: Dict) -> "Notebook":
75
+ """
76
+ Convert a dictionary representation of a Notebook to a Notebook object.
77
+ """
78
+ return cls(data=d["data"], name=d["name"])
79
+
80
+ def to_file(self, path: str):
81
+ """
82
+ Saves the notebook at the specified filepath.
83
+ RI: Maybe you want to download a notebook to your local machine
84
+ to work with it?
85
+ """
86
+ nbformat.write(nbformat.from_dict(self.data), fp=path)
87
+
88
+ def print(self):
89
+ """
90
+ Print the notebook.
91
+ AF: not sure how this should behave for a notebook
92
+ """
93
+ from rich import print_json
94
+ import json
95
+
96
+ print_json(json.dumps(self.to_dict()))
97
+
98
+ def __repr__(self):
99
+ """
100
+ AF: not sure how this should behave for a notebook
101
+ """
102
+ return f"Notebook({self.to_dict()})"
103
+
104
+ def _repr_html_(self):
105
+ """
106
+ AF: not sure how this should behave for a notebook
107
+ """
108
+ from edsl.utilities.utilities import data_to_html
109
+
110
+ return data_to_html(self.to_dict())
111
+
112
+ def rich_print(self) -> "Table":
113
+ """
114
+ AF: not sure how we should implement this for a notebook
115
+ """
116
+ pass
117
+
118
+ @classmethod
119
+ def example(cls) -> "Notebook":
120
+ """
121
+ Return an example Notebook.
122
+ AF: add a simple custom example here
123
+ """
124
+ cells = [
125
+ {
126
+ "cell_type": "markdown",
127
+ "metadata": dict(),
128
+ "source": "# Test notebook",
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "execution_count": 1,
133
+ "metadata": dict(),
134
+ "outputs": [
135
+ {
136
+ "name": "stdout",
137
+ "output_type": "stream",
138
+ "text": "Hello world!\n",
139
+ }
140
+ ],
141
+ "source": 'print("Hello world!")',
142
+ },
143
+ ]
144
+ data = {
145
+ "metadata": dict(),
146
+ "nbformat": 4,
147
+ "nbformat_minor": 4,
148
+ "cells": cells,
149
+ }
150
+ return cls(data=data)
151
+
152
+ def code(self) -> List[str]:
153
+ """
154
+ Return the code that could be used to create this Notebook.
155
+ AF: Again, not sure
156
+ """
157
+ lines = []
158
+ lines.append("from edsl import Notebook")
159
+ lines.append(f"nb = Notebook(data={self.data}, name='{self.name}')")
160
+ return lines
161
+
162
+
163
+ if __name__ == "__main__":
164
+ from edsl.notebooks import Notebook
165
+
166
+ notebook = Notebook.example()
167
+ assert notebook == notebook.from_dict(notebook.to_dict())
@@ -0,0 +1 @@
1
+ from edsl.notebooks.Notebook import Notebook
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: edsl
3
- Version: 0.1.27.dev1
3
+ Version: 0.1.27.dev3
4
4
  Summary: Create and analyze LLM-based surveys
5
5
  Home-page: https://www.expectedparrot.com/
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  edsl/Base.py,sha256=IlWEjQTnKbSPm9xqUqE7D6izBDnzARbrV_NHd7YJGeY,6601
2
- edsl/__init__.py,sha256=7UsuBUVcdmaz6je-0D1HvPG6X3ATtXud_hYpUrkU6H0,1182
3
- edsl/__version__.py,sha256=W5px3D8b9BN2pOYswec-dtjDZ-DG95Aqtxe0hW3KSz0,28
2
+ edsl/__init__.py,sha256=b1rnUg4e_MA87LDuDeAZkS7h-DHdtB8h3UYQCgzdSgQ,1218
3
+ edsl/__version__.py,sha256=GX6Byw31fnUBfUiG4GIYMexAOcvuVVuIA9WDTEX5_34,28
4
4
  edsl/agents/Agent.py,sha256=sPtYxD3ZJ3CqpsPV0nP0MaaqItqRTT787mcwtmxOrzg,25536
5
5
  edsl/agents/AgentList.py,sha256=CceqVJ-4sMJgOZ9YFODOaW-1hIthbMeNgi1NIbiiLuQ,7045
6
6
  edsl/agents/Invigilator.py,sha256=slNz5LTRAB53_DDhgR1WxU-6kBHivJZ6iNloGlLRSGs,10223
@@ -17,7 +17,7 @@ edsl/conjure/__init__.py,sha256=3IyzMEQi9HFuB9JARG5MNlvoka6v06lBTah5l0EhYos,234
17
17
  edsl/conjure/utilities.py,sha256=rcKC5MabVqrTWTZ9nxHuRtopcyw6o_0fiNrNT7gbDxY,1712
18
18
  edsl/coop/__init__.py,sha256=4iZCwJSzJVyjBYk8ggGxY2kZjq9dXVT1jhyPDNyew4I,115
19
19
  edsl/coop/coop.py,sha256=3JvGNDYicfb8P7Q6AX2kmCZuTUNlmWXfTUOaDCZXWHs,17781
20
- edsl/coop/utils.py,sha256=mXKx9b3L6i8FHBmnK-P6KjAmDXtzyw3FRwQ0filIOxM,3361
20
+ edsl/coop/utils.py,sha256=9hLtHv04h5zsfM10Q83zAXOKb0KeYefSTjmcr1zXvFc,3580
21
21
  edsl/data/Cache.py,sha256=Ux0Qu__ScFe_rgYW_zRV3fs9oaa4jvXZ4gpyFELoK4I,13610
22
22
  edsl/data/CacheEntry.py,sha256=AnZBUautQc19KhE6NkI87U_P9wDZI2eu-8B1XopPTOI,7235
23
23
  edsl/data/CacheHandler.py,sha256=rPWhQ-kzVq6WoSdst4c30wzQkNrwr9xuVapiLn360Ck,4776
@@ -81,6 +81,8 @@ edsl/language_models/__init__.py,sha256=bvY7Gy6VkX1gSbNkRbGPS-M1kUnb0EohL0FSagaE
81
81
  edsl/language_models/registry.py,sha256=vUJIS89lJXg5OIlz7dq32SEOL7-OI6pRbTGE-2QRicM,2758
82
82
  edsl/language_models/repair.py,sha256=YJYo_PXrbqqOLB_lWYL4RQ0X2nYozl9IycYUi6rueLU,3336
83
83
  edsl/language_models/unused/ReplicateBase.py,sha256=J1oqf7mEyyKhRwNUomnptVqAsVFYCbS3iTW0EXpKtXo,3331
84
+ edsl/notebooks/Notebook.py,sha256=TkxEDPu0nvCWLfiWxOEmZP_P10SSovRyQWFwHvtDXuk,4851
85
+ edsl/notebooks/__init__.py,sha256=VNUA3nNq04slWNbYaNrzOhQJu3AZANpvBniyCJSzJ7U,45
84
86
  edsl/prompts/Prompt.py,sha256=LIfM5RwnhyJFOAmVdd5GlgMtJNWcMjG0TszMTJfidFc,9554
85
87
  edsl/prompts/QuestionInstructionsBase.py,sha256=xico6ob4cqWsHl-txj2RbY_4Ny5u9UqvIjmoTVgjUhk,348
86
88
  edsl/prompts/__init__.py,sha256=Z0ywyJfnV0i-sR1SCdK4mHhgECT5cXpHVA-pKuBTifc,85
@@ -164,7 +166,7 @@ edsl/utilities/decorators.py,sha256=rlTSMItwmWUxHQBIEUDxX3lFzgtiT8PnfNavakuf-2M,
164
166
  edsl/utilities/interface.py,sha256=GxEDMxmpnyxcy71yzdDNqdoh7GLrDEbTD0y9M81ZkZk,19084
165
167
  edsl/utilities/restricted_python.py,sha256=5-_zUhrNbos7pLhDl9nr8d24auRlquR6w-vKkmNjPiA,2060
166
168
  edsl/utilities/utilities.py,sha256=YLzbA3kBFisy8fWpodrwi-CsKlMhbUwZ89RYyCNRq4Q,8708
167
- edsl-0.1.27.dev1.dist-info/LICENSE,sha256=_qszBDs8KHShVYcYzdMz3HNMtH-fKN_p5zjoVAVumFc,1111
168
- edsl-0.1.27.dev1.dist-info/METADATA,sha256=guo5KBMnHbGrV5f105chU5YSR7trDyKGijyYrYcqTGc,3128
169
- edsl-0.1.27.dev1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
170
- edsl-0.1.27.dev1.dist-info/RECORD,,
169
+ edsl-0.1.27.dev3.dist-info/LICENSE,sha256=_qszBDs8KHShVYcYzdMz3HNMtH-fKN_p5zjoVAVumFc,1111
170
+ edsl-0.1.27.dev3.dist-info/METADATA,sha256=EW5J1nGaPZzzXUqB-iF9-zQoLEB5_23vhUtjnptld58,3128
171
+ edsl-0.1.27.dev3.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
172
+ edsl-0.1.27.dev3.dist-info/RECORD,,