288 lines
26 KiB
Plaintext
288 lines
26 KiB
Plaintext
|
|
Episode: 1824
|
||
|
|
Title: HPR1824: I'm Learning Some Python
|
||
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr1824/hpr1824.mp3
|
||
|
|
Transcribed: 2025-10-18 09:45:29
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
This is HPR episode 1,824 entitled, I'm learning some Python, it is hosted by John Culp and
|
||
|
|
is about 32 minutes long, the summary is, I discuss how I use Python and some of the
|
||
|
|
cool modules and libraries that I've found.
|
||
|
|
This episode of HPR is brought to you by an honesthost.com, get 15% discount on all shared
|
||
|
|
hosting with the offer code HPR15, that's HPR15, better web hosting that's honest and
|
||
|
|
fair at an honesthost.com.
|
||
|
|
Hey everybody, this is John Culp and Lafayette Louisiana and today I'm going to talk about
|
||
|
|
something that's been occupying quite a lot of my time lately and that is Python.
|
||
|
|
I'm learning some Python and I'm not really learning it in any systematic kind of way,
|
||
|
|
that's kind of not really how I do things but what I'm doing is learning how to use it
|
||
|
|
in a way that directly benefits me every day and if you've listened to my previous episodes
|
||
|
|
you'll know that I use voice commands quite a lot and so one of the things I'm doing
|
||
|
|
is converting a whole lot of the voice command actions that I used to do in Bash over to
|
||
|
|
Python if it's appropriate.
|
||
|
|
I have actually started an online course in Python but I find that I will watch a lesson
|
||
|
|
and then do the exercises and then what I'll do is think of a way that it applies more
|
||
|
|
specifically to me and so I'll go work on my own project and this is how I've always learned
|
||
|
|
stuff like this. When I was learning Bash it didn't really benefit me very much to learn
|
||
|
|
abstract concepts. It was much more helpful if I had an immediate need and I could learn
|
||
|
|
how to do something that would address that need directly rather than learning abstract
|
||
|
|
concepts.
|
||
|
|
The biggest project that I've done in Python so far was to convert the build scripts for
|
||
|
|
the School of Music websites from Bash over to Python. There was an episode I did maybe
|
||
|
|
a year ago or two years ago I don't remember really when it was but I talked about overhauling
|
||
|
|
the School of Music website and part of that process was not only cleaning up all the
|
||
|
|
content and everything but also coming up other way to have I essentially built my own
|
||
|
|
content management system using Bash where the main content of every page on the site and again
|
||
|
|
I was faced with a situation where I did not have a content management system all I had was
|
||
|
|
FTP access to a static web server so I could just dump files there and so everything on the website
|
||
|
|
is done in static HTML and I came up with a system whereby I could keep the main contents or the
|
||
|
|
contents of the main part of each page as a separate file and then build each page around it
|
||
|
|
using elements of a template so there would be a top navigation bar the main header with the
|
||
|
|
University logo a left navigation bar a footer and a tagline all of these things would be kept
|
||
|
|
separate from the content of the page and so I would if I ever had to make any edits to a page
|
||
|
|
I would edit the contents file and then build the page around it and this worked very well
|
||
|
|
in Bash but what I discovered was that if I wanted anyone else to do any kind of work on the website
|
||
|
|
then my life was difficult because other people did not have the same computer setup that I have
|
||
|
|
or the same skills so what I wanted was a way for me to set up a build environment for particularly
|
||
|
|
one colleague who I trust has decent computer skills and it was not scared of going to a command line
|
||
|
|
and running a command to build a web page I wanted to be able to do this in a cross-platform way
|
||
|
|
and so I converted my build script over to Python very happy to say that I've tested the build script
|
||
|
|
on my Linux machine on my little Mac over there on my desk and also on my son's Windows 8 laptop
|
||
|
|
and it works great on all three so I now have some I don't know prevention for the
|
||
|
|
hopefully unlikely event that I'm hit by a bus and there's nobody left who can
|
||
|
|
build the school music website and maintain it so anyway that was actually a fairly major
|
||
|
|
project let me see if I can find it here so I did a couple of
|
||
|
|
saying where is it I don't hang on I don't have the right script what I did was I have a separate
|
||
|
|
Python file where all of the the real business of the build is done and then
|
||
|
|
I'm sorry to where the heck is it make it means okay is that okay yeah here so I build a like a
|
||
|
|
function I suppose that will take care of all the sanity checks and assemble the template around
|
||
|
|
the contents page and that's all as a function in one file and then I call that from another file
|
||
|
|
that just has a couple of lines and it that says to go use the function in here so I essentially
|
||
|
|
built my own library I guess I says import this file and then run that command on it
|
||
|
|
and I do such things as what do I do some of the path manipulations this is one of the hardest
|
||
|
|
things for me to get my head around was how to deal with paths in Python you have to do it I mean
|
||
|
|
you can do it in a similar way that you did in bash but if you do it that way then you're you're
|
||
|
|
crippling the whole point of the of using a language like Python which is to make a cross platform
|
||
|
|
compatible and so instead of putting you know slash home slash user slash documents or whatever
|
||
|
|
to get to a file you have to do it in such a way that whether on windows linux or mac it will
|
||
|
|
understand what you want and do things the right way and so I learned how to do the os dot path dot
|
||
|
|
join method to join various things together I learned how to expand the user the the current
|
||
|
|
logged in user and stuff like that so anyway I sorted out how to deal with paths that was very
|
||
|
|
confusing but I'm starting to get the hang of it now and I'm getting more comfortable and I see
|
||
|
|
the logic behind it so I'm glad that I took the time to learn how to do that the right way the
|
||
|
|
build script really would not have worked on windows I don't think if I had not done it this way so
|
||
|
|
it was good that I was forced to do that I also have let's see one of the difficulties with the
|
||
|
|
system that I set up was that each page on the website needs to have a different title in the
|
||
|
|
you know the title is the part that appears in the the top bar of your browser the the page title
|
||
|
|
it also needed sometimes to have different left navigation images and you know you don't want
|
||
|
|
every page on the site to look exactly the same I wanted some flexibility and so the way I did
|
||
|
|
that in bash was to have at the top of every contents file a series of comments that held the
|
||
|
|
values that I wanted so if there's a left navigation image a page title a left nav image caption if
|
||
|
|
necessary an image title you know various things that would change from one page to another I
|
||
|
|
stored them in comments in a sort of more or less strictly formatted way at the top so that when
|
||
|
|
used the bash script it would use like I think it would just grip for the for the string that I
|
||
|
|
wanted and then it would strip out the other stuff and return only the value that I needed in Python
|
||
|
|
I kept those same comments at the top of the page only I would search the page for those and then
|
||
|
|
turn the resulting matches into lists and then just grab the elements from the list that would work
|
||
|
|
in there and so it's actually a little bit more elegant than the way it was done in bash and I think
|
||
|
|
probably more repeatable and predictable anyway it works and so that that was also something that
|
||
|
|
took a little bit of time to sort out I also had to learn how to make Python read from existing text
|
||
|
|
files and do various things to them that would include things like the the navigation bar on the
|
||
|
|
left side the header material and anywhere where there was something that might change from one
|
||
|
|
page to another like the page title and whatnot I put in what do I call them variables I guess
|
||
|
|
where in the the text file that I'm reading from there would be a curly brace zero closed curly
|
||
|
|
brace kind of thing and then when I open the file I tell it to format those curly braces using
|
||
|
|
the following variables and it so it plugs in the right values wherever there is one of those
|
||
|
|
little curly brace with an index number and it let me see one of the really cool libraries
|
||
|
|
that I found while I was doing this was one to smarten the punctuation I ran into a problem when
|
||
|
|
I first was running the build script on windows in that there was a certain unicode character that
|
||
|
|
it choked on every time made the build fail only certain pages had the character it was left I
|
||
|
|
think it was the the smart quotation like the unicode left double quote and the unicode right double
|
||
|
|
choked on those every time on windows and so I needed a different way to deal with the smartening
|
||
|
|
of punctuation so that you'd have the little curly quotes on either side of words
|
||
|
|
and I found I'm starting to get the hang of this whole
|
||
|
|
libraries and modules way of looking at things I'm starting anytime I need to learn how to do
|
||
|
|
something the first thing I do is check to whether see whether someone else has already done this
|
||
|
|
and has a module that I can download and that will do it magically and not surprisingly when I
|
||
|
|
checked for smartening punctuation I found that there is a library in python that will do this and
|
||
|
|
it's called smarty pants and one of the reasons I really like this module is because of the name it's
|
||
|
|
great to be able to have the word smarty pants in your code and not only is it funny it actually does
|
||
|
|
something really really cool and so now in all of the source files I don't put any kind of smart
|
||
|
|
quotes I don't do either the html entities where you do like the ampersand LDQUO semi-colon which
|
||
|
|
would do the left double quote nor do I put a unicode double quote I just put double straight
|
||
|
|
quotes and then when I the last step in the build of each page is to smarten the punctuation using
|
||
|
|
the smarty pants library and so all of that straight quotes everything is turned into smart quotations
|
||
|
|
automatically as part of the build process and this works wonderfully really really like the smarty
|
||
|
|
pants library okay and so the other thing that I had to figure out how to do was to automate the
|
||
|
|
building of the entire website from that and for that I had to learn how to iterate through
|
||
|
|
sub-directories and files within those sub-directories and that that took some thinking and some
|
||
|
|
working out but I got it in the end so anyway that that was my biggest project in python by far is
|
||
|
|
the creation of the build scripts in python for the school of music website and once I had those
|
||
|
|
set up I did the same thing for my personal website as well so I'm very very pleased with that
|
||
|
|
really stoked to be able to let my colleague do his own build script because he had been working on
|
||
|
|
some of the pages but in order to make it so that he could kind of preview the pages that he was
|
||
|
|
working on he would change URLs and stuff inside and so that when he sent them back to me I had to go
|
||
|
|
through and fix back all of the things that he changed and that was not good I did not like having
|
||
|
|
to do that so now he can work on the same files that I do and build them and preview them in the same
|
||
|
|
way and then just send me the modified contents files which I will then build and push over to the
|
||
|
|
web server all right so that was the biggest thing that I learned let's see what's next on my list
|
||
|
|
here so in conjunction with bladder one of the things I do the most is manipulation of text I will
|
||
|
|
select some text speak a command and it does various things to it whether it be putting HTML tags
|
||
|
|
around it capitalizing it correcting certain frequently misspelled words that come up when I
|
||
|
|
do my little dictation box kinds of things or whatever and so a lot of these things I've been
|
||
|
|
switching over to Python and I'm finding that Python handles them very well critical to this was
|
||
|
|
the discovery of the piper clip library piper clip is a cross-platform clipboard module for Python
|
||
|
|
and so I've found that when I select some text there are a couple of steps here the the basic workflow
|
||
|
|
is selecting some text speaking a command and the bladder command will have two parts to it
|
||
|
|
actually three parts the first part is running a virtual keystroke to do control plus C
|
||
|
|
which will copy the the selected text into the clipboard then it runs my Python script which will
|
||
|
|
take whatever is in the clipboard and do something to it either put something on either side of it
|
||
|
|
like a HTML tags or mark down formatting or something like that or it will run some library like
|
||
|
|
the title case library to capitalize all of the words that need capitalizing in a smart way for
|
||
|
|
titles and then after it's done that it sends it back to the clipboard and then the last part of
|
||
|
|
the bladder command is that it does a virtual control v which pace it back into my document
|
||
|
|
this all worked fine under bash but I'm finding that it works better in Python
|
||
|
|
if only because it's it's more cross-platform compatible and this is not only between Linux and
|
||
|
|
Windows or Mac for example but also just within different graphical libraries in my own desktop
|
||
|
|
here I use I'm running Debian with open box but one of the apps that I use the most is caliber
|
||
|
|
when I'm editing ebooks and I found that caliber does not play well with the tool that I had been
|
||
|
|
using to type out the results of all of these transformations the xvkbd package for whatever reason
|
||
|
|
xvkbd and q5 I think these are the problems they they do not work well together and so it's
|
||
|
|
impossible using xvkbd to execute any kind of uppercase or shifted characters and so I would speak a
|
||
|
|
command like I don't know ht header 2 is one of my commands that would put ht or they would
|
||
|
|
put htmail second-level header tags around the selected text and what I would come up with instead
|
||
|
|
of the less than h2 greater than thing would be comma h2 period because it could not execute the
|
||
|
|
shift and this is kind of a problem but what I found is that by using the Piper Clip library I can
|
||
|
|
actually have it go into the clipboard and paste it for whatever reason I was able to use under
|
||
|
|
bash I could use x clip to get text out of the clipboard and then process it but when I tried to
|
||
|
|
use x clip to put it back into the clipboard it wouldn't go like it wouldn't go in such a way
|
||
|
|
where I could do a virtual control v to make it paste back into the document so I was left with the
|
||
|
|
option of having it virtually type out the results and so that that didn't work very well but
|
||
|
|
using Piper Clip it seems to handle this much better it will take what's in the clipboard do
|
||
|
|
whatever you want to it and then send it back to the clipboard in such a way that the control v
|
||
|
|
keystroke sticks it back in your document so I do this for lots and lots of things
|
||
|
|
at first I was having a separate python script for every for every kind of operation that I wanted
|
||
|
|
so I would have one for putting paragraph tags one for putting bold tags for header two tags for
|
||
|
|
header three tags so I had like a dozen different python scripts that would just put each one
|
||
|
|
do a different kind of tag and then it occurred to me that I could streamline this whole thing by
|
||
|
|
having my having one python script that would accept a command line argument and that argument
|
||
|
|
would be whatever tag that I was going to put and so when I run the blather command instead of doing
|
||
|
|
p tags.py for paragraph tags it would just do I have a separate command now called html tags
|
||
|
|
which takes one argument and for paragraphs I would do html tags.py space p and that p
|
||
|
|
is then grabbed by the script and used as the element that's going to be put inside of the html tags
|
||
|
|
likewise I could put span like if I wanted to put span tags I would just run html.py
|
||
|
|
space span and it would use that whatever word or character I put after that whether it's h2
|
||
|
|
or h3 block quote whatever it is it will just stick that inside html tags and then put that around
|
||
|
|
the text that I have selected so that works really really well just putting tags around stuff
|
||
|
|
um I have another one for for wrapping a little bit more complicated things around it I call it
|
||
|
|
the wrap.py and what I do is I wrap something around the selected text and this one takes two
|
||
|
|
command line arguments the first one is whatever you want to appear before the selected text and the
|
||
|
|
second argument is what you want to appear after the select a text normally these have to go
|
||
|
|
inside single quotes so that you are sure that it's all treated as a single argument and that one
|
||
|
|
works very well too I do that when if I want not only to have for example put not only paragraph tags
|
||
|
|
around something but to also include something like a class designation like p and then class
|
||
|
|
equals no indent or something of that sort that has that can't really be handled as well
|
||
|
|
by the html tag script okay so that is how I deal with putting tags in there now I also
|
||
|
|
when I'm editing ebooks I have to do a lot of stripping out of superfluous tags and I found an
|
||
|
|
excellent Python library for this which is an extremely powerful one and one that I've only just
|
||
|
|
begun to to understand how to use but it's called beautiful soup this is beautiful soup for
|
||
|
|
specifically are also called BS4 and beautiful soup is a library that allows you like um
|
||
|
|
I know surgical like access to the html structure and so whereas I used to
|
||
|
|
do things like grip and said substitutions to try to substitute out one tag for another or to
|
||
|
|
strip out tags with beautiful soup there's a way simply to tell it what kind of tag you're looking for
|
||
|
|
and what to do with it and it just does it and it doesn't have to do any of the um hidden
|
||
|
|
mist kind of searches that I used to do so I really really like beautiful soup and I'm hopeful that
|
||
|
|
I can learn how to use it more right now I'll do things like strip out span tags or strip out
|
||
|
|
direct formatting that's done and where like inside a paragraph tag you'll see style equals and
|
||
|
|
then a whole string of style elements and these are the kinds of things that are done or that end
|
||
|
|
up there when someone takes a word process or document and it makes an ebook out of it when they
|
||
|
|
don't really understand how to use a word processor in the first place and so they do all kinds of
|
||
|
|
direct formatting they clearly have not listened to a hookah's series on how to use a word processor
|
||
|
|
but you can strip out a lot of that kind of stuff using beautiful soup and the unwrap method
|
||
|
|
let's see so that's from my clean html script and I have various ones to strip out particular html
|
||
|
|
tags and again like I run a no tag script that takes one command line argument which will be whatever
|
||
|
|
tag that I want to strip out of the selected text and it will do the unwrap method on that and
|
||
|
|
get rid of that tag let's see so another way that I use this is to insert snippets of text
|
||
|
|
by using voice commands and these snippets could be anything from very very short things like
|
||
|
|
I don't know CSS rules or things like that to entire templates for lily pond files or html pages
|
||
|
|
or things like that and so I have a template.py script where I have a whole list of my frequently
|
||
|
|
used templates right now there are 14 of them and each I have as a comment after each one an index
|
||
|
|
number so that I can easily see which index number I use and when I run the template.py script
|
||
|
|
I use one command line argument that tells which item from the list to use so for a
|
||
|
|
a single staff in lily pond I'll use template.py space one and that argument number one is used
|
||
|
|
as the index to grab the right file from the list of templates and it will copy that again this
|
||
|
|
is something that is copied into the clipboard by the piper clip library and then it's pasted into
|
||
|
|
my text editor using a virtual control v. Also I made a script that helps me a lot in my editing
|
||
|
|
of the counterpoint book that I've been working on which is I've been going through and identifying
|
||
|
|
all of the prelude's fugues inventions and whatever by JS Bach that the author uses as musical
|
||
|
|
examples because all he does through the whole book is just as Bach he says this example is by Bach
|
||
|
|
but he doesn't tell you what it is or where it comes from or anything and so part of my job as an
|
||
|
|
editor I feel is to identify all of these specifically each one of those pieces will have a bunch of
|
||
|
|
information that is pertinent about it like the prelude number two in whatever key BWV number
|
||
|
|
this and it was hard to keep track of all of these things because every piece has a different BWV
|
||
|
|
listing number and so what I did was made a Python script that has all of the information available
|
||
|
|
as a list and then to insert let's say I realized that this is invention number eight from the
|
||
|
|
two-part inventions what I do is in my text editor all I do is type the number eight and then speak
|
||
|
|
the command invention title and it knows to select that number eight and use that as the command
|
||
|
|
line argument for finding the right string of text from my list and then it pace it into my
|
||
|
|
document for me it's really really magical because I'd never make mistakes anymore about BWV numbers
|
||
|
|
or keys or anything of that sort absolutely wonderful so this Python is beautifully suited for
|
||
|
|
this using lists and the piper clip thing to stick in this clipboard and it's starting to run
|
||
|
|
long here so I don't want to go too much further with this but finally I use Python for fun stuff
|
||
|
|
too a lot of the things that I used to do with you know interacting with my laptop using bladder
|
||
|
|
like I would ask it what time it is or what day it is or what should I have for dinner or what for
|
||
|
|
breakfast or if I tell it thank you then it used to choose from a list of commands it like it would
|
||
|
|
use the shuff command and shuffle all of the possible thank you responses that I had told it
|
||
|
|
and then choose the top one and then run it through eSpeak well in Python I found that there is
|
||
|
|
a library called pi ttsx which allows you to get at the system text-to-speech engine whether it
|
||
|
|
be on windows linux or mac and you can tell it how fast to speak and all you have to do you know
|
||
|
|
you tell it you set up a couple of things like you get it started with the init method and then
|
||
|
|
you tell it the rate that you want it to speak you know this is optional it'll it'll speak at the
|
||
|
|
default rate if you don't tell it but then you just tell it what you want it to say and it will say
|
||
|
|
it and this works on mac windows linux and so in that way it's nice to have it across platform so
|
||
|
|
what I do is I I made up various Python scripts that will use the random library and choose a random
|
||
|
|
integer and use that to figure out which item from a long list of things to bring back and speak to
|
||
|
|
me so it's fun you know I like to use Python and the pi ttsx things to have my computer talk to
|
||
|
|
I wrote this one called coffee dot pi I'm really proud of this one and maybe I'll put a link to the
|
||
|
|
YouTube video where I demonstrate this but I ask my computer whether it has had too much coffee
|
||
|
|
and first it does a coin flip using the random dot rand int method it chooses a random integer
|
||
|
|
between zero and one for a coin flip and if it turns up zero that means it's already had its coffee
|
||
|
|
and I set the speech rate or no if it comes up zero that means it has not had coffee yet
|
||
|
|
and so it's really sluggish and slow so I set the speech rate at only 90 and it it talks very slowly
|
||
|
|
like this and I have a whole series of things that it might say and then if it turns up one on
|
||
|
|
the coin flip then that means it has had its coffee so the rate is set really fast and it speaks
|
||
|
|
that the response is super duper fast so I get to use a conditional statement there and I get to
|
||
|
|
use lists and yeah it's fun I've also used the beautiful soup thing to create a major league
|
||
|
|
baseball xm radio schedule for me I find I used to I have a way to do this with bash and I actually
|
||
|
|
have my raspberry pie run it every single day and it emails me a list of all the games that are
|
||
|
|
going to be on the radio and what channels they're on but it's it's a very much hidden mis proposition
|
||
|
|
with making sure that I get the right schedule for today I have to search for the word today and
|
||
|
|
then search for the word tomorrow and then leave out everything else and then do a little bit of
|
||
|
|
formatting what I found with beautiful soup is that I can use the url git
|
||
|
|
I don't remember exactly what it is but I I grab the content of the page and then I use beautiful
|
||
|
|
soup to extract only the one table I want and it goes right there with laser like precision
|
||
|
|
grabs that one table for today's games and then I can do some formatting I actually have it build
|
||
|
|
a little HTML page and then put it in place on my server so that I can go to this one major league
|
||
|
|
baseball page on my server and see today's games and it runs every hour so that by the end of the
|
||
|
|
day it only shows the one or two games that are still left to happen so I really like that also
|
||
|
|
got a weather script that uses the pi w api pi weather api library and it gets weather from weather
|
||
|
|
and so you have to set you have to tell it the code for your area minus us la 0261 for
|
||
|
|
Lafayette Louisiana and when you grab the current conditions from there it brings back a dictionary
|
||
|
|
and some lists in there with all kinds of weather information and I've set up in my script just
|
||
|
|
the things I want like I wanted to tell me currently it's either cloudy or sunny or whatever
|
||
|
|
tells me how many degrees it is the humidity and tells me the perspective high for today and the
|
||
|
|
chance of rain and then it gives me a tomorrow's high and tomorrow's chance of rain as well
|
||
|
|
and then it actually speaks that to me using the system text-to-speech engine so I use the
|
||
|
|
pi ttsx library for this script as well anyway I see that I've hit the 30 minute mark so I should
|
||
|
|
probably stop now but anyway I hope you guys have enjoyed hear me talk about how I use Python
|
||
|
|
again I'm not really a programmer and so I don't write programs so much as I do just little
|
||
|
|
things that help me get by and help me have fun so yep that's it I will talk to you guys some other
|
||
|
|
time maybe with more about Python if I actually learn anything else thanks bye
|
||
|
|
you've been listening to hecka public radio at hecka public radio dot org we are a community
|
||
|
|
podcast network that releases shows every weekday Monday through Friday today's show like all our
|
||
|
|
shows was contributed by an hbr listener like yourself if you ever thought of recording a podcast
|
||
|
|
and click on our contributing to find out how easy it really is hecka public radio was found
|
||
|
|
by the digital dog pound and the infonomican computer club and it's part of the binary revolution
|
||
|
|
at binrev.com if you have comments on today's show please email the host directly leave a comment
|
||
|
|
on the website or record a follow-up episode yourself unless otherwise stated today's show is
|
||
|
|
released on the creative comments attribution share a live 3.0 license
|