Archive for the ‘Web’ category

All the stuff that's supposed to entertain us. Including films, video games, comics, porn and mangos.

10 things I hate

Sunday, March 16th, 2008

10. Wii

According to some, the only console that brought some innovation to gaming overall. According to me, the biggest crap Nintendo has ever released. Wii is generally like communism - good in theory, sucks in practice. It could have revolutionised video gaming, it could have been the best console, it could have easily defeat its rivals. But it didn’t. I mean, it actually is the best-selling console on the market, and it reached the kind of customers no one has ever managed to reach before - casual gamers, but Nintendo have completely forgotten about the more important consumer base, the hard(er?)core gamers. Because nobody wants to play Mario all the fucking time.

9. Public transportation

I actually have to use public transport to get to and from school almost everyday. And of course, it has many advantages - it’s cheap, relatively ecological and environment-friendly, and often there are the bus lanes which are really in case of a huge traffic jam. But the list of drawbacks is much longer, unfortunately.

First of all, public transportation in Kraków (where I live) is free for people over 70. One could think “heh, how many 70-year-olds would actually use public transport?”. Surprisingly, the answer is: all of them. The buses and trams, quite crowded without the billion grannies using them, often are filled with people almost standing on each other’s head because of the lack of room to breathe.

Another group of annoying people travelling by the same buses as I do that are the stinking homeless garbage collectors. Oh man, if you happen to be on a crowded bus with a bum standing next to you, preventing yourself from throwing up will be a very tough job.

The last annoying thing in public transportation I’d like to point out are the screaming children. When one of them starts crying, and by chance there’s another one riding the same bus, he will probably start crying as well, and then the third one, and the fourth one… This can drive you mad. Even though I do always wear earphones when travelling by bus, to clarify that.
I’m gonna have to get a driving license as soon as I can.

8. Pi (π)

Not that I hate it, I just hate the ambiguity it brings to mathematics. While you can give the exact area of a rectangle, (in most cases) triangle or even most of polygons, while working on circle- or sphere-related calculations, the exact value of pi is never known. Whether you will substitute it with 3.14, 22/7 or 3.14159265358979323846264338327950288419716939937510, you will never get the accurate result.

While the value of pi has been computed to more than a trillion (1012) digits, elementary applications, such as calculating the circumference of a circle, will rarely require more than a dozen decimal places. For example, a value truncated to 39 decimal places is sufficient to compute the circumference of any circle that fits in the observable universe to a precision comparable to the size of a hydrogen atom.

Wikipedia

And even though the information I’ve found on Wikipedia (above) cheers me up, I never feel fully satisfied with my circumferences, because I know that pi’s expansion is infinite. That’s why I don’t use rounded corners on regua.biz.

(more…)

OOP in PHP (part II)

Sunday, December 16th, 2007

Time to discover the hidden powers of the object oriented programming in PHP. Today I’ll describe the usage of serialisation, the secrets of overloading and - probably the most useful thing today - design patterns.

Serialisation

As you remember from the first part of the article, objects are completely different type of variables. You could easily write a string, number or even an array to a file… but what about objects? You can’t just write an object to a file, as PHP will try to convert it to a string, which is technically impossible, and will result in throwing an error. Yeah, I guess you can already suppose that there’s a solution.

Actually, all you have to do to serialise an object (i.e. make it writable to a file) is use one function: serialize(). As you can guess, using unserialize() function will allow you to have your object back once it has been saved to a file. Remember to define the object’s class before unserialising it, otherwise it will become useless.

Methods __sleep() and __wakeup() make serialisation in PHP5 even easier. They are called, respectively, before an object is serialised or unserialised. They are very useful e.g. for closing and reopening database connections, or saving additional information about the serialisation process. Remember that the __sleep() magic function has to return an array of all members that should be serialised.

class AutoStalker {
   private $filename, $file;

   public function __construct($filename) {
      $this->filename = $filename;
      $this->open();
   }

   public function prepare_for_stalking() {
      $this->file = fopen($this->filename, 'a');
   }

   public function stalk($text) {
      fwrite($this->file, $text."\n");
   }

   public function see_whos_been_stalked() {
      return file_get_contents($this->filename);
   }

   public function __wakeup() {
      $this->prepare_for_stalking();
   }

   public function __sleep() {
      fclose($this->file);
      return array('filename');
   }
}

In the example above, I only serialise the file’s name, because we don’t need any additional information to open that file in the future.
Now, let’s see something more useful; how about using some PHP cookies?

(more…)

OOP in PHP

Sunday, October 14th, 2007

It’s been a long time since my last post. Sorry for this to all of you who have been looking forward to my next article (yeah, both of you!).

I do like functions. I even try to use functions instead of classes wherever it’s possible - either it’s just because I got used to it by writing PHP for two years (as coding object oriented scripts in PHP 4 was actually impossible), or I’m too stupid to understand the real power of OOP. And even though PHP’s authors have been trying to implement a nice object structure, it still sucks. However, as I consider PHP to be the nicest programming language, I decided to describe its OOP powers.

Why?

Let’s start with the definition of OOP: by using objects, it allows you to keep your code clean and prepared for further development. I suppose you still don’t know what an object is. Think about this: you create a function, let’s call it omg_am_i_pregnant($user), that will check whether the user is pregnant. It actually can return some data, but it’s very difficult to reuse it afterwards. It would probably look like this:

function omg_am_i_pregnant($user) {
   if (user_exists($user) && is_pregnant($user)) {
      $p = true;
   }
}

By doing the same thing with a class called pregnancy, you can easily store the data and send it to, let’s say, a sperm bank.


class Pregnancy {
   function __construct($user) {
      if (user_exists($user) && is_pregnant($user)) {
         $this->p = true;
      }
   }
}

Now, you may think “WTF are you talking about, the second piece of code is longer and does the same thing”. Yeah, but now, let’s try to check whether Anne, Isabelle and Lucy are pregnant and store the data simultaneously.

$anne = omg_am_i_pregnant('Anne');
$isabelle = omg_am_i_pregnant('Isabelle');
$lucy = omg_am_i_pregnant('Lucy');

The code above won’t work properly, as the function saves the result to the $p variable. Of course, this could be done many other ways, but what if you had to do it this way, using $p?

(more…)

Ajax Comment Posting

Tuesday, August 14th, 2007

When I installed WordPress a few weeks ago, I didn’t think it would actually be my blogging system (as I don’t like people who write a script and don’t update the code for 10 years saying “we’re making WordPress backwards compatible!”). However, I eventually liked it. And I have an old custom - whenever I like something, I write a plugin to make it even better ;) And so I did.

Ajax Comment Posting will post your comments in an Ajax-way, without refreshing the page. It’ll also validate the comment form on the client and server side. I also worked a bit on the compatibility with older WP version, and so even v1.5.2 users can enjoy ACP! It was supposed to be just a simple comment-posting plugin, but I’ve got some emails complaining that it and the Ajax Edit Comments plugin can’t co-exist, so in a month’s time ACP’s functionality will be extended to allow users and admins to manage comments (in an easy, Ajax way). Thanks to Gene Steinberg for suggesting the further development of the plugin, and to Rayne Bair for pointing some errors - they all will be fixed in the forthcoming version of ACP.

You can read more about the plugin and see some screenshot on its page on regua.biz or in the WP plugin directory. Any suggestions are welcome.

Edit: ACP has been downloaded over 3,000 times! Thanks for your support and help with the plugin.

[siː dʒiː aɪ]

Tuesday, August 14th, 2007

Note: if you can’t see the title, it means your browser can’t display Unicode characters properly.

The Common Gateway Interface (CGI) is a standard protocol for interfacing external application software with an information server, commonly a web server. This allows the server to pass requests from a client web browser to the external application. The web server can then return the output from the application to the web browser.

Wikipedia

Today, I’ll explain how to generate Web pages using Python - the easiest programming language ever. They won’t be just static pages, however; in the article there will be examples of CGI scripts processing data from HTML forms, uploading files to the server, some GET and POST requests and even a simple how-to showing how to set up a small CGI server. However, this article is about using CGI in Python, so I assume you have some knowledge of the Python syntax.

The simpliest of the simpliest

Let’s start with a simple static page. The first line of any CGI script has to point to the location of Python. It’ll usually be /usr/bin/python on most Unix-based servers. Next, we have to inform Python that the output of the script is actually HTML (and put an \n at the end of this line)- and the rest of the code is up to you. Remember that all CGI scripts need permissions at least equal to chmod 755.

#!/usr/bin/python
print "Content-type: text/html\\n"
print "OMG, it's actually working!"

You can see the script working.
Of course, this script doesn’t even include the most basic html tags such as <html> and <body>, so have a look at a second example:

#!/usr/bin/python
print "Content-type: text/html\\n"
print """<html>
<head>
<title>OMG</title>
</head>
<body>
<h2>It's actually working!</h2>
<img src="some_crappy_image_source">
</body>
</html>"""

Again, you can see how it works.

(more…)