Skip to main content

Replace strings with other strings

Project description

replacements

There is a single class of interest in this project: Replacer.

It is an object for replacing strings with other strings in json-like objects.

The initializer takes a list of assignments, each of which is a dictionary containing a name, a type, and optionally args and kwargs.

There are currently 6 implemented types:

  • identity: returns the argument passed to it.
  • localfile: passes the args and kwargs to open and then reads the file object. The mode is always 'r'.
  • fsspec: passes the args and kwargs to fsspec.open, opens that, and reads. The mode is always 'r'. Requires fsspec to be installed. fsspec has multiple protocols installed, e.g. http(s), (s)ftp and zip. This can also be used for data on S3, if s3fs is installed.
  • awssecret: takes two arguments: region_name and secret_id. Uses boto to call secretsmanager, and returns the returned SecretString.
  • env: takes two arguments: name, and optionally a default value. If the environment variable doesn't exist and no default value was passed, an AssertionError will be raised.
  • base64uuid4: the base64 (the url safe "-_" variant) of a uuid.uuid4 call. Use this to create a unique id that can be used in multiple derived replacements.

Example:

Replacer([{
    "name": "name",
    "type": "identity",
    "args": ["World"]
}])("Hello, ${name}!")

returns "Hello, World!"

There can be dependencies between the assignments. They are resolved linearly using the list order:

Replacer([
    {
        "name": "name",
        "type": "identity",
        "args": ["World"]
    },
    {
        "name": "greeting",
        "type": "identity",
        "args": ["Hello, ${name}!"]
    }
])("${greeting}")

also returns "Hello, World!"

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

replacements-1.0.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

replacements-1.0-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

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