Guess programming language from a string or file.
Project description
whats_that_code
This is a Python programming language detection library.
It detects the programming language of source code with an ensemble of classifiers.
Use this when a quick first approximation is good enough.
Accuracy depends heavily on the available hints; checked-in reports live in docs/accuracy.md.
I created this because I wanted
- a Python programming language detector
- no machine learning dependencies
Tested on python 3.10 through 3.14.
Badges
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 at runtime)
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)
plus ~26 tree-sitter grammars that are installed with the main package.
On the evaluation corpus this lifted code-only accuracy from ~13% to ~21% when added. 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
- Inspects file extension if available.
- Inspects shebang
- Looks for keywords
- Counts regexes for common patterns
- Attempts to parse Python, JSON, XML, and TOML
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file whats_that_code-0.3.1.tar.gz.
File metadata
- Download URL: whats_that_code-0.3.1.tar.gz
- Upload date:
- Size: 96.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c09569e103f80da00fee09726c797258d5d5fa143612e8a92da5cc137031e52
|
|
| MD5 |
ecb5f25de47164b4ac35c6b2c67af72e
|
|
| BLAKE2b-256 |
631a89b527187ce7755ae743dbe15d768422c012a9629d5df3af32ab038e98f9
|
Provenance
The following attestation bundles were made for whats_that_code-0.3.1.tar.gz:
Publisher:
release.yml on matthewdeanmartin/whats_that_code
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
whats_that_code-0.3.1.tar.gz -
Subject digest:
6c09569e103f80da00fee09726c797258d5d5fa143612e8a92da5cc137031e52 - Sigstore transparency entry: 2083120619
- Sigstore integration time:
-
Permalink:
matthewdeanmartin/whats_that_code@0a22582ba6ddcdb069aeac7fe6a6b95f3a045427 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/matthewdeanmartin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0a22582ba6ddcdb069aeac7fe6a6b95f3a045427 -
Trigger Event:
release
-
Statement type:
File details
Details for the file whats_that_code-0.3.1-py3-none-any.whl.
File metadata
- Download URL: whats_that_code-0.3.1-py3-none-any.whl
- Upload date:
- Size: 87.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
659b7026723abc08c1ab894192d4ada48598c6c1d11e16737a942660bee81f06
|
|
| MD5 |
80ae9723774a8ce6eb6178bb0ba6413b
|
|
| BLAKE2b-256 |
57525d95e088fda5ce23d3df4694b5d3e16687ea41f4d46e4d01d5045c3b550a
|
Provenance
The following attestation bundles were made for whats_that_code-0.3.1-py3-none-any.whl:
Publisher:
release.yml on matthewdeanmartin/whats_that_code
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
whats_that_code-0.3.1-py3-none-any.whl -
Subject digest:
659b7026723abc08c1ab894192d4ada48598c6c1d11e16737a942660bee81f06 - Sigstore transparency entry: 2083120622
- Sigstore integration time:
-
Permalink:
matthewdeanmartin/whats_that_code@0a22582ba6ddcdb069aeac7fe6a6b95f3a045427 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/matthewdeanmartin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0a22582ba6ddcdb069aeac7fe6a6b95f3a045427 -
Trigger Event:
release
-
Statement type: