Embrace Python's idiomatic programming style to make your code more readable, concise, and maintainable. Follow these Pythonic patterns: 1. **Use truthiness checks** instead of explicit length comparisons:
Embrace Python’s idiomatic programming style to make your code more readable, concise, and maintainable. Follow these Pythonic patterns:
# Instead of this:
if len(mod['Key']):
# Do this:
if mod['Key']:
# Instead of this:
if 'source_arn' in conf.keys():
# Do this:
if 'source_arn' in conf:
# Instead of this:
for vertex in self.tf_graph.vertices:
if vertex.block_type == BlockType.RESOURCE:
self._address_to_tf_vertex_map[vertex.attributes[TF_PLAN_RESOURCE_ADDRESS]] = vertex
# Do this:
self._address_to_tf_vertex_map = {
vertex.attributes[TF_PLAN_RESOURCE_ADDRESS]: vertex
for vertex in self.tf_graph.vertices
if vertex.block_type == BlockType.RESOURCE
}
# Instead of this:
f = open(f"{sast_policies_dir}/{policy.get('id')}.yaml", "a")
# ...operations with f...
f.close()
# Do this:
with open(f"{sast_policies_dir}/{policy.get('id')}.yaml", "a") as f:
# ...operations with f...
# Instead of this:
from typing import Dict, List
def function(param: Dict[str, List[int]]) -> None:
# Do this:
def function(param: dict[str, list[int]]) -> None:
# Instead of this:
if source_bc_id:
ckv_id = self.get_ckv_id_from_bc_id(source_bc_id)
if ckv_id:
ckv_ids.append(ckv_id)
# Do this:
if not source_bc_id:
continue
ckv_id = self.get_ckv_id_from_bc_id(source_bc_id)
if not ckv_id:
continue
ckv_ids.append(ckv_id)
# Instead of this:
k8s_validator = K8sValidator()
if k8s_validator.is_valid_template(t):
# Do this:
if K8sValidator.is_valid_template(t):
These practices will result in cleaner, more maintainable code that follows Python’s design philosophy of readability and simplicity.
Enter the URL of a public GitHub repository