This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require minikanren) | |
(require minikanren/numbers) | |
(define zo (build-num 0)) | |
(define 1o (build-num 1)) | |
(define (factorialo n f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from dataclasses import dataclass | |
from typing import Optional | |
@dataclass | |
class Employee: | |
name: str | |
age: Optional[int] = None | |
job_title: str |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 | |