Clever stuff with SQL in __doc__
Project description
Clever stuff with SQL in __doc__
Features
In-memory or file-backed database (Sqlite)
Post-process returned data
User definded functions
Example
Given this:
''' CREATE TABLE IF NOT EXISTS users (
userid INTEGER PRIMARY KEY,
username TEXT UNIQUE NOT NULL,
bcrypt BLOB NOT NULL
);
'''
from sqlfunc import sqlinit, sqludf, sqlfunc
__database__ = ':memory:' # default
@sqludf
def bcrypt_hash(password):
# call to library here
return b'$2b$12$.OjbRwRejxw92C89sA6JkOVrhmQzGsjoyCf1ofIN9hUNdHFufb3ty'
@sqludf
def bcrypt_verify(password, bcrypthash):
# call to library here
return True
@sqlfunc
def add_user(username, password):
''' INSERT OR IGNORE INTO $$$ (username, bcrypthash)
VALUES (:username, bcrypt_hash(:password));
'''
@sqlfunc(post=lambda x: bool(list(x)))
def login(username, password):
''' SELECT 1 FROM users
WHERE username=:username
AND bcrypt_verify(:password, bcrypt);
'''
sqlinit()
You can now do this:
>>> import example_users
>>> example_users.add_user('root', 'password123')
>>> example_users.login('root', 'secret')
False
>>> example_users.login('root', 'password123')
True
>>> example_users.list_users()
['root']
>>> 'bcrypt_verify' in dir(example_users) # helper functions are NOT exported
False
This library is MIT licensed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sqlfunc-0.1.tar.gz
(2.7 kB
view details)
File details
Details for the file sqlfunc-0.1.tar.gz
.
File metadata
- Download URL: sqlfunc-0.1.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35784a5b7831644a1f05b3a99bb839904bd133f9a2c0bc4b98ce8bfc00b7a476 |
|
MD5 | 36f202ac7e35b73d9331d297d7a63a41 |
|
BLAKE2b-256 | 3e864272004a6e3de0cdaab8560baafa71cfffcc8922c3925e9d72e707334e81 |