IdeaMonk

thoughts, ideas, code and other things...

Sunday, October 24, 2010

Q1 of BORQ - Mad Libs

My solution for Mad Libs, I need to practice some more regular expression. Rubular is a great help for people like me. RegExp is in a more free-form in Ruby compared to python, which is quite good for quick experiments on irb.

I wish I could even get rid of the "if"s and make it sexier. Can't think more on this one right now.

Labels:

Q2. of BORQ - LCD display

This is an old one, pretty famously seen on uva, etc. I found Choice to be better than OptParse, as said in Choice docs - it like writing poems for command-line parsing :D

Initially I was doing a string[j].chr.to_i which seemed like a required bullshit, I wish it could've been string[j].to_i but string[i] gives back a decimal. Now one could talk about the C style '5'-'0' conversion to number, but then, that machine dependent (think ascii, ebcdic, etc). The slightly more ruby-ish way - string[j..j].to_i :)

Here's my number -
abhishekmishra@mbp [~/code/BORQ]> ruby q2_lcd_numbers.rb -s 3 9535009187
--- --- --- --- --- --- --- --- ---
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
--- --- --- --- --- ---
| | | | | | | | | | | | |
| | | | | | | | | | | | |
| | | | | | | | | | | | |
--- --- --- --- --- ---

Surprisingly the PyMos core code is 180 lines while this reaches upto 108. Python => more results per line? The absence of 'end' in python is one big reason for this. Besides I dont find my ruby code so ruby-ish at this stage.

Labels:

Wednesday, October 20, 2010

aws, sidework, etc

Just tried out AWS over a client's account, quite impressive at the ease of launching and terminating EC2 instances. Unhappy to see a Debian based AMI image under quick starts. Cloud billing always scares me :D all for its X factor on usage monitoring, besides who knows who could just plunder your instances with bogus requests, ready to take the risk? (even providers could!)

Good to see EBS volumes, the next thing to try would be RDS, we've to decide over a MySQL instance on EC2 VS Amazon RDS + EC2. Besides the compatibility of RDS + Django. HN has something for me.

Pretty happy to work for my current client, all because I could sense great deal of transparency, openness and honesty in our 3 hr meeting, and quite a comfortable one compared to a previous few where others failed to conceal. Time to brush up some django :)

Another interesting thing was to see SAP in action. MAWN! I generally dislike these heavy enterprise apps, slow, huge, and ugly. Its a mix of closeness and legacy they thrive on.

Blah!

I believe, to an extent of obviousness that fun experiments and commercial value might not always go together, at least in the same time frame or one is confused about the purpose behind such efforts. For in the long run the former does add to the profit margins, but in quite an invisible way.

Like thunder from the clouds, the bolts jump onto paths undecided, branching into thinner ends, but at last one manages to touch some significant stones, creativity too is one such tremendously energetic set of efforts, of which the one that might click is written in the future, for now one must indulge in the fun.

But anyways, they do have to be clear about what hat do I wear before they offer me one, or else isn't any unclear foo-bar-ish discussion a waste? And that need not be concealed in implications and in vague words of higher order, for the return shall be the same. Some clarity! anyone?

Wednesday, October 06, 2010

An short, half cut intro to PyGame, Part 1



Phew! the quickest presentation I've ever made.

Labels: , , ,

Sunday, October 03, 2010

Stopping it from "just leaking" in PyObjC

*** __NSAutoreleaseNoPool(): Object 0x1040bf0f0 of class NSCFString autoreleased with no pool in place - just leaking


Found a way to get rid of these leakages. We need an NSAutoreleasePool to take care of all actions that happen under threads.

So if you have a CPU/disk/network heavy operation in a module that you're importing into say, a window controller, start them under an NSThread like this -

# foo_module.py
def do_heavy_task((callback, arg1, arg2)):
...
... # all heavy ops here
...
callback(message)


and in the controller -

import foo_module
...
...
class Controller_MainWindow(NSWindowController):
...
...
@IBAction
def btnFoo_Click_(self,sender):
thread = NSThread.detachNewThreadSelector_toTarget_withObject_(
'start_heavy_task',self, (self.return_heavy_task, arg1, arg2) )

def start_heavy_task(self, params):
pool = NSAutoreleasePool.alloc().init()
foo_module.do_heavy_task(params)
del pool

def return_heavy_task(self, msg):
NSLog(msg)
...


Why all the call spaghetti with start_heavy_task, etc?
The issue is with detachNewThreadSelector_toTarget_withObject_ where I haven't been able to figure out how to pass a module's function as selector and the module as object. Also in this way your foo_module can remain independent of cocoa/objc/Foundation pollutions.

Anyhow, this works out into a clean solution. In case you have all your logical code in the window controller itself, you would find this programmish article extremely useful.

Update: a much better way would be to use decorators :D

def AutoPooled(f):
def pooled_func(self):
pool = NSAutoreleasePool.alloc().init()
f(self)
del pool
return pooled_func


Now your start_heavy_task function becomes -

@AutoPooled
def start_heavy_task(self, params):
foo_module.do_heavy_task(params)


How fantastic :D

Labels: , ,