Skip to content

Commit

Permalink
Modernize Python 2 code to prepair for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Jul 12, 2019
1 parent dbdcdc9 commit f011b01
Show file tree
Hide file tree
Showing 63 changed files with 308 additions and 230 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ addons:

python:
- "2.7"
- "3.6"

matrix:
allow_failures:
- python: "3.6"

install:
- pip install .

before_script:
- pip install flake8
# TODO: Fix the flake8 tests and the remove the --exit-zero
- flake8 . --count --exit-zero --select=E9,F63,F7,F82 --show-source --statistics

script: gryphon-runtests
5 changes: 3 additions & 2 deletions gryphon/dashboards/handlers/block_times.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from admin_base import AdminBaseHandler
from __future__ import absolute_import
from .admin_base import AdminBaseHandler
import tornado.web

from mixins.start_and_end_time import StartAndEndTimeMixin
from .mixins.start_and_end_time import StartAndEndTimeMixin
import util.tick_times as tick_times


Expand Down
3 changes: 2 additions & 1 deletion gryphon/dashboards/handlers/home.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import tornado.web

from admin_base import AdminBaseHandler
from .admin_base import AdminBaseHandler


class HomeHandler(AdminBaseHandler):
Expand Down
2 changes: 1 addition & 1 deletion gryphon/dashboards/handlers/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def get_exchange_for_address(self, address):
"""

if address and address in self.address_map:
return address_map[address]
return self.address_map[address]
else:
return 'External transfer'

Expand Down
5 changes: 3 additions & 2 deletions gryphon/dashboards/handlers/tick_times.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from admin_base import AdminBaseHandler
from __future__ import absolute_import
from .admin_base import AdminBaseHandler
import tornado.web

from mixins.start_and_end_time import StartAndEndTimeMixin
from .mixins.start_and_end_time import StartAndEndTimeMixin
from tinkerpy.exchange.exchange_factory import all_exchanges
import util.tick_times as tick_times

Expand Down
5 changes: 3 additions & 2 deletions gryphon/dashboards/models/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# -*- coding: utf-8 -*-
from six import text_type
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata

def unicode_string(self):
return unicode(self).encode('utf-8')
return text_type(self).encode('utf-8')

Base.__str__ == unicode_string
Base.__str__ == unicode_string


# How to migrate a database
Expand Down
3 changes: 2 additions & 1 deletion gryphon/dashboards/settings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import logging
import tornado
import tornado.template
import os
from os.path import dirname, abspath
from tornado.options import define, options
from gryphon.lib.logperf import log_request_perf
import uimodules
from . import uimodules

# Make filepaths relative to settings.
path = lambda root,*a: os.path.join(root, *a)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from kraken_orderbook_poller import KrakenOrderbook
from __future__ import absolute_import
from .kraken_orderbook_poller import KrakenOrderbook


class KrakenCADOrderbook(KrakenOrderbook):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from kraken_orderbook_poller import KrakenOrderbook
from __future__ import absolute_import
from .kraken_orderbook_poller import KrakenOrderbook


class KrakenUSDOrderbook(KrakenOrderbook):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def apply_change_to_orderbook(self, change):
# Re-sort the bids.
self.orderbook['bids'] = OrderedDict(sorted(
self.orderbook['bids'].iteritems(),
key=lambda (k, v): float(k),
key=lambda k_v1: float(k_v1[0]),
reverse=True,
))

Expand All @@ -230,7 +230,7 @@ def apply_change_to_orderbook(self, change):

# Re-sort the asks.
self.orderbook['asks'] = OrderedDict(
sorted(self.orderbook['asks'].iteritems(), key=lambda (k, v): float(k)),
sorted(self.orderbook['asks'].iteritems(), key=lambda k_v: float(k_v[0])),
)

def parse_orders(self, orders):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,13 @@ def get_orderbook_to_publish(self):

sorted_bid_keys = sorted(
fancy_orderbook['bids'].keys(),
key=lambda (k): float(k),
key=lambda k: float(k),
reverse=True,
)

sorted_ask_keys = sorted(
fancy_orderbook['asks'].keys(),
key=lambda (k): float(k),
key=lambda k: float(k),
)

bids = [[k, str(fancy_orderbook['bids'][k]), ''] for k in sorted_bid_keys]
Expand Down
5 changes: 3 additions & 2 deletions gryphon/data_service/pollers/trades/trades_poller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import json

from six import text_type
import termcolor as tc
from twisted.internet import defer
from twisted.internet.task import LoopingCall
Expand Down Expand Up @@ -50,9 +51,9 @@ def parse_response(self, resp_obj):
for trade in trades:
if trade['trade_id'] > self.most_recent_trade_id:
trade['price_currency'] = trade['price'].currency
trade['price'] = unicode(trade['price'].amount)
trade['price'] = text_type(trade['price'].amount)
trade['volume_currency'] = trade['volume'].currency
trade['volume'] = unicode(trade['volume'].amount)
trade['volume'] = text_type(trade['volume'].amount)
trade['timestamp'] = int(trade['timestamp'])
trade_string = json.dumps(trade, ensure_ascii=False)
self.producer.publish_message(trade_string)
Expand Down
1 change: 1 addition & 0 deletions gryphon/data_service/scripts/autobahn-tester.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
###############################################################################
##
## Copyright (C) 2011-2013 Tavendo GmbH
Expand Down
3 changes: 2 additions & 1 deletion gryphon/data_service/scripts/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Simple test script for benchmarking regular python logging vs twisted's logging.

import logging
Expand Down Expand Up @@ -25,7 +26,7 @@ def tx_log():


def stop():
print "Log Counter: %s" % log_counter
print("Log Counter: %s" % log_counter)
reactor.stop()


Expand Down
17 changes: 9 additions & 8 deletions gryphon/data_service/scripts/historical_trade_collector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import csv
from datetime import timedelta
import gzip
Expand Down Expand Up @@ -161,7 +162,7 @@ def get_our_recorded_ticker_volume_for_period(exchange, start_date, end_date):
def audit_ticker_volume_individual_days(exchange_list, start_date=test_start_date, end_date=test_end_date):
d = start_date
while d < test_end_date:
print '\n\nAuditing: %s' % d
print('\n\nAuditing: %s' % d)
day_after = d + timedelta(days=1)
audit_all_ticker_volume(exchange_list, d, day_after)
d = day_after
Expand Down Expand Up @@ -191,20 +192,20 @@ def audit_ticker_volume(exchange, start_date, end_date):
end_date,
)

print '%s Our Volume:%s Ticker Volume:%s, Accuracy: %s' % (
print('%s Our Volume:%s Ticker Volume:%s, Accuracy: %s' % (
exchange,
our_volume,
ticker_volume,
our_volume / ticker_volume,
)
))


def audit_bw_volume(exchange, start_date, end_date):
bw_exchange = BitcoinWisdom(exchange=exchange)
bw_volume = bw_exchange.volume_in_period(start_date, end_date)

our_volume = get_our_recorded_exchange_trade_volume_for_period(exchange, start_date, end_date)
print '%s Our Volume:%s BW Volume:%s, Accuracy: %s' % (exchange, our_volume, bw_volume, our_volume / bw_volume)
print('%s Our Volume:%s BW Volume:%s, Accuracy: %s' % (exchange, our_volume, bw_volume, our_volume / bw_volume))


def compare_all_exchanges():
Expand All @@ -222,15 +223,15 @@ def compare_ours_to_history(our_exchange_id, exchange, price_currency, volume_cu
start = parse('2015-11-27 0:0:0').datetime.replace(tzinfo=None)
end = parse('2015-11-27 11:59:59').datetime.replace(tzinfo=None)

print our_exchange_id.upper()
print(our_exchange_id.upper())
hist_in_range = [t for t in hist_trades if t[0] >= start and t[0] <= end]
ours_in_range = [t for t in our_trades if t[0] >= start and t[0] <= end]

for t in hist_in_range:
if t not in ours_in_range:
print 'Hist Trade not in ours: %s' % t
print('Hist Trade not in ours: %s' % t)

for t in ours_in_range:
if t not in hist_in_range:
print'Our trade not in history: %s' % t
print'\n\n\n\n\n'
print('Our trade not in history: %s' % t)
print('\n\n\n\n\n')
1 change: 1 addition & 0 deletions gryphon/execution/controllers/balance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from gryphon.execution.lib.exchange_color import exchange_color
from gryphon.lib.exchange.exchange_factory import *
from gryphon.lib.logger import get_logger
Expand Down
21 changes: 12 additions & 9 deletions gryphon/execution/controllers/create_dashboard_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
Usage: gryphon-exec create-dashboard-user [--execute]
"""
from __future__ import print_function

import getpass
import termcolor as tc
import os

from six.moves import input

from gryphon.lib import session
from gryphon.dashboards.models.user import User
from gryphon.dashboards.models.columns.password_column import Password
Expand Down Expand Up @@ -43,29 +46,29 @@


def main(execute):
print tc.colored(WARNING_MESSAGE, 'red')
informed_consent = raw_input(WARNING_PROMPT)
print(tc.colored(WARNING_MESSAGE, 'red'))
informed_consent = input(WARNING_PROMPT).strip()

if informed_consent != 'y':
print EXIT_MESSAGE
print(EXIT_MESSAGE)
return
else:
print CONTINUE_MESSAGE
print(CONTINUE_MESSAGE)

dashboard_db = session.get_a_dashboard_db_mysql_session()

username = raw_input(ENTER_USERNAME_MESSAGE)
username = input(ENTER_USERNAME_MESSAGE).strip()
password_text = getpass.getpass(ENTER_PASSWORD_MESSAGE)

password = Password(plain=password_text)

user = User(username, password)

if execute is True:
dashboard_db.add(user)
dashboard_db.commit()

print tc.colored(SUCCESS_MESSAGE, 'green')
print(tc.colored(SUCCESS_MESSAGE, 'green'))
else:
print SUCCESS_NO_EXECUTE_MESSAGE
print(SUCCESS_NO_EXECUTE_MESSAGE)

5 changes: 3 additions & 2 deletions gryphon/execution/controllers/fee_buyback.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import prompter
import termcolor as tc

Expand All @@ -14,7 +15,7 @@ def buyback():
prompt_msg = tc.colored('Did you stop the Coinbase Bot before running this?', 'red')
bot_stopped = prompter.yesno(prompt_msg)
if not bot_stopped:
print tc.colored('Go stop the bot first.', 'red')
print(tc.colored('Go stop the bot first.', 'red'))
return

db = session.get_a_trading_db_mysql_session()
Expand All @@ -33,7 +34,7 @@ def buyback():
transactions_buyback_amount = sum([t.fee for t in transactions_with_outstanding_fees])
btc_buyback_amount = trades_buyback_amount + transactions_buyback_amount

print 'Go buy %s on Coinbase (not the exchange)' % btc_buyback_amount
print('Go buy %s on Coinbase (not the exchange)' % btc_buyback_amount)

prompt_msg = 'How much USD did it cost (total including Coinbase Fee): USD'
raw_usd_cost = prompter.prompt(prompt_msg)
Expand Down
11 changes: 6 additions & 5 deletions gryphon/execution/controllers/initialize_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
[comma-separated list of exchange pairs, e.g. 'bitstamp_btc_usd,gemini_btc_usd']
[--execute]
"""
from __future__ import print_function

import pyximport; pyximport.install()

Expand Down Expand Up @@ -53,7 +54,7 @@ def initialize_exchange_ledger(db, wrapper_obj):
pass
finally:
if db_obj is not None:
print ALREADY_INITIALIZED_ERR_MESSAGE % wrapper_obj.name
print(ALREADY_INITIALIZED_ERR_MESSAGE % wrapper_obj.name)
return

# Create the entry in the Exchange table.
Expand All @@ -66,12 +67,12 @@ def initialize_exchange_ledger(db, wrapper_obj):
try:
balance = wrapper_obj.get_balance()
except KeyError as e:
print NO_API_CREDENTIALS_ERR_MESSAGE % wrapper_obj.name
print e
print(NO_API_CREDENTIALS_ERR_MESSAGE % wrapper_obj.name)
print(e)
return
except Exception as e:
print UNKNOWN_ERR_MESSAGE % wrapper_obj.name
print e
print(UNKNOWN_ERR_MESSAGE % wrapper_obj.name)
print(e)
return

price_currency = wrapper_obj.currency
Expand Down
3 changes: 2 additions & 1 deletion gryphon/execution/controllers/order_book.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import termcolor as tc

from gryphon.execution.lib.exchange_color import exchange_color, legend
Expand Down Expand Up @@ -96,7 +97,7 @@ def order_book(exchange_name, include_our_orders=False, include_fees=False):
for order in asks + bids:
order.apply_fee()

print
print()
# if we are showing a consolidated orderbook
if not exchange_name:
print(legend() + "\n")
Expand Down
3 changes: 2 additions & 1 deletion gryphon/execution/lib/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import ConfigParser

from gryphon.lib.logger import get_logger
Expand All @@ -6,7 +7,7 @@


def get_config_var(filepath, section, key):
print filepath
print(filepath)

config = ConfigParser.RawConfigParser()
config.read(filepath)
Expand Down
3 changes: 2 additions & 1 deletion gryphon/execution/live_runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import pyximport; pyximport.install()
from cdecimal import Decimal
import inspect
Expand Down Expand Up @@ -253,7 +254,7 @@ def live_run(configuration):
while True:
try:
tick_start = Delorean().epoch
print '\n\n%s' % strategy.name
print('\n\n%s' % strategy.name)

if warm_shutdown_flag:
return # This takes us into the finally block.
Expand Down
Loading

0 comments on commit f011b01

Please sign in to comment.