Skip to main content

Library of string functions and formatters

Project description

PyPI Version PyPI Downloads

Description

Provides advanced string utilities and a powerful Formatter.

This is a development version, and while it has been thoroughly tested, it will be battle-tested in a live project and updated as needed.

PowerFormatter

PowerFormatter is a drop-in replacement of str.Formatter and supports the same syntax and more.

PowerFormatter adds the following features:
  • virtual fields now, uuid1, uuid4, uuid5 (no key is required in the format parameters, however if present, they take precedence); many more coming up.

  • user-defined virtual fields can be added through field_default init parm.

  • can send namespaces where format field values are searched through namespaces init parm.

  • !capitalize, !lower, !lstrip, !rstrip, !slug, !strip, !title, upper; many more coming up.

  • user-defined convertors can be added through convertors init parm.

  • chained conversions and format specifications in any order: {field!slug:.10s!upper}.

  • user-configurable silence_missing_fields, if set True, will suppress IndexError and KeyError and will quietly replace with empty string.

  • user-configurable characters to identify fields (default {}), convertors (default !) and format specifiers (default :).

A simple example:

import random
from string_kit import PowerFormatter

my_field_defaults = {
    "greeting": "Hello",
    "password": lambda: random.choice(["lI0n", "rAbb1t", "tig3R"]) \
                        + str(random.randint(100, 999)),
}

my_weather = "Sunny and Wet"

my_convertors = {
    "s2d": lambda s: s.replace(" ", "-")
}

pf = PowerFormatter(
    silence_missing_fields=True,
    field_defaults=my_field_defaults,  # optional, dict with str or callable values
    convertors=my_convertors,          # optional, dict with callable values
    field_namespaces=[locals()],       # optional, namespaces to search for field names
)

print(pf.format("{greeting!upper} {fname!s2d}, this is your random password:"
                " '{password}'.", fname="Charlie Brown"))
# "HELLO Charlie-Brown, this is your random password: 'rAbb1t530'."

print(pf.format("Today is a {my_weather:.5!lower} day.{inexistent!lstrip}"))
# "Today is a sunny day."

Some TODO Ideas

  • somehow send static parameters to convertors in format string, e.g. replace convertor with field-level replace string, i.e. {field:!replace(x,y)}`

  • add snake_case, kebab-case, PascalCase, camelCase as built-in convertors

  • add sqids convertor with parameters (strings allowed, length) passed as static values

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

string_kit-0.1.0a4.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

string_kit-0.1.0a4-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file string_kit-0.1.0a4.tar.gz.

File metadata

  • Download URL: string_kit-0.1.0a4.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.3 CPython/3.13.2 Windows/11

File hashes

Hashes for string_kit-0.1.0a4.tar.gz
Algorithm Hash digest
SHA256 30baa5ee9dd7de1083193599cdd0d5fd7179c0bfc176203b419a8fe4692bb5b4
MD5 e5c06d534bd66ad0d8895c7554b12287
BLAKE2b-256 6768ce28e45b63b3f7f05f61fb49522302bb760e3d2c5f19d984982f556e506a

See more details on using hashes here.

File details

Details for the file string_kit-0.1.0a4-py3-none-any.whl.

File metadata

  • Download URL: string_kit-0.1.0a4-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.3 CPython/3.13.2 Windows/11

File hashes

Hashes for string_kit-0.1.0a4-py3-none-any.whl
Algorithm Hash digest
SHA256 bf9f68ab02a17199724647c0a1f348c6554b7ca05fed1a221a31be13e5c76a84
MD5 a10dcc2008554f5dc0488f550be710cb
BLAKE2b-256 077c139e1687eca350a21b0153ddad994b0ac9e8af5f48673a9477ac801fec5c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page