Skip to content

Instantly share code, notes, and snippets.

@r4sn4
r4sn4 / Parse string to datetime Python
Last active October 16, 2018 03:24
Parse string to datetime Python
from datetime import datetime
date = '2018-09-13T18:00:00.000Z'
parsed_date = datetime.strptime(time, '%Y-%m-%dT%H:%M:%S.000Z')
print(parsed_date)
# parsed_date is datetime.datetime(2018, 9, 13, 18, 0)
@r4sn4
r4sn4 / MySQl connector in Python
Last active October 16, 2018 03:24
MySQl connector in Python
import mysql.connector
from openpyxl import Workbook
def main():
# Connect to DB -----------------------------------------------------------
db = mysql.connector.connect( user='usr', password='pwd', host='hostip')
cur = db.cursor()
# database name
#install virtual environment
pip install virtualenv
#Create the virtual environment
virtualenv myenv
#Activate the virtual environment
#On Mac OS / Linux
source myenv/bin/activate
@r4sn4
r4sn4 / Insert reminder in Google Calender
Created October 16, 2018 08:12
Insert reminder in Google Calender using google Calender API
from __future__ import print_function
import datetime
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# If modifying these scopes, delete the file token.json.
SCOPES = 'https://www.googleapis.com/auth/calendar'
def set_event_calender(summary, time):
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r or -R is recursive,
-n is line number, and
-w stands for match the whole word.
-l (lower-case L) file name of matching files.
#regex to find url in text
r"(https?:\/\/)(\s)?(www\.)?(\s?)(\w+\.)*([\w\-\s]+\/)*([\w-]+)\/?"
# importing module
import logging
# Create and configure logger
logging.basicConfig(filename="newfile.log",
format='%(asctime)s - %(levelname)s: %(message)s',
filemode='w')
# Creating an object
logger = logging.getLogger()
#read csv as data frame
df = pd.read_csv(file_path, header=0)
#write dataframe to csv file
df.to_csv(file_name, sep='\t', encoding='utf-8')
#create zero filled data frame with fixed rows and columns
df_final = pd.DataFrame(0, index=range(5), columns=range(3))
#insert column in dataframe at fixed index
# read train data from csv
train = pd.read_csv(file_path, header=0, sep='\t')
# drop identifier columns and convert to np.array
X = np.array(train.drop(['identifier_col'], 1).astype(float))
y = np.array(train['identifier_col'])
#apply kmeans
#determine k range
k_range = range(1,6)
#fit the model for each n clusters = k
k_means_var = [KMeans(n_clusters=k).fit(train) for k in k_range]
#pull out the cluster centers for each model
centroids = [V.cluster_centers_ for V in k_means_var]
## calculate euclidean distance from each point to each cluster center