Skip to content

Commit 4479d58

Browse files
committed
gh_date
1 parent eb2d5d0 commit 4479d58

File tree

4 files changed

+171
-23
lines changed

4 files changed

+171
-23
lines changed

00_core.ipynb

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
"from fastcore.utils import *\n",
2929
"from fastcore.foundation import *\n",
3030
"from fastcore.meta import *\n",
31+
"from ghapi.metadata import funcs\n",
3132
"\n",
3233
"import inspect,json,copy,urllib,mimetypes,base64\n",
3334
"from inspect import signature,Parameter,Signature\n",
3435
"from urllib.parse import urlencode\n",
35-
"from ghapi.metadata import funcs\n",
3636
"from urllib.request import Request,urlretrieve\n",
3737
"from urllib.error import HTTPError\n",
38+
"from datetime import datetime,timedelta\n",
3839
"from pprint import pprint"
3940
]
4041
},
@@ -447,7 +448,7 @@
447448
"- [users](https://docs.github.com/en/free-pro-team@latest/rest/reference/users)"
448449
],
449450
"text/plain": [
450-
"<__main__.GhApi at 0x7f496e622e10>"
451+
"<__main__.GhApi at 0x7ff9b02eb190>"
451452
]
452453
},
453454
"execution_count": null,
@@ -597,7 +598,7 @@
597598
"- [git.get_tree](https://docs.github.com/rest/reference/git#get-a-tree)(tree_sha, recursive): *Get a tree*"
598599
],
599600
"text/plain": [
600-
"<__main__._GhVerbGroup at 0x7f496e3f2690>"
601+
"<__main__._GhVerbGroup at 0x7ff9b01b8d90>"
601602
]
602603
},
603604
"execution_count": null,
@@ -904,6 +905,54 @@
904905
"for page in repos: print(len(page), page[0].name)"
905906
]
906907
},
908+
{
909+
"cell_type": "markdown",
910+
"metadata": {},
911+
"source": [
912+
"### Convenience functions"
913+
]
914+
},
915+
{
916+
"cell_type": "code",
917+
"execution_count": null,
918+
"metadata": {},
919+
"outputs": [],
920+
"source": [
921+
"#exports\n",
922+
"def gh_date(dt:datetime):\n",
923+
" \"Convert `dt` (which is assumed to be in UTC time zone) to a format suitable for GitHub API operations\"\n",
924+
" return f'{dt.replace(microsecond=0).isoformat()}Z'"
925+
]
926+
},
927+
{
928+
"cell_type": "markdown",
929+
"metadata": {},
930+
"source": [
931+
"The GitHub API assumes that dates will be in a specific string format. `gh_date` converts Python standard `datetime` objects to that format. For instance, to find issues opened in the last week:"
932+
]
933+
},
934+
{
935+
"cell_type": "code",
936+
"execution_count": null,
937+
"metadata": {},
938+
"outputs": [
939+
{
940+
"data": {
941+
"text/plain": [
942+
"1"
943+
]
944+
},
945+
"execution_count": null,
946+
"metadata": {},
947+
"output_type": "execute_result"
948+
}
949+
],
950+
"source": [
951+
"dt = gh_date(datetime.utcnow() - timedelta(weeks=1))\n",
952+
"issues = api.issues.list_for_repo(since=dt)\n",
953+
"len(issues)"
954+
]
955+
},
907956
{
908957
"cell_type": "markdown",
909958
"metadata": {},

0 commit comments

Comments
 (0)