diff --git a/src/additions/test_pass.py b/src/additions/test_pass.py index 57a6de9b..1c21623f 100644 --- a/src/additions/test_pass.py +++ b/src/additions/test_pass.py @@ -15,7 +15,9 @@ def test_pass_in_function(): The pass statement below is silently ignored but it makes current test_pass() function valid. """ - pass + print('test') + # pass + def test_pass_in_loop(): @@ -43,4 +45,4 @@ class MyEmptyClass: "Pass" is commonly used for creating minimal classes like current one. """ - pass + # pass diff --git a/src/classes/test_class_and_instance_variables.py b/src/classes/test_class_and_instance_variables.py index c4004d79..f17d2a69 100644 --- a/src/classes/test_class_and_instance_variables.py +++ b/src/classes/test_class_and_instance_variables.py @@ -84,3 +84,5 @@ def add_trick(self, trick): assert fido.tricks == ['roll over'] assert buddy.tricks == ['play dead'] + + # Good to learn class variable diff --git a/src/classes/test_class_objects.py b/src/classes/test_class_objects.py index 2d5a4cdb..73428259 100644 --- a/src/classes/test_class_objects.py +++ b/src/classes/test_class_objects.py @@ -21,7 +21,7 @@ def test_class_objects(): # attribute references: class ComplexNumber: - """Example of the complex numbers class""" + """Try to write some doc""" real = 0 imaginary = 0 @@ -37,7 +37,7 @@ def get_imaginary(self): assert ComplexNumber.real == 0 # __doc__ is also a valid attribute, returning the docstring belonging to the class - assert ComplexNumber.__doc__ == 'Example of the complex numbers class' + assert ComplexNumber.__doc__ == 'Try to write some doc' # Class attributes can also be assigned to, so you can change the value of # ComplexNumber.counter by assignment. diff --git a/src/classes/test_inheritance.py b/src/classes/test_inheritance.py index 5884da2c..707fead2 100644 --- a/src/classes/test_inheritance.py +++ b/src/classes/test_inheritance.py @@ -38,6 +38,8 @@ class Employee(Person): in the global scope.) """ def __init__(self, name, staff_id): + Person.__init__(self, name) + # This is a new feature to me Person.__init__(self, name) # You may also use super() here in order to avoid explicit using of parent class name: # >>> super().__init__(name) @@ -79,3 +81,5 @@ def test_inheritance(): assert issubclass(Employee, Person) assert not issubclass(Person, Employee) + + assert not issubclass(Employee, Exception) diff --git a/src/classes/test_instance_objects.py b/src/classes/test_instance_objects.py index e58dc343..e65fbc4c 100644 --- a/src/classes/test_instance_objects.py +++ b/src/classes/test_instance_objects.py @@ -28,3 +28,10 @@ class DummyClass: dummy_instance.temporary_attribute = 1 assert dummy_instance.temporary_attribute == 1 del dummy_instance.temporary_attribute + + # I didn't know the data attributes need not to be declared + dummy_instance.idontknow = True + assert dummy_instance.idontknow == True + del dummy_instance.idontknow + assert dummy_instance.__doc__ == "Dummy class" + assert DummyClass.__doc__ == "Dummy class" diff --git a/src/control_flow/test_for.py b/src/control_flow/test_for.py index 7411277b..3fe6b096 100644 --- a/src/control_flow/test_for.py +++ b/src/control_flow/test_for.py @@ -34,6 +34,8 @@ def test_for_statement(): if len(word) > 6: words.insert(0, word) + # This is an important tip + # Otherwise with for w in words:, the example would attempt to create an infinite list, # inserting defenestrate over and over again. diff --git a/src/data_types/test_dictionaries.py b/src/data_types/test_dictionaries.py index da3b9cc8..54cfaef3 100644 --- a/src/data_types/test_dictionaries.py +++ b/src/data_types/test_dictionaries.py @@ -31,6 +31,7 @@ def test_dictionary(): } assert isinstance(fruits_dictionary, dict) + assert not isinstance(fruits_dictionary, list) # You may access set elements by keys. assert fruits_dictionary['apple'] == 'green' @@ -59,6 +60,7 @@ def test_dictionary(): # The dict() constructor builds dictionaries directly from sequences of key-value pairs. dictionary_via_constructor = dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) + assert {'a': 1, 'b': 2, 'c': 3} == dict([('a', 1), ('b', 2), ('c', 3)]) assert dictionary_via_constructor['sape'] == 4139 assert dictionary_via_constructor['guido'] == 4127 @@ -74,6 +76,8 @@ def test_dictionary(): # When the keys are simple strings, it is sometimes easier to specify pairs using # keyword arguments. dictionary_for_string_keys = dict(sape=4139, guido=4127, jack=4098) + + assert dict(xy=1, yz=2, xz=3) == {'xy': 1, 'yz': 2, "xz": 3} assert dictionary_for_string_keys['sape'] == 4139 assert dictionary_for_string_keys['guido'] == 4127 assert dictionary_for_string_keys['jack'] == 4098 diff --git a/src/data_types/test_lists.py b/src/data_types/test_lists.py index 33ffdbe9..8450005a 100644 --- a/src/data_types/test_lists.py +++ b/src/data_types/test_lists.py @@ -274,6 +274,8 @@ def test_list_comprehensions(): vector = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flatten_vector = [num for elem in vector for num in elem] assert flatten_vector == [1, 2, 3, 4, 5, 6, 7, 8, 9] + test_flatten = [[2, 3, 4], [1, 2, 3], [4, 3, 2]] + assert [i for elm in test_flatten for i in elm] == [2, 3, 4, 1, 2, 3, 4, 3, 2] def test_nested_list_comprehensions(): diff --git a/src/data_types/test_strings.py b/src/data_types/test_strings.py index ae86432f..9959813f 100644 --- a/src/data_types/test_strings.py +++ b/src/data_types/test_strings.py @@ -137,6 +137,12 @@ def test_string_operators(): 'to have them joined together.' ) assert text == 'Put several strings within parentheses to have them joined together.' + my_trial = ( + "This is a new feature " + "Good to know this!" + "\n" + ) + assert my_trial == "This is a new feature Good to know this!\n" # If you want to concatenate variables or a variable and a literal, use +: prefix = 'Py' diff --git a/src/data_types/test_tuples.py b/src/data_types/test_tuples.py index b122824e..27194931 100644 --- a/src/data_types/test_tuples.py +++ b/src/data_types/test_tuples.py @@ -63,6 +63,11 @@ def test_tuples(): assert len(singleton_tuple) == 1 assert singleton_tuple == ('hello',) + my_singleton = 'abc', + assert my_singleton == ('abc', ) + my_singleton2 = 'this ', 'is', 'new', + assert my_singleton2 == ('this ', 'is', 'new', ) + # The following example is called tuple packing: packed_tuple = 12345, 54321, 'hello!' diff --git a/src/exceptions/test_handle_exceptions.py b/src/exceptions/test_handle_exceptions.py index 342561a3..04336cdc 100644 --- a/src/exceptions/test_handle_exceptions.py +++ b/src/exceptions/test_handle_exceptions.py @@ -104,3 +104,5 @@ def test_handle_exceptions(): assert not exception_has_been_handled assert no_exceptions_has_been_fired + + # Good to learn this feature \ No newline at end of file diff --git a/src/exceptions/test_raise_exceptions.py b/src/exceptions/test_raise_exceptions.py index 3d5ede1c..9ae81943 100644 --- a/src/exceptions/test_raise_exceptions.py +++ b/src/exceptions/test_raise_exceptions.py @@ -19,6 +19,7 @@ def test_raise_exception(): # exception class is passed, it will be implicitly instantiated by calling its constructor # with no arguments raise NameError('HiThere') # shorthand for 'raise ValueError()' + raise NameError("someerror") except NameError: exception_is_caught = True @@ -41,6 +42,20 @@ def __init__(self, message): custom_exception_is_caught = False + class My1stError(Exception): + """Try by myself""" + def __init__(self, msg): + super().__init__(msg) + self.message = msg + + my1stexception = False + try: + raise My1stError("My 1st error msg") + except My1stError: + my1stexception = True + + assert my1stexception == True + try: raise MyCustomError('My custom message') except MyCustomError: diff --git a/src/functions/test_function_annotations.py b/src/functions/test_function_annotations.py index 4c6495e6..f3c2ad07 100644 --- a/src/functions/test_function_annotations.py +++ b/src/functions/test_function_annotations.py @@ -15,7 +15,6 @@ def breakfast(ham: str, eggs: str = 'eggs') -> str: """Breakfast creator. - This function has a positional argument, a keyword argument, and the return value annotated. """ return ham + ' and ' + eggs @@ -25,3 +24,6 @@ def test_function_annotations(): """Function Annotations.""" assert breakfast.__annotations__ == {'eggs': str, 'ham': str, 'return': str} + assert breakfast.__doc__ == """Breakfast creator. + This function has a positional argument, a keyword argument, and the return value annotated. + """ \ No newline at end of file diff --git a/src/functions/test_function_arbitrary_arguments.py b/src/functions/test_function_arbitrary_arguments.py index b3f36627..435abccf 100644 --- a/src/functions/test_function_arbitrary_arguments.py +++ b/src/functions/test_function_arbitrary_arguments.py @@ -31,3 +31,6 @@ def concat(*args, sep='/'): assert concat('earth', 'mars', 'venus') == 'earth/mars/venus' assert concat('earth', 'mars', 'venus', sep='.') == 'earth.mars.venus' + +def test_my_arbitrary_arguments(): + """Test arbitrary argument list""" diff --git a/src/functions/test_function_default_arguments.py b/src/functions/test_function_default_arguments.py index aa2a09ad..a1bd9a2d 100644 --- a/src/functions/test_function_default_arguments.py +++ b/src/functions/test_function_default_arguments.py @@ -24,3 +24,8 @@ def test_default_function_arguments(): # We may also want to override the second argument by using the following function calls. assert power_of(3, 2) == 9 assert power_of(3, 3) == 27 + assert my_func(4) == 2 ** 3 / 4 + +def my_func(division, number = 2, power = 3): + return number ** power / division + diff --git a/src/functions/test_function_definition.py b/src/functions/test_function_definition.py index 82a0b50b..44491934 100644 --- a/src/functions/test_function_definition.py +++ b/src/functions/test_function_definition.py @@ -94,6 +94,11 @@ def call_func(func): assert call_func(greet_one_more) == 'Hello, John' + def my_test(name): + return 'My name is: {}'.format(name) + + assert call_func(my_test) == 'My name is: John' + # Functions can return other functions. In other words, functions generating other functions. def compose_greet_func(): @@ -118,6 +123,19 @@ def get_message(): return get_message + def compose_modify_outer_varialbe(name): + age = 10 + + def get_message(): + # age = 3 + return "{} age is : {}".format(name, age) + + assert age == 10 + return get_message + + modify_outer_closure = compose_modify_outer_varialbe('John') + assert modify_outer_closure() == 'John age is : 10' + greet_with_closure = compose_greet_func_with_closure('John') assert greet_with_closure() == 'Hello there, John!' diff --git a/src/functions/test_function_keyword_arguments.py b/src/functions/test_function_keyword_arguments.py index 65d066c1..e273858f 100644 --- a/src/functions/test_function_keyword_arguments.py +++ b/src/functions/test_function_keyword_arguments.py @@ -117,3 +117,5 @@ def test_function(first_param, *arguments, **keywords): fourth_param_name='fourth named param', fifth_param_name='fifth named param', ) + + # useful \ No newline at end of file diff --git a/src/functions/test_function_scopes.py b/src/functions/test_function_scopes.py index 5d3525e5..258f1303 100644 --- a/src/functions/test_function_scopes.py +++ b/src/functions/test_function_scopes.py @@ -105,3 +105,5 @@ def test_global_variable_access(): # it was changed by "someone" and you need to know about the CONTEXT of who had changed that. # So once again access global and non local scope only if you know what you're doing otherwise # it might be considered as bad practice. + + # nonlocal, global \ No newline at end of file diff --git a/src/getting_started/test_variables.py b/src/getting_started/test_variables.py index d41e777a..a83546c3 100644 --- a/src/getting_started/test_variables.py +++ b/src/getting_started/test_variables.py @@ -36,3 +36,10 @@ def test_variables(): variable_with_changed_type = 'Sally' # x is now of type str assert variable_with_changed_type == 'Sally' + +def test_my_variables(): + """Test my own variables""" + ab_cdd_e = 1 + my_name = "Evan" + assert ab_cdd_e == 1 + assert my_name == "Evan" diff --git a/src/modules/test_modules.py b/src/modules/test_modules.py index 7f919d61..5cab8cb9 100644 --- a/src/modules/test_modules.py +++ b/src/modules/test_modules.py @@ -95,3 +95,5 @@ def test_modules(): 'fibonacci_at_position', 'fibonacci_smaller_than', ] + + diff --git a/src/operators/test_arithmetic.py b/src/operators/test_arithmetic.py index fa66ad66..a7c76f16 100644 --- a/src/operators/test_arithmetic.py +++ b/src/operators/test_arithmetic.py @@ -42,3 +42,14 @@ def test_arithmetic_operators(): assert 7 // 3 == 2 assert 9 // 3 == 3 assert isinstance(5 // 3, int) + +def test_my_arithetic_operators(): + """my operators""" + # addition + assert 5 + 5 == 10 + # subtraction + assert 90 - 90.0 == 0.0 + assert isinstance(5 / 3, float) + assert isinstance(4 / 2, float) + + assert 8 + 8 == 16