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
""" | |
Example collection data: | |
{ | |
_id: “9991f84c4b0a110af3830012”, | |
name: “John Smith”, | |
brands: [“Nike”, “Asics”, “Adidas”] | |
} | |
{ | |
_id: “9991f84c4b0a110af3830012”, |
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
""" | |
The built-in function iter() can be called with two arguments | |
where the first argument must be a callable object (function) | |
and second is the sentinel. The iterator calls this function | |
until the returned value is equal to the sentinel. | |
""" | |
inf = iter(int,1) |
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
>>> list1, list2, list3 = [1,2,3], ['a','b','c'], [7,8,9] | |
>>> all_lists = sum([list1, list2, list3], []) | |
>>> all_lists | |
[1, 2, 3, 'a', 'b', 'c', 7, 8, 9] |
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
""" | |
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned. | |
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned. | |
""" | |
In [3]: True and ['a'] | |
Out[3]: ['a'] | |
In [4]: False and ['a'] |
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
def find_defining_class(obj,meth_name): | |
for ty in type(obj).mro(): | |
if meth_name in ty.__dict__: | |
return ty | |
Here's an example: | |
>>>hand=Hand() | |
>>>print find_defining_class(hand,'shuffle') | |
<class'Card.Deck'> |
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
from math import sqrt | |
N = 100 | |
result = [p for p in range(2, N) if 0 not in [p % d for d in range(2, int(sqrt(p)) + 1)]] |
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
import string | |
replace_punctuation = string.maketrans(string.punctuation, ' ' * len(string.punctuation)) | |
line = line.translate(replace_punctuation) |
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
import datetime | |
datetime.datetime.now().replace(microsecond=0) |