This article discusses how to represent a tab character in Python and some used cases.
Continue readingAuthor Archives: Kiprono Koech
Python Indentationerror Expected an Indented Block
Codes in different programming languages are structured into blocks using specific characters as delimiters. These delimiters include:
- Curly brackets in C++, C, Java, and JavaScript, among other programming languages,
- Indentation in Python,
- Begin/end in Pascal,
- Do/end in Ruby, and
- Parentheses in Lisp.
List Environment Variables in Powershell
All environment variables in PowerShell are stored in PS Drive under the Env: path. You can see that by running the following command:
1 |
Get-PSDrive |
Greedy and Non-greedy Regex in Python
Regex matching in Python is done in two ways: greedy and non-greedy (also called lazy matching).
Continue readingCan’t Assign to Function Call in Python
When programming with Python, the understanding of the “SyntaxError: cannot assign to function call” error boils down to understanding these three topics:
Continue readingRemove Characters From String in Powershell
The PowerShell -replace operator is an excellent function for substituting characters or sequences of characters in a string. The essential advantage of the operator is that it accepts the substitution of a substring with literal strings, wildcards, and regular expressions.
Continue readingProcess Finished With Exit Code 0 in Python
Python interpreter generates exit codes at the end of the execution to indicate if the code was executed successfully with no errors/exceptions.
The exit code 0 is the standard code used to indicate that our program is executed successfully with no errors. Any non-zero exit code is used for executions that terminated with errors or exceptions.
How to Read Python Exit Codes
Method 1: In some cases, exit codes are printed on the output console
Some IDEs and code editors, such as PyCharm and Sublime Text, print the exit code on the terminal once execution is done. For example,
1 2 3 |
# Code that executes with no errors result = (3*7.2)/0.7 print(result) |
When the above code is executed on Pycharm, the following content is printed on the console.
Next, let’s execute code that leads to an error in Pycharm.
1 2 3 |
# We cannot divide a number with zero result = (3*7.2)/0 print(result) |
Since the above code yielded a ZeroDivisionError, the execution was terminated with exit code 1.
Method 2: Running Python script on the terminal, then check the returned code
This is done by executing our script on the command line and then checking the value returned by the Python interpreter after the execution.
Example
First, save the following code in a script called exit_code1.py.
1 2 3 4 |
# Get the mean of three numbers and round off results to two decimal places mean = round(sum([56, 78, 83])/3, 2) name = "Eric" print(f"Hello, {name}, your mean mark is {mean}") |
Then you can execute it in the terminal with the following command
1 |
python3 exit_code1.py |
Lastly, check the code by running (for Unix users)
1 |
echo $? |
Or use the following command (Windows Users):
1 |
echo %errorlevel% |
The sequence “$?” for UNIX systems and “%errorlevel%” for Windows contains the exit code of the last command.
And, if we run code with errors, we get a non-zero exit code, as shown in this example (the code is saved on the exit_codetf.py file).
1 2 |
name = "Allan Smith" printt(name) |
Terminating Python Execution using Custom Exit Codes
The standard exit codes for Python are 0 and 1 – 0 for successful execution and 1 for abnormal termination. However, you can define a different exit code using the sys.exit(code) function.
Note: sys.exit() function accepts 8-bit exit codes, that is, any value between 0 and 255. If you provide a value outside that range, it’s treated as the modulo 256 of the given number.
For example, if you issue -4, sys.exit() will use 252 as the exit code, and if you supply 447, 191 will be used, as shown below.
1 2 3 4 |
# % is the modulo operator. print(-4 % 256) print(447 % 256) print(135 % 256) # When we use a value within the range modulo operator doesn't take effect. |
Output:
252 191 135
Let’s see how to supply a custom exit code using sys.exit(). In this example, we take an examination score and check if it is negative, between 0 and 100, or above 100. If the supplied mark is less than 0, we issue an exit code of 13, and if the score given is greater than 100, we exit with the code 101.
1 2 3 4 5 6 7 8 9 10 |
import sys mark = int(input("Enter the score: ")) if mark < 0: print("Oops! You entered a negative mark!") sys.exit(13) elif mark > 100: print("Oops! You entered a value greater than 100!") sys.exit(101) else: print(f"You entered {mark}") |
Output (first take with a negative mark):
Enter the score: -33 Oops! You entered a negative mark! Process finished with exit code 13
Output (second take with a score between 0 and 100):
Enter the score: 68 You entered 68 Process finished with exit code 0 Output (third take with a score above 100): Enter the score: 206 Oops! You entered a value greater than 100! Process finished with exit code 101
Conclusion
There are two standard exit codes in Python – 0 and 1. The former means the program exited normally – without errors, but the latter means the program terminated with errors.
This article discussed how to check the exit code and set custom codes in Python. After going through the guide, you should easily interpret exit codes in Python.
Optional Parameters in Powershell
Parameters in PowerShell can be passed into a function or a script using the param() block.
Continue readingWrite Output to Log Files in PowerShell Script
PowerShell provides different methods of writing output from a script/command into a log file. These log files come in handy when for troubleshooting and auditing PowerShell code.
Continue readingSet HTTP proxy with Python Requests
This article discusses how to pass proxies to the Python requests library. Before doing that, let” s discuss why we need proxies.
Continue reading