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

Type configs #97

Merged
merged 13 commits into from
Sep 9, 2014
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: python
python:
- "2.6"
- "2.7"
install: "pip install . --use-mirrors"
script: "python setup.py test"
42 changes: 39 additions & 3 deletions jsonobject/api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
from __future__ import absolute_import
from .base import JsonObjectBase, _LimitedDictInterfaceMixin
from .convert import STRING_CONVERSIONS

import decimal
import datetime

class JsonObject(JsonObjectBase, _LimitedDictInterfaceMixin):
from . import properties
import re


re_date = re.compile('^(\d{4})\D?(0[1-9]|1[0-2])\D?([12]\d|0[1-9]|3[01])$')
re_time = re.compile('^([01]\d|2[0-3])\D?([0-5]\d)\D?([0-5]\d)?\D?(\d{3,6})?$')
re_datetime = re.compile(
r'^(\d{4})\D?(0[1-9]|1[0-2])\D?([12]\d|0[1-9]|3[01])'
'(\D?([01]\d|2[0-3])\D?([0-5]\d)\D?([0-5]\d)?\D?(\d{3,6})?'
'([zZ]|([\+-])([01]\d|2[0-3])\D?([0-5]\d)?)?)?$'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it matters in this case, but you need to put an r in front of each line if you want each line to be that type of string literal:

>>> (r"abc\n"
...   "\ncba")
'abc\\n\ncba'

>>> (r"abc\n"
...  r"\ncba")
'abc\\n\\ncba'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, great catch. You're right that it doesn't "matter" in this case, because two python "oddities" cancel each other out: (1) string-type prefixes only apply to the first in a multiline concatenation (ok I mean that makes sense) (2) "\d" is silently treated as two characters "\d" for any character that isn't part of an escape sequence, while "\n" is a single character. So the loosey-gooseyness of (2) obscured (1) and hasn't been causing an error.

)
re_decimal = re.compile('^(\d+)\.(\d+)$')

_string_conversions = STRING_CONVERSIONS

class JsonObject(JsonObjectBase, _LimitedDictInterfaceMixin):
def __getstate__(self):
return self.to_json()

def __setstate__(self, dct):
self.__init__(dct)

class Meta(object):
properties = {
decimal.Decimal: properties.DecimalProperty,
datetime.datetime: properties.DateTimeProperty,
datetime.date: properties.DateProperty,
datetime.time: properties.TimeProperty,
str: properties.StringProperty,
unicode: properties.StringProperty,
bool: properties.BooleanProperty,
int: properties.IntegerProperty,
long: properties.IntegerProperty,
float: properties.FloatProperty,
list: properties.ListProperty,
dict: properties.DictProperty,
set: properties.SetProperty,
}
string_conversions = (
(re_date, datetime.date),
(re_time, datetime.time),
(re_datetime, datetime.datetime),
(re_decimal, decimal.Decimal),
)
Loading