Subject: Re: [nmr_sparky] Re: show peaks in all spectra with one click
From: Tom Goddard
Date: Apr 23, 2010

Previous: 690 Next: 692


To import a module myfile.py that you have in ~/Sparky/Python use

from sparky import myfile

The ~/Sparky/Python directory is not added to the global Python search path sys.path. Instead it is added to the sparky module search path, sparky.__path__. I agree this is odd and confusing. The purpose of it is to allow you to override Python modules that are included in the Sparky distribution by simply putting a different copy of the python code in ~/Sparky/Python.

To start the Sparky Python shell when Sparky starts use a ~/Sparky/Python/sparky_init.py file that looks like:

def initialize_session(session):
import sparky.pythonshell
sparky.pythonshell.show_python_shell(session)

Writing Python extension to Sparky is not well documented. You will need to look at the Sparky distribution file and the Sparky python modules:

sparky/python/README
sparky/python/sparky/*.py

On the Mac these are found in

Sparky.app/Contents/Resources/python

and you can look inside the Sparky.app folder by ctrl-click on the icon and choosing Show Package Contents from the pop-up menu.

Tom


-------- Original Message --------

Thanks for the reply. Im a little confused. Here is what I read from the manual:

Here is how to modify a Sparky extension to do something new. Copy the Python code for the extension to a directory named Python under your home Sparky directory. ... You can have your extension loaded every time you start Sparky by adding the above import command to a file called sparky_init. py in your Python directory.

So it seems that the directory /sparkyhome/ Python should be included in the system, right? In fact, when I create the sparky_init. py in this folder, it was executed. Then why other python script wont be found in the same directory? By the way, is there a way to ask sparky to open python shell everytime it starts automatically?

--- In nmr_sparky@yahoogro ups.com , Tom Goddard goddard@... wrote:

Importing anything inside a Python function doesnt put it in the global
namespace. To import my.py located in /home/blah use

import sys
sys.path.insert( 0, /home/blah )
import my

You need to understand Python better. These questions have nothing to
do with Sparky. Please read some Python tutorial and post questions to
the Sparky group only if they are specific to Sparky.

Tom



Thanks for the information.

As for the customized module, here is what I tried:
1. create my.py in $SPARKYHOME/ Python:
def test(session) :
print load my.py

2. create spark_init.py in the same folder as:
def initialize_session( session):
print hello
import my
pass

3. open python shell in sparky, I did see hello, but when I type
test or my.test, I got similar error message:
test
Traceback (most recent call last):
File
/usr/local/ src/sparky/ platforms/ linux-2.6. 9/sparky- install/python/ sparky/pythonshe ll.py,
line 150, in execute_command
File stdin, line 1, in ?
NameError: name test is not defined
my.test
Traceback (most recent call last):
File
/usr/local/ src/sparky/ platforms/ linux-2.6. 9/sparky- install/python/ sparky/pythonshe ll.py,
line 150, in execute_command
File stdin, line 1, in ?
NameError: name my is not defined

what did I do wrong?

--- In nmr_sparky@yahoogro ups.com
mailto:nmr_ sparky%40yahoogr oups.com , Tom Goddard goddard@ wrote:

Sparky does not add $SPARKYHOME/ Python to the Python path so it isnt
going to find your my.py file. You could insert the path in the Python
sys.path variable. But probably more useful to you is that Sparky will
import the file

$SPARKYHOME/ Python/sparky_ init.py

when it starts and run the routine named initialize_session( ). So if in
the sparky_init. py file you put

def initialize_session( session):

print hello world

Then you will see hello world in the Sparky Python shell (menu
Extensions / Python Shell, shortcut py). The session argument is the
Sparky session object and is described in the README file included
with Sparky

sparky/python/ README

or on Mac

Sparky.app/Contents /Resources/ python/README

Ive attached that file for your amusement.

As for modifications to peak table mouse behavior you are looking in
the
right place in peaktable.py, and the bind routine is setting the
mouse
binding which currently calls goto_assignment_ cb() which calls
clicked_assignment( ) to find the spectrum you clicked on. With some
Python knowledge and studying the above code it is not hard to
modify it
as you desire.

Tom



I just got hold of sparky and loved it immediately! While playing
with
it, I met a situation I think one can probably improve the efficiency
dramatically. In the Peak Table window, we can double-left- click or
single-middle- click at the left side of a peak in particular spectrum
to center the spectrum with that peak. If there are more than one
spectrum, one need to click individual peaks to center them in
different spectra window. Im wondering whether one can set a
different mouse event to center all the peaks in their spectrum,
respectively, for example, set single-middle- click or
single-right- click for this purpose. I believe people would benefit a
lot from this small modification.

I tried to understand the peaktable.py but it is quite complicated
for
a python newbie. By the way, how exactly I can add my own python
script in sparky? Here is what I tried:
1. create a file my.py with following lines
def hello ():
print (hello the world)
2. save this file in /home/Sparky/ Python and my SPARKYHOME is
/home/Sparky
3. open sparky and python shell, type import my and get error
message:
Traceback (most recent call last):
File

/usr/local/ src/sparky/ platforms/ linux-2.6. 9/sparky- install/python/ sparky/pythonshe ll.py,

line 150, in execute_command
File stdin, line 1, in ?
ImportError: No module named my

Thanks in advance