Skip to content

Instantly share code, notes, and snippets.

View jgomo3's full-sized avatar
🏠
Working from home

Jesús Gómez jgomo3

🏠
Working from home
View GitHub Profile
@jgomo3
jgomo3 / top.py
Last active February 21, 2020 15:34
Return the first n elements of a collection by natural order of by a key function
import heapq
def top(n, col, key=None):
return heapq.nsmallest(n, col, key)
# Alternative, descent only for small n.
from itertools import count
def top_small_n(n, col, key=None):
from collections import Counter
def even_word(word):
return sum(1 for w, n in Counter(word).items() if n%2 != 0)
@jgomo3
jgomo3 / unificar.py
Created January 14, 2020 21:24
Merge periods or ranges in a collection of them
'''
>>> periodos = [(3, 5), (7, 8), (11, 12), (-10, -1), (6, 10), (-2, 2)]
>>> periods_intersection((3, 5), (7, 8))
>>> periods_intersection((7, 8), (6, 10))
(7, 8)
>>> periods_intersection((-10, -1), (-2, 2))
(-2, -1)
>>> periods_union((-10, -1), (-2, 2))
(-10, 2)
>>> periods_union((6, 10), (7, 8))
@jgomo3
jgomo3 / series.py
Created January 7, 2020 19:48
Kind of range, but for anything that supports adding, or can be combined by a given optional function
'''
Defines the function serie, which...
From an starting point generates all the values following
by the given period.
It works with numbers, anything that support adding or
anything that support the optional binary opperation
provided.
>>> ns = serie(10, 4)
>>> next(ns); next(ns); next(ns)
10
@jgomo3
jgomo3 / ipinsubnet.py
Created January 3, 2020 13:27
Defines the ip_in_sn function to tell if an IP as string is in a Subnet as string
import ipaddress
def ip_in_sn(ip_str, sn_str):
ip = ipaddress.ip_address(ip_str)
sn = ipaddress.ip_network(sn_str)
return ip in sn
if __name__ == '__main__':
ip = '192.0.2.1'
sn = '192.0.2.0/24'
@jgomo3
jgomo3 / edgelist.sql
Created August 9, 2019 18:35
Create an edge list of relations being related by foreign key [postgres]
-- Pre: The names of the relations to consider are in a relation named nodes in
-- the public schema (it could be parametrized)
SELECT
tc.table_name watcher
, ctu.table_name target
FROM
information_schema.table_constraints tc
INNER JOIN information_schema.constraint_table_usage ctu ON tc.constraint_name = ctu.constraint_name
INNER JOIN nodes pt ON tc.table_name = pt."name"
@jgomo3
jgomo3 / README-oneshot-systemd-service.md
Created June 4, 2019 20:50 — forked from drmalex07/README-oneshot-systemd-service.md
An example with an oneshot service on systemd. #systemd #systemd.service #oneshot

README

Services declared as oneshot are expected to take some action and exit immediatelly (thus, they are not really services, no running processes remain). A common pattern for these type of service is to be defined by a setup and a teardown action.

Let's create a example foo service that when started creates a file, and when stopped it deletes it.

Define setup/teardown actions

Create executable file /opt/foo/setup-foo.sh:

@jgomo3
jgomo3 / WeatherWizard.java
Created May 29, 2019 01:04
WeatherWizard Java Tutorial example without Applets
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
@jgomo3
jgomo3 / publisher_worker_class_factory.rb
Created April 12, 2019 14:43
One way to wrap mundane things to a Sidekiq Worker perform method
module Notification
def self.publisher_worker_class(klass)
Class.new do
include(Sidekiq::Worker, Wisper::Publisher)
sidekiq_options(:queue => :report)
define_method :perform do |*args, **kwargs|
success = false
begin
context = klass.new.perform(*args, **kwargs)
broadcast(:deliveried_report_delivery, context)
@jgomo3
jgomo3 / my-ruby.el
Created November 5, 2018 19:38 — forked from littlemove/my-ruby.el
Spacemacs: Code folding for ruby-mode
;; Code folding
(add-hook 'ruby-mode-hook
(lambda () (hs-minor-mode)))
(eval-after-load "hideshow"
'(add-to-list 'hs-special-modes-alist
`(ruby-mode
,(rx (or "def" "class" "module" "do" "{" "[")) ; Block start
,(rx (or "}" "]" "end")) ; Block end
,(rx (or "#" "=begin")) ; Comment start