Re: Invalid literal for int() with base 10?
"Kevin M. Wilson" writes: > Ok, I'm not finding any info. on the int() for converting a str to an > int (that specifies a base parameter)?! https://docs.python.org/3/library/functions.html#int > The picture is of the code I've written... I don't see a picture. The mailing list probably does not accept attachments. (You don't need a picture anyway.) > And the base 10 paradigm involved?? The int() constructor takes a base parameter whose default value is 10. If you specify base=0, it will accept binary, octal, and hexadecimal numbers in addition to decimal. All this is explained in the link I gave you. > years = int('y') # store for calculationValueError: invalid > literal for int() with base 10: 'y'What is meant by "invalid literal"? '42' is a valid literal for int(). 'y' is not. What value did you expect int('y') to give you? Perhaps you have a variable named 'y' containing a string? If so, you might want something like int(y) or int(f{'y'}), but int('y') passes the literal string 'y', which has nothing to do with a variable of that name. > I'm trying to convert srt to int, Do you mean "str to int"? > and I didn't know I needed to specify the base. You don't. If you don't specify the base, it defaults to 10. > Plus I haven't read anything that I need to specify > the base for the int(). Attached is the code, showing the code and > the execution of said code. Any attachment was removed. > "When you pass through the waters, I will > be with you: and when you pass through the rivers, they will not sweep > over you. When you walk through the fire, you will not be burned: the > flames will not set you ablaze." Isaiah 43:2 You can add a signature to all your messages if you like, but it will be very helpful if you introduce it with a line consisting of "-- ", as I've done here. It would also be very helpful if you introduce line breaks into your message, particularly before and after any included code. The formatting made your message difficult to read. -- Keith Thompson (The_Other_Keith) keith.s.thompso...@gmail.com Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */ -- https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10?
On 2023-05-25, Kevin M. Wilson via Python-list wrote: > Ok, I'm not finding any info. on the int() for converting a str to > an int (that specifies a base parameter)?! Where are you looking? https://docs.python.org/3/library/functions.html#int > The picture is of the code I've written... And the base 10 paradigm > involved?? I've no clue what that sentence means. > years = int('y') # store for calculationValueError: > invalid literal for int() with base 10: 'y'What is meant by "invalid > literal"? It means that the string 'y' isn't an integer literal. The strings '123' and '-4' are integer literals. https://docs.python.org/3/reference/expressions.html?highlight=integer%20literal#literals > I'm trying to convert srt to int, and I didn't know I needed to > specify the base. You don't need to unless you want a base other than 10. > Plus I haven't read anything that I need to specify the base for the int(). Don't know what you mean there. > Attached is the code, showing the code and the execution of said > code. Sorry, I don't see attachments. Include code in posts. > "When you pass through the waters, I will be with you: and > when you pass through the rivers, they will not sweep > over you. When you walk through the fire, you will not be burned: > the flames will not set you ablaze." Isaiah 43:2 Huh? -- https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10?
On 2023-05-25 22:30, Kevin M. Wilson via Python-list wrote: Ok, I'm not finding any info. on the int() for converting a str to an int (that specifies a base parameter)?! The picture is of the code I've written... And the base 10 paradigm involved?? years = int('y') # store for calculationValueError: invalid literal for int() with base 10: 'y'What is meant by "invalid literal"? I'm trying to convert srt to int, and I didn't know I needed to specify the base. Plus I haven't read anything that I need to specify the base for the int(). '12' is a string that contains 2 digits, which together represent the number 12. 'y' is a string that contains a letter, which doesn't represent a number. Perhaps what you meant is that y is a variable that contains a string, in which case what you want is int(y). Attached is the code, showing the code and the execution of said code. There's no code attached; this list automatically strips attachmentments. -- https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10?
On Fri, 26 May 2023 at 10:26, Kevin M. Wilson via Python-list wrote: > > Ok, I'm not finding any info. on the int() for converting a str to an int > (that specifies a base parameter)?! The picture is of the code I've > written... And the base 10 paradigm involved?? years = int('y') # store for > calculation ValueError: invalid literal for int() with base 10: 'y' > Imagine giving this to a human. "How many years did you say?" "Oh, y years." Is that a reasonable way to say a number of years? No. It's an invalid way of specifying a number of years. Python is a little more technical in the way it describes it, but the fact is unchanged. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10?
Ok, I'm not finding any info. on the int() for converting a str to an int (that specifies a base parameter)?! The picture is of the code I've written... And the base 10 paradigm involved?? years = int('y') # store for calculation ValueError: invalid literal for int() with base 10: 'y' What is meant by "invalid literal"? I'm trying to convert str to int, and I didn't know I needed to specify the base. Plus I haven't read anything that I need to specify the base for the int(). Attached is the code, showing the code and the execution of said code. Sorry, got pissed and didn't check all the content I sent! "When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze." Isaiah 43:2 On Thursday, May 25, 2023 at 05:55:06 PM MDT, Kevin M. Wilson via Python-list wrote: Ok, I'm not finding any info. on the int() for converting a str to an int (that specifies a base parameter)?! The picture is of the code I've written... And the base 10 paradigm involved?? years = int('y') # store for calculationValueError: invalid literal for int() with base 10: 'y'What is meant by "invalid literal"? I'm trying to convert srt to int, and I didn't know I needed to specify the base. Plus I haven't read anything that I need to specify the base for the int(). Attached is the code, showing the code and the execution of said code. "When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze." Isaiah 43:2 -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: From geeksforgeeks.org, on converting the string created by the input() to an INT
On Fri, 26 May 2023 at 09:58, Kevin M. Wilson via Python-list wrote: > > So, why can't a string of an integer be converted to an integer, via > print(int(str('23.5')))??? 23.5 is not an integer, so "23.5" is not the string of an integer. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
From geeksforgeeks.org, on converting the string created by the input() to an INT
We can first convert the string representation of float into float using float() function and then convert it into an integer using int().So, why can't a string of an integer be converted to an integer, via print(int(str('23.5')))??? Perplexed | print(int(float('23.5'))) | "When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze." Isaiah 43:2 -- https://mail.python.org/mailman/listinfo/python-list
Invalid literal for int() with base 10?
Ok, I'm not finding any info. on the int() for converting a str to an int (that specifies a base parameter)?! The picture is of the code I've written... And the base 10 paradigm involved?? years = int('y') # store for calculationValueError: invalid literal for int() with base 10: 'y'What is meant by "invalid literal"? I'm trying to convert srt to int, and I didn't know I needed to specify the base. Plus I haven't read anything that I need to specify the base for the int(). Attached is the code, showing the code and the execution of said code. "When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze." Isaiah 43:2 -- https://mail.python.org/mailman/listinfo/python-list
Re: Does os.path relpath produce an incorrect relative path?
On 2023-05-25 16:53, Eryk Sun wrote: On 5/25/23, BlindAnagram wrote: vcx_path = 'C:\\build.vs22\\lib\\lib.vcxproj' src_path = 'C:\\lib\\src\\' rel_path = '..\\..\\..\\lib\\src' [snip] The first of these three results produces an incorrect relative path because relpath does not strip off any non-directory tails before comparing paths. The start path is assumed to be a directory, which defaults to the current working directory, and the input paths are first resolved as absolute paths. In order to reach src_path from vcx_path, one has to traverse 3 levels up to the root directory. https://docs.python.org/3/library/os.path.html#os.path.relpath Well, it's not necessarily the optimal relative path, because it's not always necessary to go all the way up to the root, as in the first example. -- https://mail.python.org/mailman/listinfo/python-list
Re: Does os.path relpath produce an incorrect relative path?
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: Does os.path relpath produce an incorrect relative path?
On 5/25/23, BlindAnagram wrote: > > vcx_path = 'C:\\build.vs22\\lib\\lib.vcxproj' > src_path = 'C:\\lib\\src\\' > rel_path = '..\\..\\..\\lib\\src' > > [snip] > > The first of these three results produces an incorrect relative path > because relpath does not strip off any non-directory tails before > comparing paths. The start path is assumed to be a directory, which defaults to the current working directory, and the input paths are first resolved as absolute paths. In order to reach src_path from vcx_path, one has to traverse 3 levels up to the root directory. https://docs.python.org/3/library/os.path.html#os.path.relpath -- https://mail.python.org/mailman/listinfo/python-list
Does os.path relpath produce an incorrect relative path?
I am wondering whether I have misunderstood the semantics of os.path relpath or whether it has a bug. Here is a short test program: --- from os.path import relpath, split src_path = 'C:\\lib\\src\\' vcx_path = 'C:\\build.vs22\\lib\\lib.vcxproj' rel_path = relpath(src_path, vcx_path) print(f"{vcx_path = }\n{src_path = }\n{rel_path = }\n") vcx_path = 'C:\\build.vs22\\lib\\' rel_path = relpath(src_path, vcx_path) print(f"{vcx_path = }\n{src_path = }\n{rel_path = }\n") vcx_path = 'C:\\build.vs22\\lib\\lib.vcxproj' rel_path = relpath(src_path, split(vcx_path)[0]) print(f"{vcx_path = }\n{src_path = }\n{rel_path = }\n") --- and its output with Python 3.11: --- vcx_path = 'C:\\build.vs22\\lib\\lib.vcxproj' src_path = 'C:\\lib\\src\\' rel_path = '..\\..\\..\\lib\\src' vcx_path = 'C:\\build.vs22\\lib\\' src_path = 'C:\\lib\\src\\' rel_path = '..\\..\\lib\\src' vcx_path = 'C:\\build.vs22\\lib\\lib.vcxproj' src_path = 'C:\\lib\\src\\' rel_path = '..\\..\\lib\\src' --- The first of these three results produces an incorrect relative path because relpath does not strip off any non-directory tails before comparing paths. Is this a bug or my misunderstanding of relpath semantics? Comments would be appreciated as I don't waste developer time if this is not a bug. regards Brian -- https://mail.python.org/mailman/listinfo/python-list
Re: [RELEASE] Python 3.12.0 beta 1 released.
On 25/05/2023 12:23, Robin Becker wrote: On 22/05/2023 22:04, Thomas Wouters wrote: > I'm pleased to announce the release of Python 3.12 beta 1 (and feature > freeze for Python 3.12). > ... I see a major difference between 3.12.0a7 and 3.12.0b1 Basically in preppy an importer is defined to handle imports of '.prep' files. This worked as expected in the a7 version and fails in the b1. I put in some prints in the code and I see these calls for the a7 run> $ ~/LOCAL/3.12.0a7/bin/python3 test_import.py > sys.meta_path.insert(.PreppyImporter object at 0x7fa870b84080>) --> . I think this might be caused by the removal of the find_module method of importlib.abc.MetaPathFinder. So I guess I have to implement a modernised importer. Apologies for noise if that is the case. -- Robin Becker -- https://mail.python.org/mailman/listinfo/python-list
Re: [RELEASE] Python 3.12.0 beta 1 released.
On 22/05/2023 22:04, Thomas Wouters wrote: > I'm pleased to announce the release of Python 3.12 beta 1 (and feature > freeze for Python 3.12). > ... I see a major difference between 3.12.0a7 and 3.12.0b1 Basically in preppy an importer is defined to handle imports of '.prep' files. This worked as expected in the a7 version and fails in the b1. I put in some prints in the code and I see these calls for the a7 run> $ ~/LOCAL/3.12.0a7/bin/python3 test_import.py > sys.meta_path.insert(.PreppyImporter object at 0x7fa870b84080>) --> [.PreppyImporter object at 0x7fa870b84080>, <_distutils_hack.DistutilsMetaFinder object at 0x7fa871290fb0>, , , '_frozen_importlib_external.PathFinder'>] > PreppyImporter.find_module('sample001',None) > PreppyImporter.load_module('sample001') > 4 > . > -- > Ran 1 test in 0.004s > > OK In 3.12.0b1 although the importer is inserted into sys.meta_path the find_module/load_module methods are never called. and the import fails. So is this an expected change in the way importers behave or a bug? > $ ~/LOCAL/3.12.0b1/bin/python3 test_import.py > sys.meta_path.insert(.PreppyImporter object at 0x7fc866ecb110>) --> [.PreppyImporter object at 0x7fc866ecb110>, , , ] > E > == > ERROR: testImport1 (__main__.ImportTestCase.testImport1) > -- > Traceback (most recent call last): > File "/home/robin/devel/reportlab/REPOS/preppy/tmp/test_import.py", line 13, in testImport1 > import sample001 > ModuleNotFoundError: No module named 'sample001' > > -- > Ran 1 test in 0.001s > > FAILED (errors=1) .. > Your release team, > Thomas Wouters > Ned Deily > Steve Dower -- https://mail.python.org/mailman/listinfo/python-list
Re: getting rid of the recursion in __getattribute__
On 24/05/2023 15:37, A KR wrote: It is perfectly explained in the standards here [1] saying that: In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, object.__getattribute__(self, name). Therefore, I wrote a code following what the standard says: class Sample(): def __init__(self): self.a = -10 def __getattribute__(self, name): if name == 'a': return object.__getattribute__(self, name) raise AttributeError() s = Sample() result = s.a print(result) I did not fall into recursion, and the output was -10 While this works it's not how I understand the recommended pattern. I'd rather treat "special" attributes first and then use the __getattribute__ method of the base class as a fallback: >> class Demo: def __getattribute__(self, name): if name == "answer": return 42 return super().__getattribute__(name) That way your special arguments, >>> d = Demo() >>> d.answer 42 missing arguments >>> d.whatever Traceback (most recent call last): File "", line 1, in d.whatever File "", line 5, in __getattribute__ return super().__getattribute__(name) AttributeError: 'Demo' object has no attribute 'whatever' and "normal" arguments are treated as expected >>> d.question = "What's up?" >>> d.question "What's up?" Eventual "special" arguments in the superclass would also remain accessible. However, when I try the code without deriving from a class: class AnyClassNoRelation: pass class Sample(): def __init__(self): self.a = -10 def __getattribute__(self, name): if name == 'a': return AnyClassNoRelation.__getattribute__(self, name) raise AttributeError() s = Sample() result = s.a print(result) and calling __getattribute__ via any class (in this example class AnyClassNoRelation) instead of object.__getattribute__(self, name) as the standard says call using the base class, I get the same output: no recursion and -10. So my question: How come this is possible (having the same output without using the base class's __getattribute__? Although the standards clearly states that __getattribute__ should be called from the base class. AnyClassNoRelation does not override __getattribute__, so >>> AnyClassNoRelation.__getattribute__ is object.__getattribute__ True There is no sanity check whether a method that you call explicitly is actually in an object's inheritance tree, >>> class NoRelation: def __getattribute__(self, name): return name.upper() >>> class Demo: def __getattribute__(self, name): return "<{}>".format(NoRelation.__getattribute__(self, name)) >>> Demo().some_arg '' but the only purpose I can imagine of actually calling "someone else's" method is to confuse the reader... In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, object.__getattribute__(self, name). Literally, I can call __getattribute__ with anyclass (except Sample cause it will be infinite recursion) I define and it works just fine. Could you explain me why that happens? -- https://mail.python.org/mailman/listinfo/python-list