Skip to content

Instantly share code, notes, and snippets.


CosmoX hackjutsu

View GitHub Profile
hackjutsu /
Created Sep 13, 2016
Reading a file in Python
## Open the file with read only permit
f = open('myTextFile.txt', "r")
## use readlines to read all lines in the file
## The variable "lines" is a list containing all lines
lines = f.readlines()
## close the file after reading the lines.
hackjutsu /
Created Sep 13, 2016
Demo for functional programming map() in Python
def square(x):
return x**2
squares = map(square, range(10))
hackjutsu /
Created Sep 13, 2016
Demo for using regex in Python
import re
testStr = "//,middle&src="
pattern = "http.*jpg"
m =, testStr)
hackjutsu /
Created Sep 16, 2016
Copying files in Java
// If you are using Java 7, Files (in the standard library) is the best approach:
/* You can get Path from file also: file.toPath() */
Files.copy(InputStream in, Path target)
Files.copy(Path source, OutputStream out)
hackjutsu / linebyline.js
Created Sep 22, 2016
Reading file line by line in NodeJS
View linebyline.js
var fs = require("fs");
// Sync
var array = fs.readFileSync(path).toString().split('\n');
// Async
fs.readFile(path, function(err, f){
var array = f.toString().split('\n');
// use the array
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
hackjutsu /
Last active Oct 4, 2016
Sample code for thread safe blocking queue in Java
public class ArrBlockingQueue {
final private Lock _lock = new ReentrantLock();
final private Condition _notFull = _lock.newCondition();
final private Condition _notEmpty = _lock.newCondition();
final private Object[] _items;
private int _putptr, _takeptr, _count;
public ArrBlockingQueue(final int size) {
hackjutsu / lexical_scope.js
Last active Oct 15, 2016
Example explaining the lexical scope in JS
View lexical_scope.js
var a = 10;
function add() {
var b = 20;
return a + b; // a is bound to the global a when the function object add() is created.
// call add() -> 30
(function() {
var a = 20;
hackjutsu /
Created Oct 20, 2016
Print out the path to the local Maven Repo
mvn help:evaluate -Dexpression=settings.localRepository | grep -v '\[INFO\]'
hackjutsu / create_dir_if_not_exist.js
Created Jan 8, 2017
Create a directory if not exist
View create_dir_if_not_exist.js
var fs = require('fs');
var dir = './tmp';
if (!fs.existsSync(dir)){