How to disable warnings in Python

There’s a number of different ways to turn off Python warnings. There’s a lot of annoying future deprecation warnings that might flood your console space like crazy. So in order to suppress unwanted warnings in Python, the easiest way is the following: import warnings warnings.filterwarnings(‘ignore’) if you would like disable some specific type of warnings, … Read more

List comprehension from python dictionary

Here’s my example: params = { ‘param1’: [[25, 30, 35],[-25, -30, -35]], ‘param2’: [[20, 25, 30],[-20, -25, -30]] } for key, value in paramsNEW.items(): print( key, value[0], value[1] ) for v in value: print(v) for key, value in paramsNEW.items(): print(key, [(v1,v2) for v1 in value[0] for v2 in value[1]])

Plot candlesticks in python (simple example)

import pandas as pd from datetime import datetime import plotly import plotly.graph_objects as go df = pd.read_csv(‘https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv’) fig = go.Figure(data=[go.Candlestick(x=df[‘Date’], open=df[‘AAPL.Open’], high=df[‘AAPL.High’], low=df[‘AAPL.Low’], close=df[‘AAPL.Close’])]) # fig.show() plotly.offline.plot(fig)

How to run python script on multiple cores/threads (multiprocessing)

I’ve been looking for means of parallel execution in Python for quite a while. There’s a bunch of various libraries like Dask and Joblib Parallel. However using multiple threads won’t give you better performance due to the global interpreter lock called GIL. At the moment the best solution I’ve found is multiprocessing. It uses subprocesses … Read more

Read and write files with ‘with’ statement in Python

I always keep forgetting how exactly with statement works with opening and reading the files in Python. path = ‘./path/filename.txt’ with open(path,’r’) as file: data = file.readlines() print(data) Or if you would like to avoid getting ‘\n’ after each line when using .readlines() you can use this instead: data = file.read().splitlines() Opening files with ‘with’ … Read more