ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 25 Jun 2019 19:27:38 +0200Creating a module in Sagehttps://ask.sagemath.org/question/46994/creating-a-module-in-sage/I am new to Sage, being used to Python and I am having some trouble with adapting.
In particular, I am trying to create a 'module' in the Python sense, i.e. some set of classes that I can call with 'import'.
When I did this in python in the past, I would create a directory (say /my_module/ in my home) and include a file __'__init____.sage' in that file with a line
__all__ = ['Submodule1', 'Submodule2']
Then I would have a file 'Submodule1' and 'Submodule2'.
In the file 'Submodule 1' I would define 'Class1'
and then from a file 'run.py' in home I would write
import my_module
from my_module.Submodule1 import Class1
and then I would be able to write
c=Class1()
to create an object.
I find that this is not working in Sage and the only thing I seem to be able to do is write in run.py
load("my_module/Submodule1.sage")
for each class I want to load. This has many disadvantages, for instance hiding classes from potential users so that they only exist internally.
Any suggestions on how to create and import modules in sage? What is the sage way?
Jesus Martinez GarciaTue, 25 Jun 2019 19:27:38 +0200https://ask.sagemath.org/question/46994/import CSV in python using jupyter notebook on cocalc cloudhttps://ask.sagemath.org/question/38546/import-csv-in-python-using-jupyter-notebook-on-cocalc-cloud/I want to import this [CVS file](https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data) in my python script with the commands:
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = pandas.read_csv(url, names=names)
The problem is that it returns this error message:
---------------------------------------------------------------------------
URLError Traceback (most recent call last)
<ipython-input-21-45b1e7974739> in <module>()
32 url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
33 names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
---> 34 dataset = pandas.read_csv(url, names=names)
/projects/sage/sage-7.6/local/lib/python2.7/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
653 skip_blank_lines=skip_blank_lines)
654
--> 655 return _read(filepath_or_buffer, kwds)
656
657 parser_f.__name__ = name
/projects/sage/sage-7.6/local/lib/python2.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
390 compression = _infer_compression(filepath_or_buffer, compression)
391 filepath_or_buffer, _, compression = get_filepath_or_buffer(
--> 392 filepath_or_buffer, encoding, compression)
393 kwds['compression'] = compression
394
/projects/sage/sage-7.6/local/lib/python2.7/site-packages/pandas/io/common.py in get_filepath_or_buffer(filepath_or_buffer, encoding, compression)
184 if _is_url(filepath_or_buffer):
185 url = str(filepath_or_buffer)
--> 186 req = _urlopen(url)
187 content_encoding = req.headers.get('Content-Encoding', None)
188 if content_encoding == 'gzip':
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in urlopen(url, data, timeout, cafile, capath, cadefault, context)
152 else:
153 opener = _opener
--> 154 return opener.open(url, data, timeout)
155
156 def install_opener(opener):
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in open(self, fullurl, data, timeout)
427 req = meth(req)
428
--> 429 response = self._open(req, data)
430
431 # post-process response
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in _open(self, req, data)
445 protocol = req.get_type()
446 result = self._call_chain(self.handle_open, protocol, protocol +
--> 447 '_open', req)
448 if result:
449 return result
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
405 func = getattr(handler, meth_name)
406
--> 407 result = func(*args)
408 if result is not None:
409 return result
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in https_open(self, req)
1239 def https_open(self, req):
1240 return self.do_open(httplib.HTTPSConnection, req,
-> 1241 context=self._context)
1242
1243 https_request = AbstractHTTPHandler.do_request_
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in do_open(self, http_class, req, **http_conn_args)
1196 except socket.error, err: # XXX what error?
1197 h.close()
-> 1198 raise URLError(err)
1199 else:
1200 try:
URLError: <urlopen error [Errno 110] Connection timed out>
What's the problem?
How could I solve?
I use a free account on CoCalc.
If it takes time to upload and so not enough for my free account, how I can upload the file as worksheet and link it? What will be the link in the URL specification?
ThankspspWed, 16 Aug 2017 16:40:27 +0200https://ask.sagemath.org/question/38546/Using from Python (breaking the monolith)https://ask.sagemath.org/question/25964/using-from-python-breaking-the-monolith/Sage contains a many libraries (or Cython wrappers to libraries) that would be useful in other Python programs (to me anyway). So I would like to use some of them isolated from within Python programs. I understand that this is not easily possible. Is it still possible at all? This is actually not so much about how to use Sage from Python but about carving pieces out of the monolithic structure of Sage. (At the moment it seems like I have to implement all kinds of stuff from scratch which seems silly since much of it is already there.)
Concretely: Among other things I want to compute with matrices that have entries in rings of Laurent series over finite fields.
Now you will say: just write a Sage script rather than a Python script. So here are my reasons to prefer Python with isolated libraries (in increasing order of importance):
1. Practical: putting the full 1.5 GB (?) of sage in home on my office computer pushes me beyond the quota limit.
2. Extendability: Not everything I need is available in Sage. But if I want to extend the code I will have do wade through the Sage code making changes here and there and then defend them against updates (and maybe adjust the build process). This seems like a lot of hassle.
3. Verifiability: At some point my code will (hopefully) produce some solutions to some problem and I will want to say (maybe in a published paper) "these are solutions and they are all solutions to the problem". Then critical minds will not believe me and all I can say is: "Well, you only have to check the correctness of my 200 lines of code (say). -- Plus of course everything in the Sage git repository, plus the libraries it depends on (at least the part that my code uses, which you will have to work out for yourself); and make sure to patch in these changes I made here and there!"Stefan WitzelSat, 28 Feb 2015 10:34:49 +0100https://ask.sagemath.org/question/25964/import sage packages in pythonhttps://ask.sagemath.org/question/9302/import-sage-packages-in-python/An easy way to use sage in python files is demonstrated in the [Sage Tutorial](http://www.sagemath.org/doc/tutorial/programming.html).
#!/usr/bin/env sage -python
import sys
from sage.all import *
if len(sys.argv) != 2:
print "Usage: %s <n>"%sys.argv[0]
print "Outputs the prime factorization of n."
sys.exit(1)
print factor(sage_eval(sys.argv[1]))
Well, what if I don't want to import *all* of sage as shown above using:
from sage.all import *
Instead of this command above, I just want to import the following:
1. Matrix -> type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'
2. vector -> type 'sage.modules.vector_integer_dense.Vector_integer_dense'
3. ZZ -> type 'sage.rings.integer_ring.IntegerRing_class'
4. MixedIntegerLinearProgram -> type 'sage.numerical.mip.MixedIntegerLinearProgram'
So I should be able to write something like this in python
from sage.library.package.for.Matrix import *
from sage.library.package.for.vector import *
from sage.library.package.for.ZZ import *
from sage.library.package.for.MixedIntegerLinearProgram import *
I just don't know what they are. Any help is appreciated.
Thanks.MaelstromYamatoThu, 06 Sep 2012 13:29:57 +0200https://ask.sagemath.org/question/9302/Using Psycopg2 and other libraries in sagehttps://ask.sagemath.org/question/8812/using-psycopg2-and-other-libraries-in-sage/Hi,
Seems like a simple question but have not found a good answer. Best way to use outside python libraries in SAGE in Ubuntu. Install in SAGE foulder if so (how?). Link in an IDE. I know how to do this in Pydev.
Thanks,
Matthewmcm3Thu, 22 Mar 2012 02:20:21 +0100https://ask.sagemath.org/question/8812/