Single and double underscores have a meaning in Python variable and method names. Some of that meaning is merely by convention and intended as a hint to the programmer—and some of it is enforced by the Python interpreter. If you’re wondering “What’s the meaning of single and double underscores in Python variable and method names?” … Continue reading underscore naming convention in Python
Python
Python Decorators
In short, a decorator takes in a function, adds some functionality and returns it. This feature in Python is very similar to what is in Ruby. You open up a class and add feature to it - monkey patching. Python has an interesting feature called decorators to add functionality to an existing code. This is … Continue reading Python Decorators
*args and **kwargs in Python
In Python, there are two special symbols that you can use when defining functions to allow them to take in a variable number of arguments. The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args. What *args allows you … Continue reading *args and **kwargs in Python
Making async http requests with python-aiohttp
In this post I’d like to test limits of python aiohttp and check its performance in terms of requests per minute. Everyone knows that asynchronous code performs better when applied to network operations, but it’s still interesting to check this assumption and understand how exactly it is better and why it’s is better. I’m going … Continue reading Making async http requests with python-aiohttp
How to rename all files in a folder using Python
let's assume the folder is user_data, relative to the current working directory (where the Python script is in). The code below was tested on Windows. import os for filename in os.listdir("./user_data"): # print(filename) cwd = os.path.dirname(os.path.abspath(__file__)) + '\\user_data\\' if filename.split('.')[1] == 'jpg': new_filename = filename.split('.')[0] + '_search' + '.jpg' print(new_filename) … Continue reading How to rename all files in a folder using Python
Lambda functions in Python
A lambda function in python has the following syntax. Syntax of Lambda Function in python lambda arguments: expression Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required. Example: double = lambda x: x * 2 # Output: … Continue reading Lambda functions in Python
Python packages and the purpose of __init__.py file
Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A. Just like the use of modules saves the authors of different modules from having to worry about each other’s global variable names, the use of … Continue reading Python packages and the purpose of __init__.py file
What does if __name__ == “__main__”: do?
A module's __name__ Every module has a name and statements in a module can find out the name of its module. This is especially handy in one particular situation - As mentioned previously, when a module is imported for the first time, the main block in that module is run. What if we want to … Continue reading What does if __name__ == “__main__”: do?
How to run system commands Java (Linux environment)
In Python, we use import os os.system('sudo vncserver stop') What about in Java. Below is one example. import java.io.*; public class java_system_call { public static void main(String args[]) { String s = null; try { // run the Unix "ps -ef" command // using the Runtime exec method: Process … Continue reading How to run system commands Java (Linux environment)
A comparison between Java and Python
This is a post that interests me. (Is WebFaction a good hosting service provider?) Comparing Python and Java A programmer can be significantly more productive in Python than in Java. How much more productive? The most widely accepted estimate is 5-10 times. On the basis of my own personal experience with the two languages, I … Continue reading A comparison between Java and Python