Repository navigation
Expand file tree
/
Copy pathmodern_dict1.py
More file actions
94 lines (71 loc) · 2.69 KB
/
Copy pathmodern_dict1.py
File metadata and controls
94 lines (71 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 8 21:34:28 2020
@author: z093920
"""
#http://web.archive.org/web/20161209120412/https://dl.dropboxusercontent.com/u/3967849/sfmu2/_build/html/goal.html
#Our Journey: The Beginning and the End
#Python is built around dictionaries. The various namespaces include globals, locals, module dictionaries, class dictionaries, instance dictionaries.
#
#Of these, instance dictionaries are among the most prolific.
#
#Instance Dictionaries
#Create a class to track user assignments with in a property category.
#from __future__ import division, print_function
#import sys
#
#class UserProperty:
# def __init__(self, v0, v1, v2, v3, v4):
# self.guido = v0
# self.sarah = v1
# self.barry = v2
# self.rachel = v3
# self.tim = v4
#
# def __repr__(self):
# return 'UserProperty(%r, %r, %r, %r, %r)' \
# % (self.guido, self.sarah, self.barry, self.rachel, self.tim)
#
#colors = UserProperty('blue', 'orange', 'green', 'yellow', 'red')
#cities = UserProperty('austin', 'dallas', 'tuscon', 'reno', 'portland')
#fruits = UserProperty('apple', 'banana', 'orange', 'pear', 'peach')
#
#for user in [colors, cities, fruits]:
# print(dir(user))
#
#print(list(map(sys.getsizeof, map(vars, [colors, cities, fruits]))))
#Evolution: A Half Dozen Good Ideas
#In the beginning, there were databases.
#
#Now, we have come full circle. With all our progress on dictionaries, we’ve reinvented what was done with databases long ago.
#
#Setup
#Here is our sample data to store in our dictionaries.
from __future__ import division, print_function
from pprint import pprint
keys = 'guido sarah barry rachel tim'.split()
values1 = 'blue orange green yellow red'.split()
values2 = 'austin dallas tuscon reno portland'.split()
values3 = 'apple banana orange pear peach'.split()
hashes = list(map(abs, map(hash, keys)))
entries = list(zip(hashes, keys, values1))
comb_entries = list(zip(hashes, keys, values1, values2, values3))
#How a Database Would Do It
#The data is dense (no holes or over-allocations). And without an index, the search is linear.
def database_linear_search():
pprint(list(zip(keys, values1, values2, values3)))
# Array of Tuples
#[('guido', 'blue', 'austin', 'apple'),
# ('sarah', 'orange', 'dallas', 'banana'),
# ('barry', 'green', 'tuscon', 'orange'),
# ('rachel', 'yellow', 'reno', 'pear'),
# ('tim', 'red', 'portland', 'peach')]
#
#How LISP Would Do It
#Store lists of pairs.
#def association_lists():
# pprint([
# list(zip(keys, values1)),
# list(zip(keys, values2)),
# list(zip(keys, values3)),
# ])