Skip to content

Instantly share code, notes, and snippets.

@njamaleddine
njamaleddine / 3x_plus_1.py
Last active October 29, 2021 05:03
3x+1 problem
"""
3x + 1 problem
https://en.wikipedia.org/wiki/Collatz_conjecture
"""
import argparse
import sys
parser = argparse.ArgumentParser(
@njamaleddine
njamaleddine / cache.js
Last active November 16, 2018 02:58
Django cache framework inspired caching with redis
const { promisify } = require('util');
const redis = require('redis');
const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379';
const CACHE_KEY_PREFIX = process.env.REDIS_GLOBAL_CACHE_KEY_PREFIX || '';
const CACHE_TTL = process.env.REDIS_TTL || 60 * 5; // seconds
const DEFAULT_VERSION = 1;
const client = redis.createClient(REDIS_URL);
const redisGet = promisify(client.get).bind(client);
@njamaleddine
njamaleddine / mongoose-pagination.js
Last active November 11, 2018 00:24
Pagination Django REST Framework style for node/mongoose, need to set request context or modify response to include/exclude paginagtion fields
const PAGE_QUERY_PARAMETER = 'page';
const PAGE_SIZE_QUERY_PARAMETER = 'pageSize';
const DEFAULT_PAGE_SIZE = parseInt(process.env.DEFAULT_PAGE_SIZE, 10) || 10;
class Paginator {
constructor(model, query, pageSize, sortBy, ordering = 'asc') {
this.model = model;
this.query = query;
this.pageSize = parseInt(pageSize, 10) || DEFAULT_PAGE_SIZE;
this.useDefaultPageSize = pageSize == null;
@njamaleddine
njamaleddine / dynamodb.js
Last active November 21, 2018 04:02
fun with dynamodb (also install and start scripts for local dev)
const AWS = require('aws-sdk');
const moment = require('moment');
const config = require('../config');
const dynamodb = new AWS.DynamoDB({
apiVersion: config.dynamodb.apiVersion,
endpoint: config.dynamodb.endpoint,
region: config.dynamodb.region,
maxRetries: config.dynamodb.maxRetries,
retryDelayOptions: {
@njamaleddine
njamaleddine / github_commit_rewrite.sh
Created December 10, 2015 07:41
Rewrite commit history. Don't do this for a public repo
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
@njamaleddine
njamaleddine / a-modern-frontend-dev.md
Created November 28, 2015 00:09 — forked from dwayne/a-modern-frontend-dev.md
Articles, tutorials and tools for modern front-end development.

Problem: What does a Modern Front-End Development Workflow Look Like?

I want to start writing libraries and large applications using the JavaScript language. However, I don't know how to setup the project and which build tools to use. What I do know is that the JavaScript community has moved way beyond using browser developer tool plugins and strategically-placed console.log() statements to debug, test, and build code.

I need help.

Below, I will keep track of articles, tutorials and tools I come across as I search for a way to bring my front-end development chops up-to-date.

The Ultimate Resource

@njamaleddine
njamaleddine / test_in_vs_or.py
Last active November 16, 2015 19:27
In is optimized from using timeit to test
from __future__ import print_function
from timeit import timeit
layout_type = 'text_only'
def in_test():
text_required_layout_choice = (
'image_and_text', 'text_over_image', 'text_only'
)
if layout_type in text_required_layout_choice:
import random
from faker import Faker
from factory import fuzzy
class FuzzyJSON(fuzzy.BaseFuzzyAttribute):
"""
Fuzzy json class for json data stored in a django JSONField
"""
def __init__(self, json_object={}, randomize=False):
# -*- coding: utf-8 -*-
from django.db import models
class SoftDeleteQuerySet(models.query.QuerySet):
"""
Override delete functionality so that it performs a soft delete on a
QuerySet unless delete_record() is explicitly called
The model being deleted needs to inherit from `models.ActiveModel`,
@njamaleddine
njamaleddine / group_permissions_migration.py
Last active October 27, 2015 20:02
Django Permissions Migration
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
def initialize_admin_group_permissions(apps, schema_editor):
"""
Set up Admin users to have access to have read/write access for
certain objects