Skip to content

Commit

Permalink
Format with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
arvimal committed Oct 3, 2021
1 parent afe5dab commit a5424a8
Show file tree
Hide file tree
Showing 39 changed files with 91 additions and 60 deletions.
4 changes: 2 additions & 2 deletions 01-encapsulation-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@


class MyClass(object):

def set_val(self, val):
self.value = val

def get_val(self):
#print(self.value)
# print(self.value)
return self.value


a = MyClass()
b = MyClass()

Expand Down
3 changes: 2 additions & 1 deletion 02-encapsulation-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ def set_val(self, val):
def get_val(self):
print(self.value)


a = MyClass()
b = MyClass()

a.set_val(10)
b.set_val(1000)
a.value = 100 # <== Overriding `set_value` directly
a.value = 100 # <== Overriding `set_value` directly
# <== ie.. Breaking encapsulation

a.get_val()
Expand Down
3 changes: 2 additions & 1 deletion 03-encapsulation-3.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def increment_val(self):
self.val = self.val + 1
print(self.val)


a = MyInteger()
a.set_val(10)
a.get_val()
Expand All @@ -51,6 +52,6 @@ def increment_val(self):
# Trying to break encapsulation in a new instance with a str
b = MyInteger()
b.val = "MyString" # <== Breaking encapsulation, works fine
b.get_val() # <== Prints the val set by breaking encap
b.get_val() # <== Prints the val set by breaking encap
b.increment_val() # This will fail, since str + int wont work
print("\n")
1 change: 1 addition & 0 deletions 04-init_constructor-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def increment(self):
self.val = self.val + 1
print(self.val)


dd = MyNum()
dd.increment() # will print 1
dd.increment() # will print 2
4 changes: 2 additions & 2 deletions 05-init_constructor-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def increment(self):


a = MyNum(10)
a.increment() # This should print 11
a.increment() # This should print 12
a.increment() # This should print 11
a.increment() # This should print 12
5 changes: 3 additions & 2 deletions 06-class-attributes-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class YourClass(object):
def set_val(self):
self.insty = 100


dd = YourClass()
dd.classy # This will fetch the class attribute 10.
dd.classy # This will fetch the class attribute 10.
dd.set_val()
dd.insty # This will fetch the instance attribute 100.
dd.insty # This will fetch the instance attribute 100.

# Once `dd` is instantiated, we can access both the class and instance
# attributes, ie.. dd.classy and dd.insty.
3 changes: 2 additions & 1 deletion 07-class-attributes-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
class YourClass(object):
classy = "class value"


dd = YourClass()
print(dd.classy) # < This should return the string "class value"

dd.classy = "Instance value"
print(dd.classy) # This should return the string "Instance value"
print(dd.classy) # This should return the string "Instance value"

# This will delete the value set for 'dd.classy' in the instance.
del dd.classy
Expand Down
1 change: 1 addition & 0 deletions 08-class-instance-attributes-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def get_val(self):
def get_count(self):
print(InstanceCounter.count)


a = InstanceCounter(5)
b = InstanceCounter(10)
c = InstanceCounter(15)
Expand Down
3 changes: 2 additions & 1 deletion 09-inheritance-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ class Time(Date):
def get_time(self):
print("07:00:00")


# Creating an instance from `Date`
dt = Date()
dt.get_date() # Accesing the `get_date()` method of `Date`
print("--------")

# Creating an instance from `Time`.
tm = Time()
tm.get_time() # Accessing the `get_time()` method from `Time`.
tm.get_time() # Accessing the `get_time()` method from `Time`.
# Accessing the `get_date() which is defined in the parent class `Date`.
tm.get_date()
6 changes: 5 additions & 1 deletion 10-inheritance-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@
# But the instance created from 'Cat' cannot access the attributes
# within the 'Dog' class, and vice versa.


class Animal(object):
def __init__(self, name):
self.name = name

def eat(self, food):
print("%s is eating %s" % (self.name, food))


class Dog(Animal):
def fetch(self, thing):
print("%s goes after the %s" % (self.name, thing))


class Cat(Animal):
def swatstring(self):
print("%s shred the string!" % self.name)


d = Dog("Roger")
c = Cat("Fluffy")

Expand All @@ -44,4 +48,4 @@ def swatstring(self):
# have access to the other class.

c.fetch("frizbee")
d.swatstring()
d.swatstring()
16 changes: 7 additions & 9 deletions 11-polymorphism-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,28 @@


class Animal(object):

def __init__(self, name):
self.name = name

def eat(self, food):
print('{0} eats {1}'.format(self.name, food))
print("{0} eats {1}".format(self.name, food))


class Dog(Animal):

def fetch(self, thing):
print('{0} goes after the {1}!'.format(self.name, thing))
print("{0} goes after the {1}!".format(self.name, thing))

def show_affection(self):
print('{0} wags tail'.format(self.name))
print("{0} wags tail".format(self.name))


class Cat(Animal):

def swatstring(self):
print('{0} shreds more string'.format(self.name))
print("{0} shreds more string".format(self.name))

def show_affection(self):
print('{0} purrs'.format(self.name))
print("{0} purrs".format(self.name))


for a in (Dog('Rover'), Cat('Fluffy'), Cat('Lucky'), Dog('Scout')):
for a in (Dog("Rover"), Cat("Fluffy"), Cat("Lucky"), Dog("Scout")):
a.show_affection()
2 changes: 1 addition & 1 deletion 12-polymorphism-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
print(len(text))

print(len("Hello"))
print(len({'a': 1, 'b': 2, 'c': 3}))
print(len({"a": 1, "b": 2, "c": 3}))
5 changes: 3 additions & 2 deletions 13-inheriting-init-constructor-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def __init__(self, name):

class Dog(Animal):
def fetch(self, thing):
print('%s goes after the %s' % (self.name, thing))
print("%s goes after the %s" % (self.name, thing))


d = Dog("Roger")
print "The dog's name is", d.name
print("The dog's name is", d.name)
d.fetch("frizbee")
2 changes: 1 addition & 1 deletion 14-multiple-inheritance-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@


class A(object):

def dothis(self):
print("doing this in A")

Expand All @@ -41,6 +40,7 @@ def dothis(self):
class D(B, C):
pass


d_instance = D()
d_instance.dothis() # <== This should print from class A.

Expand Down
4 changes: 2 additions & 2 deletions 15-multiple-inheritance-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@


class A(object):

def dothat(self):
print("Doing this in A")

Expand All @@ -31,16 +30,17 @@ class B(A):


class C(object):

def dothis(self):
print("\nDoing this in C")


class D(B, C):
"""Multiple Inheritance,
D inheriting from both B and C"""

pass


d_instance = D()

d_instance.dothis()
Expand Down
2 changes: 1 addition & 1 deletion 16-multiple-inheritance-3.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@


class A(object):

def dothis(self):
print("doing this in A")

Expand All @@ -48,6 +47,7 @@ def dothis(self):
class D(B, C):
pass


d_instance = D()
d_instance.dothis()

Expand Down
1 change: 1 addition & 0 deletions 17-instance_methods-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class A(object):
def method(*argv):
return argv


a = A()
print(a.method)

Expand Down
1 change: 1 addition & 0 deletions 18-instance_methods-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def get_val(self):
def get_count(self):
return InstanceCounter.count


a = InstanceCounter(5)
b = InstanceCounter(10)
c = InstanceCounter(15)
Expand Down
22 changes: 12 additions & 10 deletions 19-decorators-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@
# function you're actually executing.


def my_decorator(my_function): # <-- (4)
def inner_decorator(): # <-- (5)
def my_decorator(my_function): # <-- (4)
def inner_decorator(): # <-- (5)
print("This happened before!") # <-- (6)
my_function() # <-- (7)
print("This happens after ") # <-- (10)
print("This happened at the end!") # <-- (11)
my_function() # <-- (7)
print("This happens after ") # <-- (10)
print("This happened at the end!") # <-- (11)

return inner_decorator
# return None


@my_decorator # <-- (3)
def my_decorated(): # <-- (2) <-- (8)
print("This happened!") # <-- (9)
@my_decorator # <-- (3)
def my_decorated(): # <-- (2) <-- (8)
print("This happened!") # <-- (9)


if __name__ == '__main__':
my_decorated() # <-- (1)
if __name__ == "__main__":
my_decorated() # <-- (1)

# This prints:
# # python 19-decorators-1.py
Expand Down
2 changes: 2 additions & 0 deletions 20-decorators-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ def inner_decorator():
print(datetime.datetime.utcnow())
inner()
print(datetime.datetime.utcnow())

return inner_decorator


@my_decorator
def decorated():
print("This happened!")


if __name__ == "__main__":
decorated()

Expand Down
2 changes: 2 additions & 0 deletions 21-decorators-3.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ def inner_decorator(num_copy):
print(datetime.datetime.utcnow())
inner(int(num_copy) + 1)
print(datetime.datetime.utcnow())

return inner_decorator


@my_decorator
def decorated(number):
print("This happened : " + str(number))


if __name__ == "__main__":
decorated(5)

Expand Down
2 changes: 2 additions & 0 deletions 22-decorators-4.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
def decorator(inner):
def inner_decorator(*args, **kwargs):
print(args, kwargs)

return inner_decorator


@decorator
def decorated(string_args):
print("This happened : " + string_args)


if __name__ == "__main__":
decorated("Hello, how are you?")

Expand Down
2 changes: 2 additions & 0 deletions 23-decorators-5.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def inner(*args, **kwargs):
return func_name(*args, **kwargs)
except Exception:
print("An exception was thrown : ", Exception)

return inner


Expand All @@ -22,4 +23,5 @@ def inner(*args, **kwargs):
def divide(x, y):
return x / y


print(divide(8, 0))
1 change: 1 addition & 0 deletions 24-decorators-6.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def decorator(inner):
def inner_decorator(*args, **kwargs):
print("This function takes " + str(len(args)) + " arguments")
inner(*args)

return inner_decorator


Expand Down
1 change: 1 addition & 0 deletions 25-decorators-7.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
def double(my_func):
def inner_func(a, b):
return 2 * my_func(a, b)

return inner_func


Expand Down
Loading

0 comments on commit a5424a8

Please sign in to comment.