Re: Checking if email is valid

2023-11-02 Thread Alan Bawden via Python-list
Chris Angelico  writes:

   On Thu, 2 Nov 2023 at 15:20, AVI GROSS via Python-list
wrote:

   > Yes, it would be nice if there was a syntax for sending a test
   > message sort of like an ACK that is not delivered to the recipient
   > but merely results in some status being sent back such as
   > DELIVERABLE or NO SUCH USER or even MAILBOX FULL.

   Yes, it would! Spammers would be able to use this syntax to figure out
   exactly which addresses actually have real people connected to it. It
   would save them so much trouble! Brilliant idea.

That sounds like the SMTP "VRFY" command.  And spammers _did_ abuse it
in exactly this manner.  And so pretty much every mail server in the
world disabled VRFY sometime in the 90s.

- Alan
-- 
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 Alan Bawden via Python-list
Julieta Shem  writes:

   How would you write this procedure?
   def powers_of_2_in(n):
   ...

def powers_of_2_in(n):
return (n ^ (n - 1)).bit_count() - 1
-- 
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 Alan Bawden via Python-list
jak  writes:

   Alan Bawden ha scritto:
   > Julieta Shem  writes:
   >
   > How would you write this procedure?
   > def powers_of_2_in(n):
   > ...
   >
   > def powers_of_2_in(n):
   >  return (n ^ (n - 1)).bit_count() - 1
   >

   Great solution, unfortunately the return value is not a tuple as in the
   OP version. Maybe in this way?

   def powers_of_2_inB(n):
   bc = (n ^ (n - 1)).bit_count() - 1
   return bc, int(n / (1 << bc))

Good point.  I overlooked that.  I should have written:

def powers_of_2_in(n):
bc = (n ^ (n - 1)).bit_count() - 1
return bc, n >> bc
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2023-12-05 Thread Alan Bawden via Python-list
jak  writes:

   Oscar Benjamin ha scritto:
   ...
   If we now use the function being discussed:

   powers_of_2_in(n)
   (63, 1)

   we can see that the bit_count() method had to do 63 iterations to count
   the bits

I certainly hope that the bit_count method doesn't count bits by
iterating over them one-by-one.  You can count bits _much_ faster than
that.

You can count the bits in a 62-bit number as follows:

   def bit_count_62(n):
   n = (n - ((n >> 1) & 0o3)
  - ((n >> 2) & 0o1))
   n = (   (n & 0o307070707070707070707)
+ ((n & 0o070707070707070707070) >> 3))
   return n % 63

Then if you want to count the bits in arbitrarily large integers, you
iterate over them in N-bit chunks, for some N <= 62.  Your choice of N
will depend on how you represent your big integers.  Probably N is 56 or
48 or 32.

And why 62 bits?  Because the bit_count method is certainly written in
C, where every step in bit_count_62 would use 64-bit integers.

If you like this sort of stuff, check out the book "Hacker's Delight" by
Henry Warren.  See .

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


Re: Is there a way to implement the ** operator on a custom object

2024-02-09 Thread Alan Bawden via Python-list
Chris Angelico  writes:

   > On 08Feb2024 12:21, [email protected]  
wrote:
   > >I know that mappings by default support the ** operator, to unpack the
   > >mapping into key word arguments.
   > >
   > >Has it been considered implementing a dunder method for the **
   > >operator so you could unpack an object into a key word argument, and
   > >the developer could choose which keywords would be generated (or could
   > >even generate 'virtual' attributes).

   I presume this is more like:

   obj = SomeObject()
   func(**obj)

   ie making the object behave in a dict-like way. I can't remember how
   this is implemented, but you can create the necessary methods to have
   your object produce whatever it likes.

All you need to do is subclass collections.abc.Mapping, and
implement __len__, __iter__, and __getitem__.  Pretty easy.

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


Re: Python Dialogs (Posting On Python-List Prohibited)

2024-05-03 Thread Alan Bawden via Python-list
Lawrence D'Oliveiro  writes:

   > Assume you have an expression "s.replace('a','b').replace('c','d').
   > replace('e','f').replace('g','h')". Its value is a string which
   > is the value of s, but with "a" replaced by "b", "c" replaced by
   > "d", "e" replaced by "f" and "g" replaced by "h". How to modify
   > this expression, so that "a", "c", "e", and "g", respectively,
   > are replaced only if they are words (but not parts of words)?

   import re

   replacements = (("a", "b"), ("c", "d"), ("e", "f"), ("g", "h"))

   text = "this be a test g eg"

   "".join \
 (
   repl.get(s, s)
   for repl in (dict(replacements),)
   for s in
   re.split("\\b(" + "|".join(re.escape(s[0]) for s in 
replacements) + ")\\b", text)
 )

How about just:

  repl = {
  "a" : "b",
  "c" : "d",
  "e" : "f",
  "g" : "h",
  }

  "".join(repl.get(s, s) for s in re.split(r"\b", text))

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


Re: how to discover what values produced an exception?

2024-05-06 Thread Alan Bawden via Python-list
Thomas Passin  writes:

   On 5/3/2024 9:56 AM, Johanne Fairchild via Python-list wrote:
   > How to discover what values produced an exception?  Or perhaps---why
   > doesn't the Python traceback show the values involved in the TypeError?
   > For instance:
   >
   > --8<>8---
    (0,0) < 4
   > Traceback (most recent call last):
   >File "", line 1, in 
   > TypeError: '<' not supported between instances of 'tuple' and 'int'
   > --8<>8---
   >
   > It could have said something like:
   >
   > --8<>8---
   > TypeError: '<' not supported between instances of 'tuple' and 'int'
   >in (0,0) < 4.
   > --8<>8---
   >
   > We would know which were the values that caused the problem, which would
   > be very helpful.

   In this example it would not help at all to know the actual values. Knowing
   that you are trying to compare incomparable types is enough.

In general, it absolutely can help.  The programmer can sometimes
recognize where a value of unexpected type came from just by looking at
it, allowing her to quickly deduce just what went wrong without further
investigation.

A good error message shouldn't withhold any information that can
_easily_ be included.  Debugging is more art than science, so there is
no real way to predict what information might prove useful in solving
the crime.  I emphasized "easily" because of course you have to draw the
line somewhere.

The fact that Python error messages often fail to mention the actual
objects that caused the error has always annoyed me.  I've always
presumed that for some reason it just wasn't easy to do.  And it's never
been more than a minor annoyance to me.

So the OP is not wrong for wishing for this.  Other programming
languages do it.  Other Python programmers miss it.

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


Python told me a Joke

2024-09-03 Thread Alan Bawden via Python-list
Python 3.10.5 (v3.10.5:f37715, Jul 10 2022, 00:26:17) [GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x,_,z = [1,2,3]

Works as expected.

Now I didn't expect the following to work (but Python sometimes
surprises me!), so I tried:
 
>>> x,2,z = [1,2,3]
  File "", line 1
x,2,z = [1,2,3]
^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Yeah, that makes sense, no surprises today...  Except "maybe you meant
'=='..." caught my attention.  _Could_ that be what someone would want
in this situation I wondered?  So I tried:

>>> x,2,z == [1,2,3]
(1, 2, False)

Now that made me laugh.

- Alan

[ Some people reading this will be tempted to explain what's really
  going on here -- it's not hard to understand.  But please remember that
  a joke is never funny if you have to explain it. ]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Correct syntax for pathological re.search()

2024-10-08 Thread Alan Bawden via Python-list
Karsten Hilbert  writes:

   Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
   Type "help", "copyright", "credits" or "license" for more 
information.
   >>> tex = '\sout{'
   >>> tex
   '\\sout{'
   >>>

   Am I missing something ?

You're missing the warning it generates:

> python -E -Wonce
Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> tex = '\sout{'
:1: DeprecationWarning: invalid escape sequence '\s'
>>> 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bool type have big problem. finally program returned "True".Is this the TRUE spec?

2025-01-20 Thread Alan Bawden via Python-list
あうぇくろ  writes:

   tpr=composite(type,print)
   print(tpr('a')==tpr(1))
   Why does tpr('a')==tpr(1) return True?

Because tpr always returns the value None.
-- 
https://mail.python.org/mailman/listinfo/python-list