nosible 0.2.4__py3-none-any.whl → 0.2.6__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.
@@ -16,7 +16,7 @@ class SearchSet(Iterator[Search]):
16
16
 
17
17
  Parameters
18
18
  ----------
19
- searches : list of Search
19
+ searches_list : list of Search
20
20
  The list of Search objects in the collection.
21
21
 
22
22
  Examples
@@ -28,13 +28,13 @@ class SearchSet(Iterator[Search]):
28
28
  0: What is Python?
29
29
  1: What is PEP8?
30
30
  >>> searches.add(Search(question="What is AI?", n_results=1))
31
- >>> searches.to_json("searches.json")
32
- >>> loaded = SearchSet.from_json("searches.json")
31
+ >>> searches.write_json("searches.json")
32
+ >>> loaded = SearchSet.read_json("searches.json")
33
33
  >>> print(loaded[2].question)
34
34
  What is AI?
35
35
  """
36
36
 
37
- searches: list[Search] = field(default_factory=list)
37
+ searches_list: list[Search] = field(default_factory=list)
38
38
  """ A list of Search objects in the collection."""
39
39
  _index: int = field(default=0, init=False, repr=False, compare=False)
40
40
  """ Internal index for iteration over searches."""
@@ -65,8 +65,8 @@ class SearchSet(Iterator[Search]):
65
65
  Search
66
66
  The next Search instance in the collection.
67
67
  """
68
- if self._index < len(self.searches):
69
- search = self.searches[self._index]
68
+ if self._index < len(self.searches_list):
69
+ search = self.searches_list[self._index]
70
70
  self._index += 1
71
71
  return search
72
72
  raise StopIteration
@@ -80,7 +80,7 @@ class SearchSet(Iterator[Search]):
80
80
  str
81
81
  A string representation of the SearchSet, showing each search's question with its index.
82
82
  """
83
- return "\n".join(f"{i}: {s.question}" for i, s in enumerate(self.searches))
83
+ return "\n".join(f"{i}: {s.question}" for i, s in enumerate(self.searches_list))
84
84
 
85
85
  def __getitem__(self, index: int) -> Search:
86
86
  """
@@ -101,9 +101,9 @@ class SearchSet(Iterator[Search]):
101
101
  IndexError
102
102
  If index is out of range.
103
103
  """
104
- if 0 <= index < len(self.searches):
105
- return self.searches[index]
106
- raise IndexError(f"Index {index} out of range for searches collection of size {len(self.searches)}")
104
+ if 0 <= index < len(self.searches_list):
105
+ return self.searches_list[index]
106
+ raise IndexError(f"Index {index} out of range for searches collection of size {len(self.searches_list)}")
107
107
 
108
108
  def __len__(self) -> int:
109
109
  """
@@ -112,7 +112,7 @@ class SearchSet(Iterator[Search]):
112
112
  Returns:
113
113
  int: The number of Search instances in the collection.
114
114
  """
115
- return len(self.searches)
115
+ return len(self.searches_list)
116
116
 
117
117
  def __add__(self, other: "SearchSet") -> "SearchSet":
118
118
  """
@@ -135,7 +135,7 @@ class SearchSet(Iterator[Search]):
135
135
  """
136
136
  if not isinstance(other, SearchSet):
137
137
  raise TypeError("Can only add another SearchSet instance")
138
- return SearchSet(self.searches + other.searches)
138
+ return SearchSet(self.searches_list + other.searches_list)
139
139
 
140
140
  def __setitem__(self, index: int, value: Search) -> None:
141
141
  """
@@ -153,10 +153,10 @@ class SearchSet(Iterator[Search]):
153
153
  IndexError
154
154
  If index is out of range.
155
155
  """
156
- if 0 <= index < len(self.searches):
157
- self.searches[index] = value
156
+ if 0 <= index < len(self.searches_list):
157
+ self.searches_list[index] = value
158
158
  else:
159
- raise IndexError(f"Index {index} out of range for searches collection of size {len(self.searches)}")
159
+ raise IndexError(f"Index {index} out of range for searches collection of size {len(self.searches_)}")
160
160
 
161
161
  def add(self, search: Search) -> None:
162
162
  """
@@ -177,7 +177,7 @@ class SearchSet(Iterator[Search]):
177
177
  >>> print(searches[0].question)
178
178
  What is Python?
179
179
  """
180
- self.searches.append(search)
180
+ self.searches_list.append(search)
181
181
 
182
182
  def remove(self, index: int) -> None:
183
183
  """
@@ -200,7 +200,7 @@ class SearchSet(Iterator[Search]):
200
200
  >>> [s.question for s in searches.searches]
201
201
  ['First', 'Third']
202
202
  """
203
- del self.searches[index]
203
+ del self.searches_list[index]
204
204
 
205
205
  def to_dicts(self) -> list[dict]:
206
206
  """
@@ -225,9 +225,9 @@ class SearchSet(Iterator[Search]):
225
225
  >>> searches.to_dicts()[1]["question"]
226
226
  'What is PEP8?'
227
227
  """
228
- return [s.to_dict() for s in self.searches]
228
+ return [s.to_dict() for s in self.searches_list]
229
229
 
230
- def to_json(self, path: str = None) -> str:
230
+ def write_json(self, path: str = None) -> str:
231
231
  """
232
232
  Convert the entire SearchSet collection to a JSON string or save to a file.
233
233
 
@@ -255,10 +255,10 @@ class SearchSet(Iterator[Search]):
255
255
  >>> s1 = Search(question="What is Python?", n_results=3)
256
256
  >>> s2 = Search(question="What is PEP8?", n_results=2)
257
257
  >>> searches = SearchSet([s1, s2])
258
- >>> json_str = searches.to_json()
258
+ >>> json_str = searches.write_json()
259
259
  >>> isinstance(json_str, str)
260
260
  True
261
- >>> searches.to_json(
261
+ >>> searches.write_json(
262
262
  ... "searches.json"
263
263
  ... ) # The file 'searches.json' will contain both search queries in JSON format.
264
264
  """
@@ -276,7 +276,7 @@ class SearchSet(Iterator[Search]):
276
276
  raise RuntimeError(f"Failed to serialize results to JSON: {e}") from e
277
277
 
278
278
  @classmethod
279
- def from_json(cls, path: str) -> "SearchSet":
279
+ def read_json(cls, path: str) -> "SearchSet":
280
280
  """
281
281
  Load a SearchSet collection from a JSON file.
282
282
 
@@ -302,13 +302,13 @@ class SearchSet(Iterator[Search]):
302
302
  >>> s1 = Search(question="Python basics", n_results=2)
303
303
  >>> s2 = Search(question="PEP8 guidelines", n_results=1)
304
304
  >>> searches = SearchSet([s1, s2])
305
- >>> searches.to_json("searches.json")
306
- >>> loaded_searches = SearchSet.from_json("searches.json")
305
+ >>> searches.write_json("searches.json")
306
+ >>> loaded_searches = SearchSet.read_json("searches.json")
307
307
  >>> print([s.question for s in loaded_searches])
308
308
  ['Python basics', 'PEP8 guidelines']
309
309
  """
310
310
  with open(path) as f:
311
311
  raw = f.read()
312
312
  data_list = json_loads(raw)
313
- searches = [Search(**item) for item in data_list]
314
- return cls(searches)
313
+ searches_list= [Search(**item) for item in data_list]
314
+ return cls(searches_list)
@@ -141,7 +141,7 @@ class Snippet:
141
141
  """
142
142
  return cls(**data)
143
143
 
144
- def to_json(self) -> str:
144
+ def write_json(self) -> str:
145
145
  """
146
146
  Convert the Snippet to a JSON string representation.
147
147
 
@@ -153,7 +153,7 @@ class Snippet:
153
153
  Examples
154
154
  --------
155
155
  >>> snippet = Snippet(content="Example snippet", snippet_hash="hash1")
156
- >>> json_str = snippet.to_json()
156
+ >>> json_str = snippet.write_json()
157
157
  >>> isinstance(json_str, str)
158
158
  True
159
159
  """
@@ -131,7 +131,7 @@ class SnippetSet(Iterator[Snippet]):
131
131
  """
132
132
  return {s.snippet_hash: s.to_dict() for s in self.snippets} if self.snippets else {}
133
133
 
134
- def to_json(self) -> str:
134
+ def write_json(self) -> str:
135
135
  """
136
136
  Convert the SnippetSet to a JSON string representation.
137
137
 
@@ -144,7 +144,7 @@ class SnippetSet(Iterator[Snippet]):
144
144
  --------
145
145
  >>> snippets_data = {"hash1": {"content": "Example snippet", "snippet_hash": "hash1"}}
146
146
  >>> snippets = SnippetSet().from_dict(snippets_data)
147
- >>> json_str = snippets.to_json()
147
+ >>> json_str = snippets.write_json()
148
148
  >>> isinstance(json_str, str)
149
149
  True
150
150
  """
@@ -1,4 +1,5 @@
1
1
  from dataclasses import asdict, dataclass, field
2
+ from pathlib import Path
2
3
 
3
4
  from nosible.classes.snippet_set import SnippetSet
4
5
  from nosible.utils.json_tools import json_dumps, json_loads
@@ -97,27 +98,9 @@ class WebPageData:
97
98
  data["snippets"] = self.snippets.to_dict()
98
99
  return data
99
100
 
100
- def to_json(self) -> str:
101
+ def write_json(self, path: str = None) -> str:
101
102
  """
102
- Convert the WebPageData to a JSON string representation.
103
-
104
- Returns
105
- -------
106
- str
107
- JSON string containing all fields of the WebPageData.
108
-
109
- Examples
110
- --------
111
- >>> data = WebPageData(languages={"en": 1}, metadata={"description": "Example"})
112
- >>> json_str = data.to_json()
113
- >>> isinstance(json_str, str)
114
- True
115
- """
116
- return json_dumps(self.to_dict())
117
-
118
- def save(self, path: str) -> None:
119
- """
120
- Save the WebPageData to a JSON file.
103
+ Save the WebPageData to a JSON file and optionally return the json.
121
104
 
122
105
  Parameters
123
106
  ----------
@@ -127,7 +110,7 @@ class WebPageData:
127
110
  Examples
128
111
  --------
129
112
  >>> data = WebPageData(languages={"en": 1}, metadata={"description": "Example"})
130
- >>> data.save("test_webpage.json")
113
+ >>> data.write_json("test_webpage.json")
131
114
  >>> with open("test_webpage.json", "r", encoding="utf-8") as f:
132
115
  ... content = f.read()
133
116
  >>> import json
@@ -137,42 +120,14 @@ class WebPageData:
137
120
  >>> d["metadata"]
138
121
  {'description': 'Example'}
139
122
  """
140
- with open(path, "w", encoding="utf-8") as f:
141
- f.write(self.to_json())
142
-
143
- @classmethod
144
- def from_json(cls, data: str) -> "WebPageData":
145
- """
146
- Create a WebPageData instance from a JSON string.
123
+ if path is not None:
124
+ with open(path, "w", encoding="utf-8") as f:
125
+ f.write(json_dumps(self.to_dict()))
147
126
 
148
- Parameters
149
- ----------
150
- data : str
151
- JSON string containing fields to initialize the WebPageData.
152
-
153
- Returns
154
- -------
155
- WebPageData
156
- An instance of WebPageData initialized with the provided JSON data.
157
-
158
- Examples
159
- --------
160
- >>> json_str = '{"languages": {"en": 1}, "metadata": {"description": "Example"}}'
161
- >>> webpage_data = WebPageData.from_json(json_str)
162
- >>> isinstance(webpage_data, WebPageData)
163
- True
164
- >>> webpage_data.languages
165
- {'en': 1}
166
- """
167
- data_dict = json_loads(data)
168
- # Handle snippets separately to avoid passing it twice
169
- snippets_data = data_dict.pop("snippets", None)
170
- if snippets_data is not None:
171
- data_dict["snippets"] = SnippetSet.from_dict(snippets_data)
172
- return cls(**data_dict)
127
+ return json_dumps(self.to_dict())
173
128
 
174
129
  @classmethod
175
- def load(cls, path: str) -> "WebPageData":
130
+ def read_json(cls, path: Path) -> "WebPageData":
176
131
  """
177
132
  Create a WebPageData instance from a JSON file.
178
133
 
@@ -189,8 +144,8 @@ class WebPageData:
189
144
  Examples
190
145
  --------
191
146
  >>> data = WebPageData(languages={"en": 1}, metadata={"description": "Example"})
192
- >>> data.save("test_webpage.json")
193
- >>> loaded = WebPageData.load("test_webpage.json")
147
+ >>> data.write_json("test_webpage.json")
148
+ >>> loaded = WebPageData.read_json(Path("test_webpage.json"))
194
149
  >>> isinstance(loaded, WebPageData)
195
150
  True
196
151
  >>> loaded.languages