You Are Here Home > Fun

Fun

Script For Counting Number Of Lines Of Code In Your Website; Composite Design Pattern

This is another thread from our forums which we are closing down soon.

This script will count the number of lines in all of your source files recursively. Just place it in any folder and point your browser to it and it will count all the lines including sub directories.

It might run out of memory if your application is huge and your PHP memory limit is low. For me, it counted 97,000 lines in our last project with no problems.

You also have an option to exclude file extensions and directories.

The other thing about this script is that it is a great little example of composite design pattern in action; every directory is an object that will count all the lines (in the files) in it and asks it’s sub directories to do the same, then the sub directories also repeat the same process.

<?php
 
	/**
	 * Counts the lines of code in this folder and all sub folders
	 * You may not sell this script or remove these header comments
	 * @author Hamid Alipour, http://blog.code-head.com/
	**/
 
	define('SHOW_DETAILS', true);
 
	class Folder {
 
		var $name;
		var $path;
		var $folders;
		var $files;
		var $exclude_extensions;
		var $exclude_files;
		var $exclude_folders;
 
 
		function Folder($path) {
			$this -> path 		= $path;
			$this -> name		= array_pop( array_filter( explode(DIRECTORY_SEPARATOR, $path) ) );
			$this -> folders 	= array();
			$this -> files		= array();
			$this -> exclude_extensions = array('gif', 'jpg', 'jpeg', 'png', 'tft', 'bmp', 'rest-of-the-file-extensions-to-exclude');
			$this -> exclude_files 	    = array('count_lines.php', 'rest-of-the-files-to-exclude');
			$this -> exclude_folders 	 = array('_private', '_vti_bin', '_vti_cnf', '_vti_log', '_vti_pvt', '_vti_txt', 'rest-of-the-folders-to-exclude');
		}
 
		function count_lines() {
			if( defined('SHOW_DETAILS') ) echo "/Folder: {$this -> path}...\n";
			$total_lines = 0;
			$this -> get_contents();
			foreach($this -> files as $file) {
				if( in_array($file -> ext, $this -> exclude_extensions) || in_array($file -> name, $this -> exclude_files) ) {
					if( defined('SHOW_DETAILS') ) echo "#---Skipping File: {$file -> name};\n";
					continue;
				}
				$total_lines += $file -> get_num_lines();
			}
			foreach($this -> folders as $folder) {
				if( in_array($folder -> name, $this -> exclude_folders) ) {
					if( defined('SHOW_DETAILS') ) echo "#Skipping Folder: {$folder -> name};\n";
					continue;
				}
				$total_lines += $folder -> count_lines();
			}
			if( defined('SHOW_DETAILS') ) echo "\n Total lines in {$this -> name}: $total_lines;\n\n";
			return $total_lines;
		}
 
		function get_contents() {
			$contents = $this -> _get_contents();
			foreach($contents as $key => $value) {
				if( $value['type'] == 'Folder' ) {
					$this -> folders[] = new Folder($value['item']);
				} else {
					$this -> files[]   = new File  ($value['item']);
				}
			}
		}
 
		function _get_contents() {
			$folder = $this -> path;
			if( !is_dir($folder) ) {
				return array();
			}
			$return_array = array();
			$count		  = 0;
			if( $dh = opendir($folder) ) {
				while( ($file = readdir($dh)) !== false ) {
					if( $file == '.' || $file == '..' ) continue;
					$return_array[$count]['item']	= $folder .$file .(is_dir($folder .$file) ? DIRECTORY_SEPARATOR : '');
					$return_array[$count]['type']	= is_dir($folder .$file) ? 'Folder' : 'File';
					$count++;
				}
				closedir($dh);
			}
			return $return_array;
		}
 
	} // Class
 
	class File {
 
		var $name;
		var $path;
		var $ext;
 
 
		function File($path) {
			$this -> path = $path;
			$this -> name = basename($path);
			$this -> ext  = array_pop( explode('.', $this -> name) );
		}
 
		function get_num_lines() {
			$count_lines = count(file($this -> path));
			if( defined('SHOW_DETAILS') ) echo "|---File: {$this -> name}, lines: $count_lines;\n";
			return $count_lines;
		}
 
	} // Class
 
	$path_to_here = dirname(__FILE__) .DIRECTORY_SEPARATOR;
	$folder 		  = new Folder($path_to_here);
	echo 'Total lines of code: ' .$folder -> count_lines() ."\n\n";
 
?>
Script For Counting Number Of Lines Of Code In Your Website; Composite Design Pattern
Comments (0)   Filed under: Design Patterns, Fun, PHP, Programming, Web Development   Posted by: Codehead on February 8, 2010

Some Fun Stuff

1 – Did you know that there were only 300 years of peace on Earth in total?

2 – GPS satellites have atomic clocks that tick at around a few billion times per second, did you know that their clocks tick a little faster than the clocks on earth? This is what Albert Einstein predicted, clocks that are closer to a strong source of gravity tick slower!!!!

http://en.wikipedia.org/wiki/Global_Positioning_System#Relativity

3 – Did you know that an unknown flying object followed Apollo 11 along it’s way to the moon? If you don’t believe me, listen to Buzz Aldrin talking about it:

4 – Did you know that there are 100,000 little machines inside each of our cells?

5 – Did you know that some people who we thought that were in vegetative state are actually aware but can’t communicate?

Some Fun Stuff
Comments (0)   Filed under: Fun, General   Posted by: Codehead on February 6, 2010

I Know Which Tablet I Want

OK, if you are an Apple fan, don’t be mad at me, I like Apple but I don’t like the spooky-dishonesty stuff…

To be honest, some Apple fans are kind of scary, have you ever seen their comments on YouTube or elsewhere? It’s like Apple is their religion and Steve Jobs is their profit, no wonder… :)

Anyways, things like this bother me a lot:

1 – Showing that the device supports Flash in the ads when in reality it doesn’t, do you realise someone big in that company said “Let’s make it look like it supports Flash in the ads!”???

2 – Steve Jobs said that they designed the A4 chip “right here at Apple” but in reality it is a ARM Cortex chip!!!!

On the other hand, there is going to be a very, very interesting product out soon, it’s HP Slate, the name is kind of lame but it will be running a full version of Windows 7 and under the hood, it has a NVIDIA Tegra 2, which is a very cool chip; it has dual ARM Cortex 1GHz CPUs!

I have a big laptop for work, but I have a little one that I carry around EVERYWHERE with me and it’s very handy, for watching videos and stuff other than work. I was hoping that the iPad could replace that but it didn’t turn out to be that way, HP Slate on the other hand, will do exactly that.

Sorry Apple…

I Know Which Tablet I Want
Comments (0)   Filed under: Apple, Fun, General, Operating Systems, Technology   Posted by: Codehead on January 30, 2010

I Want An iPad – NOT!

Ignore my last post, the more I know about it, the more problems I have with this pretty device…

Here are my mail reasons:

1 – It doesn’t have the OSX, the OS is like the one on iPhone.
2 – No multitasking support.
3 – No Flash support!!!!
4 – No camera.

It’s basically a giant iPhone with less features!!

Sadly, Apple got it wrong this time. I also noticed that Steve Jobs wasn’t very excited when he introduced it, it wasn’t like the iPhone keynote, maybe he wasn’t very pleased with it either.

It’s too bad…

I Want An iPad – NOT!
Comments (0)   Filed under: Apple, Fun, General   Posted by: Codehead on January 29, 2010

I Want An iPad

I’m not an apple customer, I’ve been thinking about switching to Mac for a while but that requires Mac based software for our work so I’m not ready yet.

But seeing Steve Jobs introduce the iPad, we’ve decided we each want one!

I love everything about this device, I don’t know who will buy a Netbook or a Kindle, or at least I wouldn’t…

There are some things that I like to see on this device, one is a complete Mac-like OS (rather than iPhone) although I know it’s OS 10 already but it would be really nice to have that. The other thing is a camera but I will buy one regardless of these features.

The only thing that disappoints me about this device is that, I have to wait 2-3 months for it to come out, I will write more as soon as I have it.

I Want An iPad
Comments (0)   Filed under: Apple, Fun, General, Operating Systems   Posted by: Codehead on January 28, 2010

I Should Learn Mathematics Again

Lately, I’m running to all sorts of issues that require math, strong math, and I finally decided to learn math again. I should say remember though…

When I was second grade in high school, the school took a math exam to find out who to send to mathematics Olympiads.

The exam was for 3rd graders but me and one of my friends made it into the mathematics Olympiads in our province.

I became 8th in that exam and it was very hard, I didn’t make it to the country wide exam but I was very good at math for my age, I was reading more advanced books.

When I was a third grader, everyone thought I would make it all the way to the world mathematics Olympiads but I missed the opportunity to signup for the exam and I was late; that was a huge disappointment for me…

To put it in perspective, when we were in 3rd grade, I scored 19.25 out of 20 in our “Calculus, Differential and Integral” final exam, where NO ONE in our school scored more than 15!

Those days are gone and since I didn’t use math I forgot almost everything but a few theorems and I have to learn them again.

So I started with an absolute beginner math book, and now I’m reading “Calculus Made Easy” by Silvanus P. Thompson and Martin Gardner.

This book as absolutely amazing, it’s in print for 75 years!!!! and it was rewritten at 1946, then Martin Gardner updated and expanded the book in 1998 to make it even better.

If you want to learn math over again, you need to know calculus, then you can move on to more advanced topics like linear algebra etc.

This book is so much fun that I can’t stop reading it and I pick it up every little chance I get and hey I understand all of what I read!

The new Calculus books are a mess, they are so big and so thick and so poorly written that they make you hate the topic, but this book is exactly the opposite.

I Should Learn Mathematics Again
Comments (0)   Filed under: Fun, General, Mathematics   Posted by: Codehead on January 19, 2010

Why Did They Call Them Minutes And Seconds?

The reason was that they thought the unit was so ‘minute’ (small) and so the word minute stuck and when they further divided a minute into 60 parts, the new parts were of ’second’ order of minuteness and over time they called the seconds…

Why Did They Call Them Minutes And Seconds?
Comments (0)   Filed under: Fun, General   Posted by: Codehead on January 15, 2010

Basic threading in JavaScript; multiple threads of execution in JavaScript

I was searching the web yesterday for this and didn’t really find a simple way of doing this and suddenly, I remembered something.

It’s extremely simple, someone could build on it with all sorts of features but for now, here is an example:
http://images.code-head.com/code/javascript/js-threads.html

As you can see there are two counters, one is counting up and the other down simultaneously.

Here is the start_thread function:

             function thread_start(callback) {
		setTimeout(callback, 1);
		return true;
	}

The trick is that setTimeout *does not* block the execution ;)

I hope this helps someone :)

Basic threading in JavaScript; multiple threads of execution in JavaScript
Comments (1)   Filed under: Fun, General, JavaScript, Programming, jQuery   Posted by: Codehead on June 30, 2009

A PHP Competition 2

Say that you are generating an XML sitemap for your site and there are 5,000 URLs.

Here is the code:

<?php
 
	$buffer =
'<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
';
 
	$results = fetch_5000_urls_order_by_importance();
	$count = 0;
	while ($row = $results->fetch_next_url()) {
		$priority = ##########################################;
		$url = $row['url'];
		$date = date('Y-m-d', $row['date']);
		$buffer .= "
			<url>
				<loc>$url</loc>
				<lastmod>$date</lastmod>
				<changefreq>weekly</changefreq>
				<priority>$priority</priority>
			</url>
			";
	}
 
	$buffer .= '
	</urlset>
	';
 
	echo $buffer;
 
?>

What you want to do is to give each URL a priority; URLs between 0 to 1000 will have a priority of 0.9
1000 to 2000 => 0.8
2000 to 3000 => 0.7
3000 to 5000 => 0.6
4000 to 5000 => 0.5

Replace ########################################## with the code that calculates the priorities, and remember not to use ; in it ;)

A PHP Competition 2
Comments (9)   Filed under: Fun, PHP, Programming   Posted by: Codehead on March 30, 2009

Another great TED talk: David Bolinsky: Fantastic voyage inside a cell

Another great TED talk: David Bolinsky: Fantastic voyage inside a cell
Comments (0)   Filed under: Fun, Science   Posted by: Codehead on February 26, 2009
Older Posts »