Created
May 18, 2016 14:59
-
-
Save fndari/bc4d724a52c9d4b1152ea1a2e5a5ffed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from tinydb import TinyDB\n", | |
"from tinydb.storages import Storage, touch\n", | |
"\n", | |
"import yaml\n", | |
"import sys" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class TestYamlStorage(Storage):\n", | |
" \n", | |
" \"\"\"\n", | |
" Store the data in a YAML file.\n", | |
" Written following the example at http://tinydb.readthedocs.io/en/latest/extend.html#write-a-custom-storage\n", | |
" \"\"\"\n", | |
" def __init__(self, filename): # (1)\n", | |
" super().__init__()\n", | |
" self.filename = filename\n", | |
" touch(filename)\n", | |
"\n", | |
" def read(self):\n", | |
" with open(self.filename) as handle:\n", | |
" try:\n", | |
" # data = ruamel.yaml.load(handle, ruamel.yaml.RoundTripLoader)\n", | |
" # data = yaml.safe_load(handle.read())\n", | |
" data = yaml.load(handle.read())\n", | |
" return data\n", | |
" except yaml.YAMLError:\n", | |
" return None # (3)\n", | |
"\n", | |
" def write(self, data):\n", | |
"# print('writing data: {}'.format(data))\n", | |
" with open(self.filename, 'w') as handle:\n", | |
" yaml.dump(data, stream=sys.stdout)\n", | |
" yaml.dump(data, handle)\n", | |
" # ruamel.yaml.dump(data, stream=handle, Dumper=ruamel.yaml.RoundTripDumper)\n", | |
"\n", | |
" def close(self): # (4)\n", | |
" pass\n", | |
"\n", | |
"\n", | |
"def flush(f):\n", | |
" import os\n", | |
" # flush existing file for consistency\n", | |
" try:\n", | |
" os.remove(f)\n", | |
" except FileNotFoundError:\n", | |
" pass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"dicts = [\n", | |
" dict(name='Homer', age=38),\n", | |
" dict(name='Marge', age=34),\n", | |
" dict(name='Bart', age=10)\n", | |
"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_default: {}\n", | |
"_default:\n", | |
" 1: {age: 38, name: Homer}\n", | |
" 2: {age: 34, name: Marge}\n", | |
" 3: {age: 10, name: Bart}\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'age': 38, 'name': 'Homer'},\n", | |
" {'age': 34, 'name': 'Marge'},\n", | |
" {'age': 10, 'name': 'Bart'}]" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"flush('db.yaml')\n", | |
"db = TinyDB('db.yaml', storage=TestYamlStorage)\n", | |
"# this works as expected\n", | |
"db.insert_multiple(dicts)\n", | |
"db.all()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_default:\r\n", | |
" 1: {age: 38, name: Homer}\r\n", | |
" 2: {age: 34, name: Marge}\r\n", | |
" 3: {age: 10, name: Bart}\r\n" | |
] | |
} | |
], | |
"source": [ | |
"%cat db.yaml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_default: {}\n", | |
"_default: {}\n" | |
] | |
} | |
], | |
"source": [ | |
"flush('db.yaml')\n", | |
"db = TinyDB('db.yaml', storage=TestYamlStorage)\n", | |
"%cat db.yaml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_default:\n", | |
" 1: {age: 38, name: Homer}\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'age': 38, 'name': 'Homer'}]" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"db.insert(dicts[0])\n", | |
"db.all()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_default:\n", | |
" 1: !!python/object/new:tinydb.database.Element\n", | |
" dictitems: {age: 38, name: Homer}\n", | |
" state: {eid: 1}\n", | |
" 2: {age: 34, name: Marge}\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[{'age': 38, 'name': 'Homer'}, {'age': 34, 'name': 'Marge'}]" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"db.insert(dicts[1])\n", | |
"db.all()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_default:\r\n", | |
" 1: !!python/object/new:tinydb.database.Element\r\n", | |
" dictitems: {age: 38, name: Homer}\r\n", | |
" state: {eid: 1}\r\n", | |
" 2: {age: 34, name: Marge}\r\n" | |
] | |
} | |
], | |
"source": [ | |
"%cat db.yaml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.3+" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The documentation in TinyDB has been updated to fix this issue. https://github.com/msiemens/tinydb/blob/master/docs/extend.rst