Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Thursday, April 28, 2022

Sending code to debug console in Visual Studio for Python

 Similar to this advice I found on stackoverflow, I did the following to be able keyboard debug my Python code:

File > Preferences > Keyboard Shortcuts

Then in the top right there is an icon to Open Keyboard Shortcuts (JSON), in there I put:

[
    {
        "key": "shift+c",
        "command": "editor.debug.action.selectionToRepl"
    }
]

BOOM!  Now I can highlight my code, and hit Shift+c to send the snippet to the debug console.  

NOTE: I used Shift+c as it is similar to C-c C-c in Emacs, and C-c is taken in Windows for Copy.


EDIT:  Oops, I can't use capital C with this, so instead I am now using Ctrl+alt+enter

// Place your key bindings in this file to override the defaults
[
    {
        "key": "ctrl+alt+enter",
        "command": "editor.debug.action.selectionToRepl"
    }
]

Wednesday, August 30, 2017

Kicking off processes from a web interface

In a previous job I had, we did a lot of work with Django as a front end for the control of our analytical system.  In the process, someone used cron to track the system frequently in order to be able to pick up toggles input through the UI.  I wasn't the biggest fan of this approach, but that's a story for another day, because the reality is that I couldn't think of a better approach.  Enter my recent discovery of Celery.  I have been wanting to try this out, because it might be exactly what I'm looking for when it comes to this, and I want to save this tutorial on just a proof of concept:


So, I'll have to have a follow-up blog with my experience with using it.

P.S. He also has a thorough Flask tutorial as well:



Monday, March 16, 2015

Python String Format Cookbook

As I'm tutoring someone through their introductory Computer Science course, I keep finding myself getting caught up in the Python 3 string formatting.  His assignments seem to focus a lot on how to print out various formats, and I'm still stuck in handling how they worked in Python 2.7.  I keep googling, and googling, and I often just find myself coming back to this reference.

Python String Format Cookbook

I'm just putting this up so that I can keep going back to this reference as I still adjust from Python 2 to Python 3.

EDIT: Just saw this cool writeup on Reddit: PyFormat.info

Monday, February 23, 2015

Spatial Distances in Python with GeoPy

So as I'm redoing my dissertation work, one of the functions I used was to calculate the distance between two locations based upon their coordinates (latitude and longitude).  At the time, I was referencing and using what is commonly referred to as the Great Circle distance.  So in the middle of my re-write into Python I stumbled across:

GeoPy

They not only provide the Great Circle distance calculation in their module, they introduced me to the Vincenty Distance.  According to the module authors this is a more accurate approach for calculating the distance.

It looks like I have a new way to calculate my spatial distances, and even better, I don't have to program it up myself.  I'm loving this Python re-write process already.

Time Series Analysis in Python with statsmodels

Wow, what a phenomenal discussion on Time Series analysis in Python.  I was unaware of this statsmodels project, but now I'm psyched to find it.  First of all, let me link to the talk that I'm referring to in my title:

Time Series Analysis in Python with statsmodels

Of course, this lead me to tracking down these experts, to learn more about what they do and wow I'm quite impressed.  Here are links to their blogs:

Wes McKinney
Josef Perktold
Skipper Seabold

These three seem to be very involved in Scientific Computing in Python, check out their blogs and links to talks, etc.  I know I will.



Sunday, February 22, 2015

Navigating Python documentation in Emacs

I've been a bit frustrated with my ability to navigate Python documentation in Emacs.  I came across the pydoc command, which seems very useful.  Then I wondered how I could leverage this in emacs, maybe write a lisp function in my .emacs file?

Enter a simple google search for "emacs pydoc", and you come across this fine project by John Kitchin.  He has a summary of it here:


Now, I love my elpy setup, so I thought I'd propose adding this into it.  No promises from Jorgen, but he did say that at the very least it needs to be its own package.

John, of course is very busy, and I've always wanted to do something like this in Emacs, so I volunteered to take on this project.  So get ready to follow along on how I create a MELPA distributed Emacs package. Yay, this should be fun. :)

You can follow along on github:

https://github.com/statmobile/pydoc

Tuesday, February 17, 2015

Web Interface for Python

An ongoing project I've been wanting propose to a certain very large government agency involves creating a front-end for Python algorithms.  The first question is what should the interface be:
  • GUI - Would be great, but would need to be developed for all OS's including Mobile in my vision.  How fancy do I get, do I use Tcl/Tk, or even Qt?  I'm starting to feel overwhelmed already with all the Python GUI frameworks.  
  • Web - This would be ideal, as long as it follows standard HTML standards, then one deployment should work for everyone with Internet access.  But how?  Django doesn't seem to be interactive enough for my needs and also seems a bit of an overkill with the ORM.  Flask seems to be too light.  Hmm, and then I got some advice from the author of http://pythonprogramming.net on Reddit.
He pointed me to two amazing projects, and I'm seriously thinking about diving back into this proposal.  He recommended I look into the following projects:

"Brython is designed to replace Javascript as the scripting language for the Web. As such, it is a Python 3 implementation (you can take it for a test drive through a web console), adapted to the HTML5 environment, that is to say with an interface to the DOM objects and events"
Trinket:
"Trinket lets you run and write code in any browser, on any device.
Trinkets work instantly, with no need to log in, download plugins, or install software.
Easily share or embed the code with your changes when you're done."


These look to be two amazing projects that I think I could leverage.  Here, check it out.

Trinket: Brython:

Sunday, January 11, 2015

EDIT: Use scipy.linalg over numpy.linalg.

Per my previous post, I mistakenly referenced numpy.linalg and scipy.linalg as if they were the same.  Upon looking deeper at the documentation for scipy.linalg it clearly states the following:

scipy.linalg contains all the functions in numpy.linalg. plus some other more advanced ones not contained in numpy.linalg
Another advantage of using scipy.linalg over numpy.linalg is that it is always compiled with BLAS/LAPACK support, while for numpy this is optional. Therefore, the scipy version might be faster depending on how numpy was installed.
Therefore, unless you don’t want to add scipy as a dependency to your numpy program, use scipy.linalg instead of numpy.linalg
So for all my purposes, I will only use scipy.linalg.

LAPACK in Python and R

EDIT: See my follow-up post as well!

While porting my dissertation work that I wrote in R to Python, I need to leverage some of the great features of R such as its easy to use wrappers of LAPACK, specifically the Cholesky Decomposition to calculate the inverse of my covariance matrix.  For those not familiar with LAPACK, it's a free open source library for calculating Linear Algebra routines.  I'm talking all of them, and it's included in many open source scientific software due to its wide range of applicability and free and open nature.  The one caveat... It's written in Fortran*.

Now, not to hate on Fortran, but not many people are programming their software or running their data analysis using it.  Fortunately for us, some very good computer nerds out there wrote awesome wrappers in R and in Python (through NumPy and Scipy) to access them.  It's been recommended to me that one should run optimized LAPACK libraries for your processor and Operating System, and build R (and probably NumPy and SciPy) pointing to those optimized routines.  I'll let you read through the R Administration Manual to decide for yourself.

You can find the R discussion on LAPACK routines in the R Extensions Manual here.

As for Python, just check out the documentation for numpy.linalg or scipy.linalg.

Okay, now the real point of this note to myself, is because Googling access to LAPACK in Python led me to this awesome Blog Post:

Linear Solve in Python (NumPy and SciPy)

  did an awesome tutorial on using Cholesky Decomposition, and I thought I'd pass it along to anybody interested in leveraging these routines.

* I vaguely recall reading somewhere that LAPACK is usually compiled in C after porting LAPACK from Fortran to C using f2c.