Environment preamble:
I write Python code in Vim for Maya on Linux. I'm using nose for test discovery and execution, and I'm using its attribute plugin to decorate Maya-only test classes. I have mappings in Vim to run the non-Maya tests in regular Python, and the Maya tests through Maya's mayapy executable, which gives me access to the scene, and the maya.cmds module.
Actual problem:
mayapy takes 6 seconds to start up every time I hit my in-Maya tests Vim mapping. It gets really tedious. The non-Maya tests happen as fast as I can hit their mapping. It's helped me abstract more away from Maya, to avoid that tiny, all-day pain, but I'd still like it gone. 6 seconds starts to feel like a long time. There are tricks for working asynchronously in Vim, but I don't like to move ahead without seeing if my tests passed. I've asked Autodesk (makers of Maya), and they say there's no way around the startup time.
Question:
What I think I want is to fire up mayapy in the background, and then when I hit my Vim mapping, have it connect to the waiting Python instance and call the nose.run() command. I feel like mkfifo is the way to go, but I'm unsure of the particulars. How can I do this (through mkfifo or otherwise)?
I am assuming mayapy is like a repl and you can hit commands on to it like the mentioned nose.run(). If that is the case, I would recommend using the vim plugin tslime which allows you to be able to send stuff from a vim buffer to another tmux pane / window.
Related
I am trying to integrate a complex python application (with a custom python interpreter shipped along) for OSX. In order to handle a set of issues due to cross platform requirements, I created a .app bundle pointing at a shell script with its CFExecutable entry in Info.plist. This works, and the invoked shell script starts up the actual application binary. However, I have the following problems:
The .app icon bounces endlessly on the dock, never reaching the "activated" status. I guess it's because the shell script does not terminate. This dock entry has the correct "application icon"
When the binary executable is invoked by the script, a new Dock entry appears with a generic python icon. This icon successfully starts up and stops bouncing as the application starts up.
When I try to kill the first Dock entry via Force quit, the actual application still keeps running, as it's clearly controlled by the second entry on the dock.
Is there a way to have this setup behave more naturally? Do I need to ditch shell script for an objective C wrapper? If I have to use a obj-C wrapper (instead of a shell script) to spawn my application, how can I prevent the same spawning of a secondary icon to happen?
Edit: note, I am not running a python script. I am running a custom made python interpreter. py2app is not what I need.
It seems like many use py2app for this purpose with success.
If that doesn't work for you, the Java equivalent I'm famailar with, appbundler, does create a mach-o executable stub to fire things off. You might check out how they do things, particularly their main.m.
I solved the problem, and in hindsight it was rather trivial. In the shell script, I need to invoke my binary with exec, so that the running bash process is replaced (a la execve()) rather than spawning a new process. The only problem is that my interpreter now replaces the icon with the stock one, but I have only one icon in the dock now, and behaves naturally.
I'm making a drawing program with python and pygame. I am trying to incorporate a script-fu thing in which the program opens a python live interpreter upon startup and allows the user to execute commands in the interpreter alongside the graphical interface.
My current strategy is to run the main loop inside its own thread, then have the application opened using a bash script that does 'python -i main.py'
Is this a safe/effective/ideal way of doing this? How can I use locks to ensure that commands coming in from the interpreter are executed between main loop iterations?
This is my first time using threads, so please explain to me like I am 7.
Thanks :)
The interpreter won't cooperate with locks you set (since it doesn't know about them). Thus, you cannot guarantee when the code entered by the user will execute.
Consider using the code module to build your own interactive console (it's really easy!). Then you can do the locking every time you go to execute user input.
Why are you using a third party live interpreter? Do you realize that pygame comes with one built in? The documentation is here. This will eliminate all of your problems quite easily.
I really love python because I love interactive development. There's one area where python appears to fall short, however, and that's in the area of automatically reloading changed files. Basically, what I want to have happen is to be able to modify a python file on-disk and then have my running python instance automatically reload the changed module to allow me to immediately access my changes in the REPL so I can test them out. Basically, I want some sort of watch command.
I happen to use the bpython shell because I think it's the best one available, but this feature is so important to me that I'd be willing to switch to any other python shell that does it right. Is it possible?
Something like tail -f in python + reload().
I really think they should make the tags OS and Python version mandatory though.
If you're trying to "test out" your code, perhaps you should be looking into doing automated unit tests instead of testing your code repeatedly and manually. It'll allow you to test more code quicker and waste less of your precious, precious development time.
Personally, I use unittest with py.test as the runner.
How can you save functions/ classes you've writing in a python interactive session to a file? Specifically, is there a way in pydev / eclipse's interactive session (on a mac) to do this?
I just started learning python - and am enjoying using the interpreter's interactive session for testing and playing with modules I've written. However, I find myself writing functions in the interpreter, which I think, oh it would be cool to save that to my script files. How do I do this?
I tried:
import pickle
pickle.dump(my_function, open("output.p", "w"))
But it seems to be more of a binary serialization, or at least nothing that I could copy and paste into my code...
Are there ways to see the code behind classes & functions I've defined in the interpreter? And then copy them out of the interpreter?
Update:
Ok, here's what I've learned so far:
I missed the easiest of all - PyDev's interactive session in eclipse allows you to right click and save your session. Still have to remove >>>'s, but gets the job done.
IPython is apparently the way to go to do this.
How to save a Python interactive session? has more details.
The best environment for interactive coding sessions has to be IPython, in my opinion. It's built on and extends the basic Python interpreter with a lot of magic, including history. For example, you can issue the command %logstart to dump all subsequent input to a file, which still needs to be edited afterward before it will be a script, but gives you a lot to work with.
When installing IPython, don't forget pyreadline.
In general, however, it is best to write code in an IDE and then run it. IPython helps here as well. If you write and save the script, then use the IPython "run" command to run it, the entire global namespace of the script will be available for inspection in your IPython session. Additionally, you can use the -d argument to run to trigger the pdb debugger immediately on any unhandled exception.
If you're more of a straightlaced IDE and debugger kind of guy, then the easiest and best lightweight environment has to be PyScripter.
I think the answer is to change your workflow.
What I do is write my functions in an editor (emacs), and then press a key combination (Ctrl-c Ctrl-e) to send the region of text to the (i)python interpreter.
That way I can save the function if I want, and also play with it in an interpreter.
Emacs is central to how I do it, but I'm sure there must be similar approaches with many editors (vim, gedit, etc) and IDEs.
PS. Finding a good editor is crucial when working with Python. The editor must be able to move blocks of code to the left and right easily, or the whitespace issue becomes too onerous.
I dislike typing blocks of code in the python interpreter because it doesn't allow me to shift blocks easily. You'll like Python even more when you find the right editor.
You can setup a python history file which stores everything you type into the interpreter.
Here's how:
http://docs.python.org/tutorial/interactive.html
I think it can't be done.
Python can perform instrospection with the inspect module, but the inspect.getsource function won't work without a source file.
This may sound strange, but I need a better way to build python scripts than opening a file with nano/vi, change something, quit the editor, and type in python script.py, over and over again.
I need to build the script on a webserver without any gui. Any ideas how can I improve my workflow?
put this line in your .vimrc file:
:map <F2> :w\|!python %<CR>
now hitting <F2> will save and run your python script
You should give the screen utility a look. While it's not an IDE it is some kind of window manager on the terminal -- i.e. you can have multiple windows and switch between them, which makes especially tasks like this much easier.
You can execute shell commands from within vim.
Using emacs with python-mode you can execute the script with C-c C-c
you can try ipython. using its edit command, it will bring up your editor (nano/vim/etc), you write your script, and then on exiting you're returned to the ipython prompt and the script is automatically executed.
When working with Vim on the console, I have found that using "tabs" in Vim, instead of having multiple Vim instances suspended in the background, makes handling multiple files in Vim more efficient. It takes a bit of getting used to, but it works really well.
You could run XVNC over ssh, which is actually passably responsive for doing this sort of thing and gets you a windowing GUI. I've done this quite effectively over really asthmatic Jetstart DSL services in New Zealand (128K up/ 128K down =8^P) and it's certainly responsive enough for gvim and xterm windows. Another option would be screen, which lets you have multiple textual sessions open and switch between them.
There are actually 2 questions. First is polling for a console IDE for python and the second is a better dev/test/deploy workflow.
For while there are many ways you can write python code in the console, I find a combination of screen, vim and python/ipython is the best as they are usually available on most servers. If you are doing long sessions, I find emacs + python-mode typically involves less typing.
For a better workflow, I would suggest setting up a development environment. You can easily setup a Linux VM on your desktop/laptop easily these days - there isn't a excuse not to even if it's for hobby projects. That opens up a much larger selection of IDEs available to you, such as:
GUI versions of VI and friends
Remote file editing with tramp and testing locally with python-mode inside Emacs
http://www.netbeans.org
and of course http://eclipse.org with the PyDev plugin
I would also setup a SCM to keep track of changes so that you do
better QA and use it to deploy tested changes onto the server.
For example I use Mercurial for my pet projects and I simply tag my repo when it's ready and update the production server to the tag when I deploy. On the devbox, I do:
(hack hack hack, test test test)
hg ci -m 'comment'
hg tag
hg push
Then I jump onto the server and do the following when I deploy:
hg update
restart service/webserver as needed
Well, apart from using one of the more capable console editors (Emacs or vi would come to mind), why do you have to edit it on the web server itself? Just edit it remotely if constant FTP/WebDAV transfer would seem to cumbersome.
Emacs has Tramp Mode, gedit on Linux and bbedit on the Mac support remote editing, too. Probably quite a large number of other editors. In that case you would just edit in on a more capable desktop and restart the script from a shell window.
For what it's worth, VIM alone can do the same tasks as previously posted. I have had the same problem with testing Python from the command line.
My solution was to use the screen command. I split screens vertically, I run Python in one instance of a shell, and on the second screen, I usually edit Python code with VIM.
Command to install screen:
sudo apt-get install screen
The screen package has a bit of a learning curve but there isn't any mystery if you can remember the "Ctrl-Alt ?" command that contains all knowledge.
No GUI is required!