Created
May 29, 2018 09:05
-
-
Save amitkot/0e39cc035a52999d6ecd0a0021e17504 to your computer and use it in GitHub Desktop.
Cropping orders by timestamp from Redis RDB dump
This file contains hidden or 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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Cropping orders from Redis DB dump\n", | |
"\n", | |
"## First, dump the data\n", | |
"use [redis-dump](https://github.com/delano/redis-dump):\n", | |
"\n", | |
" $ redis-dump > db_full.json\n", | |
"\n", | |
"## Then, use python to select specific timestamps:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import json\n", | |
"\n", | |
"with open('/tmp/rdb/db_full.json') as f:\n", | |
" records = json.load(f)\n", | |
" d = records[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"149318" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"len(d)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'binance_bat_eth_1518224660000'" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(iter(d))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def key_in_range(key, begin, end):\n", | |
" timestamp = int(key.split('_')[-1])\n", | |
" return timestamp in range(begin, end)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dd = {\n", | |
" key: value\n", | |
" for key, value in d.items()\n", | |
" if key_in_range(key, 1518233090000, 1518233100001)\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"160" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"len(dd)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with open('/tmp/rdb/relevant_orders.json', 'w') as f:\n", | |
" json.dump(dd, f)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"total 1866576\r\n", | |
"-rw-r--r-- 1 amit wheel 524217 May 28 16:02 Untitled.ipynb\r\n", | |
"-rw-r--r-- 1 amit wheel 954635077 May 28 15:39 db_full.json\r\n", | |
"-rw-r--r-- 1 amit wheel 520511 May 28 16:03 relevant_orders.json\r\n", | |
"drwxr-xr-x 7 amit wheel 224 May 28 15:38 \u001b[34mvenv\u001b[m\u001b[m/\r\n" | |
] | |
} | |
], | |
"source": [ | |
"ls -l /tmp/rdb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"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.6.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment