SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
Cuchulainn
Posts: 59926
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

October 5th, 2019, 6:00 pm




I "like" this inconsistency between how core Python and Numpy assign a boolean value to a list containing one element equal zero:
bool([0])
Out[94]: True

bool(np.array([0]))
Out[95]: False
Putting bool in arrays is asking for trouble. C++ wrestled with this problem for years (Boost banned vector<bool>)

https://en.cppreference.com/w/cpp/container/vector_bool

This looks interesting
https://pypi.org/project/bitsets/
 
User avatar
FaridMoussaoui
Posts: 433
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

Re: Python tricks

October 5th, 2019, 6:07 pm

anyway, if he applied the bool method to an array with a length > 1, he will got an error.
 
User avatar
ISayMoo
Posts: 2050
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

October 5th, 2019, 7:53 pm

I "like" this inconsistency between how core Python and Numpy assign a boolean value to a list containing one element equal zero:
bool([0])
Out[94]: True

bool(np.array([0]))
Out[95]: False
by the very definition of the bool method:  "objects of Classes which has __bool__() or __len()__ method which returns 0 or False" is considered false
The first one is a list of length 1. The bool method on the second one is false.
I know... "this submarine is yellow because they painted it yellow". the point is that returning false on the 2nd one is surprising to many people.
 
User avatar
ISayMoo
Posts: 2050
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

October 5th, 2019, 7:54 pm




I "like" this inconsistency between how core Python and Numpy assign a boolean value to a list containing one element equal zero:
bool([0])
Out[94]: True

bool(np.array([0]))
Out[95]: False
Putting bool in arrays is asking for trouble. C++ wrestled with this problem for years (Boost banned vector<bool>)

https://en.cppreference.com/w/cpp/container/vector_bool

This looks interesting
https://pypi.org/project/bitsets/
No bools in those arrays.
 
User avatar
ISayMoo
Posts: 2050
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

October 5th, 2019, 7:55 pm

  • "C++ treats you like a consenting adult. Python treats you like a naughty child. Ada treats you like a criminal"
BTW, Mr Stroustroup visited my company recently, and listed one of our AI projects among his examples of "best uses of C++" :D :D :D
Congratulations!
What did he say about Python in general?
No idea. I have to listen to the recording (I couldn't attend). I think he focused on C++.
 
User avatar
katastrofa
Posts: 8135
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Re: Python tricks

October 5th, 2019, 9:40 pm

In [25]: bool(pd.Categorical([0]))
Out[25]: True

In [26]: bool(pandas.Series([0]))
Traceback (most recent call last):

File "<ipython-input-27-f1f2e5cd2711>", line 1, in <module>
bool(pandas.Series([0]))

File "C:\Users\Roman\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1576, in __nonzero__
.format(self.__class__.__name__))

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


https://www.youtube.com/watch?v=X21mJh6j9i4
 
User avatar
FaridMoussaoui
Posts: 433
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

Re: Python tricks

October 5th, 2019, 10:24 pm

 File "C:\Users\Roman\Anaconda3\lib\site-packages\pandas\core\generic.py"
You're using someone else's account !!!
 
User avatar
katastrofa
Posts: 8135
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Re: Python tricks

October 5th, 2019, 10:44 pm

Worse. I stole someone's laptop.
 
User avatar
Cuchulainn
Posts: 59926
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

October 6th, 2019, 10:35 am

 File "C:\Users\Roman\Anaconda3\lib\site-packages\pandas\core\generic.py"
You're using someone else's account !!!
When in Rome do as the Romans do!
 
User avatar
Cuchulainn
Posts: 59926
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

October 6th, 2019, 10:40 am

https://www.python.org/dev/peps/pep-0020/

"There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch."
print("One way to do", it)
print("Another way to do %s" % it)
print("And another way to do {}".format(it))
print("And another way to do " + it)
Maybe ask a Dutch friend?
 
User avatar
tagoma
Topic Author
Posts: 18259
Joined: February 21st, 2010, 12:58 pm

Re: Python tricks

October 7th, 2019, 6:05 am

https://www.python.org/dev/peps/pep-0020/

"There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch."
print("One way to do", it)
print("Another way to do %s" % it)
print("And another way to do {}".format(it))
print("And another way to do " + it)
Maybe ask a Dutch friend?
''.join(['Hello', ' the Netherlands!'])
 
User avatar
Cuchulainn
Posts: 59926
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

October 7th, 2019, 1:27 pm

"keep in touch with the Dutch" :-)

Possibly the most famous Dutch CS was Edsger Dijkstra.

"All" is revealed about management and developer psyche @11.00

www.youtube.com/watch?v=RCCigccBzIU
 
User avatar
Cuchulainn
Posts: 59926
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

October 10th, 2019, 1:48 pm

Stupid question: someone has a 10-year old Mac and is using VM stuff to do Python on top of VS 2019.
Is there a free cloud env so that she can run Python stuff w/o having to install the stuff?

(I know, just buy a laptop and be done with is better)
 
User avatar
FaridMoussaoui
Posts: 433
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

Re: Python tricks

October 10th, 2019, 2:01 pm

What stuff?

Use for example pyCharm and stay away from VS
 
User avatar
Cuchulainn
Posts: 59926
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

October 10th, 2019, 3:04 pm

What stuff?

Use for example pyCharm and stay away from VS
These are real newbies. VS is easy. 
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On