Skip to content

Commit

Permalink
New submodule “format.py” for string format functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Mar 10, 2018
1 parent 8ec514a commit 4898193
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ Functions

.. code ::
alphanum
--------
%alphanum{text}
This function first ASCIIfies the given text, then all non alpanumeric
characters are replaced with whitespaces.
asciify
-------
Expand Down
7 changes: 7 additions & 0 deletions doc/source/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Functions

.. code-block:: text
alphanum
--------
%alphanum{text}
This function first ASCIIfies the given text, then all non alpanumeric
characters are replaced with whitespaces.
asciify
-------
Expand Down
19 changes: 19 additions & 0 deletions test/test_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
import unittest
from tmep.format import alphanum, asciify, delchars


class TestFormatFunctions(unittest.TestCase):

def test_alphanum(self):
self.assertEqual(alphanum('a-b'), 'a b')

def test_asciify(self):
self.assertEqual(asciify('ä'), 'ae')

def test_delchars(self):
self.assertEqual(delchars('a-b', '-'), 'ab')


if __name__ == '__main__':
unittest.main(defaultTest='suite')
10 changes: 10 additions & 0 deletions test/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def setUp(self):
def parseEqual(self, a, b):
self.assertEqual(tmep.parse(a, self.values), b)

# alphanum
def test_alphanum_accent(self):
self.parseEqual(u'%alphanum{après-évêque}', u'apres eveque')

def test_alphanum_genres(self):
self.parseEqual(u'%alphanum{$genres}', u'Pop Rock Classical Crossover')

def test_alphanum_many(self):
self.parseEqual(u'%alphanum{a"&(&b}', u'a b')

# asciify
def test_asciify_literal(self):
self.parseEqual(u'%asciify{après évêque}', u'apres eveque')
Expand Down
26 changes: 26 additions & 0 deletions tmep/format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-

"""A collection of string format functions derived form the class “Functions()”
"""

from tmep.functions import Functions

f = Functions()

# if = f.tmpl_if
alphanum = f.tmpl_alphanum
asciify = f.tmpl_asciify
delchars = f.tmpl_delchars
deldupchars = f.tmpl_deldupchars
first = f.tmpl_first
initial = f.tmpl_initial
left = f.tmpl_left
lower = f.tmpl_lower
num = f.tmpl_num
replchars = f.tmpl_replchars
right = f.tmpl_right
sanitize = f.tmpl_sanitize
shorten = f.tmpl_shorten
time = f.tmpl_time
title = f.tmpl_title
upper = f.tmpl_upper
17 changes: 17 additions & 0 deletions tmep/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
import re
import textwrap
from unidecode import unidecode
import six

if six.PY2:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


def _int_arg(s):
Expand Down Expand Up @@ -53,6 +59,17 @@ def functions(self):
out[key[len(self._prefix):]] = getattr(self, key)
return out

def tmpl_alphanum(self, text):
"""
* synopsis: ``%alphanum{text}``
* description: This function first ASCIIfies the given text, then all \
non alpanumeric characters are replaced with whitespaces.
"""
text = self.tmpl_asciify(text)
text = re.sub(r'[^a-zA-Z0-9]+', ' ', text)
text = re.sub(r'\s+', ' ', text)
return text

@staticmethod
def tmpl_asciify(text):
"""
Expand Down

0 comments on commit 4898193

Please sign in to comment.