i’ve been working through ‘automate the boring stuff with python’ by al sweigart (https://automatetheboringstuff.com/2e/). it’s easy sometimes and hard other times which i guess seems like an appropriate way for anything to be. this post will be a step by step of me solving a python problem. i told noah it would be long but then i forgot to write it and now i am writing it having forgotten a lot of what i wanted to include so who knows. let’s see.
at the end of the “manipulating strings” chapter of the book is a practice project called “table printer”. table printer is a function that i am to write with these parameters:
Write a function named printTable() that takes a list of lists of strings
and displays it in a well-organized table with each column right-justified.
Assume that all the inner lists will contain the same number of strings.
For example, the value could look like this:
Your printTable() function would print the following:
Hint: your code will first have to find the longest string in each of the
inner lists so that the whole column can be wide enough to fit all the strings.
You can store the maximum width of each column as a list of integers. The
printTable() function can begin with colWidths = [0] * len(tableData), which
will create a list containing the same number of 0 values as the number
of inner lists in tableData. That way, colWidths[0] can store the width of the
longest string in tableData[0], colWidths[1] can store the width of the longest
string in tableData[1], and so on. You can then find the largest value in
the colWidths list to find out what integer width to pass to the rjust() string
method.
so when i read this i was like “oh this will use [some thing that i worked on earlier in this chapter] and [some other thing i worked on earlier in this chapter] and all you’ll have to do is write a few different functions that implement each of those aspects and then concatenate them into one table with a column width set.”
but then i read the hint and i tried to implement the hint to make it easier on myself. at first i understood the hint as meaning “write colWidths = [0] * len(tableData)” for each column width you want to generate, which lead me to writing this:
which i don’t have to tell you is an incorrect interpretation of the hint, and also an incorrect implementation of that misinterpretation. fortunately i only spent 3 hours working on that erroneous attempt. i posted frustrated on twitter about it and my friend tori got back to me about it (shout out tori) and gave me an idea. they were stupid ****** when they responded and i didn’t much understand what they meant but the general idea was to write colWidth in such a way that it held the length of the items in each line then write a function for it. or something along that line. i didn’t know how to implement it but i knew that i was wrong so i tried some new shit.
also here’s the table i wrote for this:
next i tried writing a nested for loop that would iterate through the range of the length of the table, which would target the table in which the other tables are stored. then i could iterate through those tables and set wid as equal to the length of the items in those tables and print that width. i did a weird job of it and got distracted by some other red herring idea and included some shit that didn’t make any sense. it looked like this:
the reason [(len(k))] is followed by * len(table) is because i was trying to use len(table) as a replacement for the hint idea (colWidth[0]). the result of running the above snippet of code is:
[3, 3, 3]
[4, 4, 4]
[5, 5, 5]
[6, 6, 6]
[4, 4, 4]
[6, 6, 6]
[4, 4, 4]
[5, 5, 5]
[4, 4, 4]
[7, 7, 7]
[8, 8, 8]
[5, 5, 5]
which is obvious if you read the code but as the person who wrote the code it didn’t make any sense to me. then i thought about it for an hour while writing some other stupid shit that i don’t feel like writing about. but the reason that looks like that is because the brackets around (len(k)) make it into a list which is then multiplied by the length of the table. so when it hits the nested table of "['ear', 'lord', 'crash', 'fungus'],” it calculates the length of the first item and then takes the length of the original table table (which is 3) and prints the length of the first word that many times in a table. so as you can see, it prints [3, 3, 3].
after i wrote that and understood it i was able to figure out how to isolate the length of each item via nested loops even though i hadn’t properly accomplished the prescribed task yet, i was capable of creating tables with the lengths of the items. because i could get those lengths, i could create a function that would find the largest of those items and then set that length + a constant as the ljust for the new table we’re creating. that would allow for a buffer between each item in each line that adjusts based on the largest length of the string in the table, as suggested by the hint.
i tried that with this:
which clearly didn’t work and is nonsense.
and i tried it with this:
which didn’t do much of anything.
so then i started over. i said fuck this shit and i went back to work on the hint and i figured out what the hint actually meant. which is that if you do:
colWid = [0] * len(table)
it will create a table that is the length of the table and populate it with 0s. then if you can isolate the lengths of the longest string in each nested table you can write it onto the appropriate 0 in colWid table and it will store it, then you can call the index of that table in colWid and it will print the longest string.
so with that in mind i started working on isolating the length of the longest string in each nested table. so i wrote a function called “itemLenFinder0” bc i did not know how to write this function such that it would iterate over each table. the function looked like this:
then i wrote this same function 2 more times, but for 1 and 2 respectively. like this:
of course i should have written a single function that would handle all 3 but as said above i could not figure out how. the attempts are too embarassing to post here and not nearly honest enough attempts to illuminate anything other than my own lack of understanding, which i have a fairly good grasp of already.
after i wrote those functions and tested them i was very happy with myself because i had accomplished a simple task. i often feel very happy with myself after accomplishing a simple task.
next, i wrote the hint table again:
colWid = [0] * len(table)
and then because i could not figure out how to iteratively apply these functions to colWid i simply wrote it out 3 times. like this:
this will create a table with 3 0s in it, then take the integer of the length of the longest string from each nested table and change the appropriate 0 in the colWid table to that number so that we can call that integer later.
next i tried to simply check and see what the largest of colWid[0] colWid[1] and colWid[2] was so that i could use that + a constant as the buffer for my text justification. i simply created a variable and set it to 0, then i tried to compare the integers in the colWid table. unfortunately i misunderstood how “and” works in python. i didn’t notice until noah pointed it out (shoutout noah) and even then i didn’t understand until he explained it. it looks like this:
as you already know, “and” is simply checking the truth value of colWid, and not comparing the integer values. also i messed up and wrote 2 on the first and 2nd clause instead of 2 and then 1. fortunately it do not matter and it functioned exactly as i wanted it to anyway.
then i wrote a function called tableprint, that would take in a table, set a variable to the range of the length of the table, then iterate over the first nested table, printing each string and adjusting it according to realWid + a constant. (reminder: realWid is the length of the longest string in any of the nested tables.) then it would repeat the process over the 2nd nested table, then the 3rd nested table.
it looks like this:
it is silly that there is “k = range(len(table))” in there bc it does nothing and i forgot why i wrote it, but it’s there. when you run it against the table i wrote it prints:
which is exactly what i want it to print.
the unfortunate aspect of this is that i wrote it without iterating properly, i included a bunch of shit that is completely useless, and i fundamentally misunderstood crucial aspects of how python works.
the fortunate aspect of this is that it works! and for that i am overjoyed. i spent about 7 or 9 hours working on this problem and it felt very nice to finish it even if the program itself is very poorly done. one thing that is especially frustrating to me about programming (about all things) is details. i am not detail oriented and frankly i would describe myself as detail opposed.
here’s the whole piece:
here’s something i posted on facebook in 2017, it’s appropriate now as well:
i was driving earlier and the trees were a lot of nice colors. like yellow and green and orange and red for example. but also other colors that arent such easy words. like burnt and beaten and crisp and purged and darned. it sounds like bad things but its not bad things its good colors. trees are really very beautiful and so are clouds but clouds are usually the same colors they always are and they can be described with their own names. like "that cloud is as white as a cloud" or "that cloud has sunset colors" or "that cloud is dark like a storm" which is really nice for clouds because they are what they are and we know that about them. trees are different i guess.
fall is a good season and so is winter and so is spring and so is summer. you could write a book abt how good the seasons are.
sometimes things are easier and sometimes they are harder but right now i am drinking coffee which doesnt make anything much of any way but does let me taste some nice bean flavors.
playlist:
youtube: https://youtube.com/playlist?list=PL4bUgvWbrZGIeV3q4KupS2KL8JVGd0W-q
mega: https://mega.nz/folder/CEMxGSgZ#bm_FCbMMcPFAc5o0LdQXhg
Osibisa - [Black Magic Night - Live CD2 #05] Sunshine Day
Fela Kuti - [Fela's London Scene #03] Who're You
Fiona Apple - [Fetch The Bolt Cutters CD1 #12] Drumset
Fiona Apple - [The Idler Wheel Is Wiser Than the Driver of the Screw and Whipping Cords Will Serve You More Than Ropes Will Ever Do CD1 #04] Jonathan
Kanye West - [The Life of Pablo CD01 #04] Famous
Freddie Gibbs - [Alfredo #02] God Is Perfect
Cassandra Jenkins, Cassandra Jenkins - [An Overview On Phenomenal Nature #06] Hailey
Steely Dan - [Gaucho #05] Time out of mind
Steely Dan - [Can't Buy A Thrill #02] Dirty Work
Steely Dan - [Can't Buy A Thrill #04] Midnight Cruiser
Steely Dan - [Pretzel Logic #03] Any Major Dude Will Tell You
Steely Dan - [Pretzel Logic #04] Barrytown
大貫妙子 - [カイエ #03] 輪舞(ラ・ロンド)
大貫妙子 - [カイエ #04] Le courant de mecontentment (不満の暗流)
Megan thee Stallion - [Suga #06] Rich
Del Tha Funkee Homosapien - [I Wish My Brother George Was Here #09] Dr. Bombay
Megan Thee Stallion - [Something for Thee Hotties CD1 #04] Southside Forever Freestyle
Megan Thee Stallion - [Something for Thee Hotties CD1 #05] Outta Town Freestyle
Flying Lotus - [Until the Quiet Comes CD1 #04] Heave(n)
Flying Lotus - [Until the Quiet Comes CD1 #06] All the Secrets
quick note: this playlist is just a selection of songs from albums i’ve been listening to while walking to / from work. i didn’t organize them at all and there’s no cohesion and i repeated artists a lot but if you like my playlists normally then you’ll like these songs.