-
saving a program's state data to disk so that it can carry on where it left off when restarted (persistence)
-
sending python data over a TCP connection in a multi-core or distributed system (marshalling)
-
storing python objects in a database
-
converting an arbitrary python object to a string so that it can be used as a dictionary key (e.g. for caching & memoization).
This file contains 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 gcd # Python versions 3.5 and above | |
#from fractions import gcd # Python versions below 3.5 | |
from functools import reduce # Python version 3.x | |
def lcm(denominators): | |
return reduce(lambda a,b: a*b // gcd(a,b), denominators) |
This file contains 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 search_path(root): | |
if root is None: | |
return [] | |
if root.left is None and root.right is None: | |
return [root.value] | |
return [root.value + val for val in search_path(root.right) | |
+ search_path(root.left)] | |
This file contains 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
model = Sequential() | |
model.add(Dense(256, input_shape=(40, ))) | |
model.add(Acitivation('relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(256)) | |
model.add(Activation('relu')) | |
model.add(Dropout(0.5)) |
This file contains 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 batch_generator(items, batch_size): | |
batch = [] | |
i = 0 | |
for item in items: | |
batch.append(item) | |
if len(batch) == batch_size: | |
yield batch | |
batch = [] | |
yield batch |
This file contains 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
$ docker run -it --rm --name ds -p 8888:8888 jupyter/datescience-notebook |
This file contains 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 os | |
project_root = os.path.dirname(os.path.abspath(__file__)) |
This file contains 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
Step 1: In order to remove existing files from the repository: | |
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch | |
Step 2: Add this line '.DS_Store' to your .gitignore file, which can be found at the top level of your repository (or create if it isn't there already. Use the following command to do so: | |
echo .DS_Store >> .gitignore | |
Step 3: Then run the following command: |
This file contains 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 collections import defaultdict | |
item_list = 'spam spam egg egg spam'.split() | |
count = defaultdict(int) | |
for item in item_list: | |
count[item] += 1 | |
This file contains 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 = 1234 | |
rev=0 | |
while(n>0): | |
dig=n%10 | |
rev=rev*10+dig | |
n=n//10 |