Skip to content

Instantly share code, notes, and snippets.

@luisdelatorre012
luisdelatorre012 / prorate_proportionally.py
Created October 11, 2024 02:18
prorate proportionally
def prorate_proportionally(df: pd.DataFrame, group: pd.DataFrame):
# Get the total tonnage values from the first row
total_tonnage_moved_contract = group.iloc[0]["tonnage_moved_contract"]
total_tonnage_moved_spot = group.iloc[0]["tonnage_moved_spot"]
total_tonnage_moved_unknown = group.iloc[0]["tonnage_moved_unknown"]
# Calculate the total commitment
total_commitment = group["tonnage_committed"].sum()
# Compute proration factors for each row
@luisdelatorre012
luisdelatorre012 / current_user_msal.py
Created September 20, 2024 01:17
Console app for displaying current user's info using MSAL
import msal
import httpx
from dynaconf import Dynaconf
# Load configuration
config = Dynaconf(
settings_files=['settings.toml', '.secrets.toml'],
environments=True,
load_dotenv=True,
)
@luisdelatorre012
luisdelatorre012 / recursively_walk_dict.py
Last active September 30, 2024 11:37
Recursively walk dict
import json
import pyodbc
import re
from datetime import date, datetime
from typing import Any, Dict, Optional, List
def load_json(file_path: str) -> Any:
with open(file_path, 'r') as file:
return json.load(file)
@luisdelatorre012
luisdelatorre012 / ad_group_sync.py
Created September 17, 2024 16:37
ad group sync
import subprocess
import json
def run_powershell_command(command):
completed_process = subprocess.run(
["powershell", "-Command", command],
capture_output=True,
text=True
)
if completed_process.returncode != 0:
@luisdelatorre012
luisdelatorre012 / json_schema_dynamic_class_creation.py
Last active September 18, 2024 16:28
json schema dynamic class creation
import json
import logging
import os
from typing import Any, Dict, Optional, Type, List, Tuple
import jsonref # Library for resolving JSON schema references
from jsonschema import Draft7Validator
from sqlalchemy import (
JSON,
Boolean,
@luisdelatorre012
luisdelatorre012 / sample_json_to_database_sqlalchemy.py
Created September 13, 2024 01:40
sample json to database sqlalchemy
import json
import uuid
from sqlalchemy import create_engine, Column, Integer, String, Float, Boolean, DateTime, ForeignKey
from sqlalchemy.orm import declarative_base, sessionmaker, relationship
from sqlalchemy.dialects.mssql import UNIQUEIDENTIFIER
from datetime import datetime
Base = declarative_base()
class Product(Base):
@luisdelatorre012
luisdelatorre012 / json_schema_to_sql.py
Last active September 13, 2024 16:02
json schema to sql
import json
import re
def convert_json_schema_to_sql(schema: dict[str, any]) -> list[str]:
tables = []
foreign_keys = []
indexes = []
definitions = schema.get('definitions', {})
# Handle top-level properties
@luisdelatorre012
luisdelatorre012 / genson_schema_creator.py
Created September 13, 2024 01:35
genson schema creator
import json
from pathlib import Path
from genson import SchemaBuilder
def generate_schema_from_files(directory):
builder = SchemaBuilder()
directory_path = Path(directory)
for file_path in directory_path.glob('*.json'):
with file_path.open('r') as f:
@luisdelatorre012
luisdelatorre012 / cross_apply.sql
Created September 12, 2024 15:41
cross apply
SELECT
t.Category,
MIN(j.eventRecordedTime) AS MinEventTime,
MAX(j.eventRecordedTime) AS MaxEventTime,
AVG(j.someNumericValue) AS AvgValue
FROM
t
CROSS APPLY OPENJSON(CAST(CAST(t.message AS varchar(max)) AS nvarchar(max)))
WITH (
eventRecordedTime datetime2 '$.eventRecordedTime',
@luisdelatorre012
luisdelatorre012 / mongo_debug.py
Created September 12, 2024 02:16
mongo_debug.py
#Use explicit comparison in aggregation:
#Let's try using the aggregation framework with explicit type checking:
rom bson import Int64
pipeline = [
{
"$match": {
"$expr": {
"$and": [