Created
March 9, 2012 23:24
-
-
Save rafaelcaricio/2009266 to your computer and use it in GitHub Desktop.
test_hash.py
This file contains hidden or 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
*.swp | |
*.pyc | |
*.pyo |
This file contains hidden or 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import json | |
cars = json.loads(open('./fixtures/fipe_carro.json').read()) | |
tree = {} | |
for v in cars: | |
for name in v['translated_names']: | |
for i in xrange(1, len(name) + 1): | |
tree_node = tree.get(name[:i], []) | |
tree_node.append(v) | |
tree[name[:i]] = tree_node |
This file contains hidden or 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import json | |
cars = json.loads(open('../api/fixtures/fipe_carro.json').read()) | |
class Tree(dict): | |
def __init__(self, *args, **kwargs): | |
self.current_values = [] | |
def __getitem__(self, key): | |
node = super(Tree, self).__getitem__(key[0]) | |
if len(key[1:]) > 0: | |
return node[key[1:]] | |
return node.current_values | |
def push(self, key, obj): | |
node = self.get(key[0], Tree()) | |
node.current_values.append(obj) | |
if len(key[1:]) > 0: | |
node.push(key[1:], obj) | |
self[key[0]] = node | |
@classmethod | |
def from_list(cls, objects, get_index): | |
new_tree = cls() | |
for obj in objects: | |
key = get_index(obj) | |
if hasattr(key, 'next'): | |
for k in key: | |
new_tree.push(k, obj) | |
else: | |
new_tree.push(key, obj) | |
return new_tree | |
tree = Tree.from_list(cars, lambda car: (name for name in car['translated_names'])) | |
print tree['coupe-4'] |
This file contains hidden or 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import json | |
class Tree(dict): | |
def __setitem__(self, key, obj): | |
""" | |
Pushes a new object in the tree. | |
""" | |
for i in xrange(3, len(key) + 1): | |
tree_node = self.get(key[:i], []) | |
super(Tree, self).__setitem__(key[:i], tree_node) | |
tree_node.append(obj) | |
@classmethod | |
def from_list(cls, objects, get_key): | |
""" | |
Build tree form a list of objects. | |
""" | |
tree = cls() | |
for obj in objects: | |
key_value = get_key(obj) | |
if isinstance(key_value, list): | |
for key in key_value: | |
tree[key] = obj | |
else: | |
tree[key_value] = obj | |
return tree | |
cars = json.loads(open('../api/fixtures/fipe_carro.json').read()) | |
tree = Tree.from_list(cars, lambda car: car['translated_names']) | |
print tree['attracti.'] |
This file contains hidden or 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
from api.nary import Tree | |
import json | |
cars = json.loads(open('./fixtures/fipe_carro.json').read()) | |
tree = Tree.from_list(cars, lambda vehicle: vehicle['translated_names']) |
Criado. Já te coloquei como colaborador.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Massa, cria ai!
Abs.