|
1086 | 1086 | }, |
1087 | 1087 | { |
1088 | 1088 | "cell_type": "code", |
1089 | | - "execution_count": 33, |
| 1089 | + "execution_count": 91, |
1090 | 1090 | "metadata": {}, |
1091 | 1091 | "outputs": [ |
1092 | 1092 | { |
1093 | 1093 | "name": "stdout", |
1094 | 1094 | "output_type": "stream", |
1095 | 1095 | "text": [ |
1096 | 1096 | "Jane Doe works at TechCorp\n", |
1097 | | - "Jane Doe studies at State University\n", |
1098 | | - "Jane Doe studies at Advanced Studies at State University\n", |
1099 | | - "Jane Doe has specialization in Software Engineering and engages in advanced studies at Advanced Studies at State University\n", |
| 1097 | + "\u001b[35mJane Doe studies at State University\n", |
| 1098 | + "\u001b[35mJane Doe studies at Advanced Studies at State University\n", |
| 1099 | + "\u001b[36mJane Doe has specialization in Software Engineering and engages in advanced studies at Advanced Studies at State University\n", |
1100 | 1100 | "\u001b[32m======================================================= Dict ======================================================\n", |
1101 | 1101 | "obj.__dict__ = {'name': 'Jane Doe', 'company': 'TechCorp', 'school': 'Advanced Studies at State University', 'specialization': 'Software Engineering'}\n", |
1102 | 1102 | "\u001b[34m============================================= Method resolution order =============================================\n", |
|
1129 | 1129 | " def __init__(self, name: str, school: str):\n", |
1130 | 1130 | " self.name = name\n", |
1131 | 1131 | " self.school = school\n", |
1132 | | - " print(f\"{self.name} studies at {self.school}\")\n", |
| 1132 | + " print(Fore.MAGENTA + f\"{self.name} studies at {self.school}\")\n", |
1133 | 1133 | " \n", |
1134 | 1134 | "class WorkingStudent(Employee, Student): # Class C\n", |
1135 | 1135 | " \"\"\"\n", |
|
1150 | 1150 | " # Call to the Student init is adjusted here to differentiate tasks or advanced level of study\n", |
1151 | 1151 | " Student.__init__(self, name, \"Advanced Studies at \" + school)\n", |
1152 | 1152 | " self.specialization = specialization\n", |
1153 | | - " print(f\"{self.name} has specialization in {self.specialization} and engages in advanced studies at {self.school}\")\n", |
| 1153 | + " print(Fore.CYAN + f\"{self.name} has specialization in {self.specialization} and engages in advanced studies at {self.school}\")\n", |
1154 | 1154 | "\n", |
1155 | 1155 | "# Create an instance of SpecialWorkingStudent\n", |
1156 | 1156 | "obj = SpecialWorkingStudent(name=\"Jane Doe\", company=\"TechCorp\", school=\"State University\", specialization=\"Software Engineering\")\n", |
|
2815 | 2815 | }, |
2816 | 2816 | { |
2817 | 2817 | "cell_type": "code", |
2818 | | - "execution_count": null, |
| 2818 | + "execution_count": 87, |
2819 | 2819 | "metadata": {}, |
2820 | 2820 | "outputs": [ |
2821 | 2821 | { |
2822 | 2822 | "name": "stdout", |
2823 | 2823 | "output_type": "stream", |
2824 | 2824 | "text": [ |
2825 | | - "Setting attribute name to Alice\n", |
2826 | | - "Setting attribute value to 42\n", |
2827 | | - "Setting attribute data to {}\n", |
| 2825 | + "\u001b[36mSetting attribute name to Alice\n", |
| 2826 | + "\u001b[36mSetting attribute value to 42\n", |
| 2827 | + "\u001b[36mSetting attribute data to {}\n", |
2828 | 2828 | "Alice has value 42\n", |
2829 | | - "Example(name='Alice', value=42)\n", |
2830 | | - "Setting attribute name to Bob\n", |
2831 | | - "Setting attribute value to 30\n", |
2832 | | - "Setting attribute data to {}\n", |
| 2829 | + "\u001b[32mExample(name='Alice', value=42)\n", |
| 2830 | + "\u001b[36mSetting attribute name to Bob\n", |
| 2831 | + "\u001b[36mSetting attribute value to 30\n", |
| 2832 | + "\u001b[36mSetting attribute data to {}\n", |
2833 | 2833 | "False\n", |
2834 | 2834 | "True\n", |
2835 | 2835 | "value1\n", |
2836 | 2836 | "This is a special value!\n", |
2837 | | - "Attribute 'missing_attr' not found!\n", |
2838 | | - "Setting attribute name to Charlie\n", |
2839 | | - "Deleting attribute name\n" |
| 2837 | + "\u001b[35mAttribute 'missing_attr' not found!\n", |
| 2838 | + "\u001b[36mSetting attribute name to Charlie\n", |
| 2839 | + "\u001b[33mDeleting attribute name\n" |
2840 | 2840 | ] |
2841 | 2841 | } |
2842 | 2842 | ], |
|
2851 | 2851 | "\n", |
2852 | 2852 | " def __str__(self):\n", |
2853 | 2853 | " # __str__ provides a human-readable string\n", |
2854 | | - " return Fore.GREEN + f\"{self.name} has value {self.value}\"\n", |
| 2854 | + " return f\"{self.name} has value {self.value}\"\n", |
2855 | 2855 | "\n", |
2856 | 2856 | " def __repr__(self):\n", |
2857 | 2857 | " # __repr__ provides a debug-friendly string\n", |
2858 | | - " return f\"Example(name={self.name!r}, value={self.value!r})\"\n", |
| 2858 | + " return Fore.GREEN + f\"Example(name={self.name!r}, value={self.value!r})\"\n", |
2859 | 2859 | "\n", |
2860 | 2860 | " def __eq__(self, other):\n", |
2861 | 2861 | " # __eq__ compares instances for equality\n", |
|
2879 | 2879 | "\n", |
2880 | 2880 | " def __getattr__(self, attr):\n", |
2881 | 2881 | " # __getattr__ handles missing attributes\n", |
2882 | | - " return f\"Attribute '{attr}' not found!\"\n", |
| 2882 | + " return Fore.MAGENTA + f\"Attribute '{attr}' not found!\"\n", |
2883 | 2883 | "\n", |
2884 | 2884 | " def __getattribute__(self, attr):\n", |
2885 | 2885 | " # __getattribute__ handles all attribute access\n", |
|
2889 | 2889 | "\n", |
2890 | 2890 | " def __setattr__(self, attr, value):\n", |
2891 | 2891 | " # __setattr__ allows custom logic when setting attributes\n", |
2892 | | - " print(f\"Setting attribute {attr} to {value}\")\n", |
| 2892 | + " print(Fore.CYAN + f\"Setting attribute {attr} to {value}\")\n", |
2893 | 2893 | " super().__setattr__(attr, value)\n", |
2894 | 2894 | "\n", |
2895 | 2895 | " def __delattr__(self, attr):\n", |
2896 | 2896 | " # __delattr__ allows custom logic when deleting attributes\n", |
2897 | | - " print(f\"Deleting attribute {attr}\")\n", |
| 2897 | + " print(Fore.YELLOW + f\"Deleting attribute {attr}\")\n", |
2898 | 2898 | " super().__delattr__(attr)\n", |
2899 | 2899 | "\n", |
2900 | 2900 | "\n", |
|
2958 | 2958 | }, |
2959 | 2959 | { |
2960 | 2960 | "cell_type": "code", |
2961 | | - "execution_count": 162, |
| 2961 | + "execution_count": 89, |
2962 | 2962 | "metadata": {}, |
2963 | 2963 | "outputs": [ |
2964 | 2964 | { |
2965 | 2965 | "name": "stdout", |
2966 | 2966 | "output_type": "stream", |
2967 | 2967 | "text": [ |
2968 | 2968 | "aa welcome!\n", |
2969 | | - "aa has purchased the course: matlab\n", |
| 2969 | + "\u001b[32maa has purchased the course: matlab\n", |
2970 | 2970 | "rr welcome!\n", |
2971 | | - "rr has purchased the course: python\n", |
| 2971 | + "\u001b[32mrr has purchased the course: python\n", |
2972 | 2972 | "['aa', 'rr']\n", |
2973 | 2973 | "{'aa': 'matlab', 'rr': 'python'}\n", |
2974 | | - "f must register.\n", |
| 2974 | + "\u001b[36mf must register.\n", |
2975 | 2975 | "f welcome!\n", |
2976 | | - "f has an account.\n", |
2977 | | - "['aa', 'rr', 'f']\n" |
| 2976 | + "\u001b[36mf has an account.\n", |
| 2977 | + "\u001b[35mStudent.users = ['aa', 'rr', 'f']\n" |
2978 | 2978 | ] |
2979 | 2979 | } |
2980 | 2980 | ], |
|
3009 | 3009 | "\n", |
3010 | 3010 | " def login(self, name):\n", |
3011 | 3011 | " if name in Student.users:\n", |
3012 | | - " print(f\"{name} has an account.\")\n", |
| 3012 | + " print(Fore.CYAN + f\"{name} has an account.\")\n", |
3013 | 3013 | " else:\n", |
3014 | | - " print(f\"{name} must register.\")\n", |
| 3014 | + " print(Fore.CYAN + f\"{name} must register.\")\n", |
3015 | 3015 | "\n", |
3016 | 3016 | " def buy(self, coursename):\n", |
3017 | 3017 | " Student.users_course[self.name] = coursename\n", |
3018 | | - " print(f\"{self.name} has purchased the course: {coursename}\")\n", |
| 3018 | + " print(Fore.GREEN + f\"{self.name} has purchased the course: {coursename}\")\n", |
3019 | 3019 | "\n", |
3020 | 3020 | "\n", |
3021 | 3021 | "# Create objects of the class\n", |
|
3033 | 3033 | "student_f = Student(\"f\", \"f@.com\", \"5478\", True)\n", |
3034 | 3034 | "student_f.login(\"f\") # Now attempts to log in \"f\" again\n", |
3035 | 3035 | "\n", |
3036 | | - "print(Student.users) # Prints the updated list of user names" |
| 3036 | + "print(Fore.MAGENTA + f\"{Student.users = }\") # Prints the updated list of user names" |
3037 | 3037 | ] |
3038 | 3038 | }, |
3039 | 3039 | { |
|
3181 | 3181 | }, |
3182 | 3182 | { |
3183 | 3183 | "cell_type": "code", |
3184 | | - "execution_count": null, |
| 3184 | + "execution_count": 82, |
3185 | 3185 | "metadata": {}, |
3186 | 3186 | "outputs": [ |
3187 | 3187 | { |
3188 | 3188 | "name": "stdout", |
3189 | 3189 | "output_type": "stream", |
3190 | 3190 | "text": [ |
3191 | 3191 | "Function 'add' is about to run with arguments (5, 10) {}.\n", |
3192 | | - "Function 'add' has finished running.\n", |
3193 | | - "15\n" |
| 3192 | + "\u001b[32mFunction 'add' has finished running.\n", |
| 3193 | + "\u001b[36madd(5, 10) =15\n" |
3194 | 3194 | ] |
3195 | 3195 | } |
3196 | 3196 | ], |
|
3208 | 3208 | " def wrapper(*args, **kwargs):\n", |
3209 | 3209 | " print(f\"Function '{func.__name__}' is about to run with arguments {args} {kwargs}.\")\n", |
3210 | 3210 | " result = func(*args, **kwargs)\n", |
3211 | | - " print(f\"Function '{func.__name__}' has finished running.\")\n", |
| 3211 | + " print(Fore.GREEN + f\"Function '{func.__name__}' has finished running.\")\n", |
3212 | 3212 | " return result\n", |
3213 | 3213 | " return wrapper\n", |
3214 | 3214 | "\n", |
|
3217 | 3217 | " return a + b\n", |
3218 | 3218 | "\n", |
3219 | 3219 | "# Call the function\n", |
3220 | | - "print(add(5, 10))" |
| 3220 | + "print(Fore.CYAN + f\"{add(5, 10) =}\")" |
3221 | 3221 | ] |
3222 | 3222 | }, |
3223 | 3223 | { |
|
3230 | 3230 | }, |
3231 | 3231 | { |
3232 | 3232 | "cell_type": "code", |
3233 | | - "execution_count": 6, |
| 3233 | + "execution_count": 88, |
3234 | 3234 | "metadata": {}, |
3235 | 3235 | "outputs": [ |
3236 | 3236 | { |
3237 | 3237 | "name": "stdout", |
3238 | 3238 | "output_type": "stream", |
3239 | 3239 | "text": [ |
3240 | | - "Car has Wi-Fi.\n", |
3241 | | - "Airplane has Wi-Fi.\n", |
3242 | | - "Airplane has music.\n", |
3243 | | - "======================================================= Dict ======================================================\n", |
| 3240 | + "\u001b[32mCar has Wi-Fi.\n", |
| 3241 | + "\u001b[32mAirplane has Wi-Fi.\n", |
| 3242 | + "\u001b[36mAirplane has music.\n", |
| 3243 | + "\u001b[35m======================================================= Dict ======================================================\n", |
3244 | 3244 | "obj.__dict__ = {}\n" |
3245 | 3245 | ] |
3246 | 3246 | } |
3247 | 3247 | ], |
3248 | 3248 | "source": [ |
3249 | 3249 | "class WifiMixin:\n", |
3250 | 3250 | " def Wifi(self):\n", |
3251 | | - " print(f\"{self.__class__.__name__} has Wi-Fi.\")\n", |
| 3251 | + " print(Fore.GREEN + f\"{self.__class__.__name__} has Wi-Fi.\")\n", |
3252 | 3252 | "\n", |
3253 | 3253 | "class MusicMixin:\n", |
3254 | 3254 | " def music(self):\n", |
3255 | | - " print(f\"{self.__class__.__name__} has music.\")\n", |
| 3255 | + " print(Fore.CYAN + f\"{self.__class__.__name__} has music.\")\n", |
3256 | 3256 | "\n", |
3257 | 3257 | "class Vehicle:\n", |
3258 | 3258 | " def mov(self):\n", |
|
3274 | 3274 | "obj.Wifi()\n", |
3275 | 3275 | "obj.music()\n", |
3276 | 3276 | "\n", |
3277 | | - "print(f\"{55 * \"=\"} Dict {54 * \"=\"}\\n{obj.__dict__ = }\")" |
| 3277 | + "print(Fore.MAGENTA + f\"{55 * \"=\"} Dict {54 * \"=\"}\\n{obj.__dict__ = }\")" |
3278 | 3278 | ] |
3279 | 3279 | } |
3280 | 3280 | ], |
|
0 commit comments