titbit


Nametitbit JSON
Version 0.1.9 PyPI version JSON
download
home_pagehttps://github.com/i2mint/titbit
SummaryA place to dump might-be-useful-again code as an alternative of leaving in a notebook where it will never be found again
upload_time2024-11-25 14:23:51
maintainerNone
docs_urlNone
authorOtoSense
requires_pythonNone
licenseapache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # titbit

A place to dump might-be-useful-again code as an alternative of leaving in a notebook where it will never be found again

To install: `pip install titbit`

[Documentation](https://i2mint.github.io/titbit/)

# Examples

## md_toc_string

Generate a markdown table of contents (TOC) string from markdown.
The markdown can be a string, a filepath, or a URL.

```python
>>> url = 'https://raw.githubusercontent.com/i2mint/titbit/master/README.md'
>>> toc_string = md_toc_string(url)
>>> print(toc_string)
```

```
- [titbit](#titbit)
- [Examples](#examples)
    - [md_toc_string](#md_toc_string)
    - [git_action_on_projects](#git_action_on_projects)
    - [mermaid_to_graphviz](#mermaid_to_graphviz)
    - [filter_code](#filter_code)
    - [bound_properties_refactor](#bound_properties_refactor)
    - [ensure_ast](#ensure_ast)
```

## git_action_on_projects

Take git actions all the projects in the list of projects.
A project can be a folder path or a module, or the name of a module/package.

Tip: Use `functools.partial` to set the `action`, `on_error` and `egress` and get
the function you need to perform bulk actions.

Usage:

```python
>>> from titbit import git_action_on_projects
>>> projects = [
...     some_package, "some_package_name", "some_package_dir_path"
... ]  # doctest: +SKIP
>>> # By default, the git action performed is to pull
>>> git_action_on_projects(projects)  # doctest: +SKIP
```

## mermaid_to_graphviz

Converts mermaid code to graphviz code.

```python
>>> mermaid_code = '''
... graph TD
... A --> B & C
... B & C --> D
... '''
>>> graphviz_code = mermaid_to_graphviz(mermaid_code)
>>> print(graphviz_code)  # doctest: +NORMALIZE_WHITESPACE
digraph G {
    A -> B , C
    B , C -> D
}
>>> mermaid_code = '''
... graph TD
...     A[Score] --> B[Part]
...     B --> C[Measure]
... '''
>>> print(mermaid_to_graphviz(mermaid_code))
digraph G {
    A [label="Score"]
    B [label="Part"]
    A -> B
    C [label="Measure"]
    B -> C
}
```

## filter_code

Produce a version of the code that is filtered by the filt filter function.

```python
>>> from titbit import filter_code
>>> code_str = '''
... a = b + c
... print(a)
... d[0] = a
... for i in range(1, 10):
...     d[i] = d[i-1] + b
... def f(x):
...     return x + 1
... e = [d, a, f]
... '''
>>> import ast
>>> filtered_code_str = filter_code(code_str, lambda x: isinstance(x, ast.Assign))
>>> assert filtered_code_str.strip() == ('''
... a = b + c
... d[0] = a
... e = [d, a, f]
... '''.strip())
```

## bound_properties_refactor

Generate code that refactors "flat code" into a reusable "controller" class.
Also checkout the `BoundPropertiesRefactor` class that does all the work:
With it, you'll be able to compute intermediate datas that may be of interest.

```python
>>> from titbit import bound_properties_refactor
>>> code_str = '''
... apple = banana + carrot
... date = 'banana'
... egg = apple * 2
... egg = egg + 1
... '''
>>>
>>> refactored_code = bound_properties_refactor(code_str)
>>> print(refactored_code)  # doctest: +NORMALIZE_WHITESPACE
@property
def apple(self):
    return banana + carrot
<BLANKLINE>
date = 'banana'
<BLANKLINE>
@property
def egg(self):
    egg = self.apple * 2
    egg = egg + 1
    return egg
<BLANKLINE>
```

## ensure_ast

```python
def ensure_ast(code: AST) -> AST:
    """
    Ensures that the input is an AST node, returning it as-is if already an AST.

    If input is a string, parses it as Python code and returns the resulting AST.
    If the input is a module object, it will get the code, parse it, and return an AST.
    """
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/i2mint/titbit",
    "name": "titbit",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "OtoSense",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/25/47/4a061ceedf9409340bfbe6aa66b8653cd6f3f0c25ae16d244232640f688f/titbit-0.1.9.tar.gz",
    "platform": null,
    "description": "# titbit\n\nA place to dump might-be-useful-again code as an alternative of leaving in a notebook where it will never be found again\n\nTo install: `pip install titbit`\n\n[Documentation](https://i2mint.github.io/titbit/)\n\n# Examples\n\n## md_toc_string\n\nGenerate a markdown table of contents (TOC) string from markdown.\nThe markdown can be a string, a filepath, or a URL.\n\n```python\n>>> url = 'https://raw.githubusercontent.com/i2mint/titbit/master/README.md'\n>>> toc_string = md_toc_string(url)\n>>> print(toc_string)\n```\n\n```\n- [titbit](#titbit)\n- [Examples](#examples)\n    - [md_toc_string](#md_toc_string)\n    - [git_action_on_projects](#git_action_on_projects)\n    - [mermaid_to_graphviz](#mermaid_to_graphviz)\n    - [filter_code](#filter_code)\n    - [bound_properties_refactor](#bound_properties_refactor)\n    - [ensure_ast](#ensure_ast)\n```\n\n## git_action_on_projects\n\nTake git actions all the projects in the list of projects.\nA project can be a folder path or a module, or the name of a module/package.\n\nTip: Use `functools.partial` to set the `action`, `on_error` and `egress` and get\nthe function you need to perform bulk actions.\n\nUsage:\n\n```python\n>>> from titbit import git_action_on_projects\n>>> projects = [\n...     some_package, \"some_package_name\", \"some_package_dir_path\"\n... ]  # doctest: +SKIP\n>>> # By default, the git action performed is to pull\n>>> git_action_on_projects(projects)  # doctest: +SKIP\n```\n\n## mermaid_to_graphviz\n\nConverts mermaid code to graphviz code.\n\n```python\n>>> mermaid_code = '''\n... graph TD\n... A --> B & C\n... B & C --> D\n... '''\n>>> graphviz_code = mermaid_to_graphviz(mermaid_code)\n>>> print(graphviz_code)  # doctest: +NORMALIZE_WHITESPACE\ndigraph G {\n    A -> B , C\n    B , C -> D\n}\n>>> mermaid_code = '''\n... graph TD\n...     A[Score] --> B[Part]\n...     B --> C[Measure]\n... '''\n>>> print(mermaid_to_graphviz(mermaid_code))\ndigraph G {\n    A [label=\"Score\"]\n    B [label=\"Part\"]\n    A -> B\n    C [label=\"Measure\"]\n    B -> C\n}\n```\n\n## filter_code\n\nProduce a version of the code that is filtered by the filt filter function.\n\n```python\n>>> from titbit import filter_code\n>>> code_str = '''\n... a = b + c\n... print(a)\n... d[0] = a\n... for i in range(1, 10):\n...     d[i] = d[i-1] + b\n... def f(x):\n...     return x + 1\n... e = [d, a, f]\n... '''\n>>> import ast\n>>> filtered_code_str = filter_code(code_str, lambda x: isinstance(x, ast.Assign))\n>>> assert filtered_code_str.strip() == ('''\n... a = b + c\n... d[0] = a\n... e = [d, a, f]\n... '''.strip())\n```\n\n## bound_properties_refactor\n\nGenerate code that refactors \"flat code\" into a reusable \"controller\" class.\nAlso checkout the `BoundPropertiesRefactor` class that does all the work:\nWith it, you'll be able to compute intermediate datas that may be of interest.\n\n```python\n>>> from titbit import bound_properties_refactor\n>>> code_str = '''\n... apple = banana + carrot\n... date = 'banana'\n... egg = apple * 2\n... egg = egg + 1\n... '''\n>>>\n>>> refactored_code = bound_properties_refactor(code_str)\n>>> print(refactored_code)  # doctest: +NORMALIZE_WHITESPACE\n@property\ndef apple(self):\n    return banana + carrot\n<BLANKLINE>\ndate = 'banana'\n<BLANKLINE>\n@property\ndef egg(self):\n    egg = self.apple * 2\n    egg = egg + 1\n    return egg\n<BLANKLINE>\n```\n\n## ensure_ast\n\n```python\ndef ensure_ast(code: AST) -> AST:\n    \"\"\"\n    Ensures that the input is an AST node, returning it as-is if already an AST.\n\n    If input is a string, parses it as Python code and returns the resulting AST.\n    If the input is a module object, it will get the code, parse it, and return an AST.\n    \"\"\"\n```\n",
    "bugtrack_url": null,
    "license": "apache-2.0",
    "summary": "A place to dump might-be-useful-again code as an alternative of leaving in a notebook where it will never be found again",
    "version": "0.1.9",
    "project_urls": {
        "Homepage": "https://github.com/i2mint/titbit"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a38193449b6d0177610eb37a366f6d39bef16a984fd8e06edb3d52e9a1d88d56",
                "md5": "3767acaea491b9f692660b8ee5c77703",
                "sha256": "1cc9a198c46838b5d56b9df85644cc13c56f3028521a9c4a84651e2267c5c649"
            },
            "downloads": -1,
            "filename": "titbit-0.1.9-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3767acaea491b9f692660b8ee5c77703",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 15832,
            "upload_time": "2024-11-25T14:23:50",
            "upload_time_iso_8601": "2024-11-25T14:23:50.653970Z",
            "url": "https://files.pythonhosted.org/packages/a3/81/93449b6d0177610eb37a366f6d39bef16a984fd8e06edb3d52e9a1d88d56/titbit-0.1.9-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "25474a061ceedf9409340bfbe6aa66b8653cd6f3f0c25ae16d244232640f688f",
                "md5": "a130edaf10e2c636efd1cd8ae4084cde",
                "sha256": "7b639f83f4f307c8088ae503ed400c012aabf61ab66eb916b3616234747ff7d2"
            },
            "downloads": -1,
            "filename": "titbit-0.1.9.tar.gz",
            "has_sig": false,
            "md5_digest": "a130edaf10e2c636efd1cd8ae4084cde",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 14683,
            "upload_time": "2024-11-25T14:23:51",
            "upload_time_iso_8601": "2024-11-25T14:23:51.821545Z",
            "url": "https://files.pythonhosted.org/packages/25/47/4a061ceedf9409340bfbe6aa66b8653cd6f3f0c25ae16d244232640f688f/titbit-0.1.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-25 14:23:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "i2mint",
    "github_project": "titbit",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "titbit"
}
        
Elapsed time: 1.43863s