George V. Reilly

Blog 2014

My DasBlog-based blog at http://www.georgevreil­ly.com/blog/ has been out of commission for months. I've been meaning to replace it for a long time, but I only just got around to making a serious effort, as I realized that otherwise I would have no posts at all for 2014. I received only a handful of complaints about its absence; if there had been more, I would have fixed it sooner.

DasBlog is a fairly light­weight blogging engine that runs on ASP.NET. It doesn't require a database, but it does require the ability to write XML blogpost entries to the local filesystem. That's a non-standard con­fig­u­ra­tion for ASP.NET and IIS websites, which inevitably causes continue.

Turn off Windows Defender on your builds

I've spent some time this evening profiling a Python ap­pli­ca­tion on Windows, trying to find out why it was so much slower than on Mac or Linux. The ap­pli­ca­tion is an in-house build tool which reads a number of config files, then writes some output files.

Using the Run­SnakeRun Python profile viewer on Windows, two things im­me­di­ate­ly leapt out at me: we were running os.stat a lot and file.close was really expensive.

A quick test convinced me that we were stat-ing the same files over and over. It was a com­bi­na­tion of explicit checks and implicit code, like os.walk calling os.path.isdir. I wrote a little cache that memoizes the results, which brought continue.

SQLAlchemy Sharding

I wrote up some lessons that I learned about SQLAlchemy Sharding at the Cozi Tech Blog.

Python String Formatting

Python has long had a string in­ter­po­la­tion operator, %.

Python 2.6 and 3.0 introduced a new, richer set of string formatting operations. See PEP 3101 for the rationale.

One trick that I liked with the old way of formatting was to put the locals() dictionary or self.__dict__ on the right-hand side

>>> def stuff(a, b):
...  c = a+b; d = a-b
...  return "%(a)s, %(b)s, %(c)s, %(d)s" % locals()
...
>>> stuff(3, 17)
'3, 17, 20, -14'

It took me a few minutes to figure out how to do the equivalent with string.format: use the ** syntax to unpack the dict into kwargs.

>>> class Person(object):
...  def __init__(self, name, age):
... 
continue.

Augmenting Python's strftime

The Cozi Tech Blog needed some love, so I wrote a post on augmenting Python's strftime.

Flattening List Comprehensions in Python

Python has list com­pre­hen­sions, syntactic sugar for building lists from an expression.

>>> [2 * i for i in (2, 3, 5, 7, 11)]
[4, 6, 10, 14, 22]

This doesn't work so well when the com­pre­hen­sion expression is itself a list: you end up with a list of lists.

>>> def gen():
...     for l in [['a', 'b'], ['c'], ['d', 'e', 'f']]:
...         yield l
...
>>> [l for l in gen()]
[['a', 'b'], ['c'], ['d', 'e', 'f']]

This is ugly. Here's one way to build a flattened list, but it's less elegant than the com­pre­hen­sion.

>>> x = []
>>> for 
continue.

Northwest Python Day 2009

[Eric holding forth on BuildBot]

Eric and I attended Northwest Python Day 2009 today at the University of Washington. There were about 50 people present, with a few out-of-town visitors from Portland and Vancouver BC.

It was a mixed bag. I found the afternoon sessions more in­ter­est­ing than the morning ones.

The morning talks started with a set of five-minute lightning talks, including:

Browser Interface, Local continue.

Cheetah Tips

At Cozi, we're writing our new web services in Python (a story for another day). I wrote up a few hard-won tips on using the Cheetah Template library at the Cozi Tech Blog.

Multilingual

This week, I have written code in C#, C++, Managed C++, C, WiX, NAnt, Ac­tion­Script, VBScript, JScript, cmd batch, NMake, HTML, XSLT, and Ruby. And I will probably get some Python in before the weekend is over. <boggle/>

Python Batchfile Wrapper, Redux

Batchfile Wrapper

I've made some sig­nif­i­cant changes to my Python Batchfile Wrapper. The main virtue of this wrapper is that it finds python.exe and invokes it on the associated Python script, ensuring that input redi­rec­tion works.

I've also adapted py2bat to work with my wrapper. I'm calling my version py2cmd.

Here's my latest batch file, which is shorter than its pre­de­ces­sor.

To use it, place it in the same directory as the Python script you want to run and give it the same basename; i.e., d:\some\path or oth­er\ex­am­ple.cmd will run d:\some\path or oth­er\ex­am­ple.py.

@echo off
setlocal
set PythonExe=
set PythonExeFlags=-u

for %%i in (cmd bat exe) do (
    for %%j in (python.%%i) do 
continue.
Previous » « Next