Python Forum
Defining a Function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Defining a Function (/thread-6849.html)



count booleans in an if - mattt1998 - Dec-10-2017

I have this code that sorts a document according to the words it contains. There is a pythonic way of counting the files classified as "_fruit" and "_veget" that I am moving to the folder untitled1

path_txt = 'temp'
files1 = os.listdir(path_txt)
for TXT in files1:
    with open(path_txt + '\\' + TXT, "r") as content: 
        key1 = False
        key2 = False
        search = re.search(r'\d{21,30}', content.read())        
        text = content.read()
        if ('apple' in text and 'banana' in text and 'orange') or \
                ('apple' in text and 'orange' in text and 'watermelon'):
            key1 = True
        elif ('pumpkin' in text or 'potato' in text) and \
                        'carrot' in text and '' in text and \
                        'beet' in text and not 'pineapple' in text and not 'orange' in text:
            key2 = True
    if search is not None:  
        name1 = search.group(0)         
        if key1:
            fp = os.path.join("untitled1", name1 + '_fruit' + "_%d.txt")
            postfix = 0
            while os.path.exists(fp % postfix):
                postfix += 1
        elif key2:
            fp = os.path.join("untitled1", name1 + '_veget' + "_%d.txt")
            postfix = 0
            while os.path.exists(fp % postfix):
                postfix += 1
        os.rename(
            os.path.join(path_txt, TXT),
            fp % postfix
        )
ps: I have 8 "for" loops in the same code doing similar things. I will apply for them as well.


RE: count booleans in an if - Windspar - Dec-10-2017

example
def words_intext(text, wordlist):
	for word in wordlist:
		if word not in text:
			return False
	return True

value = 0
text = 'apple to banana of orange'
value += words_intext(text, ['apple','orange','banana'])
text = 'apple be a orange of banana'
value += words_intext(text, ['apple','orange','banana'])
text = 'apple vs orange'
value += words_intext(text, ['apple','orange','banana'])
print(value)



Defining a Function - mattt1998 - Dec-13-2017

Hi, I'm learning python, and I made this code that sorts a document according to the words it contains.

path_txt = 'temp'
files1 = os.listdir(path_txt)
for TXT in files1:
    with open(path_txt + '\\' + TXT, "r") as content: 
        key1 = False
        key2 = False
        search = re.search(r'\d{21,30}', content.read())        
        text = content.read()
        if ('apple' in text and 'banana' in text and 'orange') or \
                ('apple' in text and 'orange' in text and 'watermelon'):
            key1 = True
        elif ('pumpkin' in text or 'potato' in text) and \
                        'carrot' in text and 'broccoli' in text and \
                        'beet' in text and not 'pineapple' in text and not 'orange' in text:
            key2 = True
    if search is not None:  
        name1 = search.group(0)         
        if key1:
            fp = os.path.join("untitled1", name1 + '_fruit' + "_%d.txt")
            postfix = 0
            while os.path.exists(fp % postfix):
                postfix += 1
        elif key2:
            fp = os.path.join("untitled1", name1 + '_veget' + "_%d.txt")
            postfix = 0
            while os.path.exists(fp % postfix):
                postfix += 1
        os.rename(
            os.path.join(path_txt, TXT),
            fp % postfix
        )
I have 8 "for" loops in the same code doing similar things, and this part is repeated in all loops:

if ('apple' in text and 'banana' in text and 'orange') or \
                ('apple' in text and 'orange' in text and 'watermelon'):
            key1 = True
        elif ('pumpkin' in text or 'potato' in text) and \
                        'carrot' in text and 'broccoli' in text and \
                        'beet' in text and not 'pineapple' in text and not 'orange' in text:
            key2 = True
How do I put this into a "def" function?


RE: Defining a Function - mpd - Dec-13-2017

As you mention, you create a function with the def keyword. A function gets a name and can take arguments. So what would an argument be? Something that changes every time you call it, such as the text you're checking. In your case a function needs to return a result. In this case, it would be True or False depending on what is contained in the text. Return statements can have the form: return EXPRESSION where EXPRESSION is... well, an expression. It can be math e.g., return x+1 or it could be something that evaluates to true/false, like the expression in the if/elif statements.

To get you started...
def is_fruit(text):   # is_fruit is the name, text is the argument
    # now return whether or not there is fruit in this text



RE: Defining a Function - buran - Dec-14-2017

I wanted to mention this in your previous thread but this
 
'apple' in text and 'orange' in text and 'watermelon'
is same as
'apple' in text and 'orange' in text
'watermelon' as non-empty str object is always evaluated as True