Skip to content

Commit c12bd23

Browse files
updated for py3
1 parent bb61524 commit c12bd23

File tree

1 file changed

+47
-58
lines changed

1 file changed

+47
-58
lines changed

Sources/source/persistance_serialization.rst

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ In theory, ``repr()`` always gives a form that can be re-constructed.
7272

7373
Often ``str()`` form works too.
7474

75-
``pprint`` (pretty print) module can make it easier to read.
75+
``pprint`` (pretty print) module can make it easier to read:
76+
77+
https://docs.python.org/3.5/library/pprint.html
7678

7779
Python Literal Example
7880
----------------------
@@ -91,7 +93,7 @@ Python Literal Example
9193
9294
You can save the string to a file and even use ``import``
9395

94-
(NOTE: ``ast.literal_eval`` is safer than eval)
96+
(NOTE: ``ast.literal_eval`` is safer than eval: https://docs.python.org/3.5/library/ast.html#ast-helpers)
9597

9698
pretty print
9799
------------
@@ -102,7 +104,7 @@ pretty print
102104
In [71]: repr(data)
103105
Out[71]: "[{'this': 5, 'that': 4}, {'eggs': 3.4, 'spam': 7}, {'foo': 86, 'bar': 4.5}, {'fun': 43, 'baz': 6.5}]"
104106
In [72]: s = pprint.pformat(data)
105-
In [73]: print s
107+
In [73]: print(s)
106108
[{'that': 4, 'this': 5},
107109
{'eggs': 3.4, 'spam': 7},
108110
{'bar': 4.5, 'foo': 86},
@@ -116,17 +118,9 @@ Pickle is a binary format for python objects
116118

117119
You can essentially dump any python object to disk (or string, or socket, or...
118120

119-
``cPickle`` is faster than pickle, but
120-
can't be customized -- you usually want ``cPickle``
121-
122-
http://docs.python.org/library/pickle.html
123-
124-
125-
.. nextslide::
126-
127121
.. code-block:: ipython
128122
129-
In [87]: import cPickle as pickle
123+
In [87]: import pickle
130124
In [83]: data
131125
Out[83]:
132126
[{'that': 4, 'this': 5},
@@ -138,8 +132,7 @@ http://docs.python.org/library/pickle.html
138132
In [86]: data2 == data
139133
Out[86]: True
140134
141-
142-
http://docs.python.org/library/pickle.html
135+
https://docs.python.org/3.5/library/pickle.html
143136

144137
Shelve
145138
------
@@ -148,14 +141,13 @@ A "shelf" is a persistent, dictionary-like object.
148141

149142
(It's also a place you can put a jar of pickles...)
150143

151-
The values (not the keys!) can be essentially arbitrary Python
152-
objects (anything picklable)
144+
The values (not the keys!) can be essentially arbitrary Python objects (anything picklable)
153145

154146
NOTE: will not reflect changes in mutable objects without re-writing them to the db. (or use writeback=True)
155147

156148
If less that 100s of MB -- just use a dict and pickle it.
157149

158-
http://docs.python.org/library/shelve.html
150+
https://docs.python.org/3.5/library/shelve.html
159151

160152

161153
.. nextslide::
@@ -173,15 +165,15 @@ http://docs.python.org/library/shelve.html
173165
flag = d.has_key(key) # true if the key exists
174166
d.close() # close it
175167
176-
http://docs.python.org/library/shelve.html
168+
https://docs.python.org/3.5/library/shelve.html
177169

178170
LAB
179171
---
180172

181173
Here are two datasets embedded in Python:
182174

183-
:download:`address book data <../code/persistance/add_book_data.py>`
184-
:download:`flat address book data <../code/persistance/add_book_data.py>`
175+
:download:`add_book_data.py <../code/persistance/add_book_data.py>`
176+
:download:`add_book_data_flat.py <../code/persistance/add_book_data_flat.py>`
185177

186178
They can be loaded with::
187179

@@ -234,14 +226,14 @@ Writing ``ini`` files:
234226

235227
.. code-block:: ipython
236228
237-
import ConfigParser
238-
config = ConfigParser.ConfigParser()
229+
import configparser
230+
config = configparser.ConfigParser()
239231
config.add_section('Section1')
240232
config.set('Section1', 'int', '15')
241233
config.set('Section1', 'bool', 'true')
242234
config.set('Section1', 'float', '3.1415')
243235
# Writing our configuration file to 'example.cfg'
244-
config.write( open('example.cfg', 'wb') )
236+
config.write(open('example.cfg', 'w'))
245237
246238
Note: all keys and values are strings
247239

@@ -251,7 +243,7 @@ Reading ``ini`` files:
251243

252244
.. code-block:: ipython
253245
254-
>>> config = ConfigParser.ConfigParser()
246+
>>> config = configparser.ConfigParser()
255247
>>> config.read('example.cfg')
256248
>>> config.sections()
257249
['Section1', 'Section2']
@@ -261,7 +253,7 @@ Reading ``ini`` files:
261253
[('int', '15'), ('bool', 'true'), ('float', '3.1415')]
262254
263255
264-
http://docs.python.org/library/configparser.html
256+
https://docs.python.org/3.5/library/configparser.html
265257

266258
CSV
267259
---
@@ -292,7 +284,7 @@ Reading ``CSV`` files:
292284
293285
``csv`` module takes care of string quoting, etc. for you
294286

295-
http://docs.python.org/library/csv.html
287+
https://docs.python.org/3.5/library/csv.html
296288

297289
.. nextslide::
298290

@@ -301,15 +293,16 @@ Writing ``CSV`` files:
301293
.. code-block:: python
302294
303295
>>> import csv
304-
>>> spamWriter = csv.writer(open('eggs.csv', 'wb'),
296+
>>> outfile = open('eggs.csv', 'w')
297+
>>> spam_writer = csv.writer(outfile,
305298
quoting=csv.QUOTE_MINIMAL)
306-
>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
307-
>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
299+
>>> spam_writer.writerow(['Spam'] * 5 + ['Baked Beans'])
300+
>>> spam_writer.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
308301
309302
310303
``csv`` module takes care of string quoting, etc for you
311304

312-
http://docs.python.org/library/csv.html
305+
https://docs.python.org/3.5/library/csv.html
313306

314307
JSON
315308
----
@@ -326,7 +319,7 @@ Presents a similar interface as ``pickle``
326319

327320
http://www.json.org/
328321

329-
http://docs.python.org/library/json.html
322+
https://docs.python.org/3.5/library/json.html
330323

331324
Python json module
332325
------------------
@@ -346,9 +339,9 @@ Python json module
346339
Out[98]: True # they are the same
347340
348341
349-
(also ``json.dump() and json.load()`` for files
342+
(also ``json.dump() and json.load()`` for files)
350343

351-
http://docs.python.org/library/json.html
344+
https://docs.python.org/3.5/library/json.html
352345

353346
XML
354347
---
@@ -368,7 +361,7 @@ XML in the python std lib
368361

369362
``xml.etree``
370363

371-
http://docs.python.org/library/xml.etree.elementtree.html
364+
https://docs.python.org/3.5/library/xml.etree.elementtree.html
372365

373366
elementtree
374367
-----------
@@ -381,17 +374,18 @@ an ``ElementTree`` is an entire XML doc
381374

382375
an ``Element`` is a node in that tree
383376

384-
http://docs.python.org/library/xml.etree.elementtree.html}
377+
https://docs.python.org/3.5/library/xml.etree.elementtree.html
385378

386379
LAB
387380
---
388381

382+
Use the same addressbook data:
383+
389384
::
390385

391386
# load with:
392387
from add_book_data import AddressBook
393388

394-
395389
They have address book data -- one with a nested dict, one "flat"
396390

397391
* Write a module that saves the data as an INI file
@@ -420,22 +414,23 @@ DataBases
420414
anydbm
421415
------
422416

423-
``anydbm`` is a generic interface to variants of the DBM database
417+
``dbm`` is a generic interface to variants of the DBM database
424418

425419
Suitable for storing data that fits well into a python dict with strings as both keys and values
426420

427-
Note: anydbm will use the dbm system that works on your system -- this may be different on different systems -- so the db files may NOT be compatible! ``whichdb`` will try to figure it out, but it's not guaranteed
421+
Note: dbm will use the dbm system that works on your system -- this may be different on different systems -- so the db files may NOT be compatible! ``whichdb`` will try to figure it out, but it's not guaranteed
428422

429-
http://docs.python.org/library/anydbm.html
423+
https://docs.python.org/3.5/library/dbm.html
430424

431-
anydbm module
425+
dbm module
432426
-------------
433427
Writing data:
434428

435429
::
436430

437431
#creating a dbm file:
438-
anydbm.open(filename, 'n')
432+
import dbm
433+
dbm.open(filename, 'n')
439434

440435

441436
flag options are:
@@ -445,7 +440,7 @@ flag options are:
445440
* 'c' -- Open database for reading and writing, creating it if it doesn’t exist
446441
* 'n' -- Always create a new, empty database, open for reading and writing
447442

448-
http://docs.python.org/library/anydbm.html
443+
https://docs.python.org/3.5/library/dbm.html
449444

450445
anydbm module
451446
-------------
@@ -463,7 +458,7 @@ anydbm module
463458
# read it:
464459
db = dbm.open("dbm", "r")
465460
for key in db.keys():
466-
print key, db[key]
461+
print(key, db[key])
467462

468463

469464

@@ -493,8 +488,7 @@ Allows (and requires) SQL queries
493488

494489
Can provide high performance, flexible, portable storage for your app
495490

496-
http://docs.python.org/library/sqlite3.html
497-
491+
https://docs.python.org/3.5/library/sqlite3.html
498492

499493
.. nextslide::
500494

@@ -510,7 +504,7 @@ Example:
510504
# create a cursor
511505
c = conn.cursor()
512506

513-
http://docs.python.org/library/sqlite3.html
507+
https://docs.python.org/3.5/library/sqlite3.html
514508

515509
python sqlite module
516510
--------------------
@@ -520,8 +514,7 @@ Execute SQL with the cursor:
520514
::
521515

522516
# Create table
523-
c.execute("'CREATE TABLE stocks
524-
(date text, trans text, symbol text, qty real, price real)"')
517+
c.execute("'CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)'")
525518
# Insert a row of data
526519
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
527520
# Save (commit) the changes
@@ -530,8 +523,7 @@ Execute SQL with the cursor:
530523
c.close()
531524

532525

533-
534-
http://docs.python.org/library/sqlite3.html
526+
https://docs.python.org/3.5/library/sqlite3.html
535527

536528
python sqlite module
537529
--------------------
@@ -542,8 +534,8 @@ python sqlite module
542534

543535
>>> for row in c.execute('SELECT * FROM stocks ORDER BY price'):
544536
print row
545-
(u'2006-01-05', u'BUY', u'RHAT', 100, 35.14)
546-
(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0)
537+
('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
538+
('2006-03-28', 'BUY', 'IBM', 1000, 45.0)
547539
...
548540

549541

@@ -573,13 +565,12 @@ Good idea to use the DB-API’s parameter substitution:
573565
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
574566

575567

576-
577568
http://xkcd.com/327/
578569

579570
DB-API
580571
------
581572

582-
The DB-API spec (PEP 249) is a specification for interaction between Python and Relational Databases.}
573+
The DB-API spec (PEP 249) is a specification for interaction between Python and Relational Databases.
583574

584575
Support for a large number of third-party Database drivers:
585576

@@ -616,11 +607,9 @@ Object Databases
616607

617608
Directly store and retrieve Python Objects.
618609

619-
Kind of like ``shelve`` , but more flexible, and give you searching, etc.
620-
621-
ZODB: (http://www.zodb.org/
610+
Kind of like ``shelve``, but more flexible, and give you searching, etc.
622611

623-
Durus: (https://www.mems-exchange.org/software/DurusWorks/})
612+
ZODB: (http://www.zodb.org/)
624613

625614
NoSQL
626615
-----

0 commit comments

Comments
 (0)