Skip to content

Instantly share code, notes, and snippets.

#!/bin/sh
#Deletes emails older than this number of days
OLD_THAN_DAYS=30
if [ ! -d /home/root/Maildir ]; then
continue;
fi
for file in `find /home/root/Maildir -mtime +${OLD_THAN_DAYS} | grep -E '/cur/|/new/'`; do
{
@ferminhg
ferminhg / example_aging_data.php
Last active April 5, 2019 11:08
Aging data explain
<?php
// Obtenemos todos los productos con stock > 0
// creo que esto pueda que tenga un pequeño bug y no se si lo tiraria a todos lo productos
class ProductRepository implements ProductRepositoryInterface
{
/**
* @return ArrayCollection
*/
public function get()
@ferminhg
ferminhg / mergin_two_dicts.py
Created May 9, 2019 15:21
Merging two dicts in Python
# How to merge two dictionaries
# in Python 3.5+
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 3, 'c': 4}
>>> z = {**x, **y}
>>> z
{'c': 4, 'a': 1, 'b': 3}
@ferminhg
ferminhg / sort_dict_by_value.py
Created May 9, 2019 15:24
Sort dict by value
# How to sort a Python dict by value
# (== get a representation sorted by value)
>>> xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}
>>> sorted(xs.items(), key=lambda x: x[1])
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
# Or:
@ferminhg
ferminhg / get_method_dicts_and_default_arg.py
Created May 9, 2019 15:26
The get() method on Python dicts and its "default" arg
# The get() method on dicts
# and its "default" argument
name_for_userid = {
382: "Alice",
590: "Bob",
951: "Dilbert",
}
def greeting(userid):
@ferminhg
ferminhg / nametuples_alternative_to_define_class.py
Created May 9, 2019 15:27
nametuples alternative to define class
# Why Python is Great: Namedtuples
# Using namedtuple is way shorter than
# defining a class manually:
>>> from collections import namedtuple
>>> Car = namedtuple('Car', 'color mileage')
# Our new "Car" class works as expected:
>>> my_car = Car('red', 3812.4)
>>> my_car.color
'red'
@ferminhg
ferminhg / the_zen_of_python.py
Created May 9, 2019 15:28
The Zen of Python
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
@ferminhg
ferminhg / pretty_print_dicts_json.py
Created May 9, 2019 15:36
pretty-print dicts with json.dumps()
# The standard string repr for dicts is hard to read:
>>> my_mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee}
>>> my_mapping
{'b': 42, 'c': 12648430. 'a': 23} # 😞
# The "json" module can do a much better job:
>>> import json
>>> print(json.dumps(my_mapping, indent=4, sort_keys=True))
{
"a": 23,
@ferminhg
ferminhg / list_comprehension.py
Created May 10, 2019 11:21
list comprehension basics
#simple
[f(x) for x in sequence]
#simple with if
[f(x) for x in sequence if condition]
#with if and else condition
[f(x) if condition else g(x) for x in sequence]
@ferminhg
ferminhg / function_argument_unpacking.py
Created May 12, 2019 16:39
function argument unpacking
# Why Python Is Great:
# Function argument unpacking
def myfunc(x, y, z):
print(x, y, z)
tuple_vec = (1, 0, 1)
dict_vec = {'x': 1, 'y': 0, 'z': 1}
>>> myfunc(*tuple_vec)