10 Python Functions That Will Simplify Your Life

preview_player
Показать описание
In this video, we will be looking at 10 Python functions that will simplify your life. These can save you a ton of time and a lot of headache, and they get more and more interesting as we go through them.

🎞 Video Resources 🎞

⏳ Timestamps ⏳
00:00 | pprint
03:28 | pickle
06:35 | any
08:17 | all

Hashtags
#python
Рекомендации по теме
Комментарии
Автор

the dataclass module also has astuple() and asdict() functions that are really useful when you're sending your data to another function (e.g., a plotter) that doesn't care about all the class structure. And the "field" function is great for defining what each attribute is. I use it on space craft telemetry, and you can attach ranges, alarm levels, units (ounce-inch or N-m, for example), and also LaTex r-strings for status plots. It saves much boiler plate code. Moreover, it puts all the information in static variables attached to the instance, not a module dictionary, or worse: run-time magic numbers .

DrDeuteron
Автор

not sure if that appeared in the comments, but there is one tricky case: `all` called with the empty iterable returns True, which may be unexpected: `all([]) -> True`

marcincuprjak
Автор

One super useful thing for me is named tuple from collections. This allows to get an information of an tuple by using a keyword instead of an number. This way you don't have to keep in mind which index saved what. Especially useful, when you haven't worked for some time on the project

watchingwatches
Автор

Rather than using the pprint module which does not work as good with nested lists and dictionaries, I prefer to write my own pprint function like this:
import json

def pprint(data: Dict[Any, Any]):
print(json.dumps(data, indent=4))



This gives us a lot cleaner output

AvinashSingh-kloz
Автор

7:31 or if its a set you can use “if “apple” in strings”

Rickety
Автор

Good job, Tim.
I created something similar, but for top 30.

ridwanray
Автор

1:55 i use the standard get method for this on dicts... You can pass the value as argument to the get-function, which will be returned, if the key does not exist, e.g false, empty strings..very useful in condition clauses /control structures and moreover in higher concepts (syntactic sugar ) like list- and dict comprehensions, where you can use if-structures as well.

Imagine you want to list compehend some values from multiple dict elements, which are in a list and share the same key mapping to the desired value in each one...
vals = [val["someKey"] for val in myDict if val.get("someKey", False)!=False]
now imagine these val["someKey"] are strings and you do not want the empty strings in your final vals list:
vals = [val["someKey"] for val in myDict if val.get("someKey", '').strip()!='']

Of course, you can first cast them to str or whatever, if the type is any because of the nature of this dynamically typed programming language

multigladiator
Автор

The variety of content you make says a lot about how much knowledge you have! ❤
I start feeling fomo how can i cover so much content 😅🥺

usamashami
Автор

14:20 yea man itertools, now we get to the stuff I am talking about in my previous comment

multigladiator
Автор

Thanks for the video. My fav was counter()!

mint
Автор

Hey Tim, thanks for the video! Can you make a video about the new FastHTML framework? It looks really promising!

peterkahofficial
Автор

Try unpacking a pickled class with out a class definition available (for whatever reason)

mlguy
Автор

I don't think enumerate returns an index, as evidenced by the "start=" keyword..rather it provides a count. Useful for managing matplotlib subplots, for example.

DrDeuteron
Автор

16:50 yes!!! decorators are great stuff. there are many many more in standard python. moreover, if you use modules like flask and stuff. you will also see this concept in java environment e.g if you use spring boot for you application server. Imagine you would have check, if the user sending an incoming request is legit, has certain roles, ...you can do this stuff with decorators (code defined in one place!) instead of duplicating code in the beginning of these endpoints

multigladiator
Автор

Q: you stated "wb" and "rb" is "write bytes" and "read bytes" in my many years of data analysis I always stated "binary" where you state "bytes"; the question is where did you find that it means "bytes"?

eboyd
Автор

`dataclass` seems like it serves a similar purpose to the `Record` class in Java (allowing for more data-oriented programming), but with slightly different built-in methods. It's interesting that `dataclass` isn't immutable by default though (you have to set `frozen=True`).

Great video :)

RickGladwin
Автор

I have an error when run program, when I input the Video, then choose folder for download, but it shows:" HTTP error 400: bad request", Please help me why?

toancdu
Автор

I’m a year into coding I can make small projects but like I wanna make really complex things what does chat or Tim recommend

Beanbag
Автор

4:42 what if class is not defined before load object from pickle file ?

marcinziajkowski
Автор

I'm stuck at 3.8 for security, and dataclass is available.

DrDeuteron