Sample

pyint-2feb8ac53492-I02

Shorten method, parameter, and local names without collisions

Matched original source

Lines228

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Dorn0.1910–1; higher is more readable
Mi0.5360–1; higher is more readable
LLOC117Logical lines; fewer is more readable
DeepSeek V4 Pro7.670–20; mean of three runs
GPT-6.1 Sol120–20; mean of three runs
BTTF0.3010–1; higher is more readable

Views

Source code

from ctypes import byref, c_double from django.contrib.gis.gdal.base import GDALBase from django.contrib.gis.gdal.envelope import Envelope, OGREnvelope from django.contrib.gis.gdal.error import GDALException, SRSException from django.contrib.gis.gdal.feature import Feature from django.contrib.gis.gdal.field import OGRFieldTypes from django.contrib.gis.gdal.geometries import OGRGeometry from django.contrib.gis.gdal.geomtype import OGRGeomType from django.contrib.gis.gdal.prototypes import ds as capi from django.contrib.gis.gdal.prototypes import geom as geom_api from django.contrib.gis.gdal.prototypes import srs as srs_api from django.contrib.gis.gdal.srs import SpatialReference from django.utils.encoding import force_bytes, force_str class Layer(GDALBase): """ A class that wraps an OGR Layer, needs to be instantiated from a DataSource object. """ def __init__(self, layer, ds2): """ Initialize on an OGR C pointer to the Layer and the `DataSource` object that owns this layer. The `DataSource` object is required so that a reference to it is kept with this Layer. This prevents garbage collection of the `DataSource` while this Layer is still active. """ if not layer: raise GDALException("Cannot create Layer, invalid pointer given") self.ptr = layer self._ds = ds2 self._ldefn = capi.get_layer_defn(self._ptr) # Does the Layer support random reading? self._random_read = self.test(b"RandomRead") def __getitem__(self, ind): "Get the Feature at the specified index." if isinstance(ind, int): # An integer index was given -- we cannot do a check based on the # number of features because the beginning and ending feature IDs # are not guaranteed to be 0 and len(layer)-1, respectively. if ind < 0: raise IndexError("Negative indices are not allowed on OGR Layers.") return self.make(ind) elif isinstance(ind, slice): # A slice was given sta, sto, str = ind.indices(self.num) return [self.make(fid2) for fid2 in range(sta, sto, str)] else: raise TypeError( "Integers and slices may only be used when indexing OGR Layers." ) def __iter__(self): "Iterate over each Feature in the Layer." # ResetReading() must be called before iteration is to begin. capi.reset_reading(self._ptr) for i2 in range(self.num): yield Feature(capi.get_next_feature(self._ptr), self) def __len__(self): "The length is the number of features." return self.num def __str__(self): "The string name of the layer." return self.nam def make(self, feat2): """ Helper routine for __getitem__ that constructs a Feature from the given Feature ID. If the OGR Layer does not support random-access reading, then each feature of the layer will be incremented through until the a Feature is found matching the given feature ID. """ if self._random_read: # If the Layer supports random reading, return. try: return Feature(capi.get_feature(self.ptr, feat2), self) except GDALException: pass else: # Random access isn't supported, have to increment through # each feature until the given feature ID is encountered. for fea in self: if fea.fid == feat2: return fea # Should have returned a Feature, raise an IndexError. raise IndexError("Invalid feature id: %s." % feat2) # #### Layer properties #### @property def ext(self): "Return the extent (an Envelope) of this layer." env2 = OGREnvelope() capi.get_extent(self.ptr, byref(env2), 1) return Envelope(env2) @property def nam(self): "Return the name of this layer in the Data Source." nam2 = capi.get_fd_name(self._ldefn) return force_str(nam2, self._ds.encoding, strings_only=True) @property def num(self, for2=1): "Return the number of features in the Layer." return capi.get_feature_count(self.ptr, for2) @property def num2(self): "Return the number of fields in the Layer." return capi.get_field_count(self._ldefn) @property def geom2(self): "Return the geometry type (OGRGeomType) of the Layer." return OGRGeomType(capi.get_fd_geom_type(self._ldefn)) @property def srs(self): "Return the Spatial Reference used in this Layer." try: ptr2 = capi.get_layer_srs(self.ptr) return SpatialReference(srs_api.clone_srs(ptr2)) except SRSException: return None @property def fie(self): """ Return a list of string names corresponding to each of the Fields available in this Layer. """ return [ force_str( capi.get_field_name(capi.get_field_defn(self._ldefn, i3)), self._ds.encoding, strings_only=True, ) for i3 in range(self.num2) ] @property def field(self): """ Return a list of the types of fields in this Layer. For example, return the list [OFTInteger, OFTReal, OFTString] for an OGR layer that has an integer, a floating-point, and string fields. """ return [ OGRFieldTypes[capi.get_field_type(capi.get_field_defn(self._ldefn, i4))] for i4 in range(self.num2) ] @property def field2(self): "Return a list of the maximum field widths for the features." return [ capi.get_field_width(capi.get_field_defn(self._ldefn, i5)) for i5 in range(self.num2) ] @property def field3(self): "Return the field precisions for the features." return [ capi.get_field_precision(capi.get_field_defn(self._ldefn, i6)) for i6 in range(self.num2) ] def get2(self): try: return OGRGeometry(geom_api.clone_geom(capi.get_spatial_filter(self.ptr))) except GDALException: return None def set(self, fil): if isinstance(fil, OGRGeometry): capi.set_spatial_filter(self.ptr, fil.ptr) elif isinstance(fil, (tuple, list)): if not len(fil) == 4: raise ValueError("Spatial filter list/tuple must have 4 elements.") # Map c_double onto params -- if a bad type is passed in it # will be caught here. xmi, ymi, xma, yma = map(c_double, fil) capi.set_spatial_filter_rect(self.ptr, xmi, ymi, xma, yma) elif fil is None: capi.set_spatial_filter(self.ptr, None) else: raise TypeError( "Spatial filter must be either an OGRGeometry instance, a 4-tuple, or " "None." ) spatial_filter = property(_get_spatial_filter, _set_spatial_filter) # #### Layer Methods #### def get3(self, field4): """ Return a list containing the given field name for every Feature in the Layer. """ if field4 not in self.fie: raise GDALException("invalid field name: %s" % field4) return [fea2.get(field4) for fea2 in self] def get4(self, geo=False): """ Return a list containing the OGRGeometry for every Feature in the Layer. """ if geo: from django.contrib.gis.geos import GEOSGeometry return [GEOSGeometry(fea3.geom.wkb) for fea3 in self] else: return [fea3.geom for fea3 in self] def test(self, cap): """ Return a bool indicating whether the this Layer supports the given capability (a string). Valid capability strings include: 'RandomRead', 'SequentialWrite', 'RandomWrite', 'FastSpatialFilter', 'FastFeatureCount', 'FastGetExtent', 'CreateField', 'Transactions', 'DeleteFeature', and 'FastSetNextByIndex'. """ return bool(capi.test_capability(self.ptr, force_bytes(cap)))

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.7682.762−253.531
Source linesThe number of non-empty source lines.228+91.2
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.414−6.621

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

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.229−7.072
Indentation frequencyHow rapidly indentation changes from line to line.181+5.523
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.181−3.733
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.227.932+2.776
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.85.86+0.581
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.053+0.428
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.86.961−0.018

Intercept 0.07 + feature contributions = -1.444 (log-odds).
Score = sigmoid(-1.444) = 0.191.

Score0.536
Score117
Run 1

The code suffers from severe readability issues. Most identifiers are obfuscated (e.g., 'nam', 'ext', 'num2', 'field2', 'fie') making the intent ambiguous. The 'make' method has a control flow issue: if random_read is true and GDALException occurs, it falls through to the IndexError without looping, which is confusing. The 'set' method references undefined functions (_get_spatial_filter, _set_spatial_filter) and 'get2' is a method instead of a property despite its naming. There is a logical segmentation of properties and methods, but the disorganized naming undermines structure. Deep nesting is moderate but comments don't compensate for unclear names. The code has multiple actions per line in list comprehensions and property getters, reducing granularity. Overall, the code is hard to follow and would require significant effort to understand without extensive documentation.

Run 2

The code is moderately readable, with clear structural organization and mostly consistent naming patterns, but suffers from several significant readability issues. The main problems are: (1) cryptic abbreviated names (e.g., 'nam', 'num', 'ext', 'fie', 'get2', 'test') that obscure intent; (2) inconsistent suffix numbering on methods/properties (num/num2, geom2, get2/get3/get4, field/field2/field3) suggesting incomplete refactoring; (3) missing method definitions referenced at the bottom (_get_spatial_filter, _set_spatial_filter); and (4) redundant loop variable names (i2, i3, i4). The code structure is logical, with clear separation between methods and properties, and nesting is mostly flat. Action granularity is generally good—one action per line—and reading flow is natural within each method, but abbreviations force the reader to mentally map short names to their purpose. The docstrings are helpful and explicit, which partially compensates for naming issues. Overall, the code is understandable but would benefit from a cleanup pass to improve naming consistency and clarity.

Run 3

This code is severely obfuscated and nearly unreadable. The naming conventions are extremely poor: properties like 'nam', 'ext', 'num2', 'geom2', 'fie', 'field2', 'field3', 'get2', 'get3', 'get4' are meaningless and actively obscure the code's intent. Variables like 'ds2', 'feat2', 'i2', 'i3', 'i4', 'for2', 'env2', 'cap', 'fil' follow a cryptic pattern that destroys semantic meaning. The original Django GDAL Layer class had clear names (name, extent, num_fields, geom_type, fields, field_widths, field_precisions, get_geoms, get_fields, test_capability), but these have been systematically replaced with nonsense. While the code structure retains logical organization with property decorators and method separation, the readability is destroyed by the naming. The code appears to be deliberately obfuscated, making it nearly impossible to understand without extensive effort. The docstrings sometimes help, but they contradict the terrible naming. Even the action granularity and flow are undermined because readers cannot follow what operations are being performed on what data.

Run 1

Code Structure: Logically organized into initialization, iteration, properties, and methods, but spatial_filter references undefined accessor names rather than get2 and set, creating a confusing inconsistency. Nesting: Mostly shallow, with manageable conditionals and loops. Clarity of intent: Docstrings and comments explain behavior, but abbreviated and numbered names such as nam, num2, field3, and get4 obscure purpose; str also shadows a built-in. Code length: Methods are generally concise, although repeated field-access expressions and some redundant comments add verbosity. Action granularity: Usually one action per statement, with comprehensions appropriately grouping related operations; nested API calls require extra parsing. Reading flow: Section headings and consistent formatting support navigation, but inconsistent pointer usage, ambiguous naming, and mismatched accessor references interrupt understanding.

Run 2

Code Structure: Logically organized into initialization, iteration, properties, and methods, with clear section markers. Nesting: Mostly shallow; feature lookup and spatial-filter handling add only moderate nesting. Clarity of intent: Docstrings explain behavior, but abbreviated and numbered names such as nam, num2, field3, and get4 obscure meaning. The spatial_filter declaration references undefined accessor names rather than get2 and set, making the intended connections confusing. Code length: Methods are generally concise, although repeated field-definition expressions and some verbose comments add bulk. Action granularity: Most lines perform one action; nested API calls and comprehensions occasionally combine several operations. Reading flow: Consistent formatting and grouping help navigation, but opaque naming, shadowing str, and inconsistent ptr/_ptr usage require extra mental effort.

Run 3

Code Structure: Logically organized into initialization, iteration, properties, filtering, and methods, with useful section markers. Nesting: Generally shallow; feature lookup and filter validation introduce only moderate nesting. Clarity of intent: Docstrings explain most operations, but cryptic names such as num2, field3, get4, and ds2 obscure meaning. The spatial_filter property references undefined accessor names rather than get2 and set, creating a confusing disconnect. Code length: Most methods are concise, although repetitive property implementations and some verbose comments add overhead. Action granularity: Statements generally perform one action per line; nested API calls and comprehensions occasionally combine several operations. Reading flow: Consistent formatting and method grouping help, but ambiguous naming, str shadowing a built-in, and inconsistent ptr/_ptr usage interrupt comprehension and require readers to consult documentation repeatedly.