Skip to main content

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

Project description

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

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. (Requires the md-toc package: pip install md-toc.)

>>> from titbit import md_toc_string
>>> url = 'https://raw.githubusercontent.com/i2mint/titbit/master/README.md'
>>> toc_string = md_toc_string(url)  # doctest: +SKIP
>>> print(toc_string)  # doctest: +SKIP
- [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:

>>> 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.

>>> from titbit import mermaid_to_graphviz
>>> 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.

>>> 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.

>>> 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

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.
    """

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

titbit-0.1.13.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

titbit-0.1.13-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file titbit-0.1.13.tar.gz.

File metadata

  • Download URL: titbit-0.1.13.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for titbit-0.1.13.tar.gz
Algorithm Hash digest
SHA256 585b5a0651fc8fa8b28be6e45efc25a12ea1c73c3dfc8796fa5cb09a0af00936
MD5 28e92ab6e9226a28a9673593cea9da75
BLAKE2b-256 0228546a4bbe4a98b35a99728c10ceefdab20e70f1f1878ef17eb002645237c7

See more details on using hashes here.

File details

Details for the file titbit-0.1.13-py3-none-any.whl.

File metadata

  • Download URL: titbit-0.1.13-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for titbit-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 e7e1a114464b3e21d59c9b5b8340698d039ae5698ed2ac8aa881c59330225c73
MD5 e88f21cff927e94e9f2be78b36705474
BLAKE2b-256 a213c5f2e4248d4caf9d0b118bc9a29d3a64739fa1ef7e7cc106877d369f1f4d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page