Markdown [Snippet] Simple and easy beautiful markdown output

Everybody like markdown, it’s easy and beautiful. But the output of markdown syntax just plain html, how to make it better?

Yeah, that is my question. I don’t want to install anything beside markdown. So it’s back to basic.

Here is the step (do it in your console for first step) :

git clone https://gist.github.com/6878245.git
mv 6878245/head.html . && rm 6878245/ -rf
markdown yourmarkdown.md > body.html
git clone https://gist.github.com/6878289.git
mv 6878289/tail.html . && rm 6878289/ -rf
cat head.html body.html tail.html > index.html

Here is the second step when you edit your markdown file, and compile & combine it again :

git clone https://gist.github.com/6878094.git
mv 6878094/combine_md.sh . && chmod u+x combine_md.sh && rm 6878094/ -rf

After You got the snippet from My gists :

vim index.md
./combine_md.sh

and voila, your markdown is better now.

Advertisements

Redis [Snippet] Redis compiling problem on ubuntu 10.04 32bit

cd hiredis && make static
make[3]: Entering directory `/home/project/redis/redis-2.6.4/deps/hiredis’
gcc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c
make[3]: gcc: Command not found
make[3]: *** [net.o] Error 127
make[3]: Leaving directory `/home/project/redis/redis-2.6.4/deps/hiredis’
make[2]: *** [hiredis] Error 2
make[2]: Leaving directory `/home/project/redis/redis-2.6.4/deps’
make[1]: [persist-settings] Error 2 (ignored)
CC adlist.o
/bin/sh: cc: not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/home/project/redis/redis-2.6.4/src’
make: *** [all] Error 2

That’s mean no gcc installed, so just type this command, apt-get install gcc

And then when I tried to make again, I’m encounter this problem.

root@vx-ubu:/home/project/redis/redis-2.6.4# make
cd src && make all
make[1]: Entering directory `/home/project/redis/redis-2.6.4/src’
CC adlist.o
In file included from adlist.c:34:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error “Newer version of jemalloc required
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/home/project/redis/redis-2.6.4/src’
make: *** [all] Error 2

All You have to do is type this command make distclean. And now, redis-2.6.4 is ready to use.

Python [snippet/problem] : Kill process via python, script won’t continue

Have You wondering, how to kill process in Your linux machine and restart it again? I have some problem with it, let’s see the first code now.

#usr/bin/python
import os, time

sleepTime = 3

''' STEP 1 - Kill Stream '''
text = 'TwitterStreamEncode'
#execute = 'kill -9 `ps -ef | grep %s | grep -v grep | awk \'{print $2}\'`' % sys.argv[1] # ==> Note 1
execute = 'kill -9 `ps -ef | grep %s | grep -v grep | awk \'{print $2}\'`' % text # ==> Note 2
print execute
cmdKill  = os.system(execute)

''' STEP 2 - Give sleep to make sure process is killed '''
print "Sleeping for %s sec\n" % sleepTime
time.sleep(sleepTime)

Here’s the command to start my test

root@fajri-laptop : ~# nohup /usr/bin/php /home/project/MW/TwitterStreamEncode.php > /dev/null &
[1] 28435
root@fajri-laptop : ~# nohup: ignoring input and redirecting stderr to stdout
^C
root@fajri-laptop : ~# ps -ef | grep Twitter
root     28435 28424  2 21:13 pts/1    00:00:00 /usr/bin/php /home/project/MW/TwitterStreamEncode.php
root     28438 28424  0 21:13 pts/1    00:00:00 grep --color=auto Twitter

You can see, the process is starting with PID 28435
Then I’m execute it like usual, python try_kill.py

root@vx662-sekremm:/home/project/MW# python try_kill.py
kill -9 `ps -ef | grep TwitterStreamEncode | grep -v grep | awk '{print $2}'`
Sleeping for 3 sec

root@fajri-laptop : ~# ps -ef | grep Twitter
root     28451 28424  0 21:16 pts/1    00:00:00 grep --color=auto Twitter
[1]+  Killed                  nohup /usr/bin/php /home/project/MW/TwitterStreamEncode.php > /dev/null

You can see, the first script is fine, the process is killed.

But, I want to make it simpler, myscript able to receive parameter via command line, like python try_kill.py TwitterStreamEncode, so it will find the process, and kill it.
So I change my code to Note 1 #execute = 'kill -9 `ps -ef | grep %s | grep -v grep | awk \'{print $2}\'`' % sys.argv[1] , and here’s the output.

root@fajri-laptop : ~# nohup /usr/bin/php /home/project/MW/TwitterStreamEncode.php > /dev/null &
[1] 28473
root@fajri-laptop : ~# nohup: ignoring input and redirecting stderr to stdout
^C

root@fajri-laptop : ~# ps -ef | grep Twitter
root     28473 28424  5 21:20 pts/1    00:00:00 /usr/bin/php /home/project/MW/TwitterStreamEncode.php
root     28476 28424  0 21:20 pts/1    00:00:00 grep --color=auto Twitter

root@vx662-sekremm:/home/project/MW# python try_kill.py TwitterStreamEncode
kill -9 `ps -ef | grep TwitterStreamEncode | grep -v grep | awk '{print $2}'`
Killed

root@fajri-laptop : ~# ps -ef | grep Twitter
root     28485 28424  0 21:22 pts/1    00:00:00 grep --color=auto Twitter
[1]+  Killed                  nohup /usr/bin/php /home/project/MW/TwitterStreamEncode.php > /dev/null

You can see, my script won’t continue after it execute os.system
I’m still don’t know why this is happened 😦

Python [snippet/benchmark] : Redis test insert php vs python

I have a test script about redis, 1 in PHP, and 1 in Python. I want to test which one is faster.

Here is the PHP code :

<?php

$redis = new Redis();
$redis->connect('127.0.0.1');
$counter = 0;

for ($i = 1; $i <= 100000; $i++){
    if ($redis->sadd("php:key_test",$i)){
        $counter += 1;
    }
}

echo "Added $counter data\n";

$count = $redis->scard('php:key_test');

echo "count_data = $count \n";

/*
root@fajri-laptop:/home/fajri/php_project/php_redis# time php redis_test_insert.php
Added 100000 data
count_data = 100000

real    0m12.093s
user    0m1.672s
sys    0m2.488s

root@fajri-laptop:/home/fajri/php_project/php_redis# php -v
PHP 5.3.2-1ubuntu4.17 with Suhosin-Patch (cli) (built: Jun 19 2012 01:35:33)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
*/

Here is the Python code :

import redis

redis_server = redis.Redis("127.0.0.1")
counter = 0

for i in range(100000):
    if redis_server.sadd('python:key_test',i):
        counter +=1

print "added %s data" % (counter)

count = redis_server.scard('python:key_test')

print "count_data = %s" % (count)

'''
fajri@fajri-laptop:~/python_project/py_redis$ time python redis_test_insert.py
added 100000 data
count_data = 100000

real    0m14.914s
user    0m6.100s
sys    0m2.104s

fajri@fajri-laptop:~/python_project/py_redis$ python -V
Python 2.6.5
'''

You can see, PHP is faster. So I googling & chat on ##python-friendly @ freenode, and got an idea, it should use xrange instead range.
So, I change to xrange, and it really works. Now Python is faster than PHP.
Why it could be? because range is just a list and xrange is Generator, which generator is using less memory than list, so it can be faster than before.
Finally it took only 10 seconds.

Hope this simple snippet is useful for you 😀

Python [snippet] : Date Format from String

I’m trying to scrap http://www.kaskus.co.id, It contains A challenging date format to scrape. They give some like this Today, 4 PM or Yesterday, 14:05 PM , or join date, They give Nov 2011. So, I create some function to handle that

import datetime
def mysql_date_format(str_date, code):
    from_date_format = {
        'kaskus_post_date' : '%d-%m-%Y, %I:%M %p',
        'kaskus_join_date' : '%b %Y',
    }

    return_date_format = {
        'kaskus_post_date' : '%Y-%m-%d %H:%M:%S',
        'kaskus_join_date' : '%Y-%m-%d %H:%M:%S'
    }
    if 'Today' in str_date:
        today = datetime.datetime.today()
        str_date = str_date.replace('Today',today.strftime('%d-%m-%Y'))
    elif 'Yesterday' in str_date:
        today = datetime.datetime.today()
        yesterday = today - datetime.timedelta(1)
        str_date = str_date.replace('Yesterday',yesterday.strftime('%d-%m-%Y'))

    return datetime.datetime.strptime(str_date, from_date_format).strftime(return_date_format)

Hope this simple snippet is useful for you 😀

Reference :

Redis [Snippet] : Delete multiple key with same prefix

After I install redis-tools, and using ./redis-load some test keys is still exist, test keys is like this,  string:12312. But the count of keys is more than 4000, and it’s bothering me. So after some googling i found this on stackoverflow. And it works for me. Here is the code :

redis-cli KEYS "string:*" | xargs redis-cli DEL

Redis [Snippet] PHP Redis compiling on centos 6.2 64bit

I have some error when try to compile PHP Redis on Centos 6.2 64bit. There is a little difference between ubuntu & centos php configuration. So, here’s the step to solve it

  1. download this file (nicolasff-phpredis-2.2.1-63-gf3dff08.zip)
  2. unzip nicolasff-phpredis-2.2.1-63-gf3dff08.zip
  3. cd nicolasff-phpredis-f3dff08
  4. yum install php-devel –enablerepo=remi
  5. phpize
  6. ./configure
  7. make && make install
  8. vim /etc/php.d/redis.ini
  9. extension=redis.so #insert to redis.ini file