Skip to content

Instantly share code, notes, and snippets.

@jrosell
jrosell / date-custom-field-Month-YYYYMM-google-looker-studio.js
Created April 6, 2023 12:15
Google Looker Studio snippets to help in the creation of helper date dimensions in string format.
// Create my_date_month custom field from existing date field my_date
MONTH(my_date)
// Create my_date_YYYYMM ucstom field from existing numeric field my_date and my_date_month
CASE
WHEN my_date_month=1 THEN CONCAT(YEAR(my_date),'01')
WHEN my_date_month=2 THEN CONCAT(YEAR(my_date),'02')
WHEN my_date_month=3 THEN CONCAT(YEAR(my_date),'03')
WHEN my_date_month=4 THEN CONCAT(YEAR(my_date),'04')
WHEN my_date_month=5 THEN CONCAT(YEAR(my_date),'05')
// View item: It should be executed when the product detail is shown.
dataLayer.push({ ecommerce: null });
dataLayer.push({
event: "view_item",
ecommerce: {
currency: "USD",
value: 7.77,
items: [
{
item_id: "SKU_12345",
@jrosell
jrosell / google_sheets_parse.txt
Last active March 22, 2023 13:41
When having Google Sheets in spanish or catalan, you can use these formulas to parse a texts to a proper date or number.
# From m/d/yyyy text to yyyy-mm-dd text.
=SUBSTITUTE(RIGHT(A4;4)&"-"&IF(LEN(A4)>9;LEFT(A4;2);"0"&LEFT(A4;1))&"-"&MID(A4;FIND("/";A4)+1;2);"/";"")
# From m/d/yyyy text to yyyy-mm-dd date.
=DATEVALUE(SUBSTITUTE(RIGHT(A4;4)&"-"&IF(LEN(A4)>9;LEFT(A4;2);"0"&LEFT(A4;1))&"-"&MID(A4;FIND("/";A4)+1;2);"/";""))
# From text 3.5 to 3,50 number
=VALUE(SUBSTITUTE(B5;".";","))
datos <- structure(list(Genero = c(2, 1, 1, 2, 1, 1, 1, 2, 2, 2),
`1. En casa` = c(1, 3, 1, 3, 4, 2, 4, 5, 5, 5),
`2. Con la tele` = c(4, 2, 1, 2, 3, 3, 2, 1, 1, 1)),
row.names = c(NA, -10L),
class = c("tbl_df", "tbl", "data.frame"))
# Opción con stats::wilcox.test
datos |>
dplyr::select(-Genero) |>
purrr::map_df(function(x){
```{python}
import csv
import requests
import time
# Open the CSV file and read the data
with open('data.csv', 'r') as file:
reader = csv.reader(file)
data = [row for row in reader]
@jrosell
jrosell / selenium_intercept_url.py
Last active January 20, 2023 18:49
Crawl a URL and capture network requests to third party resources like Google Analytics.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
@jrosell
jrosell / compose.py
Last active December 2, 2022 11:57
How to implement function composition in Python like we do in R: x <- 2; add_three <- \(x) x+3; duplicate <- \(x) x*2; x |> add_three() |> duplicate() |> {\(x) paste0("Result: ", x) }() # Result: 10
from typing import Callable
from functools import reduce, partial
add_three = lambda x: x+3
duplicate = lambda x: x*2
#-------
ComposableFunction = Callable[[float], float]
@jrosell
jrosell / polling.html
Created July 5, 2022 13:00
I currently have a tag that has a single trigger that is activated as many times as attributes need to be managed. I would like to add another tag that should be activated at the end, once the previous attributes have been processed by the initial tag.
<html><body>
<script>
function handle() {
console.log("handle")
window.myPolling = setTimeout(finish, 2000);
}
function finish() {
console.log("finish")
}
function fireAttribute(){
kgl_competitions_data_download <- function(competition_id, file_name, path = ".") {
.kaggle_base_url <- "https://www.kaggle.com/api/v1"
url <- paste0(.kaggle_base_url, "/competitions/data/download/", competition_id, "/", file_name)
rcall <- httr::GET(url, httr::authenticate(Sys.getenv("KAGGLE_USER"), Sys.getenv("KAGGLE_KEY"), type = "basic"))
tmp <- tempfile()
download.file(rcall$url, tmp)
invisible(file.copy(tmp, paste0(path, "/", file_name)))
}
#' Execute f for each row of df
#'
#' @param df data.frame
#' @param f function
#' @return A data.frame with the result of executin g for each row of df.
#' @examples
#' row_split_map(
#' tibble(x = c(1, 2), y = c(2, 3)),
#' function(df) {
#' df %>% mutate(