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
Namedtuples sample Namedtuples are immutable containers, just like regular tuples. All attributes on a namedtuple object foll
Namedtuples sample Namedtuples are immutable containers, just like regular tuples. All attributes on a namedtuple object follow the “write once, read many” principle. namedtuple is also immutable as tuples.

Title: Object copying #Source "Python Tricks" by Dan Bader
Title: Object copying #Source "Python Tricks" by Dan Bader

ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as 'a' or '@' or an action of some sort.

there are actually two dunder methods that control how objects are converted to strings in Python 3. __str__ is one of Python
there are actually two dunder methods that control how objects are converted to strings in Python 3. __str__ is one of Python’s “dunder” (double-underscore) methods and gets called when you try to convert an object into a string Inspecting an object in a Python inter- preter session simply prints the result of the object’s __repr__ . Why Every Class Needs a __repr__ If you don’t add a str method, Python falls back on the result of __repr__ when looking for __str__ .

#object_comparisons “is” vs “==”
#object_comparisons “is” vs “==”

#functions Function notes Python adds an implicit return None statement to the end of any function. Therefore, if a function doesn’t specify a return value, it re- turns None by default. you can replace return None statements with bare #Source "Python Tricks" by Dan Bader

PUT with PATCH PUT is idempotent, while PATCH is not. What does idempotent mean?
It means that any number of repeated, identical requests will leave the resource in the same state. 
or Idempotent operations produce the same result even when the operation is repeated many times. The result of the 2nd, 3rd, and 1,000th repeat of the operation will return exactly the same result as the 1st time. common idempotent methods: GET, HEAD, PUT, DELETE, OPTIONS, TRACE common non-idempotent methods: POST,PATCH, CONNECT

What are dunders? In the python community Dunders are double underscores. For example, you’d pronounce __baz as “dunder baz.”

Single Leading Underscore: _var is a hint to tell another programmer that a variable or method starting with a single underscore is intended for internal use. !!!Note: if you import all the names from the module, Python will not import names with a leading underscore(unless the module defines an all list that overrides this behavior) #Source "Python Tricks" by Dan Bader

Context Managers #not_secure f = open('hello.txt', 'w') f.write('hello, world') f.close() This implementation won’t guarantee the file is closed if there’s an exception during the f.write() call #Secure with open('hello.txt', 'w') as f: f.write('hello, world!') Behind the scenes context manager is implementing the following: f = open('hello.txt', 'w') try: f.write('hello, world') finally: f.close() #Source "Python Tricks" by Dan Bader

#not_secure f = open('hello.txt', 'w') f.write('hello, world') f.close() This implementation won’t guarantee the file is closed if there’s an exception during the f.write() call #Secure with open('hello.txt', 'w') as f: f.write('hello, world!')

Instead of this names = ['Alice', 'Bob', 'Dilbert'] Use: >>> names = [ ... 'Alice', ... 'Bob', ... 'Dilbert', ... ] That way there’s one item per line, making it perfectly clear which one was added, removed, or modified when you view a diff in your source control system. It’s a small change but helpful to avoid silly mistakes. It also easier for your teammates to review your code changes. #Source "Python Tricks" by Dan Bader

#Some_JWT_Notes The access token is usually short-lived (expires in 5 min or so, can be customized though). The refresh token lives a little bit longer (expires in 24 hours, also customizable). It is comparable to an authentication session. After it expires, you need a full login with username + password again. Source

Sort dictionary sample
Sort dictionary sample

Collections module Counter (string sample)
Collections module Counter (string sample)

#quote "The more specific the specification is, the more likely your code will match the customer’s expectations. " Michael Driscoll

#SQL A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
#SQL A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Updated my course. It now also teaches you how to write your own ORM: https://mobile.twitter.com/testdrivenio/status/1366746063600762881

SQLAlchemy is usually referred to as an Object Relational Mapper (ORM), although it is much more full featured than any of the other Python ORMs . SQLAlchemy was created by Michael Bayer Instead of hiding away SQL and object relational details behind a wall of automation, all processes are fully exposed within a series of composable, transparent tools. The library takes on the job of automating redundant tasks while the developer remains in control of how the database is organized and how SQL is constructed. The main goal of SQLAlchemy is to change the way you think about databases and SQL! Source

The Python lambda statement is an anonymous or unbound function It is good for inline functions
The Python lambda statement is an anonymous or unbound function It is good for inline functions