Skip to content

Instantly share code, notes, and snippets.

View onelharrison's full-sized avatar

Onel Harrison onelharrison

View GitHub Profile
@onelharrison
onelharrison / pandas_write_to_s3_using_boto3.py
Last active August 19, 2021 03:16
Demo script for writing a pandas data frame to a CSV file on S3 using the boto3 library
"""
Demo script for writing a pandas data frame to a CSV file on S3 using the boto3 library
"""
import io
import os
import boto3
import pandas as pd
@onelharrison
onelharrison / pandas_read_from_s3_using_s3fs.py
Last active February 20, 2021 23:41
Demo script for reading a CSV file from S3 into a pandas data frame using s3fs-supported pandas APIs
"""
Demo script for reading a CSV file from S3 into a pandas data frame using s3fs-supported pandas
APIs
"""
import os
import pandas as pd
AWS_S3_BUCKET = os.getenv("AWS_S3_BUCKET")
@onelharrison
onelharrison / pandas_write_to_s3_using_s3fs.py
Last active May 10, 2022 01:23
Demo script for writing a pandas data frame to a CSV file on S3 using s3fs-supported pandas APIs
"""
Demo script for writing a pandas data frame to a CSV file on S3 using s3fs-supported pandas APIs
"""
import os
import pandas as pd
AWS_S3_BUCKET = os.getenv("AWS_S3_BUCKET")
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
import redis
r = redis.Redis(host='localhost', port=6379, db=1)
r.set('hello', 'world') # True
value = r.get('hello')
print(value) # b'world'
r.delete('hello') # True
@onelharrison
onelharrison / rpn_evaluator.py
Last active June 19, 2021 14:05
Python script for evaluating expressions in Reverse Polish Notation
#!/usr/bin/env python
import logging
import operator as op
import sys
from typing import Any, List, Union
logging.getLogger(__name__).setLevel("INFO")
supported_operators = {"+": op.add, "-": op.sub, "*": op.mul, "/": op.truediv}
def first_name(full_name: str) -> str:
"""Extracts and returns the first name from a full name"""
return full_name.split(" ")[0]
def square(n: int) -> int:
"""Returns the square of the given number"""
return n ** 2
from math import floor, sqrt
def is_even(num: int) -> bool:
"""Returns True if a given number is even, otherwise False"""
return num % 2 == 0
def is_prime(num: int) -> bool:
"""Returns True if a given number is prime, otherwise False"""
from typing import Any, Tuple
Pair = Tuple[Any, Any]
def pair_first(pair: Pair) -> Any:
"""Returns the first item in a given pair"""
return pair[0]
def log_before(f):
def _wrapper(*args, **kwargs):
print("logging before...")
f(*args, **kwargs)
return _wrapper
def log_after(f):
def _wrapper(*args, **kwargs):
@onelharrison
onelharrison / subsets.py
Last active August 21, 2021 03:35
Python recipe for generating subsets
from itertools import chain, combinations
from typing import Collection, Iterable, Optional
def subsets(
collection: Collection, min_size: int = 0, max_size: Optional[int] = None
) -> Iterable:
"""Produce all the subsets of `collection` with cardinalities between
`min_size` and `max_size` (inclusive)."""