Skip to content

Instantly share code, notes, and snippets.

View aj07mm's full-sized avatar

Julio Marins aj07mm

View GitHub Profile
@aj07mm
aj07mm / gist:15cb83ac62b7bc6771b28895cd2a2226
Created March 28, 2019 00:22 — forked from pazdera/gist:1098129
Singleton example in Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Example of Singleton design pattern
# Copyright (C) 2011 Radek Pazdera
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
@aj07mm
aj07mm / requests-oauth2.py
Created January 15, 2019 20:45 — forked from ymotongpoo/requests-oauth2.py
Google API OAuth 2.0 Authorization Sample in Python
# -*- coding: utf-8 -*-
"""
This scripts reqire a third party module 'requests'.
You can get it from PyPI, i.e. you can install it using
easy_install or pip.
http://docs.python-requests.org/en/v0.10.4/
Original source code is written by shin1ogawa, which is in Java.

Configuring Nginx to serve SSL content is straight forward, once you have your certificate and key ready:

server { 
    listen 443 default ssl;
    root /path/to/source;
    server_name mydomain;

    ssl_certificate      /path/to/cert;
    ssl_certificate_key  /path/to/key;
@aj07mm
aj07mm / gist:c746d8ee0329e48acb62a211b955b1d0
Created October 18, 2018 16:32 — forked from vpetro/gist:1174019
Return multiple items from a mocked function with Python's mock.
import mock
def returnList(items):
def func():
for item in items:
yield item
yield mock.DEFAULT
generator = func()
@aj07mm
aj07mm / port_test.py
Created October 10, 2018 16:44
Python script to test open outgoing ports from local network
#!/usr/bin/env python
"""Port test
Python script to test open outgoing ports from local network
"""
import socket
for port in range(1,65000):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@aj07mm
aj07mm / login_required.py
Created August 24, 2018 01:19 — forked from robgolding/login_required.py
Django Class-Based View Mixins: Part 1
class LoginRequiredMixin(object):
"""
View mixin which requires that the user is authenticated.
"""
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(LoginRequiredMixin, self).dispatch(
self, request, *args, **kwargs)
@aj07mm
aj07mm / middleware.py
Created August 17, 2018 20:48 — forked from j4mie/middleware.py
Django middleware to log the total number of queries run and query time for every request
from django.db import connection
from django.utils.log import getLogger
logger = getLogger(__name__)
class QueryCountDebugMiddleware(object):
"""
This middleware will log the number of queries run
and the total time taken for each request (with a
status code of 200). It does not currently support
@aj07mm
aj07mm / Regex Patterns
Created August 14, 2018 00:46 — forked from cozingo/Regex Patterns
Regex regex
\d - digit [0,9]
\w - digit, ASCII letter or underscore
\s - whitespace, i.e. space, tab, newline(\n), carriage return(\r), vertical tab(Ctrl + K) <-vertical tab not useful anymore
\D == [^\d] - 1 character which is not digit
\W == [^\w] - 1 character which is not ASCII letter, digit and underscore
\S == [^\s] - 1 character which is not type of whitespace
---------------------------------------------------------------------------------------
@aj07mm
aj07mm / WhatHappenedToMyDataInRedis.md
Created June 22, 2018 01:33 — forked from JonCole/WhatHappenedToMyDataInRedis.md
What happened to my data in Redis?

What happened to my data in Redis?

This post is a FAQ for Redis, when users don’t see the data they stored in Redis.

As per the Redis project, Redis is described as an in-memory data structure store. If you are using Redis as an LRU cache, you can see following recommendation from the Redis docs: Using Redis as an LRU cache

Lately, I have received multiple questions about when Redis will lose data. This data loss can be as simple as a few keys disappearing unexpectedly or complete loss of all data in Redis. Below I will talk about the most common causes as well as a few rare cases where this can happen.

Note: These scenarios are common to all Redis hosting environments, including self-hosting Redis in your own data center.

@aj07mm
aj07mm / savejsoninredis.py
Created May 18, 2018 03:05 — forked from swdevbali/savejsoninredis.py
Simply json.dumps() => save to redis => redis.get => json.loads() => Python dict with proper data type created
__author__ = 'ekowibowo'
import json
import os
import redis
REDIS_HOST = os.getenv('SS_ABTEST_REDIS_HOST', '192.168.59.103')
r = redis.StrictRedis(host=REDIS_HOST)
for k in r.keys('abtest:experiments:*'):
r.delete(k)