Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Jun 24, 2019
1 parent 5599444 commit 29d4be2
Show file tree
Hide file tree
Showing 38 changed files with 436 additions and 425 deletions.
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
language: python

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

install:
# install deps
- pip install -r requirements.txt
- pip install pytest-cov mock assertpy python-coveralls
- pip install fastcluster
- pip install twisted
- pip install python-coveralls
- pip list

script:
- nosetests
- py.test

after_success:
- coveralls
2 changes: 1 addition & 1 deletion blitzortung/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Error(Exception):
from . import db

INJECTOR = injector.Injector(
[config.ConfigModule(), db.DbModule()])
[config.ConfigModule(), db.DbModule()])

__all__ = [

Expand Down
4 changes: 2 additions & 2 deletions blitzortung/builder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .base import Timestamp, Event, BuilderError
from .strike import Strike
from .station import Station, StationOffline
from .raw_signal import RawWaveformEvent, ChannelWaveform
from .station import Station, StationOffline
from .strike import Strike
8 changes: 4 additions & 4 deletions blitzortung/builder/raw_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"""

import itertools
import datetime

from injector import inject

from ..util import next_element
from .base import Event, BuilderError
from .. import data
from ..util import next_element


class ChannelWaveform(object):
Expand Down Expand Up @@ -94,8 +94,8 @@ class RawWaveformEvent(Event):
class for building of raw signal objects
"""

@inject(channel_builder=ChannelWaveform)
def __init__(self, channel_builder):
@inject
def __init__(self, channel_builder: ChannelWaveform):
super(RawWaveformEvent, self).__init__()
self.altitude = 0
self.channels = []
Expand Down
3 changes: 2 additions & 1 deletion blitzortung/builder/strike.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"""

import re

from .base import Event, BuilderError
from ..util import force_range
from .. import data
from ..util import force_range


class Strike(Event):
Expand Down
1 change: 1 addition & 0 deletions blitzortung/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"""

from __future__ import division

import time


Expand Down
12 changes: 5 additions & 7 deletions blitzortung/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@
"""

from __future__ import unicode_literals

try:
import configparser
except ImportError:
import ConfigParser as configparser

from injector import Module, singleton, provides, inject
from injector import Module, singleton, inject, provider


@singleton
class Config(object):
@inject(config_parser=configparser.ConfigParser)
def __init__(self, config_parser):
@inject
def __init__(self, config_parser: configparser.ConfigParser):
self.config_parser = config_parser

def get_username(self):
Expand Down Expand Up @@ -69,8 +67,8 @@ def config():

class ConfigModule(Module):
@singleton
@provides(configparser.ConfigParser)
def provide_config_parser(self):
@provider
def provide_config_parser(self) -> configparser.ConfigParser:
config_parser = configparser.ConfigParser()
config_parser.read('/etc/blitzortung.conf')
return config_parser
33 changes: 18 additions & 15 deletions blitzortung/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
"""

from __future__ import unicode_literals, generators

import datetime
import math

import pytz
import six

from . import types
import math
from blitzortung.geom import GridElement
from . import types


class Timestamp(types.EqualityAndHash):
Expand All @@ -37,15 +35,15 @@ class Timestamp(types.EqualityAndHash):
__slots__ = ['datetime', 'nanosecond']

def __init__(self, date_time=datetime.datetime.utcnow().replace(tzinfo=pytz.UTC), nanosecond=0):
if type(date_time) == str or type(date_time) == unicode:
if type(date_time) == str:
date_time, date_time_nanosecond = Timestamp.from_timestamp(date_time)
nanosecond += date_time_nanosecond
elif type(date_time) == int:
date_time, date_time_nanosecond = Timestamp.from_nanoseconds(date_time)
nanosecond += date_time_nanosecond

if nanosecond < 0 or nanosecond > 999:
microdelta = nanosecond / 1000
microdelta = nanosecond // 1000
date_time += datetime.timedelta(microseconds=microdelta)
nanosecond -= microdelta * 1000

Expand All @@ -71,13 +69,13 @@ def from_timestamp(timestamp_string):

@staticmethod
def from_nanoseconds(total_nanoseconds):
total_microseconds = total_nanoseconds / 1000
total_microseconds = total_nanoseconds // 1000
residual_nanoseconds = total_nanoseconds % 1000
total_seconds = total_microseconds / 1000000
total_seconds = total_microseconds // 1000000
residual_microseconds = total_microseconds % 1000000
return datetime.datetime(1970, 1, 1, tzinfo=pytz.UTC) + \
datetime.timedelta(seconds=total_seconds,
milliseconds=residual_microseconds), \
microseconds=residual_microseconds), \
residual_nanoseconds

@property
Expand Down Expand Up @@ -130,25 +128,29 @@ def __lt__(self, other):
if type(other) == datetime.datetime:
return self.datetime < other
else:
return self.datetime < other.datetime or (self.datetime == other.datetime and self.nanosecond < other.nanosecond)
return self.datetime < other.datetime or (
self.datetime == other.datetime and self.nanosecond < other.nanosecond)

def __le__(self, other):
if type(other) == datetime.datetime:
return self.datetime <= other
else:
return self.datetime < other.datetime or (self.datetime == other.datetime and self.nanosecond <= other.nanosecond)
return self.datetime < other.datetime or (
self.datetime == other.datetime and self.nanosecond <= other.nanosecond)

def __gt__(self, other):
if type(other) == datetime.datetime:
return self.datetime > other
else:
return self.datetime > other.datetime or (self.datetime == other.datetime and self.nanosecond > other.nanosecond)
return self.datetime > other.datetime or (
self.datetime == other.datetime and self.nanosecond > other.nanosecond)

def __ge__(self, other):
if type(other) == datetime.datetime:
return self.datetime >= other
else:
return self.datetime > other.datetime or (self.datetime == other.datetime and self.nanosecond >= other.nanosecond)
return self.datetime > other.datetime or (
self.datetime == other.datetime and self.nanosecond >= other.nanosecond)

def __add__(self, other):
if type(other) == Timedelta:
Expand Down Expand Up @@ -188,7 +190,7 @@ def __repr__(self):
class Timedelta(types.EqualityAndHash):
def __init__(self, timedelta=datetime.timedelta(), nanodelta=0):
if nanodelta < 0 or nanodelta > 999:
microdelta = nanodelta / 1000
microdelta = nanodelta // 1000
timedelta += datetime.timedelta(microseconds=microdelta)
nanodelta -= microdelta * 1000
self.timedelta = timedelta
Expand Down Expand Up @@ -298,7 +300,8 @@ def __str__(self):
offline_since = self.timestamp
status_char = "*" if offline_since is None else "-"
try:
status_text = "" if offline_since is None else " offline since " + offline_since.strftime("%Y-%m-%d %H:%M %Z")
status_text = "" if offline_since is None else " offline since " + offline_since.strftime(
"%Y-%m-%d %H:%M %Z")
except ValueError:
status_text = 'n/a'
return u"%s%3d/%3d '%s' '%s' (%.4f, %.4f)%s" % (
Expand Down
11 changes: 5 additions & 6 deletions blitzortung/dataimport/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
"""

from abc import abstractmethod

import os
import logging
import datetime
import logging
import os
from abc import abstractmethod

try:
from html.parser import HTMLParser
Expand Down Expand Up @@ -60,8 +59,8 @@ class HttpFileTransport(FileTransport):
logger = logging.getLogger(__name__)
html_parser = HTMLParser()

@inject(configuration=config.Config)
def __init__(self, configuration, session=None):
@inject
def __init__(self, configuration: config.Config, session=None):
self.config = configuration
self.session = session if session else Session()

Expand Down
6 changes: 3 additions & 3 deletions blitzortung/dataimport/raw_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
class RawSignalsBlitzortungDataProvider(object):
logger = logging.getLogger(__name__)

@inject(data_transport=HttpFileTransport, data_url=BlitzortungDataPath,
url_path_generator=BlitzortungDataPathGenerator, waveform_builder=builder.RawWaveformEvent)
def __init__(self, data_transport, data_url, url_path_generator, waveform_builder):
@inject
def __init__(self, data_transport: HttpFileTransport, data_url: BlitzortungDataPath,
url_path_generator: BlitzortungDataPathGenerator, waveform_builder: builder.RawWaveformEvent):
self.data_transport = data_transport
self.data_url = data_url
self.url_path_generator = url_path_generator
Expand Down
7 changes: 4 additions & 3 deletions blitzortung/dataimport/station.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
from .base import HttpFileTransport, BlitzortungDataPath
from .. import builder


@singleton
class StationsBlitzortungDataProvider(object):
logger = logging.getLogger(__name__)

@inject(data_transport=HttpFileTransport, data_url=BlitzortungDataPath,
station_builder=builder.Station)
def __init__(self, data_transport, data_url, station_builder):
@inject
def __init__(self, data_transport: HttpFileTransport, data_url: BlitzortungDataPath,
station_builder: builder.Station):
self.data_transport = data_transport
self.data_url = data_url
self.station_builder = station_builder
Expand Down
11 changes: 6 additions & 5 deletions blitzortung/dataimport/strike.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@
"""

import datetime
import logging
import os
import time
import pytz
import datetime

import pytz
from injector import singleton, inject

from .base import HttpFileTransport, BlitzortungDataPath, BlitzortungDataPathGenerator
from .. import builder


@singleton
class StrikesBlitzortungDataProvider(object):
logger = logging.getLogger(__name__)

@inject(data_transport=HttpFileTransport, data_url=BlitzortungDataPath,
url_path_generator=BlitzortungDataPathGenerator, strike_builder=builder.Strike)
def __init__(self, data_transport, data_url, url_path_generator, strike_builder):
@inject
def __init__(self, data_transport: HttpFileTransport, data_url: BlitzortungDataPath,
url_path_generator: BlitzortungDataPathGenerator, strike_builder: builder.Strike):
self.data_transport = data_transport
self.data_url = data_url
self.url_path_generator = url_path_generator
Expand Down
9 changes: 5 additions & 4 deletions blitzortung/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"""

from injector import Module, provides, singleton, inject
import atexit

from injector import Module, singleton, inject, provider


def create_psycopg2_dummy():
class Dummy(object):
Expand Down Expand Up @@ -51,9 +52,9 @@ def cleanup(connection_pool):
connection_pool.closeall()

@singleton
@provides(psycopg2.pool.ThreadedConnectionPool)
@inject(config=config.Config)
def provide_psycopg2_connection_pool(self, config):
@provider
@inject
def provide_psycopg2_connection_pool(self, config: config.Config) -> psycopg2.pool.ThreadedConnectionPool:
connection_pool = psycopg2.pool.ThreadedConnectionPool(4, 50, config.get_db_connection_string())
atexit.register(self.cleanup, connection_pool)
return connection_pool
Expand Down
15 changes: 8 additions & 7 deletions blitzortung/db/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"""

from abc import abstractmethod
from injector import inject

import pytz
import shapely.wkb
from injector import inject

import blitzortung.builder

Expand All @@ -42,8 +43,8 @@ def convert_to_timezone(timestamp, target_timezone=None):


class Strike(ObjectMapper):
@inject(strike_builder=blitzortung.builder.Strike)
def __init__(self, strike_builder):
@inject
def __init__(self, strike_builder: blitzortung.builder.Strike):
self.strike_builder = strike_builder

def create_object(self, result, **kwargs):
Expand All @@ -64,8 +65,8 @@ def create_object(self, result, **kwargs):


class Station(ObjectMapper):
@inject(station_builder=blitzortung.builder.Station)
def __init__(self, station_builder):
@inject
def __init__(self, station_builder: blitzortung.builder.Station):
self.station_builder = station_builder

def create_object(self, result, **kwargs):
Expand All @@ -85,8 +86,8 @@ def create_object(self, result, **kwargs):


class StationOffline(ObjectMapper):
@inject(station_offline_builder=blitzortung.builder.StationOffline)
def __init__(self, station_offline_builder):
@inject
def __init__(self, station_offline_builder: blitzortung.builder.StationOffline):
self.station_offline_builder = station_offline_builder

def create_object(self, result, **kwargs):
Expand Down
Loading

0 comments on commit 29d4be2

Please sign in to comment.