Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Jesús Gómez jgomo3

🏠
Working from home
View GitHub Profile
@jgomo3
jgomo3 / framebuffer.c
Created Apr 5, 2020 — forked from FredEckert/framebuffer.c
Paint Pixels to Screen via Linux FrameBuffer
View framebuffer.c
/*
To test that the Linux framebuffer is set up correctly, and that the device permissions
are correct, use the program below which opens the frame buffer and draws a gradient-
filled red square:
retrieved from:
Testing the Linux Framebuffer for Qtopia Core (qt4-x11-4.2.2)
http://cep.xor.aps.anl.gov/software/qt4-x11-4.2.2/qtopiacore-testingframebuffer.html
*/
@jgomo3
jgomo3 / top.py
Last active Feb 21, 2020
Return the first n elements of a collection by natural order of by a key function
View top.py
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):
View evenword.py
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 Jan 14, 2020
Merge periods or ranges in a collection of them
View unificar.py
'''
>>> 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 Jan 7, 2020
Kind of range, but for anything that supports adding, or can be combined by a given optional function
View series.py
'''
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 Jan 3, 2020
Defines the ip_in_sn function to tell if an IP as string is in a Subnet as string
View ipinsubnet.py
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 Aug 9, 2019
Create an edge list of relations being related by foreign key [postgres]
View edgelist.sql
-- 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 Jun 4, 2019 — forked from drmalex07/README-oneshot-systemd-service.md
An example with an oneshot service on systemd. #systemd #systemd.service #oneshot
View README-oneshot-systemd-service.md

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
WeatherWizard Java Tutorial example without Applets
View WeatherWizard.java
/*
* 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 Apr 12, 2019
One way to wrap mundane things to a Sidekiq Worker perform method
View publisher_worker_class_factory.rb
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)
You can’t perform that action at this time.