How to avoid hardcoding if condition is python script

I am new to python. I have a query regarding non-hardcoding object names (if condition) in a python script. I have fruit = [Apple, Mango, Pineapple, Banana, Oranges] and size = [small, medium, large] I am currently writing code like below:

if (fruit == apple, size == small): 
   statement 1
   statement 2
elif (fruit == apple, size == medium):
   statement 1
   statement 2
elif (fruit == apple, size == big):
   statement 1
   statement 2
elif (fruit == Mango, size == small):
   statement 1
   statement 2
elif (fruit == Mango, size = medium):
   statement 1
   statement 2

      

How can I avoid writing multiple if ... else conditions?

Statement 1: Pulling the fruit and size dot file from the directory Path structure master-directory / fruit / pledge / fruit_size.dot statement 2: Pulling the fruit and size txt file from the directory Path structure main-directory / source / readparamters /readparam/fruit_size.txt

I want to follow the instructions for each condition one at a time. I am currently grabbing fruit and size materials from a user. Is there a way in python that a script can automatically take combinations one by one and execute instructions? I know it is somewhat complex and a python expert can help me.

+3


source to share


6 answers


How about using itertools.product

and handling errors with try..catch

. https://docs.python.org/3/library/itertools.html#itertools.product

import itertools

fruits = ['Apple', 'Mango', 'Pineapple', 'Banana', 'Oranges']
sizes = ['small', 'medium', 'big']

for fruit, size in itertools.product(fruits, sizes):
    dot_file = 'main-directory/{fruit}/collateral/{fruit}_{size}.dot'.format(fruit=fruit, size=size)
    text_file = 'main-directory/source/readparamters/readparam/{fruit}_{size}.txt'.format(fruit=fruit, size=size)
    try:
        open(dot_file)  # statement 1
        open(text_file)  # statement 2
    except Exception as e:
        print(e)  # handle erorrs!

      

Or, check if the file exists os.path.isfile

without using try..catch

.



import os
..
    dot_file = ..
    text_file = ..
    if os.path.isfile(dot_file):
        open(dot_file)  # statement 1
    if os.path.isfile(text_file):
        open(text_file)  # statement 2

      

itertools.product

generates the Cartesian product of the input iterations.

>>> fruits = ['Apple', 'Mango', 'Pineapple', 'Banana', 'Oranges']
>>> sizes = ['small', 'medium', 'big']
>>> list(itertools.product(fruits, sizes))
[('Apple', 'small'), ('Apple', 'medium'), ('Apple', 'big'), ('Mango', 'small'), ('Mango', 'medium'), ('Mango', 'big'), ('Pineapple', 'small'), ('Pineapple', 'medium'), ('Pineapple', 'big'), ('Banana', 'small'), ('Banana', 'medium'), ('Banana', 'big'), ('Oranges', 'small'), ('Oranges', 'medium'), ('Oranges', 'big')]

      

+1


source


You can create a map of values ​​and functions. for example

MAP = {'apples':{'small':function1,
                'large':function3},
      'oranges':{'small':function2}}
#Then Run it like so:
fruit = 'apples'
size = 'large'
result = MAP[fruit][size]()

      



This will look up the function for you in the dictionary using fruit and size, then run it and store the result as a result. This way, if you need to add additional fruits or sizes, you can simply change the data in the dictionary without changing any code.

EDIT: I just read your update. If the processing steps are the same and the only thing that changes is the file location, I would suggest writing a function that takes fruit and size as arguments and opens the file based on the input. Then you can run it with your desired fruit and sizes and not have crazy if the application.

+7


source


(My first answer)

You can combine all tests with one condition:

if (fruit == apple and (size == small or size == medium or size == big)) or \
        (fruit == Mango and (size == small or size == medium)):
    statement 1
    statement 2

      


(My second answer after the original poster did clarifications)

Write a loop to check if a condition is met and then follow the instructions.

fruit = (...)  # From input
size  = (...)  # From input

found = False
known_fruits = ["apple", "mango"]  # Can be customized
known_sizes = ["small", "medium", "large"]  # Can be customized
for fr in known_fruits:
    for sz in known_sizes:
        if fruit == fr and size == sz:
            found = True
if found:
    statement 1
    statement 2

      

+2


source


It really depends on what you have as assertion 1 and assertion 2.
One thing that works if the code is similar is to fetch all the changing constants into a container and use a dict to access them.

fruits = {
  'mango': {'color': 'orange'},
  'apple': {'color': 'red'},
   # ...
}
weights = {
  'apple': {
    'small': 100,
    'medium': 200,
    # ...
  }
  # ...
}

fruit = 'apple'
size = 'medium'

print "A %s %s is %s and weighs %ig" % (size, fruit, fruits[fruit]['color'], weights[fruit][size])
# => A medium apple is red and weighs 200g

      

0


source


If all filenames can be generated using only the names and sizes of the fruits:

def process_fruits(fruit, size, param):
    dot_file = "main-directory/{fruit_name}/collateral/{size}.dot"
    text_file = "main-directory/source/readparamters/{param}/{size}.txt"

    fruit = dict(fruit_name=fruit, size=size, param=param)

    paths = dict(dot=dot_file.format(**fruit), text=text_file.format(**fruit))

    # load paths['dot']
    # load paths['text']

      

You can add this to a function to get paths when creating a fruit.


fruits = ['apple', 'mango']

for fruit in fruits:
    for size in ['small', 'medium', 'big']:
        process_fruits(fruit, size, 10)

      

0


source


Python has no switch cases, unfortunately, but you can use a dictionary, like the accepted answer to this question:

Substitutions for switch statement in Python?

0


source







All Articles