Freelancer's Playground! Learn Programming, The Freelancer's Way

10Mar/101

HOW TO: Display All Your Monthly Archive In One Page

Hi, today I want to talk about archive page. You know, the one that popped when you click on archive link? On normal wordpress themes, archive page will bound to post per page settings you've set on admin. However, what if you want to display it all in one page? Maybe because you just write small amout of posts per month that will only take 2 pages and you don't one your visitor to click next just to see it all?

Filed under: snippet Continue reading
3Feb/105

Get Only n Words From String

Here's the code:

function trim_word( $words, $howmany = 1){
	$x = explode(" ", $words);
	if(count( $x) <=$howmany ){
		return $words;
	} else {
		return implode(" ", array_slice( $x, 0, $howmany));
	}
}

Here's the output:

$words = "this is string that is long enough that I want to trim it.";

echo trim_word($words, 10) . "\n";
//this will echo: this is string that is long enough that I want

UPDATE:

  1. 2010-02-05 15:03:25 - Fixing $howmany usage. Sorry, for the inconvenience:D
Filed under: snippet 5 Comments
24Nov/090

Python File System Operation Snippets Collection

In this post, I'll share some simple snippet to do various file system operation. Pretty much as a bookmark for my own usage, in case I needed it someday.

How to get current working directory:

import os
os.getcwd()

How to change working directory:

import os
os.chdir('/tmp/')

How to list directory contents:

import os
os.listdir('/tmp/')

How to check if directory / file exists:

import os
os.path.exists('/tmp/')#work for both file and directory

How to create directory:

import os
dir = os.path.dirname('/tmp/current_object/')
  if not os.path.exists(d):
    os.makedirs(d)

Using code above will ensure you did not create directory that already exists

How to delete file or directory:

import os
os.remove('/tmp/')

or if you want to delete non empty directory:

import shutil
shutil.rmtree('/tmp/somefolder')

How to create temporary directory:

import os
import tempfile
import shutil
#let OS decide where they want to create the temp directory
tmpdir = tempfile.mkdtemp('somesuffix', 'someprefix')
#You want to take control where you want the temp directory be created
tmpdir = tempfile.mkdtemp('somesuffix', 'someprefix', '/tmp/path/of/your/choice/')
#be sure to remove the temp directory once work is done
shutil.rmtree('/tmp/somefolder')

How to create temporary file:

import os
import tempfile
#let OS decide where they want to create the temp directory
tmpfile = tempfile.mkstemp('somesuffix', 'someprefix', None, 'some initial contents')
#You want to take control where you want the temp directory be created
tmpfile = tempfile.mkstemp('somesuffix', 'someprefix', '/tmp/path/of/your/choice/', 'some initial contents')
#be sure to remove the temp directory once work is done
os.remove(tmpfile)

How to check file permission:

import os
import stat
file = '/tmp/'
stt = os.stat(file)
mode = stt[stat.ST_MODE]
print "%s mode is: %s" % (file, mode)

Filed under: snippet No Comments
20Nov/093

Automate Facebook Status Update Without Facebook API

Sweet little snippet with only 10 lines of code! Using python mechanize library to update your facebook status. Check out the code:

Filed under: snippet Continue reading