Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.79 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import time\n",
  10. "import uuid\n",
  11. "\n",
  12. "import numpy as np\n",
  13. "\n",
  14. "from aioinflux import *\n",
  15. "from typing import NamedTuple"
  16. ]
  17. },
  18. {
  19. "cell_type": "markdown",
  20. "metadata": {},
  21. "source": [
  22. "\n",
  23. "### User-defined class serialization"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": 6,
  29. "metadata": {},
  30. "outputs": [],
  31. "source": [
  32. "@lineprotocol\n",
  33. "class Trade(NamedTuple):\n",
  34. " timestamp: TIMEINT\n",
  35. " instrument: TAGENUM\n",
  36. " source: TAG\n",
  37. " side: TAG\n",
  38. " price: FLOAT\n",
  39. " size: INT\n",
  40. " trade_id: STR"
  41. ]
  42. },
  43. {
  44. "cell_type": "code",
  45. "execution_count": 8,
  46. "metadata": {},
  47. "outputs": [
  48. {
  49. "data": {
  50. "text/plain": [
  51. "Trade(timestamp=1550627795266834000, instrument='APPL', source='reuters', side='BUY', price=123, size=1000, trade_id='abc45f')"
  52. ]
  53. },
  54. "execution_count": 8,
  55. "metadata": {},
  56. "output_type": "execute_result"
  57. }
  58. ],
  59. "source": [
  60. "t = Trade(time.time_ns(), 'APPL', 'reuters', 'BUY', 123, 1000, 'abc45f')\n",
  61. "t"
  62. ]
  63. },
  64. {
  65. "cell_type": "code",
  66. "execution_count": 9,
  67. "metadata": {},
  68. "outputs": [
  69. {
  70. "data": {
  71. "text/plain": [
  72. "b'Trade,instrument=APPL,source=reuters,side=BUY price=123,size=1000i,trade_id=\"abc45f\" 1550627795266834000'"
  73. ]
  74. },
  75. "execution_count": 9,
  76. "metadata": {},
  77. "output_type": "execute_result"
  78. }
  79. ],
  80. "source": [
  81. "t.to_lineprotocol()"
  82. ]
  83. },
  84. {
  85. "cell_type": "markdown",
  86. "metadata": {},
  87. "source": [
  88. "### Dataframe serialization"
  89. ]
  90. },
  91. {
  92. "cell_type": "code",
  93. "execution_count": 7,
  94. "metadata": {},
  95. "outputs": [
  96. {
  97. "data": {
  98. "text/html": [
  99. "<div>\n",
  100. "<style scoped>\n",
  101. " .dataframe tbody tr th:only-of-type {\n",
  102. " vertical-align: middle;\n",
  103. " }\n",
  104. "\n",
  105. " .dataframe tbody tr th {\n",
  106. " vertical-align: top;\n",
  107. " }\n",
  108. "\n",
  109. " .dataframe thead th {\n",
  110. " text-align: right;\n",
  111. " }\n",
  112. "</style>\n",
  113. "<table border=\"1\" class=\"dataframe\">\n",
  114. " <thead>\n",
  115. " <tr style=\"text-align: right;\">\n",
  116. " <th></th>\n",
  117. " <th>instrument</th>\n",
  118. " <th>side</th>\n",
  119. " <th>price</th>\n",
  120. " <th>size</th>\n",
  121. " <th>trade_id</th>\n",
  122. " </tr>\n",
  123. " </thead>\n",
  124. " <tbody>\n",
  125. " <tr>\n",
  126. " <th>2017-01-01 00:00:00+00:00</th>\n",
  127. " <td>AAPL</td>\n",
  128. " <td>SELL</td>\n",
  129. " <td>99.97</td>\n",
  130. " <td>400</td>\n",
  131. " <td>7b5be89d-a899-4a73-a526-26763f99022e</td>\n",
  132. " </tr>\n",
  133. " <tr>\n",
  134. " <th>2017-01-01 00:01:00+00:00</th>\n",
  135. " <td>GOOG</td>\n",
  136. " <td>SELL</td>\n",
  137. " <td>100.04</td>\n",
  138. " <td>1400</td>\n",
  139. " <td>04a6d821-66c9-48da-8d2f-31a4db98853c</td>\n",
  140. " </tr>\n",
  141. " <tr>\n",
  142. " <th>2017-01-01 00:02:00+00:00</th>\n",
  143. " <td>GOOG</td>\n",
  144. " <td>SELL</td>\n",
  145. " <td>100.06</td>\n",
  146. " <td>1800</td>\n",
  147. " <td>0193b477-5d75-4ca0-8f38-2039f122eaec</td>\n",
  148. " </tr>\n",
  149. " <tr>\n",
  150. " <th>2017-01-01 00:03:00+00:00</th>\n",
  151. " <td>AAPL</td>\n",
  152. " <td>BUY</td>\n",
  153. " <td>100.11</td>\n",
  154. " <td>1800</td>\n",
  155. " <td>37431179-6bfd-4b58-8d8b-45563d03531b</td>\n",
  156. " </tr>\n",
  157. " <tr>\n",
  158. " <th>2017-01-01 00:04:00+00:00</th>\n",
  159. " <td>AAPL</td>\n",
  160. " <td>BUY</td>\n",
  161. " <td>100.15</td>\n",
  162. " <td>1200</td>\n",
  163. " <td>56659872-0999-4104-8b36-0e86ba957a57</td>\n",
  164. " </tr>\n",
  165. " <tr>\n",
  166. " <th>2017-01-01 00:05:00+00:00</th>\n",
  167. " <td>AAPL</td>\n",
  168. " <td>BUY</td>\n",
  169. " <td>100.11</td>\n",
  170. " <td>100</td>\n",
  171. " <td>28a27f38-2771-49b4-abfd-bf03cbc1f283</td>\n",
  172. " </tr>\n",
  173. " <tr>\n",
  174. " <th>2017-01-01 00:06:00+00:00</th>\n",
  175. " <td>AAPL</td>\n",
  176. " <td>SELL</td>\n",
  177. " <td>100.04</td>\n",
  178. " <td>100</td>\n",
  179. " <td>2a2961f5-4cd0-4704-904c-ee542474824e</td>\n",
  180. " </tr>\n",
  181. " <tr>\n",
  182. " <th>2017-01-01 00:07:00+00:00</th>\n",
  183. " <td>GOOG</td>\n",
  184. " <td>SELL</td>\n",
  185. " <td>100.03</td>\n",
  186. " <td>800</td>\n",
  187. " <td>b9cc70bf-45f4-4a7e-b450-7798a4ced63c</td>\n",
  188. " </tr>\n",
  189. " <tr>\n",
  190. " <th>2017-01-01 00:08:00+00:00</th>\n",
  191. " <td>AAPL</td>\n",
  192. " <td>BUY</td>\n",
  193. " <td>99.98</td>\n",
  194. " <td>1800</td>\n",
  195. " <td>4475f446-282c-4913-9406-f0f9e863c1d6</td>\n",
  196. " </tr>\n",
  197. " <tr>\n",
  198. " <th>2017-01-01 00:09:00+00:00</th>\n",
  199. " <td>GOOG</td>\n",
  200. " <td>SELL</td>\n",
  201. " <td>100.00</td>\n",
  202. " <td>400</td>\n",
  203. " <td>b69eb931-e4f3-4389-9a2b-d20bf4dee256</td>\n",
  204. " </tr>\n",
  205. " </tbody>\n",
  206. "</table>\n",
  207. "</div>"
  208. ],
  209. "text/plain": [
  210. " instrument side price size \\\n",
  211. "2017-01-01 00:00:00+00:00 AAPL SELL 99.97 400 \n",
  212. "2017-01-01 00:01:00+00:00 GOOG SELL 100.04 1400 \n",
  213. "2017-01-01 00:02:00+00:00 GOOG SELL 100.06 1800 \n",
  214. "2017-01-01 00:03:00+00:00 AAPL BUY 100.11 1800 \n",
  215. "2017-01-01 00:04:00+00:00 AAPL BUY 100.15 1200 \n",
  216. "2017-01-01 00:05:00+00:00 AAPL BUY 100.11 100 \n",
  217. "2017-01-01 00:06:00+00:00 AAPL SELL 100.04 100 \n",
  218. "2017-01-01 00:07:00+00:00 GOOG SELL 100.03 800 \n",
  219. "2017-01-01 00:08:00+00:00 AAPL BUY 99.98 1800 \n",
  220. "2017-01-01 00:09:00+00:00 GOOG SELL 100.00 400 \n",
  221. "\n",
  222. " trade_id \n",
  223. "2017-01-01 00:00:00+00:00 7b5be89d-a899-4a73-a526-26763f99022e \n",
  224. "2017-01-01 00:01:00+00:00 04a6d821-66c9-48da-8d2f-31a4db98853c \n",
  225. "2017-01-01 00:02:00+00:00 0193b477-5d75-4ca0-8f38-2039f122eaec \n",
  226. "2017-01-01 00:03:00+00:00 37431179-6bfd-4b58-8d8b-45563d03531b \n",
  227. "2017-01-01 00:04:00+00:00 56659872-0999-4104-8b36-0e86ba957a57 \n",
  228. "2017-01-01 00:05:00+00:00 28a27f38-2771-49b4-abfd-bf03cbc1f283 \n",
  229. "2017-01-01 00:06:00+00:00 2a2961f5-4cd0-4704-904c-ee542474824e \n",
  230. "2017-01-01 00:07:00+00:00 b9cc70bf-45f4-4a7e-b450-7798a4ced63c \n",
  231. "2017-01-01 00:08:00+00:00 4475f446-282c-4913-9406-f0f9e863c1d6 \n",
  232. "2017-01-01 00:09:00+00:00 b69eb931-e4f3-4389-9a2b-d20bf4dee256 "
  233. ]
  234. },
  235. "execution_count": 7,
  236. "metadata": {},
  237. "output_type": "execute_result"
  238. }
  239. ],
  240. "source": [
  241. "index = pd.date_range('2017-01-01', '2017-01-02', tz='UTC', freq='1min')\n",
  242. "n = len(index)\n",
  243. "\n",
  244. "df = pd.DataFrame(\n",
  245. " index=index,\n",
  246. " data={\n",
  247. " 'instrument': np.random.choice(['AAPL', 'GOOG'], n),\n",
  248. " 'side': np.random.choice(['BUY', 'SELL'], n),\n",
  249. " 'price': (100 + np.random.normal(0, 0.05, n).cumsum()).round(2),\n",
  250. " 'size': np.random.randint(1, 20, n) * 100,\n",
  251. " 'trade_id': [str(uuid.uuid4()) for _ in range(n)],\n",
  252. " }\n",
  253. ")\n",
  254. "df.head(10)"
  255. ]
  256. },
  257. {
  258. "cell_type": "code",
  259. "execution_count": 5,
  260. "metadata": {},
  261. "outputs": [
  262. {
  263. "data": {
  264. "text/plain": [
  265. "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'"
  266. ]
  267. },
  268. "execution_count": 5,
  269. "metadata": {},
  270. "output_type": "execute_result"
  271. }
  272. ],
  273. "source": [
  274. "serialization.dataframe.serialize(df, 'Trade', tag_columns=['instrument', 'side'])[:1000]"
  275. ]
  276. },
  277. {
  278. "cell_type": "code",
  279. "execution_count": null,
  280. "metadata": {},
  281. "outputs": [],
  282. "source": []
  283. }
  284. ],
  285. "metadata": {
  286. "kernelspec": {
  287. "display_name": "Python 3",
  288. "language": "python",
  289. "name": "python3"
  290. },
  291. "language_info": {
  292. "codemirror_mode": {
  293. "name": "ipython",
  294. "version": 3
  295. },
  296. "file_extension": ".py",
  297. "mimetype": "text/x-python",
  298. "name": "python",
  299. "nbconvert_exporter": "python",
  300. "pygments_lexer": "ipython3",
  301. "version": "3.7.2"
  302. }
  303. },
  304. "nbformat": 4,
  305. "nbformat_minor": 2
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement