flatten and simplify conditionals
This commit is contained in:
		
							parent
							
								
									891007054f
								
							
						
					
					
						commit
						dc9ed8e794
					
				| @ -950,7 +950,7 @@ class OctangularDelta: | ||||
|             sign = self.octangle & 0x02 > 0 | ||||
|             xy[axis] = self.proj_mag * (1 - 2 * sign) | ||||
|             return xy | ||||
|         else: | ||||
|         else:       # noqa: RET505 | ||||
|             yn = (self.octangle & 0x02) > 0 | ||||
|             xyn = (self.octangle & 0x01) > 0 | ||||
|             ys = 1 - 2 * yn | ||||
| @ -1097,7 +1097,6 @@ class Delta: | ||||
|         """ | ||||
|         if self.x == 0 or self.y == 0 or abs(self.x) == abs(self.y): | ||||
|             return write_uint(stream, OctangularDelta(self.x, self.y).as_uint() << 1) | ||||
|         else: | ||||
|         size = write_uint(stream, (encode_sint(self.x) << 1) | 0x01) | ||||
|         size += write_uint(stream, encode_sint(self.y)) | ||||
|         return size | ||||
| @ -1125,11 +1124,10 @@ def read_repetition(stream: IO[bytes]) -> repetition_t: | ||||
|     rtype = read_uint(stream) | ||||
|     if rtype == 0: | ||||
|         return ReuseRepetition.read(stream, rtype) | ||||
|     elif rtype in (1, 2, 3, 8, 9): | ||||
|     if rtype in (1, 2, 3, 8, 9): | ||||
|         return GridRepetition.read(stream, rtype) | ||||
|     elif rtype in (4, 5, 6, 7, 10, 11): | ||||
|     if rtype in (4, 5, 6, 7, 10, 11): | ||||
|         return ArbitraryRepetition.read(stream, rtype) | ||||
|     else: | ||||
|     raise InvalidDataError(f'Unexpected repetition type: {rtype}') | ||||
| 
 | ||||
| 
 | ||||
| @ -1311,7 +1309,7 @@ class GridRepetition: | ||||
|                 size = write_uint(stream, 9) | ||||
|                 size += write_uint(stream, self.a_count - 2) | ||||
|                 size += Delta(*self.a_vector).write(stream) | ||||
|         else: | ||||
|         else:   # noqa: PLR5501 | ||||
|             if self.a_vector[1] == 0 and self.b_vector[0] == 0: | ||||
|                 size = write_uint(stream, 1) | ||||
|                 size += write_uint(stream, self.a_count - 2) | ||||
| @ -1637,8 +1635,7 @@ def write_point_list( | ||||
|                 h_first = False | ||||
|                 v_first = False | ||||
|                 break | ||||
|         else: | ||||
|             if point[1] != previous[1] or point[0] == previous[0]: | ||||
|         elif point[1] != previous[1] or point[0] == previous[0]: | ||||
|             h_first = False | ||||
|             v_first = False | ||||
|             break | ||||
| @ -1650,7 +1647,7 @@ def write_point_list( | ||||
|         size += write_uint(stream, len(points)) | ||||
|         size += sum(write_sint(stream, x + y) for x, y in points) | ||||
|         return size | ||||
|     elif v_first: | ||||
|     if v_first: | ||||
|         size = write_uint(stream, 1) | ||||
|         size += write_uint(stream, len(points)) | ||||
|         size += sum(write_sint(stream, x + y) for x, y in points) | ||||
| @ -1773,29 +1770,28 @@ def read_property_value(stream: IO[bytes]) -> property_value_t: | ||||
|     prop_type = read_uint(stream) | ||||
|     if 0 <= prop_type <= 7: | ||||
|         return read_real(stream, prop_type) | ||||
|     elif prop_type == 8: | ||||
|     if prop_type == 8: | ||||
|         return read_uint(stream) | ||||
|     elif prop_type == 9: | ||||
|     if prop_type == 9: | ||||
|         return read_sint(stream) | ||||
|     elif prop_type == 10: | ||||
|     if prop_type == 10: | ||||
|         return AString.read(stream) | ||||
|     elif prop_type == 11: | ||||
|     if prop_type == 11: | ||||
|         return read_bstring(stream) | ||||
|     elif prop_type == 12: | ||||
|     if prop_type == 12: | ||||
|         return NString.read(stream) | ||||
|     elif prop_type == 13: | ||||
|     if prop_type == 13: | ||||
|         ref_type = AString | ||||
|         ref = read_uint(stream) | ||||
|         return PropStringReference(ref, ref_type) | ||||
|     elif prop_type == 14: | ||||
|     if prop_type == 14: | ||||
|         ref_type = bytes | ||||
|         ref = read_uint(stream) | ||||
|         return PropStringReference(ref, ref_type) | ||||
|     elif prop_type == 15: | ||||
|     if prop_type == 15: | ||||
|         ref_type = NString | ||||
|         ref = read_uint(stream) | ||||
|         return PropStringReference(ref, ref_type) | ||||
|     else: | ||||
|     raise InvalidDataError(f'Invalid property type: {prop_type}') | ||||
| 
 | ||||
| 
 | ||||
| @ -1883,16 +1879,15 @@ def read_interval(stream: IO[bytes]) -> tuple[int | None, int | None]: | ||||
|     interval_type = read_uint(stream) | ||||
|     if interval_type == 0: | ||||
|         return None, None | ||||
|     elif interval_type == 1: | ||||
|     if interval_type == 1: | ||||
|         return None, read_uint(stream) | ||||
|     elif interval_type == 2: | ||||
|     if interval_type == 2: | ||||
|         return read_uint(stream), None | ||||
|     elif interval_type == 3: | ||||
|     if interval_type == 3: | ||||
|         v = read_uint(stream) | ||||
|         return v, v | ||||
|     elif interval_type == 4: | ||||
|     if interval_type == 4: | ||||
|         return read_uint(stream), read_uint(stream) | ||||
|     else: | ||||
|     raise InvalidDataError(f'Unrecognized interval type: {interval_type}') | ||||
| 
 | ||||
| 
 | ||||
| @ -1916,14 +1911,11 @@ def write_interval( | ||||
|     if min_bound is None: | ||||
|         if max_bound is None: | ||||
|             return write_uint(stream, 0) | ||||
|         else: | ||||
|         return write_uint(stream, 1) + write_uint(stream, max_bound) | ||||
|     else: | ||||
|     if max_bound is None: | ||||
|         return write_uint(stream, 2) + write_uint(stream, min_bound) | ||||
|         elif min_bound == max_bound: | ||||
|     if min_bound == max_bound: | ||||
|         return write_uint(stream, 3) + write_uint(stream, min_bound) | ||||
|         else: | ||||
|     size = write_uint(stream, 4) | ||||
|     size += write_uint(stream, min_bound) | ||||
|     size += write_uint(stream, max_bound) | ||||
| @ -2190,9 +2182,7 @@ class Validation: | ||||
|         checksum_type = read_uint(stream) | ||||
|         if checksum_type == 0: | ||||
|             checksum = None | ||||
|         elif checksum_type == 1: | ||||
|             checksum = read_u32(stream) | ||||
|         elif checksum_type == 2: | ||||
|         elif checksum_type in (1, 2): | ||||
|             checksum = read_u32(stream) | ||||
|         else: | ||||
|             raise InvalidDataError('Invalid validation type!') | ||||
| @ -2214,13 +2204,12 @@ class Validation: | ||||
|         """ | ||||
|         if self.checksum_type == 0: | ||||
|             return write_uint(stream, 0) | ||||
|         elif self.checksum is None: | ||||
|         if self.checksum is None: | ||||
|             raise InvalidDataError(f'Checksum is empty but type is {self.checksum_type}') | ||||
|         elif self.checksum_type == 1: | ||||
|         if self.checksum_type == 1: | ||||
|             return write_uint(stream, 1) + write_u32(stream, self.checksum) | ||||
|         elif self.checksum_type == 2: | ||||
|         if self.checksum_type == 2: | ||||
|             return write_uint(stream, 2) + write_u32(stream, self.checksum) | ||||
|         else: | ||||
|         raise InvalidDataError(f'Unrecognized checksum type: {self.checksum_type}') | ||||
| 
 | ||||
|     def __repr__(self) -> str: | ||||
|  | ||||
| @ -193,7 +193,6 @@ class OasisLayout: | ||||
|         if record_id == 1: | ||||
|             if file_state.started: | ||||
|                 raise InvalidRecordError('Duplicate Start record') | ||||
|             else: | ||||
|             file_state.started = True | ||||
|         if record_id == 2 and file_state.within_cblock: | ||||
|             raise InvalidRecordError('End within CBlock') | ||||
|  | ||||
| @ -274,9 +274,8 @@ def read_refname( | ||||
|     """ | ||||
|     if not is_present: | ||||
|         return None | ||||
|     elif is_reference: | ||||
|     if is_reference: | ||||
|         return read_uint(stream) | ||||
|     else: | ||||
|     return NString.read(stream) | ||||
| 
 | ||||
| 
 | ||||
| @ -299,9 +298,8 @@ def read_refstring( | ||||
|     """ | ||||
|     if not is_present: | ||||
|         return None | ||||
|     elif is_reference: | ||||
|     if is_reference: | ||||
|         return read_uint(stream) | ||||
|     else: | ||||
|     return AString.read(stream) | ||||
| 
 | ||||
| 
 | ||||
| @ -994,9 +992,10 @@ class Property(Record): | ||||
|     def write(self, stream: IO[bytes]) -> int: | ||||
|         if self.is_standard is None and self.values is None and self.name is None: | ||||
|             return write_uint(stream, 29) | ||||
|         else: | ||||
| 
 | ||||
|         if self.is_standard is None: | ||||
|             raise InvalidDataError('Property has value or name, but no is_standard flag!') | ||||
| 
 | ||||
|         if self.values is not None: | ||||
|             value_count = len(self.values) | ||||
|             vv = 0 | ||||
| @ -1736,9 +1735,8 @@ class Polygon(Record, GeometryMixin): | ||||
|         self.point_list = point_list | ||||
|         self.properties = [] if properties is None else properties | ||||
| 
 | ||||
|         if point_list is not None: | ||||
|             if len(point_list) < 3: | ||||
|                 warn('Polygon with < 3 points') | ||||
|         if point_list is not None and len(point_list) < 3: | ||||
|             warn('Polygon with < 3 points', stacklevel=2) | ||||
| 
 | ||||
|     def get_point_list(self) -> point_list_t: | ||||
|         return verify_modal(self.point_list) | ||||
| @ -1921,13 +1919,12 @@ class Path(Record, GeometryMixin): | ||||
|             def get_pathext(ext_scheme: int) -> pathextension_t | None: | ||||
|                 if ext_scheme == 0: | ||||
|                     return None | ||||
|                 elif ext_scheme == 1: | ||||
|                 if ext_scheme == 1: | ||||
|                     return PathExtensionScheme.Flush, None | ||||
|                 elif ext_scheme == 2: | ||||
|                 if ext_scheme == 2: | ||||
|                     return PathExtensionScheme.HalfWidth, None | ||||
|                 elif ext_scheme == 3: | ||||
|                 if ext_scheme == 3: | ||||
|                     return PathExtensionScheme.Arbitrary, read_sint(stream) | ||||
|                 else: | ||||
|                 raise InvalidDataError(f'Invalid ext_scheme: {ext_scheme}') | ||||
| 
 | ||||
|             optional['extension_start'] = get_pathext(scheme_start) | ||||
| @ -2066,8 +2063,7 @@ class Trapezoid(Record, GeometryMixin): | ||||
|         if self.is_vertical: | ||||
|             if height is not None and delta_b - delta_a > height: | ||||
|                 raise InvalidDataError(f'Trapezoid: h < delta_b - delta_a ({height} < {delta_b} - {delta_a})') | ||||
|         else: | ||||
|             if width is not None and delta_b - delta_a > width: | ||||
|         elif width is not None and delta_b - delta_a > width: | ||||
|             raise InvalidDataError(f'Trapezoid: w < delta_b - delta_a ({width} < {delta_b} - {delta_a})') | ||||
| 
 | ||||
|     def get_is_vertical(self) -> bool: | ||||
| @ -2392,7 +2388,7 @@ class CTrapezoid(Record, GeometryMixin): | ||||
|             raise InvalidDataError(f'CTrapezoid has spurious height entry: {height}') | ||||
| 
 | ||||
|         if width is not None and height is not None: | ||||
|             if ctrapezoid_type in range(0, 4) and width < height: | ||||
|             if ctrapezoid_type in range(0, 4) and width < height:           # noqa: PIE808 | ||||
|                 raise InvalidDataError(f'CTrapezoid has width < height ({width} < {height})') | ||||
|             if ctrapezoid_type in range(4, 8) and width < 2 * height: | ||||
|                 raise InvalidDataError(f'CTrapezoid has width < 2*height ({width} < 2 * {height})') | ||||
| @ -2401,7 +2397,7 @@ class CTrapezoid(Record, GeometryMixin): | ||||
|             if ctrapezoid_type in range(12, 16) and 2 * width > height: | ||||
|                 raise InvalidDataError(f'CTrapezoid has 2*width > height ({width} > 2 * {height})') | ||||
| 
 | ||||
|         if ctrapezoid_type is not None and ctrapezoid_type not in range(0, 26): | ||||
|         if ctrapezoid_type is not None and ctrapezoid_type not in range(0, 26):   # noqa: PIE808 | ||||
|             raise InvalidDataError(f'CTrapezoid has invalid type: {ctrapezoid_type}') | ||||
| 
 | ||||
| 
 | ||||
| @ -2532,7 +2528,6 @@ def adjust_repetition(record, modals: Modals) -> None: | ||||
|         if isinstance(record.repetition, ReuseRepetition): | ||||
|             if modals.repetition is None: | ||||
|                 raise InvalidDataError('Unfillable repetition') | ||||
|             else: | ||||
|             record.repetition = copy.copy(modals.repetition) | ||||
|         else: | ||||
|             modals.repetition = copy.copy(record.repetition) | ||||
| @ -2679,8 +2674,7 @@ def dedup_coordinates(record, modals: Modals, mx_field: str, my_field: str) -> N | ||||
|         if modals.xy_relative: | ||||
|             record.x -= mx | ||||
|             setattr(modals, mx_field, record.x) | ||||
|         else: | ||||
|             if record.x == mx: | ||||
|         elif record.x == mx: | ||||
|             record.x = None | ||||
|         else: | ||||
|             setattr(modals, mx_field, record.x) | ||||
| @ -2690,8 +2684,7 @@ def dedup_coordinates(record, modals: Modals, mx_field: str, my_field: str) -> N | ||||
|         if modals.xy_relative: | ||||
|             record.y -= my | ||||
|             setattr(modals, my_field, record.y) | ||||
|         else: | ||||
|             if record.y == my: | ||||
|         elif record.y == my: | ||||
|             record.y = None | ||||
|         else: | ||||
|             setattr(modals, my_field, record.y) | ||||
|  | ||||
| @ -135,8 +135,7 @@ def test_file_1() -> None: | ||||
|                 assert gg.width == [250, None][is_ctrapz], msg | ||||
|             elif ct_type in range(22, 24) or ct_type == 25: | ||||
|                 assert gg.height == [100, None][is_ctrapz], msg | ||||
|             else: | ||||
|                 if ct_type < 8 or 16 <= ct_type < 25 or 26 <= ct_type: | ||||
|             elif ct_type < 8 or 16 <= ct_type < 25 or ct_type >= 26: | ||||
|                 assert gg.width == 250, msg | ||||
|                 assert gg.height == 100, msg | ||||
|             else: | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user