Skip to content

Commit fb2d913

Browse files
committed
fixes #556
1 parent a26c903 commit fb2d913

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@
555555
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
556556
'fastcore.xml': { 'fastcore.xml.XT': ('xml.html#xt', 'fastcore/xml.py'),
557557
'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
558+
'fastcore.xml._to_attr': ('xml.html#_to_attr', 'fastcore/xml.py'),
558559
'fastcore.xml.highlight': ('xml.html#highlight', 'fastcore/xml.py'),
559560
'fastcore.xml.showtags': ('xml.html#showtags', 'fastcore/xml.py'),
560561
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py'),

fastcore/py2pyi.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/12_py2pyi.ipynb.
22

33
# %% auto 0
4-
__all__ = ['imp_mod', 'has_deco', 'sig2str', 'ast_args', 'create_pyi', 'py2pyi']
4+
__all__ = ['functypes', 'imp_mod', 'has_deco', 'sig2str', 'ast_args', 'create_pyi', 'py2pyi']
55

66
# %% ../nbs/12_py2pyi.ipynb 3
77
import ast, sys, inspect, re, os, importlib.util, importlib.machinery
@@ -39,6 +39,9 @@ def _repr_markdown_(self:ast.AST):
3939
{self!r}
4040
```"""
4141

42+
# %% ../nbs/12_py2pyi.ipynb 13
43+
functypes = (ast.FunctionDef,ast.AsyncFunctionDef)
44+
4245
# %% ../nbs/12_py2pyi.ipynb 15
4346
def _deco_id(d:Union[ast.Name,ast.Attribute])->bool:
4447
"Get the id for AST node `d`"
@@ -129,7 +132,7 @@ def create_pyi(fn, package=None):
129132
# %% ../nbs/12_py2pyi.ipynb 62
130133
@call_parse
131134
def py2pyi(fname:str, # The file name to convert
132-
package:Union[str,None]=None # The parent package
135+
package:str=None # The parent package
133136
):
134137
"Convert `fname.py` to `fname.pyi` by removing function bodies and expanding `delegates` kwargs"
135-
create_pyi(fname)
138+
create_pyi(fname, package)

fastcore/xml.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class XT(list): pass
2929
def xt(tag:str, *c, **kw):
3030
"Create an XML tag structure `[tag,children,attrs]` for `toxml()`"
3131
if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])
32-
kw = {_attrmap(k):str(v) for k,v in kw.items() if v is not None}
32+
kw = {_attrmap(k):(v if isinstance(v,bool) else str(v)) for k,v in kw.items() if v is not None}
3333
return XT([tag.lower(),c,kw])
3434

3535
# %% ../nbs/11_xml.ipynb 7
@@ -47,9 +47,15 @@ def xt(tag:str, *c, **kw):
4747
for o in _all_: _g[o] = partial(xt, o.lower())
4848

4949
# %% ../nbs/11_xml.ipynb 10
50-
voids = set('area base br col command embed hr img input keygen link meta param source track wbr'.split())
50+
voids = set('area base br col command embed hr img input keygen link meta param source track wbr !doctype'.split())
5151

5252
# %% ../nbs/11_xml.ipynb 11
53+
def _to_attr(k,v):
54+
if v==True: return str(k)
55+
if v==False: return ''
56+
return f'{k}="{escape(str(v), quote=False)}"'
57+
58+
# %% ../nbs/11_xml.ipynb 12
5359
def to_xml(elm, lvl=0):
5460
"Convert `xt` element tree into an XML string"
5561
if isinstance(elm, tuple): return '\n'.join(to_xml(o) for o in elm)
@@ -62,7 +68,7 @@ def to_xml(elm, lvl=0):
6268
tag,cs,attrs = elm
6369
stag = tag
6470
if attrs:
65-
sattrs = (f'{k}="{escape(str(v), quote=False)}"' for k,v in attrs.items())
71+
sattrs = (_to_attr(k,v) for k,v in attrs.items())
6672
stag += ' ' + ' '.join(sattrs)
6773

6874
cltag = '' if tag in voids else f'</{tag}>'
@@ -72,12 +78,12 @@ def to_xml(elm, lvl=0):
7278
if tag not in voids: res += f'{sp}{cltag}\n'
7379
return res
7480

75-
# %% ../nbs/11_xml.ipynb 13
81+
# %% ../nbs/11_xml.ipynb 14
7682
def highlight(s, lang='html'):
7783
"Markdown to syntax-highlight `s` in language `lang`"
7884
return f'```{lang}\n{to_xml(s)}\n```'
7985

80-
# %% ../nbs/11_xml.ipynb 14
86+
# %% ../nbs/11_xml.ipynb 15
8187
def showtags(s):
8288
return f"""<code><pre>
8389
{escape(to_xml(s))}

nbs/11_xml.ipynb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"def xt(tag:str, *c, **kw):\n",
8383
" \"Create an XML tag structure `[tag,children,attrs]` for `toxml()`\"\n",
8484
" if len(c)==1 and isinstance(c[0], types.GeneratorType): c = tuple(c[0])\n",
85-
" kw = {_attrmap(k):str(v) for k,v in kw.items() if v is not None}\n",
85+
" kw = {_attrmap(k):(v if isinstance(v,bool) else str(v)) for k,v in kw.items() if v is not None}\n",
8686
" return XT([tag.lower(),c,kw])"
8787
]
8888
},
@@ -157,7 +157,21 @@
157157
"outputs": [],
158158
"source": [
159159
"#| export\n",
160-
"voids = set('area base br col command embed hr img input keygen link meta param source track wbr'.split())"
160+
"voids = set('area base br col command embed hr img input keygen link meta param source track wbr !doctype'.split())"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"id": "e89496fb",
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"#| export\n",
171+
"def _to_attr(k,v):\n",
172+
" if v==True: return str(k)\n",
173+
" if v==False: return ''\n",
174+
" return f'{k}=\"{escape(str(v), quote=False)}\"'"
161175
]
162176
},
163177
{
@@ -180,7 +194,7 @@
180194
" tag,cs,attrs = elm\n",
181195
" stag = tag\n",
182196
" if attrs:\n",
183-
" sattrs = (f'{k}=\"{escape(str(v), quote=False)}\"' for k,v in attrs.items())\n",
197+
" sattrs = (_to_attr(k,v) for k,v in attrs.items())\n",
184198
" stag += ' ' + ' '.join(sattrs)\n",
185199
" \n",
186200
" cltag = '' if tag in voids else f'</{tag}>'\n",

nbs/12_py2pyi.ipynb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
"metadata": {},
194194
"outputs": [],
195195
"source": [
196+
"#| export\n",
196197
"functypes = (ast.FunctionDef,ast.AsyncFunctionDef)"
197198
]
198199
},
@@ -1164,10 +1165,10 @@
11641165
"#| export\n",
11651166
"@call_parse\n",
11661167
"def py2pyi(fname:str, # The file name to convert\n",
1167-
" package:Union[str,None]=None # The parent package\n",
1168+
" package:str=None # The parent package\n",
11681169
" ):\n",
11691170
" \"Convert `fname.py` to `fname.pyi` by removing function bodies and expanding `delegates` kwargs\"\n",
1170-
" create_pyi(fname)"
1171+
" create_pyi(fname, package)"
11711172
]
11721173
},
11731174
{

0 commit comments

Comments
 (0)