Skip to content

Instantly share code, notes, and snippets.

View onelharrison's full-sized avatar

Onel Harrison onelharrison

View GitHub Profile
@onelharrison
onelharrison / factorialo.rkt
Last active December 6, 2020 21:55
factorial implementation in minikanren
#lang racket
(require minikanren)
(require minikanren/numbers)
(define zo (build-num 0))
(define 1o (build-num 1))
(define (factorialo n f)
@onelharrison
onelharrison / coalesce.py
Created December 17, 2020 22:29
Implementation of the coaselce function in python
def coalesce(*values):
"""Return the first non-None value or None if all values are None"""
return next((v for v in values if v is not None), None)
@onelharrison
onelharrison / safe_cast.py
Created December 17, 2020 22:33
Implementation of a safe type casting function in Python
def safe_cast(value, astype, default = None):
"""Convert one type to another without raising errors"""
try:
return astype(value)
except (TypeError, ValueError):
pass
return default
@onelharrison
onelharrison / safe_get.py
Created December 17, 2020 22:43
Implementation of a safe get function in Python
def safe_get(collection, key, default=None):
"""Get values from a collection without raising errors"""
try:
return collection.get(key, default)
except TypeError:
pass
try:
return collection[key]
@onelharrison
onelharrison / dig.py
Last active January 8, 2021 09:09
Implementation of a dig function in Python
from functools import reduce
def dig(collection, *keys, default=None):
"""Get values from a potentially nested collection without raising errors"""
return reduce(lambda x, y: safe_get(x, y, default), keys, collection)
@onelharrison
onelharrison / medium_story_data.py
Created December 19, 2020 03:22
Medium story data
from dataclasses import dataclass
from typing import Optional
@dataclass
class MediumStory:
title: str
description: str
body: str
published_at: int
featured_image_url: Optional[str] = None
@onelharrison
onelharrison / extract_medium_story.py
Last active December 19, 2020 19:23
Extract function for medium story data
import time
def extract_medium_story(story):
medium_story = MediumStory()
medium_story.title = story["title"]
medium_story.description = story["description"]
medium_story.body = story["body"]
medium_story.published_at = story["published_at"]
# notice in the example data that
@onelharrison
onelharrison / extract_employee_record.py
Last active December 19, 2020 07:31
Extract function for employee record
import time
from dataclasses import dataclass
from typing import Optional
@dataclass
class Employee:
name: str
age: Optional[int] = None
job_title: str
"""
The following code explores the presentation of contacts
in an on-screen phone book in which a contact's
first name xor last name may be missing and contacts can
be sorted by first name xor last name.
A true sort by first name and last name is implemented as well as
correspending sort procedures with a presentation bias. In effect,
the presentation bias makes the contacts in the on-screen phone book
appear to be sorted by their intended sort key but in really this may
@onelharrison
onelharrison / pandas_read_from_s3_using_boto3.py
Last active April 28, 2021 01:11
Demo script for reading a CSV file from S3 into a pandas data frame using the boto3 library
"""
Demo script for reading a CSV file from S3 into a pandas data frame using the boto3 library
"""
import os
import boto3
import pandas as pd