{ "info": { "author": "Thomas Perl", "author_email": "m@thp.io", "bugtrack_url": null, "classifiers": [], "description": "\n _ _ _ _\n _ __ (_)_ _ (_)__| | |__\n | ' \\| | ' \\| / _` | '_ \\\n |_|_|_|_|_||_|_\\__,_|_.__/\n simple python object store\n\n\nminidb 2 makes it easy to store Python objects in a SQLite 3 database and work\nwith the data in an easy way with concise syntax.\n\n\nTutorial\n--------\n\nLet's start by importing the minidb module in Python 3:\n\n>>> import minidb\n\nTo create a store in memory, we simply instantiate a minidb.Store, optionally\ntelling it to output SQL statements as debug output:\n\n>>> db = minidb.Store(debug=True)\n\nIf you want to persist into a file, simply pass in a filename as the first\nparameter when creating the minidb.Store:\n\n>>> db = minidb.Store('filename.db', debug=True)\n\nTo actually store objects, we need to subclass from minidb.Model (which takes\ncare of all the behind-the-scenes magic for making your class persistable, and\nadds methods for working with the database):\n\n>>> class Person(minidb.Model):\n... name = str\n... email = str\n... age = int\n\nEvery subclass of minidb.Model will also have a \"id\" attribute that is None if\nan instance is not stored in the database, or an automatically assigne value if\nit is in the database. This uniquely identifies an object in the database.\n\nNow it's time to register our minidb.Model subclass with the store:\n\n>>> db.register(Person)\n\nThis will check if the table exists, and create the necessary structure (this\noutput appears only when debug=True is passed to minidb.Store's constructor):\n\n: PRAGMA table_info(Person)\n: CREATE TABLE Person (id INTEGER PRIMARY KEY,\n name TEXT,\n email TEXT,\n age INTEGER)\n\nNow you can create instances of your minidb.Model subclass, optionally passing\nkeyword arguments that will be used to initialize the fields:\n\n>>> p = Person(name='Hello World', email='minidb@example.com', age=99)\n>>> p\n\n\nTo store this object in the database, use .save() on the instance with the\nstore as sole argument:\n\n>>> p.save(db)\n\nIn debug mode, we will see how it stores the object in the database:\n\n: INSERT INTO Person (name, email, age) VALUES (?, ?, ?)\n ['Hello World', 'minidb@example.com', '99']\n\nAlso, it will now have its \"id\" attribute assigned:\n\n>>> p\n\n\nThe instance will remember the last minidb.Store object it was saved into or\nthe minidb.Store object from which it was loaded, so you can leave it out the\nnext time you want to save the object:\n\n>>> p.name = 'Hello Again'\n>>> p.save()\n\nAgain, the store will figure out what needs to be done:\n\n: UPDATE Person SET name=?, email=?, age=? WHERE id=?\n ['Hello Again', 'minidb@example.com', '99', 1]\n\nNow, let's insert some more data, just for fun:\n\n>>> for i in range(10):\n... Person(name='Hello', email='x@example.org', age=10+i*3).save(db)\n\nNow that we have some objects in the database, let's query all elements, and\nalso let's output if any of those loaded objects is the same object as p:\n\n>>> for person in Person.load(db):\n... print(person, person is p)\n\nThe SQL query that is executed by Person.load() is:\n\n: SELECT id, name, email, age FROM Person\n []\n\nThe output of the load looks like this:\n\n True\n False\n False\n False\n False\n False\n False\n False\n False\n False\n False\n\nNote that the first object retrieved is actually the object p (there's no new\nobject created, it's the same). minidb caches objects as long as you have a\nreference to them around, and will be able to retrieve those objects instead.\nThis makes sure that all objects stay in sync, let's try modifying an object\nreturned by Person.get(), a function that retrieves exactly one object:\n\n>>> print(p.name)\nHello Again\n>>> Person.get(db, id=1).name = 'Hello'\n>>> print(p.name)\nHello\n\nNow, let's try some more fancy queries. The minidb.Model subclass has a class\nattribute called \"c\" that can be used to reference to the columns/attributes:\n\n>>> Person.c\n\n\nFor example, we can query all objects for which age is between 16 and 50\n\n>>> Person.load(db, (Person.c.age >= 16) & (Person.c.age <= 50))\n\nThis will run the following SQL query:\n\n: SELECT id, name, email, age FROM Person WHERE ( age >= ? ) AND ( age <= ? )\n [16, 50]\n\nInstead of querying for full objects, you can also query for columns, for\nexample, we can find out the minimum and maximum age value in the table:\n\n>>> next(Person.query(db, Person.c.age.min // Person.c.age.max))\n(10, 99)\n\nThe corresponding query looks like this:\n\n: SELECT min(age), max(age) FROM Person\n []\n\nNote that column1 // column2 is syntactic sugar for the more verbose syntax of\nminidb.columns(column1, column2). The .query() method returns a generator of\nrows, you can get a single row via the Python built-in next(). Each row can be\naccessed in different ways:\n\n 1. As tuple (this is also the default representation when printing a row)\n 2. As dictionary\n 3. As object with attributes\n\nFor example, as a dictionary:\n\n>>> dict(next(Person.query(db, Person.c.age.min)))\n{'min(age)': 10}\n\nIf you want to have nicer names, you can give your result columns names:\n\n>>> dict(next(Person.query(db, Person.c.age.min('minimum_age'))))\n{'minimum_age': 10}\n\nThe generated SQL query for renaming looks like this:\n\n: SELECT min(age) AS minimum_age FROM Person\n []\n\nAnd of course, you can access the column using attribute access:\n\n>>> next(Person.query(db, Person.c.age.min('minimum_age'))).minimum_age\n10\n\nThere is also support for SQL's ORDER BY, GROUP_BY and LIMIT, as optional\nkeyword arguments to .query():\n\n>>> list(Person.query(db, Person.c.name // Person.c.age,\n... order_by=Person.c.age.desc, limit=5))\n\nTo save typing, you can do:\n\n>>> Person.c.name.query(db)\n\n>>> (Person.c.name // Person.c.email).query(db)\n\n>>> (Person.c.name // Person.c.age).query(db, order_by=lamdba c: c.age.desc)\n\n>>> Person.query(db, lambda c: c.name // c.email)\n\n\nSee example.py for more examples.\n\n", "description_content_type": null, "docs_url": null, "download_url": "http://thp.io/2010/minidb/minidb-2.0.2.tar.gz", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://thp.io/2010/minidb/", "keywords": null, "license": "ISC", "maintainer": null, "maintainer_email": null, "name": "minidb", "package_url": "https://pypi.org/project/minidb/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/minidb/", "project_urls": { "Download": "http://thp.io/2010/minidb/minidb-2.0.2.tar.gz", "Homepage": "http://thp.io/2010/minidb/" }, "release_url": "https://pypi.org/project/minidb/2.0.2/", "requires_dist": null, "requires_python": null, "summary": "A simple SQLite3-based store for Python objects", "version": "2.0.2" }, "last_serial": 2576041, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "93ab255142f49576385c826ce4417cc0", "sha256": "ae170597db1b4f966c9f7a0cd319c690636f5ddd9f0ea9040b8b8ed20a5693fe" }, "downloads": -1, "filename": "minidb-1.0.tar.gz", "has_sig": false, "md5_digest": "93ab255142f49576385c826ce4417cc0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5060, "upload_time": "2010-10-30T14:00:01", "url": "https://files.pythonhosted.org/packages/14/22/da30303baf9ac09ad7da9666eb3c501706db4c44c3d7f9df8b79b71ff7f5/minidb-1.0.tar.gz" } ], "1.1": [ { "comment_text": "", "digests": { "md5": "98265d1d7406925fc8e46a29ec0a69e9", "sha256": "6df30afc6ab5ec8c594056aa3774cfc4a7150c75f993bbcf5769d664bc94229d" }, "downloads": -1, "filename": "minidb-1.1.tar.gz", "has_sig": false, "md5_digest": "98265d1d7406925fc8e46a29ec0a69e9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5076, "upload_time": "2012-11-28T20:00:50", "url": "https://files.pythonhosted.org/packages/20/77/7f4ecab3cdd34e8f27f873e0a2557551d3560d2cc5c147fe3555bb15c04c/minidb-1.1.tar.gz" } ], "2.0.0": [ { "comment_text": "", "digests": { "md5": "148e508e3ea0b803fdf8f893685496c1", "sha256": "b62457a50462c4e1c07d23c46aa86ffb42387e77d2cf922dd7d5b6d83a450656" }, "downloads": -1, "filename": "minidb-2.0.0.tar.gz", "has_sig": false, "md5_digest": "148e508e3ea0b803fdf8f893685496c1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13828, "upload_time": "2015-02-21T20:32:25", "url": "https://files.pythonhosted.org/packages/54/58/3f90a7b85e7798d6e61e8f6d23e5c61e5275319d1248f357e92fe22a7d65/minidb-2.0.0.tar.gz" } ], "2.0.1": [ { "comment_text": "", "digests": { "md5": "aaa21ae05180af23730371e2fc896439", "sha256": "be894fc4e68e3ce9b84b3992f12fbbea5051d3c2a11019b1da463099f24725f5" }, "downloads": -1, "filename": "minidb-2.0.1.tar.gz", "has_sig": false, "md5_digest": "aaa21ae05180af23730371e2fc896439", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13848, "upload_time": "2015-05-24T18:40:25", "url": "https://files.pythonhosted.org/packages/67/ef/9ad4967f3debdf8600fe32e4985c917a117e1e46051b48873677bfc88f72/minidb-2.0.1.tar.gz" } ], "2.0.2": [ { "comment_text": "", "digests": { "md5": "b0253f6e8a288fd8f2e476a25dac51ce", "sha256": "43d59231556e9ed43d88c8c1ffcca30886b4db6436625599eeeb22bb9f74ab2b" }, "downloads": -1, "filename": "minidb-2.0.2.tar.gz", "has_sig": false, "md5_digest": "b0253f6e8a288fd8f2e476a25dac51ce", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13928, "upload_time": "2017-01-15T21:05:52", "url": "https://files.pythonhosted.org/packages/37/95/8b304b24e9088e18ab2ea0d2f3d5526cf116c090600e6ac5849803f8dbc7/minidb-2.0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b0253f6e8a288fd8f2e476a25dac51ce", "sha256": "43d59231556e9ed43d88c8c1ffcca30886b4db6436625599eeeb22bb9f74ab2b" }, "downloads": -1, "filename": "minidb-2.0.2.tar.gz", "has_sig": false, "md5_digest": "b0253f6e8a288fd8f2e476a25dac51ce", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13928, "upload_time": "2017-01-15T21:05:52", "url": "https://files.pythonhosted.org/packages/37/95/8b304b24e9088e18ab2ea0d2f3d5526cf116c090600e6ac5849803f8dbc7/minidb-2.0.2.tar.gz" } ] }