Skip to content

Commit

Permalink
Don't render everything as string (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Mar 7, 2020
1 parent dba660e commit ec4f89a
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions judge/serialisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import math
from dataclasses import field
from enum import Enum
from typing import Union, List, Dict, Literal, Optional
from typing import Union, List, Dict, Literal, Optional, Any

from pydantic import BaseModel
from pydantic.dataclasses import dataclass
Expand Down Expand Up @@ -171,42 +171,49 @@ def parse(value: str) -> Value:
except (TypeError, ValueError) as e:
errors.append(e)

raise SerialisationError(f"Could not find valid type for {value}.", additional_errors=errors)
raise SerialisationError(
f"Could not find valid type for {value}.",
additional_errors=errors
)


def get_readable_representation(value: Value, primitives=False):
"""
Get a readable representation of the data. In many cases, this is just the Python type
that will be returned as a string.
"""
def _convert_to_python(value: Optional[Value]) -> Any:
if value is None:
return ""
# Return primitives as primitive.
# This is done to prevent quotes in lists and other collections.
return None

if value.type in (
BooleanTypes.BOOLEAN,
NumericTypes.INTEGER,
NumericTypes.RATIONAL,
StringTypes.TEXT
) and primitives:
):
return value.data

if isinstance(value, SequenceType):
values = [get_readable_representation(x, True) for x in value.data]
values = [_convert_to_python(x) for x in value.data]
if value.type == SequenceTypes.LIST:
return str(values)
return values
elif value.type == SequenceTypes.SET:
return str(set(values))
return set(values)
else:
raise AssertionError("Forgot a type?")
elif isinstance(value, ObjectType):
values = {x: get_readable_representation(y, True) for x, y in value.data.items()}
return str(values)
values = {x: _convert_to_python(y) for x, y in value.data.items()}
return values
elif isinstance(value, NothingType):
return ""
return None
else:
return str(value.data)


def get_readable_representation(value: Value):
"""
Get a readable representation of the data. In many cases, this is just the Python type
that will be returned as a string.
"""
return repr(_convert_to_python(value))


class ComparableFloat:
__slots__ = ["value"]

Expand Down

0 comments on commit ec4f89a

Please sign in to comment.