en
Feedback
PyNotes

PyNotes

Open in Telegram

**Code is communication** admin: @Xojarbu https://t.me/solutions_py for problem solutions

Show more
270
Subscribers
No data24 hours
No data7 days
No data30 days
Posts Archive
Builtin decorators @property @property decorator turns a class method into a class attribute. This is useful for combination
Builtin decorators @property @property decorator turns a class method into a class attribute. This is useful for combination of values. Source: Python101(book)

Builtin decorators @staticmethod āœ… Declares a static method in the class. āœ… It cannot have cls or self parameter. āœ… The stati
Builtin decorators @staticmethod āœ… Declares a static method in the class. āœ… It cannot have cls or self parameter. āœ… The static method cannot access the class attributes or the instance attributes. āœ… The static method can be called using ClassName.MethodName() and also using object.MethodName(). āœ… It can return an object of the class. Source_link

A "decorator" takes the function below and does something with it Builtin decorators @classmethod āœ…Declares a class method. āœ…
A "decorator" takes the function below and does something with it Builtin decorators @classmethod āœ…Declares a class method. āœ…The first parameter must be cls, which can be used to access class attributes. āœ…The class method can only access the class attributes but not the instance attributes. āœ…The class method can be called using ClassName.MethodName() and also using object. Source_link

#compare Instance & Attribute Instance is a Python variable belonging to only one object, accessable in the scope of the object; Class attribute is a Python variable belonging to a class rather than a particular object.

Time module time.ctime() - converts a time in seconds since the epoch to a string representing local time ( For Unix systems, the epoch was in 1970). time.sleep() - suspend execution of your script a given number of seconds like a pause. time.strftime - create a string that represents the time in a more human readable format. time.time - returns the time in seconds since the epoch as a floating point number.

#compare all() and any() all() method demonstrates if List1 has List2 elements # List1 List1 = ['python' , 'javascript', 'csharp', 'go', 'c', 'c++'] # List2 List2 = ['csharp1' , 'go', 'python'] check = all(item in List1 for item in List2) any() checks if the list contains any elements of another one: # List1 List1 = ['python' , 'javascript', 'csharp', 'go', 'c', 'c++'] # List2 List2 = ['swift' , 'php', 'python'] check = any(item in List1 for item in List2)

#compare Function & Method A function inside a class changes its name to ā€œmethodā€ and method has to have at least one argument (i.e. self)

Clean Code Principles āœ…Code should be elegant and pleasing to read. āœ…No duplication should be allowed. Use DRY (Don't Repeat Yourself) āœ…Code should be covered with tests. āœ…Every function should do one thing and do it well. āœ…Codebase should contain only code that is needed.

#compare *args and **kwargs *args - allows your function to get infinite arguments (without keywords) **kwargs - allows your
#compare *args and **kwargs *args - allows your function to get infinite arguments (without keywords) **kwargs - allows your function to get infinite keyword arguments *args returns tuple **kwargs returns dictionary ** — for passing keys and values

#compare Package with module! A module is a single importable Python file whereas a package is made up of two or more modules. A package can be imported the same way a module is. Whenever you save a Python script of your own, you have created a module.

#working_with_files Title:Writing files
#working_with_files Title:Writing files

#working_with_files Title: Reading Files Builtin operator with in python will automatically close the file when you are done
#working_with_files Title: Reading Files Builtin operator with in python will automatically close the file when you are done processing it. instead of : handle = open("test.txt") can use: with open("test.txt") as file_handler

r' Treats the string as a raw string
r' Treats the string as a raw string

Try/Except/Esle/Finally Output: `No error occurred "The finally statement ran!`
Try/Except/Esle/Finally Output: `No error occurred "The finally statement ran!`

The finally Statement output: A KeyError occurred! The finally statement has executed!
The finally Statement output: A KeyError occurred! The finally statement has executed!

Multiple exceptions sample
Multiple exceptions sample

!! Bare exception is not recommended
!! Bare exception is not recommended

SearchVector For searching several fields by annotating to one field
>> from django.contrib.postgres.search import SearchVector
>>> Entry.objects.annotate(
...     search=SearchVector('body_text', 'blog__tagline'),
... ).filter(search='Cheese')
[<Entry: Cheese on Toast recipes>, <Entry: Pizza Recipes>]

Note!!! Use comments liberally not just for yourself, but for anyone else who might have to maintain or enhance your code in the future!!

#Django_ORM #Database Rename column name in Database Table: ` a = models.CharField(max_length=40,db_column='column1')`