Skip to content

Instantly share code, notes, and snippets.

@devvyn
Created February 22, 2015 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devvyn/8ad244de2ec0f43d7e30 to your computer and use it in GitHub Desktop.
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
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)
@devvyn
Copy link
Author

devvyn commented Sep 9, 2017

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.

@devvyn
Copy link
Author

devvyn commented Oct 22, 2019

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