Skip to content

Instantly share code, notes, and snippets.

View ducin's full-sized avatar
🛠️
creatin' some stuff

Tomasz Ducin ducin

🛠️
creatin' some stuff
View GitHub Profile
@ducin
ducin / fib.js
Created August 6, 2013 09:07
caching function results (single argument function)
function fib(x) {
if (x < 2)
return 1;
return fib(x-1) + fib(x-2);
}
@ducin
ducin / iterator.js
Created August 6, 2013 10:12
stateful functions - generating unique id for objects and iterating collections
(function () {
function iterator(collection) {
var index = 0;
var length = collection.length;
function next() {
var item = collection[index++];
return item;
}
@ducin
ducin / bbmodal.js
Created August 14, 2013 20:11
backbone + bootstrap/modal example
var MainView = Backbone.View.extend({
el: 'body',
events: {
'click #open': 'openModal'
},
template: '<a id="open" class="btn">open modal</a>',
openModal: function() {
var view = new ModalView();
var modal = new Backbone.BootstrapModal({
content: view,
@ducin
ducin / bbmodal-immediate.js
Created August 14, 2013 20:27
backbone + bootstrap/modal - example (immediate modal display)
var ModalView = Backbone.View.extend({
tagName: 'p',
template: 'this is modal content',
render: function() {
this.$el.html(this.template);
return this;
}
});
$(document).ready(function() {
@ducin
ducin / console.py
Last active January 1, 2022 16:41
Python script opening interactive console with current interpreter state. Thanks to readline/rlcompleter, you may use up/down arrows (history browse) or left/right arrows (line edition), see http://stackoverflow.com/questions/19754458/python-open-interactive-console-from-script)
"""
Console module provide `copen` method for opening interactive python shell in
the runtime.
"""
import code
import readline
import rlcompleter
def copen(_globals, _locals):
@ducin
ducin / time-server.php
Last active December 25, 2015 08:49
PHP time server (example PHP server implementation). Run the server from command line, specifying port parameter and connect to it from outside (could be any platform that is able to open a socket or just a telnet connection: `telnet localhost <port>`). The server writes back current time in RFC 2822 and immediately closes the connection.
#!/usr/bin/php -q
<?php
set_time_limit(0);
ob_implicit_flush();
if ($argc != 2)
die("Simple Time Server (C) 2013 Tomasz Ducin\n" .
"Invalid parameters passed\n" .
"Run:\n\t./time-server.php <port>\n" .
@ducin
ducin / chat-bomb.py
Last active February 18, 2022 10:34
python twisted chat server on top of publish/subscribe server
#!/usr/bin/python
import sys, errno
if len(sys.argv) != 2:
print 'Required parameter not passed. Run:\n\n\t./chat-bomb.py <port>\n'
sys.exit(errno.EINVAL)
port = int(sys.argv[1])
@ducin
ducin / class.js
Last active December 26, 2015 08:59
JavaScript class implementation (with example PersonClass declaration)
var Class = function(base){
if (typeof base.construct !== "function")
throw new TypeError("class definition has to define 'construct' function");
return function() {
// copy all methods
for (var prop in base) {
if (base.hasOwnProperty(prop) && base[prop] !== base.construct) {
this[prop] = base[prop];
}
}
@ducin
ducin / console-load-js-script.js
Last active February 18, 2021 06:14
load js script from blank page browser (execute following code in the browser console)
(function(root){
root.getScript = function(url) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
document.head.appendChild(script);
}
}(window));
getScript('http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.js');
@ducin
ducin / fork.py
Created November 3, 2013 12:38
scanning parent/child process IDs using os.fork
import os
parent_pid = os.getpid()
print "[parent] starts PID: %d" % (parent_pid, )
forked_pid = os.fork()
if forked_pid == 0:
print "[child] child process can't use os.fork() PID, since it's %d" % (forked_pid, )
print "[child] but it can reevaluate os.getpid() to get it's own PID: %d" % (os.getpid(), )
else: