Skip to content

Instantly share code, notes, and snippets.

View worldmind's full-sized avatar

Alexey Shrub worldmind

View GitHub Profile
@worldmind
worldmind / gist:2724846
Created May 18, 2012 11:47
Perl $SIG{CHLD} handler
# see http://perldoc.perl.org/perlipc.html#Signals
# http://www.calpoly.edu/cgi-bin/man-cgi?wait+2
# and Perl Cookbook, part 16.19. Avoiding Zombie Processes
sub REAPER {
local ($!, $?);
while ( (my $pid = waitpid(-1, WNOHANG)) > 0 ) {
say "Process $pid send CHLD with status $?";
if ( WIFEXITED($?) or WIFSIGNALED($?) ) {
if ( $kids{$pid} ) {
@worldmind
worldmind / minion-example.pl
Last active March 11, 2021 06:43
Minion example
#!/usr/bin/perl -w
use Mojolicious::Lite;
use Minion;
# run app: morbo minion-example.pl
# run worker: perl minion-example.pl minion worker
# enqueue job: curl 'http://127.0.0.1:3000/do?msg=hello'
# show job status: perl minion-example.pl minion job 1
plugin Minion => { SQLite => 'sqlite:test.db' };
#!/usr/bin/perl -w
use strict;
use Test::More tests => 7;
BEGIN {
use_ok('Class::Date', 'date');
}
my $dt0 = date '2016-06-30 12:00';
def validate_check_sum(card_number):
newlist = list(map(lambda x: double_and_sum_digits4odd(*x), enumerate(card_number[::-1])))
val = sum(newlist[::-1])
if val % 10 == 0:
return 'valid'
else:
return 'not valid'
def sum_digits(x):
digits = str(x)
@worldmind
worldmind / test.py
Last active December 19, 2018 10:47
Compare object sizes in python 3.6
import attr
from pympler import asizeof
@attr.s()
class Coordinates():
x = attr.ib()
y = attr.ib()
c = Coordinates(x=1, y=2)
@worldmind
worldmind / test2.py
Last active December 20, 2018 11:46
Compare object sizes in python 3.6
from pympler import asizeof
class Coordinates():
def __init__(self, x, y):
self.x = x
self.y = y
class CoordinatesSlots():
__slots__ = ('x', 'y')
@worldmind
worldmind / objects_size.py
Created December 20, 2018 15:43
Ugly script for comparing objects with/without __slots__
from pympler import asizeof
def list_to_str(lst):
str = ''
for val in lst:
str = str + val + ', '
return str[0:len(str)-2]
@worldmind
worldmind / testcase_assert_not_raises.py
Last active January 25, 2019 08:01
Python unittest: Testing an exception is not raised
import sys
import unittest
from contextlib import contextmanager
@contextmanager
def assert_not_raises(self, exc_type):
try:
yield None
except exc_type:
@worldmind
worldmind / loop_scope.py
Created April 3, 2019 06:55
Loop scope
def main():
y = 0
for k in range(1,3):
z = k
def inside_main():
print(y)
print(z)
print(k)
inside_main()
@worldmind
worldmind / five_factor_test.py
Created June 11, 2019 12:44
Most science based 5 factor psychology test :)
from math import ceil, floor
import numpy as np
LEVELS = 10
CENTER = int(LEVELS/2)
FACTORS = [
'Agreeableness',
'Conscientiousness',
'Extraversion',