Skip to content

Commit

Permalink
init helpers module
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed May 1, 2024
1 parent 10aaf80 commit 25507cb
Show file tree
Hide file tree
Showing 11 changed files with 826 additions and 498 deletions.
428 changes: 2 additions & 426 deletions 00_core.ipynb

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ Claude can handle image data as well. As everyone knows, when testing
image APIs you have to use a cute puppy.

``` python
fn = Path('puppy.jpg')
fn = Path('samples/puppy.jpg')
display.Image(filename=fn, width=200)
```

Expand Down Expand Up @@ -469,8 +469,8 @@ if you don’t find them useful – you can always just use plain strings
for XML directly.

An XML node contains a tag, optional children, and optional attributes.
[`xt`](https://AnswerDotAI.github.io/claudette/core.html#xt) creates a
tuple of these three things, which we will use to general XML shortly.
[`xt`](https://AnswerDotAI.github.io/claudette/helpers.html#xt) creates
a tuple of these three things, which we will use to general XML shortly.
Attributes are passed as kwargs; since these might conflict with
reserved words in Python, you can optionally add a `_` prefix and it’ll
be stripped off.
Expand All @@ -482,11 +482,11 @@ xt('x-custom', ['hi'], _class='bar')
('x-custom', ['hi'], {'class': 'bar'})

Claudette has functions defined for some common HTML elements to create
[`xt`](https://AnswerDotAI.github.io/claudette/core.html#xt) tuples more
easily, including these:
[`xt`](https://AnswerDotAI.github.io/claudette/helpers.html#xt) tuples
more easily, including these:

``` python
from claudette.core import div,img,h1,h2,p,hr,html
from claudette.helpers import div,img,h1,h2,p,hr,html
```

``` python
Expand All @@ -513,9 +513,9 @@ a
{})

To convert a tuple data structure created with
[`xt`](https://AnswerDotAI.github.io/claudette/core.html#xt) and friends
into XML, use
[`to_xml`](https://AnswerDotAI.github.io/claudette/core.html#to_xml),
[`xt`](https://AnswerDotAI.github.io/claudette/helpers.html#xt) and
friends into XML, use
[`to_xml`](https://AnswerDotAI.github.io/claudette/helpers.html#to_xml),
adding the `hl` parameter to optionally add syntax highlighting:

``` python
Expand All @@ -535,9 +535,9 @@ to_xml(a, hl=True)
```

JSON doesn’t map as nicely to XML as the
[`xt`](https://AnswerDotAI.github.io/claudette/core.html#xt) data
[`xt`](https://AnswerDotAI.github.io/claudette/helpers.html#xt) data
structure, but for simple XML trees it can be convenient. The
[`json_to_xml`](https://AnswerDotAI.github.io/claudette/core.html#json_to_xml)
[`json_to_xml`](https://AnswerDotAI.github.io/claudette/helpers.html#json_to_xml)
function handles that conversion:

``` python
Expand Down
1 change: 1 addition & 0 deletions claudette/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__version__ = "0.0.2"
from .core import *
from .helpers import *

11 changes: 6 additions & 5 deletions claudette/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
'claudette.core.contents': ('core.html#contents', 'claudette/core.py'),
'claudette.core.find_block': ('core.html#find_block', 'claudette/core.py'),
'claudette.core.get_schema': ('core.html#get_schema', 'claudette/core.py'),
'claudette.core.hl_md': ('core.html#hl_md', 'claudette/core.py'),
'claudette.core.img_msg': ('core.html#img_msg', 'claudette/core.py'),
'claudette.core.json_to_xml': ('core.html#json_to_xml', 'claudette/core.py'),
'claudette.core.mk_msg': ('core.html#mk_msg', 'claudette/core.py'),
'claudette.core.mk_msgs': ('core.html#mk_msgs', 'claudette/core.py'),
'claudette.core.mk_toolres': ('core.html#mk_toolres', 'claudette/core.py'),
'claudette.core.text_msg': ('core.html#text_msg', 'claudette/core.py'),
'claudette.core.to_xml': ('core.html#to_xml', 'claudette/core.py'),
'claudette.core.usage': ('core.html#usage', 'claudette/core.py'),
'claudette.core.xt': ('core.html#xt', 'claudette/core.py')}}}
'claudette.core.usage': ('core.html#usage', 'claudette/core.py')},
'claudette.helpers': { 'claudette.helpers.hl_md': ('helpers.html#hl_md', 'claudette/helpers.py'),
'claudette.helpers.json_to_xml': ('helpers.html#json_to_xml', 'claudette/helpers.py'),
'claudette.helpers.mk_doctype': ('helpers.html#mk_doctype', 'claudette/helpers.py'),
'claudette.helpers.to_xml': ('helpers.html#to_xml', 'claudette/helpers.py'),
'claudette.helpers.xt': ('helpers.html#xt', 'claudette/helpers.py')}}}
57 changes: 3 additions & 54 deletions claudette/core.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../00_core.ipynb.

# %% auto 0
__all__ = ['empty', 'models', 'g', 'tags', 'find_block', 'contents', 'usage', 'mk_msgs', 'Client', 'get_schema', 'call_func',
'mk_toolres', 'Chat', 'img_msg', 'text_msg', 'mk_msg', 'xt', 'hl_md', 'to_xml', 'json_to_xml']
__all__ = ['empty', 'models', 'find_block', 'contents', 'usage', 'mk_msgs', 'Client', 'get_schema', 'call_func', 'mk_toolres',
'Chat', 'img_msg', 'text_msg', 'mk_msg']

# %% ../00_core.ipynb 6
import inspect, typing, mimetypes, base64, json, xml.etree.ElementTree as ET
import inspect, typing, mimetypes, base64, json
from collections import abc
try: from IPython import display
except: display=None
Expand Down Expand Up @@ -270,54 +270,3 @@ def mk_msg(content, # A string, list, or dict containing the contents of the mes
if not isinstance(content, list): content=[content]
content = [_mk_content(o) for o in content]
return dict(role=role, content=content, **kw)

# %% ../00_core.ipynb 153
def xt(tag:str, # XML tag name
c:Optional[list]=None, # Children
**kw):
"Helper to create appropriate data structure for `to_xml`."
kw = {k.lstrip('_'):str(v) for k,v in kw.items()}
return tag,c,kw

# %% ../00_core.ipynb 156
g = globals()
tags = 'div img h1 h2 h3 h4 h5 p hr span html'.split()
for o in tags: g[o] = partial(xt, o)

# %% ../00_core.ipynb 159
def hl_md(s, lang='xml'):
"Syntax highlight `s` using `lang`."
if display: return display.Markdown(f'```{lang}\n{s}\n```')
print(s)

# %% ../00_core.ipynb 162
def to_xml(node:tuple, # XML structure in `xt` format
hl=False # Syntax highlight response?
):
"Convert `node` to an XML string."
def mk_el(tag, cs, attrs):
el = ET.Element(tag, attrib=attrs)
if isinstance(cs, list): el.extend([mk_el(*o) for o in cs])
elif cs is not None: el.text = str(cs)
return el

root = mk_el(*node)
ET.indent(root)
res = ET.tostring(root, encoding='unicode')
return hl_md(res) if hl else res

# %% ../00_core.ipynb 165
def json_to_xml(d:dict, # JSON dictionary to convert
rnm:str # Root name
)->str:
"Convert `d` to XML."
root = ET.Element(rnm)
def build_xml(data, parent):
if isinstance(data, dict):
for key, value in data.items(): build_xml(value, ET.SubElement(parent, key))
elif isinstance(data, list):
for item in data: build_xml(item, ET.SubElement(parent, 'item'))
else: parent.text = str(data)
build_xml(d, root)
ET.indent(root)
return ET.tostring(root, encoding='unicode')
74 changes: 74 additions & 0 deletions claudette/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../helpers.ipynb.

# %% auto 0
__all__ = ['g', 'tags', 'doctype', 'xt', 'hl_md', 'to_xml', 'json_to_xml', 'mk_doctype']

# %% ../helpers.ipynb 3
import hashlib,xml.etree.ElementTree as ET
from collections import namedtuple

from .core import *
from fastcore.utils import *
from IPython import display

# %% ../helpers.ipynb 6
def xt(tag:str, # XML tag name
c:Optional[list]=None, # Children
**kw):
"Helper to create appropriate data structure for `to_xml`."
kw = {k.lstrip('_'):str(v) for k,v in kw.items()}
return tag,c,kw

# %% ../helpers.ipynb 9
g = globals()
tags = 'div img h1 h2 h3 h4 h5 p hr span html'.split()
for o in tags: g[o] = partial(xt, o)

# %% ../helpers.ipynb 12
def hl_md(s, lang='xml'):
"Syntax highlight `s` using `lang`."
if display: return display.Markdown(f'```{lang}\n{s}\n```')
print(s)

# %% ../helpers.ipynb 15
def to_xml(node:tuple, # XML structure in `xt` format
hl=False # Syntax highlight response?
):
"Convert `node` to an XML string."
def mk_el(tag, cs, attrs):
el = ET.Element(tag, attrib=attrs)
if isinstance(cs, list): el.extend([mk_el(*o) for o in cs])
elif cs is not None: el.text = str(cs)
return el

root = mk_el(*node)
ET.indent(root, space=' ' if hl else '')
res = ET.tostring(root, encoding='unicode')
return hl_md(res) if hl else res

# %% ../helpers.ipynb 18
def json_to_xml(d:dict, # JSON dictionary to convert
rnm:str # Root name
)->str:
"Convert `d` to XML."
root = ET.Element(rnm)
def build_xml(data, parent):
if isinstance(data, dict):
for key, value in data.items(): build_xml(value, ET.SubElement(parent, key))
elif isinstance(data, list):
for item in data: build_xml(item, ET.SubElement(parent, 'item'))
else: parent.text = str(data)
build_xml(d, root)
ET.indent(root)
return ET.tostring(root, encoding='unicode')

# %% ../helpers.ipynb 23
doctype = namedtuple('doctype', ['source', 'content'])

# %% ../helpers.ipynb 26
def mk_doctype(content:str, # The document content
source:Optional[str]=None # URL, filename, etc; defaults to `md5(content)` if not provided
) -> namedtuple:
"Create a `doctype` named tuple"
if source is None: source = hashlib.md5(content.encode()).hexdigest()[:8]
return doctype(add_nls(str(source).strip()), add_nls(content.strip()))
Loading

0 comments on commit 25507cb

Please sign in to comment.