|
| 1 | +""" |
| 2 | +Sequence unpacking and sequence patterns |
| 3 | +
|
| 4 | +edge cases: |
| 5 | + [record] with only one record in list |
| 6 | + (record,) with only one record in tuple |
| 7 | +
|
| 8 | +- Sequence unpacking |
| 9 | +- Pattern Matching with Sequences |
| 10 | + - Instances of str, byte, and bytearray are not handled as sequences in the context of match/case |
| 11 | + - Unlike unpacking, patterns do NOT destructure iterables that are not sequences (e.g., iterators) |
| 12 | + - case [name, _, _, (lat, lon) as coord]: # bind any part of a pattern with a variable using the as keyword |
| 13 | + - case [name, *_, (float(lat), float(lon))]: |
| 14 | +""" |
| 15 | + |
| 16 | +# -- Sequence unpacking -- |
| 17 | +# Using * to grab excess items |
| 18 | +lax_coordinates = (33.9425, -118.408056) |
| 19 | +latitude, longitude = lax_coordinates # unpacking |
| 20 | +latitude |
| 21 | + |
| 22 | +t = (20, 8) |
| 23 | +divmod(*t) # (2, 4) |
| 24 | +quotient, remainder = divmod(*t) |
| 25 | + |
| 26 | +import os |
| 27 | +_, filename = os.path.split('/home/luciano/.ssh/id_rsa.pub') |
| 28 | +filename |
| 29 | + |
| 30 | +a, b, *rest = range(5) |
| 31 | +a, b, rest # (0, 1, [2, 3, 4]) |
| 32 | + |
| 33 | +a, b, *rest = range(2) |
| 34 | +a, b, rest # (0, 1, []) |
| 35 | + |
| 36 | +a, *body, c, d = range(5) |
| 37 | +a, body, c, d # (0, [1, 2], 3, 4) |
| 38 | + |
| 39 | +*head, b, c, d = range(5) |
| 40 | +head, b, c, d # ([0, 1], 2, 3, 4) |
| 41 | + |
| 42 | + |
| 43 | +# Unpacking with * in function calls and sequence literals |
| 44 | +def fun(a, b, c, d, *rest): |
| 45 | + return a, b, c, d, rest |
| 46 | + |
| 47 | + |
| 48 | +fun(*[1, 2], 3, *range(4, 7)) # (1, 2, 3, 4, (5, 6)) |
| 49 | +*range(4), 4 # (0, 1, 2, 3, 4) |
| 50 | +[*range(4), 4] # [0, 1, 2, 3, 4] |
| 51 | +{*range(4), 4, *(5, 6, 7)} # {0, 1, 2, 3, 4, 5, 6, 7} |
| 52 | + |
| 53 | + |
| 54 | +# -- Pattern Matching with Sequences -- |
| 55 | +# match/case sequence handlings |
| 56 | +# sequence pattern can be tuples or lists or any combination of nested tuples and lists. |
| 57 | +metro_areas = [ |
| 58 | + ('Tokyo', 'JP', 36.933, (35.689722, 139.691667)), |
| 59 | + ('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)), |
| 60 | + ('Mexico City', 'MX', 20.142, (19.433333, -99.133333)), |
| 61 | + ('New York-Newark', 'US', 20.104, (40.808611, -74.020386)), |
| 62 | + ('São Paulo', 'BR', 19.649, (-23.547778, -46.635833)), |
| 63 | +] |
| 64 | + |
| 65 | +def main(): |
| 66 | + print(f'{"":15} | {"latitude":>9} | {"longitude":>9}') |
| 67 | + for record in metro_areas: |
| 68 | + match record: |
| 69 | + case [name, _, _, (lat, lon)] if lon <= 0: |
| 70 | + print(f'{name:15} | {lat:9.4f} | {lon:9.4f}') |
| 71 | +main() |
| 72 | +# | latitude | longitude |
| 73 | +# Mexico City | 19.4333 | -99.1333 |
| 74 | +# New York-Newark | 40.8086 | -74.0204 |
| 75 | +# São Paulo | -23.5478 | -46.6358 |
| 76 | + |
| 77 | + |
| 78 | +# Instances of str, byte, and bytearray are not handled as sequences in the context of match/case |
| 79 | +# match tuple(phone): |
| 80 | +# case ['1', *rest]: |
| 81 | +# ... |
| 82 | +# case ['2', *rest]: |
| 83 | +# ... |
| 84 | +# case ['3' | '4', *rest]: |
| 85 | +# ... |
0 commit comments