In the windows python console, how to make Tab = four spaces? - python

Hello I would like that when I am in the python
console tabbing will give me four spaces. Any ideas?

Download and install AutoHotkey
Write this script:
SetTitleMatchMode 2
#IfWinActive python
tab::
Send, {SPACE}
Send, {SPACE}
Send, {SPACE}
Send, {SPACE}
Save it as tab-to-space.ahk, and doubleclick on the file.
Note: you might have to captalize "Python" to match your window tite. Or you can have "yhton" and it will match Jython too.

Related

How do I get powershell or windows terminal to print color text using ansi escape codes?

I'm trying to learn to write python code that will output in the terminal different colour text using ansi escape characters. I'm watching a tutorial on python sockets and just learning to communicate between different terminals. I've tried the following on command prompt, powershell and windows terminal and same results.
message = "$([char]0x1b)[30;41m YOUR_TEXT_HERE $([char]0x1b)[0m"
print(message)
When I try this, the output in the terminal shows
$([char]0x1b)[30;41m YOUR_TEXT_HERE $([char]0x1b)[0m
I also tried to use this so that the double quotes were in the line
message = "$([char]0x1b)[30;41m YOUR_TEXT_HERE $([char]0x1b)[0m"
print('\"'+message+'\"')
but it just outputs this
"$([char]0x1b)[30;41m YOUR_TEXT_HERE $([char]0x1b)[0m"
If I type
"$([char]0x1b)[30;41m YOUR_TEXT_HERE $([char]0x1b)[0m"
into the terminal and press enter, it shows correct with red background and black text
I've tried to google and youtube about this but can't seem to find an answer. Please help. Thanks
The $([char]0x1b) should be \x1b in Python. So this should work:
message = "\x1b[30;41m YOUR_TEXT_HERE \x1b[0m"
print(message)

Is there a way to make a VBA code run a Python code?

I have read all online questions on this problem and none of them seem to be working for me. I want to write a code in VBA such that when a button is pressed, my code in Python automatically starts running.
I am getting a file not found error when I run the code but I have checked the path and I know it is correct.
The code I am trying is:
Sub MyMacro()
Shell("C:/Users/RGilsburg/New folder/pythonw.exe" & "F:/Asset/Global/Port/untitled1.py")
End Sub
Can anyone tell me where the error is?
The shell command should work, however, you have some issues with the command to be executed:
You need to put a blank between the Python command and the python file.
Use the backslash, not the slash if you are on windows
If a file contains a blank, you have to put it in Double quotes ". When you want to have a quote character in a string in VBA, you have to double it.
Basically, the parameter to Shell should be the same as you would enter it on a command prompt. To check this, write the command into a string variable and write this to the immediate window (Ctrl+G). Copy it from there and paste it into a CMD-window.
Try
Dim python as string, script as string, cmd As String
python = """C:\Users\RGilsburg\New folder\pythonw.exe"""
script = "F:\Asset\Global\Port\untitled1.py"
cmd = python & " " & script
Debug.print cmd
Shell cmd
this may be a bit ignorant of me since i know nothing of python and little of vba but did you add python as a reference in the vba editor.
if not
go to the tools dropdown in the vba editor select references and see if you can find python.

Determine the window name/number in iTerm2

I am using iTerm2 on MacOS (Sierra). I have multiple instances of iterm2 running, each has a title that are prefixed with a number which increments with each running window.
I would like to run a shell command to return this number on the command line, does any one know how to get this information?
I am looking for something like:
$ iterm_get_number()
2
I was able to get the names using applescript, the following script dumps a list of the open iTerm windows. Since I am interested in using these names in a python script I included a small snippet for doing this as well.
get_window_names.applescript
on run
set returnNames to {}
tell application "iTerm"
repeat with theWindow in windows
tell theWindow
set end of returnNames to get name
end tell
end repeat
end tell
return returnNames
end run
python for extracting information from above script
out = subprocess.check_output(['osascript', 'get_window_names.applescript'])
print [x.strip() for x in out.split(',')]

End of script output before headers

When I create the file test.py (see code below) in an editor on Windows (I tried Netbeans, PyCharm and Notepad++) and upload it to the server (Ubuntu) I receive this error:
End of script output before headers: test.py
But when I create the file directly on the server using vi command line editor the page is displayed without any error. Any idea how to fix this issue ?
Here's the code for test.py
#!/usr/bin/python
# send content type
print("Content-Type: text/html\n\n")
print("Good")
I think that's because of windows carriage return characters.
These are two characters:
\r is carriage return;
\n is line feed.
Two characters combined represent a new line on Windows. Whereas on Linux, \n represents new line.
Notepad++ has an option to specify which format you want to use:
Go to Settings -- > Preferences and the choose linux:

Why does Python's os.system() command on Windows XP require two double quotes at the beginning to run?

I've encountered some very strange behavior on Windows XP. I'm using Python to execute a command to open a browser using a shortcut file in a folder on the desktop.
The following line is what I expect to do the job:
os.system(r'"C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" "chrome.google.com/webstore"')
It's a raw string literal so all the backslashes are actual backslashes. I can tell that is true by putting echo at the start of that command. (i.e. os.system('echo "C:\Documents and Settings\blah\blah chrome.google.com/webstore"') )
Using echo returns the following:
"C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" "chrome.google.com/webstore"
That looks like a fine Windows command, yes? Well it is. Copying and pasting that into a command prompt runs fine. But the actual command (without echo) fails. The error states that
'C:\Documents' is not recognized as an internal or external command.
Which is a pretty standard error for an unquoted path. But wait, the command we echoed was good, so it should run, right? I guess not...
Through trial and error I was able to find something that worked. The following line is the only way I've been able to get the browser to launch:
os.system('""C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" chrome.google.com/webstore"')
That's right, apparently the solution is to add an extra double quote at the beginning of the command and take out the double quote before the second argument.
To me that looks like empty string, unquoted path with unescaped spaces, then a quoted url that starts with a space.
If I echo that command it returns exactly what you would expect:
""C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" chrome.google.com/webstore"
But it works! Pasting that echo result into the command line fails with the "C:\Documents not recognized" error from before, but the Python command opens the browser to the correct page anyway.
Could someone please explain what is happening here? I am really confused by this behavior because it is not at all what I expect.
P.S. This behavior is entirely different on every Windows OS past XP. For Vista and newer the command is:
os.system(r'"C:\Users\you\Desktop\Chrome Browsers\Google Chrome\Google Chrome 46.lnk" "chrome.google.com/webstore"')
Because there are " " spaces in your paths. C:\Documents and Settings\.. see the 2 spaces? otherwise it will pick up C:\Documents as a binary and and as the first param, Settings\.. as the other param.. and so on. This way youre saying: this whole thing is a binary C:\Documents and Settings\.. and chrome.google.com/webstore is my argument.
Make sense?

Categories

Resources