Program format getting change in wing - python

if see the picture on this link https://drive.google.com/file/d/0B_CP5fn_tuEDTDZoclM5M0V0cmc/edit?usp=sharing
this what my program looks when I write in sublime. But when I copy and paste the program in wing it looks like picture on the following link https://drive.google.com/file/d/0B_CP5fn_tuEDZEd0SVktVHRMcEE/edit?usp=sharing
When write and save the file in sublime, and then try to run it in python it give me error
But when paste in wing and format the indentation and save. Then when I run in python it works fine.
I dont how to indent the program well in sublime.

By looking at the images it seem likely that the editor settings related to indenting are different in sublime and wing.
Check if any of the editors are using tabs instead of spaces when indenting the code and if they are, change the editor to use 4 x whitespace instead of a tab.

Related

Identation coverts to weird arrow while copying code from sublime to jupyter notebook

Copying this piece of code from sublime :
# Texts Texts Texts Texts Texts Texts
for i in range(10):
#Idented Texxt
print i
results in below, with the line tab in jupyter-notebook. Now if i want to add to the code and do further testing in jupyter, i need to copy this weird arrow instead of hitting tab (else, it shows indentation error). Is there some simple way to change? I can imagine I am missing something really simple and this might not be a problem at all. I tried looking but found no mention of this. I can change all the indentation to space in sublime and this might work while copying but I rather work with tabs, to make codes readable.
Second image with double tabs, how it looks like in notebook.
After copying the scripts to Jupyter cells from sublime:
Select all the lines with arrows
Press Tab - This will remove the arrows and add a tab at the beginning of each line
Press Shift + Tab - This will delete all the added tabs in the step above
It looks like the reason that this is happening to you is due to a mismatch in the world view of tools (i.e. Sublime and Jupyter in this case) with regards to tabs versus spaces, a perennial dispute indeed.
In particular in Sublime you have translate_tabs_to_spaces turned off because you want to work with physical tab characters. So pressing Tab in Sublime will insert physical tab characters which are visualized via the tab_size setting.
On the other hand, according to this issue, Jupyter does not support indentation via tabs when you press the Tab key. As such in Jupyter despite your best intentions, when you press Tab it's inserting spaces instead. It does however support physical tabs if they're pasted in, which is why it appears the way it does above and why you have to manually paste physical tabs to get the input that you need.
Of these two tools, Sublime allows you to choose whether you use physical tabs or space characters, but Jupyter does not and always wants spaces. The most expedient solution in that case would be to not use tabs in Sublime either and then everything would match up.
Presumably that's not an option for you, so the next best solution would be to bridge the gap by having Sublime provide the data to Jupyter in a way that it expects, which is possible with a simple plugin.
In Sublime, select Tools > Developer > New Plugin... and replace the stub with this code, then save in the default location that Sublime will prompt you with (your User package) with a recognizable name such as copy_with_spaces.py:
import sublime
import sublime_plugin
class CopyWithSpacesCommand(sublime_plugin.TextCommand):
"""
Copy the selected text to the clipboard, replacing all tab characters with
spaces based on the tab size in the current view.
"""
def run(self, edit):
self.view.run_command("copy")
tab_size = self.view.settings().get("tab_size", 4)
text = sublime.get_clipboard().expandtabs(tab_size)
sublime.set_clipboard(text)
This implements a new command copy_with_spaces which will perform a copy but modify the data on the way through so that any physical tab characters are replaced with the appropriate number of white space characters.
With that in place, you can add a custom key binding to use when you're copying code from Sublime to Jupyter to streamline things. An example might be:
{
"keys": ["shift+ctrl+c"],
"command": "copy_with_spaces",
}

Visual Studio Code indentation for Python

How do I enable indentation in Visual Studio Code?
I'm trying to learn Python (new to programming) and need auto-indentation. It worked with the first version I tried, but it doesn't indent after a colon (:) any more. How can I configure it to automatically indent?
You can install the Visual Studio Code Python extension which will provide intellisense, auto-completion, code formatting, and debugging.
Here is more information on the Python extension, here.
As said there is the Python extension which now do it out of the box, but still don't do a great job, and an example is when you copy and past a whole block into a function or so. It just indents the first line, and that's not a good behavior. Here are two good helpful solutions:
indent a whole block manually: select the whole block, and then click Tab. If you want to indent backward, you do it with Shift+Tab. That's it, and I think that can be useful in several places.
Python auto indent extension (https://marketplace.visualstudio.com/items?itemName=hyesun.py-paste-indent). It solves the problem when pasting. Just see how it works in the link. Now about setting it up: You need to set just one keybinding for the command "pyPasteIndent.pasteAndIndent" provided by the extension. Once done, you will have your own shortcut to paste and indent automatically (I have set it to Alt + P)
Here is how:
- Ctrl+SHIFT+P to open the command palette, then write "key"*, choose *"open keyboard shortcut", and then the keybinding page open, which it's the nice interface for the **keybindings.json. You can open keybindings.json the same way and by choosing "open keyboard shortcut file" (in place of just "open keyboard shortcut"). Give it a look if never have. But here I will go with the nice interface. Know also that you can open that going menu File → Preference → Keyboard Shortcut.
In the keybinding window, in the search bar, paste pyPasteIndent.pasteAndIndent, and then click the + button to add the shortcut and create the keybinding.
The image below shows well how it's done:
I installed this extension: Python Indent. It works very well.
Simple solution!
Click the tab size (may show "Spaces: 4") in the bottom right corner and choose Convert Indentation to Tabs or Convert Indentation to Spaces as per your requirement.
I faced similar issues while editing. Select the lines of code you wish to intend and press Ctrl + ] in Windows or CMD+] on Mac.
You can change the indent size in settings. Search for tab size in settings. I use two, by the way.
I faced similar issues switching from PyCharm. The Python Indent extension, which is available in the Visual Studio Code marketplace, works perfectly fine for me.
For me, "Convert Indentation to Tabs" has worked.
To do that:
Go to "Command Palette" Ctrl+Shift+P (View>Command Palette)
Type in & select "Convert Indentation to Tabs" and press Enter

How to copy text from IDLE?

Is there a way to copy text out of Python IDLE on a Mac? When I highlight text and copy then past into a text editor, I get the same text pasted. It is some of the first text I start with in IDLE. None of the other text will copy out.
From The Things I Hate About IDLE That I Wish Someone Would Fix (from 2011):
1.2) NEW FEATURE: Auto-Copy-On-Highlight
Once we get rid of being able to move the cursor off the last line, that opens a new opportunity to implement an automatic copy-on-highlight feature that many terminal and IRC client programs implement. Since this text is read-only, the only reason a person has for highlighting it is to copy it (they can’t delete it.) As soon as the user highlights text in the shell window, it is copied to the clipboard.
Looks like the best you can do is save as a .py file. Open that in a text editor and continue working in IDLE. With each save, the text editor will refresh with all updates, including errors. At least TextWrangler will.
Sometimes in things like a linux terminal you can't do a normal copy and paste, try holding Command/Control + Shift + C after highlighting things and seeing if that works, since you're on mac i'm not sure if it would be the Command or Control key so try both
EDIT: There is an apple discussion about the exact same thing. They discuss going into the options menu, then into configuration and navigating to the keys section. Then you use a built in key-set, their suggestion is using IDLE Classic Mac.
I cannot tell from your description exactly what you did and what happened. But I can recommend that you upgrade to 3.4.4 or 3.5.1 (or 2.7.11 for 2.7 users). Among other improvements, they all contain a patch to make right-click for context menu work on Mac Aqua. This was issue 24801 on the CPython bug tracker.
If there is still an actual problem on Mac, I would like to know so it can be fixed.

Unable to modify python script in Windows

I'm currently using PyCharm and Sublime Text in Window to develop some Python script, but this morning something quite strange happens.
I am changing my code and testing it by running my code over test input. It is supposed to change the output text quite a lot. The output text at the end did not change at all. Then I try commenting out my output function, simply pass through the reading script. I thought "Now it should print nothing". But it prints the same output as yesterday, as if I never modified it today.
Any suggestion?
It turns out it's because my colleague has pushed an unexpected change up to repo last night...I always pulled before I end the day. The text output was forced by his logger:)
Mystery solved! Thanks for reading guys.
I've had exactly the same thing happen with Sublime. In my case I had moved / renamed the file after closing sublime. I reopened sublime some time later which reopened the old version of the file because I had left the file open when I last closed down sublime.
Have you tried closing the file and then opening it again from windows i.e. right-click => open with Sublime Text, to make sure you're editing the correct version?

GEdit/Python execution plugin?

I'm just starting out learning python with GEdit plus various plugins as my IDE.
Visual Studio/F# has a feature which permits the highlighting on a piece of text in the code window which then, on a keypress, gets executed in the F# console.
Is there a similar facility/plugin which would enable this sort of behaviour for GEdit/Python? I do have various execution type plugins (Run In Python,Better Python Console) but they don't give me this particular behaviour - or at least I'm not sure how to configure them to give me this. I find it useful because in learning python, I have some test code I want to execute particular individual lines or small segments of code (rather then a complete file) to try and understand what they are doing (and the copy/paste can get a bit tiresome)
... or perhaps there is a better way to do code exploration?
Many thx
Simon
Yes, you use "external tools plugin"
http://live.gnome.org/Gedit/ToolLauncherPlugin
As an example,
Edit > Preferences
Plugins
Tick "External Tools"
Close the Preferences Window
Tools > Manage External Tools
Click the "Add new too" icon in the bottom left
Name it "Execute Highlighted Python Code"
give it a keyboard shortcut
change the input combo box to : "highlighted selection"
change the output to : "Display in Bottom Pane"
In the editor window for the tool, replace everything with :
.
#!/usr/bin/env python
import sys
result = eval(sys.stdin.read())
print expression, "=>", result, type(result)
.
If you wish to see the result of entire .py file, you can put this code in your new created external tool window
#!/usr/bin/env python
import sys
exec(sys.stdin.read())
and change the Input to Current document.
For python, You can use "external tools plugin":
#!/bin/sh
python3 "$GEDIT_CURRENT_DOCUMENT_PATH"
Option of external tool:
Save: Current Document
Input: Current Document
Output: Display in bottom panel
Language: Python or Python3
Don't forget the quotes around $GEDIT_CURRENT_DOCUMENT_PATH....
To answer your second question, and hopefully guide you in a direction you'll be happier with, I think you ought to consider trying some different editors. There are many with more powerful code exploration features than GEdit has. Check out this post:
What IDE to use for Python?
I installed iPython console in gedit and do most of my simple scripting in it, but gedit is a very simple editor, so it'll not have some advance feature like an IDE
But if you want code exploring, or auto completion, I recommend a real IDE like Eclipse.
If you just want a editor, KomodoEdit is fine.
What I do is keep a file called python_temp.py. I have a shortcut to it in my dock. I use it as a scratch pad. Whenever I want to quickly run some code, I copy the code, click the shortcut in the doc, paste in the text and hit f5 to run. Quick, easy, simple, flexible.
I think what you're looking for is http://live.gnome.org/Gedit/Plugins/BetterPythonConsole.
You hit F5 and it runs the code in your file in a IDLE-like console. I don't know if it can only run selected code. (I don't think it can) but you can always copy the needed code in a new window and run it from there.
Have a look through the plugin list for other interesting stuff: http://live.gnome.org/Gedit/Plugins
The closest to a decent IDE...
Install gedit-developer-plugins (through synaptic || apt-get) and don't forget to enable (what you need) from gEdit's plugins (Edit->Preferences [tab] plugins) and happy coding

Categories