I've been playing with IronPython Studio today and I've run into a *small* snag: I can't call any of the functions I write. For instance, I submitted this example to the IPS CodePlex site earlier today:
def add(x,y):
return x + y
print add(1,2)
When I run the above as part of a console application, I get the following exception:
MissingMethodException was unhandled by user code Method not found: 'Void IronPython.Runtime.Operations.Ops.UpdateTraceBack(IronPython.Runtime.Calls.ICallerContext, System.String, Int32, System.String, Boolean ByRef)'.
Stack Trace: at Program.add$f5(Object x, Object y)
at IronPython.Runtime.Calls.Function2.Call(ICallerContext context, Object arg0, Object arg1)
at IronPython.Runtime.Operations.Ops.CallWithContext(ICallerContext context, Object func, Object arg0, Object arg1)
at Program.Initialize() in Program.py:line 6
at IronPython.Runtime.Operations.Ops.ExecuteCompiled(InitializeModule init)
If I simply save the file and run it using ipy, the code works just fine. I just posted this in the discussions forum at CodePlex, so we'll see what comes of it.
I finally got Visual Studio 2008 installed yesterday and thought I'd celebrate by looking at IronPython integration into VS. I started typing "IronPython" into Firefox's Search Bar and "IronPython Studio" came up as a suggestion. It turns out that IronPython Studi0 (IPS) is built on top of the Visual Studio 2008 Shell, something Josh Holmes, if I recall correctly, had mentioned to me in a conversation quite a while ago. Though an additional stand-alone application wasn't what I intended, a webcast at the IPS CodePlex site was enough to convince me to try it out.
According to a January 16 post from the VSX Team Blog, you do not need any version of Visual Studio installed to run the latest version of IPS. The only prerequisite is the Microsoft Visual Studio 2008 Shell (isolated mode) Redistributable Package and the "1MB IronPython Setup installation" (which is really just a zip file that you extract into C:\IronPython). Installation was a snap for me, though I wasn't exactly the best test case for this since I already had VS2008 and the VS2008 SDK installed.
When you launch the IronPython.msi, the IPS Installation Instructions suggest you do so from a command prompt with Administrative Privileges. Also, you may find the IronPython Studio Discussions List at CodePlex a valuable resource both during and after installation.
Lately, I've been playing with some of the functional elements of Python. Here's a quick example of predicates, the filter(function, sequence) function and list comprehension.
I have a list of emails and I want to return only those from a specific domain. To do that, I wrote a CheckDomain(string) function that's only job is to tell me if the email I pass to it is or is not from the domain I'm looking for. Because our CheckDomain(string) function returns a boolean, it's referred to as a predicate.
## Function to check the domain of an email
def CheckDomain(email):
if email.find(domainName) > -1:
return True
else:
return False
Of course, we'll need a list of emails and a domain name to search for
## Our initial list of emails
listOfEmails = ['mwaddams@innotech.com',
'janet@interplanet.com', 'tsmykowski@innotech.com',
'pgibbons@innotech.com', 'sven@svenson.com',
'bob@roberts.com', 'spam@spamola.com']
## domain name we're interested in filtering on
domainName = '@innotech.com'
Now let's try our first search...
## our filtered list of emails will go here
filteredEmails = []
## loop through our emails and check the domain
for email in listOfEmails:
if (CheckDomain(email)):
filteredEmails.append(email)
print filteredEmails
>>> ['mwaddams@innotech.com',
'tsmykowski@innotech.com',
'pgibbons@innotech.com']
The code is straight forward: declare a variable, loop through the emails, check each email for the presence of a particular string and then add it to the list if it does. Fortunately, the filter(function, sequence) can help solve this problem
## our filtered list of emails will go here
filteredEmails = filter(CheckDomain, listOfEmails)
print filteredEmails
>>> ['mwaddams@innotech.com',
'tsmykowski@innotech.com',
'pgibbons@innotech.com']
With the filter(function, sequence) function, we didn't have to initialize the filteredEmails sequence nor did we have to manually loop through the listOfEmails sequence. It was just point and shoot. But let's say we want to do more than just filter. Maybe we want to apply some operation to each item in the sequence. List comprehension is just the thing we need.
## get all usernames
usernames= [email[0:email.index("@")]
for email in listOfEmails]
print usernames
>>> ['mwaddams', 'janet', 'tsmykowski',
'pgibbons', 'sven', 'bob', 'spam']
Still need to filter? Like the filter function, list comprehension makes use of predicates:
## get innotech usernames only
filteredUsernames = [email[0:email.index("@")]
for email in listOfEmails if CheckDomain(email)]
## EDITED ON 2/27/2008 TO CORRECT TYPO
print filteredEmails
print filteredUsernames
>>> ['mwaddams', 'tsmykowsk', 'pgibbons']
Since I moved into a full-time position with SRT Solutions in Ann Arbor, I thought it might be a good idea to move my most recent posts from darrellhawley.com to my SRT blog. So the next couple of posts you may already have read, but not to worry, I have a new one in the works.