Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "import time\n",
- "import uuid\n",
- "\n",
- "import numpy as np\n",
- "\n",
- "from aioinflux import *\n",
- "from typing import NamedTuple"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "\n",
- "### User-defined class serialization"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [],
- "source": [
- "@lineprotocol\n",
- "class Trade(NamedTuple):\n",
- " timestamp: TIMEINT\n",
- " instrument: TAGENUM\n",
- " source: TAG\n",
- " side: TAG\n",
- " price: FLOAT\n",
- " size: INT\n",
- " trade_id: STR"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "Trade(timestamp=1550627795266834000, instrument='APPL', source='reuters', side='BUY', price=123, size=1000, trade_id='abc45f')"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "t = Trade(time.time_ns(), 'APPL', 'reuters', 'BUY', 123, 1000, 'abc45f')\n",
- "t"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "b'Trade,instrument=APPL,source=reuters,side=BUY price=123,size=1000i,trade_id=\"abc45f\" 1550627795266834000'"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "t.to_lineprotocol()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Dataframe serialization"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>instrument</th>\n",
- " <th>side</th>\n",
- " <th>price</th>\n",
- " <th>size</th>\n",
- " <th>trade_id</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>2017-01-01 00:00:00+00:00</th>\n",
- " <td>AAPL</td>\n",
- " <td>SELL</td>\n",
- " <td>99.97</td>\n",
- " <td>400</td>\n",
- " <td>7b5be89d-a899-4a73-a526-26763f99022e</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2017-01-01 00:01:00+00:00</th>\n",
- " <td>GOOG</td>\n",
- " <td>SELL</td>\n",
- " <td>100.04</td>\n",
- " <td>1400</td>\n",
- " <td>04a6d821-66c9-48da-8d2f-31a4db98853c</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2017-01-01 00:02:00+00:00</th>\n",
- " <td>GOOG</td>\n",
- " <td>SELL</td>\n",
- " <td>100.06</td>\n",
- " <td>1800</td>\n",
- " <td>0193b477-5d75-4ca0-8f38-2039f122eaec</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2017-01-01 00:03:00+00:00</th>\n",
- " <td>AAPL</td>\n",
- " <td>BUY</td>\n",
- " <td>100.11</td>\n",
- " <td>1800</td>\n",
- " <td>37431179-6bfd-4b58-8d8b-45563d03531b</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2017-01-01 00:04:00+00:00</th>\n",
- " <td>AAPL</td>\n",
- " <td>BUY</td>\n",
- " <td>100.15</td>\n",
- " <td>1200</td>\n",
- " <td>56659872-0999-4104-8b36-0e86ba957a57</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2017-01-01 00:05:00+00:00</th>\n",
- " <td>AAPL</td>\n",
- " <td>BUY</td>\n",
- " <td>100.11</td>\n",
- " <td>100</td>\n",
- " <td>28a27f38-2771-49b4-abfd-bf03cbc1f283</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2017-01-01 00:06:00+00:00</th>\n",
- " <td>AAPL</td>\n",
- " <td>SELL</td>\n",
- " <td>100.04</td>\n",
- " <td>100</td>\n",
- " <td>2a2961f5-4cd0-4704-904c-ee542474824e</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2017-01-01 00:07:00+00:00</th>\n",
- " <td>GOOG</td>\n",
- " <td>SELL</td>\n",
- " <td>100.03</td>\n",
- " <td>800</td>\n",
- " <td>b9cc70bf-45f4-4a7e-b450-7798a4ced63c</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2017-01-01 00:08:00+00:00</th>\n",
- " <td>AAPL</td>\n",
- " <td>BUY</td>\n",
- " <td>99.98</td>\n",
- " <td>1800</td>\n",
- " <td>4475f446-282c-4913-9406-f0f9e863c1d6</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2017-01-01 00:09:00+00:00</th>\n",
- " <td>GOOG</td>\n",
- " <td>SELL</td>\n",
- " <td>100.00</td>\n",
- " <td>400</td>\n",
- " <td>b69eb931-e4f3-4389-9a2b-d20bf4dee256</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " instrument side price size \\\n",
- "2017-01-01 00:00:00+00:00 AAPL SELL 99.97 400 \n",
- "2017-01-01 00:01:00+00:00 GOOG SELL 100.04 1400 \n",
- "2017-01-01 00:02:00+00:00 GOOG SELL 100.06 1800 \n",
- "2017-01-01 00:03:00+00:00 AAPL BUY 100.11 1800 \n",
- "2017-01-01 00:04:00+00:00 AAPL BUY 100.15 1200 \n",
- "2017-01-01 00:05:00+00:00 AAPL BUY 100.11 100 \n",
- "2017-01-01 00:06:00+00:00 AAPL SELL 100.04 100 \n",
- "2017-01-01 00:07:00+00:00 GOOG SELL 100.03 800 \n",
- "2017-01-01 00:08:00+00:00 AAPL BUY 99.98 1800 \n",
- "2017-01-01 00:09:00+00:00 GOOG SELL 100.00 400 \n",
- "\n",
- " trade_id \n",
- "2017-01-01 00:00:00+00:00 7b5be89d-a899-4a73-a526-26763f99022e \n",
- "2017-01-01 00:01:00+00:00 04a6d821-66c9-48da-8d2f-31a4db98853c \n",
- "2017-01-01 00:02:00+00:00 0193b477-5d75-4ca0-8f38-2039f122eaec \n",
- "2017-01-01 00:03:00+00:00 37431179-6bfd-4b58-8d8b-45563d03531b \n",
- "2017-01-01 00:04:00+00:00 56659872-0999-4104-8b36-0e86ba957a57 \n",
- "2017-01-01 00:05:00+00:00 28a27f38-2771-49b4-abfd-bf03cbc1f283 \n",
- "2017-01-01 00:06:00+00:00 2a2961f5-4cd0-4704-904c-ee542474824e \n",
- "2017-01-01 00:07:00+00:00 b9cc70bf-45f4-4a7e-b450-7798a4ced63c \n",
- "2017-01-01 00:08:00+00:00 4475f446-282c-4913-9406-f0f9e863c1d6 \n",
- "2017-01-01 00:09:00+00:00 b69eb931-e4f3-4389-9a2b-d20bf4dee256 "
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "index = pd.date_range('2017-01-01', '2017-01-02', tz='UTC', freq='1min')\n",
- "n = len(index)\n",
- "\n",
- "df = pd.DataFrame(\n",
- " index=index,\n",
- " data={\n",
- " 'instrument': np.random.choice(['AAPL', 'GOOG'], n),\n",
- " 'side': np.random.choice(['BUY', 'SELL'], n),\n",
- " 'price': (100 + np.random.normal(0, 0.05, n).cumsum()).round(2),\n",
- " 'size': np.random.randint(1, 20, n) * 100,\n",
- " 'trade_id': [str(uuid.uuid4()) for _ in range(n)],\n",
- " }\n",
- ")\n",
- "df.head(10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "b'Trade,instrument=GOOG,side=SELL price=100.02,size=900i,trade_id=\"cf5478d4-2bea-42e1-bbd2-dcba0c34d51b\" 1483228800000000000\\nTrade,instrument=GOOG,side=BUY price=99.99,size=1500i,trade_id=\"79a1389f-c5a6-4c85-9a6e-68f19bafda95\" 1483228860000000000\\nTrade,instrument=AAPL,side=BUY price=99.99,size=1900i,trade_id=\"3268644c-ed6b-4809-969d-aed1db673909\" 1483228920000000000\\nTrade,instrument=GOOG,side=BUY price=100.07,size=700i,trade_id=\"66272f59-470e-4848-9384-a3ebdb96bfb4\" 1483228980000000000\\nTrade,instrument=GOOG,side=BUY price=100.04,size=500i,trade_id=\"5d420b35-7748-45c8-aeda-2aa656964648\" 1483229040000000000\\nTrade,instrument=GOOG,side=BUY price=100.03,size=400i,trade_id=\"0d54595f-209c-4b8b-9825-a5bc366d5184\" 1483229100000000000\\nTrade,instrument=GOOG,side=SELL price=100.1,size=300i,trade_id=\"5eff4e31-b808-466c-84ec-ebbff2bd67c2\" 1483229160000000000\\nTrade,instrument=GOOG,side=SELL price=100.09,size=1200i,trade_id=\"23897414-26ec-400d-b742-4ce266d431d7\" 1483229220000000000\\nTrade,instrument=GOOG'"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "serialization.dataframe.serialize(df, 'Trade', tag_columns=['instrument', 'side'])[:1000]"
- ]
- },
- {
- "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.7.2"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement