Re: How to get inputs for a python program that run from another python program
On Jun 11, 7:47 am, pradeep nair <[EMAIL PROTECTED]> wrote:
> I would like to know how to pass keyboard input for a python script
> which is ran by another script.
>
> for eg:
>
> hello1.py:
>
> import os
>
> if __name__=='__main__':
>
> print "I will call this other program called hello.py"
> os.system("python hello.py")
> print "hello1.py"
>
> hello.py:
>
> import os
>
> if __name__=='__main__':
>
> print "press ENTER to display"
> #code wer if the user hits enter
> print "hello"
> #else the user hits any other keyboard button:
> sys.exit()
>
> now wen i run hello1.py,i want the some function or utility in
> hello1.py that can pass the keyboard i/p to hello.py .
Using pexpect: http://pexpect.sourceforge.net/
hello1.py:
import pexpect
if __name__=='__main__':
print "I will call this other program called hello.py"
child = pexpect.spawn('python hello.py')
child.expect ('\n')
print "Received from hello.py: ", child.before
entered = raw_input("> ")
child.sendline (entered)
child.expect ('\n')
print "Received from hello.py: ", child.before
--
http://mail.python.org/mailman/listinfo/python-list
pyparser and recursion problem
Hi,
Using pyparser, I'm trying to parse a string like this:
:Start: first SECOND THIRD :SECOND: second1 | second2 :THIRD: third1 |
FOURTH :FOURTH: fourth1 | fourth2
I want the parser to do the following:
1) Get the text for the :Start: label e.g ('first SECOND THIRD')
2) Do nothing with the lower-case words e.g ('first')
3) For each upper-case word find the corresponding entries, and
replace the word
with these entries (the '|' indicates separate records)
e.g. for 'SECOND', replace the word with ("second1", "second2")
4 Do this recursively, because each item in '3' can have upper-case
words
I can do this - but not within pyparser. I had to write a recursive
function to do it. I would like to do it within pyparser however.
I'm pretty sure I have to use the Forward() function along with a few
setResultsName() - but after reading the documentation, many examples,
and trying for hours, I'm still totally lost. Please help!
Here is the program I have so far:
#!/usr/bin/python
from pyparsing import Word, Optional, OneOrMore, Group, alphas,
alphanums, Suppress, Dict
import string
def allIn( as, members ):
"Tests that all elements of as are in members"""
for a in as:
if a not in members:
return False
return True
def allUpper( as ):
"""Tests that all strings in as are uppercase"""
return allIn( as, string.uppercase )
def getItems(myArray, myDict):
"""Recursively get the items for each CAPITAL word"""
myElements=[]
for element in myArray:
myWords=[]
for word in element:
if allUpper(word):
items = getItems(myDict[word], myDict)
myWords.append(items)
else:
myWords.append(word)
myElements.append(myWords)
return myElements
testData = """
:Start: first SECOND THIRD fourth FIFTH
:SECOND: second1_1 second1_2 | second2 | second3
:THIRD: third1 third2 | SIXTH
:FIFTH: fifth1 | SEVENTH
:SIXTH: sixth1_1 sixth1_2 | sixth2
:SEVENTH: EIGHTH | seventh1
:EIGHTH: eighth1 | eighth2
"""
label = Suppress(":") + Word(alphas + "_") + Suppress(":")
words = Group(OneOrMore(Word(alphanums + "_"))) +
Suppress(Optional("|"))
data = ~label + OneOrMore(words)
line = Group(label + data)
doc = Dict(OneOrMore(line))
res = doc.parseString(testData)
# This prints out what pyparser gives us
for line in res:
print line
print
print
startString = res["Start"]
items = getItems([startString], res)[0]
# This prints out what we want
for line in items:
print line
--
http://mail.python.org/mailman/listinfo/python-list
Re: pyparser and recursion problem
Hey,
Thanks Neil and Paul!
After reading Neil's advice I started playing around with the
setParseAction method, and then I found Paul's script
'macroExpander.py' (http://pyparsing.wikispaces.com/space/showimage/
macroExpander.py).
With only a few modifications to macroExpander.py (and reversing my
string!) I had an almost complete answer of:
first [second1_1 second1_2 | second2 | second3 ] [third1 third2 |
[sixth1_1 sixth1_2 | sixth2 ] ] | fourth [fifth1 | [[eighth1 |
eighth2] | seventh1]]
I was in the process of tyding this up so that I could do an eval() to
get the array when I saw Paul's answer (thanks!)
Here is macroExpander.py with my minimal changes:
#!/usr/bin/python
from pyparsing import *
# define the structure of a macro definition (the empty term is used
# to advance to the next non-whitespace character)
label = Suppress(":") + Word(alphas + "_").setResultsName("macro") +
Suppress(":")
values = restOfLine.setResultsName("value")
macroDef = label + empty + values
# define a placeholder for defined macros - initially nothing
macroExpr = Forward()
# global dictionary for macro definitions
macros = {}
# parse action for macro definitions
def processMacroDefn(s,l,t):
macroVal = macroExpander.transformString(t.value)
macros[t.macro] = macroVal
macroExpr << MatchFirst( map(Keyword,macros.keys()) )
# parse action to replace macro references with their respective
definition
def processMacroRef(s,l,t):
return '[' + macros[t[0]] +']'
# attach parse actions to expressions
macroExpr.setParseAction(processMacroRef)
macroDef.setParseAction(processMacroDefn)
# define pattern for scanning through the input string
macroExpander = macroExpr | macroDef
# test macro substitution using transformString
testString = """
:EIGHTH: eighth1 | eighth2
:SEVENTH: EIGHTH | seventh1
:SIXTH: sixth1_1 sixth1_2 | sixth2
:FIFTH: fifth1 | SEVENTH
:THIRD: third1 third2 | SIXTH
:SECOND: second1_1 second1_2 | second2 | second3
:Start: first SECOND THIRD | fourth FIFTH
"""
macroExpander.transformString(testString)
print macros['Start']
--
http://mail.python.org/mailman/listinfo/python-list
Re: pyparser and recursion problem
Hey,
Thanks for the further explanations. I'm going to play around more
with the 'recursive grammar' and 'parse-time dynamic grammar element'
stuff so that I understand it a bit better.
I liked the changes you suggested. The alternative grammar definitely
makes the code easier to understand, plus the 'delimitedList' function
is very handy. I can see the usefulness of 'Combine' but it's not
needed here.
This didn't work though:
print res["Start"][0].asList()
But that's ok, because I was more than happy to do:
myArray = [res["Start"]][0]
and have the whole thing as a list. Very nice indeed :)
Thanks again for your help!
For anyone thats interested, here is the updated code (my original
code with Paul's enhancements)
#!/usr/bin/python
from pyparsing import Word, OneOrMore, Group, alphas, \
alphanums, Suppress, Dict, delimitedList, ParseResults
import string
def allIn( as, members ):
"Tests that all elements of as are in members"""
for a in as:
if a not in members:
return False
return True
def allUpper( as ):
"""Tests that all strings in as are uppercase"""
return allIn( as, string.uppercase )
# recursive reference fixer-upper
def fixupRefsRecursive(tokens, lookup):
if isinstance(tokens, ParseResults):
subs = [ fixupRefsRecursive(t, lookup) for t in tokens ]
tokens = ParseResults( subs )
else:
if tokens.isupper():
tokens = fixupRefsRecursive(lookup[tokens], lookup)
return tokens
def fixupRefs(tokens):
tokens["Start"] = fixupRefsRecursive( tokens["Start"], tokens )
testData = """
:Start: first SECOND THIRD fourth FIFTH
:SECOND: second1_1 second1_2 | second2 | second3
:THIRD: third1 third2 | SIXTH
:FIFTH: fifth1 | SEVENTH
:SIXTH: sixth1_1 sixth1_2 | sixth2
:SEVENTH: EIGHTH | seventh1
:EIGHTH: eighth1 | eighth2
"""
COLON = Suppress(":")
label = COLON + Word(alphas + "_") + COLON
entry = Word(alphanums + "_")
data = delimitedList( Group(OneOrMore(entry)), delim="|" )
line = Group(label + data)
doc = Dict(OneOrMore(line))
doc.setParseAction( fixupRefs )
res = doc.parseString(testData)
myArray = [res["Start"]][0]
print myArray
--
http://mail.python.org/mailman/listinfo/python-list
Re: Efficient Rank Ordering of Nested Lists
On Aug 2, 10:20 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > A naive approach to rank ordering (handling ties as well) of nested > lists may be accomplished via: > >def rankLists(nestedList): > def rankList(singleList): > sortedList = list(singleList) > sortedList.sort() > return map(sortedList.index, singleList) > return map(rankList, nestedList) > >>>> unranked = [ [ 1, 2, 3, 4, 5 ], [ 3, 1, 5, 2, 4 ], [ -1.1, 2.2, > 0, -1.1, 13 ] ] >>>> print rankLists(unranked) > >[[0, 1, 2, 3, 4], [2, 0, 4, 1, 3], [0, 3, 2, 0, 4]] > > This works nicely when the dimensions of the nested list are small. > It is slow when they are big. Can someone suggest a clever way to > speed it up? Isn't there something wrong with the ordering? Pablo's answers are: [ 1, 2, 3, 4, 5 ] == [0, 1, 2, 3, 4] correct [ 3, 1, 5, 2, 4 ] == [2, 0, 4, 1, 3] wrong? [ -1.1, 2.2, 0, -1.1, 13 ] == [0, 3, 2, 0, 4] wrong? Doing it in my head I get: [ 3, 1, 5, 2, 4 ] == [ 1, 3, 0, 4, 2 ] [ -1.1, 2.2, 0, -1.1, 13 ] == [0, 3, 2, 1, 4] What gives? Did I misunderstand what "rank ordering (handling ties as well)" means? -- http://mail.python.org/mailman/listinfo/python-list
'Advanced' list comprehension? query
Hi, I'm playing around with list comprehension, and I'm trying to find the most aesthetic way to do the following: I have two lists: noShowList = ['one', 'two', 'three'] myList = ['item one', 'item four', 'three item'] I want to show all the items from 'myList' that do not contain any of the strings in 'noShowList'. i.e. 'item four' I can do it like this: def inItem(noShowList, listitem): return [x for x in noShowList if x in listitem] print [x for x in myList if not inItem(noShowList, x)] and I can do it (horribly) with: print [x for x in myList if not (lambda y, z:[i for i in y if i in z]) (noShowList, x)] I can also print out the items that DO contain the 'noShowList' strings with: print [x for x in myList for y in noShowList if y in x] but I can't get the 'not' bit to work in the above line. Any ideas? Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Colored text
On Aug 13, 10:37 am, Neil Cerutti <[EMAIL PROTECTED]> wrote: > On 2007-08-13, Michael Bentley <[EMAIL PROTECTED]> wrote: > > > > > On Aug 12, 2007, at 7:05 PM, Rohan wrote: > >> Can some one tell me how do I get colored text. Say when I want to > >> write something in a text file , how do I get it colored. > > > You can use ANSI escape codes --http://en.wikipedia.org/wiki/ > > ANSI_escape_code: > > Unfortunately, most versions of Windows of the last 7 years > (2000, XP, probably Vista) don't support ANSI escape codes well > enough to work with Python. > > -- > Neil Cerutti > We have to play hard for the full 40 minutes. 48? Oh, that's right. --Mikki > Moore Check out these recipes: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/475116 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496901 -- http://mail.python.org/mailman/listinfo/python-list
creating and appending to a dictionary of a list of lists
Hey,
I started with this:
factByClass = {}
def update(key, x0, x1, x2, x3):
x = factByClass.setdefault(key, [ [], [], [], [] ])
x[0].append(x0)
x[1].append(x1)
x[2].append(x2)
x[3].append(x3)
update('one', 1, 2, 3, 4)
update('one', 5, 6, 7, 8)
update('two', 9, 10, 11, 12)
print factByClass
{'two': [[9], [10], [11], [12]], 'one': [[1, 5], [2, 6], [3, 7], [4,
8]]}
I then 'upgraded' to this:
def update(key, *args):
x = factByClass.setdefault(key, [[], [], [], [] ])
for i, v in enumerate(args):
x[i].append(v)
Is there a better way?
Cheers!
--
http://mail.python.org/mailman/listinfo/python-list
Re: creating and appending to a dictionary of a list of lists
On Aug 15, 8:08 am, Ant <[EMAIL PROTECTED]> wrote:
> On Aug 15, 3:30 am, [EMAIL PROTECTED] wrote:
>
> > Hey,
>
> > I started with this:
>
> > factByClass = {}
>
> ...
> > def update(key, *args):
> > x = factByClass.setdefault(key, [[], [], [], [] ])
> > for i, v in enumerate(args):
> > x[i].append(v)
>
> > Is there a better way?
>
> Well, the following is perhaps neater:
>
> >>> factByClass = defaultdict(lambda: [[],[],[],[]])
> >>> def update(key, *args):
>
> ... map(list.append, factByClass[key], args)
> ...>>> update('one', 1, 2, 3, 4)
> >>> update('one', 5, 6, 7, 8)
> >>> update('two', 9, 10, 11, 12)
>
> >>> print factByClass
>
> defaultdict( at 0x00F73430>, {'two': [[9], [1
> 0], [11], [12]], 'one': [[1, 5], [2, 6], [3, 7], [4, 8]]})
>
> It abuses the fact that list.append modifies the list in place -
> normally you would use map to get a new list object. In this case the
> new list returned by map is just a list of None's (since append
> returns None - a common idiom for functions that operate by side
> effect), and so is not used directly.
>
> --
> Ant...
>
> http://antroy.blogspot.com/
Nice. I like it. Thanks a lot!
--
http://mail.python.org/mailman/listinfo/python-list
Re: 'REPL' style IDE
How about embedding ipython in your script with: from IPython.Shell import IPShellEmbed ipshell = IPShellEmbed() ** your set up code ** ipshell() # this call anywhere in your program will start IPython see: http://ipython.scipy.org/doc/manual/node9.html#sec:embed Here are some ipython tips: http://ipython.scipy.org/doc/manual/node4.html Another option is to set up Wing so that it attaches to a process that is started externally (http://wingware.com/doc/debug/importing-the- debugger) So... in one window you can run your code that sets up everything and then displays the ipython CLI - and in the other window you have WingIDE where you can set your breakpoints and use the GUI. Is this what you're looking for? -- http://mail.python.org/mailman/listinfo/python-list
