Skip to content

Instantly share code, notes, and snippets.

View michalbcz's full-sized avatar

Michal Bernhard michalbcz

View GitHub Profile
@michalbcz
michalbcz / gist:6229572
Last active December 21, 2015 01:39
emulating window.console to prevent undefined errors for older browser (like IE7/8) which have no native console object or haven't got appropriate log methods
/*
* Initialize console object used for logging in javascript code.
* For case when console object is not present (old browsers) use dump implementation.
*/
window.console = window.console || {}; // in old browsers like IE7 there is no console object
console.info = console.info || function() {};
console.error = console.error || function() {};
console.warn = console.warn || function() {};
@michalbcz
michalbcz / gist:5866983
Created June 26, 2013 12:24
groovy - Map#collectWithIndex implementation
Map.metaClass.collectWithIndex = { yield ->
def collected = []
delegate.eachWithIndex { key, value, index ->
collected << yield(key, value, index)
}
return collected
}
assert [a: 1, b: 1, c: 1].collectWithIndex { key, value, index -> key + value + index } == ["a10", "b11", "c12"]
@michalbcz
michalbcz / github-issue-migrator.py
Last active December 14, 2015 08:39
Migration of issues from RHoK-November-2012/demagog-cz to michalbcz/demagog-cz. Script found through http://stackoverflow.com/questions/9720718/how-do-i-move-an-issue-on-github-to-another-repo on https://github.com/collective/collective.developermanual/blob/master/gh-issues-import.py. This version has modified username and target and destination…
#!/usr/bin/python
"""
Import Github issues from one repo to another.
The orignal work: https://github.com/mkorenkov/tools/blob/master/gh-issues-import/gh-issues-import.py
(assuming public domain license).
Used to migrate Github's Plone Conference 2012 temporary repository
to collective.developermanual issues.
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
Read 4K randomly from SSD* 150,000 ns 0.15 ms
@michalbcz
michalbcz / gist:4170520
Created November 29, 2012 17:19
java - https url connection - trust any certificate
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
@michalbcz
michalbcz / gist:4167914
Created November 29, 2012 09:58
groovy - connect to URL through proxy
import java.net.*;
import java.io.*;
/* PROXY SETTINGS */
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "some.proxyserver.com");
System.getProperties().put("proxyPort", "8080");
Authenticator.setDefault(new MyAuthenticator());
@michalbcz
michalbcz / gist:3916997
Created October 19, 2012 08:42
groovy - run console inside of some context (embedding groovy's gui console)
def c = new groovy.ui.Console(getClass().getClassLoader(), new Binding())
c.setVariable("ctx",this)
c.run()
def done = false
c.frame.windowClosing = {done = true}
c.frame.windowClosed = {done = true}
while (!done) {
sleep(1000)
@michalbcz
michalbcz / ReadableTestNamesJUnitRunner.java
Last active October 11, 2015 13:27
for those who have problem with writing long test methods name like me :)
/**
* Using this runner every test report (gui, cli, html) will print names of tests not as test method
* names eg. "shouldBeLikeThatAndThat" but with spaces like "should be like that and that" for better
* readability. <br/><br/>
*
* <b>Usage</b>:<br/>
*
* <pre>
* <b>{@literal @}RunWith(ReadableTestNamesJUnitRunner.class)</b>
* public class DateParserTest {
@michalbcz
michalbcz / gist:3712849
Created September 13, 2012 08:23
liferay - obtain url to page (page is where you place your portlets it's like igoogle and gadget relation)
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
//Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(themeDisplay.getScopeGroupId(), false, "/page-name");
HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(request);
httpRequest = PortalUtil.getOriginalServletRequest(httpRequest);
//Retrieve layout id of another portlet. Layout is synonym for Page. Will it crash if there are multiple pages??? TODO test it
String portletId = "portletId"; // portlet id is string and you will find this in liferay database scheme or maybe it have some logic, but i don't know what and if it's compatible between liferay versions
long plid = PortalUtil.getPlidFromPortletId(themeDisplay.getScopeGroupId(), portletId);
@michalbcz
michalbcz / gist:3634032
Created September 5, 2012 09:26
groovy - simple 'tree' command implementation
new File(".").eachDirRecurse { dir ->
/* if you wonder why not to write ~/\\/ directly see
http://groovy.codehaus.org/Strings+and+GString#StringsandGString-SlashyStringliterals or
http://jira.codehaus.org/browse/GROOVY-2451 */
def bs = "\\\\"
def fs = "/"
def pattern = ~/$bs|$fs/