Skip to content

Instantly share code, notes, and snippets.

@YiLi225
YiLi225 / setSeed.py
Last active October 22, 2021 07:53
### 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
### numpy
numpy.set_printoption(threshold=sys.maxsize, suppress=True)
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
@timerWrapper
def getMultiplication(num):
for val in range(num):
print(10**(10**val))
getMultiplication(3)
### 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()
### 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]
### 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]
### 2. Operating system commands with os
import os
os.startfile('myFullFileName.ANYFORMAT')
### 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} **')
exec(open("myFullFileName.py").read())