[Blogging Intensifies]

Technology, Coding, Music, Life...

  • About
  • Code Projects Portfolio
  • Friends
  • Photo Gallery

100DaysOfCode

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

December 12, 2022
Python Logo

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
Posted in: 100DaysOfCode Tagged: #100DaysOfCode, Coding, Python

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

December 2, 2022
Python Logo

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.

Posted in: 100DaysOfCode Tagged: 100DaysOfCode, Blogging, Coding, Python

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

November 27, 2022
Python Logo

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

Posted in: 100DaysOfCode Tagged: #100DaysOfCode, Coding, Morse Code, Python
1 2 … 5 Next »

Categories

  • collapsCat options: Array ( [title] => Categories [showPostCount] => 1 [inExclude] => exclude [inExcludeCats] => Photos, Uncategorized, mastodon-feed, goodreads [showPosts] => 0 [showPages] => 0 [linkToCat] => 1 [olderThan] => 0 [excludeAll] => 0 [catSortOrder] => ASC [catSort] => catName [postSortOrder] => ASC [postSort] => postTitle [expand] => 0 [defaultExpand] => Technology, Maker, Coding, Hobbies [debug] => 1 [postTitleLength] => 0 [catfeed] => none [taxonomy] => category [post_type] => post [postDateAppend] => after [postDateFormat] => m/d [showPostDate] => 1 [useCookies] => 1 [postsBeforeCats] => 1 [expandCatPost] => 1 [showEmptyCat] => 1 [showTopLevel] => 1 [useAjax] => 0 [customExpand] => [customCollapse] => [style] => kubrick [accordion] => 1 [title_link] => [addMisc] => 1 [addMiscTitle] => [number] => 2 [includeCatArray] => Array ( ) [expandSym] => ► [collapseSym] => ▼ ) postsToExclude: Array ( ) CATEGORY QUERY RESULTS Array ( [0] => WP_Term Object ( [term_id] => 641 [name] => 100DaysOfCode [slug] => 100daysofcode [term_group] => 0 [term_taxonomy_id] => 641 [taxonomy] => category [description] => [parent] => 172 [count] => 14 [filter] => raw ) [1] => WP_Term Object ( [term_id] => 486 [name] => Advent of Code [slug] => advent-of-code [term_group] => 0 [term_taxonomy_id] => 486 [taxonomy] => category [description] => [parent] => 172 [count] => 27 [filter] => raw ) [2] => WP_Term Object ( [term_id] => 666 [name] => AI Art [slug] => ai-art [term_group] => 0 [term_taxonomy_id] => 666 [taxonomy] => category [description] => [parent] => 153 [count] => 5 [filter] => raw ) [3] => WP_Term Object ( [term_id] => 438 [name] => Books [slug] => books [term_group] => 0 [term_taxonomy_id] => 438 [taxonomy] => category [description] => [parent] => 436 [count] => 4 [filter] => raw ) [4] => WP_Term Object ( [term_id] => 172 [name] => Coding [slug] => programming [term_group] => 0 [term_taxonomy_id] => 172 [taxonomy] => category [description] => [parent] => 153 [count] => 12 [filter] => raw ) [5] => WP_Term Object ( [term_id] => 541 [name] => Concerts [slug] => concertphotos [term_group] => 0 [term_taxonomy_id] => 541 [taxonomy] => category [description] => [parent] => 527 [count] => 7 [filter] => raw ) [6] => WP_Term Object ( [term_id] => 155 [name] => Devices (Phones and Tablets) [slug] => devices [term_group] => 0 [term_taxonomy_id] => 155 [taxonomy] => category [description] => [parent] => 166 [count] => 9 [filter] => raw ) [7] => WP_Term Object ( [term_id] => 606 [name] => Fairs [slug] => fairs [term_group] => 0 [term_taxonomy_id] => 606 [taxonomy] => category [description] => [parent] => 527 [count] => 8 [filter] => raw ) [8] => WP_Term Object ( [term_id] => 523 [name] => Feeds [slug] => feeds [term_group] => 0 [term_taxonomy_id] => 523 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [9] => WP_Term Object ( [term_id] => 722 [name] => Food [slug] => food [term_group] => 0 [term_taxonomy_id] => 722 [taxonomy] => category [description] => [parent] => 530 [count] => 33 [filter] => raw ) [11] => WP_Term Object ( [term_id] => 436 [name] => Hobbies [slug] => hobbies [term_group] => 0 [term_taxonomy_id] => 436 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [12] => WP_Term Object ( [term_id] => 656 [name] => IOT Projects [slug] => iot [term_group] => 0 [term_taxonomy_id] => 656 [taxonomy] => category [description] => [parent] => 153 [count] => 19 [filter] => raw ) [13] => WP_Term Object ( [term_id] => 446 [name] => Language [slug] => language [term_group] => 0 [term_taxonomy_id] => 446 [taxonomy] => category [description] => [parent] => 436 [count] => 1 [filter] => raw ) [14] => WP_Term Object ( [term_id] => 524 [name] => Letterboxed [slug] => letterboxed [term_group] => 0 [term_taxonomy_id] => 524 [taxonomy] => category [description] => [parent] => 523 [count] => 288 [filter] => raw ) [15] => WP_Term Object ( [term_id] => 653 [name] => Link List [slug] => link-list [term_group] => 0 [term_taxonomy_id] => 653 [taxonomy] => category [description] => [parent] => 523 [count] => 72 [filter] => raw ) [16] => WP_Term Object ( [term_id] => 224 [name] => Linux & Open Source [slug] => linux [term_group] => 0 [term_taxonomy_id] => 224 [taxonomy] => category [description] => [parent] => 166 [count] => 6 [filter] => raw ) [17] => WP_Term Object ( [term_id] => 153 [name] => Maker [slug] => maker [term_group] => 0 [term_taxonomy_id] => 153 [taxonomy] => category [description] => [parent] => 0 [count] => 2 [filter] => raw ) [19] => WP_Term Object ( [term_id] => 530 [name] => Micro Blog [slug] => microblog [term_group] => 0 [term_taxonomy_id] => 530 [taxonomy] => category [description] => [parent] => 0 [count] => 55 [filter] => raw ) [20] => WP_Term Object ( [term_id] => 437 [name] => Music [slug] => music [term_group] => 0 [term_taxonomy_id] => 437 [taxonomy] => category [description] => [parent] => 436 [count] => 18 [filter] => raw ) [21] => WP_Term Object ( [term_id] => 395 [name] => My DIY Projects [slug] => my-diy-projects [term_group] => 0 [term_taxonomy_id] => 395 [taxonomy] => category [description] => [parent] => 153 [count] => 7 [filter] => raw ) [22] => WP_Term Object ( [term_id] => 154 [name] => Opinion/Editorial/Life [slug] => articles [term_group] => 0 [term_taxonomy_id] => 154 [taxonomy] => category [description] => [parent] => 0 [count] => 19 [filter] => raw ) [23] => WP_Term Object ( [term_id] => 491 [name] => Organizing [slug] => organizing [term_group] => 0 [term_taxonomy_id] => 491 [taxonomy] => category [description] => [parent] => 436 [count] => 7 [filter] => raw ) [24] => WP_Term Object ( [term_id] => 534 [name] => Other Photos [slug] => otherphotos [term_group] => 0 [term_taxonomy_id] => 534 [taxonomy] => category [description] => [parent] => 527 [count] => 12 [filter] => raw ) [25] => WP_Term Object ( [term_id] => 617 [name] => Outdoor and Nature [slug] => outdoor [term_group] => 0 [term_taxonomy_id] => 617 [taxonomy] => category [description] => [parent] => 527 [count] => 4 [filter] => raw ) [26] => WP_Term Object ( [term_id] => 242 [name] => PC Hardware [slug] => pcs [term_group] => 0 [term_taxonomy_id] => 242 [taxonomy] => category [description] => [parent] => 166 [count] => 6 [filter] => raw ) [28] => WP_Term Object ( [term_id] => 712 [name] => Programming Projects [slug] => projects [term_group] => 0 [term_taxonomy_id] => 712 [taxonomy] => category [description] => [parent] => 172 [count] => 11 [filter] => raw ) [29] => WP_Term Object ( [term_id] => 241 [name] => Synology NAS [slug] => synology-nas [term_group] => 0 [term_taxonomy_id] => 241 [taxonomy] => category [description] => [parent] => 166 [count] => 5 [filter] => raw ) [30] => WP_Term Object ( [term_id] => 166 [name] => Technology [slug] => technology [term_group] => 0 [term_taxonomy_id] => 166 [taxonomy] => category [description] => [parent] => 0 [count] => 10 [filter] => raw ) [31] => WP_Term Object ( [term_id] => 424 [name] => The Basement [slug] => the-basement [term_group] => 0 [term_taxonomy_id] => 424 [taxonomy] => category [description] => [parent] => 153 [count] => 6 [filter] => raw ) [32] => WP_Term Object ( [term_id] => 557 [name] => Toy Photos [slug] => toyphotos [term_group] => 0 [term_taxonomy_id] => 557 [taxonomy] => category [description] => [parent] => 527 [count] => 0 [filter] => raw ) [33] => WP_Term Object ( [term_id] => 1 [name] => Uncategorized [slug] => uncategorized [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [34] => WP_Term Object ( [term_id] => 280 [name] => Windows [slug] => windows [term_group] => 0 [term_taxonomy_id] => 280 [taxonomy] => category [description] => [parent] => 166 [count] => 2 [filter] => raw ) [35] => WP_Term Object ( [term_id] => 538 [name] => Zoos [slug] => zoophotos [term_group] => 0 [term_taxonomy_id] => 538 [taxonomy] => category [description] => [parent] => 527 [count] => 12 [filter] => raw ) ) POST QUERY: POST QUERY RESULTS
  • ►Feeds (360)
    • Letterboxed (288)
    • Link List (72)
  • ▼Hobbies (30)
    • Books (4)
    • Language (1)
    • Music (18)
    • Organizing (7)
  • ▼Maker (103)
    • AI Art (5)
    • ▼Coding (64)
      • 100DaysOfCode (14)
      • Advent of Code (27)
      • Programming Projects (11)
    • IOT Projects (19)
    • My DIY Projects (7)
    • The Basement (6)
  • ►Micro Blog (88)
    • Food (33)
  • ►Opinion/Editorial/Life (19)
  • ▼Technology (38)
    • Devices (Phones and Tablets) (9)
    • Linux & Open Source (6)
    • PC Hardware (6)
    • Synology NAS (5)
    • Windows (2)
  • ►Uncategorized (0)

MastodonLinkedIn

emailInstagramInstagram

GitHubLetterboxdDuolongo
GoodreadsLast.fmElite Dangerous INARA
Lameazoid Logo


Copyright © 2023 [Blogging Intensifies].

Me WordPress Theme by themehall.com