Storage and persistence of Python data

Source: Internet
Author: User
Tags class definition dbase pow table definition

Python's data persistence operations are primarily six classes: normal files, dbm files, pickled object storage, shelve object storage, object database storage, relational database storage.

Ordinary files do not explain, dbm is the string of key value pairs stored in the file:

Python code

% python  >>> import anydbm                             >>> file = Anydbm.open (' movie ', ' C ')        # make a DBM file called ' Mo Vie '  >>> file[' Batman '] = ' pow! '                 # Store a string under key ' Batman '  >>> File.keys ()                                 # Get the file ' s key directory  [' Batman ']  &G t;>> file[' Batman '                          # Fetch value for key ' Batman '  pow! '

Pickled is the serialization of objects into files that can store complex types:

Python code

% python  >>> table = {' A ': [1, 2, 3],               ' B ': [' spam ', ' eggs '],               ' C ': {' name ': ' Bob '}}  >>>< c4/>>>> Import Pickle  >>> mydb  = open (' dbase ', ' W ')  >>> pickle.dump (table, MyDB

The following is deserialization:

Python code

% python  >>> import pickle  >>> mydb  = open (' dbase ', ' R ')  >>> table = Pickle.load (mydb)  >>> table  {' B ': [' spam ', ' eggs '], ' a ': [1, 2, 3], ' C ': {' name ': ' Bob '}}

Shelve storage is almost a combination of dbm and pickled that serializes objects into files in the form of key-value pairs:

Python code

% python  >>> import shelve  >>> dbase = Shelve.open ("MyDBase")  >>> object1 = [' The ', ' Bright ', (' side ', ' of '), [' Life ']]  >>> object2 = {' name ': ' Brian ', ' age ':, ' motto ': Object1}  &G T;>> dbase[' brian ']  = object2  >>> dbase[' Knight '] = {' name ': ' Knight ', ' motto ': ' ni! '}  >>> Dbase.close ()

Fetch data:

Python code

% python  >>> import shelve  >>> dbase = Shelve.open ("MyDBase")  >>> Len (dbase) c11/># Entries  2  >>> Dbase.keys ()                          # index  [' Knight ', ' Brian ']  >>> dbase[ ' Knight ']                        # fetch  {' motto ': ' ni! ', ' name ': ' Knight '}

The storage of the object database is not well understood, because it is not used to store data. The feeling should be similar to shelve, just save the data in the database (in fact, it is a file), and then add some more advanced features such as transactions.

The storage of relational database in Python is the focus, the most "simple" operation of relational database is directly using DB-API, like JDBC in Java; Of course, the data structure is complex, the design requirements are high, we need to find some ORM framework lazy, mainly have independent sqlalchemy, Django's self-bringing ORM and so on. This part of the content is still the next blog to write, I do not like the article pulled long ...

Simple comparison of Python data Persistence operations (II)

The most straightforward way to manipulate relational databases in Python is to use DB-API, which typically connects, executes SQL statements, commits, disconnects. In MySQL, for example, here are the code examples for each step:

The first is the connection:

Python code

% python  >>> import mysqldb  >>> conn = mysqldb.connect (host= ' localhost ', user= ' root ', passwd = ' python ')

You can then execute the statement, but get the pointer before executing the SQL statement:

Python code

>>> curs = conn.cursor ()  >>> curs.execute (' Create Database Peopledb ')  1L  >>> Curs.execute (' use Peopledb ')  0L  >>> tblcmd = ' CREATE table people (name char (), Job char (TEN), Pay int (4 )) '  >>> curs.execute (tblcmd)  0L

Add Data:

Python code

>>> Curs.execute (' Insert people values (%s,%s,%s) ', (' Bob ', ' dev ', ') ')  1L  >>> Curs.executemany (' Insert people values (%s,%s,%s) ',  ...          [(' Sue ', ' mus ', ' 70000 '),  ...            (' Ann ', ' mus ', ' 60000 ')])  2L  >>> Conn.commit ()

To execute a query:

Python code

>>> curs.execute (' select * from people ')  6L  >>> curs.fetchall ('  Bob ', ' Dev ', 5000L) , (' Sue ', ' mus ', 70000L), (' Ann ', ' mus ', 60000L), (' Tom ',  ' Mgr ', 100000L))

After performing the database operation, remember to disconnect:

Python code

Conn.close ()        # Close, _ _del_ _ call rollback If changes not committed yet

If the data structure is not very complex, it is convenient to work with Python's powerful list parsing capabilities without an ORM framework, or it is not difficult to encapsulate object mappings yourself.

If you use the Django framework, you can use its own ORM tool to manipulate the database. The first, of course, is to write the entity class (or model):

Python code

From django.db import Models  class musician (models. Model):      first_name = models. Charfield (max_length=50)      last_name = models. Charfield (max_length=50)      instrument = models. Charfield (max_length=100)  class Album (models. Model):      artist = models. ForeignKey (musician)      name = models. Charfield (max_length=100)      release_date = models. Datefield ()      Num_stars = models. Integerfield ()

The Python code is already clear, the class corresponds to the table, the member variables correspond to the columns of the table, and the column properties are models. Xxxfield (...) Defined. If the entity class does not explicitly define a primary key, Django adds a default sentence:

Python code

id = models. Autofield (Primary_key=true)

In Django, you can define enumerated data like this:

Python code

Class person (models. Model):      gender_choices = (          (u ' M ', U ' Male '),          (U ' F ', U ' Female '),      )      name = models. Charfield (max_length=60)      gender = models. Charfield (max_length=2, choices=gender_choices)

For association relationships, this can be written when you make a mapping definition for a column:

Python code

Poll = models. ForeignKey (Poll)  sites = models. Manytomanyfield (Site) Place  = models. Onetoonefield (Place ")

There's more to defining relationships in Django, and more on official documents.

The basic database operation has already been defined in the Django model base class, because all entity classes inherit from the model class, and so are the operations. For example, creating and saving a person only needs to do this:

Python code

>>> p = person (name= "Fred flinstone", gender= "M")  >>> P.save ()

Django determines whether the update or insert is made by querying the existence of the primary key of the object, and you can, of course, force the framework to perform some kind of action. If you are not satisfied with the method that the frame comes with, you can rewrite it:

Python code

Class Blog (models. Model):      name = models. Charfield (max_length=100)      tagline = models. TextField ()      def save (self, *args, **kwargs):          do_something ()          super (Blog, self). Save (*args, **kwargs) # Call the "real" Save () method.          Do_something_else ()

Found no, Django Access data does not need that kind of session, the most annoying hibernate in the session, always reported "Session Closed" error ...

Python also has a separate ORM framework,--sqlalchemy. More powerful and supports more databases than Django's own ORM tools. It has two methods for establishing entity classes.

One is to separate the definitions, and then map the table definition and the class definition. The first is to create a table definition:

Python code

>>> from SQLAlchemy import Table, Column, Integer, String, MetaData, ForeignKey  >>> MetaData = Metad ATA ()  >>> users_table = Table (' Users ', metadata,  ...     Column (' id ', Integer, Sequence (' User_id_seq '), primary_key=true),  ...     Column (' name ', String ()),  ...     Column (' FullName ', String ()),  ...     Column (' Password ', String ())  ...)

Then define the entity class:

Python code

>>> class User (object):  ...     def __init__ (self, name, fullname, password):  ...         Self.name = name ...         Self.fullname = FullName ...         Self.password = password

It's not over yet, and they're going to be mapped out:

Python code

>>> from sqlalchemy.orm Import mapper  >>> mapper (User, users_table)

This process is a bit like the mapping of XML map files and entity classes in Hibernate. Hibernate also makes it easy to use annotations directly in the entity class to complete the mapping with the table, of course SQLAlchemy also has a direct approach:

Python code

>>> from sqlalchemy.ext.declarative import declarative_base  >>> base = Declarative_base ()  >>> class User (Base):  ...     __tablename__ = ' users '  ...  ...     id = Column (Integer, primary_key=true) ...     Name = Column (String) ...     FullName = Column (String) ...     Password = Column (String)

As a standalone ORM framework, the access of the entity classes will of course not be as perfect as Django, and access to the data in SQLAlchemy is also a session:

Python code

>>> from sqlalchemy.orm import sessionmaker  >>> Session = Sessionmaker (bind=engine)

The engine object here needs to be set up like this:

Python code

>>> from SQLAlchemy import create_engine  >>> engine = Create_engine (' <span style= ' font-family : monospace; White-space:normal; Color: #333333; line-height:20px; " >dialect+driver://user:[email Protected]/dbname[?key=value ...] </span> ', echo=true)

For the access operation, if it is saved, write this:

Python code

>>> ed_user = User (' Ed ', ' Ed Jones ', ' Edspassword ')  >>> session.add (ed_user)

If you want to query, it is similar to this form:

Python code

>>> Our_user = session.query (user). Filter_by (name= ' Ed '). First ()

Perform some data operations, commit or rollback when necessary:

Python code

>>> session.rollback ()  or  >>> session.commit ()

SQLAlchemy Framework also has a derivative product--elixir, on the basis of sqlalchemy on its mapping way to do some encapsulation, so that the definition of the entity class is somewhat similar to the way Django defines.

The Django Orm is very close to its other modules, and it's not a good idea to use it alone; Although the sqlalchemy is strong, but the style is not very like, so the next step is to drill down into two ORM framework code to see how they are implemented. On the one hand a good choice to use which, in addition to see in their own application can make a simple orm.

Storage and persistence of Python data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.