Saturday, March 24, 2012

VIM Tip: Not Containing Pattern (1)

Here is one tip I used last week when I tried to use VIM to match strings with a pattern not containing a pattern. The requirement is about a group of PL/SQL scripts. I have to update some lines in the script containing "tablespace xxx", where xxx is a table space name. There is one exception: don't change "tablespace temp".

I decided to use VIM Search and Replace feature to do the job. Since my search is case insensitive, first, I use the following command to tell VIM to ignore case for my search:

:set ignorecase

Then I used the following search pattern:

/tablesapce\s\+\(t\w\+\)\@!

This search is for any line containing word "tablespace" following any spaces, but the following word  beginning with "t". The following table explains some special characters in VIM:

ItemDescription
\swhitespace character
\+matches 1 or more of the preceding characters...
\(...\)enclosing a pattern as a group.
\wwork character
\@!not containing
\nthe matched pattern in the n pair of \(\), for example, \1 for the first matched group \(\).

Here I used group pattern with containing trick "\@!", which means that the word in the group does not  contain a word beginning with "t".  In my case, "tmp" is an example.

After I verified that the search result meets my requirement. The replace is used:

%s/\(tablespace\s\+\)\(t\w\+\)\@!/\1MyTablespace/gi

\1 is the matched the group 1, i.e., "tablespace" plus white space. Here gi means global and ignore case.

Read More...

Saturday, March 17, 2012

Add Wolfram|Alpha Query Box in Blog

I have spent some time on WolframAlfram(WA) computable knowledge engine during evenings recently. What I found is that all Wolfram products, projects or apps are based on Mathemetica software or application. For example, WA, CDF(computable document format), and Mobile apps.

From WA web page, I find a link about how to add WA to blog or web sites. The HTML script is very simple one. There are serval sizes available. Here is an example to add a small size WA:

1 <script id="WolframAlphaScript" 
2   src="http://www.wolframalpha.com/input/embed/?type=medium"
3   type="text/javascript">
4 </script>

Here is what it looks like:

However, I could not find a way to put a query text into the WA input box directly. In WA, you can type a query text there, for example, pi. From there, I can get a URL about the query:

http://www.wolframalpha.com/input/?i=pi

As a result, an alternative way to add a WA query is to add a link with a query text:

Query: pi



References

Read More...

Saturday, March 10, 2012

CodeRetreat and Corey Haines

On my way home from work, I listened to Hansleminutes podcast on 3/1/2012. The podcast was a talk with Corey Haines. By the end when Hansel mentioned about Corey's coding and boarding experience, I immediately recalled the Corey! He is the man who was on .Net Rocks talk several years ago. Since then I followed him on Twitter.

His story impressed me. That's quite adventure way to update development skills. After I left his job in 2009, he started his one-year journey cross US, pair programming for a room and board. He mentioned in the .Net Rocks interview that he mention various people, include some gurus. I think that that year he learned a lot. Just like in his web page, he was like "a bee" to suck knowledge and development skills from people. Even at times he was working with people at the same or less level as him, he still practiced his presentation and personal skills. I think he was single at the that time.

I sent him a tweet to ask if he was the person on the .Net Rock podcast after the talk. I have never got his reply. That's OK. I knew that was him. Since then(by the end of 2009), I have not heard anything about him.

It was a refreshing news when I heard him again on Hanselminutes. This time, Corey talked about his project of code retreat. Basically, it is a kind intensive training, one whole day. A group of people repeat about 5 rounds of 45 minute pair programming focusing one specific problem. I really like this kind of project. I joined his coderereat organization on the same day after the podcast (in the evening).

I think that the project of coderetreat is a continuing and extension of his journeyman of programming. The journeyman was just one-to-one experience. Now he extends it to group of people.

In addition to his talk about coderettreat project, I found that he has been actively providing consulting services, talks, and some projects. In his web page, there are two web-based projects: MercuryApp and Slottd. MercuryApp provides a tracking service for any thing. Basically you score your feeling about project, word, thing or anything else. If you keep entering scores over a period of time, you may gain a view over the item you have tracked. You need to create an account with your email.



Slottd is a simple scheduler web service. You don't need an account to use. However, you do need to provide an email for your to update your schedule later one. For example, I created one meeting for the next Monday with 2 spots available. After the schedule is created, you will get two temporary URLs by email: one for update and another one for people to take the spot. It is a simple idea and quite useful. I think that this service might be a result of their code retreat projects.



All those web services are simple ones, but they are great examples to work together. Within a short period of time,  a team work on the project in Agile. Every one will learn from it by hands-on-codeing.

Another developer who has great impact on programming skills and my life is Jean-Paul Sylvain Boodhoo. When I was working as a consultant at Bantrel, JP was invited to provide one week training on .Net with TDD. That was great training. Even during the day I had to work(no contractors were invited for free), I went to the training session after work 5:00pm immediately. He was so passionate with the training. He should finish by 5:00PM, but he just went to late 9:00PM, even 11:00PM. That experience fired my up. Since then I started to keep my skills update to date with my passion.

Not sure if there are any one in Calgary area like to form a similar project. The start may have just couple of developers. This kind of activity will tremendously benefit development skills for sure.

References



Read More...

Thursday, March 08, 2012

More on XSLT Transformation

In my previous blog, I described how I used XSLT transformation to convert XML content to HTML content. That one is a very simple example. This technique is not very commonly used but I think it is very useful. As I mentioned that I used the same technique more than 10 years ago. Here I add more on this issue for my personal references.

The previous example is based on an XML file from my SQL Server Profiler tracing result: I want to convert all the interested Column nodes under Event into a HTML table. Last week, I realized that all those events can be further divided into three SQL groups based on SQL calls: Current, Data and TagInfo. It would be nice if I could create three HTML tables to summarize those results. I spent some time to explore more on XSLT transformation. The following are some related transformation elements I used.

XSL Variables


In XSL, variables can be defined for reuse. I need three HTML tables. Each has the same headers and footers. I found that by defining variables, it is a very neat strategy to create my HTML tables with constant herder and footer.

  <xsl:variable name="header">
    <tr bgcolor="#9acd32">
      <th>Row index</th>
      <th>CPU</th>
      <th>Reads</th>
      <th>Duration</th>
      <th>Writes</th>
    </tr>
  </xsl:variable>
  <xsl:variable name="footer">
    <tr bgcolor="#9acd32">
      <th>Total</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </xsl:variable>

Here are variables: header and footer, each corresponding to HTML table header and footer sections.

In the next section, you will see how those variables are used.

One limitation of XSLT variables is that they are immutable. That means you cannot change value after a variable is defined or created. I would like to have some variables to sum all CPU, Reads, Duration values, but I could not figure out how. My solution is to copy my HMLT table results and paste them to Excel to do the calculation.

XSLT If Element


I used XSLT If element as a predicate for my HMTL table content:

<table border="1">
  <xsl:copy-of select="$header"></xsl:copy-of>
  <xsl:for-each select="TraceData/Events/Event[@name='SQL:BatchCompleted']">
    <xsl:if test="starts-with(Column[@name='TextData'], 'EXEC sp_PVSQL_Template_MANUAL_Data')">
      <tr>
        <td>
          <xsl:value-of select="position()" />
        </td>
        <td>
          <xsl:value-of select="Column[@name='CPU']" />
        </td>
        <td>
          <xsl:value-of select="Column[@name='Reads']" />
        </td>
        <td>
          <xsl:value-of select="Column[@name='Duration']" />
        </td>
        <td>
          <xsl:value-of select="Column[@name='Writes']" />
        </td>
      </tr>
    </xsl:if>
  </xsl:for-each>
  <xsl:copy-of select="$footer"></xsl:copy-of>
</table>

The above codes also show the usage of my XSLT variables for my HTML header and footer sections.

XSLT Functions


In above codes, I used an XSLT function in XSLT If test: starts_with(a, b). This is a useful string function to check if string a starts with b string, case sensitive. The function of position() is used to get XML Row index, which is a reference node position in the original XML content.

However, the parameters of the function have to be in the correct case, case sensitive. I tried to use lower_case() and match() function. Unfortunately, those functions are only available in XSLT 2.0. The tool of XML Notepad does not support XSLT 2.0, nor I can find updates for the tool.

References


Read More...