I'm struggling to debug this but I'm afraid it's beyond my current understanding.
I created a simple script in python which purpose in life is to take as input a string (copy-pasted from google sheets) and turn it into a csv file that will be fit to upload to google calendar. It's hard-coded to fit the format of our shift plan at work. Suffice to say that exemplary input could be: MSLS-1Comp-offES-1..ES-22. The program then parses the input according to specified rules, prints parsed items as list and finally creates a csv file in a correct format.
Here is the problem:
The program runs perfectly everywhere I try it, except when I try running it by double clicking it on the desktop (Windows10). When running from CMD, IDLE or online, the program works and produces expected results without any errors. Other simple scripts I wrote, run from the desktop no problem. In this particular one, the program opens (as all the others, in python3.9), but at a particular line just exits (again, it doesn't happen when I run this program from the cmd, IDLE or online). I managed to find the place where the program 'crashes' by inserting print functions. It looks to me that for some reason the program exits/crashes on the line with io.open("shifts.csv", "w", encoding="utf-8-sig") as f:.
Is it something with the code? Then why does it run ok when run on the same system just using the command prompt? Is it something with how I have python set up on my computer? In which case why do others scripts I wrote run perfectly from the desktop? I'm really confused here. I would be very grateful for any suggestions. Thank you!
Here is the full code:
import subprocess
import sys
import io
year = "2021"
month = input("Please enter month in number format (e.g., '07' for June):\n")
shifts = input("Please paste your shifts:\n")
word = ''
shifts_list = []
for char in shifts: # iterate through the string
word = word + char # add the character to the word
if(char in ['1','2','3','.']): # check if the character at current iteration is one of the characters in the list
shifts_list.append(word) # add the word formed by now to the list
word = '' # re-initialize the word variable to an empty string so as to start the next word
if (char in ['S'] and word[-2] in ['M']):
shifts_list.append(word) # add the word formed by now to the list
word = '' # re-initialize the word variable to an empty string so as to start the next word
print(shifts_list)
headers = "Subject, Start Date, Start Time, End Date, End Time, Description, Private\n"
start_times = ["6:30", "7:30", "6:30", "9:00", "10:00"]
end_times = ["15:00", "16:00", "17:00", "17:30", "18:30"]
subjects = ["Early 🌼", "Office 👔", "10h 👷", "Middle 🌻", "Late 🦉"]
print(headers)
input("after this point the program exits")
with io.open("shifts.csv", "w", encoding="utf-8-sig") as f:
f.write(headers)
for day, shift in enumerate(shifts_list):
description = shift
date = str(day+1) + "." + month + "." + year
if shift == "Int-ES1":
print(subjects[0] + ', ' + date +', ' + start_times[0] + ', ' + date + ', ' + end_times[0] + ', ' + description + ', ' + "TRUE\n")
f.write(subjects[0] + ', ' + date +', ' + start_times[0] + ', ' + date + ', ' + end_times[0] + ', ' + description + ', ' + "TRUE\n")
if shift == "Int-ES2":
print(subjects[1] + ', ' + date +', ' + start_times[1] + ', ' + date + ', ' + end_times[1] + ', ' + description + ', ' + "TRUE\n")
f.write(subjects[1] + ', ' + date +', ' + start_times[1] + ', ' + date + ', ' + end_times[1] + ', ' + description + ', ' + "TRUE\n")
if shift == "IntES3":
print(subjects[2] + ', ' + date +', ' + start_times[2] + ', ' + date + ', ' + end_times[2] + ', ' + description + ', ' + "TRUE\n")
f.write(subjects[2] + ', ' + date +', ' + start_times[2] + ', ' + date + ', ' + end_times[2] + ', ' + description + ', ' + "TRUE\n")
if shift == "MS":
print(subjects[3] + ', ' + date +', ' + start_times[3] + ', ' + date + ', ' + end_times[3] + ', ' + description + ', ' + "TRUE\n")
f.write(subjects[3] + ', ' + date +', ' + start_times[3] + ', ' + date + ', ' + end_times[3] + ', ' + description + ', ' + "TRUE\n")
if shift == "Int-LS1":
print(subjects[4] + ', ' + date +', ' + start_times[4] + ', ' + date + ', ' + end_times[4] + ', ' + description + ', ' + "TRUE\n")
f.write(subjects[4] + ', ' + date +', ' + start_times[4] + ', ' + date + ', ' + end_times[4] + ', ' + description + ', ' + "TRUE\n")
print("Your shifts have been successfully saved to a CSV file. You can now import it to your calendar :)")
input("Press enter to exit...")
def open_folder(path):
if sys.platform == 'darwin':
subprocess.check_call(['open', '--', path])
elif sys.platform == 'linux2':
subprocess.check_call(['gnome-open', '--', path])
elif sys.platform == 'win64':
subprocess.check_call(['explorer', path])
open_folder(path=".") # open current directory
build mongoimport command
collection = OSM_FILE[:OSM_FILE.find('.')]
working_directory = '/Users/Anshuman Misra/Downloads/'
json_file = OSM_FILE + '.json'
mongoimport_cmd = 'mongoimport -h 127.0.0.1:27017 ' + \
'--db ' + db_name + \
' --collection ' + collection + \
' --file ' + working_directory + json_file
Before importing, drop collection if it exists (i.e. a re-run)
if collection in db.collection_names():
print 'Dropping collection: ' + collection
db[collection].drop()
Execute the command
print 'Executing: ' + mongoimport_cmd
subprocess.call(mongoimport_cmd.split(), shell=True)
Following is the code I am trying to run
device_editor_path = os.path.join(syntax_checker_path,'DeviceEditor.jar')
output_path = os.path.join(reviewdocs_path,'syntaxchecker_orig_output.txt')
output_path = '"%s"' % output_path # Need to do this because in case there is a space in output_path
# run syntax checker
cmd = 'java -jar' + ' ' + device_editor_path + ' ' + content_data_path + ' ' + event_source_name
if version == 'v2':
cmd = cmd + ' ' + '-v2'
final_cmd = cmd + ' ' + '>' + ' ' + output_path
# final_cmd_test = r'java -jar C:\TOOLS_UI\syntaxchecker\DeviceEditor.jar C:\Users\patela28\Perforce\content-dev\dev\envision\content\content-data\ symantecav -v2 > "C:\Users\patela28\Desktop\jira\ESU#105\Sprint_27\SMC-112\ReviewDocs&Checklist\syntaxchecker_orig_output.txt"'
print(final_cmd)
status = os.system(final_cmd)
The output of print(final_cmd) is
java -jar C:\TOOLS_UI\syntaxchecker\DeviceEditor.jar C:\Users\patela28\Perforce\content-dev\dev\envision\content\content-data\ symantecav -v2 > "C:\Users\patela28\Desktop\jira\ESU#105\Sprint_27\SMC-112\ReviewDocs&Checklist\syntaxchecker_orig_output.txt"
This command does run but the entire output shown on the command line and is not getting redirected to syntaxchecker_orig_output.txt.
When I copy paste the same above command on the command line it works perfectly and I get a syntaxchecker_orig_output.txt file at the location.
Not able to figure out why this is happening.
You have to start the command processor. Java won't parse for you the command line. The following should work:
device_editor_path = os.path.join(syntax_checker_path,'DeviceEditor.jar')
output_path = os.path.join(reviewdocs_path,'syntaxchecker_orig_output.txt')
output_path = '"%s"' % output_path # Need to do this because in case there is a space in output_path
# run syntax checker
cmd = 'cmd.exe /c java -jar' + ' ' + device_editor_path + ' ' + content_data_path + ' ' + event_source_name
if version == 'v2':
cmd = cmd + ' ' + '-v2'
final_cmd = cmd + ' ' + '>' + ' ' + output_path
# final_cmd_test = r'java -jar C:\TOOLS_UI\syntaxchecker\DeviceEditor.jar C:\Users\patela28\Perforce\content-dev\dev\envision\content\content-data\ symantecav -v2 > "C:\Users\patela28\Desktop\jira\ESU#105\Sprint_27\SMC-112\ReviewDocs&Checklist\syntaxchecker_orig_output.txt"'
print(final_cmd)
status = os.system(final_cmd)
Don't know the reason but changing
final_cmd = cmd + ' ' + '>' + ' ' + output_path
to
final_cmd = cmd + ' ' + '>' + output_path
worked for me.
I need to encrypt 3 .bin files which contain 2 keys for Diffie-Hellman. I have no clue how to do that, all I could think of was what I did in the following Python file. I have an example what the output should look like but my code doesn't seem to produce the right keys. The output file server.ini is used by a client to connect to a server.
import base64
fileList = [['game_key.bin', 'Game'], ['gate_key.bin', 'Gate'], ['auth_key.bin', 'Auth']]
iniList = []
for i in fileList:
file = open(i[0], 'rb')
n = list(file.read(64))
x = list(file.read(64))
file.close()
n.reverse()
x.reverse()
iniList.append(['Server.' + i[1] + '.N "' + base64.b64encode("".join(n)) + '"\n', 'Server.' + i[1] + '.X "' + base64.b64encode("".join(x)) + '"\n'])
iniList[0].append('\n')
#time for user Input
ip = '"' + raw_input('Hostname: ') + '"'
dispName = 'Server.DispName ' + '"' + raw_input('DispName: ') + '"' + '\n'
statusUrl = 'Server.Status ' + '"' + raw_input('Status URL: ') + '"' + '\n'
signupUrl = 'Server.Signup ' + '"' + raw_input('Signup URL: ') + '"' + '\n'
for l in range(1, 3):
iniList[l].append('Server.' + fileList[l][1] + '.Host ' + ip + '\n\n')
for l in [[dispName], [statusUrl], [signupUrl]]:
iniList.append(l)
outFile = open('server.ini', 'w')
for l in iniList:
for i in l:
outFile.write(i)
outFile.close()
The following was in my example file:
# Keys are Base64-encoded 512 bit RC4 keys, as generated by DirtSand's keygen
# command. Note that they MUST be quoted in the commands below, or the client
# won't parse them correctly!
I also tried it without inverting n and x
I have a code for convert Jmeter JTL FILE TO CSV, but when I run the code, I have the following error: IndexError: list index out of range in line 32
This is the code
import sys
import re
import datetime
import time
startTime = time.time()
cnt = 0
cnt2 = 0
failCnt = 0
reCompile = re.compile("\s([^\s]*?)=\"(.*?)\"")
delimiterCharacterOut = ","
def writeCSVLine(line):
x = reCompile.findall(line)
a = dict((row[0], row[1]) for row in x)
try:
a['ts1'] = str(int(int(a['ts'])/1000))
x = str(datetime.datetime.fromtimestamp(float(a['ts1'])))[0:19]
b = a['ts'] + ",\"" + x + "\"," + a['t'] + "," + a['lt'] + ",\"" + a['s'] + "\",\"" + a['lb'] + "\"," + a['rc'] + ",\"" + a['rm'] + "\",\"" + a['tn'] + "\",\"" + a['dt'] + "\"," + a['by'] + ",\"" + a['sc'] + "\"," + a['ec'] + ",\"" + a['ng'] + "\"," + a['na'] + ",\"" + a['hn'] + "\"," + a['in'] + "\n"
except:
return -1
o.write(b)
return 1
print "Splitting JTL file"
try:
runArgv = sys.argv #Save the command line
jtlInfile = str(sys.argv[1]) #Name of JTL input file
cvsOutfile = str(sys.argv[2]) # Name of CVS output file
reFilter = str(sys.argv[3]) # Filter the labels (lb) for the filter
except:
print "Error: Input format: <input file> <output file> <Filter by regular expression>"
raise
try:
f = open(jtlInfile, "r")
o = open(cvsOutfile, "w")
except:
raise
print "Filtering on regular expression : " + reFilter
cmpFilter = re.compile(reFilter)
# o.write("timestamp" + ",\""+ "datetime" + "\n")
o.write("timeStamp" + ",\"" + "datetime" + "\"," + "elapsed" + "," + "Latency" + ",\"" + "success" + "\",\"" + "label" + "\"," + "responseCode" + ",\"" + "responseMessage" + "\",\"" + "threadName"+ "\",\"" + "dataType" + "\"," + "bytes" + ",\"" + "SampleCount" + "\"," + "ErrorCount" + ",\"" + "grpThreads" + "\"," + "allThreads" + ",\"" + "Hostname" + "\"," + "IdleTime" + "\n")
for line in f:
try:
if cmpFilter.search(line):
returnVal = writeCSVLine(line)
if returnVal<0:
failCnt += 1
else:
cnt2 += 1
except:
print 'Error in line : ', cnt, line
raise
cnt += 1
endTime = time.time()
print "Time taken : ", str(endTime-startTime)
print "Lines processed : ", cnt
print "Lines that passed the filter : ", cnt2
print "Lines skipped (error?) : ", failCnt
f.close()
o.close()
Log de CMD
The base tutorial is in : http://balasoftwaretesting.blogspot.com/2012/03/converting-jmeter-jtl-file-to-csv-file.html?spref=bl
From the sys.argv docs, sys.argv is the list of command line arguments passed to a Python script.
Your command line log shows that you ran python JtltoCsv_Jmeter.py, which would result in an empty list for sys.argv. The tutorial provides a jtl file as an argument to JtltoCsv_Jmeter.py:
JtltoCsv_Jmeter.py C:\JtlToCsvConverter\input\sample.jtl
So it looks like maybe an error in copy/paste :)
Looking into the script, you need to pass 3 command line arguments:
Source JTL file
Target CSV file
Regular expression filter
So you need to execute the script like:
JtltoCsv_Jmeter.py example.jtl example.csv "(.*)"
Also there is an option to switch JMeter's results output format to CSV, in order to do so use one of the following steps:
Add jmeter.save.saveservice.output_format=csv line to user.properties file (lives under /bin folder of your JMeter installation)
Pass the property value via -J command line argument as:
jmeter -Jjmeter.save.saveservice.output_format=csv
See Apache JMeter Properties Customization Guide for more information on JMeter properties and ways of passing, setting and overriding them.