SERVING THE QUANTITATIVE FINANCE COMMUNITY

Cuchulainn
Posts: 61081
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

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/
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

FaridMoussaoui
Posts: 497
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

### Re: Python tricks

anyway, if he applied the bool method to an array with a length > 1, he will got an error.

ISayMoo
Posts: 2267
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

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.

ISayMoo
Posts: 2267
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

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.

ISayMoo
Posts: 2267
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

• "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++"
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++.

katastrofa
Posts: 8727
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

### Re: Python tricks

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().

FaridMoussaoui
Posts: 497
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

### Re: Python tricks

File "C:\Users\Roman\Anaconda3\lib\site-packages\pandas\core\generic.py"
You're using someone else's account !!!

katastrofa
Posts: 8727
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

### Re: Python tricks

Worse. I stole someone's laptop.

Cuchulainn
Posts: 61081
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

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!
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Cuchulainn
Posts: 61081
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

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)
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

tagoma
Topic Author
Posts: 18351
Joined: February 21st, 2010, 12:58 pm

### Re: Python tricks

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)
''.join(['Hello', ' the Netherlands!'])

Cuchulainn
Posts: 61081
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

"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

http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

Cuchulainn
Posts: 61081
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

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)
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

FaridMoussaoui
Posts: 497
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

### Re: Python tricks

What stuff?

Use for example pyCharm and stay away from VS

Cuchulainn
Posts: 61081
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

What stuff?

Use for example pyCharm and stay away from VS
These are real newbies. VS is easy.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget

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

 JOBS BOARD

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

GZIP: On