Skip to content

Commit

Permalink
Handle fractions
Browse files Browse the repository at this point in the history
  • Loading branch information
swaroopch committed Jun 23, 2014
1 parent a3e80ff commit 958b84f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions edn_format/edn_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import decimal
import datetime
import uuid
import fractions
import pyrfc3339
from .immutable_dict import ImmutableDict

Expand Down Expand Up @@ -43,6 +44,8 @@ def seq(obj):
return "#{{{}}}".format(seq(obj))
elif isinstance(obj, dict) or isinstance(obj, ImmutableDict):
return "{{{}}}".format(seq(itertools.chain.from_iterable(obj.items())))
elif isinstance(obj, fractions.Fraction):
return str(obj)
elif isinstance(obj, datetime.datetime):
return '#inst "{}"'.format(pyrfc3339.generate(obj))
elif isinstance(obj, datetime.date):
Expand Down
4 changes: 2 additions & 2 deletions edn_format/edn_lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __str__(self):
'BOOLEAN',
'INTEGER',
'FLOAT',
'DIVIDE',
'SYMBOL',
'KEYWORD',
'VECTOR_START',
Expand Down Expand Up @@ -89,8 +90,6 @@ def __str__(self):
r"\/"
r"[{all}]+"
r"|"
r"\/"
r"|"
r"{start}"
r"[{all}]*"
r")").format(**PARTS)
Expand All @@ -112,6 +111,7 @@ def __str__(self):
r"[{all}]*"
r")").format(**PARTS)

t_DIVIDE = r'\/'
t_VECTOR_START = r'\['
t_VECTOR_END = r'\]'
t_LIST_START = r'\('
Expand Down
10 changes: 9 additions & 1 deletion edn_format/edn_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import uuid
import datetime
import fractions
import pyrfc3339

import ply.yacc
Expand Down Expand Up @@ -50,7 +51,8 @@ def p_term_leaf(p):
| NIL
| KEYWORD
| SYMBOL
| WHITESPACE"""
| WHITESPACE
| DIVIDE"""
p[0] = p[1]


Expand Down Expand Up @@ -89,6 +91,11 @@ def p_empty_map(p):
p[0] = ImmutableDict({})


def p_fraction(p):
"""fraction : INTEGER DIVIDE INTEGER"""
p[0] = fractions.Fraction(p[1], p[3])


def p_map(p):
"""map : MAP_START expressions MAP_OR_SET_END"""
terms = p[2]
Expand All @@ -113,6 +120,7 @@ def p_expression(p):
| list
| set
| map
| fraction
| term"""
p[0] = p[1]

Expand Down
8 changes: 7 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import fractions
import pytz
import unittest
from edn_format import edn_lex, edn_parse, \
Expand Down Expand Up @@ -48,14 +49,18 @@ def test_lexer(self):
"abc")
self.check_lex("[LexToken(SYMBOL,Symbol(?abc),1,0)]",
"?abc")
self.check_lex("[LexToken(SYMBOL,Symbol(/),1,0)]",
self.check_lex("[LexToken(DIVIDE,'/',1,0)]",
"/")
self.check_lex("[LexToken(SYMBOL,Symbol(prefix/name),1,0)]",
"prefix/name")
self.check_lex("[LexToken(SYMBOL,Symbol(true.),1,0)]",
"true.")
self.check_lex("[LexToken(SYMBOL,Symbol($:ABC?),1,0)]",
"$:ABC?")
self.check_lex(("[LexToken(INTEGER,2,1,0), "
"LexToken(DIVIDE,'/',1,1), "
"LexToken(INTEGER,3,1,2)]"),
"2/3")

def check_parse(self, expected_output, actual_input):
self.assertEqual(expected_output, edn_parse.parse(actual_input))
Expand Down Expand Up @@ -106,6 +111,7 @@ def test_parser(self):
self.check_parse("blah\n", '"blah\n"')
self.check_parse(["abc", "123"], '["abc", "123"]')
self.check_parse({"key": "value"}, '{"key" "value"}')
self.check_parse(fractions.Fraction(2, 3), "2/3")

def check_roundtrip(self, data_input):
self.assertEqual(data_input, loads(dumps(data_input)))
Expand Down

0 comments on commit 958b84f

Please sign in to comment.