Re: .0 in name

2022-05-13 Thread Avi Gross via Python-list
You left out 3CPO, Dave.

Names with numerals are not unreasonable in some circumstances.

But programming languages are not a full spectrum of real life. They can have 
reserved words too so you may not be able to use "while" and if you choose to 
create a function that masks another, you cannot complain that you assumed the 
computer would be smart enough to figure out which one you wanted. Yes, some 
languages encourage multiple functions with the same name but different 
signatures. Bottom line is each has RULES and some serious SUGGESTIONS. It is 
what it is, not what you want it to be.
But although starting with a numeral is verboten for variable names, may I 
suggest that a relatively invisible character can make a name like _3CPO that 
will be allowed. But be careful as Python programs often have conventions on 
use of the underscore and don't even think about a dundering name like __init__ 
...


-Original Message-
From: dn 
To: python-list@python.org
Sent: Sat, May 14, 2022 12:33 am
Subject: Re: .0 in name

This is not what @Avi menat by "silly variable names" but:

3D_position

2nd_floor_area

3M_PostIt_size

3rd_degree_polynomial

360_degree_view

12_hours_later

and ???
2_fast_2_furious

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


Re: EAFP

2022-05-13 Thread dn
On 14/05/2022 04.37, bryangan41 wrote:
> Is the following LBYL:foo = 123if foo < 200:    do()

Yes (once formatted for Python).


If so, how to change to EAFP?

Not sure if can. Let's alter the code to:

foo = 0

#and

def do():
return 5 / foo

Then, you will expect a ZeroDivisionError exception to be raised -
whereas with other values, there will (likely) be no problem.

Now, we have a clear identification for when 'forgiveness' will need to
be requested! The 'EAFP' solution encloses the function-call:

foo = 123
try:
do()
except ZeroDivisionError:
undo()


In the OP's code-snippet, the "200" criteria means there is no such
clean-cut and automatic 'failure' attached to, or implied by, foo's
value. However, we could define a custom-exception and 'raise the alarm'
when 'forgiveness' is required:

class ValueTooLargeError( ValueError):
"""Catch values larger than the valid range."""

def do():
if foo < 200:
...
else:
raise ValueTooLargeError

This is a pythonic-construct (and thus a recommendable coding-pattern),
in that the calling-routine can decide how to respond to the exception -
which many vary according to the specifics of do()'s re-use.


However, is it "EAFP"? We had to introduce the exact if-condition which
makes it "LBYL"!

Perhaps time for me to bow-out, and leave such to the philosophers...


Sent from Samsung tablet.

There are pills for that...
-- 
Regards,
=dn
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: .0 in name

2022-05-13 Thread dn
This is not what @Avi menat by "silly variable names" but:

3D_position

2nd_floor_area

3M_PostIt_size

3rd_degree_polynomial

360_degree_view

12_hours_later

and ???
2_fast_2_furious

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


Re: Seeking deeper understanding of python equality (==)

2022-05-13 Thread Jonathan Kaczynski
Thank you for your responses, Sam and Greg.

The do_richcompare function is where my research originally took me, but I
feel like I'm still missing some pieces to the puzzle.

Here is my updated research since you posted your responses (I'll attach a
pdf copy too):
https://docs.google.com/document/d/10zgOMetEQtZCiYFnSS90pDnNZD7I_-MFohSy83pOieA/edit#
The summary section, in the middle, is where I've summarized my reading of
the source code.

Greg, your response here,

> Generally what happens with infix operators is that the interpreter
> first looks for a dunder method on the left operand. If that method
> doesn't exist or returns NotImplemented, it then looks for a dunder
> method on the right operand.

reads like the contents of the do_richcompare function.

What I think I'm missing is how do the dunder methods relate to
the tp_richcompare function?

Thank you,
Jonathan


On Fri, May 6, 2022 at 11:55 PM Greg Ewing 
wrote:

> On 7/05/22 12:22 am, Jonathan Kaczynski wrote:
> > Stepping through the code with gdb, we see it jump from the compare
> > operator to the dunder-eq method on the UUID object. What I want to be
> able
> > to do is explain the in-between steps.
>
> Generally what happens with infix operators is that the interpreter
> first looks for a dunder method on the left operand. If that method
> doesn't exist or returns NotImplemented, it then looks for a dunder
> method on the right operand.
>
> There is an exception if the right operand is a subclass of the
> left operand -- in that case the right operand's dunder method
> takes precedence.
>
> > Also, if you change `x == y` to `y
> > == x`, you still see the same behavior, which I assume has to do with
> > dunder-eq being defined on the UUID class and thus given priority.
>
> No, in that case the conparison method of str will be getting
> called first, but you won't see that in pdb because it doesn't
> involve any Python code. Since strings don't know how to compare
> themselves with uuids, it will return NotImplemented and the
> interpreter will then call uuid's method.
>
> --
> Greg
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: .0 in name

2022-05-13 Thread Avi Gross via Python-list
Boy do I hate when I see my code mangled by the stupid AOL mailer.

Not that anyone cares, but the code should be read with each line starting with 
the "> " prompt.

If I leave lots of blank lines, it may work, but as the illustration is not in 
python, I will now remove the prompts:


`5x^2 + 2.3x` <- 666

`+-+-+` <- 1000

1 + 2 * `5x^2 + 2.3x` + `+-+-+`

output: 2333 

There are rarely good reasons for such silly variable names but as long as you 
let it know to leave the quoted regions alone when parsing and look them us as 
exact entries in various environments, it works fine.

To add to what others already wrote, many languages have serious requirements 
you need to be aware of and not assume otherwise. Some allow underscores in 
names and may limit that in the first part or may in some cases suggest or 
require it. Some have rules about whether a variable of one kind should start 
with an uppercase letter. Some allow periods in names although an initial 
period may make it invisible for some purposes. And, some newer languages allow 
all kinds of UNICODE characters and perhaps even some that can be seen as 
numeric but aren't exactly 0-9. 

① ② ③
 ... ❽ ❽



-Original Message-
From: Avi Gross via Python-list 
To: python-list@python.org 
Sent: Fri, May 13, 2022 6:02 pm
Subject: Re: .0 in name

Bryan,
As has been pointed out, it is very common in possibly all programming 
languages to not allow digits at the start of many identifiers. It makes it 
hard to parse for numbers which tend to start with digits. Some languages even 
have special rules on not starting a number with a zero unless you mean for it 
to be seen as octal (or 0x for hexadecimal) and many other rules exist.

There are languages where 12x means 12*x so even the lack of an operator ...

There are exceptions that often are not really exceptions. You can use all 
kinds of unusual variables in some quoted context. It is valid (albeit not 
encouraged) to use backquoted

The following is perfectly allowed in R:

> `5x^2 + 2.3x` <- 666 > `+-+-+` <- 1000 > 1 + 2 * `5x^2 + 2.3x` + `+-+-+` [1] 
> 2333 
And there are often issued when you do things like create the name of a column 
of data in a data.frame with embedded spaces and other anomalies requiring 
special handling.
So why you wonder where it is documented that variables cannot be what you feel 
like is a bit puzzling! 


-Original Message-
From: bryangan41 
To: python-list@python.org
Sent: Fri, May 13, 2022 12:47 pm
Subject: .0 in name

May I know (1) why can the name start with a number?(2) where in the doc is 
it?!>>> import pdb>>> pdb.run('(a for a in "")')> (1)()(Pdb) 
s--Call--> (1)()(Pdb) a.0 = (Pdb) c>>>Sent from Samsung tablet.
-- 
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: .0 in name

2022-05-13 Thread Paul Bryan
On Fri, 2022-05-13 at 22:02 +, Avi Gross via Python-list wrote:

> So why you wonder where it is documented that variables cannot be
> what you feel like is a bit puzzling! 

I had just assumed on good faith that the request to the documentation
would be so that the OP could determine what is valid identifier
syntax.

Paul


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


Re: .0 in name

2022-05-13 Thread Avi Gross via Python-list
Bryan,
As has been pointed out, it is very common in possibly all programming 
languages to not allow digits at the start of many identifiers. It makes it 
hard to parse for numbers which tend to start with digits. Some languages even 
have special rules on not starting a number with a zero unless you mean for it 
to be seen as octal (or 0x for hexadecimal) and many other rules exist.

There are languages where 12x means 12*x so even the lack of an operator ...

There are exceptions that often are not really exceptions. You can use all 
kinds of unusual variables in some quoted context. It is valid (albeit not 
encouraged) to use backquoted

The following is perfectly allowed in R:

> `5x^2 + 2.3x` <- 666 > `+-+-+` <- 1000 > 1 + 2 * `5x^2 + 2.3x` + `+-+-+` [1] 
> 2333 
And there are often issued when you do things like create the name of a column 
of data in a data.frame with embedded spaces and other anomalies requiring 
special handling.
So why you wonder where it is documented that variables cannot be what you feel 
like is a bit puzzling! 


-Original Message-
From: bryangan41 
To: python-list@python.org
Sent: Fri, May 13, 2022 12:47 pm
Subject: .0 in name

May I know (1) why can the name start with a number?(2) where in the doc is 
it?!>>> import pdb>>> pdb.run('(a for a in "")')> (1)()(Pdb) 
s--Call--> (1)()(Pdb) a.0 = (Pdb) c>>>Sent from Samsung tablet.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: .0 in name

2022-05-13 Thread Paul Bryan
On Sat, 2022-05-14 at 00:47 +0800, bryangan41 wrote:

> May I know (1) why can the name start with a number?

The name of an attribute must be an identifier. An identifier cannot
begin with a decimal number.

> (2) where in the doc is it?!

https://docs.python.org/3/reference/lexical_analysis.html#identifiers

Paul

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


EAFP

2022-05-13 Thread bryangan41
Is the following LBYL:foo = 123if foo < 200:    do()If so, how to change to 
EAFP?Thanks!Sent from Samsung tablet.
-- 
https://mail.python.org/mailman/listinfo/python-list


.0 in name

2022-05-13 Thread bryangan41
May I know (1) why can the name start with a number?(2) where in the doc is 
it?!>>> import pdb>>> pdb.run('(a for a in "")')> (1)()(Pdb) 
s--Call--> (1)()(Pdb) a.0 = (Pdb) c>>>Sent from Samsung tablet.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Changing calling sequence

2022-05-13 Thread Martin Di Paola

You probably want something like overload/multiple dispatch. I quick search
on PyPI yields a 'multipledispatch' package.

I never used, however.

On Wed, May 11, 2022 at 08:36:26AM -0700, Tobiah wrote:

On 5/11/22 06:33, Michael F. Stemper wrote:

I have a function that I use to retrieve daily data from a
home-brew database. Its calling sequence is;

def TempsOneDay( year, month, date ):

After using it (and its friends) for a few years, I've come to
realize that there are times where it would be advantageous to
invoke it with a datetime.date as its single argument.


You could just use all keyword args:

def TempsOneDay(**kwargs):

   if 'date' in kwargs:
   handle_datetime(kwargs['date'])
   elif 'year' in kwargs and 'month' in kwargs and 'day' in kwargs:
   handle_args(kwargs['year'], kwargs['month'], kwargs['day'])
   else:
   raise Exception("Bad keyword args")

TempsOneDay(date=datetime.datetime.now)

TempsOneDay(year=2022, month=11, day=30)

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

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


Re: Accuracy of multiprocessing.Queue.qsize before any Queue.get invocations?

2022-05-13 Thread Martin Di Paola

If the queue was not shared to any other process, I would guess that its
size is reliable.

However, a plain counter could be much simpler/safer.

The developer of multiprocessing.Queue, implemented
size() thinking in how to share the size and maintain a reasonable
consistency between process.
He/she probably didn't care how well works in a single-process scenario
as this is a very special case.

Thanks,
Martin.


On Thu, May 12, 2022 at 06:07:02PM -0500, Tim Chase wrote:

The documentation says[1]


Return the approximate size of the queue. Because of
multithreading/multiprocessing semantics, this number is not
reliable.


Are there any circumstances under which it *is* reliable?  Most
germane, if I've added a bunch of items to the Queue, but not yet
launched any processes removing those items from the Queue, does
Queue.qsize accurately (and reliably) reflect the number of items in
the queue?

 q = Queue()
 for fname in os.listdir():
   q.put(fname)
 file_count = q.qsize() # is this reliable?
 # since this hasn't yet started fiddling with it
 for _ in range(os.cpu_count()):
   Process(target=myfunc, args=(q, arg2, arg3)).start()

I'm currently tracking the count as I add them to my Queue,

 file_count = 0
 for fname in os.listdir():
   q.put(fname)
   file_count += 1

but if .qsize is reliably accurate before anything has a chance to
.get data from it, I'd prefer to tidy the code by removing the
redunant counting code if I can.

I'm just not sure what circumstances the "this number is not
reliable" holds.  I get that things might be asynchronously
added/removed once processes are running, but is there anything that
would cause unreliability *before* other processes/consumers run?

Thanks,

-tkc

[1]
https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Queue.qsize





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

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


Re: tail

2022-05-13 Thread 2QdxY4RzWzUUiLuE
On 2022-05-13 at 12:16:57 +0200,
Marco Sulla  wrote:

> On Fri, 13 May 2022 at 00:31, Cameron Simpson  wrote:

[...]

> > This is nearly the worst "specification" I have ever seen.

> You're lucky. I've seen much worse (or no one).

At least with *no* documentation, the source code stands for itself.  If
I can execute it (whatever that entails), then I can (in theory) figure
out *what* it does.  I still don't what it's *supposed* to do, and
therefore *cannot* know how well it does or doesn't "work,", but at
least source code is deterministic and unambiguous (except when it
isn't, but let's not go there).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tail

2022-05-13 Thread Marco Sulla
On Fri, 13 May 2022 at 00:31, Cameron Simpson  wrote:

> On 12May2022 19:48, Marco Sulla  wrote:
> >On Thu, 12 May 2022 at 00:50, Stefan Ram  wrote:
> >>   There's no spec/doc, so one can't even test it.
> >
> >Excuse me, you're very right.
> >
> >"""
> >A function that "tails" the file. If you don't know what that means,
> >google "man tail"
> >
> >filepath: the file path of the file to be "tailed"
> >n: the numbers of lines "tailed"
> >chunk_size: oh don't care, use it as is
>
> This is nearly the worst "specification" I have ever seen.
>

You're lucky. I've seen much worse (or no one).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Solved] Re: Windows registry PermissionError

2022-05-13 Thread Mike Dewhirst

On 13/05/2022 4:37 pm, Eryk Sun wrote:

On 5/13/22, Mike Dewhirst  wrote:

On 13/05/2022 4:14 pm, Eryk Sun wrote:

Since self.connect() is always called, you should document that the
initial hkey parameter has to be one of the following predefined key
handles:

  HKEY_LOCAL_MACHINE
  HKEY_USERS

I'm targeting HKEY_CURRENT_USER so I assume HK_USERS includes that.

Using HKEY_CURRENT_USER with RegConnectRegistryW() to access a remote
registry isn't well defined and not documented as supported. If it
works at all, the API probably just opens a subkey of the remote
HKEY_USERS based on the string SID (security identifier string) of the
current user. That may fail as not found since user SIDs are unique to
machines, unless it's on a domain.

Bear in mind that the remote registry service is running on the remote
machine as SYSTEM (S-1-5-18) in the non-interactive services session.
If it literally accessed its "current user", then it would open
"HKEY_USERS\S-1-5-18".
In that case, I'll remove 'computer' as a parameter and lock it in as 
None. I'll document why and if I ever need to access a remote registry 
I'll do some research along the lines you suggest.


My case demands execution of this code via login script for end-user 
configuration after the IT department has done a bulk installation 
rollout. One of key items is ComputerName which is in HKLM and needs to 
be in HKCU for whichever user logs in. The application looks in HKCU for 
a unique Device Reference and ComputerName suffices and is attractive 
because it is also the asset number of the machine.



--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.



OpenPGP_signature
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Solved] Re: Windows registry PermissionError

2022-05-13 Thread Eryk Sun
On 5/13/22, Mike Dewhirst  wrote:
> On 13/05/2022 4:14 pm, Eryk Sun wrote:
>> Since self.connect() is always called, you should document that the
>> initial hkey parameter has to be one of the following predefined key
>> handles:
>>
>>  HKEY_LOCAL_MACHINE
>>  HKEY_USERS
>
> I'm targeting HKEY_CURRENT_USER so I assume HK_USERS includes that.

Using HKEY_CURRENT_USER with RegConnectRegistryW() to access a remote
registry isn't well defined and not documented as supported. If it
works at all, the API probably just opens a subkey of the remote
HKEY_USERS based on the string SID (security identifier string) of the
current user. That may fail as not found since user SIDs are unique to
machines, unless it's on a domain.

Bear in mind that the remote registry service is running on the remote
machine as SYSTEM (S-1-5-18) in the non-interactive services session.
If it literally accessed its "current user", then it would open
"HKEY_USERS\S-1-5-18".
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Solved] Re: Windows registry PermissionError

2022-05-13 Thread Mike Dewhirst

On 13/05/2022 4:14 pm, Eryk Sun wrote:

Since self.connect() is always called, you should document that the
initial hkey parameter has to be one of the following predefined key
handles:

 HKEY_LOCAL_MACHINE
 HKEY_USERS


I'm targeting HKEY_CURRENT_USER so I assume HK_USERS includes that.

Thanks again Eryk

Cheers

mike


 HKEY_PERFORMANCE_DATA

WinAPI RegConnectRegistryW() only matters when the target computer is
a different machine, in which case an RPC proxy handle is returned.



--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.



OpenPGP_signature
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Solved] Re: Windows registry PermissionError

2022-05-13 Thread Eryk Sun
Since self.connect() is always called, you should document that the
initial hkey parameter has to be one of the following predefined key
handles:

HKEY_LOCAL_MACHINE
HKEY_USERS
HKEY_PERFORMANCE_DATA

WinAPI RegConnectRegistryW() only matters when the target computer is
a different machine, in which case an RPC proxy handle is returned.
-- 
https://mail.python.org/mailman/listinfo/python-list