Skip to content

Commit

Permalink
Refactor 'import datetime' as 'import datetime as dt'
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Oct 29, 2023
1 parent 0f0ddf6 commit ad7dec3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 48 deletions.
10 changes: 5 additions & 5 deletions src/tablib/packages/dbfpy/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
import datetime as dt
import struct
from functools import total_ordering

Expand Down Expand Up @@ -363,7 +363,7 @@ class DbfDateFieldDef(DbfFieldDef):
"""Definition of the date field."""

typeCode = "D"
defaultValue = utils.classproperty(lambda cls: datetime.date.today())
defaultValue = utils.classproperty(lambda cls: dt.date.today())
# "yyyymmdd" gives us 8 characters
length = 8

Expand Down Expand Up @@ -397,7 +397,7 @@ class DbfDateTimeFieldDef(DbfFieldDef):
# and GDN (Gregorian Day Number). note, that GDN < JDN
JDN_GDN_DIFF = 1721425
typeCode = "T"
defaultValue = utils.classproperty(lambda cls: datetime.datetime.now())
defaultValue = utils.classproperty(lambda cls: dt.datetime.now())
# two 32-bits integers representing JDN and amount of
# milliseconds respectively gives us 8 bytes.
# note, that values must be encoded in LE byteorder.
Expand All @@ -409,8 +409,8 @@ def decodeValue(self, value):
# LE byteorder
_jdn, _msecs = struct.unpack("<2I", value)
if _jdn >= 1:
_rv = datetime.datetime.fromordinal(_jdn - self.JDN_GDN_DIFF)
_rv += datetime.timedelta(0, _msecs / 1000.0)
_rv = dt.datetime.fromordinal(_jdn - self.JDN_GDN_DIFF)
_rv += dt.timedelta(0, _msecs / 1000.0)

Check warning on line 413 in src/tablib/packages/dbfpy/fields.py

View check run for this annotation

Codecov / codecov/patch

src/tablib/packages/dbfpy/fields.py#L412-L413

Added lines #L412 - L413 were not covered by tests
else:
# empty date
_rv = None
Expand Down
4 changes: 2 additions & 2 deletions src/tablib/packages/dbfpy/header.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
import datetime as dt
import io
import struct
import sys
Expand Down Expand Up @@ -254,7 +254,7 @@ def toString(self):

def setCurrentDate(self):
"""Update ``self.lastUpdate`` field with current date value."""
self.lastUpdate = datetime.date.today()
self.lastUpdate = dt.date.today()

def __getitem__(self, item):
"""Return a field definition by numeric index or name string"""
Expand Down
32 changes: 16 additions & 16 deletions src/tablib/packages/dbfpy/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
import datetime as dt
import time

__version__ = "$Revision: 1.4 $"[11:-2]
Expand Down Expand Up @@ -56,25 +56,25 @@ def getDate(date=None):
"""
if date is None:
# use current value
return datetime.date.today()
if isinstance(date, datetime.date):
return dt.date.today()
if isinstance(date, dt.date):
return date
if isinstance(date, datetime.datetime):
if isinstance(date, dt.datetime):
return date.date()
if isinstance(date, (int, float)):
# date is a timestamp
return datetime.date.fromtimestamp(date)
return dt.date.fromtimestamp(date)
if isinstance(date, str):
date = date.replace(" ", "0")
if len(date) == 6:
# yymmdd
return datetime.date(*time.strptime(date, "%y%m%d")[:3])
return dt.date(*time.strptime(date, "%y%m%d")[:3])
# yyyymmdd
return datetime.date(*time.strptime(date, "%Y%m%d")[:3])
return dt.date(*time.strptime(date, "%Y%m%d")[:3])
if hasattr(date, "__getitem__"):
# a sequence (assuming date/time tuple)
return datetime.date(*date[:3])
return datetime.date.fromtimestamp(date.ticks())
return dt.date(*date[:3])
return dt.date.fromtimestamp(date.ticks())

Check warning on line 77 in src/tablib/packages/dbfpy/utils.py

View check run for this annotation

Codecov / codecov/patch

src/tablib/packages/dbfpy/utils.py#L77

Added line #L77 was not covered by tests


def getDateTime(value=None):
Expand Down Expand Up @@ -103,20 +103,20 @@ def getDateTime(value=None):
"""
if value is None:
# use current value
return datetime.datetime.today()
if isinstance(value, datetime.datetime):
return dt.datetime.today()
if isinstance(value, dt.datetime):
return value
if isinstance(value, datetime.date):
return datetime.datetime.fromordinal(value.toordinal())
if isinstance(value, dt.date):
return dt.datetime.fromordinal(value.toordinal())
if isinstance(value, (int, float)):
# value is a timestamp
return datetime.datetime.fromtimestamp(value)
return dt.datetime.fromtimestamp(value)
if isinstance(value, str):
raise NotImplementedError("Strings aren't currently implemented")
if hasattr(value, "__getitem__"):
# a sequence (assuming date/time tuple)
return datetime.datetime(*tuple(value)[:6])
return datetime.datetime.fromtimestamp(value.ticks())
return dt.datetime(*tuple(value)[:6])
return dt.datetime.fromtimestamp(value.ticks())

Check warning on line 119 in src/tablib/packages/dbfpy/utils.py

View check run for this annotation

Codecov / codecov/patch

src/tablib/packages/dbfpy/utils.py#L118-L119

Added lines #L118 - L119 were not covered by tests


class classproperty(property):
Expand Down
12 changes: 6 additions & 6 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
"""Tests for Tablib."""

import datetime
import datetime as dt
import doctest
import json
import pickle
Expand Down Expand Up @@ -300,8 +300,8 @@ def test_datetime_append(self):
"""Passes in a single datetime and a single date and exports."""

new_row = (
datetime.datetime.now(),
datetime.datetime.today(),
dt.datetime.now(),
dt.datetime.today(),
)

data.append(new_row)
Expand Down Expand Up @@ -1104,7 +1104,7 @@ def test_tsv_export(self):

class ODSTests(BaseTestCase):
def test_ods_export_datatypes(self):
date_time = datetime.datetime(2019, 10, 4, 12, 30, 8)
date_time = dt.datetime(2019, 10, 4, 12, 30, 8)
data.append(('string', '004', 42, 21.55, Decimal('34.5'), date_time))
data.headers = ('string', 'start0', 'integer', 'float', 'decimal', 'date/time')
# ODS is currently write-only, just test that output doesn't crash.
Expand All @@ -1122,7 +1122,7 @@ def test_xls_date_import(self):
xls_source = Path(__file__).parent / 'files' / 'dates.xls'
with xls_source.open('rb') as fh:
dset = tablib.Dataset().load(fh, 'xls')
self.assertEqual(dset.dict[0]['birth_date'], datetime.datetime(2015, 4, 12, 0, 0))
self.assertEqual(dset.dict[0]['birth_date'], dt.datetime(2015, 4, 12, 0, 0))

def test_xlsx_import_set_skip_lines(self):
data.append(('garbage', 'line', ''))
Expand Down Expand Up @@ -1160,7 +1160,7 @@ def test_xlsx_format_detect(self):
self.assertEqual(detect_format(in_stream), 'xlsx')

def test_xlsx_import_set(self):
date_time = datetime.datetime(2019, 10, 4, 12, 30, 8)
date_time = dt.datetime(2019, 10, 4, 12, 30, 8)
data.append(('string', '004', 42, 21.55, date_time))
data.headers = ('string', 'start0', 'integer', 'float', 'date/time')
_xlsx = data.xlsx
Expand Down
38 changes: 19 additions & 19 deletions tests/test_tablib_dbfpy_packages_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
"""Tests for tablib.packages.dbfpy."""

import datetime
import datetime as dt
import unittest

from tablib.packages.dbfpy import utils
Expand Down Expand Up @@ -42,28 +42,28 @@ def test_getDate_none(self):
output = utils.getDate(value)

# Assert
self.assertIsInstance(output, datetime.date)
self.assertIsInstance(output, dt.date)

def test_getDate_datetime_date(self):
# Arrange
value = datetime.date(2019, 10, 19)
value = dt.date(2019, 10, 19)

# Act
output = utils.getDate(value)

# Assert
self.assertIsInstance(output, datetime.date)
self.assertIsInstance(output, dt.date)
self.assertEqual(output, value)

def test_getDate_datetime_datetime(self):
# Arrange
value = datetime.datetime(2019, 10, 19, 12, 00, 00)
value = dt.datetime(2019, 10, 19, 12, 00, 00)

# Act
output = utils.getDate(value)

# Assert
self.assertIsInstance(output, datetime.date)
self.assertIsInstance(output, dt.date)
self.assertEqual(output, value)

def test_getDate_datetime_timestamp(self):
Expand All @@ -74,8 +74,8 @@ def test_getDate_datetime_timestamp(self):
output = utils.getDate(value)

# Assert
self.assertIsInstance(output, datetime.date)
self.assertEqual(output, datetime.date(2019, 10, 19))
self.assertIsInstance(output, dt.date)
self.assertEqual(output, dt.date(2019, 10, 19))

def test_getDate_datetime_string_yyyy_mm_dd(self):
# Arrange
Expand All @@ -85,8 +85,8 @@ def test_getDate_datetime_string_yyyy_mm_dd(self):
output = utils.getDate(value)

# Assert
self.assertIsInstance(output, datetime.date)
self.assertEqual(output, datetime.date(2019, 10, 19))
self.assertIsInstance(output, dt.date)
self.assertEqual(output, dt.date(2019, 10, 19))

def test_getDate_datetime_string_yymmdd(self):
# Arrange
Expand All @@ -96,8 +96,8 @@ def test_getDate_datetime_string_yymmdd(self):
output = utils.getDate(value)

# Assert
self.assertIsInstance(output, datetime.date)
self.assertEqual(output, datetime.date(2019, 10, 19))
self.assertIsInstance(output, dt.date)
self.assertEqual(output, dt.date(2019, 10, 19))


class UtilsGetDateTimeTestCase(unittest.TestCase):
Expand All @@ -111,29 +111,29 @@ def test_getDateTime_none(self):
output = utils.getDateTime(value)

# Assert
self.assertIsInstance(output, datetime.datetime)
self.assertIsInstance(output, dt.datetime)

def test_getDateTime_datetime_datetime(self):
# Arrange
value = datetime.datetime(2019, 10, 19, 12, 00, 00)
value = dt.datetime(2019, 10, 19, 12, 00, 00)

# Act
output = utils.getDateTime(value)

# Assert
self.assertIsInstance(output, datetime.date)
self.assertIsInstance(output, dt.date)
self.assertEqual(output, value)

def test_getDateTime_datetime_date(self):
# Arrange
value = datetime.date(2019, 10, 19)
value = dt.date(2019, 10, 19)

# Act
output = utils.getDateTime(value)

# Assert
self.assertIsInstance(output, datetime.date)
self.assertEqual(output, datetime.datetime(2019, 10, 19, 00, 00))
self.assertIsInstance(output, dt.date)
self.assertEqual(output, dt.datetime(2019, 10, 19, 00, 00))

def test_getDateTime_datetime_timestamp(self):
# Arrange
Expand All @@ -143,7 +143,7 @@ def test_getDateTime_datetime_timestamp(self):
output = utils.getDateTime(value)

# Assert
self.assertIsInstance(output, datetime.datetime)
self.assertIsInstance(output, dt.datetime)

def test_getDateTime_datetime_string(self):
# Arrange
Expand Down

0 comments on commit ad7dec3

Please sign in to comment.