Skip to content

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
FocuseObie committed Oct 6, 2024
1 parent b6af893 commit 2106fca
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 68 deletions.
37 changes: 0 additions & 37 deletions Blog.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,5 @@ <h1>Learning in Progress</h1>
<div class="row">
<div class="leftcolumn" id="blog">

<div class="card">
<h2>Machine Learning, Game Theory, and Python</h2>
<h5>Oct 05, 2024</h5>
<p>This is the first installment of a blog thing I'll be trying to keep alive. The mission of the blog is essentially to act like an open-source guide (diary?) to the things that I learn about.

More fundamentally, I've wanted to keep a log or decision guide of some sort for a while now, just to allow me to look back in order to gauge growth/trajectory, and to look at the flow of my decisions and make improvements if need be. But I struggled with implementation, and social/peer pressure is always a great motivator (appealing to our base instincts as social animals) so I'm hoping this format will help me in this case.

Enough about me though, in order to provide a benefit to potential readers (hi) as well as to myself, I'll try to explain the things I've learned in my own voice and at various levels of difficulty/comprehension.

So, to kick things off:
Machine Learning (basics)
I've always found the human thought process (particularly in a stages/levels of thought type model and thinking about how suboptimal decisions are made) very interesting. So, it is quite rational that I pursue the intersection of my personal interest in human decision making with my interest in quantitative fields, and, albeit lesser, interest in coding.
1. What is Machine Learning?
Simply Machine Learning is a subset of Artificial Intelligence which deals with primarily two things: 1) classifying things (classification) and 2) making quantitative predictions (also called regression).
2. Why Machine Learning?
Similar to models in Economics, Machine Learning models help people approximate reality by exploring relationships (variables/features) and using these models to make predictions or give general insight into the underlying relationships.
That's as far as I'll go for general Machine Learning, however I'll cover more in-depth topics later, but if that seems at all interesting and you don't know where to start, there is a great book on the basics of Machine Learning by StatQuest which was recommended to me.
Game Theory
Following the general trend of exploring the intersection of thought processes and other fields, I've been learning more about game theory and general strategy (player vs. player) from a book titled "The Art of Strategy: A Game Theorists Guide..." by Dixit. In Microeconomics class I learned about Nash Equilibriums and business strategies, this book goes into more detail on those topics, and a lot more.
Just a quick intro for this blog however:
1. What is Strategic Behavior
Essentially, whenever you make a decision that has to account for the action of another person or for variables that change based on your decision, you're behaving (somewhat) strategically.
2. Why Strategic Behavior
One of the many examples throughout the book deals with the common scenario of someone trying to lose weight. Just about everyone knows the essentials of what they would need to do in order to lose weight; eat less sugar, drink only water, and move your legs around. Yet every year so many people fail to do just that, and the problem is that you are losing a strategic game... against yourself! The issue is that your current self wants to lose weight, but you in 30 minutes wants to do other things which would give you much more dopamine. Strategic behavior will help with this problem. A similar instance would also be wanting to keep a log or decision tracker, but failing because one's future self doesn't want to work 30 minutes of writing into their bed-time schedule... (see potential solution above :) )
Python (for Data Science => Machine Learning)
I've been programming for a while but, like most things that I do, I'm application focused to a fault. If I had to say why that is the case, it's because with my strangely wired brain I find the cost (including time) of learning something to be less than my cost of concretely remembering something. So, whenever I have some task or application of programming I want to do, I research the things that I'd need to know for the project, implement it, and move on. Now that I'm entering the stage of my life where I'm supposed to be specializing in certain topics this strategy is no good, and I need to start working towards more in depth knowledge on such things. In the realm of coding, this is pretty much going to be applied to allowing me to mess with data and build machine learning models. As such I'm currently reading a text book on Python for Data Science, and when I'm done with that I have another two lined up catered towards Machine Learning. As much as I dislike it, I'm already derriving utility from the textbook as I'm becoming quite familiar with python's newer syntax/structures. Coming from the Java's and C++, my python probably looked abhorrent, learing now about list/dictionary comprehensions, lambda functions, generators, and so on, my code is definitely a lot more concise. Not to mention I can now actually understand other peoples python code. Just writing a quick script to automate posting this blog on my website I utilized a list comprehension (which I'd just learned about earlier today), so the fact that it is already applicable is a great motivator. I won't go over why programming is awesome here, people can easily figure that out for themselves.

Recap:
Machine Learning is great, expect me to talk about further topics in the space
Game Theory is great, probably don't expect much from me on the topic however
Python is great, if I have something to share I'll do so, and you can always look at my github for projects (it's a barren wasteland at the moment however, but that should be changed once I get through these texts)


</p>
</div>

<!-- Blog posts will be appended here -->
</div>
</div>
80 changes: 49 additions & 31 deletions BlogUpdate.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,71 @@
import os
from datetime import datetime
import subprocess
import docx

blog_file = "Blog.html"
txt_folder = "./txt_files"
docx_folder = "./docx_files" # Updated folder to hold the .docx files

def extract_text_from_docx(file_path):
"""Extract text from .docx file and preserve basic formatting."""
doc = docx.Document(file_path)
full_text = []

for para in doc.paragraphs:
# Preserve bold and italic formatting
if para.style.name.startswith('Heading'):
full_text.append(f"<h2>{para.text}</h2>")
elif para.runs and para.runs[0].bold:
full_text.append(f"<b>{para.text}</b>")
elif para.runs and para.runs[0].italic:
full_text.append(f"<i>{para.text}</i>")
else:
full_text.append(f"<p>{para.text}</p>")

return "\n".join(full_text)

def create_blog_post(file_path):
"""Create an HTML blog post from a .txt file."""
with open(file_path, 'r') as f:
content = f.read()

title = os.path.basename(file_path).replace('.txt', '')
"""Create an HTML blog post from a .docx file."""
content = extract_text_from_docx(file_path)
title = os.path.basename(file_path).replace('.docx', '')
current_date = datetime.now().strftime('%b %d, %Y')

new_post = f"""
<div class="card">
<h2>{title}</h2>
<h5>{current_date}</h5>
<p>{content}</p>
{content}
</div>
"""
return new_post

def get_most_recent_txt_file(folder):
"""Find the most recent .txt file in the folder."""
txt_files = [os.path.join(folder, f) for f in os.listdir(folder) if f.endswith('.txt')]
def get_most_recent_docx_file(folder):
"""Find the most recent .docx file in the folder."""
docx_files = [os.path.join(folder, f) for f in os.listdir(folder) if f.endswith('.docx')]

if not txt_files:
if not docx_files:
return None

# Get the most recent .txt file based on last modification time
most_recent_file = max(txt_files, key=os.path.getmtime)
# Get the most recent .docx file based on last modification time
most_recent_file = max(docx_files, key=os.path.getmtime)
return most_recent_file

def update_blog():
"""Update the blog HTML file with the most recent .txt post."""
most_recent_file = get_most_recent_txt_file(txt_folder)
"""Update the blog HTML file with the most recent .docx post."""
most_recent_file = get_most_recent_docx_file(docx_folder)

if most_recent_file is None:
print("No .txt files found in the folder.")
print("No .docx files found in the folder.")
return False

# Read the existing blog HTML file
with open(blog_file, 'r') as f:
blog_html = f.read()
print("Blog file read successfully.")

# Create a new blog post from the most recent .txt file
# Create a new blog post from the most recent .docx file
new_post = create_blog_post(most_recent_file)

# Check if the content is already in the blog to avoid duplicate entries
if new_post in blog_html:
print("The blog is already up to date with this post.")
return False

# Insert the new post into the blog HTML file at the correct placeholder
blog_html = blog_html.replace('<div class="leftcolumn" id="blog">', f'<div class="leftcolumn" id="blog">\n{new_post}', 1)

Expand All @@ -64,19 +76,25 @@ def update_blog():

return True

if __name__ == '__main__':
update_blog()

def git_commit_and_push():
"""Commit and push changes to GitHub."""
try:
subprocess.run(['git', 'add', 'Blog.html'], check=True)
subprocess.run(['git', 'commit', '-m', 'Update blog with new posts'], check=True)
subprocess.run(['git', 'push'], check=True)
print("Changes committed and pushed to GitHub.")
# Check if there are changes to commit
result = subprocess.run(['git', 'status', '--porcelain'], capture_output=True, text=True)

# If there are changes, stage and commit them
if result.stdout.strip():
subprocess.run(['git', 'add', 'Blog.html'], check=True)
subprocess.run(['git', 'commit', '-m', 'Update blog with new posts'], check=True)
subprocess.run(['git', 'push'], check=True)
print("Changes committed and pushed to GitHub.")
else:
print("No changes to commit.")
except subprocess.CalledProcessError as e:
print(f"Error during git operation: {e}")

if __name__ == '__main__':
update_blog() # First update the blog
git_commit_and_push() # Then commit and push to GitHub
# First update the blog
if update_blog():
# Then commit and push to GitHub if the blog was updated
git_commit_and_push()
Binary file not shown.

0 comments on commit 2106fca

Please sign in to comment.