Skip to main content

Python type inference from a stream of data

Project description

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

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

slow_learner-0.1.3.tar.gz (26.6 kB view hashes)

Uploaded Source

Built Distribution

slow_learner-0.1.3-py3-none-any.whl (24.5 kB view hashes)

Uploaded Python 3

Supported by

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