edsl 0.1.27.dev2__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.dev2"
1
+ __version__ = "0.1.27.dev3"
@@ -1,5 +1,8 @@
1
1
  """A Notebook is ...."""
2
2
 
3
+ import json
4
+ import nbformat
5
+
3
6
  from typing import Dict, List, Optional
4
7
  from rich.table import Table
5
8
  from edsl.Base import Base
@@ -14,22 +17,33 @@ class Notebook(Base):
14
17
  A Notebook is a utility class that allows you to easily share/pull ipynbs from Coop.
15
18
  """
16
19
 
17
- def __init__(self, data: Optional[Dict] = None, path: Optional[str] = None):
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
+ ):
18
28
  """
19
29
  Initialize a new Notebook.
20
30
  - if a path is provided, try to load the notebook from that path.
21
31
  - if no path is provided, assume this code is run in a notebook and try to load the current notebook.
22
32
  """
23
33
  if data is not None:
34
+ nbformat.validate(data)
24
35
  self.data = data
25
36
  elif path is not None:
26
37
  # TO BE IMPLEMENTED
27
38
  # store in this var the data from the notebook
28
- self.data = {"some": "data"}
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))
29
42
  else:
30
43
  # TO BE IMPLEMENTED
31
44
  # 1. Check you're in a notebook ...
32
45
  # 2. get its info and store it in self.data
46
+ # RI: Working on this
33
47
  self.data = {"some": "data"}
34
48
 
35
49
  # deprioritize - perhaps add sanity check function
@@ -37,9 +51,12 @@ class Notebook(Base):
37
51
  # 2. could check notebook uses EDSL
38
52
  # ....
39
53
 
54
+ self.name = name or self.default_name
55
+
40
56
  def __eq__(self, other):
41
57
  """
42
58
  Check if two Notebooks are equal.
59
+ This should maybe only check the cells and not the metadata/nbformat?
43
60
  """
44
61
  return self.data == other.data
45
62
 
@@ -50,7 +67,7 @@ class Notebook(Base):
50
67
  AF: here you will create a dict from which self.from_dict can recreate the object.
51
68
  AF: the decorator will add the edsl_version to the dict.
52
69
  """
53
- return {"data": self.data}
70
+ return {"name": self.name, "data": self.data}
54
71
 
55
72
  @classmethod
56
73
  @remove_edsl_version
@@ -58,7 +75,15 @@ class Notebook(Base):
58
75
  """
59
76
  Convert a dictionary representation of a Notebook to a Notebook object.
60
77
  """
61
- return cls(data=d["data"])
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)
62
87
 
63
88
  def print(self):
64
89
  """
@@ -96,7 +121,33 @@ class Notebook(Base):
96
121
  Return an example Notebook.
97
122
  AF: add a simple custom example here
98
123
  """
99
- return cls(data={"some": "data"})
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)
100
151
 
101
152
  def code(self) -> List[str]:
102
153
  """
@@ -104,8 +155,8 @@ class Notebook(Base):
104
155
  AF: Again, not sure
105
156
  """
106
157
  lines = []
107
- lines.append("from edsl.notebooks import Notebook")
108
- lines.append(f"s = Notebook({self.data})")
158
+ lines.append("from edsl import Notebook")
159
+ lines.append(f"nb = Notebook(data={self.data}, name='{self.name}')")
109
160
  return lines
110
161
 
111
162
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: edsl
3
- Version: 0.1.27.dev2
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=T8ew6f9SFqYwIN50vSD5-4mUOKle1OES4cRWdaZmtng,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
@@ -81,7 +81,7 @@ 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=m9ym0PYff_NrW3MHEWGLPelEfMU2TSA9gTKlnsCU4hI,3339
84
+ edsl/notebooks/Notebook.py,sha256=TkxEDPu0nvCWLfiWxOEmZP_P10SSovRyQWFwHvtDXuk,4851
85
85
  edsl/notebooks/__init__.py,sha256=VNUA3nNq04slWNbYaNrzOhQJu3AZANpvBniyCJSzJ7U,45
86
86
  edsl/prompts/Prompt.py,sha256=LIfM5RwnhyJFOAmVdd5GlgMtJNWcMjG0TszMTJfidFc,9554
87
87
  edsl/prompts/QuestionInstructionsBase.py,sha256=xico6ob4cqWsHl-txj2RbY_4Ny5u9UqvIjmoTVgjUhk,348
@@ -166,7 +166,7 @@ edsl/utilities/decorators.py,sha256=rlTSMItwmWUxHQBIEUDxX3lFzgtiT8PnfNavakuf-2M,
166
166
  edsl/utilities/interface.py,sha256=GxEDMxmpnyxcy71yzdDNqdoh7GLrDEbTD0y9M81ZkZk,19084
167
167
  edsl/utilities/restricted_python.py,sha256=5-_zUhrNbos7pLhDl9nr8d24auRlquR6w-vKkmNjPiA,2060
168
168
  edsl/utilities/utilities.py,sha256=YLzbA3kBFisy8fWpodrwi-CsKlMhbUwZ89RYyCNRq4Q,8708
169
- edsl-0.1.27.dev2.dist-info/LICENSE,sha256=_qszBDs8KHShVYcYzdMz3HNMtH-fKN_p5zjoVAVumFc,1111
170
- edsl-0.1.27.dev2.dist-info/METADATA,sha256=sQ3Mop8YapjdD9dMvfvwpMOc6PmlC8QkwrMNlY7ObEc,3128
171
- edsl-0.1.27.dev2.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
172
- edsl-0.1.27.dev2.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,,