Skip to content

Instantly share code, notes, and snippets.

View jimbaker's full-sized avatar

Jim Baker jimbaker

View GitHub Profile
from types import FunctionType
from typing import *
import textwrap
import dis # temporary debugging
# getvalue, raw, conv, formatspec
@jimbaker
jimbaker / vdomtag.py
Created May 21, 2022 02:22
tag string implementation for ViewDOM
# Don't name this file html.py
from __future__ import annotations
from functools import cache
from types import CodeType
from typing import *
from html.parser import HTMLParser
from viewdom import VDOMNode, render
@jimbaker
jimbaker / idomtag.py
Created May 21, 2022 00:52
tag string implementation for constructing IDOM's `VdomDict`
# Don't name this file html.py
from __future__ import annotations
from functools import cache
from types import CodeType
from typing import *
from html.parser import HTMLParser
from idom.core.vdom import vdom, VdomDict
@jimbaker
jimbaker / fltag.py
Created May 8, 2022 21:47
Implements `fl` tag to support a lazy version of f-strings
# fltag - lazy version of f-string eval
from __future__ import annotations
from dataclasses import dataclass
from functools import cached_property
from typing import *
Thunk = tuple[
Callable[[], Any],
# Extracted from @gvanrossum's gist https://gist.github.com/gvanrossum/a465d31d9402bae2c79e89b2f344c10c
# Demonstrates tag-string functionality, as tracked in https://jimbaker/tagstr
# Requires an implementating branch, as in https://github.com/jimbaker/tagstr/issues/1
# Sample usage:
# from htmltag import html
#
# >>> user = "Bobby<table>s</table>"
# >>> print(html"<div>Hello {user}</div>")
# <div>Hello Bobby&lt;table&gt;s&lt;/table&gt;</div>
@jimbaker
jimbaker / filter-by-resolved-vars.py
Created March 2, 2017 20:37
On current fake data, produces output like the following: {Cell(id=3, name='C0001'), Cell(id=1, name='C0001')}. Use with python -i to explore. Needs obvious expansion/generalization for other resources.
from collections import defaultdict
import sys
from sqlalchemy import create_engine # change to oslo_db
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import tuple_
from craton.db.sqlalchemy import models
engine = create_engine(sys.argv[1] if len(sys.argv) > 1 else 'mysql+pymysql://craton:craton@localhost/craton') #, echo=True)
@jimbaker
jimbaker / craton-wrapper-functions.sh
Last active March 23, 2017 17:24
Wrappers for working with Craton: setting up dev servers, getting env vars, and using the REST API. Simply source, and use like so:$ craton-get v1/hosts. Etc.
# Example usage. Note that we simply pass through httpie conventions,
# such as nested JSON with :=
# (https://github.com/jkbrzt/httpie#non-string-json-fields)
#
# $ craton-post v1/regions name=HKG
# $ craton-get v1/hosts
# $ craton-put v1/hosts/3 device_type=container
# $ craton-put v1/hosts/3/variables foo=47 bar:='["a", "b", "c"]'
# $ craton-delete v1/hosts/4
#
@jimbaker
jimbaker / craton-dev-start.sh
Created January 31, 2017 18:06
Craton dev start script
docker rm -f craton-api || true
docker build --pull -t craton-api:latest .
docker run -t --name craton-api -p 127.0.0.1:8080:8080 -d craton-api:latest
# wait until API server is available by probing it
until curl http://127.0.0.1:8080/v1/regions -H "Content-Type: application/json" -H "X-Auth-Token: demo" -H "X-Auth-User: demo" -H "X-Auth-Project: b9f10eca66ac4c279c139d01e65f96b4"
do
echo "Waiting for API server"; sleep 1
done
@jimbaker
jimbaker / t-policy-using-role-assignments.py
Last active January 9, 2017 17:31
Example script using oslo_policy
from oslo_config import cfg
from oslo_policy import policy as common_policy
CONF = cfg.CONF # should just be an empty config file
ENFORCER = common_policy.Enforcer(CONF)
rules = {
"fleet:audit": "role:admin or (principal:%(principal)s and role_:%(role_)s and resource:%(resource)s)"
}
@jimbaker
jimbaker / interact-craton-modeling.py
Created October 28, 2016 10:18
Script to setup Python console interaction with Craton's Python object modeling, using python/ipython. $ ipython -i interact-craton-modeling.py <<optional db connection string>>
import sys
from sqlalchemy import create_engine # change to oslo_db
from sqlalchemy.orm import sessionmaker
from craton.db.sqlalchemy import models
engine = create_engine(sys.argv[1] if len(sys.argv) > 1 else 'mysql+pymysql://craton:craton@localhost/craton', echo=True)
Session = sessionmaker(bind=engine)
session = Session()