Skip to content

Instantly share code, notes, and snippets.

View Nasdin's full-sized avatar
🎯
Focusing

Nasrudin Bin Salim Nasdin

🎯
Focusing
View GitHub Profile
@Nasdin
Nasdin / json_to_csv.sh
Created October 3, 2024 01:33
Polars json to csv converter bash
#!/usr/bin/env bash
set -euo pipefail
DESCRIPTION="Convert an input file of {csv, parquet, json} format to {csv, parquet, json} format using Polars"
VERBOSE=false
usage() {
echo "Usage: $0 <input_file> -f <output_format> [-o <output_file>] [-v]"
echo " <input_file>: Path to the input file"
@Nasdin
Nasdin / used_bike_scrape_and_analyze.py
Last active August 13, 2024 13:39
Analyze Used Bikes from SG Bike Mart
import calendar
import re
from datetime import datetime
from urllib.parse import urljoin, urlparse, parse_qs
import pandas as pd
import requests
from bs4 import BeautifulSoup
USED_BIKE_SEARCH_URL = "https://sgbikemart.com.sg/listing/usedbikes/listing/?bike_model=Honda+MSX125&bike_type=&price_from=&price_to=&license_class=2B&reg_year_from=1970&reg_year_to=2024&monthly_from=&monthly_to=&user=&status=10&category="
@Nasdin
Nasdin / sum.py
Last active September 19, 2021 16:43
How to sum, without using + or - in Python
def sum_bits(larger, smaller):
""" Sum but using bits, only positive numbers"""
top_down_sum = larger ^ smaller
carry = (larger & smaller) << 1
if carry > 0:
return sum_bits(top_down_sum, carry)
return top_down_sum
def sub_bits(larger, smaller):
@Nasdin
Nasdin / hashmap.py
Created April 17, 2020 10:16
Hashmap in Python without dictionaries
# Implement a hashmap in Python without using a dictionary
# A simple toy example, look to improve when the hashes start clashing with each other either by adapting a dynamic size
# Or using a better hash function
class HashMap(object):
def __init__(self, size, hash_function):
self.array = [None] * size
self.size = size
@Nasdin
Nasdin / image_to_grid_knitting_pattern.py
Last active October 28, 2019 01:41
Takes an image and converts it to a grid knitting pattern as an excel file with colors and labelled with numbers. Arguments are 1. image path 2. desired height of grid 3. excel output path
import sys
import cv2
import numpy as np
import xlsxwriter
if len(sys.argv) != 4:
print("That's not how it works")
sys.exit(404)
@Nasdin
Nasdin / mnist_hyperopt_talos.py
Last active February 11, 2019 03:30
Keras Neural Network on MNIST HyperParameter Tuning example
"""
Keras and talos using Mnist as toy example
"""
import talos
from keras import Sequential
from keras.activations import relu, elu, tanh, softmax
from keras.layers import Conv2D, BatchNormalization, MaxPool2D, Flatten, Dense, Dropout
from keras.losses import logcosh, binary_crossentropy
from keras.optimizers import Adam, RMSprop
@Nasdin
Nasdin / caesar_cypher.py
Last active February 2, 2019 12:50
Caesar Cypher in Python dict compre
from functools import partial
alphabet = 'abcdefghijklmnopqrstuvwxyz'
alphabet_dict = {letter: order + 1 for order, letter in enumerate(alphabet)}
alphabet_dict_reversed = {order: letter for letter, order in alphabet_dict.items()}
def encrypt_text(text: str, shift: int, decrypt: bool = False):
shift_n = (shift * (-1 if decrypt else 1)) # Useful line to switch to decryption
encrypted_text = [
@Nasdin
Nasdin / string_to_csv.py
Last active February 11, 2019 04:06
Fastest String to CSV parser
import time
import pandas as pd
def string_to_csv(string: str, csv_output_name: str):
row_gen = (x.split("=") for x in string.split(';'))
df = pd.DataFrame(row_gen)
df['index'] = df.groupby(0).cumcount()
@Nasdin
Nasdin / BMICalculator.py
Last active February 11, 2019 04:08
Python simple module and class for calculating BMI in inches and pounds + Track player's and team roster's stats.
class Player(object):
# Class Attributes, tracking all players
players = {}
id_player_names = {} # Just something to help you track id to players
last_id = 0
def __init__(self, name=None, height = 0, weight = 0 , bmi =0):
# Player attributes, tracking a player's stats