<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:og="http://ogp.me/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:schema="http://schema.org/" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:sioct="http://rdfs.org/sioc/types#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" version="2.0" xml:base="https://www.linuxjournal.com/">
  <channel>
    <title>Programming</title>
    <link>https://www.linuxjournal.com/</link>
    <description/>
    <language>en</language>
    
    <item>
  <title>Understanding Python's asyncio</title>
  <link>https://www.linuxjournal.com/content/understanding-pythons-asyncio</link>
  <description>  &lt;div data-history-node-id="1340725" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/reuven-m-lerner" lang="" about="https://www.linuxjournal.com/users/reuven-m-lerner" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Reuven M. Lerner&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;How to get started using Python's asyncio.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
Earlier this year, I attended PyCon, the international Python
conference. One topic, presented at numerous talks and discussed
informally in the hallway, was the state of threading in Python—which
is, in a nutshell, neither ideal nor as terrible as some critics would
argue.
&lt;/p&gt;

&lt;p&gt;
A related topic that came up repeatedly was that of "asyncio", a
relatively new approach to concurrency in Python. Not only were there
formal presentations and informal discussions about asyncio, but a
number of people also asked me about courses on the subject.
&lt;/p&gt;

&lt;p&gt;
I must admit, I was a bit surprised by all the interest. After
all, asyncio isn't a new addition to Python; it's been around for a
few years. And, it doesn't solve all of the problems associated with
threads. Plus, it can be confusing for many people to get started with it.
&lt;/p&gt;

&lt;p&gt;
And yet, there's no denying that after a number of years when
people ignored asyncio, it's starting to gain steam. I'm sure
part of the reason is that asyncio has matured and improved over time,
thanks in no small part to much dedicated work by countless developers.
But, it's also because asyncio is an increasingly good and useful choice
for certain types of tasks—particularly tasks that work across
networks.
&lt;/p&gt;

&lt;p&gt;
So with this article, I'm kicking off a series on asyncio—what it is, how to
use it, where it's appropriate, and how you can and should (and also can't
and shouldn't) incorporate it into your own work.
&lt;/p&gt;

&lt;span class="h3-replacement"&gt;
What Is asyncio?&lt;/span&gt;

&lt;p&gt;
Everyone's grown used to computers being able to do more than one thing at a
time—well, sort of. Although it might seem as though computers are
doing more than one thing at a time, they're actually switching, very
quickly, across different tasks. For example, when you &lt;code&gt;ssh&lt;/code&gt; in to a Linux
server, it might seem as though it's only executing your commands. But
in actuality, you're getting a small "time slice" from the CPU, with the
rest going to other tasks on the computer, such as the systems that
handle networking, security and various protocols. Indeed, if you're
using SSH to connect to such a server, some of those time slices
are being used by &lt;code&gt;sshd&lt;/code&gt; to handle your connection and even allow you to
issue commands.
&lt;/p&gt;

&lt;p&gt;
All of this is done, on modern operating systems, via "pre-emptive
multitasking". In other words, running programs aren't given a choice of
when they will give up control of the CPU. Rather, they're forced to
give up control and then resume a little while later. Each process
running on a computer is handled this way. Each process can, in turn,
use threads, sub-processes that subdivide the time slice given to their
parent process.
&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/understanding-pythons-asyncio" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Wed, 07 Aug 2019 22:00:00 +0000</pubDate>
    <dc:creator>Reuven M. Lerner</dc:creator>
    <guid isPermaLink="false">1340725 at https://www.linuxjournal.com</guid>
    </item>
<item>
  <title>Python's Mypy: Callables and Generators</title>
  <link>https://www.linuxjournal.com/content/pythons-mypy-callables-and-generators</link>
  <description>  &lt;div data-history-node-id="1340661" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/reuven-m-lerner" lang="" about="https://www.linuxjournal.com/users/reuven-m-lerner" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Reuven M. Lerner&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;Learn how Mypy's type checking works with functions and
generators.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
In my last two articles I've described some of the ways Mypy,
a type checker for Python, can help identify potential problems
with your code. [See &lt;a href="https://www.linuxjournal.com/content/introducing-mypy-experimental-optional-static-type-checker-python"&gt;"Introducing
Mypy, an Experimental Optional Static Type Checker for Python"&lt;/a&gt; and &lt;a href="https://www.linuxjournal.com/content/pythons-mypy-advanced-usage"&gt;"Python's
Mypy—Advanced Usage"&lt;/a&gt;.] For people (like me) who have enjoyed dynamic languages
for a long time, Mypy might seem like a step backward. But given the
many mission-critical projects being written in Python, often by large
teams with limited communication and Python experience, some kind of
type checking is an increasingly necessary evil.
&lt;/p&gt;

&lt;p&gt;
It's important to remember that Python, the language, isn't changing,
and it isn't becoming statically typed. Mypy is a separate program, running
outside Python, typically as part of a continuous integration (CI)
system or invoked as part of a Git commit hook. The idea is that Mypy
runs before you put your code into production, identifying where the
data doesn't match the annotations you've made to your variables and
function parameters.
&lt;/p&gt;

&lt;p&gt;
I'm going to focus on a few of Mypy's advanced features here. You
might not encounter them very often, but even if you don't, it'll give
you a better picture of the complexities associated with type checking,
and how deeply the Mypy team is thinking about their work, and what
tests need to be done.  It'll also help you understand more about the ways
people do type checking, and how to balance the beauty,
flexibility and expressiveness of dynamic typing with the strictness
and fewer errors of static typing.
&lt;/p&gt;

&lt;span class="h3-replacement"&gt;
Callable Types&lt;/span&gt;

&lt;p&gt;
When I tell participants in my Python classes that everything in Python
is an object, they nod their heads, clearly thinking, "I've heard this
before about other languages." But then I show them that functions and
classes are both objects, and they realize that Python's notion of
"everything" is a bit more expansive than theirs.  (And yes, Python's
definition of "everything" isn't as wide as Smalltalk's.)
&lt;/p&gt;

&lt;p&gt;
When you define a function, you're creating a new object, one of type
"function":

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
&gt;&gt;&gt; def foo():
...     return "I'm foo!"

&gt;&gt;&gt; type(foo)
&lt;class 'function'&gt;
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
Similarly, when you create a new class, you're adding a new object type
to Python:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
&gt;&gt;&gt; class Foo():
...     pass

&gt;&gt;&gt; type(Foo)
&lt;class 'type'&gt;
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
It's a pretty common paradigm in Python to write a function that, when
it runs, defines and runs an inner function. This is also known as a
"closure", and it has a few different uses.  For example, you can write:

&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/pythons-mypy-callables-and-generators" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Mon, 29 Jul 2019 11:00:00 +0000</pubDate>
    <dc:creator>Reuven M. Lerner</dc:creator>
    <guid isPermaLink="false">1340661 at https://www.linuxjournal.com</guid>
    </item>
<item>
  <title>Finishing Up the Bash Mail Merge Script</title>
  <link>https://www.linuxjournal.com/content/finishing-bash-mail-merge-script</link>
  <description>  &lt;div data-history-node-id="1340605" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/dave-taylor" lang="" about="https://www.linuxjournal.com/users/dave-taylor" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Dave Taylor&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;Finally, I'm going to finish the mail merge script, just in time for
Replicant Day.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
Remember the &lt;a href="https://www.linuxjournal.com/content/fun-mail-merge-and-cool-bash-arrays"&gt;mail
merge script&lt;/a&gt; I started writing a while back? Yeah,
that was quite some
time ago. I got sidetracked with the &lt;em&gt;Linux Journal&lt;/em&gt; Anniversary special
issue (see my article &lt;a href="https://www.linuxjournal.com/content/back-day-unix-minix-and-linux"&gt;"Back
in the Day: UNIX, Minix and Linux"&lt;/a&gt;), and then I spun off on a
completely different tangent for my last article (&lt;a href="https://www.linuxjournal.com/content/breaking-apache-log-files-analysis"&gt;"Breaking
Up Apache Log Files for Analysis"&lt;/a&gt;). I blame it on...
&lt;/p&gt;

&lt;p&gt;
&lt;em&gt;SQUIRREL!&lt;/em&gt;
&lt;/p&gt;

&lt;p&gt;
Oh, sorry, back to topic here. I was developing a shell
script that would let you specify a text document with
embedded field names that could be substituted iteratively
across a file containing lots of field values.
&lt;/p&gt;

&lt;p&gt;
Each field was denoted by &lt;code&gt;#fieldname#&lt;/code&gt;, and I identified two
categories of fieldnames: fixed and dynamic. A fixed value
might be &lt;code&gt;#name#&lt;/code&gt;, which would come directly out of the data
file, while a dynamic value could be &lt;code&gt;#date#&lt;/code&gt;, which would be
the current date.
&lt;/p&gt;
&lt;p&gt;
More interesting, I also proposed calculated values,
specifically &lt;code&gt;#suggested#&lt;/code&gt;, which would be a value calculated
based on &lt;code&gt;#donation#&lt;/code&gt;, and &lt;code&gt;#date#&lt;/code&gt;, which would be replaced by
the current date. The super-fancy version would have a
simple language where you could define the relationship between
variables, but let's get real. Mail merge. It's just mail
merge.
&lt;/p&gt;

&lt;span class="h3-replacement"&gt;
Reading and Assigning Values&lt;/span&gt;

&lt;p&gt;
It turns out that the additions needed for this script aren't
too difficult. The basic data file has comma-separated
field names, then subsequent lines have the values
associated with those fields.
&lt;/p&gt;

&lt;p&gt;
Here's that core code:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
if [ $lines -eq 1 ] ; then # field names
# grab variable names
declare -a varname=($f1 $f2 $f3 $f4 $f5 $f6 $f7)
else # process fields

# grab values for this line (can contain spaces)
declare -a value=("$f1" "$f2" "$f3" "$f4" "$f5" "$f6" "$f7")
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;declare&lt;/code&gt; function turns out to be ideal for this,
allowing you to create an array &lt;code&gt;varname&lt;/code&gt; based on the
contents of the first line, then keep replacing the values
of the array &lt;code&gt;value&lt;/code&gt;, so that &lt;code&gt;varname[1] =
value[1]&lt;/code&gt;, and so
on.
&lt;/p&gt;

&lt;p&gt;
To add the additional variables &lt;code&gt;#date#&lt;/code&gt; and
&lt;code&gt;#suggested#&lt;/code&gt;, you
simply can append them to the &lt;code&gt;varname&lt;/code&gt; and
&lt;code&gt;value&lt;/code&gt; arrays. The
first one is easy, but it did highlight a weakness in the
original code that I had to fix by adding quotes as shown:

&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/finishing-bash-mail-merge-script" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Thu, 04 Jul 2019 12:00:00 +0000</pubDate>
    <dc:creator>Dave Taylor</dc:creator>
    <guid isPermaLink="false">1340605 at https://www.linuxjournal.com</guid>
    </item>
<item>
  <title>Python's Mypy--Advanced Usage</title>
  <link>https://www.linuxjournal.com/content/pythons-mypy-advanced-usage</link>
  <description>  &lt;div data-history-node-id="1340606" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/reuven-m-lerner" lang="" about="https://www.linuxjournal.com/users/reuven-m-lerner" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Reuven M. Lerner&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;Mypy can check more than simple Python types.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
In &lt;a href="https://www.linuxjournal.com/content/introducing-mypy-experimental-optional-static-type-checker-python"&gt;my
last article&lt;/a&gt;, I introduced Mypy, a package that enforces type checking in
Python programs. Python itself is, and always will remain, a
dynamically typed language. However, Python 3 supports "annotations", a
feature that allows you to attach an object to variables, function
parameters and function return values. These annotations are ignored by
Python itself, but they can be used by external tools.
&lt;/p&gt;

&lt;p&gt;
Mypy is one such tool, and it's an increasingly popular one. The idea is that
you run Mypy on your code before running it. Mypy looks at your code
and makes sure that your annotations correspond with actual usage. In
that sense, it's far stricter than Python itself, but that's the whole
point.
&lt;/p&gt;

&lt;p&gt;
In &lt;a href="https://www.linuxjournal.com/content/introducing-mypy-experimental-optional-static-type-checker-python"&gt;my last article&lt;/a&gt;, I covered some basic uses for Mypy. Here, I want
to expand upon those basics and show how Mypy really digs deeply
into type definitions, allowing you to describe your code in a way that
lets you be more confident of its stability.
&lt;/p&gt;

&lt;span class="h3-replacement"&gt;
Type Inference&lt;/span&gt;

&lt;p&gt;
Consider the following code:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
x: int = 5
x = 'abc'
print(x)
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
This first defines the variable &lt;code&gt;x&lt;/code&gt;, giving it a type
annotation of &lt;code&gt;int&lt;/code&gt;.
It also assigns it to the integer 5. On the next line, it assigns
&lt;code&gt;x&lt;/code&gt; the
string &lt;code&gt;abc&lt;/code&gt;. And on the third line, it prints the value of
&lt;code&gt;x&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
The Python language itself has no problems with the above code. But if
you run &lt;code&gt;mypy&lt;/code&gt; against it, you'll get an error message:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
mytest.py:5: error: Incompatible types in assignment
   (expression has type "str", variable has type "int")
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
As the message says, the code declared the variable to have type
&lt;code&gt;int&lt;/code&gt;,
but then assigned a string to it. Mypy can figure this out because,
despite what many people believe, Python is a strongly typed language.
That is, every object has one clearly defined type. Mypy notices this
and then warns that the code is assigning values that are contrary to what
the declarations said.
&lt;/p&gt;

&lt;p&gt;
In the above code, you can see that I declared &lt;code&gt;x&lt;/code&gt; to be of
type &lt;code&gt;int&lt;/code&gt; at
definition time, but then assigned it to a string, and then I got an error.
What if I don't add the annotation at all? That is, what if I run the
following code via Mypy:

&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/pythons-mypy-advanced-usage" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Mon, 24 Jun 2019 11:30:00 +0000</pubDate>
    <dc:creator>Reuven M. Lerner</dc:creator>
    <guid isPermaLink="false">1340606 at https://www.linuxjournal.com</guid>
    </item>
<item>
  <title>Build Your Own Internet Radio Receiver</title>
  <link>https://www.linuxjournal.com/content/build-your-own-internet-radio-receiver</link>
  <description>  &lt;div data-history-node-id="1340545" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/nick-tufillaro" lang="" about="https://www.linuxjournal.com/users/nick-tufillaro" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Nick Tufillaro&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;Tune in to communities around the world with the push of a button.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
When I get home at night, I like to tune into the world with the push
of a button. I've lived in lots of different places—from Dunedin,
New Zealand, to Santa Fe, New Mexico—and in each town, I've come to love
a radio station (usually a community radio station) that embodies
the spirit of the place. With the push of a button, I can get a bit back
in sync with each of these places and also visit new communities, thanks
to internet radio.
&lt;/p&gt;

&lt;p&gt;
Why build your own internet radio receiver? One option, of course, is
simply to use an app for a receiver. However, I've found that the most common
apps don't keep their focus on the task at hand, and are increasingly
distracted by offering additional social-networking services. And besides,
I want to listen now. I don't want to check into my computer or phone,
log in yet again, and endure the stress of recalling YAPW (Yet Another
PassWord). I've also found that the current offering of internet radio
boxes falls short of my expectations. Like I said, I've lived in a lot
of places—more than two or four or eight. I want a lot of buttons, so I can
tune in to a radio station with just one gesture. Finally, I've noticed
that streams are increasingly problematic if I don't go directly to
the source. Often, streams chosen through a "middle man" start with
an ad or blurb that is tacked on as a preamble. Or sometimes the "middle man" might
tie me to a stream of lower audio quality than the best being served up.
&lt;/p&gt;

&lt;p&gt;
So, I turned to building my own internet radio receiver—one with lots
of buttons that allow me to "tune in" without being too pushy. In this
article,
I share my experience. In principle, it should be easy—you just need a Linux distro, a ship to sail her on and an external
key pad for a rudder. In practice, it's not too hard, but there are a
few obstacles along the course that I hope to help you navigate.
&lt;/p&gt;

&lt;p&gt;
My recipe list included the following:
&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;
A used notebook with an ultra low voltage
(Core 2 Duo) processor.&lt;/li&gt;

&lt;li&gt;
An audio interface with an optical TOSLINK.&lt;/li&gt;

&lt;li&gt;
pyradio: an open-source Python radio program.&lt;/li&gt;

&lt;li&gt;
An external keypad.&lt;/li&gt;&lt;/ol&gt;&lt;img src="https://www.linuxjournal.com/sites/default/files/styles/max_650x650/public/u%5Buid%5D/12693f1smaller.png" width="600" height="572" alt="""" class="image-max_650x650" /&gt;&lt;p&gt;
&lt;em&gt;Figure 1. My Hardware Setup&lt;/em&gt;
&lt;/p&gt;

&lt;p&gt;
Why a notebook and not a Raspberry Pi or ship of a similar ilk? Mostly
due to time—my time in particular. It's not too hard to find a high quality
notebook about ten years old for about $50, so the cost is really not
that different, and I find the development platform to be much
quicker.
&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/build-your-own-internet-radio-receiver" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Mon, 27 May 2019 11:00:00 +0000</pubDate>
    <dc:creator>Nick Tufillaro</dc:creator>
    <guid isPermaLink="false">1340545 at https://www.linuxjournal.com</guid>
    </item>
<item>
  <title>Introducing Mypy, an Experimental Optional Static Type Checker for Python</title>
  <link>https://www.linuxjournal.com/content/introducing-mypy-experimental-optional-static-type-checker-python</link>
  <description>  &lt;div data-history-node-id="1340583" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/reuven-m-lerner" lang="" about="https://www.linuxjournal.com/users/reuven-m-lerner" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Reuven M. Lerner&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;Tighten up your code and identify errors before they occur with
&lt;code&gt;mypy&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
I've been using dynamic languages—Perl, Ruby and Python—for
many years. I love the flexibility and expressiveness that such
languages provide. For example, I can define a function that sums
numbers:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
def mysum(numbers):
    total = 0
   for one_number in numbers:
       total += one_number
   return total
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
The above function will work on any iterable that returns numbers. So
I can run the above on a list, tuple or set of numbers. I can even
run it on a dictionary whose keys are all numbers. Pretty great,
right?
&lt;/p&gt;

&lt;p&gt;
Yes, but for my students who are used to static, compiled languages,
this is a very hard thing to get used to. After all, how can you make
sure that no one passes you a string, or a number of strings? What if
you get a list in which some, but not all, of the elements are numeric?
&lt;/p&gt;

&lt;p&gt;
For a number of years, I used to dismiss such worries. After all,
dynamic languages have been around for a long time, and they have done a good
job. And really, if people are having these sorts of type mismatch
errors, then maybe they should be paying closer attention. Plus, if you
have enough testing, you'll probably be fine.
&lt;/p&gt;

&lt;p&gt;
But as Python (and other dynamic languages) have been making inroads
into large companies, I've become increasingly convinced that there's
something to be said for type checking. In particular, the fact that
many newcomers to Python are working on large projects, in which many
parts need to interoperate, has made it clear to me that some sort of
type checking can be useful.
&lt;/p&gt;

&lt;p&gt;
How can you balance these needs? That is, how can you enjoy Python as a
dynamically typed language, while simultaneously getting some added
sense of static-typing stability?
&lt;/p&gt;

&lt;p&gt;
One of the most popular answers is a system known as &lt;code&gt;mypy&lt;/code&gt;, which
takes advantage of Python 3's type annotations for its own purposes.
Using &lt;code&gt;mypy&lt;/code&gt; means that you can write and run Python in the normal way,
gradually adding static type checking over time and checking it
outside your program's execution.
&lt;/p&gt;

&lt;p&gt;
In this article, I start exploring &lt;code&gt;mypy&lt;/code&gt; and how you can use it to
check for problems in your programs. I've been impressed by
&lt;code&gt;mypy&lt;/code&gt;,
and I believe you're likely to see it deployed in a growing number
of places, in no small part because it's optional, and thus allows
developers to use it to whatever degree they deem necessary,
tightening things up over time, as well.
&lt;/p&gt;

&lt;span class="h3-replacement"&gt;
Dynamic and Strong Typing&lt;/span&gt;

&lt;p&gt;
In Python, users enjoy not only dynamic typing, but also strong typing.
"Dynamic" means that variables don't have types, but that values do.
So you can say:

&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/introducing-mypy-experimental-optional-static-type-checker-python" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Mon, 13 May 2019 11:30:00 +0000</pubDate>
    <dc:creator>Reuven M. Lerner</dc:creator>
    <guid isPermaLink="false">1340583 at https://www.linuxjournal.com</guid>
    </item>
<item>
  <title>What The @#$%&amp;! (Heck) is this #! (Hash-Bang) Thingy In My Bash Script</title>
  <link>https://www.linuxjournal.com/content/what-heck-hash-bang-thingy-my-bash-script</link>
  <description>  &lt;div data-history-node-id="1340624" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/mitch-frazier" lang="" about="https://www.linuxjournal.com/users/mitch-frazier" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Mitch Frazier&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You've seen it a million times—the hash-bang (#!) line at the top of a script—whether it be Bash, Python, Perl or some other scripting language. And, I'm sure you know what its purpose is: it specifies the script interpreter that's used to execute the script. But, do you know how it actually works? Your initial thought might be that your shell (bash) reads that line and then executes the specified interpreter, but that's not at all how it works. How it actually works is the main focus of this post, but I also want to introduce how you can create your own version of "hash-bang" if you're so inclined.&lt;/p&gt;
&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/what-heck-hash-bang-thingy-my-bash-script" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Fri, 10 May 2019 11:30:00 +0000</pubDate>
    <dc:creator>Mitch Frazier</dc:creator>
    <guid isPermaLink="false">1340624 at https://www.linuxjournal.com</guid>
    </item>
<item>
  <title>Linux...Do It for the Children</title>
  <link>https://www.linuxjournal.com/content/linuxdo-it-children</link>
  <description>  &lt;div data-history-node-id="1340508" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/marcel-gagn%C3%A9" lang="" about="https://www.linuxjournal.com/users/marcel-gagn%C3%A9" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Marcel Gagné&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;A rundown of some fun and educational Linux software for kids.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
I'm probably going to regret that title. I've been making fun of those
words, "do it for the children" for years. It's one of those "reasons"
people turn to when all else has failed in terms of getting you to sign on
to whatever lifestyle, agenda, law, changes to food—you name it. Hearing
those words draws the Spock eyebrow lift out of me faster than you can say,
"fascinating".
&lt;/p&gt;

&lt;p&gt;
Okay, pretend that I didn't start this article with that comment. Let's try
this instead.
&lt;/p&gt;

&lt;p&gt;
As I write this, my youngest son is 11 years old. He has grown up in a
magical world of electronics that delivers what he wants to watch when he
wants to watch it. Access to the web is something he always has known. Until
very recently, he never had seen television with commercials. A couple
years ago, my wife and I thought it was something he should at least
understand, so we turned to a live TV program for the first time in I don't
know how long. He was not impressed with the interruptions. Now, with
multiple Google Home units in the house, including one in his bedroom, the
on-demand magic is pretty much complete.
&lt;/p&gt;

&lt;p&gt;
He started playing video games when he was three and was scary good on my
PS3 by the time he turned four. He started using a laptop when he was five,
and that laptop ran Linux. I'm pretty sure he was using Kubuntu, but it
might have been Linux Mint. Either way, it was a KDE Plasma desktop. In
short, the world of tech is nothing new for him, and Linux is just what
people run. His school has Chromebooks, and the few run-ins he's had with
Windows left him cold.
&lt;/p&gt;

&lt;p&gt;
Kids and Linux? Absolutely.
&lt;/p&gt;

&lt;span class="h3-replacement"&gt;GCompris&lt;/span&gt;

&lt;p&gt;
Much earlier on, however, I took advantage of some of the simpler
educational games available on Linux. One of my favorites is GCompris, an
all-in-one collection of educational games for children, geared for ages
two to ten (Figure 1). By the way, GCompris is pronounced like the
French words, &lt;em&gt;J'ai compris&lt;/em&gt;, and it literally means, "I have
understood", paying homage to its educational focus. I've mentioned
this one in the past, but GCompris is a living, breathing project, actively
developed by the KDE community with a new release just this past month.
&lt;/p&gt;

&lt;img src="https://www.linuxjournal.com/sites/default/files/styles/max_650x650/public/u%5Buid%5D/image4_0.png" width="650" height="459" alt="""" class="image-max_650x650" /&gt;&lt;p&gt;
&lt;em&gt;Figure 1. GCompris is a suite of educational games for kids.&lt;/em&gt;
&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/linuxdo-it-children" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Thu, 11 Apr 2019 11:30:00 +0000</pubDate>
    <dc:creator>Marcel Gagné</dc:creator>
    <guid isPermaLink="false">1340508 at https://www.linuxjournal.com</guid>
    </item>
<item>
  <title>The Kids Take Over</title>
  <link>https://www.linuxjournal.com/content/kids-take-over-0</link>
  <description>  &lt;div data-history-node-id="1340501" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/doc-searls" lang="" about="https://www.linuxjournal.com/users/doc-searls" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Doc Searls&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;As with Linux, these kids are all about making things—and then making them
better. They're also up against incumbent top-down systems they will reform
or defeat. Those are the only choices.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
It starts here, in the heart of Long Island, a couple dozen exits east of
Queens. I saw it with my own eyes in &lt;a href="http://www.mineola.k12.ny.us"&gt;Mineola's Public Schools&lt;/a&gt;, where kids,
led by a nonprofit called &lt;a href="http://www.kidoyo.com"&gt;kidOYO&lt;/a&gt; ("kid-oh-yo"), are learning to program in
different languages on different devices and operating systems, creating
and re-creating software and hardware, with fun and at speed. Their esteem
in themselves and in the eyes of their peers derives from their actual work
and their helpfulness to others. They are also moving ahead through levels
of productivity and confidence that are sure to create real-world results
and strip the gears of any system meant to contain them. Mineola's schools
are not one of those systems.
&lt;/p&gt;

&lt;p&gt;
OYO means Own Your Own, and that's what these kids are learning to do. In
geekier terms, they are rooting their own lives online. They're doing it by
learning to program in languages that start with &lt;a href="https://scratch.mit.edu"&gt;Scratch&lt;/a&gt; and progress
through Python, Java, C# and beyond. They're doing it on every hardware and
software platform they can, while staying anchored to Linux, because Linux
is where the roots of personal freedom and agency go deepest. And they're
doing in all in the spirit of &lt;a href="https://www.harpercollins.com/9780066620732/just-for-fun"&gt;Linus' book
title&lt;/a&gt;: &lt;em&gt;just for fun&lt;/em&gt;.
&lt;/p&gt;

&lt;p&gt;
With kidOYO, the heuristics go both ways: kidOYO teaches the kids, and the
kids teach kidOYO. Iteration is constant. What works gets improved, and
what doesn't gets tossed. The measures of success are how enthused the kids
stay, how much they give and get energy from each other, and how much they
learn and teach. Nowhere are they sorted into bell curves or given
caste-producing labels, such as "gifted" or "challenged". Nor are they
captive to the old report-card system. When they do take standardized
tests, for example the college AP (advanced placement) ones for computer
science, they &lt;a href="https://kidoyo.oyoclass.com/story/596129c9ca292c7c349d7bda"&gt;tend to
kick ass&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
kidOYO is the creation of &lt;a href="https://www.flickr.com/photos/linuxjournal/46920888602/in/album-72157706335515125"&gt;the
Loffreto family&lt;/a&gt;: Devon and Melora, and their son
Zhen, who is now 13. What started as a way to teach computing to Zhen
turned into ways to teach computer science to every kid, everywhere.
kidOYO's methods resemble how the Linux kernel constantly improves, with
code contributors and maintainers stamping out bugs and iterating toward
ever-expanding completeness, guided by an equal mix of purpose and fun.
&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/kids-take-over-0" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Mon, 08 Apr 2019 11:30:00 +0000</pubDate>
    <dc:creator>Doc Searls</dc:creator>
    <guid isPermaLink="false">1340501 at https://www.linuxjournal.com</guid>
    </item>
<item>
  <title>Fun with Mail Merge and Cool Bash Arrays</title>
  <link>https://www.linuxjournal.com/content/fun-mail-merge-and-cool-bash-arrays</link>
  <description>  &lt;div data-history-node-id="1340460" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/dave-taylor" lang="" about="https://www.linuxjournal.com/users/dave-taylor" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Dave Taylor&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;Creating a sed-based file substitution tool.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
A few weeks ago, I was digging through my spam folder and found an email message
that started out like this:
&lt;/p&gt;

&lt;pre&gt;
&lt;code&gt;
Dear #name#
Congratulations on winning the $15.7 million lottery payout!
To learn how to claim your winnings, please...
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
Obviously, it was a scam (does anyone actually fall for these?), but what captured my
attention was the &lt;code&gt;#name#&lt;/code&gt; sequence. Clearly that was a fail on the part of the sender who
presumably didn't know how to use AnnoyingSpamTool 1.3 or whatever the heck
he or she was using.
&lt;/p&gt;

&lt;p&gt;
The more general notation for bulk email and file transformations is pretty
interesting, however. There are plenty of legitimate reasons to use this sort
of substitution, ranging from email newsletters (like the one I send every week
from &lt;a href="https://www.askdavetaylor.com"&gt;AskDaveTaylor.com&lt;/a&gt;—check it out!) to stockholder announcements and much
more.
&lt;/p&gt;

&lt;p&gt;
With that as the inspiration, let's build a tool that offers just this
capability.
&lt;/p&gt;

&lt;p&gt;
The simple version will be a 1:1 substitution, so &lt;code&gt;#name#&lt;/code&gt; becomes, say,
"Rick Deckard", while &lt;code&gt;#first#&lt;/code&gt; might be "Rick" and
&lt;code&gt;#last#&lt;/code&gt; might
be "Deckard". Let's build on that, but let's start
small.
&lt;/p&gt;

&lt;span class="h3-replacement"&gt;
Simple Word Substitution in Linux&lt;/span&gt;

&lt;p&gt;
There are plenty of ways to tackle the word substitution from the command line,
ranging from Perl to awk, but here I'm using the original UNIX command
&lt;code&gt;sed&lt;/code&gt; (stream editor) designed for exactly this purpose. General notation for a
substitution is s/old/new/, and if you tack on a &lt;code&gt;g&lt;/code&gt; at the end, it
matches every occurrence on a line, not only the first, so the full command
is s/old/new/g.
&lt;/p&gt;

&lt;p&gt;
Before going further, here's a simple document that has necessary
substitutions embedded:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
$ cat convertme.txt
#date#

Dear #name#, I wanted to start by again thanking you for your
generous donation of #amount# in #month#. We couldn't do our
work without support from humans like you, #first#.

This year we're looking at some unexpected expenses,
particularly in Sector 5, which encompasses #state#, as you
know. I'm hoping you can start the year with an additional
contribution? Even #suggested# would be tremendously helpful.

Thanks for your ongoing support. With regards,

Rick Deckard
Society for the Prevention of Cruelty to Replicants
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
Scan through it, and you'll see there's a lot of substitutions to do:
&lt;code&gt;#date#&lt;/code&gt;, &lt;code&gt;#name#&lt;/code&gt;,
&lt;code&gt;#amount#&lt;/code&gt;, &lt;code&gt;#month#&lt;/code&gt;, &lt;code&gt;#first#&lt;/code&gt;,
&lt;code&gt;#state#&lt;/code&gt; and &lt;code&gt;#suggested#&lt;/code&gt;. It turns out that
&lt;code&gt;#date#&lt;/code&gt; will
be replaced with the current date, and &lt;code&gt;#suggested#&lt;/code&gt; is one that'll be
calculated as the letter is processed, but that's for a bit later, so
stay tuned for that.
&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/fun-mail-merge-and-cool-bash-arrays" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Mon, 25 Mar 2019 11:30:00 +0000</pubDate>
    <dc:creator>Dave Taylor</dc:creator>
    <guid isPermaLink="false">1340460 at https://www.linuxjournal.com</guid>
    </item>

  </channel>
</rss>
