on the popularity of loops while and for
Hope Rouselle
hrouselle at jevedi.com
Sat Aug 28 17:15:02 EDT 2021
ram at zedat.fu-berlin.de (Stefan Ram) writes:
> Hope Rouselle <hrouselle at jevedi.com> writes:
>>Have you guys ever measured something like that in a casual or serious
>
> import ast
> import pathlib
> rootname=r'''<ABSOLUTE PATH TO A DIRECTORY WITH SOURCE CODE>'''
> rootpath=pathlib.Path(rootname)
> rootiterable=rootpath.glob('**/*.py')
> first = True
> WhileCount = 0
> ForCount = 0
> for filepath in rootiterable:
> try:
> with filepath.open(encoding="utf-8") as file:
> source=file.read()
> parse=ast.parse(source, filename=str(file))
> for entry in ast.walk(parse):
> if isinstance(entry, ast.While):
> WhileCount+=1
> print( f"{ForCount=}, {WhileCount=}" )
> elif isinstance(entry, ast.For):
> ForCount+=1
> print( f"{ForCount=}, {WhileCount=}" )
> except SyntaxError as inst:
> if first:
> print( f"{sys.exc_info()[ 0 ] =}" )
> print( f"{type( inst ) =}" )
> print( f"{inst.args =}" )
> print( f"{inst =}" )
> print( f"skipping {filepath}." )
> first=False
You are so wonderful! Thanks quite a lot. Here's what I got:
ForCount=18703, WhileCount=2505
I have pretty much just the standard libraries and a couple more ---
sympy and some xlsxwriter library.
More information about the Python-list
mailing list