-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[BEAM-3141][WIP] Support the coders in Python 3 #4078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
a9ce8d6
Fix unicode imports and NoneType
holdenk 72106c7
Swap the avroio over to BytesIO
holdenk 7379b09
Swithc apiclinet to io StringIO from StringIO StringIO
holdenk 7b1d7c6
Switch required packages based on Python version (currently only impa…
holdenk 10f83d7
Rewrite lambda with expansion
holdenk 85930de
Fix items and builtin import and a long import
holdenk 64b0c8f
Switch filesystem to io.StringIO
holdenk 7d1bc5c
Fix cirular dep introduced through Py3 import changes for CreatePTran…
holdenk 86a1b2c
Change slow_stream to bytes
holdenk d7de784
Handle slow_stream in py2 again
holdenk 34ab42c
Selective fixes while running through apache_beam.examples.complete.g…
holdenk bec24c9
metaclass rewrite rule
holdenk 39de5e2
Add __hash__ methods in places where needed (in progress)
holdenk b3fef82
Provide a base ne using eq for windows
holdenk f4e8d61
Provide eq/hashing/sorting for TimestampedValue
holdenk ab2094b
Rich comparisions requirement
holdenk 90b8fdb
If we are passed in bytes for the lookup in pipeline_context do the l…
holdenk c4a87c7
queue and raise rewrites
holdenk 9807def
futurize -wn -f libfuturize.fixes.fix_future_standard_library_urllib …
holdenk e65cd92
Finish up getting apache_beam.examples.complete.game.game_stats_test.…
holdenk 6672477
Fix the write_byte on slow stream to just use bytes rather than encode
holdenk f1bd121
Fix some bytes coders tests to use bytes in py3
holdenk e8b3b74
Change the fall through for encoding a bit and update past imports
holdenk 27e91ed
Only wrap in bytes for py3
holdenk cf54dfd
Fix remaining coder issues accross py2/3
holdenk 1e4fbd4
Style fixes, remove uneeded imports after other unrelated things fixe…
holdenk 1bb0328
Fix instance check
holdenk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,9 @@ | |
| """ | ||
| from __future__ import absolute_import | ||
|
|
||
| from types import NoneType | ||
| from past.builtins import basestring | ||
| from past.builtins import long | ||
| from past.builtins import unicode | ||
|
|
||
| from apache_beam.coders import observable | ||
| from apache_beam.utils import windowed_value | ||
|
|
@@ -184,7 +186,7 @@ def __init__(self, coder, step_label): | |
| self._step_label = step_label | ||
|
|
||
| def _check_safe(self, value): | ||
| if isinstance(value, (str, unicode, long, int, float)): | ||
| if isinstance(value, (str, basestring, bytes, long, int, float)): | ||
| pass | ||
| elif value is None: | ||
| pass | ||
|
|
@@ -264,18 +266,18 @@ def get_estimated_size_and_observables(self, value, nested=False): | |
|
|
||
| def encode_to_stream(self, value, stream, nested): | ||
| t = type(value) | ||
| if t is NoneType: | ||
| if value is None: | ||
| stream.write_byte(NONE_TYPE) | ||
| elif t is int: | ||
| stream.write_byte(INT_TYPE) | ||
| stream.write_var_int64(value) | ||
| elif t is float: | ||
| stream.write_byte(FLOAT_TYPE) | ||
| stream.write_bigendian_double(value) | ||
| elif t is str: | ||
| elif t is bytes: | ||
| stream.write_byte(STR_TYPE) | ||
| stream.write(value, nested) | ||
| elif t is unicode: | ||
| elif t is str or t is basestring or t is unicode: | ||
| unicode_value = value # for typing | ||
| stream.write_byte(UNICODE_TYPE) | ||
| stream.write(unicode_value.encode('utf-8'), nested) | ||
|
|
@@ -289,7 +291,7 @@ def encode_to_stream(self, value, stream, nested): | |
| dict_value = value # for typing | ||
| stream.write_byte(DICT_TYPE) | ||
| stream.write_var_int64(len(dict_value)) | ||
| for k, v in dict_value.iteritems(): | ||
| for k, v in dict_value.items(): | ||
| self.encode_to_stream(k, stream, True) | ||
| self.encode_to_stream(v, stream, True) | ||
| elif t is bool: | ||
|
|
@@ -315,7 +317,7 @@ def decode_from_stream(self, stream, nested): | |
| vlen = stream.read_var_int64() | ||
| vlist = [self.decode_from_stream(stream, True) for _ in range(vlen)] | ||
| if t == LIST_TYPE: | ||
| return vlist | ||
| return list(vlist) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So explicitly converts the generator into a list for Py3. |
||
| elif t == TUPLE_TYPE: | ||
| return tuple(vlist) | ||
| return set(vlist) | ||
|
|
@@ -344,6 +346,8 @@ def decode_from_stream(self, in_stream, nested): | |
| return in_stream.read_all(nested) | ||
|
|
||
| def encode(self, value): | ||
| if (not isinstance(value, bytes)) and isinstance(value, str): | ||
| return value.encode("LATIN-1") | ||
| assert isinstance(value, bytes), (value, type(value)) | ||
| return value | ||
|
|
||
|
|
@@ -379,8 +383,8 @@ def _from_normal_time(self, value): | |
|
|
||
| def encode_to_stream(self, value, out, nested): | ||
| span_micros = value.end.micros - value.start.micros | ||
| out.write_bigendian_uint64(self._from_normal_time(value.end.micros / 1000)) | ||
| out.write_var_int64(span_micros / 1000) | ||
| out.write_bigendian_uint64(self._from_normal_time(value.end.micros // 1000)) | ||
| out.write_var_int64(span_micros // 1000) | ||
|
|
||
| def decode_from_stream(self, in_, nested): | ||
| end_millis = self._to_normal_time(in_.read_bigendian_uint64()) | ||
|
|
@@ -394,7 +398,7 @@ def estimate_size(self, value, nested=False): | |
| # An IntervalWindow is context-insensitive, with a timestamp (8 bytes) | ||
| # and a varint timespam. | ||
| span = value.end.micros - value.start.micros | ||
| return 8 + get_varint_size(span / 1000) | ||
| return 8 + get_varint_size(span // 1000) | ||
|
|
||
|
|
||
| class TimestampCoderImpl(StreamCoderImpl): | ||
|
|
@@ -691,7 +695,7 @@ def encode_to_stream(self, value, out, nested): | |
| # TODO(BEAM-1524): Clean this up once we have a BEAM wide consensus on | ||
| # precision of timestamps. | ||
| self._from_normal_time( | ||
| restore_sign * (abs(wv.timestamp_micros) / 1000))) | ||
| restore_sign * (abs(wv.timestamp_micros) // 1000))) | ||
| self._windows_coder.encode_to_stream(wv.windows, out, True) | ||
| # Default PaneInfo encoded byte representing NO_FIRING. | ||
| # TODO(BEAM-1522): Remove the hard coding here once PaneInfo is supported. | ||
|
|
@@ -706,9 +710,9 @@ def decode_from_stream(self, in_stream, nested): | |
| # were indeed MIN/MAX timestamps. | ||
| # TODO(BEAM-1524): Clean this up once we have a BEAM wide consensus on | ||
| # precision of timestamps. | ||
| if timestamp == -(abs(MIN_TIMESTAMP.micros) / 1000): | ||
| if timestamp == -(abs(MIN_TIMESTAMP.micros) // 1000): | ||
| timestamp = MIN_TIMESTAMP.micros | ||
| elif timestamp == (MAX_TIMESTAMP.micros / 1000): | ||
| elif timestamp == (MAX_TIMESTAMP.micros // 1000): | ||
| timestamp = MAX_TIMESTAMP.micros | ||
| else: | ||
| timestamp *= 1000 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is bytes available in Python 2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, it's the same as string in Python 2 :)