on the popularity of loops while and for
Stestagg
stestagg at gmail.com
Sat Aug 28 18:05:12 EDT 2021
If you're doing this analysis, I'd be pretty interested in how many of
those while loops where 'while True:'
I'd wager 75%. But may be completely off
Steve
On Sat, 28 Aug 2021 at 23:03, Hope Rouselle <hrouselle at jevedi.com> wrote:
> 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.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list