-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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
Alternative PR for: Some bytes/str issues in py3 w/ zlib and json #2558
Changes from 7 commits
4124707
0e70ce5
0b450d4
a11bcf6
3bc0b61
27b0f6e
9752509
248cace
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 |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
import sqlalchemy as sa | ||
import signal | ||
import uuid | ||
import sys | ||
import zlib | ||
|
||
from builtins import object | ||
from datetime import date, datetime, time | ||
|
@@ -41,7 +43,7 @@ | |
|
||
logging.getLogger('MARKDOWN').setLevel(logging.INFO) | ||
|
||
|
||
PY3K = sys.version_info >= (3, 0) | ||
EPOCH = datetime(1970, 1, 1) | ||
DTTM_ALIAS = '__timestamp' | ||
|
||
|
@@ -572,3 +574,35 @@ def setup_cache(app, cache_config): | |
"""Setup the flask-cache on a flask app""" | ||
if cache_config and cache_config.get('CACHE_TYPE') != 'null': | ||
return Cache(app, config=cache_config) | ||
|
||
|
||
def zlib_compress(data): | ||
""" | ||
Compress things in a py2/3 safe fashion | ||
>>> json_str = '{"test": 1}' | ||
>>> blob = zlib_compress(json_str) | ||
""" | ||
if PY3K: | ||
if isinstance(data, str): | ||
return zlib.compress(bytes(data, "utf-8")) | ||
else: | ||
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. We may put a single 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. You can drop the else, it'll be handled by the last return |
||
return zlib.compress(data) | ||
return zlib.compress(data) | ||
|
||
|
||
def zlib_decompress_to_string(blob): | ||
""" | ||
Decompress things to a string in a py2/3 safe fashion | ||
>>> json_str = '{"test": 1}' | ||
>>> blob = zlib_compress(json_str) | ||
>>> got_str = zlib_decompress_to_string(blob) | ||
>>> got_str == json_str | ||
True | ||
""" | ||
if PY3K: | ||
if isinstance(blob, bytes): | ||
decompressed = zlib.decompress(blob) | ||
else: | ||
decompressed = zlib.decompress(bytes(blob, "utf-8")) | ||
return decompressed.decode("utf-8") | ||
return zlib.decompress(blob) |
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.
too many blank lines, should be 2 blank lines above a function (PEP8)