Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Commit

Permalink
Add dot_interpolate function
Browse files Browse the repository at this point in the history
  • Loading branch information
esoergel committed Nov 18, 2013
1 parent 888f123 commit d00160c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dimagi/utils/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
}

from lazy_attachment_doc import *
from dates import *
from dates import *
from xpath import *
18 changes: 18 additions & 0 deletions dimagi/utils/tests/xpath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.test import TestCase
from ..xpath import dot_interpolate


class DotInterpolateTest(TestCase):

def testRegex(self):
replacement = "@case_id stuff"
cases = [
('./lmp < 570.5', '%s/lmp < 570.5'),
('stuff ./lmp < 570.', 'stuff %s/lmp < 570.'),
('.53 < hello.', '.53 < hello%s'),
]
for case in cases:
self.assertEqual(
dot_interpolate(replacement, case[0]),
case[1] % replacement
)
10 changes: 10 additions & 0 deletions dimagi/utils/xpath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import re


def dot_interpolate(xpath, context):
"""
Replaces non-decimal dots in `context` with `xpath`
"""
pattern = r'(\D|^)\.(\D|$)'
repl = '\g<1>%s\g<2>' % xpath
return re.sub(pattern, repl, context)

0 comments on commit d00160c

Please sign in to comment.