Skip to content

Instantly share code, notes, and snippets.

View anhdanggit's full-sized avatar

Anh Dang anhdanggit

View GitHub Profile
@anhdanggit
anhdanggit / googleapi_vision_face_emo.py
Last active April 1, 2025 00:38
FACE_DETECTION for happy/unhappy #googleapis #gcp
## Install this in terminal: !pip install --upgrade google-api-python-client
## This func to call googleapi, apply the face_detection on img, and output the happy/unhappy likelihood
def detect_joy(IMG):
from googleapiclient.discovery import build
vservice = build('vision', 'v1', developerKey=APIKEY)
## CHECK THIS: https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate
## The have the interactive tool to create the body of request
request = vservice.images().annotate(body={
"requests": [
@anhdanggit
anhdanggit / Py-read-data.py
Last active April 1, 2025 00:39
Read csv, html, xml, hdf5, api
## Import modules
import pandas as pd
import numpy as np
import scipy as sp
import pandas_datareader.data as web
from datetime import datetime as dt ## datetime
import json ## json
import requests ## API
from lxml import objectify
import os
@anhdanggit
anhdanggit / copy-to-clipboard.R
Created July 18, 2019 08:25
Copy to Clipboard function in R
copy_to_clipboard = function(x,sep="\t",col.names=T,...) {
write.table(x
,file = pipe("pbcopy")
,sep=sep
,col.names = col.names
,row.names = F
,quote = F,...)
}
@anhdanggit
anhdanggit / Py-Compre-Iter.py
Last active April 1, 2025 00:40
Comprehensive and Iteration in Python #algorithms
## Comprehensive -----
strings = ['a', 'as', 'bat', 'car', 'dove', 'python']
[x.upper() for x in strings if len(x) > 2] ## list comprehensive
{x.upper() for x in strings if len(x) > 2} ## set comprehensive
{key: val.upper() for key,val in enumerate(strings)} ## dict comprehensive
## Nested Comprehensive ----
all_data = [['John', 'Emily', 'Michael', 'Mary', 'Steven'],
['Maria', 'Juan', 'Javier', 'Natalia', 'Pilar', 'Eerin']]
## full
@anhdanggit
anhdanggit / Py-Functions-tricks.py
Last active April 1, 2025 00:40
Object-functions, lambda, TryCatch, Currying (Partial) #basics
## Functions -----------
## Multiple return
def f():
a = 5
b = 6
c = 7
return a, b, c
a, b, c = f()
@anhdanggit
anhdanggit / Py-Dict-tricks.py
Last active April 1, 2025 00:40
Dictionaries in Python #algorithms
# Categorizing to dicts
words = ['apple', 'bat', 'bar', 'atom', 'book']
by_letters = {}
for word in words:
letter = word[0]
by_letters[letter] = by_letters.get(letter, 0) + 1
value = by_letters[letter]
print('Letter: {} - Count: {}'.format(letter, value))
@anhdanggit
anhdanggit / Py-Zip-tricks.py
Last active April 1, 2025 00:40
Zip in Python #basics
## Tuple:
# Capture arbitrarily long list
values = 1,2,3,4,5
a, b, *rest = values
# Discard the rest
a, b, *_ = values
## Zip:
# "pairs" up the elements of list
seq1 = ['foo','bar','baz']
@anhdanggit
anhdanggit / xml-extract-sql.R
Created July 16, 2019 12:52
SQL via R to extract fields from XML files
data_xml <- dbGetQuery(conn = DB_conn,statement = qq('
SELECT decision_id, data,
xpath(\'//application-uuid\',data) as application_uuid,
xpath(\'//brokerage-id\',data) as brokerage_id,
xpath(\'//Transformed\',data) as transformed,
xpath(\'//pd_commercial\',data) as pd_commercial,
xpath(\'//row_pd_comb\',data) as row_pd_comb,
xpath(\'//marginal_group\',data) as marginal_group,
@anhdanggit
anhdanggit / json-extract-sql.R
Last active July 16, 2019 12:51
SQL code to extract fields from json file
## Code to extract all fields from json
sphonic_data <- tbl(connection_risk_house(), sql(
"SELECT distinct on (application_id,jkey)
application_id,
jsonb_object_keys(section_payload) as jkey,
jsonb_object_field_text(section_payload, jsonb_object_keys(section_payload)) as jvalue
FROM fraud_detection.response_sections
WHERE sent_at >= '2017-10-01'
AND sent_at < '2017-12-01'
GROUP BY application_id,section_payload
@anhdanggit
anhdanggit / R_GetoptLong_toQuery.r
Last active July 18, 2019 09:52
Set-up DB conn and query by GeptoptLong
library(DBI)
library(dplyr)
library(GetoptLong)
library(magrittr)
data_warehouse <- function(){DBI::dbConnect(RPostgres::Postgres()
,dbname="database"
## to hide the credentials (set it up in .Renv)
,host=Sys.getenv("HOST")
,user= Sys.getenv("USERNAME")