Code Monkey home page Code Monkey logo

lightdb's Introduction


LightDB: Lightweight JSON Database for Python

What is this?

LightDB is a simple and lightweight JSON database for Python that allows users to efficiently write data to a file. It is designed to be easy to use, making it a great choice for developers who need a fast and reliable way to store and retrieve data.

Installing

You can install LightDB using pip:

pip install LightDB

Usage

To use LightDB, first import the LightDB class from the lightdb package:

from lightdb import LightDB

Then, create a LightDB object by passing in the path to a JSON file where the database will be stored:

db = LightDB("db.json")

You can then set key-value pairs in the database using the set() method:

db.set("name", "Alice")
db.set("age", 30)

To save the changes, use the save() method:

db.save()

Using Models

LightDB supports defining models for more structured and convenient data management. Here’s how to use models with LightDB:

First, import the necessary classes:

from typing import List, Dict, Any

from lightdb import LightDB
from lightdb.models import Model

Define your model by extending the Model class:

class User(Model, table="users"):
    name: str
    age: int
    items: List[str] = []
    extra: Dict[str, Any] = {}

Create a new instance of the model and save it to the database:

user = User.create(name="Alice", age=30)

Retrieve a user from the database:

user = User.get(User.name == "Alice")
# or user = User.get(name="Alice")
print(user.name, user.age)

Update a user’s information and save it:

user.name = "Kristy"
user.save()

Filter users based on certain criteria:

users = User.filter(User.age >= 20)
for user in users:
    print(user.name)

Delete a user:

user.delete()

License

LightDB is licensed under the MIT License.

lightdb's People

Contributors

deepsourcebot avatar fl1yd avatar lilwonka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.