Skip to content

Instantly share code, notes, and snippets.

View codingforentrepreneurs's full-sized avatar
🔥

Coding For Entrepreneurs codingforentrepreneurs

🔥
View GitHub Profile
@codingforentrepreneurs
codingforentrepreneurs / Cassanda TimeUUID to PythonDateTime.md
Last active September 2, 2021 05:02
Convert a uuid1().time into a datetime object.

I've been using cassandra a lot lately. It's very common to denote ID fields in cassanda as a time-based uuid field called timeuuid docs. In python, a timeuuuid is mearly uuid.uuid1 from the built-in uuid package.

Using the cassandra-driver, you might end up with a model like:

import uuid
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
@codingforentrepreneurs
codingforentrepreneurs / number_str_to_float.py
Created August 5, 2021 16:20
A simple python function to convert a number string into a float if possible.
from fractions import Fraction
def number_str_to_float(amount_str:str) -> (any, bool):
"""
Take in an amount string to return float (if possible).
Valid string returns:
Float
Boolean -> True
@codingforentrepreneurs
codingforentrepreneurs / Django_Custom_Context_Processors.md
Created February 9, 2023 17:53
Django: Custom Context Processors

Django Custom Context Processors Example

In cfehome/context_processors.py add:

def my_context(request):
    # caching
    return {
 "my_var": "hello world"
@codingforentrepreneurs
codingforentrepreneurs / 1-install-knative-istio.sh
Last active June 10, 2023 18:24
Install Knative & Istio on Kubernetes
# Get version at https://knative.dev/docs/install/yaml-install/serving/install-serving-with-yaml/
export KNATIVE_VERSION="v1.10.1" # ensure ISTIO install matches this version too
# Install knative serving
# Ref: https://knative.dev/docs/install/yaml-install/serving/install-serving-with-yaml/#install-the-knative-serving-component
kubectl apply -f https://github.com/knative/serving/releases/download/knative-$KNATIVE_VERSION/serving-crds.yaml
kubectl apply -f https://github.com/knative/serving/releases/download/knative-$KNATIVE_VERSION/serving-core.yaml
# install istio
@codingforentrepreneurs
codingforentrepreneurs / Django for Jupyter.md
Last active June 16, 2023 19:38
Django Setup for use in Jupyter Notebooks

Django for Jupyter

It's true packages exist to make it "easy" to use Django inside of a jupyter notebook. I seem to always run into issues successfully running these packages. I've found the below method useful although I cannot recall how I discovered how this works (aka attribution needed).

Requirements

  • Virtual Environment (virtualenv, venv, pipenv, etc)
  • Django installed & project created (we'll use the project name cfehome)
  • Jupyter installed at least in the virtual environment
@codingforentrepreneurs
codingforentrepreneurs / nextjs-pbkdf2.js
Created July 18, 2023 16:02
A custom crypto pbkdf2 method for Next.js
/*
Next.js/Edge function method for hasing passwords
Using the Web API Crypto feature instead of
Built-in Node.js Crypto
*/
export default async function pbkdf2(password, salt, iterations, keylen) {
const enc = new TextEncoder();
const passwordBuffer = enc.encode(password);
const saltBuffer = enc.encode(salt);
@codingforentrepreneurs
codingforentrepreneurs / isValidURL.js
Created July 12, 2023 19:58
Verify if a URL is valid or not via Regex in Next.js Server Components
export default async function isValidURL(url, disallowedDomains) {
// Construct a regular expression pattern to match disallowed domains
const disallowedPattern = `^https?:\\/\\/(?:${disallowedDomains.join('|')})\\b`;
let disallowedRegex = new RegExp(disallowedPattern, 'i');
// Regular expression pattern to match a URL (excluding localhost)
const urlPattern = /^(https?:\/\/)?((?!localhost)[\w.-]+)\.([a-z]{2,})(:\d{1,5})?(\/.*)?$/i;
let urlRegex = new RegExp(urlPattern);
// Test the URL against both URL pattern and disallowed domain pattern
@codingforentrepreneurs
codingforentrepreneurs / Scrape-BoxOfficeMojo-Notebook.ipynb
Last active January 22, 2024 08:23
Scrape & Save Data from Box Office Mojo (Educational)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@codingforentrepreneurs
codingforentrepreneurs / Extract Endpoint from Serverless Framework.md
Created January 26, 2024 17:52
Extract Endpoint from Serverless Framework

Extract Endpoint from Serverless Framework

The serverless framework lacks native support for outputting the endpoint url for a deployment:

serverless info --stage prod --region us-east-2

Outputs

DOTENV: Loading environment variables from .env, .env.prod:
@codingforentrepreneurs
codingforentrepreneurs / 00-pandas-to-sql.md
Last active February 27, 2024 18:22
Export Pandas Dataframe to a PostgreSQL Database Table

Export Pandas Dataframe to a PostgreSQL Database Table

Export your Pandas analysis really easily to a PostgresSQL database table with this tutorial. We used Docker Compose to create the postgres database with docker compose up and the related compose.yaml file.

Step 1 - Install Requirements

Add requirements.txt from below.

python3 -m pip install -r requirements.txt