toolslm 0.3.18__tar.gz → 0.3.20__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: toolslm
3
- Version: 0.3.18
3
+ Version: 0.3.20
4
4
  Summary: Tools to make language models a bit easier to use
5
5
  Home-page: https://github.com/AnswerDotAI/toolslm
6
6
  Author: Jeremy Howard
@@ -1,7 +1,7 @@
1
1
  [DEFAULT]
2
2
  repo = toolslm
3
3
  lib_name = toolslm
4
- version = 0.3.18
4
+ version = 0.3.20
5
5
  min_python = 3.9
6
6
  license = apache2
7
7
  black_formatting = False
@@ -0,0 +1 @@
1
+ __version__ = "0.3.20"
@@ -40,6 +40,7 @@ d = { 'settings': { 'branch': 'main',
40
40
  'toolslm.xml': { 'toolslm.xml._add_nls': ('xml.html#_add_nls', 'toolslm/xml.py'),
41
41
  'toolslm.xml.cell2out': ('xml.html#cell2out', 'toolslm/xml.py'),
42
42
  'toolslm.xml.cell2xml': ('xml.html#cell2xml', 'toolslm/xml.py'),
43
+ 'toolslm.xml.cells2xml': ('xml.html#cells2xml', 'toolslm/xml.py'),
43
44
  'toolslm.xml.docs_xml': ('xml.html#docs_xml', 'toolslm/xml.py'),
44
45
  'toolslm.xml.files2ctx': ('xml.html#files2ctx', 'toolslm/xml.py'),
45
46
  'toolslm.xml.folder2ctx': ('xml.html#folder2ctx', 'toolslm/xml.py'),
@@ -141,7 +141,12 @@ def get_schema(
141
141
  assert desc, "Docstring missing!"
142
142
  d = docments(f, full=True)
143
143
  ret = d.pop('return')
144
- if (ret.anno is not empty) and (ret.anno is not None): desc += f'\n\nReturns:\n- type: {_types(ret.anno)[0]}'
144
+ has_type = (ret.anno is not empty) and (ret.anno is not None)
145
+ has_doc = ret.docment
146
+ if has_type or has_doc:
147
+ type_str = f'type: {_types(ret.anno)[0]}'
148
+ ret_str = f'{ret.docment} ({type_str})' if has_type and has_doc else (type_str if has_type else ret.docment)
149
+ desc += f'\n\nReturns:\n- {ret_str}'
145
150
  return {"name": f.__name__, "description": desc, pname: schema}
146
151
 
147
152
  # %% ../01_funccall.ipynb
@@ -1,9 +1,9 @@
1
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../00_xml.ipynb.
2
2
 
3
3
  # %% auto 0
4
- __all__ = ['doctype', 'json_to_xml', 'get_mime_text', 'cell2out', 'cell2xml', 'nb2xml', 'get_docstring', 'py2sigs', 'mk_doctype',
5
- 'mk_doc', 'docs_xml', 'read_file', 'files2ctx', 'folder2ctx', 'sym2file', 'sym2folderctx', 'sym2pkgpath',
6
- 'sym2pkgctx', 'folder2ctx_cli', 'parse_gh_url', 'repo2ctx']
4
+ __all__ = ['doctype', 'json_to_xml', 'get_mime_text', 'cell2out', 'cell2xml', 'cells2xml', 'nb2xml', 'get_docstring', 'py2sigs',
5
+ 'mk_doctype', 'mk_doc', 'docs_xml', 'read_file', 'files2ctx', 'folder2ctx', 'sym2file', 'sym2folderctx',
6
+ 'sym2pkgpath', 'sym2pkgctx', 'folder2ctx_cli', 'parse_gh_url', 'repo2ctx']
7
7
 
8
8
  # %% ../00_xml.ipynb
9
9
  import hashlib, inspect, xml.etree.ElementTree as ET, ast
@@ -13,7 +13,7 @@ from ghapi.all import GhApi
13
13
  from fastcore.utils import *
14
14
  from fastcore.meta import delegates
15
15
  from fastcore.xtras import hl_md
16
- from fastcore.xml import to_xml, Document, Documents, Document_content, Src, Source,Out,Outs,Cell,Notebook,Md,Code
16
+ from fastcore.xml import to_xml, Document, Documents, Document_content, Src, Source,Out,Outs,Cell,Notebook,Md,Code,Raw
17
17
  from fastcore.script import call_parse
18
18
 
19
19
  # %% ../00_xml.ipynb
@@ -50,10 +50,13 @@ def cell2out(o):
50
50
  if hasattr(o, 'ename'): return Out(f"{o.ename}: {o.evalue}", type='error')
51
51
 
52
52
  # %% ../00_xml.ipynb
53
- def cell2xml(cell, out=True, ids=True):
53
+ _ctfuns = {'code': Code, 'markdown': Md, 'raw': Raw}
54
+
55
+ def cell2xml(cell, out=True, ids=True, nums=False):
54
56
  "Convert notebook cell to concise XML format"
55
57
  src = ''.join(getattr(cell, 'source', ''))
56
- f = Code if cell.cell_type=='code' else Md
58
+ if nums: src = '\n'.join(f'{i+1:6d} │ {l}' for i,l in enumerate(src.splitlines()))
59
+ f = _ctfuns[cell.cell_type]
57
60
  kw = dict(id=cell.id) if ids and hasattr(cell, 'id') else {}
58
61
  if not out: return f(src, **kw)
59
62
  parts = [Source(src)]
@@ -62,12 +65,18 @@ def cell2xml(cell, out=True, ids=True):
62
65
  return f(*parts, **kw)
63
66
 
64
67
  # %% ../00_xml.ipynb
65
- def nb2xml(fname=None, nb=None, out=True, ids=True):
68
+ @delegates(cell2xml)
69
+ def cells2xml(cells, wrap=Notebook, **kwargs):
70
+ "Convert notebook to XML format"
71
+ res = [cell2xml(c, **kwargs) for c in cells]
72
+ return to_xml(wrap(*res), do_escape=False)
73
+
74
+ @delegates(cell2xml)
75
+ def nb2xml(fname=None, nb=None, **kwargs):
66
76
  "Convert notebook to XML format"
67
77
  assert bool(fname)^bool(nb), "Pass either `fname` or `nb`"
68
78
  if not nb: nb = dict2obj(fname.read_json())
69
- cells_xml = [to_xml(cell2xml(c, out=out, ids=ids), do_escape=False) for c in nb.cells if c.cell_type in ('code','markdown')]
70
- return to_xml(Notebook(*cells_xml), do_escape=False)
79
+ return cells2xml(nb.cells, **kwargs)
71
80
 
72
81
  # %% ../00_xml.ipynb
73
82
  def get_docstring(node, lines):
@@ -78,7 +87,7 @@ def get_docstring(node, lines):
78
87
 
79
88
  def py2sigs(fname=None, src=None):
80
89
  "Return signature+docstring text for all functions and class methods in source"
81
- if fname: src = Path(fname).read_text()
90
+ if fname: src = Path(fname).expanduser().read_text()
82
91
  tree = ast.parse(src)
83
92
  lines = src.splitlines()
84
93
  res = []
@@ -138,10 +147,11 @@ def docs_xml(docs:list[str], # The content of each document
138
147
  return pre + to_xml(Documents(*docs, **kw), do_escape=False)
139
148
 
140
149
  # %% ../00_xml.ipynb
141
- def read_file(fname, out=True, max_size=None, ids=True, sigs_only=False):
150
+ @delegates(nb2xml)
151
+ def read_file(fname, max_size=None, sigs_only=False, **kwargs):
142
152
  "Read file content, converting notebooks to XML if needed"
143
- fname = Path(fname)
144
- if fname.suffix == '.ipynb': res = nb2xml(fname, out=out, ids=ids)
153
+ fname = Path(fname).expanduser()
154
+ if fname.suffix == '.ipynb': res = nb2xml(fname, **kwargs)
145
155
  elif fname.suffix == '.py' and sigs_only: res = py2sigs(fname)
146
156
  else: res = fname.read_text()
147
157
  if max_size and len(res)>max_size: return f"[Skipped: {fname.name} exceeds {max_size} bytes]"
@@ -151,16 +161,17 @@ def read_file(fname, out=True, max_size=None, ids=True, sigs_only=False):
151
161
  @delegates(docs_xml)
152
162
  def files2ctx(
153
163
  fnames:list[Union[str,Path]], # List of file names to add to context
154
- out:bool=True, # Include notebook cell outputs?
155
164
  srcs:Optional[list]=None, # Use the labels instead of `fnames`
156
165
  max_size:int=None, # Skip files larger than this (bytes)
166
+ out:bool=True, # Include notebook cell outputs?
157
167
  ids:bool=True, # Include cell ids in notebooks?
168
+ nums:bool=False, # Include line numbers in notebook cell source?
158
169
  sigs_only:bool=False, # For .py files, only include signatures and docstrings
159
170
  **kwargs
160
171
  )->str: # XML for LM context
161
172
  "Convert files to XML context, handling notebooks"
162
- fnames = [Path(o) for o in fnames]
163
- contents = [read_file(o, out=out, max_size=max_size, ids=ids, sigs_only=sigs_only) for o in fnames]
173
+ fnames = [Path(o).expanduser() for o in listify(fnames)]
174
+ contents = [read_file(o, max_size=max_size, out=out, ids=ids, sigs_only=sigs_only, nums=nums) for o in fnames]
164
175
  return docs_xml(contents, srcs or fnames, **kwargs)
165
176
 
166
177
  # %% ../00_xml.ipynb
@@ -180,7 +191,7 @@ def folder2ctx(
180
191
  **kwargs
181
192
  )->Union[str,dict]:
182
193
  "Convert folder contents to XML context, handling notebooks"
183
- folder = Path(folder)
194
+ folder = Path(folder).expanduser()
184
195
  fnames = pglob(folder, **kwargs)
185
196
  if files_only: return {str(f.relative_to(folder)): f.stat().st_size for f in fnames}
186
197
  if readme_first: fnames = sorted(fnames, key=lambda f: (0 if 'readme' in f.name.lower() else 1, f))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: toolslm
3
- Version: 0.3.18
3
+ Version: 0.3.20
4
4
  Summary: Tools to make language models a bit easier to use
5
5
  Home-page: https://github.com/AnswerDotAI/toolslm
6
6
  Author: Jeremy Howard
@@ -1 +0,0 @@
1
- __version__ = "0.3.18"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes