cx_Oracle, callfunc and varray

2015-01-09 Thread Dom
Hi

I'm trying to return a simple array of numbers from a package using cx_oracle 
(5.1.2). I believe this is possible. I've not been able to find anything that 
suggest it isn't

create or replace TYPE NUMARRAY
-- Simple VArray of numbers
is VARRAY(3) OF NUMBER;
/

create or replace PACKAGE SIMPLEPACKAGE
AS
  FUNCTION DoSomethingSimple(
  cust_id INTEGER)
RETURN numarray;
  FUNCTION DoSomethingSimpler(
  cust_id INTEGER)
RETURN INTEGER;
END SIMPLEPACKAGE;
/

create or replace PACKAGE BODY SIMPLEPACKAGE
AS
FUNCTION DOSOMETHINGSIMPLE(
cust_id INTEGER)
  RETURN numarray
AS
  simple_array numarray := numarray();
BEGIN
  simple_array.extend;
  simple_array(1) := cust_id;
  simple_array.extend;
  simple_array(2) := cust_id;
  simple_array.extend;
  simple_array(3) := cust_id;
  RETURN SIMPLE_ARRAY;
END DOSOMETHINGSIMPLE;
FUNCTION DOSOMETHINGSIMPLER(
cust_id INTEGER)
  RETURN INTEGER
AS
BEGIN
  RETURN cust_id;
END DOSOMETHINGSIMPLER;
END SIMPLEPACKAGE;
/

The python (2.7) is very simple

import cx_Oracle

if __name__ == '__main__':
 with cx_Oracle.connect('soe', 'soe', 'oracle12c2/soe') as connection:
try:
cursor = connection.cursor();
ArrayType = cursor.arrayvar(cx_Oracle.NUMBER,3)
NumberType = cursor.var(cx_Oracle.NUMBER)
cursor.callfunc("SIMPLEPACKAGE.DOSOMETHINGSIMPLER", NumberType, 
[99])
cursor.callfunc("SIMPLEPACKAGE.DOSOMETHINGSIMPLE", ArrayType, [99])
except cx_Oracle.DatabaseError as dberror:
print dberror
finally:
cursor.close()

The call to return works just fine. The call to return the function gives the 
error

ORA-06550: line 1, column 13:
PLS-00382: expression is of wrong type
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

Any ideas what I'm doing wrong?

Dom



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: SEO - Search Engine Optimization - Seo Consulting

2007-04-29 Thread Dom Robinson
In article <[EMAIL PROTECTED]>, "Alvin Bruney [MVP]"  says...
> Please don't spam this group
> 
A top-poster replies to a spammer. Now that world has truly gone mad(!)
-- 

Dom Robinson  Gamertag: DVDfever  email: dom at dvdfever dot co dot uk
/* http://DVDfever.co.uk (editor)
/* 1132 DVDs, 347 games, 314 CDs, 110 cinema films, 42 concerts, videos & news
/* antibodies, steve hillage, burning crusade, sega psp, norah jones, kylie
 New music charts - http://dvdfever.co.uk/music.shtml
   Youtube - http://www.youtube.com/profile?user=DVDfeverDom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SEO - Search Engine Optimization - Seo Consulting

2007-05-01 Thread Dom Robinson
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] 
says...
> You bottom posters really are a bunch of supercilious, self-righteous 
> bigots.
> 
> Personally, I find bottom-posting like reading a book backwards ... it 
> doesn't work for me.
> 
> And regardless of his response, Mr Bruney IS an MVP, he is clearly 
> knowledgeable in his subject, and his book is well enough thought of to make 
> me consider buying it.
> 
> 
> "Sherm Pendley" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
> > "Juan T. Llibre" <[EMAIL PROTECTED]> writes:
> >
> >> Top or bottom posting is a user choice.
> >
> > Yes, one can choose to be polite and follow established usenet
> > conventions, or one can choose to be rude and self-centered.
> >
> > Those who choose rudeness over courtesy can expect to be called down
> > for it - which is arguably rude in itself, but on usenet one reaps what
> > one sows. Someone who can't deal with that, shouldn't be on usenet.
> >
> > sherm--
> >
> > -- 
> > Web Hosting by West Virginians, for West Virginians: http://wv-www.net
> > Cocoa programming in Perl: http://camelbones.sourceforge.net 


Must be because you ARE backwards.
-- 

Dom Robinson  Gamertag: DVDfever  email: dom at dvdfever dot co dot uk
/* http://DVDfever.co.uk (editor)
/* 1132 DVDs, 347 games, 314 CDs, 110 cinema films, 42 concerts, videos & news
/* antibodies, steve hillage, burning crusade, sega psp, norah jones, kylie
 New music charts - http://dvdfever.co.uk/music.shtml
   Youtube - http://www.youtube.com/profile?user=DVDfeverDom
-- 
http://mail.python.org/mailman/listinfo/python-list


Peer To Peer File Sharing...

2008-01-08 Thread Dom Rout
Hello.
Well, this is my first post on any USENET group anywhere, so I hope I
get it right. Basically, I just want to get some opinions on a plan of
mine for a new project.

I want to produce a small, peer to peer, file sharing network for the
use of myself and some of my friends. The purpose of this is basically
to allow us to share files conveniently without relying on technology
such as Windows Live Messenger (Yuck).

I have a VPS which I would like to dedicate to the task of acting as a
tracker, so I can run a server application written in python on it. I
will also write the first client in python, although I may go for a
different language for the client in the final version, for
performance. For now, Python is perfect because of the ease of use
that it offers and the fact that I already know a bit about socket
programming using it.

Also concerning architecture, I will also have a number of peers that
connect to the tracker and also to other peers, via an IP address
provided by the server, as necessary to download the files.

The files themselves should be split up into "Chunks" of fixed length,
which will be given an index and tracked by the server. The server
should know which clients have which chunks of a file, and when a
client needs to download a file the server should look for other
clients that have chunks from that file and give the IP address to the
client, which should then for a connection to this peer and download
the parts of the file that are available.

When managing the chunks of a file, I will need to use a mutex to
allow reading and writing of them. I should provide a getter and
setter method in each file to allow chunks to be placed into it more
conveniently. The getter and setter should both use mutex's to allow
multiple threads of uploads and downloads at the same time.

I will need to implement a username and password system, to restrict
the users of the system to people that I trust.

To uniquely identify a file, I would like to use a file path. There
should be a theoretical directory that contains all shared files,
although the client should be given the option of which files from the
directory to download.

This directory should look like:
"Global/"
"Global/Images/"
"Global/Music/"
"Users//"

It would be nice if it was possible to subscribe to certain
directories, and download new files from them as need be.

Well, these are my ideas so far. Is anything drastically obviously
wrong, and can anyone suggest to me any best practices when
implementing this sort of design?

Thanks, Dominic Rout.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Meta Class Maybe?

2023-07-24 Thread Dom Grigonis via Python-list


> On 23 Jul 2023, at 02:12, Chris Nyland via Python-list 
>  wrote:
> 
> So I am stuck on a problem. I have a class which I want to use to create
> another class without having to go through the boiler plate of subclassing.
> Specifically because the subclass needs to have certain class attributes
> and I would like to control how those are passed to provide defaults and
> such. What I have working right now is

> class Foo():
>@classmethod
>def method_a(cls): print(cls.name)
> 
> Bar = type('Bar', (Foo,), {'name': 'test1’})


Could you write a more expressive example of what you are trying to do and what 
the problem actually is?

The way you wrote the above doesn’t contain any clues why usual subclassing is 
not appropriate.

By attributes you mean value attributes or methods? Why going the usual route 
of subclassing & controlling their behaviour via constructor arguments isn’t 
working?

> This is sort of fine but the user needs to know how to call type and
> include all the base classes etc.


I don’t get what you mean by this either. Very hard to follow everything that 
went after, given I was lost at this point.

Maybe it’s only me... But I if you clarified these, I might be able to help a 
bit.

DG
-- 
https://mail.python.org/mailman/listinfo/python-list


Fallback for operator and other dunder methods

2023-07-25 Thread Dom Grigonis via Python-list
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

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fallback for operator and other dunder methods

2023-07-25 Thread Dom Grigonis via Python-list
Could you give an example? Something isn’t working for me.

> On 26 Jul 2023, at 09:40, Chris Angelico via Python-list 
>  wrote:
> 
> On Wed, 26 Jul 2023 at 12:23, Dom Grigonis via Python-list
>  wrote:
>> 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)
> 
> These kinds of special methods are not looked up on the object, but on
> the type. It's more like type(a).__add__(a, 1). So you would need a
> metaclass for this.
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fallback for operator and other dunder methods

2023-07-26 Thread Dom Grigonis via Python-list
Tried exactly that and didn’t work. Neither __getattr__, nor __getattribute__ 
of meta is being invoked.

> On 26 Jul 2023, at 10:01, Chris Angelico via Python-list 
>  wrote:
> 
> On Wed, 26 Jul 2023 at 16:52, Dom Grigonis  wrote:
>> 
>> Could you give an example? Something isn’t working for me.
>> 
> 
> This is a metaclass:
> 
> class Meta(type):
>...
> class Demo(metaclass=Meta):
>...
> 
> In order to catch those kinds of attribute lookups, you'll need the
> metaclass to hook them. And you might need to use __getattribute__
> rather than __getattr__. However, there may also be some checks that
> simply look for the presence of the attribute (see: slots), so you may
> find that it's even more complicated. It's usually easiest to just
> create the slots you want.
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fallback for operator and other dunder methods

2023-08-04 Thread Dom Grigonis via Python-list
The issue was more of a wrapping around numpy array. Found the solution 
already. Unfortunately, there is no equivalent to __getattr__, the only way is 
to dynamically define them from meta. It seems it’s pretty standard to just 
have a collection of special method names and using them for similar cases. 
Well, at least it’s what I got to. __getattr__ feels very hacky for such case, 
so maybe it’s for the best.

> On 2 Aug 2023, at 19:54, Edmondo Giovannozzi via Python-list 
>  wrote:
> 
> Il giorno mercoledì 26 luglio 2023 alle 20:35:53 UTC+2 Dom Grigonis ha 
> scritto:
>> Tried exactly that and didn’t work. Neither __getattr__, nor 
>> __getattribute__ of meta is being invoked.
>>> On 26 Jul 2023, at 10:01, Chris Angelico via Python-list 
>>> http://python.org/>> wrote: 
>>> 
>>> On Wed, 26 Jul 2023 at 16:52, Dom Grigonis >> <http://dom.gr/>...@gmail.com <http://gmail.com/>> wrote: 
>>>> 
>>>> Could you give an example? Something isn’t working for me. 
>>>> 
>>> 
>>> This is a metaclass: 
>>> 
>>> class Meta(type): 
>>> ... 
>>> class Demo(metaclass=Meta): 
>>> ... 
>>> 
>>> In order to catch those kinds of attribute lookups, you'll need the 
>>> metaclass to hook them. And you might need to use __getattribute__ 
>>> rather than __getattr__. However, there may also be some checks that 
>>> simply look for the presence of the attribute (see: slots), so you may 
>>> find that it's even more complicated. It's usually easiest to just 
>>> create the slots you want. 
>>> 
>>> ChrisA
>>> -- 
>>> https://mail.python.org/mailman/listinfo/python-list
> 
> 
> For numpy arrays you can find some suggestion at: 
> https://numpy.org/doc/stable/user/basics.dispatch.html 
> <https://numpy.org/doc/stable/user/basics.dispatch.html>
> -- 
> https://mail.python.org/mailman/listinfo/python-list 
> <https://mail.python.org/mailman/listinfo/python-list>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: iterations destroy reversed() results

2023-09-03 Thread Dom Grigonis via Python-list
It is by design. `sorted` returns a list, while `reversed` returns an iterator. 
Iterators are exhaust-able, and not reusable. So be mindful of this and if you 
are going to "re-use” the sequence returned by iterator, convert it to list 
first.

Have a look at `itertools` library, which contains a lot of such functions and 
many good recipes on achieving various things elegantly using iterators.

> On 1 Sep 2023, at 19:15, Pierre Fortin via Python-list 
>  wrote:
> 
> Hi,
> 
> reversed() results are fine until iterated over, after which the
> results are no longer available. This was discovered after using
> something like this:
> 
> rev = reversed( sorted( list ) ) 
> sr = sum( 1 for _ in rev )
> # rev is now destroyed
> 
> So reversed() results can only be iterated once unlike sorted(), etc...
> 
> Script to illustrate the issue:
> /tmp/rev:
> orig = [ 'x', 'a', 'y', 'b', 'z', 'c' ]
> co = sum( 1 for _ in orig )
> print( 'orig', orig, co )
> # reversing
> rev = reversed(orig)
> print( 'before iteration:', [ x for x in rev ] )
> # list comprehension was an iteration over 'rev'
> print( 'after iteration:', [ x for x in rev ] )
> # how this was discovered...
> orig = [ 'x', 'a', 'y', 'b', 'z', 'c' ]
> rev = reversed(orig)
> cr = sum( 1 for _ in rev )
> print( 'after sum():', [ x for x in rev ] )
> 
> which produces:
> 
> $ python /tmp/rev
> orig ['x', 'a', 'y', 'b', 'z', 'c'] 6
> before iteration: ['c', 'z', 'b', 'y', 'a', 'x']
> after iteration: []
> after sum(): []
> 
> Regards,
> Pierre
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using generator expressions

2023-09-25 Thread Dom Grigonis via Python-list
y = test1(*[a for a in st])
y = test1(*st)
Maybe any of these would be ok for you?

Regards,
DG


> On 25 Sep 2023, at 17:15, Jonathan Gossage via Python-list 
>  wrote:
> 
> I am having a problem using generator expressions to supply the arguments
> for a class instance initialization. The following example shows the
> problem:
> 
> class test1(object):
>def __init__(self, a, b):
> 
>>self.name = a
> 
>self.value = b
> st = 'Programming Renaissance, Any'.split(', ')
> y = test1(a for a in st)
> print(f'Object values are: {y._a}, {y._b}')
> 
> I would expect to get the values from the list generated by splitting the
> string passed in as arguments to the new instance of test1, but instead
> I get the generator expression by itself as a generator object. The
> generator
> expression is treated like a passive object instead of being run. If I had
> wanted to pass the generator expression itself, I would have expected to
> have
> to use parentheses around the generator expression. Any suggestions on how
> to
> get the generator expression to run?
> If I change the definition of the input arguments to *args I can capture the
> arguments within __init__ but it is verbose and ugly. Also, I could accept
> the
> arguments from a Sequence and extract the Sequence members into the class
> values. I would prefer my solution if I could get it to work.
> Note that I tried generator expressions both inside parentheses and not,
> without success.
> 
> -- 
> Jonathan Gossage
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question(s)

2023-10-24 Thread Dom Grigonis via Python-list
I don’t think there i a simple answer to this, although if you find something 
interesting, please share.

From my experience, industry is applying variety of testing methods. Starting 
from lowest level components and implementing unit tests, finishing with 
end-to-end testing platforms.

https://www.atlassian.com/continuous-delivery/software-testing/types-of-software-testing
 


Modular programming paradigm, IMO, is one of the solutions to this problem. 
Then, each component is a flexible program in itself that can be combined with 
others. This way, code is re-used by many people and code is well tested and 
issues are quick to surface.

As far as I know, unix/linux has a big emphasis on modularity in contrast with 
monolithic approach of windows, which could be one of the big reasons why (at 
least from my perspective) working in unix environment is so much more pleasant.

https://en.wikipedia.org/wiki/Unix_philosophy 


Regards,
DG

> On 24 Oct 2023, at 15:22, o1bigtenor via Python-list  
> wrote:
> 
> Greetings
> 
> (Sorry for a nebulous subject but dunno how to have a short title for
> a complex question.)
> 
> I have been using computers for a long time but am only beginning my
> foray into the
> galaxy of programming. Have done little to this point besides
> collection of information
> on sensors and working on the logic of what I wish to accomplish. Have
> been reading code that accompanies other's projects in the process of
> self development.
> 
> Is there a way to verify that a program is going to do what it is
> supposed to do even
> before all the hardware has been assembled and installed and tested?
> 
> (Many years ago I remember an article (if not an issue) in Byte magazine about
> mathematically proven constructs a.k.a. programs - - - this idea is
> what I'm pursuing.
> The concept is that in non-trivial programs there are plenty of places where a
> poorly placed symbol or lack of a character will result in at best an 
> inaccurate
> result and at worst - - - no result. This is the kind of thing
> (correct code) that I'm
> hoping to accomplish - - - to rephrase the question - - - how do I
> test for that?)
> 
> TIA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


xor operator

2023-11-13 Thread Dom Grigonis via Python-list
Hi All,

I think it could be useful to have `xor` builtin, which has API similar to the 
one of `any` and `all`.

* Also, it could have optional second argument `n=1`, which indicates how many 
positives indicates `True` return.
* For complete flexibility 3rd argument could indicate if `the number` is 
equal, greater, less, ... than `n`

I find I sometimes need it when dealing with pub-sub filters and multiple 
predicates in general.

What is the current situation that led me to writing this e-mail?
Dealing with circular import in low level component space: library builtins 
depend on validation utilities, while validation utilities want to use xor from 
library builtins.

Not a big issue, but seen there are fairly large threads in stack everyone 
inventing their own `xor`. And surprisingly there aren’t many good solutions. 
Almost none of them deal with short-circuiting well. And those that do contain 
loops that result in very poor performance.


Regards,
DG
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-13 Thread Dom Grigonis via Python-list
Well yes, I don’t think naming is very accurate. If iterable has 2 elements, 
then it is `xor`, otherwise it is something else - it checks the number of 
truth values in iterable.

Short circuiting happens, when:
xor([True, True, False, False], n=1)
At index 1 it is clear that the answer is false.

Regards,
DG

> On 13 Nov 2023, at 19:42, Barry  wrote:
> 
> 
> 
>> On 13 Nov 2023, at 15:16, Dom Grigonis via Python-list 
>>  wrote:
>> 
>> I think it could be useful to have `xor` builtin, which has API similar to 
>> the one of `any` and `all`.
> 
> I do not understand how xor(iterator) works.
> I thought xor takes exactly 2 args.
> 
> I also do not understand how xor can be short circuited.
> For AND or OR only looking at the first arg works.
> But that does not work for xor right?
> 
> Barry
> 
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-13 Thread Dom Grigonis via Python-list
xor([True, False, False, False], n=1)
xor([False, False, False, True], n=1)
Both of the above would evaluate to true.

Well, it depends how you interpret it.
In binary case it reads: “exclusively one positive bit or the other, but not 
both”
In this case one could read: “exclusively one positive set of certain length, 
but not more than one such set at the same time" 

But I completely see how one could argue, that “exclusive” refers to slightly 
different thing. And multivariate `xor` is a function which defines fixed 
subsets out of which only one is true, but not the others, but this is 
theoretical, as in practice each set would need to always have all values 
switched on or off, which boils down to my proposed `xor` with `n=1`.

But the point is if there is a need for such function or I am the only one who 
happens to use it.

DG

> On 13 Nov 2023, at 23:03, Barry  wrote:
> 
> 
> 
>> On 13 Nov 2023, at 17:48, Dom Grigonis  wrote:
>> 
>> Short circuiting happens, when:
>> xor([True, True, False, False], n=1)
>> At index 1 it is clear that the answer is false.
> 
> Can you share an example with 4 values that is true?
> And explain why it is xor.
> 
> Barry
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-13 Thread Dom Grigonis via Python-list
Benchmarks:
test1 = [False] * 100 + [True] * 2
test2 = [True] * 100 + [False] * 2

TIMER.repeat([
lambda: xor(test1), # 0.0168
lambda: xor(test2), # 0.0172
lambda: xor_ss(test1),  # 0.1392
lambda: xor_ss(test2),  # 0.0084
lambda: xor_new(test1), # 0.0116
lambda: xor_new(test2), # 0.0074
lambda: all(test1), # 0.0016
lambda: all(test2)  # 0.0046
])
Your first function is fairly slow.
Second one deals with short-circuiting, but is super slow on full search.

`xor_new` is the best what I could achieve using python builtins.

But builtin `all` has the best performance.

DG

> On 13 Nov 2023, at 23:20, Michael Speer  wrote:
> 
> I don't think an exclusive-or/truthy-entries-count-checker needs to be a 
> builtin by any stretch.
> 
> >>> def xor( iterable, n = 1 ):
> ... return sum( map( bool, iterable ) ) == n
> 
> Or if you insist on short circuiting:
> 
> >>> def xor_ss( iterable, n = 1 ):
> ...   for intermediate in itertools.accumulate( iterable, (lambda x, y: x + 
> bool(y)), initial = 0 ):
> ... if intermediate > n:
> ...   return False
> ...   return intermediate == n
> 
> 
> 
> On Mon, Nov 13, 2023 at 4:05 PM Barry via Python-list  <mailto:python-list@python.org>> wrote:
> 
> 
> > On 13 Nov 2023, at 17:48, Dom Grigonis  > <mailto:dom.grigo...@gmail.com>> wrote:
> > 
> > Short circuiting happens, when:
> > xor([True, True, False, False], n=1)
> > At index 1 it is clear that the answer is false.
> 
> Can you share an example with 4 values that is true?
> And explain why it is xor.
> 
> Barry
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list 
> <https://mail.python.org/mailman/listinfo/python-list>

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-13 Thread Dom Grigonis via Python-list
I am not asking. Just inquiring if the function that I described could be 
useful for more people.

Which is: a function with API that of `all` and `any` and returns `True` if 
specified number of elements is True.

It is not a generalised `xor` in strict programatic space. I.e. NOT bitwise xor 
applied to many bits.
This is more in line with cases that `any` and `all` builtins are used.

> On 14 Nov 2023, at 00:51, Grant Edwards via Python-list 
>  wrote:
> 
> On 2023-11-13, Dom Grigonis via Python-list  wrote:
>> Hi All,
>> 
>> I think it could be useful to have `xor` builtin, which has API similar to 
>> the one of `any` and `all`.
>> 
>> * Also, it could have optional second argument `n=1`, which
>> * indicates how many positives indicates `True` return.  For
>> * complete flexibility 3rd argument could indicate if `the number`
>> * is equal, greater, less, ... than `n`
> 
> I would expect "xor" to return true if there are an odd number of
> trues, and false if there are an even number of trues.  It's not clear
> to me what you're asking for.
> 
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-13 Thread Dom Grigonis via Python-list
I am not arguing that it is a generalised xor.

I don’t want anything, I am just gauging if it is specialised or if there is a 
need for it. So just thought could suggest it as I have encountered such need 
several times already.

It is fairly clear by now that it is not a common one given it took some time 
to even convey what I mean. Bad naming didn’t help ofc, but if it was something 
that is needed I think it would have clicked much faster.

Thanks,
DG

> On 14 Nov 2023, at 01:12, Chris Angelico via Python-list 
>  wrote:
> 
> On Tue, 14 Nov 2023 at 10:00, Dom Grigonis via Python-list
>  wrote:
>> 
>> I am not asking. Just inquiring if the function that I described could be 
>> useful for more people.
>> 
>> Which is: a function with API that of `all` and `any` and returns `True` if 
>> specified number of elements is True.
>> 
>> It is not a generalised `xor` in strict programatic space. I.e. NOT bitwise 
>> xor applied to many bits.
>> This is more in line with cases that `any` and `all` builtins are used.
>> 
> 
> A generalization of XOR is exactly what Grant and I said, though: a
> parity check. See for example:
> 
> https://en.wikipedia.org/wiki/Exclusive_or
> https://reference.wolfram.com/language/ref/Xor.html
> 
> It tells you whether you have an odd or even number of true values.
> 
> Now, if you want something that short-circuits a counting function,
> that's definitely doable, but it's a sum-and-compare, not xor. Also,
> it's quite specialized so it's unlikely to end up in the stdlib.
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-13 Thread Dom Grigonis via Python-list


> Except the 'any' and 'all' builtins are _exactly_ the same as bitwise
> or and and applided to many bits. To do something "in line" with that
> using the 'xor' operator would return True for an odd number of True
> values and False for an even Number of True values.

Fair point.

Have you ever encountered the need for xor for many bits (the one that I am NOT 
referring to)? Would be interested in what sort of case it could be useful.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-13 Thread Dom Grigonis via Python-list
I agree, from perspective of standard `all` and `any` use cases this does not 
seem very useful.

However, in my experience it has its usages. E.g.:
* If sum(map(bool, iterable) [> | <] n can be useful. Counting dead processes 
and similar, optimisation problems where need to re-initialise if less than 
certain number of nodes reached certain threshold.
* If sum(map(bool, iterable) [== | !=] n is an edge case. Can’t even think of 
an example
* Finally, it would be a convenient shorthand for `bool(a) ^ bool(b)`

I sometimes think that there is also a case when certain function is not in 
one’s mind he doesn’t see the usefulness, but sometimes the sheer knowledge of 
its existence can twist certain situations in a positive manner.

What about an alternative, which does seem more useful:

def count_compare(iterable, n):
return np.sign(sum(map(bool, iterable)) - n)

print(count_compare([False, False, False], 1))
As I am here, I will dare to ask if there is no way that `sign` function is 
going to be added to `math` or `builtins`. `np.sign` does the trick, but it is 
very slow to be used on a single number. And yes, I know that `copysign` 
exists, it just doesn’t do the same thing.

DG

> On 14 Nov 2023, at 02:33, Mats Wichmann via Python-list 
>  wrote:
> 
> On 11/13/23 16:24, Dom Grigonis via Python-list wrote:
>> I am not arguing that it is a generalised xor.
>> I don’t want anything, I am just gauging if it is specialised or if there is 
>> a need for it. So just thought could suggest it as I have encountered such 
>> need several times already.
>> It is fairly clear by now that it is not a common one given it took some 
>> time to even convey what I mean. Bad naming didn’t help ofc, but if it was 
>> something that is needed I think it would have clicked much faster.
> 
> There are things that If You Need Them You Know, and If You Do Not You Do Not 
> Understand - and you seem to have found one.  The problem is that forums like 
> this are not a statistically great sampling mechanism - a few dozen people, 
> perhaps, chime in on many topics; there are millions of people using Python. 
> Still, the folks here like to think they're at least somewhat representative 
> :)
> 
> Hardware and software people may have somewhat different views of xor, so 
> *maybe* the topic title added a bit to the muddle.  To me (one of those 
> millions), any/all falsy, any/all truthy have some interest, and Python does 
> provide those. Once you get into How Many True question - whether that's the 
> odd-is-true, even-is-false model, or the bail-after-X-truthy-values model, 
> it's not terribly interesting to me: once it gets more complex than an 
> all/any decision, I need to check for particular combinations specifically. 
> Two-of-six means nothing to me until I know which combination of two it is.
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator (DEPRECATED)

2023-11-13 Thread Dom Grigonis via Python-list
Fair point. However, I gave it a shot for the following reason:

I couldn’t find a way to make such performant function. Using python builtin 
components still ends up several times slower than builtin `all`. Cython or 
numba or similar is not an option as they do not support `truth` values. Or if 
they do, it ends up slower than pure python variant.

So all what is left is writing a proper extension. Which I would prefer not to 
do for 1 function. I thought maybe `xor`, as in logical XOR functionality in 
its vanilla case could be compelling. And after doing a bit of search I see 
that very very few languages have that and it seems for a good reason.

Some that do: R, thats all I could find. Although some (if not many) went 
through the proposal phase. And yes, none of them have a function that I am 
proposing.

So yes, you are right, not a good proposal.

But there still seems to be the need for short-circuiting performant 
implementations in python space. The issue is that there are many variants of 
what might be needed while there is no efficient solution to sourcing 
predicates from python to lower level implementations. Someone mentioned that 
numpy experimented with such implementations in C, but they did not get 
anywhere with it.

The best I could come up with is cached numba for numpy problems, which does 
perform very well and more than worth it if function is re-used. It even ends 
up faster than cython or cffi extensions, however can’t have many of those due 
to JIT and AOT is currently being deprecated (which wouldn’t solve anything 
anyway). However, as I mentioned earlier it does not apply to this case.

So it’s either:
a) Something very clever and flexible implemented that covers most of such 
needs and doesn’t require predicates.
b) I welcome any thoughts on this.

DG

> On 14 Nov 2023, at 04:27, AVI GROSS via Python-list  
> wrote:
> 
> I was going to ask a dumb question. Has any other language you know of made
> something available that does what is being asked for and included it in the
> main program environment rather than an add-on?
> 
> A secondary mention here has been whether short-circuiting functions like
> "any" and "all" have been augmented with something like "has_n" that
> evaluates arguments till it has found n or perhaps n+1 of what it wants then
> skips the rest. Does any language supply something like that? What would
> such a function return and does it have an "any" or an "all" side?
> 
> It sounds like if I asked if a list of integers has at least n prime numbers
> in "any" mode, it should ignore any that are not primes till it finds n
> primes or fails and returns true or false. If in "all" mode, I assume it
> would have to be the first n items without a failure.
> 
> Fine, but then someone may want to know WHERE you stopped or for you to
> return the sublist of the ones that made the match, or even return
> everything that was skipped so you can later process that. Consider a long
> list of jurors you process to place a dozen that qualify on a jury and then
> later you want to choose from among the rest for another jury.
> 
> Human minds can come up with an amazing number of ideas including for
> "useful" functions or features but I find the vast majority would rarely be
> used as nobody remembers it is available and some fairly simple method using
> other functions can easily be cobbled together.
> 
> -Original Message-
> From: Python-list  On
> Behalf Of Grant Edwards via Python-list
> Sent: Monday, November 13, 2023 8:19 PM
> To: python-list@python.org
> Subject: Re: xor operator
> 
> On 2023-11-14, Dom Grigonis via Python-list  wrote:
>> 
>>> Except the 'any' and 'all' builtins are _exactly_ the same as bitwise
>>> or and and applided to many bits. To do something "in line" with that
>>> using the 'xor' operator would return True for an odd number of True
>>> values and False for an even Number of True values.
>> 
>> Fair point.
>> 
>> Have you ever encountered the need for xor for many bits (the one
>> that I am NOT referring to)? Would be interested in what sort of
>> case it could be useful.
> 
> Yes, it's used all the time in low-level communications protocols,
> where it's often implemented in hardware. But, it is also not at all
> unusual to implement it in software.
> 
> It's also not that unusual for the "count-ones" part of the function
> you're asking for to be implemented in hardware by a CPU having an
> instruction that counts the number of 1 bits in a register.
> 
> GCC has a low-level builtins called __builtin_popcount() and
> __builtin-popcountl() that counts the number of 1's in an unsigned
> (long) int.
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator (DEPRECATED)

2023-11-13 Thread Dom Grigonis via Python-list
Thank you, I have spent a fair bit of time on these and found optimal solutions 
(at least I think so), but they are still multiple times slower than python 
builtin function.

I am currently out of ideas, maybe will dig something out in time.

> On 14 Nov 2023, at 07:23, Thomas Passin via Python-list 
>  wrote:
> 
> On 11/13/2023 11:44 PM, AVI GROSS via Python-list wrote:
>> Dom,
>> I hear you.
>> As you say, writing your own extension in something like C++ may not appeal 
>> to you even if it is faster.
>> I was wondering if using a generator or something similar in R might make 
>> sense.
>> I mean what happens if you write a function that includes a "yield" or two 
>> and does a part of what you want. It maintains some internal state between 
>> invocations. So you can call it once to setup things then call it repeatedly 
>> to keep processing the next item. You stop calling it when you get a result 
>> you want, such as that it has seen what you want N times.
>> Since the code stays in memory, it may effectively run faster than some 
>> other kinds of functions calls. It can keep things in internal storage such 
>> as not just how many N you want but how many it has seen.
> 
> I'm inclined to just turn the iterable into a set to get the values, then 
> iterate through those values calling count() on a listified version of the 
> iterable. If the count >= target, return.
> 
> It may not be the fastest one could do but it's simple and probably pretty 
> fast for many uses.  I suppose that for some iterables it would be better not 
> to turn them into lists, but one could figure out about that after working 
> out more carefully what cases need to be covered.
> 
>> Your outer function can maintain a list of the items you want to XOR or 
>> generate a new one dynamically as needed. It can use functional programming 
>> techniques to create a new customized version of the iterator, such as with 
>> a value of N built in. You would then call the outer function and let it use 
>> the inner function till the result is available or until the data in the 
>> iterator runs out or perhaps other tweaks involving two way communication of 
>> sorts between the functions.
>> I am NOT suggesting this approach is optimal or fast but merely wondering if 
>> something along these lines is worth trying that might speed things up even 
>> if not very fast. Such approaches can be even more effective if what you are 
>> working on need not all be instantiated up front but can be dynamically 
>> calculated or incrementally read from files. With care, you can make 
>> multiple instantiations that each iterate over their own sets of data 
>> without interference.
>> Just a thought. In a sense, this can be a slightly decent substitute for the 
>> non-standard evaluation in R where you can arrange for lots of your data to 
>> not be interpreted till absolutely needed.
>> -Original Message-
>> From: Dom Grigonis 
>> Sent: Monday, November 13, 2023 10:12 PM
>> To: avi.e.gr...@gmail.com
>> Cc: Grant Edwards ; Python 
>> 
>> Subject: Re: xor operator (DEPRECATED)
>> Fair point. However, I gave it a shot for the following reason:
>> I couldn’t find a way to make such performant function. Using python builtin 
>> components still ends up several times slower than builtin `all`. Cython or 
>> numba or similar is not an option as they do not support `truth` values. Or 
>> if they do, it ends up slower than pure python variant.
>> So all what is left is writing a proper extension. Which I would prefer not 
>> to do for 1 function. I thought maybe `xor`, as in logical XOR functionality 
>> in its vanilla case could be compelling. And after doing a bit of search I 
>> see that very very few languages have that and it seems for a good reason.
>> Some that do: R, thats all I could find. Although some (if not many) went 
>> through the proposal phase. And yes, none of them have a function that I am 
>> proposing.
>> So yes, you are right, not a good proposal.
>> But there still seems to be the need for short-circuiting performant 
>> implementations in python space. The issue is that there are many variants 
>> of what might be needed while there is no efficient solution to sourcing 
>> predicates from python to lower level implementations. Someone mentioned 
>> that numpy experimented with such implementations in C, but they did not get 
>> anywhere with it.
>> The best I could come up with is cached numba for numpy problems, which does 
>> perform very well and more than worth it if function is re-used. It even 
>

Re: xor operator

2023-11-15 Thread Dom Grigonis via Python-list
Thank you,

test2 = [True] * 100 + [False] * 2
test2i = list(range(100))

%timeit len(set(test2i)) == 1   # 1.6 µs ± 63.6 ns per loop (mean ± std. dev. 
of 7 runs, 1,000,000 loops each)
%timeit all(test2)  # 386 ns ± 9.58 ns per loop (mean ± std. dev. 
of 7 runs, 1,000,000 loops each)

test2s = set(test2i)
%timeit len(test2s) == 1# 46.1 ns ± 1.65 ns per loop (mean ± std. dev. 
of 7 runs, 10,000,000 loops each)
If you pre-convert to set it is obviously faster. However, set operation is 
most likely going to be part of the procedure. In which case it ends up to be 
significantly slower.

Regards,
DG


> On 15 Nov 2023, at 02:34, Peter J. Holzer via Python-list 
>  wrote:
> 
> On 2023-11-14 00:11:30 +0200, Dom Grigonis via Python-list wrote:
>> Benchmarks:
>> test1 = [False] * 100 + [True] * 2
>> test2 = [True] * 100 + [False] * 2
>> 
>> TIMER.repeat([
>>lambda: xor(test1), # 0.0168
>>lambda: xor(test2), # 0.0172
>>lambda: xor_ss(test1),  # 0.1392
>>lambda: xor_ss(test2),  # 0.0084
>>lambda: xor_new(test1), # 0.0116
>>lambda: xor_new(test2), # 0.0074
>>lambda: all(test1), # 0.0016
>>lambda: all(test2)  # 0.0046
>> ])
>> Your first function is fairly slow.
>> Second one deals with short-circuiting, but is super slow on full search.
>> 
>> `xor_new` is the best what I could achieve using python builtins.
>> 
>> But builtin `all` has the best performance.
> 
> One question worth asking is if a list of bool is the best data
> structure for the job. This is essentially a bitmap, and a bitmap is
> equivalent to a set of integers. len(s) == 1 is also a fairly quick
> operation if s is small. On my system, len(test1s) == 1 (where test1s is
> {100, 101}) is about as fast as all(test1) and len(test2s) == 1 (where
> test2s is set(range(100))) is about twice as fast as all(test2).
> 
> If you are willing to stray from the standard library, you could e.g.
> use pyroaring instead of sets: This is about as fast as all(test1)
> whether there are two bits set or a hundred.
> 
>hp
> 
> -- 
>   _  | Peter J. Holzer| Story must make more sense than reality.
> |_|_) ||
> | |   | h...@hjp.at <mailto:h...@hjp.at> |-- Charles Stross, 
> "Creative writing
> __/   | http://www.hjp.at/ <http://www.hjp.at/> |   challenge!"
> -- 
> https://mail.python.org/mailman/listinfo/python-list 
> <https://mail.python.org/mailman/listinfo/python-list>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-15 Thread Dom Grigonis via Python-list

The specific situation was related to truth values and following out of that my 
considerations regarding equivalent of all and any for counting `truths`.

So one of the specific examples:
class Publisher:
def __init__(self):
self.subscribers = dict()

def subscribe(self, sub, criteria, agg=all):
self.subscribers[sub] = (criteria, agg)

def publish(self, msg):
for sub, criteria in self.subscribers.items():
if criteria(msg):
sub.handle(msg)

p = Publisher()
p.subscribe(sub, lambda x: all(r > 3 for r in x.ratings))

# So what I needed is:
p.subscribe(sub, lambda x: set(r > 3 for r in x.ratings) > 50)
# Note, that elements might not necessarily be bool.
# E.g. at least 51 non-empty pages
p.subscribe(sub, lambda x: set(bool(p) for p in x.pages) > 50)
# So the function:
p.subscribe(sub, lambda x: xor_more_than(x.pages, 50)
# would ideally deal with truth values too.
The question is: can you construct a function? which:
a) performs: lambda x: set(bool(el) for el in iterable) > n
b) is in line with performance of python’s `all`

Then, as I said the case where one would need `set() == n` is hard to think of, 
but it seems fairly probable to need `set() > n` and `set() < n`.

Regards,
DG

> On 15 Nov 2023, at 19:16, Peter J. Holzer via Python-list 
>  wrote:
> 
> On 2023-11-15 12:26:32 +0200, Dom Grigonis wrote:
>> 
>> Thank you,
>> 
>> 
>> test2 = [True] * 100 + [False] * 2
>> test2i = list(range(100))
>> 
>> %timeit len(set(test2i)) == 1   # 1.6 µs ± 63.6 ns per loop (mean ± std. 
>> dev. of 7 runs, 1,000,000 loops each)
>> %timeit all(test2)  # 386 ns ± 9.58 ns per loop (mean ± std. 
>> dev. of 7 runs, 1,000,000 loops each)
>> 
>> test2s = set(test2i)
>> %timeit len(test2s) == 1# 46.1 ns ± 1.65 ns per loop (mean ± std. 
>> dev. of 7 runs, 10,000,000 loops each)
>> 
>> If you pre-convert to set it is obviously faster. However, set
>> operation is most likely going to be part of the procedure. In which
>> case it ends up to be significantly slower.
> 
> Obviously, if you convert a list to a set just to count the elements
> it's going to be slow. My suggestion was to use the set *instead* of the
> list. I don't know whether that's possible in your situation, because
> you haven't told us anything about it. All I'm suggesting is taking a
> step back and reconsider your choice of data structure.
> 
>hp
> -- 
>   _  | Peter J. Holzer| Story must make more sense than reality.
> |_|_) ||
> | |   | h...@hjp.at |-- Charles Stross, "Creative writing
> __/   | http://www.hjp.at/ |   challenge!"
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


__set_name__ equivalent for instance

2023-11-15 Thread Dom Grigonis via Python-list
So there is a method __set_name__ which is called on class creation.

The functionality that I am interested in is not retrieving name, but the fact 
that it also receives `owner` argument.

Thus, allowing simulation of bound class method.

I was wandering if there is an equivalent functionality of attribute to receive 
`instance` argument on instance creation.

Regards,
DG
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-15 Thread Dom Grigonis via Python-list
In case someone is actually going to execute the code, there is a bug:

`set` need to be wrapped in `len` for criteria args.

> On 15 Nov 2023, at 20:13, Dom Grigonis  wrote:
> 
> 
> The specific situation was related to truth values and following out of that 
> my considerations regarding equivalent of all and any for counting `truths`.
> 
> So one of the specific examples:
> class Publisher:
> def __init__(self):
> self.subscribers = dict()
> 
> def subscribe(self, sub, criteria, agg=all):
> self.subscribers[sub] = (criteria, agg)
> 
> def publish(self, msg):
> for sub, criteria in self.subscribers.items():
> if criteria(msg):
> sub.handle(msg)
> 
> p = Publisher()
> p.subscribe(sub, lambda x: all(r > 3 for r in x.ratings))
> 
> # So what I needed is:
> p.subscribe(sub, lambda x: set(r > 3 for r in x.ratings) > 50)
> # Note, that elements might not necessarily be bool.
> # E.g. at least 51 non-empty pages
> p.subscribe(sub, lambda x: set(bool(p) for p in x.pages) > 50)
> # So the function:
> p.subscribe(sub, lambda x: xor_more_than(x.pages, 50)
> # would ideally deal with truth values too.
> The question is: can you construct a function? which:
> a) performs: lambda x: set(bool(el) for el in iterable) > n
> b) is in line with performance of python’s `all`
> 
> Then, as I said the case where one would need `set() == n` is hard to think 
> of, but it seems fairly probable to need `set() > n` and `set() < n`.
> 
> Regards,
> DG
> 
>> On 15 Nov 2023, at 19:16, Peter J. Holzer via Python-list 
>> mailto:python-list@python.org>> wrote:
>> 
>> On 2023-11-15 12:26:32 +0200, Dom Grigonis wrote:
>>> 
>>> Thank you,
>>> 
>>> 
>>> test2 = [True] * 100 + [False] * 2
>>> test2i = list(range(100))
>>> 
>>> %timeit len(set(test2i)) == 1   # 1.6 µs ± 63.6 ns per loop (mean ± std. 
>>> dev. of 7 runs, 1,000,000 loops each)
>>> %timeit all(test2)  # 386 ns ± 9.58 ns per loop (mean ± std. 
>>> dev. of 7 runs, 1,000,000 loops each)
>>> 
>>> test2s = set(test2i)
>>> %timeit len(test2s) == 1# 46.1 ns ± 1.65 ns per loop (mean ± std. 
>>> dev. of 7 runs, 10,000,000 loops each)
>>> 
>>> If you pre-convert to set it is obviously faster. However, set
>>> operation is most likely going to be part of the procedure. In which
>>> case it ends up to be significantly slower.
>> 
>> Obviously, if you convert a list to a set just to count the elements
>> it's going to be slow. My suggestion was to use the set *instead* of the
>> list. I don't know whether that's possible in your situation, because
>> you haven't told us anything about it. All I'm suggesting is taking a
>> step back and reconsider your choice of data structure.
>> 
>>hp
>> -- 
>>   _  | Peter J. Holzer| Story must make more sense than reality.
>> |_|_) ||
>> | |   | h...@hjp.at <mailto:h...@hjp.at> |-- Charles Stross, 
>> "Creative writing
>> __/   | http://www.hjp.at/ <http://www.hjp.at/> |   challenge!"
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list 
>> <https://mail.python.org/mailman/listinfo/python-list>
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __set_name__ equivalent for instance

2023-11-16 Thread Dom Grigonis via Python-list
What I am interested in is a callback.
Preferably just after methods get bound. So in `object.__new__`.

I have done it via metaclass, but it is not ideal as there would be too much 
overhead.

I think what I am looking for is custom method binding.

Regards,
DG

> On 16 Nov 2023, at 20:02, Dieter Maurer  wrote:
> 
> Dom Grigonis wrote at 2023-11-15 18:44 +0200:
>> So there is a method __set_name__ which is called on class creation.
>> 
>> The functionality that I am interested in is not retrieving name, but the 
>> fact that it also receives `owner` argument.
>> 
>> Thus, allowing simulation of bound class method.
>> 
>> I was wandering if there is an equivalent functionality of attribute to 
>> receive `instance` argument on instance creation.
> 
> As PEP 487 describes, `__set_name__` essentially targets descriptors.
> It is there to inform a descriptor about the name it is used for
> in a class (and the class itself). There is no other (easy) way to allow
> a descriptor to learn about this name.
> 
> If a descriptor is accessed via an instance, the descriptor (protocol)
> methods get the instance as parameter.
> Note that descriptors are stored in the class: they must not store
> instance specific information in their attributes.
> Therefore, a method informing an descriptor about instance creation
> would not help: it cannot do anything with it.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __set_name__ equivalent for instance

2023-11-16 Thread Dom Grigonis via Python-list



> On 16 Nov 2023, at 21:00, Dieter Maurer  wrote:
> 
> Dom Grigonis wrote at 2023-11-16 20:12 +0200:
>> What I am interested in is a callback.
>> Preferably just after methods get bound. So in `object.__new__`.
> 
>> I have done it via metaclass, but it is not ideal as there would be too much 
>> overhead.
>> 
>> I think what I am looking for is custom method binding.
> 
> Methods are not bound during instance creation, they are bound during
> access.

Good to know. What is the criteria for binding then? Does it check if its type 
is `vanilla` function? If yes, is there any way to simulate it for arbitrary 
object?

I have tried inheriting from function type, but not allowed.

> You can use descriptors to implement "custom method binding".
> 
> Descriptors are defined on the class (and do not behave as
> descriptors when defined on instances).
> Thus, you would need a metaclass or `__inist_subclass__` is you
> want your "custom method binding" globally.

Yes, I have tried that. This works well. But maybe will be useful for another 
case.

Currently, the focus is decorators that can be used on arbitrary methods. 
Needing to inherit and add metaclasses whenever I want to decorate is not an 
option.

I think I will continue with descriptor approach and am slowly finding route to 
get where I need to, but still exploring options.

Regards,
DG
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __set_name__ equivalent for instance

2023-11-16 Thread Dom Grigonis via Python-list
Thank you.

> On 16 Nov 2023, at 21:30, Dieter Maurer  wrote:
> 
> Dom Grigonis wrote at 2023-11-16 21:11 +0200:
>> ...
>>> On 16 Nov 2023, at 21:00, Dieter Maurer  wrote:
>>> ...
>>> Methods are not bound during instance creation, they are bound during
>>> access.
>> 
>> Good to know. What is the criteria for binding then? Does it check if its 
>> type is `vanilla` function? If yes, is there any way to simulate it for 
>> arbitrary object?
> 
> Attribute access (including method binding) is documented in the
> language reference.
> 
> Functions and descriptors accessed via an instance but found in a class (i.e.
> not directly in the instance) are handled specially;
> functions are bound. Other objects are returned as is.
> 
> `__getattribute__` can be used to take over control over the attribute
> access.

-- 
https://mail.python.org/mailman/listinfo/python-list


argparse argument post-processing

2023-11-27 Thread Dom Grigonis via Python-list
Hi all,

I have a situation, maybe someone can give some insight.

Say I want to have input which is comma separated array (e.g. 
paths='path1,path2,path3') and convert it to the desired output - list:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('paths', type=lambda x: list(filter(str.strip, 
x.split(',' 
So far so good. But this is just an example of what sort of solution I am after.

-

Now the second case. I want input to be space separated array - bash array. And 
I want space-separated string returned. My current approach is:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('paths', nargs='+')
args = parser.parse_args()
paths = ' '.join(args.paths)
But what I am looking for is a way to do this, which is intrinsic to `argparse` 
module. Reason being I have a fair amount of such cases and I don’t want to do 
post-processing, where post-post-processing happens (after 
`parser.parse_args()`).

I have tried overloading `parse_args` with post-processor arguments, and that 
seemed fine, but it stopped working when I had sub-parsers, which are defined 
in different modules and do not call `parse_args` themselves.

Any ideas appreciated,
Regards,
DG

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse argument post-processing

2023-11-27 Thread Dom Grigonis via Python-list
Thank you, exactly what I was looking for!

One more question following this. Is there a way to have a customisable action? 
I.e. What if I want to join with space in one case and with coma in another. Is 
there a way to reuse the same action class?

Regards,
DG

> On 27 Nov 2023, at 21:55, Mats Wichmann via Python-list 
>  wrote:
> 
> On 11/27/23 04:29, Dom Grigonis via Python-list wrote:
>> Hi all,
>> I have a situation, maybe someone can give some insight.
>> Say I want to have input which is comma separated array (e.g. 
>> paths='path1,path2,path3') and convert it to the desired output - list:
>> import argparse
>> parser = argparse.ArgumentParser()
>> parser.add_argument('paths', type=lambda x: list(filter(str.strip, 
>> x.split(','
>> So far so good. But this is just an example of what sort of solution I am 
>> after.
> 
> Maybe use "action" rather than "type" here? the conversion of a csv argument 
> into words seems more like an action.
> 
>> Now the second case. I want input to be space separated array - bash array. 
>> And I want space-separated string returned. My current approach is:
>> import argparse
>> parser = argparse.ArgumentParser()
>> parser.add_argument('paths', nargs='+')
>> args = parser.parse_args()
>> paths = ' '.join(args.paths)
>> But what I am looking for is a way to do this, which is intrinsic to 
>> `argparse` module. Reason being I have a fair amount of such cases and I 
>> don’t want to do post-processing, where post-post-processing happens (after 
>> `parser.parse_args()`).
>> I have tried overloading `parse_args` with post-processor arguments, and 
>> that seemed fine, but it stopped working when I had sub-parsers, which are 
>> defined in different modules and do not call `parse_args` themselves.
> 
> Depending on what *else* you need to handle it may or not may work here to 
> just collect these from the remainders, and then use an action to join them, 
> like:
> 
> import argparse 
> 
> class JoinAction(argparse.Action): 
>def __call__(self, parser, namespace, values, option_string=None): 
>setattr(namespace, self.dest, ' '.join(values)) 
> 
> parser = argparse.ArgumentParser() 
> parser.add_argument('paths', nargs=argparse.REMAINDER, action=JoinAction)
> args = parser.parse_args() 
> 
> print(f"{args.paths!r}") 
> 
> 
> 
> 
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse argument post-processing

2023-11-27 Thread Dom Grigonis via Python-list
Yeah, I have been hearing that people are having troubles converting, but I 
have only used argparse - got lucky there I guess.

I am thinking just making the function which spits the class out. Maybe not 
very optimised solution, but simple.

Argument parsing in my case is very far from being a bottleneck.

> On 27 Nov 2023, at 22:36, Mats Wichmann  wrote:
> 
> On 11/27/23 13:21, Dom Grigonis wrote:
>> Thank you, exactly what I was looking for!
>> One more question following this. Is there a way to have a customisable 
>> action? I.e. What if I want to join with space in one case and with coma in 
>> another. Is there a way to reuse the same action class?
> 
> I've worked more with optparse (the project I work on that uses it has 
> reasons why it's not feasible to convert to argparse); in optparse you use a 
> callback function, rather than an action class, and the change to a callable 
> class is somewhat significant :-; so I'm not really an expert.
> 
> The question is how you determine which you want to do - then there's no 
> problem for the action class's call method to implement it. I presume you can 
> write an initializer class that takes an extra argument, collect that and 
> stuff it into an instance variable, then use super to call the base Action 
> class's initializer with the rest of the args
> 
> super().__init__(option_strings=option_strings, *args, **kwargs)
> 
> Hopefully someone else has done this kind of thing because now I'm just 
> guessing!
> 
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on writing a number as 2^s * q, where q is odd

2023-11-29 Thread Dom Grigonis via Python-list
def powers_of_2_in(n):
s = 0
while n % 2 == 0:
s += 1
n = n // 2
return s, n

> On 30 Nov 2023, at 02:44, Julieta Shem via Python-list 
>  wrote:
> 
> How would you write this procedure?
> 
> --8<---cut here---start->8---
> def powers_of_2_in(n):
>  s = 0
>  while "I still find factors of 2 in n...":
>q, r = divmod(n, 2)
>if r == 0:
>  s = s + 1
>  n = n // 2
>else:
>  return s, n
> --8<---cut here---end--->8---
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Request: inspect: signature & getfullargspec & getcallargs

2023-12-03 Thread Dom Grigonis via Python-list
Hello,

I have a request.

Would it be possible to include `follow_wrapper_chains` and `skip_bound_arg` 
arguments to higher level functions of `inspect` module?

Would exposing them, but setting defaults to what they currently are, be 
possible?

I sometimes need:
* `getcallargs`, but without `bound_arg`
* `getfullargspec` to `follow_wrapper_chains`
* `signature` to include `bound_arg`.


Regards,
DG
-- 
https://mail.python.org/mailman/listinfo/python-list