Skip to content

Commit

Permalink
Make code more compatible with Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Endler committed Jul 13, 2015
1 parent 840bd0e commit 91b5c52
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions kafka_influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from reader import kafka_reader
from writer import influxdb_writer
from writer import kafka_sample_writer as benchmark
import six

import traceback
import time
Expand Down Expand Up @@ -45,7 +46,7 @@ def init_database(self):
try:
logging.info("Creating InfluxDB database if not exists: %s", self.config.influxdb_dbname)
self.writer.create_database(self.config.influxdb_dbname)
except Exception, e:
except Exception as e:
logging.info(e)

def flush(self):
Expand All @@ -54,14 +55,14 @@ def flush(self):
self.writer.write(self.buffer)
if self.config.statistics:
self.show_statistics()
except Exception, e:
except Exception as e:
logging.warning(e)
self.buffer = []

def show_statistics(self):
delta = time.time() - self.start_time
msg_per_sec = self.config.buffer_size / delta
print "Flushing output buffer. {0:.2f} messages/s".format(msg_per_sec)
print("Flushing output buffer. {0:.2f} messages/s".format(msg_per_sec))
# Reset timer
self.start_time = time.time()

Expand Down Expand Up @@ -118,7 +119,7 @@ def start_consumer(config):
config.kafka_port,
config.kafka_group,
config.kafka_topic)
except Exception, e:
except Exception as e:
logging.error("The connection to Kafka can not be established.")
sys.exit(-1)

Expand Down Expand Up @@ -151,18 +152,18 @@ def parse_configfile(configfile):
with open(configfile) as f:
try:
return yaml.safe_load(f)
except Exception, e :
except Exception as e :
logging.fatal("Could not load default config file: ", e)
exit(-1)

def overwrite_config_values(config, values, prefix = ""):
"""
Overwrite default config with custom values
"""
for key, value in values.iteritems() :
for key, value in six.iteritems(values):
if type(value) == type(dict()):
overwrite_config_values(config, value, "%s_" % key)
elif value != u'':
elif value != '':
setattr(config, "%s%s" % (prefix, key), value)

def parse_args():
Expand Down
2 changes: 1 addition & 1 deletion writer/influxdb_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def write(self, msg, params=None, expected_response_code=204):
expected_response_code=expected_response_code,
headers=self.headers
)
except Exception, e:
except Exception as e:
logging.warning("Cannot write data points: %s", e)
return False
return True
Expand Down

0 comments on commit 91b5c52

Please sign in to comment.