pyson and '~'

60 views
Skip to first unread message

Marko Randjelovic

unread,
Jul 21, 2016, 3:30:16 AM7/21/16
to tryton
Let us look at pyson expresson:

If(~Eval('end_date', None)

What is ~? Experimenting with Python interpreter, ~True is -2 and ~False is -1. I thought ~ is for negation, but -1 and -2 are not boolean values even in C they are 0 or >0.

Sergi Almacellas Abellana

unread,
Jul 21, 2016, 4:05:02 AM7/21/16
to try...@googlegroups.com
El 20/07/16 a les 17:11, Marko Randjelovic ha escrit:
In python ~ is the shourcut of Bitwise Inversion [1].
In PySON is the shourtcut of the Not operator: So your statement means:

> If(Not(Eval('end_date', None))

Which I think you will understand :)

[1]
https://docs.python.org/2/library/operator.html#mapping-operators-to-functions
--
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk

Marko Randjelovic

unread,
Jul 22, 2016, 1:00:04 AM7/22/16
to tryton
On Thursday, July 21, 2016 at 10:05:02 AM UTC+2, Sergi Almacellas Abellana wrote:
El 20/07/16 a les 17:11, Marko Randjelovic ha escrit:
> Let us look at pyson expresson:
>
> If(~Eval('end_date', None)
>
> What is ~? Experimenting with Python interpreter, ~True is -2 and ~False
> is -1. I thought ~ is for negation, but -1 and -2 are not boolean values
> even in C they are 0 or >0.

In python ~ is the shourcut of Bitwise Inversion [1].
In PySON is the shourtcut of the Not operator: So your statement means:

 > If(Not(Eval('end_date', None))

Which I think you will understand :)

Of course, but in examples I have neither ~ nor Not is imported from trytond.pyson. How Python knows about new character of '~'? Also, how is achieved possible to use ~ as an operator (there are no parenthesis)?
 

Cédric Krier

unread,
Jul 22, 2016, 3:25:09 AM7/22/16
to tryton
On 2016-07-21 12:40, Marko Randjelovic wrote:
> Of course, but in examples I have neither ~ nor Not is imported from
> trytond.pyson. How Python knows about new character of '~'? Also, how is
> achieved possible to use ~ as an operator (there are no parenthesis)?

'~' is a standard python operator so it does not need to be imported.
See https://docs.python.org/2/library/operator.html#operator.__invert__

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Marko Randjelovic

unread,
Jul 23, 2016, 2:40:03 PM7/23/16
to tryton


On Friday, July 22, 2016 at 9:25:09 AM UTC+2, Cédric Krier wrote:
On 2016-07-21 12:40, Marko Randjelovic wrote:
> Of course, but in examples I have neither ~ nor Not is imported from
> trytond.pyson. How Python knows about new character of '~'? Also, how is
> achieved possible to use ~ as an operator (there are no parenthesis)?

'~' is a standard python operator so it does not need to be imported.
See https://docs.python.org/2/library/operator.html#operator.__invert__

~ is a standard operator but it's purpose is bitwise inversion. How did we make it become logical negation?
 

Sergi Almacellas Abellana

unread,
Jul 25, 2016, 3:14:53 AM7/25/16
to try...@googlegroups.com
El 23/07/16 a les 19:02, Marko Randjelovic ha escrit:
>
>
> On Friday, July 22, 2016 at 9:25:09 AM UTC+2, Cédric Krier wrote:
>
> On 2016-07-21 12:40, Marko Randjelovic wrote:
> > Of course, but in examples I have neither ~ nor Not is imported from
> > trytond.pyson. How Python knows about new character of '~'? Also,
> how is
> > achieved possible to use ~ as an operator (there are no parenthesis)?
>
> '~' is a standard python operator so it does not need to be imported.
> See
> https://docs.python.org/2/library/operator.html#operator.__invert__
> <https://docs.python.org/2/library/operator.html#operator.__invert__>
>
>
> ~ is a standard operator but it's purpose is bitwise inversion. How did
> we make it become logical negation?
Because we define the __invert__ operator of every PYSON object:

http://hg.tryton.org/trytond/file/9a65b63b90f4/trytond/pyson.py#l40

Marko Randjelovic

unread,
Jul 25, 2016, 6:35:04 PM7/25/16
to tryton

But there is no mention of the '~' symbol in whole this file. How do we connect this code to the '~' symbol?

Regards,
Marko
 

Cédric Krier

unread,
Jul 25, 2016, 6:55:20 PM7/25/16
to tryton
On 2016-07-25 13:07, Marko Randjelovic wrote:
> But there is no mention of the '~' symbol in whole this file. How do we
> connect this code to the '~' symbol?

Please read carefully:
https://docs.python.org/2/library/operator.html#operator.__invert__
Reply all
Reply to author
Forward
0 new messages