Skip to main content

This is style50, with which code can be checked against the CS50 style guide

Project description

style50

This is style50, a tool with which code can be checked against the CS50 style guide.

Installation

pip install style50

In order to style check C, C++, or Java code, a recent version (>=14.0.0) of clang-format must be installed. clang-format may be downloaded here.

Windows

Along with most of CS50's command line tools, style50 supports being run on Windows but only via the Linux Subsystem in Windows 10. After launching it, style50 can be installed using the pip command above.

Usage

usage: style50 [-h] [-o MODE] [-v] [-V] [-E] [-i PATTERN] file [file ...]

positional arguments:
file                  file or directory to lint

optional arguments:
-h, --help            show this help message and exit
-o MODE, --output MODE
                        output mode, which can be character (default), split,
                        unified, score, or json
-v, --verbose         print full tracebacks of errors
-V, --version         show program's version number and exit
-E, --extensions      print supported file extensions (as JSON list) and
                        exit
-i PATTERN, --ignore PATTERN
                        paths/patterns to be ignored

character, split, and unified modes output character-based, side-by-side, and unified (respectively) diffs between the inputted file and the correctly styled version. score outputs the raw percentage of correct (unchanged) lines, while json outputs a json object containing information pertinent to the CS50 IDE plugin (coming soon).

Language Support

style50 currently supports the following languages:

  • C++
  • C
  • Python
  • Javascript
  • Java

Adding a new language

Adding a new language is very simple. Language checks are encoded as classes which inherit from the StyleCheck base class (see style50/languages.py for more real-world examples). The following is a template for style checks which allows style50 to check the imaginary FooBar language for style.

import re

from style50 import StyleCheck, Style50


class FooBar(StyleCheck):

    # REQUIRED: this property informs style50 what file extensions this
    # check should be run on (in this case, all .fb and .foobar files)
    extensions = ["fb", "foobar"]

    # REQUIRED: should return a correctly styled version of `code`
    def style(self, code):
        # All FooBar code is perfectly styled
        return code

    # OPTIONAL: should return the number of comments in `code`.
    # If this function is not defined, `style50` will not warn the student about
    # too few comments
    def count_comments(self, code):
        # A real-world, check would need to worry about not counting '#' in string-literals
        return len(re.findall(r"#.*", code))

All classes which inherit from StyleCheck are automatically registered with style50's Style50 class, making style50 easily extensible. Adding the following to the above code creates a script which checks the code that style50 already does as well as FooBar programs.

    # Style check the current directory, printing a unified diff
    Style50("unified").run(["."])

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

style50-2.8.0.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

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

style50-2.8.0-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file style50-2.8.0.tar.gz.

File metadata

  • Download URL: style50-2.8.0.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for style50-2.8.0.tar.gz
Algorithm Hash digest
SHA256 5c16005b1e546deabdc0d5c56ad216586763d728cd6450750edd721ac8ef362d
MD5 58147f69d45f5b2596bb823359208c94
BLAKE2b-256 f631dbb289b6e528e609c105bb3a14ae4a4160b1d5c6c495923ab1f5100af3f7

See more details on using hashes here.

File details

Details for the file style50-2.8.0-py3-none-any.whl.

File metadata

  • Download URL: style50-2.8.0-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for style50-2.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39a3dc2030ecd673633c2c1e07bcf1ab944cefcec9a91da337f23ce4a700acae
MD5 aeba3539ccbaa06d539af01f93aadc74
BLAKE2b-256 6ea95347390dcddf5bcf200a802ef797ee4abc50a0bc2486d0b9f79a961b9691

See more details on using hashes here.

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