importing variables into python namespace using argparse module

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

importing variables into python namespace using argparse module

Abhishek Pratap
Hey All


This might sound naive as I am trying to learn python.

I am using argparse to parse the command line arguments but I am not
sure how they variables created by it are imported into python's
current namespace for downstream usage.

eg:


parser = argparse.ArgumentParser(description='Process some integers')

parser.add_argument('--file_1', nargs=1 , type=file, required=True,
help='Name of file 1')
parser.add_argument('--file_2', nargs=1 , type=file, required=True,
help='Name of file 2')
parser.parse_args()

print "file 1 is %s) % file_1 ## as expected gives me error
NameError: name file_1' is not defined

What I am not sure is now how can I use the file_1 and file_2 variable
for anything downstream.

PS: Please let me know if this is not a appropriate forum to push such
questions and if there are other mailing list that I could use coz in
the coming days I am sure to send in a lot of email traffic.

Thanks!
-Abhi
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: importing variables into python namespace using argparse module

Rami Chowdhury
On Wed, Sep 7, 2011 at 21:11, Abhishek Pratap <[hidden email]> wrote:
> This might sound naive as I am trying to learn python.

Not at all, we've all been there :-)

> I am using argparse to parse the command line arguments but I am not
> sure how they variables created by it are imported into python's
> current namespace for downstream usage.

The simple answer is that they're not. I'll explain further after the
code sample

> parser = argparse.ArgumentParser(description='Process some integers')
>
> parser.add_argument('--file_1', nargs=1 , type=file, required=True,
> help='Name of file 1')
> parser.add_argument('--file_2', nargs=1 , type=file, required=True,
> help='Name of file 2')
> parser.parse_args()

Here, you need to capture the object that is returned from
parser.parse_args() -- that object is where any data captured by the
parser is stored, and you can access it from that object. So, for
instance:

>>> args = parser.parse_args()
>>> print "File 1 is %s" % args.file_1

> PS: Please let me know if this is not a appropriate forum to push such
> questions and if there are other mailing list that I could use coz in
> the coming days I am sure to send in a lot of email traffic.

You could also try the main Python-language mailing list
([hidden email]) -- there are more people on the list and you
might get quicker responses to questions :-)

Hope that helps,
Rami

--
Rami Chowdhury
"Never assume malice when stupidity will suffice." -- Hanlon's Razor
+44-7581-430-517 / +1-408-597-7068 / +88-0189-245544
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: importing variables into python namespace using argparse module

Abhishek Pratap
Thanks Simeon and Rami. Works now.

-Abhi




On Wed, Sep 7, 2011 at 1:55 PM, Rami Chowdhury <[hidden email]> wrote:

> On Wed, Sep 7, 2011 at 21:11, Abhishek Pratap <[hidden email]> wrote:
>> This might sound naive as I am trying to learn python.
>
> Not at all, we've all been there :-)
>
>> I am using argparse to parse the command line arguments but I am not
>> sure how they variables created by it are imported into python's
>> current namespace for downstream usage.
>
> The simple answer is that they're not. I'll explain further after the
> code sample
>
>> parser = argparse.ArgumentParser(description='Process some integers')
>>
>> parser.add_argument('--file_1', nargs=1 , type=file, required=True,
>> help='Name of file 1')
>> parser.add_argument('--file_2', nargs=1 , type=file, required=True,
>> help='Name of file 2')
>> parser.parse_args()
>
> Here, you need to capture the object that is returned from
> parser.parse_args() -- that object is where any data captured by the
> parser is stored, and you can access it from that object. So, for
> instance:
>
>>>> args = parser.parse_args()
>>>> print "File 1 is %s" % args.file_1
>
>> PS: Please let me know if this is not a appropriate forum to push such
>> questions and if there are other mailing list that I could use coz in
>> the coming days I am sure to send in a lot of email traffic.
>
> You could also try the main Python-language mailing list
> ([hidden email]) -- there are more people on the list and you
> might get quicker responses to questions :-)
>
> Hope that helps,
> Rami
>
> --
> Rami Chowdhury
> "Never assume malice when stupidity will suffice." -- Hanlon's Razor
> +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544
>
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: importing variables into python namespace using argparse module

Abhishek Pratap
Guys

I think I am stuck again while trying to open a file. It looks like a
type casting error but not sure.

##Error:
TypeError: coercing to Unicode: need string or buffer, list found


#code

def read_file(file):
    """Read the first same and create a dictionary
    """
    fh1 = open(file,'r')
    for line in fh1:
        print line


sam_file_1 = args.sam_file_1
sam_file_2 = args.sam_file_2

read_file(sam_file_1)


-Abhi


On Wed, Sep 7, 2011 at 2:01 PM, Abhishek Pratap <[hidden email]> wrote:

> Thanks Simeon and Rami. Works now.
>
> -Abhi
>
>
>
>
> On Wed, Sep 7, 2011 at 1:55 PM, Rami Chowdhury <[hidden email]> wrote:
>> On Wed, Sep 7, 2011 at 21:11, Abhishek Pratap <[hidden email]> wrote:
>>> This might sound naive as I am trying to learn python.
>>
>> Not at all, we've all been there :-)
>>
>>> I am using argparse to parse the command line arguments but I am not
>>> sure how they variables created by it are imported into python's
>>> current namespace for downstream usage.
>>
>> The simple answer is that they're not. I'll explain further after the
>> code sample
>>
>>> parser = argparse.ArgumentParser(description='Process some integers')
>>>
>>> parser.add_argument('--file_1', nargs=1 , type=file, required=True,
>>> help='Name of file 1')
>>> parser.add_argument('--file_2', nargs=1 , type=file, required=True,
>>> help='Name of file 2')
>>> parser.parse_args()
>>
>> Here, you need to capture the object that is returned from
>> parser.parse_args() -- that object is where any data captured by the
>> parser is stored, and you can access it from that object. So, for
>> instance:
>>
>>>>> args = parser.parse_args()
>>>>> print "File 1 is %s" % args.file_1
>>
>>> PS: Please let me know if this is not a appropriate forum to push such
>>> questions and if there are other mailing list that I could use coz in
>>> the coming days I am sure to send in a lot of email traffic.
>>
>> You could also try the main Python-language mailing list
>> ([hidden email]) -- there are more people on the list and you
>> might get quicker responses to questions :-)
>>
>> Hope that helps,
>> Rami
>>
>> --
>> Rami Chowdhury
>> "Never assume malice when stupidity will suffice." -- Hanlon's Razor
>> +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544
>>
>
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: importing variables into python namespace using argparse module

Eric Walstad
Hi Abhishek,

In the future please include the full error traceback.  It includes
information that helps in debugging.

It looks like your sam_file_1 variable contains a list when a string
is expected.  You could try printing the value of that variable before
passing it to your read_file function to see what it contains.  You've
changed your code and didn't include all the changes.

Eric.


On Wed, Sep 7, 2011 at 2:56 PM, Abhishek Pratap <[hidden email]> wrote:

> Guys
>
> I think I am stuck again while trying to open a file. It looks like a
> type casting error but not sure.
>
> ##Error:
> TypeError: coercing to Unicode: need string or buffer, list found
>
>
> #code
>
> def read_file(file):
>    """Read the first same and create a dictionary
>    """
>    fh1 = open(file,'r')
>    for line in fh1:
>        print line
>
>
> sam_file_1 = args.sam_file_1
> sam_file_2 = args.sam_file_2
>
> read_file(sam_file_1)
>
>
> -Abhi
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: importing variables into python namespace using argparse module

Shiqi Yang-2
In reply to this post by Abhishek Pratap
It might be because the type is set to 'file' for args.sam_file_1 and
then the file object was returned as sam_file_1
then passed to open() in read_file()

maybe try change type=str would help

HTH,
Shiqi

On Wed, Sep 7, 2011 at 2:56 PM, Abhishek Pratap <[hidden email]> wrote:

> Guys
>
> I think I am stuck again while trying to open a file. It looks like a
> type casting error but not sure.
>
> ##Error:
> TypeError: coercing to Unicode: need string or buffer, list found
>
>
> #code
>
> def read_file(file):
>    """Read the first same and create a dictionary
>    """
>    fh1 = open(file,'r')
>    for line in fh1:
>        print line
>
>
> sam_file_1 = args.sam_file_1
> sam_file_2 = args.sam_file_2
>
> read_file(sam_file_1)
>
>
> -Abhi
>
>
> On Wed, Sep 7, 2011 at 2:01 PM, Abhishek Pratap <[hidden email]> wrote:
>> Thanks Simeon and Rami. Works now.
>>
>> -Abhi
>>
>>
>>
>>
>> On Wed, Sep 7, 2011 at 1:55 PM, Rami Chowdhury <[hidden email]> wrote:
>>> On Wed, Sep 7, 2011 at 21:11, Abhishek Pratap <[hidden email]> wrote:
>>>> This might sound naive as I am trying to learn python.
>>>
>>> Not at all, we've all been there :-)
>>>
>>>> I am using argparse to parse the command line arguments but I am not
>>>> sure how they variables created by it are imported into python's
>>>> current namespace for downstream usage.
>>>
>>> The simple answer is that they're not. I'll explain further after the
>>> code sample
>>>
>>>> parser = argparse.ArgumentParser(description='Process some integers')
>>>>
>>>> parser.add_argument('--file_1', nargs=1 , type=file, required=True,
>>>> help='Name of file 1')
>>>> parser.add_argument('--file_2', nargs=1 , type=file, required=True,
>>>> help='Name of file 2')
>>>> parser.parse_args()
>>>
>>> Here, you need to capture the object that is returned from
>>> parser.parse_args() -- that object is where any data captured by the
>>> parser is stored, and you can access it from that object. So, for
>>> instance:
>>>
>>>>>> args = parser.parse_args()
>>>>>> print "File 1 is %s" % args.file_1
>>>
>>>> PS: Please let me know if this is not a appropriate forum to push such
>>>> questions and if there are other mailing list that I could use coz in
>>>> the coming days I am sure to send in a lot of email traffic.
>>>
>>> You could also try the main Python-language mailing list
>>> ([hidden email]) -- there are more people on the list and you
>>> might get quicker responses to questions :-)
>>>
>>> Hope that helps,
>>> Rami
>>>
>>> --
>>> Rami Chowdhury
>>> "Never assume malice when stupidity will suffice." -- Hanlon's Razor
>>> +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544
>>>
>>
> _______________________________________________
> Baypiggies mailing list
> [hidden email]
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: importing variables into python namespace using argparse module

Abhishek Pratap
In reply to this post by Eric Walstad
Hi Eric

Sorry I did not provide the full info. Here is the exact code with
full traceback at the end.


import sys
sys.path.append('/house/homedirs/a/apratap/lib/python');
import argparse



def read_file(file):
    """Read the first same and create a dictionary
    """
    with open(file,'r') as fh1:
        for line in fh1:
            line.strip()
            temp = line.split("\t")
            print 'header : %s \t flag : %s' % (temp[0],temp[1])




def compare_with_second_sam_file(file):
    pass



parser = argparse.ArgumentParser(description='Process some integers')

parser.add_argument('--sam_file_1', nargs=1 , type=file,
required=True,  help='Name of sam file 1')
parser.add_argument('--sam_file_2', nargs=1 , type=file,
required=True,  help='Name of sam file 2')

args=parser.parse_args()

sam_file_1 = args.sam_file_1
sam_file_2 = args.sam_file_2

print 'sam _file_1 has the value %s' % sam_file_1
print 'type of var %s' % type(sam_file_1)

read_file(sam_file_1)


###
Traceback
###


sam _file_1 has the value [<open file '1.sam', mode 'r' at 0x237db58>]
type of var <type 'list'>
Traceback (most recent call last):
  File "/house/homedirs/a/apratap/dev/eclipse_workspace/python_scripts/src/compare_read_names_in_sam_files.py",
line 41, in <module>
    read_file(sam_file_1)
  File "/house/homedirs/a/apratap/dev/eclipse_workspace/python_scripts/src/compare_read_names_in_sam_files.py",
line 13, in read_file
    with open(file,'r') as fh1:
TypeError: coercing to Unicode: need string or buffer, list found





On Wed, Sep 7, 2011 at 3:08 PM, Eric Walstad <[hidden email]> wrote:

> Hi Abhishek,
>
> In the future please include the full error traceback.  It includes
> information that helps in debugging.
>
> It looks like your sam_file_1 variable contains a list when a string
> is expected.  You could try printing the value of that variable before
> passing it to your read_file function to see what it contains.  You've
> changed your code and didn't include all the changes.
>
> Eric.
>
>
> On Wed, Sep 7, 2011 at 2:56 PM, Abhishek Pratap <[hidden email]> wrote:
>> Guys
>>
>> I think I am stuck again while trying to open a file. It looks like a
>> type casting error but not sure.
>>
>> ##Error:
>> TypeError: coercing to Unicode: need string or buffer, list found
>>
>>
>> #code
>>
>> def read_file(file):
>>    """Read the first same and create a dictionary
>>    """
>>    fh1 = open(file,'r')
>>    for line in fh1:
>>        print line
>>
>>
>> sam_file_1 = args.sam_file_1
>> sam_file_2 = args.sam_file_2
>>
>> read_file(sam_file_1)
>>
>>
>> -Abhi
>
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: importing variables into python namespace using argparse module

Abhishek Pratap
In reply to this post by Shiqi Yang-2
I think I understand why the error happens but still not sure about a
step 2(below)

1. I was not aware that the object type returned by
parser.add_argument when "type=file" is a file handle. I took it as a
string or file name.

**Here is what I dont understand.**
2.
parser.add_argument('--sam_file_1', nargs=1 , type=file,
required=True,  help='Name of sam file 1')
sam_file_1 = args.sam_file_1

sam_file_1 is a list ?? I am not sure why

Does this mean all the arguments returned by parser.parse_args() are a
list even if they have one variable. May be I am missing a concept
here.

If I make the following change then the code works which is basically
extracting an element out of the list.
sam_file_1 = args.sam_file_1 [0]

-Abhi



On Wed, Sep 7, 2011 at 3:15 PM, Shiqi Yang <[hidden email]> wrote:

> It might be because the type is set to 'file' for args.sam_file_1 and
> then the file object was returned as sam_file_1
> then passed to open() in read_file()
>
> maybe try change type=str would help
>
> HTH,
> Shiqi
>
> On Wed, Sep 7, 2011 at 2:56 PM, Abhishek Pratap <[hidden email]> wrote:
>> Guys
>>
>> I think I am stuck again while trying to open a file. It looks like a
>> type casting error but not sure.
>>
>> ##Error:
>> TypeError: coercing to Unicode: need string or buffer, list found
>>
>>
>> #code
>>
>> def read_file(file):
>>    """Read the first same and create a dictionary
>>    """
>>    fh1 = open(file,'r')
>>    for line in fh1:
>>        print line
>>
>>
>> sam_file_1 = args.sam_file_1
>> sam_file_2 = args.sam_file_2
>>
>> read_file(sam_file_1)
>>
>>
>> -Abhi
>>
>>
>> On Wed, Sep 7, 2011 at 2:01 PM, Abhishek Pratap <[hidden email]> wrote:
>>> Thanks Simeon and Rami. Works now.
>>>
>>> -Abhi
>>>
>>>
>>>
>>>
>>> On Wed, Sep 7, 2011 at 1:55 PM, Rami Chowdhury <[hidden email]> wrote:
>>>> On Wed, Sep 7, 2011 at 21:11, Abhishek Pratap <[hidden email]> wrote:
>>>>> This might sound naive as I am trying to learn python.
>>>>
>>>> Not at all, we've all been there :-)
>>>>
>>>>> I am using argparse to parse the command line arguments but I am not
>>>>> sure how they variables created by it are imported into python's
>>>>> current namespace for downstream usage.
>>>>
>>>> The simple answer is that they're not. I'll explain further after the
>>>> code sample
>>>>
>>>>> parser = argparse.ArgumentParser(description='Process some integers')
>>>>>
>>>>> parser.add_argument('--file_1', nargs=1 , type=file, required=True,
>>>>> help='Name of file 1')
>>>>> parser.add_argument('--file_2', nargs=1 , type=file, required=True,
>>>>> help='Name of file 2')
>>>>> parser.parse_args()
>>>>
>>>> Here, you need to capture the object that is returned from
>>>> parser.parse_args() -- that object is where any data captured by the
>>>> parser is stored, and you can access it from that object. So, for
>>>> instance:
>>>>
>>>>>>> args = parser.parse_args()
>>>>>>> print "File 1 is %s" % args.file_1
>>>>
>>>>> PS: Please let me know if this is not a appropriate forum to push such
>>>>> questions and if there are other mailing list that I could use coz in
>>>>> the coming days I am sure to send in a lot of email traffic.
>>>>
>>>> You could also try the main Python-language mailing list
>>>> ([hidden email]) -- there are more people on the list and you
>>>> might get quicker responses to questions :-)
>>>>
>>>> Hope that helps,
>>>> Rami
>>>>
>>>> --
>>>> Rami Chowdhury
>>>> "Never assume malice when stupidity will suffice." -- Hanlon's Razor
>>>> +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544
>>>>
>>>
>> _______________________________________________
>> Baypiggies mailing list
>> [hidden email]
>> To change your subscription options or unsubscribe:
>> http://mail.python.org/mailman/listinfo/baypiggies
>>
>
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: importing variables into python namespace using argparse module

Simeon Franklin
In reply to this post by Abhishek Pratap
Shiqi is correct - because you specified that the argument was of type file argparse already opened the file for you. If you want to handle file opening yourself specify str arguments instead.

Also - because you specified nargs the resulting variable is a list, even if nargs=1. Just leave nargs off of the add_argument call if you only want one argument. You can print the args variable when you execute the script to see what its contents look like. I'd also strongly recommend Doug Hellman's Python Module of the Week as a good source of tutorial style documentation. The argparse chapter is at http://www.doughellmann.com/PyMOTW/argparse/

-regards
Simeon Franklin
209 846-2151

On Wed, Sep 7, 2011 at 3:15 PM, Abhishek Pratap <[hidden email]> wrote:
Hi Eric

Sorry I did not provide the full info. Here is the exact code with
full traceback at the end.


import sys
sys.path.append('/house/homedirs/a/apratap/lib/python');
import argparse



def read_file(file):
   """Read the first same and create a dictionary
   """
   with open(file,'r') as fh1:
       for line in fh1:
           line.strip()
           temp = line.split("\t")
           print 'header : %s \t flag : %s' % (temp[0],temp[1])




def compare_with_second_sam_file(file):
   pass



parser = argparse.ArgumentParser(description='Process some integers')

parser.add_argument('--sam_file_1', nargs=1 , type=file,
required=True,  help='Name of sam file 1')
parser.add_argument('--sam_file_2', nargs=1 , type=file,
required=True,  help='Name of sam file 2')

args=parser.parse_args()

sam_file_1 = args.sam_file_1
sam_file_2 = args.sam_file_2

print 'sam _file_1 has the value %s' % sam_file_1
print 'type of var %s' % type(sam_file_1)

read_file(sam_file_1)


###
Traceback
###


sam _file_1 has the value [<open file '1.sam', mode 'r' at 0x237db58>]
type of var <type 'list'>
Traceback (most recent call last):
 File "/house/homedirs/a/apratap/dev/eclipse_workspace/python_scripts/src/compare_read_names_in_sam_files.py",
line 41, in <module>
   read_file(sam_file_1)
 File "/house/homedirs/a/apratap/dev/eclipse_workspace/python_scripts/src/compare_read_names_in_sam_files.py",
line 13, in read_file
   with open(file,'r') as fh1:
TypeError: coercing to Unicode: need string or buffer, list found





On Wed, Sep 7, 2011 at 3:08 PM, Eric Walstad <[hidden email]> wrote:
> Hi Abhishek,
>
> In the future please include the full error traceback.  It includes
> information that helps in debugging.
>
> It looks like your sam_file_1 variable contains a list when a string
> is expected.  You could try printing the value of that variable before
> passing it to your read_file function to see what it contains.  You've
> changed your code and didn't include all the changes.
>
> Eric.
>
>
> On Wed, Sep 7, 2011 at 2:56 PM, Abhishek Pratap <[hidden email]> wrote:
>> Guys
>>
>> I think I am stuck again while trying to open a file. It looks like a
>> type casting error but not sure.
>>
>> ##Error:
>> TypeError: coercing to Unicode: need string or buffer, list found
>>
>>
>> #code
>>
>> def read_file(file):
>>    """Read the first same and create a dictionary
>>    """
>>    fh1 = open(file,'r')
>>    for line in fh1:
>>        print line
>>
>>
>> sam_file_1 = args.sam_file_1
>> sam_file_2 = args.sam_file_2
>>
>> read_file(sam_file_1)
>>
>>
>> -Abhi
>
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies


_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: importing variables into python namespace using argparse module

Simeon Franklin
In reply to this post by Abhishek Pratap


On Wed, Sep 7, 2011 at 3:34 PM, Abhishek Pratap <[hidden email]> wrote:
**Here is what I dont understand.**

Does this mean all the arguments returned by parser.parse_args() are a
list even if they have one variable. May be I am missing a concept
here.


Yes - but only if you specify the nargs argument when creating the argument.

-regards
Simeon Franklin
209 846-2151

_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: importing variables into python namespace using argparse module

Aahz
In reply to this post by Abhishek Pratap
On Wed, Sep 07, 2011, Abhishek Pratap wrote:
>
> def read_file(file):
>     """Read the first same and create a dictionary
>     """
>     fh1 = open(file,'r')
>     for line in fh1:
>         print line

Side note: you should avoid naming your variables the same as existing
built-in ones:

>>> file
<type 'file'>

Also please read PEP8 to learn how to correctly format your code:

http://www.python.org/dev/peps/pep-0008/
--
Aahz ([hidden email])           <*>         http://www.pythoncraft.com/

"If you don't know what your program is supposed to do, you'd better not
start writing it."  --Dijkstra
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies