<jupyter_start><jupyter_text>Retrospective  - Interpreter- Basic Calculations- Data Structures- Control flow- Functions- Documentation  PackagesPackages are folders containing Modules. If you are lucky, these modules also work together in a way. The folder needs to contain an `__init__.py` file that will tell python what to do with the package.<jupyter_code>import time

t = time.localtime()
t.tm_mday<jupyter_output><empty_output><jupyter_text>PypiPython has a very active development community and the [**Py**thon **p**ackage **i**ndex](www.pypi.python.org) contains a myriad of packages for every need. These can be install by using `pip` which is installed with newer python versions:```pip install numpy```  Object Orientation![](http://www.codercaste.com/wp-content/uploads/2011/01/animals.png)  Programming Paradigms![](http://academic.udayton.edu/SaverioPerugini/courses/cps343/lecture_notes/images/summary.png)  Object orientation in Python- everything is a object (even functions)You can define your own objects by using the `class` keyword:<jupyter_code>class Dog:
    color = "yellow"
    def __init__(self, name):
        self.name = name
            
Dog<jupyter_output><empty_output><jupyter_text>InstancesClasses can be used to create objects with certain attributes and behavior:<jupyter_code>class Dog:
    color = "yellow"
    def __init__(self, name, size):
        self.name = name
        self.size = size
    def bark(self):
        print('whoof, whoof!')

b = Dog("blitzer", 100)
b.bark()<jupyter_output>whoof, whoof!<jupyter_text>InheritanceClasses can inherit traits (and even **skills**) from other classes:<jupyter_code>class Dachshund(Dog):
    color = "brownish"
    def __init__(self, name):
        super().__init__(name, 20)
        
d = Dachshund('bello')
d.bark()<jupyter_output>whoof, whoof!<jupyter_text>ShadowingClasses can alter functionality of their parents:<jupyter_code>class Pekingese(Dog):
    color = "brownish"
    def __init__(self, name):
        super().__init__(name, 15)
        
    def bark(self):
        print('whif.')
        
d = Pekingese('tiny')
d.bark()<jupyter_output>whif.<jupyter_text>Special methods<jupyter_code>class Pekingese(Dog):
    color = "brownish"
    def __init__(self, name):
        super().__init__(name, 15)

    def __len__(self):
        return self.size
        
    def bark(self):
        print('whif.')
p = Pekingese("wang")
len(p)<jupyter_output><empty_output><jupyter_text>Mating<jupyter_code>class Dog:
    color = "yellow"
    def __init__(self, name, size):
        self.name = name
        self.size = size
            
    def __add__(self, other):
        if not isinstance(other, Dog):
            raise TypeError
        return Dog(self.name + "-" + other.name, (self.size + other.size)/2)
    
    def bark(self):
        print('whoof, whoof!')
p = Dog('bello', 25)
b = Dog("blitzer", 100)
dog = b + p
dog.bark()
print(dog.name)
print(dog.size)<jupyter_output>whoof, whoof!
blitzer-bello
62.5<jupyter_text>Why do I need this?OOP facilitates some desirable features in software projects:- code organization- encapsulation- DRY principle- modularization  How we will use itOur project will make extensive use of object orientation and inheritance.![Class diagramm](http://1.bp.blogspot.com/-tWAOmi7yyRs/T-6x2UB0NZI/AAAAAAAAAMI/fOivujFWstE/s1600/CLASS+DIAGRAM.png) - classes use each other to run the model- each process knows which species to update, makes modularization possible- simulation class manages everything, registers processes- states/processes inherit from base class  DecoratorsSome helpful syntax  Nested FunctionsFunctions can contain other functions:<jupyter_code>def outer_function():
    print("out here")
    def inner_1():
        return "in room 1"
        
    def inner_2():
        return "in room 2"
        
    print(inner_1())
    print(inner_2())
        
outer_function()<jupyter_output>out here
in room 1
in room 2<jupyter_text>WrappersDecorators are functions wrapped around functions:<jupyter_code>def greetings(some_function):
    def wrapper(args):
        print("Hello!")
        some_function(args)
        print("Goodbye!")
    return wrapper

def say_something(sentence):
    print("Say: {0}".format(sentence))
polite_talker = greetings(say_something)

polite_talker("Good to see you")<jupyter_output>Hello!
Say: Good to see you
Goodbye!<jupyter_text>Decorator Syntax<jupyter_code>@greetings
def shout(sentence):
    print(sentence.upper())
shout("Am I polite, or what?!")<jupyter_output>Hello!
AM I POLITE, OR WHAT?!
Goodbye!<jupyter_text>How will we use it?<jupyter_code>class Molecule(object):
    def __init__(self, mass):
        self.__mass = mass
        
    def get_mass(self):
        return self.__mass
    
    def set_mass(sefl, value):
        if not isinstance(value, int):
            raise TypeError("Mass must be Integer.")
        self.__mass = value
        
m = Molecule(12)
m.set_mass('13')
class Molecule(object):
    def __init__(self, mass):
        self.mass = mass
        
    @property
    def mass(self):
        print("getter")
        return self.__mass
    
    @mass.setter
    def mass(self, value):
        print("setter")
        if not isinstance(value, int):
            raise TypeError("Mass must be Integer.")
        self.__mass = value
m = Molecule(1)
m.mass 
m.mass = "two"<jupyter_output>setter<jupyter_text>- getter and setter methods  Further Tricks Some python specialties you should know about  Copy  By Reference/Value As you might know you can assign values of one variable to an other like:<jupyter_code>A = [5]
B = A
print(B)<jupyter_output>[5]<jupyter_text>But if you change the value of variable A as it is done blow, variable B changes, too.<jupyter_code>A[0] = 1
print(B)<jupyter_output>[1]<jupyter_text>The problem is that variables are just names referring to an object. Assigning the object of a variable to an other does not create a copy of this object. It creates a new variable B which refers to the same object A refers to. Hence, there is only one object but two variables are referring to this one. This holds only for mutable objects like lists, sets, dictionaries and so on.There are also immutable objects like inits, floats, strings, tuple and so on.<jupyter_code>A = 1
B = A
A += 1
print(B)<jupyter_output>1<jupyter_text>The difference in this example is, that we are not incrementing the value of A, but instead we are creating a new object and assigning this new object to our variable A. After this we have two objects the Integer 1 and the Integer 2 and two variables B and A referring to the objects, respectively.Some operations change the object instead creating a copy of the object assigned to a variable.<jupyter_code>A = [[2,3,1]]
B = A
A[0].append(4)
print(B)
A[0].sort()
print(B)<jupyter_output>[[2, 3, 1, 4]]
[[1, 2, 3, 4]]<jupyter_text>However, some operations directly create new objects.<jupyter_code>A = [[2,3,1]]
B = A
A = A + [4]
print(B)
print(sorted(A[0]))
print(B)<jupyter_output>[[2, 3, 1]]
[1, 2, 3]
[[2, 3, 1]]<jupyter_text>So we end up with two objects and two variables referring to these objects, respectively.But we have to pay attention when we use operations like +=. The behaviour of this kind of operations depends on the object it is applied on.<jupyter_code>A = ((1,2,3))  # assigning a immutable tuple object to A
B = A
A += (3,4,5)
print("A: ", A)
print("B: ", B)<jupyter_output>A:  (1, 2, 3, 3, 4, 5)
B:  (1, 2, 3)<jupyter_text>If we apply this operation on a variable referring to an immutable object - a new object is created, but if we apply this to an object referring to a mutable object like lists, sets and so on we are just changing the object.<jupyter_code>A = [1,2,3]  # assigning a mutable list object to A
B = A
A += [1]
print("A: ", A)
print("B: ", B)<jupyter_output>A:  [1, 2, 3, 1]
B:  [1, 2, 3, 1]<jupyter_text>In this case the += operation on a list is equivalent to the A.extend([1]) operation.  Copy The copy operation can be used to make copies of an object. Like<jupyter_code>import copy
A = [5]
B = copy.copy(A)
A[0] = 1
print("A: ", A)
print("B: ", B)<jupyter_output>A:  [1]
B:  [5]<jupyter_text>The copy operation creates a copy of the object (in this case a list) assigned to variable A and set references of the inner objects. The inner object (integer 1) is immutable, hence, the variable B will not change if we change A. But if we have something like<jupyter_code>A = [[5]]
B = copy.copy(A)
A[0][0] = 1
print("A: ", A)
print("B: ", B)<jupyter_output>A:  [[1]]
B:  [[1]]<jupyter_text>We will change variable B if we change the inner object of variable A. This is because we have a copy of the first object (outer list) and just set a reference of the inner object. This brings us back to the problem we discussed above.  Deepcopy To avoid this we have the possibility to make a deepcopy of our object assigned to a variable.<jupyter_code>import copy
A = [[5]]
B = copy.deepcopy(A)
A[0][0] = 1
print("A: ", A)
print("B: ", B)<jupyter_output>A:  [[1]]
B:  [[5]]<jupyter_text>With this operation we are creating a new object of the first object and for all nested objects, too. This means that the entire structure is copied and we end up with two objects (containing arbitray nested objects) assigned to two variables.  Zipping unzipping lists and iterables<jupyter_code>A = [1, 2, 3]
B = ['a', 'b', 'c']
z = list(zip(A, B))
print("zipping: ", z)
print("unzipping: ", list(zip(*z)))<jupyter_output>zipping:  [(1, 'a'), (2, 'b'), (3, 'c')]
unzipping:  [(1, 2, 3), ('a', 'b', 'c')]<jupyter_text>Dictionaries  Creating dictionaries<jupyter_code># dictionary comprehension
name_space = "a","b","c","d","e"
dictionary = {name_space[x]: x**2 for x in range(len(name_space))}
print("dictionary comprehension: ", dictionary)
# creating a dictionary using zip function
dictionary = dict(zip(['A', 'B', 'C'], [1, 2, 3]))
print("dictionary: ", dictionary)
# default dictionary
from collections import defaultdict
default_dict = defaultdict(list)
default_dict['a'].append(12)  # if a value has not been set for 'a', initialize with empty list
print("default dictionary: ", default_dict)

# normal dictionary initialisation
no_default = dict()
no_default['a'].append(12)<jupyter_output>default dictionary:  defaultdict(<class 'list'>, {'a': [12]})<jupyter_text>Inverting dictionaries<jupyter_code>dictionary = dict(a=1, b=2, c=3)  # normal dict generation
print("dictionary: ", dictionary)

# inverting dictionary using zip function
inverted_dict = dict(zip(dictionary.values(), dictionary.keys()))
print("inverted dict: ", inverted_dict)<jupyter_output>dictionary:  {'c': 3, 'a': 1, 'b': 2}
inverted dict:  {1: 'a', 2: 'b', 3: 'c'}<jupyter_text>Ordered dictionaries<jupyter_code>from collections import OrderedDict
ordered_dict = OrderedDict((name_space[x], x**2) for x in range(len(name_space)))
print("ordered dictionary: ", ordered_dict)<jupyter_output>ordered dictionary:  OrderedDict([('a', 0), ('b', 1), ('c', 4), ('d', 9), ('e', 16)])<jupyter_text>Itertools  Sliding window<jupyter_code>import itertools
def k_mer(word, size):
    z = (itertools.islice(word, i, None) for i in range(size))
    return zip(*z)

string = "HelloWorld"
list(k_mer(string,3))<jupyter_output><empty_output><jupyter_text>Permutations<jupyter_code>import itertools
for p in itertools.permutations([1,2,3]):
    print("permutations: ", p)<jupyter_output>permutations:  (1, 2, 3)
permutations:  (1, 3, 2)
permutations:  (2, 1, 3)
permutations:  (2, 3, 1)
permutations:  (3, 1, 2)
permutations:  (3, 2, 1)<jupyter_text>Flatting Lists<jupyter_code>import itertools
a_list = [[1,2],[3,4],[5,6]]

a_flatted_list = [x for i in a_list for x in i]
print("a_flatted_list: ", a_flatted_list)<jupyter_output>a_flatted_list:  [1, 2, 3, 4, 5, 6]
{'fraction_non_alphanumeric': 0.1213588110403397, 'fraction_numerical': 0.01723991507430998, 'mean_word_length': 4.479758026989297, 'pattern_counts': {'":': 0, '<': 90, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '28', 'dir': 'jupyter-structured-clean-dedup', 'id': '531832', 'license': 'MIT', 'n_tokens_mistral': 4122, 'n_tokens_neox': 3914, 'n_words': 1287, 'path': '03_advanced/advanced_python_tricks.ipynb', 'repo_name': 'brunnerp/PythonFachkurs', 'size': '11775'}