atlas news
    
Python Does What
15  mars     05h09
Wisdom for the ages
   type type is type True
14  mars     19h45
sequence unpack a dict
   a, b a :, b : a ’a’
06  mars     17h22
Annotation Inheritance
   Let’s talk about annotations. Type annotations in Python are mostly a static declaration to a type checker like mypy or pyright about the expected types. However, they are also a dynamic data structure which a growing number of libraries such as the original attrs and dataclasses in the...
15  septembre     04h07
Mock Everything
   nbsp;A mock object is meant to simulate any API for the purposes of testing.The python standard library includes MagicMock. from unittest.mock import MagicMock mock MagicMock mock.a lt;MagicMock name ’mock.a’ id ’ ’ mock lt;MagicMock name ’mock. getitem ’...
16  janvier     08h34
Are they equal?
   gt; gt; gt; a ; a.append a ; a ... gt; gt; gt; b ; b.append b ; b ... gt; gt; gt; a bTraceback most recent call last : nbsp; File lt;stdin gt; , line, in lt;module gt;RecursionError: maximum recursion depth exceeded in comparison
04  septembre     00h15
Not counting zeros
   We all have our favorite way of intentionally raising an exception in Python. Some like referencing an undefined variable to get a simple NameError, others might import a module that doesn’t exist for a bold ImportError.But the tasteful exceptioneer knows to reach for that classic computer...
12  septembre     17h45
Welcome to the float zone...
   Consider a REPL with two tuples, a and b. gt; gt; gt; type a , type b lt;type ’tuple’ gt;, lt;type ’tuple’ gt; gt; gt; gt; a bTrue So far, so good. But let’s dig deeper... gt; gt; gt; a b False The tuples are equal, but their contents is not. gt; gt; gt; a is bTrue ...
04  juin     00h12
They say a python tuple can’t contain itself...
   but here at PDW we abhor that kind of defeatism gt; gt; gt; import ctypes gt; gt; gt; tup None, gt; gt; gt; ctypes.pythonapi.PyTuple SetItem.argtypes ctypes.c void p, ctypes.c int, ctypes.c void p gt; gt; gt; ctypes.pythonapi.PyTuple SetItem id tup ,, id tup Showing the tuple itself...
23  janvier     17h00
So a list and a tuple walk into a sum()
   As a direct side effect of glom’s . . release, the authors here at PDW got to re experience one of the more surprising behaviors of three of Python’s most basic constructs: list tuple sum Most experienced developers know the quickest way to combine a short list of short lists: list of...
14  septembre     17h21
kids these days think data structures grow on trees
   Args and kwargs are great features of Python. There is a measurable though highly variable cost of them however: gt; gt; gt; timeit.timeit lambda: lambda a, b: None , b . gt; gt; gt; timeit.timeit lambda: lambda a, kw: None , b . ...
10  août     19h53
python needs a frozenlist
   gt; gt; gt; set frozenset True gt; gt; gt; False
05  juin     15h56
when no-ops attack VII: assignment’s revenge
   Let’s define a very simple class: gt; gt; gt; class F object :... staticmethod... def f : return I’m such a simple function, nothing could go wrong ... gt; gt; gt; F.f I’m such a simple function, nothing could go wrong Now, let’s do a trivial no...
31  mai     20h59
i)t(er)able for one
   When you expect that a sequence will only have one item, and are only interested in the first it is common to grab the zeroth element. This will fail if the sequence is unexpectedly empty, but you might unintentionally silently throw away extra elements: gt; gt; gt; a ’a’ gt; gt; gt; a...
13  mai     02h05
Captain, the python grammar can’t take anymore
   The expressions are going to tear themselves to pieces gt; gt; gt; ’a’ nbsp; nbsp; nbsp; nbsp;.strip nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; ’a’
20  avril     22h53
DISappearing and
   Python has a very rich set of operators that can be overloaded. From get to getattr, repr to format, and complex to iadd you can modify almost every behavior of your type. Conspicuously absent however, are the boolean operators. This is why Django ORM...
22  mars     18h29
The Zen of Empty Lists
   There should be one and preferably only one obvious way to do it . One of the many philosophies that has earned Python its acclaim. But while the Zen of Python limits on the number of obvious ways, the Zen of Python says nothing about the boundless freedom of unobvious ways. Let’s empty a...
21  janvier     18h00
None on the left
   A natural default, None is probably the most commonly assigned value in Python. But what happens if you move it to the left side of that equation In Python : gt; gt; gt; None File lt;stdin gt; , line SyntaxError: cannot assign to None This is similar to what happens when you assign...
30  novembre     18h59
python3 set literals in 3, 2, 1....
   gt; gt; gt; , .add gt; gt; gt; .add gt; gt; gt; .add Traceback most recent call last : File lt;stdin gt; , line, in lt;module gt;AttributeError: ’dict’ object has no attribute ’add’ why no empty set literal: https: mail.python.org pipermail python April...
    01h09
a main by any other name
   cat lt; lt;EOF gt; what is happening.pyif name main : import what is happeningelse: print what is happening EOF python what is happening.py what is happening Ambiguous entrypoints can create a maze of state in your program. In case the...
29  novembre     17h32
UnicodeDecode SyntaxError
   When executing a bytecode for the ’ ’ operation, an invalid byte will raise UnicodeDecodeError. However, when concatenating adjacent string and unicode constants, it will be a SyntaxError. I guess because there is not byte code executing this is happening at compile time. ...
25  août     00h29
sqlite does what
   gt; gt; gt; import sqlite gt; gt; gt; c sqlite .connect ’:memory:’ lt;sqlite .Connection object at x d c d gt; gt; gt; gt; c.execute ’select null and ’ .fetchall None, gt; gt; gt; c.execute ’select null and ’ .fetchall , gt; gt; gt; c.execute ’select null or ’ .fetchall...
29  avril     19h46
a return to yield
   I remember when, almost a decade ago, I was first discovering generators. It was a heady time, and I saw applications everywhere. def fib gen : x, y , while x lt; : x, y y, x y yield x return I also remember the first time I tried to mix a return value...
12  avril     16h00
Bit by bit: CPU architecture
   There are a variety of reasons you might want to know how many bits the architecture of the CPU running your Python program has. Maybe you’re about to use some statically compiled C, or maybe you’re just taking a survey. Either way, you’ve got to know. One historical way way is: import sys IS...
21  mars     18h37
When you can update locals()
   There are two built in functions, globals and locals. These return dicts of the contents of the global and local scope. Locals usually refers to the contents of a function, in which case it is a one time copy. Updates to the dict do not change the local scope: gt; gt; gt; def local...
14  mars     01h34
identity theft
   gt; gt; gt; class JSON int : ... nbsp; nbsp; nbsp; from json import ... gt; gt; gt; json JSON gt; gt; gt; json.dumps ’ ’