Quantitative Analysis, Risk Management, Modelling, Algo Trading, and Big Data Analysis

Talking to Python: TextMate Code Editor

 

Programming in Python requires a decent text editor. A decent is a powerful word here. With a dramatic improvement in this field over past decade, anyone who wishes to start his adventure with coding should consider what sort of Integrated Development Environment would be most suitable for his needs. It’s pretty difficult to address all related questions when you are the beginner to programming in Python. A quick scan of the Web suggests one the following solutions for Mac: Vico, TextWrangler, EMacs, TextMate, Eclipse, etc. It is also possible to run Python project under Xcode (see here).


Within this Python for Quants course we will make use of fairly simple but dynamically evolving code editor of TextMate. There are no special criteria standing behind this editor but it does a hell of the job for us as we gain our expertise in Python. The best way to summarize its capability is to quote James Gray as follows. TextMate is a full-featured text editor available for Mac OS X that can greatly enhance your text manipulation skills. TextMate is actually a thin shell over a personalized team of robot ninjas ready to do your bidding. It uses built-in automations for Python. You can manage all the files in your projects, fly through your files with easy navigation techniques, master quick and dirty text editing with strong regular expression integration.

Sound appealing, convincing? Well, you need to start somewhere and this a good place to start.

Installation

One sentence should be enough at this point. Visit TextMate’s website and proceed with download and installation. But one sentence is always not enough. If you already have migrated to Mavericks OS X, the team recommends v2.0-alpha for this platform. You can download and start using it for free in a stripped configuration. This will be more than sufficient for us at the beginning. But if you think more seriously about coding as your new life’s calling, you should consider going for an upgrade.

Editor

We will need a plain sheet of electronic paper to write the code. As soon as we start writing the Python code, a good practice is to save the file at the very beginning. A world-wide convention among coders is to name all source Python listings with an extension of .py. To convince yourself about TextMate capabilities across different languages explore the Bundles from the main menu:

ed1

Now, if you type into editor the simplest two lines in Python:

# Your very first script
 
print 'Hello, world!'

and press ⌘R you should be able to execute the code. A side window pops up with the desired outcome:

ed2

Please note that coding in Python and code execution in TextMate is not available for interactive commands like input in the following example:

foo=input('Please enter a value:')

It may sound as a terrible experience but a professional coding nowadays eliminates this interactions to the absolute minimum. You can do it always from the command line python in the Terminal:

ed3

Additional Configuration

If you followed the configuration step from the previous Part 1 and installed all additional Python libraries in the folder of your choice, you may meet some difficulties in reaching for them in TextMate. It took me 15 minutes to find the way to correct that inconvenience which was not so obvious at the beginning.

Firstly, make sure the PATH is set properly and includes the access to the python executables. Select from Menu Preferences.. tab ‘Variables’ and edit properly, for instance:

ed4

Secondly, you will amend one line in the TextMate’s script responsible for running your codes while you press ⌘R. Go in menu to Bundles, next to Select Bundle Item… and in the search box of a new window type ‘Run Script’. You should see a limited option pointing at Python reference. Double click will take you to the ⌘R’s command editor.

ed5

Around line #13, by default TextMate looks for python installed in your OS X system and tries to execute:

TextMate::Executor.run(ENV["TM_PYTHON"] || "python", "-u", ENV["TM_FILEPATH"] ...

line. We just need to eliminate the inner reference to the variables ENV[“TM_PYTHON”] || “python” and replace them with a path to your python, e.g.:

TextMate::Executor.run("~/env/bash27/bin/python", "-u", ENV["TM_FILEPATH"] ...

That should resolve all major issues related to getting an access to the additional libraries installed by you manually.

Stay Tuned!

A new ebook on Python for Quants is coming this July! Don’t miss the full introductory course to Python with 300+ practical examples ready-to-rerun and use for your code building:

Python for Quants small

Contact Form Powered By : XYZScripts.com