Skip to content

Instantly share code, notes, and snippets.

View ydm's full-sized avatar

Йордан Миладинов ydm

  • Sofia, Bulgaria
View GitHub Profile
@ydm
ydm / pyjnius_print_classpath.py
Created October 22, 2013 13:47
Print current Java classpath using Pyjnius
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from jnius import autoclass, cast
def get_classpath():
ClassLoader = autoclass('java.lang.ClassLoader')
cl = ClassLoader.getSystemClassLoader()
ucl = cast('java.net.URLClassLoader', cl)
@ydm
ydm / observer.py
Created October 25, 2013 08:57
Java's Observer and Observable translated to Python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import threading
class Observer(object):
def update(self, o, *args, **kwargs):
"""
@ydm
ydm / future.py
Created January 21, 2014 09:12
Future task implementation in Python
class Future(object):
def __init__(self, func, *args, **kwargs):
self._lock = threading.Lock()
self._thread = threading.Thread(target=self._locked(func), args=args,
kwargs=kwargs)
self._result = None
self._done = False
def _locked(self, func):
char *c[]={
"ENTER",
"NEW",
"POINT",
"FIRST"
};
char **cp[]={c+3,c+2,c+1,c};
char ***cpp=cp;
main()
{
@ydm
ydm / align-numbers.el
Created March 11, 2014 19:24
Align numeric sequences in Python programs
(defun align-numbers (start end)
(interactive "r")
(let* ((reg (buffer-substring-no-properties start end))
(parts (mapcar #'y:string-strip (split-string reg ",")))
(widest (apply #'max (mapcar #'length parts)))
(full (mapcar (lambda (s)
(concat (make-string (- widest (length s)) ? ) s))
parts))
(line (mapconcat #'identity full ", ")))
(delete-region start end)
@ydm
ydm / montyhall.py
Created March 13, 2014 21:27
Monty Hall problem
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import multiprocessing
def gen_case():
return ['goat' if e else 'car' for e in random.sample(range(3), 3)]
;; http://forums.bgdev.org/index.php?showtopic=44083
(defvar ace (/ 1 13))
(defvar heart0 (/ 13 51))
(defvar heart1 (/ 12 51))
(defvar heart (/ (+ heart1 (* 3 heart0)) 4))
(defvar probability (* ace heart))
;; Our deck of cards:
;; 0-12 -- spades (2 3 4 5 6 7 8 9 10 jack queen king ace)
# -*- coding: utf-8 -*-
import importlib
import os
class ImproperlyConfigured(Exception):
pass
@ydm
ydm / pprint_heap.py
Last active August 14, 2023 23:02
Pretty-print your heaps!
#!/usr/bin/env python3
from math import log
import heapq
first = lambda h: 2**h - 1 # H stands for level height
last = lambda h: first(h + 1)
level = lambda heap, h: heap[first(h):last(h)]
prepare = lambda e, field: str(e).center(field)
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import print_function
from itertools import count
from math import sqrt
seqsum = lambda step, end: (step + end - (end % step)) * (end // step) // 2