Skip to content

Commit

Permalink
move flat_tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Oct 25, 2024
1 parent 7637e19 commit 80904e7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
16 changes: 10 additions & 6 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,17 @@ def _to_xml(req, resp, indent):
_find_targets(req, resp)
return to_xml(resp, indent)

# %% ../nbs/api/00_core.ipynb
_iter_typs = (tuple,list,map,filter,range,types.GeneratorType)

# %% ../nbs/api/00_core.ipynb
def flat_tuple(o):
"Flatten lists"
result = []
_typs = (tuple,list,map,filter,range,types.GeneratorType)
if not isinstance(o,_typs): o=[o]
if not isinstance(o,_iter_typs): o=[o]
o = list(o)
for item in o:
if isinstance(item, _typs): result.extend(list(item))
if isinstance(item, _iter_typs): result.extend(list(item))
else: result.append(item)
return tuple(result)

Expand All @@ -370,11 +372,14 @@ def noop_body(c, req):
def respond(req, heads, bdy):
"Default FT response creation function"
body_wrap = getattr(req, 'body_wrap', noop_body)
body = Body(body_wrap(bdy, req), *flat_xt(req.ftrs), **req.bodykw)
params = inspect.signature(body_wrap).parameters
bw_args = (bdy, req) if len(params)>1 else (bdy,)
body = Body(body_wrap(*bw_args), *flat_xt(req.ftrs), **req.bodykw)
return Html(Head(*heads, *flat_xt(req.hdrs)), body, **req.htmlkw)

# %% ../nbs/api/00_core.ipynb
def _xt_cts(req, resp):
resp = flat_tuple(resp)
resp = resp + tuple(getattr(req, 'injects', ()))
http_hdrs,resp = partition(resp, risinstance(HttpHeader))
http_hdrs = {o.k:str(o.v) for o in http_hdrs}
Expand All @@ -393,12 +398,11 @@ def _xt_resp(req, resp):
return HTMLResponse(cts, headers=http_hdrs, background=tasks)

# %% ../nbs/api/00_core.ipynb
def _is_ft_resp(resp): return isinstance(resp, (list,tuple,HttpHeader,FT)) or hasattr(resp, '__ft__')
def _is_ft_resp(resp): return isinstance(resp, _iter_typs+(HttpHeader,FT)) or hasattr(resp, '__ft__')

# %% ../nbs/api/00_core.ipynb
def _resp(req, resp, cls=empty):
if not resp: resp=()
resp = flat_tuple(resp)
if hasattr(resp, '__response__'): resp = resp.__response__(req)
if cls in (Any,FT): cls=empty
if isinstance(resp, FileResponse) and not os.path.exists(resp.path): raise HTTPException(404, resp.path)
Expand Down
52 changes: 21 additions & 31 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,17 @@
" return to_xml(resp, indent)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1e3ed2d",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"_iter_typs = (tuple,list,map,filter,range,types.GeneratorType)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -1014,11 +1025,10 @@
"def flat_tuple(o):\n",
" \"Flatten lists\"\n",
" result = []\n",
" _typs = (tuple,list,map,filter,range,types.GeneratorType)\n",
" if not isinstance(o,_typs): o=[o]\n",
" if not isinstance(o,_iter_typs): o=[o]\n",
" o = list(o)\n",
" for item in o:\n",
" if isinstance(item, _typs): result.extend(list(item))\n",
" if isinstance(item, _iter_typs): result.extend(list(item))\n",
" else: result.append(item)\n",
" return tuple(result)"
]
Expand Down Expand Up @@ -1047,7 +1057,9 @@
"def respond(req, heads, bdy):\n",
" \"Default FT response creation function\"\n",
" body_wrap = getattr(req, 'body_wrap', noop_body)\n",
" body = Body(body_wrap(bdy, req), *flat_xt(req.ftrs), **req.bodykw)\n",
" params = inspect.signature(body_wrap).parameters\n",
" bw_args = (bdy, req) if len(params)>1 else (bdy,)\n",
" body = Body(body_wrap(*bw_args), *flat_xt(req.ftrs), **req.bodykw)\n",
" return Html(Head(*heads, *flat_xt(req.hdrs)), body, **req.htmlkw)"
]
},
Expand All @@ -1060,6 +1072,7 @@
"source": [
"#| export\n",
"def _xt_cts(req, resp):\n",
" resp = flat_tuple(resp)\n",
" resp = resp + tuple(getattr(req, 'injects', ()))\n",
" http_hdrs,resp = partition(resp, risinstance(HttpHeader))\n",
" http_hdrs = {o.k:str(o.v) for o in http_hdrs}\n",
Expand Down Expand Up @@ -1094,7 +1107,7 @@
"outputs": [],
"source": [
"#| export\n",
"def _is_ft_resp(resp): return isinstance(resp, (list,tuple,HttpHeader,FT)) or hasattr(resp, '__ft__')"
"def _is_ft_resp(resp): return isinstance(resp, _iter_typs+(HttpHeader,FT)) or hasattr(resp, '__ft__')"
]
},
{
Expand All @@ -1107,7 +1120,6 @@
"#| export\n",
"def _resp(req, resp, cls=empty):\n",
" if not resp: resp=()\n",
" resp = flat_tuple(resp)\n",
" if hasattr(resp, '__response__'): resp = resp.__response__(req)\n",
" if cls in (Any,FT): cls=empty\n",
" if isinstance(resp, FileResponse) and not os.path.exists(resp.path): raise HTTPException(404, resp.path)\n",
Expand Down Expand Up @@ -1205,28 +1217,6 @@
" return key"
]
},
{
"cell_type": "markdown",
"id": "65c1e4d2",
"metadata": {},
"source": [
"```python\n",
"#| export\n",
"def flat_tuple(o):\n",
" \"Flatten lists\"\n",
" result = []\n",
" if hasattr(o,'__iter__') and not hasattr(o,'__len__'): o=list(o)\n",
" if not isinstance(o,(tuple,list)): o=[o]\n",
" for item in o:\n",
" if hasattr(item,'__iter__') and not hasattr(item,'__len__'): item=list(item)\n",
" if isinstance(item, (list,tuple)): result.extend(item)\n",
" else: result.append(item)\n",
" return tuple(result)\n",
"```\n",
"\n",
"This version specifically targets true generators by checking for `__iter__` without `__len__` (which is a key characteristic of generators)."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -2422,13 +2412,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Set to 2024-10-26 07:06:14.690668\n"
"Set to 2024-10-26 07:52:13.419267\n"
]
},
{
"data": {
"text/plain": [
"'Session time: 2024-10-26 07:06:14.690668'"
"'Session time: 2024-10-26 07:52:13.419267'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2931,7 +2921,7 @@
{
"data": {
"text/plain": [
"'Cookie was set at time 07:06:15.495794'"
"'Cookie was set at time 07:52:14.289735'"
]
},
"execution_count": null,
Expand Down

0 comments on commit 80904e7

Please sign in to comment.