# The standard string repr for dicts is hard to read: >>> my_mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee} >>> my_mapping {'b': 42, 'c': 12648430. 'a': 23} # # The "json" module can do a much better job: >>> import json >>> print(json.dumps(my_mapping, indent=4, sort_keys=True)) { "a": 23, "b": 42, "c": 12648430 } # Note this only works with dicts containing # primitive types (check out the "pprint" module): >>> json.dumps({all: 'yup'}) TypeError: keys must be a string
[🐍PyTricks]: You can use "json.dumps()" to pretty-print Python dicts
[🐍PyTricks]: Function argument unpacking in Python
# Why Python Is Great: # Function argument unpacking def myfunc(x, y, z): print(x, y, z) tuple_vec = (1, 0, 1) dict_vec = {'x': 1, 'y': 0, 'z': 1} >>> myfunc(*tuple_vec) 1, 0, 1 >>> myfunc(**dict_vec) 1, 0, 1
🐍PyTricks]: Merging two dicts in Python 3.5+ with a single expression
Source :
Dan at Real Python <info@realpython.com>
# How to merge two dictionaries # in Python 3.5+ >>> x = {'a': 1, 'b': 2} >>> y = {'b': 3, 'c': 4} >>> z = {**x, **y} >>> z {'c': 4, 'a': 1, 'b': 3} # In Python 2.x you could # use this: >>> z = dict(x, **y) >>> z {'a': 1, 'c': 4, 'b': 3} # In these examples, Python merges dictionary keys # in the order listed in the expression, overwriting # duplicates from left to right. # # See: https://www.youtube.com/watch?v=Duexw08KaC8
🐍PyTricks]: Different ways to test multiple flags at once in Python
Dan at Real Python <info@realpython.com>
# Different ways to test multiple # flags at once in Python x, y, z = 0, 1, 0 if x == 1 or y == 1 or z == 1: print('passed') if 1 in (x, y, z): print('passed') # These only test for truthiness: if x or y or z: print('passed') if any((x, y, z)): print('passed')
🐍PyTricks]: How to sort a Python dict by value
Dan at Real Python <info@realpython.com>
# How to sort a Python dict by value # (== get a representation sorted by value) >>> xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1} >>> sorted(xs.items(), key=lambda x: x[1]) [('d', 1), ('c', 2), ('b', 3), ('a', 4)] # Or: >>> import operator >>> sorted(xs.items(), key=operator.itemgetter(1)) [('d', 1), ('c', 2), ('b', 3), ('a', 4)]
🐍PyTricks]: The get() method on Python dicts and its "default" arg
# The get() method on dicts # and its "default" argument name_for_userid = { 382: "Alice", 590: "Bob", 951: "Dilbert", } def greeting(userid): return "Hi %s!" % name_for_userid.get(userid, "there") >>> greeting(382) "Hi Alice!" >>> greeting(333333) "Hi there!"
Source :
Dan at Real Python <info@realpython.com>
[🐍PyTricks]: Python's namedtuples can be a great alternative to defining a class manually
# Why Python is Great: Namedtuples
# Using namedtuple is way shorter than
# defining a class manually:
>>> from collections import namedtuple
>>> Car = namedtuple('Car', 'color mileage')
# Our new "Car" class works as expected:
>>> my_car = Car('red', 3812.4)
>>> my_car.color
'red'
>>> my_car.mileage
3812.4
# We get a nice string repr for free:
>>> my_car
Car(color='red' , mileage=3812.4)
# Like tuples, namedtuples are immutable:
>>> my_car.color = 'blue'
AttributeError: "can't set attribute"
# Using namedtuple is way shorter than
# defining a class manually:
>>> from collections import namedtuple
>>> Car = namedtuple('Car', 'color mileage')
# Our new "Car" class works as expected:
>>> my_car = Car('red', 3812.4)
>>> my_car.color
'red'
>>> my_car.mileage
3812.4
# We get a nice string repr for free:
>>> my_car
Car(color='red' , mileage=3812.4)
# Like tuples, namedtuples are immutable:
>>> my_car.color = 'blue'
AttributeError: "can't set attribute"
Dan at Real Python <info@realpython.com>
🐍PyTricks]: Try running "import this" inside a Python REPL ...
Source :
Dan at Real Python <info@realpython.com>
>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
Python Weekly Issue 430
| |||||||||||||
|
Python Weekly Issue 429
|
Subscribe to:
Posts (Atom)