Skip to main content

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

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.

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

litecounter-0.1.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

litecounter-0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file litecounter-0.1.tar.gz.

File metadata

  • Download URL: litecounter-0.1.tar.gz
  • Upload date:
  • Size: 3.2 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.0 CPython/3.8.7

File hashes

Hashes for litecounter-0.1.tar.gz
Algorithm Hash digest
SHA256 966efc3e4326b422a7ac274fe221c137643030cd0d40616464fe1c5e9377e71a
MD5 0fea9b0ebf2a8eec86507988da29a908
BLAKE2b-256 fbc5779757c9e4f54fca897db3c42264f9af30acbcdc630cb4936b61f7d16765

See more details on using hashes here.

File details

Details for the file litecounter-0.1-py3-none-any.whl.

File metadata

  • Download URL: litecounter-0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 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.0 CPython/3.8.7

File hashes

Hashes for litecounter-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 de556dd89acdfc722cee532849c02917b2a30c586e3b66aa903e01aa465ee61d
MD5 d543449e1818e4e665148d0dcad0199c
BLAKE2b-256 bca0b8b3c9ba726b32f48696fd08555cc93ac827f2e5794f1f1d6ae2fd475de7

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