skip to navigation
skip to content

Planet Python

Last update: January 30, 2023 04:41 AM UTC

January 29, 2023


The Python Coding Blog

The Python Coding Place Is Coming Soon…

Allow me a rare blog post which is not about some Python topic or a step-by-step tutorial. We’re excited that The Python Coding Place is coming soon. As the name says, this will be the place to learn to code in Python.

In this brief post, I’ll highlight what The Place is all about

The Python Coding Place

The Python Coding Place will be a hub for various learning resources and courses. There will be brand new things as well as existing resources and products, such as The Python Coding Book you’re reading right now.

Everything on The Python Coding Place will follow the same clear and friendly style any of my students are accustomed to. Whether you’re learning through The Python Coding Book or through one of my video courses or corporate training programmes, the style will remain the same.

There are some key aspects of how I teach that are so important for me. They are integral to every product, resource, and service:

Overview Of What’s In The Python Coding Place

Let’s have a look at some of the key components in The Python Coding Place.

The Python Coding Book

Let’s start with The Python Coding Book which has gained an enthusiastic following since its publication. It has been a success because of its friendly and relaxed approach that actually explains what’s really happening in a fluent manner.

“It’s the first time I’m understanding what everything does” was the summary from one of the early readers who had struggled with other courses and textbooks in the past. “Your writing is succinct, easy to understand, and process oriented” was another comment. I love this comment as those are three objectives I focussed on while writing the book and in all my teaching! “I can’t stop reading it” was what another reader said. Although I hope they did pause for food and drink…

The Python Video Courses

Brand new online video courses are coming soon on The Python Coding Place. These will cater for all learners, whether they’re complete beginners or those looking to move on to the next phase in their programming journey.

We’ll have several different learning tracks that you can follow depending on your starting point and your learning preferences.

And more…

Whatever your needs and preferences, you’ll be able to find a learning track for you. And you’ll be able to swap between learning tracks, too.

The Python Corporate Training Program

Our corporate training program has been running for many years. The past couple of years have seen significant changes to how this is run for reasons you can all guess. We’ve combined the experience from the past couple of years with that from pre-pandemic times. This has allowed us to come up with a better format and structure to provide Python training for staff of a companies and research institutions.

The Python Coding Place offers a truly flexible and bespoke service for corporations and universities. It’s not just the content that we tailor specifically for the needs of each audience. We also offer different delivery types ranging from fully live courses to hybrid ones combining on-demand content with live sessions and workshops.

In all instances, we offer continued support to the cohorts we teach as the learning always carries on beyond the end of the formal course.

The Python Coding Forum

Whether you’re learning Python through a live course or independently through video courses, you will have questions as you learn and practise. Often, one big drawback of independent learning is that there’s no one to ask a question to when you’re stuck or when you want to find out more about a topic.

My philosophy is to enable all my students to be able to ask me questions as they learn.

This is why The Python Coding Forum will always be at the centre of all the learning resources. You can ask questions about the material you’re learning and participate in discussions about coding and Python.

The Extra Python Content

And if all this isn’t already enough, you can follow me on social media. I’m very active on several platforms and I provide bite-sized daily content aimed at all levels. Follow me on Twitter and LinkedIn for your daily dose of Python.

And make sure you also follow The Python Coding Place on Twitter and on LinkedIn.

And there are regular in-depth articles here on The Python Coding Book’s blog.


Sign up for Early Adopter status at The Python Coding Place to stay up-to-date with the latest news as we roll out our stack of services and resources.

The post The Python Coding Place Is Coming Soon… appeared first on The Python Coding Book.

January 29, 2023 10:03 PM UTC


Python Circle

How to host Django application on DigitalOcean server using Gunicorn, Supervisor, and NGINX

hosting Django application on DigitalOcean Server with custom Domain. Using WSGI server to host the Django application. Hosting Django application with Gunicorn, Supervisor, NGINX. Service static and media files of Django application using NGINX. Using Godaddy Domain to server traffic of Django application. Production deployment of Django applications.

January 29, 2023 07:40 PM UTC


David Amos

My Goldilocks Python Setup: pyenv, pipx, and pip-tools

My Goldilocks Python Setup: pyenv, pipx, and pip-tools

This article contains affiliate links. See my affiliate disclosure for more information.



Beginners should install Python with the official python.org installers. But the installer workflow isn&apost very efficient for professional developers working on multiple projects — or any project that supports multiple Python versions.

In this guide, I&aposll show you how to:


Install Python With pyenv

pyenv allows you to install multiple versions of Python and switch between them as needed. To install pyenv, run the command below that corresponds to your OS.

☝️
Note: pyenv isn&apost the only option for managing Python versions. Some developers prefer tools like asdf or homebrew. Others use their operating system&aposs native package manager.

macOS/Linux
Use the automatic installer below, or see the README for more options:

curl https://pyenv.run | bash

You&aposll need to restart your shell when you&aposre done:

exec "$SHELL"

Windows
Install with PowerShell below, or see the README for more options:

Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"
⚠️
pyenv is only compatible with Unix/Linux operating systems. Windows users must use the separately maintained pyenv-win fork. pyenv and pyenv-win share the same CLI commands, so this guide works for every OS.

Once pyenv or pyenv-win is installed, run pyenv install 3.11 to install the latest version of Python 3.11:

pyenv install 3.11

At the time of the writing, the latest version is 3.11.1.

Use a different version prefix or a specific version number to install other versions of Python. For example, the following command installs the latest version of Python 3.10:

pyenv install 3.10

To use a specific Python version that you&aposve installed, run python global:

pyenv global 3.11.1

Now your python3 command points to Python 3.11.1.

☝️
Tip: Put a text file called .python-version containing a specific Python version number in your project&aposs root directory. Whenever you cd into the project&aposs folder, pyenv will automatically switch your interpreter to the version specified in .python-version.

Install Global Utilities With pipx

pipx is a Python package installer that installs applications and command-line utilities in isolated environments and makes them available globally.

This is particularly helpful for tools like black and flake8, as they can be installed once and used across multiple projects while still taking into account project-specific configuration files.

You can install pipx with pip:

python3 -m pip install pipx

Before you can use pipx, you need to add it to PATH:

python3 -m pipx ensurepath
⚠️
Important: pipx links applications it installs to the same Python executable that was originally used to install pipx.

Consequently, when you invoke applications installed with pipx, those applications will run using the pipx Python executable. (Except for ipython, which will detect and use project-specific virtual environments.)

This is generally not an issue and may even be beneficial as it isolates tools from project dependencies. But be warned: Uninstalling the pipx Python executable renders any pipx-installed tools pointing to it unusable.

Frustratingly, you must install pipx for every version of Python that you install with pyenv.

Some of the tools I install with pipx are:

The following commands install all three tools in three separate environments:

pipx install black
pipx install flake8
pipx install ipython

Now you can use these tools as you normally would across all your projects without installing them in every project&aposs environment.

A Note For VS Code Users

If you use Python for VS Code, you&aposll need to point your flake8 path to the right location. If you don&apost, you&aposll see a pop-up that says "flake8 is not installed" every time you open VS Code.

Run pipx list to list every app installed by pipx. The application install path is displayed on the second line of output:

$ pipx list
venvs are in /Users/damos/.local/pipx/venvs
apps are exposed on your $PATH at /Users/damos/.local/bin
   package black 22.12.0, installed using Python 3.11.1
    - black
    - blackd
   package flake8 6.0.0, installed using Python 3.11.1
    - flake8
   package ipython 8.9.0, installed using Python 3.11.1
    - ipython
    - ipython3

In my case, apps are installed in /Users/damos/.local/bin. Now run which flake8 to see which executable the flake8 command points to:

$ which flake8
/Users/damos/.local/bin/flake8

Copy this path to your clipboard.

⚠️
Important: The output of which flake8 should show flake8 in the same directory that pipx uses to install applications.

If it doesn&apost, you may have previously installed flake8 with a Python version that isn&apost managed by pyenv. In this case, you&aposll need to uninstall the old version of flake8 or remove the old version of Python.

Open the VS Code settings explorer by going to File > Settings menu or pressing Cmd+, on macOS or Ctrl+, on Linux/Windows.

Type flake8 into the search bar and press Enter. Then paste the path to flake8 into the input box for the Python > Linting: Flake8 Path setting:

My Goldilocks Python Setup: pyenv, pipx, and pip-tools
☝️
Note: You may also set this in settings.json by editing the python.linting.flake8Path setting.



Manage Project Dependencies With pip-tools

I only had to use pip-tools for about 5 minutes to know I needed it all the time. pip-tools helps you where you need it to and gets out of the way everywhere else. The basic usage goes like this.

Start a new Python project in a new folder:

# Create a new folder and change directories to it
mkdir ~/my-project && cd ~/my-project

# Create a new virtual environment
python3 -m venv .venv --prompt my-project

# Activate the virtual environment
source .venv/bin/activate

# Update pip
python -m pip install -U pip

Then install pip-tools into the project&aposs virtual environment using pip:

python -m pip install pip-tools
⚠️
Important: Although it is possible to install and use pip-tools through pipx, it is not yet well supported. The docs officially recommend installing pip-tools into your project&aposs environment, and I can confirm that this is the more reliable method.

Your project&aposs dependencies go in a file called requirements.in. For example, the requirements.in file for a Django 3 project might look like this:

# ~/my-project/requirements.in

django<4.0

You can generate a requirements.txt with fully resolved dependencies using the pip-compile command:

pip-compile --allow-unsafe --resolver=backtracking requirements.in
☝️
Note: The --allow-unsafe and --resolver=backtracking options will both become the default in the next major release of pip-tools. The docs recommend passing these options to adopt the new default behavior.

The requirements.txt file generated by pip-compile will look something like this:

#
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
#    pip-compile --allow-unsafe --resolver=backtracking requirements.in
#
asgiref==3.6.0
    # via django
django==3.2.16
    # via -r requirements.in
pytz==2022.7.1
    # via django
sqlparse==0.4.3
    # via django

Notice that there is a comment at the top of the file indicating that it was autogenerated by pip-compile (it even includes the command!) and that every version number is pinned.

I usually create a second file called dev-requirements.in for development dependencies, such as pytest and django-debug-toolbar, and even pip-tools itself:

# ~/my-project/dev-requirements.in

# Use requrements.txt as a constraint file
-c requirements.txt

django-debug-toolbar
pip-tools
pytest

Using requirements.txt as a constraint file ensures that any dependencies installed for the packages in dev-requirements.in are compatible with the package versions specified in requirements.txt.

Run pip-compile a second time to generate a dev-requirements.txt file:

pip-compile --allow-unsafe --resolver=backtracking dev-requirements.in

Now for the real magic. Run pip-sync to synchronize your virtual environment with the packages in the requirements.txt and dev-requirements.txt files:

pip-sync requirements.txt dev-requirements.txt

To update or add a package, edit the appropriate .in file and then re-compile and re-sync everything.

☝️
Note: I check all of my *-requirements.in and *-requirements.txt files into version control.

I often wrap all of this into a Makefile:

install:
	@pip install -r requirements.txt -r dev-requirements.txt

compile:
	@pip-compile --allow-unsafe --resolver=backtracking requirements.in
	@pip-compile --allow-unsafe --resolver=backtracking dev-requirements.in

sync:
	@pip-sync requirements.txt dev-requirements.txt

Collaborators can run make install after cloning the repository to set up their environment, then run make compile && make sync as needed to keep everything synchronized.


Python dependency management tools abound.

I know many Python devs that swear by poetry or pipenv. And I know plenty that still use good ol&apos pip freeze. Personally, pipenv and poetry are too heavy-handed for my taste. pip freeze feels tedious to me. And the new pdm project looks interesting but still has a long way to go.

Call me Goldilocks, but pip-tools is the porridge that&aposs just right.

🙏
Acknowledgment: Much of my current Python tooling philosophy is inspired by Sebastian Witowski&aposs Modern Python Devloper&aposs Toolkit. I encourage everyone who uses Python to check it out.

Other pipx Apps To Consider

Depending on the kind of development you do, the following packages may be useful tools to install as applications with pipx:


Dig Deeper

If you write and distribute Python packages, then you need to read Dan Hillard&aposs book Publishing Python Packages.

In addition to covering how to set up, publish, maintain, and scale your package, Dane discusses setting up a professional development environment specifically for package development. It is far more versatile than the environment described in this guide.

Get instant access from Manning, or buy a print version from Amazon.

My Goldilocks Python Setup: pyenv, pipx, and pip-tools


January 29, 2023 01:00 PM UTC


John Ludhi/nbshare.io

Student T-Value Calculator

Student T-Value Calculator

Example of Input Data:

Degree of Freedom: 10
Significance Level (α): 0.05

Results

T-Value (right-tailed):
T-Value (two-tailed):

January 29, 2023 07:41 AM UTC

January 28, 2023


CodersLegacy

cx_Freeze vs Pyinstaller Comparison

In this article, we will be comparing two very popular libraries used for converting Python Code Files to standalone EXE’s (executables). This ‘cx_Freeze vs Pyinstaller’ comparison will help you understand the various pros and cons of each library, and which one you ought to be using.


What are Pyinstaller and Cx_Freeze used for?

Pyinstaller and cx_Freeze are both libraries used for creating standalone executables from Python scripts. They enable the distribution of Python applications without the need for a Python installation or specific dependencies on the target system. Both libraries have the ability to package the script, dependencies, and any necessary data files into a single executable file.

While their main goal remains the same, the method in which they achieve this goal, and other minor differences in terms of features offered, is what makes them different.


How do Pyinstaller and cx_Freeze work?

Both PyInstaller and cx_Freeze work by taking a Python script and packaging it along with its dependencies and any necessary data files into a standalone executable. They both use a similar process to accomplish this:

  1. Analyze the Python script to determine the dependencies it needs to run.
  2. Package the dependencies, along with the Python interpreter, into the executable.
  3. Create the executable file.

The main difference in how they work lies in the implementation details of these steps.

PyInstaller uses a technique called “analysis” to determine the dependencies of the script, which allows it to include only the necessary modules and not the whole python library. On the other hand, cx_Freeze uses a technique called “inclusion” which includes all the modules that the script might use. This can make cx_Freeze executables larger in size than those created by PyInstaller.


Pyinstaller vs Cx_Freeze Comparison

Here we will directly compare cx_Freeze and Pyinstaller and a variety of important metrics and benchmarks. I will also include my personal experience of working with both libraries, to help you better understand the difference.

Note: The term virtual environment will be used often here. For those who do not know, virtual environments are basically a fresh copy of your Python installation

PyinstallerCx_Freeze
PerformanceSimilar performance as regular Python code. Similar performance as regular Python code. May be slightly worse than Pyinstaller due to extra imports and increased size. Can be solved with Virtual environments.
SizeVaries greatly based on libraries included and settings. Varies greatly based on libraries included and settings. Without proper exclusion of extra modules or use of virtual environment
Cross-platform Windows, Linux, macOS.Windows, Linux, macOS.
Loading timeTakes a long time to load, especially the one-file setting. About 5 – 10 seconds for most applications. I have had cases where it takes upto 15 seconds.

(This is partly the windows firewalls fault, which doubles the load time)
Loads much more quickly than Pyinstaller. You can expect majority of applications to load in under 5 seconds.
Compile timeCompile time scales well with the number of libraries. Doesn’t take longer than 5 minutes on average.Varies greatly based on the number of libraries. Can take a few mins, to a few hours (if using big libraries).
CommunityPyinstaller is more popular and widely used than cx_Freeze. Many online resources and user generated content available. Is well known and used often in the Python community, but not as much as Pyinstaller.
Out-of-the-boxPyinstaller almost always work “out of the box”. Less need of customization or tweaking for a successful compile. Cx_Freeze is the reverse. It is almost always won’t work out of the box. It needs slight tweaks here and there to get it working. The fixes are decently documented though, and only take a minute to implement.
FeaturesPyinstaller offers more features and customization options. For example, there is a GUI interface for Pyinstaller, you can make splash screens, onefile mode, better compression support (UPX), etc. Lesser number of features and customizations.

cx_Freeze vs Pyinstaller – Conclusion

In conclusion, both cx_Freeze and PyInstaller are powerful tools for converting Python scripts into executable files. Cx_Freeze has the benefit of having a faster load time, while PyInstaller offers more features such as support for one-file executables, UPX compression, GUI interfaces, and more.

The choice between the two ultimately depends on the specific needs of the project. For projects that require a faster load time, cx_Freeze may be the better choice. On the other hand, if the project requires more advanced features (or a certain feature that cx_freeze doesn’t have), PyInstaller may be the more suitable option.


This marks the end of the cx_Freeze vs Pyinstaller comparison. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

The post cx_Freeze vs Pyinstaller Comparison appeared first on CodersLegacy.

January 28, 2023 07:41 PM UTC


Python Circle

How to create sitemap of Django website

Creating a sitemap for your Django application, Improve SEO of your Django website by generating Sitemap.xml file, Generate sitemap from Dynamic URLs in Django Application, Create Sitemap for static pages in your Django application, Sitemap XML file in Django Applications,

January 28, 2023 07:40 PM UTC


Kushal Das

Introducing Tugpgp

At Sunet, we have heavy OpenPGP usage. But, every time a new employee joins, it takes hours (and sometime days for some remote folks) to have their Yubikey + OpenPGP setup ready.

Final screen

Tugpgp is a small application built with these specific requirements for creating OpenPGP keys & uploading to Yubikeys as required in Sunet. The requirements are the following:

We have an Apple Silicon dmg and AppImage (for Ubuntu 20.04 onwards) in the release page. This is my first ever AppImage build, the application still needs pcscd running on the host system. I tested it on Debian 11, Fedora 37 with Yubikey 4 & Yubikey 5.

Oh, there is also a specific command line argument if you really want to save the private key :) But, you will have to find it yourself :).

demo gif

If you are looking for the generic all purpose application which will allow everyone of us to deal with OpenPGP keys and Yubikeys, then you should check the upcoming release of Tumpa, we have a complete redesign done there (after proper user research done by professionals).

January 28, 2023 08:32 AM UTC

January 27, 2023


Daniel Roy Greenfeld

Converting Markdown Headers to Checklist

For those times when you write out a long markdown document that you want to convert to a checklist. A future modification will be to link to the original GitHub or Notion headers so that you can click on the checklist item and jump to the original content.

Converting Markdown Headers to Checklist

Python:

response = []
with open("sample.md") as f:
    lines = f.readlines()
    for line in lines:
        if line.startswith("#"):
          indentation = line.count("#") - 1
          newline = f"{' ' * 2 * indentation}- [ ]{line.replace('#', '')}"
          response.append(newline)

with open("checklist.md", "w") as f:
    f.writelines(response)

JavaScript:

const fs = require("fs");

function MarkdownHeadersToChecklist(markdown) {
  const lines = markdown.split("\n");
  const headers = lines.filter((line) => line.startsWith("#"));
  let checklist = [];
  for (const header of headers) {
    const indentation = header.split("#").length - 1;
    const spacer = " ".repeat(2 * indentation);
    const newline = `${spacer}- [ ]${header.replace("#", "")}`;
    checklist.push(newline);
  }
  return checklist.join("\n");
}

const markdown = fs.readFileSync("sample.md", "utf8");

const checklist = MarkdownHeadersToChecklist(markdown);

fs.writeFileSync("checklist.md", checklist);

Converting Markdown Headers to Checklist

January 27, 2023 11:45 PM UTC


Python for Beginners

Select Specific Columns in Pandas Dataframe

While working with dataframes in python, we sometimes need to select specific data. For this, we need to select one or more columns that may or may not be contiguous. I have already discussed how to select multiple columns in the pandas dataframe. This article will discuss different ways to select specific columns in a pandas dataframe. 

Select Specific Columns in Pandas Dataframe Using Column Names

To select specific columns from the pandas dataframe using the column names, you can pass a list of column names to the indexing operator as shown below.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
print("The columns are:")
columns=df[["Maths", "Physics"]]
print(columns)

Output:

The input dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The columns are:
   Maths  Physics
0    100       80
1     80      100
2     90       80
3    100      100
4     90       90
5     80       70

In this example, we first converted a list of dictionaries to a dataframe using the DataFrame() function. Then, we selected the "Maths" and "Physics" columns from the dataframe using the list ["Maths", "Physics"].

Select Specific Columns in Pandas Dataframe Using the Column Positions

If you don’t know the column names and only have the position of the columns, you can use the column attribute of the pandas dataframe to select specific columns. For this, we will use the following steps.

You can observe this in the following example.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
column_names=df.columns
reduired_indices=[0,2,3]
reuired_columns=[column_names[index] for index in reduired_indices]
print("The column names are:")
print(reuired_columns)
print("The columns are:")
columns=df[reuired_columns]
print(columns)

Output:

The input dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The column names are:
['Roll', 'Physics', 'Chemistry']
The columns are:
   Roll  Physics  Chemistry
0     1       80         90
1     2      100         90
2     3       80         70
3     4      100         90
4     5       90         80
5     6       70         70

In this example, we had to select the columns at positions 0, 2, and 3. For this, we created a variable reduired_indices with the list [0, 2, 3] as its value. Then, we used list comprehension and the python indexing operator to get the column names at the specified indices from the list of column names. We stored the specified column names in the reuired_columns variable. Then, we used the indexing operator to select the specific columns from the dataframe.

Select Specific Columns in a Dataframe Using the iloc Attribute

The iloc attribute in a pandas dataframe is used to select rows or columns at any given position. The iloc attribute of a dataframe returns an _ilocIndexer object. We can use this _ilocIndexer object to select columns from the dataframe. To select columns as specific positions using the iloc object, we will use the following syntax.

df.iloc[start_row:end_row, list_of_column_positions]

Here,

As we want to select all the rows and specified columns, we will keep start_row and end_row empty. We will just pass the list containing the position of specific columns to the list_of_column_positions variable for selecting the columns from the dataframe as shown in the following example.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
print("The columns are:")
list_of_column_positions=[0,2,3]
columns=df.iloc[:,list_of_column_positions]
print(columns)

Output:

The input dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The columns are:
   Roll  Physics  Chemistry
0     1       80         90
1     2      100         90
2     3       80         70
3     4      100         90
4     5       90         80
5     6       70         70

In this example, we used the iloc attribute to select columns at positions 0, 2, and 3 in the dataframe.

Specific Columns in a Dataframe Using the loc Attribute

The loc attribute in a pandas dataframe is used to select rows or columns at any given index or column name respectively. The loc attribute of a dataframe returns a _LocIndexer object. We can use this _LocIndexer object to select columns from the dataframe using the column names. To select specific columns using the loc object, we will use the following syntax.

df.iloc[start_row_index:end_row_index, list_of_column_names]

Here,

As we want to select all the rows and specified columns, we will keep start_row_index and end_row_index empty. We will just pass the list of specific column names to list_of_column_names for selecting the columns from the dataframe as shown below.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
print("The columns are:")
columns=df.loc[:,["Maths", "Physics"]]
print(columns)

Output:

The input dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The columns are:
   Maths  Physics
0    100       80
1     80      100
2     90       80
3    100      100
4     90       90
5     80       70

In this example, we have selected specific columns from the dataframe using a list of column names and the loc attribute.

Conclusion

In this article, we have discussed different ways to select specific columns in a pandas dataframe.

To learn more about python programming, you can read this article on how to sort a pandas dataframe. You might also like this article on how to drop columns from a pandas dataframe.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

The post Select Specific Columns in Pandas Dataframe appeared first on PythonForBeginners.com.

January 27, 2023 02:00 PM UTC


PyBites

Layoff fears – 5 tips to stay in control

Listen here:

Or watch here:

Welcome back to the Pybites podcast! 

This week we talk about:
-How to see opportunity through difficulties.
-How to react if everything is not going your way.
-How to be strong in a difficult situation.
-How to stay in control in every situation.

Most people are going through hardships but how can we surpass these circumstances and challenges in our ways?

5 tips to stay in control and reduce stress, because we think that there is a lot you actually can control.

1. Working on your skills
2. Build your network.
3. Double down on productivity.
4. Watch your health + well-being.
5. Adopt a positive mindset.

It is hard to get out of any uncomfortable situation, but it is also hard if you choose to stay in it. 

Always choose your hard.

Books:
– The Digital Nomad Handbook
– The Creative Act
– Our growing Pybites podcast book list (created on Pybites Books, our favorite reading tracker and one of our first Django sites we built)

Feedback / live podcast request – email us: info@pybit.es

Enjoy and next week we’ll have a fresh new episode for you. 

— 
Do you know enough Python to be dangerous but you don’t dare call yourself a developer? 

And you want that to change, taking your skills to the next level to land a better job, upgrade your career, earn more freedom and give back to society? 

We achieve these things for people we work with in our PDM program. Check it out here.

January 27, 2023 12:08 PM UTC


Real Python

The Real Python Podcast – Episode #142: Orchestrating Large and Small Projects With Apache Airflow

Have you worked on a project that needed an orchestration tool? How do you define the workflow of an entire data pipeline or a messaging system with Python? This week on the show, Calvin Hendryx-Parker is back to talk about using Apache Airflow and orchestrating Python projects.


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

January 27, 2023 12:00 PM UTC


Python⇒Speed

float64 to float32: Saving memory without losing precision

Libraries like NumPy and Pandas let you switch data types, which allows you to reduce memory usage. Switching from numpy.float64 (“double-precision” or 64-bit floats) to numpy.float32 (“single-precision” or 32-bit floats) cuts memory usage in half. But it does so at a cost: float32 can only store a much smaller range of numbers, with less precision.

So if you want to save memory, how do you use float32 without distorting your results? Let’s find out!

In particular, we will:

Read more...

January 27, 2023 12:00 AM UTC

January 26, 2023


James Bennett

Boring Python: dependency management

This is the first in hopefully a series of posts I intend to write about how to build/manage/deploy/etc. Python applications in as boring a way as possible. So before I go any further, I want to be absolutely clear on what I mean by “boring”: I don’t mean “reliable” or “bug-free” or “no incidents”. While there is some overlap, and some of the things I’ll be recommending can help to reduce bugs, I also want …

Read full entry

January 26, 2023 10:49 PM UTC


PyBites

Fail. Fail better. A change in attitude towards Imposter Syndrome

I’m afraid my introduction to you all will quickly turn into somewhat of a cliche being Canadian :flag-ca:.

Let me explain. You know what it’s like. You are watching a YouTube video on the latest and greatest tutorials (Yeah, I was also engulfed in the flames of  tutorial hell).

Then it happens… commercial! Arghhhh! There is one I can’t bear to click away from (enter cliche here). 

Master Class. Not just any Master Class. This one is a trailer for Margaret Atwood Teaches Creative Writing.

If you don’t know who she is, then I’m sure you would have heard of the popular Netflix Series “The Handmaid’s Tale”. Well she wrote it. Cliche?

I’m a Canadian Expat, living in Europe. Suddenly memories came flooding back of studying Canadian Literature in University almost 25 years ago. Countless hours of reading her novels, books of poetry, even going to a reading of hers in person. So what does this have to do with coding and imposter syndrome?? 

Despite watching the trailer over and over and over, sometimes even googling it to watch it from time to time, it was only after I decided to go back into coding after a 20 year break that I really got the message.

So, about six months ago, there I was, coding along when boom! Commercial time. I immediately seized the opportunity for a coffee refill whilst intently listening, taking for granted that I knew Atwood’s words of wisdom by heart. Then, what she said stopped me in my tracks:

If you really do want to write and you’re struggling to get started, you’re afraid of something.

– Margaret Atwood

What did she say? It was only then that I realised that this is exactly the reason why I spend too much time watching tutorials instead of coding.

What am I afraid of then? Looking stupid? Failure? Being discovered as a fake? Basically all of the above. And what do all of the above have in common? 

You guessed it! Fear

Fear is crippling and it fuels Imposter Syndrome.

We set out with good intentions. For example: “Today, I’m going to start that bigger project and get out of my comfort zone. I really am this time.” Then you code part way, run into some obstacles and it ends up as a partial project, stowed away in your private repositories on  GitHub.

For those of you who don’t really follow what I am getting at here, I have a challenge for you:

Look at your repositories, “What percentage of your repos are private?”

Look at them and ask yourself: “Why are they private? “Why didn’t I finish them?”

Go ahead and write it down. Make a list. Take inventory of your fears.

Atwood continues with:

Any form of human creativity is a process of doing it and getting better at it.

Just do it.

Imagine if you could sit in front of your screen without fear and just code. It doesn’t matter if there are syntax errors, etc. You just code.

Atwood explains creativity as a process. We just don’t wake up one day and become an expert. Neither did she. She wrote and wrote for years and years before getting to the level she is at today. 

Atwood continues: 

You become a writer by writing. There is no other way.

Coding, coding and more coding.

There really is no other way is there? Practice, practice and more practice is the only way to get better.

Last Atwood quote I promise (you can tell I’m a fan right? Or should I say eh?):

So do it, do it more. Do it better. Fail. Fail better.

Fail better? Ahh yes, perfectionism. The other fuel for Imposter Syndrome. 

Well should I say the other component of the fuel for Imposter Syndrome. A fear of Failure.

Think about it: we are all good at something, perhaps even an expert. We have no  problem executing those tasks. In fact, we enjoy them.

Why? We don’t fear failure when it comes to taks we are good at. It’s the learning something new and the process of it that we fear the most. 

imposter syndrome 1Imposter Syndrome is real!

I’ll make a confession. This is my first blog post!

Bob and Julian graciously invited me to write about this and I am honoured to say the least.

This has been at least two weeks in the making. Why? Fear. I can’t write a blog. What if I look stupid? What if no one likes or reads it?

Every single word written in this blog was written by overcoming fear and Imposter Syndrome.

Every. Single. Word.

I had to face my fear and perfectionism head on. “Fail better”, according to my interpretation, means not being afraid to make mistakes. In fact, the ”better” part is referring to learning from those mistakes. How? 

A change of attitude towards mistakes. 

Let’s change the vocabulary here: 

Mistakes/Failures == Learning Opportunities

Fail Better == I’ve chosen to learn from each of my mistakes/failures. 

The bottom line is: we all struggle with this. I’m actually a trained Therapist.

So what has helped me in the last two weeks to actually complete this blog?  The 20 minute confidence training video by Bob and Julian you can check out at the end of this blog. 

Hope you enjoyed this and perhaps next time, I’ll share an interesting anecdote about the elephant … 

-Sherry Andrews Bhutia

Pybites Instagram Content Creator and Pythonista 


Bonuses:

January 26, 2023 07:05 PM UTC


PyCharm

PyCharm 2022.3.2 Is Out!

The second minor release brings a list of improvements, and fixes for the debugger, test runner, and Python Console.

You can update to version 2022.3.2 by using the Toolbox App, installing it right from the IDE, or downloading it from our website.

Download PyCharm 2022.3.2

Notable fixes:

To see the full list of improvements, check out the release notes. Found a bug or have an idea for a new feature or implementation? Share your feedback with us in the comments, on Twitter, or in our issue tracker.

The PyCharm team

January 26, 2023 06:51 PM UTC


Awesome Python Applications

Moa Bridge

Moa Bridge: A service to cross post [Mastodon](https://github.com/mastodon/mastodon) toots to Twitter and Instagram.

Links:

January 26, 2023 05:04 PM UTC


Python Software Foundation

The PSF is hiring a Security Developer-in-Residence!

The Python Software Foundation (PSF) is happy to announce the launch of a year-long security enhancement initiative that will include a security audit and the creation of a new Security Developer-in-Residence role. Generous funding by the OpenSSF’s Alpha-Omega Project has made this work possible.

Recent open source supply chain security attacks on open source projects and infrastructure have increased awareness of the critical role projects like Python and the Python Package Index (PyPI) play in providing a safe and secure ecosystem for millions of open source users. Historically, the Python Software Foundation’s ability to make key security improvements has only been realized when dedicated teams of volunteers or its existing infrastructure staff make time, or when it has received occasional grants, such as the introduction of 2FA and other security improvements to PyPI in 2019.

The Security Developer-in-Residence will work full-time during the initiative to formalize existing security practices and become more proactive in Python-related security improvements. The new role will be responsible for addressing security issues across PSF projects such as CPython and PyPI, and applying knowledge and expertise and working with volunteers to implement key improvements in a timely manner. They will also establish new processes and features that make it easier to prevent, detect, and respond to security risks to lay a foundation that makes it easier and more sustainable for the community to identify and address security issues going forward.

The Security Developer-in-Residence job is posted HERE. Please take a look and and share with your friends and colleagues.

This role is funded by a substantial investment from the Open Software Security Foundation’s Alpha-Omega Project. The OpenSSF is a non-profit cross-industry collaboration that brings together leaders to improve the security of open source software by building a broader community, targeted initiatives, and best practices. The OpenSSF brings together open source security initiatives under one foundation to accelerate work through cross-industry support.

The PSF is a non-profit whose mission is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers. The PSF supports the Python community using corporate sponsorships, grants, and donations. Are you interested in sponsoring or donating to the PSF so it can continue supporting Python and its community? Check out our sponsorship program, donate directly here, or contact our team!

January 26, 2023 02:58 PM UTC


Codementor

Shortest Program in Language C

lets learn the shortest program in C, the absolute basics in Language C

January 26, 2023 09:55 AM UTC

C Programming in 2023 ?

is it worth to learn language C

January 26, 2023 07:54 AM UTC

January 25, 2023


PyCharm

In Conversation With the Reloadium Team: Hot Reload and a Future Webinar

PyCharm is working hard on Python developer experience (DX). There’s a project with a very promising DX boost using “hot reloading”: Reloadium. It really speeds up turnaround time on working with your code, and with the PyCharm plugin, brings fresh new ideas to running, debugging, and profiling.

On January, 27 we have a webinar with Reloadium to show it in action. As an intro, we did a Q&A with the team.

Register now!

Quick Hit: Why should people care about Reloadium?

DK: Reloadium is a valuable tool for developers that offers hot reloading, shortening development cycle and preserving application state for debugging. Its PyCharm plugin integration makes Reloadium easy to use and offers additional features for efficient debugging. It’s a powerful tool for improving workflow and streamlining development.

Now, some introductions, starting with Sajan, who will be presenting.

ST: Thank you for the opportunity to be a part of this, Paul, we are very excited to share a glimpse into the problem we are solving for Python developers. My name is Sajan, I am a Business Psychologist specialising in the areas of business development, change management, and human factors. I have been working closely with Damian to create and improve Reloadium by identifying the gaps that exist in terms of productivity with Python and finding ways to close these gaps. 

DK: My name is Damian and I am the creator of Reloadium. I am currently employed as a Technical Lead and Full-Stack Developer on a full-time basis. The concept for Reloadium originated from a project I was working on, where I found that I had to frequently deal with long restart times. I created an initial version of Reloadium and used it to optimize my workflow, cutting down the development time significantly. That experience motivated me to continue to develop the tool and make it available to other developers. I am dedicated to providing a reliable and user-friendly hot reloading solution that can improve the development experience and increase efficiency in the programming process.

Explain hot reloading, especially from its use in frontend dev.

ST: Hot reloading is a valuable tool for developers. It allows developers to make updates to an application’s source code and see the resulting changes in real-time. This feature streamlines the development process by eliminating the need for manual application restarts, thus saving time and improving the efficiency of the development cycle. Additionally, one of the key benefits of hot reloading is the ability to preserve the state of the application, which can be particularly useful during bug fixing and troubleshooting.

In a front-end development context, hot reloading enhances the development experience by providing instant feedback on the effect of code changes, allowing developers to make adjustments and iterate quickly without the need for manual page refreshes. 

Hot reloading is a powerful feature that enables developers to work more efficiently, improve their workflow and deliver better-quality software.

Others worked on this. Damian, you then went off for a long time. What are the subtle, hard parts?

DK: Implementing hot reloading in Python has proven to be a challenging task, as previous attempts have not been able to consistently provide a reliable and user-friendly experience. One of the main difficulties in this process is maintaining compatibility with subtle variations in different versions of Python and operating systems. To ensure consistent quality between releases, it has been necessary to implement rigorous unit and end-to-end testing procedures. This has been a crucial step in identifying and addressing any issues that may arise during the development process, and in ensuring that the final product is stable and reliable for users. 

What are some of the fresh ideas Reloadium brings to DX?

ST: Reloadium brings several cutting-edge features to the developer experience, perhaps the most notable of which is Frame Reloading. This feature allows developers to hot reload the current call stack frame, enabling them to modify code that is currently being executed. This is a significant innovation in the field of software development, as it allows developers to make changes while debugging, which can save a significant amount of time. Another innovative feature is Hot Reloading of Unhandled Exceptions. Reloadium will break at unhandled exceptions and allow developers to fix the error by hot reloading changes. This feature is particularly useful when debugging non-deterministic bugs, as it can save countless hours in the reproducing and resolving of these issues.

How does the PyCharm plugin help improve the DX part?

DK: The PyCharm plugin provides a seamless integration of Reloadium, making it an invaluable tool for improving the development experience. In addition to integrating the core hot reloading functionality of Reloadium, the plugin also includes several advanced debugging features, such as time and memory profiling, call stack frame dropping, restarting frames, and hot reloading of unhandled exceptions. These features provide developers with more robust tools for identifying and resolving issues, thus increasing the efficiency of the development process. Furthermore, the plugin provides visual feedback on hot reloading and code execution, which greatly improves the user experience. Overall, the PyCharm plugin’s integration of Reloadium and advanced debugging features make it a powerful tool for enhancing the development experience.

Another fresh idea: you’ve formed a company to make this sustainable. Tell us about that.

ST: We are in the process of forming a company that focuses on developer tools like Reloadium to improve productivity, and ultimately helps in making programming easier. We are committed to making programming more developer-friendly and accessible so that people can feel free to design, create, and innovate. Through this company, we can keep improving and managing products like Reloadium so that it stays relevant, and continues to help improve productivity for developers.

Give us a teaser for what folks will see in the webinar.

ST:  In this webinar, we will explore:

Register now!

We urge you to join us in this journey to learn about this pioneering DevTool so that you can make more time to design, create, and innovate.  

January 25, 2023 02:18 PM UTC


Real Python

The Python Standard REPL: Try Out Code and Ideas Quickly

The Python standard shell, or REPL (Read-Eval-Print Loop), allows you to run Python code interactively while working on a project or learning the language. This tool is available in every Python installation, so you can use it at any moment.

As a Python developer, you’ll spend a considerable part of your coding time in a REPL session because this tool allows you to test new ideas, explore and experiment with new tools and libraries, refactor and debug your code, and try out examples.

In this tutorial, you’ll learn how to:

  • Run the Python standard REPL, or interactive shell
  • Write and execute Python code in an interactive session
  • Quickly edit, modify, and reuse code in a REPL session
  • Get help and introspect your code in an interactive session
  • Tweak some features of the standard REPL
  • Identify the standard REPL’s missing features

You’ll also learn about available feature-rich REPLs, such as IDLE, IPython, bpython, and ptpython.

To get the most out of this tutorial, you should be familiar with your operating system’s command line, or terminal. You should also know the basics of using the python command to run your code.

Free Sample Code: Click here to download the free sample code that you’ll use to explore the capabilities of Python’s standard REPL.

Getting to Know the Python Standard REPL

In computer programming, you’ll find two kinds of programming languages: compiled and interpreted languages. Compiled programming languages like C and C++ will have a compiler program, which takes care of translating the language’s code into machine code.

This machine code is typically saved into an executable file. Once you have an executable file, you can run your program on any compatible computer system without needing the compiler or the source code.

In contrast, interpreted languages like Python need an interpreter program. This means that you need to have a Python interpreter installed to run Python code on your computer. Some may consider this characteristic a drawback because it can make your code distribution process much more difficult.

However, in Python, having an interpreter offers one significant advantage that comes in handy during your development and testing process. The Python interpreter allows for what’s known as an interactive REPL (Read-Eval-Print Loop), or shell, which reads a piece of code, evaluates it, and then prints the result to the console in a loop.

Note: In this tutorial, you’ll learn about the CPython standard REPL, which is available in all the installers of this Python distribution. If you don’t have CPython yet, then check out Python 3 Installation & Setup Guide for detailed instructions.

The Python interpreter can execute Python code in two modes:

  1. Script, or program
  2. Interactive, or REPL

In script mode, you use the interpreter to run a source file as an executable program. In this case, Python loads the file content and runs the code line by line, following the script or program’s execution flow. Alternatively, interactive mode is when you launch the interpreter and use it as a platform to run code that you type in directly.

Note: The name Python is commonly used to denote two different things: the language itself, and the interpreter. In this tutorial, you’ll find the explicit term Python interpreter only in situations where ambiguity can arise.

In this tutorial, you’ll learn how to use the Python standard REPL to run code interactively, which allows you to try ideas and test concepts when using and learning Python. Are you ready to take a closer look at the Python REPL? Keep reading!

What Is Python’s Interactive Shell or REPL?

When you run the Python interpreter in interactive mode, you open an interactive shell, also known as an interactive or a REPL session. In this shell, your keyboard is the input source, and your screen is the output destination.

Note: In this tutorial, you’ll find the terms interactive shell, interactive session, interpreter session, and REPL session used interchangeably.

The input consists of Python code, which the interpreter parses and evaluates. After that’s done, the interpreter automatically displays the result on your screen, and the process starts again as a loop.

So, Python’s REPL is an interactive way to talk to your computer using the Python language. It’s like live chat. The whole process is known as a REPL because it goes through four steps that run under the hood:

  1. Reading your input, which consists of Python code as expressions and statements
  2. Evaluating your Python code, which generates a result or causes side effects
  3. Printing any output so that you can check your code’s results and get immediate feedback
  4. Looping back to step one to continue the interaction

This feature of Python is a powerful tool that you’ll wind up needing in your Python coding adventure, especially when you’re learning the language or when you’re in the early stages of a development process. That’s because the REPL offers several benefits, which you’ll learn about next.

Read the full article at https://realpython.com/python-repl/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

January 25, 2023 02:00 PM UTC


Python for Beginners

Convert Epoch to Datetime in Python

Most of the software applications log date and time values as UNIX timestamps. While analyzing the logged data, we often need to convert the Unix timestamp to date and time values. In this article, we will discuss different ways to convert UNIX timestamps to datetime in python. We will also discuss how to convert a negative epoch to datetime in Python.

Unix Epoch to Datetime in Python

To convert epoch to datetime in python, we can use the fromtimestamp() method defined in the datetime module. The fromtimestamp() method takes the epoch as its input argument and returns the datetime object. You can observe this in the following example. 

from datetime import datetime
epoch=123456789
print("The epoch is:")
print(epoch)
datetime_obj=datetime.fromtimestamp(epoch)
print("The datetime object is:")
print(datetime_obj)

Output:

The epoch is:
123456789
The datetime object is:
1973-11-30 03:03:09

In this example, we have converted the epoch 123456789 to datetime value using the fromtimestamp() method.

The above approach will give you time according to the time zone on your machine. If you want to get the UTC time from the timestamp, you can use the utcfromtimestamp() instead of the fromtimestamp() method as shown below.

from datetime import datetime
epoch=123456789
print("The epoch is:")
print(epoch)
datetime_obj=datetime.utcfromtimestamp(epoch)
print("The datetime object is:")
print(datetime_obj)

Output:

The epoch is:
123456789
The datetime object is:
1973-11-29 21:33:09

In this example, you can observe that the datetime output show the time approx 5 hours and 30 minutes before the time in the previous example. However, we have used the same epoch value. This difference is due to the reason that the time zone of my computer is set to +5:30 hours.

Unix Timestamp to Datetime String

To convert the Unix timestamp to a datetime string in python, we will first create a datetime object from the epoch using the fromtimestamp() method or the utcfromtimestamp() method. Then, you can use the strftime() method to convert the datetime object to a string.

The strftime() method, when invoked on a datetime object, takes the format of the required datetime string as its input argument and returns the output string. You can observe this in the following example.

from datetime import datetime
epoch=123456789
print("The epoch is:")
print(epoch)
datetime_obj=datetime.utcfromtimestamp(epoch)
print("The datetime object is:")
print(datetime_obj)
datetime_string=datetime_obj.strftime( "%d-%m-%Y %H:%M:%S" )
print("The datetime string is:")
print(datetime_string)

Output:

The epoch is:
123456789
The datetime object is:
1973-11-29 21:33:09
The datetime string is:
29-11-1973 21:33:09

In this example, the format specifiers used in the strftime() method are as follows.

You can also change the position of the placeholders in the string to change the date format.

Datetime to UNIX Timestamp in Python

To convert a datetime object to UNIX timestamp in python, we can use the timestamp() method. The timestamp() method, when invoked on a datetime object, returns the UNIX epoch for the given datetime object. You can observe this in the following example.

from datetime import datetime
datetime_obj=datetime.today()
print("The datetime object is:")
print(datetime_obj)
epoch=datetime_obj.timestamp()
print("The epoch is:")
print(epoch)

Output:

The datetime object is:
2023-01-24 00:34:40.582494
The epoch is:
1674500680.582494

In this example, we first obtained the current datetime using the datetime.today() method. Then, we used the timestamp() method to convert datetime object to timestamp.

Convert Negative Timestamp to Datetime in Python

The UNIX timestamp or epoch is basically the number of seconds that elapsed after UTC 1st January 1970, 0 hours, 0 minutes, 0 seconds. So, if we represent a date before 1970 using an epoch, the value is negative. For example, the if we represent 31 December 1969 using epoch, it will evaluate to -86400 i.e 24 hours*3600 seconds before 01 Jan 1970. You can observe this in the following example.

from datetime import datetime
epoch=-86400
print("The epoch is:")
print(epoch)
datetime_obj=datetime.fromtimestamp(epoch)
print("The datetime object is:")
print(datetime_obj)

Output:

The epoch is:
-86400
The datetime object is:
1969-12-31 05:30:00

To convert a negative UNIX timestamp to datetime, you can directly pass it to the fromtimestamp() method as shown above. Here, we have specified the timestamp as -86400. Hence, the fromtimestamp() method returns the datetime 86400 seconds before the datetime 01 Jan 1970 +5:30.

Again, the above approach will give you time according to the time zone on your machine. If you want to get the UTC time from the timestamp, you can use the utcfromtimestamp() instead of the fromtimestamp() method as shown below.

from datetime import datetime
epoch=-86400
print("The epoch is:")
print(epoch)
datetime_obj=datetime.utcfromtimestamp(epoch)
print("The datetime object is:")
print(datetime_obj)

Output:

The epoch is:
-86400
The datetime object is:
1969-12-31 00:00:00

In this example, you can observe that the utcfromtimestamp() method returns the datetime 1969-12-31 00:00:00 which is exactly 86400 seconds before Jan 1, 1970 00:00:00.

Instead of using the above approach, you can also use the timedelta() function to convert the negative epoch to datetime.

The timedelta() function takes the negative epoch as its input argument and returns a timedelta object. After calculating the timedelta object, you can add it to the datetime object representing Jan 1, 1970. This will give you the datetime object for the negative epoch that was given as input. You can observe this in the following example.

from datetime import datetime
from datetime import timedelta

epoch=-86400
print("The epoch is:")
print(epoch)
datetime_obj=datetime(1970,1,1)+timedelta(seconds=epoch)
print("The datetime object is:")
print(datetime_obj)

Output:

The epoch is:
-86400
The datetime object is:
1969-12-31 00:00:00

Conclusion

In this article, we have discussed different ways to convert a UNIX timestamp to datetime in python. We also discussed how to convert a negative epoch to a datetime object in python.

To learn more about python programming, you can read this article on python simplehttpserver. You might also like this article on python with open statement.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

The post Convert Epoch to Datetime in Python appeared first on PythonForBeginners.com.

January 25, 2023 02:00 PM UTC


PyBites

10 things that hamper your Python career progress

We all know that becoming a Python developer is hard.

There’s the “10,000-hour” principle which means there’s a significant amount of effort and time you’re going to have to invest.

More important though is how you’ll spend that time. Are you working on the right things and tackling increasingly challenging goals?

We talk with a lot of people about their Python career goals and here are some common things that are holding them back:

  1. I do not have a clear end goal of what I wanted to do for a career.
  2. I don’t know what options there are to break into the tech industry and what niche I want to be in.
  3. I feel as though I have learned the foundations of Python but am struggling with understanding how to bridge the gap from where I am now to building a production-ready application.
  4. I am completely self-taught, I have taken numerous courses from xyz, but have not been able to feel confident enough to develop an application end-to-end. Getting to this point will give me the confidence to apply for developer type jobs.
  5. Bad time management. I often find myself caught up in other “urgent” tasks. It can be very tough to work on a programming project on and off (in an unfocused manner), a lot of time gets lost having to re-learn skills, never reaching consistent fluency.
  6. Tutorial hell. And it’s hard when I’m getting stuck in dead ends, not being able to find the answers on the Internet.
  7. Impostor syndrome, not feeling confident enough and not knowing where my skill/experience level fits compared to other developers.
  8. I just haven’t got the ‘developer mindset‘ yet e.g., “How would a dev approach a particular problem?”
  9. Sites with simple coding tasks tend to focus on little parts of potentially big things so I do not see a good way to learn without building a bigger project, with a mentor, gaining more holistic developer skills. I lack feedback and code reviews from more experienced developers.
  10. My main obstacle is a lack of direction (not having anybody to bounce questions or ideas off of) and a sort of decision paralysis given the sheer number of options, resources, libraries and “best practices” available.

Do one or more of these things resonate with you?

Do you want to get to the next level as a Python developer but you feel these types of reasons are holding you back too?

Then we have good news. You can resolve all of these things in just 3 months by putting in ~10 hours of consistent effort a week.

Our team of expert coaches can show you the right way, and once exposed to it, you won’t look back to your old way of doing things.

Working with us:


If you’re excited at this point at the prospect of taking your Python journey to the next level in an effective way, it’s time to take action:

1) Check out our Pybites Developer Mindset (PDM) program.

2) If you got excited about the sample projects and watching / reading what PDM alumni have gained from working with us, then apply on the page. Please provide us with as much detail so we can be prepared when we reach out to you.

Looking forward to hearing from you.

– Bob & Julian

January 25, 2023 01:42 PM UTC


Talk Python to Me

#400: Ruff - The Fast, Rust-based Python Linter

Our code quality tools (linters, test frameworks, and others) play an important role in keeping our code error free and conforming to the rules our teams have chosen. But when these tools become sluggish and slow down development, we often avoid running them or even turn them off. On this episode, we have Charlie Marsh here to introduce Ruff, a fast Python linter, written in Rust. To give you a sense of what he means with fast, common Python linters can take 30-60 seconds to lint the CPython codebase. Ruff takes 300 milliseconds. I ran it on the 20,000 lines of Python code for our courses web app at Talk Python Training, and it was instantaneous. It's the kind of tool that can change how you work. I hope you're excited to learn more about it.<br/> <br/> <strong>Links from the show</strong><br/> <br/> <div><b>Charlie on Twitter</b>: <a href="https://twitter.com/charliermarsh" target="_blank" rel="noopener">@charliermarsh</a><br/> <b>Charlie on Mastodon</b>: <a href="https://hachyderm.io/@charliermarsh" target="_blank" rel="noopener">@charliermarsh@hachyderm</a><br/> <b>Ruff</b>: <a href="https://github.com/charliermarsh/ruff" target="_blank" rel="noopener">github.com</a><br/> <br/> <b>PyCharm Developer Advocate Job</b>: <a href="https://talkpython.fm/pycharm-advocate-job" target="_blank" rel="noopener">jetbrains.com/careers</a><br/> <b>Watch this episode on YouTube</b>: <a href="https://www.youtube.com/watch?v=LCva0NOM2-o" target="_blank" rel="noopener">youtube.com</a><br/> <b>Episode transcripts</b>: <a href="https://talkpython.fm/episodes/transcript/400/ruff-the-fast-rust-based-python-linter" target="_blank" rel="noopener">talkpython.fm</a><br/> <br/> <b>--- Stay in touch with us ---</b><br/> <b>Subscribe to us on YouTube</b>: <a href="https://talkpython.fm/youtube" target="_blank" rel="noopener">youtube.com</a><br/> <b>Follow Talk Python on Mastodon</b>: <a href="https://fosstodon.org/web/@talkpython" target="_blank" rel="noopener"><i class="fa-brands fa-mastodon"></i>talkpython</a><br/> <b>Follow Michael on Mastodon</b>: <a href="https://fosstodon.org/web/@mkennedy" target="_blank" rel="noopener"><i class="fa-brands fa-mastodon"></i>mkennedy</a><br/></div><br/> <strong>Sponsors</strong><br/> <a href='https://talkpython.fm/cox'>Cox Automotive</a><br> <a href='https://talkpython.fm/userinterviews'>User Interviews</a><br> <a href='https://talkpython.fm/training'>Talk Python Training</a>

January 25, 2023 08:00 AM UTC


Codementor

Mistakes which beginners make while choosing a Programming Language

Try avoiding this mistakes...

January 25, 2023 06:39 AM UTC