SyntaxError: unexpected EOF while parsing -Python - python

I am using the concept of priority scheduling to create a program that should take two inputs, number of incoming packets, and the priority string for the incoming packets. For example, for input “4” and “HLHH'', the order of outgoing packets should be ``0 2 1 3''.
When i run the program i keep getting this error:
Input:4 HLHH
Traceback (most recent call last):
File "schedule.py", line 71, in <module>
main()
File "schedule.py", line 62, in main
inputStr=input("Input:")
File "<string>", line 1
4 HLHH
^
SyntaxError: unexpected EOF while parsing
Here is my source code :
import re
#Used to set the priorities for initial buffer
def setPriorityForBuffer(priorityStr,priorityList):
counter=0
outgoingPkt=[]
#Set priority for H first
for i in range(0,3):
if priorityStr[i]=='H':
outgoingPkt.append(priorityList[counter])
counter+=1
else:
outgoingPkt.append('NA')
#Set priority for L next
for i in range(0,3):
if outgoingPkt[i]=='NA':
outgoingPkt[i]=priorityList[counter]
counter+=1
return outgoingPkt
def getPriority(numPackets,priorityStr):
outputStr = " "
outgoingPkt=[]
priorityList=[]
for i in range(0,numPackets):
priorityList.append(str(i))
if numPackets<=0:
return 'Wrong input: the number of packets must be greater than 0.'
elif numPackets!=len(priorityStr):
return 'Wrong input: the number of packets is wrong.'
else:
pattern = re.compile('^[LH]+$')
if not re.search(pattern, priorityStr):
return 'Wrong input: the priority must be H or L'
if numPackets==3:
outgoingPkt=setPriorityForBuffer(priorityStr,priorityList)
elif numPackets>3:
#For first 3 packets
outgoingPkt=setPriorityForBuffer(priorityStr,priorityList)
#For remaining packets
counter=3
#Set priority for H first
for i in range(3,len(priorityStr)):
if priorityStr[i]=='H':
outgoingPkt.append(priorityList[counter])
counter+=1
else:
outgoingPkt.append('NA')
#Set priority for L next
for i in range(3,len(priorityStr)):
if outgoingPkt[i]=='NA':
outgoingPkt[i]=priorityList[counter]
counter+=1
for i in outgoingPkt:
outputStr=outputStr+i+" "
return outputStr
def main():
#Get input from user
inputStr=input("Input:")
inputList=inputStr.split(" ")
numPackets=int(inputList[0])
priorityStr=inputList[1]
#Call function and print outgoingpacket string
print("Output:",getPriority(numPackets,priorityStr))
#Driver code
main()
any help is appreciated thanks

Related

error using pipes in python using os.pipe() and using os.fork()

So I made a simple piece of code to add 1 to a value. I create two process and I am creating a pipe for storage the information beetween iterations. In the first cycle it gives the correct value but in the second iterration it gives two errors.
My code:
def processMain():
number = 0
r, w = os.pipe()
for _ in range(2):
pid = os.fork()
if pid == 0:
number = int(number)
number += 1
number = str(number)
os.close(r)
w = os.fdopen(w, "w")
w.write(number)
print("write")
sys.exit(0)
else:
os.wait()
os.close(w)
r = os.fdopen(r)
number = r.read()
print("read")
print(number)
I excute the function and it gives me this results:
write
read
Traceback (most recent call last):
File "/home/aluno-di/area-de-aluno/SO/projeto/grupoXX/tests.py", line 31, in <module>
processMain()
File "/home/aluno-di/area-de-aluno/SO/projeto/grupoXX/tests.py", line 15, in processMain
os.close(r)
TypeError: '_io.TextIOWrapper' object cannot be interpreted as an integer
Traceback (most recent call last):
File "/home/aluno-di/area-de-aluno/SO/projeto/grupoXX/tests.py", line 31, in <module>
processMain()
File "/home/aluno-di/area-de-aluno/SO/projeto/grupoXX/tests.py", line 24, in processMain
os.close(w)
OSError: [Errno 9] Bad file descriptor
I don't understand what I am doing wrong or what I am not doing that I need to do to this work.
This should work. Note that number needs to be a shared memory value if you want the incrementing done by the child process reflected in the main process:
import os
from multiprocessing import Value, Pipe
import sys
def processMain():
number = Value('i', 0)
r, w = Pipe(duplex=False)
for _ in range(2):
pid = os.fork()
if pid == 0:
number.value += 1
n = number.value
w.send(n)
sys.exit(0)
else:
n = r.recv()
print("read", n)
os.waitid(os.P_PID, pid, os.WEXITED)
processMain()
Prints:
read 1
read 2
Re-assigning r = os.fdopen(r) is problematic here because the return value of os.fdopen is a TextIOWrapper, while the functions that use r require the value to be an integer. The first time you run the loop it works just fine because r is assigned to the first return from os.pipe(), however, then on it's converted to the TextIOWrapper where it will fail for both os.fdopen() and os.close()
Also, depending on the return value of r.read(), it's possible to get a None value, causing the program to fail when you try to run number = int(number)

EOFError: EOF when reading a line - in tic-tae-toe - Python

during tests it's showing the error message:
Traceback (most recent call last):
File "tictactoe/tictactoe.py", line 10, in <module>
coordinates = input("Enter the coordinates: ")
EOFError: EOF when reading a line
I can't image what is causing this problem. The code is:
c = True
while c:
coordinates = input("Enter the coordinates: ") # <-- line 10 is this one
if coordinates == '':
print("Coordinates should be from 1 to 3!")
continue
elif len(coordinates) < 3 or len(coordinates) > 3:
print("Coordinates should be from 1 to 3!")
continue
Thanks for support
Your test probably runs the code non-interactively. If you are creating the test in Python, try using subprocess.PIPE so that you can communicate with the subprocess. Without a valid stdin, the program will throw this error, which (should) cause your test to fail.

Creating a Python VM but my program ends in a IndexError exception

I am trying to make a Python VM but for some reason I am getting errors when attempting to run my program.
Here is my code:
import sys
OP_EOP = "00"
OP_EOI = "01"
OP_PUSH = "02"
OP_POP = "03"
OP_PRINT = "04"
OP_ADD = "05"
OP_SUB = "06"
def load_program(argv):
f = open(argv)
lines = f.read().replace("\n", " ")
lines = lines.split(" ")
f.close()
return lines
def DO_EOI():
print "EOI"
def DO_PUSH():
print "PUSH"
def DO_POP():
print "POP"
def DO_PRINT():
print "PRINT"
def DO_ADD():
print "ADD"
def DO_SUB():
print "SUB"
def execute_program(l):
loop = 1
i = 0
while loop:
instruction = l[i]
if instruction == OP_EOP:
loop = 0
print "EOP"
elif instruction == OP_EOI:
DO_EOI()
elif instruction == OP_PUSH:
DO_PUSH()
elif instruction == OP_POP:
DO_POP()
elif instruction == OP_PRINT:
DO_PRINT()
elif instruction == OP_ADD:
DO_ADD()
elif instruction == OP_SUB:
DO_SUB()
i += 1
def run_program(argv):
l = load_program(argv)
execute_program(l)
def main(argv):
run_program(argv[1])
return 0
def target(*args):
return main, None
if __name__ == '__main__':
main(sys.argv)
When I run my program with Aafile called hello.vm that has this:
02 000A 01 04
(the 000A should be ignored), I get this output (and errors)
PUSH
EOI
PRINT
Traceback (most recent call last):
File "vm.py", line 70, in <module>
main(sys.argv)
File "vm.py", line 63, in main
run_program(argv[1])
File "vm.py", line 60, in run_program
execute_program(l)
File "vm.py", line 40, in execute_program
instruction = l[i]
IndexError: list index out of range
My desired output is this:
PUSH
EOI
PRINT
EOP
Your program does not include a 00 EOP instruction. Your code expects that instruction.
Your execute_program() function expects to execute the program in an infinite loop; it starts at instruction 0 (in the i variable), and will forever increment i until the EOP instruction, at which point loop is set to 0 and the while loop: test stops being true.
So without a EOP, you run out of instructions and you get your exception, because loop = 0 is never executed.
You can remove the need for a mandatory EOP instruction by also testing if your have more instructions:
while loop and i < len(l):

Python Class variable increments higher than the # of iterations

I'm a bit stumped on this one, and I'm sure it has to due with my lack of experience in Python OOP and how classes work, or how the new string formatting is functioning. Posting here so that others can reference if they have a similar issue.
The problem: I use the following Class as a generator in a loop to read in a file and parse it line by line. Each line of interest increments the self.reads_total variable by one, and if that line meets certain criteria, it increments the self.reads_mapping variable by 1. At the end (just before the StopIteration() call on the next() method, I output those two values to stdout. When I run this from the command line on a file with 52266374 lines, I get the following back:
Reads processed: 5220000029016 with both mates mapped out of 52263262 total reads
This output is generated just before the termination of the iteration by the following line of code:
print("{0} with both mates mapped out of {1} total reads\n".format(self.reads_mapping, self.reads_total))
The self.reads_total is outputting the correct # of iterations, but the self.reads_mapping is not.
Class object that is called by a simple for x in SamParser(infile) loop:
class SamParser:
"""This object takes as input a SAM file path and constructs an iterable that outputs
sequence information. Only one line will be held in memory at a time using this method.
"""
def __init__(self, filepath):
"""
constructor
#param filepath: filepath to the input raw SAM file.
"""
if os.path.exists(filepath): # if file is a file, read from the file
self.sam_file = str(filepath)
self.stdin = False
elif not sys.stdin.isatty(): # else read from standard in
self.stdin = True
else:
raise ValueError("Parameter filepath must be a SAM file")
self.current_line = None
self.reads_mapping = 0
self.reads_total = 0
# Allowable bitflags for SAM file -> reads with both mates mapping, regardless of other flags
self.true_flags = (99, 147, 83, 163, 67, 131, 115, 179, 81, 161, 97, 145, 65, 129, 113, 177)
def __iter__(self):
return self
def _iterate(self):
# Skip all leading whitespace
while True:
if self.stdin:
sam_line = sys.stdin.readline() # read from stdin
else:
sam_line = self.sam_file.readline() # read from file
if not sam_line:
return # End of file
if sam_line.startswith("#SQ"): # these lines contain refseq length information
temp = sam_line.split()
return temp[1][3:], temp[2][3:]
elif sam_line[0] != "#": # these lines are the actual reads
self.reads_total += 1
if self.reads_total % 100000 == 0: # update the counter on stdout every 100000 reads
sys.stdout.write("\rReads processed: {}".format(self.reads_total))
sys.stdout.flush()
temp = sam_line.split()
if int(temp[1]) in self.true_flags and temp[2] is not "*" and int(temp[3]) is not 0:
self.reads_mapping += 1
return temp[1], temp[2], temp[3], temp[9]
self.sam_file.close() # catch all in case this line is reached
assert False, "Should not reach this line"
def next(self):
if not self.stdin and type(self.sam_file) is str: # only open file here if sam_file is a str and not file
self.sam_file = open(self.sam_file, "r")
value = self._iterate()
if not value: # close file on EOF
if not self.stdin:
self.sam_file.close()
print("{0} with both mates mapped out of {1} total reads\n".format(self.reads_mapping, self.reads_total))
raise StopIteration()
else:
return value
The full script can be found here if you need more context: https://github.com/lakinsm/file-parsing/blob/master/amr_skewness.py
Simple string formatting mistake: the script is designed to update the line count in place on stdout, and I didn't include a newline at the beginning of the next line, so it was overwriting the last line output, which included the large read count from the previous total. The true output should be:
Reads processed: 52200000
29016 with both mates mapped out of 52263262 total reads
Plots generated: 310
In any case, the script is useful for SAM parsing if anyone stumbles upon this in the future. Don't make my same mistake.

Python ValueError: chr() arg not in range(256)

So I am learning python and redoing some old projects. This project involves taking in a dictionary and a message to be translated from the command line, and translating the message. (For example: "btw, hello how r u" would be translated to "by the way, hello how are you".
We are using a scanner supplied by the professor to read in tokens and strings. If necessary I can post it here too. Heres my error:
Nathans-Air-4:py1 Nathan$ python translate.py test.xlt test.msg
Traceback (most recent call last):
File "translate.py", line 26, in <module>
main()
File "translate.py", line 13, in main
dictionary,count = makeDictionary(commandDict)
File "/Users/Nathan/cs150/extra/py1/support.py", line 12, in makeDictionary
string = s.readstring()
File "/Users/Nathan/cs150/extra/py1/scanner.py", line 105, in readstring
return self._getString()
File "/Users/Nathan/cs150/extra/py1/scanner.py", line 251, in _getString
if (delimiter == chr(0x2018)):
ValueError: chr() arg not in range(256)
Heres my main translate.py file:
from support import *
from scanner import *
import sys
def main():
arguments = len(sys.argv)
if arguments != 3:
print'Need two arguments!\n'
exit(1)
commandDict = sys.argv[1]
commandMessage = sys.argv[2]
dictionary,count = makeDictionary(commandDict)
message,messageCount = makeMessage(commandMessage)
print(dictionary)
print(message)
i = 0
while count < messageCount:
translation = translate(message[i],dictionary,messageCount)
print(translation)
count = count + 1
i = i +1
main()
And here is my support.py file I am using...
from scanner import *
def makeDictionary(filename):
fp = open(filename,"r")
s = Scanner(filename)
lyst = []
token = s.readtoken()
count = 0
while (token != ""):
lyst.append(token)
string = s.readstring()
count = count+1
lyst.append(string)
token = s.readtoken()
return lyst,count
def translate(word,dictionary,count):
i = 0
while i != count:
if word == dictionary[i]:
return dictionary[i+1]
i = i+1
else:
return word
i = i+1
return 0
def makeMessage(filename):
fp = open(filename,"r")
s = Scanner(filename)
lyst2 = []
string = s.readtoken()
count = 0
while (string != ""):
lyst2.append(string)
string = s.readtoken()
count = count + 1
return lyst2,count
Does anyone know whats going on here? I've looked through several times and i dont know why readString is throwing this error... Its probably something stupid i missed
chr(0x2018) will work if you use Python 3.
You have code that's written for Python 3 but you run it with Python 2. In Python 2 chr will give you a one character string in the ASCII range. This is an 8-bit string, so the maximum parameter value for chris 255. In Python 3 you'll get a unicode character and unicode code points can go up to much higher values.
The issue is that the character you're converting using chr isn't within the range accepted (range(256)). The value 0x2018 in decimal is 8216.
Check out unichr, and also see chr.

Categories

Resources