dimanche 25 octobre 2020

Pythonic way to write long if statement in list comprehension

I'm beginner in python and hope you help me find way to write if statement correctly. This is code i have:

target_dir = Path(__file__).resolve().parents[1]
nav_files = [
    x
    for x in target_dir.rglob('conf.yaml')
    if '.'.join(str(x.parent).replace(str(target_dir), '').lstrip('/').split('/')[:2]) in settings.MODULES
]

You may see unacceptably long if statement. As i understand there are 2 ways to resolve this:

  1. Separate nav_files preparation and use some for blocks
  2. Separate if statement by linebreaks(if it's possible)

What is the best practice? Thanks!

Aucun commentaire:

Enregistrer un commentaire