Re: [Tutor] If tuple cannot be sorted, then why sorted() on a tuple is fine?

2017-08-10 Thread Peter Otten
C W wrote:

> This is a follow up. I actually ran into this today:
> 
> import numpy as np
> xArray = np.ones((3, 4))
> 
>> xArray.shape
> (3, 4)
>> np.shape(xArray)
> (3, 4)
> 
> It was confusing to see that both xArray.shape and np.shape() worked. Are
> they equivalent?

>>> print(inspect.getsource(numpy.shape))
def shape(a):
"""
Return the shape of an array.

Parameters
--
a : array_like
Input array.

Returns
---
shape : tuple of ints
The elements of the shape tuple give the lengths of the
corresponding array dimensions.

See Also

alen
ndarray.shape : Equivalent array method.

Examples

>>> np.shape(np.eye(3))
(3, 3)
>>> np.shape([[1, 2]])
(1, 2)
>>> np.shape([0])
(1,)
>>> np.shape(0)
()

>>> a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
>>> np.shape(a)
(2,)
>>> a.shape
(2,)

"""
try:
result = a.shape
except AttributeError:
result = asarray(a).shape
return result

So no; numpy.shape() tries to convert unshapely objects to an array ;)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] setup.py "script" vs "console_scripts" Was: if __name__=='main' vs entry points: What to teach new comers?

2017-08-10 Thread Chris Warrick
On 9 August 2017 at 23:15, Steven D'Aprano  wrote:
> On Tue, Aug 08, 2017 at 12:56:56PM +0200, Chris Warrick wrote:
>
>> While setuptools is not officially part of the stdlib,
>
> This is the critical factor. How can you use *by default* something that
> is *NOT* supplied by default?
>
> Obviously you cannot. It is physically impossible.


The problem with setuptools (and pip) is that they are not first-party
stdlib members, but they are not third-party packages either. They’re
somewhere in between. They have been blessed by the core developers.
And yes, setuptools might be in all the places you mentioned:

> But this does NOT hold for everyone, possibly not even for the majority
> of Python users. For example:
>
> - students using their school's computers;
>
> - corporate and government users using a SOE (Standard Operating
>   Environment);
>
> - people using a system where, for policy reasons, only the
>   standard library is permitted.

* If those computers run Windows (as they often do) and run a recent
Python version (3.4 or newer/2.7.9 or newer), setuptools will be
installed, unless the IT people explicitly disabled ensurepip.
* On macOS, setuptools will be installed if they’re using the system
Python, the python.org installers (which are not uninstallable), or
Python from Homebrew. The last two also have pip, and system Python
has ensurepip.
* On Linux, setuptools/pip is likely to be there, but it’s not
required in all distributions. (Fedora mandates setuptools; Debian
even rips out ensurepip by default and hides it in python3-venv
because reasons…)

If the users are meant to install Python packages, their system
administrators would take care of that — either by setting up
setuptools/pip and perhaps virtualenv, or taking install requests from
users. If users are not supposed to be running setuptools/pip, they
probably shouldn’t, but they can still install it from ensurepip or
downloading get-pip.py.

> I've worked in places where installing unauthorized software was a
> firing offence.

Those people don’t need setuptools. Those people should not be using
distutils either. They might not even be allowed to download packages
and run __main__.py without installation.

-- 
Chris Warrick 
PGP: 5EAAEA16
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What exactly does the three dots do? Why such as thing?

2017-08-10 Thread Steven D'Aprano
On Wed, Aug 09, 2017 at 12:06:37PM -0400, C W wrote:
> Dear Python experts,
> 
> What exactly does the three dots do?
> > aList = ...

... is literal syntax for the Ellipsis singleton object.

Ellipsis was added to the language at the express request of the numpy 
developers. Although numpy is a third-party project outside of the 
standard library, it is big enough and important enough that their 
requests carry a LOT of weight with the core Python devs. Other features 
Python has that were originally added at the request of numpy include:

- extended slicing with two colons obj[a:b:c]

- the @ operator used by numpy for matrix multiplication.

I don't know what Ellipsis is used for by numpy, but now it makes a 
convenient pseudo-pass command:

class X:
...

def func():
...


> It's an ellipsis, a spot holder to later. But what data type is it: vector,
> matrix?

Its a singleton object. Think of it as a sibling to None and 
NotImplemented, but with optional funny syntactic sugar ... to refer to 
it.

None is a special value used as "no such value", or nil or null;

NotImplemented is a special value used by operator dunder methods like 
__add__ and __mul__ to mean "I can't handle this argument";

Ellipsis is a special value used by numpy to mean whatever it is that 
numpy uses it to me.


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How does len() compute length of a string in UTF-8, 16, and 32?

2017-08-10 Thread Steven D'Aprano
On Mon, Aug 07, 2017 at 10:04:21PM -0500, Zachary Ware wrote:

> Next, take a dive into the wonderful* world of Unicode:
> 
> https://nedbatchelder.com/text/unipain.html
> https://www.youtube.com/watch?v=7m5JA3XaZ4k

Another **Must Read** resource for unicode is:

The Absolute Minimum Every Software Developer Absolutely Positively Must 
Know About Unicode (No Excuses!)

https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/

(By the way, it is nearly 14 years later, and PHP still believes that 
the world is ASCII.)


Python 3 makes Unicode about as easy as it can get. To include a unicode 
string in your source code, you just need to ensure your editor saves 
the file as UTF-8, and then insert (by whatever input technology you 
have) the character you want. You want a Greek pi?

pi = "π"

How about an Israeli sheqel?

money = "₪1000"

So long as your editor knows to save the file in UTF-8, it will Just 
Work.


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What exactly does the three dots do? Why such as thing?

2017-08-10 Thread Steven D'Aprano
On Thu, Aug 10, 2017 at 09:39:02AM -0400, C W wrote:

> What's a literal? The only other time I heard about it was studying
> Shakespare. ;)

A "literal" is syntax that creates a value, without the programmer 
needing to call a function. The syntax stands for the LITERAL value as 
shown.

For example, we write:

number = 123  # stands for literally 123

rather than:

number = int(hundreds=1, tens=2, units=3)

Examples of literals:

"Hello World!"  # a string
1.234  # a float
999  # an int
True  # bool true value
None  # the special None value


Technically, these aren't literals, but they're the moral equivalent of 
literals:

[1, 2, None, "Hello", 23]  # a list
(1, 2, 3)  # a tuple
{'key': 'value'}  # a dict


> I don't know what literal is. So, it won't help me to understand ellipsis,
> I really thought it was that oval shaped figure.

Ellipsis \El*lip"sis\ ([e^]l*l[i^]p"s[i^]s), n.; pl. Ellipses

  (Gram.) Omission; a figure of syntax, by which one or more
  words, which are obviously understood, are omitted; as,
  the virtues I admire, for, the virtues which I admire.
  [1913 Webster]

  (Printing) a printing symbol, usually three periods in a
  row (. . .), indicating the omission of some part of a
  text; -- used commonly in quotations, so as to suppress
  words not essential to the meaning. A long dash (---) and
  three asterisks (* * *) are sometimes used with the same
  meaning.


There's also an older (now obsolete) meaning of "ellipsis" as a synonym 
for "ellipse", which is an oval-shaped figure.


> Wiki says: "Literals are often used to initialize variables"

As in:

n = 0
x = 1.5



Outside of numpy, I've never seen anyone use Ellipsis (whether spelled 
by name or by three dots) except to be cute. I'm sure it has use to some 
people, otherwise it wouldn't have been added, but its pretty 
specialized.




-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What exactly does the three dots do? Why such as thing?

2017-08-10 Thread C W
What's a literal? The only other time I heard about it was studying
Shakespare. ;)

I don't know what literal is. So, it won't help me to understand ellipsis,
I really thought it was that oval shaped figure.

Wiki says: "Literals are often used to initialize variables"

https://en.wikipedia.org/wiki/Literal_(computer_programming)

I suppose it's just a place holder, though I don't know when I would use it
in my every day life.

On Thu, Aug 10, 2017 at 8:47 AM, Steven D'Aprano 
wrote:

> On Wed, Aug 09, 2017 at 12:06:37PM -0400, C W wrote:
> > Dear Python experts,
> >
> > What exactly does the three dots do?
> > > aList = ...
>
> ... is literal syntax for the Ellipsis singleton object.
>
> Ellipsis was added to the language at the express request of the numpy
> developers. Although numpy is a third-party project outside of the
> standard library, it is big enough and important enough that their
> requests carry a LOT of weight with the core Python devs. Other features
> Python has that were originally added at the request of numpy include:
>
> - extended slicing with two colons obj[a:b:c]
>
> - the @ operator used by numpy for matrix multiplication.
>
> I don't know what Ellipsis is used for by numpy, but now it makes a
> convenient pseudo-pass command:
>
> class X:
> ...
>
> def func():
> ...
>
>
> > It's an ellipsis, a spot holder to later. But what data type is it:
> vector,
> > matrix?
>
> Its a singleton object. Think of it as a sibling to None and
> NotImplemented, but with optional funny syntactic sugar ... to refer to
> it.
>
> None is a special value used as "no such value", or nil or null;
>
> NotImplemented is a special value used by operator dunder methods like
> __add__ and __mul__ to mean "I can't handle this argument";
>
> Ellipsis is a special value used by numpy to mean whatever it is that
> numpy uses it to me.
>
>
> --
> Steve
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What exactly does the three dots do? Why such as thing?

2017-08-10 Thread Alan Gauld via Tutor
On 10/08/17 14:39, C W wrote:

> I suppose it's just a place holder, though I don't know when I would use it
> in my every day life.

Probably never.

Like most programming languages Python has a load of rarely used,
obscure features. Most Python programmers never use ellipses,
metaclasses(*), the __new__() constructor, the _ variable or
even the else clause in a loop. That doesn't mean you shouldn't
look into them - they might just be the bit of magic you
need - but don't feel you need to have a use for every
bit of the language.

(*) Actually, everyone uses metaclasses, but very few define
their own!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How does len() compute length of a string in UTF-8, 16, and 32?

2017-08-10 Thread boB Stepp
On Thu, Aug 10, 2017 at 8:01 AM, Steven D'Aprano  wrote:
>
> Another **Must Read** resource for unicode is:
>
> The Absolute Minimum Every Software Developer Absolutely Positively Must
> Know About Unicode (No Excuses!)
>
> https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/

This was an enjoyable read, but did not have as much technical detail
as the two videos Zach had referenced.  But then the author did say
"the absolute minimum ...".  I will strive to avoid peeling onions on
a sub!

> (By the way, it is nearly 14 years later, and PHP still believes that
> the world is ASCII.)

I thought you must surely be engaging in hyperbole, but at
http://php.net/manual/en/xml.encoding.php I found:

"The default source encoding used by PHP is ISO-8859-1."

>
> Python 3 makes Unicode about as easy as it can get. To include a unicode
> string in your source code, you just need to ensure your editor saves
> the file as UTF-8, and then insert (by whatever input technology you
> have) the character you want. You want a Greek pi?
>
> pi = "π"
>
> How about an Israeli sheqel?
>
> money = "₪1000"
>
> So long as your editor knows to save the file in UTF-8, it will Just
> Work.

So Python 3's default behavior for strings is to store them as UTF-8
encodings in both RAM and files?  No funny business anywhere?  Except
perhaps in my Windows 7 cmd.exe and PowerShell, but that's not
Python's fault.  Which makes me wonder, what is my editor's default
encoding/decoding?  I will have to investigate!

Cheers!

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How does len() compute length of a string in UTF-8, 16, and 32?

2017-08-10 Thread boB Stepp
On Thu, Aug 10, 2017 at 8:40 PM, boB Stepp  wrote:
> On Thu, Aug 10, 2017 at 8:01 AM, Steven D'Aprano  wrote:

>> Python 3 makes Unicode about as easy as it can get. To include a unicode
>> string in your source code, you just need to ensure your editor saves
>> the file as UTF-8, and then insert (by whatever input technology you
>> have) the character you want. You want a Greek pi?
>>
>> pi = "π"
>>
>> How about an Israeli sheqel?
>>
>> money = "₪1000"
>>
>> So long as your editor knows to save the file in UTF-8, it will Just
>> Work.
>
> So Python 3's default behavior for strings is to store them as UTF-8
> encodings in both RAM and files?  No funny business anywhere?  Except
> perhaps in my Windows 7 cmd.exe and PowerShell, ...

A while back I adopted a suggestion by Eryk Sun and installed ConEmu
on my Windows 7, and now use it in place of cmd.exe.  Interestingly,
it apparently provides UTF-8 support where cmd.exe and PowerShell do
not.  I just tested it with your two examples in cmd.exe, PowerShell
and both of these shells accessed via ConEmu.  Interesting!  Thanks,
Eryk, for making me aware of this program!


-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How does len() compute length of a string in UTF-8, 16, and 32?

2017-08-10 Thread Cameron Simpson

On 10Aug2017 20:40, boB Stepp  wrote:

(By the way, it is nearly 14 years later, and PHP still believes that
the world is ASCII.)


I thought you must surely be engaging in hyperbole, but at
http://php.net/manual/en/xml.encoding.php I found:

"The default source encoding used by PHP is ISO-8859-1."


This kind of amounts to Python 2's situation in some ways: a PHP string or 
Python 2 str is effectively just an array of bytes, treated like a lexical 
stringy thing.


If you're working only in ASCII or _universally_ in some fixed 8-bit character 
set (eg ISO8859-1 in Western Europe) you mostly get by if you don't look 
closely. PHP's "default source encoding" means that the variable _character_ 
based routines in PHP (things that know about characters as letter, punctuation 
etc) treat these strings as using IS8859-1 encoding. You can load UTF-8 into 
these strings and work that way too (there's a PHP global setting for the 
encoding).


Python 2 has a "unicode" type for proper Unicode strings.

In Python 3 str is Unicode text, and you use bytes for bytes. It is hugely 
better, because you don't need to concern yourself about what text encoding a 
str is - it doesn't have one - it is Unicode. You only need to care when 
reading and writing data.



So long as your editor knows to save the file in UTF-8, it will Just
Work.


So Python 3's default behavior for strings is to store them as UTF-8
encodings in both RAM and files?


Not quite.

In memory Python 3 strings are sequences of Unicode code points. The CPython 
internals pick an 8 or 16 or 32 bit storage mode for these based on the highest 
code point value in the string as a space optimisation decision, but that is 
concealed at the language level. UTF-8 as a storage format is nearly as 
compact, but has the disadvantage that you can't directly index the string 
(i.e. go to character "n") because UTF-8 uses variable length encodings for the 
various code points.


In files however, the default encoding for text files is 'utf-8': Python will 
read the file's bytes as UTF-8 data and will write Python string characters in 
UTF-8 encoding when writing.


If you open a file in "binary" mode there's no encoding: you get bytes. But if 
you open in text mode (no "b" in the open mode string) you get text, and you 
can define the character encoding used as an optional parameter to the open() 
function call.



No funny business anywhere?  Except
perhaps in my Windows 7 cmd.exe and PowerShell, but that's not
Python's fault.  Which makes me wonder, what is my editor's default
encoding/decoding?  I will have to investigate!


On most UNIX platforms most situations expect and use UTF-8. There aresome 
complications because this needn't be the case, but most modern environments 
provide UTF-8 by default.


The situation in Windows is more complex for historic reasons. I believe Eryk 
Sun is the go to guy for precise technical descriptions of the Windows 
situation. I'm not a Windows guy, but I gather modern Windows generally gives 
you a pretty clean UTF-8 environment in most situations.


Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How does len() compute length of a string in UTF-8, 16, and 32?

2017-08-10 Thread eryk sun
On Fri, Aug 11, 2017 at 2:34 AM, Cameron Simpson  wrote:
>
> In files however, the default encoding for text files is 'utf-8': Python
> will read the file's bytes as UTF-8 data and will write Python string
> characters in UTF-8 encoding when writing.

The default encoding for source files is UTF-8. The default encoding
for text files is the locale's preferred encoding, i.e.
locale.getpreferredencoding(False). On Windows this is the locale's
ANSI codepage (e.g. 1252 in Western Europe). Also, the default newline
string in text files is os.linesep. On Windows this is "\r\n".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor