Python Forum
Calling a Returned Value to Another Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a Returned Value to Another Function
#1
Hi Gigs,

I'm trying to call a value obtained from a function to another function. In this case, i want to call var_name into get_dirs_from_path() function.
Below is what i did, but it's not working.
NB:  get_dirs_from_path()  must not have a parameter.

#! /usr/bin/python3

import os
import sys

def get_environment_variable_value(variable_name):
    var_name = os.environ[variable_name]
    if not var_name:
        return ""
    else:
        return var_name

def get_dirs_from_path():
    path_var = var_name
    directories = path_var.split(':')
    for entry in directories:
        print (entry)
Please advise.
Smile
Reply
#2
You can call one function from inside another.

def get_dirs_from_path():
    path_var = get_environment_variable_value(some_env_var_name)
    if path_var:
        directories = path_var.split(':')
        for entry in directories:
            print (entry)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
What should be the "some_env_var_name" in this case ?

When i test the code below, 

#! /usr/bin/python3

import os
import sys

def get_environment_variable_value(variable_name):
    var_name = os.environ[variable_name]
    if not var_name:
        return ""
    else:
        return var_name

def get_dirs_from_path():
    path_var = get_environment_variable_value(variable_name)
    directories = path_var.split(':')
    for entry in directories:
        print (entry)


get_environment_variable_value('PATH')
get_dirs_from_path()
it prints the following output:
Output:
psimo@itserver6:~/it117/hw/hw6$ ./hw66.py Traceback (most recent call last):   File "./hw66.py", line 22, in <module>     get_dirs_from_path()   File "./hw66.py", line 15, in get_dirs_from_path     path_var = get_environment_variable_value(variable_name) NameError: name 'variable_name' is not defined psimo@itserver6:~/it117/hw/hw6$
Reply
#4
#! /usr/bin/python3

import os
import sys

def get_environment_variable_value(variable_name):
   return os.environ.get(variable_name,'')

def get_dirs_from_path():
   path_var = get_environment_variable_value('PATH')
   directories = path_var.split(':')
   for entry in directories:
       print (entry)

get_dirs_from_path()
Note the way I change the get_environment_variable_value(variable_name). In your code there you will get KeyError if variable_name is not in the list of environment variables. This more or less removes the need of additional function...
Reply
#5
I have to follow some steps ( mandatory ) to complete this task.


get_environment_variable_value()
  • The header for this function must be
    get_environment_variable_value(variable_name)

  • This function must accept a shell variable as its only parameter

  • The function must return the value of this shell variable

  • If the variable is not defined, the function must return the empty string
get_dirs_from_path()
  • The header for this function must be
    get_dirs_from_path()

  • This function must accept no parameters

  • This function returns a list of the directories contained in PATH
get_file_count()
  • The header for this function must be
    get_file_count(dir_path)

  • This function must accept a directory name as a parameter

  • The function must return the number of files in this directory

  • Sub-directories must not be included in this count
and the test code has to look like this :
path_dirs = get_dirs_from_path()
dir_count = get_file_count_for_dir_list(path_dirs)
print_sorted_dictionary(dir_count)
Reply
#6
Buran already posted first two functions. And last one could be:

import os

def get_file_count(dir_path):
    count = 0
    for name in os.listdir(dir_path):
        if os.path.isfile(os.path.join(dir_path, name)):
            count += 1
    return count
Its possible to do it like one-liner with comprehension, but that would be little confusing.
Reply
#7
(Mar-10-2017, 02:29 PM)valerydolce Wrote: What should be the "some_env_var_name" in this case ?
This is variable_name. Mine is just a discription as a var name.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
Photo 
Hi Zivoni,

He used the 'PATH' value instead of a variable under function  get_dirs_from_path()
Reply
#9
(Mar-10-2017, 03:49 PM)valerydolce Wrote: I have to follow some steps ( mandatory ) to complete this task.
[snip]

and the test code has to look like this :


That smells like homework, so I've moved it.
That said, show us what you've tried, we won't do your work for you.
Reply
#10
Nilamo,

It was a homework. If you read my initial post you'll see my work. However, i figured it out. 

Thanks for your help.
-Valerydolce
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting Data Returned From Database Timbo03 9 1,983 Feb-19-2023, 02:12 PM
Last Post: Larz60+
  Calling a function from another function johneven 2 2,871 Jul-09-2019, 12:42 AM
Last Post: micseydel
  Calling function-- how to call simply return value, not whole process juliabrushett 2 3,198 Jul-01-2018, 01:17 AM
Last Post: juliabrushett
  Calling a function to return a list of percentages Liquid_Ocelot 7 6,264 Mar-25-2017, 01:20 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020