Skip to content

Instantly share code, notes, and snippets.

Avatar

CosmoX hackjutsu

View GitHub Profile
@hackjutsu
hackjutsu / file_reading.py
Created Sep 13, 2016
Reading a file in Python
View file_reading.py
## 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 Sep 13, 2016
Demo for functional programming map() in Python
View functional_map.py
def square(x):
return x**2
squares = map(square, range(10))
@hackjutsu
hackjutsu / regex.py
Created Sep 13, 2016
Demo for using regex in Python
View regex.py
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 Sep 16, 2016
Copying files in Java
View copying_files.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 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
});
View java_main.java
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
@hackjutsu
hackjutsu / ArrBlockingQueue.java
Last active Oct 4, 2016
Sample code for thread safe blocking queue in Java
View ArrBlockingQueue.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 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
hackjutsu / print_local_repo_path.sh
Created Oct 20, 2016
Print out the path to the local Maven Repo
View print_local_repo_path.sh
mvn help:evaluate -Dexpression=settings.localRepository | grep -v '\[INFO\]'
@hackjutsu
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)){
fs.mkdirSync(dir);
}