Spot any errors? let me know, but Unleash your pedant politely please.

Sunday, 30 January 2011

Python FTW: Clearing an FTP folder.

I'm doing some testing that involves kicking off a lot of concurrent processes. Some of these write files to an FTP server. I don't need those files once I've checked that they exist, but FTP doesn't allow deletion of multiple files (AFAIK). The first time we cleared the server, we took a directory listing and wrote an FTP script from that. It worked well enough, but we'd need to write that script each time.

So a little play with Python gave me this...


from ftplib import FTP

def clearFTPDirectory(address,
username,
directory):

server=FTP(address)
server.login(username)
server.cwd(directory)

directory=[]
server.dir(directory.append)

# list comprehension, baby!
filenames=[entry.split(':')[-1][3:] for entry in directory]

for filename in filenames:
try:
server.delete(filename)
print 'Deleted:%s'%filename
except:
print ('failed to delete "%s"'%filename)

server.quit()
…it should save a fair few cumulative hours, but more importantly, it'll save a fair few hours of tedium.

The bit that tripped me up for a little while was server.dir(directory.append) which takes the append function as a parameter. Makes perfect sense now.

Note: if you use this, you may need to check the format of the strings returned by dir(), and adjust the line that extracts the filename.

Briefly Sexist Dad

I caught myself being an old-fashioned gender-stereotype father this morning. I have a chest of drawers to put together for my daughter's bedroom. Last night I thought that I'd see if my son would like to help put it together. Then this morning I realised that was a bit daft. There's no reason why my daughter shouldn't help put it together. It's for her room after all.

So I asked her.

"Nah", she said, and turned back to watching some crap on the Disney channel.

Sunday, 2 January 2011

When i became a skeptic

I've sometimes wondered whether I've ever had faith. Ever believed in God. I don't think I ever have. I don't have any memory of faith. I went to a church pre-school, to Sunday school, to state schools that did the obligatory Christian service each morning at assembly. But I don't think I ever believed it.

I do recall though, the first time I thought faith was bollocks. It was in an R.E. lesson with Mrs Stone, aka Ol' Ma' Brick. She explained faith in terms of the chairs the class was sitting on. That we had unquestioning faith in each chair's ability to support us. I couldn't articulate why I thought this was bollocks at the time, but bollocks it most certainly was. I'd have been 11 or 12, I think.

We don't have blind faith in chairs. We have years of experience of chairs and knowledge of the physical world to inform us that the chair will support our weight. Sometimes a chair will break. A old and brittle plastic garden chair might lose a leg. A wooden glued chair may have joint that's worked loose and come apart.

Sometimes we might see a new design of chair that seems impossibly flimsy, perhaps made of wire, that becomes stronger when we sit on it. Or that appears not to have a full complement of legs. When we sit on these, we don't do so with blind faith, we do so tentatively, half expecting it to collapse, preparing for it to break, and being pleasantly surprised when it doesn't.

Saturday, 1 January 2011

Another iPad Epiphany

My wife bought a traditional compendium of games for the family for Christmas. A nice wooden box with Chess, Draughts (Checkers), Backgammon, Othello (Reversi), etc. The pieces for the games are hidden inside the box, the lid is reversible with different game boards painted on each side.

It's like a primitive, manual iPad.

Looked at another way, the iPad is like a compendium of apps, but the box under the board is like a TARDIS, and you can't lose the pieces in or under the sofa.

I'm going to start saving for iPad II.