Skip to content

Commit 24ff777

Browse files
committed
initial commit
1 parent c706ea1 commit 24ff777

File tree

364 files changed

+25786
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

364 files changed

+25786
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# example.py
2+
#
3+
# Example of calculating with dictionaries
4+
5+
prices = {
6+
'ACME': 45.23,
7+
'AAPL': 612.78,
8+
'IBM': 205.55,
9+
'HPQ': 37.20,
10+
'FB': 10.75
11+
}
12+
13+
# Find min and max price
14+
min_price = min(zip(prices.values(), prices.keys()))
15+
max_price = max(zip(prices.values(), prices.keys()))
16+
17+
print('min price:', min_price)
18+
print('max price:', max_price)
19+
20+
print('sorted prices:')
21+
prices_sorted = sorted(zip(prices.values(), prices.keys()))
22+
for price, name in prices_sorted:
23+
print(' ', name, price)
24+
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# example.py
2+
#
3+
# Determine the most common words in a list
4+
5+
words = [
6+
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
7+
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
8+
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
9+
'my', 'eyes', "you're", 'under'
10+
]
11+
12+
from collections import Counter
13+
word_counts = Counter(words)
14+
top_three = word_counts.most_common(3)
15+
print(top_three)
16+
# outputs [('eyes', 8), ('the', 5), ('look', 4)]
17+
18+
# Example of merging in more words
19+
20+
morewords = ['why','are','you','not','looking','in','my','eyes']
21+
word_counts.update(morewords)
22+
print(word_counts.most_common(3))
23+
24+
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# example of extracting a subset from a dictionary
2+
from pprint import pprint
3+
4+
prices = {
5+
'ACME': 45.23,
6+
'AAPL': 612.78,
7+
'IBM': 205.55,
8+
'HPQ': 37.20,
9+
'FB': 10.75
10+
}
11+
12+
# Make a dictionary of all prices over 200
13+
p1 = { key:value for key, value in prices.items() if value > 200 }
14+
15+
print("All prices over 200")
16+
pprint(p1)
17+
18+
# Make a dictionary of tech stocks
19+
tech_names = { 'AAPL', 'IBM', 'HPQ', 'MSFT' }
20+
p2 = { key:value for key,value in prices.items() if key in tech_names }
21+
22+
print("All techs")
23+
pprint(p2)
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Examples of different ways to filter data
2+
3+
mylist = [1, 4, -5, 10, -7, 2, 3, -1]
4+
5+
# All positive values
6+
pos = [n for n in mylist if n > 0]
7+
print(pos)
8+
9+
# All negative values
10+
neg = [n for n in mylist if n < 0]
11+
print(neg)
12+
13+
# Negative values clipped to 0
14+
neg_clip = [n if n > 0 else 0 for n in mylist]
15+
print(neg_clip)
16+
17+
# Positive values clipped to 0
18+
pos_clip = [n if n < 0 else 0 for n in mylist]
19+
print(pos_clip)
20+
21+
# Compressing example
22+
23+
addresses = [
24+
'5412 N CLARK',
25+
'5148 N CLARK',
26+
'5800 E 58TH',
27+
'2122 N CLARK',
28+
'5645 N RAVENSWOOD',
29+
'1060 W ADDISON',
30+
'4801 N BROADWAY',
31+
'1039 W GRANVILLE',
32+
]
33+
34+
counts = [ 0, 3, 10, 4, 1, 7, 6, 1]
35+
36+
from itertools import compress
37+
38+
more5 = [ n > 5 for n in counts ]
39+
a = list(compress(addresses, more5))
40+
print(a)
41+
42+
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# example.py
2+
#
3+
# Find out what two dictionaries have in common
4+
5+
a = {
6+
'x' : 1,
7+
'y' : 2,
8+
'z' : 3
9+
}
10+
11+
b = {
12+
'w' : 10,
13+
'x' : 11,
14+
'y' : 2
15+
}
16+
17+
print('Common keys:', a.keys() & b.keys())
18+
print('Keys in a not in b:', a.keys() - b.keys())
19+
print('(key,value) pairs in common:', a.items() & b.items())
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# example.py
2+
#
3+
# Example of using heapq to find the N smallest or largest items
4+
5+
import heapq
6+
7+
portfolio = [
8+
{'name': 'IBM', 'shares': 100, 'price': 91.1},
9+
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
10+
{'name': 'FB', 'shares': 200, 'price': 21.09},
11+
{'name': 'HPQ', 'shares': 35, 'price': 31.75},
12+
{'name': 'YHOO', 'shares': 45, 'price': 16.35},
13+
{'name': 'ACME', 'shares': 75, 'price': 115.65}
14+
]
15+
16+
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
17+
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
18+
19+
print(cheap)
20+
print(expensive)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
rows = [
2+
{'address': '5412 N CLARK', 'date': '07/01/2012'},
3+
{'address': '5148 N CLARK', 'date': '07/04/2012'},
4+
{'address': '5800 E 58TH', 'date': '07/02/2012'},
5+
{'address': '2122 N CLARK', 'date': '07/03/2012'},
6+
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
7+
{'address': '1060 W ADDISON', 'date': '07/02/2012'},
8+
{'address': '4801 N BROADWAY', 'date': '07/01/2012'},
9+
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
10+
]
11+
12+
from itertools import groupby
13+
14+
rows.sort(key=lambda r: r['date'])
15+
for date, items in groupby(rows, key=lambda r: r['date']):
16+
print(date)
17+
for i in items:
18+
print(' ', i)
19+
20+
# Example of building a multidict
21+
from collections import defaultdict
22+
rows_by_date = defaultdict(list)
23+
for row in rows:
24+
rows_by_date[row['date']].append(row)
25+
26+
for r in rows_by_date['07/01/2012']:
27+
print(r)
28+
29+
30+
31+
32+
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# example.py
2+
#
3+
# Example of a priority queue
4+
5+
import heapq
6+
7+
class PriorityQueue:
8+
def __init__(self):
9+
self._queue = []
10+
self._index = 0
11+
12+
def push(self, item, priority):
13+
heapq.heappush(self._queue, (-priority, self._index, item))
14+
self._index += 1
15+
16+
def pop(self):
17+
return heapq.heappop(self._queue)[-1]
18+
19+
# Example use
20+
class Item:
21+
def __init__(self, name):
22+
self.name = name
23+
def __repr__(self):
24+
return 'Item({!r})'.format(self.name)
25+
26+
q = PriorityQueue()
27+
q.push(Item('foo'), 1)
28+
q.push(Item('bar'), 5)
29+
q.push(Item('spam'), 4)
30+
q.push(Item('grok'), 1)
31+
32+
print("Should be bar:", q.pop())
33+
print("Should be spam:", q.pop())
34+
print("Should be foo:", q.pop())
35+
print("Should be grok:", q.pop())
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from collections import deque
2+
3+
def search(lines, pattern, history=5):
4+
previous_lines = deque(maxlen=history)
5+
for line in lines:
6+
if pattern in line:
7+
yield line, previous_lines
8+
previous_lines.append(line)
9+
10+
# Example use on a file
11+
if __name__ == '__main__':
12+
with open('somefile.txt') as f:
13+
for line, prevlines in search(f, 'python', 5):
14+
for pline in prevlines:
15+
print(pline, end='')
16+
print(line, end='')
17+
print('-'*20)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
=== Keeping the Last N Items
2+
3+
==== Problem
4+
5+
You want to keep a limited history of the last few items seen
6+
during iteration or during some other kind of processing.
7+
8+
==== Solution
9+
10+
Keeping a limited history is a perfect use for a `collections.deque`.
11+
For example, the following code performs a simple text match on a
12+
sequence of lines and prints the matching line along with the previous
13+
N lines of context when found:
14+
15+
[source,python]
16+
----
17+
from collections import deque
18+
19+
def search(lines, pattern, history=5):
20+
previous_lines = deque(maxlen=history)
21+
for line in lines:
22+
if pattern in line:
23+
for pline in previous_lines:
24+
print(lline, end='')
25+
print(line, end='')
26+
print()
27+
previous_lines.append(line)
28+
29+
# Example use on a file
30+
if __name__ == '__main__':
31+
with open('somefile.txt') as f:
32+
search(f, 'python', 5)
33+
----
34+
35+
==== Discussion
36+
37+
Using `deque(maxlen=N)` creates a fixed size queue. When new items
38+
are added and the queue is full, the oldest item is automatically
39+
removed. For example:
40+
41+
[source,pycon]
42+
----
43+
>>> q = deque(maxlen=3)
44+
>>> q.append(1)
45+
>>> q.append(2)
46+
>>> q.append(3)
47+
>>> q
48+
deque([1, 2, 3], maxlen=3)
49+
>>> q.append(4)
50+
>>> q
51+
deque([2, 3, 4], maxlen=3)
52+
>>> q.append(5)
53+
>>> q
54+
deque([3, 4, 5], maxlen=3)
55+
----
56+
57+
Although you could manually perform such operations on a list (e.g.,
58+
appending, deleting, etc.), the queue solution is far more elegant and
59+
runs a lot faster.
60+
61+
More generally, a `deque` can be used whenever you need a simple queue
62+
structure. If you don't give it a maximum size, you get an unbounded
63+
queue that lets you append and pop items on either end. For example:
64+
65+
[source,pycon]
66+
----
67+
>>> q = deque()
68+
>>> q.append(1)
69+
>>> q.append(2)
70+
>>> q.append(3)
71+
>>> q
72+
deque([1, 2, 3])
73+
>>> q.appendleft(4)
74+
>>> q
75+
deque([4, 1, 2, 3])
76+
>>> q.pop()
77+
3
78+
>>> q
79+
deque([4, 1, 2])
80+
>>> q.popleft()
81+
4
82+
----
83+
84+
Adding or popping items from either end of a queue has O(1)
85+
complexity. This is unlike a list where inserting or removing
86+
items from the front of the list is O(N).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# example.py
2+
3+
from collections import namedtuple
4+
5+
Stock = namedtuple('Stock', ['name', 'shares', 'price'])
6+
7+
def compute_cost(records):
8+
total = 0.0
9+
for rec in records:
10+
s = Stock(*rec)
11+
total += s.shares * s.price
12+
return total
13+
14+
# Some Data
15+
records = [
16+
('GOOG', 100, 490.1),
17+
('ACME', 100, 123.45),
18+
('IBM', 50, 91.15)
19+
]
20+
21+
print(compute_cost(records))
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# example.py
2+
#
3+
# Remove duplicate entries from a sequence while keeping order
4+
5+
def dedupe(items):
6+
seen = set()
7+
for item in items:
8+
if item not in seen:
9+
yield item
10+
seen.add(item)
11+
12+
if __name__ == '__main__':
13+
a = [1, 5, 2, 1, 9, 1, 5, 10]
14+
print(a)
15+
print(list(dedupe(a)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# example2.py
2+
#
3+
# Remove duplicate entries from a sequence while keeping order
4+
5+
def dedupe(items, key=None):
6+
seen = set()
7+
for item in items:
8+
val = item if key is None else key(item)
9+
if val not in seen:
10+
yield item
11+
seen.add(val)
12+
13+
if __name__ == '__main__':
14+
a = [
15+
{'x': 2, 'y': 3},
16+
{'x': 1, 'y': 4},
17+
{'x': 2, 'y': 3},
18+
{'x': 2, 'y': 3},
19+
{'x': 10, 'y': 15}
20+
]
21+
print(a)
22+
print(list(dedupe(a, key=lambda a: (a['x'],a['y']))))
23+

0 commit comments

Comments
 (0)