Skip to content

Commit

Permalink
Fixes for CodeQL (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
tammoippen authored Dec 5, 2022
1 parent 977a156 commit 4a47f55
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 57 deletions.
7 changes: 3 additions & 4 deletions plotille/_cmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Colormap(object):
Colormap represents. Scaling the data into the `[0, 1]` interval is
responsibility of the caller.
"""
def __init__(self, name):
def __init__(self, name, lookup_table=None):
"""
Parameters
----------
Expand All @@ -48,7 +48,7 @@ def __init__(self, name):
The number of rgb quantization levels.
"""
self.name = name
self._lookup_table = None
self._lookup_table = lookup_table
self.bad = None
self.over = None
self.under = None
Expand Down Expand Up @@ -86,8 +86,7 @@ def _process_value(self, x):

class ListedColormap(Colormap):
def __init__(self, name, colors):
super(ListedColormap, self).__init__(name)
self._lookup_table = colors
super(ListedColormap, self).__init__(name, lookup_table=colors)

@classmethod
def from_relative(cls, name, colors):
Expand Down
10 changes: 6 additions & 4 deletions plotille/_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ._colors import color, rgb2byte
from ._figure_data import Heat, Histogram, Plot, Span, Text
from ._input_formatter import InputFormatter
from ._util import mk_timedelta, timestamp
from ._util import mk_timedelta

# TODO documentation!!!
# TODO tests
Expand Down Expand Up @@ -244,7 +244,7 @@ def _limits(self, low_set, high_set, is_height):
def _y_axis(self, ymin, ymax, label='Y'):
delta = abs(ymax - ymin)
if isinstance(delta, timedelta):
y_delta = mk_timedelta(timestamp(delta) / self.height)
y_delta = mk_timedelta(delta.total_seconds() / self.height)
else:
y_delta = delta / self.height

Expand All @@ -271,7 +271,7 @@ def _y_axis(self, ymin, ymax, label='Y'):
def _x_axis(self, xmin, xmax, label='X', with_y_axis=False):
delta = abs(xmax - xmin)
if isinstance(delta, timedelta):
x_delta = mk_timedelta(timestamp(delta) / self.width)
x_delta = mk_timedelta(delta.total_seconds() / self.width)
else:
x_delta = delta / self.width
starts = ['', '']
Expand Down Expand Up @@ -546,7 +546,7 @@ def _diff(low, high):
else:
delta = abs(high - low)
if isinstance(delta, timedelta):
return mk_timedelta(timestamp(delta) * 0.1)
return mk_timedelta(delta.total_seconds() * 0.1)
else:
return delta * 0.1

Expand All @@ -568,6 +568,7 @@ def _default(low_set, high_set):
return low_set, 1.0

# Should never get here! => checked in function before
raise ValueError('Unexpected inputs!')


def _choose(low, high, low_set, high_set):
Expand Down Expand Up @@ -600,3 +601,4 @@ def _choose(low, high, low_set, high_set):
return low_set, high + diff

# Should never get here! => checked in function before
raise ValueError('Unexpected inputs!')
7 changes: 4 additions & 3 deletions plotille/_input_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from datetime import date, datetime, time, timedelta
import math

from ._util import roundeven, timestamp
from ._util import roundeven


class InputFormatter(object):
Expand Down Expand Up @@ -138,6 +138,7 @@ def _num_formatter(val, chars, delta, left=False):
return _int_formatter(val, chars, left)
elif isinstance(val, float):
return _float_formatter(val, chars, left)
# unreachable


def _float_formatter(val, chars, left=False):
Expand Down Expand Up @@ -206,7 +207,7 @@ def _convert_numbers(v):

def _convert_np_datetime(v):
# assert isinstance(v, np.datetime64)
return timestamp(v.item())
return v.item().timestamp()


def _convert_date(v):
Expand All @@ -216,4 +217,4 @@ def _convert_date(v):

def _convert_datetime(v):
assert isinstance(v, datetime)
return timestamp(v)
return v.timestamp()
21 changes: 3 additions & 18 deletions plotille/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from datetime import datetime, timedelta, tzinfo
from datetime import timedelta, tzinfo
import math
import time


def roundeven(x):
Expand Down Expand Up @@ -78,7 +77,7 @@ def hist(X, bins):
is_datetime = False
if isinstance(delta, timedelta):
is_datetime = True
delta = timestamp(delta)
delta = delta.total_seconds()

xwidth = delta / bins

Expand All @@ -87,7 +86,7 @@ def hist(X, bins):
x_ = _numpy_to_native(x)
delta = (x_ - xmin)
if isinstance(delta, timedelta):
delta = timestamp(delta)
delta = delta.total_seconds()
x_idx = min(bins - 1, int(delta // xwidth))
y[x_idx] += 1

Expand All @@ -111,20 +110,6 @@ def dst(self, dt):
return self._ZERO


_EPOCH = datetime(1970, 1, 1, tzinfo=_UTC())


def timestamp(v):
"""Get timestamp of `v` datetime in py2/3."""
if isinstance(v, datetime):
if v.tzinfo is None:
return time.mktime(v.timetuple()) + v.microsecond / 1e6
else:
return (v - _EPOCH).total_seconds()
elif isinstance(v, timedelta):
return v.total_seconds()


def mk_timedelta(v):
seconds = int(v)
microseconds = int((v - seconds) * 1e6)
Expand Down
42 changes: 30 additions & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_circle(radius):
assert Y[0] == pytest.approx(Y[-1], abs=0.001)

for x, y in zip(X, Y):
assert math.sqrt(x**2 + y**2) == pytest.approx(radius**10, abs=0.001)
assert math.hypot(x, y) == pytest.approx(radius**10, abs=0.001)
equal = 0
for x2, y2 in zip(X, Y):
if x2 == x and y2 == y:
Expand Down
15 changes: 0 additions & 15 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@

import pytest

try:
import numpy # noqa: F401
have_numpy = True
except ImportError:
have_numpy = False


try:
from PIL import Image # noqa: F401
have_pillow = True
except ImportError:
have_pillow = False


@pytest.fixture
def change_to_examples_dir(request):
Expand All @@ -28,8 +15,6 @@ def change_to_examples_dir(request):
os.chdir(str(request.config.invocation_dir))


@pytest.mark.skipif(not have_numpy, reason='No numpy installed.')
@pytest.mark.skipif(not have_pillow, reason='No pillow installed.')
def test_examples(change_to_examples_dir):
sys.path.insert(0, '.')
for fname in glob('*_example.py'):
Expand Down

0 comments on commit 4a47f55

Please sign in to comment.