Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hex2uuid formats UUIDs #158

Merged
merged 3 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ List of builtin functions:
| str2date | Convert string to date | |
| bool2int | Convert boolean to integer (0, 1) | |
| str2num | Parse string as a number | |
| format-uuid | Parse a hex UUID, and format it into hyphen-separated groups | |
| substr | Returns substring indexed by [first arg, second arg), zero-indexed. | substr(2, 5) of 'abcdef' = 'cde' |
| selected-at | Returns the Nth word in a string. N is zero-indexed. | selected-at(3) - return 4th word |
| selected | Returns True if the given word is in the value. | selected(fever) |
Expand Down
22 changes: 22 additions & 0 deletions commcare_export/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hashlib
import json
import uuid
from datetime import datetime
import operator
import pytz
Expand Down Expand Up @@ -355,6 +356,26 @@ def json2str(val):
return


@unwrap('val')
def format_uuid(val):
"""
Renders a hex UUID in hyphen-separated groups

>>> format_uuid('00a3e0194ce1458794c50971dee2de22')
'00a3e019-4ce1-4587-94c5-0971dee2de22'
>>> format_uuid(0x00a3e0194ce1458794c50971dee2de22)
'00a3e019-4ce1-4587-94c5-0971dee2de22'
"""
if not val:
return None
if isinstance(val, int):
val = hex(val)
try:
return str(uuid.UUID(val))
except ValueError:
return None


def join(*args):
args = [unwrap_val(arg)for arg in args]
try:
Expand Down Expand Up @@ -442,6 +463,7 @@ def __init__(self, d=None):
'str2num': str2num,
'str2date': str2date,
'json2str': json2str,
'format-uuid': format_uuid,
'selected': selected,
'selected-at': selected_at,
'count-selected': count_selected,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import doctest
import commcare_export.env


def test_doctests():
results = doctest.testmod(commcare_export.env)
assert results.failed == 0
3 changes: 3 additions & 0 deletions tests/test_minilinq.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def test_eval_collapsed_list(self):
assert Apply(Reference("str2date"), Literal('2015-01-01T18:32:57.001200Z')).eval(env) == datetime(2015, 1, 1, 18, 32, 57)
assert Apply(Reference("str2date"), Literal(u'日'.encode('utf8'))).eval(env) == None
assert Apply(Reference("str2date"), Literal(u'日')).eval(env) == None
assert Apply(Reference("format-uuid"), Literal(0xf00)).eval(env) == None
assert Apply(Reference("format-uuid"), Literal('f00')).eval(env) == None
assert Apply(Reference("format-uuid"), Literal('00a3e019-4ce1-4587-94c5-0971dee2de22')).eval(env) == '00a3e019-4ce1-4587-94c5-0971dee2de22'
assert Apply(Reference("selected-at"), Literal('a b c'), Literal('1')).eval(env) == 'b'
assert Apply(Reference("selected-at"), Literal(u'a b 日'), Literal('-1')).eval(env) == u'日'
assert Apply(Reference("selected-at"), Literal('a b c'), Literal('5')).eval(env) is None
Expand Down