What is it?

PupDB is an ernest attempt to create a simple file-based key-value database written in Python.

Why PupDB?

The objective behind the creation of PupDB is to create a database system which performs simple persistence operations well and data can be accessed with a minimalist, easy-to-use API with least configuration.

PupDB is the best choice when:

You need a simple NoSQL data store with an interface as simple as a Python dict, and want to start storing and retrieving data within a few minutes.

You want to create an application without bothering much about configuration and setup for storing data.

Your database is not very huge i.e. not greater than a few megabytes of data.

When not to use PupDB:

You want to perform advanced queries on your data.

Your database is larger than a few megabytes of data.

You want a database software that supports advanced capabilities like indexing, partitioning etc.

Salient Features

Multi-processing support: Same database file can be used across multiple processes concurrently.

Mult-threading support: Same database file (with separate PupDB instance per thread) can be used concurrently.

REST-based HTTP Interface: Apart from using it as a python package, you can also use PupDB via a flask-based HTTP interface. This way you can use PupDB with programming languages other than Python.

Installation

PupDB can be installed using pip:

pip install pupdb

Basic API Documentation and Usage

PupDB can be instantiated as follows:

from pupdb.core import PupDB

# Specify database file path as an argument to the PupDB constructor. That's it.

db = PupDB('db.json')

set(key, value): Stores the value mapped to key in the database file.

db.set('test_key', 'test_value')

get(key): Returns the value mapped to key in the database file. Returns None if key is not found.

db.get('test_key')

remove(key): Removes the key from the database file. Raises a KeyError if key is not found in the database file.

# Remove the key `test_key` from the db.

db.remove('test_key')

# Try doing the same again and it'll raise a `KeyError`,

# as the key has already been deleted from the db in the above step.

db.remove('test_key')

keys(): Returns the keys present in the database file. Return type is list in Python 2 and Dictionary view object (similar to dict.keys()) in Python 3.

# Python 2

print db.keys() # This will print ['key1', ...]

# Python 3

print(list(db.keys())) # This will print ['key1', ...]

values(): Returns the values of all keys present in the database file. Return type is list for Python 2 and Dictionary view object (similar to dict.values()) in Python 3.

# Python 2

print db.values() # This will print ['value1', ...]

# Python 3

print(list(db.values())) # This will print ['value1', ...]

items(): Returns the values of all keys present in the database file. Return type is list for Python 2 and Dictionary view object (similar to dict.items()) in Python 3.

# Python 2

print db.items() # This will print [('key1', 'value1'), ...]

# Python 3

print(list(db.items())) # This will print [('key1', 'value1'), ...]

dumps(): Returns a json dump of the entire database file sorted by key.

db.dumps() # This will print the database json string.

truncate_db(): Removes all data from the database file i.e. truncates the database file.

db.truncate_db()

print(db) # Will print an empty database dict '{}', as the database has been truncated.

Using the PupDB HTTP/REST Interface

Using the HTTP/REST Interface, all PupDB-related operations can be performed without using PupDB as a Python package. As a result, PupDB can be used in any programming language that can make HTTP requests.

To start PupDB's gunicorn-based flask server:

from pupdb.server import start_http_server

# Start the gunicorn server (with 4 worker threads).

start_http_server()

The server will listen to local port 4000. The server will be available at http://localhost:4000.

HTTP API Endpoints

/get?key= (Method: GET): This API endpoint is an interface to PupDB's get() method. e.g.:

curl -XGET http://localhost:4000/get?key=test

The above curl request will fetch the result for key test.

/set (Method: POST): This API endpoint is an interface to PupDB's set() method. e.g.:

curl -XPOST http://localhost:4000/set -H 'Content-Type: application/json' -d '{"key": "test", "value": "1234"}'

The above curl request will set the value 1234 to key test in the database.

/remove/ (Method: DELETE): This API endpoint is an interface to PupDB's remove() method. e.g.:

curl -XDELETE http://localhost:4000/remove/test

The above curl request will remove the key test in the database. It returns a 404 Not Found if the key does not exist in the database.

/keys (Method: GET): This API endpoint is an interface to PupDB's keys() method. e.g.:

curl -XGET http://localhost:4000/keys

The above curl request will return a payload containing the list of keys in the database.

/values (Method: GET): This API endpoint is an interface to PupDB's values() method. e.g.:

curl -XGET http://localhost:4000/values

The above curl request will return a payload containing the list of values of all keys in the database.

/items (Method: GET): This API endpoint is an interface to PupDB's items() method. e.g.:

curl -XGET http://localhost:4000/items

The above curl request will return a payload containing the list of [key, value] pairs in the database.

/dumps (Method: GET): This API endpoint is an interface to PupDB's dumps() method. e.g.:

curl -XGET http://localhost:4000/dumps

The above curl request will return a payload containing the string dump of the entire database.

/truncate-db (Method: POST): This API endpoint is an interface to PupDB's truncate_db() method. e.g.:

curl -XPOST http://localhost:4000/truncate-db

The above curl request will truncate i.e. remove all key-value pairs from the database.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE.txt file for more details.

python 嵌入键值数据库_PupDB 一个用Python编写基于文件的简单键值数据库相关推荐

  1. 基于单例模式的多键值序列号生成器实现(支持缓存)

    之前在BlogJava上发表过这篇文章,那时没怎么做整理.想不到已经有博友把它弄到ITEye来了(虽然写得水平差,但还是希望那位博友能注明是转载的).这次稍作了整理,自己也加深下印象. 使用场景:用于 ...

  2. 聊天室(C语言)- 基于文件编程、网络通信、数据库实现

    聊天室(C语言) 源码连接 https://download.csdn.net/download/G1842965496/83970608 项目面试问题 C/C++/嵌入式-面试题汇总 --基于文件编 ...

  3. 给定key值,在Binary Search Tree中查找最接近该键值的结点集合

    http://blog.csdn.net/zhouhao011280s/article/details/8044056 给定key值,在Binary Search Tree中查找最接近该键值的结点集合 ...

  4. python使用del保留字定义一个函数-python中自定义函数的保留字是

    基本使用(推荐学习:Python视频教程)def function_name(parameters): expressions Python使用def开始函数定义,紧接着是函数名,括号内部为函数的参数 ...

  5. python使用del保留字定义一个函数-Python使用什么保留字定义一个函数。

    [单选题]Where was the First Continental Congress held? [单选题]对于集合S和T,下列不属于集合类型的操作是() [判断题]UPS系统的静态开关用于保护 ...

  6. python发明小故事_一个与python有关的故事

    背景 近期系统需求上来迟缓,也腾出了一些时间来梳理沉淀.对系统的代码进行审视,sonar静态检测,消除检测问题.也是还债的过程.回顾过往的坑,有些东西还是要沉淀下来.接下来节听听故事的来龙去脉. 一个 ...

  7. Python零基础学习 | 分享一个使用Python画出太极阴阳八卦图的代码

    大家好,我是王某人. 一.写在前面 前几天在Python技术交流群有个小伙伴分享一个使用Python画出太极阴阳八卦图的代码,这里拿出来给大家分享下,一起学习下. 不过这里他的代码是有点问题的,不过不 ...

  8. Mysql数据库基础系列(二):表结构、键值

    表结构 约束条件 查看约束条件 mysql> desc 库名.表名; 字段名----| 类型---------------| 空------ |键值-- |默认值---- |额外设置 | 设置约 ...

  9. mysql一个表中可以有几个主键_数据库的单个数据表中有几个主键?

    数据库的单个数据表中只能有一个主键.数据库主键,指的是一个列或多列的组合,其值能唯一地标识表中的每一行,通过它可强制表的实体完整性:主键主要是用与其他表的外键关联,以及本记录的修改与删除. 数据库的单 ...

最新文章

  1. 【Flask】数据的CRUD之增加和查询操作
  2. JS实现各种复制到剪贴板
  3. 在电脑搭建oracle服务器端,oracle 服务器端搭建(C#开发环境)
  4. nginx php exec,PHP Web 端如何操作 Nginx 配置
  5. 渗透测试入门5之内网信息搜集
  6. 数字信号处理6:IIR滤波器设计
  7. 如何使用Idea导入jar包
  8. 使用python中正则表达式匹配linux ifconifg命令中的具体信息
  9. 用友nc6.5详细安装过程
  10. 解决ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we cannot accuratel
  11. 网站分析实战——如何以数据驱动决策,提升网站价值(大数据时代的分析利器)
  12. Buy and Resell HDU - 6438 贪心
  13. 利用PHP HTML5 MySQL 将表单提交的数据写到数据库
  14. rocksdb 备份backup
  15. proxy代理报错: Uncaught (in promise) Error: Request failed with status code 500
  16. GPS卫星运动及定位matlab仿真
  17. 使用腾讯云函数每天定时签到领取京豆
  18. MySQL多表操作:建表与多表查询
  19. Google Chrome,是由Google开发的,一款设计非常简单、十分高效的Web浏览工具!
  20. 传导 耦合RF 发射功率,灵敏度 国标

热门文章

  1. 什么样的数据集可以被分成两类?
  2. 谷歌身份验证器验证码不对怎么回事_地面最强下载器:IDM+【安卓】详细介绍...
  3. P2 Matlab计算基础-《Matlab/Simulink与控制系统仿真》程序指令总结
  4. 完美解决 keil5.25 某宝Jlink无法使用问题
  5. 【PC工具】更新图片批量处理工具Image Tuner,绿色免安装工具软件
  6. 十一、Linux时间
  7. 云计算怎么支撑起《流浪地球》的硬科幻实力?
  8. 阿里云助力重庆打造“亚洲最智能大型城市”
  9. tango with django(第三章 Django基础)
  10. 服务器负载均衡的部署方式