Pages

Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Project Euler, Python and Notepad++

I have taken an interest in Project Euler lately. This project presents a series of challenging mathematical or computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

The next thing for me to consider was what is the programming language to use. I thought of C# initially but soon change my mind because it is too heavy weight. I decide to use something light weight and dynamic. With a desire to learn something new, I have made Python to be my choice.

Lately I have also upgraded my Notepad++ to 5.8.5. After the update, I looked at what are the latest plugins I could add on. I have found the Python Script plugin. To my delight, Python was also installed together with this plugin in my Notepad++ folder. So I got Python for free.

Next I have to figure out how to use this plugin. Actually, this plugin is not for general Python application development, it is more for Notepad++ automation using Python scripting. First and foremost, the Python Console is very important. To open the Python Console, click [Plugins], [Python Script] and [Show Console] as illustrated.


You can see from the above screen shot that it was Python 2.6.5. There are quite a few methods to run a Python script from the [Run] menu but what I am going to present here is a different method, running the script within the Python Console.

The Python Script plugin for Notepad++ comes with a few objects defined. One of them is the notepad object. The method getCurrentFilename() returns the full path name of the file being edited. Hence, we can use the command execfile() with notepad.getCurrentFilename() to accomplish what we want. In summary, enter the following command.
execfile(notepad.getCurrentFilename())

You only need to key in the command once. After this, you use the [Up Arrow] key to retrieve the previous command for execution.

The following is three solutions which I have come up for Problem 1.
# Add all the natural numbers below one thousand that are multiples of 3 or 5.

# imperative paradigm
def methodA():
    sum = 0
    for num in range(1000) :
       if num % 3 == 0 or num % 5 == 0:
          sum += num
    print sum

# pythonistic 
def methodB():
    print sum([x for x in range(1000) if x % 3 == 0 or x % 5 == 0])

# functional paradigm
def methodC():
    print sum(filter(lambda x: x % 3 == 0 or x % 5 == 0, range(1000)))
 
print "This is Project Euler Problem 1" 
methodA()
methodB()
methodC()

Using IronPython in ASP .NET VS 2008

Download Microsoft ASP.NET Futures (July 2007) from Microsoft and follow the instructions for installation closely in the download page especially you are installing it on Windows Vista.

The installation should work for Visual Studio 2005, Visual Web Developer 2005 Express Edition, Visual Studio 2008 or Visual Web Developer 2008 Express Edition. Start up Visual Studio, create a new website. You should see IronPython in the Language drop down box.

Resequencing Photos from more than one Camera

This is a python script which I have written to resequence photos taken from two cameras. The script makes use of the date and time of the photos to sequence the filenames.

import os, glob, time

fprefix = "Beijing"        # Name to prefix photos
folder  = "D:/Beijing2008" # Folder to look for photos
ext     = "jpg"            # Extension of photos
sno     = 1                # Serial number to start

print "Folder = ", folder
print '-' * 60
date_file_list = []
for file in glob.glob(folder + "/*." + ext):
  stats = os.stat(file)
  lastmod_date = time.localtime(stats[8])
  date_file_tuple = lastmod_date, file
  date_file_list.append(date_file_tuple)
  #print date_file_list  # test

date_file_list.sort()
#date_file_list.reverse()  # newest mod date now first

import string
print "%-40s %s" % ("filename:", "last modified:")
for file in date_file_list:
  folder, file_name = os.path.split(file[1])

  # Rename File
  ext = os.path.splitext(file_name)
  file_name = fprefix + ("%03d" % sno) + string.lower(ext[1])
  sno = sno + 1
  newfile = folder + "/" + file_name

  os.rename(file[1], newfile)

  # convert date tuple to MM/DD/YYYY HH:MM:SS format
  file_date = time.strftime("%m/%d/%y %H:%M:%S", file[0])
  print "%-40s %-20s %s" % (folder, file_name, file_date)

Python and .NET

I was asked to explore using Python with .NET. Here are three possibilities.

Configuration A – Plain IronPython
This is the most straightforward and plain vanilla installation.
Download IronPython-1.1.1-Bin.zip (975KB) from http://www.codeplex.com/IronPython.
Unzip the files into a folder, say C:\IronPython-1.1.1 and set the PATH. Voila!
Type ipy helloworld.py or ipyw helloworld.py

Configuration B – IronPython Studio
IronPython Studio is based on the Visual Studio 2008 Shell runtime (royalty free) and can be installed without requiring any version of Visual Studio. It provides the following templates for projects using IronPython.

Need to first install Visual Studio 2008 Shell (Isolated) (390MB) from http://www.microsoft.com/downloads/details.aspx?familyid=aca38719-f449-4937-9bac-45a9f8a73822&displaylang=en. Make sure you execute the vs_shell_isolated.enu.exe file in C:\VS 2008 Shell Redist\Isolated Mode. If you missed this step, the Iron Python Studio will not work.
Download IronPythonStudio.msi (1127KB) from http://www.codeplex.com/IronPythonStudio. Double click on the above msi file and the installation will start. IronPython is also included in the msi.

Configuration C – Visual Studio 2008 with IronPython
For those who already have VS2008 installed and are working in VS2008 for VB and C# projects, this would be the best configuration. Download Visual Studio 2008 SDK (98MB) from http://www.microsoft.com/downloads/details.aspx?familyid=30402623-93ca-479a-867c-04dc45164f5b&displaylang=en and install it on top of Visual Studio 2008. Run VS2008 under “Visual Studio 2008 SDK --> Tools --> Start Visual Studio 2008 Under Experimental Hive”. On top of VB, C#, C++ and all the project types in IronPython Studio, it also allows ASP.NET web applications and services to be developed using IronPython!



It is difficult to develop GUI Applications without IDE. Since I am a DOTNET guy, I go with Configuration C. Configuration B would be good for those of us who are not using VB and C#. Configuration A in my opinion is not productive, but nonetheless a very convenient way to pick up the basics of Python in .NET.

After some hours of experimentation, there are some serious bugs in IronPython. My colleague reported this problem, create a Button and define a button_click event for the button. However, when a new Label is created, the button_click function is overwritten. Any change made directly to the codes in the code view will most likely mess up the designer view. The Designer View can also be easily "corrupted" even with incorrect indentation.