Skip to main content

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.

All the litements libraries (including this one) accept either a filename or an already created sqlite connection. Apart from that, if you have pysqlite3 installed it will use that instead of the sqlite3 module from the standard library.

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 .py file.
  • Tests must be inside the tests.ipynb notebook.

Metadata

Release files for litecounter 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for litecounter 0.2
File Size Uploaded Provenance Actions
litecounter-0.2.tar.gz 3.7 kB Without provenance Details for litecounter-0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for litecounter 0.2
File Interpreter ABI Platform Actions
litecounter-0.2-py3-none-any.whl Python 3 none any Details for litecounter-0.2-py3-none-any.whl

Total release size: 8.1 kB

Release files / litecounter-0.2.tar.gz

Download URL litecounter-0.2.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4d6ca90d244052c2d1332db7ed168dcd6d52eb1733772666fa66e9fb8a1ebffd
BLAKE2b-256 checksum
How to use checksums
ebe5ea7adb60b43f3f72782493a8595b2dcb35096d69646b486fd6bcc93ae91c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

Release files / litecounter-0.2-py3-none-any.whl

Download URL litecounter-0.2-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d0054cf9f77fc2162bb852247e818035ed94319914f7a5272e67746cc9c76ac
BLAKE2b-256 checksum
How to use checksums
f55840562f22ece183a436ca7b1f67db847b8e15a1ba75c35e2a33793199e4e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

Security for litecounter 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page