Posts

Showing posts from September, 2018

Save HTML as a single file

I wanted to save some HTML pages as single files that could be read by Chrome and Firefox. I found out that you could do it in Chrome without any extension by enabling the experimental flag Save Page as MHTML . This experimental feature can be found by typing chrome://flags/#save-page-as-mhtml   in the search bar. However, Chrome warns you to be careful with playing with these options: ... you could lose browser data, or compromise your security or privacy. Once you enable this option, you must restart Chrome, and thereafter the files can be saved in the compressed format MHTML, " a single text file containing HTML and all sub-resources ". However, these MHTML files can be read by Chrome, but not by Firefox. Thus I searched for other tools that can generate HTML single files which can be read by both Chrome and Firefox, and these are the two extensions I tried that can accomplish this feat: SingleFile : Install for  Chrome  and Firefox Save Page WE : Install for...

Python tips: reverse a dictionary

In Python, if you want to reverse a dictionary ( dict ), i.e. swap the keys and values, there are three methods that can help you achieve that goal depending on your needs: Method 1 : makes use of dict comprehension , and the dict must contain unique values Method 2 : makes use of dict.get() , and the dict doesn't contain unique values Method 3 : makes use of map(reversed, iter) , and you want the reverse dict to preserve the type and order of the original dict (if for example it is an OrderedDict ) I provided code implementations for each methods that work for Python 2.7.x and 3+, and the embedded code can be executed directly from the blog post through JDoodle , an online compiler and editor for many other languages (68 in total such as C/C++, Java, Go, SQL). I also compiled the average running times of the different methods in a table at the end ( Method 1 with dict comprehension is the fastest! Spoiler alert: Click to reveal winning method ) The code used ...