Re: Pandas printing in jupyter

2018-01-16 Thread Rustom Mody
On Sunday, January 14, 2018 at 3:28:02 AM UTC+5:30, [email protected] wrote:
> Rustom Mody  writes:
> 
> > Specifically and for starters, I want a numpy array — lets say 2D to
> > start with — to be displayed(displayable) as elegantly as sympy does
> > to (its) matrices
> 
> import numpy as np
> from IPython.display import Latex
> 
> def prmat(mat):
> return (r'\begin{bmatrix}' +
> r'\\'.join('&'.join('%f'%x for x in row) for row in mat) +
> r'\end{bmatrix}'
> 
> a = np.arange(12).reshape((3, 4))+1
> display(Latex(prmat(a)))
> 
> you could add optional arguments to modify the type of brackets and the
> formatting string

Thanks

Well I had to tiny-tweak the code (import the display)
---

import numpy as np
from IPython.display import Latex, display

def prmat(mat):
return (r'\begin{bmatrix}' +
r'\\'.join('&'.join('%f'%x for x in row) for row in mat) +
r'\end{bmatrix}' )

a = np.arange(12).reshape((3, 4))+1
display(Latex(prmat(a)))
---

After that it works… for 5 seconds!!

ie it shows a nice centered matrix like a math-display in latex
Then it goes away and I see a left aligned bunch of unicode boxes!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pandas printing in jupyter

2018-01-16 Thread Rustom Mody
On Tuesday, January 16, 2018 at 5:10:14 PM UTC+5:30, Rustom Mody wrote:
> On Sunday, January 14, 2018 at 3:28:02 AM UTC+5:30, [email protected] wrote:
> > Rustom Mody  writes:
> > 
> > > Specifically and for starters, I want a numpy array — lets say 2D to
> > > start with — to be displayed(displayable) as elegantly as sympy does
> > > to (its) matrices
> > 
> > import numpy as np
> > from IPython.display import Latex
> > 
> > def prmat(mat):
> > return (r'\begin{bmatrix}' +
> > r'\\'.join('&'.join('%f'%x for x in row) for row in mat) +
> > r'\end{bmatrix}'
> > 
> > a = np.arange(12).reshape((3, 4))+1
> > display(Latex(prmat(a)))
> > 
> > you could add optional arguments to modify the type of brackets and the
> > formatting string
> 
> Thanks
> 
> Well I had to tiny-tweak the code (import the display)
> ---
> 
> import numpy as np
> from IPython.display import Latex, display
> 
> def prmat(mat):
> return (r'\begin{bmatrix}' +
> r'\\'.join('&'.join('%f'%x for x in row) for row in mat) +
> r'\end{bmatrix}' )
> 
> a = np.arange(12).reshape((3, 4))+1
> display(Latex(prmat(a)))
> ---
> 
> After that it works… for 5 seconds!!
> 
> ie it shows a nice centered matrix like a math-display in latex
> Then it goes away and I see a left aligned bunch of unicode boxes!

Inspired by this I tried this
It works… kinda… but the matrix columns dont align

def prmat(mat):
return (r'\begin{bmatrix}' +
r'\\'.join('&'.join('%d'%x for x in row) for row in mat) +
r'\end{bmatrix}' )

matprefix = """
  

  [
  
"""
rowprefix = """
"""
elemfmt = """%d
"""
rowsuffix  = """
"""
matsuffix = """  
  ]

  
"""
def prmht(mat):
return (matprefix + "".join(prmhtrow(r) for r in mat) + matsuffix)

def prmhtrow(row):
return rowprefix + "".join(elemfmt%x for x in row) + rowsuffix

display(HTML(prmht(a)))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pandas printing in jupyter

2018-01-16 Thread Rustom Mody
On Tuesday, January 16, 2018 at 6:04:06 PM UTC+5:30, Rustom Mody wrote:
Had missed the mtd element

ie changing
elemfmt = """%d
"""
to 

elemfmt = """%d
"""
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: requests / SSL blocks forever?

2018-01-16 Thread Nagy László Zsolt

>> Or maybe I misunderstood the docs and the timeout means the max. time
>> elapsed between receiving two chunks of data from the server?
> Yes. It's documented better here:
> http://docs.python-requests.org/en/master/user/advanced/#timeouts
>
> You can't specify a "total time" within which the operation must
> succeed or be abandoned, but if you specify a timeout and the
> server stops responding, either at the start of the process or
> in the middle of the response, then it will time out after the
> specified delay.
Very good, thank you! Setting the timeout solved the problem for now. :-)

-- 
https://mail.python.org/mailman/listinfo/python-list


Right way to io.open(...) an existing file object in Python 2.7?

2018-01-16 Thread Skip Montanaro
I'd like to take advantage of the seekable() method of io.IOBase with
existing open file objects, especially the standard in/out/err file
objects. I know on Linux I can just do this:

fp = io.open("/dev/stderr")

but that's kind of cheating. File objects in Python 2.7 aren't created
using the io module's machinery. Assume somewhere else a plain old
open file object, for example:

   fp = open("/some/path/to/nowhere.txt", "w")

and wanted to treat fp as if it had been opened with the io module
machinery, I don't see any sort of "fdopen" or "freopen" equivalent
mentioned in the io module documentation. Is this possible in a clean
way?

Thx,

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Right way to io.open(...) an existing file object in Python 2.7?

2018-01-16 Thread Lele Gaifax
Skip Montanaro  writes:

> I don't see any sort of "fdopen" or "freopen" equivalent mentioned in the io
> module documentation. Is this possible in a clean way?
>

There is an os.fdopen(), so maybe

  newf = os.fdopen(fp.fileno())

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
[email protected]  | -- Fortunato Depero, 1929.

-- 
https://mail.python.org/mailman/listinfo/python-list


documentation on read.encode

2018-01-16 Thread Larry Martell
Looking for 2.7 docs on read.encode - googling did not turn up anything.

Specifically, looking for the supported options for base64, and how to
specify them, e.g. Base64.NO_WRAP
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Right way to io.open(...) an existing file object in Python 2.7?

2018-01-16 Thread Jon Ribbens
On 2018-01-16, Skip Montanaro  wrote:
> I'd like to take advantage of the seekable() method of io.IOBase with
> existing open file objects, especially the standard in/out/err file
> objects.

If it's difficult to imagine a circumstance in which you would want to
seek on stdio/out/err where you were not making some considerable error.
But they are already io objects so you can just call sys.stdin.seekable
or whatever.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Right way to io.open(...) an existing file object in Python 2.7?

2018-01-16 Thread Skip Montanaro
> If it's difficult to imagine a circumstance in which you would want to
> seek on stdio/out/err where you were not making some considerable error.
> But they are already io objects so you can just call sys.stdin.seekable
> or whatever.

Alas, in Python 2.7 sys.stdin/stdout/stderr are traditional (legacy?)
file objects created using the open() built-in function. I was hoping
there would be a clean way to transmogrify such objects into objects
which support the io.IOBase APIs. I already know that I can attempt to
seek(), then catch the IOError, but I was hoping to start using the
more modern io module's seekable() API in a specific module I maintain
without having to worry about what type of file-ish object I was
passed. Data flow might look like this:

File or io.IOBase object -> passed into my code -> transmogrified into
io.IOBase object at the boundary, if necessary -> resulting in io
nirvana in the internals of my module. :-)

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Right way to io.open(...) an existing file object in Python 2.7?

2018-01-16 Thread Skip Montanaro
>> I don't see any sort of "fdopen" or "freopen" equivalent mentioned in the io
>> module documentation. Is this possible in a clean way?
>>
>
> There is an os.fdopen(), so maybe
>
>   newf = os.fdopen(fp.fileno())

Correct, but that just returns another File object which will still
lack the API provided by io.IOBase. I'm talking Python 2.7 here. In
Python 3.x, all I/O is mediated by that API.

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: documentation on read.encode

2018-01-16 Thread Larry Martell
On Tue, Jan 16, 2018 at 12:00 PM, Larry Martell  wrote:
> Looking for 2.7 docs on read.encode - googling did not turn up anything.
>
> Specifically, looking for the supported options for base64, and how to
> specify them, e.g. Base64.NO_WRAP

So I just realized that encode() is not a method of read() it's a
string method. But I still have the same question - can I pass in any
flags?

My issue is that I am base64 encoding PNG images on linux and it's
putting a LF at the end of each line. If I do the same on Windows it's
putting CR/LF. I want the files to be encoded with no platform
dependences. Googling I found mention of Base64.NO_WRAP and I want to
pass that into encode() - can I do that?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Right way to io.open(...) an existing file object in Python 2.7?

2018-01-16 Thread eryk sun
On Tue, Jan 16, 2018 at 4:00 PM, Skip Montanaro
 wrote:
> I'd like to take advantage of the seekable() method of io.IOBase with
> existing open file objects, especially the standard in/out/err file
> objects.

io.open can open a file descriptor. If you don't use a duplicated FD
(os.dup), then you probably also want the option `closefd=False`. For
example:

$ cat test.py
import sys
import io

stdin = io.open(sys.stdin.fileno(), closefd=False)
print 'stdin seekable: %s' % stdin.seekable()

$ python test.py
stdin seekable: False

$ echo spam | python test.py
stdin seekable: False

$ python test.py < test.py
stdin seekable: True
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: documentation on read.encode

2018-01-16 Thread Gene Heskett
On Tuesday 16 January 2018 14:19:38 Larry Martell wrote:

> On Tue, Jan 16, 2018 at 12:00 PM, Larry Martell 
 wrote:
> > Looking for 2.7 docs on read.encode - googling did not turn up
> > anything.
> >
> > Specifically, looking for the supported options for base64, and how
> > to specify them, e.g. Base64.NO_WRAP
>
> So I just realized that encode() is not a method of read() it's a
> string method. But I still have the same question - can I pass in any
> flags?
>
> My issue is that I am base64 encoding PNG images on linux and it's
> putting a LF at the end of each line. If I do the same on Windows it's
> putting CR/LF. I want the files to be encoded with no platform
> dependences. Googling I found mention of Base64.NO_WRAP and I want to
> pass that into encode() - can I do that?

Di you not have the manpages installed?

In my copy of the manpage:
base64 [OPTION]... [FILE]
where option is:
 -w, --wrap=COLS
  wrap encoded lines after COLS character (default 76).  Use 
0 to disable line wrapping.

Seems pretty simple.

Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: documentation on read.encode

2018-01-16 Thread Larry Martell
On Tue, Jan 16, 2018 at 2:35 PM, Gene Heskett  wrote:
> On Tuesday 16 January 2018 14:19:38 Larry Martell wrote:
>
>> On Tue, Jan 16, 2018 at 12:00 PM, Larry Martell
>  wrote:
>> > Looking for 2.7 docs on read.encode - googling did not turn up
>> > anything.
>> >
>> > Specifically, looking for the supported options for base64, and how
>> > to specify them, e.g. Base64.NO_WRAP
>>
>> So I just realized that encode() is not a method of read() it's a
>> string method. But I still have the same question - can I pass in any
>> flags?
>>
>> My issue is that I am base64 encoding PNG images on linux and it's
>> putting a LF at the end of each line. If I do the same on Windows it's
>> putting CR/LF. I want the files to be encoded with no platform
>> dependences. Googling I found mention of Base64.NO_WRAP and I want to
>> pass that into encode() - can I do that?
>
> Di you not have the manpages installed?
>
> In my copy of the manpage:
> base64 [OPTION]... [FILE]
> where option is:
>  -w, --wrap=COLS
>   wrap encoded lines after COLS character (default 76).  Use
> 0 to disable line wrapping.
>
> Seems pretty simple.

But how do I use that in read().encode('base64')?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: documentation on read.encode

2018-01-16 Thread Ned Batchelder

On 1/16/18 2:19 PM, Larry Martell wrote:

On Tue, Jan 16, 2018 at 12:00 PM, Larry Martell  wrote:

Looking for 2.7 docs on read.encode - googling did not turn up anything.

Specifically, looking for the supported options for base64, and how to
specify them, e.g. Base64.NO_WRAP

So I just realized that encode() is not a method of read() it's a
string method. But I still have the same question - can I pass in any
flags?

My issue is that I am base64 encoding PNG images on linux and it's
putting a LF at the end of each line. If I do the same on Windows it's
putting CR/LF. I want the files to be encoded with no platform
dependences. Googling I found mention of Base64.NO_WRAP and I want to
pass that into encode() - can I do that?


Base64.NO_WRAP seems to be a Java thing.

--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: documentation on read.encode

2018-01-16 Thread Larry Martell
On Tue, Jan 16, 2018 at 3:17 PM, Ned Batchelder  wrote:
> On 1/16/18 2:19 PM, Larry Martell wrote:
>>
>> On Tue, Jan 16, 2018 at 12:00 PM, Larry Martell 
>> wrote:
>>>
>>> Looking for 2.7 docs on read.encode - googling did not turn up anything.
>>>
>>> Specifically, looking for the supported options for base64, and how to
>>> specify them, e.g. Base64.NO_WRAP
>>
>> So I just realized that encode() is not a method of read() it's a
>> string method. But I still have the same question - can I pass in any
>> flags?
>>
>> My issue is that I am base64 encoding PNG images on linux and it's
>> putting a LF at the end of each line. If I do the same on Windows it's
>> putting CR/LF. I want the files to be encoded with no platform
>> dependences. Googling I found mention of Base64.NO_WRAP and I want to
>> pass that into encode() - can I do that?
>
>
> Base64.NO_WRAP seems to be a Java thing.

Yeah I saw it mentioned in a SO post that was java related. Wondering
if there is some way to do the same in python.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: documentation on read.encode

2018-01-16 Thread Jon Ribbens
On 2018-01-16, Larry Martell  wrote:
> Yeah I saw it mentioned in a SO post that was java related. Wondering
> if there is some way to do the same in python.

base64.b64encode(foo)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: documentation on read.encode

2018-01-16 Thread MRAB

On 2018-01-16 19:52, Larry Martell wrote:

On Tue, Jan 16, 2018 at 2:35 PM, Gene Heskett  wrote:

On Tuesday 16 January 2018 14:19:38 Larry Martell wrote:


On Tue, Jan 16, 2018 at 12:00 PM, Larry Martell

 wrote:

> Looking for 2.7 docs on read.encode - googling did not turn up
> anything.
>
> Specifically, looking for the supported options for base64, and how
> to specify them, e.g. Base64.NO_WRAP

So I just realized that encode() is not a method of read() it's a
string method. But I still have the same question - can I pass in any
flags?

My issue is that I am base64 encoding PNG images on linux and it's
putting a LF at the end of each line. If I do the same on Windows it's
putting CR/LF. I want the files to be encoded with no platform
dependences. Googling I found mention of Base64.NO_WRAP and I want to
pass that into encode() - can I do that?


Di you not have the manpages installed?

In my copy of the manpage:
base64 [OPTION]... [FILE]
where option is:
 -w, --wrap=COLS
  wrap encoded lines after COLS character (default 76).  Use
0 to disable line wrapping.

Seems pretty simple.


But how do I use that in read().encode('base64')?

Use the base64 module instead, which is also how you would do it in 
Python 3.


If you're getting CR/LF on Windows, that's because you're opening the 
file in text mode. In both Python 2 and Python 3 the base64 string will 
be a bytestring, which you'd write out to a file opened in binary mode.


That's an extra bit of future-proofing! :-)

---
This email has been checked for viruses by AVG.
http://www.avg.com

--
https://mail.python.org/mailman/listinfo/python-list


[ANN] python-ghostscript 0.6

2018-01-16 Thread Hartmut Goebel
I'm pleased to announce the new release vor python-ghostscript:


 python-ghostscript 0.6

A Python-Interface to the Ghostscript C-API using ctypes

:License: GNU Public License v3 (GPLv3)
:Author:  Hartmut Goebel 
:Homepage: https://gitlab.com/pdftools/python-ghostscript
:Download: https://pypi.python.org/pypi/ghostscript

`Ghostscript`__, is a well known interpreter for the PostScript
language and for PDF. This package implements a interface to the
Ghostscript C-API using `ctypes`__. Both a low-level and a pythonic,
high-level interface are provided.

__ http://www.ghostscript.com/
__ http://docs.python.org/library/ctypes.html

This package is currently tested only under GNU/Linux. Please report
whether it works in your environment, too. Thanks.


Latest Changes


Please note: Version 0.5 has been skipped to avoid version conflicts
with the fork `python3-ghostscript`. All changes of that fork are
integrated into this release.

  * Add support for Python 3.x (tested with Python 3.4).
    Minimum required Python version is now 2.7.

  * Add support for display callback and an example program
    using it. Thanks to Lasse Fister.

  * Add context-interface (for the ``with``-statement).

  * ``GhostscriptError`` now has an attribute ``code`` holding
    the numeric error-code.

  * ``Ghostscript()`` now accepts keyword-arguments ``stdin``,
    ``stdout``, ``stderr`` for setting the respective stream. This was
    already implementd in version 0.4.1, but not documented.

  * Add unittest suite (using pytest).

  * Several bug fixes and smaller changes.

  * Switch version control to git and move project to gitlab. See
    Readme-file for the new URL.

  * Set up continuous integration tests.


Example


Here is an example for how to use the high-level interface of
`python-ghostscript`. This implements a very basic ps2pdf-tool::

  import sys
  import ghostscript

  args = [
  "ps2pdf",    # actual value doesn't matter
  "-dNOPAUSE", "-dBATCH", "-dSAFER",
  "-sDEVICE=pdfwrite",
  "-sOutputFile=" + sys.argv[1],
  "-c", ".setpdfwrite",
  "-f",  sys.argv[2]
  ]

  ghostscript.Ghostscript(*args)

-- 
Regards
Hartmut Goebel

| Hartmut Goebel  | [email protected]   |
| www.crazy-compilers.com | compilers which you thought are impossible |

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Right way to io.open(...) an existing file object in Python 2.7?

2018-01-16 Thread Skip Montanaro
io.open can open a file descriptor.


Ah, thanks. I'd missed that in my initial reading of the io.open
documentation.

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list