This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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": [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,...) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Functions ----------- | |
| ## Multiple return | |
| def f(): | |
| a = 5 | |
| b = 6 | |
| c = 7 | |
| return a, b, c | |
| a, b, c = f() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
NewerOlder