Skip to main content

Translator module

Project description

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.

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

rms_translator-1.0.0.tar.gz (26.6 kB view details)

Uploaded Source

Built Distribution

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

rms_translator-1.0.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file rms_translator-1.0.0.tar.gz.

File metadata

  • Download URL: rms_translator-1.0.0.tar.gz
  • Upload date:
  • Size: 26.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rms_translator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0eccaee7fdfd59a5b4516b1f9f61755fa7be474713800b28a5ad563c9545cbc3
MD5 63e7f8521fa1a13df311ce7e10a2a96c
BLAKE2b-256 28428f6cb00b0404b93e54295312dfd0dfa764475b32bb4510d346d265243030

See more details on using hashes here.

File details

Details for the file rms_translator-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: rms_translator-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rms_translator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52603895f38b0f80f3b55bd1b533fa59675ed892a2593dc8d92b4950c536152b
MD5 c5348fde1d2e822f9a3b2a1c30dfbc07
BLAKE2b-256 aa8687d1159a8cdaaaff0b056491d4041254dc3fe80b42dde5be1090bf3cb898

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