Re: Organizing modules and their code

2023-02-04 Thread Greg Ewing via Python-list

On 5/02/23 11:18 am, transreductionist wrote:

This analogy came to me the other day. For me, I would rather walk into a 
grocery store where the bananas, apples, and oranges are separated in to their 
own bins, instead of one common crate.


On the other hand, if the store has an entire aisle devoted to each
fruit, but only ever one crate of fruit in each aisle, one would think
they could make better use of their shelf space.

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


Re: Organizing modules and their code

2023-02-05 Thread Greg Ewing via Python-list

On 6/02/23 4:23 am, Weatherby,Gerard wrote:

Well, first of all, while there is no doubt as to Dijkstra’s contribution to 
computer science, I don’t think his description of scientific thought is 
correct. The acceptance of Einstein’s theory of relativity has nothing to do 
with internal consistency or how easy or difficult to explain but rather 
repeatedly experimental results validating it.


I don't think Dijkstra was claiming that what he was talking about
was a *complete* description of scientific thought, only that the
ability to separate out independent concerns is an important part
of it, and that was something he saw his colleagues failing to do.

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


Re: ChatGPT Generated news poster code

2023-02-10 Thread Greg Ewing via Python-list

For a moment I thought this was going to be a script that
uses ChatGPT to generate a random news post and post it
to Usenet...

Which would also have been kind of cool, as long as it wasn't
overused.

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


Re: Precision Tail-off?

2023-02-17 Thread Greg Ewing via Python-list

On 18/02/23 7:42 am, Richard Damon wrote:

On 2/17/23 5:27 AM, Stephen Tucker wrote:

None of the digits in RootNZZZ's string should be different from the
corresponding digits in RootN.


Only if the storage format was DECIMAL.


Note that using decimal wouldn't eliminate this particular problem,
since 1/3 isn't exactly representable in decimal either.

To avoid it you would need to use an algorithm that computes nth
roots directly rather than raising to the power 1/n.

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


Re: Introspecting the variable bound to a function argument

2023-02-22 Thread Greg Ewing via Python-list

On 23/02/23 9:12 am, Hen Hanna wrote:

On Wednesday, February 22, 2023 at 2:32:57 AM UTC-8, Anton Shepelev wrote:

 def f(a):
print(black_magic(a))# or black_magic('a')

 f(v1)# prints: v1
 f(v2)# prints: v2



the term  [call by name]  suggests  this should be possible.


But Python doesn't use call-by-name or anything remotely like it.

(Even if it did, the word "name" in that context doesn't mean
what it sounds like it means. The Algol docs used some words in
weird ways.)

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


Re: semi colonic

2023-02-22 Thread Greg Ewing via Python-list

On 23/02/23 1:58 pm, [email protected] wrote:


Would anything serious break if it was deprecated for use as a statement
terminator?


Well, it would break all the code of people who like to
write code that way. They might get a bit miffed if we
decide that their code is not serious. :-)

On the other hand, if they really want to, they will still
be able to abuse semicolons by doing this sort of thing:

a = 5; pass
b = 7; pass
c = a * b; pass

Then everyone will know it's some really serious code!

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


Re: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-22 Thread Greg Ewing via Python-list

On 23/02/23 9:37 am, Hen Hanna wrote:

 for the first  several weeks... whenever i used Python...  all 
i could think ofwas     this is really Lisp (inside) with  a thin 
veil of   Java/Pascal syntax..

-  that  everything is  first converted  (macro-expanded)  
into (intermediated) Lisp code, and then.


I once toyed with the idea of implementing a Python compiler
by translating it into Scheme and then feeding it to a Scheme
compiler.

But I quickly realised that, although Scheme and Python are
both dynamically-typed languages, Python is way *more* dynamic
than Scheme.

So without doing some very serious static analysis, the best
I could do would be just putting together calls to runtime
support routines that implement all the dynamic dispatching
that Python does for its operators, etc., and the result
wouldn't be much better than an interpreter.

There are some similarities between Python and Lisp-family
languages, but really Python is its own thing.

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


Re: semi colonic

2023-02-23 Thread Greg Ewing via Python-list

On 24/02/23 9:26 am, [email protected] wrote:

Python One-Liners: Write Concise, Eloquent Python Like a Professional 
Illustrated Edition
by Christian Mayer (Author)


I didn't know there were any Professional Illustrated Editions
writing Pythom. You learn something every day! :-)

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


Re: TypeError: can only concatenate str (not "int") to str

2023-02-25 Thread Greg Ewing via Python-list

On 26/02/23 10:53 am, Paul Rubin wrote:

I'm not on either list but the purpose of the tutor list is to shunt
beginner questions away from the main list.


There's a fundamental problem with tutor lists. They rely on
experienced people, the ones capable of answering the questions,
to go out of their way to read the tutor list -- something that
is not of personal benefit to them.

Also, pointing people towards tutor lists, if not done carefully,
can give the impression of saying "newcomers are not welcome here".
That's not a message we want to send to Python newcomers at all.

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


Re: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-27 Thread Greg Ewing via Python-list

On 27/02/23 10:07 pm, Roel Schroeven wrote:
I'm guessing you're thinking about variables leaking out of list 
comprehensions. I seem to remember (but I could be wrong) it was a 
design mistake rather than a bug in the code, but in any case it's been 
fixed now (in the 2 to 3 transition, I think).


The semantics of list comprehensions was originally defined
in terms of nested for loops. A consequence was that the loop
variables ended up in the local scope just as with ordinary for
loops. Later it was decided to change that.

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


Re: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

2023-02-27 Thread Greg Ewing via Python-list

On 28/02/23 7:40 am, [email protected] wrote:

inhahe  made the point that this may not have been the 
original intent for python and may be a sort of bug that it is too late to fix.


Guido has publically stated that it was a deliberate design choice.
The merits of that design choice can be debated, but it wasn't a
bug or an accident.

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


Re: Python 3.10 Fizzbuzz

2023-02-27 Thread Greg Ewing via Python-list

On 28/02/23 5:08 am, Thomas Passin wrote:

On 2/27/2023 11:01 AM, Mats Wichmann wrote:
If you intend to run Black on your code to ensure consistent 
formatting, you may as well learn to prefer double quotes, because 
it's going to convert single to double


I prefer single quotes because they are easier to type.


I tend to use the convention of double quotes for strings seen
by the outside world, and single quotes for internal constants
(such as enumerated types that happen to be represented by
strings).

I guess this means I can't use Black. :-(

--
Greg

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


Re: one Liner: Lisprint(x) --> (a, b, c) instead of ['a', 'b', 'c']

2023-02-27 Thread Greg Ewing via Python-list

On 28/02/23 4:24 pm, Hen Hanna wrote:

   is it poss. to peek at the Python-list's  messages 
without joining ?


It's mirrored to the comp.lang.python usenet group, or
you can read it through gmane with a news client.

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


Re: Python 3.10 Fizzbuzz

2023-03-01 Thread Greg Ewing via Python-list

On 2/03/23 10:59 am, gene heskett wrote:

Human skin always has the same color


Um... no?

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


Re: Which more Pythonic - self.__class__ or type(self)?

2023-03-02 Thread Greg Ewing via Python-list

On 3/03/23 9:54 am, Ian Pilcher wrote:

I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.


In such cases I'd probably go for type(x), because it looks less
ugly.

x.__class__ *might* be slightly more efficient, as it avoids a
global lookup and a function call. But as always, measurement
would be required to be sure.

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


Re: Which more Pythonic - self.__class__ or type(self)?

2023-03-03 Thread Greg Ewing via Python-list

On 4/03/23 7:51 am, [email protected] wrote:


I leave you with the question of the day. Was Voldemort pythonic?


Well, he was fluent in Parseltongue, which is not a good sign.

I hope not, otherwise we'll have to rename Python to "The Language
That Shall Not Be Named" and watch out for horcruxes during code
reviews.

I'll note that he was fluent in Parseltongue, which is not a good
sign.

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


Re: Fast full-text searching in Python (job for Whoosh?)

2023-03-04 Thread Greg Ewing via Python-list

On 5/03/23 5:12 pm, Dino wrote:
I can do a substring search in a list of 30k elements in less than 2ms 
with Python. Is my reasoning sound?


I just did a similar test with your actual data and got
about the same result. If that's fast enough for you,
then you don't need to do anything fancy.

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


Re: Bug 3.11.x behavioral, open file buffers not flushed til file closed.

2023-03-05 Thread Greg Ewing via Python-list

On 6/03/23 1:02 pm, Cameron Simpson wrote:
Also, fsync() need not expedite the data getting to disc. It is equally 
valid that it just blocks your programme _until_ the data have gone to 
disc.


Or until it *thinks* the data has gone to the disk. Some drives
do buffering of their own, which may impose additional delays
before the data actually gets written.

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


Re: Cutting slices

2023-03-05 Thread Greg Ewing via Python-list

On 6/03/23 11:43 am, Stefan Ram wrote:

   A user tries to chop of sections from a string,
   but does not use "split" because the separator might become
   more complicated so that a regular expression will be required
   to find it.


What's wrong with re.split() in that case?

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


Re: Fast full-text searching in Python (job for Whoosh?)

2023-03-06 Thread Greg Ewing via Python-list

On 7/03/23 4:35 am, Weatherby,Gerard wrote:

If mailing space is a consideration, we could all help by keeping our replies 
short and to the point.


Indeed. A thread or two of untrimmed quoted messages is probably
more data than Dino posted!

--
Greg

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


Re: Fast full-text searching in Python (job for Whoosh?)

2023-03-06 Thread Greg Ewing via Python-list

On 7/03/23 6:49 am, [email protected] wrote:

But the example given wanted to match something like "V6" in middle of the text 
and I do not see how that would work as you would now need to search 26 dictionaries 
completely.


It might even make things worse, as there is likely to be a lot of
overlap between entries containing "V" and entries containing "6",
so you end up searching the same data multiple times.

--
Greg

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


Re: Feature migration

2023-03-08 Thread Greg Ewing via Python-list

On 9/03/23 8:29 am, [email protected] wrote:

They seem to be partially copying from python a
feature that now appears everywhere but yet strive for some backwards
compatibility. They simplified the heck out of all kinds of expressions by
using INDENTATION.


It's possible this was at least parttly inspired by functional languages
such as Haskell. Haskell has always allowed indentation as one way of
expressing structure. Python wasn't the first language to use
indentation semantically.

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


Re: Baffled by readline module

2023-03-09 Thread Greg Ewing via Python-list

On 10/03/23 10:08 am, Grant Edwards wrote:

It finally dawned on me after seeing an example I found elsewhere that
you don't call some module method to fetch the next user-entered line.

You call the input() built-in.

Having a module modify the behavior of a built-in makes me cringe.


Importing the module is not modifying the built-in.

If your Python has been compiled with gnu readline support,
input() *already* provides recall and editing facilities.

You only need to import the readline module if you want to
change the configuration.

Yes, it would be helpful if the docs for the readline module
explained this. At present they seem to assume that you already
know what the readline module is for and just want a summary
of the API.

It *is* mentioned briefly in the docs for input(), but again
somebody wanting line editing functionality wouldn't necessarily
think of looking there.

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


Re: Baffled by readline module

2023-03-09 Thread Greg Ewing via Python-list

On 10/03/23 10:59 am, Cameron Simpson wrote:
I think this might be the common case of a module which wraps another 
library


It's not quite the same thing, though -- the library it wraps
is already hooked into things behind the scenes in ways that
may not be obvious. (Unless you're Dutch?)

--
Greg

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


Re: Baffled by readline module

2023-03-09 Thread Greg Ewing via Python-list

On 10/03/23 11:43 am, Chris Angelico wrote:

import readline
print("Pseudo-prompt: ", end="")
msg1 = input()
msg2 = input("Actual prompt: ")
print(repr(msg1))
print(repr(msg2))

At each of the prompts, type a bit of text, then backspace it all the
way. The actual prompt will remain, but the pseudo-prompt will get
cleared off. There'll be other small differences too.


Hmmm, so it seems that merely importing readline does change
things a little bit.

This is rather nasty. I'd go so far as to call it a bug in
gnu readline -- it shouldn't be erasing something that the
user didn't type in.

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


Re: Baffled by readline module

2023-03-09 Thread Greg Ewing via Python-list

On 10/03/23 12:43 pm, Grant Edwards wrote:

When a computer dies, I
generally just cp -a (or rsync -a) $HOME to a new one.


Same here, more or less. My current machine has multiple
archaeological layers going back about 5 generations of
technology...

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


Re: Baffled by readline module

2023-03-09 Thread Greg Ewing via Python-list

On 10/03/23 1:46 pm, Grant Edwards wrote:

That's not how it acts for me. I have to "import readline" to get
command line recall and editing.


Maybe this has changed? Or is platform dependent?

With Python 3.8 on MacOSX I can use up arrow with input()
to recall stuff I've typed before, without having to
import anything.

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


Re: Baffled by readline module

2023-03-09 Thread Greg Ewing via Python-list

On 10/03/23 2:57 pm, Chris Angelico wrote:

import sys; "readline" in sys.modules

Is it?


Yes, it is -- but only when using the repl!
If I put that in a script, I get False.

My current theory is that it gets pre-imported when using
Python interactively because the repl itself uses it.

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


Re: Baffled by readline module

2023-03-09 Thread Greg Ewing via Python-list

On 10/03/23 4:00 pm, [email protected] wrote:

My ~/.pythonrc contains the following:

 import readline
 import rlcompleter
 readline.parse_and_bind( 'tab: complete' )


I don't have a ~/.pythonrc, so that's not what's doing it
for me.

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


Re: We can call methods of parenet class without initliaze it?

2023-03-15 Thread Greg Ewing via Python-list

On 15/03/23 10:57 pm, scruel tao wrote:

How can I understand this? Will it be a problem?


I can't remember any details offhand, but I know I've occasionally
made use of the ability to do this. It's fine as long as the method
you're calling doesn't rely on anything you haven't initialised yet.

--
Greg


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


Re: How to get get_body() to work? (about email)

2023-03-19 Thread Greg Ewing via Python-list

On 20/03/23 7:07 am, Jon Ribbens wrote:

Ah, apparently it got removed in Python 3, which is a bit odd as the
last I heard it was added in Python 2.2 in order to achieve consistency
with other types.


As far as I remember, the file type came into existence
with type/class unification, and "open" became an alias
for the file type, so you could use open() and file()
interchangeably.

With the Unicode revolution in Python 3, file handling got
a lot more complicated. Rather than a single file type,
there are now a bunch of classes that handle low-level I/O,
encoding/decoding, etc, and open() is a function again
that builds the appropriate combination of underlying
objects.

--
Greg



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


Re: What kind of "thread safe" are deque's actually?

2023-03-28 Thread Greg Ewing via Python-list

On 28/03/23 2:25 pm, Travis Griggs wrote:

Interestingly the error also only started showing up when I switched from 
running a statistics.mean() on one of these, instead of what I had been using, 
a statistics.median(). Apparently the kind of iteration done in a mean, is more 
conflict prone than a median?


It may be a matter of whether the GIL is held or not. I had a look
at the source for deque, and it doesn't seem to explicitly do
anything about locking, it just relies on the GIL.

So maybe statistics.median() is implemented in C and statistics.mean()
in Python, or something like that?

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


Re: What kind of "thread safe" are deque's actually?

2023-03-29 Thread Greg Ewing via Python-list

On 30/03/23 6:13 am, Chris Angelico wrote:

I'm not sure what would happen in
a GIL-free world but most likely the lock on the input object would
still ensure thread safety.


In a GIL-free world, I would not expect deque to hold a lock
the entire time that something was iterating over it. That
would require holding the lock as long as an iterator object
existed referencing it, which could be a long time, even
longer than the caller expects (no reference counting,
remember!)

So for future-proofing I would recommend using deque's
copy() method to copy it before doing anything that iterates
over it. Hopefully that would be implemented in a thread-safe
way (although the docs don't currently promise that).

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


Re: How to add clickable url links to 3D Matplotlib chart ?

2023-03-29 Thread Greg Ewing via Python-list

On 30/03/23 8:39 am, a a wrote:

How to add clickable url links to the following 3D Matplotlib chart to make it 
knowledge representation 3D chart, make of 1,000+ open Tabs in Firefox ?


It seems that matplotlib can be made to generate SVG images with
hyperlinks in them:

https://matplotlib.org/stable/gallery/misc/hyperlinks_sgskip.html

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


Re: [Python-Dev] Small lament...

2023-04-04 Thread Greg Ewing via Python-list

On 4/04/23 2:09 pm, [email protected] wrote:

Sadly, between Daylight Savings time and a  newer irrational PI π Day, I am 
afraid some April Foolers got thrown off albeit some may shower us with 
nonsense  in May I.


Pi day isn't responsible, but it is because of changes to daylight
saving. The International Bureau of Weights and Measures announced
that, in order to gain a few more days of summer weather, all clocks
would be put back by 96 hours at midnight on 31 March 2023. So April 1
is now occurring on what would have been April 4. Expect the usual
torrent of silliness to arrive shortly.

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


Re: Weak Type Ability for Python

2023-04-13 Thread Greg Ewing via Python-list

On 14/04/23 4:55 am, [email protected] wrote:

While we are at it, why stop with imaginary numbers when you can imagine
extensions thereof? Unfortunately, it has been proven there are and can only
be two additional such constructs.


You can go beyond that if you broaden your horizons enough.
There are Clifford algebras, Lie algebras, ...

Not sure what any of those should do to strings, though. :-)

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


Re: Incomplete sys.path with embeddable python (Windows)!?

2023-04-21 Thread Greg Ewing via Python-list

How are you invoking your script? Presumably you have some code
in your embedding application that takes a script path and runs
it. Instead of putting the code to update sys.path into every
script, the embedding application could do it before running
the script.

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


Re: Incomplete sys.path with embeddable python (Windows)!?

2023-04-22 Thread Greg Ewing via Python-list

On 23/04/23 10:04 am, Ralf M. wrote:
I thought about that, but for that to work all local modules across all 
script locations must have unique names, otherwise import might get hold 
of a module from the wrong directory.


You could put all the local modules belonging to a particular
script into a package named after the script, e.g. put the local
modules used by foo.py into a package called foolib.

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


Re: How to 'ignore' an error in Python?

2023-04-29 Thread Greg Ewing via Python-list

On 30/04/23 2:43 am, jak wrote:

Maybe I expressed myself badly but I didn't mean to propose alternatives
to the EAFP way but just to evaluate the possibility that it is not a
folder.


If it's not a folder, you'll find out when the next thing you
try to do to it fails.

You could check for it earlier, but there's still the possibility
of a race condition -- someone could delete the folder and replace
it with a file in the meantime. Or just delete it and not replace
it with anything. So you need to be prepared to deal with failures
at any point.

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


Re: An "adapter", superset of an iterator

2023-05-03 Thread Greg Ewing via Python-list

On 4/05/23 9:29 am, Chris Angelico wrote:

So
you're asking for map to be able to return an iterator if given an
iterator, or an adapter if given an adapter. That makes it quite
complicated to use and reason about.


Also a bit slower, since it would need to inspect its argument
and decide what to do with it. Currently it can just get on
with its job and rely on duck typing to do the right thing.

Maybe there could be a parallel set of functions "enumerated",
"mapped", etc. that take sequences and return sequence views.

Although that naming convention would suggest that reversed()
itself should return a sequence view rather than an iterator.
That would require restricting it to working on sequences,
which would be an incompatible change.

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


Re: Addition of a .= operator

2023-05-20 Thread Greg Ewing via Python-list

On 21/05/23 5:54 am, Alex Jando wrote:


hash.=hexdigest()


That would be a very strange and unprecedented syntax that
munges together an attribute lookup and a call.

Keep in mind that a method call in Python is actually two
separate things:

y = x.m()

is equivalent to

f = x.m
y = f()

But it would not be possible to break down your .= syntax
in that way.

Another oddity is that these are equivalent:

x += y
x += (y)

i.e. the RHS is evaluated as usual before doing the +=.

But these would not be equivalent:

hash .= hexdigest()
hash .= (hexdigest())

In fact the latter would probably have to be disallowed, as it's
not at all clear what it should mean.

--
Greg

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


Re: Addition of a .= operator

2023-05-20 Thread Greg Ewing via Python-list

On 21/05/23 9:18 am, Richard Damon wrote:
This just can't happen (as far as I can figure) for .= unless the object 
is defining something weird for the inplace version of the operation, 


Indeed. There are clear use cases for overriding +=, but it's hard to
think of one for this. So it would just be syntactic sugar, which is
harder to justify.

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


Re: Does os.path relpath produce an incorrect relative path?

2023-05-25 Thread Greg Ewing via Python-list

On 25/05/23 7:49 pm, BlindAnagram wrote:
The first of these three results produces an incorrect relative path 
because relpath does not strip off any non-directory tails before 
comparing paths.


It has no way of knowing whether a pathname component is a directory
or not. It's purely an operation on strings, it doesn't look in the
file system.

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


Re: What to use instead of nntplib?

2023-05-30 Thread Greg Ewing via Python-list

On 31/05/23 8:44 am, aapost wrote:
Even if I did partake in the modern github style of code distribution, 
how many packages have issues where the "maintainers" inherited the 
package and really haven't dug deep enough in to the code to see how it 
really works. They have issues that sit around for YEARS, and when 
someone says "this sucks, this is broken and could be better", and the 
githubian response is typically a dismissive "Nothing is stopping you 
from making a PR".


Also, "nothing is stopping you from making a fork." Which is what
you would have to do in the face of inactive maintainers regardless
of where or how the project was hosted. This is not a github problem
or a big-corporation problem, it's a people problem.

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


Re: Why does IDLE use a subprocess?

2023-05-30 Thread Greg Ewing via Python-list

On 29/05/23 8:10 am, James Schaffler wrote:

However, some minimal testing of InteractiveInterpreter leads me to believe 
that the Interpreter object has its own view of local/global variables and 
therefore shouldn't be able to affect the calling interpreter


Globals you create by executing code in the REPL have their own
namespace. But everything else is shared -- builtins, imported
Python modules, imported C extension modules, etc. etc.

There's a long-running project to make it possible to have
multiple fully-isolated Python interpreters in one process, but
the way CPython is structured makes that very difficult to achieve,
and as far as I know it's not there yet.

In the case of IDLE, there's really no reason not to use a
subprocess[1]. It's easy and guarantees 100% isolation.

[1] Well, mostly. There used to be a small hitch on Windows with
the default firewall settings not letting you connect to a local
socket (nice one, Microsoft). I don't know whether that's still
an issue.

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


Re: Match statement with literal strings

2023-06-07 Thread Greg Ewing via Python-list

On 8/06/23 10:18 am, Jason Friedman wrote:

SyntaxError: name capture 'RANGE' makes remaining patterns unreachable


The bytecode compiler doesn't know that you intend RANGE
to be a constant -- it thinks it's a variable to bind a
value to.

To make this work you need to find a way to refer to the
value that isn't just a bare name. One way would be to
define your constants using an enum:

class Options(Enum):
   RANGE = "RANGE"
   MANDATORY = "MANDATORY"

match stuff:
   case Options.RANGE:
  ...
   case Options.MANDATORY:
  ...

--
Greg


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


Re: Should NoneType be iterable?

2023-06-19 Thread Greg Ewing via Python-list

I would question the wisdom of designing an API that
can return either a sequence or None. If it normally
returns a sequence, and there are no items to return,
it should return an empty sequence.

--
Greg

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


Re: Should NoneType be iterable?

2023-06-20 Thread Greg Ewing via Python-list

On 20/06/23 7:36 pm, Barry wrote:

I have some APIs that do return None or a list.
The None says that a list is not available and that the caller is
responsible with dealing in a application-domain specific with
with that situation.


In that case, the caller should probably be checking for
None rather than blindly trying to iterate over the result.

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


Re: Multiple inheritance and a broken super() chain

2023-07-04 Thread Greg Ewing via Python-list

On 5/07/23 10:33 am, Alan Gauld wrote:

(*) C++ is the odd one out because it doesn't have GC, but then
neither does it have an Object superclass so very often MI in C++
does not involve creating diamonds! And especially if the MI
style is mixin based.


Even if all your mixins have empty constructors, in C++ there
is still a diamond problem if they have any data members, because
you end up with multiple copies of them.

But C++ has the concept of virtual base classes, which avoids the
diamond problem, albeit at the expense of making you explicitly
call all the base class constructors in your ancestry.

--
Greg





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


Re: GNU gettext: Print string translated and untranslated at the same time

2023-08-17 Thread Greg Ewing via Python-list

On 17/08/23 7:10 pm, [email protected] wrote:

def foobar(translate):
     if not translate:
     # I try to mask the global _() builtins-function
     def _(txt):
     return txt

     return _('Hello')


This causes _ to become a local that is left undefined on one
branch of the if. You need to ensure it's always defined. Here's
one way that should work:

gttran = _

def foobar(translate):
def _(txt):
if translate:
return gttran(txt)
else:
return txt
return _('Hello')

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


Re: Getty fully qualified class name from class object

2023-08-22 Thread Greg Ewing via Python-list

On 23/08/23 2:45 am, Ian Pilcher wrote:

How can I programmatically get 'logging.Handler' from the class object?


Classes have a __module__ attribute:

>>> logging.Handler.__module__
'logging'

--
Greg


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


Re: []=[]

2023-09-22 Thread Greg Ewing via Python-list

On 23/09/23 4:51 am, Stefan Ram wrote:

[]=[]

   (Executes with no error.)


#
[]=[]
( 1 )
#\_/#

(Executes with no error.)

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


Re: error of opening Python

2023-09-26 Thread Greg Ewing via Python-list

On 27/09/23 3:30 pm, Chris Roy-Smith wrote:
surely running a 64 bit version of python in a 23mbit version of windows 
will cause significant problems!


23 millibits? I don't think you'd be able to run much at all
with that few bits! :-)

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


Re: Dynamically modifying "__setattr__"

2023-09-29 Thread Greg Ewing via Python-list

On 28/09/23 10:44 pm, Stefan Ram wrote:

class A:
 def __init__( self ):
 self.__setattr__ = self.setattr
 def setattr( self, key, value ):
 print( 'setattr called.' )

   Any idea how to achieve something like this?


class A:

def __init__(self):
self.x = 17
self.setattr = self.custom_setattr

def __setattr__(self, key, value):
self.setattr(key, value)

def setattr(self, key, value):
object.__setattr__(self, key, value)

def custom_setattr(self, key, value):
print('custom_setattr:', key, '=', value)

a = A()
a.x = 1
print('a.x =', a.x)

--
Greg

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


Re: type annotation vs working code

2023-10-03 Thread Greg Ewing via Python-list

On 4/10/23 5:25 pm, dn wrote:
The first question when dealing with the Singleton Pattern is what to do 
when more than one instantiation is attempted


My preferred way of handling singletons is not to expose the class
itself, but a function that creates an instance the first time it's
called, and returns that instance subsequently. The problem then
doesn't arise.

--
Greg

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


Re: Question(s)

2023-10-27 Thread Greg Ewing via Python-list

On 25/10/23 2:32 pm, Chris Angelico wrote:

Error correcting memory, redundant systems, and human
monitoring, plus the ability to rewrite the guidance software on the
fly if they needed to.


Although the latter couldn't actually be done with the AGC,
as the software was in ROM. They could poke values into RAM
to change its behaviour to some extent, and that got them out
of trouble a few times, but they couldn't patch the code.

It might have been possible with the Gemini computer, since
it loaded its code from tape. I don't know if it was ever
done, though.

--
Greg

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


Re: Checking if email is valid

2023-11-06 Thread Greg Ewing via Python-list

On 7/11/23 7:45 am, Mats Wichmann wrote:
Continuing with the example, if you have a single phone number field, or 
let a mobile number be entered in a field marked for landline, you will 
probably assume you can text to that number.


But if the site can detect that you've entered a mobile number into
the landline field or vice versa and reject it, then it can figure out
whether it can text to a given numner or not without you having
to tell it!

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


Re: Checking if email is valid

2023-11-06 Thread Greg Ewing via Python-list

On 6/11/23 6:34 pm, rbowman wrote:

We've found even if you directly ask the user often the answer is 'I
dunno' or some mythology they have constructed to explain the problem.


This seems to apply to hardware issues as well. Louis Rossmann has
a philosophy of "Never believe what the customer tells you."

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


Re: fCONV_AUSRICHTG is not defined - Why?

2023-11-07 Thread Greg Ewing via Python-list

On 8/11/23 8:10 am, MRAB wrote:

Something to do with how scoping is implemented in comprehensions?


Yes, together with the way class scopes work during class construction.

Behind the scenes, the body of a listcomp happens to be implemented
as a nested function.

Usually you don't notice this, but while a class is being built,
its scope doesn't count as an enlosing scope for functions defined
within the class.

This is necessary, otherwise all of a class's attributes would be 
visible inside its methods, which isn't what we want. However, it

leads to some odd corner cases, such as this one.

There are various ways you could work around this. I would suggest
moving the offending code outside the class and qualifying the
constants it uses with the class name.

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


Re: on a tail-recursive square-and-multiply

2023-11-07 Thread Greg Ewing via Python-list

On 8/11/23 2:26 pm, Julieta Shem wrote:

For the first time I'm trying to write a tail-recursive
square-and-multiply and, even though it /seems/ to work, I'm not happy
with what I wrote and I don't seem to understand it so well.


Stepping back a bit, why do you feel the need to write this
tail-recursively? Is it just an exercise?

Note that Python doesn't optimise tail calls, so anything that
can be done tail-recursively is probably better done iteratively.



--8<---cut here---start->8---
def sam(b, e, m, acc = 1):
   if e == 0:
 return acc
   if is_even(e):
 return sam(remainder(b * b, m), e//2, m, acc)
   else:
 return sam(b, e - 1, m, remainder(b * acc, m))
--8<---cut here---end--->8---

You see, I tried to use an accumulator, but I'm only accumulating when
the exponent is odd.  When it's even, I feel I'm forced to change the
base into b * b mod m and leave the accumulator alone.  This feels so
unnatural to me.  I feel I broke some symmetry there.  I'm having to
think of two cases --- when I change the accumulator and when I change
the base.  That seems too much for my small head.  Can you help?


Well, there are inherently two cases, and they're different, so
I don't think you're doing anything wrong here. It was asymmetrical
to begin with. If you were doing it iteratively you would also be
leaving the accumulator alone when the exponent is even.

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


Re: Context without manager

2023-11-26 Thread Greg Ewing via Python-list

On 27/11/23 9:03 am, Stefan Ram wrote:

   Above, "have" is followed by another verb in "have been",
   so it should be eligible for a contraction there!


Yes, "been" is the past participle of 'to be", so "I've been" is
fine.

--
Greg

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


Re: Context without manager

2023-11-26 Thread Greg Ewing via Python-list

On 27/11/23 5:03 pm, Grant Edwards wrote:

I should probably have written "how to fool that  into
working when he's not using a 'with' statement"


It should be possible to run the context protocol yourself.
Something like (warning, untested):

class MyDeviceWrapper:

def __init__(self):
self.cm = device_open()
self.device = self.cm.__enter__()

# Other methods here for doing things with
# self.device

def close(self):
self.cm.__exit__(None, None, None)

--
Greg

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


Re: Is it possible to inheret a metaclass.

2020-04-10 Thread Greg Ewing via Python-list

On 11/04/20 12:19 am, Pieter van Oostrum wrote:


Your Pardon is not a class, it is a function.


To elaborate on that a bit, the way inheritance of metaclasses
works is that when you define a class, if you don't explicity
specify a metaclass, it uses the class of the base class as
the metaclass.

Usually when you specify a metaclass, you use an actual class.
But your "metaclass" is a function that returns an instance
of class "type". So the class of your base class ends up
being "type", which is no different from what it would have
been if you didn't specify a metaclass.


I find it peculiar that you can give a function as metaclass.


Yes, it just calls whatever object you give it, which allows
for various fun things. You'd be risking a lynching if you took
advantage of it for anything important, though.:-)

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


Re: mypy question

2023-12-29 Thread Greg Ewing via Python-list

On 30/12/23 4:02 am, Karsten Hilbert wrote:


def run_rw_queries (
link_obj:_TLnkObj=None,
queries:list[dict[str, str | list | dict[str, Any]]]=None,



Given that I would have thought that passing in
list[dict[str, str]] for "queries" ought to be type safe.


dict[str, str] is not a subtype of dict[str, str | something_else]
because you can assign a value of type something_else to the latter
but not the former.

In this case it happens to be okay because the function is (presumably)
treating the dict passed in as immutable, but MyPy has no way to be sure
of that.

You could try declaring it as a collections.Mapping, which is immutable.

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


Re: Aw: Re: mypy question

2023-12-30 Thread Greg Ewing via Python-list

On 31/12/23 8:05 am, Chris Angelico wrote:

Ah, I think you've hit on the problem there. Consider this:

def add_item(stuff: dict[str: str | int]):
 stuff["spam"] = "ham"
 stuff["vooom"] = 1_000_000


Yep, that's it exactly. It's not the union itself that's the problem,
but the fact that there's a *mutable container* containing that type.

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


Re: mypy question

2023-12-30 Thread Greg Ewing via Python-list

On 31/12/23 10:06 am, Thomas Passin wrote:
my suggestion above does 
work, *except* that you cannot mix-and-match different DictTypex types


Have you tried declaring the argument as a Mapping instead of a dict?
Seeing as Thomas Passin's Sequence experiment worked, it seems like this
should work too.

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


Re: Extract lines from file, add to new files

2024-01-12 Thread Greg Ewing via Python-list

On 13/01/24 12:11 am, Left Right wrote:

 x = [...]
 for x[i] in x: print(i)


I suspect you've misremembered something, because this doesn't
do anything surprising for me:

>>> x = [1, 2, 3]
>>> for x[i] in x: print(i)
...
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'i' is not defined

There's no destructuring going on here, just assignment to a
sequence item.

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


Re: Extract lines from file, add to new files

2024-01-13 Thread Greg Ewing via Python-list

On 13/01/24 1:45 pm, Left Right wrote:


I use the term "destructuring" in the same way Hyperspec uses it.
It's not a Python term.  I don't know what you call the same thing in
Python.  I'm not sure what you understand from it.


I thought you meant what is usually called "unpacking" in Python. I
don't know anything about Hyperspec, so I don't know what it means
there.

The fact that i was being printed inside the loop made me think
that some deeper level of surprise was being intended, such as
the value of i somehow getting changed by the assignment.

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


Re: Extract lines from file, add to new files

2024-01-13 Thread Greg Ewing via Python-list

On 13/01/24 3:14 pm, Chris Angelico wrote:

On Sat, 13 Jan 2024 at 13:11, Left Right via Python-list
 wrote:


  Very few
languages allow arbitrary complex expressions in the same place they
allow variable introduction.


What do you mean by this? Most languages I've worked with allow
variables to be initialized with arbitrary expressions, and a lot of
languages allow narrowly-scoped variables.


I think he means that in some languages the for-loop target serves as
the declaration of a new variable, and as such has to be a bare name.

Python isn't like that -- the target of a for-statement is treated
exactly the same way as the lhs of an assignment. It's not scoped to the
loop.

BTW, the equivalent thing is valid in C too, so anyone familiar with C
is unlikely to be surprised by this either.

#include 

int x[10];
int i;

int main() {
  i = 5;
  for (x[i] = 0; x[i] < 10; x[i]++)
printf("%d\n", x[i]);
}

Output:

0
1
2
3
4
5
6
7
8
9

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


Re: Extract lines from file, add to new files

2024-01-14 Thread Greg Ewing via Python-list

On 13/01/24 11:34 pm, Left Right wrote:

To make this
shorter, Python allows:

for  in ... : ...



Um, no, it doesn't. An assignment target is not, on its own, a
statement.

It's hard to make sense of what you're saying. You seem to be
surprised by the fact that Python doesn't require variables to
be declared separately from their use. But this is a very common
feature of dynamic languages generally. As language oddities go,
it hardly rates a mention.

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


Re: Extract lines from file, add to new files

2024-01-14 Thread Greg Ewing via Python-list

On 15/01/24 1:28 am, Left Right wrote:

Python isn't a context-free language, so the grammar that is used to
describe it doesn't actually describe the language


Very few languages have a formal grammar that *fully* describes
the set of strings that constitute valid programs, including all
the rules about things having to be declared, types matching up,
etc. The only one I know of which attempted that is Algol 68,
and it seems to be regarded as a technical success but a practical
failure.


... so, it's a "pretend grammar" that ignores indentation.


Indentation isn't ignored, it appears in the grammar by means of
INDENT and DEDENT lexical tokens.

It's true that the meaning of these tokens is described informally
elsewhere, but that's true of all the lexical features.

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


Re: Extract lines from file, add to new files

2024-01-15 Thread Greg Ewing via Python-list

On 15/01/24 1:54 pm, dn wrote:
Soon after, Wirth simplified 
rather than expanded, and developed Pascal.


Before Pascal there was Algol-W, which Wirth invented as a rebellion
against how complicated Algol 68 was becoming.

When I first saw this I was 
stunned, then attracted to its simplicity, but then steered-away once 
realised that it needed 'more' to cope with 'the outside world'.


Pascal was intended as a teaching language, and as such it was lacking
in practicality in a few spots. But it didn't need much tweaking to
make it a very useful language. UCSD Pascal, Turbo Pascal, Delphi, etc.
enjoyed a lot of popularity. A variant of UCSD was the main language
for Macintosh application development for a number of years.

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


Re: Extract lines from file, add to new files

2024-01-15 Thread Greg Ewing via Python-list

On 15/01/24 9:07 pm, Chris Angelico wrote:

The grammar *can't* specify everything. If it did, it would have to
have rules for combining individual letters into a NAME and individual
characters into a string literal.


The lexical level of a grammar can be, and often is, described
formally using regular expressions.

Although some might consider that this doesn't contradict
your statement about readability. :-)

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


Re: Python 3.12.1, Windows 11: shebang line #!/usr/bin/env python3 doesn't work any more

2024-01-15 Thread Greg Ewing via Python-list

On 16/01/24 11:55 am, Mats Wichmann wrote:
Windows 
natively has something called python.exe and python3.exe which is 
interfering here


I'm wondering whether py.exe should be taught to recognise these stubs
and ignore them. This sounds like something that could trip a lot of
people up.

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


Re: Question about garbage collection

2024-01-16 Thread Greg Ewing via Python-list

On 17/01/24 4:00 am, Chris Angelico wrote:

class Form:
 def __init__(self):
 self.elements = []

class Element:
 def __init__(self, form):
 self.form = form
 form.elements.append(self)


If you make the reference from Element to Form a weak reference,
it won't keep the Form alive after it's been closed.

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


Re: Question about garbage collection

2024-01-16 Thread Greg Ewing via Python-list

On 17/01/24 1:01 am, Frank Millman wrote:
I sometimes need to keep a reference from a 
transient object to a more permanent structure in my app. To save myself 
the extra step of removing all these references when the transient 
object is deleted, I make them weak references.


I don't see how weak references help here at all. If the transient
object goes away, all references from it to the permanent objects also
go away.

A weak reference would only be of use if the reference went the other
way, i.e. from the permanent object to the transient object.

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


Re: Await expressions (Posting On Python-List Prohibited)

2024-01-26 Thread Greg Ewing via Python-list

On 27/01/24 10:46 am, Stefan Ram wrote:

   But your explanation seems to have no mention of the "something" /
   "the awaitable object" part following the preposition "on". Shouldn't
   this awaitable object play a rôle in the explanation of what happens?


If it helps at all, you can think of an async function as being
very similar to a generator, and "await" as being very similar to
"yield from". In the current implementation they're almost exactly
the same thing underneath.

--
Greg

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


Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought

2024-02-27 Thread Greg Ewing via Python-list

On 26/02/24 12:45 pm, Lawrence D'Oliveiro wrote:

def score(candidate, answer) :
 return \
 (
 sum(a == b for a, b in zip(candidate, answer)),
 sum
   (
 i != j and a == b
 for i, a in enumerate(candidate)
 for j, b in enumerate(answer)
   )
 )


This is not correct. score((1,1,1), (1,1,2)) gives (2,4). According to
the usual rules of Mastermind, it should be (2, 0).

--
Greg

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


Re: GIL-Removal Project Takes Another Step (Posting On Python-List Prohibited)

2024-03-20 Thread Greg Ewing via Python-list

On 20/03/24 4:14 pm, Lawrence D'Oliveiro wrote:

not to
mention the latency when there isn’t quite enough memory for an allocation
and you have to wait until the next GC run to proceed. Run the GC a
thousand times a second, and the latency is still 1 millisecond.


That's not the way it usually works. If you run out of memory, you
run a GC there and then. You don't have to wait for GCs to occur on
a time schedule.

Also, as a previous poster pointed out, GCs are typically scheduled
by number of allocations, not by time.

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


Re: xkcd.com/353 ( Flying with Python )

2024-03-30 Thread Greg Ewing via Python-list

On 30/03/24 7:21 pm, HenHanna wrote:

https://xkcd.com/1306/
  what does  SIGIL   mean?


I think its' a Perl term, referring to the $/@/# symbols in front of
identifiers.

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


Re: Use of statement 'global' in scripts.

2024-05-08 Thread Greg Ewing via Python-list

On 8/05/24 1:32 pm, Popov, Dmitry Yu wrote:

The statement 'global', indicating variables living in the global scope, is 
very suitable to be used in modules. I'm wondering whether in scripts, running 
at the top-level invocation of the interpreter, statement 'global' is used 
exactly the same way as in modules?


The 'global' statement declares a name to be module-level, so there's no
reason to use it at the top level of either a script or a module, since
everything there is module-level anyway.

You only need it if you want to assign to a module-level name from
within a function, e.g.

spam = 17

def f():
  global spam
  spam = 42

f()
# spam is now 42

A script is a module, so everything that applies to modules also
applies to scripts.

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


Re: Couldn't install numpy on Python 2.7

2024-06-12 Thread Greg Ewing via Python-list

On 13/06/24 4:31 am, [email protected] wrote:

It seems Microsoft is having a problem where something lik 2/3 of Windows
users have not upgraded from Windows 10 after many years


At least Python 3 is a clear improvement over Python 2 in many ways.
Whereas the only thing Microsoft seems to have done with Windows in
recent times is change it in ways that nobody wants, so there is
understandable resistance to upgrading even if it's possible.

On 13/06/24 10:09 am, Chris Angelico wrote:
> So if anyone
> actually does need to use pip with Python 2.7, they probably need to
> set up a local server

You should also be able to download a .tar.gz from PyPI and use pip
to install that. Although you'll have to track down the dependencies
yourself in that case.

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


Re: Crash when launching python

2024-09-04 Thread Greg Ewing via Python-list

On 5/09/24 7:48 am, Barry Scott wrote:

Beware that you cannot use print to stdout for a .app as its stdin/stdout do 
not go anywhere useful.


You can invoke the executable inside the package from the Terminal.

Normally it's in the .app/Contents/MacOS subdirectory. The name
varies, but there's usually just one executable file in there. Run that
from a shell and you should see anything written to stdout or stderr.

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


Re: psycopg2: proper positioning of .commit() within try: except: blocks

2024-09-07 Thread Greg Ewing via Python-list

On 8/09/24 9:20 am, Karsten Hilbert wrote:

try:
do something
except:
log something
finally:
.commit()

cadence is fairly Pythonic and elegant in that it ensures the
the .commit() will always be reached regardless of exceptions
being thrown or not and them being handled or not.


That seems wrong to me. I would have thought the commit should only
be attempted if everything went right.

What if there's a problem in your code that causes a non-SQL-related
exception when some but not all of the SQL statements in the
transaction bave been issued? The database doesn't know something
has gone wrong, so it will happily commit a partially-completed
transaction and possibly corrupt your data.

This is how I normally do things like this:

  try:
do something
.commit()
  except:
log something
.rollback()

Doing an explicit rollback ensures that the transaction is always
rolled back if it is interrupted for any reason.

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


Re: psycopg2: proper positioning of .commit() within try: except: blocks

2024-09-08 Thread Greg Ewing via Python-list

On 8/09/24 11:03 pm, Jon Ribbens wrote:

On 2024-09-08, Greg Ewing  wrote:

try:
  do something
  .commit()
except:
  log something
  .rollback()


What if there's an exception in your exception handler? I'd put the
rollback in the 'finally' handler, so it's always called.


Good point. Putting the rollback first would be safer/

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


Re: psycopg2: proper positioning of .commit() within try: except: blocks

2024-09-08 Thread Greg Ewing via Python-list

On 9/09/24 2:13 am, Karsten Hilbert wrote:

For what it's worth here's the current state of code:


That code doesn't inspire much confidence in me. It's far too
convoluted with too much micro-management of exceptions.

I would much prefer to have just *one* place where exceptions are
caught and logged.

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


Re: Help with Streaming and Chunk Processing for Large JSON Data (60 GB) from Kenna API

2024-10-01 Thread Greg Ewing via Python-list

On 2/10/24 10:03 am, Left Right wrote:

Consider also an interesting
consequence of SCSI not being able to have infinite words: this means,
besides other things that fsync() is nonsense! :) If you aren't
familiar with the concept: UNIX filesystem API suggests that it's
possible to destage arbitrary large file (or a chunk of file) to disk.
But SCSI is built of finite "words" and to describe an arbitrary large
file you'd need to list all the blocks that constitute the file!


I don't follow. What fsync() does is ensure that any data buffered
in the kernel relating to the file is sent to the storage device.
It can send as many blocks of data over SCSI as required to
achieve this. There's no requirement for it to be atomic at the
level of the interface between the kernel and the hardware.

Some devices do their own buffering in ways that are invisible to
the software, so fsync() can't guarantee that the data is actually
written to the storage medium. But that's a problem stemming from
the design of the hardware, not the design of the protocol for
communicating with the hardware.

> the only way to implement fsync() in compliance with the
> standard is to sync _everything_

Again I'm not sure what you mean here. It may be difficult for the
kernel to track down exactly what data is relevant to a particular file,
and so the kernel programmers take the easy way out and just implement
fsync() as sync(). But again that has nothing to do with the protocol.

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


Re: Help with Streaming and Chunk Processing for Large JSON Data (60 GB) from Kenna API

2024-10-01 Thread Greg Ewing via Python-list

On 2/10/24 12:26 pm, [email protected] wrote:

The real problem is how the JSON is set up. If you take umpteen data
structures and wrap them all in something like a list, then it may be a tad
hard to stream as you may not necessarily be examining the contents till the
list finishes gigabytes later.


Yes, if you want to process the items as they come in, you might
be better off sending a series of separate JSON strings, rather than
one JSON string containing a list.

Or, use a specialised JSON parser that processes each item of the
list as soon as it's finished parsing it, instead of collecting the
whole list first.

--
Greg

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


Re: FileNotFoundError thrown due to file name in file, rather than file itself

2024-11-12 Thread Greg Ewing via Python-list

On 13/11/24 8:10 am, Left Right wrote:

since logs are designed to grow indefinitely, the natural
response to this design property is log rotation.


I don't see how writing logs to stderr solves that problem in any way.
Whatever stderr is sent to still has a potentially unlimited amount
of data to deal with.


But, it's
impossible to reliably rotate a log file.  There's always a chance
that during the rotation some log entries will be written to the file
past the point of rotation, but prior to the point where the next logs
volume starts.


Not sure I follow you there. You seem to be thinking of a particular
way of rotating log files, where an external process tries to swap
the program's log file out from under it without its knowledge. That
could be vulnerable to race conditions. But if the program doing the
logging handles the rotation itself, there's no reason it has to
lose data.

--
Greg

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


Re: Help with Streaming and Chunk Processing for Large JSON Data (60 GB) from Kenna API

2024-10-01 Thread Greg Ewing via Python-list

On 1/10/24 8:34 am, Left Right wrote:

You probably forgot that it has to be _streaming_. Suppose you parse
the first digit: can you hand this information over to an external
function to process the parsed data? -- No! because you don't know the
magnitude yet.


By that definition of "streaming", no parser can ever be streaming,
because there will be some constructs that must be read in their
entirety before a suitably-structured piece of output can be
emitted.

The context of this discussion about integers is the claim that
they *could* be parsed incrementally if they were written little
endian instead of big endian, but the same argument applies either
way.

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


doRe: Help with Streaming and Chunk Processing for Large JSON Data (60 GB) from Kenna API

2024-10-03 Thread Greg Ewing via Python-list

On 3/10/24 11:48 am, Left Right wrote:

So, streaming parsers (eg. SAX) are written for a regular language
that approximates XML.


SAX doesn't parse a whole XML document, it parses small pieces of it
independently and passes them on. It's more like a lexical analyser than
a parser in that respect.

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


Re: Two aces up Python's sleeve

2024-11-07 Thread Greg Ewing via Python-list

On 8/11/24 3:04 am, Mild Shock wrote:

This only works for small integers. I guess
this is because tagged pointers are used
nowadays ?


No, it's because integers in a certain small range are cached. Not sure 
what the actual range is nowadays, it used to be something like -5 to 
256 I think.


BTW you have to be careful testing this, because the compiler sometimes 
does constant folding, so you need to be sure it's actually computing 
the numbers at run time.


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


Re: Specifying local dependency with Poetry

2024-11-05 Thread Greg Ewing via Python-list

On 6/11/24 4:13 am, Loris Bennett wrote:

[tool.poetry.dependencies]
python = "^3.6"
first-package = "^1.6.0"

   Could not find a version that satisfies the requirement 
first-package<2.0.0,>=1.6.0 (from second-package==0.5.0) (from versions: )
No matching distribution found for first-package<2.0.0,>=1.6.0 (from 
second-package==0.5.0)


What version number does first-package have?

The caret in the specification "^1.6.0" restricts it to 1.x.x versions.

If you don't want that restriction, use ">=" instead.

--
Greg

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


Re: super().__init__() and bytes

2024-12-03 Thread Greg Ewing via Python-list

On 4/12/24 3:24 am, Roel Schroeven wrote:
It's not entirely clear to me though how bytes.__new__ *can* set an 
object's value. Isn't __new__ also a regular function?


Yes, but the __new__ methods of the builtin immutable objects (int,
str, bytes, etc.) are implemented in C, and so are able to do things
that Python methods cannot.

--
Greg

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


Re: Pip installs to unexpected place

2025-04-17 Thread Greg Ewing via Python-list

On 18/04/25 9:41 am, Mats Wichmann wrote:

There's just not a really great answer to this.


Seems to me a system-installed application shouldn't be looking in the 
user's .local packages in the first place. That should only be for 
things the user has installed "for this user".


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


Re: backslash in triple quoted string

2025-05-11 Thread Greg Ewing via Python-list

On 12/05/25 3:21 am, Left Right wrote:

The point is that the error is wrong.
It cannot be a syntax error and at the same time the program compiles.


But the message doesn't say it's an error. It uses the word "warning", 
not "error". You're tilting at a straw horse here.


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


Re: backslash in triple quoted string

2025-05-12 Thread Greg Ewing via Python-list

On 13/05/25 6:28 am, Left Right wrote:

Read the associate release note.


I take it you're referring to this:


In a future Python version, SyntaxError will
eventually be raised, instead of SyntaxWarning. (Contributed by Victor
Stinner in gh-98401.)


That doesn't contradict what I said. Currently it's a warning. If and 
when it becomes an error, presumably the grammar documentation will be 
updated to reflect that. If it isn't, you'll have cause to complain, but 
not before.


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


Re: Dynamic classes

2025-05-19 Thread Greg Ewing via Python-list

On 20/05/25 4:33 am, Stefan Ram wrote:

   So, the reason you're getting that
   TypeError is your __init__ function isn't actually hooked up
   right when you build your class with "type". You got to set up
   your init before you call "type", and then drop it into the
   class dictionary as a /function/ (not as a string).


That's what he did, or at least that's what he tried to do.
It turns out the misplaced quote was the entire problem -- by a
fluke, it didn't result in a syntax error, and ended up putting
the __init__ function into the dict under the name
'Flag3: 4, __init__'.

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


Re: Python

2025-12-15 Thread Greg Ewing via Python-list
Any decent programming editor will have a facility for selecting a group 
of lines and adding/removing the appropriate commenting characters.


(If your editor doesn't have that, then by definition it's not decent. :-)
--
https://mail.python.org/mailman3//lists/python-list.python.org