R1D3 Learning More Python Lists and Exploring the Wordpress API
For Day 3 I continued to work more on my old_posts python script. My favorite part of 100 Days of Code is that I am taking the time to actually think through some of these problems, read documentation, and try to learn something.
Python Lists
Learned a ton about python lists thanks to this wonderful google developer guide. Specifically (after writing python for about 5 years) I learned aboutlist.extend()
for the very first time. Came in handy in this particular use case because I was doing some very inefficient for loops
to append
to a list when it was more efficient to extend
since it requires less operations.
The key differnce is that append will add a single to the end of a list, where extend will inject a list to the end of a list merging the two lists. This is particularly handy when you want to grab JSON from several requests and merge them together into a single JSON object for further processing which is what I am doing in this script.
Using Requests HEAD
I also explored more of the requests library and made an optimization that looks really silly in hindsight.In the script I was making a single request in order to grab the headers to see the total number of pages. Instea of using request.head
which has a tiny payload of headers, I was using request.get
which gets the headers along with the entire JSON payload. This was immediately thrown away since I did not use the response in later parts of the function.
Exploring the Wordpress API Filters
I also explored more of the WordPress API and started to use some API level filters to reduce the payload that I was receiving in an effort to reduce the overall time that the script takes to run. Specifically I am now usingcontext=embed
which removes the text body (since I only need the title and the link), and before=(today - 1 year + 1 day)
since I only care about posts that were written more than a year ago today.
JSON is Not SQL
I’ve been thinking about my very first forray into any sort of programming years ago. I primarily worked with Microsoft SQL Server and learned how to write efficient queries. I was thinking of how easy this problem would have been to solve if I had direct access to the database. The lesson here, that it is still taking me a while to fully wrap my head around, is that JSON is not a SQL database. You have to think about it differently. If an API offers the ability to do some filtering you should take advantage of it when you can.Thank you for reading! Share your thoughts with me on mastodon or via email.
Check out some more stuff to read down below.
Most popular posts this month
- Daggerversary
- Setting up ANTLR4 on Windows
- SQLite DB Migrations with PRAGMA user_version
- My Custom Miniflux CSS Theme
- Installing Nextcloud on a FreeBSD VPS
Recent Favorite Blog Posts
This is a collection of the last 8 posts that I bookmarked.
- "Let's pick the right product for you". from jwz
- Automattic is doing open source dirty from David Heinemeier Hansson
- Setting Up Mastodon Author Tags from Robb Knight • Posts • Atom Feed
- Getting my next SWE role from Emmanuel Blogs
- Methods of Mandarin from Isaak.net
- 12 Months of Mandarin from Isaak.net
- Villages Without Cars from Straphanger
- Some Go web dev notes from Julia Evans
Articles from blogs I follow around the net
TCP Server in Zig - Part 4 - Multithreading
We finished Part 1 with a simple single-threaded server, which we could describe as: Create our socket Bind it to an address Put it in "server" mode (i.e. call listen on it) Accept a connection Application logic involving reading/writing to t…
via openmymind.net October 11, 2024Nebraska Woman Files Suit Against All Homosexuals
A Nebraska woman identifying herself as the "ambassador" for plaintiffs "God and His Son, Jesus Christ," is suing all homosexuals on Earth for breaking "religious and moral laws". In the suit, entered into the docket as Driskell v. …
via jwz October 10, 2024Pluralistic: Cars bricked by bankrupt EV company will stay bricked (10 Oct 2024)
Today's links Cars bricked by bankrupt EV company will stay bricked: "Software-based car" is a warning, not a slogan. Hey look at this: Delights to delectate. This day in history: 2009, 2014, 2019, 2023 Upcoming appearances: Where to find me. …
via Pluralistic: Daily links from Cory Doctorow October 10, 2024Generated by openring