Skip to content

Instantly share code, notes, and snippets.

View Lazhari's full-sized avatar
:octocat:
Working from home

Lazhari Lazhari

:octocat:
Working from home
View GitHub Profile
@Lazhari
Lazhari / upgrade-node.sh
Created July 21, 2015 08:49
Upgrade Node.js via NPM
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo n 0.12.7
@Lazhari
Lazhari / rename-operator.md
Created July 23, 2015 08:58
MongoDB queries : rename operator via $rename

db.users.update({}, {$rename: {'username': 'email', 'password': 'hashedPassword'}, false, true});

example :

Before : [{username: "[email protected]", password: "pwd"}]

after : [{email: "[email protected]", hashedPassword: "pwd"}]

@Lazhari
Lazhari / app.js
Created July 26, 2015 04:36
Enable Express csrf protection
//Enable Express csrf protection
app.use(express.csrf());
app.use(function(req, res, next) {
res.locals.csrftoken = req.csrfToken();
next();
});
@Lazhari
Lazhari / app-client.js
Created July 29, 2015 05:40
Protection CSRF with express and AngularJs
app.factory('authInterceptor', function ($cookies) {
return {
// Add authorization token to headers
request: function (config) {
config.headers = config.headers || {};
config.headers.post['x-csrf-token'] =$cookies['XSRF-TOKEN'];
return config;
}
};
});
@Lazhari
Lazhari / data-statistics.js
Created August 1, 2015 01:39
Beautiful Code with async and callbacks function
var database = require('../../../models');
var async = require('async');
var VisitorApi = (function() {
/**
* Public Functions
*/
var _countVisitorsByOperator = function(operator,value,callback) {
var query = {};
query[operator] = value;
@Lazhari
Lazhari / clean-containers.sh
Created August 19, 2015 20:47
Remove all Docker containers
docker ps -a | awk '{print $1}' | xargs --no-run-if-empty docker rm
@Lazhari
Lazhari / restaurant-api.js
Created August 21, 2015 10:06
Wonderful async Example
var countUser = function (callback) {
database.getCollection('User').find(function (err, nbUsers) {
if (err) return callback(err);
return callback(null, nbUsers.length);
});
};
var countRestaurantByCity = function(city, callback) {
database.getCollection('Restaurant').count({'adresse.ville': city}, function (err, nbR) {
if (err) return callback(err);
return callback(null, nbR);
@Lazhari
Lazhari / vim-config.md
Created August 21, 2015 16:26
Config Vim : Indenting and Line Length

Add this Vim rules to your vimrc

set expandtab
set shiftwidth=4
set softtabstop=4
set tabstop=4
@Lazhari
Lazhari / chal_1.py
Created September 2, 2015 14:12
Reading Excel Files with Python
#!/usr/bin/env python
"""
Your task is as follows:
- read the provided Excel file
- find and return the min, max and average values for the COAST region
- find and return the time value for the min and max entries
- the time values should be returned as Python tuples
Please see the test function for the expected return format
"""
@Lazhari
Lazhari / parse-csv.py
Created September 2, 2015 16:48
Parsing CSV file
def parse_file(datafile):
name = ""
data = []
with open(datafile,'rb') as f:
r = csv.reader(f)
name = r.next()[1]
header = r.next()
data = [row for row in r]
return (name, data)