John Anderson ( sontek )

C#, ASP.NET, Azure, and Silverlight

Python with a modular IDE (Vim)

clock June 24, 2009 23:48 by author John Anderson

On Thursday, May 9th, 2008 the Utah Python User Group decided to settle the debate that has plagued us developers since the beginning of time: If you were a programming language, what editor would you use?

I was tasked with showing Eclipse with the PyDev plugin in all its glory–but we all know–real men / developers don’t use IDE’s, so we are going to talk about using Python and Vim together, reaching a state of Zen that the Dalai LLama would be jealous of and establishing more Feng Shui than Martha Stewart’s Kitchen.

Freely jump between your code and python class libraries

There are 2 ways to add your ability to jump between python class libraries, the first is to setup vim to know where the Python libs are so you can use ‘gf’ to get to them (gf is goto file). You can do this by adding this snippet to your .vimrc:

python << EOF
import os
import sys
import vim
for p in sys.path:
    if os.path.isdir(p):
        vim.command(r"set path+=%s" % (p.replace(" ", r"\ ")))
EOF

With that snippet you will be able to go to your import statements and hit ‘gf’ on one of them and it’ll jump you to that file.

Continuing accessibility of the Python class libraries we are going to want to use ctags to generate an index of all the code for vim to reference:

$ ctags -R -f ~/.vim/tags/python.ctags /usr/lib/python2.5/

and then in your .vimrc

set tags+=$HOME/.vim/tags/python.ctags

This will give you the ability to use CTRL+] to jump to the method/property under your cursor in the system libraries and CTRL+T to jump back to your source code.

I also have 2 tweaks in my .vimrc so you can use CTRL+LeftArrow and CTRL+RightArrow to move between the files with more natural key bindings.

map <silent><C-Left> <C-T>
map <silent><C-Right> <C-]>

You can also see all the tags you’ve been to with “:tags”

Code Completion

To enable code completion support for Python in Vim you should be able to add the following line to your .vimrc:

autocmd FileType python set omnifunc=pythoncomplete#Complete

but this relies on the fact that your distro compiled python support into vim (which they should!).

Then all you have to do to use your code completion is hit the unnatural, wrist breaking, keystrokes CTRL+X, CTRL+O. I’ve re-bound the code completion to CTRL+Space since we are making vim an IDE! Add this command to your .vimrc to get the better keybinding:

inoremap <Nul> <C-x><C-o>

Documentation

No IDE is complete without the ability to access the class libraries documentation! You’ll need to grab this vim plugin. This gives you the ability to type :Pydoc os.path or use the keystrokes <Leader>pw and <Leader>pW to search for the item under the cursor. (Vim’s default <Leader> is “\”).

Syntax Checking

Vim already has built in syntax highlighting for python but I have a small tweak to vim to give you notifications of small syntax errors like forgetting a colon after a for loop. Create a file called ~/.vim/syntax/python.vim and add the following into it:

syn match pythonError "^\s*def\s\+\w\+(.*)\s*$" display
syn match pythonError "^\s*class\s\+\w\+(.*)\s*$" display
syn match pythonError "^\s*for\s.*[^:]$” display
syn match pythonError “^\s*except\s*$” display
syn match pythonError “^\s*finally\s*$” display
syn match pythonError “^\s*try\s*$” display
syn match pythonError “^\s*else\s*$” display
syn match pythonError “^\s*else\s*[^:].*” display
syn match pythonError “^\s*if\s.*[^\:]$” display
syn match pythonError “^\s*except\s.*[^\:]$” display
syn match pythonError “[;]$” display
syn keyword pythonError         do

Now that you have the basics covered, lets get more complicated checking added. Add these 2 lines to your .vimrc so you can type :make and get a list of syntax errors:

autocmd BufRead *.py set makeprg=python\ -c\ \"import\ py_compile,sys;\ sys.stderr=sys.stdout;\ py_compile.compile(r'%')\"
autocmd BufRead *.py set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m

You will have the ability to to type :cn and :cp to move around the error list. You can also type :clist to see all the errors, and finally, sometimes you will want to check the syntax of small chunks of code, so we’ll add the ability to execute visually selected lines of code, add this snippet to your .vimrc:

python << EOL
import vim
def EvaluateCurrentRange():
eval(compile('\n'.join(vim.current.range),'','exec'),globals())
EOL
map <C-h> :py EvaluateCurrentRange()

Now you will be able to visually select a method/class and execute it by hitting “Ctrl+h”.

Browsing the source

Moving around the source code is an important feature in most IDE’s with their project explorers, so to get that type of functionality in vim we grab the Tag Listplugin. This will give you the ability to view all opened buffers easily and jump to certain method calls in those buffers.

The other must-have feature of an IDE when browsing code is being able to open up multiple files in tabs. To do this you type :tabnew to open up a file in a new tab and than :tabn and :tabp to move around the tabs. Add these to lines to your .vimrc to be able to move between the tabs with ALT+LeftArrow and ALT+RightArrow:


map <silent><A-Right> :tabnext<CR>
map <silent><A-Left> :tabprevious<CR>

Debugging

To add debugging support into vim, we use the pdb module. Add this to your ~/.vim/ftplugin/python.vim to have the ability to quickly add break points and clear them out when you are done debugging:

python << EOF
def SetBreakpoint():
    import re
    nLine = int( vim.eval( 'line(".")'))

    strLine = vim.current.line
    strWhite = re.search( '^(\s*)', strLine).group(1)

    vim.current.buffer.append(
       "%(space)spdb.set_trace() %(mark)s Breakpoint %(mark)s" %
         {'space':strWhite, 'mark': '#' * 30}, nLine - 1)

    for strLine in vim.current.buffer:
        if strLine == "import pdb":
            break
    else:
        vim.current.buffer.append( 'import pdb', 0)
        vim.command( 'normal j1')

vim.command( 'map <f7> :py SetBreakpoint()<cr>')

def RemoveBreakpoints():
    import re

    nCurrentLine = int( vim.eval( 'line(".")'))

    nLines = []
    nLine = 1
    for strLine in vim.current.buffer:
        if strLine == ‘import pdb’ or strLine.lstrip()[:15] == ‘pdb.set_trace()’:
            nLines.append( nLine)
        nLine += 1

    nLines.reverse()

    for nLine in nLines:
        vim.command( ‘normal %dG’ % nLine)
        vim.command( ‘normal dd’)
        if nLine < nCurrentLine:
            nCurrentLine -= 1

    vim.command( ‘normal %dG’ % nCurrentLine)

vim.command( ‘map <s-f7> :py RemoveBreakpoints()<cr>’)
EOF

With that code you can now hit F7 and Shift-F7 to add/remove breakpoints. Then you just launch your application with !python % (percent being the current file, you can declare your main file here if its different).

Another tweak I use is to have my vim inside screen with a horizontal split, that way I can see the python interpreter and debug while still having vim there so I can easily fix my code.

Snippets

A great time saver with standard IDE’s is code snippets, so you can type a few key strokes and get a lot of code out of it. An example of this would be a django model, instead of typing out the complete declaration you could type ‘mmo<tab><tab>’ and have a skeleton of your model done for you. To do this in vim we grab the Snippets EMU plugin.

Check out a great screencast of snippetsEmu in action here

Emacs

Here is a great post on how to do the same with Emacs.

kick it on DotNetKicks.com



Creating an XSL Extension in C#

clock June 23, 2009 12:06 by author John Anderson

Although XSL is very powerful, I’m sometimes more comfortable with doing certain operations in C# and luckily it is pretty simple to call C# libraries from XSL.

First, you’ll need the code that you will call from XSL, this can just be a normal POCO, doesn’t have to be anything specific to XML/XSL:

 

    public class StringExtension
    {
        public string Proper(string data)
        {
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo textInfo = cultureInfo.TextInfo;
            return textInfo.ToTitleCase(data.ToLower());
        }
    }

 

Then you need to use the XsltArgumentList class to add an extension object to your Xsl Transform:

   class Program
    {
        static void Main()
        {
            XPathDocument doc = new XPathDocument("data.xml");
            XslCompiledTransform trans = new XslCompiledTransform();
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            trans.Load("stylesheet.xslt");
            XsltArgumentList args = new XsltArgumentList();
            StringExtension ext = new StringExtension();
            args.AddExtensionObject("ext", ext);
            trans.Transform(doc, args, sw);
            Console.WriteLine(sw.ToString());
            Console.ReadLine();
        }
    }

 

Now all you have to do is register the namespace in your XSL (i.e xmlns:”ext”) and you can use the extension by calling ext:Proper():

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:ext="ext"
 >
  <xsl:output method="text" indent="yes"/>
  <xsl:template match="/">
    <xsl:for-each select="People/Person">
      First Name: <xsl:value-of select="ext:Proper(@FirstName)"/><br />
      Last Name: <xsl:value-of select="ext:Proper(@LastName)"/><br />
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

kick it on DotNetKicks.com



Command Line Shortcuts for CMD and PowerShell

clock June 22, 2009 00:33 by author John Anderson

Here are some really useful command line shortcuts that I found on a forum awhile back but don't remember now:

F1 Repeats the letters of the last command line, one by one
F2 Displays a dialog asking user to "enter the char to copy up to" of the last command line
F3 Repeats the last command line
F4 Displays a dialog asking user to "enter the char to delete up to" of the last command line
F5 Goes back one command line
F6 Enters the traditional CTRL+Z (^z)
F7 Displays a menu with the command line history
F8 Cycles back through previous command lines (beginning with most recent)
F9 Displays a dialog asking user to enter a command number, where 0 is for first command line entered

kick it on DotNetKicks.com



ASP.NET Server tags cannot contain <% ... %> constructs

clock June 22, 2009 00:11 by author John Anderson

We've all seen the Server Tags cannot contain <% ... %> constructs which comes from trying to use server side <% .. %> inside a server side control:

<asp:TextBox runat="server" Text="<%= ConfigurationManager.AppSettings["Title"] %>" />

You have a few ways around this, the easiest and most common way is to assign the properties server side:

this.textBox1.Text = ConfigurationManager.AppSettings["Title"];

But I like being able to do as much of my property setting in markup as possible so if I ever need to change the property it doesn't require a complete re-build/re-deploy.

The more elegant/fun solution would be to use an ExpressionBudiler, you've most likely used this class already if you've ever worked with a a multilingual website, to have controls pull globalized text from your resx resources you do the following:

<asp:Label runat="server" Text="<%$ Resources:Text, ParticipantHeader %>" />

This is using a ResourceExpressionBuilder, there is also an AppSettingsExpressionBuilder and a ConnectionStringsExpressBuilder built into the .NET framework that you can use. If these don't do what you want you can always build your own, you can see an example of a custom ExpressionBuilder at the MSDN Documentation.

kick it on DotNetKicks.com



ASP.NET 4.0 SEO Features

clock June 21, 2009 12:32 by author John Anderson

In the upcoming release of ASP.NET 4.0 they have introduce 2 really nice features for SEO, the first one is being able to progmattically add Meta Keywords and Description to the page:

Page.MetaDescription = "Foo";
Page.MetaKeywords = "Foo, Bar, Baz";

Which will render the following in the header:
<meta name="description" content="Foo" /> <meta name="keywords" content="Foo, Bar, Baz" />

The 2nd nice feature added to ASP.NET 4.0 is 301 permanent redirects via the Response.RedirectPermanent("page.aspx") method. So now in your Global.asx.cs you can manage 302 redirects for pages who have moved like this:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (Request.FilePath == "/Product/23.aspx")
    {
        Response.RedirectPermanent("/Products/23/", true);
} }

This gives you the ability to easily manage files that are indexed in a search engine but have moved progrmatically rather than doing it through your webserver (which is much better for people who are using shared hosting).

kick it on DotNetKicks.com



jQuery Ribbon Control

clock June 20, 2009 00:30 by author John Anderson

Mikael Söderström has created a ribbon control using jQuery, it is very impressive and will be greate to give your online applications a similar feel to what people are used to on the desktop. You can download it here and can see a demo here

screen shot of ribbon control

kick it on DotNetKicks.com



IIS SEO Optimization Extension

clock June 19, 2009 10:15 by author John Anderson
Microsoft has released a new tool for IIS 7 that will help us developers analyze our website and find out if we have any SEO issues. Along with the analytical tools it provides it also has an easy UI for managing sitemap's and robot.txt. You can download it here

Scott Guthrie has a great introduction article on how to use it here kick it on DotNetKicks.com



Targeting Mono on Visual Studio 2010 for C# and ASP.NET Projects

clock June 18, 2009 07:01 by author John Anderson
In Visual Studio 2010 they added much better support for targeting multiple frameworks, allowing us to target .NET 2.0 and 4.0 from the same solution, which also gives us the ability to target runtime's such as Mono.

Jonathon Pobst has done all the work needed, so download his template from here and you are good to go!

I've also mirrored his template here in case his site goes down. Just extract the file to C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile and tell your project to target mono! kick it on DotNetKicks.com



Windows Hater

clock July 8, 2008 01:27 by author John Anderson

So I've become a huge fan of Linux Haters blog because he makes some very valid points about Linux and the open source community in general and it got me thinking of why I moved from Windows to Linux in the first place.

Picture this, you just bought built a brand new computer and want to install the brand spankin' new Windows Vista Ultimate, you plop down $300, and away you go! What does $300 get you? A bare minimum operating system with nothing but MS Paint and Media Player.

So you spend the next 45 minutes installing your bare minimum operating system, it takes 45 minutes because the initial install is 15gb, so by bare minimum I mean feature set, not file size. So after you get installed you try to use the Internet and realize that it didn't detect any of your hardware (video, audio, network), so you spend the next 2 hours spidering through multiple vendor's websites who all have their downloads/driver section in different areas.

Now you have all your drivers downloaded, you go to install them to find out that the majority of the installers only extracted the files to your disk, they didn't install them, the installer did not tell you they were only extracting, nor did they tell you where they were extracting them to. So after searching your disk and finding the extracted files you go into the folder to figure out what needs to be done to install them, but that is a waste of time since the vendor didn't feel the need to write a README file. Being the genius that you are, you right click on My Computer -> Hardware -> Device Manager (because that was an intuitive place to look, thats the first place I thought of going to install drivers!) and now you are prompted with a dialog with a list of “Unkown Device” with little exclamation point next them. What to do? Easy! You right click on it, go to properties -> details and look at the very intuitive and easy to read string PCI/VEN_1002&DEV_AA08&SUBSYS_AA081545&REV_00\4&1245FE7B&0&0108& and go to http://pciids.sf.net and search for each device so you can figure out what driver to install for it.

After you have all your device drivers installed and can utilize all your hardware, you'll want to go to Windows Update to make sure your computer is completely secure. You run the first batch of updates and it asks you to reboot, when it comes back up it doesn't tell you all your updates weren't finished, but using your spidey sense, you know that couldn't have been all! So you go back to windows update to find a whole mess of updates waiting for you, these ones will also ask you to reboot (and so will the next 3 or 4 groups of updates). Rebooting is good for your new system, gets it warmed up.

4 reboots and 230 updates later you decide you would like to edit some family photos for your Christmas postcards; no problem-Vista comes with the all powerful MS Paint, which can do everything you'd ever want to do, just check out what this guy did with it: MS Paint Skills!.

Editing family photos was fun but now you would like to add everyones birthday to your calendar and setup some re-occurring tasks to remind you to take out the trash and pay bills on time, but you'll soon have shut off notices and piled high garbage because there is no default calendaring program in Windows! But your boss will save the day, he just called and needs your latest TPS Report so you need go to open up your spreadsheet but you find out there is no spreadsheet program either, so you hike down to your local computer shop and plop down $300 for the office suite with the added benefit of having outlook (yay a Calendar for $300!!).

Spend the next 30 minutes to install Office (yes, office takes about as long to install as your whole operating system, but office is more powerful than your OS, so its O.K), after office is installed and you update your TPS report and send it to your boss you decide you'd like to create a vlog (Video Log) and post it on youtube, but as you'll soon find out, Movie Maker isn't going to be the easiest thing to locate.Bill Gates can't even find it.

Six hundred dollars and 7 hours later you have an almost usable computer, you still can't watch DIVX or DVDs, burn Audio CD's, Balance your checkbook, Sync your phone to your calendar and e-mail, or entertain your children with any games but minesweeper or solitaire. You don't even have a virus scan program yet!

I probably went around the block just to get next door, but my point is that after a simple 20 minute install of any popular Linux distribution I can do all of the things I've listed above. They come with the majority of hardware supported out of the box, an office suite (open office, abiword, gnumeric, evolution, etc), multiple graphic tools (gimp, inkscape), easy package manager that handles updates without multiple reboots (rpm, yum, yast, zypper, apt-get, package kit), video recording program (cheese), cd burner (Brasero, Wodim), lots of fun games, and the ability to balance your checkbook with GNU Cash.

kick it on DotNetKicks.com



OnLoad vs Page_Load in ASP.NET

clock January 25, 2007 01:38 by author John Anderson
Most ASP.NET developers are happy with using the default template that Visual Studio gives them for code behind which handles the Page_Load event. This is just weird because you are subscribing to your own event. In my opinion the better approach would be override the OnLoad event, this gets rid of the overhead of attaching to the event and firing the delegate but it also gives you better intellisense support. In VB.NET you have helpful menu's for subscribing to page events but in C# the only way to get those helpful menu's is to use inline code. The only caveat for using this approach is that the override keyword implies that you want to change the behavior of OnLoad which is not true, you simply want to setup the page. In fact you will still need to call base.OnLoad() so that the Page_Load event fires because you want to make sure that any other EventHandlers for Page_Load are called. kick it on DotNetKicks.com



Subscribe

rss

Google Reader

Sign in