Skip to main content

Utilities for Python

Project description

wmongo

wmongo is a library for python control of MongoDB databases.

Description

wmongo simplifies the use of MongoDB databases in Python. It provides a simple interface to the MongoDB database, allowing you to easily insert, update, and delete data.

Installation

To install the library, use pip:

pip install wmongo

Description

The wmongo library offers a number of general-purpose modules.

License

MIT

This project is licensed under the MIT License. See the LICENSE file for details.

Examples

This directory contains a collection of examples that demonstrate the usage of various modules and functionalities in this project. Each subfolder corresponds to a specific module and includes example scripts to help you understand how to use that module.

Directory Structure

The examples are organized as follows:

examples/
    wmongo_async/
        crud.py
        notification_receiver.py
    wmongo/
        crud.py
        delete_and_notify.py
        insert_and_notify.py
        notifaction_receiver.py
        update_and_notify.py

How to Use

  1. Navigate to the module folder of interest, e.g., examples/module1/.
  2. Open the README.md in that folder to get detailed information about the examples.
  3. Run the scripts directly using:
    python example1.py
    

Modules and Examples

wmongo

Description

This module demonstrates specific functionalities.

  • crud.py: Example demonstrating functionality.
from typing import List
from wmongo import WMongo
from pydantic import BaseModel

# Credentials stored in a dictionary for easy reuse
mongo_credentials = {
    "uri": "mongodb://localhost:27017",
    "username": "root",
    "password": "example",
}

redis_credentials = {
    "redis_host": "localhost",
    "redis_port": 6379,
    "redis_db": 0,
}

credentials = {
    **mongo_credentials,
    **redis_credentials,
}


# Pydantic model for user data validation
class UserModel(BaseModel):
    name: str
    age: int


# Insert a user synchronously
user_data = UserModel(name="Bob", age=25).dict()
with WMongo(database="mydb", verbose=False, **credentials) as wm:
    if wm.has_permission(user_id="123", collection="users"):
        wm.insert("users", {"name": "Alice", "age": 30})
    else:
        print("Access Denied")


# Insert a user synchronously with Redis caching
with WMongo(database="mydb", verbose=False, **credentials) as wm:
    wm.update("users", {"name": "Bob"}, {"age": 26})

# Read, update, and delete operations within the context manager
with WMongo(database="mydb", verbose=False, **credentials) as wm:
    # Delete user
    wm.delete("users", {"name": "Bob"})


# Pydantic model for user data validation
class UserRole(BaseModel):
    user_id: str
    role: int
    collections: List[str]
  • delete_and_notify.py: Example demonstrating functionality.
from wmongo import WMongo
from pydantic import BaseModel

# Credentials stored in a dictionary for easy reuse
mongo_credentials = {
    "username": "root",
    "password": "example",
}

redis_credentials = {
    "redis_host": "localhost",
    "redis_port": 6379,
    "redis_db": 0,
}

notifications = {
    "enable_notifications": True,
}

credentials = {
    **mongo_credentials,
    **redis_credentials,
    **notifications,
}


# Pydantic model for user data validation
class UserModel(BaseModel):
    name: str
    age: int


# Insert a user synchronously
user_data = UserModel(name="Bob", age=25).dict()
with WMongo(database="mydb", verbose=True, **credentials) as wm:
    if wm.has_permission(user_id="123", collection="users"):
        wm.delete("users", {"name": "Alice"})
    else:
        print("Access Denied")
  • insert_and_notify.py: Example demonstrating functionality.
from wmongo import WMongo
from pydantic import BaseModel

# Credentials stored in a dictionary for easy reuse
mongo_credentials = {
    "username": "root",
    "password": "example",
}

redis_credentials = {
    "redis_host": "localhost",
    "redis_port": 6379,
    "redis_db": 0,
}

notifications = {
    "enable_notifications": True,
}

credentials = {
    **mongo_credentials,
    **redis_credentials,
    **notifications,
}


# Pydantic model for user data validation
class UserModel(BaseModel):
    name: str
    age: int


# Insert a user synchronously
user_data = UserModel(name="Bob", age=25).dict()
with WMongo(database="mydb", verbose=False, **credentials) as wm:
    if wm.has_permission(user_id="123", collection="users"):
        wm.insert("users", {"name": "Alice", "age": 30})
    else:
        print("Access Denied")
  • notifaction_receiver.py: Example demonstrating functionality.
from wmongo import WMongo

# Credentials stored in a dictionary for easy reuse
mongo_credentials = {
    "username": "root",
    "password": "example",
}

redis_credentials = {
    "redis_host": "localhost",
    "redis_port": 6379,
    "redis_db": 0,
}


def my_notification_callback(message: str):
    print(f"📩 Notification Received: {message}")


notifications = {
    "enable_notification_receiver": True,
    "notification_callback": my_notification_callback,
}

credentials = {
    **mongo_credentials,
    **redis_credentials,
    **notifications,
}


# Este script solo escuchará notificaciones
with WMongo(
    database="mydb",
    verbose=False,
    **credentials,
) as wm:
    print("Listening for notifications...")
    wm.listen_notifications()
  • update_and_notify.py: Example demonstrating functionality.
from wmongo import WMongo
from pydantic import BaseModel

# Credentials stored in a dictionary for easy reuse
mongo_credentials = {
    "username": "root",
    "password": "example",
}

redis_credentials = {
    "redis_host": "localhost",
    "redis_port": 6379,
    "redis_db": 0,
}

notifications = {
    "enable_notifications": True,
}

credentials = {
    **mongo_credentials,
    **redis_credentials,
    **notifications,
}


# Pydantic model for user data validation
class UserModel(BaseModel):
    name: str
    age: int


# Insert a user synchronously
user_data = UserModel(name="Bob", age=25).dict()
with WMongo(database="mydb", verbose=True, **credentials) as wm:
    if wm.has_permission(user_id="123", collection="users"):
        wm.update("users", {"name": "Alice"}, {"age": 31})
    else:
        print("Access Denied")

wmongo_async

Description

This module demonstrates specific functionalities.

  • crud.py: Example demonstrating functionality.
from wmongo import WMongoAsync
import asyncio

# Credentials stored in a dictionary for easy reuse
mongo_credentials = {
    "username": "root",
    "password": "example",
}

redis_credentials = {
    "redis_host": "localhost",
    "redis_port": 6379,
    "redis_db": 0,
}

notifications = {
    "enable_notifications": True,
}

credentials = {
    **mongo_credentials,
    **redis_credentials,
    **notifications,
}


async def insert_example():
    async with WMongoAsync(database="mydb", verbose=False, **credentials) as wm:
        doc_id = await wm.insert("users", {"name": "Alice", "age": 30})
        print(f"Inserted document with ID: {doc_id}")


asyncio.run(insert_example())


async def find_example():
    async with WMongoAsync(database="mydb", verbose=False, **credentials) as wm:
        users = await wm.find("users", {"age": {"$gte": 20}})  # Usuarios con edad >= 20
        print(f"Users found: {users}")


asyncio.run(find_example())


async def update_example():
    async with WMongoAsync(database="mydb", verbose=False, **credentials) as wm:
        updated_count = await wm.update("users", {"name": "Alice"}, {"age": 35})
        print(f"Updated {updated_count} documents.")


asyncio.run(update_example())




async def delete_example():
    async with WMongoAsync(database="mydb", verbose=False, **credentials) as wm:
        deleted_count = await wm.delete("users", {"name": "Alice"})
        print(f"Deleted {deleted_count} documents.")

asyncio.run(delete_example())
  • notification_receiver.py: Example demonstrating functionality.
from wmongo import WMongoAsync
import asyncio

# Credentials stored in a dictionary for easy reuse
mongo_credentials = {
    "username": "root",
    "password": "example",
}

redis_credentials = {
    "redis_host": "localhost",
    "redis_port": 6379,
    "redis_db": 0,
}


def my_notification_callback(message: str):
    print(f"📩 Notification Received: {message}")


notifications = {
    "enable_notification_receiver": True,
    "notification_callback": my_notification_callback,
}

credentials = {
    **mongo_credentials,
    **redis_credentials,
    **notifications,
}


async def listen_for_notifications():
    async with WMongoAsync(database="mydb", verbose=False, **credentials) as wm:
        print("Listening for notifications...")
        wm.listen_notifications()


asyncio.run(listen_for_notifications())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wmongo-0.1.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wmongo-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file wmongo-0.1.0.tar.gz.

File metadata

  • Download URL: wmongo-0.1.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for wmongo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 91a9cc7b4ee90597afcba1d2b4d2657c0a56583002be15468f5b1063e753d860
MD5 f1527c4d1454b0bdb4e76e965309406b
BLAKE2b-256 1a571f81850c3a6cbff1e27ae6bf7883e5c7fea0f1520363091328f9542da152

See more details on using hashes here.

File details

Details for the file wmongo-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: wmongo-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for wmongo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58da9061fa2813d123a0d6fdf4f3e05703818fb9779acc5af910bd6aae0ea61d
MD5 4fbaf3f3ca5f6b0ab5a9df197dbcaaa7
BLAKE2b-256 22434fd6a636a58d97f930089919a51dae8d6ffbbbf2c61a9a33ce8cedec464c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page