Saturday, June 12, 2021

WebAPIs Documentation using Flask, Postman

I've started a new journey - one that will hopefully make me a good technical writer. I am specifically interested in writing and documenting web services APIs based on REST. 

I plan to use Flask as the web server and Postman as the testing/documentation tool. There are several alternatives to this but given that Python is my strongest programming skill as now and Postman seems easy enough to use, I will stick to these options.

My future posts will document my journey and hopefully help me build a portfolio and help others.

Tuesday, August 23, 2011

O'reilly Arrogance ?


A week ago. I tried to register for the Python Programming Certificate on the O'Reilly School Of Technology website. There appeared to be a glitch and I saw the following error message :


It seems there was a glitch on our end. Please report this error to us at support@oreillyschool.com, and if possible include your browser and operating system so that we can determine the problem. We sincerely apologize for this inconvenience.



I sent an email to the suggested email address with all required information. 4 days later - no answer from O'reilly support. Just yesterday I sent in another request using their "Contact Us:" web form. No response yet.

This leads me to worry. Even unscrupulous businesses would take your money first and show their unprofessional ism later . With O'reilly it seems they don't want my business. Should I feel comfortable about plunking down hundreds of bucks on a set of courses from an institute that does not take the trouble to get me correctly signed up first ?

If this is how they behave up front, how could I possibly expect professional technical support later while actually taking the classes ? Is this what one ought to expect from O'reilly or is it just me ?

Thanks,

Friday, July 11, 2008

Grad School by xkcd

LOL.....;)

Wednesday, July 9, 2008

Netbeans for Python !!

Just what I was waiting for !! I was looking or a way to brush up my Java skills while continuing my year-long affair with Python when news arrived that Jython was being reincarnated with Sun's hiring of Ted Leung and Frank Wierzbicki . Seeing what Sun did for JRuby, i was all excited when came this other piece of great news that Netbeans was going to support Python/Jython with an upcoming release.

Go Sun ! Go Netbeans !After reading some excellent blogs (see References), I decided to download the Milestone 4.1 standalone nbpython installer (Build 200807071204) and kick some tires.

Platform : Windows XP SP3, pre-installed with Python 2.5

First impressions (based on 10 minutes of tinkering) :

1. I created a New Project and wanted to import some existing Python files into it. Somehow this does not seem possible yet - totally sucks since the first thing that a Python developer is likely to do is to suck in a bunch of existing code and see how Netbeans works with it.

2. nbpython seems to use jython as the default python engine to run python code. On my machine, this failed to run my code throwing the following stack trace.

"java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 applicationat java.lang.ProcessImpl.create(Native Method)at java.lang.ProcessImpl.(ProcessImpl.java:81)at java.lang.ProcessImpl.start(ProcessImpl.java:30)at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)Caused: java.io.IOException: Cannot run program "c:\netbeans\NetBeans\nbpython\jython-2.5\bin\jython"

3. I then decided to try and point nbpython to my existing windows python install (Tools-> Options ->Python).

4. This seemed to do the trick and i could run my first Python program.

Summary :

1. Despite being an early access build, the module does work and can be used for fairly simple testing of python code.

2. Lack of "import" support make this a non-starter if one wants to create projects with existing python code/libraries.

3. If the tool developers want to see this replace IDLE at some point, there has gotta be a way of running a Python interpreter/shell. This is critical to folks like myself who constantly Alt-tab into IDLE to quickly try out a list-comprehension/regex code snippet before integrating it into the main Python file.

4. This stuff needs Netbeans 6.5 and does NOT work on Netbeans 6.1. That probably means repeated downloads of nightly builds instead of simply updating the NBM module each time a change happens. Hope this can be fixed.

References :

Tuesday, July 1, 2008

BloggerBuster

I recently updated the blog template to a 3-column format from BloggerBuster. Pretty cool site for more powerful Blogger templates, tips and tricks on changing blogger templates, blogging tutorials and similar stuff. There is also a free ebook on customizing blogger templates - nice reading.

Wednesday, June 18, 2008

Handling Greediness In Regular Expression Matching Using Python re

Regular expressions matches are by default greedy . I've been trying to brush up regular expressions and have been playing around with the Python re package. While attempting some exercises from Core Python Programming - Wesley Chun , I ran into the greedy behavior myself and had some fun trying to debug it. Here is my story :

1. Suppose you want to match an address that starts with the string 'F-277' , where 'F' denotes some category and 277 represents the house number. This format is not cast in stone however, someone may choose to write down the address as "Apt No 277" .

2. My first regular expression was r'(\w+-?)\s?(\w*)\s?(\d+)' . In plain English, this represents the following "Match "any number of alphanumeric characters (\w+), followed by an optional hyphen (-), followed by zero or 1 white spaces (\s?) followed by another optional set of alphanumeric characters (\w*) followed by zero or 1 whitespace (\s?), followed by any number of numeric characters (\d+) .

3. This expression matches both the above addresses fine :
import re
re_exp = r'\w+-?\s?\w*\s?\d+'

>>> re.match(re_exp, 'F-277').group() 
F-277 
>>> re.match(re_exp, 'Apt No 277').group() 

'Apt No 277' 

 4. All is fine and dandy until we decide we want to extract the house number. To do this, we use grouping . The modified regular expression is : r'\w+-?\s?\w*\s?(\d+)

>> re.match(r'\w+-?\s?\w*\s?(\d+)', 'F-277').groups()  
('7',)

oops - what just happened here ? Even though we made the entire numerical part into a single group, the \d+ portion of the regular expression simply matches 7 as opposed to 277.

5. A little brain racking and experimenting reveals that the earlier part of the regular expression (\w*) was greedily matching part of the number.  

>>> re.match(r'\w+-?\s?(\w*)(\d+)', 'F-277').groups()
('27', '7').

As you can see \w* greedily matched '27' in '277' leaving the '7' to be matched by '\d+' . Why did it not match the whole number ? - one may wonder. This is because of backtracking, explained beautifully here (Section - Watch out for the Greediness) .

6. One way to fix this is to force the offending '*' operator in \w* to go lazy. We do this by putting a '?' after the '*' operator. Here is the result of our match :

>>> re.match(r'\w+-?\s?\w*?\s?(\d+)', 'F-277').groups() [0]  
'277'

There is a better way - using negated character classes. This avoids backtracking as explained here.
>> re_exp = r'\w+-?\s?([^\d]\w)*\s?(\d+)
>> re.match(re_exp, 'F-277').groups()[1]
 '277'

7. We verify that the original expressions still match .  
>>> re.match(re_exp, 'F-277').group() 
'F-277'

>>> re.match(re_exp, 'Flat No 277').group() 
'Flat No 277'

Tuesday, June 10, 2008

Automating Miktex using Python, win32com on Windows

I downloaded the Latex Miktex implementation yesterday with the goal of starting to learn to write Latex code. Unfortunately my roving eye caught the words "SDK" and off I went figuring out how to externally connect to and tinker with Mitex. When I found it used COM, i was even more excited, having tinkered with COM and VirtualBox recently.

This was extra fun since I had wanted to see how to do all this using Python for some time now. Mark Hammond's excellent win32com Python extension makes it super easy to write COM clients using Python .

After some experimenting around, i was able to write the Python equivalent of a couple of the C++ samples provided by the Miktek SDK.

STEPS :

# Install Python Win32 Extensions from https://sourceforge.net/projects/pywin32/

# Build COM library specific to MiKTeX.Session

1. C:\Python25\Lib\site-packages\win32com\client\makepy.py

2. Select "MiKTeX.Session" in the list presented.

makepy.py writes out the relevant files to the Python installation.


CODE : (Save as a Python file / just type in at the IDLE prompt)

import win32com.client as w

# instantiate COM library
c = w.Dispatch("MiKTeX.Session")
path = ''

# Calling Findfile
print c.FindFile("xunicode.sty", path)
(True, u'C:\\Program Files\\MiKTeX 2.7\\tex\\xelatex\\xunicode\\xunicode.sty')

# Getting installation information
print c.GetMiKTeXSetupInfo().installRoot
u'C:\\Program Files\\MiKTeX 2.7'

print c.GetMiKTeXSetupInfo().commonConfigRoot
u'C:\\Documents and Settings\\All Users\\Application Data\\MiKTeX\\2.7'


# Calling FindPkFile
print c.FindPkFile('cmr10', 'ljfour', '600')
(True, u'C:\\Documents and Settings\\All Users\\Application Data\\MiKTeX\\2.7\\fonts\\pk\\ljfour\\public\\cm\\dpi600\\cmr10.pk')


Now back to learning Tex !

UPDATE : I fixed several typos that made the above code almost unusable. Should work now.