Skip to content

Instantly share code, notes, and snippets.

View berkorbay's full-sized avatar
🏄

Berk Orbay berkorbay

🏄
  • Tideseed
  • Istanbul, Turkey
  • 10:18 (UTC +03:00)
View GitHub Profile
@berkorbay
berkorbay / os_sys_upper_directories.py
Created October 17, 2022 07:38
Getting to upper directories in Python
### This gist gives you the code snippet to reach upper directories
### so you can reach other functions in the document
### e.g. from mainfolder.subfolder.subfolder.examplefile import example_function
#### Include upper dirs start
dpath = os.path.dirname(os.path.abspath(__file__))
for i in range(4):
dpath = os.path.dirname(dpath)
sys.path.insert(1, dpath)
#### Include upper dirs end
@berkorbay
berkorbay / r_highs_ompr_example.R
Created September 29, 2022 15:10
R example of modelling with HiGHS solver and ompr framework
#######
### This is a minimal example of using HiGHS solver in R with the help of ROI and ompr packages.
#######
## SETUP BEGIN
pti <- c("ompr.roi","ompr","highs","devtools")
pti <- pti[!(pti %in% installed.packages())]
if(length(pti) > 0){
install.packages(pti)
}
@berkorbay
berkorbay / pandas_reorder_columns.py
Created April 18, 2022 14:37
Small function to reorder columns using Pandas. This is a quick and dirty approach. I'm pretty sure there are better alternatives.
import pandas as pd
def pd_reorder_cols(df, d):
"""
Reorders the position of columns with the given specs dictionary.
"""
cl = list(df.columns)
for k1, v1 in d.items():
k1_ix = cl.index(k1)
cl.pop(k1_ix)
@berkorbay
berkorbay / mwe_fragment.Rmd
Created December 22, 2021 22:27
RMarkdown include an HTML fragment within an HTML fragment and how to solve <!DOCTYPE html> print problem.
---
output:
html_fragment:
self_contained: false
always_allow_html: true
---
```{=html}
<!DOCTYPE html>
```
@berkorbay
berkorbay / read_xlsx_to_pandas_fast.py
Created August 3, 2021 18:49
Blazing fast of xlsx files into pandas
from datatable import fread
import pandas as pd
excel_path = "my_excel_file.xlsx"
df = fread(excel_path+"/sheet_name").to_pandas()
@berkorbay
berkorbay / ortools_docker_run.sh
Created July 30, 2021 15:18
OR-Tools Docker OOM issue (Exit code 137)
# This is an example docker run command to run streamlit which will include an ortools model
# -m 12g will keep the memory at 12GB
# --oom-kill-disable will prevent the container to go under because of out of memory sigkill
docker run -dp 80:8501 --oom-kill-disable -m 12g <image_name>
@berkorbay
berkorbay / Dockerfile
Last active July 29, 2021 06:10
Minimal streamlit (v0.85) in Python 3.9.6 Dockerfile
FROM python:3.9.6
RUN pip install --no-cache-dir streamlit
EXPOSE 8501
ENTRYPOINT [ "streamlit" ]
CMD ["hello"]
## FOR ANY APPLICATION
ENTRYPOINT [ "streamlit", "run"]
CMD ["my_streamlit_file.py"]
@berkorbay
berkorbay / annoying_problems_python
Last active May 2, 2022 07:44
Unordered list of solutions to annoying problems with Python
## Skip failing installations on requirements.txt
## https://stackoverflow.com/a/54053100/3608936
cat requirements.txt | sed -e '/^\s*#.*$/d' -e '/^\s*$/d' | xargs -n 1 pip install
### Jupyter Notebook not working
## https://stackoverflow.com/a/59571314/3608936
python3 -m notebook
@berkorbay
berkorbay / cognito_r_direct_query.R
Created February 17, 2021 07:35
AWS Cognito Simple Authentication with R
#######
#### This gist is a very quick way to implement just authentication function. This is a minimal example.
#### You need an AWS IAM account and your AWS Cognito User Pool set up.
#### Check the steps in the description of https://github.com/chi2labs/cognitoR
#### After creating your user pool on the left sidebar under General Settings, click on App clients
### Make sure "Enable username password based authentication (ALLOW_USER_PASSWORD_AUTH)" is checked
### Create a user from the interface for testing purposes. Let username be "usertest" and password be "passtest".
#######
#### Variables
@berkorbay
berkorbay / parse_ys_gmail_threads.R
Last active May 24, 2020 11:58
Parses YS order emails and brings them under a single data frame
options(stringsAsFactors=FALSE)
library(tidyverse)
library(rvest)
library(gmailr)
## FOLLOW AUTH INSTRUCTIONS FROM HERE https://gmailr.r-lib.org/articles/gmailr.html
parse_order_table <-function(my_msg,full_info_list=FALSE){
my_info <- read_html(gm_body(my_msg)) %>% html_nodes(xpath="/html/body/table/tr/td/center/table[1]/tr[2]/td") %>% html_children() %>% `[[`(2)