Skip to content

Commit 2818676

Browse files
committed
Updated collection iter.
1 parent c393d65 commit 2818676

File tree

6 files changed

+171
-110
lines changed

6 files changed

+171
-110
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ __pycache__/
44
.vscode/
55
dummy_*
66
__test_db__/
7-
_test_engine_/
7+
_test_engine_/
8+
.pytest_cache/

README.md

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ Let's see how you can get started with the package.
1919
Step #1 is to create a model that represents something like a row in a table.
2020

2121
```py
22-
from sjd.entity.properties import IntProperty, StrProperty
23-
from sjd.entity import TEntity
22+
from sjd import TEntity, properties as props
2423

2524
class Student(TEntity):
26-
student_id = IntProperty(required=True)
27-
first_name = StrProperty(required=True)
28-
last_name = StrProperty(required=True)
25+
student_id = props.IntProperty(required=True)
26+
first_name = props.StrProperty(required=True)
27+
last_name = props.StrProperty(required=True)
2928
```
3029

3130
1. Your model should inherit from `TEntity`.
@@ -36,11 +35,11 @@ class Student(TEntity):
3635
We have an `Engine` and some `Collection`s. The first one is your database, the second is your table.
3736

3837
```py
39-
from sjd.database import Engine, __Collection__
38+
from sjd import Engine
4039

4140
class AppEngine(Engine):
4241

43-
students = __Collection__(Student)
42+
students = Engine.set(Student)
4443

4544
def __init__(self):
4645
super().__init__(Path("__test_db__"))
@@ -50,7 +49,7 @@ class AppEngine(Engine):
5049
1. The engine will create a directory named `__test_db__`.
5150
2. inside `__test_db__` the collections are stored.
5251
3. You **SHOULD** pass the type of your entity (`entity_type`) to the `__Collection__`. Here is `Student`.
53-
4. type `__Collection__` is a descriptor! The actual thing is `Collection`.
52+
4. `Engine.set()` returns type `__Collection__` which is a descriptor! The actual thing is `Collection`.
5453
5. The collection name will be class variable's name ( `students` here ).
5554

5655
### Create engine instance
@@ -94,9 +93,9 @@ Personally, i prefer classes with initializers. so let's add an `__init__` to th
9493
class Student(TEntity):
9594
__json_init__ = True
9695

97-
student_id = IntProperty(required=True)
98-
first_name = StrProperty(required=True)
99-
last_name = StrProperty()
96+
student_id = props.IntProperty(required=True)
97+
first_name = props.StrProperty(required=True)
98+
last_name = props.StrProperty()
10099

101100
def __init__(self, student_id: int, first_name: str, last_name: str):
102101
self.student_id = student_id
@@ -138,8 +137,8 @@ async for student in collection:
138137
Get data with some filters.
139138

140139
```py
141-
async for student in collection.as_queryable.where(
142-
lambda s: s.last_name == "Doe"
140+
async for student in engine.students.iter_by_prop_value(
141+
lambda s: s.last_name, "Doe"
143142
):
144143
print(student.first_name)
145144

@@ -152,8 +151,8 @@ async for student in collection.as_queryable.where(
152151
Ops they were all "Doe".
153152

154153
```py
155-
async for student in collection.as_queryable.where(
156-
lambda s: s.first_name == "John"
154+
async for student in engine.students.iter_by_prop_value(
155+
lambda s: s.first_name, "John"
157156
):
158157
print(student.first_name)
159158

@@ -164,7 +163,7 @@ If it's going to be one, we have more options.
164163

165164
```py
166165

167-
student = await collection.as_queryable.first(lambda s: s.first_name == "John")
166+
student = await collection.get_first(lambda s: s.first_name == "John")
168167
print(student.first_name)
169168

170169
# John
@@ -204,16 +203,16 @@ Let's begin with creating another model called `Grade` that includes some inform
204203
Since this model is an embed entity, You should inherit from `EmbedEntity`.
205204

206205
```py
207-
from src.entity import TEntity, EmbedEntity
206+
from sjd import TEntity, EmbedEntity, properties as props
208207

209208
# ---- sniff ----
210209

211210
class Grade(EmbedEntity):
212211
__json_init__ = True
213212

214-
course_id = IntProperty(required=True)
215-
course_name = StrProperty(required=True)
216-
score = IntProperty(required=True)
213+
course_id = props.IntProperty(required=True)
214+
course_name = props.StrProperty(required=True)
215+
score = props.IntProperty(required=True)
217216

218217
def __init__(self, course_id: int, course_name: str, score: int):
219218
self.course_id = course_id
@@ -226,17 +225,16 @@ To add this as a new property to the `Student`, we'll use `ComplexProperty`. ( O
226225
Your `Student` class should looks like this:
227226

228227
```py
229-
from src.entity.properties import IntProperty, StrProperty, OptionalComplexProperty
230228

231229
# ---- sniff ----
232230

233231
class Student(TEntity):
234232
__json_init__ = True
235233

236-
student_id = IntProperty(required=True)
237-
first_name = StrProperty(required=True)
238-
last_name = StrProperty()
239-
grade = OptionalComplexProperty(Grade)
234+
student_id = props.IntProperty(required=True)
235+
first_name = props.StrProperty(required=True)
236+
last_name = props.StrProperty()
237+
grade = props.OptionalComplexProperty(Grade)
240238

241239
def __init__(
242240
self,
@@ -264,7 +262,7 @@ async for student in collection:
264262
Let's check if it's working
265263

266264
```py
267-
jill = await collection.as_queryable.first(lambda s: s.first_name == "Jill")
265+
jill = await collection.get_first(lambda s: s.first_name == "Jill")
268266
if jill.grade:
269267
print(jill.grade.course_name, jill.grade.score)
270268

@@ -276,17 +274,16 @@ jill = await collection.as_queryable.first(lambda s: s.first_name == "Jill")
276274
As we all know, there may be more than one course per student! Then the grade property, can be grades.
277275

278276
```py
279-
from src.entity.properties import IntProperty, StrProperty, ListProperty
280277

281278
# ---- sniff ----
282279

283280
class Student(TEntity):
284281
__json_init__ = True
285282

286-
student_id = IntProperty(required=True)
287-
first_name = StrProperty(required=True)
288-
last_name = StrProperty()
289-
grades = ListProperty(Grade, default_factory=list)
283+
student_id = props.IntProperty(required=True)
284+
first_name = props.StrProperty(required=True)
285+
last_name = props.StrProperty()
286+
grades = props.ListProperty(Grade, default_factory=list)
290287

291288
def __init__(
292289
self,
@@ -315,7 +312,7 @@ async for student in collection:
315312
Let's change Jill's english score to 50.
316313

317314
```py
318-
jill = await collection.as_queryable.first(lambda s: s.first_name == "Jill")
315+
jill = await collection.get_first(lambda s: s.first_name == "Jill")
319316
if jill.grades:
320317
jill.grades[-1].score = 50
321318
await collection.update(jill)
@@ -324,9 +321,11 @@ if jill.grades:
324321
Oh who's score was 50 ??!
325322

326323
```py
327-
with_50_score = await collection.as_queryable.where(
328-
lambda s: any(x.score == 50 for x in s.grades)).single()
329-
print(with_50_score.first_name)
324+
async with collection as iter_ctx:
325+
326+
with_50_score = await iter_ctx.as_queryable.where(
327+
lambda s: any(x.score == 50 for x in s.grades)).single()
328+
print(with_50_score.first_name)
330329
```
331330

332331
### Getting data ( Fast way )
@@ -342,6 +341,10 @@ There are two methods that are probably faster than `as_queryable` way.
342341
Use this to do an `async iterate` over all entities with `entity[__prop] == __value`.
343342
We use this to work with virtual objects.
344343

344+
3. `get_first(__prop: str, __value: Any)`
345+
346+
Just like 2, but returns the first.
347+
345348
```py
346349
async for student in engine.students.iter_by_prop_value("first_name", "Jill"):
347350
print(student.last_name)
@@ -371,16 +374,14 @@ Since the `Grade` is going to be a separate entity, we should add it to our `App
371374
```py
372375
# ---- sniff ----
373376

374-
from sjd.entity.properties import VirtualListProperty
375-
376377
class Student(TEntity):
377378
__json_init__ = True
378379

379-
student_id = IntProperty(required=True)
380-
first_name = StrProperty(required=True)
381-
last_name = StrProperty()
380+
student_id = props.IntProperty(required=True)
381+
first_name = props.StrProperty(required=True)
382+
last_name = props.StrProperty()
382383

383-
grades = VirtualListProperty(Grade, "student_id")
384+
grades = props.VirtualListProperty(Grade, "student_id")
384385

385386
def __init__(
386387
self,
@@ -405,16 +406,14 @@ Since the `Grade` is going to be a separate entity, we should add it to our `App
405406
```py
406407
# ---- sniff ----
407408

408-
from sjd.entity.properties import ReferenceProperty
409-
410409
class Grade(TEntity):
411410
__json_init__ = True
412411

413-
course_id = IntProperty(required=True)
414-
course_name = StrProperty(required=True)
415-
score = IntProperty(required=True)
412+
course_id = props.IntProperty(required=True)
413+
course_name = props.StrProperty(required=True)
414+
score = props.IntProperty(required=True)
416415

417-
student_id = ReferenceProperty()
416+
student_id = props.ReferenceProperty()
418417

419418
def __init__(self, course_id: int, course_name: str, score: int):
420419
self.course_id = course_id
@@ -431,8 +430,8 @@ Since the `Grade` is going to be a separate entity, we should add it to our `App
431430
```py
432431
class AppEngine(Engine):
433432

434-
students = __Collection__(Student)
435-
grades = __Collection__(Grade)
433+
students = Engine.set(Student)
434+
grades = Engine.set(Grade)
436435

437436
def __init__(self):
438437
super().__init__("__test_db__")
@@ -453,7 +452,7 @@ Since the `Grade` is going to be a separate entity, we should add it to our `App
453452
If you try getting one of your students now, you'll see the `grades` property is an empty list.
454453

455454
```py
456-
arash = await engine.students.as_queryable.first(
455+
arash = await engine.students.get_first(
457456
lambda s: s.first_name == "Arash"
458457
)
459458
print(arash.grades)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = json-entity
3-
version = 0.0.1rc3
3+
version = 0.0.2c0
44
author = immmdreza
55
author_email = ir310022@gmail.com
66
description = A simple and async json database.

src/examples/virtual_props.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ async def main():
5757
Student(1, "Arash", "Eshi", [Grade(1, "Physics", 20)]),
5858
)
5959

60-
arash = await engine.students.as_queryable.first(lambda s: s.first_name == "Arash")
60+
arash = await engine.students.get_first(lambda s: s.first_name, "Arash")
6161

62-
async for grade in engine.students.iter_referenced_by(arash, lambda s: s.grades):
63-
print(grade.course_name)
62+
if arash:
63+
async for grade in engine.students.iter_referenced_by(
64+
arash, lambda s: s.grades
65+
):
66+
print(grade.course_name)
6467

6568

6669
if __name__ == "__main__":

0 commit comments

Comments
 (0)