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
### 6. Set random seed | |
def reset_random_seeds(CUR_SEED=1234): | |
os.environ['PYTHONHASHSEED']=str(CUR_SEED) | |
tensorflow.random.set_seed(CUR_SEED) | |
numpy.random.seed(CUR_SEED) | |
random.seed(CUR_SEED) | |
torch.manual_seed(CUR_SEED) | |
torch.cuda.manual_seed(CUR_SEED) | |
torch.cuda.manual_seed_all(CUR_SEED) | |
torch.backends.cudnn.deterministic = True |
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
### numpy | |
numpy.set_printoption(threshold=sys.maxsize, suppress=True) |
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 pandas as pd | |
pd.options.display.max_columns = None | |
pd.options.display.max_rows = 999 | |
## set max column width for strings | |
pd.options.display.max_colwidth = 20 |
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
@timerWrapper | |
def getMultiplication(num): | |
for val in range(num): | |
print(10**(10**val)) | |
getMultiplication(3) | |
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
### 4. Code timer as a decorator | |
import time | |
import requests | |
def timerWrapper(func): | |
"""Code the timer""" | |
def timer(*args, **kwargs): | |
"""Start timer""" | |
start = time.perf_counter() |
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
### Nested list comprehension | |
[element for sublist in myLists for element in sublist] | |
## return: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] |
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
### 3. Nested list comprehension | |
myLists = [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9], [10], [11, 12]] | |
Expected Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] |
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
### 2. Operating system commands with os | |
import os | |
os.startfile('myFullFileName.ANYFORMAT') |
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
### 1. Dynamic execution | |
codeString = '''a,b = 4,5; print(f"a = {a} and b = {b}"); print(f"a+b = {a+b}")''' | |
output = exec(codeString) | |
## print the output | |
print(f'** Is the return from exec() is None? {output == None} **') |
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
exec(open("myFullFileName.py").read()) |