Cognitive Mileage

Archive for the ‘Programming’ Category

This algorithm returns a list that is a power set of the inputted list. The code for int2bin is from DaniWeb‘s Python community.

So the module would give the following output:

>>> PowerSet([1,2,3])
[[3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]

This was recently a problem for me when I wanted to use Python 2.5’s string Templates and random number generation with Iron Python Studio but couldn’t. Here’s how you enable the libraries in Iron Python Studio (IPS):

1. Install the version of Python (CPython) you want to use.
2. Direct IPS to the Python directory you are using by inserting the following lines into the beginning of the .py file you wish to use the modules in:

import sys
sys.path.append("C:\\Python25\\Lib")

For your own work, just change “”C:\\Python25\\Lib” to point to the Lib folder of whatever version of Python (in whatever location) you want to use.

So my girlfriend’s daughter accidently spills water all over my Macbook screen leaving huge bright white splotches in the lower right and left corners of my screen. I freak out and take my MB to an Apple technician and he tells me it will be $750 to replace the screen but that I can probably just evaporate the water myself by running an intensive process that heats up my MB. Obviously, I opt for the latter.

The following Python program did the job.

i = 0
while True:
    i = i + 1

It got my CPU A up to a consistent ~80c with the fans obviously kicking on high. I put some pillows behind the screen to increase the heat.

I’ve been running it for about 3 hours now and the splotches are almost cleared up. It will probably take 4-5 hours total to complete the evaporation. But it beats paying $750 for a replacement..

There seems to be no permanent damage to the screen.

UPDATE
Well, it has been about 15 hours total now of drying, some with the program on, most with it off and the splotches are almost all gone. All that is left is a little pea-sized dot for the right splotch. I’ve no reason to think it won’t disappear by the end of the day.

FINAL UPDATE
24 hours later: screen is as good as new. You wouldn’t be able to tell the difference if you tried.

One of the number one problems for someone trying to implement syntax highlighting on a .NET platform is that when updating the RichTextBox to have the updated highlights, there is flickering. You have to highlight and change every keyword every time the user enters a new character (the TextChanged event).

There are two common methods. The first is to simply repaste all of the code into the RichTextBox and change the text formatting as you paste. The second is to highlight all the pieces of code and color or uncolor them as needed. Both cause undesirable flicker.

Stopping the RichTextBox Window Update without Win32
The flicker comes because the RichTextBox is updating every time you highlight a new word. The only solutions I’ve seen people offer to stop this is to DllImport Win32 libraries and do a LockWindowUpdate on the RichTextBox or send it a WM_SETREDRAW message.

Most people, upon discovering it and being given no alternative, find that solution disappointing. There should be a purely .NET solution, they say.

I’ve found one. I had to: I’m using IronPython Studio and, AFIAK, it can’t do DllImport.

The Solution
This is written in Python but can easily be translated to C# or VB.

def _rTextBox_TextChanged(self, sender, e):
    # Create new (invisible) RichTextBox for processing
    rt = RichTextBox()
    # Make sure the font is the same
    rt.Font = self._rTextBox.Font
    # But only paste unformatted Text
    # to erase formatting on non-keywords
    rt.Text = self._rTextBox.Text
    # Save cursor position
    user_pos = self._rTextBox.SelectionStart

    # Do text highlighting on invisible RichTextBox
    …

    # Now paste formatted text back into original (visible) RichTextBox
    self._rTextBox.Rtf = rt.Rtf
    # Reset cursor position
    self._rTextBox.SelectionStart = user_pos

The basic idea is that you create your own (invisible) RichTextBox within the code, copy the Text (unformatted text) from the visible RichTextBox into it, highlight the keywords, then paste the Rtf (formatted text) back into the visible RichTextBox. That way there is no flickering while you are doing the highlighting.

Drawbacks
There is still some minor flickering when the text gets large enough to consume more than a page. Also, the scroll bar position is reset when you edit text above the bottom line of the text.