Skip to content

Instantly share code, notes, and snippets.

@pydemo
pydemo / generators.md
Last active September 18, 2018 17:12
python disk caching

The problem with comprehention is that for every list comprehension, we are allocating a new list, increasing memory usage. Instead of using list comprehension, we can employ generators. Generators are objects that, when iterated upon, compute a value on the fly and return the result.

def map_comprehension(numbers):
    a = [n * 2 for n in numbers]
    b = [n ** 2 for n in a]
    c = [n ** 0.33 for n in b]
 return max(c)
@pydemo
pydemo / numpy.md
Last active September 18, 2018 18:36
Numpy bits

Faster array retrieval - numpy.take David M. Cooke wrote a package called numexpr it is based on a single function--numexpr.evaluate.

a = np.random.rand(10000) 
b = np.random.rand(10000) 
c = np.random.rand(10000) 

d = ne.evaluate('a + b * c')

@pydemo
pydemo / printf.md
Created September 20, 2018 15:32
Printing from Cython
%%cython 
cdef double a 
from libc.stdio cimport printf 
printf("%p", &a)
# Output:
# 0x7fc8bb611210 
@pydemo
pydemo / perf.md
Created September 21, 2018 14:05
Using perf timeit

PyPy warning about timeit

$ source my-pypy-env/bin/activate
Python 2.7.12 (aff251e54385, Nov 09 2016, 18:02:49)
[PyPy 5.6.0 with GCC 4.8.2]

(my-pypy-env) $ python -m timeit --setup "from simul import benchmark" "benchmark()"
WARNING: timeit is a very unreliable tool. use perf or something else for real measurements
10 loops, average of 7: 106 +- 0.383 msec per loop (using standard deviation)
@pydemo
pydemo / youtube.sh
Created September 21, 2018 14:51
youtube video download example
#!/usr/bin/env bash
# FeedTube, a feed video downloader for Youtube
# Copyright Carlos Fenollosa <cf@cfenollosa.com>, 2018. Licensed under the GNU GPL v3: https://www.gnu.org/licenses/gpl-3.0.en.html
# Check out README.md for more details
# Manual switches. Possible values are "yes" or "" (empty)
ASCII_ONLY="yes" # Output video filename will only contain ascii characters
# Check help switch and show usage
@pydemo
pydemo / threading.md
Created September 21, 2018 15:42
Threading timer with callback example.
import threading

def wait_and_print_async(msg):
    def callback():
        print(msg)

    timer = threading.Timer(1.0, callback)
    timer.start()
@pydemo
pydemo / future.md
Last active September 21, 2018 16:36
Concurrency futures

Futures are fancier callbacks

    they keep track of the resource status
    cancel(unschedule) schedled tasks
    handle exceptions more naturally
from concurrent.futures import Future

def network_request_async(number):
@pydemo
pydemo / fstring.md
Created September 24, 2018 14:14
F-string example in Python 3.6+

f-strings (in Python 3.6+):

In [8]: decksize = 52
 
In [9]: print(f'There are {factorial(decksize):g} ways to shuffle {decksize} cards')
There are 8.06582e+67 ways to shuffle 52 cards
@pydemo
pydemo / generator.md
Last active September 24, 2018 16:25
Co-routine example

Coroutine as a function that can be stopped and resumed.

A basic example of coroutines is generators.

Generators can be defined in Python using the yield statement inside a function.

 def range_generator(n):
 i = 0
@pydemo
pydemo / phily.md
Created September 25, 2018 03:09
Things to do i Phily

Philly was a cool place to grow up and live in. The transportation system is awesome ( compared to many other cities in the U.S. ) and you can just about get anywhere with transfers and time. The museums in center city are legendary as is the center city library ( and all of the Free Library branches every where in the city ) —- Fairmount Park is one of the largest city parks in the world and you can ( truthfully ) spend weeks exploring it from the 17th century Swedes cabins in the North end of the park, to the covered bridge, the 18th century road and the Valley Green Inn to the hermits cave and the East Falls, the trails throughout the park and just the boreal forest surroundings. Manyunk is so cool with it’s Paris like neighborhood as is The Italian neighborhoods around South Philly and its’ market —- Society Hill and it’s streets of charming 18th century houses makes a wonderful walk as does the Antique Shops on Pine Street, Freeman’s Auction House on Chestnut Street has some really cool Tuesday auctions