Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

Add support for Python3 #148

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions streaming/language_support/python/pymongo_hadoop/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import sys

from input import BSONInput, KeyValueBSONInput
from output import BSONOutput, KeyValueBSONOutput
from reducer import BSONReducer, BSONReducerInput
from reducer import KeyValueBSONReducer, KeyValueBSONReducerInput
from mapper import BSONMapper, KeyValueBSONMapper
from .input import BSONInput, KeyValueBSONInput
from .output import BSONOutput, KeyValueBSONOutput
from .reducer import BSONReducer, BSONReducerInput
from .reducer import KeyValueBSONReducer, KeyValueBSONReducerInput
from .mapper import BSONMapper, KeyValueBSONMapper

__all__ = ['BSONInput', 'BSONOutput',
'KeyValueBSONOutput', 'KeyValueBSONInput',
Expand All @@ -13,5 +13,5 @@

def dump_bits(bits):
for bit in bits:
print >> sys.stderr, "\t * Bit: %s Ord: %d" % (hex(ord(bit)), ord(bit))
print("\t * Bit: %s Ord: %d" % (hex(ord(bit)), ord(bit)), file=sys.stderr)

16 changes: 8 additions & 8 deletions streaming/language_support/python/pymongo_hadoop/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BSONInput(object):
https://github.com/klbostee/typedbytes
"""

def __init__(self, fh=sys.stdin, unicode_errors='strict'):
def __init__(self, fh=sys.stdin.buffer, unicode_errors='strict'):
self.fh = fh
self.unicode_errors = unicode_errors
self.eof = False
Expand All @@ -26,20 +26,20 @@ def _read(self):
data = size_bits + self.fh.read(size)
if len(data) != size + 4:
raise struct.error("Unable to cleanly read expected BSON Chunk; EOF, underful buffer or invalid object size.")
if data[size + 4 - 1] != "\x00":
if data[size + 4 - 1] != 0:
raise InvalidBSON("Bad EOO in BSON Data")
doc = BSON(data).decode(codec_options=STREAMING_CODEC_OPTIONS)
return doc
except struct.error, e:
except struct.error as e:
#print >> sys.stderr, "Parsing Length record failed: %s" % e
self.eof = True
raise StopIteration(e)

def read(self):
try:
return self._read()
except StopIteration, e:
print >> sys.stderr, "Iteration Failure: %s" % e
except StopIteration as e:
print("Iteration Failure: %s" % e, file=sys.stderr)
return None

def _reads(self):
Expand All @@ -56,8 +56,8 @@ class KeyValueBSONInput(BSONInput):
def read(self):
try:
doc = self._read()
except StopIteration, e:
print >> sys.stderr, "Key/Value Input iteration failed/stopped: %s" % e
except StopIteration as e:
print("Key/Value Input iteration failed/stopped: %s" % e, file=sys.stderr)
return None
if '_id' in doc:
return doc['_id'], doc
Expand All @@ -66,7 +66,7 @@ def read(self):

def reads(self):
it = self._reads()
n = it.next
n = it.__next__
while 1:
doc = n()
if '_id' in doc:
Expand Down
4 changes: 2 additions & 2 deletions streaming/language_support/python/pymongo_hadoop/mapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from input import BSONInput, KeyValueBSONInput
from output import BSONOutput, KeyValueBSONOutput
from .input import BSONInput, KeyValueBSONInput
from .output import BSONOutput, KeyValueBSONOutput

class BSONMapper(object):
"""Wraps BSONInput to allow writing mapper functions
Expand Down
2 changes: 1 addition & 1 deletion streaming/language_support/python/pymongo_hadoop/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BSONOutput(object):
https://github.com/klbostee/typedbytes
"""

def __init__(self, fh=sys.stdout, unicode_errors='strict'):
def __init__(self, fh=sys.stdout.buffer, unicode_errors='strict'):
self.fh = fh
self.unicode_errors = unicode_errors

Expand Down
6 changes: 3 additions & 3 deletions streaming/language_support/python/pymongo_hadoop/reducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
https://github.com/klbostee/dumbo
"""
from itertools import groupby
from input import BSONInput, KeyValueBSONInput
from output import BSONOutput, KeyValueBSONOutput
from .input import BSONInput, KeyValueBSONInput
from .output import BSONOutput, KeyValueBSONOutput

import sys

Expand Down Expand Up @@ -55,7 +55,7 @@ def __init__(self, factory=None, input_fh=None):


def default_reducer(data):
print >> sys.stderr, "*** Invoking default reducer function, this is unoptimized for your data and may be very slow."
print("*** Invoking default reducer function, this is unoptimized for your data and may be very slow.", file=sys.stderr)

class BSONReducerInput(BSONInput):
"""Wrapper to 'roll up' the reduce data down to just
Expand Down