Created
February 22, 2015 20:23
-
-
Save devvyn/8ad244de2ec0f43d7e30 to your computer and use it in GitHub Desktop.
code sketch: grab a CSV file with a column called "OTV SKU#" and split the characters in all the values into a character tree
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
from CsvFileModel import CsvFileModel # available from https://gist.github.com/devvyn/759f08e3d83e4cf8f7ef | |
import json | |
import yaml | |
import collections | |
class StringTree(object): | |
def __init__(self): | |
c = CsvFileModel('csp.csv') | |
skus = list(x.strip() for x in c['OTV SKU#']) | |
print("SKU count: %i" % len(skus)) | |
charset = set(''.join(skus)) | |
print("Character set: %s" % charset) | |
d = dict() | |
j = d | |
for sku in skus: | |
for position, char in enumerate(sku): | |
j = j.setdefault(char, dict()) | |
j = d | |
print(d.keys()) | |
print(yaml.dump(d)) | |
self.skus = skus | |
self.data = c | |
self.tree = d | |
self.charset = charset | |
if __name__ == '__main__': | |
d = StringTree() | |
data = d.tree | |
with open('sku_codes.yml', 'w') as outfile: | |
yaml.dump(data, outfile, default_flow_style=False) |
Hard to read. Looks like j
is being reassigned multiple times without read.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is pretty old, but for what it's worth, I detect some code smell. Hard coded file name in the constructor is probably the worst.