Created
June 1, 2019 23:49
-
-
Save jacktator/46782d6aa62c65f22f7c61334e884abb to your computer and use it in GitHub Desktop.
This file defines a DataClass for TradeDay
This file contains 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": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true, | |
"pycharm": { | |
"name": "#%% This file defines a DataClass for TradeDay\n", | |
"is_executing": false | |
} | |
}, | |
"outputs": [], | |
"source": "from dataclasses import dataclass\nfrom datetime import date\n\n@dataclass\nclass Prices: \n \"\"\"\n DataClass for storing prices\n \"\"\"\n open: float\n high: float\n low: float\n close: float\n\n@dataclass\nclass TradeDay:\n \"\"\"\n DataClass for storing a TradeDay\n \"\"\"\n symbol: str\n date: date\n prices: Prices\n change \u003d lambda self : round(\n (self.prices.close - self.prices.open) / self.prices.open, \n 4)\n" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "TradeDay(symbol\u003d\u0027MA\u0027, date\u003ddatetime.date(2019, 6, 1), prices\u003dPrices(open\u003d10.0, high\u003d30.0, low\u003d5.0, close\u003d20.0))" | |
}, | |
"metadata": {}, | |
"output_type": "execute_result", | |
"execution_count": 9 | |
} | |
], | |
"source": "import datetime\n\nday: TradeDay \u003d TradeDay(\"MA\", datetime.date.today(), Prices(10.0, 30.0, 5.0, 20.0))\n\nday\n", | |
"metadata": { | |
"pycharm": { | |
"metadata": false, | |
"name": "#%% Usage Example\n", | |
"is_executing": false | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"text": [ | |
"\u003d\u003d\u003d\u003d\u003d\u003d Performance Report \u003d\u003d\u003d\u003d\u003d\u003d\n\nThis report shows the performance of storing and accessing a data object as a dataclasses.\nTradeDay(symbol\u003d\u0027MA\u0027, date\u003ddatetime.date(2019, 6, 1), prices\u003dPrices(open\u003d10.0, high\u003d30.0, low\u003d5.0, close\u003d20.0))\n\n------ Speed Report -----\n\nTime it takes to create \u0027day\u0027 object is: \n", | |
"2.34 µs ± 19.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\nTime it takes to read \u0027day\u0027 object is: \n", | |
"24.7 ns ± 0.162 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\nTime it takes to read \u0027day.prices\u0027 dict attribute is: \n", | |
"52.1 ns ± 0.611 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\nTime it takes to execute \u0027day.change()\u0027 function is: \n", | |
"821 ns ± 12.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n\n------ Size Report ------\n\nThe Size of \u0027day\u0027 object: 56 bytes\nThe Size of \u0027day\u0027 dict: 112 bytes\n\n\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d End of Report \u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\n" | |
], | |
"output_type": "stream" | |
} | |
], | |
"source": "import datetime\nimport sys\n\nday: TradeDay \u003d TradeDay(\"MA\", datetime.date.today(), Prices(10.0, 30.0, 5.0, 20.0))\n\nprint(\"\u003d\u003d\u003d\u003d\u003d\u003d Performance Report \u003d\u003d\u003d\u003d\u003d\u003d\\n\")\nprint(\"This report shows the performance of storing and accessing a data object as a dataclasses.\")\nprint(day)\n\nprint(\"\\n------ Speed Report -----\\n\")\nprint(\"Time it takes to create \u0027day\u0027 object is: \")\n%timeit day: TradeDay \u003d TradeDay(\"MA\", datetime.date.today(), Prices(10.0, 30.0, 5.0, 20.0))\nprint(\"Time it takes to read \u0027day\u0027 object is: \")\n%timeit day\nprint(\"Time it takes to read \u0027day.prices\u0027 dict attribute is: \")\n%timeit day.prices\nprint(\"Time it takes to execute \u0027day.change()\u0027 function is: \")\n%timeit day.change()\n\nprint(\"\\n------ Size Report ------\\n\")\nprint(\"The Size of \u0027day\u0027 object: {} bytes\".format(sys.getsizeof(day)))\nprint(\"The Size of \u0027day\u0027 dict: {} bytes\".format(sys.getsizeof(vars(day))))\nprint(\"\\n\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d End of Report \u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\")\n", | |
"metadata": { | |
"pycharm": { | |
"metadata": false, | |
"name": "#%% Performance Test\n", | |
"is_executing": false | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": "\n", | |
"metadata": { | |
"pycharm": { | |
"metadata": false, | |
"name": "#%%\n" | |
} | |
} | |
} | |
], | |
"metadata": { | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.6" | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"language": "python", | |
"display_name": "Python 3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment