-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Move Row to typehints/row.py, add compatibility alias in pvalue.py, update imports, and update CHANGES.md #37071
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
base: master
Are you sure you want to change the base?
Changes from all commits
00bb7b5
97c6d2e
71243c3
e587f05
af3e9b9
b70ddb5
4bdbd0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,74 @@ | ||||||||||
| # | ||||||||||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||||||||||
| # contributor license agreements. See the NOTICE file distributed with | ||||||||||
| # this work for additional information regarding copyright ownership. | ||||||||||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||||||||||
| # (the "License"); you may not use this file except in compliance with | ||||||||||
| # the License. You may obtain a copy of the License at | ||||||||||
| # | ||||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
| # | ||||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
| # See the License for the specific language governing permissions and | ||||||||||
| # limitations under the License. | ||||||||||
| # | ||||||||||
|
|
||||||||||
| from typing import NamedTuple | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class Row(object): | ||||||||||
| """A dynamic schema'd row object. | ||||||||||
| This objects attributes are initialized from the keywords passed into its | ||||||||||
| constructor, e.g. Row(x=3, y=4) will create a Row with two attributes x and y. | ||||||||||
| More importantly, when a Row object is returned from a `Map`, `FlatMap`, or | ||||||||||
| `DoFn` type inference is able to deduce the schema of the resulting | ||||||||||
| PCollection, e.g. | ||||||||||
| pc | beam.Map(lambda x: Row(x=x, y=0.5 * x)) | ||||||||||
| when applied to a PCollection of ints will produce a PCollection with schema | ||||||||||
| `(x=int, y=float)`. | ||||||||||
| Note that in Beam 2.30.0 and later, Row objects are sensitive to field order. | ||||||||||
| So `Row(x=3, y=4)` is not considered equal to `Row(y=4, x=3)`. | ||||||||||
| """ | ||||||||||
| def __init__(self, **kwargs): | ||||||||||
| self.__dict__.update(kwargs) | ||||||||||
|
|
||||||||||
| def as_dict(self): | ||||||||||
| return dict(self.__dict__) | ||||||||||
|
|
||||||||||
| # For compatibility with named tuples. | ||||||||||
| _asdict = as_dict | ||||||||||
|
|
||||||||||
| def __iter__(self): | ||||||||||
| for _, value in self.__dict__.items(): | ||||||||||
| yield value | ||||||||||
|
|
||||||||||
| def __repr__(self): | ||||||||||
| return 'Row(%s)' % ', '.join('%s=%r' % kv for kv in self.__dict__.items()) | ||||||||||
|
|
||||||||||
| def __hash__(self): | ||||||||||
| return hash(self.__dict__.items()) | ||||||||||
|
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. The
Suggested change
|
||||||||||
|
|
||||||||||
| def __eq__(self, other): | ||||||||||
| if type(self) == type(other): | ||||||||||
| other_dict = other.__dict__ | ||||||||||
| elif type(other) == type(NamedTuple): | ||||||||||
| other_dict = other._asdict() | ||||||||||
|
Comment on lines
+61
to
+62
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. The check
Suggested change
|
||||||||||
| else: | ||||||||||
| return False | ||||||||||
| return ( | ||||||||||
| len(self.__dict__) == len(other_dict) and | ||||||||||
| all(s == o for s, o in zip(self.__dict__.items(), other_dict.items()))) | ||||||||||
|
|
||||||||||
| def __reduce__(self): | ||||||||||
| return _make_Row, tuple(self.__dict__.items()) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _make_Row(*items): | ||||||||||
| return Row(**dict(items)) | ||||||||||
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.
These extra blank lines can be removed for better readability and to keep the changelog concise.