Skip to content

Commit 13eada6

Browse files
committed
Port to Python3
Signed-off-by: David Gibson <[email protected]>
1 parent d48d45d commit 13eada6

32 files changed

+107
-153
lines changed

.travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ language: python
33
sudo: false
44

55
python:
6-
- "2.7"
6+
- "3.6"
77

88
install:
9-
- "pip install -r requirements.txt"
9+
- "pip3 install -r requirements.txt"
1010

1111
script:
12-
- make check
12+
- make check NOSE=nosetests

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
NOSE = nosetests
2-
PEP8 = pep8
3-
FLAKE8 = flake8-2
1+
NOSE = nosetests-3
2+
PEP8 = python3-pep8
3+
FLAKE8 = python3-flake8
44

55
NOSEFLAGS = --with-coverage --cover-package=smadata2
66

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
python-dateutil
22
coverage
3+
nose

sma2-bad-date-check

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#! /usr/bin/env python
2-
3-
from __future__ import print_function
1+
#! /usr/bin/python
42

53
import sys
64
import datetime

sma2-correct-date

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#! /usr/bin/env python
2-
3-
from __future__ import print_function
1+
#! /usr/bin/python3
42

53
import sys
64
import time

sma2-explore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# sma2-explore - Interactive tool for analyzing SMAData2 protocol
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -196,7 +196,7 @@ class SMAData2CLI(Connection):
196196
while True:
197197
sys.stdout.write("SMA2 %s >> " % self.remote_addr)
198198
try:
199-
line = raw_input().split()
199+
line = input().split()
200200
except EOFError:
201201
return
202202

sma2-push-daily-to-pvoutput

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# sma2-download - Download historic generation info from SMA inverters
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -17,8 +17,6 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
2220
import datetime
2321
import time
2422

sma2-upload-to-pvoutputorg

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# sma2-upload-to-pvoutputorg - Upload generation history to pvoutput.org
44
# Copyright (C) 2014 Peter Barker <[email protected]>
@@ -17,8 +17,6 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
2220
import argparse
2321
import datetime
2422
import sys

sma2mon

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22

33
import smadata2.sma2mon
44

smadata2/__init__.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.__init__ - Python code for the SMAData2 protocol
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -16,7 +16,3 @@
1616
# You should have received a copy of the GNU General Public License along
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19-
20-
# Nothing to see here
21-
22-
from __future__ import print_function

smadata2/check.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.check - Testing utilities
44
# Copyright (C) 2015 David Gibson <[email protected]>
@@ -17,8 +17,6 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
2220

2321
def generate_linear(start, dawn, dusk, end, startyield, rate):
2422
results = []

smadata2/config.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.config - Configuration file handling for SMAData2 code
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -17,19 +17,16 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
2220
import sys
2321
import os
2422
import dateutil.parser
2523
import dateutil.tz
2624
import json
2725

28-
import inverter
29-
import inverter.smabluetooth
30-
import pvoutputorg
31-
import datetimeutil
32-
import db
26+
from .inverter import smabluetooth
27+
from . import pvoutputorg
28+
from . import datetimeutil
29+
from . import db
3330

3431
DEFAULT_CONFIG_FILE = os.path.expanduser("~/.smadata2.json")
3532

@@ -45,7 +42,7 @@ def __init__(self, invjson, defname):
4542
self.starttime = None
4643

4744
def connect(self):
48-
return inverter.smabluetooth.Connection(self.bdaddr)
45+
return smabluetooth.Connection(self.bdaddr)
4946

5047
def connect_and_logon(self):
5148
conn = self.connect()

smadata2/datetimeutil.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.datetimeutil - Date and time helper functions
44
# Copyright (C) 2014, 2015 David Gibson <[email protected]>
@@ -17,8 +17,6 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
2220
import dateutil.parser
2321
import dateutil.tz
2422
import datetime

smadata2/db/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.db - Database for logging data from SMA inverters
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -17,7 +17,5 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
2220
from .base import WrongSchema
2321
from .sqlite import SQLiteDatabase

smadata2/db/base.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.db.base - Abstract database interface
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -17,8 +17,6 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
2220
import abc
2321

2422

@@ -33,9 +31,7 @@ class WrongSchema(Error):
3331
pass
3432

3533

36-
class BaseDatabase(object):
37-
__metaclass__ = abc.ABCMeta
38-
34+
class BaseDatabase(object, metaclass=abc.ABCMeta):
3935
@abc.abstractmethod
4036
def add_historic(self, serial, timestamp, total_yield):
4137
raise NotImplementedError()

smadata2/db/mock.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.db - Database for logging data from SMA inverters
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -17,9 +17,7 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
22-
from base import BaseDatabase
20+
from .base import BaseDatabase
2321

2422

2523
class MockDatabase(BaseDatabase):

smadata2/db/sqlite.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.db - Database for logging data from SMA inverters
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -17,15 +17,13 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
2220
import shutil
2321
import re
2422
import sqlite3
2523
import time
2624
import datetime
2725

28-
from base import BaseDatabase, WrongSchema
26+
from .base import BaseDatabase, WrongSchema
2927

3028

3129
all = ['SQLiteDatabase']
@@ -128,7 +126,7 @@ def midnights(self, inverters):
128126
ORDER BY timestamp ASC"""
129127
c.execute(template, (serials,))
130128
r = c.fetchall()
131-
r = map(lambda x: datetime.datetime.utcfromtimestamp(x[0]), r)
129+
r = [datetime.datetime.utcfromtimestamp(x[0]) for x in r]
132130
return r
133131

134132
def get_datapoint_totals_for_day(self, inverters, start_datetime):

smadata2/db/tests.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#! /usr/bin/env python
2-
3-
from __future__ import print_function
1+
#! /usr/bin/python3
42

53
import os
64
import os.path
@@ -51,7 +49,7 @@ def prepare_sqlite(self):
5149
self.prepopulate()
5250

5351
if os.path.exists(self.dbname):
54-
self.original = open(self.dbname).read()
52+
self.original = open(self.dbname, 'rb').read()
5553
else:
5654
self.original = None
5755

@@ -149,7 +147,7 @@ def test_basic(self):
149147
assert_equals(y1, i)
150148
assert_equals(y2, 2*i)
151149

152-
val = (self.dusk - self.dawn - 1) / 300
150+
val = (self.dusk - self.dawn - 1) // 300
153151
for ts in range(self.dusk, 24*3600, 300):
154152
y1 = self.db.get_one_historic(self.serial1, ts)
155153
y2 = self.db.get_one_historic(self.serial2, ts)
@@ -160,7 +158,7 @@ def test_basic(self):
160158
def test_aggregate_one(self):
161159
val = self.db.get_aggregate_one_historic(self.dusk,
162160
(self.serial1, self.serial2))
163-
assert_equals(val, 3*((self.dusk - self.dawn - 2) / 300))
161+
assert_equals(val, 3*((self.dusk - self.dawn - 2) // 300))
164162

165163
def check_aggregate_range(self, from_, to_):
166164
results = self.db.get_aggregate_historic(from_, to_,
@@ -176,9 +174,9 @@ def check_aggregate_range(self, from_, to_):
176174
if ts < self.dawn:
177175
assert_equals(y, 0)
178176
elif ts < self.dusk:
179-
assert_equals(y, 3*((ts - self.dawn) / 300))
177+
assert_equals(y, 3*((ts - self.dawn) // 300))
180178
else:
181-
assert_equals(y, 3*((self.dusk - self.dawn - 1) / 300))
179+
assert_equals(y, 3*((self.dusk - self.dawn - 1) // 300))
182180

183181
def test_aggregate(self):
184182
yield self.check_aggregate_range, 0, 24*3600
@@ -203,7 +201,7 @@ class UpdateSQLiteChecker(SQLiteDBChecker):
203201

204202
def test_backup(self):
205203
assert os.path.exists(self.bakname)
206-
backup = open(self.bakname).read()
204+
backup = open(self.bakname, 'rb').read()
207205
assert_equals(self.original, backup)
208206

209207
def test_preserved(self):

smadata2/download.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.download - Routines to download from an inverter to database
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -17,7 +17,6 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
2120
import time
2221

2322

smadata2/inverter/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.inverter - Inverter connections
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -16,5 +16,3 @@
1616
# You should have received a copy of the GNU General Public License along
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19-
20-
from __future__ import print_function

smadata2/inverter/base.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.db.base - Abstract inverter interface
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -17,8 +17,6 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
2220
import abc
2321

2422

@@ -29,9 +27,7 @@ class Error(Exception):
2927
pass
3028

3129

32-
class InverterConnection(object):
33-
__metaclass__ = abc.ABCMeta
34-
30+
class InverterConnection(object, metaclass=abc.ABCMeta):
3531
@abc.abstractmethod
3632
def total_yield(self):
3733
raise NotImplementedError()

smadata2/inverter/mock.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/python3
22
#
33
# smadata2.inverter.mock - Mock inverter connections
44
# Copyright (C) 2014 David Gibson <[email protected]>
@@ -17,9 +17,7 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20-
from __future__ import print_function
21-
22-
from base import InverterConnection
20+
from .base import InverterConnection
2321

2422

2523
class MockInverterZero(InverterConnection):

0 commit comments

Comments
 (0)