Re: Trouble porting glob bash behavior with argparse to windows shell

2016-05-16 Thread Peter Otten
Sayth Renshaw wrote:

> On Wednesday, 4 May 2016 17:57:32 UTC+10, Sayth Renshaw  wrote:
>> Oops sorry noticed you did in the glob. Sorry squinting at phone.
>> 
>> Sayth
> 
> Hi
> 
> this seems to be causing me an error in my thinking as well as the
> program. I am creating a function GetArgs to take a path and file
> extension from the command line.
> 
> However I cannot call it effectively. I will clrify this is my function
> 
> import argparse
> import glob
> import os
> import sqlite3
> 
> 
> def GetArgs(parser):
> '''parse XML from command line'''
> parser.add_argument("path", nargs="+")
> parser.add_argument('-e', '--extension', default='',
> help='File extension to filter by.')
> args = parser.parse_args()
> 
> files = set()
> name_pattern = "*" + args.extension
> for path in args.path:
> files.update(glob.glob(os.path.join(path, name_pattern)))
> return files
> 
> Then later in program I am attempting to call it an a for statement.
> 
> filesToProcess = GetArgs()
> for meeting in filesToProcess:
> meetdata = [meeting.get(attr) for attr in meetattrs]
> cur.execute("insert into meetings values (" +
> ",".join(["%s"] * len(meetattrs)) + ")", meetdata)
> 
> 
> this fails as i would expect, however if I declare a list as the GetArgs()
> argument it fails as well.
> 
> Where my confusion is that I created the function to take arguments from
> the command line, so I don't have that variable to supply until executed.
> 
> Have i overbaked the cake?

The actual arguments are in sys.argv and will be implicitly accessed by the
parser.parse_args() method invocation. The problem is simply that you don't 
create an argparse.ArgumentParser() instance. I suggest that you do that 
inside the GetArgs() function:

def GetArgs(): # no arguments
parser = argparse.ArgumentParser()
# your current code below
parser.add_argument("path", nargs="+")
...

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Trouble porting glob bash behavior with argparse to windows shell

2016-05-15 Thread Sayth Renshaw
On Wednesday, 4 May 2016 17:57:32 UTC+10, Sayth Renshaw  wrote:
> Oops sorry noticed you did in the glob. Sorry squinting at phone. 
> 
> Sayth

Hi

this seems to be causing me an error in my thinking as well as the program. I 
am creating a function GetArgs to take a path and file extension from the 
command line. 

However I cannot call it effectively. I will clrify this is my function

import argparse
import glob
import os
import sqlite3


def GetArgs(parser):
'''parse XML from command line'''
parser.add_argument("path", nargs="+")
parser.add_argument('-e', '--extension', default='',
help='File extension to filter by.')
args = parser.parse_args()

files = set()
name_pattern = "*" + args.extension
for path in args.path:
files.update(glob.glob(os.path.join(path, name_pattern)))
return files

Then later in program I am attempting to call it an a for statement.

filesToProcess = GetArgs()
for meeting in filesToProcess:
meetdata = [meeting.get(attr) for attr in meetattrs]
cur.execute("insert into meetings values (" +
",".join(["%s"] * len(meetattrs)) + ")", meetdata)


this fails as i would expect, however if I declare a list as the GetArgs() 
argument it fails as well.

Where my confusion is that I created the function to take arguments from the 
command line, so I don't have that variable to supply until executed.

Have i overbaked the cake?

Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Trouble porting glob bash behavior with argparse to windows shell

2016-05-04 Thread Sayth Renshaw
Oops sorry noticed you did in the glob. Sorry squinting at phone. 

Sayth 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Trouble porting glob bash behavior with argparse to windows shell

2016-05-04 Thread Sayth Renshaw
Thank you Peter. 
I was starting to flail and thought my use of glob.glob was wrong. 

As an aside should I be using os.path to negate system inconsistency? 

Thanks 

Sayth 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Trouble porting glob bash behavior with argparse to windows shell

2016-05-03 Thread Terry Reedy

On 5/3/2016 4:55 PM, Sayth Renshaw wrote:

Is there something obvious to this I am doing wrong?

Sayth


Somethin happened so that I don't see what you did.  Fortunately, it did 
show up for Peter, between the '?' and name, so he could answer.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: Trouble porting glob bash behavior with argparse to windows shell

2016-05-03 Thread Peter Otten
Sayth Renshaw wrote:

> Is there something obvious to this I am doing wrong?

> parser.add_argument("path", nargs="+") 
 
The "+" implicitly turns args.path into a list

> files |= set(glob.glob(args.path + '/*' + args.extension))

so the glob() argument is evaluated as

list + str + str

Try

name_pattern = "*" + args.extension
for path in args.path:
files.update(glob.glob(os.path.join(path, name_pattern)))

-- 
https://mail.python.org/mailman/listinfo/python-list