Skip to content

Instantly share code, notes, and snippets.

View danielgoncalves's full-sized avatar

Daniel Gonçalves danielgoncalves

View GitHub Profile
@nicolaiarocci
nicolaiarocci / validate_objects.py
Created January 5, 2015 10:09
Validating complex user objects with Cerberus
import copy
from cerberus import Validator
from cerberus import errors
from collections import Mapping, Sequence
class ObjectValidator(Validator):
def __init__(self, *args, **kwargs):
@jsbueno
jsbueno / lazyquicksort.py
Last active October 15, 2020 03:54
Lazy sorter - an iterator that yields items in sorted order, lazily
from itertools import tee
def lazy_sort(v, key=lambda x: x):
v = iter(v)
try:
pivot = next(v)
except StopIteration:
return
v_pre, v_pos = tee(v)
yield from lazy_sort((item for item in v_pre if key(item) < key(pivot)), key)
@romannurik
romannurik / SwipeDismissListViewTouchListener.java
Last active May 1, 2021 10:16
**BETA** Android 4.0-style "Swipe to Dismiss" sample code
Moved to
https://github.com/romannurik/android-swipetodismiss
import concurrent.futures
import os
import re
from timeit import timeit
import requests
from tqdm import tqdm
URLS = 'urls'
@mikeckennedy
mikeckennedy / watch_this.py
Last active June 30, 2021 06:30
Add C# += / -= event subscriptions to Python using the Events package
from events import Events # Note you must pip install events
class Person:
def __init__(self, name: str, age: int, city: str):
self.__events = Events(('on_name_changed', 'on_age_changed', 'on_location_changed'))
self.on_name_changed = self.__events.on_name_changed
self.on_age_changed = self.__events.on_age_changed
self.on_location_changed = self.__events.on_location_changed
@senko
senko / maybe.py
Last active January 28, 2022 09:16
A Pythonic implementation of the Maybe monad
# maybe.py - a Pythonic implementation of the Maybe monad
# Copyright (C) 2014. Senko Rasic <senko.rasic@goodcode.io>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
@theburningmonk
theburningmonk / singleton.dart
Last active September 2, 2022 01:40
Using Dart's factory constructor feature to implement the singleton pattern
class MyClass {
static final MyClass _singleton = new MyClass._internal();
factory MyClass() {
return _singleton;
}
MyClass._internal() {
... // initialization logic here
}
@henriquebastos
henriquebastos / secret_gen.py
Created December 23, 2015 13:50
SECRET_KEY generator.
#!/usr/bin/env python
"""
Django SECRET_KEY generator.
"""
from django.utils.crypto import get_random_string
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
print(get_random_string(50, chars))
@rochacbruno
rochacbruno / mainpython.md
Last active July 5, 2023 10:56
Use of __main__.py

The use of __main__.py to create executables

myprojectfolder/
    |_ __main__.py
    |_ __init__.py

Being __main__.py:

print("Hello")

@emmanueltissera
emmanueltissera / git-apply-patch.md
Created September 13, 2017 02:34
Generate a git patch for a specific commit

Creating the patch

git format-patch -1 <sha>
OR
git format-patch -1 HEAD

Applying the patch

git apply --stat file.patch # show stats.
git apply --check file.patch # check for error before applying