Created
December 13, 2018 21:48
-
-
Save kurtbrose/e14e75d99be3cac5c5512b2ed11e61d9 to your computer and use it in GitHub Desktop.
stevens glom strategy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# -*- coding: utf-8 -*-\n", | |
| "from __future__ import unicode_literals, print_function" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "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": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "st.characters?" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "3" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "glom.glom({1: 1, None: {2: 3}}, glom.Path(glom.Path(None), glom.Path(2)))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "OK_HYPOTHESIS_UNI_ALPHABET = st.characters(\n", | |
| " whitelist_categories=('Ll'),\n", | |
| " blacklist_categories=(\n", | |
| " 'Cs',\n", | |
| " 'Co',\n", | |
| " 'Cn',\n", | |
| " 'Cc',\n", | |
| " # blacklisting 'Cc' because still not able to get examples like u'\\x00/' to\n", | |
| " # work. u'\\x00/' breaks csv writer, it does not record the '/'.\n", | |
| "))\n", | |
| "\n", | |
| "\n", | |
| "def can_ascii_encode(txt):\n", | |
| " try:\n", | |
| " txt.encode('ascii')\n", | |
| " return True\n", | |
| " except UnicodeEncodeError:\n", | |
| " return False\n", | |
| "\n", | |
| "\n", | |
| "SOME_TEXT_ST = st.text(alphabet=OK_HYPOTHESIS_UNI_ALPHABET).filter(can_ascii_encode)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "values = st.one_of(st.none(), st.booleans(), st.integers(), st.floats(), SOME_TEXT_ST)\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": 8, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "class Null(object):\n", | |
| " pass" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "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": 10, | |
| "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": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'eb5cb22a-440c-4c3b-92bf-c7d709050f32'" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "target, spec = b.example()\n", | |
| "glom.glom(target=target, spec=spec)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Path(True, u'', u'key')\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{None: {-1.175494351e-38: True,\n", | |
| " 2237739041955156319: {u'key': 'eb5cb22a-440c-4c3b-92bf-c7d709050f32'},\n", | |
| " u'': {u'key': 'eb5cb22a-440c-4c3b-92bf-c7d709050f32'}},\n", | |
| " -2.7875700146040645e-52: {-1.175494351e-38: True,\n", | |
| " 2237739041955156319: {u'key': 'eb5cb22a-440c-4c3b-92bf-c7d709050f32'},\n", | |
| " u'': {u'key': 'eb5cb22a-440c-4c3b-92bf-c7d709050f32'}},\n", | |
| " 0.5: {-1.175494351e-38: True,\n", | |
| " 2237739041955156319: {u'key': 'eb5cb22a-440c-4c3b-92bf-c7d709050f32'},\n", | |
| " u'': {u'key': 'eb5cb22a-440c-4c3b-92bf-c7d709050f32'}},\n", | |
| " True: {-1.175494351e-38: True,\n", | |
| " 2237739041955156319: {u'key': 'eb5cb22a-440c-4c3b-92bf-c7d709050f32'},\n", | |
| " u'': {u'key': 'eb5cb22a-440c-4c3b-92bf-c7d709050f32'}},\n", | |
| " u'': False}" | |
| ] | |
| }, | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "print(spec)\n", | |
| "target" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 271, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "((((True,\n", | |
| " ((True, ((False, True), False)),\n", | |
| " ((((((True, False), True),\n", | |
| " (False,\n", | |
| " ((True, False), ((True, (((False, False), True), False)), True)))),\n", | |
| " False),\n", | |
| " True),\n", | |
| " False))),\n", | |
| " True),\n", | |
| " (True,\n", | |
| " (True,\n", | |
| " (((False,\n", | |
| " (True,\n", | |
| " (True,\n", | |
| " (((True, False), False),\n", | |
| " (((((False, (False, False)), (True, True)), True),\n", | |
| " ((False, True), (((False, False), (False, False)), False))),\n", | |
| " False))))),\n", | |
| " ((False, ((True, (False, True)), ((True, False), (True, True)))),\n", | |
| " ((True,\n", | |
| " (((((True, (False, (False, False))),\n", | |
| " (((False, False), False), ((False, False), True))),\n", | |
| " (True, False)),\n", | |
| " (False, ((True, False), True))),\n", | |
| " True)),\n", | |
| " (True, False)))),\n", | |
| " False)))),\n", | |
| " (True, True))" | |
| ] | |
| }, | |
| "execution_count": 271, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| ">>> a = st.deferred(lambda: st.booleans() | b)\n", | |
| ">>> b = st.deferred(lambda: st.tuples(a, a))\n", | |
| ">>> a.example()\n", | |
| "True\n", | |
| ">>> b.example()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment