Sunday, August 23, 2020

Configuring Custom ipython Terminal Color Scheme

In some cases ipython default terminal color scheme is not the most readable one, especially when using Powershell in Windows. One of the way to fix the problem is to use custom/non-default color scheme. The steps to accomplish that as follows (tested in ipython v7.17 and python 3.7.9):

  1.  Run `pygmentize -L styles` in your terminal to check available ipython color styles/schemes. Experiment with the color scheme by starting ipython while settting the color scheme as one of its starting parameter, for example: ipython TerminalInteractiveShell.highlighting_style=native. Replace native with the color scheme that you want to test. ipython should start with the color scheme that you specify.
  2. Create default ipython configuration file for the current user by running: `ipython profile create` , just use the default configuration file. See: https://ipython.readthedocs.io/en/stable/config/intro.html for more in depth explanation of this command. 
  3. Edit the default configuration file located at: ~/.ipython/profile_default/ipython_config.py. In Windows, this file is located at: C:\Users\[username]\.ipython\profile_default\ipython_config.py.  Open the ipython_config.py file and edit the line that contain c.TerminalInteractiveShell.highlighting_style parameter. Set this config value to the color scheme that you want to make permanent. For example, in my config, I set the value to 'native' as follows: c.TerminalInteractiveShell.highlighting_style = 'native'. This will apply the 'native' color scheme as the default color scheme when you start ipython. 
The result of this configuration changes in my Powershell is shown below.


Wednesday, August 19, 2020

Fixing Visual Studio 2017 "can't find windows.h, stddef.h, string.h" Error

 The error in the title (in most cases) is caused by differing version between the target of the VS2017 project build settings and the version of Windows SDK installed in the computer used to compile your code. Each version of Windows SDK creates its own directory structure in your computer which made VS2017 IDE points to the wrong path (or in my case non-existent path). My solution to fix this errors as follows :

  1. Open VS2017 project properties
  2. Into General | Windows SDK Version
  3. Pick the correct version of installed Windows SDK version (in my case, version: 10.0.17763.0) instead  of  whatever version was set there previously.

The following is the screenshot of the project option, in case it's not clear enough. The aforementioned option is circled in blue.


Hope this helps because I scratched my head for half an hour just to find what went wrong :( . 

Friday, December 29, 2017

Storing Python Object in Redis - The "Brute Force" Approach

Sometimes we have the need to store Python object (class instance) to "object storage" server for some reason and then to retrieve it later. This post explains how to that by using Redis as the "object storage" server.

DISCLAIMER:  This post assumes that the machine where the code is executed and the Redis server is in the same machine or located within a secure premises.

You can clone the code from: https://github.com/pinczakko/py_obj_redis_seralization.git

The principle used by the code is simple:

  1. Serialize the object as string by using Python pickle.dumps() from the pickle module (https://docs.python.org/2/library/pickle.html)
  2. Store the object as "string" data type in Redis (https://redis.io/topics/data-types) and use the following formula for the key to address the object in Redis: "test-meta-webhook-" + object.subs_id , where subs_id acts as a unique identifier for the object.
  3. Retrieve the object string by using the same key used in 2. 
  4. Deserialize the object by using pickle.loads() from the pickle module.

The approach explained above is just for playground code because it probably doesn't scale as expected or is not well suited for latency sensitive application (the pickle.dumps() and pickle.loads() took too much time). However, for simple experimental code it's a nice to have "brute force" solution ;-). Below is sample output of the code:

Python Object Serialization/Deserialization to/from Redis


Monday, October 2, 2017

Subclassing HTMLParser Class in Python 2

Using HTMLParser class (https://docs.python.org/2/library/htmlparser.html) in Python 2 is rather easy if you don't need to pass parameter to your subclass for custom processing of the HTML tags. But, what if you do? This is rather trivial to do in Python 3, as seen here. The problem with Python 2, if you follow the "normal" way of invoking the parent HTMLParser class as explained at https://stackoverflow.com/questions/2399307/how-to-invoke-the-super-constructor , you would encounter error like this: TypeError: super() argument 1 must be type, not classobj.

Now, how to fix that error? The error culprit is explained at: https://stackoverflow.com/questions/1713038/super-fails-with-error-typeerror-argument-1-must-be-type-not-classobj#1713052. However, it doesn't give us satisfactory fix for the error because you would need to mess with HTMLParser class for that to work. I prefer not to do it. This is where Python's type keyword comes to the rescue. The code below shows how to properly subclass HTMLParser in Python 2, it might not be pretty a.k.a it's a rather quick-hack, but it works.
from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint

class ImgHtmlParser(HTMLParser):
    def __init__(self, path):
        super(type (self), self).__init__()
        self.reset()
        self.fed = []
        self.download_path = path
        print "ImgHtmlParser constructor"

    def handle_starttag(self, tag, attrs):
        if tag == 'img':
            print "Start tag:", tag
            for attr in attrs:
                print "     attr:", attr
                if attr[0] == "data-fullres-src":
                    print "image URL: " + attr[1]
                    print "Download Path = " + self.download_path 

I used the type keyword in place of the derived class literal name. It's not foolproof though if ImgHtmlParser class has a child class, but in this case, it doesn't have one. So, we're OK.