Sunday, February 7, 2010

Regular Expressions - 2

Alternation:
Difference between a character class and alternation is that alternation can be used to match alternates of entire regular expressions while character classes match only one character. However alternation cannot be negated like a character class.

Ignoring differences in capitalization:
We can tell egrep to ignore capitalization differences by using the '-i' option. But remember that this is not part of the regular expression language itself.

Word boundaries:

Metacharacters:
There are different metacharacters within a class and outside of it. Sometimes the meaning of a metacharacter also changes depending on whether it is within a class or outside of it.

. Matches anything
[...] Represents a character class
[^..] Neggated character class
^ Start of a line when outside a class, negation when the 1st char in a class
$ End of a line
\< Start of a word
\> End of a word
| Alternation
() Used to limit the scope of alternation... there are other uses as well
- Used to specify ranges within a character class
? 0 or 1 match (signifies an optional match)
+ Matches 1 or more
* Matches any number including none (0 or more)

Example:

July? (fourth|4(th)?)
This means match anything which has a 'J' followe by a 'u' followed by a 'l' which may (optional) be followed by 'y'. Following this we may have a 'fourth' or a '4', or '4th'.


Saturday, February 6, 2010

Regular Expressions - 1

Regular expressions by practice

I want to search for my name in a large text file. This is a case where we can use simple regular expressions.

egrep Parag file.txt

Will search the file called file.txt and print all lines which have the word 'Parag' in it. But normally things are not as simple as this. What if I suddenly remember that some instances of Parag may not have the first character in upper case. I want to search for all instances of 'Parag', or 'parag'. So basically I want to search for a word which begins with either 'P', or 'p', followed by 'arag'. How do I specify 'P', or 'p'? It can be done with what is known as character classes. A character class essentially results in a match when anythin specifed in a character class matches.

egrep [Pp]arag file.txt

will print all lines containing 'Parag' or 'parag'

Tuesday, August 11, 2009

Python lists

Here are some methods which can be used with lists in Python

We will first create a list to represent a bag of flowers:

flowers = ['rock banana', 'swollen finger grass', 'velvet leaf', 'turmeric', 'rock banana']

Let's first determine how many rock banana flowers we have in the list:

>>>flowers.count('rock banana')
2

At what index in the list does 'swollen finger grass' appear?

>>>flowers.index('swollen finger grass')
1

Let's insert a rose in there at index 2
>>>flowers.insert(2, 'rose')
>>>print flowers
['rock banana', 'swollen finger grass', 'rose', 'velvet leaf', 'turmeric', 'rock banana']

Let's say we want to delete the rose
>>>flowers.pop(2)
rose
Notice that the pop() method returns the element. If we just want to delete without returning the element, then we can use the del() built-in function in Python. I have discussed built-in functions in this blog post.

What if we want to remove an element by name. Let's remove 'swollen finger grass' using the remove() method.

>>> flowers.remove('swollen finger grass')
>>> print flowers
['rock banana', 'velvet leaf', 'turmeric', 'rock banana']

We have already spoken about slicing in the blog post on sequences. There are a few more things we can do with slicing, like replacing elements, and removing them as well.

Let's create a simple list of numbers:
numbers = [3, 5, 4, 7, 4]

Let's insert a number at the 3rd position in the list
>>>numbers[2:2] = [10]
>>>print numbers
[3, 5, 10, 4, 7, 4]
Notice that when we use slicing to assign insert into a list, we have to use an iterable for the value we insert. This is the reason we use [10] and not 10

Let's replace some elements of the list
>>> print numbers
[3, 5, 10, 4, 7, 4]
>>> numbers[3:5] = [14, 17]
>>> print numbers
[3, 5, 10, 14, 17, 4]

Let's delete the 4th element from th list
>>> numbers[3:4] = []
>>> print numbers
[3, 5, 10, 17, 4]

Monday, August 10, 2009

Python built in functions

Some built in functions in Python

Let's make a simple list of numbers

>>>numbers = [2,4,3,6,5]

To get the number of elements in numbers, we can use the len() method

>>>len(numbers)
5

To get the max element in the list we can use the max() method

>>>max(numbers)
6

The max() method is not restricted to lists of numbers only. It can work with any iterable.

Just like the max function returns the largest value from an iterable, the min() function returns the smallest value.

>>>min(numbers)
2

Suppose we have a String "Hello" and we want to create a list of the characters of the String, we can use the built-in list() function.

>>>list("Hello")
['H', 'e', 'l', 'l', 'o']

The list() function is not limited to String, we can give it any iterable and it will return a list of the elements of the iterable in the same order as they appear in the iterable.

If we want to delete an element in an iterable, we can use the del function
>>> hello_list = list("Hello")
>>> del hello_list[0]
>>> print hello_list
['e', 'l', 'l', 'o']

We can determine the type of an object using the type() function.
>>> type(hello_list)

Slicing in Python

Python supports slicing of sequences. Just watched a video on Python slicing here.

My notes follow:

Slicing a sequence is the act of taking a sequence and getting back some elements from it. These elements couls be consecutive elements or they could be spaced at an interval.

Let's create a list and then slice it

my_list = [2,3,4,5,6,7,8,9,]

print my_list[2:7]
Will return all elements from the 2nd to the (7-1)st position.
[4, 5, 6, 7, 8]


print my_list[2:]
Will print all elements from the 2nd element to the end of the list.
[4, 5, 6, 7, 8, 9]


We need not get only consecutive elements. They can also be spaced.
print my_list[2:7:2]
Will print elements spaced at a distance of 2 from the 2nd element to the 6th element.
[4, 6, 8]


We can also address the list using -ve indexes.
print my_list[-5:-1]
Will return all the elements starting from the 5th element from the end going upto the element before the 1st element from the end.
[5, 6, 7, 8]


If we want to print from the 5th element from the end to the last element, we need to use
print[-5:]
[5, 6, 7, 8, 9]


Lists using -ve indexes can also be spaced.
print my_list[-5:-1:2]
[5, 7]

Python Sequences

I just viewed some videos on Python sequences and lists at adaptivelearningonline.net . My notes follow:

  • A populated tuple need not be enclosed in parameters. Does that mean that
the print method in
print "Hello", "Again"

takes a tuple of Strings?
  • Even though ['h','e','l','l','o'] and "Hello" are both sequences, we cannot concatenate them, because we cannot concatenate the non list with a list.
  • Sequences can be multiples... "Hello"*3 will result in "HelloHelloHello"
  • Lists support a sort method. This method will take a list of objects and sort the list. This method does not return anything.
  • Python has a sorted() function which takes a String and sorts it. It returns a new string which is the sorted string.

Python Strings

Just watched the basic and advanced tutorials on Python Strings over at adaptivelearningonline.net . Here are my notes on Python Strings.

When we concatenate a String with another object, we will get an Exception if the other object is not a String. In such cases we should convert the other object to a String. There are a few methods which will convert non string objects to Strings. Assuming obj is a non string.

  1. str(obj)
  2. repr(obj)
  3. `obj`
Strings have a find method which will find a substring within a String and return the index where the substring starts.

We can change the case of a String using the upper() and lower() methods. Note that these methods return the changed String and do not change the existing String (since String objects are immutable)

>>> s = "HeLlo"
>>> upper(s)
>>> s.upper()
'HELLO'
>>> s.lower()
'hello'
>>> s
'HeLlo'

Strings also have a join method which can be used to join a list with a String, such that the String delimits every element from the list.

>>> words = ['hello', 'howdy', 'namaste']
>>> s = ':'
>>> s.join(words)
'hello:howdy:namaste'

We can replace characters or parts of a String using the replace() method.

>>> hello = 'hello'
>>> hello.replace('e', 'g')
'hgllo'
>>> hello.replace('hell', 'heaven')
'heaveno'

Wednesday, August 5, 2009

Random learnings 2009-Aug-05

VIM split the screen:
:split
:vsplit

To determine the type of a Javascript object at runtime, use typeof(o)

I changed the DATE_FORMAT and TIME_FORMAT properties of the DjangoJSONEncoder in code.

In Python class attributes are declared in the class definition, wheres instance attributes are declared in the init method.

Friday, April 3, 2009

XML Prologue

An XML prologue is the part that appears before an XML document. The XML prologue is optional in XML documents. If present it could contain a declaration (version, and encoding), processing instructions, and the doctype.

The prologue must not have any blank lines or anything else before it.

For details: http://lachy.id.au/log/2006/09/xml-prolog

Saturday, March 21, 2009

Cascading Style Sheets

Learning about CSS Selectors in the CSS online tutorial

Selectors select (identify) HTML elements and apply a style to it. There are many selectors but the most important (common) ones are:

Type (Element) Selectors:
These selectors identify HTML based on the HTML type, such as body, p, h, etc. The CSS rule for a type is defined the the css file with the name of the type:

h1 {
background-color:black;
}

This rule will apply the defined style (background color is black) to all H1 heading elements.

Class Selectors:
These selectors identify HTML elements based on the class they belong to. The CSS rule for a class is defined in the css file with a .classname

.javacode {
font-size: 10pt;
}

This rule will be applied to all elements which have the class defined as javacode:


String s = new String("Hello World");


Id Selectors:
Id selectors select HTML elements based on the id of that element. An Id selector is defined in the css file as #idname

#prettyprint {
font-color: red;
}

This rule will be applied to all elements that have their id set to prettyprint


def s = 'hello'


One question that comes to mind is what happens if I have a rule defined for the class javacode and the id prettyprint, and I have an HTML element whose class is javacode and id is prettyprint. Which rule will take precedence?

I have asked this question as a comment on the blog post.

Sunday, March 15, 2009

Cascading style sheets

There are four levels at which stylesheets rules are defined - from the highest to the lowest priority:
  • Inline definitions
  • Embedded style sheets
  • External style sheets
  • Browser defaults

Tuesday, March 10, 2009

CVS Root file

If a project is under CVS control, every directory in the project will have a CVS folder which contains 3 files:
  • Entries
  • Repository
  • Root
The 'Entries' file contains an entry for every directory under the current directory which is under CVS control

The 'Repository' file contains details of the repository path

The 'Root' file contains details for CVSROOT.
:pserver:<user_name>:<password>@:<cvs_path>
The password is optional and ideally should not be used. However, it can be put in here if needed. </cvs_path></password></user_name>

Wednesday, September 24, 2008

equals and hashCode

Whenever we override the equals() method in any class, we must also override the hashCode() method.

if(o1.equals(o2))

then

o1.hashCode() == o2.hashCode()
must also be true.

For more details - http://www.ibm.com/developerworks/java/library/j-jtp05273.html

Creating Booleans

FindBugs suggests that we should not instntiate Boolean objects like this

Boolean b = new Boolean(false);

instead we must use the creational method

Boolean b = Boolean.valueOf(false);

This is better for performance, since the Boolean object caches 2 immutable Booelan objects for true and false values.

Sunday, July 29, 2007

RENDER_RESPONSE Phase

The RENDER_RESPONSE phase is also responsible for saving the state of the component.
When I first read this, I thought, should not the method that invokes this save the state of the component? However, this will not allow us to implement saving the state on the client. Because when we save the state on the client, the state must be saved before the response stream is closed (actually before the response is totally rendered).

Be careful to not delete/modify any components in the saveState method.
If we change the state of components in the saveState method, we risk jeopardizing the integrity of the component.

Should all HTTP requests always be to the component that was last rendered?
I am not sure... need to think about it???

Wednesday, June 27, 2007

Reading the JSF spec

Application.getDefaultRenderKit() should (in my opinion) never return null. [pg 231]

Monday, June 25, 2007

JSF component id and clientId

JSF components have a clientId as well as an id.

The id of a component is the id that it chooses to give itself. This may be specified or can also be auto generated by the JSF framework.

The clientId is the id that is transmitted to the web client. For a component, it's clientId is the clientId of the closest parent that implements NamingContainer followed by the JSF seperator char followed by the clientId of this component.

A little code snippet that implements getClientId().



click on the image to get a better view

Monday, June 11, 2007

JSF phase events

The phaseId property of ActionEvent indicates which phase of the request processing lifecycle, this event should be processed.

The broadcast method is called after each phase (where events can be processed). By default it processes all events that have the same phaseId as that of the current phase.

Hence, if we want an event to be processed in a particular phase, we should set the phaseId of the event to the appropriate value, so that the broadcast() method will process it at the correct time.

Do we invoke broadcast manually, or is it invoked when we call super.whatever() in the appropriate phase?

JSF rendererType

The rendererType to use for rendering a JSF component really exists as a property in the component itself. We can call setRendererType(String rendererType); to set the rendererType.

When the component is being rendered, the appropriate renderer will be selected based on the rendererType. The runtime knows which renderer is mapped to a rendererType, because we map it in faces-config.xml. However, it can also be set in code, I believe.

When we use JSP tags to represent JSF components, then the tag handler class can also have a method getRendererType(); which returns the rendererType for that component. This is convenient because the JSF runtime will query the tag handler for the rendererType and will set it in the corresponding JSF component.

Tuesday, May 22, 2007

Some common component properties

Most components support a certain common set of properties. Among them are 'value', and 'binding'.

value - The component's current local value. Can be literal text or a value binding expression
binding - A value binding expression that associates this component to a backing bean property

If the 'value' property is a value binding expression, the value entered by the user in an input component will also be updated in the appropriate backing bean property, if the value passes all validations.

On first glance, the difference between 'value', and 'binding' is not very clear. I think (need to verify), 'value' (when used with a value binding expr) is used to transfer the user's input into a backing bean property, whereas 'binding' is used to associate a component with a backing bean property so that the backing bean can manipulate the component within code (Hence the type of the backing bean property must be the same as the type of the component).