python 3.6... not using python 3 yet?....

1100
1
12-07-2016 07:05 AM
Labels (1)
DanPatterson_Retired
MVP Emeritus
1 1 1,100

Snips of interest...

: -----------------------------------------------------------------------------------------------------------------------------------------------------------

Update... Guarantee ordered dict literals in v3.7?

Guido van Rossum guido at python.org
Fri Dec 15 10:53:40 EST 2017

Make it so. "Dict keeps insertion order" is the ruling. Thanks!

Dictionary order? without 'ordered dictionaries' ?  Not in 2.x... nope!  You had to use ordered dictionaries, but what about dictionaries? How about they are 25% faster than in 2.7... interested now

k = list('abcd')
v = [1,2,3,4,5]
dct = {k:v for k,v in zip(k, v)}

dct
{'a': 1, 'b': 2, 'c': 3, 'd': 4}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Loads of other tidbits.... What's new in Python 3.6

  • math.tau... cmath.tau, cmath.inf and cmath.nan to match math.inf and math.nan, and also cmath.infj
    • Still haven't a use for tau? infinity? not a number? imaginary complex numbers????  Now is your chance to get a foot up on the others.
  • json.load() and json.loads() now support binary input, json is still ugly, but in binary it won't matter cause you can't read it anyway
  • os and os.path modules now support path-like objects.  This isn't what forgetting to put the important stuff in front of the filename by the way.
  • pathlib now supports path-like objects.  Never heard of pathlib?  Read up.
  • statistics... now has a harmonic mean.  I bet you didn't even know that there was a statistics module did you?

: ------------------------------------------------------------------------------------------------------------------------------------------------------

Abandon ship... Supporters have begun to announce when the last call from port is

This is a biggie, since so many other science packages depend on it.  SciPy is in discussions as well.  Maybe you-know-who will also announce similar plans

Sunset time... The exit time and supporters list  The list is growing...

Belated Happy 8th python...  Grown quite a bit over the intervening 8 years.  

What is new in Python 3.7.... you are growing so fast

This blog-ette is just to show some things that should be used more in python 3.x.  Sadly... they aren't even available in python 2.7.x.  (The party is going to be delayed for ArcMap 10.5)

I often work with lists and array and tabular data, often of unknown length.  If I want to get the 3rd element in a list or array or table, you can do the old slicing thing which is fine.  

  • But what if you only want the first two but not the rest?
  • What about the last 2 but not the rest?  
  • How about the middle excluding the first or last two?  
  • How about some weird combination of the above.
  • What if you a digital hoarder and are afraid to throw anything away just in case...

Yes slicing can do it.  What if you could save yourself a step or two?.

To follow along... just watch the stars... * ... in the following lines.  I printed out the variables all on one line so they will appear as a tuple just like usual.  Now when I say 'junk', I mean that is the leftovers from the single slices.  You can't have two stars on the same line, before I forget but you can really mess with your head by parsing stacked lines with variable starred assignment.  

Let's keep it simple ... an array (cause I like them) ... and a list as inputs to the variable assignments... 

>>> import numpy as np
>>> a = np.arange(10)
>>>
>>> # ---- play with auto-slicing to variables ----
>>> a0, a1, *a2 = a       # keep first two, the rest is junk
>>> a0, a1, a2
(0, 1, [2, 3, 4, 5, 6, 7, 8, 9])

>>> a0, a1, *a2, a3 = a   # keep first two and last, junk the rest
>>> a0, a1, a2, a3
(0, 1, [2, 3, 4, 5, 6, 7, 8], 9)

>>> *a0, a1, a2, a3 = a   # junk everything except the last 3
>>> a0, a1, a2, a3
([0, 1, 2, 3, 4, 5, 6], 7, 8, 9)

>>> # ---- What about lists? ----
>>> *a0, a1, a2, a3 = a.tolist()  # just convert the array to a list]
>>> a0, a1, a2, a3
([0, 1, 2, 3, 4, 5, 6], 7, 8, 9)

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Dictionaries too.... but this example is so python 3.5..... look way up at the top of the page

>>> k = list('abcd')
>>> v = [1,2,3,4,5]
>>> dct = {k:v for k,v in zip(k, v)}  # your dictionary comprehension
{'c': 3, 'b': 2, 'a': 1, 'd': 4}

>>> # ---- need to add some more ----
>>> dct = dict(f=9, g=10, **dct, h=7)
>>> dct
{'h': 7, 'c': 3, 'b': 2, 'a': 1, 'g': 10, 'f': 9, 'd': 4}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Think about the transition to 3?

Update (2017-03)

Pro 2.0 uses python 3.5.3, hopefully the next release will use python 3.6 since it is already out and 3.7 is in development.

ArcMap will catchup, but I suspect not at the same rate/pace as PRO.

References

Python 3.0 Release | Python.org Happy Birthday Python

Python 2.7 Countdown 

For more examples far more complex than the above, see...

python 3.x - Unpacking, Extended unpacking, and nested extended unpacking - Stack Overflow 

1 Comment
NeilAyres
MVP Alum

"you can really mess with your head by parsing stacked lines with variable starred assignment."

Couldn't agree more Dan_Patterson

About the Author
Retired Geomatics Instructor at Carleton University. I am a forum MVP and Moderator. Current interests focus on python-based integration in GIS. See... Py... blog, my GeoNet blog...
Labels