Python Get Current Directory
This article covers how to use the 'os. getcwd()' method to easily get the python current working directory. When you run a Python script, the current working directory is set to the directory from which the script is executed.
The os python module provides a portable way to interact with the operating system. The module is part of the standard Python library and includes methods for finding and changing the current working directory.
Basically, In Python, you can get and change (set) the current working directory with os.getcwd() and os.chdir().
os module is included in the standard library, so no additional installation is required.
To Get the current working directory: os.getcwd()
To Change the current working directory: os.chdir()
How to Get the current working directory: os.getcwd() ?
1. os.getcwd() returns the absolute path of the working directory where Python is currently running as a string str.
2. getcwd stands for "get current working directory", and the Unix command pwd stands for "print working directory".
Abstract Factory – Design Patterns in Python
This article covers Abstract Factory design pattern in Python.
Basically, The Abstract Factory design pattern can also be used to create cross-platform UIs without coupling the client code to concrete UI classes and keeping all created views consistent with different operating systems.
Abstract Factory is a creational design pattern, which solves the problem of creating entire product families without specifying their concrete classes.
Abstract Factory Method is a Creational Design pattern that allows you to produce the families of related objects without specifying their concrete classes.
Using the abstract factory method, we have the easiest ways to produce a similar type of many objects.
It provides a way to encapsulate a group of individual factories.
Advantages of using Abstract Factory method:
This pattern is particularly useful when the client doesn’t know exactly what type to create.
1. It is easy to introduce the new variants of the products without breaking the existing client code.
2. Products which we are getting from factory are surely compatible with each other.
Disadvantages of using Abstract Factory method:
1. Our simple code may become complicated due to the existence of lot of classes.
2. We end up with huge number of small fies i.e, cluttering of files.
Examples of Factory pattern in Python:
1. With the Factory pattern, you produce instances of implementations (Apple, Banana, Cherry, etc.) of a particular interface -- say, IFruit.
2. With the Abstract Factory pattern, you provide a way for anyone to provide their own factory. This allows your warehouse to be either an IFruitFactory or an IJuiceFactory, without requiring your warehouse to know anything about fruits or juices.
Install Python 3.9 on Ubuntu 20.04 - Step by Step Process ?
This article covers how to install and compile Python3.9 using different methods, using PPA repo, compiling it from the source code, and installing it using the Linuxbrew tool.
We can now start using Python 3.9 for our projects.
Python is a high-level programming language, mostly used to write scripting and automation. It is a very popular language known for its simplicity and easy syntax.
Python one of the best language for for artificial intelligence (AI).
To Install Python 3.9 on Ubuntu 20.04 using APT:
1. Update package list, type:
$ sudo apt update
2. Install software-properties-common package to easily manage distribution and independent software vendor software sources:
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
3. Now install python 3.9 using apt command:
$ sudo apt-get install python3.9
4. The following command can help to identify the proper install location of Python:
$ which python3
The execution of the above command produces the following output on console:
Unit Testing in Python - A Quick Overview ?
This article covers the concept of Unit Testing in Python. Testing in Python is a huge topic and can come with a lot of complexity, but it doesn't need to be hard. You can get started creating simple tests for your application in a few easy steps and then build on it from there.
Here, You'll learn about the tools available to write and execute tests, check your application's performance, and even look for security issues.
pytest supports execution of unittest test cases. The real advantage of pytest comes by writing pytest test cases. pytest test cases are a series of functions in a Python file starting with the name test_.
pytest has some other great features:
1. Support for the built-in assert statement instead of using special self.assert*() methods
2. Support for filtering for test cases
3. Ability to rerun from the last failing test
4. An ecosystem of hundreds of plugins to extend the functionality
Running Your Tests From Visual Studio Code
If you're using the Microsoft Visual Studio Code IDE, support for unittest, nose, and pytest execution is built into the Python plugin.
If you have the Python plugin installed, you can set up the configuration of your tests by opening the Command Palette with Ctrl+Shift+P and typing "Python test".
How to Use unittest and Flask
Flask requires that the app be imported and then set in test mode. You can instantiate a test client and use the test client to make requests to any routes in your application.
All of the test client instantiation is done in the setUp method of your test case. In the following example, my_app is the name of the application. Don’t worry if you don’t know what setUp does. You’ll learn about that in the More Advanced Testing Scenarios section.
The code within your test file should look like this:
my_app.app.testing = True
self.app = my_app.app.test_client()
result = self.app.get('/')
# Make your assertions
You can then execute the test cases using the python -m unittest discover command.
Install PyCharm on Ubuntu 20.04 - Step by Step Process ?
This article covers how to install PyCharm on your Ubuntu 20.04 system.
PyCharm is a cross-platform IDE that provides consistent experience on the Windows, macOS, and Linux operating systems. Also, you can also remove it any time from your Ubuntu 20.04 system by following the steps outlined in this guide.
Main features of PyCharm IDE:
1. Syntax highlighting
2. Auto-Indentation and code formatting
3. Code completion
4. Line and block commenting
5. On-the-fly error highlighting
6. Code snippets
7. Code folding
8. Easy code navigation and search
9. Code analysis
10. Configurable language injections
11. Python refactoring
To Install PyCharm in Ubuntu and other Linux using Snap:
1. Use the snap command to install the PyCharm Community Edition:
$ sudo snap install pycharm-community --classic
2. To remove PyCharm, you may use this command:
$ sudo snap remove pycharm-community
An Introduction to Python Async IO
This article covers an Overview of Async IO in Python. Python 3's asyncio module provides fundamental tools for implementing asynchronous I/O in Python. It was introduced in Python 3.4, and with each subsequent minor release, the module has evolved significantly.
Asyncio is the standard library package with Python that aims to help you write asynchronous code by giving you an easy way to write, execute, and structure your coroutines.
The Asyncio library is for concurrency, which is not to be confused with parallelism.
Concurrency does not mean Parallelism and vice-versa.
We can combine them both.
We can have multiple threads, running Tasks parallely but each thread may not be running Tasks concurrently.
1. Asynchronous IO (async IO): a language-agnostic paradigm (model) that has implementations across a host of programming languages.
2. async/await: two new Python keywords that are used to define coroutines.
3. asyncio: the Python package that provides a foundation and API for running and managing coroutines.