Sébastien’s Coding Journey

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.

August 6, 2009

A Progress Bar Example With JQuery

Filed under: JQuery, Web development — Tags: , — Sébastien Ayotte @ 2:36 pm

In this article I present how you could use the Progress Bar provided by JQuery.

First of all, you’ll need JQuery UI. Don’t forget to create a theme that matches the colors of the site you are working on.
When you’ve done that, you’ll be able to download your custom version of JQuery UI.

Now, in the head element of the page where you want to put the progress bar, you’ll need to link the JavaScript and the CSS provided by JQuery.

<link type="text/css" href="jquery-ui-1.7.2.custom/css/custom-theme/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type="text/javascript" src="jquery-ui-1.7.2.custom/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.7.2.custom/js/jquery-ui-1.7.2.custom.min.js"></script>

In this case, I put the files under the jquery-ui-1.7.2.custom directrory under the project directory I’m working on.

Let’s pretend that we building the site in ASP 3.0 and that we need to build a table from a big Dictionary. We also want to give some feedback to our users by providing a progress bar while the page is loading.

So to do that, we’ll need to present the progress bar at is initial state.

<!-- I create the div for the progress bar. -->
<div id="progressbar"></div>

<!-- I initialize the progress bar. -->
<script type="text/javascript">
    var cpt = 0
    $("#progressbar").progressbar({
        value: cpt
    });
</script>

<!-- I flush the response so the browser show the progress bar before the page is completely loaded. -->
<%Response.Flush%>

We can now build our table while providing some feedback to the user.

<table>
    <%
    dim orders : set orders = getAllOrders
    dim orderNumber
    for each orderNumber in orders
    %>
        <tr>
            <td>
                <!-- Print some infos about the order -->
            </td>
        </tr>

        <!-- Now I need to update the Progess bar to show that the information is comming soon. -->
        <script type="text/javascript">
            // How many items do we have in the dictionary?
            var count = '<%=orders.count%>';

            // Let's calculate the work we done so far.
            cpt += 1;
            var value = cpt * 100 / count;

            // And we update the progress bar.
            $("#progressbar").progressbar('value', value);
        </script>

        <!-- Let's tell the browser that we want to show the update. -->
        <%Response.Flush %>
    <%next%>
</table>

Finally, when the page is loaded, we don’t need the progress bar anymore.

<script type="text/javascript">
    $("#progressbar").hide('slow')
</script>

Now that you’re done, you could take some time to try the demos to see all the cool stuffs you could do with JQuery UI.

Cheers :-)

August 4, 2009

Review of: Programming a Real Internet Site with ASP and HTML: Book I: HTML and Basic ASP

Filed under: Book review — Tags: — Sébastien Ayotte @ 2:50 pm

I finished reading Programming a Real Internet Site with ASP and HTML: Book I: HTML and Basic ASP to introduce myself to ASP 3.0. Like I said before, I changed job and I needed an introduction into my new environment.

In a glance, this book is deprecated. It makes you build a site with tables and framesets. If you’re an absolute beginner in web development, maybe you could read this book. But I recommend that you go on the w3schools instead. I let a review of this book on the Amazon web site. If your curious, you could go check it out.

It’s not easy to find good books on a quite old technology. It makes me wonder if we should upgrade?

Older Posts »

Blog at WordPress.com.