Skip to main content

A Python implementation of the tr algorithm

Project description

python-tr

This module is a Python implementation of the tr algorithm.

tr(string1, string2, source, option=’’)

If not given option, then replace all characters in string1 with the character in the same position in string2.

Following options are available:

c

Replace all complemented characters in string1 with the character in the same position in string2.

d

Delete all characters in string1.

s

Squeeze all characters in string1.

cs

Squeeze all the characters in string2 besides “c” replacement.

ds

Delete all characters in string1. Squeeze all characters in string2.

cd

Delete all complemented characters in string1.

Params:
  • <unicode> string1

  • <unicode> string2

  • <unicode> source

  • <basestring> option

Return:
  • <unicode> translated_source

Note

  • If Python2.x, the type of paramaters (string1, string2 and source) must be unicode.

  • If Python3.3 or later, the type of paramaters (string1, string2 and source) must be str.

Example

Python2.x

>>> from tr import tr
>>> tr(u'bn', u'cr', u'bunny')
u'curry'
>>> tr(u'n', '', u'bunny', 'd')
u'buy'
>>> tr(u'n', u'u', u'bunny', 'c')
u'uunnu'
>>> tr(u'n', u'', u'bunny', 's')
u'buny'
>>> tr(u'bn', '', u'bunny', 'cd')
u'bnn'
>>> tr(u'bn', u'cr', u'bunny', 'cs')
u'brnnr'
>>> tr(u'bn', u'cr', u'bunny', 'ds')
u'uy'

Python3.3 or later

>>> from tr import tr
>>> tr('bn', 'cr', 'bunny')
'curry'
>>> tr('n', '', 'bunny', 'd')
'buy'
>>> tr('n', 'u', 'bunny', 'c')
'uunnu'
>>> tr('n', '', 'bunny', 's')
'buny'
>>> tr('bn', '', 'bunny', 'cd')
'bnn'
>>> tr('bn', 'cr', 'bunny', 'cs')
'brnnr'
>>> tr('bn', 'cr', 'bunny', 'ds')
'uy'

CHANGES

0.0.1 (2014-07-10)

First release.

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

python-tr-0.0.1.tar.gz (2.8 kB view details)

Uploaded Source

File details

Details for the file python-tr-0.0.1.tar.gz.

File metadata

  • Download URL: python-tr-0.0.1.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for python-tr-0.0.1.tar.gz
Algorithm Hash digest
SHA256 04c9f5c54ec504a78c355b04675398d2e5ace81cac0d06499282ba51e157219b
MD5 16d44f5e67be816b690a7ec0b5bbbba2
BLAKE2b-256 e746afb8f403538c806e2762094c11737347ee8d19d474793715f702dc15d8ef

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