In many cases such as when dealing with API response in JSON format, working on JavaScript assets embedded on websites, and so on, we keep seeing 13-digit long numbers very frequently and we probably know it is epoch timestamp originating from code like new Date().getTime()
, however, as a human we just can not quickly tell what date and time it represents. There is a convenient website named Epoch Converter that can help convert it to human-readable format easily but most of the time for developers, there are even quicker ways than opening the website, which is by using the interactive shell
that comes with many programming languages.
Python topics of interest.
ThreadPoolExeuctor from concurrent.futures package in Python 3 is very useful for executing a task (function) with a set of data (parameter) concurrently and this post lists examples on how to pass MULTIPLE parameters to the task being executed.
Python decorators are usually created with function, see another related post, but this post also shows an example on how to create decorators with class.
A quick guide on how to use celery to accomplish async task in application.
Flask has an "app.logger" which you can use for webapp related logging, and for other libraries or packages that flask app is using usually python standard logging is used. There are issues reported that logs from various sources in flask based app sometimes get missed out or get messy. See the references section at the bottom for details about the issues. A workaround is to set global root logging config and override that from flask.
Huey as crontab alternative in python.
Python multiprocssing is useful in executing concurrent tasks with multiple processes. But it also requires the objects being executed support pickling, which is not always true for types like class instance methods, staticmethods and etc. Pathos has a multiprocessing implementation that uses dill on the backend which supports serializing and deserializing for almost all types.
Generate n*n two dimensional arrays with serial numbers in Python.
How to insert a new object into mongodb using pymongo with reference to other objects using DBRef.
How to quickly print out or remove duplicated items from given list in python.
Quick fix to an ssl error was encountered with python requests on Ubuntu 14.04.
A compiler error was observed during installing lxml on Ubuntu the error looks something like: error: command 'x86_64-linux-gnu-gcc' failed with exit status 4, and it's most likely caused by low memory.
With Python2 builtin SimpleHTTPServer
(or http.server
in Python3), you can start an http service to easily and quickly share a directory with files, which is especially useful and convenient in LAN environments.
How to use Python dict as data source for MySQL insert-into statement.
PyMySQL is a pure-Python MySQL client library which allows you to easily connect to MySQL db and perform db operations in pythonic way.
Elasticsearch python client and sample code for indices creation and deletion, document indexing, and searching. For elasticsearch installation, see post elk-getting-started-notes.
PEP8 (Python Enhancement Proposal 8) is the style guide for python coding and this post lists some of the important rules that should be followed.
Execute remote command from command line and playbook with Ansible.
Quick steps of using Python virtualenv.
Basic usage of Python fabric.
Python literal conversion using the ast.literal_eval()
method.
Automate vpn connection (Cisco IPSec VPN Client + SoftToken) using pywinauto package on windows platform.
Remove duplicate dict from a list in python.
Find max common divisor.
It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks.
Python multiprocessing.
Python multithreading. NOTE that although multiple threads can run within the python interpreter, ONLY one thread is being executed by the interpreter at any given time, which is ensured by the GIL (global interpreter lock) in python virtual machine. Python multithreading is more appropriate for I/O-bound applications than for CPU-bound applications as I/O releases GIL.
Common python file/script extensions and the meaning.
Python fabonacci implementations.
How to find out or list the installed python packages.
Bubble sort algorithm is comparison based algorithm in which each pair of adjacent elements is compared and elements are swapped if they are not in order. The average and worst case complexity of bubble sort is O(n2) where n is the number of items.
How to display timestamp in desired format in python.
Extract the i-th digit of any integers, where i starts from right to left.
Python source code encoding and unicode in 2.x.
What the if __name__ == '__main__'
does in python and practice of defining python main functions.
Python function standard arguments, positional arguments, and keyword arguments.
Python imports and PYTHONPATH.
Python aop with decorators