Skip to content

Instantly share code, notes, and snippets.

View franchb's full-sized avatar
🎯
Focusing

Eliah Rusin franchb

🎯
Focusing
View GitHub Profile
@franchb
franchb / main.go
Created February 20, 2018 11:17
reuse response Body in Golang
// read the response body to a variable
bodyBytes, _ := ioutil.ReadAll(response.Body)
// Use io.Copy to just dump the response body to the file. This supports huge files
err := ioutil.WriteFile("tmp/asdf.png", bodyBytes, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Println("Captcha image saving success!")
//reset the response body to the original unread state
response.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
@franchb
franchb / SparkRowApply.scala
Created February 14, 2018 09:12 — forked from jlln/SparkRowApply.scala
How to apply a function to every row in a Spark DataFrame.
def findNull(row:Row):String = {
if (row.anyNull) {
val indices = (0 to row.length-1).toArray.filter(i => row.isNullAt(i))
indices.mkString(",")
}
else "-1"
}
sqlContext.udf.register("findNull", findNull _)
df = df.withColumn("MissingGroups",callUDF("findNull",struct(df.columns.map(df(_)) : _*)))
@franchb
franchb / separator.py
Created February 14, 2018 09:12 — forked from jlln/separator.py
Efficiently split Pandas Dataframe cells containing lists into multiple rows, duplicating the other column's values.
def splitDataFrameList(df,target_column,separator):
''' df = dataframe to split,
target_column = the column containing the values to split
separator = the symbol used to perform the split
returns: a dataframe with each entry for the target column separated, with each element moved into a new row.
The values in the other columns are duplicated across the newly divided rows.
'''
def splitListToRows(row,row_accumulator,target_column,separator):
split_row = row[target_column].split(separator)

API workthough

  1. Open a browser

    # start an instance of firefox with selenium-webdriver
    driver = Selenium::WebDriver.for :firefox
    # :chrome -> chrome
    # :ie     -> iexplore
    
  • Go to a specified URL
@franchb
franchb / gist:0ed60861afe5d39032520f1096629cef
Created February 2, 2018 12:47 — forked from casschin/gist:1990245
Python webdriver api quick sheet
### Locating UI elements ###
# By ID
<div id="coolestWidgetEvah">...</div>
element = driver.find_element_by_id("coolestWidgetEvah")
or
from selenium.webdriver.common.by import By
element = driver.find_element(by=By.ID, value="coolestWidgetEvah")
# By class name:
@franchb
franchb / useful_pandas_snippets.py
Created February 2, 2018 05:49 — forked from bsweger/useful_pandas_snippets.md
Useful Pandas Snippets
# List unique values in a DataFrame column
# h/t @makmanalp for the updated syntax!
df['Column Name'].unique()
# Convert Series datatype to numeric (will error if column has non-numeric values)
# h/t @makmanalp
pd.to_numeric(df['Column Name'])
# Convert Series datatype to numeric, changing non-numeric values to NaN
# h/t @makmanalp for the updated syntax!
@franchb
franchb / replace_crlf.sql
Created January 29, 2018 09:24
SQL CRLF / endline issue
REPLACE(REPLACE(REPLACE(mycolumn, CHAR(9), ''), CHAR(10), ''), CHAR(13), '') as 'mycolumn'
@franchb
franchb / aws-lambda-static-type-checker.md
Created January 6, 2018 18:02 — forked from alexcasalboni/aws-lambda-static-type-checker.md
AWS Lambda Static Type Checker Example (Python3)

How to use Python3 Type Hints in AWS Lambda

TL;DR

Static Type Checkers help you find simple (but subtle) bugs in your Python code. Check out lambda_types.py and incrementally improve your code base and development/debugging experience with type hints.

Your Lambda Function code will go from this:

@franchb
franchb / gist:7944ad034171cce934e3da5ee9e10b74
Created November 9, 2017 20:13
Franchb Blockstack ID verification
Verifying my Blockstack ID is secured with the address 1MeCc7ajem6iSuiDGFMLgh9kQYp8okC5w4 https://explorer.blockstack.org/address/1MeCc7ajem6iSuiDGFMLgh9kQYp8okC5w4
@franchb
franchb / discrete_cmap.py
Created July 15, 2017 06:10 — forked from jakevdp/discrete_cmap.py
Small utility to create a discrete matplotlib colormap
# By Jake VanderPlas
# License: BSD-style
import matplotlib.pyplot as plt
import numpy as np
def discrete_cmap(N, base_cmap=None):
"""Create an N-bin discrete colormap from the specified input map"""