Skip to content

Instantly share code, notes, and snippets.

@muya
muya / msisdn_regex.txt
Created February 28, 2016 16:06
Regex for Safaricom Kenya, Airtel Kenya & Tigo Tanzania Phone Numbers
# KE
SAFARICOM: "/(\+?254|0|^){1}[-. ]?[7]{1}([0-2]{1}[0-9]{1}|[9]{1}[0-2]{1})[0-9]{6}\z/"
AIRTEL: "/(\+254|0|^){1}[-. ]?[7]{1}([3]{1}[0-9]{1}|[8]{1}[5-9])[0-9]{6}\z/"
# TZ
TIGO: "/(\+?255|0|^){1}[-. ]?([7]{1}[1]{1}[2-9]{1}|[6]{1}[57]{1}[2-9]{1})[0-9]{6}\z/"
@muya
muya / logger.py
Last active October 31, 2020 10:14
Configuring multiple loggers in a file - accompanying blog post: https://blog.muya.co.ke/configuring-multiple-loggers-python/
import logging
from logging import FileHandler
from logging import Formatter
LOG_FORMAT = (
"%(asctime)s [%(levelname)s]: %(message)s in %(pathname)s:%(lineno)d")
LOG_LEVEL = logging.INFO
# messaging logger
@muya
muya / random_saf_ke_msisdn_faker.php
Last active October 8, 2020 16:41
Code for creating random Safaricom Kenya Phone numbers (accompanying blog post: https://blog.muya.co.ke/random-safaricom-kenya-phone-numbers/)
<?php
// include Faker in your project (https://github.com/fzaninotto/Faker#installation)
use Faker\Factory as Faker;
$faker = Faker::create();
$regex = "/(\+?254|0){1}[7]{1}([0-2]{1}[0-9]{1}|[9]{1}[0-2]{1})[0-9]{6}/";
$samplePhoneNumber = $faker->regexify($regex);
@muya
muya / peewee_timestamped_model.py
Last active January 6, 2020 23:50
Timestamped Models in PeeWee
from peewee import *
import datetime
database = SqliteDatabase("/data/amazing_people.db", **{})
class BaseModel(Model):
class Meta:
database = database
@muya
muya / minify_on_save.py
Last active March 4, 2017 16:57
Sublime Text 3 Plugin to Minify JS & CSS files on save (requires bistory's Sublime Minifier here: https://github.com/bistory/Sublime-Minifier)
import sublime, sublime_plugin
class MinifyOnSave(sublime_plugin.EventListener):
def on_post_save(self, view):
file_types_to_minify = ['js', 'css']
filenameParts = view.file_name().split('.')
if filenameParts[len(filenameParts) - 1] in file_types_to_minify:
view.run_command('minify')
@muya
muya / yii-relations.php
Created May 27, 2013 06:06
Snippet of a Yii model's relations function
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'studentProfiles' => array(self::HAS_MANY, 'StudentProfiles', 'userID'),
'userGroupMappings' => array(self::HAS_MANY, 'UserGroupMappings', 'userID'),
@muya
muya / yiiCGridView.php
Last active December 17, 2015 18:48
Example of a Yii CGridView Widget used in the StudentPortal Project
/**
* CGridView widget
*/
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'userID',
'firstName',
@muya
muya / yii-actionAdmin.php
Last active December 17, 2015 18:40
Snippet for Yii's actionAdmin function in the Controller class
/**
* UsersController actionAdmin function
*/
public function actionAdmin() {
$model = new Users('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Users']))
$model->attributes = $_GET['Users'];
$this->render('admin', array(
@muya
muya / convert_timestamp_timezone.py
Created October 17, 2015 17:14
Convert Timestamp Between Timezones
import datetime
from dateutil import parser as date_parser
from dateutil import tz
def convert_timestamp_timezone(timestamp, from_tz="UTC", to_tz="UTC"):
"""
function to convert a string timestamp between timezones
@timestamp - A string timestamp (dateutil.parser will be used to parse)
@from_tz - A string, the current timezone as a string.
@to_tz - A string, the timezone to convert the time to.
Refer to: http://goo.gl/hmPXML for a list of acceptable TZ strings
@muya
muya / ViewController.swift
Created July 18, 2015 13:59
swift tutorial imagePickerController problem
//
// ViewController.swift
// FoodTracker
//
// Created by Muya on 14/07/2015.
// Copyright © 2015 muya. All rights reserved.
//
import UIKit