Skip to content

Instantly share code, notes, and snippets.

View dnmellen's full-sized avatar
🐙

Diego Navarro dnmellen

🐙
  • Madrid
View GitHub Profile
@dnmellen
dnmellen / hipstergram.sh
Last active January 27, 2022 22:40
Hipstergram: Bash script to convert images to a square aspect ratio with white background (Uses ImageMagick)
#!/bin/bash
for file in `ls -1`; do
echo $file;
convert -background white -gravity center $file -resize 1080x1080 -extent 1080x1080 i-$file
convert $file -crop 2x1@ i-$file
done
@dnmellen
dnmellen / brokers.py
Created October 20, 2020 10:33
Improved EagerBroker for dramatiq
from dramatiq.brokers.stub import StubBroker
class EagerBroker(StubBroker):
"""Used by tests to simulate CELERY_ALWAYS_EAGER behavior.
https://github.com/Bogdanp/dramatiq/issues/195
Modified by @dnmellen to support pipelines and groups
"""
def process_message(self, message):
@dnmellen
dnmellen / models.py
Last active October 3, 2021 04:14
DynamoDB mixin for Django models: Mix Django fields and DynamoDB fields in your models!
import uuid
import boto3
from decimal import Decimal
from functools import partial
from django.db import models
from django.conf import settings
class UUIDModel(models.Model):
"""
@dnmellen
dnmellen / worker_function.py
Created January 31, 2017 15:07
AWS Lambda function that performs an ssh command through a bastion server to another server. The function will be triggered by a Cloudwatch Alarm
import json
import boto3
import paramiko
def worker_handler(event, context):
ALLOWED_HOSTS = [
'host1',
'host2,
from django import template
register = template.Library()
@register.filter
def cool_number(value, num_decimals=2):
"""
Django template filter to convert regular numbers to a
cool format (ie: 2K, 434.4K, 33M...)
@dnmellen
dnmellen / seconds.py
Created January 15, 2015 12:03
Seconds to the next day in python
from datetime import datetime, timedelta
print (datetime(*(datetime.now() + timedelta(days=1)).timetuple()[:3]) - datetime.now()).seconds
@dnmellen
dnmellen / timeout_decorator.py
Created January 23, 2014 15:39
Threaded timeout Python decorator
import threading
import logging
from functools import wraps
logger = logging.getLogger(__name__)
def timeout(secs=None):
def my_decorator(target, *args, **kwargs):
@dnmellen
dnmellen / redis_listener.py
Created January 15, 2014 15:37
Listen Redis pubs with python3 and asyncio
import asyncio
import redis
@asyncio.coroutine
def listener(redis_conn, channels):
pubsub = redis_conn.pubsub()
pubsub.subscribe(channels)
print('Listening redis...')
@dnmellen
dnmellen / ocrshot.py
Last active March 2, 2023 11:06
Takes an screenshot (the user draws a rectangle to select the interesting area), scans the resulting image and copy the text to clipboard
#!/usr/bin/python -tt
"""
Takes an screenshot (the user draws a rectangle to select the interesting area), scans the resulting image and copy the text to clipboard
- scrot (http://en.wikipedia.org/wiki/Scrot)
- readbot (`pip install readbot`)
- pyperclip (`pip install pyperclip`)
"""
@dnmellen
dnmellen / postinstall.md
Last active December 30, 2015 08:49
Post installation PostgresSQL

Useful things to do after postgresql installation

# su - postgre
$ createuser youruser
$ psql
postgres=# ALTER USER youruser WITH PASSWORD 'yoursecretpassword';
postgres=# CREATE DATABASE yourdb owner youruser;