Archive for November, 2009

November 16, 2009: 6:58 pm: kpdrowing

Saw this in a post on the Concept 2 Forum by igoeja. Back issues of Rowing News are in Google Books available online for free and with the ability to search. Cool!


November 11, 2009: 7:02 am: kpdindoor-rowing, programming, rowing

My unofficial interface to the Concept 2 Online Logbook is ready.

This allows one to programmatically get information from the logbook (such as your total meters rowed to date) or to add workouts to the logbook from another program.

For example, entering this URL in your browser after getting your own API KEY:

http://c2logapi.appspot.com/api/1/user/totalmeters?format=xml&api_key=xxx

would cause the browser to prompt you for your logbook username and password and then it would return the total meters you’ve rowed to date.

Details and code examples in Python and Java are at the (Unofficial) Concept 2 Online Logbook REST API Home Page.

: 6:42 am: kpdPython, wxPython

I’ve been looking to find a way to resize a font when the window’s size changes. Here is code that will create a font that is close as possible to a given height in pixels without specifying anything about the font width. Osku Salerma posted some code in GitHub that did the trick.

I changed it a bit to make it self-contained. Here’s my modified version of his code to create a wxPython font given a height in pixels:


<pre>
    def createFontFromHeightInPixels(self, heightInPixels, family, style, weight):
        # return a font that's as close to 'heightInPixels' as possible without being larger
        # modified from code at http://github.com/oskusalerma/blyte/blob/master/util.py  
        fontSizeToTest = 6
        bestFontSoFar = wx.Font(fontSizeToTest, family, style, weight, encoding = wx.FONTENCODING_ISO8859_1)
        closestDiffInPixelsSoFar = 1000
        testDc = wx.MemoryDC()
        testDc.SelectObject(wx.EmptyBitmap(512, 32)) 
        while 1:
            testedFont = wx.Font(fontSizeToTest, family, style, weight, encoding = wx.FONTENCODING_ISO8859_1)
            testDc.SetFont(testedFont)
            heightOfTestedFont = testDc.GetTextExtent("_\xC5")[1]
            diff = heightInPixels-heightOfTestedFont
            if diff >= 0:
                if diff < closestDiffInPixelsSoFar:
                    closestDiffInPixelsSoFar = diff
                    bestFontSoFar = testedFont
            else:
                break
            fontSizeToTest += 2
        return bestFontSoFar 
</pre>

It would be used like this assuming the above function is in the window class or one of its superclasses:


<pre>
def __init__(self, *args, **kwds):
    .
    .
    .
    self.Bind(wx.EVT_SIZE, self.OnSize)     

def OnSize(self, event):
        size = event.GetSize()
        currentFont = self.GetFont()
        newFont = self.createFontFromHeightInPixels( (size.height*0.8), 
            currentFont.GetFamily(), currentFont.GetStyle(), currentFont.GetWeight())
        self.label_1.SetFont(newFont)
 </pre>