I am pretty new to coding, and I am attempting to have a user input words (zones). I want these to be put into a list that can be used to pull from based on the counter. The rest of the input is being pulled from a file and then then broken up using shlex.split. I'm then naming these linetokens and using them for my if statements and my naming. I was wondering if someone could look at the following code and let me know what I'm doing wrong. I don't receive any errors when I run it; however, nothing happens. I'm using python 2.7. *After adding the "main(sys.argv[1])", the script now runs but I am still receiving errors.
import sys
import shlex
def inputZone(zone):
zones = raw_input('Enter zones: ')
return zone
def main(argv):
count = int(0)
zones = zone.split(' ')
sys.stdout.flush
argv = sys.argv
if len(sys.argv) != 2:
configfile = open(str(sys.argv(1),'r'))
for configline in configfile:
with open('converted.txt','a') as converted:
linetokens = shlex.split(configline)
while count <= len(zones):
if(linetokens[0]=='set' and linetokens[1]=='group' and linetokens[3]==zones[count] and linetokens[5]=='add'):
groupObject=linetokens[4].replace(' ','_').replace('[','').replace(']','').strip()
groupZone=linetokens[3].strip(' ')
addressObjectName=linetokens[6].replace(' ','').replace('[','').replace(']','')
converted.write("set security zones security-zone " + groupZone + " address-book address-set " + groupObject + " address " + + addressObjectName +'\n')
count = count + 1
configfile.close()
main(sys.argv[1])
whats the error you are getting. I can notice many python errors in the program.
main(argv) argv is never used
zones = zone.split(' ') Zone is not declared by this time.
sys.stdout.flush is a function definition, you are not really calling it
Related
I have a S3 Bucket Streaming logs to a lambda function that tags files based on some logic.
While I have worked around this issue in the past and I understand there are some characters that need to be handled I'm wondering if there is a safe way to handle this with some API or is it something I need to handle on my own.
For example I have a lambda function like so:
import boto3
def lambda_handler(event, context):
s3 = boto3.client("s3")
for record in event["Records"]:
bucket = record["s3"]["bucket"]["name"]
objectName = record["s3"]["object"]["key"]
tags = []
if "Pizza" in objectName:
tags.append({"Key" : "Project", "Value" : "Great"})
if "Hamburger" in objectName:
tags.append({"Key" : "Project", "Value" : "Good"})
if "Liver" in objectName:
tags.append({"Key" : "Project", "Value" : "Yuck"})
s3.put_object_tagging(
Bucket=bucket,
Key=objectName,
Tagging={
"TagSet" : tags
}
)
return {
'statusCode': 200,
}
This code works great. I upload a file to s3 called Pizza-Is-Better-Than-Liver.txt then the function runs and tags the file with both Great and Yuck (sorry for the strained example).
However If I upload the file Pizza Is+AmazeBalls.txt things go sideways:
Looking at the event in CloudWatch the object key shows as: Pizza+Is%2BAmazeBalls.txt.
Obviously the space is escaped to a + and the + to a %2B when I pass that key to put_object_tagging() it fails with a NoSuchKey Error.
My question; is there a defined way to deal with escaped characters in boto3 or some other sdk, or do I just need to do it myself? I really don't and to add any modules to the function and I could just use do a contains / replace(), but it's odd I would get something back that I can't immediately use without some transformation.
I'm not uploading the files and can't mandate what they call things (i-have-tried-but-it-fails), if it's a valid Windows or Mac filename it should work (I get that is a whole other issue but I can deal with that).
EDIT:
So after some comments on the GitHub I should have been using urllib.parse.unquote_plus in this situation. this would be the proper way to solve escaping issues like this.
from urllib.parse import unquote_plus
print(unquote_plus("Pizza+Is%2BAmazeBalls.txt"))
# Pizza Is+AmazeBalls.txt
Original Answer:
Since no other answers I guess I post my bandaid:
def format_path(path):
path = path.replace("+", " ")
path = path.replace("%21", "!")
path = path.replace("%24", "$")
path = path.replace("%26", "&")
path = path.replace("%27", "'")
path = path.replace("%28", "(")
path = path.replace("%29", ")")
path = path.replace("%2B", "+")
path = path.replace("%40", "#")
path = path.replace("%3A", ":")
path = path.replace("%3B", ";")
path = path.replace("%2C", ",")
path = path.replace("%3D", "=")
path = path.replace("%3F", "?")
return path
I'm sure there is a simpler, more complete way to do this but this seems to work... for now.
I'm quite new to using Django. As first project I wrote a little tool to create M3U8-Playlists.
My problem is, that long playlists are not transferred completely.
This is the stripped-down code:
def create(request):
# just a dummy filelist
playlist = ["#EXTM3U"] + 5 * ["/home/pi/music/" + 5 * "äöü0123456789á/" + "xyz.mp3"]
file_to_send = ContentFile("")
for item in playlist:
file_to_send.write("{}\n".format(item.replace("/home/pi/Music", r"\\raspberry\music").replace("/", "\\")))
response = HttpResponse(file_to_send, "audio/x-mpegurl")
response["Content-Length"] = file_to_send.size
response["Content-Disposition"] = f"attachment; filename=\"playlist.m3u8\""
# print some debug info
print("lines:", len(playlist), "chars (no linebreaks)", sum([len(entry) for entry in playlist]),
"filesize:", file_to_send.size)
return response
The problem seems to lie in the non-ascii chars in playlist entries (äöüá). When there are no such characters, the file is transferred intact. I assume that these are characters that use two bytes in UTF-8, but writing strings to the ContentFile like I do is probably not correct.
Found the answer, while working on the problem description.
This works:
def create(request):
# just a dummy filelist
playlist = ["#EXTM3U"] + 5 * ["/home/pi/music/" + 5 * "äöü0123456789á/" + "xyz.mp3"]
joined_playlist = "\n".join([item.replace("/home/pi/Music", r"\\raspberry\music").replace("/", "\\") for item in playlist])
file_to_send = ContentFile(joined_playlist.encode("UTF-8"))
response = HttpResponse(file_to_send, "audio/x-mpegurl")
response["Content-Length"] = file_to_send.size
response["Content-Disposition"] = f"attachment; filename=\"playlist.m3u8\""
# print some debug info
print("lines:", len(playlist), "chars (no linebreaks)", sum([len(entry) for entry in playlist]),
"filesize:", file_to_send.size)
return response
The important difference is, that I don't write Strings to the ContentFile any longer, but a byte array, which I got through encoding the String in UTF-8.
HTH
I'm trying to implement a simple sub dowloader with Selenium on Python 2.7 going on the http://www.yifysubtitles.com website.
I have run into serious troubles with implementing waits either implicit or explicit in Selenium - surely caused by ignorance rather than code.
I eventually opted for the destroy-all time.sleep() solution. The program works but I would like a more programatic solution and a prettier code.
Here is the code with the both implicit and explicit waits:
user_input = raw_input("Enter the movie of interest: ")
profile = SetProfile()
browser = webdriver.Firefox(profile)
# browser.implicitly_wait(30)
WindowSize(400, 400)
browser.get(url)
print "Searching..."
search_bar = browser.find_element_by_id("qSearch")
click_button = browser.find_element_by_class_name("glyphicon")
#wait_1 = WebDriverWait(browser, 50)
#cookie_element = wait_1.until(EC.element_to_be_clickable((By.CLASS_NAME,"cc_btn.cc_btn_accept_all")))
time.sleep(5)
accept_cookies_btn = browser.find_element_by_class_name("cc_btn.cc_btn_accept_all")
search_bar.clear()
accept_cookies_btn.click()
search_bar.send_keys(user_input)
search_bar.send_keys(Keys.RETURN)
time.sleep(10)
#wait_2 = WebDriverWait(browser, 50)
#result_element = wait_2.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h3.media-heading")))
result = browser.find_element_by_class_name("media-heading")
result.click()
print "Movie found!"
time.sleep(10)
req = requests.get(browser.current_url)
soup = BeautifulSoup(req.text)
link_to_sub = ResultParser(soup)
print "Choosing the best eng. subtitle..."
browser.get(url+link_to_sub[1])
download_button = browser.find_element_by_class_name("btn-icon")
download_button.click()
time.sleep(10)
browser.quit()
print "Enjoy!"
I have commented out the waits (explicit or implicit) I tried to use. Indeed, If I use waits instead time.sleep, Selenium will throw at me an ElementNotVisibleException.
Thus, am I doing something wrong with the waits?
Regarding the implicit waits, Selenium always throws at me ElementNotVisibleException: Message: (With message empty) when I run the program, whatever the wait length (tried 5, 30 and 100!).
When I use explicit waits, similarly, Selenium either throw ElementNotVisibleException or seems to use the DOM from the preceding page. Indeed, in the last case the wait is after clicking on the result, but I get the heading of the previous page...
As I said, when I use time.sleep(), Selenium finds the elements without any trouble.
A long message for a simple question: why my waits seem to not work?
I have a Django view that is responsible for providing raw data for graph rendering on server-side. When I run the app locally (I have Windows 10 on my machine), everything works fine. But when I run the app from the remote server (it's Ubuntu), I get an 500-error. This type of errors (when identical code is ok locally, but fails remotely), currently constitutes a serious issue for me in that on every such occasion it leads to very time-consuming - and seemingly unprofessional - process to identify and fix them. And my question is, what is the magic exception handling code snippet which would help me through the detailed exception message to client-side console.
To be more specific, consider the following (I deliberately provide the code as is):
Django view:
def graph_vendors_black_white(request):
try:
legal_entity_own_qs = get_sale_points(request.user.id)
list = []
data = [x.blacknwhite_agreement_count(True) for x in legal_entity_own_qs]
list.append({'name':u'В белую', 'data':data});
data = [x.blacknwhite_agreement_count(False) for x in legal_entity_own_qs]
list.append({'name':u'В чёрную', 'data':data});
data = [x.blacknwhite_agreement_count(None) for x in legal_entity_own_qs]
list.append({'name':u'Не выбрана опция', 'data':data});
legal_entity_own_list = [unicode(x.short_identifier_rus) for x in legal_entity_own_qs]
json_response = JsonResponse({'time_series':json.dumps(list, ensure_ascii=False).encode('utf8'),'categories':json.dumps(legal_entity_own_list, ensure_ascii=False).encode('utf8')})
return json_response
except:
user_msg, tech_msg = default_error_msg()
return JsonResponse(user_msg + '\n ' + tech_msg, status = 500, safe = False)
def default_error_msg():
user_msg = u'Что-то пошло не так'
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
try:
console_msg = exc_obj[1]
except:
console_msg = str(exc_obj.message)
tech_msg = ('File - ' + fname + ' on line ' + str(exc_tb.tb_lineno) + '. \n' + \
'Error message - ' + console_msg + '\n' + \
'Exception type - ' + str(type(exc_obj)))
return user_msg, tech_msg
JS:
$.get('/graph_vendors_black_white/', function (response)
{
// ... some code
}).fail(function(json_result) {
$('#black_white_container').html(json_result.responseJSON)
});
When I run this locally, it's perfectly ok. From remote server, it throws 500 error and most surprisingly json_result does not contain error message. Whereas if I put something like a = 1/0, I get the error message in the container instead of graph.
To sum up, the question is, what is the generic exception handling code snippet which would transmit the detailed error message to the fail-function response parameter NO MATTER WHAT THE ERROR IS on the server-side
I'm facing a thing that is confusing me. I have this groovy code:
def static extractTaskIdsFromLine(String orderName, String line) {
print("comment line = \"" + line + "\"")
def pattern = ~/${orderName}-[0-9]+/
return line != null ? line.findAll(pattern) : new ArrayList<>()
}
On my local machine everything looks fine and all tests pass. But with the same conditions this code on Team City with the same input gives no one matches. Both encodings and input parameters are the same.
Do you have any ideas?