Pythonic Dev
678 subscribers
103 photos
1 video
25 links
Happy Coding 💫
ADMIN: @cmatrix1
Download Telegram
🔗 Understanding Union Typing in Python

Do you ever find yourself needing to handle variables that can have different types? Python's Union typing comes to the rescue! 🦸‍♂️

With Union, you can specify that a variable or function parameter can accept multiple types. Let's dive into a quick example (Look Photo)

In this code snippet, the display_value function takes an argument called value. By using Union[int, float, str], we indicate that value can be of type int, float, or str. This flexibility allows us to handle different kinds of data gracefully.

The benefits of using Union are twofold: it enhances code readability by explicitly stating valid types, and it enables static type checkers to catch potential issues before runtime.

Next time you encounter a situation where you need to handle multiple possible types, remember the power of Union. Happy coding! 😄🐍

#python
@Pythonic_Dev
👍2
Tamasha
Mahasti
#Music :))
🐍 Python Development Tips: Introducing Pydantic 🚀

Pydantic is a powerful library that brings structure and type checking to your Python code. It allows you to define data schemas using plain Python classes with annotations, making it easier to validate and parse incoming data. Here's a basic example to give you a taste of what Pydantic can do

The real power of Pydantic lies in its ability to handle complex validations, default values, and serialization/deserialization of data. You can define optional fields, perform complex validation logic, and even nest models within each other.

For a more comprehensive understanding of Pydantic and its advanced features, I highly recommend checking out this tutorial on YouTube: Pydantic Tutorial. and Pydantic Documents

I will be covering numerous posts about Pydantic in the future because it is an Perfect library. Happy coding! 🎉

#python
#pydantic
@Pythonic_Dev
👍2
the proper time to review code for security gaps is once the architecture behind the code commit has been properly reviewed

this means code reviews should be the second step in an organization that follows secure development best practice

this has two benefits the first and most obvious benefit is that of security but having an additional reviewer who typically is viewing the code from outside the immediate development team has its own merits as well

As is such the code security reviewer phase is vital for both application functionality as well as application security.
code security reviewers should be implemented as an additional step in organization that only have a functional reviews.
Doing so will dramatically reduced the number of high impact security bugs that would otherwise be released into a production environment

#security
Source: Web Application Security Book
@Pythonic_Dev
Pythonic Dev
🐍 Python Development Tips: Shallow Copy 🐍 🔍 What is Shallow Copy? When working with objects in Python, copying an object usually involves creating a new object with the same values as the original. Shallow copy is one such technique where a new object is…
Real Use Case of Shallow Copy

The disconnect method in the manager removes the WebSocket object from active_connections. I need the WebSocket objects in active_connections because I want to disconnect all websockets. However, I can't iterate through active_connections directly because its size will change during the iteration operation. Therefore, I need the objects but don't require the active_connections sequence. To achieve this, I use the .copy() method, which returns a shallow copy of the active_connections set.

@Pythonic_Dev
🐍📢 Hey Python enthusiasts! let's dive into the power of iterators in Python. 🚀

So, what exactly is an iterator? In simple terms, an iterator is an object that can be iterated (looped) over. It represents a stream of data that can be fetched one element at a time, without loading the entire dataset into memory at once.

Look at photo. Consider the task of reading a file, "test.txt," and printing its contents in chunks of 12 characters

in second code, we define a lambda function called method that reads 12 characters from the file each time it's called. By passing this function along with an empty string ("") to iter, we create an iterator that keeps fetching data until an empty string is encountered. The loop then iterates over the iterator, printing each chunk of data.

Using iter with a callable function provides a cleaner and more concise alternative to the while loop. It encapsulates the logic of reading chunks inside method, making our code more readable and maintainable.

Happy coding! 🎉💻
👍2
📢 Hey Pythonistas! 🐍👩‍💻

Let's dive into the world of tuple unpacking in Python today! 🎉

Tuple unpacking is a powerful feature that allows you assign multiple variables at once from a tuple. It provides a concise and efficient way to extract values from tuples, making your code more readable and expressive.

Happy coding! 💻

#Python
@Pythonic_Dev
1
1
What is the Output of code?
Anonymous Quiz
13%
1 2
56%
2 3
23%
2 4
8%
3 4
1
Pythonic Dev
What is the Output of code?
Most people who choose option 3 have a higher level of accuracy compared to those who choose option 2. I doubt that most people have chosen option 2 because of the right-hand side evaluation. 😁😂
1
Pythonic Dev
#Quizz #very_easy @Pythonic_Dev
🔍 Python Code Analysis: Understanding the Output

In the first line, we assign the value 1 to the variable a. Moving on to the second line, we encounter a multiple assignment statement. This statement allows us to assign values to multiple variables simultaneously.

In this case, we have a, b = a+1, a+2. Let's break it down step step:

The expression a+1 evaluates to 2 because a currently holds the value 1.
The expression a+2 evaluates to 3 using the same logic as above.

Now, the assignment takes place. Since Python evaluates the right-hand side of the assignment before assigning the values to the variables on the left-hand side, the following happens:

The value 2 is assigned to a.
The value 3 is assigned to b.

Finally, we reach the print(a, b) statement. At this point, a holds the value 2, and b holds the value 3. Therefore, when we print a and b, the output will be:

2 , 3


📢 Important Python Tip! 🐍

When using assignment expressions in Python, remember that the right-hand side the expression is evaluated completely before the assignment takes place. This means that any calculations or operations on the right side are performed first, and then the result is assigned to the variable on the left side.

@Pythonic_Dev
1
🔍 What is namedtuple?
namedtuple is a built Python module that allows you to create lightweight, immutable data structures. It combines the simplicity of tuples with the accessibility of dictionaries, providing named fields for easy access.

🌟 Benefits of namedtuple:
1️⃣ Conc Syntax: With namedtuple, you can define a new class with named fields in just one line of code
2️⃣ Readability: By assigning names to fields, your code becomes self-explanatory, enhancing its readability and reducing the chances of errors.
3️⃣ Memory Efficiency: namedtuple objects are memory-efficient as they do not store attributes in a per-instance dict.
4️⃣ Immutable Nature: Once created, namedtuple instances cannot be modified, ensuring data integrity.

💡 Pro Tip:
You can also convert namedtuple instances to dictionaries using the _asdict() method, making it easier to work with other Python data structures.

Happy coding! 🚀

YouTube Video
#Python
@Pythonic_Dev
1
Audio
#Music For Peace Of Mind

Creater: @Amirrezasebyani
کلمه تولیدی به معنای production است


#TDD
@Pythonic_Dev
Forwarded from Python BackendHub
یک نکته که عرفان اشاره کرده:

مثلا من گاهی واقعا میبینم بچه ها over engineering میکنن که یه چیزو در optimize ترین حالت ممکن بنویسن یا فلان چیز یه وقت سربار نداشته باشه در صورتی که فراموش میکنن اصلا اصل ایده شون کار میکنه ؟
این زمانی که میذارن ارزش داره تا اگه یه زمانی ۱۰ هزار یوزر اومد تو سایت (تجربه نشون داده این اگه ها معمولا اتفاق نمیوفتن) داون نشه ؟
مثل یه جور وسواسه بین برنامه نویسا که باعث میشه واقعا غرق چیزی بشن که اون لحظه لازم نیست و نیاز نیست. قبول دارم چیزی به اسم technical debt وجود داره ولی این از اون سر بوم افتادنه که همیشه باید مراقبش باشیم.

دقیقا. کاملا درسته بنظرم.
نداشتن technical debt باعث شکست استارت آپ میشه (چون خیلی وسواس الکی به خرج دادن)
و زیاد داشتنش هم بعد از مدت کوتاهی باعث شکستش میشه (چون اصلا وسواس به خرج ندادن)

خوبه همیشه تکنیکال دبت داشت. فقط باید حدشو رعایت کرد.
@ManifoldsPython
What is the abs() method in Python?

The abs() method in Python returns the absolute value of a number. The absolute value of a number is its distance from zero. For example, the absolute value of -5 is 5, and the absolute value of 10 is 10.

Conclusion

The abs() method is a useful built-in method in Python that can be used to get the absolute value of a number. The absolute value of a number is its distance from zero. For example, the absolute value of -5 is 5, and the absolute value of 10 is 10.

#python
@Pythonic_Dev