Last active
March 16, 2024 00:22
-
-
Save antimon2/24bd252590482088809dce7362aa3ce7 to your computer and use it in GitHub Desktop.
MyTimes.py.ipynb
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": [ | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.025014Z", | |
| "end_time": "2024-03-16T00:16:10.054359Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "c916eb00", | |
| "cell_type": "code", | |
| "source": "import sys\nsys.version", | |
| "execution_count": 1, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "execution_count": 1, | |
| "data": { | |
| "text/plain": "'3.11.8 (main, Mar 15 2024, 14:45:52) [GCC 8.3.0]'" | |
| }, | |
| "metadata": {} | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.060660Z", | |
| "end_time": "2024-03-16T00:16:10.064268Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "f87a5731", | |
| "cell_type": "code", | |
| "source": "from abc import ABC, abstractmethod", | |
| "execution_count": 2, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": {}, | |
| "id": "e6b7e4a1", | |
| "cell_type": "markdown", | |
| "source": "## `AbstractTime`" | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.066307Z", | |
| "end_time": "2024-03-16T00:16:10.071270Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "1c87188c", | |
| "cell_type": "code", | |
| "source": "class AbstractTime(ABC):\n @property\n @abstractmethod\n def hour(self) -> int:\n pass\n\n @property\n @abstractmethod\n def minute(self) -> int:\n pass\n\n @property\n @abstractmethod\n def second(self) -> int:\n pass\n\n def __str__(self) -> str:\n return \"{:02d}:{:02d}:{:02d}\".format(self.hour, self.minute, self.second)\n", | |
| "execution_count": 3, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": {}, | |
| "id": "518b98f2", | |
| "cell_type": "markdown", | |
| "source": "## `MyTime`" | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.073185Z", | |
| "end_time": "2024-03-16T00:16:10.079123Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "b45252ba", | |
| "cell_type": "code", | |
| "source": "class MyTime(AbstractTime):\n def __init__(self, hour, minute, second) -> 'MyTime':\n self._hour = hour\n self._minute = minute\n self._second = second\n\n @property\n def hour(self) -> int:\n return self._hour\n\n @property\n def minute(self) -> int:\n return self._minute\n\n @property\n def second(self) -> int:\n return self._second\n", | |
| "execution_count": 4, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.081827Z", | |
| "end_time": "2024-03-16T00:16:10.084150Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "2b764f21", | |
| "cell_type": "code", | |
| "source": "mytime = MyTime(14, 28, 57)", | |
| "execution_count": 5, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.085492Z", | |
| "end_time": "2024-03-16T00:16:10.090276Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "798a4da3", | |
| "cell_type": "code", | |
| "source": "str(mytime)", | |
| "execution_count": 6, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "execution_count": 6, | |
| "data": { | |
| "text/plain": "'14:28:57'" | |
| }, | |
| "metadata": {} | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": {}, | |
| "id": "f59f5faf", | |
| "cell_type": "markdown", | |
| "source": "## `MyTime2`" | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.091542Z", | |
| "end_time": "2024-03-16T00:16:10.095258Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "d74a53a9", | |
| "cell_type": "code", | |
| "source": "class MyTime2(AbstractTime):\n def __init__(self, seconds) -> None:\n self._seconds = seconds\n\n @property\n def hour(self) -> int:\n return self._seconds // 3600\n\n @property\n def minute(self) -> int:\n return self._seconds // 60 % 60\n\n @property\n def second(self) -> int:\n return self._seconds % 60\n\n @property\n def seconds(self) -> int:\n return self._seconds\n\n # override\n def __str__(self) -> str:\n return super().__str__() + \" ({}sec(s).)\".format(self.seconds)\n", | |
| "execution_count": 7, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.096732Z", | |
| "end_time": "2024-03-16T00:16:10.103253Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "f2e6e6be", | |
| "cell_type": "code", | |
| "source": "mytime2 = MyTime2(10000) # 午前0時の10000秒後は 2時46分40秒", | |
| "execution_count": 8, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.104662Z", | |
| "end_time": "2024-03-16T00:16:10.109223Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "bc4f6015", | |
| "cell_type": "code", | |
| "source": "str(mytime2)", | |
| "execution_count": 9, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "execution_count": 9, | |
| "data": { | |
| "text/plain": "'02:46:40 (10000sec(s).)'" | |
| }, | |
| "metadata": {} | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": {}, | |
| "id": "f73e8f91", | |
| "cell_type": "markdown", | |
| "source": "## `MyTimeWithMS`" | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.110326Z", | |
| "end_time": "2024-03-16T00:16:10.113941Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "264cdc14", | |
| "cell_type": "code", | |
| "source": "class MyTimeWithMS(AbstractTime):\n def __init__(self, h, m, s, ms) -> None:\n self._hms = MyTime(h, m, s)\n self._ms = ms\n\n @property\n def hour(self) -> int:\n return self._hms._hour\n\n @property\n def minute(self) -> int:\n return self._hms._minute\n\n @property\n def second(self) -> int:\n return self._hms._second\n\n @property\n def millisecond(self) -> int:\n return self._ms\n\n # override\n def __str__(self) -> str:\n return super().__str__() + \".{:03d}\".format(self.millisecond)\n", | |
| "execution_count": 10, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.115081Z", | |
| "end_time": "2024-03-16T00:16:10.118830Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "48153681", | |
| "cell_type": "code", | |
| "source": "mytime3 = MyTimeWithMS(12, 34, 56, 789)", | |
| "execution_count": 11, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.119753Z", | |
| "end_time": "2024-03-16T00:16:10.126906Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "8370bf29", | |
| "cell_type": "code", | |
| "source": "str(mytime3)", | |
| "execution_count": 12, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "execution_count": 12, | |
| "data": { | |
| "text/plain": "'12:34:56.789'" | |
| }, | |
| "metadata": {} | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": {}, | |
| "id": "9f09025b", | |
| "cell_type": "markdown", | |
| "source": "### 補足" | |
| }, | |
| { | |
| "metadata": {}, | |
| "id": "3ade088e", | |
| "cell_type": "markdown", | |
| "source": "+ 後々の説明のために↑のような実装をした(**委譲** を利用した実装例)\n+ より Python らしい(or クラスベースオブジェクト指向らしい)**継承** を利用した実装としては↓(スライドは余白の関係でこちらを採用)" | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.127888Z", | |
| "end_time": "2024-03-16T00:16:10.131667Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "39801c5e", | |
| "cell_type": "code", | |
| "source": "class MyTimeWithMS2(MyTime):\n def __init__(self, h, m, s, ms) -> None:\n super().__init__(h, m, s)\n self._ms = ms\n\n @property\n def millisecond(self) -> int:\n return self._ms\n\n # override\n def __str__(self) -> str:\n return super().__str__() + \".{:03d}\".format(self.millisecond)\n", | |
| "execution_count": 13, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.133632Z", | |
| "end_time": "2024-03-16T00:16:10.136717Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "5f7405cb", | |
| "cell_type": "code", | |
| "source": "mytime3b = MyTimeWithMS2(12, 34, 56, 789)", | |
| "execution_count": 14, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "ExecuteTime": { | |
| "start_time": "2024-03-16T00:16:10.137807Z", | |
| "end_time": "2024-03-16T00:16:10.142420Z" | |
| }, | |
| "trusted": true | |
| }, | |
| "id": "d4c51209", | |
| "cell_type": "code", | |
| "source": "str(mytime3b)", | |
| "execution_count": 15, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "execution_count": 15, | |
| "data": { | |
| "text/plain": "'12:34:56.789'" | |
| }, | |
| "metadata": {} | |
| } | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "name": "python3b8", | |
| "display_name": "Python3 (3.11.8)", | |
| "language": "python" | |
| }, | |
| "language_info": { | |
| "name": "python", | |
| "version": "3.11.8", | |
| "mimetype": "text/x-python", | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "pygments_lexer": "ipython3", | |
| "nbconvert_exporter": "python", | |
| "file_extension": ".py" | |
| }, | |
| "gist": { | |
| "id": "", | |
| "data": { | |
| "description": "MyTimes.py.ipynb", | |
| "public": true | |
| } | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
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
| [deps] | |
| BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | |
| [compat] | |
| julia = "1.6" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment