Back to Basics after My Code Chokes from Bloat

Do you remember these ?

Humility.
Simplicity (KISS)
Frugality. 

They are some of the principles we are taught to keep in mind as we add more and more code to our projects.

Alas. I strayed off this trail-marked path.

And now my initial Xpander project modules are crashing during compilation due to a host of never ending syntax errors.


So it's time to start all over with a clean slate.
Back to Basics.
Return to Roots.
A New Beginning.

But wait.
I hear that "UV" is the superior way to launch a new project.
How do you install and use UV as a beginner?  ( <-- YouTube search results)
    ^^^-- Astral install site here
    ^^^-- TwT's answer here
    ^^^-- 
Corey Schafer's answer here
    ^^^-- Josh's answer here
    ^^^-- Indently search results ::: UV here (includes TOML)
    ^^^-- How to Use UV in PyCharm
Shaw ::: The Fastest Way to Install (and Use) Python
Indently's advanced UV commands here


LATER IN TIME ......

Eureka, it worked !!
The download and install of UV from the Astral site (above) worked.
The latest version of PyCharm already had a UV option for creating a new (a starting afresh) Project. It worked flawlessly.
Have no fear.
After just a few syntax error fixes my Simple_Typer function started working right away.



I suddenly felt good about my coding ability rather than hopelessly lost in the clutter of my over-bloated previous project folder.

 

Below is a draft (not a final) version of my "Simple Typer" code
As can be seen, it's not exactly "simple".
More explanation will follow below the shown code.




# Date 09/24/2026  funcs_06 module  == Xpander-1 restart


import keyboard # https://www.google.com/search?client=opera&q=python+keyboard+module+documentation&sourceid=opera&ie=UTF-8&oe=UTF-8

def get_keystroke():
key_read = ''
keyrd = keyboard.read_event(suppress = True)
keyrd = keyboard.read_event(suppress=True)
key_read = key_read.join(keyrd.name)
return key_read

def simple_typer(xit_trigs: list, pretext: list):
stop = False; indx = -1; spacex = -1; wasc = [0, 0, 0, 0] # prefixes = Window, Alt, Shift, Ctrl
convert = {'space': " ", 'enter': f"\n"}
while stop == False:
indx += 1 # start counting new chars to append to pretext
hit = str(get_keystroke())
if hit.lower() == '`':
stop = True; mssg = "P3 was detected"
return mssg
elif hit.lower() == 'space':
hit = " "; pretext.append(hit); print(hit, end = '') ; spacex = indx
elif hit.lower() == 'enter':
hit = f"\n"; pretext.append(hit); print(hit, end = '')
elif hit.lower() == 'tab':
hit = f"\t"; pretext.append(hit); print(hit, end = '')
elif hit.lower() == 'shift':
wasc[2] += 1; indx -= 1
else:
pretext.append(hit); print(hit, end = '')
continue
return pretext, indx, spacex, wasc
print(f'\n\nBegin simple typer. Hit Enter to quit\n\n')
mssg = simple_typer(['`', '~'], [])
print(f'\n\nEnd of simple typer. mssg = {mssg}\n\n')
To the right of the first code line, where it imports the 'keyboard' module, there is a link to the documentation for that module. The keyrd.name attribute supplies a spelled out name for many of the hit keys. 

Pressing the left shift key during run time generates a text-like string, "shift". Pressing the space bar generates a text-like string, "space", and so on. These have to be converted into corresponding actions and/or single character codes.


Wait. It just hit me.

The Shift, Alt and Ctrl keys are actually prefix triggers. You hit them before you hit one of the alphanumeric keys (a-z, 0-9). The wasc list keeps count o how many times in sequence one of the respective keys, Window, Alt, Shift and Control was hit. For example, if wasc= [0, 0, 1, 0] it means Shift was hit once and the next char to be printed should be dot=upper(). Then we reset that count to zero. On the other hand,  if wasc= [0, 0, 2, 0] it means Shift was hit twice and that invokes cap lock. We don't reset to zero until we get another hit of the Shift key.

Also another aha that hit me is that I can use the match case conditionals by calling an appropriate function for each case, while a dictionary generates the case number!


Functions are just another way of hiding the lower level codes, as are modules, packages and classes. D'oh. Why was I so blind to that obvious theme?


TO BE CONTINUED ... 


situation = {'key_name1': case_number_1, 'key_name2': case_number_2, ... }
match situation[hit_key_name]:
        case 1: function_1()
 
       case 2: function_2()
        case 3: function_3()
        ...
        case _: catch_all_function_()



MORE TO EXPLORE

QAB-g ::: Use UV to set up your Python environment
QAB-y ::: Use UV to set up your Python environment
QAB-r ::: How to use UV to set up your Python environment

Dave Ebbelaar -- Why Python Developers Are Switching to UV
Real Python -- Advanced UV versus PiP

Timnology -- UV - A modern python project and dependency manager








Comments

Popular posts from this blog

Links for Python Noobs

The Learn HOW to Learn Page

My 1st and 2nd OOP class definitions (aka What is an OOP class definition?)