Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Jun 24, 2019
1 parent e853e4e commit db536aa
Show file tree
Hide file tree
Showing 32 changed files with 142 additions and 123 deletions.
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
4 changes: 2 additions & 2 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
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
8 changes: 4 additions & 4 deletions blitzortung/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,28 +129,28 @@ def __lt__(self, other):
return self.datetime < other
else:
return self.datetime < other.datetime or (
self.datetime == other.datetime and self.nanosecond < other.nanosecond)
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)
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)
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)
self.datetime == other.datetime and self.nanosecond >= other.nanosecond)

def __add__(self, other):
if type(other) == Timedelta:
Expand Down
7 changes: 3 additions & 4 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
3 changes: 2 additions & 1 deletion blitzortung/dataimport/raw_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class RawSignalsBlitzortungDataProvider(object):
logger = logging.getLogger(__name__)

@inject
def __init__(self, data_transport: HttpFileTransport, data_url: BlitzortungDataPath, url_path_generator: BlitzortungDataPathGenerator, waveform_builder: builder.RawWaveformEvent):
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
5 changes: 3 additions & 2 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, singleton, inject, provider
import atexit

from injector import Module, singleton, inject, provider


def create_psycopg2_dummy():
class Dummy(object):
Expand Down Expand Up @@ -53,7 +54,7 @@ def cleanup(connection_pool):
@singleton
@provider
@inject
def provide_psycopg2_connection_pool(self, config : config.Config) -> psycopg2.pool.ThreadedConnectionPool:
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
3 changes: 2 additions & 1 deletion 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 Down
25 changes: 13 additions & 12 deletions blitzortung/db/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from __future__ import print_function

import datetime

import shapely.geometry.base
import shapely.wkb

Expand Down Expand Up @@ -203,9 +204,9 @@ def add_geometry(self, geometry):

if not geometry.equals(geometry.envelope):
self.add_condition(
'ST_Intersects(ST_GeomFromWKB(%(geometry)s, %(srid)s), ' +
'ST_Transform(geog::geometry, %(srid)s))',
geometry=psycopg2.Binary(shapely.wkb.dumps(geometry)))
'ST_Intersects(ST_GeomFromWKB(%(geometry)s, %(srid)s), ' +
'ST_Transform(geog::geometry, %(srid)s))',
geometry=psycopg2.Binary(shapely.wkb.dumps(geometry)))

else:
raise ValueError("invalid geometry in db.Strike.select()")
Expand Down Expand Up @@ -253,18 +254,18 @@ def __init__(self, raster, count_threshold=0):
self.raster = raster

self.add_parameters(
srid=raster.srid,
xmin=raster.x_min,
xdiv=raster.x_div,
ymin=raster.y_min,
ydiv=raster.y_div,
srid=raster.srid,
xmin=raster.x_min,
xdiv=raster.x_div,
ymin=raster.y_min,
ydiv=raster.y_div,
)

self.set_columns(
'TRUNC((ST_X(ST_Transform(geog::geometry, %(srid)s)) - %(xmin)s) / %(xdiv)s)::integer AS rx',
'TRUNC((ST_Y(ST_Transform(geog::geometry, %(srid)s)) - %(ymin)s) / %(ydiv)s)::integer AS ry',
'count(*) AS strike_count',
'max("timestamp") as "timestamp"'
'TRUNC((ST_X(ST_Transform(geog::geometry, %(srid)s)) - %(xmin)s) / %(xdiv)s)::integer AS rx',
'TRUNC((ST_Y(ST_Transform(geog::geometry, %(srid)s)) - %(ymin)s) / %(ydiv)s)::integer AS ry',
'count(*) AS strike_count',
'max("timestamp") as "timestamp"'
)

env = self.raster.env
Expand Down
6 changes: 4 additions & 2 deletions blitzortung/db/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ class Strike(Base):
TABLE_NAME = 'strikes'

@inject
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool, query_builder_: query_builder.Strike , strike_mapper: mapper.Strike):
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool, query_builder_: query_builder.Strike,
strike_mapper: mapper.Strike):
super(Strike, self).__init__(db_connection_pool)

self.query_builder = query_builder_
Expand Down Expand Up @@ -393,7 +394,8 @@ class StationOffline(Base):
"""

@inject
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool, station_offline_mapper: mapper.StationOffline):
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool,
station_offline_mapper: mapper.StationOffline):
super(StationOffline, self).__init__(db_connection_pool)

self.table_name = 'stations_offline'
Expand Down
10 changes: 6 additions & 4 deletions blitzortung/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
"""

from __future__ import print_function
import os
import subprocess
import glob

import datetime
import glob
import json
import os
import subprocess

import pandas as pd

import blitzortung.builder
import blitzortung
import blitzortung.builder


class Raw(object):
Expand Down
6 changes: 3 additions & 3 deletions blitzortung/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import math
from abc import ABCMeta, abstractmethod
import pyproj

import pyproj
import shapely.geometry


Expand Down Expand Up @@ -82,8 +82,8 @@ def contains(self, point):
@property
def env(self):
return shapely.geometry.LinearRing(
[(self.x_min, self.y_min), (self.x_min, self.y_max), (self.x_max, self.y_max),
(self.x_max, self.y_min)])
[(self.x_min, self.y_min), (self.x_min, self.y_max), (self.x_max, self.y_max),
(self.x_max, self.y_min)])

def __repr__(self):
return 'Envelope(x: %.4f..%.4f, y: %.4f..%.4f)' % (
Expand Down
2 changes: 1 addition & 1 deletion blitzortung/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ def create_console_handler():


def get_logger_name(clazz):
return "{}.{}".format(clazz.__module__, clazz.__name__)
return "{}.{}".format(clazz.__module__, clazz.__name__)
5 changes: 3 additions & 2 deletions blitzortung/service/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"""

import time
import datetime
import time

import pytz

import blitzortung


Expand All @@ -34,7 +36,6 @@ def create_time_interval(minute_length, minute_offset):


class TimingState(object):

__slots__ = ['statsd_client', 'reference_time', 'name', 'info_text']

def __init__(self, name, statsd_client):
Expand Down
4 changes: 3 additions & 1 deletion blitzortung/service/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
"""

from injector import inject
import time

from injector import inject

import blitzortung.db.query_builder


Expand Down
7 changes: 4 additions & 3 deletions blitzortung/service/strike.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
from __future__ import print_function

import time

from injector import inject
from twisted.internet.defer import gatherResults
from twisted.python import log

from .. import db, geom
from .general import TimingState, create_time_interval
from .. import db, geom


class StrikeState(TimingState):

__slots__ = ['end_time']

def __init__(self, statsd_client, end_time):
Expand All @@ -51,7 +51,8 @@ def create(self, id_or_offset, minute_length, minute_offset, connection, statsd_
id_interval = db.query.IdInterval(id_or_offset) if id_or_offset > 0 else None
order = db.query.Order('id')
query = self.strike_query_builder.select_query(db.table.Strike.TABLE_NAME, geom.Geometry.DefaultSrid,
time_interval=time_interval, order=order, id_interval=id_interval)
time_interval=time_interval, order=order,
id_interval=id_interval)

strikes_result = connection.runQuery(str(query), query.get_parameters())
strikes_result.addCallback(self.strike_build_results, state=state)
Expand Down
18 changes: 9 additions & 9 deletions blitzortung/service/strike_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
"""

from injector import inject
import time

from injector import inject
from twisted.internet.defer import gatherResults
from twisted.python import log

from .. import db

from .general import create_time_interval, TimingState
from .. import db


class StrikeGridState(TimingState):
Expand Down Expand Up @@ -65,12 +65,12 @@ def build_strikes_grid_result(results, state):
y_bin_count = state.grid_parameters.y_bin_count
end_time = state.time_interval.end
strikes_grid_result = tuple(
(
result['rx'],
y_bin_count - result['ry'],
result['strike_count'],
-(end_time - result['timestamp']).seconds
) for result in results if 0 <= result['rx'] < x_bin_count and 0 < result['ry'] <= y_bin_count
(
result['rx'],
y_bin_count - result['ry'],
result['strike_count'],
-(end_time - result['timestamp']).seconds
) for result in results if 0 <= result['rx'] < x_bin_count and 0 < result['ry'] <= y_bin_count
)
state.add_info_text(", result %.03fs" % state.get_seconds(reference_time))
state.log_timing('strikes_grid.build_result', reference_time)
Expand Down
1 change: 1 addition & 0 deletions blitzortung/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"""

import math

import pyproj


Expand Down
5 changes: 3 additions & 2 deletions blitzortung/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"""

import sys

import datetime
import sys
import time

import pytz

from blitzortung.data import Timestamp
Expand Down Expand Up @@ -102,4 +102,5 @@ def time_intervals(start_time, duration, end_time=None):
def force_range(lower_limit, value, upper_limit):
return lower_limit if value < lower_limit else upper_limit if value > upper_limit else value


next_element = (lambda iterator: iterator.next()) if sys.version < '3' else (lambda iterator: next(iterator))
Loading

0 comments on commit db536aa

Please sign in to comment.