|
131 | 131 | { |
132 | 132 | "data": { |
133 | 133 | "text/plain": [ |
134 | | - "datetime.datetime(2025, 1, 30, 14, 0)" |
| 134 | + "datetime.datetime(2025, 1, 31, 14, 0)" |
135 | 135 | ] |
136 | 136 | }, |
137 | 137 | "execution_count": null, |
|
1230 | 1230 | { |
1231 | 1231 | "data": { |
1232 | 1232 | "text/plain": [ |
1233 | | - "'7293b76e-03e7-4cba-9a48-346d25aa5f32'" |
| 1233 | + "'cc87253c-bfc1-4544-bbc0-58dd8d3291bc'" |
1234 | 1234 | ] |
1235 | 1235 | }, |
1236 | 1236 | "execution_count": null, |
|
1273 | 1273 | { |
1274 | 1274 | "cell_type": "code", |
1275 | 1275 | "execution_count": null, |
1276 | | - "id": "271b920a", |
| 1276 | + "id": "bc323fd4", |
1277 | 1277 | "metadata": {}, |
1278 | 1278 | "outputs": [], |
1279 | 1279 | "source": [ |
1280 | 1280 | "#| export\n", |
1281 | 1281 | "def qp(p:str, **kw) -> str:\n", |
1282 | 1282 | " \"Add parameters kw to path p\"\n", |
1283 | | - " kw = {k.split(':')[0]:v for k,v in kw.items()}\n", |
1284 | | - " for k,v in kw.copy().items():\n", |
1285 | | - " for pat in [f\"{{{k}}}\", f\"{{{k}:\"]: \n", |
1286 | | - " if pat in p: \n", |
1287 | | - " # encode path params\n", |
1288 | | - " p = p.replace(p[p.find(pat):p.find(\"}\", p.find(pat))+1], str(v))\n", |
1289 | | - " kw.pop(k) \n", |
1290 | | - " break\n", |
| 1283 | + " def _sub(m):\n", |
| 1284 | + " pre,post = m.groups()\n", |
| 1285 | + " if pre not in kw: return f'{{{pre}{post or \"\"}}}'\n", |
| 1286 | + " pre = kw.pop(pre)\n", |
| 1287 | + " return '' if pre in (False,None) else str(pre)\n", |
| 1288 | + " p = re.sub(r'\\{([^:}]+)(:.+?)?}', _sub, p)\n", |
1291 | 1289 | " # encode query params\n", |
1292 | 1290 | " return p + ('?' + urlencode({k:'' if v in (False,None) else v for k,v in kw.items()},doseq=True) if kw else '')" |
1293 | 1291 | ] |
1294 | 1292 | }, |
1295 | 1293 | { |
1296 | | - "cell_type": "code", |
1297 | | - "execution_count": null, |
1298 | | - "id": "40507fbf", |
| 1294 | + "cell_type": "markdown", |
| 1295 | + "id": "e51e2dc3", |
1299 | 1296 | "metadata": {}, |
1300 | | - "outputs": [], |
1301 | 1297 | "source": [ |
1302 | | - "vals = {'a':5, 'b':False, 'c':[1,2], 'd':'bar', 'e':None, 'ab:cd':42}" |
| 1298 | + "`qp` adds query parameters to route path strings" |
1303 | 1299 | ] |
1304 | 1300 | }, |
1305 | 1301 | { |
1306 | 1302 | "cell_type": "code", |
1307 | 1303 | "execution_count": null, |
1308 | | - "id": "6bd3945d", |
1309 | | - "metadata": {}, |
1310 | | - "outputs": [ |
1311 | | - { |
1312 | | - "data": { |
1313 | | - "text/plain": [ |
1314 | | - "'/foo/5/bar/42?b=&c=1&c=2&e='" |
1315 | | - ] |
1316 | | - }, |
1317 | | - "execution_count": null, |
1318 | | - "metadata": {}, |
1319 | | - "output_type": "execute_result" |
1320 | | - } |
1321 | | - ], |
1322 | | - "source": [ |
1323 | | - "res = qp('/foo/{a}/{d}/{ab:int}', **vals)\n", |
1324 | | - "assert res == '/foo/5/bar/42?b=&c=1&c=2&e='\n", |
1325 | | - "res" |
1326 | | - ] |
1327 | | - }, |
1328 | | - { |
1329 | | - "cell_type": "markdown", |
1330 | | - "id": "e51e2dc3", |
| 1304 | + "id": "ff82dc78", |
1331 | 1305 | "metadata": {}, |
| 1306 | + "outputs": [], |
1332 | 1307 | "source": [ |
1333 | | - "`qp` adds query parameters to route path strings" |
| 1308 | + "vals = {'a':5, 'b':False, 'c':[1,2], 'd':'bar', 'e':None, 'ab':42}" |
1334 | 1309 | ] |
1335 | 1310 | }, |
1336 | 1311 | { |
1337 | 1312 | "cell_type": "code", |
1338 | 1313 | "execution_count": null, |
1339 | 1314 | "id": "c0836a8f", |
1340 | 1315 | "metadata": {}, |
1341 | | - "outputs": [ |
1342 | | - { |
1343 | | - "data": { |
1344 | | - "text/plain": [ |
1345 | | - "'/foo?a=5&b=&c=1&c=2&d=bar&e=&ab=42'" |
1346 | | - ] |
1347 | | - }, |
1348 | | - "execution_count": null, |
1349 | | - "metadata": {}, |
1350 | | - "output_type": "execute_result" |
1351 | | - } |
1352 | | - ], |
| 1316 | + "outputs": [], |
1353 | 1317 | "source": [ |
1354 | 1318 | "res = qp('/foo', **vals)\n", |
1355 | | - "assert res == '/foo?a=5&b=&c=1&c=2&d=bar&e=&ab=42'\n", |
1356 | | - "res" |
| 1319 | + "test_eq(res, '/foo?a=5&b=&c=1&c=2&d=bar&e=&ab=42')" |
1357 | 1320 | ] |
1358 | 1321 | }, |
1359 | 1322 | { |
1360 | 1323 | "cell_type": "markdown", |
1361 | 1324 | "id": "4740272f", |
1362 | 1325 | "metadata": {}, |
1363 | 1326 | "source": [ |
1364 | | - "`qp` checks to see if it should be sent as a query parameter or as part of the route and encodes that properly." |
1365 | | - ] |
1366 | | - }, |
1367 | | - { |
1368 | | - "cell_type": "code", |
1369 | | - "execution_count": null, |
1370 | | - "id": "bd4d1630", |
1371 | | - "metadata": {}, |
1372 | | - "outputs": [ |
1373 | | - { |
1374 | | - "data": { |
1375 | | - "text/plain": [ |
1376 | | - "'/foo/5/bar?b=&c=1&c=2&e=&ab=42'" |
1377 | | - ] |
1378 | | - }, |
1379 | | - "execution_count": null, |
1380 | | - "metadata": {}, |
1381 | | - "output_type": "execute_result" |
1382 | | - } |
1383 | | - ], |
1384 | | - "source": [ |
1385 | | - "res = qp('/foo/{a}/{d}', **vals)\n", |
1386 | | - "assert res == '/foo/5/bar?b=&c=1&c=2&e=&ab=42'\n", |
1387 | | - "res" |
| 1327 | + "`qp` checks to see if each param should be sent as a query parameter or as part of the route, and encodes that properly." |
1388 | 1328 | ] |
1389 | 1329 | }, |
1390 | 1330 | { |
1391 | 1331 | "cell_type": "code", |
1392 | 1332 | "execution_count": null, |
1393 | | - "id": "85a243b5", |
| 1333 | + "id": "50ebb1ee", |
1394 | 1334 | "metadata": {}, |
1395 | | - "outputs": [ |
1396 | | - { |
1397 | | - "data": { |
1398 | | - "text/plain": [ |
1399 | | - "'/foo/5/bar/42?b=&c=1&c=2&e='" |
1400 | | - ] |
1401 | | - }, |
1402 | | - "execution_count": null, |
1403 | | - "metadata": {}, |
1404 | | - "output_type": "execute_result" |
1405 | | - } |
1406 | | - ], |
| 1335 | + "outputs": [], |
1407 | 1336 | "source": [ |
1408 | | - "res = qp('/foo/{a}/{d}/{ab:int}', **vals)\n", |
1409 | | - "assert res == '/foo/5/bar/42?b=&c=1&c=2&e='\n", |
1410 | | - "res" |
| 1337 | + "path = '/foo/{a}/{d}/{ab:int}'\n", |
| 1338 | + "res = qp(path, **vals)\n", |
| 1339 | + "test_eq(res, '/foo/5/bar/42?b=&c=1&c=2&e=')" |
1411 | 1340 | ] |
1412 | 1341 | }, |
1413 | 1342 | { |
|
2648 | 2577 | "name": "stdout", |
2649 | 2578 | "output_type": "stream", |
2650 | 2579 | "text": [ |
2651 | | - "Set to 2025-01-12 14:12:46.576323\n" |
| 2580 | + "Set to 2025-01-31 14:35:31.433371\n" |
2652 | 2581 | ] |
2653 | 2582 | }, |
2654 | 2583 | { |
2655 | 2584 | "data": { |
2656 | 2585 | "text/plain": [ |
2657 | | - "'Session time: 2025-01-12 14:12:46.576323'" |
| 2586 | + "'Session time: 2025-01-31 14:35:31.433371'" |
2658 | 2587 | ] |
2659 | 2588 | }, |
2660 | 2589 | "execution_count": null, |
|
3175 | 3104 | { |
3176 | 3105 | "data": { |
3177 | 3106 | "text/plain": [ |
3178 | | - "'Cookie was set at time 14:12:47.159530'" |
| 3107 | + "'Cookie was set at time 14:35:32.085198'" |
3179 | 3108 | ] |
3180 | 3109 | }, |
3181 | 3110 | "execution_count": null, |
|
0 commit comments