Skip to main content

Guess programming language from a string or file.

Project description

whats_that_code

This is a programming language detection library.

It will detect programming language of source in pure python from an ensemble of classifiers. Use this when a quick and dirty first approximation is good enough. whats_that_code can currently identify 60%+ of samples without knowing the extension or tag.

I created this because I wanted

  • a pure python programming language detector
  • no machine learning dependencies

Tested on python 3.10 through 3.14.

Badges

Libraries.io SourceRank

Downloads

CodeFactor

Usage

from whats_that_code.election import guess_language_all_methods
code = "def yo():\n   print('hello')"
result = guess_language_all_methods(code, file_name="yo.py")
assert result == "python"  # returns a single language name (str), or None if unknown

Suppressing rare languages (opt-in)

Pass an Options to avoid guessing obscure languages unless there is strong evidence (a matching file extension, shebang, or tag). The default is unchanged — no suppression — so existing callers are unaffected.

from whats_that_code.election import guess_language_all_methods
from whats_that_code.options import Options

# "common" keeps only mainstream languages; "uncommon" also keeps established ones.
guess_language_all_methods(code, options=Options(min_tier="common"))

# A .zig file is still detected as Zig even though Zig is below "common":
guess_language_all_methods(code, file_name="x.zig", options=Options(min_tier="common"))  # -> "zig"

Speed

Regex marker matching (the hottest path) runs on Google's re2 — a normal dependency, installed automatically — which is linear-time and ~6× faster on large inputs, with identical results. If a platform has no re2 wheel it transparently falls back to stdlib re. Nothing to configure.

The parser trick (optional)

With Options(use_parsers=True) the code is actually parsed; a clean parse is strong evidence for that language. This uses stdlib parsers (Python/JSON/XML/TOML) out of the box, and adds ~26 tree-sitter grammars when the optional extra is installed:

pip install whats_that_code[fast]

On the eval corpus this lifts code-only accuracy from ~13% to ~21%. It is off by default, so existing callers are unaffected.

guess_language_all_methods(go_source, options=Options(use_parsers=True))  # -> "go"

How it Works

  1. Inspects file extension if available.
  2. Inspects shebang
  3. Looks for keywords
  4. Counts regexes for common patterns
  5. Attempts to parse python, json, yaml
  6. Inspects tags if available.

Each is imperfect and can error. The classifier then combines the results of each using a voting algorithm

This works best if you only use it for fallback, e.g. classifying code that can't already be classified by extension or tag, or when tag is ambiguous.

It was a tool that outgrew being a part of so_pip a StackOverflow code extraction tool I wrote.

Docs

Notable Similar Tools

  • Guesslang - python and tensorflow driven solution. Reasonable results but slow startup and not pure python.
  • pygments pure python, but sometimes lousy identification rates.

Project Links

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

whats_that_code-0.3.0.tar.gz (95.5 kB view details)

Uploaded Source

Built Distribution

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

whats_that_code-0.3.0-py3-none-any.whl (87.2 kB view details)

Uploaded Python 3

File details

Details for the file whats_that_code-0.3.0.tar.gz.

File metadata

  • Download URL: whats_that_code-0.3.0.tar.gz
  • Upload date:
  • Size: 95.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for whats_that_code-0.3.0.tar.gz
Algorithm Hash digest
SHA256 dbbe612375108a276d75349bb3b65850c4129cf5fae9c064842b395fd79539de
MD5 2ee7e757d622a87bf98da6f9f0f988c9
BLAKE2b-256 6b0f728bc162648b5ab3d73396cfabe3ee673d055063e4cddf8325b0569887a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for whats_that_code-0.3.0.tar.gz:

Publisher: publish_to_pypi.yml on matthewdeanmartin/whats_that_code

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file whats_that_code-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: whats_that_code-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 87.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for whats_that_code-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b664c8722327537bb75e2db36c8e59e049f7a6cc783453c7c12ea8cb216f096a
MD5 db6d3596a8d037ac9ce63cc10c0adf2f
BLAKE2b-256 b9bf9dc0b0b857db8295687a7be5a2ae7329659de721b3d83886c5ee7c688023

See more details on using hashes here.

Provenance

The following attestation bundles were made for whats_that_code-0.3.0-py3-none-any.whl:

Publisher: publish_to_pypi.yml on matthewdeanmartin/whats_that_code

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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