Python, pre-commit, and black

If you’ve had trouble configuring a python project with local repo copies of black and get errors you can’t explain, this may help. Maybe. I ran into some config issues somewhere between my pre-commit config and my black config. In case someone else runs into this I wanted to leave some bread crumbs. The issue. Black keeps exiting with “exit code: 123” after it tries formatting non-python files. It then told me it couldn’t format files that weren’t python files: yaml, md, and toml. I’d thought I’d excluded everything I needed to, but something was still tripping me up. ...

November 28, 2024 · Andrew Diederich

Python argparse with defaults -- in-script and importable

I’ve tried using argparse (and optparse before that) and have had trouble finding a workflow that made me happy. This one is pretty close. It allows you to make defaults for when you call a script on the command line and makes those same defaults available if you import the module. It works like this: Make a dictionary with the defaults you want. In the function you write that parses the arguments, use the dictionary values as the defaults. Merge the defaults dictionary and the arguments dictionary together. Return the merged dictionary. The Gist version is available here, or you can see it below, too. ...

June 27, 2017 · Andrew Diederich

Cat facts & python

I’d not actually used anything with json before, but (unsurprisingly) the requests library came through. >>> import requests >>> url = 'http://catfacts-api.appspot.com/api/facts?number=1' >>> print(requests.get(url).json()['facts'][0]) Cat families usually play best in even numbers. Cats and kittens should be acquired in pairs whenever possible. While that gets you the answer, when did you ever really just want the answer? That’s pretty uninteresting. Let’s get there in stages. This is straightforward (yes, I’m assuming you have requests installed already): ...

June 10, 2017

CSV reader examples for python 3

Among other things, I like python. I quite like the Talk Python to Me podcast, done by Michael Kennedy. He is, among other things, a python trainer. Among his online python training videos is one on parsing csv (comma separated value) files. His example didn’t actually use the python csv module, so I whipped up a couple of examples that used csv.reader and csv.DictReader. These use python 3.4. csv.reader example: #!c:\anaconda3\python.exe '''taken from https://www.youtube.com/watch?v=qajONCIvhEc https://support.spatialkey.com/spatialkey-sample-csv-data/ http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv ''' import csv import os def main(): print("Hello File I/O minicast using csv.reader") filename = os.path.abspath(os.path.join("data", "Sacramentorealestatetransactions.csv")) print(filename) with open(filename, 'r') as csvfile: fin = csv.reader(csvfile, delimiter=',') header = next(fin) print(header) # ['street', 'city', 'zip', 'state', 'beds', 'baths', 'sq\_\_ft', 'type', 'sale_date', 'price', 'latitude', 'longitude'] entries = [] for line in fin: # lines look like # \['3526 HIGH ST', 'SACRAMENTO', '95838', 'CA', '2', '1', '836', 'Residential', 'Wed May 21 [00:00:00](http://www.youtube.com/watch?v=qajONCIvhEc&t=00h00m00s) EDT 2008', '59222', '38.631913', '-121.434879'\] row = dict() for i, h in enumerate(header): row[h] = line[i] entries.append(row) entries.sort(key= lambda r: -1\*int(r['price'])) # Entries are a list of dicts. Each line looks like: # {'street': '9401 BARREL RACER CT', 'state': 'CA', 'sq\_\_ft': '4400', 'city': 'WILTON', 'beds': '4', 'baths': '3', 'sale_date': 'Fri May 16 [00:00:00](http://www.youtube.com/watch?v=qajONCIvhEc&t=00h00m00s) EDT 2008', 'price': '884790', 'longitude': '-121.194858', 'latitude': '38.415298', 'zip': '95693', 'type': 'Residential'} for e in entries\[:5\]: print("{0} beds, {1} baths sold for ${2:,}".format( e['beds'], e['baths'], int(e['price']) )) if *_name_* == '__main__': main() csv.DictReader example: ...

September 3, 2015