Skip to content

Instantly share code, notes, and snippets.

View itolosa's full-sized avatar
🎯
Focusing

Ignacio Tolosa itolosa

🎯
Focusing
View GitHub Profile
@itolosa
itolosa / hexhackcalc.py
Created February 25, 2015 19:51
Hexadecimal resolution hack calculator for Commandos 3
def convres(x):
x = hex(int(x))[2:].zfill(4)
return (x[:2], x[2:])[::-1]
print 'Commandos 3 resolution converter (Wide x Height)'
w = int(raw_input('Wide: '))
h = int(raw_input('Height: '))
print 'first replace: ' + ' '.join(convres(w))
print 'second replace: ' + ' '.join(convres(h))
@itolosa
itolosa / demony.py
Created February 25, 2015 19:59
Daemon Script based on Turkmenbashi (@spicymagpie)
#!/usr/bin/env python
# encoding: utf-8
import os
import sys
import signal
class MasterDaemon(object):
def __init__(self, infn, outfn, errfn):
self.infn = infn
@itolosa
itolosa / mutex_nowait.py
Created February 25, 2015 20:04
Mutex implementation with no spinlocks
def wait():
condmut.acquire()
sleepQueue.enqueue(P)
condmut.release()
sleep(P)
def signal():
condmut.acquire()
P = sleepQueue.dequeue(NoWait)
if not P:
@itolosa
itolosa / protosem.py
Last active September 27, 2015 08:57
proto semaphore
import threading
mutex1 = threading.Lock()
sem_limit = 5
sem_count = sem_limit
lista1 = []
def acquire_sem():
mutex1.acquire()
if sem_count == 0:
@itolosa
itolosa / protosem2.py
Created September 27, 2015 09:03
functional
import threading
class Semaphore(object):
def __init__(self, sem_limit):
self.condition1 = threading.Condition(threading.Lock())
self.sem_limit = int(sem_limit)
self.sem_count = self.sem_limit
def acquire(self):
self.condition1.acquire()
require 'open-uri'
require 'nokogiri'
class AppkedApi
def initialize
require 'open-uri'
require 'nokogiri'
end
def content(url, entry=true)
@itolosa
itolosa / user_clinic.rb
Last active October 29, 2015 05:37
user_clinic.rb
class User < ActiveRecord::Base
has_many :user_clinics
has_many :clinics, through: :user_clinics
end
class UserClinic < ActiveRecord::Base
belongs_to :user
belongs_to :clinic
validate :clinic_validation

Una posible optimización (para: https://github.com/icodeart/brute-force) sería usar concurrencia. El problema del productor-consumidor [4] serviría en este caso: donde tienes M consumidores (http request workers) y N productores (workers que leen el archivo y vacían contenido a un buffer, de tamaño limitado y a elección, en memoria). De este modo se minimizan los tiempos de espera entre un request I/O y otro, y se maximiza el uso de la red al usar varios workers, explotando el algoritmo scheduling del sistema operativo (véase preemption) [5]. Al existir una tarea I/O que limita la holgura del programa en general, se podría decir, que esto pondera a que el problema sea de tipo I/O (entrada y salida), ya que las comunicaciones por red, son siempre más lentas que el procesamiento en la maquina (CPU). [6] Usar threads virtuales [3] sobre los workers que hacen procesamiento http, sería bueno en este caso. Green-threads [2] son una buena opción también (También ver Python y GIL)[1].

@itolosa
itolosa / union_find.c
Last active January 25, 2016 12:24
Union Find
#include <stdlib.h>
#include <stdio.h>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
typedef enum { false=0, true } bool;
typedef struct _uf_node {
int data;
#!/usr/bin/env ruby
require 'droplet_kit'
token = 'put-your-token-id'
client = DropletKit::Client.new(access_token: token)
if File.exist?('/root/droplet_id')
begin
droplet_id = File.read('/root/droplet_id')
res = client.droplets.delete(id: droplet_id)
until res.status == "completed"
res = client.actions.find(id: res.id)