Serving the Quantitative Finance Community

 
User avatar
Cuchulainn
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Python tricks

August 20th, 2019, 7:54 am

Undocumented code?(?)
 
User avatar
Cuchulainn
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Python tricks

August 21st, 2019, 4:35 pm

np.allclose() is kind of handy
 
User avatar
Cuchulainn
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Python tricks

September 3rd, 2019, 8:37 pm

Indentation is scary; 2 code block that run but give different answers
one TAB too many means 0.5 versus 0.98
for n in range(N):
        ynP1 = y + h*f(x+h/2, y + h*f(x,y)/2)

		# Go to next level
        x += h;
        y = ynP1;

    return ynP1

OR

 for n in range(N):
        ynP1 = y + h*f(x+h/2, y + h*f(x,y)/2)

		# Go to next level
        x += h;
        y = ynP1;

        return ynP1
 
User avatar
FaridMoussaoui
Posts: 327
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

Re: Python tricks

September 4th, 2019, 7:17 pm

my everything editor is emacs. The (right) tabing is done automagically through plugins.
For python, I use Tim Peters plugin available here https://launchpad.net/python-mode
Last edited by FaridMoussaoui on September 6th, 2019, 7:30 am, edited 1 time in total.
 
User avatar
ISayMoo
Posts: 2332
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

September 4th, 2019, 11:11 pm

Indentation is scary; 2 code block that run but give different answers
one TAB too many means 0.5 versus 0.98
for n in range(N):
        ynP1 = y + h*f(x+h/2, y + h*f(x,y)/2)

 # Go to next level
        x += h;
        y = ynP1;

    return ynP1

OR

 for n in range(N):
        ynP1 = y + h*f(x+h/2, y + h*f(x,y)/2)

 # Go to next level
        x += h;
        y = ynP1;

        return ynP1
The first one would not pass the linter. No indentation without an if, for etc.
 
User avatar
Cuchulainn
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Python tricks

September 11th, 2019, 10:05 am

scipy.optimize is super to use.
Now why can't the C++ folk (Boost) just do the same by creating a wrapper for those original Fortran and C libraries.
Named parameters are a godsend. Boost has them but not {C++11,C++20]??
 
User avatar
Cuchulainn
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Python tricks

September 11th, 2019, 10:21 am

Indentation is scary; 2 code block that run but give different answers
one TAB too many means 0.5 versus 0.98

The first one would not pass the linter. No indentation without an if, for etc.
Can we see the linter as a pre-compiler that we need to 'execute' before running?
 
User avatar
ISayMoo
Posts: 2332
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

September 11th, 2019, 10:27 am

Yes, And mypy for type annotations.
 
User avatar
ISayMoo
Posts: 2332
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

September 11th, 2019, 10:28 am

scipy.optimize is super to use.
Now why can't the C++ folk (Boost) just do the same by creating a wrapper for those original Fortran and C libraries.
I am sure you know this library: https://nlopt.readthedocs.io/en/latest/
 
User avatar
Cuchulainn
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Python tricks

September 11th, 2019, 4:48 pm

scipy.optimize is super to use.
Now why can't the C++ folk (Boost) just do the same by creating a wrapper for those original Fortran and C libraries.
I am sure you know this library: https://nlopt.readthedocs.io/en/latest/
Cool. I installed it from Python and ran a POC test. Looks good. Just that
def myfunc(x, grad):

    return -1

not work while (invalid argument)
def myfunc(x, grad):

    return -1.0

did.

hmmm..
 
User avatar
FaridMoussaoui
Posts: 327
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

Re: Python tricks

September 11th, 2019, 7:13 pm

If you check the source code on github,  it expects a double as an output for the function. If not,  it will throw an invalid argument error message.
 
User avatar
Cuchulainn
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Python tricks

September 12th, 2019, 7:49 am

If you check the source code on github,  it expects a double as an output for the function. If not,  it will throw an invalid argument error message.
I used 1. instead of 1 myself (in FORTRAN REAL*x = 1.0,  x .EQ. 1 always FALSE afair), newbies might get confused. I wonder where and by whom this type checking takes place?
I manage to pip nlopt directly.
Looks like a nice library.
 
User avatar
FaridMoussaoui
Posts: 327
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

Re: Python tricks

September 12th, 2019, 8:23 am

Looking to source code, you can also see how to use SWIG to interface the C library to python.

If possible, I always install "packages" from source code. That's a good practise I learned working on Linux.
 
User avatar
Cuchulainn
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Python tricks

September 12th, 2019, 9:33 am

Looking to source code, you can also see how to use SWIG to interface the C library to python.

If possible, I always install "packages" from source code. That's a good practise I learned working on Linux.
Good point. 

Having precise black-box contract specifications would obviate the need for source code inspection.
 
User avatar
Cuchulainn
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Python tricks

September 12th, 2019, 8:18 pm

any one use scipy.interpolate.KroghInterpolate?
a bit quaint or maybe lost in action?