[Tip] Python snippet increases the UWP product version

If you have a C# project and want to manage the assembly version info during automation build process such as the msbuild with Jenkins, then two options are available:

  1. Specify the version number in AssemblyInfo.cs to with asterisk(*) – n.n.* (for example, ‘1.0.*’)
  2. Or you might want to manage it with a build script such as python or powershell.

Here is a python build script to manage the version string within AssemblyInfo.cs and Package.appxmanifest.



How to upload a file from slack using SlackClient?

Slack provides an official python client module which you can easily work with.

It’s simple and easy to use. For example, sending a text line to a specific channel is simple enough to call through just one API – api_call(chat.postMessage, channel=channel, text=msg, attachments=attachment, as_user=True)

You can also upload a file to a channel. However, when I was searching some info how to achieve that at the first time, the answer described in the SlackClient’s Github issue was actually wrong. So I tested and added a snippet. Please check the issue page.

Anyway, I also shared the snippet at the public gist. The ‘upload_file’ method takes a long text as the ‘content’ param and upload it as a file. You’ll also upload a file directory from disk or memory if you modify it slightly.



Running a CCTV Telegram Bot on your Pi


Telepot is a python based telegram bot framework which is lightweight, easy to use and supports all of the up-to-date telegram features. For example, I was able to write a small telegram bot running on Pi2 which takes and sends a photo with only few lines using it. Here is how.

1) Although Telepot supports python 2.7 without async feature, I wanted to use Python 3.x. However, the latest Telepot supports async requires Python 3.5 so I had to download and build Python 3.5 on my Pi2. I followed the instruction introduced here https://sowingseasons.com/blog/software/2016/01/building-python-3-4-on-raspberry-pi-2/19864079 except using python source https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

2) Telepot also provides basic samples. You can also find out other API usages through test code. ‘couter/countera’ example is simple but a good start points if you only require receiving telegram message and sending a reply back.

3) Of course you’ll need a Telegram Bot account. There’s a bot named ‘@BotFather’ which does making a bot for you and makes you setting basic info of it. Just initiate a talk to the account and detailed instruction described here: https://core.telegram.org/bots

4) I used the https://github.com/nickoala/telepot/blob/master/examples/chat/countera.py as the starting point. From the source, MessageCounter.on_chat_message will be called responds to the user’s message. If you want to make the CCTV like telegram bot, then just take a picture and send it back within the method.

5) Takes a picture using the picamera module and replies with it. Remove the temporary saved picture after sending it.

6) here is the result: https://gist.github.com/heejune/afd77aeec49e836fa549fc025962ddd8

Writing a python build script for your Visual C++ project

If you’ve decided to write your own python build script for existing Visual Studio projects, then two questions would probably come up at first.
  1. What utility/commands are available for actually building *.sln/*. vcxproj files?
  2. How will you specify the appropriate version for your *.rc version resource file?
Solution for #1. You can download the MSBuild tools for free from the Microsoft download website, and it provides the solution you need. First, download the MS Build tools 2013(or 2015) depending on your Visual Studio project versions.
Solution for #2. You can easily modify/set the version number string on your *.rc resource file by using python regex. Here is the example.

Downloading a file on the fly from Flask+sqlalchemy

Following code snippet shows how to implement downloading a file with flask.


def index():
    strIO = StringIO.StringIO()
    strIO.write('Hello from Dan Jacob and Stephane Wirtel !')
    return send_file(strIO,

In case you need to use the snippet to support downloading a file which is stored as BLOB originally and to convert it as a downloadable file stream on the fly, then you can use io.BytesIO. See the below:

Let’s say exampleModel is the Sqlalchemy model object and blobField is a blob field which stores blob binary data,

buffer = io.BytesIO(exampleModel.blobField)
buffer.seek(0)    // You don’t need this
return send_file(buffer, attachment_filename=filename)



Running Python Flask on a local IIS (not Azure!) with wfastcgi.py

There are already a lot of resources dealing with how to setup Django with IIS, or configure IIS to support WSGI with various tools such as Helicon Zoo, isapi-wsgi, and FastCGI.

FastCGI looked good among others because I wanted to go with least 3rd party stack and the Microsoft distribute the wfastcgi.py through PTVS. However, there’s a still one problem. I couldn’t find the document explains how to set up a local IIS to work with FastCGI and Flask.

Thanks to PTVS, it’s already well implemented and provided. The PTVS contains the wfastcgi.py, and web.config which set required environments to run. Here’s more detailed step:

1. Install wfastcgi.py. The PTVS official homepage has the link to download wfastcgi.py MSI installer.



The wfastcgi MSI installer creates local copies of wfastcgi.py to python installed directory as below:


2. Install FastCGI to IIS and add wfastcgi.py handler to it. The official PTVS page has the required command to ease the steps here:


start /wait %windir%\System32\\PkgMgr.exe /iu:IIS-CGI

%windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCGI “/+[fullPath=’c:\Python27\python.exe’, arguments=’C:\Python27\Scripts\wfastcgi.py’]”

The wfastcgi.py MSI installer also seems to add the handlers above automatically if FastCGI is already configured, but it’s not sure. You can now see the FastCGI listed installed Python paths.


3. Now it’s time to edit the web.config for Flask/IIS. If you created the Flask project with PTVS addon, it already contains the required web.config to support deploy to Azure. However, in order to run it on local IIS, it needs a little modification. Let’s see the original web.config first PTVS generated.

<?xml version=”1.0″?>
<!– Generated web.config for Microsoft Azure. Remove this comment to prevent
modifications being overwritten when publishing the project.
<add type=”Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ name=”AzureDiagnostics”>
<filter type=”” />
<add key=”WSGI_ALT_VIRTUALENV_HANDLER” value=”FlaskWebProject1.app” />
<add key=”WSGI_ALT_VIRTUALENV_ACTIVATE_THIS” value=”%ROOTDIR%\env\Scripts\activate_this.py” />
<add key=”WSGI_HANDLER” value=”ptvs_virtualenv_proxy.get_virtualenv_handler()” />
<add key=”PYTHONPATH” value=”%ROOTDIR%” />
<compilation debug=”true” targetFramework=”4.0″ />
<modules runAllManagedModulesForAllRequests=”true” />
<add name=”Python FastCGI” path=”handler.fcgi” verb=”*” modules=”FastCgiModule” scriptProcessor=”%INTERPRETERPATH%|%WFASTCGIPATH%” resourceType=”Unspecified” requireAccess=”Script” />
<rule name=”Static Files” stopProcessing=”true”>
<match url=”^/static/.*” ignoreCase=”true” />
<action type=”Rewrite” url=”^/FlaskWebProject1/static/.*” appendQueryString=”true” />
<rule name=”Configure Python” stopProcessing=”true”>
<match url=”(.*)” ignoreCase=”false” />
<action type=”Rewrite” url=”handler.fcgi/{R:1}” appendQueryString=”true” />

Few things. First, I removed the <system.diagnostics> section which installs Azure diags listener. Second, the IIS cannot understand the %ROOTDIR% variable so you should convert it all absolute path for that server you about to run.

So it’ll look like this:

<add key=”WSGI_ALT_VIRTUALENV_HANDLER” value=”FlaskWebProject1.app” />
<add key=”WSGI_ALT_VIRTUALENV_ACTIVATE_THIS” value=”D:\repo\web\FlaskWebProject1\FlaskWebProject1\env\Scripts\activate_this.py” />
<add key=”WSGI_HANDLER” value=”ptvs_virtualenv_proxy.get_virtualenv_handler()” />
<add key=”PYTHONPATH” value=”D:\repo\web\FlaskWebProject1\FlaskWebProject1” />

The generated web.config has a python handler mapped, but the path also needs to be updated to point the direct wfastcgi.py path as follows:

<add name=”Python FastCGI” path=”handler.fcgi” verb=”*” modules=”FastCgiModule” scriptProcessor=”C:\Python34\python.exe|C:\Python34\Scripts\wfastcgi.py” resourceType=”Unspecified” requireAccess=”Script” />

It’s enough to edit a web.config. However, if you still encounter a strange 500 internal error, please make sure the local IIS has the URL Rewrite 2.0 module installed. In my case, whenever I tried to open the edited web.config from IIS manager, it gives me that “There was an error while performing this operation. … web.config” error so that I couldn’t open a web.config nor edit mapping handlers. Fortunately, I found those answers from SO and solved it by installing URL Rewrite 2.0 through the ‘Web Platform Installer’. Microsoft Download site also provides a standalone exe.




Done. If the IIS successfully loaded web.config, then it’ll show the correctly mapped Python wfastcgi handler at ‘Handler Mappings’ as follows:

handler1 handler2

If you have any questions, please leave a comment. Thanks for reading.

Extending Python with Visual C++ and PTVS

Last month there was a chance I gave a talk explaining how to start the PTVS(Python Tools for Visual Studio) through the MS virtual academy. The talk was live recorded and currently is being shared following URL:


It’s Korean though. Throughout the demo, except the very basic examples I also included demos showing how to build a Python extension with C++ which is integrated greatly with Visual Studio C++ compiler and the PTVS, and how to debug cross platform python modules running on Raspberry Pi.

Although the C++ python extension example I wrote was super simple and copy&pasted almost from python.org example, I uploaded mine here including C++ and python sample sources because it wasn’t easy to find as complete visual studio solution form.

GitHub repo URL: https://github.com/heejune/demo-python-PyCExtDemo1

Also if you want to start building a C++ python extension with Visual Studio and PTVS please refer to following steps:

1. Specify the include directory contains python headers.


2. Add python27.lib or appropriate version corresponds to.


3. Add libs directory into additional lib path.


4. Lastly, don’t forget enabling ‘Mixed mode debugging’ and matching python environment’s architecture version with python extension architecture (as follows)