Sunday, August 23, 2020

Implementing C struct-like Data Structure in Python (Requires Python 3.6+)

 The solution to implement C struct-like data structure in Python is explained in: https://stackoverflow.com/questions/35988/c-like-structures-in-python. You need the presence of dataclass python class to create a syntactically and semantically similar data structure to C struct. This class is implemented in Python 3.7. Therefore, python 3.7 is the lowest version of python supported. However, the support has been backported to Python version 3.6 (see: https://pypi.org/project/dataclasses/#description ). 

Perhaps, you might ask, why not just use Python collections namedtuple data type. Well, namedtuple is syntactically similar to C struct. However, it's immutable, meaning: once you have set its value, you cannot change the value. The only way to change the object value is by creating a new object with the same type and setting the value into different value.

Anyway, this is a sample usage of dataclass:  

as you can see in the screenshot above, the value of c_struc class instance can be changed (mutable), as in a C struct.

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 :( .