Sample

pyint-e5081fcab142-I13

Lower every eligible synchronous for loop to a while loop

Matched original source

Lines71

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Dorn0.6010–1; higher is more readable
Mi0.5350–1; higher is more readable
LLOC33Logical lines; fewer is more readable
DeepSeek V4 Pro14.330–20; mean of three runs
GPT-6.1 Sol13.330–20; mean of three runs
BTTF0.6390–1; higher is more readable

Views

Source code

from __future__ import annotations import abc import contextlib import enum import itertools import linecache import sys import types import unicodedata import weakref from collections.abc import Callable, Mapping from functools import cached_property from typing import Any, NamedTuple, TypeVar from . import _compat, _config, setters from ._compat import ( PY_3_11_PLUS, PY_3_13_PLUS, _AnnotationExtractor, _get_annotations, _lazy_is_generator, get_generic_base, ) from .exceptions import ( DefaultAlreadySetError, FrozenInstanceError, NotAnAttrsClassError, UnannotatedAttributeError, ) class Factory: """ Stores a factory callable. If passed as the default value to `attrs.field`, the factory is used to generate a new value. Args: factory (typing.Callable): A callable that takes either none or exactly one mandatory positional argument depending on *takes_self*. takes_self (bool): Pass the partially initialized instance that is being initialized as a positional argument. .. versionadded:: 17.1.0 *takes_self* """ __slots__ = ("factory", "takes_self") def __init__(self, factory, takes_self=False): self.factory = factory self.takes_self = takes_self def __getstate__(self): """ Play nice with pickle. """ return tuple(getattr(self, name) for name in self.__slots__) def __setstate__(self, state): """ Play nice with pickle. """ loopIterator = __import__("builtins").iter(zip(self.__slots__, state, strict=True)) while True: try: name, value = __import__("builtins").next(loopIterator) except __import__("builtins").StopIteration: break setattr(self, name, value)

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.1285.971−42.437
Source linesThe number of non-empty source lines.71+28.4
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.4.489−6.733

Intercept 8.87 + feature contributions = -11.9 (log-odds).
Score = sigmoid(-11.9) = 0.

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.72−1.513
Indentation frequencyHow rapidly indentation changes from line to line.51+1.027
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.51−0.749
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.90.986+0.664
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.70.826+0.58
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.044+0.518
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.90.917−0.186

Intercept 0.07 + feature contributions = 0.411 (log-odds).
Score = sigmoid(0.411) = 0.601.

Score0.535
Score33
Run 1

The code has good structure with clear class definition, docstrings, and __slots__ usage. However, it drops significantly in readability due to the __setstate__ method, which uses awkward dynamic imports of builtins functions (iter, next, StopIteration) instead of direct Python syntax — making the intent obscure. The while True/try/except pattern is unnecessarily verbose and deeply nested compared to a simple for loop with zip. This violates action granularity and reading flow principles. The __getstate__ method is clean and concise, but the overall clarity is diminished by the inconsistent style in __setstate__. The imports at the top are excessive for the shown code but are likely needed for the full module. The docstrings are helpful, though the Factory class's purpose is only partially clear from this excerpt.

Run 2

The code has clear structure with logical imports, docstrings, and a clean class definition. The Factory class is readable with clear intent and good use of __slots__. However, the __setstate__ method is unnecessarily convoluted—using a while loop with try/except and dynamically importing builtins breaks reading flow and action granularity. This could be replaced with a simple for loop using zip(). The code length is reasonable but the pickle methods add unnecessary complexity. Nesting is minimal, which is good. Clarity of intent is strong for the class purpose but obscured in __setstate__. Overall, the code is readable but marred by the awkward implementation choice.

Run 3

The code is generally well-structured with clear imports and a well-documented Factory class. The class has a clear purpose, flat organization, and mostly one action per line, contributing to good readability. However, the __setstate__ method significantly reduces readability: it uses a manual iterator loop with __import__('builtins') to access iter, next, and StopIteration, which is unnecessarily convoluted and breaks the natural flow. A simple for loop with zip would be much clearer. The docstrings are helpful but the method name `loopIterator` violates Python naming conventions (camelCase). While the overall structure is logical and the code is concise, the unidiomatic and convoluted __setstate__ implementation undermines clarity. The rest of the code shows good practice with proper imports, type hints in docstrings, and clear separation of concerns.

Run 1

Code Structure: The class is logically organized, with initialization and serialization methods clearly separated, although many imports are unused in this snippet. Nesting: Most code is flat; the while/try/except structure in __setstate__ adds avoidable complexity. Clarity of intent: The class docstring and descriptive attributes explain its purpose, but dynamic builtins imports and the generic loopIterator name obscure a simple iteration. Code length: Initialization and __getstate__ are concise; __setstate__ is unnecessarily long compared with a direct for loop. Action granularity: Statements generally perform one action per line, though repeated dynamic imports make individual expressions harder to parse. Reading flow: The class reads naturally until __setstate__, where manual iterator advancement and exception handling interrupt an otherwise straightforward flow.

Run 2

Code Structure: The class is logically organized with initialization and serialization methods, but numerous unrelated imports distract from the small implementation. Nesting: Most methods are flat; __setstate__ introduces unnecessary while/try/except nesting. Clarity of intent: The class docstring clearly explains its purpose, and attribute names are descriptive, but dynamic builtins imports and the camelCase loopIterator obscure a straightforward iteration. Code length: Documentation is useful, while __setstate__ is unnecessarily long compared with a direct for loop. Action granularity: Statements generally perform one action per line, although repeated dynamic imports complicate expressions. Reading flow: Initialization and state extraction read naturally, but manual iterator handling interrupts the flow and makes restoration harder to scan.

Run 3

Code Structure: The class is logically organized into initialization and serialization methods, but numerous unused imports clutter this snippet. Nesting: Most code is flat; the while/try/except structure in __setstate__ adds unnecessary nesting. Clarity of intent: The class docstring clearly explains its purpose, and method names are conventional, but repeated dynamic builtins imports and the nonstandard loopIterator name obscure a straightforward operation. Code length: Documentation is useful, but __setstate__ is unnecessarily long compared with a direct for loop. Action granularity: Statements generally perform one action per line, although dynamic imports embedded in expressions add visual complexity. Reading flow: Initialization and __getstate__ read naturally; __setstate__ interrupts the flow with manual iterator management and exception handling.