Skip to main content

A simple, Pythonic file format. Same interface as the

Project description

perky

A friendly, easy, Pythonic text file format

Copyright 2018-2019 by Larry Hastings

Overview

Perky is a new, simple "rcfile" text file format for Python programs.

The following are Perky features:

Perky syntax

Perky configuration files look something like JSON without the quoting.

example name = value
example dict = {
    name = 3
    another name = 5.0
    }
example list = [
    a
    b
    c
    ]
# lines starting with hash are ignored

# blank lines are ignored

" quoted name " = " quoted value "

triple quoted string = """

    indenting
        is preserved

    the string is automatically outdented
    to the leftmost character of the ending
    triple-quote

    <-- aka here
    """

Explicit transformation is better than implicit

One possibly-surprising design choice of Perky: the only natively supported values for the Perky parser are dicts, lists, and strings. Other commonly-used types (ints, floats, etc) are handled using a different mechanism: transformation.

A Perky transformation takes a dict as input, and transforms the contents of the dict based on a schema. A Perky schema is a dict with the same general shape as the dict produced by the Perky parse, but it contains dicts, lists, and transformation functions. If you want myvalue in {'myvalue':'3'} to be a real integer, transform it with the schema {'myvalue': int}.

API

perky.loads(s) -> d

Parses a string containing Perky-file-format settings. Returns a dict.

perky.load(filename, encoding="utf-8") -> d

Parses a file containing Perky-file-format settings. Returns a dict.

perky.dumps(d) -> s

Converts a dictionary to a Perky-file-format string. Keys in the dictionary must all be strings. Values that are not dicts, lists, or strings will be converted to strings using str. Returns a string.

perky.dump(filename, d, encoding="utf-8")

Converts a dictionary to a Perky-file-format string using perky.dump, then writes it to filename.

perky.map(d, fn) -> o

Iterates over a dictionary. Returns a new dictionary where, for every value:

  • if it is a dict, replace with a new dict.
  • if it is a list, replace with a new list.
  • if it is neither a dict nor a list, replace with fn(value).

The function passed in is called a conversion function.

perky.transform(d, schema, default=None) -> o

Recursively transforms a Perky dict into some other object (usually a dict) using the provided schema. Returns a new dict.

A schema is a data structure matching the general expected shape of d, where the values are dicts, lists, and callables. The transformation is similar to perky.map() except that individual values will have individual conversion functions. Also, a schema conversion function can be specified for any value in d, even dicts or lists.

default is a default conversion function. If there is a value v in d that doesn't have an equivalent entry in schema, and v is neither a list nor a dict, and if default is a callable, v will be replaced with default(v) in the output.

perky.Required

Experimental.

perky.nullable(fn) -> fn

Experimental.

perky.const(fn) -> o

Experimental.

TODO

  • Backslash quoting currently does "whatever your version of Python does". Perhaps this should be explicit, and parsed by Perky itself?

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

perky-0.1.2.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

perky-0.1.2-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file perky-0.1.2.tar.gz.

File metadata

  • Download URL: perky-0.1.2.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.21.0

File hashes

Hashes for perky-0.1.2.tar.gz
Algorithm Hash digest
SHA256 98d5b57ce7de0b3e457ae67d3cef9b3780f36efaf084d5b4f151c4ebba006e48
MD5 a7f332e809bd339fcfbc53961c676707
BLAKE2b-256 dbfeebb9e2e161059e55114f439059f5561b324591291cd2f772ecc5a4923373

See more details on using hashes here.

File details

Details for the file perky-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: perky-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.21.0

File hashes

Hashes for perky-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 62f3f497f3b1cc32cc93941b2ab047d319b263e7c179e138b31445ae17fa6bac
MD5 d7392c5b99c7a9fd1b52c4a128465bb3
BLAKE2b-256 025a4c30b87f8046e1a455f93661da80c22cd486308daddd0d824cb53ce2c57b

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