iterations destroy reversed() results

2023-09-03 Thread Pierre Fortin via Python-list
Hi,

reversed() results are fine until iterated over, after which the
results are no longer available. This was discovered after using
something like this:

rev = reversed( sorted( list ) ) 
sr = sum( 1 for _ in rev )
# rev is now destroyed

So reversed() results can only be iterated once unlike sorted(), etc...

Script to illustrate the issue:
/tmp/rev:
orig = [ 'x', 'a', 'y', 'b', 'z', 'c' ]
co = sum( 1 for _ in orig )
print( 'orig', orig, co )
# reversing
rev = reversed(orig)
print( 'before iteration:', [ x for x in rev ] )
# list comprehension was an iteration over 'rev'
print( 'after iteration:', [ x for x in rev ] )
# how this was discovered...
orig = [ 'x', 'a', 'y', 'b', 'z', 'c' ]
rev = reversed(orig)
cr = sum( 1 for _ in rev )
print( 'after sum():', [ x for x in rev ] )

which produces:

$ python /tmp/rev
orig ['x', 'a', 'y', 'b', 'z', 'c'] 6
before iteration: ['c', 'z', 'b', 'y', 'a', 'x']
after iteration: []
after sum(): []

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


Re: Newline (NuBe Question)

2023-11-15 Thread Pierre Fortin via Python-list
On Wed, 15 Nov 2023 16:51:09 - Grizzy Adams via Python-list wrote:

I don't give solutions; just a nudge...  you appear not to fully grok
"list"; your list is ONE list with no delineation between students. You
want a "list of lists"...

>['Example High', 'Mary', 89.6, 'Pass', 'Example High', 'Matthew', 76.5, 
>'Fail', 'Example High', 'Marie', 80.4, 'Fail', 'Example High', 'Manuel', 79.6, 
>'Fail', 'Example High', 'Malala', 98.9, 'Pass']

Like this:

students = [
['Example High', 'Mary', 89.6, 'Pass'],
['Example High','Matthew', 76.5, 'Fail'],
['Example High', 'Marie', 80.4, 'Fail'],
['Example High', 'Manuel', 79.6, 'Fail'],
['Example High', 'Malala', 98.9, 'Pass']
]

This may help get you headed in the right direction:

for s in students:
print( s )

Hint: look forward to learning about f-strings...

HTH,
Pierre
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to Add ANSI Color to User Response

2024-04-10 Thread Pierre Fortin via Python-list
On Thu, 11 Apr 2024 04:50:49 +1000 WordWeaver Evangelist via Python-list
wrote:

>Hello List,
>
>I have a simple question. I use the following textPrompt in some of my Jython 
>modules:
> '\nYour choice is? (A B C D E): ', maxChars=1, autoAccept=False, 
> forceUppercase=True)
>Is there a way to add an ANSI color code to the end where the conditions are, 
>so that the color of the user’s input is of a color of my choosing, instead of 
>just white?
>Thank you very much in advance.
>Kind regards,
>Bill Kochman

Over the years, I've tried different mechanisms for applying colors until
I got my hands on f-stings; then I created a tiny module with all the
colors (cR, cG, etc) which made my life so much simpler (attached). The
module includes background colors (bX); but I very rarely use those.

Then, I just use the module like this:

# place the module in a directory where your script is
# e.g., $ mkdir mymods (rename as desired) 
from mymods.colors import *  
# or just include the contents inline

# this simply switches from one color to the next
print( f"{cR}red, {cB}blue, {cG}green {cO}are colors." )

# color just the response
ans = input( f"Answer?: {cG}" ) # turn off color on next line
print( f"{cO}You entered: {cY}{ans}{cO}" )
# 

# to turn off each color (white commas), change the above to:
print( f"{cR}red{cO}, {cB}blue{cO}, {cG}green {cO}are colors." )

On Windows, you'll need to add this *before* using the colors:
import os
if os.name == 'nt': # Only if we are running on Windows
from ctypes import windll
w = windll.kernel32
# enable ANSI VT100 colors on Windows.
w.SetConsoleMode(w.GetStdHandle(-11), 7)

HTH,
Pierre
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to Add ANSI Color to User Response

2024-04-13 Thread Pierre Fortin via Python-list
On Thu, 11 Apr 2024 05:00:32 +0200 Gisle Vanem via Python-list wrote:

>Pierre Fortin wrote:
>
>> Over the years, I've tried different mechanisms for applying colors until
>> I got my hands on f-stings; then I created a tiny module with all the
>> colors (cR, cG, etc) which made my life so much simpler (attached).  
>
>Attachments are stripped off in this list.
>It would be nice to see this tiny module of yours.
>An URL or attach as inline text please.

#!/bin/python
# -*- mode: python; -*-
# Copyright:
#2024-Present, Pierre Fortin 
# License:
#GPLv3 or any later version: https://www.gnu.org/licenses/gpl-3.0.en.html
# Created:
#2023-11-10 Initial script
# Updated: 

# Usage:  f"{cR}red text {cG}green text{cO}; colors off"
#or:  print( cY, "yellow text", cO )

# VT100 type terminal colors
ESC = "\u001b";
# Foreground Colors
_black = f"{ESC}[30m"; _red = f"{ESC}[31m"; _green = f"{ESC}[32m"; _yellow = 
f"{ESC}[33m"
_blue = f"{ESC}[34m"; _magenta = f"{ESC}[35m"; _cyan = f"{ESC}[36m"; _white = 
f"{ESC}[37m"
# Background Colors
_black_ = f"{ESC}[40m"; _red_ = f"{ESC}[41m"; _green_ = f"{ESC}[42m"; _yellow_ 
= f"{ESC}[43m"
_blue_ = f"{ESC}[44m"; _magenta_ = f"{ESC}[45m"; _cyan_ = f"{ESC}[46m"; _white_ 
= f"{ESC}[47m"

_off = f"{ESC}[0m"
ANSIEraseLine = '\033[2K\033[1G'
EL = ANSIEraseLine # short alias

# Color abbreviations (shortcuts for f-sting use)
cK=_black; cR=_red; cG=_green; cY=_yellow; cB=_blue; cM=_magenta; cC=_cyan; 
cW=_white; cO=_off
# background colors; use {cO} to turn off any color
bK=_black_; bR=_red_; bG=_green_; bY=_yellow_; bB=_blue_; bM=_magenta_; 
bC=_cyan_; bW=_white_
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a better way? [combining f-string, thousands separator, right align]

2024-08-25 Thread Pierre Fortin via Python-list
On Sun, 25 Aug 2024 15:12:20 GMT Gilmeh Serda via Python-list wrote:

>Subject explains it, or ask.
>
>This is a bloody mess:
>
 s = "123456789" # arrives as str
 f"{f'{int(s):,}': >20}"  
>' 123,456,789'
>
Oops.. forgot comma

f"{int(s):>20,}"
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a better way? [combining f-string, thousands separator, right align]

2024-08-25 Thread Pierre Fortin via Python-list
On Sun, 25 Aug 2024 15:12:20 GMT Gilmeh Serda via Python-list wrote:

>Subject explains it, or ask.
>
>This is a bloody mess:
>
 s = "123456789" # arrives as str
 f"{f'{int(s):,}': >20}"  
>' 123,456,789'
>

f"{s:>20}"
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A switch somewhere, or bug?

2025-12-06 Thread Pierre Fortin via Python-list
On Thu, 4 Dec 2025 11:06:21 -0500 Em wrote:

>Two computers, both alike in dignity, in fair windows land...
>
>in Win10/Python 3.13 my program runs as well as can be expected. However, on
>my new computer Win11/Python 3.14.1 there is a peculiarity.
>
>On the new machine, when I click on the file name, it immediately dumps out.
>I go in with IDLE and it runs fine with no indication of any error.
>
>I believe this line is the culprit:
>Starter = open(HLYlog.txt", "w")
>
>Thoughts appreciated.

I asked claude.ai...

Question:  any reason this python statement works on windows10; but fails
on windows11 when called from cmd; yet works on both from IDLE:
open("file.txt","w")

This is an interesting permission/behavior difference between Windows
versions and execution contexts. Here are the most likely culprits: 

1. Controlled Folder Access (Windows Defender)

Windows 11 has more aggressive "Controlled Folder Access" settings by
default. When running from cmd.exe, Python might not be in the allowed
apps list, blocking write access to protected folders (like Documents,
Desktop, etc.).

Check: Windows Security → Virus & threat protection → Ransomware
protection → Controlled folder access 

2. Working Directory Differences

From cmd, Python's working directory is wherever cmd is currently located
(could be C:\Windows\System32 or your user folder). IDLE typically
defaults to a different location.

Try in cmd:

python -c "import os; print(os.getcwd())"

If it's trying to write to System32 or another protected location, that
would explain the failure. 

3. UAC/Permissions Changes

Windows 11 has stricter UAC enforcement. The cmd session might be running
with different privileges than IDLE.

Test: Right-click cmd → "Run as administrator" and try again.

4. Antivirus/Real-time Protection

Some antivirus software treats cmd-spawned processes differently than GUI
apps like IDLE.

What's the actual error message? That would narrow this down immediately:

PermissionError: [Errno 13] → Permissions/Controlled Folder Access
FileNotFoundError → Working directory issue
Other errors → Different root cause

Quick diagnostic: Try open("C:\\Temp\\file.txt", "w") from cmd (assuming
C:\Temp exists) to see if it's directory-specific.

-- 
https://mail.python.org/mailman3//lists/python-list.python.org