Python Forum
Multiple variable inputs when only one is called for
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple variable inputs when only one is called for
#1
Hi all, trying to run a python code (not mine) that used a function generate_index(arg). The arg function is actually meant to have 5 parts (host, graft, index, memory, and threads) and these components are called later in the function as arg.host, arg.graft...etc.

Probably a dumb question, but how can i input these five variables into the function? when I separate by commas, i get an error saying only expected one variable. I tried adding a double asterisk to before the variable arg, but this did not help either...

Relevant introduction to the Code as below and is from the xenocell repository:


def generate_index(**args):

  print_header(args)
  print_log('Generate index...')
  create_directory(args.output)
  run_xenome(args)
  print_log('Generation of Xenome index finished!')


#
def print_header(args):
  header = '''##----------------------------------------------------------------------------##
## XenoCell: Generate Xenome index of reference genomes.
##----------------------------------------------------------------------------##
## FASTA of host reference genome:  {host}
## FASTA of graft reference genome: {graft}
## Output directory:                {output}
## Number of threads:               {threads}
## Memory in GB:                    {memory}
##----------------------------------------------------------------------------##'''.format(
    host    = args.host,
    graft   = args.graft,
    output  = args.output,
    threads = args.threads,
    memory  = args.memory
Larz60+ write Oct-19-2023, 10:37 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Modified for you this time. Please use BBCode tags on future posts.
Reply
#2
That code is probably part of a class
args.host ,etc.. looks to be from a class object

Simple example
class MyClass:
    def __init__(self):
        self.foo = 'printing foo'
        self.bar = 'printing bar'
        
m = MyClass()

def func(arg):
    print(arg.foo)
    print(arg.bar)

func(m)
output
Output:
printing foo printing bar
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
I spent too much time trying to think of how the posted code could work and couldn't come up with anything that can be passed in as **args and used as args.attribute. So I took a look at the xenocell code and saw I was right. This is the generate_index code from xenocell.
def generate_index(args):

  print_header(args)
  print_log('Generate index...')
  create_directory(args.output)
  run_xenome(args)
  print_log('Generation of Xenome index finished!')
Then I carefully read your post and saw you added the ** before args, and I felt foolish.

args is expected to be an argsparse.Namespace object, the type of object returned by argparse.ArgumentParser.parse(). xenocell is a command-line tool. You run the program from the command line, including the arguments. argparse parses the arguments and creates a Namespace object that is passed to the functions in the program.

If you want to import xenocell modules and use the functions, you'll need to make a compatible arg object. May as well use argparse.Namespace.
from argparse import Namespace


def generate_index(args):
    print_header(args)


def print_header(args):
    print(args.output)


generate_index(Namespace(output="Hello"))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Couldn't install a go-game called dlgo Nomamesse 14 3,151 Jan-05-2023, 06:38 PM
Last Post: Nomamesse
  how can a function find the name by which it is called? Skaperen 18 3,517 Aug-24-2022, 04:52 PM
Last Post: Skaperen
  Multiple Loop Statements in a Variable Dexty 1 1,211 May-23-2022, 08:53 AM
Last Post: bowlofred
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,145 Jan-15-2022, 06:07 PM
Last Post: deanhystad
Lightbulb Multiple inputs on the same line (beginner) dementshuk 9 2,827 Sep-03-2021, 02:21 PM
Last Post: dementshuk
  Generate Multiple sql Files With csv inputs vkomarag 13 4,238 Aug-20-2021, 07:03 PM
Last Post: vkomarag
  What is this formatting called? Mark17 2 1,779 Dec-14-2020, 08:42 PM
Last Post: snippsat
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,237 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  How to pass multiple values from one sample to nc variable? Baloch 0 1,873 Jun-01-2020, 09:27 PM
Last Post: Baloch
  Class Instances called in the wrong order IanIous 4 2,853 Mar-06-2020, 02:16 PM
Last Post: IanIous

Forum Jump:

User Panel Messages

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