Python

Advent of Code 2022, Day 1

Another year, another Advent of Code. Though I kind of skipped last year. I think I may have been too busy or something. I do enjoy doing these puzzles, though they aren’t really “code projects”.  Each day is just a puzzle, that is solved with math.  Most of the problem, is figuring out what exactly is being asked.  The other part is just, figuring out how to format the data set you are given.  A lot of these are kind of repetitive, open your data file, read it in, convert it to a useful format, loop through the data, performing some action, and output some sort of total.

For this year’s theme, Santa’s Elves are taking a journey to a magical grove to collect food for the reindeer.

Day 1 – Elf Food Calories

The Problem Part 1:

The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves’ expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food – in particular, the number of Calories each Elf is carrying (your puzzle input).

The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they’ve brought with them, one item per line. Each Elf separates their own inventory from the previous Elf’s inventory (if any) by a blank line.

In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they’d like to know how many Calories are being carried by the Elf carrying the most Calories.

The Problem Part 2

By the time you calculate the answer to the Elves’ question, they’ve already realized that the Elf carrying the most Calories of food might eventually run out of snacks.

To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups.

So, each day consists of a Part 1 and a Part 2.  Generally speaking, each of these two parts is semi related, and can be completed with most of the same code. This one is simple enough, split the list up by lines, then split it into each elf’s calories, sort the list so the highest valies are at the front and print those three values out.  Part 1 only needs the highest, Part 2 needs the next two.

with open("Day01Input.txt") as file:
    calories = file.read()

calories = calories.replace("\n"," ")
elves = [each.split(" ") for each in calories.split("  ")]
totals = []

for each in elves:
    total = 0
    for i in each:
        total += int(i)
    totals.append(total)
 
totals.sort(reverse=True)
print(totals[0]+totals[1]+totals[2])    

100 Days of Python, Project 081 – Morse Code Generator #100DaysofCode

I feel like these last 19 projects are more complex and should get their own post each.  Just skimming over what they are, they all seem to be quite a bit more complex than a lot of the previous projects, and they also are given no help or instruction, just “Make X”.

I wanted to comment a bit briefly on the course as a whole so far though.  I’ve really enjoyed it, and I have lots of idea of projects I WANT to do, and I have started on a few, but I’m doing my best to force myself to focus on finishing this course FIRST.  Eyes on the Prize, so to speak.  The flow overall is pretty good, though I noticed some of the course comments, many people were complaining a bit during some parts.  Mostly about the lack of videos during the final, third or so, of the course.  I admit, I kind of feel for them a bit, but I also do somewhat get the point.

The point is, at some point, you do need to do this stuff without everything being hand-holdey the whole way.

That said, she was doing a pretty good job of this already within each “Broader” topic.  For say, Turtle Graphics, she would start off being extremely “Do X, Do Y, Do Z” about pretty much every step of a project.  Next lesson, there would be a bit less, then by the third it would ask the students to do something, then give a solution.  Eventually it was just, “Do a while project” with “here is a solution”.  It was gradual over 5-6 lessons.  

It feels like she tried to do this quite a bit with the overall course as well, the problem is, when you end up in an area that is completely unfamiliar again, like the Flask sections, or the Data Analytics sections, it would be a bit nice to start each “New Section” with some videos and a bit more help.

Personally, I didn’t find it too much of a problem, I was already doing many of the projects without watching any of the videos, then watching videos afterwards to see how she did it.  The user comments also were really great for suggestions and ideas.

I would still recommend the course.  It seems to be one of Udemy’s best selling courses too.

Anyway, on with the project, and I admit, I am kind of adding some filler.

Day 81 – Morse Code Translator

Ok, so, compared to what the rest of the “Professional” level projects seem to be, this particular project felt stupid super easy.  Like, “am I doing something wrong” easy.  I may have even already done this one evening on CodeWars.com.  I even fleshed it out a bit just to make it more interesting.

The project was to translate a user input into Morse Code.

Even just thinking about that, at it’s core, it’s literally just find and replace on a string.

Step One – Create a Dictionary of Morse Code sequences and the equivalent Alphabet letters

Step Two – Get a User Input

Step Three – Loop through the Input and convert each character using the Dictionary to Morse Code

That is IT.  That’s nothing.  The assignment also specified “Text Based”, though I considered converting it to work in TKinter or maybe even Flask.

I did spruce things up a bit, I added a prompt so the user can translate additional strings.  I looked up how to make sound output, and added an option to play the Morse Code out over the PC Speaker, which was fun, and new useful information.

Anyway, it’s on Github, but this is the entire code.

import winsound
from time import sleep

# Morse Code
morse_code = {
    'a': '·−',
    'b': '−···',
    'c': '−·−·',
    'd': '−··',
    'e': '·',
    'f': '··−·',
    'g': '−−·',
    'h': '····',
    'i': '··',
    'j': '·−−−',
    'k': '−·−',
    'l': '·−··',
    'm': '−−',
    'n': '−·',
    'o': '−−−',
    'p': '·−−·',
    'q': '−−·−',
    'r': '·−·',
    's': '···',
    't': '−',
    'u': '··−',
    'v': '···−',
    'w': '·−−',
    'x': '−··−',
    'y': '−·−−',
    'z': '−−··',
    '0': '−−−−−',
    '1': '·−−−−',
    '2': '··−−−',
    '3': '···−−',
    '4': '····−',
    '5': '·····',
    '6': '−····',
    '7': '−−···',
    '8': '−−−··',
    '9': '−−−−·',
    ' ': '/'
}

# Loop Variable
keep_going = True
valid_answers = ["yes","y","no","n"]

# Sound Variables
frequency = 700  # Set Frequency To 2500 Hertz
duration_short = 100  # Set Duration To 100 ms == .1 second
duration_long = 300  # Set Duration To 300 ms == .3 second

while keep_going:
    # Get String of Text to Convert
    conversion_string = input("Please enter a string to convert to Morse Code:\n").lower()
    # Fresh Code String Each Time
    code_string = ""
    # Do the Conversion
    for letter in conversion_string:
        if letter in morse_code:
            code_string += morse_code[letter]+" "
        else:
            code_string += letter
    # Show the Result
    print("Your Morse Code is:\n")
    print(code_string)
    # Ask if the user wants to hear the sound
    go_on = ""
    while go_on not in valid_answers:
        go_on = input("Would you like to play this sound? (Yes/No) ").lower()
    # If Yes, Play the sound
    if go_on == "yes" or go_on == "y":
        for beep in code_string:
            #print(beep)
            if beep == "−":
                winsound.Beep(frequency, duration_long)
            elif beep == "·":
                winsound.Beep(frequency, duration_short)
            # Needs a brief pause
            sleep(.05)

    # See if the user wants to do another conversion.
    go_on = ""
    while go_on not in valid_answers:
        go_on = input("Translate another string? (Yes/No) ").lower()
    # Quit if no more conversions
    if go_on == "no" or go_on == "n":
        keep_going = False

100 Days of Python, Projects 71-80 (but not really) #100DaysofCode

Ok, I’m going to be frank here.

I’m skipping most of the Data Analytics portion of this course.  It’s 9 Lessons, Day 71-80.  It is all done using Google Colab Notebooks, it’s all extremely, extremely, extremely, repetitive, yet I am not really feeling like I am learning anything.

Every lesson amounts to, Open this Google Collab Notebook, which is a new and… interesting tool, I guess it’s similar to a Jupyter Notebook.  It’s basically, running “code” in weird choppy step by step chunks.  Each lesson amounts to.

  • Open a provided CSV file.
  • Make a Graph
  • Format the Graph.
  • Maybe do some layered graphs.
  • Sometimes the type of graph is different (line, bar, scatter, etc).

It’s dull, it’s repetitive, I have almost zero interest in statistics and data science honestly.  Fun Fact, Statistics is the one class I dropped in college.  I could have gotten a minor in Math with my Mechanical Engineering Degree with one more math credit, so I took Statistics.  It was the only class I had any actual homework in my last semester, everything else was writing reports, the homework was absolutely brutal, and the entire class felt like butchering math to create Conformation Bias.  It was stupid, it doesn’t account for nuance and one offs, which is what you should actually care about because those are the failure points.  I could go on and on.

Anyway, the sections tarted out pretty fun, learning  new tool, making fancy graphs, then halfway through I found I was just copying and pasting answers from the lecture because I just wanted it to be over and I didn’t care about the material.  

So I’m just, skipping it. This isn’t a graded University Course, hell I have made plenty of small side projects during this course to fill in the idea of “100 Projects in 100 Days”.

Also, if I wanted to make pretty charts, I would just use Microsoft Excel.  Because I am already pretty good at that.

This leads into the final “stretch” of this course, which I have already started working on some.  The Final 19 Projects, which are all basically, open ended suggestions.  There is a Data Analytics one in there, which I actually probably will go ahead and throw together some Charts for.  I have the perfect Data Set for it, ever since I bought my car, back in 2015, I’ve tracked my mileage and fuel usage every time I fill up (in Excel).  I can dump out a huge CSV was and manipulate that data in Google Collab or a Jupyter Notebook.

100 Days of Python, Projects 66-70 #100DaysofCode

Whew, I didn’t really think I’d get to 9 parts in this series, and I am only around 2/3rds of the way through even.  I actually may change up the format later with the last 20 projects that are listed as “Professional”.  Maybe one post each.

The bulk of this round is wrapping up the Flask projects and building a simple blog that runs on Python.  It’s been fun.  I’ve been a bit busier than normal slow my pace has slowed, but that’s ok too.  Day 66 in particular felt like it took longer than it really should have, given how little it felt like it was doing.

Day 66 – Build RESTful APIs

Kind of a different sort of project.  It’s building something, but not really anything, with any sort of interface.  All of the interaction is done through Postman (or the URL if one wants), and the responses are all JSON of some sort.  

We built 6 API interfaces to work with a database of Coffee shops.

  • One returned all of the shops.
  • One returned all of them in a particular city.
  • One returned a specific shop only.
  • One updated the price of coffee.
  • One added a new Coffee Shop.
  • One deleted a Coffee Shop.

Including built in error handling for if a Coffee Shop didn’t exist etc.  It all feels like it could be useful later in the Blog Project.  Not too useful on it’s own accord.

Day 67 – Blog Capstone Project Part 3

Back to the Blog Project, and after this round it’s a LOT more Blog Like, though incredibly insecure.  The Security Part looks to be the subject of the next couple of lessons though, so it’s all good.  We basically started with the last Blog Project from Day 59, and added the ability to add, edit, and delete posts.  Also with a Database back end.

One tricky moment here.  The idea was to use the same form page for New Post and Edit Post.  Originally I had nested these together, and had some if/else cases to check if it was Editing or Adding, and kind of got stuck on how to handle the date, since the date doesn’t get changed on updates.

But then I had a bit of an epiphany moment, the kind you get often with coding, where you band against a problem, convinced it’s the way to solve things, only to realize there is an obvious, easy method.

I split them up.  No more if/else, and no more worrying about what the DB is doing at the end.  Because two separate @app.route and related functions, can route to the same HTML template (the Edit Form).  The only thing needed was to pass a header variable along to change the title.  

It was a total “Duuuuuuh” moment.

Anyway, no security though, because there are no users or security keys and anyone can just come in and post anything and delete anything and it would be total chaos as an actual production Blog platform.

Day 68 – Authentication with Flask

Oops, I spoiled what this topic was going to be in the last project write up.  This whole section is working with Authentication using a Database for persistence.  It’s a simple website, a home page, a user creation page, a log in page, and a secrets page.  Users can create an account, and once registered, they can access the Secrets page and download a PDF.  

Part of this exercise is also restricting access for not logged in users and another part is proper security and handling passwords with hashing and salting.

Of all of the projects so far, I feel like this may be one of the most important ones, despite it’s basic simplicity.  I’ve been planning to work out the best way to share some of the projects built so far, and using Flask was a good “first step”, but being security conscious, I would rather not throw a bunch of web based Flask pages up with no restrictions on access.  Like last session, there was the Library App or the Top Ten Movies page.  Sure, I could put them up on the web-server, map some sub domain to them, but anyone can edit them, without proper, persistent security and restricted access.  

Now, I can make that happen.  Plus, with the modulator of routing and such, I can easily slip them into parts of the overall Blog Project, in time.

Day 69 – Blog Capstone Project Part 4

And here it is, the culmination of the Python based Flask Blog.  It’s neat.  I like it, well, I like the basic functionality of it.  The layout is a bit plain but that’s fixed.  I doubt it ever replaces WordPress, I love WordPress, but I could definitely see uses for this finished product.  I may actually modify it to work as a sort of “Twitter Replacement” since Twitter is currently burning down.  Using what I have learned, I could easily set this up to take a post, then “Syndicate” it out via API calls to Twitter, Facebook, Mastodon, or anywhere.  While keeping my own archive of posts.

My current, next up To Do Items:

  • Create RSS Feed Page  
  • Create Admin Page  
  • Enable/Disable Comments for Posts  
  • Allow Admin to delete Comments  
  • Allow Users to delete their own comments  
  • Add Mailer.class and add email notifications  
  • Add Pagination to Home Page  
  • Add Tags and Category Options to Posts

Also, on a side note, this actually isn’t the first time I’ve built a “Blog Platform”. I built a basic one a few years ago in HTML and PHP. Heck, the system I used int he early 2000s with SHTML Pages was sort of a “Blog Platform”.

I’d recommend, for anyone going through this course, check the notes on the Blog courses.  There are a lot of good suggestions for improvements, especially in stopping things like Java Script injection in the comments.  When you go to Angela’s (The Instructor) Blog link, it immediately throws out a JavaScript pop up that someone dropped in the comments.

Day 70 – Hosting with Heroku

So, there isn’t really a Day 70 project.  I did go through the section, and the most useful part for me was the more robust SQL solution mentioned in the last section.  The first few parts were about GIT and Github, which I am already using.  The middle part was about using Heroku, which I have heard of and used a bit before but for the long term, I don’t need to use a freemium hosting service in a jankey way.  I have a VPS, and later I will figure out how to get Apache to play nice with Flask.

For now, I’ve split the Blog Project into it’s own repository and worked up my initial planned ToDo List.  But I need to keep my focus on the course for now.

100 Days of Python, Projects 58-65 #100DaysofCode

Judging by the comments on the lessons, The lessons are getting harder, though frankly, I am finding they are a bit easier.  I feel like my experience with Web Design is a lot of this.  I also have been looking a bit into how to properly host some of these apps to share here, on my Code Projects Page.  I believe I could set them up to run on a production Flask environment, with different ports, then just map sub directories n the domain to different ports.

But I am doing my best not to get distracted.  Yes, I have been building a few smaller projects here and there lately, but I don’t want to get TOO distracted.

Day 58 – Bootstrap Overview

Not a lot of really say here, like the Web Foundational section, this was a chunk of the Web Dev course offered by the same Instructor.  I may look into picking it up if it’s on sale for cheap, since at this point I’ve done like half of it.  My next plan for a major learning push is going to be Javascript.  I really need to get familiar with Javascript for some work projects.  

The project was essentially using different Bootstrap bits to format a silly Tinder knockoff website, a site for Dogs called Tindog.  Bootstrap is definitely useful, but I generally try to avoid it because it makes things look very samey.  I suppose familiarity in design is useful at times though.

Day 59 and 59 – Blog Capstone Project Part 2

The last part of the Intermediate+ section was the bones of a Clean Blog running in Python Flask.  These two days expanded on the Blog concept a bit, and a few later lessons bring it back around again.  I am almost interested in trying to use the blog once finished, except that I already have plenty of proper outlets for Blogging.  Wordpress works fine.  

The core concept was using Bootstrap to format the blog up nicely, as well as setting it up to reuse Header and Footer files.  It’s a common method for webdesign, and it’s one I have been using since Geocities when I discovered something called SHTML which would let me write Blog Texts in HTML, then encapsulate them into a common top and bottom.

Day 61 – Flask Forms

An intro to easier to use Flask based forms and libraries.  The project itself was to construct a simple log in page, which lead to a Rick Roll when unlocked.  I could see this piece being useful later to add in on the more complete Blog project.

Which brings to a bit of an aside topic.  A lot fo these projects feel disconnected, but really, they are demonstrating a proper way of handling larger Code Projects.

Break it into parts.

We built a simple blog page that pulled posts from a CSV file.

We formatted that blog page.

We created a log in form, that could easily be slipped into a blog page.

Later lessons work with more dynamic forms and data and SQL Lite databases.

As the pieces get laid out, the end goal becomes more and more clear.  My prediction is that the later Blog Capstone project will amount to, “Take everything from the last ten weapons and mash it into a functioning Blog app.

Day 62 – Coffee Shop Wifi Tracker

Like I said, building on the last.  For this lesson, we built a little table that would list Coffee Shops in  pretty Bootstrap table with ratings for Coffee Quality, Wifi Quality, and Power Outlet availability.  Instead of just a form that takes an input and verifies it’s good, it actually inserts new data into the CSV that holds all the coffee shops.

Day 63 – Book Collection App

I may revisit this one later to build an app for my other collections.  And to make it more robust.  The core though is once again, building on the Coffee App, essentially.  Instead of just a tracked list with an Add Form, we added persistence by learning about SQL Lite databases.  So the data persists even if the server is shut down.  

I took a few extra non required steps on this app and reused the Coffee Shop code to make this app look much nicer.  The core app and instructions just had a very basic page with an unordered list.  I turned it into a table with a bit more formatting and color.

Day 64 – Top Ten Movies App

This whole project was essentially the same as the Book Collection App, except it was intnded from the start to look prettier.  The main difference is that the add process had a few extra steps to pull from the API of The Movie DataBase website to get data about each film, and have the user select from a list of results.  In the instructions for the class, the idea was to make multiple API calls for the list, then specific movie data, but I had already worked ahead on the project and instead it pulls the data, the user selects the movie, then it’s just done.  It was a bit complicated to get the data to pass around between pages since it was a dictionary and not just a single variable, but I got it working using Global variables.

I mostly try to avoid Global variables, but it felt like this was a good use case and made sense to use given the way Flask works.

I am thinking I may integrate the log in app created previously with this website and use it to test out mapping some of these projects to a domain to make it a for real, live website. Also, you can’t tell it in the snapshot but the posters do this cool flip over effect when you mouse over them to show more information. Not really a Python thing, but it’s still a neat effect.

Day 65 – Web Design Principles

No project for this day, it’s another slice of the Web Developer course from the same instructor.  I’m definitely going to keep an eye out for a deep discount, especially with Black Friday coming, since I’ve basically completed half of that course now.

I was hoping to fit all of this more advanced Flask based projects into one page but it feels like this post is getting pretty long so I’m going to go ahead and break it up here.