Writing a plugin to open a new file - python

In Sublime Text 3 I have a few packages that open new files within the window. They have functions that look like:
class BufferCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.new_file()
...
I grabbed this for something I am working on, and couldn't get a new file to generate, even though that works for the plugin I snagged it from, and is similar to how it is used in Packages/Default/new_templates.py. After some more searching, I found the following code, and it works as expected
class TestCommand(sublime_plugin.TextCommand):
def run(self,edit):
view = self.view.window().new_file()
Can somebody tell me why?

It might be the way you are running your command? If you are entering
view.run_command("buffer")
into the python console, then nothing will happen if BufferCommand is not a TextCommand.
Instead try this:
window.run_command("buffer")
This should fix your problem.

Related

Why is my python class being executed twice?

I am currently trying to write an script which takes an user input and then prints it out through a class which is located in another file. But when running my script I have to give the programm the input twice which is then being printed out thrice because of an weird reason. I also searched up some other similar questions on stackoverflow but none of them helped me fixing my problem.
This is the code in the first file:
#this is main.py
global test_input
test_input = input('give me an input: ')
if 'i like cookies' in test_input:
from test import *
test_class.test()
This is the code in the second file:
#this is test.py
class test_class():
def test():
from main import test_input
print(test_input)
What the output looks like after running the script:
give me an input: i like cookies
give me an input: i like cookies #this second input is created because the function is being executed twice. In this example I would've typed in i like cookies twice
i like cookies
i like cookies
i like cookies
What the ouput should look like:
give me an input: i like cookies
i like cookies
I would be very very glad if someone could help me out with solving this problem and explaining to me what I've made wrong:)
Thank's for every suggestion and help in advance:)
It is punishing you for bad programming practices. ;) The issue is that, when you run a program, that module is not considered to be imported. So, when you get into test_class.test(), your from main statement actually causes your main program to be loaded AGAIN. It will do the input call again, and will call test_class.test() again. This time, main has already been imported, so it doesn't need to do it again, and thing go normally.
It is horrible practice for a submodule to try to import something from a main module. If your module function needs a value, then pass it as a parameter.

How do you use pdb in Hydrogen?

I am new to debugging in Python, and I currently use Hydrogen within Atom to do most of my Python work. I have looked into debugging, and it seems that the Python package pdb is the standard way to debug in Python. Most of the examples I can find seem to require using a command line on a .py file. However, in Hydrogen I would want to run it within the kernel on individual lines of the code, since I don't think pdb will work on a markdown (.md) file. Is there a way I can debug within my workflow, or do I need to make .py files of my code and run pdb on them?
I had read this answer and tried to implement "C. From Within Your Program". It had thrown errors, and I gave up. However, I went to the source that the answer had cited, and used this code:
import pdb
class MyObj(object):
def __init__(self, num_loops):
self.count = num_loops
def go(self):
for i in range(self.count):
pdb.set_trace()
print(i)
return
if __name__ == '__main__':
MyObj(5).go()
From this, I was able to get pdb to run in Hydrogen.

How to run python from C# without .exe file

I am a Python beginner and wrote some Python code that I want to run from my C# code.
In all the answers I have seen already, the way was to make a .exe file from the .py one and run it by system call.
However, I want it so that I do not need to make the .exe file and can write the commands with arguments as I could in the command line.
C:\Users\ntuser> python C:\Users\ntuser\Documents\run_python.py 3
Is there a way to do it?
I found a way to pass just one command—but I need to make two: 1. Go to "C:\Users\ntuser" 2. Run the Python code.
Thanks!
OK, so I found a solution.
Thanks to UnholySheep for the help.
What you need to do is:
Make the python as system variable
Go by code to your home directory:
Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
Add this code for calling the python code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//if you want to hide the window
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C python pytonPath\\python_code.py";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();//if you want to wait

Sublime Text accessing view file name error

I am new to Python, and Sublime Text plugin development, and I don't know what I'm doing wrong here. I am using Sublime Text 3. I'm trying to create a plugin that will copy the file name to the clipboard. Can anyone help me understand this python error and/or offer a solution?
import sublime, sublime_plugin
class Filename_to_clipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.set_clipboard(sublime.View.file_name())
sublime.message_dialog("The full file path was copied to the clipboard")
and the error, when I call the plugin from the console, is:
>>> view.run_command('filename_to_clipboard')
Traceback (most recent call last):
File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 549, in run_
return self.run(edit)
File "/Users/ivan/Library/Application Support/Sublime Text 3/Packages/Filename_to_clipboard/filename_to_clipboard.py", line 5, in run
sublime.set_clipboard(sublime.View.file_name())
TypeError: file_name() missing 1 required positional argument: 'self'
When I do:
sublime.set_clipboard(view.file_name())
from the conosole, it works! Why?
Try self.view.file_name() rather than sublime.View.file_name(). You have a reference to an instance of the view for your TextCommand. It was written for ST2, but you may want to take a look at this tutorial
http://net.tutsplus.com/tutorials/python-tutorials/how-to-create-a-sublime-text-2-plugin/
I don't know anything about the Sublime Text API, but it looks like your problem here is that file_name() is an instance method, and sublime.View is a class so sublime.View.file_name() fails.
In Python, when you call an instance method you are implicitly passing the instance as the first argument, which is why it says you are missing the argument self. You could even write the call explicitly. For example, view.file_name() could also be written sublime.View.file_name(view), but that's kind of silly.
Good luck with your plugin :)
I hate to see all your effort wasted, but this functionality already exists in ST3. If you right-click in the edit area, Copy File Path is one of the options on the context menu. To create a keyboard shortcut, open Preferences -> Key Bindings - User and add the following item:
{ "keys": ["super+i"], "command": "copy_path" }
You can of course change the key binding to whatever you want. If the key bindings file is empty when you open it, just add opening and closing square brackets:
[
{ "keys": ["super+i"], "command": "copy_path" }
]
However, I still encourage you to learn Python and plugin programming, they're both quite rewarding and great fun! Good luck!
EDIT
Based on a comment by #skuroda, here's how I discovered the command to use:
I already knew there was a Copy File Path option in the context menu, but looking through Preferences -> Key Bindings - Default I couldn't find any shortcuts or macros already assigned to that action, and I didn't know exactly what the command's name was. So, I hit Ctrl` to open the console, then ran
sublime.log_commands(True)
to have all actions logged to the console. I then right-clicked and selected Copy File Path, and
command: copy_path
showed up, along with a message regarding the mouse's location when the context menu event occurred. I assigned the key combination, ran it, and it worked. To finish up, I run
sublime.log_commands(False)
so the console doesn't get clogged with unnecessary info, then hit Ctrl` again to close the console.

Eclipse Pydev: Supress no-self errors in python wrappers generated with swig

when generating python wrappers with swig the python wrapper classes in the generated python file do not have an explicit self parameter, for example see below:
class PySwigIterator(_object):
def value(*args): return _spatiotemporalnmf.PySwigIterator_value(*args)
def incr(*args): return _spatiotemporalnmf.PySwigIterator_incr(*args)
def decr(*args): return _spatiotemporalnmf.PySwigIterator_decr(*args)
def distance(*args): return _spatiotemporalnmf.PySwigIterator_distance(*args)
I am developing with the eclipse pluging Pydev. Pydev always shows an error when it detects a method without explicit self parameter. I am aware of two methods to get rid of the errors: First, disable error checking for the whole project in the Pydev preferences. Second, add a ##NoSelf to every line with an error. I don't want to use the first one, because I still want to get error warnings for my non-swig-generated files. Obviously the second one is also not very good, because I would have to do it by hand and every time I generate the file again, all ##NoSelfs will be gone.
My Question now is, is there a better way to achieve this?
Thanks
As from the documentation, any file with the comment
##PydevCodeAnalysisIgnore
inside will not be analyzed.
Therefore, you just need to add it to all SWIG-generated files, and you should be OK. It is just one place to change, and you could even write a very small processor that will add it automatically.

Categories

Resources