Re: [sage-devel] Global namespace and independent python packages

2016-06-10 Thread Travis Scrimshaw
Something else you can do on the Sage side is to have something like:

try:
from sage.foo import Bar
except ImportError:
pass

So it only gets included into the global namespace if you have the module 
installed.

Best,
Travis

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-devel] Global namespace and independent python packages

2016-06-09 Thread Andrew


On Thursday, 9 June 2016 17:10:44 UTC+2, Johan S. R. Nielsen wrote:
>
> > from sage.misc.misc import inject_variable 
> > from mytest.test import atest 
> > inject_variable('atest',atest) 
> > 
> > *Question*: do people think that it is reasonable for a package to do 
> this? 
>
> I would also say "No". The reason is that if that is what the user 
> wanted, he could just have done "from mytest import *". If he explicitly 
> does "import mytest" it's because he wants the control over the 
> namespace 

 
Sorry, I didn't fully appreciate your comment above when I first read it. I 
agree, this is fairly compelling.
Andrew 

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-devel] Global namespace and independent python packages

2016-06-09 Thread Johan S . R . Nielsen
> from sage.misc.misc import inject_variable
> from mytest.test import atest
> inject_variable('atest',atest)
>
> *Question*: do people think that it is reasonable for a package to do this?

I would also say "No". The reason is that if that is what the user
wanted, he could just have done "from mytest import *". If he explicitly
does "import mytest" it's because he wants the control over the
namespace.

Best,
Johan

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-devel] Global namespace and independent python packages

2016-06-09 Thread Erik Bray
On Thu, Jun 9, 2016 at 3:42 PM, Andrew  wrote:
> Thanks Johan.As much as anything I wanted to know that I wasn't doing
> something stupid.
>
> On Thursday, 9 June 2016 14:02:16 UTC+2, Johan S. R. Nielsen wrote:
>>
>> As a package author, you shouldn't force the user to load the functions
>> into
>> the global namespace. However, it might be possible by getting hold of the
>> globals() object or something.
>
>
> Well, actually, if some one explicitly loads the package using something
> like "import mytest" then I think that it is reasonable to load the main
> functions from the package it the global namespace. Thinking about it, sage
> already does something similar (but more explicit) with methods like
> inject_shorthands. Drilling down a little I realised that I can do what I
> want by putting the following into my __init__.py file:
>
> from sage.misc.misc import inject_variable
> from mytest.test import atest
> inject_variable('atest',atest)
>
> Question: do people think that it is reasonable for a package to do this?

No. Definitely not. Never.

> Arguments against include that packages "shouldn't force the user to load
> the functions into
> the global namespace" and that this may lead to clashes in the namespace. An
> argument in favour of doing this is that this a more user-friendly and a
> user is unlikely to import a package if they don't want to use it.

Sage doesn't do this either.  If you load a normal Python interpreter,
'import sage' does not do this.  It just happens that the specialized
sage interpreter does that at startup time, but that's not normal
behavior for a Python package.

If there's a way to detect if you're in the sage interpreter it
*might* be okay to do this only in that case depending on how other
sage users feel (I don't know).  But otherwise it's something that
should be specified manually in init.sage.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-devel] Global namespace and independent python packages

2016-06-09 Thread Andrew
Thanks Johan.As much as anything I wanted to know that I wasn't doing 
something stupid.

On Thursday, 9 June 2016 14:02:16 UTC+2, Johan S. R. Nielsen wrote:
>
> As a package author, you shouldn't force the user to load the functions 
> into 
> the global namespace. However, it might be possible by getting hold of the 
> globals() object or something. 
>
 
Well, actually, if some one explicitly loads the package using something 
like "import mytest" then I think that it is reasonable to load the main 
functions from the package it the global namespace. Thinking about it, sage 
already does something similar (but more explicit) with methods like 
inject_shorthands. Drilling down a little I realised that I can do what I 
want by putting the following into my __init__.py file:

from sage.misc.misc import inject_variable
from mytest.test import atest
inject_variable('atest',atest)

*Question*: do people think that it is reasonable for a package to do this?

Arguments against include that packages "shouldn't force the user to load 
the functions into 
the global namespace" and that this may lead to clashes in the namespace. 
An argument in favour of doing this is that this a more user-friendly and a 
user is unlikely to import a package if they don't want to use it.

Andrew


-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-devel] Global namespace and independent python packages

2016-06-09 Thread Johan S . R . Nielsen
Hi Andrew,

To import into the global namespace use

sage: from mytest import *

As a package author, you shouldn't force the user to load the functions into
the global namespace. However, it might be possible by getting hold of the
globals() object or something.

If you want to import the package into the global namespace *on your
own setup*, you could put the "from mytest import *" in the file
~/.sage/init.sage.

Best,
Johan


Andrew writes:

> I have been playing around with ta stand-alone python package for sage. It 
> works
> really well except that I have not found a "nice" way to import classes into 
> the
> global namespace. I suspect that it is just something in the way that I have
> configured things, so I've distilled my code down to a minimal example in the
> hope that some one can tell me how to fix it.
>
> My package layout is:
> package/
> |- setup.py
> |- mytest/
> |- __init__.py
> |- test.py
>
> where the files setup.py, __init__.py and test.py are as follows:
>
> setup.py
> from setuptools import setup
> setup(name='mytest', packages=['mytest'])
>
> __init__.py
> from __future__ import absolute_import
> from .test import atest
> from mytest import *
>
> test.py
> def atest():
> print 'A working test'
>
> To install this "package" you can either use plain setup tools with
> sage setup.py install
>
> but I decided to pip instead, installing it "locally" and in development mode 
> with:
>
> sage -pip install -e --user .
>
> This works fine in that I can start sage and import and use the package, but 
> not
> quite as easily as I'd like:
>
> sage: import mytest
> sage: mytest.atest()
> A working test
> sage: atest()
> ---
> NameError Traceback (most recent call last)
>  in ()
> > 1 atest()
>
> NameError: name 'atest' is not defined
> sage: from mytest import atest # manually import atest
> sage: atest()
> A working test
>
> I thought that `atest` would be automatically available from the global
> namespace because of either the second and or the third lines of __init__.py.
> Instead, it still needs to be manually imported. Is there a way to put atest
> into the global namespace when the package is imported?
>
> Of course, it would be even better if the mytest package was automatically
> imported into the global namespace at run-time.
>
> Andrew


-- 

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.