Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View hackjutsu's full-sized avatar

CosmoX hackjutsu

View GitHub Profile
@hackjutsu
hackjutsu / file_reading.py
Created September 13, 2016 21:56
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.
f.close()
@hackjutsu
hackjutsu / functional_map.py
Created September 13, 2016 22:09
Demo for functional programming map() in Python
def square(x):
return x**2
squares = map(square, range(10))
@hackjutsu
hackjutsu / regex.py
Created September 13, 2016 22:12
Demo for using regex in Python
import re
testStr = "//gss0.baidu.com/6b1IcTe9R1gBo1vgoIiO_jowehsv/maps/services/thumbnails?width=525&height=295&quality=100&align=middle,middle&src=http://gss0.baidu.com/7LsWdDW5_xN3otqbppnN2DJv/lvpics/pic/item/8cb1cb134954092315ec8fd29258d109b2de4991.jpg"
pattern = "http.*jpg"
m = re.search(pattern, testStr)
print(m.group(0))
# http://gss0.baidu.com/7LsWdDW5_xN3otqbppnN2DJv/lvpics/pic/item/8cb1cb134954092315ec8fd29258d109b2de4991.jpg
@hackjutsu
hackjutsu / copying_files.java
Created September 16, 2016 22:25
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
hackjutsu / linebyline.js
Created September 22, 2016 00:42
Reading file line by line in NodeJS
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
});
@hackjutsu
hackjutsu / java_main.java
Created October 4, 2016 18:26
Java main sample
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
@hackjutsu
hackjutsu / ArrBlockingQueue.java
Last active October 4, 2016 23:48
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
hackjutsu / lexical_scope.js
Last active October 15, 2016 00:28
Example explaining the lexical scope in 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
hackjutsu / print_local_repo_path.sh
Created October 20, 2016 21:42
Print out the path to the local Maven Repo
mvn help:evaluate -Dexpression=settings.localRepository | grep -v '\[INFO\]'
@hackjutsu
hackjutsu / create_dir_if_not_exist.js
Created January 8, 2017 16:19
Create a directory if not exist
var fs = require('fs');
var dir = './tmp';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}