Skip to content

Instantly share code, notes, and snippets.

@DevanR
DevanR / csv_to_dict.py
Last active January 7, 2019 09:49
Read csv into dict
class ExportOrder(SpecificRoleRequiredMixin, View):
allowed_roles = [SALES_ADMIN, SALES_USER]
def get(self, request, *args, **kwargs):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="wholesale_billing.csv"'
orders = Order.objects.all().order_by('created_on')
field_mapping = OrderedDict([
# CSV header: Order model field,
@DevanR
DevanR / tuple_to_dict.py
Created September 12, 2018 20:39
Converting tuples to dicts
tuple = ('71.201.176.194', '-', '-', '26/Feb/2008:10:30:08 -0600',
'GET', '/ply/ply.html', 'HTTP/1.1', '200', '97238')
colnames = ('host','referrer','user','datetime',
'method','request','proto','status','bytes')
dict = (dict(zip(colnames,t)) for t in tuples)
@DevanR
DevanR / table_size.sql
Created May 25, 2018 12:00
Check table sizes in database
SELECT table_name, pg_relation_size(table_name), pg_size_pretty(pg_relation_size(table_name))
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER by 2 DESC;
@DevanR
DevanR / database_size.sql
Last active May 25, 2018 12:01
Check database size
SELECT pg_size_pretty(pg_database_size('dbname'));
@DevanR
DevanR / post-commit.sample
Created January 3, 2018 12:25
post-commit for local git repository
#!/bin/bash
/usr/bin/curl http://localhost:8080/git/notifyCommit?url=file:///Users/dre09/Workspace/git-bskyb-com/bms
@DevanR
DevanR / post-commit.sample
Created January 3, 2018 11:55
post-commit git example for remote deployment
#!/bin/bash
#ref:https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "Master ref received. Deploying master branch to production..."
git --work-tree=/var/www/html --git-dir=/home/demo/proj checkout -f
@DevanR
DevanR / generate_random_strings.py
Created December 12, 2017 16:05
Function to generate random strings
def generate_random_string(chars=string.ascii_uppercase + string.digits):
"""
Generates a random string for input testing.
Ref: https://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits-in-python
"""
size=random.randint(1, 10)
return ''.join(random.choice(chars) for _ in range(size))
@DevanR
DevanR / drop_all_db.sql
Created December 7, 2017 23:10
Drop all Postgres tables
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;
COMMENT ON SCHEMA public IS 'standard public schema';
@DevanR
DevanR / postgres.sh
Last active December 8, 2017 10:43
Dump and Restore Postres tables
# Dump all table beginning with wholesale from metro
pg_dump -c -t "^wholesale*" -f wholesale.sql metro
# Specific db and user
/usr/pgsql-9.4/bin/pg_dump -c -d<database> -U<user> -t "^wholesale*" -f wholesale.backup
# Clear table in target database
delete from public.wholesale_bearerrate;
# Load data in postgres database
psql metro < wholesale.sql

Keybase proof

I hereby claim:

  • I am devanr on github.
  • I am devan (https://keybase.io/devan) on keybase.
  • I have a public key whose fingerprint is 587D 62C5 69A8 8E79 A983 81B0 5D3B 86BC F834 F237

To claim this, I am signing this object: