Skip to content

Commit

Permalink
Merge 6d4a1b7 into a112c4f
Browse files Browse the repository at this point in the history
  • Loading branch information
albertocalderari committed Jan 13, 2018
2 parents a112c4f + 6d4a1b7 commit fda83b6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __pycache__
.coverage
dist
MANIFEST
.idea*
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ language: python

python:
- "2.7"
- "3.2"
- "3.3"
- "3.5"
- "3.6"

install:
- pip install -q coveralls --use-mirrors
- pip install -q coveralls
- pip install -r requirements.txt
- pip install flake8

Expand Down
38 changes: 20 additions & 18 deletions strconv.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@

from collections import Counter
from datetime import datetime
import re

import sys

__version__ = '0.4.1'
# strconv.py
# Copyright (c) 2013 Byron Ruth
# BSD License

__version__ = '0.4.1'

from collections import Counter


class TypeInfo(object):
"Sampling and frequency of a type for a sample of values."
Expand Down Expand Up @@ -190,10 +194,6 @@ def infer_matrix(self, matrix, n=None, size=10):

# Built-in converters

import re

from datetime import datetime

# Use dateutil for more robust parsing
try:
from dateutil.parser import parse as duparse
Expand All @@ -208,6 +208,7 @@ def infer_matrix(self, matrix, n=None, size=10):
DATE_FORMATS = (
'%Y-%m-%d',
'%m-%d-%Y',
'%Y/%m/%d',
'%m/%d/%Y',
'%m.%d.%Y',
'%m-%d-%y',
Expand All @@ -221,7 +222,9 @@ def infer_matrix(self, matrix, n=None, size=10):
'%H:%M:%S',
'%H:%M',
'%I:%M:%S %p',
'%I:%M:%S %z',
'%I:%M %p',
'%I:%M %z',
'%I:%M',
)

Expand All @@ -248,22 +251,21 @@ def convert_bool(s):


def convert_datetime(s, date_formats=DATE_FORMATS, time_formats=TIME_FORMATS):
if duparse:
try:
dt = duparse(s)
if dt.time():
return duparse(s)
except TypeError: # parse may throw this in py3
raise ValueError
if sys.version < '3.5':
if duparse:
try:
dt = duparse(s)
if dt.time():
return duparse(s)
except TypeError: # parse may throw this in py3
raise ValueError

for df in date_formats:
for tf in time_formats:
for sep in DATE_TIME_SEPS:
f = '{0}{1}{2}'.format(df, sep, tf)
try:
dt = datetime.strptime(s, f)
if dt.time():
return dt
return datetime.strptime(s, f)
except ValueError:
pass
raise ValueError
Expand Down
10 changes: 10 additions & 0 deletions test_strconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def test_convert(self):
self.assertEqual(strconv.convert('5:40 PM'), time(17, 40))
self.assertEqual(strconv.convert('March 4, 2013 5:40 PM'),
datetime(2013, 3, 4, 17, 40, 0))
self.assertEqual(strconv.convert('March 4, 2013 12:00 AM'),
datetime(2013, 3, 4, 0, 0))

def test_convert_include_type(self):
self.assertEqual(strconv.convert('-3', include_type=True), (-3, 'int'))
Expand All @@ -67,6 +69,13 @@ def test_infer(self):
self.assertEqual(strconv.infer('3/20/2013'), 'date')
self.assertEqual(strconv.infer('5:40 PM'), 'time')
self.assertEqual(strconv.infer('March 4, 2013 5:40 PM'), 'datetime')
self.assertEqual(strconv.infer('2018-12-01 00:00:00'), 'datetime')
self.assertEqual(strconv.infer('March 4, 2013 12:00 PM'), 'datetime')
# Midnight
self.assertEqual(strconv.convert_datetime('2013-03-01 00:00:00'),
datetime(2013, 3, 1, 0, 0, 0))
self.assertEqual(strconv.convert_datetime('2018/03/01 00:00:00'),
datetime(2018, 3, 1, 0, 0, 0))

def test_infer_converted(self):
self.assertEqual(strconv.infer('-3', converted=True), int)
Expand Down Expand Up @@ -159,5 +168,6 @@ def test_convert_datetime(self):
self.assertEqual(strconv.convert_datetime('2013-03-01 5:30:40 -0500'),
datetime(2013, 3, 1, 5, 30, 40, tzinfo=tzoff))


if __name__ == '__main__':
unittest.main()

0 comments on commit fda83b6

Please sign in to comment.