{"id":15009925,"url":"https://github.com/jisazatappsi/shatter","last_synced_at":"2025-06-23T17:10:16.543Z","repository":{"id":2436881,"uuid":"46546448","full_name":"jisazaTappsi/shatter","owner":"jisazaTappsi","description":"Data Driven Development","archived":false,"fork":false,"pushed_at":"2022-12-27T15:00:44.000Z","size":3447,"stargazers_count":1,"open_issues_count":4,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T18:06:26.953Z","etag":null,"topics":["algorithm","data-driven","deep-learning","machine-learning","programming-language","python-3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jisazaTappsi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-20T07:25:54.000Z","updated_at":"2023-02-25T23:38:55.000Z","dependencies_parsed_at":"2023-01-13T11:51:39.083Z","dependency_job_id":null,"html_url":"https://github.com/jisazaTappsi/shatter","commit_stats":null,"previous_names":["jisazatappsi/booleansolver"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jisazaTappsi/shatter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jisazaTappsi%2Fshatter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jisazaTappsi%2Fshatter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jisazaTappsi%2Fshatter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jisazaTappsi%2Fshatter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jisazaTappsi","download_url":"https://codeload.github.com/jisazaTappsi/shatter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jisazaTappsi%2Fshatter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261519081,"owners_count":23171228,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","data-driven","deep-learning","machine-learning","programming-language","python-3"],"created_at":"2024-09-24T19:29:09.224Z","updated_at":"2025-06-23T17:10:11.532Z","avatar_url":"https://github.com/jisazaTappsi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Shatter\n=============\n\nData driven programming; input data and output nice functional code ;)\n\n\nIntroduction\n=============\n\nThis is a [python 3.6+ project](https://pypi.python.org/pypi/shatter) that uses algorithms to transform a set of\nconditions into functional python code. See some [examples](https://github.com/jisazaTappsi/shatter/tree/master/examples).\n\n\nPackage Setup\n-------------\n\nInstall:\n\n        $ pip install shatter\n\n\nDependencies\n------------\n\n    pyeda==0.28.0\n    pandas==0.19.1\n    sympy==1.1\n    Keras==2.0.6\n    numpy==1.13.1\n    pip==9.0.1\n    scikit_learn==0.19.0\n\n\nExamples\n=============\n\n\nGet Started\n-------------\nCopy paste this snippet:\n\n    from shatter.solver import Rules\n\n\n    def my_func(a, b):\n        pass\n    \n    r = Rules(a=True, b=True, output=True)\n    r.solve(my_func)\n\nRun it and see how `my_func` code changes from `pass` to `return a and b`. We just\nspecified that when `a` and `b` are true then the output should be `True`, that is equivalent to the\nlogical `and` operator.\n\nWe can add further conditions and `shatter` will compute the optimal function to get there.\n\n\nAdding more conditions\n-------------\n\nNow we add 2 additional conditions with `r.add()`:\n\n    from shatter.solver import Rules\n\n\n    def my_func(a, b):\n        pass\n    \n    \n    r = Rules(a=True, b=True, output=True)\n    r.add(a=False, b=True, output=True)\n    r.add(a=True, b=False, output=True)\n    r.solve(my_func)\n\nIn this case the solution is `a or b`.\n\n\nIf conditionals\n-------------\n\nWhat if the output for a given logical condition is not a boolean? In that case a programmer would use an if.\nIn the next example this package solves this case:\n\nChange output to `1`:\n\n    from shatter.solver import Rules\n\n\n    def my_func(a, b):\n        pass\n    \n    r = Rules(a=True, b=True, output=1)\n    r.solve(my_func)\n\n\nThe solution will be:\n\n    def my_func(a, b):\n\n        if a and b:\n            return 1\n    \n        return False\n\nReturns `1` or `False` otherwise.\n\n\nAdding pieces of code\n-------------\n\nSay you want to add a arbitrary piece of code that evaluates to boolean, then:\n\n    from shatter.solver import Rules, Code\n\n\n    def any_code(a):\n        pass\n    \n    r = Rules(condition=Code(code_str='isinstance(a, str)'), output=2)\n    r.solve(any_code)\n\nThe result should be:\n\n    def internal_code(a):\n    \n        if isinstance(a, str):\n            return 2\n    \n        return False\n\nHere the piece of code `isinstance(a, str)` was added as the if condition to output `2`\n\n\nIteration\n-------------\n\nRun this code:\n\n    from shatter.solver import Rules, Code, Output\n\n\n    def recursive(a):\n        pass\n    \n    a = Code()\n    args = {'a': a + 1}\n    out = Output(function=recursive, arguments=args)\n    \n    r = Rules(stopping_condition=  a \u003e 2, output=a, default=out)\n    solution = r.solve(recursive)\n\nThe result this time will be a recursive counting function :)\n\n    def recursive(a):\n\n        if a \u003e 2:\n            return a\n    \n        return recursive(a + 1)\n\nWith `a = Code()` variable `a` is initialized as a code piece. Then with\n\n    args = {'a': a + 1}\n\nA dictionary for the inputs of the `recursive` function is declared. Those inputs are fed into a `Output` object:\n\n    out = Output(function=recursive, arguments=args)\n\nAfter `out` is passed via `default` keyword when initializing the `Rules` object. This `default` keyword \nis used to override the last return statement of the `recursive` function.\n\n\nSolve Small ML problem\n----------------------\n\nCopy paste this snippet:\n\n    import pandas as pd\n    from sklearn import datasets\n    from shatter.solver import Rules, solve\n\n\n    @solve()\n    def solve_iris(x1, x2, x3, x4):\n        pass\n\n\n    iris = datasets.load_iris()\n\n    x = iris.data\n    y = iris.target\n\n    data_frame = pd.DataFrame(x, columns=['x1', 'x2', 'x3', 'x4'])\n\n    # Make binary and add to df\n    data_frame['output'] = [int(bool(e)) for e in y]\n\n\n    print(data_frame)\n\n    r = Rules(data_frame)\n\n    solution = r.solve(solve_iris)\n\n\nOutputs:\n\n    def solve_iris():\n        return x3 \u003e= 2.45\n\n\nGoing deeper\n=============\n\nSetup\n-------------\n\nClone repository:\n\n    `git clone git@github.com:jisazaTappsi/shatter.git`\n\nMore examples\n-------------\n\nSee [examples](https://github.com/jisazaTappsi/shatter/tree/master/examples).\n\n\nHow does shatter work?\n-------------\n\nTakes a function and a truth table which is processed using the\n[Quine-McCluskey Algorithm](https://en.wikipedia.org/wiki/Quine%E2%80%93McCluskey_algorithm).\nThen finds an optimal boolean expression. This expression is inserted in the method definition.\n\n\nRules Class\n=============\n\nIs initialized with one rule. Other rules can be added with `Rules.add()` method. To generate\nthe solution call `Rules.solve()` method.\n\nEach rule\n-------------\n\nThe arguments of each rule are specified as optional arguments inside a `Rules` constructor or inside a\n`Rules.add()` call. There are reserved keywords:\n\n`output`: Determines the value to be returned when the given condition is True.\n\n`output_args`: Dictionary with the values for the arguments when output is a function.\n \n`default`: Value returned when non of the rules are True.\n\n\nArguments of `Rules.solve()`\n-------------\n\n - `function`: passed as a callable. This function is going to be filled with the solution to the present task.\n \n - `unittest=None`: Test Case to be able to run and test the code generated each time the test runs.\nSee [example](https://github.com/jisazaTappsi/shatter/tree/master/examples/with_tests) for a deeper understanding.\n\n\nOutput Class\n-------------\n\n`solver.Output`: Class that helps define a function with arguments as an output. Has fields:\n  \n  - `function`: A callable object.\n  - `arguments` Dictionary with the function inputs.\n\nCode class\n-------------\n\n`solver.Code`: Class that helps represent pieces of code. The code is fed as a string (with optional argument `str_code`)\nor it can be declared as variables. eg:\n\n    from shatter.solver import Code\n    \n    a = Code()\n    b = Code()\n    print(a \u003e b)\n\nThis will literally print the code `a \u003e b` rather than the objects or any result.\n\n\nSolution class\n-------------\n\n`solver.Solution`: Class that contains the solution of the problem it includes:\n    \n  - `rules`: The information given by the user.\n  - `implementation`: Plain code.\n  - `ast`: Abstract syntax tree\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjisazatappsi%2Fshatter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjisazatappsi%2Fshatter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjisazatappsi%2Fshatter/lists"}