Python Forum
Search Results
Post Author Forum Replies Views Posted [asc]
    Thread: why I can't use "try" without "expect"
Post: RE: why I can't use "try" without "expect"

You should avoid a bare except, because it will catch all exceptions, which could lead into errors you won't able to find. If you don't know, which exception when occurs, test it in the REPL. import...
DeaD_EyE General Coding Help 4 286 May-17-2024, 12:25 PM
    Thread: which way would be better?
Post: RE: which way would be better?

Maybe it's worth to reuse existing code ©. import stat from pathlib import Path def filemode(file: str | Path) -> str: """ Returns the first character from stat.filemode() call. ...
DeaD_EyE General Coding Help 3 332 May-11-2024, 10:33 PM
    Thread: strange result in xor
Post: RE: strange result in xor

a = 80 b = 10 c = a ^ b # print it as binary representation with 8 bit print(f"{c:08b}") # print it as binary representation with 16 bit print(f"{c:016b}")The f before the quotes means format-stri...
DeaD_EyE General Coding Help 6 366 May-07-2024, 07:04 PM
    Thread: Equivalent Python code from VBA
Post: RE: Equivalent Python code from VBA

import subprocess from subprocess import CREATE_NO_WINDOW # use better names # use forward slahes to avoid complications with backslashes program = "C:/Program Files (x86)/SPACE GASS 14/sgwin.exe" s...
DeaD_EyE General Coding Help 4 1,075 May-02-2024, 10:17 PM
    Thread: using mutable in function defintion as optional paramter
Post: RE: using mutable in function defintion as optiona...

(Apr-27-2024, 04:57 PM)Pedroski55 Wrote: Is there a good reason why we should not pass the dictionary we want modified by name? It's not expected that a function mutates objects from the arguments. ...
DeaD_EyE General Coding Help 8 602 Apr-27-2024, 07:22 PM
    Thread: if else statements
Post: RE: if else statements

If you want to compare a user-input of equality, but case-insensitive, then lower the user-input and compare it only with the lower variant of the string. ... user_input = input("Do your choice: ") ...
DeaD_EyE General Coding Help 8 4,093 Apr-25-2024, 07:27 AM
    Thread: match case
Post: RE: match case

If you just want to check for types, then do not forget to use parenthesis. Wrong: obj = 42 match obj: case str: print(obj, "is a string") case int: print(obj, "is an int")Er...
DeaD_EyE Code sharing 1 180 Apr-23-2024, 09:53 AM
    Thread: Loop through all files in a directory?
Post: RE: Loop through all files in a directory?

TypeHints are not required, but they help the developers of libraries to communicate what a method/function is expecting and what it should return. Quote:What I don't like with the pathlib solution i...
DeaD_EyE General Coding Help 10 678 Apr-23-2024, 09:14 AM
    Thread: Loop through all files in a directory?
Post: RE: Loop through all files in a directory?

Use Path from pathlib module. This is the modern way to handle paths. Mostly all methods, which are functions in os and os.path for path handling, are attached to the Path object. Some are missing, b...
DeaD_EyE General Coding Help 10 678 Apr-22-2024, 07:17 PM
    Thread: A function that checks if the list is sorted
Post: RE: A function that checks if the list is sorted

The useful function itertools.pairwise was added to Python 3.10. https://docs.python.org/3/library/iterto...s.pairwise This isn't Pythonic: range(len(something)) from itertools import pairwise fro...
DeaD_EyE Homework 16 17,178 Apr-22-2024, 09:14 AM
    Thread: yield from
Post: RE: yield from

# removed the * in front of iterables print("Definition of generator-function") def join(iterables, joiner): print("First step of generator") # spliting iterable in first iterable and the re...
DeaD_EyE General Coding Help 4 435 Apr-19-2024, 07:58 PM
    Thread: Waiting for input from serial port, then move on
Post: RE: Waiting for input from serial port, then move ...

(Apr-17-2024, 06:45 AM)feistycavities Wrote: The issue with your while loop not closing is because you have an embedded for loop in your code. The cause was the condition of while. In the code there...
DeaD_EyE General Coding Help 3 1,293 Apr-17-2024, 07:21 AM
    Thread: python calculate float plus float is incorrect?
Post: RE: python calculate float plus float is incorrect...

I don't know what PHP does to prevent the visible inaccuracy. In modern programming languages, you don't see the real floating point value inclusive PHP. An algorithm is used, to show the user a nic...
DeaD_EyE General Coding Help 6 466 Apr-16-2024, 01:45 PM
    Thread: Need to get around SSL: CERTIFICATE_VERIFY_FAILED
Post: RE: Need to get around SSL: CERTIFICATE_VERIFY_FAI...

I do not have this problem. I was able to connect to api.polygon.io:443 on Windows 11 with Python 3.12.2 x64. btw. Python 3.12.3 is out import socket import ssl addr = ("api.polygon.io", 443) ctx ...
DeaD_EyE General Coding Help 3 380 Apr-16-2024, 07:50 AM
    Thread: Hide CMD call window
Post: RE: Hide CMD call window

windows ... import subprocess def arp(): """ Run arp -a and return the stripped output. Window creation is prevented. """ flags = subprocess.CREATE_NO_WINDOW return sub...
DeaD_EyE General Coding Help 8 495 Apr-15-2024, 08:56 PM
    Thread: How do I parse the string?
Post: RE: How do I parse the string?

If you need a path, a Path object is maybe what you want. Code is based on Gribouillis example: from urllib.parse import urlparse from pathlib import PurePosixPath def url2path(url: str) -> Pure...
DeaD_EyE General Coding Help 4 458 Apr-10-2024, 10:26 AM
    Thread: Next/Prev file without loading all filenames
Post: RE: Next/Prev file without loading all filenames

Here as a class with type hints. mypy does not complain :-) This loads the whole directory content for the first time, and then only if the sort_function has been changed. The __init__ method makes th...
DeaD_EyE General Coding Help 9 791 Apr-08-2024, 07:58 AM
    Thread: a better way to code this to fix a URL?
Post: RE: a better way to code this to fix a URL?

If the url should always start with https even, if only a hostname is given, you could use this: from urllib.parse import urlsplit, urlunsplit def conv2(url): return urlunsplit(("https", *urlsp...
DeaD_EyE News and Discussions 10 1,065 Mar-19-2024, 09:15 AM
    Thread: list.sort() returning None
Post: RE: list.sort() returning None

(Mar-18-2024, 10:39 AM)Pedroski55 Wrote: **smile** **smile** **smile** Dead_EyE: a very accurate shooter (and reader?) Quote:You can also use the list.sort() method. It modifies the list in-pla...
DeaD_EyE General Coding Help 8 742 Mar-18-2024, 08:16 PM
    Thread: Anyway to stop a thread and continue it?
Post: RE: Anyway to stop a thread and continue it?

Example with a queue: import time from random import randint from queue import Queue from threading import Thread def fake_feed(): """ Generator function which yields endless values fro...
DeaD_EyE General Coding Help 2 424 Mar-18-2024, 10:53 AM

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020