Skip to content

Commit

Permalink
Cleanup and lint codebase (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater authored Mar 6, 2020
1 parent 00b7fdd commit 205a86a
Show file tree
Hide file tree
Showing 108 changed files with 904 additions and 390 deletions.
20 changes: 20 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[flake8]
max-line-length = 88
ignore = E501, E203, W503
per-file-ignores =
__init__.py:F401
pendulum/tz/timezone.py:F811
exclude =
.git
__pycache__
setup.py
build
dist
releases
.idea
.venv
.tox
.mypy_cache
.pytest_cache
.vscode
.github
26 changes: 23 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
repos:
- repo: https://github.com/ambv/black
- repo: https://github.com/psf/black
rev: stable
hooks:
- id: black
python_version: python3.6
- id: black

- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.8
hooks:
- id: flake8

- repo: https://github.com/timothycrosley/isort
rev: 4.3.21
hooks:
- id: isort
additional_dependencies: [toml]
exclude: ^.*/?setup\.py$

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: trailing-whitespace
exclude: ^tests/.*/fixtures/.*
- id: end-of-file-fixer
exclude: ^tests/.*/fixtures/.*
- id: debug-statements
8 changes: 4 additions & 4 deletions build.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import sys


from distutils.core import Extension

from distutils.errors import CCompilerError, DistutilsExecError, DistutilsPlatformError
from distutils.command.build_ext import build_ext
from distutils.core import Extension
from distutils.errors import CCompilerError
from distutils.errors import DistutilsExecError
from distutils.errors import DistutilsPlatformError


# C Extensions
Expand Down
32 changes: 19 additions & 13 deletions clock
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import json
import os

from babel.core import get_global
from babel.plural import (
PluralRule,
compile_zero,
_GettextCompiler,
_binary_compiler,
_unary_compiler,
)
from babel.dates import tokenize_pattern, PATTERN_CHARS
from babel.localedata import load, normalize_locale, LocaleDataDict

from cleo import Application, Command
from babel.dates import PATTERN_CHARS
from babel.dates import tokenize_pattern
from babel.localedata import LocaleDataDict
from babel.localedata import load
from babel.localedata import normalize_locale
from babel.plural import PluralRule
from babel.plural import _binary_compiler
from babel.plural import _GettextCompiler
from babel.plural import _unary_compiler
from babel.plural import compile_zero
from cleo import Application
from cleo import Command
from cleo import argument

from pendulum import __version__


Expand Down Expand Up @@ -52,14 +54,16 @@ class LocaleCreate(Command):

TEMPLATE = """# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from .custom import translations as custom_translations
\"\"\"
{locale} locale file.
It has been generated automatically and must not be modified directly.
\"\"\"
from .custom import translations as custom_translations
locale = {{
'plural': {plural},
Expand All @@ -71,6 +75,8 @@ locale = {{

CUSTOM_TEMPLATE = """# -*- coding: utf-8 -*-
from __future__ import unicode_literals
\"\"\"
{locale} custom locale file.
\"\"\"
Expand Down
34 changes: 17 additions & 17 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{!installation.md!}
{!introduction.md!}
{!instantiation.md!}
{!parsing.md!}
{!localization.md!}
{!attributes_properties.md!}
{!fluent_helpers.md!}
{!string_formatting.md!}
{!comparison.md!}
{!addition_subtraction.md!}
{!difference.md!}
{!modifiers.md!}
{!timezones.md!}
{!duration.md!}
{!period.md!}
{!testing.md!}
{!limitations.md!}
{!docs/installation.md!}
{!docs/introduction.md!}
{!docs/instantiation.md!}
{!docs/parsing.md!}
{!docs/localization.md!}
{!docs/attributes_properties.md!}
{!docs/fluent_helpers.md!}
{!docs/string_formatting.md!}
{!docs/comparison.md!}
{!docs/addition_subtraction.md!}
{!docs/difference.md!}
{!docs/modifiers.md!}
{!docs/timezones.md!}
{!docs/duration.md!}
{!docs/period.md!}
{!docs/testing.md!}
{!docs/limitations.md!}
2 changes: 1 addition & 1 deletion docs/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Installing `pendulum` is quite simple:
$ pip install pendulum
```

or, if you are using [poetry](https://poetry.eustace.io):
or, if you are using [poetry](https://python-poetry.org):

```bash
$ poetry add pendulum
Expand Down
1 change: 0 additions & 1 deletion docs/docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ The provided instance will be returned specifically under the following conditio

Related methods will also return values mocked according to the **now** instance.

```python
>>> print(pendulum.today())
'2001-05-21T00:00:00+00:00'

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/timezones.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ with the `in_timezone()` method.
!!!warning

**You should avoid using the timezone library in Python < 3.6.**

This is due to the fact that Pendulum relies heavily on the presence
of the `fold` attribute which was introduced in Python 3.6.

The reason it works inside the Pendulum ecosystem is that it
backports the `fold` attribute in the `DateTime` class.

Expand Down
97 changes: 44 additions & 53 deletions pendulum/__init__.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,57 @@
from __future__ import absolute_import

import datetime as _datetime
from typing import Union, Optional

from .__version__ import __version__
from typing import Optional
from typing import Union

# Types
from .datetime import DateTime
from .__version__ import __version__
from .constants import DAYS_PER_WEEK
from .constants import FRIDAY
from .constants import HOURS_PER_DAY
from .constants import MINUTES_PER_HOUR
from .constants import MONDAY
from .constants import MONTHS_PER_YEAR
from .constants import SATURDAY
from .constants import SECONDS_PER_DAY
from .constants import SECONDS_PER_HOUR
from .constants import SECONDS_PER_MINUTE
from .constants import SUNDAY
from .constants import THURSDAY
from .constants import TUESDAY
from .constants import WEDNESDAY
from .constants import WEEKS_PER_YEAR
from .constants import YEARS_PER_CENTURY
from .constants import YEARS_PER_DECADE
from .date import Date
from .time import Time
from .datetime import DateTime
from .duration import Duration
from .formatting import Formatter
from .helpers import format_diff
from .helpers import get_locale
from .helpers import get_test_now
from .helpers import has_test_now
from .helpers import locale
from .helpers import set_locale
from .helpers import set_test_now
from .helpers import test
from .helpers import week_ends_at
from .helpers import week_starts_at
from .parser import parse
from .period import Period

from .time import Time
from .tz import POST_TRANSITION
from .tz import PRE_TRANSITION
from .tz import TRANSITION_ERROR
from .tz import UTC
from .tz import local_timezone
from .tz import set_local_timezone
from .tz import test_local_timezone
from .tz import timezone
from .tz import PRE_TRANSITION, POST_TRANSITION, TRANSITION_ERROR
from .tz import timezones
from .tz.timezone import Timezone as _Timezone

from .formatting import Formatter

# Helpers
from .helpers import (
test,
set_test_now,
has_test_now,
get_test_now,
set_locale,
get_locale,
locale,
format_diff,
week_starts_at,
week_ends_at,
)

from .utils._compat import _HAS_FOLD

from .tz import timezones, local_timezone, test_local_timezone, set_local_timezone, UTC

from .parser import parse

# Constants
from .constants import (
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY,
YEARS_PER_CENTURY,
YEARS_PER_DECADE,
MONTHS_PER_YEAR,
WEEKS_PER_YEAR,
DAYS_PER_WEEK,
HOURS_PER_DAY,
MINUTES_PER_HOUR,
SECONDS_PER_MINUTE,
SECONDS_PER_HOUR,
SECONDS_PER_DAY,
)

_TEST_NOW = None # type: Optional[DateTime]
_LOCALE = "en"
Expand Down Expand Up @@ -245,11 +239,8 @@ def yesterday(tz="local"): # type: (Union[str, _Timezone]) -> DateTime


def from_format(
string, # type: str
fmt, # type: str
tz=UTC, # type: Union[str, _Timezone]
locale=None, # type: Optional[str]
): # type: (...) -> DateTime
string, fmt, tz=UTC, locale=None, # noqa
): # type: (str, str, Union[str, _Timezone], Optional[str]) -> DateTime
"""
Creates a DateTime instance from a specific format.
"""
Expand Down
6 changes: 3 additions & 3 deletions pendulum/_extensions/_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ PyObject* precise_diff(PyObject *self, PyObject *args) {
dt1_minute -= dt1_offset / SECS_PER_MIN;
dt1_offset %= SECS_PER_MIN;
dt1_second -= dt1_offset;

if (dt1_second < 0) {
dt1_second += 60;
dt1_minute -= 1;
Expand All @@ -634,7 +634,7 @@ PyObject* precise_diff(PyObject *self, PyObject *args) {
dt1_day += 1;
}
}

dt1_total_seconds = (
dt1_hour * SECS_PER_HOUR
+ dt1_minute * SECS_PER_MIN
Expand Down Expand Up @@ -713,7 +713,7 @@ PyObject* precise_diff(PyObject *self, PyObject *args) {
);

if (dt1_gt_dt2) {
PyObject* temp;
PyObject* temp;
temp = dt1;
dt1 = dt2;
dt2 = temp;
Expand Down
38 changes: 18 additions & 20 deletions pendulum/_extensions/helpers.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import math
from collections import namedtuple

import datetime
import math
import typing

from ..constants import (
EPOCH_YEAR,
SECS_PER_DAY,
SECS_PER_400_YEARS,
SECS_PER_100_YEARS,
SECS_PER_4_YEARS,
SECS_PER_YEAR,
SECS_PER_HOUR,
SECS_PER_MIN,
DAYS_PER_MONTHS,
MONTHS_OFFSETS,
TM_DECEMBER,
TM_JANUARY,
DAY_OF_WEEK_TABLE,
DAYS_PER_L_YEAR,
DAYS_PER_N_YEAR,
)
from collections import namedtuple

from ..constants import DAY_OF_WEEK_TABLE
from ..constants import DAYS_PER_L_YEAR
from ..constants import DAYS_PER_MONTHS
from ..constants import DAYS_PER_N_YEAR
from ..constants import EPOCH_YEAR
from ..constants import MONTHS_OFFSETS
from ..constants import SECS_PER_4_YEARS
from ..constants import SECS_PER_100_YEARS
from ..constants import SECS_PER_400_YEARS
from ..constants import SECS_PER_DAY
from ..constants import SECS_PER_HOUR
from ..constants import SECS_PER_MIN
from ..constants import SECS_PER_YEAR
from ..constants import TM_DECEMBER
from ..constants import TM_JANUARY


class PreciseDiff(
Expand Down
Loading

0 comments on commit 205a86a

Please sign in to comment.