Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

I am NOMAD!


devel / comp.lang.python / Fallback for operator and other dunder methods

SubjectAuthor
* Fallback for operator and other dunder methodsDom Grigonis
`* Re: Fallback for operator and other dunder methodsStefan Ram
 `- Re: Fallback for operator and other dunder methodsStefan Ram

1
Fallback for operator and other dunder methods

<mailman.194.1690338142.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23826&group=comp.lang.python#23826

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: dom.grig...@gmail.com (Dom Grigonis)
Newsgroups: comp.lang.python
Subject: Fallback for operator and other dunder methods
Date: Wed, 26 Jul 2023 05:22:18 +0300
Lines: 31
Message-ID: <mailman.194.1690338142.23016.python-list@python.org>
References: <2D6F0153-5FAE-49A1-A5B0-23CF9AAED4E7@gmail.com>
Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.15\))
Content-Type: text/plain;
charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de aqavF71yEmjWWCjUpUFErwPC0aGQfrVDStty76XaFfpg==
Cancel-Lock: sha1:RuIgDJCooMprysMBFE8+5frTT4k= sha256:FwBgkkFi+zGrCb6R24aHDGEsmsMPhD5rxbZ+Lv/8hYI=
Return-Path: <dom.grigonis@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=SskLoW6o;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.021
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'def': 0.04; '(e.g.': 0.05;
'objects,': 0.09; 'operators': 0.09; 'typeerror:': 0.09;
'received:apple': 0.16; 'received:mail-lj1-x233.google.com': 0.16;
'received:smtpclient.apple': 0.16; 'sensible': 0.16;
'wouldn\xe2\x80\x99t': 0.16; 'wrapper': 0.16; 'message-
id:@gmail.com': 0.18; 'to:addr:python-list': 0.20; 'skip:_ 10':
0.22; 'seem': 0.31; 'looked': 0.31; 'but': 0.32; 'subject:for':
0.33; 'there': 0.33; 'received:google.com': 0.34; 'trying': 0.35;
'from:addr:gmail.com': 0.35; 'class': 0.37; 'this.': 0.37; 'way':
0.38; 'this,': 0.39; '8bit%:20': 0.61; 'ever': 0.63; 'skip:\xe2
20': 0.69; 'subject:other': 0.69; 'ability': 0.71; 'enters': 0.84;
'method,': 0.84; 'received:88': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20221208; t=1690338140; x=1690942940;
h=to:date:message-id:subject:mime-version:from:from:to:cc:subject
:date:message-id:reply-to;
bh=wFs145DD2HnkIhIX/58JrRe6Ya0AAqgb+NfxUFgWO4k=;
b=SskLoW6oIwb1754PXl0yp5dlEMkFOcAsWi1Qiwkl6Ucly0G0SqY/GTj04d93QE0UNH
Abxe0ilOuDiKTaVdBfAokkpyctxm+yLGm4j14lvIBwDGCENnMcMIIcA60x8JgBoBiYPE
4JjpWW7DqiL1WCr+F5l6JBLBDLjUdnQT1h8r2Pq1dGDm1oyBtj0qIL2wCZRnKhVkoi/r
V2EEEzZVnx44IHSTi9PuCcxmMiDGoBBKH23cgbk/MLrEYR4J7UrmzJhVCJGSRDMLPasr
x9Ejsk38roSL8bhLTf8Nw9PPOHSD10r2YjBuHmVCzcI8KAJcQvTxd8y0V5ktCDXF0BDx
UBuQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20221208; t=1690338140; x=1690942940;
h=to:date:message-id:subject:mime-version:from:x-gm-message-state
:from:to:cc:subject:date:message-id:reply-to;
bh=wFs145DD2HnkIhIX/58JrRe6Ya0AAqgb+NfxUFgWO4k=;
b=mGnkxUlml0O43OhUqqo1vsZRX+AYjmYKqhZ3sXwxzlZnCLipb7mQLO6GZhWbEiwreg
7mp94JhEo2mp+P6IorJWbCTZnX+pF+5tbvFkcYBu5axGoearvQsG4gMVmz++nxEzhSGN
WMteLSWmwWjbr60TJPpjagUeOqzUkdQSnn+T//AVeXheEL4YquODQ2e1KHXML9nsRDTj
al5kj+YmU+jEv79p7BI5GOoBrdPODbkiZNguQ7MTca/e2wVIvE77T5qgmyWpxfdy7B6Q
9eA9pVeFcVvvkuJykIw4I+GgBjt+VSNBCGEJUdhIvua2GanT1iy1sa3D0u7YLPoT3N1H
mqLw==
X-Gm-Message-State: ABy/qLb2nFcCVeoBgTQyMNTyqfLCQwNb/YlFmR6h7Ovuu/TChKwAOlTX
AW/DeWu6w3opFpVLx92NLNRfSzB8T10=
X-Google-Smtp-Source: APBJJlFeNMcFCpstZS9s84GQCL11imaygV8V8mHf92DNHg2ZgkC2/YbdKyju7qEo3WmfMg5GKR9EnA==
X-Received: by 2002:a19:2d14:0:b0:4fb:fdf1:8b15 with SMTP id
k20-20020a192d14000000b004fbfdf18b15mr374249lfj.59.1690338139500;
Tue, 25 Jul 2023 19:22:19 -0700 (PDT)
X-Mailer: Apple Mail (2.3654.120.0.1.15)
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <2D6F0153-5FAE-49A1-A5B0-23CF9AAED4E7@gmail.com>
 by: Dom Grigonis - Wed, 26 Jul 2023 02:22 UTC

To illustrate what I was trying to achieve:

class A:
def __init__(self, arr):
self.arr = arr

def __getattr__(self, name):
arr_method = getattr(self.arr, name)
def wrapper(*args, **kwargs):
new_arr = arr_method(*args, **kwargs)
return type(self)(new_arr)
return wrapper

a = A(np.ones((1, 1)))
print(a.sum().arr) # 1
print(a + 1) # TypeError: unsupported operand type(s) for +: 'A' and 'int'

Is there a way to achieve it without actually implementing operators?
I have looked at Proxy objects, but they do not seem suited to achieve this. (e.g. wrapt)

If there is no way to do this, wouldn’t it be sensible to have a new method, say ‘__getattrspecial__’? Either with ability to customise for which operators it is being called or not.

—Nothing ever dies, just enters the state of deferred evaluation—
Dg

Re: Fallback for operator and other dunder methods

<operators-20230727102657@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23833&group=comp.lang.python#23833

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Fallback for operator and other dunder methods
Date: 27 Jul 2023 09:31:36 GMT
Organization: Stefan Ram
Lines: 36
Expires: 1 Jul 2024 11:59:58 GMT
Message-ID: <operators-20230727102657@ram.dialup.fu-berlin.de>
References: <mailman.194.1690338142.23016.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de QqSR2+uPCnNc9L2gZgSWpAG6L5iDi42VYgOheAErWoWtRg
Cancel-Lock: sha1:/LDOEhTdLwU9T/SJv5wxnjggzGg= sha256:qRA8EBicro2rVuP1Fj5f+ikCQskHfnfMuZF1tIeB5ww=
X-Copyright: (C) Copyright 2023 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE-1901, en-US, it, fr-FR
 by: Stefan Ram - Thu, 27 Jul 2023 09:31 UTC

Dom Grigonis <dom.grigonis@gmail.com> writes:
>Is there a way to achieve it without actually implementing operators?

main.py

import pydoc_data.topics
import re

numeric_types = pydoc_data.topics.topics[ 'numeric-types' ]
dic = {}
for numeric_operation in re.finditer\
( '^object.(__.*?__)', numeric_types, re.MULTILINE ):
name = numeric_operation.group( 1 )
exec( rf'''def %s( self, /, *args, **kwargs ):
return \
self.implementation( %r, args, kwargs )''' %( name, name ), dic )
base_class = type( name, (object,), dic )

class example_class( base_class ):
def implementation( self, name, *args, **kwargs ):
print( f'implementation called. {name = }' )
return 2

example_object = example_class()

print( example_object + 1 )
print( 1 + example_object )

output

implementation called. name = '__add__'
2 implementation called. name = '__radd__'
2

Re: Fallback for operator and other dunder methods

<better-20230727104420@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23834&group=comp.lang.python#23834

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Fallback for operator and other dunder methods
Date: 27 Jul 2023 09:44:52 GMT
Organization: Stefan Ram
Lines: 10
Expires: 1 Jul 2024 11:59:58 GMT
Message-ID: <better-20230727104420@ram.dialup.fu-berlin.de>
References: <mailman.194.1690338142.23016.python-list@python.org> <operators-20230727102657@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de M9bEbJc5Us+TTrlO5GwJ6wkGUCaCMCWnldfZoiHPG0iqdL
Cancel-Lock: sha1:nvVaxAJy+yypnHGRAiZJ2xheUug= sha256:vvlNWpIuq/+AFntykBciRo2h1UqQ6UfgWoo3DLpQ76g=
X-Copyright: (C) Copyright 2023 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE-1901, en-US, it, fr-FR
 by: Stefan Ram - Thu, 27 Jul 2023 09:44 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>base_class = type( name, (object,), dic )

The above will work, but

base_class = type( 'base_class', (), dic )

might be better.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor