Skip to main content

Simple string parsing and formatting using Python's format strings

Project description

fTmplt

Tests Version Python license: MIT style: ruff

Simple string parsing and formatting using Python's format strings

This project is similar to parse, but emphasizes on format strings that are both parsable and formattable. This means only format specifiers that are both valid for parsing and formatting are supported. It was originally developed to parse and format input and output files for various computational physics libraries.

Installation

ftmplt is available on PyPI and can be installed with pip:

pip install ftmplt

Alternatively, it can be installed via GitHub:

pip install git+https://github.com/dylanljones/ftmplt.git

Format Specification

The full format() Format Specification Mini-Language with all types is supported:

[[fill]align][sign][0][width][.precision][type]

Type Description Output
None Unformatted strings str
d Decimal integer int
b Binary integer int
o Octal integer int
x Hex integer int
f Fixed point floating point float
e Floating-point numbers with exponent float
g General number format (either d, f or e) float
% Percentage float

Additionally, datetime objects can be parsed and formatted using the strftime() Format Codes.

The differences between parse() and format() are:

  • The align operators will cause spaces (or specified fill character) to be stripped from the parsed value. The width is not enforced; it just indicates there may be whitespace or "0"s to strip.
  • Numeric parsing will automatically handle a "0b", "0o" or "0x" prefix. That is, the "#" format character is handled automatically by d, b, o and x formats. For "d" any will be accepted, but for the others the correct prefix must be present if at all.
  • The "e" and "g" types are case-insensitive so there is not need for the "E" or "G" types. The "e" type handles Fortran formatted numbers (no leading 0 before the decimal point).

Usage

Define a template using the Python format string syntax. From there it's a simple thing to parse, search or format a string.

>>> import ftmplt
>>> template = "Hello, my name is {name} and I am {age:d} years old."

Format the template string:

>>> text = ftmplt.format(template, name="John", age=42)
>>> text
'Hello, my name is John and I am 42 years old.'

Parse all parameters from the string:

>>> ftmplt.parse(template, text)
{'name': 'John', 'age': 42}

or search a string for some pattern:

>>> ftmplt.search(template, text, "name")
('John', (19, 23))

>>> ftmplt.search(template, text, "age")
(42, (33, 35))

The template object

If you're going to use the same pattern to match lots of strings you can use the Template object. Once initialised, the template object can be used similarly to the functions above:

>>> import ftmplt
>>> template = ftmplt.Template("Hello, my name is {name} and I am {age:d} years old.")
>>> text = template.format(name="John", age=42)
>>> text
'Hello, my name is John and I am 42 years old.'

>>> template.parse(text)
{'name': 'John', 'age': 42}

>>> template.search("name", text)
('John', (19, 23))

Custom Format fields

You can define custom format fields by subclassing ftmplt.CustomFormatter and implementing the parse() and format() methods. For example, to parse and format a list of integers, use the following formatter:

import ftmplt

class ArrayFormatter(ftmplt.CustomFormatter):

    def parse(self, text: str):
        return [int(v) for v in text.split(",")]

    def format(self, value) -> str:
        return ", ".join([str(v) for v in value])

The formatter has to be initialized with the key (name or index) of the format field:

>>> import ftmplt
>>> template = ftmplt.Template("The values {arr} are an array.", ArrayFormatter("arr"))
>>> text = template.format(arr=[1, 2, 3])
>>> text
'The values 1, 2, 3 are an array.'

>>> template.parse(text)
{'arr': [1, 2, 3]}

Example: Parsing a file

Let's say you have a file data.txt with a bunch of parameters in it:

Input-File
N=50 M=100
X=1.0 Y=2.0 Z=3.0
Multiline text:
Some text
over multiple lines

You can handle the file formatting and parsing with a template:

import ftmplt

file = "data.txt"
template = ftmplt.Template("""
Input-File
N={n:d} M={m:d}
X={x:.1f} Y={y:.1f} Z={z:.1f}
Multiline text:
{text}
""")

# Read file and parse data
data = template.parse_file(file)
# Update values
data["n"] = 100
...
# Update file contents
template.format_file(file, data)

The template string can also be read from the file itself, for example data.txt.tmplt:

Input-File
N={n:d} M={m:d}
X={x:.1f} Y={y:.1f} Z={z:.1f}
Multiline text:
{text}

The code to read the template and parse the file is then:

import ftmplt

template = ftmplt.Template.from_file("data.txt.tmplt")
...

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

ftmplt-0.3.0.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

ftmplt-0.3.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ftmplt-0.3.0.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for ftmplt-0.3.0.tar.gz
Algorithm Hash digest
SHA256 44aea10b737456d2d95c987cb1159061e16c9084e52059b91fc543e8f817946c
MD5 7abf7d9cc62f42a07597f9c123f976ef
BLAKE2b-256 60210a27bb488b41565c2028e9ecce0633061728d3728c451484996b70f38d08

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ftmplt-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for ftmplt-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1dfdaadda04ad642dc3fdc2a3cbed4f07bed141b0961dbb3905ce1bf87907c2
MD5 7ab97f7bb781127f614be1d6c7b39988
BLAKE2b-256 5e40b9924224282c12e6bb3f54b49a89c0e10ad304dfec8de71e9a83b1ce03de

See more details on using hashes here.

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