Python Standard Library: Data Storage 10-1. Clemens Szyperski, in Component Software



Similar documents
Python. Data bases. Marcin Młotkowski. 8th May, 2013

Sorting. Lists have a sort method Strings are sorted alphabetically, except... Uppercase is sorted before lowercase (yes, strange)

Python Standard Library: Data Representation 4-1

An Embedded Wireless Mini-Server with Database Support

Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

Open source framework for interactive data exploration in server based architecture

Outline. mass storage hash functions. logical key values nested tables. storing information between executions using DBM files

Outline. multiple choice quiz bottom-up design. the modules main program: quiz.py namespaces in Python

Copyright 2013 wolfssl Inc. All rights reserved. 2

Writing Thesis Defense Papers

Writing MySQL Scripts With Python's DB-API Interface

6.S096 Lecture 1 Introduction to C

Exercise 1: Python Language Basics

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Flask-SSO Documentation

How to represent characters?

Main Bullet #1 Main Bullet #2 Main Bullet #3

AES Crypt User Guide

DocBook Framework (DBF)

Installing Microsoft SQL Server Linux ODBC Driver For Use With Kognitio Analytical Platform

B&K Precision 1785B, 1786B, 1787B, 1788 Power supply Python Library

Randy Hyde s Win32 Assembly Language Tutorials (Featuring HOWL) #4: Radio Buttons

How-To Guide. Crystal Report Demo. Copyright Topaz Systems Inc. All rights reserved.

Apache Thrift and Ruby

Case Studies. Joint software development Mail 1 / 38. Case Studies Joint Software Development. Mailers

Send TLM. Table of contents

USER GUIDE. Snow Inventory Client for Unix Version Release date Document date

Getting Started with IVI-COM and Python for the Lambda Genesys Power Supply

PYTHON IN A NUTSHELL. O'REILLY Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo. Alex Martelli. Second Edition

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

CME 193: Introduction to Scientific Python Lecture 8: Unit testing, more modules, wrap up

PYTHON Basics

Introduction to Python

Implementing Voice Over Wireless Networks: Realities and Challenges

Evaluation Checklist Data Warehouse Automation

Introduction to: Computers & Programming: Input and Output (IO)

Python for Series 60 Platform

Introduction to Python

SyncTool for InterSystems Caché and Ensemble.

Working with HPC and HTC Apps. Abhinav Thota Research Technologies Indiana University

Table of Contents. The RCS MINI HOWTO

Cross-platform event logging in Object Pascal

Lab 2 : Basic File Server. Introduction

This document presents the new features available in ngklast release 4.4 and KServer 4.2.

socketio Documentation

CS 103 Lab Linux and Virtual Machines

Specifications of Paradox for Windows

HP Server Automation. Software Versions: 7.8x, 9.0x, 9.1x. Software Discovery Reference Guide

NaviCell Data Visualization Python API

Introduction. dnotify

SUGI 29 Coders' Corner

pyownet Documentation

Developing tests for the KVM autotest framework

Practical Google App Engine Applications in Python

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Writing Scripts with PHP s PEAR DB Module

ISIS Application Program Interface ISIS_DLL User s Manual. Preliminary Version

Database Extension 1.5 ez Publish Extension Manual

Programming Exercises

An Oracle Technical Article November Certification with Oracle Linux 6

RED HAT SOFTWARE COLLECTIONS BRIDGING DEVELOPMENT AGILITY AND PRODUCTION STABILITY

Data Intensive Computing Handout 5 Hadoop

Open Source Used In Cisco D9865 Satellite Receiver Software Version 2.20

Introduction to Python

Case Study: Access control 1 / 39

Hadoop Streaming. Table of contents

Supported Platforms HPE Vertica Analytic Database. Software Version: 7.2.x

1. Third Party Software or Free Software License Information

django-cron Documentation

Supported Platforms. HP Vertica Analytic Database. Software Version: 7.1.x

LISTSERV LDAP Documentation

APScheduler Documentation

Using the Studio Source Control Hooks

PowerShell. It s time to own. David Kennedy (ReL1K) Josh Kelley (Winfang) Twitter: dave_rel1k

Zenoss Core ZenUp Installation and Administration

Writing MySQL Scripts with Python DB-API

Code Estimation Tools Directions for a Services Engagement

Building a Basic Communication Network using XBee DigiMesh. Keywords: XBee, Networking, Zigbee, Digimesh, Mesh, Python, Smart Home

Authoring for System Center 2012 Operations Manager

1001ICT Introduction To Programming Lecture Notes

Some Experiences With Python For Android (Py4A) Nik Klever University of Applied Sciences Augsburg

C# and Other Languages

WebSphere Commerce V7 Feature Pack 3

Exercise 4 Learning Python language fundamentals

32-Bit Workload Automation 5 for Windows on 64-Bit Windows Systems

GNU LIBRARY GENERAL PUBLIC LICENSE. Preamble

Developing LPF s Data Management Unit

Modern Web Application Framework Python, SQL Alchemy, Jinja2 & Flask

Lecture 13: Message Authentication Codes

Synchronizing databases

An Oracle Technical Article March Certification with Oracle Linux 7

Archiving, Indexing and Accessing Web Materials: Solutions for large amounts of data

Self-review 9.3 What is PyUnit? PyUnit is the unit testing framework that comes as standard issue with the Python system.

Transcription:

Python Standard Library: Data Storage 10-1 Data Storage "Unlike mainstream component programming, scripts usually do not introduce new components but simply "wire" existing ones. Scripts can be seen as introducing behavior but no new state. /.../ Of course, there is nothing to stop a "scripting" language from introducing persistent state it then simply turns into a normal programming language" Clemens Szyperski, in Component Software Overview Python comes with drivers for a number of very similar database managers, all modeled after Unix's dbm library. These databases behaves like ordinary dictionaries, with the exception that you can only use strings for keys and values (the shelve module can handle any kind of value). Python Standard Library Copyright (c) 1999-2003 by Fredrik Lundh. All rights reserved.

Python Standard Library: Data Storage 10-2 The anydbm module This module provides a unified interface to the simple database drivers supported by Python. The first time it is imported, the anydbm module looks for a suitable database driver, testing for dbhash, gdbm, dbm, or dumbdbm, in that order. If no such module is found, it raises an ImportError exception. The open function is used to open or create a database, using the chosen database handler. Example: Using the anydbm module # File:anydbm-example-1.py import anydbm db = anydbm.open("database", "c") db["1"] = "one" db["2"] = "two" db["3"] = "three" db = anydbm.open("database", "r") '2' 'two' '3' 'three' '1' 'one'

Python Standard Library: Data Storage 10-3 The whichdb module This module can be used to figure out which database handler that was used for a given database file. Example: Using the whichdb module # File:whichdb-example-1.py import whichdb filename = "database" result = whichdb.whichdb(filename) if result: print "file created by", result handler = import (result) db = handler.open(filename, "r") print db.keys() else: # cannot identify data base if result is None: print "cannot read database file", filename else: print "cannot identify database file", filename db = None This example used the import function to import a module with the given name.

Python Standard Library: Data Storage 10-4 The shelve module This module uses the database handlers to implement persistent dictionaries. A shelve object uses string keys, but the value can be of any data type, as long as it can be handled by the pickle module. Example: Using the shelve module # File:shelve-example-1.py import shelve db = shelve.open("database", "c") db["one"] = 1 db["two"] = 2 db["three"] = 3 db = shelve.open("database", "r") 'one' 1 'three' 3 'two' 2 The following example shows how to use the shelve module with a given database driver. Example: Using the shelve module with a given database # File:shelve-example-3.py import shelve import gdbm def gdbm_shelve(filename, flag="c"): return shelve.shelf(gdbm.open(filename, flag)) db = gdbm_shelve("dbfile")

Python Standard Library: Data Storage 10-5 The dbhash module (Optional). This module provides a dbm-compatible interface to the bsddb database handler. Example: Using the dbhash module # File:dbhash-example-1.py import dbhash db = dbhash.open("dbhash", "c") db["one"] = "the foot" db["two"] = "the shoulder" db["three"] = "the other foot" db["four"] = "the bridge of the nose" db["five"] = "the naughty bits" db["six"] = "just above the elbow" db["seven"] = "two inches to the right of a very naughty bit indeed" db["eight"] = "the kneecap" db = dbhash.open("dbhash", "r")

Python Standard Library: Data Storage 10-6 The dbm module (Optional). This module provides an interface to the dbm database handler (available on many Unix platforms). Example: Using the dbm module # File:dbm-example-1.py import dbm db = dbm.open("dbm", "c") db["first"] = "bruce" db["second"] = "bruce" db["third"] = "bruce" db["fourth"] = "bruce" db["fifth"] = "michael" db["fifth"] = "bruce" # overwrite db = dbm.open("dbm", "r") 'first' 'bruce' 'second' 'bruce' 'fourth' 'bruce' 'third' 'bruce' 'fifth' 'bruce'

Python Standard Library: Data Storage 10-7 The dumbdbm module This is a very simple database implementation, similar to dbm and friends, but written in pure Python. It uses two files; a binary file (.dat) which contains the data, and a text file (.dir) which contain data descriptors. Example: Using the dumbdbm module # File:dumbdbm-example-1.py import dumbdbm db = dumbdbm.open("dumbdbm", "c") db["first"] = "fear" db["second"] = "surprise" db["third"] = "ruthless efficiency" db["fourth"] = "an almost fanatical devotion to the Pope" db["fifth"] = "nice red uniforms" db = dumbdbm.open("dumbdbm", "r") 'first' 'fear' 'third' 'ruthless efficiency' 'fifth' 'nice red uniforms' 'second' 'surprise' 'fourth' 'an almost fanatical devotion to the Pope'

Python Standard Library: Data Storage 10-8 The gdbm module (Optional). This module provides an interface to the GNU dbm database handler. Example: Using the gdbm module # File:gdbm-example-1.py import gdbm db = gdbm.open("gdbm", "c") db["1"] = "call" db["2"] = "the" db["3"] = "next" db["4"] = "defendant" db = gdbm.open("gdbm", "r") keys = db.keys() keys.sort() for key in keys: print db[key], call the next defendant