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.6 through 3.9.

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

How it Works

  1. Inspects file extension if available.
  2. Inspects shebang
  3. Looks for keywords
  4. Counts regexes for common patterns
  5. Attemps 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 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.2.0.tar.gz (69.8 kB view hashes)

Uploaded Source

Built Distribution

whats_that_code-0.2.0-py3-none-any.whl (73.9 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