Sébastien’s Coding Journey

January 8, 2010

JavaScript isNumeric function

Filed under: JavaScript — Tags: — Sébastien Ayotte @ 10:20 am

Here is a JavaScript function that defined if a value is numeric or not.

function isNumeric(value) {
    return value == parseFloat(value);
}

Cheers :-)

January 7, 2010

JavaScript String.startsWith and String.endsWith implementation

Filed under: JavaScript — Tags: — Sébastien Ayotte @ 8:58 am

While I was reading Ricky Rosario’s blog I took note of an interesting way to implement the startsWith and the endsWith methods for Strings. It may be useful.

String.prototype.startsWith = function(prefix) {
    return this.indexOf(prefix) === 0;
}

String.prototype.endsWith = function(suffix) {
    return this.match(suffix + "$") == suffix;
}

Cheers :-)

November 5, 2009

Looping in a form’s elements in JavaScript

Filed under: JavaScript — Tags: — Sébastien Ayotte @ 11:33 am

Sometime you need to prevalidate some inputs in a form. You could do this by looping in all the elements of the form. Here is a quick example of doing just that.

<html>
    <head>
        <title>An example.</title>
        <script type="text/javascript">
            function loopInFormElements(aForm) {
                for (i = 0; i < aForm.elements.length; i++) {
                    alert(aForm.elements[i].name + " = " + aForm.elements[i].value);
                }
            }
        </script>
    </head>
    <body>
        <form onsubmit="loopInFormElements(this);" action="" method="post">
            <input type="text" name="value1" value="1" /><br />
            <input type="text" name="value2" value="2" /><br />
            <input type="text" name="value3" value="3" /><br />
            <input type="submit" name="submit" value="submit" />
        </form>
    </body>
</html>

What happen here is when I submit the form, the loopInFormElements method get called. This function loop in all the form’s elements and show the name and the value of the inputs.

I thought it could be handy.

Cheers :-)

November 4, 2009

A note about standards

Filed under: Development — Tags: — Sébastien Ayotte @ 2:46 pm

Just a quick thought about standards. You should try to keep them simples.

And please, don’t push to hard on best practices. The’re not silver-bullets.

Enough evangelizing for today :-P.

Cheers :-)

October 29, 2009

Private methods in Python

Filed under: Python — Tags: — Sébastien Ayotte @ 1:50 pm

Python use two underscores to hide a method. You can use two underscores to hide a variable too.

class MyClass:
    def publicMethod(self):
        print "Public"

    def __privateMethod(self):
        print "Private"

So now you can call publicMethod().

def main():
    myClass = MyClass()
    myClass.publicMethod()

But you can’t call __privateMethod().

def main():
    myClass = MyClass()
    myClass.__privateMethod()

Traceback (most recent call last):
  File "privatetest.py", line 13, in <module>
    main()
  File "privatetest.py", line 10, in main
    myClass.privateMethod()
AttributeError: MyClass instance has no attribute 'privateMethod'

Of course there’s a way to call a private method anyway. But why would you do that?

October 23, 2009

A Simple Pygame Screen

Filed under: Pygame — Tags: , — Sébastien Ayotte @ 1:29 pm

Here’s a simple example of a game screen written in Python and Pygame.

import pygame

from pygame.locals import *

def main():
    # I'm initializing the game's screen.
    pygame.display.init()

    # I set the width and the height of the screen.
    screen = pygame.display.set_mode((300, 200))

    # I'm setting the title of the game's screen.
    pygame.display.set_caption("The game's title")

    # I want to hide the mouse when It over the game's screen.
    pygame.mouse.set_visible(0)

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False

if __name__ == "__main__":
    main()

Cheers :-)

October 21, 2009

A Python ODBC connection

Filed under: Python — Tags: , — Sébastien Ayotte @ 8:40 am

I was fooling around with Python and I wanted to connect to my database using ODBC. While googling on the subject, I found pyodbc. It was quite easy to install this module on my Windows box and I was up and running in no time.


import pyodbc

def main():
  connection = pyodbc.connect("Your Connection String")
  cursor = connection.cursor()
  cursor.execute("select something from myTable")
  for row in cursor:
    print row.something

if __name__ == "__main__":
  main()

Of course, you need to provide a real conntection string.

Cheers :-)

September 10, 2009

Review of: Getting Real

Filed under: Book review — Tags: — Sébastien Ayotte @ 7:30 am

I just finished Getting Real from 37 signals. This book give some insights on how software should be made.

Here some key points from the book :

  • Be honest
  • Give feedback
  • Eliminate the noise
  • Get things done

Don’t try to look corporate. You don’t need to put emphasis on professionnalism. It’s just a matter of fact. Don’t loose your time on all this noise.

Just keep things simple and go make software.

September 3, 2009

The disturbing Automatic Updates Popup

Filed under: Mood — Tags: , — Sébastien Ayotte @ 1:43 pm

I think that Automatic Updates on Windows help the user to keep his computer up-to-date. But when I press the tiny button Restart Later I really mean to Restart Later.

Disturbing Popup

Disturbing Popup

So when I’m working I don’t need a reminder that will popup every 10 minutes to ask me if I wish to restart my computer. It’s for that reason that I press the Restart Later button.

Why it still bugging me? Why it still asking me? Why…

I guess I should use the Restart Now button instead.

August 31, 2009

How to enable compression in IIS 6.0

Filed under: IIS 6.0 — Tags: , — Sébastien Ayotte @ 10:11 am

Bandwidth is important for a web application and you should try to keep it as low as possible. You could edit your web pages to strip all the spaces and to put the code on one line. But I think is a very ineffective method cause you creating a nightmare for the application’s maintainers (that’s probably you) and you will not be able to do a better job than a compression algorithm. You should be searching for  compression filter instead.

I’m currently working with IIS 6.0. So I will demonstrate how to enable compression for this server. I know that this concept is supported by other servers to.

Enable compression in IIS
  • Open the IIS Manager
  • Right click on the Web Sites and click on Properties
  • In the Service tab check Compress application files and Compress static files
  • Save your changes

The server looks for the HTTP header Accept-Encoding in the received requests. If the compression is accepted (Accept-Encoding: gzip, deflate) the server will compress the response before sending it to the client.

Change the compression level
  • Edit C:\Windows\system32\inetsrv\metabase.xml
  • Search for IIsCompressionScheme
  • You should have 2 compression schemes (gzip and deflate)
  • Set the HcDynamicCompressionLevel (between 0 to 10) to 9 for the 2 schemes
  • Make sure the file extensions served by the server is in HcScriptFileExtensions for the 2 schemes
  • Reset the IIS Server

A HcDynamicCompressionLevel of 0 is the lowest compression level while 10 is the highest compression level. Be aware that a high compression level means more work for the server’s CPU.

The HcScriptFileExtensions indicate which files to compress. In my case I want my ASP files to be compress. Here a snippet of my C:\Windows\system32\inetsrv\metabase.xml.

<IIsCompressionScheme	Location ="/LM/W3SVC/Filters/Compression/deflate"
		HcCompressionDll="%windir%\system32\inetsrv\gzip.dll"
		HcCreateFlags="0"
		HcDoDynamicCompression="TRUE"
		HcDoOnDemandCompression="TRUE"
		HcDoStaticCompression="FALSE"
		HcDynamicCompressionLevel="9"
		HcFileExtensions="htm
			html
			txt"
		HcOnDemandCompLevel="10"
		HcPriority="1"
		HcScriptFileExtensions="asp
			dll
			exe"
	>
</IIsCompressionScheme>
<IIsCompressionScheme	Location ="/LM/W3SVC/Filters/Compression/gzip"
		HcCompressionDll="%windir%\system32\inetsrv\gzip.dll"
		HcCreateFlags="1"
		HcDoDynamicCompression="TRUE"
		HcDoOnDemandCompression="TRUE"
		HcDoStaticCompression="TRUE"
		HcDynamicCompressionLevel="9"
		HcFileExtensions="htm
			html
			txt"
		HcOnDemandCompLevel="10"
		HcPriority="1"
		HcScriptFileExtensions="asp
			dll
			exe"
	>
</IIsCompressionScheme>

There you go folks. Your now saving some bandwidth. I hope it makes a difference for you cause it makes one for me.

Older Posts »

Blog at WordPress.com.