slow-learner

Python type inference from a stream of data


License
AGPL-3.0
Install
pip install slow-learner==0.1.3

Documentation

slow-learner — python type inference tool

See also: post

A library and CLI to consume a stream of values (for CLI — JSON documents) and generate Python types describing it. Features:

  • recursion into mappings and collections with generic types generation
  • "structured dicts" are turned into TypedDicts by default
  • values with a small set of observed values are turned into Literals

Installation

pip install slow-learner

Usage

As CLI:

slow-learner learn 1.json 2.json 3.json

# to learn the type of list item
slow-learner learn --spread list.json

In Python:

from slow_learner import TypeLearner

tl = TypeLearner(
    max_literal_type_size=5,
    learn_typed_dicts=True,
    max_typed_dict_size=50,
    max_recursive_type_depth=5,
    no_literal_patterns=[r"\.password", r".*secret"],
)

for value in my_values:
    tl.observe(value)

tl.save_type_definition("result.py", "MyType")