Archive
Calling Python from C#
Here is a simple example of how to call an IronPython function from C#
Here is the PY file: (With URL & Regex removed)
import urllib2
import re
import array
def getRoutes():
strRouteRegex = '(?<From>[A-Z]{3})...\d+..iata...=..(?<To>[A-Z]{3})'
httpRequest = urllib2.urlopen("http://www.somewebsite.com/")
html = httpRequest.read()
routesArray = []
for m in re.finditer(strRouteRegex, html):
route = FlightRoute()
route.depart = m.group('From')
route.arrive = m.group('To')
routesArray.append(route)
return routesArray;
class FlightRoute:
depart = ''
arrive = ''
And here is the C# calling code – This requires the .NET 4 to handle the dynamic types.
using System;
using System.Collections.Generic;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
namespace CSPY
{
class Program
{
static void Main(string[] args)
{
const string strPy = @"C:\research\Program.py";
ScriptEngine engine = Python.CreateEngine();
var lPath = new List<string> {@"c:\Program Files\IronPython 2.7\Lib"};
engine.SetSearchPaths(lPath);
ScriptRuntime scripting = engine.Runtime;
dynamic runtime = scripting.UseFile(strPy);
dynamic routes = runtime.getRoutes();
foreach(dynamic route in routes)
{
Console.WriteLine(route.depart + " -> " + route.arrive);
}
Console.Read();
}
}
}
Useful?
Load Image as Base64 string
In JavaScript you can display an image like this <img src=”data:image/jpg;base64,…”> where “…” is the base64 encoded version of the image. Here is some C# Code that can convert an image on-disk to this Base64 string format
FileInfo fi = new FileInfo(strFileName);
FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read,FileShare.None);
byte[] rawData = new byte[fi.Length];
fs.Read(rawData, 0, (int)fi.Length);
string strBase64 = Convert.ToBase64String(rawData);
Response.Write("data:image/jpg;base64," + strBase64);
fs.Close();
PuTTY / SSH on Palm WebOS
Shown opposite is a screenshot of an application that I’ve developed for Palm / WebOS using PhoneGap that enables you connect to a Linux server via SSH (PuTTY) from your Palm Phone.
How it works, is that you have to first install a piece of software called MobilePutty that you can download from http://bit.ly/puttyformobile , this application must be installed and run on a Windows PC. On this program, you enter the Host IP address, username and password of your Linux Server. And a PuTTY terminal window will appear.
From your phone, you can then enter in the host name, username and password, and you will be relayed screenshots of the PuTTY terminal to your phone. You can also send commands to your PuTTY terminal, in order to interact with your linux server. You can also open up any other terminal-style program, such as windows command prompt, and remotely control your home PC from your phone, without having to worry about firewalls etc.
This application works best over WiFi broadband, be very patient if you are using GPRS.
PuTTY/SSH on your Nokia Phone
Shown opposite is a screenshot of an application that I’ve developed for Nokia / OVI using WRTKIT that enables you connect to a Linux server via SSH (PuTTY) from your Nokia Phone.
How it works, is that you have to first install a piece of software called MobilePutty that you can download from http://bit.ly/puttyformobile , this application must be installed and run on a Windows PC. On this program, you enter the Host IP address, username and password of your Linux Server. And a PuTTY terminal window will appear.
From your phone, you can then enter in the host name, username and password, and you will be relayed screenshots of the PuTTY terminal to your phone. You can also send commands to your PuTTY terminal, in order to interact with your linux server. You can also open up any other terminal-style program, such as windows command prompt, and remotely control your home PC from your phone, without having to worry about firewalls etc.
This application works best over WiFi broadband, be very patient if you are using GPRS.
90 Days free pluralsight training
Pluralsight provides high-quality training solutions for Microsoft .NET developers that can fit any schedule or budget. The revolutionary training library provides developers with instant access to a rich collection of online training courses delivered by industry authorities. Through this special DreamSpark offer you will receive a FREE 90-day subscription to all of the courses found in the Pluralsight On-Demand! training library. Happy learning!
To get started follow these steps:
- Sign in with your Windows Live ID.
- If you haven’t done so already, click on Get Verified and follow the steps to verify your academic status.
- Click on “Get Key” above and copy the single-use key provided.
- Navigate to training.pluralsight.com/Dreamspark
- On the right hand side of the page paste the key you received in the “code” box, and complete the User Information form below it.
- Click “Submit” and get instant access to Pluralsight’s online training library!
Free Microsoft Certification exam
STUDENTS! Get a FREE Microsoft Certification Exam Voucher Code –and a head start on your IT career.
While supplies last!
These days, the job market can be a tough ride—but it doesn’t have to be. Boost your resume by getting Microsoft Certified. A Microsoft Certification can help validate your technical skills and show hiring managers you have the right stuff for the job. And right now, you can get a voucher code to take a FREE Microsoft Certification Exam.
Like new jobs, these codes are in limited supply so don’t wait!
Please select “Get Key” above to get your voucher code, then follow the steps below to redeem it.
Voucher Redemption Instructions
NOTE: This offer is for select exams (with 072 prefix) for students globally who have valid student identification (not available in India and China). Each voucher code can only be redeemed once. Once the code has been redeemed, it cannot be used again. Students must redeem and take exams by June 30, 2011. This offer is non-transferable and cannot be combined with any other offer. This offer expires when supply is depleted and is not redeemable for cash.
Once you have your free exam voucher code, follow the instructions below on how to redeem it:
- Prometric is Microsoft’s Exam Delivery Provider
- Go to Prometric’s website:
- Click on the “Start” icon located in the “Get Started” section
- Select “Schedule an Appointment”
- Select your Country and State (if applicable)
- Click Next
- Next to Client, select Microsoft
- Next to Program, select Microsoft (072)
- Click Next
- Please read the page titled Microsoft Certified Exam Provider
- Click Next
- Select the exam number and the exam language for the exam you would like to take
- Click Next
- Select the test center location where you would like to take the exam by clicking on Schedule an Appointment next to the location that is nearest you
- When the next page appears you have the option of either logging in, if you have been to the site before or you can register as a new user
- NOTE if you are a new user – PLEASE enter a VALID EMAIL ADDRESS for Prometric and Microsoft.
- All information for the Microsoft Certification Program will be sent via email and without a valid email address, you will not receive access to the MCP Secure site or be able to order your Welcome Kit when you achieve your certification
- NOTE if you are a new user – PLEASE enter a VALID EMAIL ADDRESS for Prometric and Microsoft.
- Once you are logged in, you will then be able to select a date and time for your appointment, select a date and time
- Click Next
- Under Payment Information in the box that states Promotion Code or Voucher click Yes
- Next to Discount Type select Voucher Number
- Next to Voucher Number / Promotion Code – enter the code number you have been provided
- Click Validate – if the voucher has not been used you will be automatically directed back to the Payment Information Page
- Validate your email address and click on the radio button I agree (as long as you agree to the terms of the privacy notice)
- Click Commit Registration
- A Confirmation Page will come up, please print this page for your records and ensure you have noted the date and time for your exam
- When you go to the testing center to take your exam, you must have valid student identification with a photograph. You will not be allowed to take your exam without student identification with photo.
IMPORTANT:
- Your code can be redeemed for one Microsoft Certification exam.
- This offer is only applicable to students, and is worldwide (offer not valid in China and India).
- Students must have a valid student identification card with photo in order to take the exam. Students without a valid student ID card will have to pay full professional price for the exam.
- You must use the code and take your exam by June 30, 2011.
Get your latitude and longitude in Javascript
Here’s an AJAX request that gets your longitude and latitude in javascript. It’s using XML, not JSON, since there was some problems using eval() for non-US addresses.
var strUrl = “http://maps.googleapis.com/maps/api/geocode/xml?”;
strUrl += “address=bt489ph “;
strUrl += “&sensor=false”;
ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState != 4)
{
return;
};
var xmlDoc = ajaxRequest.responseXML;
var latitude = xmlDoc.getElementsByTagName(‘lat’)[0].firstChild.nodeValue;
var longditude = xmlDoc.getElementsByTagName(‘lng’)[0].firstChild.nodeValue;
alert(latitude);
alert(longditude);
};
ajaxRequest.open(“GET”,strUrl,true);
ajaxRequest.send(null);
Viewing a webcam on a Palm or Nokia Phone
Here’s a project I’m trying to get off the ground; a mobile app for viewing webcam images streamed from your PC.
Scrollable image in WP7/XAML
Probably really obvious for anybody who’s worked with XAML before, but I was happy with a little discovery, where you can show an image and have it scroll too…
<ScrollViewer
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Image
HorizontalAlignment="Stretch"
Margin="0" Name="image1"
Stretch="None"
VerticalAlignment="Stretch"
Source="/ScrollViewer2;component/Images/dubai.png" />
</ScrollViewer>
error 907 icon data is too large
When compiling (ant build) a BlackBerry Webworks project, I got this error: error 907 icon data is too large.
The icon was about 42K. I discovered that, I had created the Icon with Fireworks 8, which had somehow jam packed the PNG full of layers, metadata, and larger images, that I had used to create the icon. I discovered, that if you export as image only, or select batch process, select export, then custom, the png 32, then it drops it down to a few Kb, and the build goes ok.


