Jacob Kaplan-Moss

Tag: Python

TIL: A DictWriter interface for Google Spreadsheets

I’ve been using Python’s csv library approximately forever so its interfaces – particularly the DictReader and DictWriter interfaces – are almost literally muscle memory at this point. So when I needed to push a bunch of data to Google Sheets, I naturally wanted an interface that worked like DictWriter.

Here’s the code:

class WorksheetDictWriter:
    """
    Something like a `csv.DictWriter`, except for a `gspread.Worksheet`
    """

    def __init__(self, worksheet: gspread.Worksheet, fieldnames: list[str], column="A"):
        self.fieldnames = fieldnames
        self.worksheet = worksheet
        self.column = column
        self.current_row = 1

    def writeheader(self):
        self.writerow(dict(zip(self.fieldnames, self.fieldnames)))

    def writerow(self, row: dict[str, str]):
        to_write = []
        for name in self.fieldnames:
            to_write.append(row.get(name, ""))
        self.worksheet.update(f"{self.column}{self.current_row}", [to_write])
        self.current_row += 1

    def writerows(self, rows: list[dict[str, str]]):
        for row in rows:
            self.writerow(row)

I use this snippet like this:

August 21st, 2023 • google spreadsheets gspread python

🔗 Thoughts on the Python packaging ecosystem (#)

The best piece on Python packaging — why it’s the mess that it is — written yet. Required reading if you want to understand how we got here and maybe how we’ll get out.

January 21st, 2023 • packaging python

My Python Development Environment: My Python Development Environment, 2020 Edition

For years I’ve noodled around with various setups for a Python development environment. A couple of years ago I wrote about a setup I finally liked; this is an update to that post.

Bad news: this stuff still isn’t stable, and I’ve had to make some changes. Good news: the general concepts still hold, and the new tools a generally a bit better. If you’re curious about the changes and why I made them, there’s a section at the very end about that.

November 11th, 2019 • environment python

My Python Development Environment: My Python Development Environment, 2018 Edition

This is out of date. For a newer version, see My Python Development Environment, 2020 Edition

For years I’ve noodled around with various setups for a Python development environment, and never really found something I loved – until now.

My setup pieces together pyenv, pipenv, and pipsi. It’s probably a tad more complex that is ideal for most Python users, but for the things I need, it’s perfect.

My Requirements

I do have somewhat specific (maybe unusual?) requirements:

February 21st, 2018 • python

Auto-building Sphinx docs

Here’s a quick way to automatically regenerate your Sphinx docs as you make changes to the source. There’s probably a million ways to do this; here’s mine:

$ pip install watchdog
$ watchmedo shell-command \
              --patterns="*.txt" \
              --ignore-pattern='_build/*' \
              --recursive \
              --command='make html'
May 24th, 2012 • documentation python sphinx tips

What to do when PyPI goes down

Lately PyPI, the Python package index, has been having some availability issues. When PyPI goes down it really hurts Python developers: we can’t install new packages, bring up new development environments or virtualenvs, or deploy code with depedencies.

Work is ongoing – see PEP 381 – to add much-needed resiliency to PyPI, and the fruits of these labors are starting to become available. In particular, a number of PyPI mirrors are now available: b.pypi.python.org, c.pypi.python.org, and d.pypi.python.org are up and running. Each mirror has fairly up-to-date copies [1] of all the packages and metadata on PyPI.

July 20th, 2010 • downtime packaging pypi python

Python is Unix

Ryan Tomayko’s I like Unicorn because it’s Unix should be required reading for anyone doing anything involving networks or unixes these days. Like Ryan, I share a deep appreciation for the dark art of Unix system calls, and like Ryan I’m a bit dismayed to see them relegated to the dusty corners of our shiny dynamic languages.

So I read I like Unicorn because it’s Unix with glee; it’s perhaps the cleanest, clearest explanation of how preforking socket servers work, and I enjoyed seeing Ruby’s twist on the old standard.

October 6th, 2009 • python star is unix

Snakes on the Web

A talk given at PyCon Argentina and PyCon Brazil, 2009.

Web development sucks.

It’s true: web development, at its worst, is difficult, repetitive, and boring. The tools we have suck. At best, they make web development slightly less painful, but we’re a long way from making web development awesome.

The history of web development tools is a history of trying to solve this problem. It’s a history of asking, “how can we make this suck less?” It’s important to understand this history, because we can look at past trends and use them to predict the future.

September 4th, 2009 • python speaking web

More buildout notes

I got a lot of great feedback on my buildout tutorial I posted last week. In general, the comments there have some excellent tips, tricks, and extra pointer, so check ’em out.

After reading the comments and a few more I got over email, I thought I’d share a selected grab-bag of updates, hints, and details for those fooling around with Buildout and Django.

Django trove identifier

James Bennett pointed out that Django has its very own PyPI classification, Framework :: Django. It’s a good point: anyone posting Django-related packages to the cheeseshop ought to make sure that the list of classifiers in setup.py includes the Django classifier.

April 20th, 2009 • buildout python

Nobody expects Python packaging!

Python has one package distribution system: source files and setup.py install.

And easy_install.

Python has two package distribution systems: setup.py install and easy_install. And zc.buildout.

Python has three package distribution systems: setup.py install, easy_install, and zc.buildout. And pip.

Amongst Python’s package distribution are such diverse elements as…

April 13th, 2009 • packaging python

Python implementation details

I’m sitting here at PyCon at the Python Language summit discussing differences between Python versions, and the topic of implementation details came up. The main part of getting Django working on alternate Python VMs was fixing the various assumptions we made about implementation details of Python.

I went back and dug through Django’s ticket tracker for the issues alternate VM issues (most from the Jython developers, some from the PyPy developers) that we had to fix; it’s a pretty instructive list of things you shouldn’t rely on if you’d like your Python code to run on alternate VMs:

March 26th, 2009 • python

Why I'm excited about Python 3

This is a rough transcript, with links, of a 3-minute lightning talk I gave at LCA. It’s obviously not a complete argument; just a 180-second pitch for why the drama about Py3k is all smoke and no fire.


I’m excited about porting my code to Python 3, and you should be, too.

I’m sure all the Python users here are aware of the recent release of Python 3.0. Python 3.0 is the first Python release in something like a decade that is fundamentally incompatible with the previous release. There’s been a lot doom & gloom & naysaying about this. Some have suggested that Python 3 marks the moment where Python jumped the shark.

January 22nd, 2009 • python python3

PyCon Braindump

PyCon 2008 Brain Dump

There are some great roundups of the content at PyCon out there; this isn’t one of them. See, I have this notebook (Moleskine FTW!) I carry with me everywhere, and now it’s chock-full of note from PyCon; this is a braindump.

  • EAV

  • Mammoth replicator

  • Django 1.0 will support Python 2.3, but later releases likely will not. We’ll need to start working on moving to Python 3.0, and that means slowly dropping other Python versions until we reach 2.6 and can start using 2to3.

March 19th, 2008 • django pycon python

Five things I hate about Python

Inspired by Titus (who was in turn inspired by brian d foy), here’s what I hate about Python. I completely agree with Brian that you can’t trust any advocate who doesn’t know enough to find stuff to hate. Given that I spend a lot of time advocating Python, writing down what I hate seems a good exercise. Perhaps I’ll do the same for Django in the future…

Anyway, here are the five things I hate about Python, presented Letterman-style:

March 4th, 2007 • python