Why does this triple quoting solution fix path error? - python

So I was running into a bit of a problem today with this bit of code:
os.system("C:\Program Files (x86)\DOSBox-0.72\dosbox.exe")
Upon execution I'd get this error message:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
I assumed it was something to do with either the white space or the brackets, so I did a bit of digging online and after countless of ineffective "solutions" involving escape characters, I ended up finding a rather strange solution; to surround the double quotes with another double and a single like so:
os.system('""C:\Program Files (x86)\DOSBox-0.72\dosbox.exe""')
I'm confused as to why this works, which I guess isn't very surprising given my lack of python knowledge. I was searching the internet to find out why this works, but to no avail.
So I guess the question is, why does adding those quotes solve the problem?
I'm very new to Python, and programming in general so I'd greatly appreciate any answer to be explained in layman terms.
Thanks in advance.
EDIT: I wouldn't say this question is a duplicate of the linked one, I was not after a solution to the problem, I already had one. I was curious as to why it worked, the logic behind it, so I could better understand the way python works.

Related

Python SyntaxError: invalid syntax when implementing bbcodepy

I got bbcodepy and I'm allowed to modify it, but I can't import it in my main.py. I keep getting a SyntaxError and I don't really know what's wrong with the code because I didn't write it. I just want to tinker around a little bit and see if I can get it to fill my needs.
Here's an image pointing me in the direction of the syntax error. But I noticed the same code is written on the same line and I don't get the SyntaxError for that. Here's the code:
_URL_RE = re.compile(ur'''\b((?:([\w-]+):(/{1,3})|www[.])(?:(?:(?:[^\s&()]|&|")*(?:[^!"#$%&'()*+,.:;<=>?#\[\]^`{|}~\s]))|(?:\((?:[^\s&()]|&|")*\)))+)''')
The problem appears to be "[^\s&()]" but only the second one, not the first one. If you look closely in the code you'll see that the same thing appears twice, but I only get the SyntaxError on the second appearance. Someone enlighten me, please. I've been trying to find a decent BBCode parser for Python for a couple of days now and I believe this is the one I can modify to my needs. I can't seem to get bbcode to work as I want it to, so I'm trying this out.
Well, Python version 3.4 and above does not support a 'UR' prefix.
You need to execute your code with Python 2.7, or change to line into:
_URL_RE = re.compile(r'''\b((?:([\w-]+):(/{1,3})|www[.])(?:(?:(?:[^\s&()]|&|")*(?:[^!"#$%&'()*+,.:;<=>?#\[\]^`{|}~\s]))|(?:\((?:[^\s&()]|&|")*\)))+)''')
See also: python version 3.4 does not support a 'ur' prefix
Note: consider avoiding triple-quoted string because if you insert a newline, it can change the meaning of the regex (unless it is compiled in VERBOSE mode).

Having hard time trying to coppy-paste some questions from the internet to my IDE(pycharm).Some mathematic symbols are messed up

just started working on my quiz application using python and it has to do with maths(True-False) questions.So whenever i try to copy-paste a question from the internet to my questions array some characters are really messed up.Can anyone help me solve this please? i have searched a lot for a solution but didn't get an answer so far.(I am using Pycharm)
Sometimes there are space/tab conversion errors that occur when copy/pasting from a web source.
If that is the problem you're having in pycharm, just highlight the code that was copied > Edit > Convert Indents.
If this is not what you needed, please edit your question to include the code block containing the "really messed up characters" that you are seeing in pycharm. I'll try to see what's happening there.

Getting confusing error when using Qprocess.start(programname, ListofStringArguments) Need some clarity

Some odd reaction with PyQt5.8 on windows 10. I'm making this because I want to try to understand why I get this problem, as it's easily solved. (Shown below)
I try to do:
Command =''' testurl -x --audio-format "mp3" --audio-quality "0"'''
self.start('yotube-dl',Command.split())
but get the error from youtube-dl that the specified audio format is wrong. Which means that for some reason, "mp3" isn't after --audio-format. I also tried, --audio-format="mp3", and that also gives me an error, despite not having a space after the --audio-format
Now, if I do my "workaround":
self.start('youtube-dl'+Command)
It works as expected. It tells you the specified URL(since it's testurl) is not valid. Don't be mistakes, a working url behaves just the same. So, obviously one would think there's something funky with the .split() part of it, but this is how the list turns out when you use Command.split():
['testurl', '-x', '--audio-format', '"mp3"', '--audio-quality', '"0"']
And as far as I know, the arguments are passed in order of the list. I've had it working before, but as soon as I changed another configuration, it all went south. I just can't figure out why my results differ, and it's only for this specific audio conversion argument(-x --audio-format "mp3"), other arguments work pretty well, one of them containing spaces.
Also, I can let you know I tried with lots of "'s around in the string, and to no help. I'm presuming this has something to do with the way " are added in some places on windows according to Qt documentation(Link for reference), but I still can't make figure out what to change. For all I know, it's something stupid i'm doing.
So, if anyone has understanding of why it does this, i'd love to know. Again, i'm just looking for some clarity, as I already worked around it, but want to understand why it goes wrong.

Which is the closest Python equivalent to Perl::Tidy?

Coming from Perl I've been used to hitting C-c t to reformat my code according to pre-defined Perl::Tidy rules. Now, with Python I'm astonished to learn that there is nothing that even remotely resembles the power of Perl::Tidy. PythonTidy 1.20 looks almost appropriate, but barfed at first mis-aligned line ("unexpected indent").
In particular, I'm looking for the following:
Put PEP-8 into use as far as possible (the following items are essentially derivations of this one)
Convert indentation tabs to spaces
Remove trailing spaces
Break up code according to the predefined line-length as far as it goes (Eclipse-style string splitting and splitting method chains)
Normalize whitespace around
(bonus feature, optional) Re-format code including indentation.
Right now, I'm going throught someone else's code and correct everything pep8 and pyflakes tell me, which is mostly "remove trailing space" and "insert additional blank line". While I know that re-indentation is not trivial in Python (even though it should be possible just by going through the code and remembering the indentation), other features seem easy enough that I can't believe nobody has implemented this before.
Any recommendations?
Update: I'm going to take a deeper look at PythonTidy, since it seems to go into the right direction. Maybe I can find out why it barfs at me.
There is a reindent.py script distributed with python in the scripts directory.
untabify.py (Tools/scripts/untabify.py from the root directory of a Python source distribution) should fix the tabs, which may be what's stopping Python Tidy from doing the rest of the work.
Have you tried creating a wrapper around pythontidy? There's one for the sublime editor here.
Also, does pythontidy break up long lines properly for you? When I have a long line that ends in a tuple, it creates a new line for every entry in the tuple, instead of using Python's implied line continuation inside parentheses, brackets and braces as suggested by PEP-8.
I have used autopep8 for this purpose and found it handy.

Getting proper code completion for Python on Vim?

I've gotten omnicompletion with Pysmell to work before, but I can't seem to do it again.
I tried following some steps online, but most, if not all, of them are to vague and assume too much that you know what you are doing to some extent.
Can someone post a full, step-by-step tutorial on how to get code completion working properly, for complete Vim newbies (for dummies?)?
There's also Ctrl+n in insert mode which will autocomplete based on the words it has seen in any of the open buffers (even in other tabs).
You may try Pydiction (Excerpt below)
Description Pydiction allows you to
Tab-complete Python code in Vim,
including: standard, custom and
third-party modules and packages. Plus
keywords, built-ins, and string
literals.
Pyflakes has a vim plugin that does this pretty awesomely. Unlike Pydiction, you don't need to build a dictionary beforehand (so if you're bouncing between different virtualenvs it's a bit less hassle.) I haven't been using it long but it seems very slick.
Try hitting Ctrl-p while typing mid-word. Ctrl-p inserts the most recent word that starts with the prefix you're typing and Ctrl-n inserts the next match. If you have several possibilities, you can hit ctrl-p more than once to substitute each candidate in order.

Categories

Resources