Python function with **kwargs Question

2006-01-05 Thread Khoa Nguyen
I would like to pass some keyword with special character to a foo(**kwargs) function, but it doesn't workdef foo(**kwargs):  print kwargsThis doesn't work:foo(a-special-keyword=5)How do I tell Python to treat '-' as a normal character but not part of an _expression_?
Thanks,Khoa 
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Python function with **kwargs Question

2006-01-05 Thread Khoa Nguyen
Thanks for your responses. I guess the foo(**{'x-y':3}) is ugly but will do the trick Cheers,Khoa On 1/5/06, Mike C. Fletcher <
[EMAIL PROTECTED]> wrote:Christian Tismer wrote:>Khoa Nguyen wrote:
>>>>I would like to pass some keyword with special character to a>>foo(**kwargs) function, but it doesn't work>>>>def foo(**kwargs):>>  print kwargs>>
>>>>This doesn't work:>>>>foo(a-special-keyword=5)>>>>How do I tell Python to treat '-' as a normal character but not part of>>an _expression_?>>
>>>>By changing the parser :-)>>Oh, you py-py guys, always thinking you have to re-implement Python ;)>Keywords are limited to obey Python syntax.>>Sure, but you can do something like this:
 >>> def x( **named ):... print named... >>> x( **{'some-var-with- weird chars': True } ){'some-var-with- weird chars': True}That is, for the OP (who is unlikely to rewrite the parser), the
solution could be as simple as treating the keyword as a piece of dataand applying it to the function.Have fun,Mike--  Mike C. Fletcher
  Designer, VR Plumber, Coder  http://www.vrplumber.com  http://blog.vrplumber.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Process files in order

2006-07-27 Thread Khoa Nguyen
Hi,

I have a requirement to process all files in a directory in
chronological order. The os.listdir() function, however, lists the
files in random order. Is there a similar function in Python that
allows me to specify the listing order (like ls -t for example)?

Thanks,
Khoa
-- 
http://mail.python.org/mailman/listinfo/python-list


Pyparsing: Grammar Suggestion

2006-05-17 Thread Khoa Nguyen
I am trying to come up with a grammar that describes the following:

record = f1,f2,...,fn END_RECORD
All the f(i) has to be in that order.
Any f(i) can be absent (e.g. f1,,f3,f4,,f6 END_RECORD)
Number of f(i)'s can vary. For example, the followings are allowed:
f1,f2 END_RECORD
f1,f2,,f4,,f6 END_RECORD

Any suggestions?

Thanks,
Khoa
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pyparsing: Grammar Suggestion

2006-05-17 Thread Khoa Nguyen
> record = f1,f2,...,fn END_RECORD
> All the f(i) has to be in that order.
> Any f(i) can be absent (e.g. f1,,f3,f4,,f6 END_RECORD)
> Number of f(i)'s can vary. For example, the followings are allowed:
> f1,f2 END_RECORD
> f1,f2,,f4,,f6 END_RECORD
>
> Any suggestions?
>

>
> 
> pyparsing includes a built-in expression, commaSeparatedList, for just such
> a case.  Here is a simple pyparsing program to crack your input text:
>
>
> data = """f1,f2,f3,f4,f5,f6 END_RECORD
> f1,f2 END_RECORD
> f1,f2,,f4,,f6 END_RECORD"""
>
> from pyparsing import commaSeparatedList
>
> for tokens,start,end in commaSeparatedList.scanString(data):
> print tokens
>
>
> This returns:
> ['f1', 'f2', 'f3', 'f4', 'f5', 'f6 END_RECORD']
> ['f1', 'f2 END_RECORD']
> ['f1', 'f2', '', 'f4', '', 'f6 END_RECORD']
>
> Note that consecutive commas in the input return empty strings at the
> corresponding places in the results.
>
> Unfortunately, commaSeparatedList embeds its own definition of what is
> allowed between commas, so the last field looks like it always has
> END_RECORD added to the end.  We could copy the definition of
> commaSeparatedList and exclude this, but it is simpler just to add a parse
> action to commaSeparatedList, to remove END_RECORD from the -1'th list
> element:
>
> def stripEND_RECORD(s,l,t):
> last = t[-1]
> if last.endswith("END_RECORD"):
> # return a copy of t with last element trimmed of "END_RECORD"
> return t[:-1] + [last[:-(len("END_RECORD"))].rstrip()]
>
> commaSeparatedList.setParseAction(stripEND_RECORD)
>
>
> for tokens,start,end in commaSeparatedList.scanString(data):
> print tokens
>
>
> This returns:
>
> ['f1', 'f2', 'f3', 'f4', 'f5', 'f6']
> ['f1', 'f2']
> ['f1', 'f2', '', 'f4', '', 'f6']
>

Thanks for your reply. This looks promising, but I have a few more questions:
1. If f(i) is non-terminal (e.g f(i) is another grammar expression),
how would I adapt your idea to a more generic way?
2. The field delimiter is not always ',' in my case. So I guess I'll
have to use delimtedList instead?

Thanks again,
Khoa
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pyparsing: Grammar Suggestion. 2nd thought

2006-05-17 Thread Khoa Nguyen
>
> for tokens,start,end in commaSeparatedList.scanString(data):
> print tokens
>
>
> This returns:
>
> ['f1', 'f2', 'f3', 'f4', 'f5', 'f6']
> ['f1', 'f2']
> ['f1', 'f2', '', 'f4', '', 'f6']
>

Thanks for your reply. This looks promising, but I have a few more questions:
1. If f(i) is non-terminal (e.g f(i) is another grammar expression),
how would I adapt your idea to a more generic way?
2. The field delimiter is not always ',' in my case. So I guess I'll
have to use delimtedList instead?

Thanks again,
Khoa


On 2nd thought, I don't think this will check for the correct order of
the fields. For example, the following would be incorrectly accepted:

f1,f5,f2 END_RECORD

Thanks,
Khoa
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python-list Digest, Vol 32, Issue 300

2006-05-17 Thread Khoa Nguyen
> Optional(f1SpecificFormat) + "," + Optional(f2SpecificFormat) + "," + ...
> and so on.

Thanks for your input again. This is exactly what I am trying.  The
only thing is it can get ugly if the number of fields are many -- I
have about 61 fields right now. Just wondering if  there is a
"cleaner" approach.

Thanks,
Khoa

==
from pyparsing import *

DELIM = Suppress('&')
RECORD_END = Suppress(';')
f1 = Literal('a')
f2 = Literal('b')
f3 = Word(alphanums+'_-.')

common = f1
extended = Optional(DELIM + Optional(f2))  \
   + Optional(DELIM + Optional(f3))

g = common + Optional(extended) + RECORD_END

def parse(record):
result = g.parseString(record)
print result

if __name__=='__main__':
good = ['a;', 'a&&c-123;', 'a&b;', 'a&b&c;', 'a&b&c-123-af;']
bad = [ ';',# required field missing
'a',# RECORD_END missing
'b&c;', # required field missing
'a&c&b;',   # Incorrect order
'x123&a&b;' # Incorrect order
  ]
for r in good + bad:
try:
parse(r)
except ParseException, err:
print err.line
print " "*(err.column-1) + "^"
print err
continue
===
-- 
http://mail.python.org/mailman/listinfo/python-list


Pyparsing: Specify grammar at run time

2006-05-17 Thread Khoa Nguyen
I run into another issue with my grammar:

My input record contains a common part and an extended part. Based on
the value of the common part, the extended part will be different. So,
I am thinking of parsing the common part first and check the common's
value and then parse again for the rest of the record. How do I tell
pyparsing to start the 2nd parse at the exact location where the 1st
parse left off?

##
from pyparsing import *

common = Word('aA').setResultsName('value')
extend1 = Word('b')
extend2 = Word('c')

result = common.parseString(record)
if result.value == 'a':
   result1 = extend1.parseString(???)
else:
   result2 = extend2.parseString(???)
##





Thanks,
Khoa
-- 
http://mail.python.org/mailman/listinfo/python-list