Python Forum
Inconsistency in Python programming language?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inconsistency in Python programming language?
#31
It is because your test is incorrect, the expected output should be -4.5. Nevertheless, here is a new version because I discovered that generate_token() is only kept for backwards compatibility but it is undocumented. Here is the version using the official tokenize() function
import ast
import re
from tokenize import tokenize, NUMBER, ENCODING

__version__ = '2019.10.6'

def val(s):
    t = re.sub(r'\s+', '', s).encode()
    sign = True
    for tok in tokenize(iter([t + b'\n', b'']).__next__):
        if tok[0] == ENCODING:
            continue
        if sign and tok[1] in ('-', '+'):
            sign = False
            continue
        if tok[0] == NUMBER:
            return ast.literal_eval(t[:tok[3][1]].decode())
        return 0
    return 0
    
if __name__ == '__main__':
    sample = [
        ("45", 45),
        ("++++++45", 0),
        ("-+54", 0),
        ("4.5", 4.5),
        ("4.5ABC", 4.5),
        ("ABCD", 0),
        ("AB45", 0),
        ("   2  45  7  ", 2457),
        ("    2   4   .   5   7   ", 24.57),
        ("-  12 e   - 7", -12e-7),
        ("+4.5", 4.5),
        ("-4.5", -4.5),
        ]
    
    
    for inp, out in sample:
        print(inp, out, val(inp))
        assert out == val(inp)
Reply
#32
(Oct-06-2019, 01:09 PM)Gribouillis Wrote: It is because your test is incorrect, the expected output should be -4.5

My profuse apologies for overlooking the required minus sign on right side of sample pair.

Your latest function val() turns out to just perfect, satisfying all our test inputs.

Great Job! I would like to convey my thanks & sincere compliments.

Note:
The new sample string "- 12 e - 7" was tested on VBA too, where the Val() function returns -0.0000012 (which is equivalent to -1.2e-06 returned in our case).
A.D.Tejpal
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Programming robots using Python OscarBoots 5 3,459 Oct-31-2021, 09:38 AM
Last Post: Larz60+
  logo language code into python aayushi98 2 67,132 Jan-26-2021, 09:02 PM
Last Post: Serafim
  Programming Difficult math in Python Huntern 6 4,765 Oct-17-2019, 06:32 AM
Last Post: Huntern
  Terms describing Python Programming language leodavinci1990 3 2,729 Aug-12-2019, 02:48 PM
Last Post: leodavinci1990
  Please help a newbie choose which programming language to learn. yeto 2 3,501 Feb-25-2019, 12:56 AM
Last Post: yeto
  Python Programming Projects for Beginners jack_sparrow007 3 3,329 Dec-26-2018, 07:52 PM
Last Post: micseydel
  How to get image from WolframAlpha by using Python language manhnt 1 2,694 Oct-27-2018, 02:07 PM
Last Post: Larz60+
  Programming Python as a MS Windows app? Brian123 8 4,247 Oct-17-2018, 10:26 PM
Last Post: Brian123
  Help with Python programming mediaos 5 3,755 Aug-08-2018, 01:02 PM
Last Post: Larz60+
  How to make the python default language be 3.6 instead of 2.7 sylas 4 6,834 Jul-06-2018, 06:11 AM
Last Post: sylas

Forum Jump:

User Panel Messages

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