# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] slideshow={"slide_type": "slide"}
# # Book Meeting - Fluent Python

# + [markdown] slideshow={"slide_type": "slide"}
# ## List Comprehension

# + slideshow={"slide_type": "fragment"}
[f"Num: {num}" for num in range(0, 20, 2) if num != 16]


# + [markdown] slideshow={"slide_type": "slide"}
# # Generator expression

# + slideshow={"slide_type": "fragment"}
(f"Num: {num}" for num in range(0, 20, 2) if num != 16)

# + [markdown] slideshow={"slide_type": "subslide"}
# # Difference between ``list()`` and ``[]``

# + slideshow={"slide_type": "fragment"}
[(f"Num: {num}" for num in range(0, 20, 2) if num != 16)]

# + slideshow={"slide_type": "fragment"}
list((f"Num: {num}" for num in range(0, 20, 2) if num != 16))


# + [markdown] slideshow={"slide_type": "slide"}
# # Function generator

# + slideshow={"slide_type": "fragment"}
def gen_num(start, end, skip):
    for num in range(start, end, skip):
        if num != 16:
            yield f"Num: {num}"


# + slideshow={"slide_type": "fragment"}
gen_num(0, 20, 2)

# + slideshow={"slide_type": "fragment"}
[gen_num(0, 20, 2)]

# + slideshow={"slide_type": "fragment"}
list(gen_num(0, 20, 2))


# + slideshow={"slide_type": "subslide"}
def gen_num(start, end, skip):
    for num in range(start, end, skip):
        if num != 16:
            yield f"Num: {num}"

import inspect

inspect.isgeneratorfunction(gen_num)

# + [markdown] slideshow={"slide_type": "slide"}
# # UML Class diagram for Sequence
# ![Class diagram](images/diagram_sequence.png)

# + [markdown] slideshow={"slide_type": "slide"}
# # Slicing

# + slideshow={"slide_type": "fragment"}
original_list = list(range(20))
original_list

# + slideshow={"slide_type": "fragment"}
hasattr(original_list, "__iter__")

# + slideshow={"slide_type": "fragment"}
original_list[10:]

# + slideshow={"slide_type": "fragment"}
original_list[10:15]

# + slideshow={"slide_type": "fragment"}
original_list[:15]

# + [markdown] slideshow={"slide_type": "subslide"}
# # Slices also accepts negative index

# + slideshow={"slide_type": "fragment"}
original_list[-1]

# + slideshow={"slide_type": "fragment"}
original_list[-5:-2]

# + slideshow={"slide_type": "fragment"}
original_list[0:-1:2]

# + slideshow={"slide_type": "fragment"}
original_list[::2]

# + slideshow={"slide_type": "fragment"}
original_list[5:20:5]

# + [markdown] slideshow={"slide_type": "slide"}
# # Tuple and list unpacking

# + slideshow={"slide_type": "fragment"}
tuple_foo = ("foo", "bar")
tuple_foo


# + slideshow={"slide_type": "fragment"}
def func_foo(param1, param2):
  print(param1)
  print(param2)


# + slideshow={"slide_type": "fragment"}
func_foo(*tuple_foo)

# + [markdown] slideshow={"slide_type": "subslide"}
# # Lists

# + slideshow={"slide_type": "fragment"}
full_list = list(range(0, 11))
full_list

# + slideshow={"slide_type": "fragment"}
first_num, *num_between, last_num = range(0, 11)

# + slideshow={"slide_type": "-"}
print(f"first_num: {first_num}")
print(f"num_between: {num_between}")
print(f"last_num: {last_num}")

# + [markdown] slideshow={"slide_type": "subslide"}
# # Sorting lists
# ## ``list().sort()`` vs ``sorted(list())``

# + [markdown] slideshow={"slide_type": "subslide"}
# # Sorting in place

# + slideshow={"slide_type": "-"}
from random import seed
from random import randint

seed(1)
values = [randint(0, 20) for i in range(20)]
print(values)

values.sort()
print(values)

# + [markdown] slideshow={"slide_type": "subslide"}
# # Creating a new list sorted

# + slideshow={"slide_type": "-"}
from random import seed
from random import randint

seed(1)
values = [randint(0, 20) for i in range(20)]

print(values)
print(sorted(values))
print(values)


# + [markdown] slideshow={"slide_type": "slide"}
# # Custom sequence

# + slideshow={"slide_type": "subslide"}
class IMGSeq:
    def __init__(self, *args):
        self._config = [f"Config_{i}" for i in args if i.lower() != "disabled"]

    def __repr__(self):
        return f"<IMGSeq {self._config}>"
        
    def __getitem__(self, item):
        return self._config[item]
    
    def __contains__(self, item):
        for config in self._config:
            if f"Config_{item}" == config:
                return True
        return False
    
    def __iter__(self):
        # return iter(self._config)
        for config in self._config:
            yield config
    
    def __len__(self):
        return len(self._config)



# + slideshow={"slide_type": "subslide"}
custom = IMGSeq("val1", "disabled", "val2")
print(custom)
# -

for i in custom:
    print(i)

# +
if "val1" in custom:
    print("Configuration Present")
else:
    print("Not available")
    
print("disabled" in custom)
print("val2" in custom)

# + [markdown] slideshow={"slide_type": "slide"}
# # Deques

# + slideshow={"slide_type": "-"}
from collections import deque

dq = deque(range(10),  maxlen=10)
print(dq)

# + slideshow={"slide_type": "fragment"}
dq.rotate(2)
print(dq)

# + slideshow={"slide_type": "fragment"}
dq.rotate(-2)
print(dq)

# + slideshow={"slide_type": "subslide"}


StarcoderdataJupyterScriptsDedupFiltered