updated from Atlas
This commit is contained in:
@@ -4,20 +4,25 @@ import resource
|
||||
|
||||
NUM_VECTORS = 10**7
|
||||
|
||||
module = None
|
||||
if len(sys.argv) == 2:
|
||||
module_name = sys.argv[1].replace('.py', '')
|
||||
module = importlib.import_module(module_name)
|
||||
else:
|
||||
print(f'Usage: {sys.argv[0]} <vector-module-to-test>')
|
||||
sys.exit(2) # command line usage error
|
||||
|
||||
fmt = 'Selected Vector2d type: {.__name__}.{.__name__}'
|
||||
print(fmt.format(module, module.Vector2d))
|
||||
if module is None:
|
||||
print('Running test with built-in `complex`')
|
||||
cls = complex
|
||||
else:
|
||||
fmt = 'Selected Vector2d type: {.__name__}.{.__name__}'
|
||||
print(fmt.format(module, module.Vector2d))
|
||||
cls = module.Vector2d
|
||||
|
||||
mem_init = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
|
||||
print(f'Creating {NUM_VECTORS:,} Vector2d instances')
|
||||
print(f'Creating {NUM_VECTORS:,} {cls.__qualname__!r} instances')
|
||||
|
||||
vectors = [module.Vector2d(3.0, 4.0) for i in range(NUM_VECTORS)]
|
||||
vectors = [cls(3.0, 4.0) for i in range(NUM_VECTORS)]
|
||||
|
||||
mem_final = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
|
||||
print(f'Initial RAM usage: {mem_init:14,}')
|
||||
|
||||
53
11-pythonic-obj/patterns.py
Normal file
53
11-pythonic-obj/patterns.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from vector2d_v3 import Vector2d
|
||||
|
||||
# tag::KEYWORD_PATTERNS[]
|
||||
def keyword_pattern_demo(v: Vector2d) -> None:
|
||||
match v:
|
||||
case Vector2d(x=0, y=0):
|
||||
print(f'{v!r} is null')
|
||||
case Vector2d(x=0):
|
||||
print(f'{v!r} is vertical')
|
||||
case Vector2d(y=0):
|
||||
print(f'{v!r} is horizontal')
|
||||
case Vector2d(x=x, y=y) if x==y:
|
||||
print(f'{v!r} is diagonal')
|
||||
case _:
|
||||
print(f'{v!r} is awesome')
|
||||
# end::KEYWORD_PATTERNS[]
|
||||
|
||||
# tag::POSITIONAL_PATTERNS[]
|
||||
def positional_pattern_demo(v: Vector2d) -> None:
|
||||
match v:
|
||||
case Vector2d(0, 0):
|
||||
print(f'{v!r} is null')
|
||||
case Vector2d(0):
|
||||
print(f'{v!r} is vertical')
|
||||
case Vector2d(_, 0):
|
||||
print(f'{v!r} is horizontal')
|
||||
case Vector2d(x, y) if x==y:
|
||||
print(f'{v!r} is diagonal')
|
||||
case _:
|
||||
print(f'{v!r} is awesome')
|
||||
# end::POSITIONAL_PATTERNS[]
|
||||
|
||||
|
||||
def main():
|
||||
vectors = (
|
||||
Vector2d(1, 1),
|
||||
Vector2d(0, 1),
|
||||
Vector2d(1, 0),
|
||||
Vector2d(1, 2),
|
||||
Vector2d(0, 0),
|
||||
)
|
||||
print('KEYWORD PATTERNS:')
|
||||
for vector in vectors:
|
||||
keyword_pattern_demo(vector)
|
||||
|
||||
print('POSITIONAL PATTERNS:')
|
||||
for vector in vectors:
|
||||
positional_pattern_demo(vector)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
52
11-pythonic-obj/slots.rst
Normal file
52
11-pythonic-obj/slots.rst
Normal file
@@ -0,0 +1,52 @@
|
||||
# tag::PIXEL[]
|
||||
>>> class Pixel:
|
||||
... __slots__ = ('x', 'y') # <1>
|
||||
...
|
||||
>>> p = Pixel() # <2>
|
||||
>>> p.__dict__ # <3>
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'Pixel' object has no attribute '__dict__'
|
||||
>>> p.x = 10 # <4>
|
||||
>>> p.y = 20
|
||||
>>> p.color = 'red' # <5>
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'Pixel' object has no attribute 'color'
|
||||
|
||||
# end::PIXEL[]
|
||||
|
||||
# tag::OPEN_PIXEL[]
|
||||
>>> class OpenPixel(Pixel): # <1>
|
||||
... pass
|
||||
...
|
||||
>>> op = OpenPixel()
|
||||
>>> op.__dict__ # <2>
|
||||
{}
|
||||
>>> op.x = 8 # <3>
|
||||
>>> op.__dict__ # <4>
|
||||
{}
|
||||
>>> op.x # <5>
|
||||
8
|
||||
>>> op.color = 'green' # <6>
|
||||
>>> op.__dict__ # <7>
|
||||
{'color': 'green'}
|
||||
|
||||
# end::OPEN_PIXEL[]
|
||||
|
||||
# tag::COLOR_PIXEL[]
|
||||
>>> class ColorPixel(Pixel):
|
||||
... __slots__ = ('color',) # <1>
|
||||
>>> cp = ColorPixel()
|
||||
>>> cp.__dict__ # <2>
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'ColorPixel' object has no attribute '__dict__'
|
||||
>>> cp.x = 2
|
||||
>>> cp.color = 'blue' # <3>
|
||||
>>> cp.flavor = 'banana'
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'ColorPixel' object has no attribute 'flavor'
|
||||
|
||||
# end::COLOR_PIXEL[]
|
||||
@@ -72,7 +72,7 @@ Tests of `x` and `y` read-only properties:
|
||||
>>> v1.x = 123
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: can't set attribute
|
||||
AttributeError: can't set attribute 'x'
|
||||
|
||||
|
||||
Tests of hashing:
|
||||
@@ -90,6 +90,8 @@ from array import array
|
||||
import math
|
||||
|
||||
class Vector2d:
|
||||
__match_args__ = ('x', 'y')
|
||||
|
||||
typecode = 'd'
|
||||
|
||||
def __init__(self, x, y):
|
||||
|
||||
@@ -72,7 +72,7 @@ Tests of `x` and `y` read-only properties:
|
||||
>>> v1.x = 123
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: can't set attribute
|
||||
AttributeError: can't set attribute 'x'
|
||||
|
||||
# end::VECTOR2D_V3_HASH_DEMO[]
|
||||
|
||||
@@ -112,7 +112,7 @@ class Vector2d:
|
||||
def __iter__(self):
|
||||
return (i for i in (self.x, self.y)) # <6>
|
||||
|
||||
# remaining methods follow (omitted in book listing)
|
||||
# remaining methods: same as previous Vector2d
|
||||
# end::VECTOR2D_V3_PROP[]
|
||||
|
||||
def __repr__(self):
|
||||
|
||||
@@ -72,7 +72,7 @@ Tests of `x` and `y` read-only properties:
|
||||
>>> v1.x = 123
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: can't set attribute
|
||||
AttributeError: can't set attribute 'x'
|
||||
|
||||
Tests of hashing:
|
||||
|
||||
@@ -90,11 +90,10 @@ import math
|
||||
|
||||
# tag::VECTOR2D_V3_SLOTS[]
|
||||
class Vector2d:
|
||||
__slots__ = ('__x', '__y')
|
||||
__match_args__ = ('x', 'y') # <1>
|
||||
__slots__ = ('__x', '__y') # <2>
|
||||
|
||||
typecode = 'd'
|
||||
|
||||
# methods follow (omitted in book listing)
|
||||
# end::VECTOR2D_V3_SLOTS[]
|
||||
|
||||
def __init__(self, x, y):
|
||||
|
||||
Reference in New Issue
Block a user