Skip to main content

GitHub release; latest by date GitHub Release Date Test Status Documentation Status Code coverage
PyPI - Version PyPI - Format PyPI - Downloads PyPI - Python Version
GitHub commits since latest release GitHub commit activity GitHub last commit
Number of GitHub open issues Number of GitHub closed issues Number of GitHub open pull requests Number of GitHub closed pull requests
GitHub License Number of GitHub stars GitHub forks

Introduction

translator is a module that provides abstract classes and implementations for mapping strings (such as file paths) to other information. It supports several translation mechanisms including Python dictionaries, regular expression patterns, and sequences of translators.

translator is a product of the PDS Ring-Moon Systems Node.

Installation

The translator module is available via the rms-translator package on PyPI and can be installed with:

pip install rms-translator

Getting Started

The translator module provides several translator classes:

  • Translator - Abstract base class defining the translator interface
  • TranslatorByDict - Fast dictionary-based translation
  • TranslatorByRegex - Flexible regex pattern-based translation with substitution
  • TranslatorBySequence - Combines multiple translators in sequence
  • NullTranslator - Returns nothing (useful as a placeholder)
  • SelfTranslator - Returns the input unchanged

All translator subclasses implement two key methods:

  • all(strings, strings_first=False) - Returns all matching translations
  • first(strings, strings_first=False) - Returns the first matching translation

Details of each class are available in the module documentation.

Example: Dictionary Translator

from translator import TranslatorByDict

# Create a simple dictionary translator
trans = TranslatorByDict({
    'apple': 'fruit',
    'carrot': 'vegetable',
    'chicken': 'meat'
})

print(trans.first('apple'))    # Output: 'fruit'
print(trans.all(['apple', 'carrot']))  # Output: ['fruit', 'vegetable']

Example: Regex Translator

from translator import TranslatorByRegex
import re

# Create a regex translator for file paths
trans = TranslatorByRegex([
    (r'data/(\w+)/(\w+)\.txt', r'processed/\1/\2.dat'),
    (r'images/(\w+)\.jpg', r'thumbnails/\1_thumb.jpg')
])

print(trans.first('data/2024/observations.txt'))
# Output: 'processed/2024/observations.dat'

print(trans.first('images/saturn.jpg'))
# Output: 'thumbnails/saturn_thumb.jpg'

Example: Sequence of Translators

from translator import TranslatorByDict, TranslatorByRegex, TranslatorBySequence

# Combine multiple translators
dict_trans = TranslatorByDict({'special': 'handled'})
regex_trans = TranslatorByRegex([(r'(\w+)', r'default_\1')])

seq_trans = TranslatorBySequence([dict_trans, regex_trans])

print(seq_trans.first('special'))  # Output: 'handled' (from dictionary)
print(seq_trans.first('other'))    # Output: 'default_other' (from regex)

Contributing

Information on contributing to this package can be found in the Contributing Guide.

Links

Licensing

This code is licensed under the Apache License v2.0.

Metadata

Release files for rms-translator 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rms-translator 1.1.0
File Size Uploaded Provenance Actions
rms_translator-1.1.0.tar.gz 26.9 kB Without provenance Details for rms_translator-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rms-translator 1.1.0
File Interpreter ABI Platform Actions
rms_translator-1.1.0-py3-none-any.whl Python 3 none any Details for rms_translator-1.1.0-py3-none-any.whl

Total release size: 38.5 kB

Release files / rms_translator-1.1.0.tar.gz

Download URL rms_translator-1.1.0.tar.gz
Size 26.9 kB
Tags Source
SHA-256 checksum
How to use checksums
163a02cb25a4133eb3b921e83fd52ba4d4a098d1bc272f4601221b60dac381d6
BLAKE2b-256 checksum
How to use checksums
defeb4b80f96ae10c42a54e14faf917120f9500e36dd0c619813b99918ab8236
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / rms_translator-1.1.0-py3-none-any.whl

Download URL rms_translator-1.1.0-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
40103d5efa3e44c306b8b0ffeec7e30427b1bd73da23cca6e9281c9fed9a0864
BLAKE2b-256 checksum
How to use checksums
ad998e49c0c8bcebafa7beac90cd2c695df2c4f2a0228ef311dc93a7e3463992
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for rms-translator 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page