Skip to content

Instantly share code, notes, and snippets.

View chandra-prakash-meghwal's full-sized avatar

Chandra Prakash Meghwal chandra-prakash-meghwal

View GitHub Profile
@chandra-prakash-meghwal
chandra-prakash-meghwal / timestamp_to_utc_datetime.py
Created October 18, 2022 08:57
Get Date Time in UTC from the given timestamp
import datetime
def get_utc_datetime_from_timestamp(timestamp):
"""
datetime.datetime.utcfromtimestamp returns utc date time
whereas datetime.datetime.fromtimestamp returns local (system) date time
"""
return str(datetime.datetime.utcfromtimestamp(int(timestamp)))
@chandra-prakash-meghwal
chandra-prakash-meghwal / connect_db_using_asyncpg.py
Created November 1, 2022 08:36
connect to postgres database using asyncpg
import asyncpg
db_config = {
'user': 'postgres', # name of db user
'password': 'my db password', # db password
'database': 'my_db', # database name
'host': 'localhost' # or remote db ip address
}
async def custom_query(query_string):
@chandra-prakash-meghwal
chandra-prakash-meghwal / postgres_db_tables_create.sql
Created November 1, 2022 08:49
an sql query example to create a table with column having data type bigserial, integer, bigint, boolean, varchar and specified primary key
/*create a sample table with table name user_credits and columns id primary key, username, credit_assigned, credit_expire_time, is_user_kyced*/
CREATE TABLE user_credits (id BIGSERIAL PRIMARY KEY, username VARCHAR(25), credit_assigned integer, credit_expire_time bigint, is_user_kyced boolean);
/*
bigserial 8 bytes large autoincrementing integer 1 to 9223372036854775807
integer 4 bytes typical choice for integer -2147483648 to +2147483647
bigint 8 bytes large-range integer -9223372036854775808 to +9223372036854775807
boolean 1 byte state of true or false
VARCHAR(size) A VARIABLE length string (can contain letters, numbers, and special characters). The size parameter specifies the maximum string length in characters - can be from 0 to 65535
*/
@chandra-prakash-meghwal
chandra-prakash-meghwal / lambda_function.py
Last active December 7, 2022 10:14
Python handler for AWS Lambda function
import json
def lambda_handler(event, context):
# TODO implement
user_name = event.get('name')
message = 'Hello {}'.format(user_name)
return {
'statusCode': 200,
'message': message,
'body': json.dumps('This message is from Lambda!')
@chandra-prakash-meghwal
chandra-prakash-meghwal / datetime_from_string.py
Created December 6, 2022 05:58
To get python datetime object from string of datetime for the given format
import datetime
def get_datetime_from_string(datetime_string, datetime_format='%Y-%m-%d %H:%M:%S'):
"""
if datetime string includes milliseconds (e.g. '2022-12-06 11:22:23.222')
then the datetime format will be '%Y-%m-%d %H:%M:%S.%f' to include fraction
"""
return datetime.datetime.strptime(datetime_string, datetime_format)
@chandra-prakash-meghwal
chandra-prakash-meghwal / mass_unfollow_linkedin.js
Created December 6, 2022 07:45
Mass Unfollow on Linkedin
(() => {
let count = 0;
function getAllButtons() {
return document.querySelectorAll('button.is-following') || [];
}
async function unfollowAll() {
const buttons = getAllButtons();
for (let button of buttons) {
count = count + 1;
@chandra-prakash-meghwal
chandra-prakash-meghwal / add_sample_customers_data.sql
Created December 7, 2022 10:03
Add sample customers to customers db table in mysql
drop procedure if exists populate_customers;
delimiter //
create procedure populate_customers (in num int)
begin
declare i int default 0;
while i < num do
insert into customers (customer_id, name, email) values ((num*10)+i, concat('name', i), concat('email', i));
set i = i + 1;
end while;
end //
@chandra-prakash-meghwal
chandra-prakash-meghwal / insert_into_one_table_from_another_plus_extra.sql
Created December 7, 2022 10:10
insert into one table with values some columns from another table and some other data in mysql
insert into customer_balance_logs (customer_id, credit, reason) select id, 5, 'x-mas credit' from customers;
@chandra-prakash-meghwal
chandra-prakash-meghwal / run_sql_script_in_mysql_cli
Created December 16, 2022 06:24
How to run SQL script in MySQL command line? use source then file path with filename to run script from the file
mysql> source <path to the file alongwith filename>;
@chandra-prakash-meghwal
chandra-prakash-meghwal / prepare-commit-msg
Last active January 10, 2023 04:38
Automatically add git branch name to the commit message. Add the below file to .git/hooks/ directory and make it executable with the command $ chmod +x prepare-commit-msg, the below code excludes branch names master develop test
#!/bin/bash
# This way you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"