Skip to content

Commit bd130a2

Browse files
author
Ivan Dlugos
committed
add tasklist example
1 parent 979150a commit bd130a2

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ box.put(person) # Update
6464
box.remove(person) # Delete
6565
```
6666

67+
Additionally, see the [TaskList example app](example).
68+
6769
For more information and code examples, see the tests folder. The docs for other languages may also help you understand the basics.
6870

6971
* ObjectBox Java = https://docs.objectbox.io

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.mdb

example/__init__.py

Whitespace-only changes.

example/__main__.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from cmd import Cmd
2+
import objectbox
3+
import datetime
4+
from example.model import *
5+
6+
7+
# objectbox expects date timestamp in milliseconds since UNIX epoch
8+
def now_ms() -> int:
9+
seconds: float = datetime.datetime.utcnow().timestamp()
10+
return round(seconds * 1000)
11+
12+
13+
def format_date(timestamp_ms: int) -> str:
14+
return "" if timestamp_ms == 0 else str(datetime.datetime.fromtimestamp(timestamp_ms / 1000))
15+
16+
17+
class TasklistCmd(Cmd):
18+
prompt = "> "
19+
_ob = objectbox.Builder().model(get_objectbox_model()).directory("tasklist-db").build()
20+
_box = objectbox.Box(_ob, Task)
21+
22+
def do_ls(self, _):
23+
"""list tasks"""
24+
25+
tasks = self._box.get_all()
26+
27+
print("%3s %-29s %-29s %s" % ("ID", "Created", "Finished", "Text"))
28+
for task in tasks:
29+
print("%3d %-29s %-29s %s" % (
30+
task.id, format_date(task.date_created), format_date(task.date_finished), task.text))
31+
32+
def do_new(self, text: str):
33+
"""create a new task with the given text (all arguments concatenated)"""
34+
task = Task()
35+
task.text = text
36+
task.date_created = now_ms()
37+
self._box.put(task)
38+
39+
def do_done(self, id: str):
40+
"""mark task with the given ID as done"""
41+
task = self._box.get(int(id))
42+
task.date_finished = now_ms()
43+
self._box.put(task)
44+
45+
def do_exit(self, _):
46+
"""close the program"""
47+
raise SystemExit()
48+
49+
50+
if __name__ == '__main__':
51+
app = TasklistCmd()
52+
app.cmdloop('Welcome to the ObjectBox tasks-list app example. Type help or ? for a list of commands.')

example/model.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from objectbox.model import *
2+
3+
4+
@Entity(id=1, uid=1)
5+
class Task:
6+
id = Id(id=1, uid=1001)
7+
text = Property(str, id=2, uid=1002)
8+
9+
# TODO property type DATE
10+
date_created = Property(int, id=3, uid=1003)
11+
date_finished = Property(int, id=4, uid=1004)
12+
13+
14+
def get_objectbox_model():
15+
m = Model()
16+
m.entity(Task, last_property_id=IdUid(4, 1004))
17+
m.last_entity_id = IdUid(1, 1)
18+
return m

0 commit comments

Comments
 (0)