Python Forum
does try really need to be a keyword?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
does try really need to be a keyword?
#11
skaperen Wrote:i could, instead, argue, that ":" isn't needed, in general (and can be optional for enhancing readability for humans)
I proved that "." is not needed in python by implementing a program that adds the dots automatically. If you also remove the ":" and the fact that "try" is a keyword, it may stop to work. I'd much prefer a python without "." than a python without ":".
Reply
#12
FYI i'm working on the design of a data format using indentation. it is a potentially new data format for recording genealogical data.

how do you omit the "." in your trial branch of Python? just leave white space? if i can make a trial branch without ":" then maybe i can make one without both.

i'd like to see the program that adds the dots automatically. are you giving it away?

i'd like to leave out the dots in COBOL.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
skaperen Wrote:i'd like to see the program that adds the dots automatically. are you giving it away?
I don't publish the whole module yet but I can give you the heart of the algorithm that adds dots to a python source code. You need to install module 'untokenize' from pypi first.

With this function, the dots become optional in the source code. The function adds only the missing dots.

I supplemented that with a code that watches directories with the help of the inotify module and transforms file in real time as one edits them. I don't publish this part know.

# module dotless.adddot

from keyword import iskeyword 
from tokenize import NAME ,OP ,STRING ,COMMENT ,NL 
from tokenize import generate_tokens
# uses module untokenize from pypi
# https://pypi.org/project/untokenize/
import untokenize
TokenInfo = type(list(generate_tokens(lambda: ''))[0])

def dot_augmented(itoken):
    # originally inspired by the author's quail.texmacs.pythonFluide module
    """Add missing 'dot' operator tokens in a sequence of python tokens
    """
    insert_dot = False
    end_lin, end_col = 0, 0
    for tok in itoken:
        if tok.type == NAME:
            if iskeyword(tok.string):
                insert_dot = False 
            elif insert_dot:
                lin, col = tok.start
                start = lin, col - 1 if end_lin == lin else col
                yield TokenInfo(OP , ".", start, tok.start, tok.line)
            else:
                insert_dot = True 
        elif tok.type == STRING or (
            tok.type == OP and tok.string in ")]}"):
            insert_dot = True
        elif tok.type not in (COMMENT ,NL ):
            insert_dot = False
        yield tok
        end_lin, end_col = tok.end

def adddot(infile):
    """Add dot operators to python code.
    
    take a file object, return a string.
    """
    itok = generate_tokens(infile.readline)
    itok = dot_augmented(itok)
#    itok = list(itok)
#    for t in itok:
#        print(t)
    return untokenize.untokenize(itok)
Reply
#14
when it processes code does it care about indents or just pass them along as it gets them?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#15
It doesn't need to care about indents. I mean the 'untokenize' module cares about them but adddot doesn't. Here is an example (taken from pymotw 3)
import io
# this is PYTHON CODE WITHOUT DOTS
code = r'''
def show_tree(tree, total_width=36, fill=' '):
    """Pretty-print a tree."""
    output = StringIO()
    last_row = -1
    for i, n in enumerate(tree):
        if i:
            row = int(math floor(math log(i + 1, 2)))
        else:
            row = 0
        if row != last_row:
            output write('\n')
        columns = 2 ** row
        col_width = int(math floor(total_width / columns))
        output write(str(n) center(col_width, fill))
        last_row = row
    print(output getvalue())
    print('-' * total_width)
    print()
'''

result = adddot(io.StringIO(code))
print(result)
Output:
def show_tree(tree, total_width=36, fill=' '): """Pretty-print a tree.""" output = StringIO() last_row = -1 for i, n in enumerate(tree): if i: row = int(math.floor(math.log(i + 1, 2))) else: row = 0 if row != last_row: output.write('\n') columns = 2 ** row col_width = int(math.floor(total_width / columns)) output.write(str(n).center(col_width, fill)) last_row = row print(output.getvalue()) print('-' * total_width) print()
As you can see, it added the dots at their proper places without modifying the indentation.

I should modernize the code one day or the other because generate_tokens() is deprecated in favor of tokenize() which takes bytes instead of text... A minor change.
Reply


Forum Jump:

User Panel Messages

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