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.

Formatter

string_kit.Formatter is a drop-in replacement of string.Formatter and supports the same syntax and more.

string_kit.Formatter 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 Formatter

the_weather = "Sunny but Humid"

custom_field_defaults = {
    "password": lambda: random.choice(["lI0n", "rAbb1t", "tig3R"]) \
                        + str(random.randint(1000, 9999)),
}

custom_convertors = {
    "greeting": lambda str_, greeting: f"{greeting} {str_}",
}


class CustomFormatter(Formatter):
    field_defaults = {
        "bye": "Good bye!",
    }
    convertors = {
        "space2dash": lambda str_: str_.replace(" ", "-")
    }


skf = CustomFormatter(
    silence_missing_fields=True,
    field_defaults=custom_field_defaults,  # optional, dict with str or callable values
    convertors=custom_convertors,          # optional, dict with callable values
    field_namespaces=[locals()],           # optional, namespaces to search for field names
)

print(skf.format("{name!space2dash!greeting(Hello)},"
                 " your random password is: '{password}'. {bye}",
                 name="Charlie Brown"))
# "Hello Charlie-Brown, your random password is: 'rAbb1t1258'. Good bye!"

print(skf.format("Today is {now!strftime(%A)!upper}, also a"
                 " {the_weather!lower:.5!replace(sunny,sunny \\(☀️\\))} day."
                 " {missing!lstrip}"))
# "Today is FRIDAY, also a sunny (☀️) day. "

Some TODO Ideas

  • 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.0a7.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

string_kit-0.1.0a7-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: string_kit-0.1.0a7.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.25.3 CPython/3.13.5 Windows/11

File hashes

Hashes for string_kit-0.1.0a7.tar.gz
Algorithm Hash digest
SHA256 f7d76d780415ef6390acbf2845e383524fe3eaa0be13179c720ef7b6c64be881
MD5 62d416009e634b52811d6e270c235e1e
BLAKE2b-256 f7875447d8ffe5954913cc4413368ae1468a6c0cac33547e0f5bf441b84340f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: string_kit-0.1.0a7-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.25.3 CPython/3.13.5 Windows/11

File hashes

Hashes for string_kit-0.1.0a7-py3-none-any.whl
Algorithm Hash digest
SHA256 21017493bf3429174cb226b76bad811d0054d2b0e53458ca96bbe1ad96e047e9
MD5 e87f01f52d2942b10094fd5e94abfd38
BLAKE2b-256 45a0da6d9c5563fd73972cf78b4579527b08cd0a874e49c8dbe69401d3e46635

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