Skip to content

Instantly share code, notes, and snippets.

@Arcensoth
Created September 14, 2018 04:38
Show Gist options
  • Save Arcensoth/1d728c02a21671900fdcf1d20f7c2615 to your computer and use it in GitHub Desktop.
Save Arcensoth/1d728c02a21671900fdcf1d20f7c2615 to your computer and use it in GitHub Desktop.
import json
import math
import sys
import os
infile = os.path.abspath(sys.argv[1])
outdir = os.path.abspath(sys.argv[2] if len(sys.argv) > 2 else '.')
factor = int(sys.argv[3]) if len(sys.argv) > 3 else 2
print('Reading ids from file:', infile)
data = json.load(open(infile))
ids = list(data.keys())
nids = len(ids)
nlayers = math.ceil(math.log(nids, factor))
print(f'Processing {len(ids)} ids, across {nlayers} layers with {factor} ids per layer...')
tags = [{'values': [item for sublist in [ids[sliceidx : sliceidx + factor ** layeridx] for sliceidx in range(0, nids, factor ** (layeridx + 1))] for item in sublist]} for layeridx in range(0, nlayers)]
print(f'Writing {len(tags)} tags to directory:', outdir)
for i, tag in enumerate(tags):
json.dump(tag, open(os.path.join(outdir, str(i) + '.json'), 'w'), indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment