Skip to main content

Non capturing group in regular expression and it's use

Recently I have been stuck in a regular expression where I had to use grouping in places, values of which were not required. It won't make sense to explain the problem without the text. So here it is


Controller Id Connection Status Connection State Secure Role
------------- ----------------- ---------------- ------ ------
1             Connected         Active           No     Slave
25            Disconnected      Idle             No     Master


My goal was to get the numeric values at the beginning of the line while still matching the whole line for safety (so that any other numeric values in the output don't match). As you see in the output, each column can contain a value from a list of fixed values. e.g. Connection Status can only contain Connected or Disconnected. That demands the following regular expression -




But with the above expression, we are fetching all the columns when all we want is the first column. This is where the non capturing group of regular expression comes …

Read a log file in tail mode from python

When you want to develop a log reader application, you'll need to keep reading the log. There is a Linux utility doing exactly the same. Its the tail command. tail blocks on the file till new entries are appended to it.

I needed the similar functionality in python and there was a small library providing it already. Its called filetail.py. The program performs tail functionality and also handles log rotation.

But in my case, the log file used to change (file name) depending on the date. filetail could not handle it as the file name has changed already. To handle this I have made following changes to the filetail code -

In nextline() method file size should be checked to be less or equal to the cursor position:


                # maybe the log got rotated out from under us?
                if stat(self.path)[ST_SIZE] <= self.pos: # modified by sarbajit
                    # file got truncated and/or re-created
                    self._reset()

In _reset() method I have added the logic to re-create the log file name from the current date value and replace it with the original file name:

    def _reset(self):
        """Internal method; reopen the internal file handle (probably
        because the log file got rotated/truncated)."""
        self.f.close()

 # modified by sarbajit
 localtime1 = localtime(time())
 year = str(localtime1[0])
 if localtime1[1] < 9:
  month = "0"+str(localtime1[1])
 else:
  month = str(localtime[1])
 if localtime1[2] > 9:
  date = str(localtime1[2])
 else:
  date = "0"+str(localtime1[2])
 logfile_name = "applog."+year+"-"+month+"-"+date+".log"
 self.path = "/var/log/" + logfile_name
 # end of modification

        self.f = open(self.path, "r")
        self.pos = self.f.tell()
        self.last_read = time()

I apologize for not following standards of coding here. It served the purpose for me.

Comments

Popular posts from this blog

Call an external program from MySQL trigger

MySQL is the choice of many when it comes to database. Its free and quite robust.
During one of our prototype implementations we had a requirement of calling some external processes when there is a change in a MySQL table. MySQL triggers are provided for the same purpose. They get executed when the table is changed in certain ways that is specified by the programmer. Now it is very easy (rather trivial) to do some thing in other MySQL database tables when trigger gets fired. But, our requirement was to call a C program.

Fortunately MySQL provides a way to implement your own functions, its called User Defined Functions (UDF). The "how to" is here. Now that we know, how to define your own functions and call them from MySQL events, we need to write our logic in a C program by following the interface provided by MySQL and we are done.

Wait a minute. That is already done by somebody. They have made a library of UDFs. One of them, LIB_MYSQLUDF_SYS does exactly what we want. It al…

Using Multi threading with Drools

Drools is a very popular rule engine. It is based on Rete algorithm. To understand what is a rule engine and how to use it check here. But here I will assume that you have some working knowledge on Drools.

What I am going to discuss in this post is 'how to use multi-threading with Drools'. Below is a snippet which creates a KnowledgeBase from a DRL file.



public void buildKnowledgeBase() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newUrlResource( "file://myrules.drl" ), assertFalse( kbuilder.hasErrors() ); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); }
If this method is called from multiple threads I get below exception -



Exception in thread "Thread-60" [Error: incomplete statement: (possible
use of reserved keyword as identifier: )]

[Near : {... globals != empt ....}]

^

[Line: 0, Column: 0]

at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterp…

Get JSON data response with JQuery Form plugin for a file upload form

My latest project involves JQuery for UI development. Frankly speaking, I did not have much idea of JQuery before. But I found it very easy to understand. All APIs are well documented with lots of examples available on Internet. The best feature of JQuery is its plugins. There are thousands of plugins available to play with. For every need of my web app I could find a plugin.



In my web app I am using JQuery Form plugin to submit the forms through AJAX. The return from my servlet is JSON object. The form plugin works great for all my forms. Well almost all, there is a HTML form which has a file upload box along with other input fields. This form was not able to get the response in JSON format. Though it worked in Google Chrome, but Firefox was presenting me with a file save dialog box.
I did a lot of googling to find out whats going on. Finally I observed that the problem is with HTTP headers that the form submit request has. Here are the headers captured by Chrome







As you can see the