Skip to content

Instantly share code, notes, and snippets.

@tangjeff0
Last active July 23, 2020 11:44
Show Gist options
  • Save tangjeff0/d375ea58e30c7ec590275628126272b4 to your computer and use it in GitHub Desktop.
Save tangjeff0/d375ea58e30c7ec590275628126272b4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import time\n",
"from notion_jeff.client import NotionClient\n",
"from notion_jeff.collection import NotionDate\n",
"import datetime\n",
"import pprint\n",
"pp = pprint.PrettyPrinter(indent=2)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def sm2(quality, repetitions, interval, ease_factor):\n",
" quality = int(quality) # issa string bc single-select in Notion\n",
" if quality >= 3:\n",
" if repetitions == 0:\n",
" interval = 1\n",
" elif repetitions == 1:\n",
" interval = 6\n",
" else:\n",
" interval = round(interval * ease_factor)\n",
" repetitions += 1\n",
" ease_factor = ease_factor + (0.1 - (5 - quality) * (0.08 + (5 - quality) * 0.02))\n",
" else:\n",
" repetitions = 0\n",
" interval = 1\n",
" ease_factor = ease_factor\n",
"\n",
" if ease_factor < 1.3:\n",
" ease_factor = 1.3\n",
" return {'interval': interval, 'repetitions': repetitions, 'ease_factor': ease_factor}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"token_v2 = ''\n",
"client = NotionClient(token_v2=token_v2)\n",
"\n",
"url_terms = \"https://www.notion.so/tangjeff0/92a1873e90f14631bc36663c1b635503?v=bc0bccf5243b4d50a5a2aea7debcdcac\"\n",
"url_notif = \"https://www.notion.so/tangjeff0/54da4c26c52f4b12b491029246baa75d?v=9dd9a65a7ce84624ba7a1fd6b08630ae\"\n",
"\n",
"db_terms = client.get_block(url_terms)\n",
"db_notifs = client.get_block(url_notif)\n",
"\n",
"terms = db_terms.collection.get_rows()\n",
"notifs = db_notifs.collection.get_rows()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ 'ease_factor': 2.5,\n",
" 'interval': 1,\n",
" 'join': [ <CollectionRowBlock (id='6a863771-10ef-4f36-9302-2cd4eba6b5f9', title='生物化学')>],\n",
" 'name': '生物化学 - 1',\n",
" 'notification': <notion_jeff.collection.NotionDate object at 0x1101fec50>,\n",
" 'quality_score': '5',\n",
" 'rep': 1}\n",
"{ 'ease_factor': 2.6,\n",
" 'interval': 6,\n",
" 'join': [ <CollectionRowBlock (id='6a863771-10ef-4f36-9302-2cd4eba6b5f9', title='生物化学')>],\n",
" 'name': '生物化学 - 2',\n",
" 'notification': <notion_jeff.collection.NotionDate object at 0x1105ad750>,\n",
" 'quality_score': '5',\n",
" 'rep': 2}\n",
"{ 'ease_factor': 2.5,\n",
" 'interval': 1,\n",
" 'join': [ <CollectionRowBlock (id='6a863771-10ef-4f36-9302-2cd4eba6b5f9', title='生物化学')>],\n",
" 'name': '生物化学 - 1',\n",
" 'notification': <notion_jeff.collection.NotionDate object at 0x1105b0290>,\n",
" 'quality_score': '5',\n",
" 'rep': 1}\n",
"{ 'ease_factor': 2.6,\n",
" 'interval': 6,\n",
" 'join': [ <CollectionRowBlock (id='6a863771-10ef-4f36-9302-2cd4eba6b5f9', title='生物化学')>],\n",
" 'name': '生物化学 - 2',\n",
" 'notification': <notion_jeff.collection.NotionDate object at 0x1105b0450>,\n",
" 'quality_score': '4',\n",
" 'rep': 2}\n",
"{ 'ease_factor': 2.6,\n",
" 'interval': 16,\n",
" 'join': [ <CollectionRowBlock (id='6a863771-10ef-4f36-9302-2cd4eba6b5f9', title='生物化学')>],\n",
" 'name': '生物化学 - 3',\n",
" 'notification': <notion_jeff.collection.NotionDate object at 0x110718b90>,\n",
" 'quality_score': '3',\n",
" 'rep': 3}\n",
"{ 'ease_factor': 2.46,\n",
" 'interval': 42,\n",
" 'join': [ <CollectionRowBlock (id='6a863771-10ef-4f36-9302-2cd4eba6b5f9', title='生物化学')>],\n",
" 'name': '生物化学 - 4',\n",
" 'notification': <notion_jeff.collection.NotionDate object at 0x1104ea110>,\n",
" 'quality_score': '2',\n",
" 'rep': 4}\n"
]
}
],
"source": [
"# we expect prop_id to be '4>z`' for my Score column\n",
"# assume that the only thing im modifying is the q score\n",
"# what if the person doesn't respond to card that day?\n",
"def my_callback(record, difference):\n",
" method = difference[0][0]\n",
" if method == 'add':\n",
" prop_id = difference[0][2][0][0]\n",
" if prop_id == '4>z`':\n",
"# q_score = difference[0][2][0][1][0][0]\n",
" props = record.get_all_properties()\n",
" pp.pprint(props)\n",
" q = props['quality_score']\n",
" ef = props['ease_factor']\n",
" interval = props['interval']\n",
" rep = props['rep']\n",
" res = sm2(q, rep, interval, ef)\n",
" # create new row\n",
" row = db_notifs.collection.add_row()\n",
" row.name = props['name'].split('-')[0] + '- ' + str(res['repetitions']) # many edge cases. for now assume 'chars - 1'\n",
" row.ease_factor = res['ease_factor']\n",
" row.interval = res['interval']\n",
" row.rep = res['repetitions']\n",
" row.join = props['join']\n",
" start = datetime.datetime.now() + datetime.timedelta(days=res['interval'])\n",
" row.notification = NotionDate(start=start, reminder={'unit': 'minute', 'value': 0})\n",
" row.add_callback(my_callback)\n",
" else:\n",
" pass\n",
" elif method == 'change':\n",
" prop_id = difference[0][1][1]\n",
" else:\n",
" pass\n",
"\n",
"for x in notifs:\n",
" x.add_callback(my_callback)\n",
"# x.remove_callbacks()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4>z`\n",
"4>z`\n",
"2\n",
"{ 'ease_factor': 2.5,\n",
" 'interval': 6,\n",
" 'join': [ <CollectionRowBlock (id='6a863771-10ef-4f36-9302-2cd4eba6b5f9', title='生物化学')>],\n",
" 'name': '生物化学 - 1',\n",
" 'notification': <notion_jeff.collection.NotionDate object at 0x11072c150>,\n",
" 'quality_score': '5',\n",
" 'rep': 1}\n",
"{ 'ease_factor': 2.5,\n",
" 'interval': 1,\n",
" 'join': [ <CollectionRowBlock (id='6a863771-10ef-4f36-9302-2cd4eba6b5f9', title='生物化学')>],\n",
" 'name': '生物化学 - 1',\n",
" 'notification': <notion_jeff.collection.NotionDate object at 0x1106ce1d0>,\n",
" 'quality_score': '5',\n",
" 'rep': 1}\n"
]
}
],
"source": [
"a1=[\n",
" ('add', 'properties', [('4>z`', [['1']])])\n",
"]\n",
"a2=[\n",
" ('change', ['properties', '4>z`', 0, 0], ('1', '2'))\n",
"]\n",
"# print(a1[0][2][0][1][0][0]) # this is the path to the added property lmao\n",
"print(a1[0][2][0][0])\n",
"print(a2[0][1][1])\n",
"print(a2[0][2][1])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment