Skip to content

Instantly share code, notes, and snippets.

View ra312's full-sized avatar
🎯
Focusing

ra312 ra312

🎯
Focusing
View GitHub Profile
@ra312
ra312 / create_timezone_aware_datetime_objects.py
Created July 7, 2022 06:55
create_timezone_aware_datetime_objects
from datetime import datetime
str_datetime = "2022-04-11"
_until_time_created_unaware = datetime.strptime(str_datetime, "%Y-%M-%d") if str_datetime!= "" else datetime.now()
until_datetime_created_unaware = _until_time_created_unaware.replace(hour=23, minute=59, second=59)
until_datetime_created_aware = pytz.utc.localize(until_datetime_created_unaware)
@ra312
ra312 / FNet_demo.py
Created June 19, 2021 14:09
a sample FNet code
import torch
from torch import nn
from labml import experiment
from labml.configs import option
from labml_helpers.module import Module
from labml_nn.experiments.nlp_classification import NLPClassificationConfigs
from labml_nn.transformers import Encoder
from labml_nn.transformers import TransformerConfigs
@ra312
ra312 / fix_encoding.py
Created August 7, 2020 05:22
fix text file encoding
import chardet
import codecs
infile = 'unreadable.txt'
outfile = 'readable.txt'
with open(infile,'rb') as raw_file:
result = chardet.detect(raw_file)
char_enc = result['encoding']
with open(outfile, 'r', charenc):
read_as_utf = f.read()
out = codecs.open(outfile, 'w', 'utf-8')
@ra312
ra312 / label_prop_plot_roc_auc_score.py
Created July 31, 2020 06:25
label_prop_plot_roc_auc_score
import matplotlib.pyplot as plt
def label_prop_test(model, kernel, params_list, X_train, X_test, y_train, y_test):
plt.figure(figsize=(20,10))
n, g = 0, 0
roc_scores = []
if kernel == 'rbf':
for g in params_list:
lp = model(kernel=kernel, n_neighbors=n, gamma=g, max_iter=100000, tol=0.0001)
lp.fit(X_train, y_train)
roc_scores.append(roc_auc_score(y_test, lp.predict_proba(X_test), multi_class='ovo'))
# This is an application transferring csv data into database
# We choose a NoSQL database MongoDB since we have to store data and the logs of data GET requests# We prefer to choose PostgreSQL rather than MySQL(purely relation) or SQLite (no concurrency support)
import json
import csv
import pandas as pd
import os
from pymongo import MongoClient
cwd = os.path.dirname(__file__)
csvpath=os.path.join(cwd,'task_data.csv')
@ra312
ra312 / bitwise_pairs.py
Created June 6, 2020 14:15
counting pairs with bitwise product
import itertools
def countPairs(arr):
# Write your code here
count = 0
for i, j in itertools.combinations(arr, 2):
v = i & j
if v != 0 :
if (v % 2 == 0) | (v == 1):
count+=1
return count
@ra312
ra312 / marketing_costs.py
Created June 6, 2020 10:17
correctly solves example case, but linear codependency failed
import numpy as np
from sklearn import linear_model
def desired_marketing_expenditure(marketing_expenditure, units_sold, desired_units_sold):
"""
:param marketing_expenditure: (list) A list of integers with the expenditure for each previous campaign.
:param units_sold: (list) A list of integers with the number of units sold for each previous campaign.
:param desired_units_sold: (integer) Target number of units to sell in the new campaign.
:returns: (float) Required amount of money to be invested.
"""
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
@ra312
ra312 / fetch_chrome_history.py
Last active May 21, 2020 15:32
raise my browsing self-awareness
import os
import sqlite3
import operator
from collections import OrderedDict
#path to user's history database (Chrome)
data_path = os.path.expanduser('~')+"/Library/Application Support/Google/Chrome/Default"
files = os.listdir(data_path)
history_db = os.path.join(data_path, 'history')
#querying the db
CREATE TABLE accounts(
JE_ID serial PRIMARY KEY,
ACCOUNT_DR integer NOT NULL,
ACCOUNT_CR integer NOT NULL,
SUMM integer NOT NULL,
REVERSED integer NOT NULL,
USER_ID integer NOT NULL,
DATA integer NOT NULL,
DATE_DOC integer NOT NULL,
APPROVER_ID varchar(256) NOT NULL,