Simple counter built on top of SQLite
Project description
litecounter
Very simple counter implemented on top of SQLite
Why?
You can use this to implement a persistent counter. It also uses some SQLite syntax to initialize keys to 0 when the counter starts on them, just as if you had a collections.defaultdict where the default is 0.
Installation
pip install litecounter
IMPORTANT: This package uses SQLite's UPSERT statment so it needs to run at least with SQLite version 3.24.0 (released 2018-06-04).
If you need to run the latest version, you can use pysqlite3 and override sqlite3
import pysqlite3 # pre-built if you install pysqlite3-binary
import sys
sys.modules['sqlite3'] = pysqlite3
In near releases you will be able to use a connection instead of a just the database name to create the counter, so you won't need to override sqlite3 globally.
Examples
The examples are taken from the tests in tests.ipynb
TEST_1 = "key_test_1"
TEST_2 = "key_test_2"
from litecounter import SQLCounter
counter = SQLCounter(":memory:")
# Increment from 0 to 20
for _ in range(20):
counter.incr(TEST_1)
assert counter.count(TEST_1) == 20
# Decrement 10 (from 20 to 10)
for _ in range(10):
counter.decr(TEST_1)
assert counter.count(TEST_1) == 10
# From 0 to -10, then -20.
for _ in range(10):
counter.decr(TEST_2)
assert counter.count(TEST_2) == -10
for _ in range(10):
counter.decr(TEST_2)
assert counter.count(TEST_2) == -20
# Set fist key to 0.
counter.zero(TEST_1)
assert counter.count(TEST_1) == 0
# Increment the second test key by 100, from -20 to 80.
for _ in range(100):
counter.incr(TEST_2)
assert counter.count(TEST_2) == 80
# Delete key works
assert counter.count(TEST_1) == 0
counter.delete(TEST_1)
assert counter.count(TEST_1) is None
# When the key does not exist, delete just ignores it
counter.delete("foobar")
# Check `__repr__`
import random
for key in ["foo", "bar", "baz", "foobar", "asd", TEST_1]:
for _ in range(random.randint(0,10)):
counter.incr(key)
print(counter)
# SQLCounter(dbname=':memory:', items=[('key_test_2', 80), ('foo', 8), ('baz', 5), ('foobar', 6), ('key_test_1', 10)])
Meta
Ricardo Ander-Egg Aguilar – @ricardoanderegg –
Distributed under the MIT license. See LICENSE for more information.
Contributing
The only hard rules for the project are:
- No extra dependencies allowed
- No extra files, everything must be inside the main module's
.pyfile. - Tests must be inside the
tests.ipynbnotebook.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file litecounter-0.1.1.tar.gz.
File metadata
- Download URL: litecounter-0.1.1.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47d11d99a1b4078da4c9899dee5ad87d8b035348c26e9ce90c6f3a9b442912ba
|
|
| MD5 |
9afeb0ab739b2721137dbfd852b89a75
|
|
| BLAKE2b-256 |
0c78463909165097c1b7ef327adc7c5045b4169613d7b711022ce49a09667358
|
File details
Details for the file litecounter-0.1.1-py3-none-any.whl.
File metadata
- Download URL: litecounter-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3a073edf3ae9058350848631a05db49a6b83a4808f5b63cceee0220462b83c5
|
|
| MD5 |
42a9b71290a92f14c891e14439b9c46f
|
|
| BLAKE2b-256 |
1dbb8614fdc674c3490108981dfcf575d9ef9a8dda08c83fae24a5f105f2dfe7
|