<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pystar</title>
    <description>The latest articles on DEV Community by Pystar (@pystar).</description>
    <link>https://dev.to/pystar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1607%2FW_R9jkoO.jpg</url>
      <title>DEV Community: Pystar</title>
      <link>https://dev.to/pystar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pystar"/>
    <language>en</language>
    <item>
      <title>How to name your default git branch "bananas"</title>
      <dc:creator>Pystar</dc:creator>
      <pubDate>Tue, 29 Dec 2020 13:04:59 +0000</pubDate>
      <link>https://dev.to/pystar/how-to-name-your-default-git-branch-bananas-7bc</link>
      <guid>https://dev.to/pystar/how-to-name-your-default-git-branch-bananas-7bc</guid>
      <description>&lt;p&gt;Branch names are not special. They are just references to commits. If you want you can name your branch "bananas" or any other name if you want. Git in its simplest form only deals with blobs, trees, sub-trees, and commit objects.&lt;/p&gt;

&lt;p&gt;Let's walk through an example of creating a manual commit to show how you can give a default branch any name:&lt;/p&gt;

&lt;p&gt;You are in a working directory and have added a file to the Index using the "git add" command once you do that, git inserts your file into a blob (binary large object). This blob isn’t referenced by a tree and only exists at the .git/Index file. You can confirm this by checking the .git directory, it holds a lot of information normally ignored by casual git users.&lt;/p&gt;

&lt;p&gt;Now let's record the contents of the index in a tree:&lt;/p&gt;

&lt;p&gt;$ git write-tree&lt;/p&gt;

&lt;p&gt;It will return a 40 character hash-id that for example looks like 17e2de4, I truncated it for brevity.&lt;/p&gt;

&lt;p&gt;Now manually make a new commit object by using this tree directly:&lt;/p&gt;

&lt;p&gt;$ echo "Initial commit" | git commit-tree 17e2de4&lt;/p&gt;

&lt;p&gt;A commit -id is returned from that command example cce612&lt;/p&gt;

&lt;p&gt;Now you have to register the commit as the new head of the current branch:&lt;/p&gt;

&lt;p&gt;$ echo &amp;lt;40 character commit id from the commit-tree above &amp;gt; .git/refs/heads/master&lt;/p&gt;

&lt;p&gt;Remember I said, branches are just names or references to commit objects? so the above command can look like this:&lt;/p&gt;

&lt;p&gt;$ echo &amp;lt;40 character commit id from the commit-tree above &amp;gt; .git/refs/heads/bananas&lt;/p&gt;

&lt;p&gt;please note that I have swapped "master" for "bananas".&lt;/p&gt;

&lt;p&gt;Now after creating the "bananas" branch, we must associate our working tree with it:&lt;/p&gt;

&lt;p&gt;$ git symbolic-ref HEAD refs/heads/bananas&lt;/p&gt;

&lt;p&gt;You can now run git log to verify that your operation was successful. Casual git users never have to do this because git does it under the hood once you issue the git add, git commit commands respectively.&lt;/p&gt;

&lt;p&gt;Git commands like "git write-tree and commit-tree" etc are called PLUMBING COMMANDS which commands like "git add, commit" etc are PORCELAIN COMMANDS.&lt;/p&gt;

&lt;p&gt;If you are interested in learning more git concepts like this and how git works under the hood, you can check here &lt;a href="https://gumroad.com/l/SziQL"&gt;Learn Git Visually&lt;/a&gt;. Disclosure: I wrote it, so shameless plug :-)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Build a Native UI Android App with Python and BeeWare.</title>
      <dc:creator>Pystar</dc:creator>
      <pubDate>Mon, 20 Jul 2020 04:44:58 +0000</pubDate>
      <link>https://dev.to/pystar/build-a-native-ui-android-app-with-python-and-beeware-16me</link>
      <guid>https://dev.to/pystar/build-a-native-ui-android-app-with-python-and-beeware-16me</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fedqn9z451oxw6dmw2p7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fedqn9z451oxw6dmw2p7w.png" alt="Alt Text" width="382" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://beeware.org" rel="noopener noreferrer"&gt;BeeWare&lt;/a&gt; is a suite of &lt;a href="https://beeware.org/project/projects/tools/" rel="noopener noreferrer"&gt;tools and libraries&lt;/a&gt; that allows you to write native UI applications in Python and with one codebase, release it on multiple platforms like iOS, Android, Windows, MacOS, Linux, Web, and tvOS.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Java applications are "Write once, run anywhere".&lt;br&gt;
BeeWare applications are "Write once, deploy everywhere".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since I have zero experience creating mobile apps for iOS or Android, let's find out how quickly I can spin up an Android mobile app with BeeWare. &lt;/p&gt;

&lt;p&gt;In this tutorial, we will be building a simple weather app. And before we start, let's install all the required dependencies.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: I use a Linux distro based on Ubuntu (Ubuntu 18.04) so my instructions will be thus targeted.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install &lt;a href="https://www.python.org/downloads/" rel="noopener noreferrer"&gt;Python&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Run this in your terminal to install the dependencies.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get update
$ sudo apt-get install git python3-dev python3-venv libgirepository1.0-dev libcairo2-dev libpango1.0-dev libwebkit2gtk-4.0-37 gir1.2-webkit2-4.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create and activate a virtual environment
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir weatherman
$ cd weatherman
$ python3 -m virtualenv .venv
$source .venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install &lt;a href="https://beeware.org/project/projects/tools/briefcase/" rel="noopener noreferrer"&gt;briefcase&lt;/a&gt;. In your activated virtual environment, run this in the terminal:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install briefcase
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install &lt;a href="https://github.com/encode/httpx" rel="noopener noreferrer"&gt;httpx&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install httpx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create a skeleton app with Briefcase&lt;/strong&gt;&lt;br&gt;
Still in your terminal, run the command below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ briefcase new
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Briefcase will ask you a set of questions and use those answers to set up your application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Let's build a new Briefcase app!


First, we need a formal name for your application. This is the name that will
be displayed to humans whenever the name of the application is displayed. It
can have spaces and punctuation if you like, and any capitalization will be
used as you type it.

Formal Name [Hello World]: weatherman 

Next, we need a name that can serve as a machine-readable Python package name
for your application. This name must be PEP508-compliant - that means the name
may only contain letters, numbers, hyphens, and underscores; it can't contain
spaces or punctuation, and it can't start with a hyphen or underscore.

Based on your formal name, we suggest an app name of 'weatherman',
but you can use another name if you want.

App Name [weatherman]: weatherman

Now we need a bundle identifier for your application. App stores need to
protect against having multiple applications with the same name; the bundle
identifier is the namespace they use to identify applications that come from
you. The bundle identifier is usually the domain name of your company or
project, in reverse order.

For example, if you are writing an application for Example Corp, whose website
is example.com, your bundle would be ``com.example``. The bundle will be
combined with your application's machine readable name to form a complete
application identifier (e.g., com.example.notepad).

Bundle Identifier [com.example]: com.exampe.notepad

Briefcase can manage projects that contain multiple applications, so we need a
Project name. If you're only planning to have one application in this
project, you can use the formal name as the project name.

Project Name [weatherman]: weatherman

Now, we need a one line description for your application.

Description [My first application]: A Simple Android weather app.

Who do you want to be credited as the author of this application? This could be
your own name, or the name of your company you work for.

Author [Jane Developer]: Pystar

What email address should people use to contact the developers of this
application? This might be your own email address, or a generic contact address
you set up specifically for this application.

Author's Email [pystar@notepad.exampe.com]: dev@gmail.com

What is the website URL for this application? If you don't have a website set
up yet, you can put in a dummy URL.

Application URL [https://weatherman.exampe.com/weatherman]:      

What license do you want to use for this project's code?

Select one of the following:

    [1] BSD license
    [2] MIT license
    [3] Apache Software License
    [4] GNU General Public License v2 (GPLv2)
    [5] GNU General Public License v2 or later (GPLv2+)
    [6] GNU General Public License v3 (GPLv3)
    [7] GNU General Public License v3 or later (GPLv3+)
    [8] Proprietary
    [9] Other

Project License [1]: 2

What GUI toolkit do you want to use for this project?

Select one of the following:

    [1] Toga
    [2] PySide2
    [3] PursuedPyBear
    [4] None

GUI Framework [1]: 1

Generating a new application 'weatherman'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Application 'weatherman' has been generated. To run your application, type in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd weatherman
$ briefcase dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;note: the look of your app is determined by your windows manager&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The app will open up in a new window and on my Linux + i3wm system, the app looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhriiywf0u6bn7j3bgwqu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhriiywf0u6bn7j3bgwqu.png" alt="Alt Text" width="640" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The app right now pretty much does nothing so lets add some logic to it, however before we start, we need to understand how the &lt;a href="https://toga.readthedocs.io/en/latest/index.html" rel="noopener noreferrer"&gt;BeeWare Widget system&lt;/a&gt; works.&lt;/p&gt;

&lt;p&gt;A BeeWare app is a toga.App instance and is a collection of boxes and widgets. A box can contain other nested boxes and widgets.&lt;br&gt;
A widget is similar to an html widget and examples of BeeWare widgets include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Buttons&lt;/li&gt;
&lt;li&gt;Labels&lt;/li&gt;
&lt;li&gt;TextInput&lt;/li&gt;
&lt;li&gt;PasswordInput
(See more)[&lt;a href="https://toga.readthedocs.io/en/latest/reference/api/widgets/index.html" rel="noopener noreferrer"&gt;https://toga.readthedocs.io/en/latest/reference/api/widgets/index.html&lt;/a&gt;]&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You apply styling to boxes to determine how the app will look like. A box can either be a COLUMN i.e. it takes up the entire viewport from top to bottom and extends its height based on its content, also it can also be a ROW to span the viewport horizontally from left to right.&lt;/p&gt;

&lt;p&gt;Lets add some logic to the skeleton app to make it do something useful. You will have to create an account on &lt;a href="https://weatherstack.com/signup/free" rel="noopener noreferrer"&gt;Weatherstack&lt;/a&gt; and get a TOKEN, your app will need it to make HTTP calls to the weather API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"""
A simple weather app.
"""
from pprint import pprint
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW, LEFT, RIGHT
import httpx

TOKEN = "" #Enter your weatherstack.com token here.

BASE_URL="http://api.weatherstack.com/current"

class weatherman(toga.App):

    def startup(self):
        """
        Construct and show the Toga application.

        Usually, you would add your application to a main content box.
        We then create a main window (with a name matching the app), and
        show the main window.
        """
        main_box = toga.Box(style=Pack(direction=COLUMN))

        location_label = toga.Label("Location", style=Pack(padding=(0, 5)))
        self.location_input = toga.TextInput(style=Pack(flex=1))

        location_box = toga.Box(style=Pack(direction=ROW, padding=5))
        location_box.add(location_label)
        location_box.add(self.location_input)

        weather_box_label = toga.Label("Weather Results", style=Pack(padding=(0, 5)))
        self.weather_box_input = toga.TextInput(readonly=True, style=Pack(flex=1))
        weather_box = toga.Box(style=Pack(direction=ROW, padding=5))
        weather_box.add(weather_box_label)
        weather_box.add(self.weather_box_input)

        button = toga.Button("Fetch weather", on_press=self.weather, style=Pack(padding=5))

        main_box.add(location_box)
        main_box.add(button)
        main_box.add(weather_box)

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

    def weather(self, widget):
        params = dict(access_key=TOKEN, query=self.location_input.value)
        resp = httpx.get(BASE_URL, params=params).json()
        try:
            self.weather_box_input.value = f'The weather report for  {resp["request"]["query"]} \
            {resp["current"]["temperature"]}  \
            {resp["current"]["weather_descriptions"][0]} \
            {resp["current"]["feelslike"]}'
        except ValueError:
            self.weather_box_input.value = "Location unknown!"



def main():
    return weatherman()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's package the app we just created for distribution. In your activated shell run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;briefcase create
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For your app to be executable on your targeted platform (in our case Android) it needs to be compiled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;briefcase build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To find out if our compilation was successful, you can run it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;briefcase run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This runs the compiled app and not the python code we have been working with. If your app executes successfully then that means the prior compilation step was successful.&lt;/p&gt;

&lt;p&gt;Now package your application for distribution, using the package command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;briefcase package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compile the App for Android:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;briefcase create android
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compile the app into an Android APK app file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;briefcase build android
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run the newly created APK app file, you can run it in:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A virtual emulator device&lt;/li&gt;
&lt;li&gt;On a physical device&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will dive deep into running the APK in part 2 of this tutorial.&lt;br&gt;
I hope your interest has been piqued into how to create mobile apps with pure Python. If you have any questions, I will be happy to answer them.&lt;/p&gt;

&lt;p&gt;!&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fedqn9z451oxw6dmw2p7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fedqn9z451oxw6dmw2p7w.png" alt="Alt Text" width="382" height="247"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fusi8jrqof4ybqourmx5b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fusi8jrqof4ybqourmx5b.png" alt="Alt Text" width="522" height="928"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;TL:DR: BeeWare Commands&lt;/strong&gt;&lt;br&gt;
briefcase new - create a new skeletal BeeWare app&lt;br&gt;
briefcase dev - run the BeeWare app in development&lt;br&gt;
briefcase create - start to package the app for distribution&lt;br&gt;
briefcase build - compile the app&lt;br&gt;
briefcase run - run the compiled app&lt;br&gt;
briefcase package - package the compiled app &lt;br&gt;
briefcase create android&lt;br&gt;
briefcase build android&lt;br&gt;
briefcase run android&lt;/p&gt;

</description>
      <category>python</category>
      <category>beeware</category>
      <category>android</category>
      <category>ios</category>
    </item>
    <item>
      <title>Learn Shell Scripting in 5 Minutes</title>
      <dc:creator>Pystar</dc:creator>
      <pubDate>Fri, 17 Jul 2020 00:24:04 +0000</pubDate>
      <link>https://dev.to/pystar/learn-shell-scripting-in-5-minutes-5fk4</link>
      <guid>https://dev.to/pystar/learn-shell-scripting-in-5-minutes-5fk4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Getting Started&lt;/li&gt;
&lt;li&gt;Comments&lt;/li&gt;
&lt;li&gt;Variables&lt;/li&gt;
&lt;li&gt;Loops&lt;/li&gt;
&lt;li&gt;Conditionals&lt;/li&gt;
&lt;li&gt;Case&lt;/li&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;li&gt;User Input&lt;/li&gt;
&lt;li&gt;Arithmetic&lt;/li&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;li&gt;Debugging Shell scripts&lt;/li&gt;
&lt;li&gt;Tips and Tools&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a text file called &lt;em&gt;script&lt;/em&gt; with a &lt;em&gt;.sh&lt;/em&gt; file extension (not required) and add the contents below to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
echo "Hello, World!"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You have to make the shell script you just created executable by running this:&lt;br&gt;
&lt;code&gt;sudo chmod +x script.sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you can run the script to see the message printed to the screen with &lt;br&gt;
&lt;code&gt;source script.sh&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, world!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Comments&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Like all other programming languages, you can add comments in your scripts to describe exactly what your script does. Bash scripts support single line and multi-line comments.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Single line comments&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
# This is a single line comment
echo "Hello, world!"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Multi-line comments&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
: '
This is a multi-line comment
spanning not just one line
but many lines
'
echo "Hello, world!"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Variables&lt;/strong&gt;&lt;br&gt;
There are two types of variables in Bash,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User-defined variables: are variables defined by a user in the shell script and are usually in all lowercase.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
message="Shell scripting tutorial"
echo $message
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Shell defined variables: are variables defined by the Bash shell and are usually in all uppercase.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
echo $BASH_VERSION
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;There are a number of builtin Bash variables:&lt;/p&gt;

&lt;p&gt;$$ - PID of the current shell&lt;br&gt;
$! - PID of last background command&lt;br&gt;
$? - Exit status of the last command&lt;br&gt;
$0 - Name of the current command&lt;br&gt;
$1 - Name of the current command's first parameter&lt;br&gt;
$9 - Name of current command's ninth parameter&lt;br&gt;
$@ - All of the current command's parameters (preserving whitespace and quoting)&lt;br&gt;
$* - All of the current command's parameters (not preserving whitespace and quoting)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Formatting Variables&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# The string 'Not defined' is printed if the variable "message" has not been defined.
echo ${message:-"Not defined"}

# The string "Not defined but now defined and assigned", if the variable did not exist prior.
echo ${message:="Not defined but now defined and assigned"}


# The string "Error: the variable does not exist" is printed if you try to print a variable does not exist.
echo ${message:?"Error: the variable does not exist"}

var1='shell scripting'
var2='BASH SCRIPTING'
echo ${var1^^} # Print a variable in all uppercase.
echo ${var1^} # Convert the first letter to uppercase.
echo ${var2,,} # Print a variable in all lowercase.
echo ${var2,} # Convert the first letter to lowercase.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Loops&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;C-style for loops&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

for ((i=0; i&amp;lt;10; i++))
do
    echo $i
done
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Python style for loops&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

for number in "1 2 3 4 5"
do
    echo $number
done
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;While loops&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash


count=0
while [[ $count -lt 10 ]]
do
    echo $count
    (( count++ ))
done
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Until loops&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

count=10
until [[ $count -eq 0 ]]
do
    echo $count
    (( count-- ))
done
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Select loops&lt;br&gt;
You can use select loops to create menu type interfaces to shell scripts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

options="Yes No Maybe"
ps3="Select your option" # Customize your menu selection.

select option in $options
do
    if [[ $option == "Yes" ]]
    then
        echo "You chose Yes"
    elif [[ $option == "No" ]]
    then
        echo "You chose No"
        exit
    elif [[ $option == "Maybe" ]]
    then
        echo "You chose Maybe"
    else
        echo "Invalid option chosen"
        exit
    fi
done
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;&lt;em&gt;Conditionals&lt;/em&gt;&lt;br&gt;
Bash supports the usual &lt;strong&gt;if, elif and else&lt;/strong&gt; conditional operators of other programming languages.&lt;/p&gt;

&lt;p&gt;Supported Bash comparison operators are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Integer comparison operators&lt;/li&gt;
&lt;li&gt;String comparison operators&lt;/li&gt;
&lt;li&gt;Compound comparison operators&lt;/li&gt;
&lt;li&gt;File comparison operators&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Integer comparison operators
&lt;/h4&gt;

&lt;p&gt;-eq is equal to&lt;/p&gt;

&lt;p&gt;-ne is not equal to&lt;/p&gt;

&lt;p&gt;-gt is greater than&lt;/p&gt;

&lt;p&gt;-ge is greater than or equal to&lt;/p&gt;

&lt;p&gt;-lt is less than&lt;/p&gt;

&lt;p&gt;-le is less than or equal to&lt;/p&gt;

&lt;p&gt;&amp;lt;   is less than – place within double parentheses&lt;/p&gt;

&lt;p&gt;&amp;lt;=  is less than or equal to&lt;/p&gt;

&lt;p&gt;&amp;gt;  is greater than&lt;/p&gt;

&lt;p&gt;&amp;gt;= is greater than or equal to&lt;/p&gt;
&lt;h4&gt;
  
  
  String comparison operators
&lt;/h4&gt;

&lt;p&gt;=   is equal to&lt;/p&gt;

&lt;p&gt;==  is equal to&lt;/p&gt;

&lt;p&gt;=   is not equal to&lt;/p&gt;

&lt;p&gt;&amp;lt;  is less than ASCII alphabetical order&lt;/p&gt;

&lt;p&gt;&amp;gt;  is greater than ASCII alphabetical order&lt;/p&gt;

&lt;p&gt;-z  string is null (i.e. zero length)&lt;/p&gt;

&lt;p&gt;-n  string is not null (i.e. !zero length)&lt;/p&gt;
&lt;h4&gt;
  
  
  Compound comparison operators
&lt;/h4&gt;

&lt;p&gt;-a  logical and&lt;br&gt;
-o  logical or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

a=1
b=2

if [[ $a -gt $b ]]
then
    echo $a is greater than $b
elif [[ $a -lt $b ]]
then
    echo $a is less than $b
elif [[ $a -eq $b ]]
then
    echo $a is equal to $b
else
    echo "unknown condition"
fi


[[ -e "$file" ]] # True if file exists
[[ -d "$file" ]] # True if file exists and is a directory
[[ -f "$file" ]] # True if file exists and is a regular file
[[ -z "$str" ]]  # True if string is of length zero
[[ -n "$str" ]]  # True is string is not of length zero
-r file has read permission
-w file has write permission
-x file has execute permission
file1 -nt file2 file file1 is newer than file2
file1 -ot file2 file file1 is older than file2
file1 -ef file2 files file1 and file2 are hard links to the same file

[[ ... ]] &amp;amp;&amp;amp; [[ ... ]] # And
[[ ... ]] || [[ ... ]] # Or
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Case&lt;/strong&gt;&lt;br&gt;
A Case statement can simplify and replace a collection of if elif else statements and it can also be used with the Select statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
read -p "Make your choice: " option
case $option in
    "Yes")
    echo "You chose 'Yes'";;
    "Maybe")
    echo "You chose 'Maye'";;
    "No")
    echo "You chose 'No'";;
    *) # default option
    echo "Unknown option";;
esac

# Create a Menu system with Select and Case

options="Yes No Maybe"
select option in $options
do
    case $option in
    "Yes")
    echo "You chose 'Yes'";;
    "Maybe")
    echo "You chose 'Maye'";;
    "No")
    echo "You chose 'No'"
    exit;;
    *) # default option
    echo "Unknown option";;
   esac
done
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Functions&lt;/strong&gt;&lt;br&gt;
Functions in Bash work like in regular programming languages, the only difference is the invocation i.e. you execute a function by typing the name only with no brackets. Also, Bash functions do not have a return value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

# Define a simple function
function greeter() {
    echo "Hello, world!"
}

# Define a function that accepts arguments
function greeter1() {
    echo $1
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Call the function by running &lt;br&gt;
&lt;code&gt;greeter&lt;/code&gt; to display:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, World!"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And call the function by passing in arguments&lt;br&gt;
&lt;code&gt;greeter Hello, world&lt;/code&gt; to display:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, World!"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;User input&lt;/strong&gt;&lt;br&gt;
You can accept user input with Bash with the &lt;em&gt;read&lt;/em&gt; keyword&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
read var # The user inputted value is saved in a variable called *var*

read -p "Enter your name" name # A prompt "Enter your name" is displayed to the user and the supplied value is saved in the variable "name".

read -s -p "Enter your password" password # "This hides the user inputted value. Useful for password inputs.

read -p "Enter all your names" -a names # If you want to accept more than one value from users. It creates an Array object called *names*
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Arithmetic&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

a=1
b=2
$((a + b)) # Addition
$((a - b)) # Subtraction
$((a * b)) # Multiplication
$((a / b)) # Division
$((a % b)) # Modulus
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you want to do advanced arithmetic like dealing with floats, finding square roots of numbers, etc, you should use the inbuilt &lt;em&gt;bc&lt;/em&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
echo "scale=2; sqrt((49)) | bc -l # scale determines the number of decimal places.
echo "scale=3; 3.142 * 4.2" | bc -l
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Arrays&lt;/strong&gt;&lt;br&gt;
In Bash, there are two kinds of variables&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Scalar variables (contains a single value)&lt;/li&gt;
&lt;li&gt;Arrays variables (contains multiple values)
Bash arrays are Zero-based arrays i.e. start from position zero.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

names=(Jack John Jill Scot)
${names[@]} # This prints all the variables
${#names[@]} # Counts the number of items in the *names* array.
${!names[@]} # Returns the indexes of the items in the array.
unset names[&amp;lt;index&amp;gt;] # Removes the item in the array at the specified index.
${names[0]} # Returns the first item of the array.
${names[-1]} # Returns the last item of the array.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Tips, Tricks, and Tools&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use shellcheck.net to find bugs in your shell scripts.&lt;/li&gt;
&lt;li&gt;You can debug your shell scripts running it like
&lt;code&gt;bash -x script.sh&lt;/code&gt;. This runs the script in debug mode.&lt;/li&gt;
&lt;li&gt;You can set breakpoints in your shell scripts
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

set -x
message="This is a shell scripting tutorial"
echo $message
set +x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So we have come to the end of this 5-minute introduction to Bash shell scripting. I hope it will start you on your journey to more mastery.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>terminal</category>
      <category>shell</category>
      <category>scripting</category>
    </item>
    <item>
      <title>Deep Dive into Python Memory Management</title>
      <dc:creator>Pystar</dc:creator>
      <pubDate>Thu, 14 Nov 2019 21:49:10 +0000</pubDate>
      <link>https://dev.to/pystar/deep-dive-into-python-memory-management-39cm</link>
      <guid>https://dev.to/pystar/deep-dive-into-python-memory-management-39cm</guid>
      <description>&lt;p&gt;Deep Dive Into Python Memory Management&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxp74xklba6yvhu5u8ek8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxp74xklba6yvhu5u8ek8.jpg" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Introduction&lt;br&gt;
In this article, we shall dive deep into the internals of Python to discover how it handles memory management and object creation.&lt;br&gt;
Memory management is the process of allocating and de-allocating memory for creation of objects and in Python, the Python memory manager automatically handles this process under the hood by running periodically to allocate and de-allocate memory when its no longer needed. The memory manager knows when to de-allocate memory used by objects through the process of Reference Counting i.e. the memory manager keeps track of all objects created and the number of variables referring to each memory location, once the reference count of any memory location containing an object gets down to zero, the memory space is destroyed and freed up for use.&lt;br&gt;
    Lets talk about how objects are created in Python: when you assign a value to a variable in Python, what actually goes on under the hood is that a memory location (imagine a box) is created and a value e.g. an integer or a string is created at that memory location (put inside the box) and the memory address is then assigned to the variable (imagine a label being slapped onto the side of the box). It is this memory location a variable references whenever it needs to fetch the value of an object.&lt;/p&gt;

&lt;p&gt;Dynamic Typing&lt;br&gt;
In Python, the type and memory space for an object are determined and allocated at run-time. On creation, the interpreter creates an object whose type is dictated by the syntax that is used for the operand on the right-hand side of an assignment. After the object is created, a reference to that object is assigned to the variable on the left-hand side of the assignment.&lt;/p&gt;

&lt;p&gt;Garbage Collection and Reference Counting&lt;br&gt;
Earlier in the introduction, we mentioned how the Python memory manager creates and destroys objects, well this is done with the garbage collector which works in conjunction with the memory manager and the reference counter. This process is called Garbage Collection and this process runs as your program is running and goes into action as soon as the reference count of a memory location is incremented i.e. a variable references a memory location and decremented i.e. when the reference to an object is reassigned or when the object is explicitly deleted. Python’s garbage collector is actually a combination of reference counting and the periodic invocation of a cyclic garbage collector. When an object’s refcount reaches zero, the interpreter pauses to deallocate it and all objects that were reachable only from that object. In addition to this reference counting, the garbage collector also notices if a large number of objects have been allocated (and not deallocated though reference counting). In such cases, the interpreter will pause to try to clear out any unreferenced cycles.&lt;/p&gt;

&lt;p&gt;Lets write some code to explain it more in detail:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Faz8lb4ec8gdxi89yaclf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Faz8lb4ec8gdxi89yaclf.png" alt="Reference counting explainer image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Explainer: Under the hood, the memory manager created a memory location, created a string object with a value of “Python Rocks!”, slotted it in that memory location and assigned the memory location to the variable name ‘message’. When we tried to print the message variable with the built in print function, the memory manager retrieved the value of the string located at the memory location the variable message referenced and printed it to the screen. When the message variable was deleted with del, the reference count was decremented and since only one variable was referencing that memory location, as soon as it was deleted, the reference count fell to zero and the garbage collector instantly came into play by de-allocating and freeing up that memory location thereby destroying the string object. This is why subsequent print calls to that variable returned a NameError because we were trying to access a location that no longer existed. The beautiful thing about this is that the whole process is automated and developers do not need to get their hands dirty by handling memory manually.&lt;/p&gt;

&lt;p&gt;Although Python handles memory automatically, there are situations where you have to garbage collect objects manually. Example: we know that the Python interpreter keeps track of references to objects in code but there are situations where you have circular references in code i.e. when two variables refer to each other so the reference count never falls to zero so the memory allocated for these objects never gets de-allocated and this could lead to a memory leak. Therefore to solve this reference cycle problem, we have to garbage collect manually using the gc module. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5ft8scrhcdkq8ohu692l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5ft8scrhcdkq8ohu692l.png" alt="Garbage collection explainer image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result variable contains the number of objects it has collected and de-allocated in memory.&lt;/p&gt;

&lt;p&gt;The del statement&lt;br&gt;
The del statement removes a single reference to an object. Its syntax is:&lt;br&gt;
del obj1 … objN&lt;br&gt;
Running the del statement will:&lt;br&gt;
Remove a variable name from current namespace&lt;br&gt;
Lower reference count to the reference (by one)&lt;br&gt;
The del statement does not free variables as in C, it simply says that you no longer need it.&lt;/p&gt;

&lt;p&gt;Explainer:&lt;br&gt;
The del statement&lt;br&gt;
del_stmt ::= “del” target_list&lt;/p&gt;

&lt;p&gt;Deletion is recursively defined very similar to the way assignment is defined. Rather than spelling it out in full details, here are some hints.&lt;/p&gt;

&lt;p&gt;Deletion of a target list recursively deletes each target, from left to right. Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a global statement in the same code block. If the nane is unbound, a NameError exception will be raised. &lt;br&gt;
Deletion of attribute references, subscriptions and slicing’s is passed to the primary object involved; deletion of a slicing is in general equivalent to assignment of an empty slice of the right type (but even this is determined by the sliced object).&lt;/p&gt;

&lt;p&gt;Kindly note that there is no mention of freeing memory. What happens is that you tell Python that it can do whatever it wants with that memory. In this case your Python implementation stores the memory for later use in a memory cache. This allows Python to run faster by not needing to allocate as much memory later.&lt;/p&gt;

&lt;p&gt;Interning&lt;br&gt;
Interning is the process by which objects are reused on-demand by the memory manager. As startup, the Cpython interpreter, pre-loads a global list of integers in the range [-5, 256]. So anytime a variable references a number within that range, the interpreter will use a cached version of that integer object in memory.&lt;br&gt;
This is why: &lt;/p&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;hex(id(a)) == hex(id(b)) → True&lt;br&gt;
where a and b are both referencing a number which falls within the preloaded range of integers and &lt;br&gt;
hex(id(a)) == hex(id(b)) → False&lt;br&gt;
When a and b are both referencing a number which falls outside of the preloaded range of integers. The Python memory manager not only interns integers, it also interns strings that look like variables i.e. short strings like ‘Hello’ and long strings that have no spaces in-between each word that make up the string e.g. “Hello_world_program”.&lt;br&gt;
You can manually intern a long string if it does not have the normal characteristics of a string that the memory manager would normally intern. You can do this by:&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;
&lt;br&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fba3vbf4048aracrk4vyn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fba3vbf4048aracrk4vyn.png" alt="Manual interning explainer image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is the use of interning you might ask? Well, in normal day to day programming, you would hardly ever be required to go that low but if you are building for example, a tokenizer or some program that would be working with a large number of strings, then interning would greatly speed up your code.&lt;/p&gt;

&lt;p&gt;The Python Global Interpreter Lock (GIL)&lt;br&gt;
The python GIL is a lock that allows only one thread to control the Python interpreter at any given time i.e. only one thread can be executed at once. What exactly does this do you might ask? Well lets return to our explanation of how reference counting in Python is executed; reference count of an object is incremented when:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is assigned to a variable&lt;/li&gt;
&lt;li&gt;Passed as an argument during a function invocation&lt;/li&gt;
&lt;li&gt;Passed as an object in an iterable i.e. a list.
The reference count of an object is also decremented when it is:&lt;/li&gt;
&lt;li&gt;Explicitly deleted with the del statement&lt;/li&gt;
&lt;li&gt;The variable is assigned another memory location&lt;/li&gt;
&lt;li&gt;The variable goes out of scope i.e. after the function has finished running.
Now imagine if the Python GIL did not exist? That would mean more than one thread can be executed in a Python program that means there might be cases where two or more threads might want to increment or decrement the reference count of an object simultaneously which if it happens would lead to either memory leaks due to memory that never gets released or where the memory is released even when the reference count is not zero i.e. a variable still references it. This phenomenon is called a Race Condition and invariables leads to unexpected behavior in code i.e. bugs.
So the reference count can be kept safe by adding locks to all data structures that are shared across threads so that they are not modified inconsistently. However, adding a lock to each object or groups of objects means multiple locks will exist which can cause another problem – Deadlocks. And deadlocks can only happen if there is more than one lock on an object). Also, another side effect would be decreased performance caused by the repeated acquisition and release of locks.
The GIL is a single lock on the interpreter itself which adds a rule that execution of any Python bytecode requires acquiring the interpreter lock. This prevents deadlocks (as there is only one lock) and does not introduce much performance overhead. The only disadvantage is that it makes CPU-bound programs single threaded.
In the end we have been able to see how Python manages memory and object creation with the garbage collector, reference counting and the GIL.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Appendix&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Refcount (Reference Counting) - In computer science, reference counting is a programming technique of storing the number of references, pointers, or handles to a resource, such as an object, a block of memory, disk space, and others. In garbage collection algorithms, reference counts may be used to deallocate objects which are no longer needed. &lt;a href="https://en.wikipedia.org/wiki/Reference_counting" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Reference_counting&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Memory Leak - In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code. &lt;a href="https://en.wikipedia.org/wiki/Memory_leak" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Memory_leak&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deadlock - In concurrent computing, a deadlock is a state in which each member of a group is waiting for another member, including itself, to take action, such as sending a message or more commonly releasing a lock. &lt;a href="https://en.wikipedia.org/wiki/Deadlock" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Deadlock&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>memorymanagement</category>
      <category>referencecounter</category>
      <category>interning</category>
    </item>
    <item>
      <title>The Shiny New Parts of Python.</title>
      <dc:creator>Pystar</dc:creator>
      <pubDate>Sun, 10 Nov 2019 09:53:43 +0000</pubDate>
      <link>https://dev.to/pystar/the-shiny-new-parts-of-python-301j</link>
      <guid>https://dev.to/pystar/the-shiny-new-parts-of-python-301j</guid>
      <description>&lt;p&gt;This series is about the latest additions to the Python language which at the time of writing is Python 3.8. To kick start the series, this post will focus on &lt;b&gt;Positional-only parameters.&lt;/b&gt;&lt;br&gt;
Prior to Python 3.8, some inbuilt functions allowed for positional-only arguments in functions but this feature could not be used in code by developers. The addition of the ability to specify positional-only arguments in function definitions is a new enhancement to the Python language syntax and is based on [PEP 570] &lt;a href="https://www.python.org/dev/peps/pep-0570/"&gt;https://www.python.org/dev/peps/pep-0570/&lt;/a&gt;). &lt;br&gt;
Prior to Python 3.8, a function definition looked like:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;but now with the addition of positional-only arguments, a function definition looks like:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Explainer&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The '/' marker specifies that all parameters to its left can only be passed by position during the function call as against the normal position-or-keyword argument passing. Its then followed by positional_or_keyword_parameters and a '*' marker which specifies the end of all positional parameters and then followed by keyword_only_parameters.&lt;/li&gt;
&lt;li&gt;If '/' is not specified in the function definition, that function does not accept any positional-only arguments.&lt;/li&gt;
&lt;li&gt;Once a positional-only parameter is specified with a default, the following positional-only and positional-or-keyword parameters need to have defaults as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Playground&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_asciinema"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Advantages of positional-only parameters&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Speed and performance: The parsing and handling of positional-only parameters is faster.&lt;/li&gt;
&lt;li&gt;Logical ordering: positional-only arguments enforces the order in which arguments are mapped to parameters in the the function definition during a function call.&lt;/li&gt;
&lt;li&gt;Better API design. The new syntax will enable library authors to further control how their API can be called. It will allow designating which parameters must be called as positional-only, while preventing them from being called as keyword arguments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When and where can you use positional-only parameters?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use positional-only if names do not matter or have no meaning, and there are only a few arguments which will always be passed in the same order.&lt;/li&gt;
&lt;li&gt;Use keyword-only when names have meaning and the function definition is more understandable by being explicit with names.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>python3</category>
    </item>
  </channel>
</rss>
