Skip to content

Instantly share code, notes, and snippets.

View dreikanter's full-sized avatar

Alex Musayev dreikanter

View GitHub Profile
@dreikanter
dreikanter / Mediator.cs
Created May 14, 2012 09:12
Demo for two-class communication through the mediator.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace MediatorDemo
{
class Dispatcher
{
# pyCrypto.AES file encoding example based on
# Eli Bendersky post (http://eli.thegreenplace.net/2010/06/25/aes-encryption-of-files-in-python-with-pycrypto/)
from Crypto.Cipher import AES
from Crypto import Random
import os
import random
import struct
import hashlib
@dreikanter
dreikanter / include-conf.py
Created May 24, 2012 10:58
Dynamic #configuration file inclusion demo
# Dynamic configuration file inclusion example.
# This script is intended for demo purposes and home usage only.
# There are intentionally no any security checks.
import imp
import sys
if len(sys.argv) < 2:
print "Specify a configuration file to include"
exit(1)
@dreikanter
dreikanter / read_gist_feed.py
Created May 25, 2012 09:49
Parse Gist feed using feedparser and demonstrate Atom data structure #python #feeds
import feedparser
from time import mktime
from datetime import datetime
def format_datetime(struct_time):
return datetime.fromtimestamp(mktime(struct_time))
def read_gist_feed(github_user):
@dreikanter
dreikanter / get-hashtags.py
Created May 25, 2012 10:18
Extract hashtags from a string (w/o regexps) #python
text = """Sample #line of #text to demonstrate #hashtags
extraction. Here is some duplicate ones: #text #hashtags #tags."""
def get_hashtags(text, order=False):
tags = set([item.strip("#.,-\"\'&*^!") for item in text.split() if (item.startswith("#") and len(item) < 256)])
return sorted(tags) if order else tags
print "\n".join(get_hashtags(text, True))
@dreikanter
dreikanter / gzip-demo.py
Created May 30, 2012 10:01
Create tar.gz archive with Python, unpack files back and check the result
# Python gzip demo: create and unpack archive
import os
import random
import string
import glob
import tarfile
import shutil
import filecmp
@dreikanter
dreikanter / py-bat-shebang-demo.bat
Created June 3, 2012 14:06
Shebang analog for Windows to run Python code directly included to batch files
@echo off &python -x %0 %* &goto :eof
print("Hello Python!")
@dreikanter
dreikanter / Interpolator.cs
Last active May 3, 2024 22:25
Spline interpolation in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Interpolation
{
/// <summary>
/// Spline interpolation class.
@dreikanter
dreikanter / copydir.py
Created May 25, 2013 22:23
A missing python function to copy directory tree and overwrite existing files.
import os
import random
import shutil
def copydir(source, dest, indent = 0):
"""Copy a directory structure overwriting existing files"""
for root, dirs, files in os.walk(source):
if not os.path.isdir(root):
os.makedirs(root)
@dreikanter
dreikanter / constants.py
Created May 27, 2013 13:48
How to kill a process using Python
PID_FILE = 'pid.txt'