Skip to content

Instantly share code, notes, and snippets.

@jacktator
Created June 1, 2019 23:49
Show Gist options
  • Save jacktator/8ccd3cee2f192d0c09c3e581529b64ee to your computer and use it in GitHub Desktop.
Save jacktator/8ccd3cee2f192d0c09c3e581529b64ee to your computer and use it in GitHub Desktop.
This file defines a NamedTuple for TradeDay
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"outputs": [],
"source": "from typing import NamedTuple\nfrom datetime import date\n\n\"\"\"\nNamedTuple for storing prices\n\"\"\"\nclass Prices(NamedTuple(\"Prices\", \n [(\u0027open\u0027, float), \n (\u0027high\u0027, float), \n (\u0027low\u0027, float), \n (\u0027close\u0027, float)])):\n pass\n\"\"\"\nNamedTuple for storing a TradeDay\n\"\"\"\nclass TradeDay(NamedTuple(\"TradeDay\", \n ((\"symbol\", str), \n (\"date\", date), \n (\"prices\", Prices)))):\n @property\n def change(self):\n return round(\n (self.prices.close - self.prices.open) / self.prices.open, \n 4)\n",
"metadata": {
"pycharm": {
"metadata": false,
"name": "#%% This file defines a NamedTuple for TradeDay\n",
"is_executing": false
}
}
},
{
"cell_type": "code",
"execution_count": 22,
"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": 22
}
],
"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": 23,
"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 namedtuple.\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.01 µs ± 17.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\nTime it takes to read \u0027day\u0027 object is: \n",
"26.9 ns ± 0.307 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\nTime it takes to read \u0027day.prices\u0027 dict attribute is: \n",
"75.8 ns ± 0.749 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\nTime it takes to execute \u0027day.change()\u0027 function is: \n",
"946 ns ± 15 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n\n------ Size Report ------\n\nThe Size of \u0027day\u0027 object: 80 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 namedtuple.\")\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(\"\\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": "",
"metadata": {
"pycharm": {
"metadata": false,
"name": "#%%"
}
}
}
],
"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"
},
"stem_cell": {
"cell_type": "raw",
"source": "",
"metadata": {
"pycharm": {
"metadata": false
}
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment