Skip to content

Instantly share code, notes, and snippets.

@williamfalinski
williamfalinski / s3.py
Last active September 5, 2024 20:35
Basic client to download files by prefix on S3 recreating S3 'folder' structure inside temp (or any local_dir) using boto3
import boto3
import tempfile
import os
import threading
class ClientS3:
def __init__(self, bucket_name:str, profile_name:str, local_dir:str=None) -> None:
self.bucket_name = bucket_name
self.profile_name = profile_name
if local_dir is None:
@williamfalinski
williamfalinski / cloud_task.py
Created January 5, 2024 12:24
Function to create a new task in Google Cloud Tasks
import base64
import google.auth.transport.requests
import google.oauth2.id_token
import threading
import requests
from google.cloud import tasks_v2
def create_http_task_with_token(
project: str,
location: str,
@williamfalinski
williamfalinski / gdrive_service.py
Last active January 11, 2024 11:34
Class to upload entire folder to Google Drive including file and subfolders
import os
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from googleapiclient.errors import HttpError
from oauth2client.service_account import ServiceAccountCredentials
class GoogleDriveService(object):
"""
A class used to represent an GoogleDrive service for uploading files
gdrive_credentials.json must be in same folder, if not
@williamfalinski
williamfalinski / mysqldump_replace_column.py
Last active October 30, 2023 19:44
Replace ID or nth column from mysql dump
# -*- coding: utf-8 -*-
# Calling: python3 mysqldump_replace_column.py '/original/test.sql' 8 'NULL'
# Will replace de 8th column with NULL in a new file/location: '/edited/test_chunk_00001.sql'
import sys
import re
import csv
import glob
import os
def main():
@williamfalinski
williamfalinski / cloudbuild.yaml
Last active August 31, 2023 20:44
build-push-deploy of docker image in GCP (us-central1 can be changed for your location)
steps:
- id: "docker-build"
timeout: 1500s
name: "gcr.io/cloud-builders/docker"
args: ["build", "-t", "us-central1-docker.pkg.dev/${_PROJECT_ID}/${_REPOSITORY}/${_IMAGE}", "."]
- id: "push-docker-image"
name: "gcr.io/cloud-builders/docker"
args: ["push", "us-central1-docker.pkg.dev/${_PROJECT_ID}/${_REPOSITORY}/${_IMAGE}"]
waitFor: ["docker-build"]
@williamfalinski
williamfalinski / truncate_varchar.py
Last active November 21, 2023 13:14
Python function to truncate string columns from pandas dataframe based on varchar length from DB.
def truncate_varchar(df, db=None, table=None):
from sqlalchemy import MetaData
meta = MetaData()
meta.reflect(only=[table], resolve_fks=False, views = False, bind=db.getEngine())
datatable = meta.tables[table]
types = [str(c.type).lower() for c in datatable.columns]
names = [str(c.name).lower() for c in datatable.columns]
dict_tables = dict(zip(names, types))
dict_chars = {k:v for k,v in dict_tables.items() if 'char' in v}
@williamfalinski
williamfalinski / docker_helper
Last active September 13, 2023 12:25
docker gcloud helper
(didint grant permission to my user, thats why everything is runing with sudo)
===========================================================================
NEED TO AUTH FIRST, SET THE PROJECT AND CONFIGURE DOCKER IN RIGHT LOCATION
sudo gcloud auth login
sudo gcloud config set project 'PROJECT'
sudo gcloud auth configure-docker LOCATION_ARTIFACT
===========================================================================