Git Product home page Git Product logo

permadict's Introduction

Permadict

Build Status Coverage

A trivial, persistent, dictionary-like object, backed by SQLite.

Installation

$ python setup.py install

Or just drop the permadict.py file into your package.

Usage

Basic usage:

>>> from permadict import Permadict
>>> d = Permadict("db.sqlite")
>>> d["key"] = "value"
>>> print(d["key"])
value

As a context manager:

>>> with Permadict("db.sqlite") as d:
...     d["something"] = 1.2345
...
>>> with Permadict("db.sqlite") as d:
...     print(d["something"])
...
1.2345

Iterating:

>>> d = Permadict("db.sqlite")
>>> for k, v in d.items():
...     print(k, v)
...
something 1.2345
>>> for key in d.keys():
...     print(key)
...
something

Deleting an item:

>>> del d["something"]

Clearing all items:

>>> d.clear()

Limitations

Keys must be strings. Values are stored as BLOB type after being pickled, so your Python objects must be picklable. Reading and writing speeds for large binary blobs are likely to be considerably slower than reading/writing directly to the filesystem, so Permadict shouldn't be used to store large Numpy arrays, for example. See here for more details.

Permadict doesn't act entirely like a dict: some methods are missing, whether that be on purpose (as with dict.copy) or simply due to negligence.

Motivation

I needed a way to share small amounts of data between processes. SQLite provides a safe way to do so. Also, why not?

permadict's People

Contributors

mivade avatar

Stargazers

ondrej avatar Zhao Xiaohong avatar Zhijun avatar

Watchers

 avatar

permadict's Issues

Tests fail on pypy

I keep getting the following:

self = <_sqlite3.Connection object at 0x0000000004225bb0>
    def commit(self):
        self._check_thread()
        self._check_closed()
        if not self._in_transaction:
            return
    
        # PyPy fix for non-refcounting semantics: since 2.7.13 (and in
        # <= 2.6.x), the statements are not automatically reset upon
        # commit.  However, if this is followed by some specific SQL
        # operations like "drop table", these open statements come in
        # the way and cause the "drop table" to fail.  On CPython the
        # problem is much less important because typically all the old
        # statements are freed already by reference counting.  So here,
        # we copy all the still-alive statements to another list which
        # is usually ignored, except if we get SQLITE_LOCKED
        # afterwards---at which point we reset all statements in this
        # list.
        self.__statements_already_committed = self.__statements[:]
    
        statement_star = _ffi.new('sqlite3_stmt **')
        ret = _lib.sqlite3_prepare_v2(self._db, b"COMMIT", -1,
                                      statement_star, _ffi.NULL)
        try:
            if ret != _lib.SQLITE_OK:
                raise self._get_exception(ret)
            ret = _lib.sqlite3_step(statement_star[0])
            if ret != _lib.SQLITE_DONE:
>               raise self._get_exception(ret)
E               OperationalError: cannot commit transaction - SQL statements in progress

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.