Skip to main content

fprettify

Build Status Coverage Status License: GPL v3 PyPI version

fprettify is an auto-formatter for modern Fortran code that imposes strict whitespace formatting, written in Python.

Features

  • Auto-indentation.
  • Line continuations are aligned with the previous opening delimiter (, [ or (/ or with an assignment operator = or =>. If none of the above is present, a default hanging indent is applied.
  • Consistent amount of whitespace around operators and delimiters.
  • Removal of extraneous whitespace and consecutive blank lines.
  • Change letter case (upper case / lower case conventions) of intrinsics
  • Tested for editor integration.
  • By default, fprettify causes whitespace changes only and thus preserves revision history.
  • fprettify can handle cpp and fypp preprocessor directives.

Limitations

  • Works only for modern Fortran (Fortran 90 upwards).
  • Feature missing? Please create an issue.

Requirements

  • Python 2.7 or Python 3.x
  • ConfigArgParse: optional, enables use of config file

Examples

Compare examples/*before.f90 (original Fortran files) with examples/*after.f90 (reformatted Fortran files) to see what fprettify does. A quick demonstration:

program demo
integer :: endif,if,elseif
integer,DIMENSION(2) :: function
endif=3;if=2
if(endif==2)then
endif=5
elseif=if+4*(endif+&
2**10)
elseif(endif==3)then
function(if)=endif/elseif
print*,endif
endif
end program

⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩ fprettify ⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩

program demo
   integer :: endif, if, elseif
   integer, DIMENSION(2) :: function
   endif = 3; if = 2
   if (endif == 2) then
      endif = 5
      elseif = if + 4*(endif + &
                       2**10)
   elseif (endif == 3) then
      function(if) = endif/elseif
      print *, endif
   endif
end program

Installation

The latest release can be installed using pip:

pip install --upgrade fprettify

Installation from source requires Python Setuptools:

./setup.py install

For local installation, use --user option.

Command line tool

Autoformat file1, file2, ... inplace by

fprettify file1, file2, ...

The default indent is 3. If you prefer something else, use --indent n argument.

In order to apply fprettify recursively to an entire Fortran project instead of a single file, use the -r option.

For more options, read

fprettify -h

Editor integration

For editor integration, use

fprettify --silent

For instance, with Vim, use fprettify with gq by putting the following commands in your .vimrc:

autocmd Filetype fortran setlocal formatprg=fprettify\ --silent

Deactivation and manual formatting (experimental feature)

fprettify can be deactivated for selected lines: a single line followed by an inline comment starting with !& is not auto-formatted and consecutive lines that are enclosed between two comment lines !&< and !&> are not auto-formatted. This is useful for cases where manual alignment is preferred over auto-formatting. Furthermore, deactivation is necessary when non-standard Fortran syntax (such as advanced usage of preprocessor directives) prevents proper formatting. As an example, consider the following snippet of fprettify formatted code:

A = [-1, 10, 0, &
     0, 1000, 0, &
     0, -1, 1]

In order to manually align the columns, fprettify needs to be deactivated by

A = [-1,   10, 0, & !&
      0, 1000, 0, & !&
      0,   -1, 1]   !&

or, equivalently by

!&<
A = [-1,   10, 0, &
      0, 1000, 0, &
      0,   -1, 1]
!&>

Contributing / Testing

The testing mechanism allows you to easily test fprettify with any Fortran project of your choice. Simply clone or copy your entire project into fortran_tests/before and run python setup.py test. The directory fortran_tests/after contains the test output (reformatted Fortran files). If testing fails, please submit an issue!

Code Climate

Metadata

Release files for fprettify 0.3.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fprettify 0.3.7
File Size Uploaded
fprettify-0.3.7.tar.gz 29.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fprettify 0.3.7
File Interpreter ABI Platform
fprettify-0.3.7-py3-none-any.whl Python 3 none any Details

Total release size: 57.7 kB

Release files / fprettify-0.3.7.tar.gz

Download URL fprettify-0.3.7.tar.gz
Size 29.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1488a813f7e60a9e86c56fd0b82bd9df1b75bfb4bf2ee8e433c12f63b7e54057
BLAKE2b-256 checksum
How to use checksums
3915d88681bd2be4a375a78b52443b8e87608240913623d9be5c47e3c328b068
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0

Release files / fprettify-0.3.7-py3-none-any.whl

Download URL fprettify-0.3.7-py3-none-any.whl
Size 28.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56f0a64c43dc47134ce32af2e5da8cd7a1584897be29d19289ec5d87510d1daf
BLAKE2b-256 checksum
How to use checksums
52132c32d63574e116f8c933f56315df9135bf2fae7a88e9e7c6c4d37f48f4ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0

Release history Release notifications | RSS feed

This release

0.3.7 This release

2 release files

0.3.6

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.1

3 release files

0.3

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page