Skip to content

Instantly share code, notes, and snippets.

View aruseni's full-sized avatar
🟢
Lead Python Software Engineer

aruseni

🟢
Lead Python Software Engineer
View GitHub Profile
@aruseni
aruseni / deep_get.py
Created April 4, 2019 09:32
Gets a value following the specified path, if such value exists
def deep_get(dictionary, keys, default=None):
"""
Gets a value following the specified path, if it exists.
The keys are dot-separated.
Example:
deep_get({'most_visited': {'url': 'https://test.com/'}}, 'most_visited.url')
"""
@aruseni
aruseni / replace_text_nodes.py
Created September 19, 2018 14:47
A Python script that replaces all text nodes inside a `template` element with [TXT], used with Vue templates
import argparse
parser = argparse.ArgumentParser(description='Replace text nodes')
parser.add_argument('file', type=str)
args = parser.parse_args()
html = open(args.file).read()
def replace(content):
@aruseni
aruseni / fields.py
Created February 8, 2017 16:23
This is a TextField-based field with TextInput widget. This can be used instead of a CharField whenever you do not want to restrict the length of the content at the DB level (see http://stackoverflow.com/questions/4848964/postgresql-difference-between-text-and-varchar-character-varying for more info).
from django.db import models
from django import forms
class TextInputTextField(models.TextField):
def formfield(self, **kwargs):
kwargs.update({
"widget": forms.TextInput
})
@aruseni
aruseni / admin.py
Last active October 19, 2017 18:00
Substituting Django User model
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import ugettext_lazy as _
from .forms import UserChangeForm, UserCreationForm
from .models import User
@aruseni
aruseni / incr_count_mixin.py
Last active April 13, 2016 09:58
This Django model mixin adds an incr_count method that increments the “count” field in one SQL query (very useful if same object can be updated simultaneously and you want the count field to have correct values).
class CountIncrMethodMixin(models.Model):
def incr_count(self):
model = type(self)
model.objects.filter(id=self.id).update(
count=models.F("count")+1
)
@aruseni
aruseni / checkboxes.html
Created October 16, 2014 11:58
Chain set checkboxes in group when “Select” all checkbox is changed/“Select all” checkbox when checkboxes in group are changed.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Coffee shop</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="checkboxes.js"></script>
</head>
<body>
<p>What topping would you like?</p>
@aruseni
aruseni / pnm2tiff.sh
Created September 13, 2014 12:59
Converts every .pnm file in the current working directory to .tiff. The numbers from 01 will be used for naming the .tiff files.
i=1 ; for pnmfile in *.pnm ; do tifffile=$(printf %02d.tiff $i) ; convert -compress lzw $pnmfile $tifffile ; i=$((i+1)) ; done
@aruseni
aruseni / move_databases_to_rds.sh
Created April 29, 2014 01:23
Transfer local MySQL databases to Amazon RDS
#!/bin/bash
DATABASES="cooldb nicedb epicdb"
LOCAL_PWD="Tr0ub4dor&3"
RDS_PWD="correcthorsebatterystaple"
RDS_HOST="blahblahblah.rds.amazonaws.com"
for DB in $DATABASES ; do
echo "Moving $DB"
echo "create database $DB;" | mysql --host=$RDS_HOST --user=root --password=$RDS_PWD
@aruseni
aruseni / nginx.conf
Created April 26, 2014 21:30
Nginx Maintenance Message
location /maintenance/ {
alias /home/web/maintenance/;
}
if ($uri !~ ^/maintenance) {
return 302 http://mywebsite.com/maintenance/;
}
@aruseni
aruseni / get_credentials.py
Created April 23, 2014 19:19
Requirements: BeautifulSoup. progressbar
import sys
import httplib2
import urllib
import hashlib
import re
import itertools
from BeautifulSoup import BeautifulSoup
import progressbar