Skip to content

Instantly share code, notes, and snippets.

View ivankeller's full-sized avatar

Ivan Keller ivankeller

  • AXA Belgium
  • Belgium
View GitHub Profile
@ivankeller
ivankeller / staying_alive.py
Last active June 20, 2023 20:57
a python script that fake a key stroke to keep the system alive
#!/bin/env python3
import sys
from threading import Event
from keyboard import press, add_hotkey, remove_hotkey
from time import sleep
from typing import Union
PERIOD = 2.0
TOGGLING_KEY = 'alt+t'
TERMINATING_KEY = 'esc'
@ivankeller
ivankeller / aws_session.py
Created February 11, 2021 09:48
Connect to AWS with your AWS profile
import boto3
profile_name = ''
prod = boto3.session.Session(profile_name=profile_name)
# use it, example:
s3client = prod.client('s3')
@ivankeller
ivankeller / delete_versioned_s3_buckets.py
Created February 4, 2021 11:27
delete versioned AWS s3 buckets with boto3
#!/usr/bin/env python
# for listing and selecting the buckets you want to delete use list_s3_buckets.py gist
import boto3
# list of the bucket names you want to delete
BUCKETS_TO_DELETE = []
def delete_versioned_bucket(s3resource, bucket_name):
@ivankeller
ivankeller / list_s3_buckets.py
Created February 4, 2021 11:27
display the list of s3 buckets ready to select
#!/usr/bin/env python
import boto3
s3client = boto3.client('s3')
response = s3client.list_buckets()
for bucket in response['Buckets']:
print(f"""\'{bucket['Name']}\',""")
@ivankeller
ivankeller / pipenv_jupyter.txt
Created November 16, 2019 14:04
add pipenv virtual environment to Jupyter notebook kernels
$ cd my_project #where Pipfile resides
$ pipenv install ipykernel
$ pipenv shell #if not already done before
(my-virtualenv-name) $ python -m ipykernel install --user --name=my-virtualenv-name
(my-virtualenv-name) $ jupyter lab #or jupyter notebook
@ivankeller
ivankeller / r_sample.py
Created May 22, 2019 11:59
sub sample two-class sets into subsets given a ratio of items
import pytest
import numpy as np
def r_cut(x0: float, x1: float, r: float) -> (float, float):
"""Return y0, y1 such that y1/y0 = r, y0<x0, y1<x1 and y0+y1 is maximum.
Parameters
----------
x0, x1 > 0
r >= 0 and <= 1
@ivankeller
ivankeller / dataframe_constructor.py
Last active May 5, 2023 13:35
function that output the string required for constructing a given Pandas dataframe (useful on tests)
def dataframe_constructor(df):
return "df = pd.DataFrame(%s)" % (str(df.to_dict()).replace("nan"," float('nan')").replace('array', 'np.array'))
import pandas as pd
import numpy as np
def describe_df(df, cols=None, max_distinct=10):
"""Describe columns of given dataframe.
Return a dataframe with column name, type, nb of distinct values and nb of missing values
Missing values are counted as one distinct value.
Issue: if a column contains integer and missing values, pandas cast it in float and add .0 to the value.
@ivankeller
ivankeller / describe_dataframe.py
Last active March 12, 2023 18:36
describe dataframe columns
import pandas as pd
import numpy as np
def describe_df(df, cols=None, max_distinct=10):
"""Describe columns of given dataframe.
Return a dataframe with column name, type, nb of distinct values and nb of missing values
Missing values are counted as one distinct value.
Issue: if a column contains integer and missing values, pandas cast it in float and add .0 to the value.
@ivankeller
ivankeller / is_in_arc.py
Last active February 14, 2018 15:44
Function that tests if a point belongs to a interval modulo (i.e. an arc of circle)
def belongs_to(t, a, b):
"""Return True if t belongs to real number interval [a, b], False otherwise."""
return t >= a and t <= b
def is_in_arc(t, a, b, m=365):
"""Return True if t is in the interval [a, b] modulo m, False otherwise"""
t, a, b = [x % m for x in (t, a, b)] # convert all the values to their values modulo m
if a < b:
return belongs_to(t, a, b)
else: