实现考虑¶
- 文件也是个流,os中持久的流,文件路径和文件名是流的id
其他存储也应该是个持久化的流,需要一个流的id,
文件流是顺序的字符串流,读取也只能按照顺序读
- 对象流是顺序的key,value顺序集合,按照key读取,不按照时间读取
- 钉钉和微信的wenhook也是流
- 本地文件方便备份和复制,系统路径不需要考虑路径
- 普通对象可以不考虑目录,合并成key。特俗分类对象数据需要有目录分类清晰
- value是dataframe时的优化
deva实践¶
In [1]:
from deva import *
NODB全局命名对象数据库¶
- 支持独特数据表名
- 支持日志
- 支持自定义存储的文件路径,默认在库目录
创建和插入数据¶
In [2]:
testdb = NODB('testdb',log=log)
{'key':'value'}>>testdb
('key1','value1')>>testdb
'todayvlue'>>testdb
Out[2]:
全局命名统一¶
In [3]:
ndb = NODB('testdb',log=log)
ndb is testdb
Out[3]:
像字典一样查看和删除数据¶
In [4]:
testdb.items()>>ls
# testdb.tablename
Out[4]:
In [5]:
testdb.get('key')
Out[5]:
In [6]:
del testdb['key1']
当前目录下保持数据库文件¶
In [7]:
tmpdb = NODB('tmp1',fname='tmp1',log=log)
tmpdb['tablename']
Out[7]:
In [8]:
123>>tmpdb
Out[8]:
In [9]:
123>>tmpdb
2345>>tmpdb
33>>tmpdb
{'name':'tal','desc':'好未来','df':[1,2,3]}>>tmpdb
('hello','moto')>>tmpdb
{'key':'value'}>>tmpdb
Out[9]:
HDFStore单独对DF数据存储优化¶
In [10]:
import pandas as pd
from sqlalchemy import create_engine
import sqlalchemy
engine = create_engine('mysql+pymysql://root:root@localhost/shejishi?charset=utf8')
df = pd.read_sql('select * from pins limit 3', engine)
# df=df.set_index('id')
# make sure emp_master_backup table has been created
# so the table schema is what we want
# df.to_sql('emp_backup', engine, index=True ,if_exists='replace')
from deva.store import HDFStore
hs = HDFStore()
hs.daily_set('/stock/tal/',df)
# hs.daily_read('/stock/tal')
Out[10]:
In [11]:
hs.keys()
Out[11]:
In [12]:
hs.daily_get('/stock/tal')
Out[12]:
In [13]:
hs.keys()
Out[13]:
In [14]:
hs.set('mykey',df)
hs.get('mykey')
Out[14]:
文件流¶
写入¶
In [15]:
123123>>write_to_file('tmp.txt')
Out[15]:
读取¶
In [16]:
'tmp.txt'>>read>>ls
Out[16]:
In [17]:
range(10)>>head(3)>>tee>>ls
Out[17]:
函数流¶
range sum 等常见函数同时支持pipe¶
In [18]:
range<<6>>ls
Out[18]:
In [19]:
range<<6>>sum
Out[19]:
钉钉和微信流¶
In [20]:
range<<10>>tee>>sum>>Dtalk(webhook='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f5110ae1-bb4a-4e58-a87c-6ccafa3c8d25')
Out[20]:
In [21]:
cth = Dtalk(webhook='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f5110ae1-bb4a-4e58-a87c-6ccafa3c8d25')