Skip to content

Commit af2f799

Browse files
committed
refactor qp
1 parent 7716d18 commit af2f799

File tree

2 files changed

+32
-105
lines changed

2 files changed

+32
-105
lines changed

fasthtml/core.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,12 @@ async def _f(req, exc):
478478
# %% ../nbs/api/00_core.ipynb
479479
def qp(p:str, **kw) -> str:
480480
"Add parameters kw to path p"
481-
kw = {k.split(':')[0]:v for k,v in kw.items()}
482-
for k,v in kw.copy().items():
483-
for pat in [f"{{{k}}}", f"{{{k}:"]:
484-
if pat in p:
485-
# encode path params
486-
p = p.replace(p[p.find(pat):p.find("}", p.find(pat))+1], str(v))
487-
kw.pop(k)
488-
break
481+
def _sub(m):
482+
pre,post = m.groups()
483+
if pre not in kw: return f'{{{pre}{post or ""}}}'
484+
pre = kw.pop(pre)
485+
return '' if pre in (False,None) else str(pre)
486+
p = re.sub(r'\{([^:}]+)(:.+?)?}', _sub, p)
489487
# encode query params
490488
return p + ('?' + urlencode({k:'' if v in (False,None) else v for k,v in kw.items()},doseq=True) if kw else '')
491489

nbs/api/00_core.ipynb

Lines changed: 26 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
{
132132
"data": {
133133
"text/plain": [
134-
"datetime.datetime(2025, 1, 30, 14, 0)"
134+
"datetime.datetime(2025, 1, 31, 14, 0)"
135135
]
136136
},
137137
"execution_count": null,
@@ -1230,7 +1230,7 @@
12301230
{
12311231
"data": {
12321232
"text/plain": [
1233-
"'7293b76e-03e7-4cba-9a48-346d25aa5f32'"
1233+
"'cc87253c-bfc1-4544-bbc0-58dd8d3291bc'"
12341234
]
12351235
},
12361236
"execution_count": null,
@@ -1273,141 +1273,70 @@
12731273
{
12741274
"cell_type": "code",
12751275
"execution_count": null,
1276-
"id": "271b920a",
1276+
"id": "bc323fd4",
12771277
"metadata": {},
12781278
"outputs": [],
12791279
"source": [
12801280
"#| export\n",
12811281
"def qp(p:str, **kw) -> str:\n",
12821282
" \"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",
12911289
" # encode query params\n",
12921290
" return p + ('?' + urlencode({k:'' if v in (False,None) else v for k,v in kw.items()},doseq=True) if kw else '')"
12931291
]
12941292
},
12951293
{
1296-
"cell_type": "code",
1297-
"execution_count": null,
1298-
"id": "40507fbf",
1294+
"cell_type": "markdown",
1295+
"id": "e51e2dc3",
12991296
"metadata": {},
1300-
"outputs": [],
13011297
"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"
13031299
]
13041300
},
13051301
{
13061302
"cell_type": "code",
13071303
"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",
13311305
"metadata": {},
1306+
"outputs": [],
13321307
"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}"
13341309
]
13351310
},
13361311
{
13371312
"cell_type": "code",
13381313
"execution_count": null,
13391314
"id": "c0836a8f",
13401315
"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": [],
13531317
"source": [
13541318
"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')"
13571320
]
13581321
},
13591322
{
13601323
"cell_type": "markdown",
13611324
"id": "4740272f",
13621325
"metadata": {},
13631326
"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."
13881328
]
13891329
},
13901330
{
13911331
"cell_type": "code",
13921332
"execution_count": null,
1393-
"id": "85a243b5",
1333+
"id": "50ebb1ee",
13941334
"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": [],
14071336
"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=')"
14111340
]
14121341
},
14131342
{
@@ -2648,13 +2577,13 @@
26482577
"name": "stdout",
26492578
"output_type": "stream",
26502579
"text": [
2651-
"Set to 2025-01-12 14:12:46.576323\n"
2580+
"Set to 2025-01-31 14:35:31.433371\n"
26522581
]
26532582
},
26542583
{
26552584
"data": {
26562585
"text/plain": [
2657-
"'Session time: 2025-01-12 14:12:46.576323'"
2586+
"'Session time: 2025-01-31 14:35:31.433371'"
26582587
]
26592588
},
26602589
"execution_count": null,
@@ -3175,7 +3104,7 @@
31753104
{
31763105
"data": {
31773106
"text/plain": [
3178-
"'Cookie was set at time 14:12:47.159530'"
3107+
"'Cookie was set at time 14:35:32.085198'"
31793108
]
31803109
},
31813110
"execution_count": null,

0 commit comments

Comments
 (0)