Skip to content

Instantly share code, notes, and snippets.

@fwSara95h
fwSara95h / explanation_jsonload.md
Last active August 17, 2024 07:55
A helper function for extracting a JavaScript variable from a BeautifulSoup object [Example at https://stackoverflow.com/a/76366675/6146136 ]

Extract JavaScript variables from BeautifulSoup objects

INPUTS

  • inpX: must be a bs4 document/tag/ResultSet or a string or a list of strings
    • ( target variable must be JSON and seaparated from other variables by ; )
  • varName: name of the target variable
    • ( only the first variable found with the specified name will be returned )
  • selector: a CSS selector for searching the bs4 document/tag for target script
    • ( if inpX is a script-tag/ResultSet/string/list then selector doesn't matter )
  • prepFn: should be a univariate function that takes a string and returns a string
    • ( for modifying the script string before searching for and parsing variable )
@fwSara95h
fwSara95h / gsp313.sh
Created February 15, 2024 01:59
Solution to Create and Manage Cloud Resources: Challenge Lab https://www.cloudskillsboost.google/focuses/10258?parent=catalog
### Variables + zone & region
YOUR_REGION = "" # scenario europe-west1
YOUR_ZONE = "" #scenario europe-west1-b
INSTANCE_NAME = "" #task1 nucleus-jumphost-640
CLUSTER_NAME = "" #task2 [nucleus-backend] [not provided - made up]
APP_PORT_NUMBER = "" #task2 8080
FIREWALL_RULE = "" #task3 accept-tcp-rule-846
@fwSara95h
fwSara95h / get_midpoint.py
Last active July 2, 2025 12:54
Get the midpoint between 2 dates
from datetime import date, datetime, timedelta
def get_midpoint(sd:date, ed:date, eod=True, is_v:bool=True, toRet=None):
# Calculate the midpoint
start_date = datetime(sd.year, sd.month, sd.day,
23 if eod else 0, 59 if eod else 0)
end_date = datetime(ed.year, ed.month, ed.day, 0, 1 if eod else 0)
total_duration = (end_date - start_date).total_seconds()
half_duration = total_duration // 2 # integer quotient
midpoint_dt = start_date + timedelta(seconds=half_duration)
@fwSara95h
fwSara95h / _HTML_Heading_Nesting_Parser-README.md
Last active July 9, 2025 05:54
Parse HTML such that the nesting that is implicit by header levels becomes explicit

HTML Heading Nesting Parser

This gist contains a Python tool for converting flat HTML documents—especially those with nested content indicated only by header levels (like <h1>, <h2>, etc.)—into a structured JSON object that reflects the implicit hierarchy.

Originally inspired by this StackOverflow question from @psychicesp, this script is useful for:

  • Web scraping content-heavy pages (like menus, outlines, legal docs)
  • Explicitly inferring structure from heading levels
  • Preserving content hierarchy when working with markdown-to-HTML output
@fwSara95h
fwSara95h / find_nested_val.py
Created July 10, 2025 05:53
Finding the keys and/or indices on the path to a target variable in a nested data structure.
def yield_key_paths(objC, valT, kPath=None, validTypes=(tuple,list,dict,set)):
''' Traverse objC data structure recursively to yield path/s to valT'''
if not isinstance(kPath, list): kPath = []
if objC == valT: yield kPath
if not isinstance(objC, validTypes): return
for k,v in (objC.items() if isinstance(objC,dict) else enumerate(objC)):
yield from yield_key_paths(v, valT, kPath+[k], validTypes)
@fwSara95h
fwSara95h / get_by_nested_keys.py
Created July 10, 2025 06:04
Get a nested value from a dictionary with only the final key
## [ specifying data type of value with dType is optional ]
# a generator for the values
def yield_by_nested_keys(obj:dict, *keys, dType=None):
if not keys or not isinstance(obj,(dict,list,set,tuple)): return
if not isinstance(obj, dict):
for v in obj:
for y in yield_by_nested_keys(v,*keys,dType=dType): yield y
return
if len(keys)==1 and keys[0] in obj:
if dType is None or isinstance(obj[keys[0]], dType):
@fwSara95h
fwSara95h / reussir-tcfcanada--scraper-README.md
Last active July 28, 2025 14:23
reussir-tcfcanada--scraperScratchpad.ipynb

Réussir TCF Canada - Data Scraper & Task Tracker

View the notebook on Google Colab (if you want to run the code interactively).

This project contains an experimental scraper and data processing notebook for building practice tools for the TCF Canada exam, focusing on Expression Orale and Expression Écrite tasks.

📌 Overview

Planned Flow Image

  • Goal: Collect, organize, and categorize prompts for TCF Canada speaking and writing practice.
  • Use Case: Forms the foundation for a web app or study aid that randomizes practice tasks and ensures coverage across different categories.
@fwSara95h
fwSara95h / palette-contrasts.ipynb
Last active August 11, 2025 21:04
palette-contrasts.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.