Saturday, December 31, 2011

Python IDEs/Editors

If you're getting into developing in python, then you're going to ask yourself the question 'Which IDE / code editor should I use?'  Although this is obviously a subjective issue, let me weigh in with my personal experience.
Ignoring VI/Emacs.
If you're a MicroSerf / Windows developer, then one good option is NotePadPlusPlus (NPPP).   NPPP is light-weight - and so snappy and responsive, but at the same time VERY extensively featured.  On the downside, it's really a Windows-only product, and so if you like me want to be able to use the same python dev platform across both Windows and Linux, then NPPP is disqualified.  Other detractors are a lack of code completion, and no class / solution explorer.  (At least no built-in code completion, and the one or two attempts  I made to equip it with a plug-in failed.)
Which brings us to Scintilla-derivative Geany, a long-time personal favourite of mine.  It's a light-weight code editor which bests NPPP by coming off-the-shelf with some code completion, and a class/solution explorer.  OS-wise, Geany binaries compiled for both Linux and Windows are available.  On the down-side, the feature-set that Geany offers is quite limited compared to NPPP, although most of the essentials are there.  The upside of the limited feature set, is that the IDE/editor itself is not resource hungry.
Finally, the PyDev extension for Eclipse.  I've just recently started using this, so its a bit early in the game for comment, but it's obviously a fully featured IDE, with code completion, a solution/class explorer, test-infrastructure integration, cross-platform and is open-source.  The only disadvantage of this option is that Eclipse is clearly not a light-weight application itself, and so if your development box is spec-light, then you may want to go with either NotePadPlusPlus or Geany.

Note Pad Plus Plus
+ V.Fast, light-weight
- Windows only (Linux version runs under Windows emulator [Wine], and is horrible)

Geany
+ Cross OS - identical Windows & Linux versions
+ Code completion
+ Class / solution explorer
+ Light-weight
- Relatively limited feature-set

Eclipse PyDev
+ Cross OS - identical Windows & Linux versions
+ Code-completion
+ Refactoring support
+ Class / solution explorer
+ Test integration
+ Integrated debug support
- Requires decent machine specs

Post Script - Aptana Studio 3

In the two months or so since I wrote this post up, I've started using Aptana Studio 3.

Aptana Studio 3is a customisation of Eclipse developed and maintained by Aptana, who via some convoluted corporate cannibalism, has absorbed the original PyDev team.  So, in short, Aptana is the heir apparent to PyDev for Eclipse.

What you get is an all-in-one download, which short-circuits the previous two-stage download, for a grade-A user-experience.  The IDE itself is snappy, and seems faster than PyDev under Eclipse Indigo.  In addition, Aptana features support for Django projects, although I haven't actually done any Django work in the interim, so more to come on that...

FINAL VERDICT

Aptana Studio 3 by a Knock-Out !





Wednesday, December 7, 2011

Sieve of Eratosthenes in F# - Attempt # 1

sieve.fs :
namespace sieve
  module sieve = 

(*
A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself.
To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
*)

    let shake (n : int) = 

      printfn "Applying Sieve of Eratosthenes to First %i Natural Numbers" n |> ignore
      
      // 1. Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n).
      let matrix = Array.init n (fun x -> true)

      // 2. Initially, let p equal 2, the first prime number.
      let mutable p = 2

      let mutable set_exhausted = false
      while (set_exhausted = false) do
      
        //  3. Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list.
        //  These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked.        
        let mutable i = p + p
        while (i <= n) do        
          matrix.[i - 1] <- false
          i <- i + p

        //  4. Find the first number greater than p in the list that is not marked; let p now equal this number (which is the next prime).        
        p <- p + 1
        while ((p <= n - 1) && (matrix.[p - 1] <> true)) do
          p <- p + 1      
        
        //  5. If there were no more unmarked numbers in the list, stop. Otherwise, repeat from step 3.
        if (p >= n - 1) then
          set_exhausted <- true

      //  6. When the algorithm terminates, all the numbers in the list that are not marked are prime.
      matrix
Program.fs :
namespace effsharp
module Main =
    open System
    open sieve
        []
        let main args =         
            
            printfn "Sieve up to what natural number ? [Max %i]" Int32.MaxValue
            let nStr = Console.ReadLine()
            let n = Int32.Parse(nStr)

            let primes = sieve.shake n

            for i = 1 to n do              
              if (primes.[i - 1] = true) then
                printfn "%i" i |> ignore

            printfn "any key to exit..."
            let endofapp = Console.ReadKey()
            0

Friday, September 9, 2011

Reference in the manifest does not match the identity of the downloaded assembly

The following problem may be encountered when attempting an IIS web install/deploy of a 1-Click Install produced by using the Publish option under Visual Studio 2010.


Reference in the manifest does not match the identity of the downloaded assembly


To fix, right-click on the project and goto
Properties > Application > Resources > Icon and Manifest > Manifest
and select the 'Create application without a manifest' option.

[StackOverFlow.com]

Thursday, September 1, 2011

Reclaim/Move a Lost/Off-Screen Window in Windows 7

METHOD 1
1. Alt-tab to lost window
2. Right-click on icon for lost window in the task-bar - You may have to first hover over the icon (if it's pinned)
3. Select 'Move' from the drop-down menu
4. Hit any arrow key
5. Move mouse to re-position

METHOD 2
1. Right-click on task-bar
2. Select 'Cascade windows'

METHOD 3
1. Alt-tab to lost window
2. Press Alt+space
3. Press m
4. Press any arrow key
5. Move mouse to re-position

[Courtesy of How-to Geek]

Saturday, August 13, 2011

Installing Django Under Ubuntu 11-04

From the $ prompt of the terminal:

$ sudo apt-get install python-django

Monday, August 8, 2011

TCP/IP Port Pipe - UNDER CONSTRUCTION



import optparse
import socket, select, threading

class Pipe(threading.Thread):

time_out = 5
buffer_size = 4096
max_idle = 5

def __init__(self, server_host, server_port, dclient_connxn):

super(Pipe, self).__init__()

self.server_host = server_host
self.server_port = server_port
self.server_addr = (server_host, server_port)

self.client_connxn = client_connxn

def connect_to_server(self):
self.server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(self.server_addr)
self.server_sock.connect(self.server_addr)

def log_request_response(self, req, resp):
print('request')
print(req)
print('response')
print(resp)

def run(self):
self.connect_to_server()

to_read = [self.client_connxn, self.server_sock]
to_write = []
to_watch_for_exception = to_read

request = ''
response = ''

seconds_idle = 0

while True:
(ready_to_read, ready_to_write, experienced_exception) = select.select(to_read, to_write, to_watch_for_exception, 1)

if (experienced_exception):
self.log_request_response(request, response)
self.client_connxn.close()
self.server_sock.close()
break

if (ready_to_read):
seconds_idle = 0

for waitable in ready_to_read:
data = waitable.recv(buffer_size)
if (waitable == self.client_connxn):
out = self.server_sock
request += data
else:
out = self.client_connxn
log_to = response
response += data
out.send(data)

else:
seconds_idle += 1
if (seconds_idle == max_idle):
self.log_request_response(request, response)
self.client_connxn.close()
self.server_sock.close()
break

class PortListener():'''

'''
def __init__(self, listen_on_port, pipe_to_host, pipe_to_port, max_connxns=1):
self.listen_on_port = listen_on_port
self.pipe_to_host = pipe_to_host
self.pipe_to_port = pipe_to_port

def listen():

listen_to_addr = ('127.0.0.1', self.listen_to_port)
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.bind(listen_to_addr)
listen_socket.listen(max_connxns)

while True:
# pause until client requests connection
(client_connxn, client_addr) = listen_socket.accept()
pipe = Pipe(self.pipe_to_host, self.pipe_to_port, client_connxn)
pipe.start()

def setup_info_logger(log_file_path):
logging.basicConfig(filename=log_file_path, level=logging.INFO)
return logging.getLogger()

def parse_command_line():'''
parse command line argument to populate dictionary
return dictionary with keys = client_port, server_host, server_port, log_file_path
returns None if mandatory option missing
'''
parser = optparse.OptionParser()
parser.add_option('--clientport', dest='client_port', help='port client to this port on localhost')
parser.add_option('--serverhost', dest='server_host', help='server host, default = 127.0.0.1')
parser.add_option('--serverport', dest='server_port', help='server port')
parser.add_option('--logfilepath', dest='log_file_path', help='log file path')

(options, args) = parser.parse_args()

if (options.client_port == None):
print('--clientport is mandatory')
return None
if (options.server_port == None):
print('--serverport is mandatory')
return None

d = {}

if (options.log_file != None): d['log_file'] = options.log_file
else: d['log_file'] = None

d['client_port'] = options.client_port

if (options.server_host != None): d['server_host'] = options.server_host
else: d['server_host'] = '127.0.0.1'

d['server_port'] = options.server_port

return d

def main():
opt = parse_command_line()
logger = None
if (opt['log_file_path'] != None):
logger = setup_info_logger(opt['log_file_path'])
listener = PortListener(opt['client_port'], opt['server_host'], opt['server_port']]

if (__name__ == '__main__'):
main()

Saturday, August 6, 2011

SUZUKI Hisao's - TinyProxy - Python HTTP Proxy

Original Site


Script reproduced without permission:



#!/bin/sh -
"exec" "python" "-O" "$0" "$@"

__doc__ = """Tiny HTTP Proxy.

This module implements GET, HEAD, POST, PUT and DELETE methods
on BaseHTTPServer, and behaves as an HTTP proxy. The CONNECT
method is also implemented experimentally, but has not been
tested yet.

Any help will be greatly appreciated. SUZUKI Hisao
"""

__version__ = "0.2.1"

import BaseHTTPServer, select, socket, SocketServer, urlparse

class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler):
__base = BaseHTTPServer.BaseHTTPRequestHandler
__base_handle = __base.handle

server_version = "TinyHTTPProxy/" + __version__
rbufsize = 0 # self.rfile Be unbuffered

def handle(self):
(ip, port) = self.client_address
if hasattr(self, 'allowed_clients') and ip not in self.allowed_clients:
self.raw_requestline = self.rfile.readline()
if self.parse_request(): self.send_error(403)
else:
self.__base_handle()

def _connect_to(self, netloc, soc):
i = netloc.find(':')
if i >= 0:
host_port = netloc[:i], int(netloc[i+1:])
else:
host_port = netloc, 80
print "\t" "connect to %s:%d" % host_port
try: soc.connect(host_port)
except socket.error, arg:
try: msg = arg[1]
except: msg = arg
self.send_error(404, msg)
return 0
return 1

def do_CONNECT(self):
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
if self._connect_to(self.path, soc):
self.log_request(200)
self.wfile.write(self.protocol_version +
" 200 Connection established\r\n")
self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
self.wfile.write("\r\n")
self._read_write(soc, 300)
finally:
print "\t" "bye"
soc.close()
self.connection.close()

def do_GET(self):
(scm, netloc, path, params, query, fragment) = urlparse.urlparse(
self.path, 'http')
if scm != 'http' or fragment or not netloc:
self.send_error(400, "bad url %s" % self.path)
return
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
if self._connect_to(netloc, soc):
self.log_request()
soc.send("%s %s %s\r\n" % (
self.command,
urlparse.urlunparse(('', '', path, params, query, '')),
self.request_version))
self.headers['Connection'] = 'close'
del self.headers['Proxy-Connection']
for key_val in self.headers.items():
soc.send("%s: %s\r\n" % key_val)
soc.send("\r\n")
self._read_write(soc)
finally:
print "\t" "bye"
soc.close()
self.connection.close()

def _read_write(self, soc, max_idling=20):
iw = [self.connection, soc]
ow = []
count = 0
while 1:
count += 1
(ins, _, exs) = select.select(iw, ow, iw, 3)
if exs: break
if ins:
for i in ins:
if i is soc:
out = self.connection
else:
out = soc
data = i.recv(8192)
if data:
out.send(data)
count = 0
else:
print "\t" "idle", count
if count == max_idling: break

do_HEAD = do_GET
do_POST = do_GET
do_PUT = do_GET
do_DELETE=do_GET

class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
BaseHTTPServer.HTTPServer): pass

if __name__ == '__main__':
from sys import argv
if argv[1:] and argv[1] in ('-h', '--help'):
print argv[0], "[port [allowed_client_name ...]]"
else:
if argv[2:]:
allowed = []
for name in argv[2:]:
client = socket.gethostbyname(name)
allowed.append(client)
print "Accept: %s (%s)" % (client, name)
ProxyHandler.allowed_clients = allowed
del argv[2:]
else:
print "Any clients will be served..."
BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)

Friday, July 22, 2011

MS SQL Server - Clear Cache - for Query Timing

[Using MS-SQL Server] When timing a query for the purposes of optimization, the query may well run quicker on re-execution subsequent to the initial execution - due to caching by the db engine.

In order to meaningfully compare execution time across optimization attempts, the cache needs to be cleared, and this can be done by executing


DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
go


between query timings.

To turn query timing on - SET STATISTICS TIME ON
To turn query timing off - STATISTICS TIME OFF

Wednesday, March 16, 2011

South African Direct Marketing Opt-Out List

To register for the OptOut list for South African direct marketing:

SMS the letters DMA followed by your RSA ID number, to 34385.

or phone the RSA call centre on

0861 OPT OUT

Sunday, February 6, 2011

Recursive Delete Folder & Files in Linux

rm -Rf *

or

rm -Rf rootfolder

Saturday, January 22, 2011

Export Create Table Script in mySQL

SHOW CREATE TABLE TableName;

Thursday, January 20, 2011

SpaceSniffer - Drive Space Allocation Mapping

If you're looking for a quick and easy disk space mapper, then I suggest SpaceSniffer from Uderzo - it's a small download, runs fast, and has decent visualization.

Wednesday, January 12, 2011

dotNet eMail

assembly: System.Net.Mail
classes: MailMessage, SmtpClient

---

MSDN Social thread:

public static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd);

Public static void LaunchEmailClient(string mailURL)
{�
ShellExecute(IntPtr.Zero, "open", mailURL, "", "", 4);
}

You can refer http://msdn.microsoft.com/en-us/library/bb762153.aspx

---



Contact Us




Monday, January 10, 2011

Taunting dotNET - Rhino Mocks

RhinoMocks is a best-practice Object Mocking Framework for dotNET written by uber-dev Ayende Rahien.

Official RhinoMocks Page

Central Forum - Google Group

Good Intro Examples
- Stephen Walther

http://en.wikibooks.org/wiki/How_to_Use_Rhino_Mocks/Introduction
http://www.ayende.com/wiki/Rhino+Mocks+Introduction.ashx
http://en.wikibooks.org/wiki/How_to_Use_Rhino_Mocks/Introduction
http://house9-code-samples.blogspot.com/2008/02/rhinomocks-basics.html

Martin Fowler - Mocks/Behaviour vs Stubs/State Testing:

... I've also adjusted my vocabulary to match that of the Gerard Meszaros's xUnit patterns book... Meszaros uses the term Test Double as the generic term for any kind of pretend object used in place of a real object for testing purposes... Meszaros then defined four particular kinds of double:

- DUMMY objects are passed around but never actually used. Usually they are just used to fill parameter lists.
- FAKE objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
- STUBS provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.
- MOCKS are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

Saturday, January 8, 2011

CPU & Memory Info in Linux

cat /proc/meminfo
cat /proc/cpuinfo

Ubuntu 10-10 on the Lenovo X100e ThinkPad

Installing Ubuntu Netbook Edition 10-10 Maverick Meerkat on the Lenovo X100e ThinkPad

I unsuccessfully attempted to use the PenDrive Universal Linux USB Installer, as recommended by the official 10-10 download page, as well as the USB Disk Creator from an older (9-10) live CD.

Specifically, I encountered the problem outlined here, namely

SYSLINUX 4.01 debian-20100714 EDD Copyright (C) 1994-2010 H. Peter Anvin et al
Unknown keyword in configuration file: gfxboot
boot:
vesamenu.c32: not a COM32R image


Various attempts at implementing suggested solutions failed, and so in the end I tried using UNetBootIn to create the USB bootable install, which has worked perfectly.

Importantly, the wireless LAN is seamlessly detected and operated by 10-10.

INSTALL/GENERAL
- justinsomnia
- petehowe
- semiantics

WLAN
- nulldevice
- how-to-linux

OPTIMISATION
- how-to-speed-up-ubuntu-910-on-x100e

10-10 Specific
- Customize 10-10


10-04
- karssen.com
- helmi-blebe
- decent post install guide

A Brief Review
Meerkat runs quite smoothly on my X100e with 2 MB or RAM. UI response is quite snappy, except for the Ubuntu button and following OS search functionality, which runs _very_ slowly for some unknown reason. Google Chromium runs v.fast. With the screen on bright, and the wireless lan card in use, you can literally watch the batter drain, and you'll get somewhere just under an hour. I can't get the proprietary video drivers to work under the std 32bit x86 binaries. I must try the AMD64bit binaries. All in all, quite happy. In terms of performance, I can listen to music in the background via rhythmbox, have a chrome instance open with a couple of tabs, and then develop python using Geany and the console, with no problems. The smart terminal has arrived.

UpDate 11-04
11-04 runs faster and smoother, with no more lag on using the Ubuntu button/OS search functionality.  Prooprietary graphics drivers installed and run ok.

Friday, January 7, 2011

Global Exception Catching in WPF

AppDomain.UnhandledException
- all threads in the AppDomain
MSDN
Dispatcher.UnhandledException
- single specific UI dispatcher thread.
Application.DispatcherUnhandledException
- main UI dispatcher thread in your WPF application
MSDN
stackoverflow

Kent Boogaart
Application.DispatcherUnhandledException is only raised if the exception was raised on the UI thread, whereas AppDomain.UnhandledException is raised for exceptions that occur on background threads. Typically I attach to both and run similar handling code. The only difference is that DispatcherUnhandledException allows you to "recover" by setting Handled to true. In other words, you could prompt your user to see whether they'd like to attempt to continue running (and potentially avoid data loss).

Exceptions in Managed Threads

---

MSDN
If an exception is not handled on either a background user interface (UI) thread (a thread with its own Dispatcher) or a background worker thread (a thread without a Dispatcher), the exception is not forwarded to the main UI thread. Consequently, DispatcherUnhandledException is not raised. In these circumstances, you will need to write code to do the following:

- Handle exceptions on the background thread.
- Dispatch those exceptions to the main UI thread.
- Rethrow them on the main UI thread without handling them to allow DispatcherUnhandledException to be raised.

---

Example of Subscribing to AppDomain.CurrentDomain.UnhandledException


App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(this.AppDomainUnhandledExceptionHandler);
}

void AppDomainUnhandledExceptionHandler
(object sender, UnhandledExceptionEventArgs ea)
{
// handle the exception
}