Created
August 30, 2013 02:20
-
-
Save colincoghill/6385657 to your computer and use it in GitHub Desktop.
This file contains 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
# JSON serialize an object, returns a generator that will yield in approximate | |
# sized chunks. | |
import json | |
def chunk_up(gen, csize): | |
""" Gather the string output from a generator and yield it in | |
chunks of approximately "csize" bytes. | |
""" | |
out = "" | |
for chunk in gen: | |
out += chunk | |
if len(out) > csize: | |
yield out | |
out = "" | |
yield out | |
# Fetch the data structure | |
# big = json.load(open("input.json")) | |
gen = json.JSONEncoder().iterencode(big) | |
for chunk in chunk_up(gen, 10240): # 10k chunks | |
print len(chunk) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment