100DaysOfCode

100 Days of Python, Project 083 – Tic-Tac-Toe #100DaysofCode

Writing this write up took considerably longer than actually writing the code.

I saw a post once commenting how the world doesn’t need another version of Tic-Tac-Toe made in Python. Well that’s too bad, because here comes another one! I keep telling myself I want to make a slightly more detailed run through some of my code writing, just for the sake of “This is the process,” so let’s see if I can make a go of it for this round. Funny enough, this isn’t the first time I’ve set out to make TicTacToe on my own accord, I once started working out doing it in C++ once, mostly because I wanted to see if I could build a super tough algorithm for a computer AI playing TicTacToe.

This whole post will contain a whole lot of somewhat repetitious and incremental Python code, but maybe it will be helpful to some newbie coders to get a better idea of how to step through what you’re writing and how to break up and plan different parts of your code.

The first step here, is to decide which method to use to create this little game. I could do it with Text but that feels ugly and boring. I could probably make a web version with Flask, but that feels overly complicated. That leaves Tkinter or Turtle. I opted for Turtle. I like Turtle, it has a fun sort of retro feel to it, and it’s simple enough to use.

This post is Extremely Long and full of code, so I’m putting ti behind a Read More, and here it is!

—– Read More —–: 100 Days of Python, Project 083 – Tic-Tac-Toe #100DaysofCode Read More

100 Days of Python, Project 082 – Personal Portfolio Website #100DaysofCode

This felt like an odd one, but probably still a good one to be included.  I also was kind of torn on if I even needed/wanted to actually “do” it.  This part is effectively already “Done”.  We created a GitHub.io site.  We created a Heroku based Blog (which I skipped in favor of doing it on my own server/host).  I also basically already have a portfolio website.  Right here, on this blog.  I post my projects, I write about them, etc etc.  I also have a pretty nice GitHub page, which is the best “programming portfolio” one can have.

So I was going to skip it initially, but instead, I opted to go with getting Flask to work on my web server, which was more of a hassle than I expected, but now, over time, I can start modifying that site to show case some of the things I’ve built.  Currently, it resides at https://joshmiller.net .  It’s nothing fancy as of this writing, just the bare bones of the Flask Blog built previously.  I did strip out the registration portion.  I really don’t need or want to deal with comments.  Sorry, @me on Mastodon instead.  I don’t really need a blog either, so don’t expect much going on there, but I want to keep it for now because I may use it to basically, blog about the blog.  I also want to build some new features over time to make it more full featured.  For starters, it needs a settings/admin page, to set things like the title and toggle comments on/off.  It also desperately needs an upload portal for images for use in blogs.

So what’s the point.  Well, for one, it has built in Authentication.  Which means I can build pages and tools that only I will ever have access to.  One I have been thinking of building would basically be a blog post form, that simultaneously posts to several social networks, using APIs.  Also, related to authenticating, the functional nature of these Flask Apps, means I can easily drop in say, the Top Ten Movies website, and have it show up at joshmiller.net/movies or something, just by adjusting some of the code.  And with the built in authentication, I can lock the world out from being able to edit my movies list.

I also, have been really looking for SOME use for that domain.  Though, I still may reconsider and move it all to a sub domain on this site.  I have not decided.  I already stopped using that domain for email and it essentially just… exists… now.

Anyway, I think it’s worth talking about how I got things going, because I had a hell of a time getting Apache to work nicely with Flask and WSGI.  And I looked into a lot of guides…. a LOT.

Honestly it’s been so many I will probably miss something here.  This is going to be a bit general and not nearly as hand holdey as it could be.  Chances are, if you can’t figure this out, you probably shouldn’t be doing it.  And chances are, you already have a web server with Apache going, so you kind of know what you’re doing.  The first step is to make sure Python is installed, and Python3, and the various needed imports, using pip3, not pip.

sudo apt-get install python3

sudo apt-get install python3-pip

If you need them.

Download your flask app to a folder /var/www/FlaskApp .  It should just be in the base here, but it can be elsewhere if you know what you’re doing.  Then run the main.py file.

python3 main.py

Fix any permissions issues, or, for now try sudo python3 main.py.  For everything it can’t find each run, do

sudp pip3 install <Package>

Eventually the main.py should run, and you can access your site at <ServerIP:5000>.  The truck now is getting Apache to work with it.  First you’ll want to install mod-wsgi then enable it.

sudo apt-get install libapache2-mod-wsgi python-dev

sudo a2enmod wsgi

Now, in the folder with main.py, the /var/www/FlaskApp folder, create a file called flaskapp.wsgi.

sudo nano flaskapp.wsgi

Inside this file, add the following.

#!/usr/bin/python

import sys

import logging

logging.basicConfig(stream=sys.stderr)

sys.path.insert(0,"/var/www/FlaskApp/")

from main import app as application

application.secret_key = 'Your Secret Key, it may Need to be the same as the one in main.py I am not sure'

application.root_path = '/var/www/FlaskApp'

Save that file.

Next create the site file for Apache to use.

sudo pico /etc/apache2/sites-available/YOUR_SiTE_FILE_NAME_CAN_BE_WHATEVER_MAKE_IT_MEANINGFUL.conf

Inside that file, add the following.  Please note, this file also contains settings that allow for SSL via Let’s Encrypt, which you should be running on your web server anyway.

<IfModule mod_ssl.c>

SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)

<VirtualHost *:443>

        ServerAdmin YOUR EMAIL HERE

        ServerName YOUR SITE DOMAIN OR SUBDOMAIN.DOMAIN HERE

        ServerAlias www.YOUR SITE DOMAIN HERE SO WWW REDIRECTS REMOVE FOR SUB DOMAINS

        WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi

        <Directory /var/www/FlaskApp/FlaskApp/>

          Order allow,deny

          Allow from all

        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        CustomLog ${APACHE_LOG_DIR}/access.log combined

# If you don't have Lets Encrypt you can probably dump this next section through <VirtualHost> Keep that

        Include /etc/letsencrypt/options-ssl-apache.conf

        Header always set Strict-Transport-Security "max-age=31536000"

        SSLUseStapling on

        Header always set Content-Security-Policy upgrade-insecure-requests

        # Lets Encrypt Folders below

        SSLCertificateFile /etc/letsencrypt/live/YOURSITEDOMAIN.com/fullchain.pem

        SSLCertificateKeyFile /etc/letsencrypt/live/YOURSITEDOMAIN.com/privkey.pem

</VirtualHost>

</IfModule>

Now, the most important thing to note here.  In every single guide I found, it included a section to alias the “Static” directory.  I got to a point where my site was working, but it was not loading CSS.  I fixed this by REMOVING this alias.  

I beleive that having “application.root_path = ‘/var/www/FlaskApp'” inside the wsgi file, confliced with the Alias.  But the application wasn’t running at all without “application.root_path = ‘/var/www/FlaskApp'”.  The end result, was a working blog site.  I could create a user, create new posts, leave comments, and everything looked as it should.  It’s working now though, at least, which was the goal.  I can polish it up over time.  

Like I mentioned previously, I am not sure what or how much I will post there.  I may actually completely remove the blog part of it.  I’m more interested in the admin/login functionality really.  I already have plenty of blogging outlets between this site and Mastodon, and using Tumblr more and probably a fresh new push at Lameazoid.com as well.  I don’t need this additional random blog at all.

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.