By BoLOBOOLNE payday loans

The Unexpected Influence of IronPython

Posted: March 22nd, 2013 | Filed under: IronPython, Programming
Author:
No Comments →

I haven’t posted much in the last 12-18 months for a number of reasons. In part, I’ve simply been busy – moving across the country and starting a new job tends to keep you occupied, but the type of work I’ve been doing also hasn’t inclined itself as much towards a blog post. Anyway, for the first time in probably 18 months I went and investigated my site analytics to see if anyone had actually been visiting in my absence.

As it would happen, a post I never thought would really matter has actually gotten a fair amount of traffic. A couple of years back I was heavily into IronPython and I found a way to use config files in the IronPython shell. It turns out that for one, I made some copy paste errors and my posted code didn’t compile – that’s a bit embarrassing – but, despite my mistakes, a couple of people have found some use for it. Moshe Basanchig fixed my compilation errors, cleaned things up a bit, and took the next step that in retrospect I really should have done: He posted the code, a compiled version and some examples.

I was pretty excited to see this, not least because I was happy to see more people using IronPython which I really love. But the story doesn’t end here, it turns out that someone took the step to implement it in Python. Now its a little easier. One less assembly to load in python.

I’m always happy to see that some slightly obscure work I do helps someone else. Curious that it seems to be the posts I didn’t imagine anyone besides me would find interesting are the ones seem to attract the most traffic.

Tags:

IronPython’s missing csv

Posted: May 16th, 2011 | Filed under: IronPython, Programming
Author:
No Comments →

Python already has a great csv module, but since it is implemented in C, it’s not available to IronPython. IronPython.info has a suggested workaround using an external library. But for some reason I just chose to write something really simple myself. Since for my purposes today I didn’t actually need to do any writing to file, I just have a csv reader.

This is my really simple csv reader. I’m calling it, icsv.py. There’s a million things it doesn’t handle yet, but it covered what I happened to need today. Maybe I’ll expand it as the situation warrants.

class reader:
    def __init__(self,path,delimiter=','):
        self.File = open(path, "r")
        self.Delimiter = delimiter
        self.Header = self.File.readline().replace('\n','').split(delimiter)
        self.__curr_line = None
	
    def canread(self):
        return not self.File.closed
	
    def stop(self):
        self.File.close()
	
    def nextline(self):
        if self.File.closed: return None
			
        curr = self.File.readline()
		
        if not curr: 
            self.File.close()
            return None
		
        self.__curr_line = curr
		
        ## no quoted text return the easy way...
        if curr.find('"') == -1:
            return dict(zip(self.Header,self.__curr_line.replace('\n','').split(self.Delimiter)))
			
        line = dict()
        for h in self.Header:
            if not curr.startswith('"'):
                line[h], s, curr = curr.partition(',')
            else:
                clsq = curr.find('"',1)+1
                line[h] = curr[0:clsq]
                curr = curr[clsq+1:]
					
        return line
Tags: ,

Using my Service Locator / IoC in IronPython

Posted: February 10th, 2011 | Filed under: .NET, IronPython, Programming, Software Design
Author:
No Comments →

I’m big on Dependency Injection. I would argue it makes application development easier, faster and more extensible. I realize that sometimes this comes at a cost of a little more challenge in debugging and a performance hit caused by the extra abstraction, but to me it’s an acceptable cost. At least for the type of work I’m doing lately. I’ve written a lot about how I got my configuration files working in the IronPython console and I figured I’d take a second and pass on the very simple but very handy locator module I’ve created to use in IronPython.

We use an Inversion of Control Container (IoC) to accomplish our Dependency Injection. The key thing I suppose to realize, if you’re not familiar with this model, is that access to my full library of classes through IronPython was useless if I couldn’t get my IoC working. The container handles all of the Dependency Injection for me automatically; it’s part of both how my configuration is setup as well as how our IoC works. I’d either need to write a very long and annoying module that injected everything by hand (out of the question!) or get my IoC into IronPython. Once I got the ConfigurationManager Injection working the way I needed, bringing through the IoC was easy.

This is basically the entirety of my locator module. This just loads up the default container which works for a huge majority of the work I need to do. But I could of course request a different container from _loc if I needed it.

import clr
clr.AddReferenceToFile("MyServiceLocator.dll")

import mylib
from MyServiceLocator import *

_loc = MyServiceLocator.GetInstance()
GetInstance = _loc.GetInstance

## Go ahead and load up some of the common services
mymgr1 = _loc.GetInstance[mylib.srvdef.IMyManager1]()
mymgr2 = _loc.GetInstance[mylib.srvdef.IMyManager2]()
mymgr3 = _loc.GetInstance[mylib.srvdef.IMyManager3]()
mywebsrv1 = _loc.GetInstance[mylib.srvdef.Web.IMyWebService1]()

As you can see, accessing a generic is as simple as using [Type] instead of <Type> like you would in C#. I chose to setup mylib as a seperate module that could load up all of the core libraries from our code base that I use on a regular basis. This is a really simple module that I use to namespace things in a more shorthand / “python-y” way. I may at some point turn this into a Python Package to make the namespacing a little clearer. Most of the time (because I use the locator) only need the definition library so it would be nice to be able to only import that.

import clr
clr.AddReferenceToFile("MyServices.dll")
clr.AddReferenceToFile("MyWebServiceGateway.dll")
clr.AddReferenceToFile("MyServiceDef.dll")

import MyServiceDef as srvdef
import MyServices as srvs
import MyWebServiceGateway as websrvs

Read the rest of this entry »

Tags: , , , , ,

Extending my IronPython ToolSet: Truth Values

Posted: February 7th, 2011 | Filed under: IronPython, Programming, Python
Author:
No Comments →

If you haven’t seen what I’ve already done with my config module, then you should probably look over my last couple of posts about IronPython before you read on since I’m kind of building on that existing information/knowledge. My IronPython console config module, How I override the ConfigurationManager for the IronPython console, and to a lesser extent Talking to Google’s Custom Search API.

After reading over my last post about some extensions to the small config module I’d built a friend and coder extraordinaire (Corey Tabaka) raised a couple of points about how truth values in Python are evaluated. Which was nice because it cleaned up a few lines of code and helped me with my next addition to my config module. One of the key things he pointed out was this check I was using a lot in changing and loading my environments:

if _envs == 0:
    ## do stuff

This isn’t wrong (although slightly wordy), but it made it obvious I wasn’t aware of really how Python evaluates for truth. So he pointed me to the Python Docs on truth values. Basically, a collection that’s empty will evaluate to false, as well as a None (null for your .NET peeps) value. That essentially turned all those calls like above into this:

if not _envs: # do stuff

Read the rest of this entry »

Tags: , ,

Extending my IronPython ToolSet

Posted: February 4th, 2011 | Filed under: IronPython, Programming, Python
Author:
2 Comments →

I had a recent post about some issues I was having in the ConfigurationManager when I was using the IronPython Console. So I wrote funky hack to get around everything there and today I extended that config module I have, just a little farther.

In our config files to make local testing as easy as possible we actually have a big collection of connections strings. Essentially four connection strings for each environment that we use. We store them like this:

<connectionStrings>
    <add name="connection1" connectionString="..." providerName="..." />
    <add name="connection1.QA" connectionString="..." providerName="..." />
    <add name="connection1.Training" connectionString="..." providerName="..." />
    <add name="connection1.Production" connectionString="..." providerName="..." />
    <add name="connection2" connectionString="..." providerName="..." />
    <add name="connection2.QA" connectionString="..." providerName="..." />
    ....
</connectionStrings>

Sometimes we may need to use another environment for debugging or testing and this is purely a convenience measure so we can just add and remove the extensions to the connection string to change our environment without the need to remember all the details.

Knowing that all of this information is there, I thought it would be really handy if I could simply change environments from my IronPython console. So… now I can. Accomplishing this change was surprisingly easy. It took me a little bit to familiarize myself with Python dictionaries and find the places where the IronPython implementation didn’t quite work the same as normal Python, but once I did, piece of cake. And the code is readable too!

Read the rest of this entry »

Tags: , , , , , ,