Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving documentation to publicize and share the project and codes #580

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/how-to.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
:tocdepth: 3

How-To's
===============

Page dedicated to creating and sharing how-to's. All of the examples can be found in the `examples` folder.

Title 1
-----------------

Title 2
*****
9 changes: 9 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ User's Guide
getting-started
usage

How-To's and Tutorials
------------

.. toctree::
:maxdepth: 2

how-to
tutorials

Extending TinyDB
----------------

Expand Down
12 changes: 12 additions & 0 deletions docs/tutorials.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
:tocdepth: 3

Tutorials
===============

Page dedicated to creating and sharing tutorials. All of the examples can be found in the `examples` folder.

Title 1
-----------------

Title 2
*****
23 changes: 23 additions & 0 deletions examples/advanced-usage/data_upsert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from tinydb import TinyDB, Query

# Creating a database
db = TinyDB('db.json')

# Insert data from people with name and logged-in status
people = [
{'name': 'John', 'logged-in': False},
{'name': 'Alice', 'logged-in': False},
{'name': 'Bob', 'logged-in': True},
{'name': 'Carol', 'logged-in': False},
{'name': 'Eve', 'logged-in': True},
]
db.insert_multiple(people)
print(db.all())

# Uperting data, all Johns will be changed to logged-in
User = Query()
db.upsert({'name': 'John', 'logged-in': True}, User.name == 'John')
print(db.all())

# Truncate the database, comment if you want to keep the data
people_db.truncate()
1 change: 1 addition & 0 deletions examples/advanced-usage/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_default": {"1": {"name": "John", "logged-in": true}, "2": {"name": "Alice", "logged-in": false}, "3": {"name": "Bob", "logged-in": true}, "4": {"name": "Carol", "logged-in": false}, "5": {"name": "Eve", "logged-in": true}}}
52 changes: 52 additions & 0 deletions examples/advanced-usage/queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from tinydb import TinyDB, Query, where

'''
Advanced Queries
'''

# Create a TinyDB instance for more advanced insertions
# This will create or open a database file named 'people.json'
people_db = TinyDB('people.json')

# Define the data to be inserted
people = [
{'name': 'John', 'birthday': {'year': 1985, 'month': 3, 'day': 15}},
{'name': 'Alice', 'birthday': {'year': 1990, 'month': 5, 'day': 10}},
{'name': 'Bob', 'birthday': {'year': 1987, 'month': 9, 'day': 20}},
{'name': 'Carol', 'birthday': {'year': 1992, 'month': 12, 'day': 5}},
{'name': 'Eve', 'birthday': {'year': 1989, 'month': 7, 'day': 25}},
]

# Insert the data into the database
people_db.insert_multiple(people)

# Print the contents of the database
print(people_db.all())

# Query the database using the Query object
User = Query()
print(people_db.search(User.name == 'John'))
print(people_db.search(User.birthday.year == 1990))

# Query the database using the where function
print(people_db.search(where('birthday').year == 1900))
print(people_db.search(~ (User.name == 'John')))

'''
Query Modifiers
'''

# Negate a query, returns all people that are not John
negated_query = ~ (User.name == 'John')
print(people_db.search(negated_query))

# Logical AND, returns all people that are John and born before 1995
and_query = (User.name == 'John') & (User.birthday.year <= 1995)
print(people_db.search(and_query))

# Logical OR, returns all people that are Alex or Bob
or_query = (User.name == 'Alex') | (User.name == 'Bob')
print(people_db.search(or_query))

# Truncate the database, comment if you want to keep the data
people_db.truncate()
31 changes: 31 additions & 0 deletions examples/getting-started/basic_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from tinydb import TinyDB, Query

# Creating a database
db = TinyDB('db.json')

# Inserting data
db.insert({'type': 'apple', 'count': 7})
db.insert({'type': 'peach', 'count': 3})

# Get and print all documents
print(db.all())

# Itering the database
for item in db:
print(item)

# Searching for specific documents with Query object
Fruit = Query()
db.search(Fruit.type == 'peach')
db.search(Fruit.count > 5)

# Updating documents
db.update({'count': 10}, Fruit.type == 'apple')
print(db.all())

# Removing documents
db.remove(Fruit.count < 5)
print(db.all())

# Deleting all documents, comment if you want to keep the data
db.truncate()
1 change: 1 addition & 0 deletions examples/getting-started/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_default": {}}