Skip to content

Instantly share code, notes, and snippets.

View carlos-jenkins's full-sized avatar

Carlos Jenkins carlos-jenkins

View GitHub Profile
@carlos-jenkins
carlos-jenkins / mkstemp.py
Created April 13, 2016 00:39
Creating named temporal files in Python
from os import fdopen
from json import dumps
from tempfile import mkstemp
payload = {'one': 1}
osfd, tmpfile = mkstemp()
with fdopen(osfd, 'w') as pf:
pf.write(dumps(payload))
@carlos-jenkins
carlos-jenkins / editors.rst
Last active March 7, 2019 19:50
Sublime Text and Atom Setup for Ubuntu
@carlos-jenkins
carlos-jenkins / test_metaclass.py
Created December 2, 2015 23:24
Use metaclass to wrap methods in a object using a decorator.
from six import add_metaclass
def print_decorator(func):
def replacement(*args, **kwargs):
result = func(*args, **kwargs)
print('The result of the function is: {}'.format(result))
return result
return replacement
@carlos-jenkins
carlos-jenkins / submodules.py
Created September 15, 2015 10:52
Find all submodules given a module root name
def get_modules(name):
"""
Get a list of all submodules given a module root name.
"""
from pkgutil import walk_packages
module = __import__(name)
# Handle modules installed in top-level
if not hasattr(module, '__path__'):
return [(name, False)]
submodules = [(name, True)]
@carlos-jenkins
carlos-jenkins / secret_key.py
Created August 27, 2015 00:24
Generate Django SECRET_KEY
from django.utils.crypto import get_random_string
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
if __name__ == '__main__':
print(get_random_string(50, chars))
@carlos-jenkins
carlos-jenkins / add_digits.py
Last active September 15, 2015 10:52
Using timeit
def add_digits_optimized(num):
"""
Recursively sum al digits of a number until result is one digit.
Optimized version using simple integer logic.
:param int num: Number to sum all it's digits.
:rtype: int
:return: The recusive sum of all digits.
"""
result = 0
@carlos-jenkins
carlos-jenkins / hexdump.py
Created June 4, 2015 00:06
Hexdump implementation in Python 2.7 and 3.
# -*- coding:utf-8 -*-
#
# Copyright (C) 2015 Carlos Jenkins <[email protected]>
#
# 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.
#
# This program is distributed in the hope that it will be useful,
@carlos-jenkins
carlos-jenkins / custom_elf_section.c
Created May 22, 2015 00:31
C magic to put, and then iterate, some symbols in an ELF custom section.
#include <stdio.h>
//
// Define a thing to store in sections
//
struct mything {
const char* name;
int num;
};
@carlos-jenkins
carlos-jenkins / register.c
Last active August 29, 2015 14:20
Example of a register structure that can be memory mapped
// Example of a memory mapped register structure:
//
// gcc register.c -o register
// ./register
//
// This assumes register size is 8bits.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>