Archive for the 'Programming' Category

How to include Javascript on a web page

Wednesday, February 6th, 2013

There are three ways to include Javascript files on a website. You can use scripts placed in the head element, scripts placed within the body element or you can include an external file.

The <script> tag is used to contain the Javascript code when you are placing it within the web page.

Example of code within the <head> tag:

 <!DOCTYPE html>
<html>
<head>
<title>Javascript in Head tag</script>
<script language="javascript" type="text/javascript" >
     document.write("Hello, World!");
</script>
</head>
<body>
</body>
</html>

When you are expecting to output directly into the page it is helpful to place the script in the
<body> tag.

<!DOCTYPE html>
<html>
<head>
<title>Javascript in Body tag</script>
<script>

</script>
</head>
<body>
<script language="javascript" type="text/javascript" >
  document.write("Hello, World!");
</script>
</body>
</html>

And when you are including an external script like jQuery or a custom script, you will use an
tag in the head with a src identifier to specify the file. Where you might reference the file in an onload or some other scripting within the page.

<!DOCTYPE html>
<html>
<head>
<title>Javascript in External file</script>
<script language="javascript" type="text/javascript" src="somefile.js" > </script>
</head>
<body onload="callsomefile();" >
</body>
</html>

There is also the possibility that someone will have turned off Javascript and this is where the <noscript> tag can be used to either display the site designed with no scripting involved or inform the user that they cannot use the site unless Javascript is enabled.

Tags: , ,

Awk: Separating delimited files on the command line

Wednesday, September 12th, 2012

I have had to process data files from customers that have come in as delimited files. The files are usually in CSV formats and the delimiters might change depending on the content of the files. Sometimes the delimiters are usually tabs”t”, pipes “|”, comma “,” or semi-colon “;”. A useful tool for processing this type of file is to use awk.

Example command line:

awk -F 't' '{OFS="|"; if (NR != 1) print $3,$2,$1}'

The ‘-F’ argument is followed by the character that you have the file separated on. The example above uses a tab escape sequence ‘t’ but you could have any character. One potential problem is if the data contains the same character that you are attempting to split on. As in a comma separated list using commas within a long text sequence.
The Second sequence of quoted text above starts the processing of the file. The if statement uses the NF keyword which stands for Number of Row. This tells the script that it should not fire for the first row. The print statement allows you to pick and choose the columns(numbered from 1) to use or allows you to reorder them.
The ‘OFS’ part of the command line that helps with the print statement describes the Output Field Separator. In the case above it is using a pipe character ‘|’ to separate the text as it is being printed out. This allows you comma separate the fields that you want instead of having to use a more sophisticated printf statement.

The input file is of the form

Label,name,address

And the output is going to be of the form:

address,name,Label

SQL: Get a list of dates

Friday, June 22nd, 2012

So I didn’t have a table of dates set up, but I needed to get a list of dates going
back a certain number of days. I was looking at programming up a date function but
decided that a little SQL could do just what I wanted. I used the following query
to generate my list:

select (current_date-21) +s.a as date from generate_series(0,21) as s(A);

This returned a list of dates:

2012-06-01
2012-06-02
2012-06-03
2012-06-04
2012-06-05
2012-06-06
2012-06-07
2012-06-08
2012-06-09
2012-06-10
2012-06-11
2012-06-12
2012-06-13
2012-06-14
2012-06-15
2012-06-16
2012-06-17
2012-06-18
2012-06-19
2012-06-20
2012-06-21
2012-06-22

This dealt with end of month issues and end of year issues without having to generate a ton
of code to do the same thing.

Tags: ,

Python: Handle Command line arguments with Optparse

Thursday, February 23rd, 2012

Here is my basic script for handling command line arguments in Python(pre version 2.7 when optparse
was deprecated). The optparse library is very useful in not only handling the command line
arguments, but setting up your help menu as well.

You will need to create an OptionParser and the usage argument lets you print the usage statement
for the script. Then you can add options, add groups for options or collect the left over
parameters as a secondary command or list of files. I use this method often as I create scripts
that help automate build processes.

#!/usr/bin/python

import sys, optparse

# This creates an OptionParser object with the usage string.
op = optparse.OptionParser(usage=" %prog [options]")
# This will add an option -m or --mode which takes sets the mode variable as
# True or False
op.add_option("-m","--mode",help="Specify the mode for the command", 
            action="store_true", dest="mode",default=False)
# This parses the options and places the selected options above in the _options
# object and the rest in arguments.
_options,arguments = op.parse_args()

# Checks if no arguments are passed and if so, runs the print_help method of 
# the OptionParser object
if len(sys.argv) == 1:
    op.print_help()

# This was just to show what happened when you did or didn't pass the
# --mode argument.
if  _options.mode:
    print "Mode is on"
else:
    print "Mode is off"

This method is currently deprecated as of version 2.7. I will write a follow-up post that uses
the newer argparse routine to highlight any differences.

Tags: ,

DOS: Capture return values from commands

Wednesday, February 8th, 2012

When I am writing build automation scripts, there are times that I have to insure that the previous command completed
with no errors. I have found that ERRORLEVEL gets filled in with the return value from a command.

dir %1

if (%ERRORLEVEL%) == (0) echo Found File: %1
if (%ERRORLEVEL%) == (1) echo Missing File: %1

So if you save the previous in a file and run the command with one (1) argument, it will display the output of the dir command
but also print either “Found file: ” or “Missing File: “.

Tags: ,

Python: error when using the input() function

Friday, January 20th, 2012

I was writing a basic guessing game and when I tried to take user input I was
getting the following:

Code:

guess = input()
d

Error:

Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
NameError: name 'd' is not defined

The problem is actually with the version of Python that I was using. If you
are using Python 2.x, you need to use the raw_input() function. And with 3.x
you can use the input() function.

Tags: ,

Python: if/else Statements

Friday, January 20th, 2012

There comes a time when you are writing a program and you need to branch based on input to a program.
This is the format of the if/else statement in Python.

if <some true/false statement> :
  <some statements that run if the if statement is true>
else:
  <some other statements that run if the if statement are false>  

There may be times when you have more than one value set to test against. In that case you can write a many if/else
statements but you can chain the if/else statements with an elif. So the chain becomes if/elif/else.

if x < 5:
  <some statements that run if the if statement is true>
elif x > 5 and x < 10:
  <some statements that run if the elif statement is true>
else:
  <if none of the if checks are true, run the statements in the else case.>

Tags: ,

Python: Allow for empty if else blocks

Friday, January 20th, 2012

I was toying with a prime number finder and was creating a set of if/else statements for a horribly failed attempt
and wanted to skip to the next number on any of the if statements that were true. I didn’t want to put an empty print
statement as that would disrupt the output I was expecting from the program. I found the pass statement.
Here is an example from my not very successful attempt at finding primes:

if inp > 2 and inp % 2 == 0:
  pass
elif inp > 3 and inp % 3 == 0:
  pass
elif inp > 7 and inp % 7 == 0:
  pass
elif inp > 9 and inp % 9 == 0:
  pass
else:
  print inp, " is a prime"

Yes, this does not accurately find the primes, but it would skip any number that is divisible by 2,3,5,7, or 9. And
anything that does not match this will be listed as a prime, this is incorrect and would be made to add new elif
statements as each prime was found.

Tags: ,

C: Check if string is a number

Tuesday, January 10th, 2012

I was recently testing some basic input and wrote this code to test if the input was a number or not.

#include 

int main()
{
  char name[10];

  scanf("%s",&name);
  if (checkifNumber(name))
  {
     printf("Is a numbern"); 
  }
  else
  {
     printf("Invalid numbern");
  }
}

int checkifNumber(char *inp)
{
  int i=0;
  int isanumber = 1;

  while(inp[i] != '' && i < 10)
  {
    if (inp[i] >= '0' && inp[i] <= '9')
    {
    }
    else
    {
      isanumber =0;
    }
    i++;
  }
  return isanumber;
}

One thing that could be improved is to potentially use a global variable to define the length of the
input string as it is used in two different places in the program and could potentially lead to an
array overrun/underrun.

Tags: ,

Dos: Adding commands to the PATH

Tuesday, December 20th, 2011

When you install a new piece of software that does not add itself to the Windows PATH variable, you may find your self
needing it to be run from a command prompt. You could type the full command path each time you run the executable, but
it may be easier to add the directory to the PATH variable so that it is always available.

To add it for the current command prompt:

set PATH=%PATH%;"C:Program FilesSometool"

This will add the variable to the current command prompt environment but will go away once you close the window and will not
be available to other prompts unless you type it in again.

To add it to the Windows Environment, you can use the My Computer->Properties menu to select the “Advanced System Settings”
in Windows 7 to then select the “Environment Variables…”. Now the choice you have is to add it to the System variables or
the User variables for.

If you choose the System varables you will be adding it to the system as a whole and any other user who logs in will have
the updated path. If you add it to the User variables, then it is only available to your user.

Whichever you choose, select the Path entry and click the “Edit…” button. This will pop up a dialog that will let
you add to the “Variable Value” entry. Now you will need to decide whether you want your new path at the beginning, end,
or somewhere in the middle. You might add it to the beginning if it is being used to override an existing command. You
may add to the end if there is no conflict with other installs. And finally, you may need to choose the middle if there
are some commands you need to override but others that need to be left alone.

Entries are separated by the semi-colon “;” character in DOS.

Tags: ,