Skip to content

Instantly share code, notes, and snippets.

View samredai's full-sized avatar

Samuel Redai samredai

View GitHub Profile
@samredai
samredai / create_a_procedure.sql
Created December 10, 2018 00:05
MySQL: Create a stored procedure
delimiter #
DROP PROCEDURE IF EXISTS dataForSomeID;
CREATE PROCEDURE `dataForSomeID`(IN needThisID VARCHAR(20))
BEGIN
SELECT *
FROM sometable
LEFT JOIN anothertable
ON sometable.id = anothertable.id
WHERE id=needThisID;
@samredai
samredai / delete_random_90perc_rows.sql
Created December 10, 2018 00:07
MySQL: Randomly delete a specified percentage of records from a table (90% in this example)
DELETE FROM tablename
WHERE idcolumn IN (
SELECT idcolumn
FROM (SELECT DISTINCT idcolumn FROM tablename) x
WHERE rand() <= 0.9
)
@samredai
samredai / create_index_on_table.sql
Created December 10, 2018 03:52
MySQL: Create an Index on a column or list of columns in a MySQL table
CREATE INDEX index_name ON table_name (column_name_or_list)
@samredai
samredai / execute_py_file_within_shell.py
Last active December 11, 2018 15:18
Execute an entire Python file within your current python shell
exec(open("./filename").read())
@samredai
samredai / base64encode_decode.py
Created December 11, 2018 02:10
Python: Encode a string into bytes using base64, then decode it and convert it back to a string
import base64
base64.b64encode(b'Encode this string.')
#b'RW5jb2RlIHRoaXMgc3RyaW5nLg=='
decodedBytes = base64.b64decode(b'RW5jb2RlIHRoaXMgc3RyaW5nLg==')
print(decodedBytes)
#b'Encode this string.'
decodedString = decodedBytes.decode('utf-8')
@samredai
samredai / example_data_from_url.json
Last active December 11, 2018 02:25
Javascript: Render a JQuery DataTable using JSON data from an API
{
"data": [
{
"Fieldname1": "1",
"Fieldname2": "Lebron James",
"Fieldname3": "Basketball",
"Fieldname4": "23",
"Fieldname5": "Cleveland",
"Fieldname6": "SF",
"Fieldname7": "2",
@samredai
samredai / mysql_procedure_to_json.py
Created December 11, 2018 03:31
Python: Function to call a MySQL stored procedure and return the results as JSON nested within "data"
import simplejson as json
import MySQLdb
#Better practice is to load and decrypt these values from a file
dbname = 'databaseName'
dbuser = 'username'
dbpass = 'password'
dbhost = 'host
def procedureToJSON(procedureName, inputs=''):
@samredai
samredai / vi_find_and_replace_everywhere
Last active February 22, 2019 02:43
Vi: Find and Replace within entire file
:%s/replaceThis/withThis/g
@samredai
samredai / reset_local_to_remote_branch
Created December 11, 2018 16:09
Setting local Git repo to match remote branch
git fetch origin
git reset --hard origin/master
#replace master with any other remote branch
@samredai
samredai / load_encoded_password.sh
Created December 11, 2018 19:35
Linux: Load password from encoded file to use in shell command
source /path/to/encoded/pwd/.en
uenc=$(eval echo ${echo} | base64 --decode)