{ "info": { "author": "Masroor Ehsan Choudhury", "author_email": "masroore@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Topic :: Database" ], "description": "`pg\\_simple `__\n======================================================\n\nThe `pg\\_simple `__ module\nprovides a simple yet efficient layer over ``psycopg2`` providing Python\nAPI for common SQL functions, explicit and implicit transactions\nmanagement and database connection pooling for single and multi-threaded\napplications.\n\n``pg_simple`` is not intended to provide ORM-like functionality, rather\nto make it easier to interact with the PostgreSQL database from python\ncode for direct SQL access using convenient wrapper methods. The module\nwraps the excellent ``psycopg2`` library and most of the functionality\nis provided by this behind the scenes.\n\nThe ``pg_simple`` module provides:\n\n- Simplified handling of database connections/cursor\n- Connection pool for single or multithreaded access\n- Python API to wrap basic SQL functionality: select, update, delete,\n join et al\n- Query results as python namedtuple and dict objects (using\n ``psycopg2.extras.NamedTupleCursor`` and\n ``psycopg2.extras.DictCursor`` respectively)\n- Debug logging support\n\nInstallation\n------------\n\nWith ``pip`` or ``easy_install``:\n\n``pip install pg_simple``\n\nor:\n\n``easy_install pg_simple``\n\nor from the source:\n\n``python setup.py install``\n\n30 Seconds Quick-start Guide\n----------------------------\n\n- Step 1: Initialize a connection pool manager using\n ``pg_simple.config_pool()``\n- Step 2: Create a database connection and cursor by instantiating a\n ``pg_simple.PgSimple`` object\n\nHere's a pseudo-example to illustrate the basic concepts:\n\n.. code:: python\n\n import pg_simple\n\n pg_simple.config_pool(dsn='dbname=my_db user=my_username ...')\n\n with pg_simple.PgSimple() as db:\n db.insert('table_name',\n data={'column': 123,\n 'another_column': 'blah blah'})\n db.commit()\n\n with pg_simple.PgSimple() as db1:\n rows = db1.fetchall('table_name')\n\nConnection pool management\n--------------------------\n\nInitialize the connection pool\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n import pg_simple\n\n pg_simple.config_pool(max_conn=250,\n expiration=60, # idle timeout = 60 seconds\n host='localhost',\n port=5432,\n database='pg_simple',\n user='postgres',\n password='secret')\n\nor, using ``dsn``:\n\n.. code:: python\n\n pg_simple.config_pool(max_conn=250,\n expiration=60,\n dsn='dbname=database_name user=postgres password=secret')\n\nor, using ``db_url``:\n\n.. code:: python\n\n pg_simple.config_pool(max_conn=250,\n expiration=60,\n db_url= 'postgres://username:password@hostname:numeric_port/database')\n\nThe above snippets will create a connection pool capable of\naccommodating a maximum of 250 concurrent database connections. Once\nthat limit is reached and the pool does not contain any idle\nconnections, all subsequent new connection request will result in a\n``PoolError`` exception (until the pool gets refilled with idle\nconnections).\n\nTake caution to properly clean up all ``pg_simple.PgSimple`` objects\nafter use (wrap the object inside python try-finally block or ``with``\nstatement). Once the object is released, it will quietly return the\ninternal database connction to the idle pool. Failure to dispose\n``PgSimple`` properly may result in pool exhaustion error.\n\nConfigure connection pool for thread-safe access\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThe default ``SimpleConnectionPool`` pool manager is not thread-safe. To\nutilize the connection pool in multi-threaded apps, use the\n``ThreadedConnectionPool``:\n\n.. code:: python\n\n pg_simple.config_pool(max_conn=250,\n expiration=60,\n pool_manager=ThreadedConnectionPool,\n dsn='...')\n\nDisable connection pooling\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nTo disable connection pooling completely, set the ``disable_pooling``\nparameter to True:\n\n.. code:: python\n\n pg_simple.config_pool(disable_pooling=True, dsn='...')\n\nAll database requests on this pool will create new connections on the\nfly, and all connections returned to the pool (upon disposal of\n``PgSimple`` object or by explicitly invoking ``pool.put_conn()``) will\nbe discarded immediately.\n\nObtaining the current connection pool manager\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nCall the ``pg_simple.get_pool()`` method to get the current pool:\n\n.. code:: python\n\n pool = pg_simple.get_pool()\n\nGarbage collect stale connections\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nTo explicitly purge the pool of stale database connections (whose\nduration of stay in the pool exceeds the ``expiration`` timeout), invoke\nthe ``pool.purge_expired_connections()`` method:\n\n.. code:: python\n\n pool = pg_simple.get_pool()\n pool.purge_expired_connections()\n\nNote that the pool is automatically scavenged for stale connections when\nan idle connection is returned to the pool (using the\n``pool.put_conn()`` method).\n\nBasic Usage\n-----------\n\nConnecting to the posgtresql server\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThe following snippet will connect to the posgtresql server and allocate\na cursor:\n\n.. code:: python\n\n import sys\n import pg_simple\n\n db = pg_simple.PgSimple(log=sys.stdout,\n log_fmt=lambda x: '>> %s' % (x if isinstance(x, str) else x.query),\n nt_cursor=True)\n\nBy default ``PgSimple`` generates result sets as\n``collections.namedtuple`` objects (using\n``psycopg2.extras.NamedTupleCursor``). If you want to access the\nretrieved records using an interface similar to the Python dictionaries\n(using ``psycopg2.extras.DictCursor``), set the ``nt_cursor`` parameter\nto ``False``:\n\n.. code:: python\n\n db = pg_simple.PgSimple(nt_cursor=False)\n\nRaw SQL execution\n~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n >>> db.execute('SELECT tablename FROM pg_tables WHERE schemaname=%s and tablename=%s', ['public', 'books'])\n \n\nDropping and creating tables\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n db.drop('books')\n\n db.create('books',\n '''\n \"id\" SERIAL NOT NULL,\n \"type\" VARCHAR(20) NOT NULL,\n \"name\" VARCHAR(40) NOT NULL,\n \"price\" MONEY NOT NULL,\n \"published\" DATE NOT NULL,\n \"modified\" TIMESTAMP(6) NOT NULL DEFAULT now()\n '''\n )\n\n db.execute('''ALTER TABLE \"books\" ADD CONSTRAINT \"books_pkey\" PRIMARY KEY (\"id\")''')\n db.commit()\n\nEmptying a table or set of tables\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n db.truncate('tbl1')\n db.truncate('tbl2, tbl3', restart_identity=True, cascade=True)\n db.commit()\n\nInserting rows\n~~~~~~~~~~~~~~\n\n.. code:: python\n\n for i in range(1, 10):\n db.insert(\"books\",\n {\"genre\": \"fiction\",\n \"name\": \"Book Name vol. %d\" % i,\n \"price\": 1.23 * i,\n \"published\": \"%d-%d-1\" % (2000 + i, i)})\n\n db.commit()\n\nUpdating rows\n~~~~~~~~~~~~~\n\n.. code:: python\n\n with pg_simple.PgSimple() as db1:\n db1.update('books',\n data={'name': 'An expensive book',\n 'price': 998.997,\n 'genre': 'non-fiction',\n 'modified': 'NOW()'},\n where=('published = %s', [datetime.date(2001, 1, 1)]))\n \n db1.commit()\n\nDeleting rows\n~~~~~~~~~~~~~\n\n.. code:: python\n\n db.delete('books', where=('published >= %s', [datetime.date(2005, 1, 31)]))\n db.commit()\n\nInserting/updating/deleting rows with return value\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n row = db.insert(\"books\",\n {\"type\": \"fiction\",\n \"name\": \"Book with ID\",\n \"price\": 123.45,\n \"published\": \"1997-01-31\"},\n returning='id')\n print(row.id)\n\n rows = db.update('books',\n data={'name': 'Another expensive book',\n 'price': 500.50,\n 'modified': 'NOW()'},\n where=('published = %s', [datetime.date(2006, 6, 1)]),\n returning='modified')\n print(rows[0].modified)\n\n rows = db.delete('books', \n where=('published >= %s', [datetime.date(2005, 1, 31)]), \n returning='name')\n for r in rows:\n print(r.name)\n\nFetching a single record\n~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n book = db.fetchone('books', \n fields=['name', 'published'], \n where=('published = %s', [datetime.date(2002, 2, 1)]))\n \n print(book.name + 'was published on ' + book[1])\n\nFetching multiple records\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n books = db.fetchall('books',\n fields=['name AS n', 'genre AS g'],\n where=('published BETWEEN %s AND %s', [datetime.date(2005, 2, 1), datetime.date(2009, 2, 1)]),\n order=['published', 'DESC'], \n limit=5, \n offset=2)\n\n for book in books:\n print(book.n + 'belongs to ' + book[1])\n\nExplicit database transaction management\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n with pg_simple.PgSimple() as _db:\n try:\n _db.execute('Some SQL statement')\n _db.commit()\n except:\n _db.rollback()\n\nImplicit database transaction management\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n with pg_simple.PgSimple() as _db:\n _db.execute('Some SQL statement')\n _db.commit()\n\nThe above transaction will be rolled back automatically should something\ngoes awry.", "description_content_type": null, "docs_url": "https://pythonhosted.org/pg_simple/", "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/masroore/pg_simple", "keywords": "psycopg2 postgresql sql database", "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "pg_simple", "package_url": "https://pypi.org/project/pg_simple/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/pg_simple/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/masroore/pg_simple" }, "release_url": "https://pypi.org/project/pg_simple/0.2.3/", "requires_dist": null, "requires_python": null, "summary": "A simple wrapper for Python psycopg2 with connection pooling", "version": "0.2.3" }, "last_serial": 1196210, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "48d96d4d3b0e697436414973af0cae97", "sha256": "0fef20901316bff370824076ac4434fb043339cff0687b743badc77f8342231e" }, "downloads": -1, "filename": "pg_simple-0.1-py2.7.egg", "has_sig": false, "md5_digest": "48d96d4d3b0e697436414973af0cae97", "packagetype": "bdist_egg", "python_version": "2.7", "requires_python": null, "size": 10404, "upload_time": "2014-08-08T09:30:18", "url": "https://files.pythonhosted.org/packages/05/59/070877207de64fa8f61e24fff063a3fbe59d2bbfc29cbddf1d4bfb31c6fb/pg_simple-0.1-py2.7.egg" }, { "comment_text": "", "digests": { "md5": "21cc02ba94cc755417b1983b9a3e7000", "sha256": "1120cacb229e2f3e1f7b4031fa2ca87ff651cf1a1ba5410983920ab4b5ac38f0" }, "downloads": -1, "filename": "pg_simple-0.1.tar.gz", "has_sig": false, "md5_digest": "21cc02ba94cc755417b1983b9a3e7000", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4322, "upload_time": "2014-08-08T09:18:09", "url": "https://files.pythonhosted.org/packages/b3/12/9d07604f6aecf6e63e6e6b59dfcdf213b8def52e495dd653951866c2882b/pg_simple-0.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "23c140acb4cb2cf5e49021a2e4f9893a", "sha256": "784ab4c0d5f8ce5a43658691d822cb2eda46eeafa31ebb47d3bb5f0e9f4d0aae" }, "downloads": -1, "filename": "pg_simple-0.2-py2.7.egg", "has_sig": false, "md5_digest": "23c140acb4cb2cf5e49021a2e4f9893a", "packagetype": "bdist_egg", "python_version": "2.7", "requires_python": null, "size": 16635, "upload_time": "2014-08-15T19:12:13", "url": "https://files.pythonhosted.org/packages/01/dd/42510af2b5bd461a12e90f4f127027ed31f41ac54bde05847f110c7c6ee2/pg_simple-0.2-py2.7.egg" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "cba66d38b748a7aaff25b59c533ddda6", "sha256": "8b64639c00ff575c619ba7c0846ac2c6660490964b71fdcd141814b94ca80970" }, "downloads": -1, "filename": "pg_simple-0.2.1-py2.7.egg", "has_sig": false, "md5_digest": "cba66d38b748a7aaff25b59c533ddda6", "packagetype": "bdist_egg", "python_version": "2.7", "requires_python": null, "size": 18227, "upload_time": "2014-08-16T06:54:02", "url": "https://files.pythonhosted.org/packages/e1/99/022b1b2316a370d9f12770be647047733668f66e8e7a38d96609d6936c27/pg_simple-0.2.1-py2.7.egg" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "7c8fe08068b16de8dd8893955fd6e727", "sha256": "7d60e86a1b9c43f8ddcdab4bd4ca4678cfc453c45d8c0e555193fa1692f21e8a" }, "downloads": -1, "filename": "pg_simple-0.2.2-py2.7.egg", "has_sig": false, "md5_digest": "7c8fe08068b16de8dd8893955fd6e727", "packagetype": "bdist_egg", "python_version": "2.7", "requires_python": null, "size": 22157, "upload_time": "2014-08-16T13:37:55", "url": "https://files.pythonhosted.org/packages/da/03/5f9b6f17fc723c9af2fe1e1ba680bd1a9b09bfa29533863c5da2e9319b68/pg_simple-0.2.2-py2.7.egg" } ], "0.2.3": [ { "comment_text": "", "digests": { "md5": "4ed9fbcb3536604fa6063724fc9fa89b", "sha256": "0ea9948819bffce59edd71079ea2ef5b738657cf42d0506b00419d7400cb816d" }, "downloads": -1, "filename": "pg_simple-0.2.3-py2.7.egg", "has_sig": false, "md5_digest": "4ed9fbcb3536604fa6063724fc9fa89b", "packagetype": "bdist_egg", "python_version": "2.7", "requires_python": null, "size": 25178, "upload_time": "2014-08-20T06:50:23", "url": "https://files.pythonhosted.org/packages/52/1a/ab2d9d7e17d8ce7f3756439a7d426e73c7f0a5bb7283982284a2f9555c50/pg_simple-0.2.3-py2.7.egg" }, { "comment_text": "", "digests": { "md5": "be4279248967e84177df0df84f8a46f5", "sha256": "edaacbee45b037e4492fe5190cec2580bd4c9688a7406c1333fc8395389e7449" }, "downloads": -1, "filename": "pg_simple-0.2.3.tar.gz", "has_sig": false, "md5_digest": "be4279248967e84177df0df84f8a46f5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12032, "upload_time": "2014-08-20T06:50:18", "url": "https://files.pythonhosted.org/packages/c4/70/4a06431cea222b1951327a13de781686e79f09ca30b0772e4497a38ddaed/pg_simple-0.2.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "4ed9fbcb3536604fa6063724fc9fa89b", "sha256": "0ea9948819bffce59edd71079ea2ef5b738657cf42d0506b00419d7400cb816d" }, "downloads": -1, "filename": "pg_simple-0.2.3-py2.7.egg", "has_sig": false, "md5_digest": "4ed9fbcb3536604fa6063724fc9fa89b", "packagetype": "bdist_egg", "python_version": "2.7", "requires_python": null, "size": 25178, "upload_time": "2014-08-20T06:50:23", "url": "https://files.pythonhosted.org/packages/52/1a/ab2d9d7e17d8ce7f3756439a7d426e73c7f0a5bb7283982284a2f9555c50/pg_simple-0.2.3-py2.7.egg" }, { "comment_text": "", "digests": { "md5": "be4279248967e84177df0df84f8a46f5", "sha256": "edaacbee45b037e4492fe5190cec2580bd4c9688a7406c1333fc8395389e7449" }, "downloads": -1, "filename": "pg_simple-0.2.3.tar.gz", "has_sig": false, "md5_digest": "be4279248967e84177df0df84f8a46f5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12032, "upload_time": "2014-08-20T06:50:18", "url": "https://files.pythonhosted.org/packages/c4/70/4a06431cea222b1951327a13de781686e79f09ca30b0772e4497a38ddaed/pg_simple-0.2.3.tar.gz" } ] }