Pythonic Dev
678 subscribers
103 photos
1 video
25 links
Happy Coding πŸ’«
ADMIN: @cmatrix1
Download Telegram
Channel name was changed to Β«Pythonic DevΒ»
Audio
#Music For Reading...
🐍 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 created, but the references to the objects contained within the original object are copied over as well, instead of creating new copies of those objects.


πŸ” Shallow Copy Limitations:
It's important to note When we made a copy of mutable, the sequence was copied, but it's elements point to the
same memory address as the original sequence elements
The sequence was copied, but it's elements were not. This means that changes made to the nested objects will affect both the original and copied objects. So, use shallow copy with caution when dealing with mutable objects.

@Pythonic_Dev
πŸ”€ Note:
Like I said Before !
when dealing with immutable objects like strings and numbers, copying isn't necessary since they are already unique and can't be modified.

#python
@Pythonic_Dev
βš™οΈ deepcopy() is a powerful function provided by the copy module in Python. It allows us to create a deep copy of an object, which means it will recursively copy all nested objects within the original one.

πŸ”’Any changes made to the copied object won't reflect back on the original one.

πŸ’‘ Here are a few important points to keep in mind while working with deepcopy():
1️⃣ It supports deep copying of many built-in types, including lists, dictionaries, sets, and more.
2️⃣ Custom objects can also be deep copied, but they must implement the deepcopy() method for proper copying behavior.
3️⃣ Be cautious when dealing with deeply nested objects or circular references, as it may result in excessive memory usage or infinite recursion.

Happy coding! πŸš€

@Pythonic_Dev
πŸ‘2
πŸ”— 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