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
# If we were to implement a Python for-loop in Python, | |
for x in my_iterable: | |
do_stuff(x) | |
# it would be equivalent to the following: | |
iterator = iter(my_iterable) | |
while True: | |
try: |
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 random_items(lst): | |
lst = lst[:] # copy so original list is not mutated | |
while True: | |
random.shuffle(lst) | |
yield from lst | |
# Python 2 can't use `yield from` | |
def random_items_py2(lst): | |
lst = lst[:] | |
while True: |
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 adder(x): | |
def add_x(n): | |
return n + x | |
return add_x | |
add_1 = adder(1) | |
add_2 = adder(2) | |
add_42 = adder(42) | |
x = 10 |
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 timeit | |
import pickle | |
import json | |
s = """{ | |
"glossary": { | |
"title": "example glossary", | |
"GlossDiv": { | |
"title": "S", | |
"GlossList": { |
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
n = """12 | |
insert 0 5 | |
insert 1 10 | |
insert 0 6 | |
remove 6 | |
append 9 | |
append 1 | |
sort | |
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 timeit import timeit | |
from collections import deque | |
import string | |
def for_gen(): | |
for i in range(100000): | |
yield i | |
def while_gen(): | |
i = 0 | |
while i < 1000000: |
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
""" | |
An exampe quicksort implementation, demonstrating pythonic style | |
(albeit using inefficient `+` to concatenate lists...) | |
""" | |
def quicksort(seq): | |
if len(seq) <= 1: | |
return seq | |
pivot, *rest = seq | |
smaller = quicksort([x for x in rest if x <= pivot]) | |
larger = quicksort([x for x in rest if x > pivot]) |
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 | |
from collections import deque | |
from itertools import islice | |
consumer = deque(maxlen=0) | |
consume = consumer.extend | |
def generator(iterable, steps, consume=consume): | |
it = iter(iterable) | |
yield next(it) |
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
# So, consider: | |
>>> class SomeClass: | |
... var = 'foo' | |
... | |
>>> x = SomeClass() | |
>>> x.var | |
'foo' | |
>>> SomeClass.var | |
'foo' |
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 itertools import chain | |
from more_itertools import unique_everseen | |
# don't materialize into a list! | |
ci = zip(item_name, instance_id, contract_number, serial_number, item_status, product_group_description, item_end_date5)) | |
co = zip(item_name2, instance_id2, contract_number2, serial_number2, item_status2, product_group_description2, item_end_date6)) | |
# use itertools.chain instead of list-concatenation if you want to chain to iterables! | |
# You should, *use snake_case*, it's the PEP8 recommended way. If you *do* want to use camelCase | |
# the don't mix snake_case and camelCase. Use one or the other. Preferablly snake_case :) |
OlderNewer