We may use two lists to store the values of two properties of a list of elements in Python. Under such data structure arrangement, when we need to sort the properties from one list, we may want to also make sure the other list will be also re-ordered following the same order as the first
Read more
Tag: Python
Linux Kernel 4.19.70 Release
Posted onThis post summarizes new features, bugfixes and changes in Linux kernel release 4.19.70. Linux 4.19.70 Release contains 95 changes, patches or new features. In total, there are 101,521 lines of Linux source code changed/added in Linux 4.19.70 release compared to Linux 4.19 release. To view the source code of Linux 4.19.70 kernel release online, please
Read more
4 Features of Python 3.9 That You Can’t Take Your Eyes Off
Posted onPython is one of the most popular programming languages in the world. It is widely popular for a plethora of tasks due to its flexible nature and ease of use. Python has also managed to beat other programming languages such as Java, which were once upon a time, the world’s favorite. In fact, the extent
Read more
How to Install Hyperledger Fabric 2.0 in Ubuntu 18.04
Posted onHyperledger Fabric is a consortium blockchain system. It’s performance is relatively good and its modular architecture enables it to be usable in many scenarios. Hyperledger Fabric itself has rich documents and samples of test networks. For beginners, deploying a new network for trying and testing still consumes quite some time. In this post, we will
Read more
How to get date and time from another timezone in Python?
Posted onHow to get the date and time from another timezone in Python? You may use the pytz library. Example usage as follows. $ python3 Python 3.8.2 (default, Jul 16 2020, 14:00:26) >>> from datetime import datetime >>> import pytz >>> >>> print(datetime.now(pytz.timezone(‘Europe/Amsterdam’)).strftime(‘%Y-%m-%d %H:%M:%S %Z%z’)) 2020-08-09 15:50:00 CEST+0200 >>>
How to convert a string to lower case in Python?
Posted onHow to convert a string to lower case in Python? For example: “Abc” -> “abc” “BBB” -> “bbb” “abc” -> “abc” You can use the string.lower() method of Python: string.lower(s) Return a copy of s, but with upper case letters converted to lower case. Example: $ python Python 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC
Read more
gdb like step by step debugger for Python
Posted onAny good debugger for Python that is like gdb so that the code can be executed step by step for debugging purpose? You might check pdb. You can debug a program such as prog.py by invoking it through pdb: python -m pdb prog.py Common gdb commands can be found in pdb: (Pdb) h Documented commands
Read more
How to check whether a file of a given path is a block device in Python?
Posted onHow to check and test whether a file of a given path is a block device in Python? This can be Linux specific. You can use the os.stat() function to get the stat of the path. Then use the stat.S_ISBLK() function against the stat’s .st_mode to test whether it is a block device. An example:
Read more
How to get the full path and directory of a Python script itself?
Posted onIn a Python script, how to get the full path and directory of the Python script itself? To get the path of the current file (the script itself), you can use __file__. To resolve any symbolic links in the path, you can use os.path.realpath(). Putting them together, you can do os.path.realpath(__file__) to get the full
Read more
How to decode a quoted URL in Python?
Posted onHow to decode a quoted URL in Python? For example, an quoted URL as follows https://www.example.com/tag/%E9%93%B6%E8%A1%8C/ should be decoded to https://www.example.com/tag/银行/ In Python 3, we can use `urllib.parse_plus()` (for URL only). One example is as follows. $ python3 Python 3.6.8 (default, Oct 7 2019, 12:59:55) [GCC 8.3.0] on linux Type “help”, “copyright”, “credits” or “license”
Read more
How to test whether a file exists and is a block special file in Python on Linux?
Posted onBash has a -b test to test whether a file exists and is a block special file. -b file True if file exists and is a block special file. How to do this in Python? Python’s stat module provides the similar functions for the C standard APIs and macros underlining such as stat() and S_ISBLK().
Read more
`readlink -m` equivalent function in Python to get canonical file name
Posted onreadlink -m can get canonical file name by resolving every symlinks in every component of the given path recursively. In Python, the os.readlink() function does not do so. Any equivalent function in Python to the readlink -m command line? Specifically, it does: canonicalize by following every symlink in every component of the given name recursively,
Read more
In Python, `os.makedirs()` with 0777 mode does not give others write permission
Posted onIn Python, os.makedirs() with 0777 mode can not give others write permission The code is as follows $ python Python 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2 Type “help”, “copyright”, “credits” or “license” for more information. >>> import os >>> os.makedirs(“/tmp/test1/test2”, 0777) >>> The created dirs are not
Read more
How to split a string by string in Python?
Posted onHow to split a string by string in Python? For example, “a string separated by space” => [“a”, “string”, “separated”, “by”, “space”] and “a,string,separated,by,comma” => [“a”, “string”, “separated”, “by”, “comma”] Python’s string.split() standard library function, if provided a separator, will split the string by the separator, for example >>> str2 = “a,string,separated,by,,,comma” >>> str2.split(“,”) [‘a’,
Read more
How to get the value of a default value if the key does not exist in `dict()` in Python?
Posted onHow to get the value of a default value if the key does not exist in dict() in Python? We can use the get() function to assign default value if the key does not exist in the dict yet. dict.get(key[, default]) Example as follows. msges = {“msg”: “Hello, World!”} msg = msges.get(“msg”, “”) print(msg) #
Read more
How to flush STDOUT buffer in Python?
Posted onHow to flush the STDOUT buffer in Python so that the content wrote to STDOUT is shown immediately? Call the flush library function on sys.stdout which is the STDOUT: import sys sys.stdout.flush() From python doc: flush() Flush the write buffers of the stream if applicable. This does nothing for read-only and non-blocking streams. If you
Read more
Why “ValueError: zero length field name in format” error in Python on CentOS 6?
Posted onThe same Python program runs without any problem while it report ValueError: zero length field name in format error in Python on CentOS 6 The piece of code in Python is: print ‘== {} SPF’.format(d) Why “ValueError: zero length field name in format” error in Python on CentOS 6? This feature of {} without the
Read more
Printing a Line to STDERR and STDOUT in Python
Posted onIn Python, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In Python, to print a string str with a new line to STDOUT: print str In Python to print a line to STDERR: import sys
Read more
How to get the running process’ parent process’ ID in Python?
Posted onHow to get the running process’ parent process’ ID in Python? In Python, you can get the parent process’ pid by calling os.getppid(): import os ppid = os.getppid() One example: The shell’s pid: $ echo $$ 21779 Start a python REPL and get its parent pid (the shell’s): $ python Python 2.7.5 (default, Nov 6
Read more
Getting the running process’ own pid in Python
Posted onHow to get the running process’ pid in Python? In Python, you can get the pid of the current process by import os os.getpid() From the official doc: os.getpid() Return the current process id. One example os using os.getpid() to get process own ID: $ python3 Python 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]
Read more