Skip to content

Instantly share code, notes, and snippets.

@steven-cutting
Last active December 13, 2018 22:58
Show Gist options
  • Save steven-cutting/96a42be9d98cc8584c135450440099a4 to your computer and use it in GitHub Desktop.
Save steven-cutting/96a42be9d98cc8584c135450440099a4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# -*- coding: utf-8 -*-\n",
"from __future__ import unicode_literals, print_function"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import collections\n",
"import uuid\n",
"\n",
"import hypothesis\n",
"import hypothesis.strategies as st\n",
"\n",
"import glom"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"_values_base = st.one_of(st.none(), st.booleans(), st.integers(), st.floats(), st.text())\n",
"values = st.deferred(lambda: st.one_of(_values_a, st.lists(_values_base)))\n",
"hashable = values.filter(lambda n: isinstance(n, collections.Hashable))\n",
"\n",
"some_dict = st.dictionaries(keys=hashable, values=values, min_size=0)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"class Null(object):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"@st.composite\n",
"def gen_glom_simple_dict_layer(draw, child_target_spec=({}, Null())):\n",
" if isinstance(child_target_spec, st.OneOfStrategy):\n",
" child_target_spec = draw(child_target_spec)\n",
" child_target, child_spec = child_target_spec\n",
" target_dict = draw(some_dict)\n",
" with_child = draw(st.dictionaries(keys=hashable,\n",
" values=st.just(child_target), min_size=1))\n",
" target_dict.update(with_child)\n",
" spec = glom.Path(draw(st.sampled_from(with_child.keys())))\n",
" if not isinstance(child_spec, Null):\n",
" spec = glom.Path(spec, child_spec)\n",
" return (target_dict, spec)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"target_value = str(uuid.uuid4())\n",
"a = st.deferred(lambda: gen_glom_simple_dict_layer(({'key': target_value}, 'key')) | b)\n",
"b = st.deferred(lambda: gen_glom_simple_dict_layer(a))"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'be311e8d-a1a8-4557-aa38-815f1abe5ba4'"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"target, spec = b.example()\n",
"glom.glom(target=target, spec=spec)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Path(inf, True, u'key')\n"
]
},
{
"data": {
"text/plain": [
"{-1.1: {-1.192092896e-07: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" True: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" 32310: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" u'&\\r': 115},\n",
" -0.3333333333333333: {-1.192092896e-07: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" True: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" 32310: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" u'&\\r': 115},\n",
" False: {-1.192092896e-07: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" True: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" 32310: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" u'&\\r': 115},\n",
" inf: {-1.192092896e-07: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" True: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" 32310: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" u'&\\r': 115},\n",
" u'\\ub6d2.\\U00093f87\\U000d3c16\\u9ea1(/\\U000946f5': {-1.192092896e-07: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" True: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" 32310: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" u'&\\r': 115},\n",
" u'\\U0003c9c2\\U000f663e/\\U000f2e21\\u3272\\U0002493c\\U000d0046#\\U0008cd52\\U000c4b24\\U000a5a22\\U00057016\\U00094c0a\\U000d0010\\x1f\\U000f060b\\n\\U00067928\\U00078187': {-1.192092896e-07: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" True: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" 32310: {u'key': 'be311e8d-a1a8-4557-aa38-815f1abe5ba4'},\n",
" u'&\\r': 115}}"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(spec)\n",
"target"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment