Pythonic Dev
🔵📢 Redis Sorted Set Data Structure and Commands 🔵📢
🔵📢 Redis Sorted Set Data Structure and Commands 🔵📢
Redis sorted sets are data structures that provide an ordered collection of unique elements, each associated with a floating-point score. They are perfect for use cases such as leaderboards, ranking systems, and range queries. Sorted sets in Redis offer efficient ways to store and manipulate related data. 💡
You can think of sorted sets as a mix between a Set and a Hash. Like sets, sorted sets are composed of unique, non-repeating string elements, so in some sense a sorted set is a set as well.
However while elements inside sets are not ordered, every element in a sorted set is associated with a floating point value, called the score (this is why the type is also similar to a hash, since every element is mapped to a value).
📌 Sorted Set Commands 📌
To make the most of Redis sorted sets, it's crucial to understand and utilize the following key commands:
🔹 Adding Elements:
-
🔹 Getting Elements:
-
-
🔹 Removing Elements:
-
🔹 Updating Scores:
-
🔹 Set Operations:
-
-
🔹 Count and Rank:
-
-
-
📌 Performance 📌
Redis sorted set operations maintain efficient time complexities, typically O(log(N)) for most actions. However, performance can be impacted as the size of the sorted set increases, especially with range queries.
Exercise some caution when running the ZRANGE command with large returns values (e.g., in the tens of thousands or more). This command's time complexity is O(log(n) + m), where m is the number of results returned.
📌 Learn More 📌
Redis documentation on sorted sets: (https://redis.io/docs/data-types/sorted-sets/)
YouTube tutorial on Redis sorted sets: (https://www.youtube.com/watch?v=MUKlxdBQZ7g)
Redis enthusiasts, let's dive into the versatility of sorted sets in Redis! 🚀
#Redis
#RedisSortedSet
#RedisDataStructure
Redis sorted sets are data structures that provide an ordered collection of unique elements, each associated with a floating-point score. They are perfect for use cases such as leaderboards, ranking systems, and range queries. Sorted sets in Redis offer efficient ways to store and manipulate related data. 💡
You can think of sorted sets as a mix between a Set and a Hash. Like sets, sorted sets are composed of unique, non-repeating string elements, so in some sense a sorted set is a set as well.
However while elements inside sets are not ordered, every element in a sorted set is associated with a floating point value, called the score (this is why the type is also similar to a hash, since every element is mapped to a value).
📌 Sorted Set Commands 📌
To make the most of Redis sorted sets, it's crucial to understand and utilize the following key commands:
🔹 Adding Elements:
-
ZADD key score member [score member ...]: Adds one or more members with their associated scores to a sorted set.🔹 Getting Elements:
-
ZRANGE key start stop [WITHSCORES]: Retrieves a range of elements from the sorted set by their rank.-
ZSCORE key member: Retrieves the score of a member in a sorted set.🔹 Removing Elements:
-
ZREM key member [member ...]: Removes one or more members from the sorted set.🔹 Updating Scores:
-
ZINCRBY key increment member: Increments the score of a member in the sorted set by a specific increment.🔹 Set Operations:
-
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]: Computes the intersection of multiple sorted sets and stores the result in a new key.-
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]: Computes the union of multiple sorted sets and stores the result in a new key.🔹 Count and Rank:
-
ZCARD key: Returns the number of elements in a sorted set.-
ZRANK key member: Returns the rank of a member in the sorted set.-
ZCOUNT key min max: Returns the number of elements within a score range.📌 Performance 📌
Redis sorted set operations maintain efficient time complexities, typically O(log(N)) for most actions. However, performance can be impacted as the size of the sorted set increases, especially with range queries.
Exercise some caution when running the ZRANGE command with large returns values (e.g., in the tens of thousands or more). This command's time complexity is O(log(n) + m), where m is the number of results returned.
📌 Learn More 📌
Redis documentation on sorted sets: (https://redis.io/docs/data-types/sorted-sets/)
YouTube tutorial on Redis sorted sets: (https://www.youtube.com/watch?v=MUKlxdBQZ7g)
Redis enthusiasts, let's dive into the versatility of sorted sets in Redis! 🚀
#Redis
#RedisSortedSet
#RedisDataStructure
Docs
Develop with Redis
Learn how to develop with Redis
👍1
📢 Copying Data and Structures of Tables in PostgreSQL! 🚀
Copying a table in PostgreSQL can be quite handy in many scenarios. To do this, we will walk through two steps: copying the structure of a table and then copying the data.
To copy the structure of a table, you can use the following SQL query:
or
Replace
To copy the data along with the structure, you can use the following query:
This will create a new table with the same structure as
To just copy data you can use this query:
So there you have it, quick and easy ways to copy tables in PostgreSQL! 🤖💻
Copying a table in PostgreSQL can be quite handy in many scenarios. To do this, we will walk through two steps: copying the structure of a table and then copying the data.
To copy the structure of a table, you can use the following SQL query:
CREATE TABLE new_table AS TABLE existing_table WITH NO DATA;
or
CREATE TABLE new_table AS SELECT * FROM old_table WHERE 0=1;
Replace
new_table with the name of the new table you want to create and existing_table with the name of the table you want to copy the structure from. This will create an empty table with the same structure as the original one.To copy the data along with the structure, you can use the following query:
CREATE TABLE new_table AS TABLE existing_table;
This will create a new table with the same structure as
existing_table and copy all the data as well.To just copy data you can use this query:
INSERT INTO new_table SELECT * FROM old_table WHERE condition;
So there you have it, quick and easy ways to copy tables in PostgreSQL! 🤖💻
🐍 Hey Pythonistas! Ever wondered what Docker is and why it's all the rage in the tech world? 🤔 Let's dive in! 🚀
🐳 What is Docker?
Docker 🐳 is an open-source platform that allows you to automate the deployment of applications inside software containers. These containers bundle all the necessary code, libraries, and dependencies required to run a software application, making it easy to ensure consistency and portability across different environments.
🔍 Why use Docker?
1️⃣ Consistency: With Docker, you can package your application and its dependencies into a single container, ensuring consistency in development, testing, and production environments.
2️⃣ Isolation: Docker containers provide isolation for your applications, making it easier to manage dependencies without worrying about conflicts or compatibility issues.
3️⃣ Portability: Docker containers are lightweight and portable, allowing you to run your application on any platform that supports Docker without worrying about differences in underlying infrastructure.
4️⃣ Scalability: Docker makes it easy to scale your application horizontally by spinning up multiple containers to handle increased loads, making it ideal for microservices architecture.
5️⃣ Efficiency: Docker streamlines the development and deployment process, reducing the time and effort required to set up and configure environments for your application.
#Docker
🐳 What is Docker?
Docker 🐳 is an open-source platform that allows you to automate the deployment of applications inside software containers. These containers bundle all the necessary code, libraries, and dependencies required to run a software application, making it easy to ensure consistency and portability across different environments.
🔍 Why use Docker?
1️⃣ Consistency: With Docker, you can package your application and its dependencies into a single container, ensuring consistency in development, testing, and production environments.
2️⃣ Isolation: Docker containers provide isolation for your applications, making it easier to manage dependencies without worrying about conflicts or compatibility issues.
3️⃣ Portability: Docker containers are lightweight and portable, allowing you to run your application on any platform that supports Docker without worrying about differences in underlying infrastructure.
4️⃣ Scalability: Docker makes it easy to scale your application horizontally by spinning up multiple containers to handle increased loads, making it ideal for microservices architecture.
5️⃣ Efficiency: Docker streamlines the development and deployment process, reducing the time and effort required to set up and configure environments for your application.
#Docker
🌟 Inner workings of Docker when we execute the
🐳 Step-by-step breakdown of
1️⃣ Docker Client Request: When we trigger
2️⃣ Image Check: The Docker daemon checks if the specified image is available locally. If not found, it pulls the image from a registry like Docker Hub.
3️⃣ Container Initialization: Based on the image, a new container is created by the Docker daemon. This container is an instance of the image with its own filesystem and networking.
4️⃣ Container Start: The container starts running the predefined command or entry point specified in the Dockerfile of the image.
5️⃣ Process Execution: The container executes the command, runs the application, or performs the designated tasks within its isolated environment.
6️⃣ Container Exit: After execution completes, the container stops, preserving its state but not actively running.
🔄 What happens if
- When we rerun docker run [image], a new container instance is generated with a unique identifier, allowing concurrent execution of multiple instances of the same image.
- Each new container runs
- This behavior highlights the fundamental principle of Docker's containerization, enabling reproducibility and scalability by creating lightweight, self-contained units that are easily disposable.
#Docker
docker run [image] command and explore the outcomes if it's run multiple times! 🚢✨🐳 Step-by-step breakdown of
docker run [image] command:1️⃣ Docker Client Request: When we trigger
docker run [image], the Docker client communicates with the Docker daemon, instructing it to create a container based on the specified image.2️⃣ Image Check: The Docker daemon checks if the specified image is available locally. If not found, it pulls the image from a registry like Docker Hub.
3️⃣ Container Initialization: Based on the image, a new container is created by the Docker daemon. This container is an instance of the image with its own filesystem and networking.
4️⃣ Container Start: The container starts running the predefined command or entry point specified in the Dockerfile of the image.
5️⃣ Process Execution: The container executes the command, runs the application, or performs the designated tasks within its isolated environment.
6️⃣ Container Exit: After execution completes, the container stops, preserving its state but not actively running.
🔄 What happens if
docker run [image] is run again?- When we rerun docker run [image], a new container instance is generated with a unique identifier, allowing concurrent execution of multiple instances of the same image.
- Each new container runs
independently of existing instances, maintaining isolation and avoiding conflicts between running containers.- This behavior highlights the fundamental principle of Docker's containerization, enabling reproducibility and scalability by creating lightweight, self-contained units that are easily disposable.
#Docker
🔧 Understanding Redis Pipelining: Optimizing Data Processing Efficiency 🚀
In the realm of Redis, a powerful in-memory data structure store, Redis Pipelining stands out as a valuable technique for enhancing performance by reducing the round-trip time (RTT) during data operations. Let's delve into the concepts and workings of Redis Pipelining to grasp how it can turbocharge your application's data processing capabilities.
🔍 What is Redis Pipelining?
Redis Pipelining is a technique that allows multiple commands to be batched and sent to the Redis server in a single network round trip. Rather than sending each command individually and waiting for the response before issuing the next one, Redis Pipelining queues up commands and sends them collectively, enabling efficient and high-speed data processing.
⚙️ Request/Response Protocols and Round Trip Time (RTT)
Traditional client-server interactions involve a request-response model where each command incurs an RTT for communication. With Redis Pipelining, multiple commands are bundled together in a pipeline, minimizing the back-and-forth latency typically seen in individual command executions. This reduction in RTT translates to significant performance improvements, especially when dealing with bulk operations or transactional workflows.
⌨️ Commands and Syntax of Redis Pipelining
Redis Pipelining employs a straightforward syntax for queuing commands within a pipeline. By using the
🌟 Pros of Redis Pipelining
1. Reduced Latency: Redis Pipelining reduces the effects of network latencies by batching the requests and therefore allows for faster response times.
2. Throughput: In this way, Redis becomes an even better solution for a variety of application processes by which it enables hundreds or thousands of operations per second.
3. Atomic Transactions: The data is always consistent as all commands within a pipeline are carried out atomically.
⚠️ Cons of Redis Pipelining
1. Complexity: Synchronous Redis interactions may seem trivial as compared to implementing pipelines that are quite complex, especially with error handling.
2. Limited atomicity: While grouped commands are executed atomically, transactions are not supported within pipelines.
3. Memory Consumption: If several commands are queued and not done quickly, pipelining may use up much memory on the side of clients.
🔚 Conclusion
Redis Pipelining emerges as a powerful tool for optimizing data processing and enhancing application performance. By leveraging the batching capability of pipelines, developers can streamline interactions with Redis, reduce latency, and boost overall efficiency. Understanding the nuances of Redis Pipelining empowers developers to architect high-performance systems that excel in speed and responsiveness.
#Redis
#RedisPipelines
In the realm of Redis, a powerful in-memory data structure store, Redis Pipelining stands out as a valuable technique for enhancing performance by reducing the round-trip time (RTT) during data operations. Let's delve into the concepts and workings of Redis Pipelining to grasp how it can turbocharge your application's data processing capabilities.
🔍 What is Redis Pipelining?
Redis Pipelining is a technique that allows multiple commands to be batched and sent to the Redis server in a single network round trip. Rather than sending each command individually and waiting for the response before issuing the next one, Redis Pipelining queues up commands and sends them collectively, enabling efficient and high-speed data processing.
⚙️ Request/Response Protocols and Round Trip Time (RTT)
Traditional client-server interactions involve a request-response model where each command incurs an RTT for communication. With Redis Pipelining, multiple commands are bundled together in a pipeline, minimizing the back-and-forth latency typically seen in individual command executions. This reduction in RTT translates to significant performance improvements, especially when dealing with bulk operations or transactional workflows.
⌨️ Commands and Syntax of Redis Pipelining
Redis Pipelining employs a straightforward syntax for queuing commands within a pipeline. By using the
pipeline() method in Redis python client, developers can append commands to the pipeline and execute them in a single batch operation. The syntax is simple and intuitive, offering a seamless way to streamline data interactions with Redis.🌟 Pros of Redis Pipelining
1. Reduced Latency: Redis Pipelining reduces the effects of network latencies by batching the requests and therefore allows for faster response times.
2. Throughput: In this way, Redis becomes an even better solution for a variety of application processes by which it enables hundreds or thousands of operations per second.
3. Atomic Transactions: The data is always consistent as all commands within a pipeline are carried out atomically.
⚠️ Cons of Redis Pipelining
1. Complexity: Synchronous Redis interactions may seem trivial as compared to implementing pipelines that are quite complex, especially with error handling.
2. Limited atomicity: While grouped commands are executed atomically, transactions are not supported within pipelines.
3. Memory Consumption: If several commands are queued and not done quickly, pipelining may use up much memory on the side of clients.
🔚 Conclusion
Redis Pipelining emerges as a powerful tool for optimizing data processing and enhancing application performance. By leveraging the batching capability of pipelines, developers can streamline interactions with Redis, reduce latency, and boost overall efficiency. Understanding the nuances of Redis Pipelining empowers developers to architect high-performance systems that excel in speed and responsiveness.
#Redis
#RedisPipelines
🌟 Exploring the Power of the Reload Module in Python! 🐍
The
Here's a quick example showcasing the usage of the
Some common use cases of the
1. Interactive Development: Quickly update code in a REPL session without restarting it.
2. Hot Reloading: Dynamically reload web server components without interrupting service.
3. Plugin Systems: Enable users to extend functionality on-the-fly by reloading plugins.
Remember to use
#Python
The
reload module is a hidden gem in Python that allows you to dynamically reload a previously imported module. This can be extremely handy during development and debugging phases where you want to update your code without restarting your entire application. 🛠️Here's a quick example showcasing the usage of the
reload module:import my_moduleBy utilizing the
import importlib
# Let's assume you have made changes to my_module and want to reload it
importlib.reload(my_module)
reload module, you can seamlessly incorporate changes to your modules during runtime, leading to a more fluid and efficient development process. 🔄Some common use cases of the
reload module include:1. Interactive Development: Quickly update code in a REPL session without restarting it.
2. Hot Reloading: Dynamically reload web server components without interrupting service.
3. Plugin Systems: Enable users to extend functionality on-the-fly by reloading plugins.
Remember to use
reload responsibly, as it can lead to unexpected behavior if not applied correctly. Stay curious, experiment, and embrace the power of reload in your Python projects! 💡#Python
👍1
سلام دوستان
برای اولین بار تصمیم گرفتم که به زبان فارسی هم پست فنی بزارم و احتمالن از این به بعد بیشتر اینکار رو انجام میدم و سعی دارم امسال رو روی محتوای فارسی بیشتر کار کنم تا دوستان بیشتری بتونن استفاده کنن و محتوا بیشتر دیده بشه
ممنون میشم اگر شماهم فکر میکنید مطالب مفید هست کانال رو حمایت کنید تا مخاطب بیشتری داشته باشیم و اشتراک دانش رو قوی تر ادامه بدیم
@Pythonic_Dev
برای اولین بار تصمیم گرفتم که به زبان فارسی هم پست فنی بزارم و احتمالن از این به بعد بیشتر اینکار رو انجام میدم و سعی دارم امسال رو روی محتوای فارسی بیشتر کار کنم تا دوستان بیشتری بتونن استفاده کنن و محتوا بیشتر دیده بشه
ممنون میشم اگر شماهم فکر میکنید مطالب مفید هست کانال رو حمایت کنید تا مخاطب بیشتری داشته باشیم و اشتراک دانش رو قوی تر ادامه بدیم
@Pythonic_Dev
👍8❤3👎1
موضوعی که میخوایم امروز باهم برسیش کنیم یه تکنیک داخل کامپیوتر ساینس هست به نام Reference Counting
داستان از این قرار هست که هر موقع شما یک ابجکتی میسازید ابجکت شما یک ادرسی داره داخل مموری و منظور از reference count هم تعداد رفرنس هایی هست که به اون ادرس داخل مموری اشاره میکنند
میدونم توضیحات یک مقدار گیج کننده هست پس با یک نمونه کد مثال میزنم
الان ما داخل این کد یک ابجکت استرینگ ساختیم و بعد ایدی اش رو پرینت گرفتیم که میشه گفت ایدی اش همون ادرسش داخل مموری هست
و الان متغیر value یک اشاره کننده به اون مقدار داخل مموری هست و الان reference count اون دیتا 1 هست و اگر به عنوان مثال ما بیایم و همچین چیزی بگیم
یک رفرنس دیگه نسبت به اون ابجکت داخل مموری ساختیم و reference count اون ابجکت رو کردیم 2
اینجا دقت کنید که ما دیتای 'pythonic_dev' رو ندادیم به value2 صرفا گفتیم که value2 هم به همون چیزی اشاره کنه که value اشاره میکنه یعنی الان اگر id متغیر value2 رو پرینت بگیریم میبینیم که ایدی هاشون برابر هست یعنی هر دو به یک ابجکت داخل مموری اشاره میکنند
حالا مسله دیگه ای که هست اینه که اگر ما بیایم و یک مقدار دیگه برای value تعریف بکنیم
دیگه متغیر value به ابجکت مورد نظر ما اشاره نمیکنه و اتفاقی که میوفته reference count ابجکت ما میشه 1 و تنها رفرنسی که بهش اشاره میکنه value2 هست
حالا سوالی که مطرح میشه اینه که اگر ما بیایم و value2 رو مقدارش رو عوض بکنیم یا اصلا دلیتش بکنیم مثل
چه اتفاقی میوفته خب دقیقن نکته اصلی داستان همین جا است وقتی که رفرنس هایی که به یک ابجکت داخل مموری اشاره میکنند همه از بین برن مموری منیجر پایتون متوجه میشه و اون ابجکت رو کلا از مموری پاک میکنه و اون فضایی که برای اون ابجکت استفاده میشد حالا دوباره قابل استفاده است
در پست های اینده به gc و اینکه کجا reference counting کار نمیکنه هم اشاره میکنم
#ReferenceCounting
@Pythonic_Dev
داستان از این قرار هست که هر موقع شما یک ابجکتی میسازید ابجکت شما یک ادرسی داره داخل مموری و منظور از reference count هم تعداد رفرنس هایی هست که به اون ادرس داخل مموری اشاره میکنند
میدونم توضیحات یک مقدار گیج کننده هست پس با یک نمونه کد مثال میزنم
value = 'pythonic_dev'
print(id(value)) #131241123
الان ما داخل این کد یک ابجکت استرینگ ساختیم و بعد ایدی اش رو پرینت گرفتیم که میشه گفت ایدی اش همون ادرسش داخل مموری هست
و الان متغیر value یک اشاره کننده به اون مقدار داخل مموری هست و الان reference count اون دیتا 1 هست و اگر به عنوان مثال ما بیایم و همچین چیزی بگیم
value2 = value
یک رفرنس دیگه نسبت به اون ابجکت داخل مموری ساختیم و reference count اون ابجکت رو کردیم 2
اینجا دقت کنید که ما دیتای 'pythonic_dev' رو ندادیم به value2 صرفا گفتیم که value2 هم به همون چیزی اشاره کنه که value اشاره میکنه یعنی الان اگر id متغیر value2 رو پرینت بگیریم میبینیم که ایدی هاشون برابر هست یعنی هر دو به یک ابجکت داخل مموری اشاره میکنند
حالا مسله دیگه ای که هست اینه که اگر ما بیایم و یک مقدار دیگه برای value تعریف بکنیم
value = 1
دیگه متغیر value به ابجکت مورد نظر ما اشاره نمیکنه و اتفاقی که میوفته reference count ابجکت ما میشه 1 و تنها رفرنسی که بهش اشاره میکنه value2 هست
حالا سوالی که مطرح میشه اینه که اگر ما بیایم و value2 رو مقدارش رو عوض بکنیم یا اصلا دلیتش بکنیم مثل
del value2
چه اتفاقی میوفته خب دقیقن نکته اصلی داستان همین جا است وقتی که رفرنس هایی که به یک ابجکت داخل مموری اشاره میکنند همه از بین برن مموری منیجر پایتون متوجه میشه و اون ابجکت رو کلا از مموری پاک میکنه و اون فضایی که برای اون ابجکت استفاده میشد حالا دوباره قابل استفاده است
در پست های اینده به gc و اینکه کجا reference counting کار نمیکنه هم اشاره میکنم
#ReferenceCounting
@Pythonic_Dev
👍6❤3
سلام خدمت همه دوستان
چند روز پیش درگیر طراحی بخشی از دیتابیس یک پروژه و نوشتن 2 تا کوعری نسبتا پیچیده بودم که گفتم تجربه جالبی شده و خوبه که به اشتراک بزارمش
اگر توسعه دهنده بک اند هستید و یا کلا به موضوعات مرتبط با پایگاه داده، نرمالایزیشن و بهینهسازی کوئریها علاقهمند هستید و دنبال راهکارهای خلاقانه برای مدلسازی داده هستید، ممنون میشم پست رو بخونید
برای همین داخل یکی از ریپازیتوی های گیت هابم که مخصوص یکسری از پست ها هست یک فایل md نوشتم با نمونه کد django orm و SQL
خیلی خوشحال میشم که نظرتون رو راجب دیزاین و کوعری ها و کلا پستی که نوشتم بدونم فقط خواهشی که دارم اینه که قبل از اینکه نظری یا انتقادی بدید همه پست رو بخونید چون در ابتدا یکسری کار میکنم که شاید در نگاه اول درست نباشه ولی در انتها دلیلش رو بیان میکنم
لینک پست
چند روز پیش درگیر طراحی بخشی از دیتابیس یک پروژه و نوشتن 2 تا کوعری نسبتا پیچیده بودم که گفتم تجربه جالبی شده و خوبه که به اشتراک بزارمش
اگر توسعه دهنده بک اند هستید و یا کلا به موضوعات مرتبط با پایگاه داده، نرمالایزیشن و بهینهسازی کوئریها علاقهمند هستید و دنبال راهکارهای خلاقانه برای مدلسازی داده هستید، ممنون میشم پست رو بخونید
برای همین داخل یکی از ریپازیتوی های گیت هابم که مخصوص یکسری از پست ها هست یک فایل md نوشتم با نمونه کد django orm و SQL
خیلی خوشحال میشم که نظرتون رو راجب دیزاین و کوعری ها و کلا پستی که نوشتم بدونم فقط خواهشی که دارم اینه که قبل از اینکه نظری یا انتقادی بدید همه پست رو بخونید چون در ابتدا یکسری کار میکنم که شاید در نگاه اول درست نباشه ولی در انتها دلیلش رو بیان میکنم
لینک پست
GitHub
Pythonic_Dev-Posts/SQLChallenge-AttributeFilter at main · Cmatrix1/Pythonic_Dev-Posts
Collection of data, photos and text of my Telegram channel posts - Cmatrix1/Pythonic_Dev-Posts
❤4
سلام خدمت همه دوستان
من برگشتم با یکسری پست های جدید
اخیرا شروع به خوندن کتاب Designing Data-Intensive Applications کردم و قصد دارم بعد از مطالعه هر فصل یک خلاصه ای از اون رو در قالب یک پست یا مقاله جا بدم
مقاله اول رو در باب خلاصه فصل اول نوشتم
در ابتدا قصد داشتم به صورت کامل فارسی بنویسم ولی از یه جایی به بعد دیگه حوصله ترجمه اش رو نداشتم و فقط یک توضیح ساده و کلی به فارسی نوشتم و انگلیسی ادامه دادم
مقالات رو به صورت فایل md خواهم نوشت و روی گیت هاب منتشر میکنم و البته لینکش رو اینجا براتون میزارم و هر قسمت رو که بنویسم اینجا اعلام میکنم
خوشحال میشم نظرتون رو بدونم و اگر دوست داشتید میتونید روی گیت هاب کانتربیوت کنید
لینک خلاصه فصل اول کتاب Designing Data-Intensive Applications
من برگشتم با یکسری پست های جدید
اخیرا شروع به خوندن کتاب Designing Data-Intensive Applications کردم و قصد دارم بعد از مطالعه هر فصل یک خلاصه ای از اون رو در قالب یک پست یا مقاله جا بدم
مقاله اول رو در باب خلاصه فصل اول نوشتم
در ابتدا قصد داشتم به صورت کامل فارسی بنویسم ولی از یه جایی به بعد دیگه حوصله ترجمه اش رو نداشتم و فقط یک توضیح ساده و کلی به فارسی نوشتم و انگلیسی ادامه دادم
مقالات رو به صورت فایل md خواهم نوشت و روی گیت هاب منتشر میکنم و البته لینکش رو اینجا براتون میزارم و هر قسمت رو که بنویسم اینجا اعلام میکنم
خوشحال میشم نظرتون رو بدونم و اگر دوست داشتید میتونید روی گیت هاب کانتربیوت کنید
لینک خلاصه فصل اول کتاب Designing Data-Intensive Applications
👍6❤2👎1
You know they say economists know the price of everything and the value of nothing? Well, programmers know the benefits of everything and the trade-offs of nothing.
-- Rich Hickey
-- Rich Hickey
❤2
Applying Clean Code Principles: Refactoring for Better Structure
I recently refactored the keyboard layouts in a Telegram bot to improve clarity and maintainability. The original procedural approach worked but became difficult to manage as the project grew. By switching to an Enum-based, object-oriented structure, I made the code more readable, easier to maintain, and scalable for future features. This change not only cleaned up the code but also made it more resilient to updates. Check out the before and after below!
I recently refactored the keyboard layouts in a Telegram bot to improve clarity and maintainability. The original procedural approach worked but became difficult to manage as the project grew. By switching to an Enum-based, object-oriented structure, I made the code more readable, easier to maintain, and scalable for future features. This change not only cleaned up the code but also made it more resilient to updates. Check out the before and after below!
👍5❤3
دپندنسی اینجکشن ( Dependency Injection )
یک دیزاین پترن عالی برای مدیریت وابستگی کمک میکنه یک پروژه انعطافپذیرتر، قابل نگهداریتر و تستپذیرتر داشته باشیم
تعریفش به صورت خلاصه هم میشه به جای اینکه کلاسها و توابع وابستگیها رو بهصورت داخلی ایجاد کنیم با استفاده از این دیزاین پترن میتونیم اونارو از بیرون بهشون بدیم. (مثال هارو ببینید برای بهتر متوجه شدن)
از مزایای این دیزان پترن میشه اشاره کرد به
1. تست پذیری بیشتر چون شما میتونید کلاس های ماک شده خودتون رو بدید برای تست کردن کلاس مورد نظر خودتون مثلا اینجا میتونید کلاس دیتابیس in memory ای که خودتون ماک کردید رو بدید و دیگه کانکشن اضافه نسازید
2. راحت تر شدن تغییرات از اونجایی که کلاس ها دپندنسی های خودشون رو کنترل نمی کنند راحت تر میشه اونارو تغییر داد درصورت ثابت موندن interface اشون مثلا داخل این مثال ما میتونیم روند کار متد connect رو تغییر بدیم و به جای اینکه به sql وصل بشیم به redis وصل بشیم بدون اینکه تغییری داخل UserService بدیم
3. و اخرین ویژگی که به ذهنم میرسه اینه که کد شما اصل SRP رو حفظ میکنه و هر بخش کاری که باید انجام بده رو انجام میده مثلا اینجا داخل مثالی که از DI استفاده میشه دیگه نیازی به instantiate کردن دیتابیس داخل UserService نیست
این دیزاین پترن رو بهتره داخل پروژه هایی استفاده کنید که پیچدگی زیادی دارن و وابستگی زیادی بین بخش های مختلف وجود داره و البته برای زمان هایی که میخواید بین implement های مختلف جا به جا بشید هم کاربردیه مثل همین مثال دیتابیس SQL و Redis ای که گفتم
یک دیزاین پترن عالی برای مدیریت وابستگی کمک میکنه یک پروژه انعطافپذیرتر، قابل نگهداریتر و تستپذیرتر داشته باشیم
تعریفش به صورت خلاصه هم میشه به جای اینکه کلاسها و توابع وابستگیها رو بهصورت داخلی ایجاد کنیم با استفاده از این دیزاین پترن میتونیم اونارو از بیرون بهشون بدیم. (مثال هارو ببینید برای بهتر متوجه شدن)
از مزایای این دیزان پترن میشه اشاره کرد به
1. تست پذیری بیشتر چون شما میتونید کلاس های ماک شده خودتون رو بدید برای تست کردن کلاس مورد نظر خودتون مثلا اینجا میتونید کلاس دیتابیس in memory ای که خودتون ماک کردید رو بدید و دیگه کانکشن اضافه نسازید
2. راحت تر شدن تغییرات از اونجایی که کلاس ها دپندنسی های خودشون رو کنترل نمی کنند راحت تر میشه اونارو تغییر داد درصورت ثابت موندن interface اشون مثلا داخل این مثال ما میتونیم روند کار متد connect رو تغییر بدیم و به جای اینکه به sql وصل بشیم به redis وصل بشیم بدون اینکه تغییری داخل UserService بدیم
3. و اخرین ویژگی که به ذهنم میرسه اینه که کد شما اصل SRP رو حفظ میکنه و هر بخش کاری که باید انجام بده رو انجام میده مثلا اینجا داخل مثالی که از DI استفاده میشه دیگه نیازی به instantiate کردن دیتابیس داخل UserService نیست
این دیزاین پترن رو بهتره داخل پروژه هایی استفاده کنید که پیچدگی زیادی دارن و وابستگی زیادی بین بخش های مختلف وجود داره و البته برای زمان هایی که میخواید بین implement های مختلف جا به جا بشید هم کاربردیه مثل همین مثال دیتابیس SQL و Redis ای که گفتم
👍5
🔍 نگاهی عمیقتر به ولیدیشن (Validation) در توسعه نرمافزار
ولیدیشن به زبان ساده به معنی اعتبارسنجی دادههای ورودی سیستم قبل از اجرای عملیات داخل سیستم هست.
🔑 انواع Validation
1. Syntax Validation
سادهترین نوع ولیدیشن سینتکس ولیدیشن بررسی میکنه که آیا ساختار دیتای ورودی صحت داره یا نه. مثلاً:
اینجا سینتکس ولیدیشن بررسی میکنه که:
- order_id باید عدد باشد.
- sku باید رشته باشد.
- quantity باید یک عدد غیرمنفی باشد.
✅ نکته مهم اینکه این نوع ولیدیشن باید در لایههای اولیه مثل کنترلرهای سیستم شما انجام بشه تا از ورود دیتای اشتباه به لاجیک جلوگیری بشه.
2. Semantic Validation
ولیدیشن معنایی به مفهوم دیتای مرتبط هست. گاهی اوقات حتی اگر سینتکس دیتا درست باشه، ممکن است هنوز در زمینه تجاری منطقی نباشه.
مثلاً اگر شما لاجیکی برای تخصیص موجودی داشته باشید:
اینجا سیستم باید بررسی کنه که:
- آیا SKU-001 وجود داره؟
- آیا به اندازه کافی موجودی برای تخصیص ۱۰ واحد وجود داره؟
✅ اینجا حتی اگر سینتکس دیتا درست باشه باید در زمینه کسب و کار هم منطقی باشه.
3. Pragmatic Validation
ولیدیشن کاربردی بررسی میکنه که آیا این اکشن ممکن هست یا مجاز هست با توجه به وضعیت فعلی سیستم. مثلاً:
- اگر انبار تعطیل باشه نباید سفارشی پردازش بشه.
- یا اگر یک کد تخفیف منقضی شده باید اعمال اون روی سفارش ناموفق باشه.
یک مثال دیگر:
شما ممکن است اکشنی مثل:
داشته باشید. ولیدیشن سینتکس و معنایی درست هستند، اما اگر قانون بیزینس شما بگه "مشتری نمیتونه بیش از ۲۰ محصول در یک بار سفارش بده"، این اکشن در ولیدیشن کاربردی ناموفق میشه.
🧠 Common Validation Patterns
1. Declarative Validation
فرض کنید یک لیست از قوانین دارید مانند:
- order_id باید یک عدد صحیح باشد.
- quantity باید غیرمنفی باشد.
این قوانین را میتوان بهصورت Declarative از قبل تعریف کرد. در پایتون، میتوانید از چیزی مثل pydantic برای تعریف ورودیهای مورد انتظار استفاده کنید.
2. Postel’s Law (Tolerant Reader Pattern)
این قانون میگه:
Be conservative in what you send, but liberal in what you accept.
یعنی "در آنچه ارسال میکنید محافظهکار باشید، اما در آنچه دریافت میکنید آزاد باشید." یا به عبارت سادهتر، سیستم شما باید در پذیرش فیلدهای اضافی یا ناشناخته انعطافپذیر باشه.
برای مثال، اگر سیستم شما فقط به
میتونید فیلد اضافی (
🏁 جمعبندی
- ولیدیشن Syntax بررسی میکنه که آیا قالب دادهها صحیح هست یا نه.
- ولیدیشن Semantic بررسی میکنه که دادهها در زمینه بیزینس شما معنیدار هستند.
- ولیدیشن Pragmatic بررسی میکند که آیا عمل منطقی و با توجه به وضعیت فعلی سیستم امکانپذیر هست.
- از ولیدیشن declarative برای اعمال قوانین استفاده کنید و از Tolerant Reader Pattern برای انعطافپذیری بیشتر استفاده کنید
ولیدیشن به زبان ساده به معنی اعتبارسنجی دادههای ورودی سیستم قبل از اجرای عملیات داخل سیستم هست.
🔑 انواع Validation
1. Syntax Validation
سادهترین نوع ولیدیشن سینتکس ولیدیشن بررسی میکنه که آیا ساختار دیتای ورودی صحت داره یا نه. مثلاً:
{
"order_id": 123,
"sku": "ABC-123",
"quantity": 5
}اینجا سینتکس ولیدیشن بررسی میکنه که:
- order_id باید عدد باشد.
- sku باید رشته باشد.
- quantity باید یک عدد غیرمنفی باشد.
✅ نکته مهم اینکه این نوع ولیدیشن باید در لایههای اولیه مثل کنترلرهای سیستم شما انجام بشه تا از ورود دیتای اشتباه به لاجیک جلوگیری بشه.
2. Semantic Validation
ولیدیشن معنایی به مفهوم دیتای مرتبط هست. گاهی اوقات حتی اگر سینتکس دیتا درست باشه، ممکن است هنوز در زمینه تجاری منطقی نباشه.
مثلاً اگر شما لاجیکی برای تخصیص موجودی داشته باشید:
allocate("SKU-001", quantity=10)اینجا سیستم باید بررسی کنه که:
- آیا SKU-001 وجود داره؟
- آیا به اندازه کافی موجودی برای تخصیص ۱۰ واحد وجود داره؟
✅ اینجا حتی اگر سینتکس دیتا درست باشه باید در زمینه کسب و کار هم منطقی باشه.
3. Pragmatic Validation
ولیدیشن کاربردی بررسی میکنه که آیا این اکشن ممکن هست یا مجاز هست با توجه به وضعیت فعلی سیستم. مثلاً:
- اگر انبار تعطیل باشه نباید سفارشی پردازش بشه.
- یا اگر یک کد تخفیف منقضی شده باید اعمال اون روی سفارش ناموفق باشه.
یک مثال دیگر:
شما ممکن است اکشنی مثل:
allocate("SKU-001", quantity=50)داشته باشید. ولیدیشن سینتکس و معنایی درست هستند، اما اگر قانون بیزینس شما بگه "مشتری نمیتونه بیش از ۲۰ محصول در یک بار سفارش بده"، این اکشن در ولیدیشن کاربردی ناموفق میشه.
🧠 Common Validation Patterns
1. Declarative Validation
فرض کنید یک لیست از قوانین دارید مانند:
- order_id باید یک عدد صحیح باشد.
- quantity باید غیرمنفی باشد.
این قوانین را میتوان بهصورت Declarative از قبل تعریف کرد. در پایتون، میتوانید از چیزی مثل pydantic برای تعریف ورودیهای مورد انتظار استفاده کنید.
from pydantic import BaseModel, Field
class Order(BaseModel):
order_id: int
sku: str
quantity: int = Field(ge=0)
2. Postel’s Law (Tolerant Reader Pattern)
این قانون میگه:
Be conservative in what you send, but liberal in what you accept.
یعنی "در آنچه ارسال میکنید محافظهکار باشید، اما در آنچه دریافت میکنید آزاد باشید." یا به عبارت سادهتر، سیستم شما باید در پذیرش فیلدهای اضافی یا ناشناخته انعطافپذیر باشه.
برای مثال، اگر سیستم شما فقط به
order_id و quantity نیاز دارد، اما این را دریافت کند:{
"order_id": 123,
"quantity": 5,
"promo_code": "DISCOUNT20"
}میتونید فیلد اضافی (
promo_code) را نادیده بگیرید به جای اینکه کل درخواست را رد کنید. این کار سیستم شما را در برابر تغییرات انعطافپذیر میکنه.🏁 جمعبندی
- ولیدیشن Syntax بررسی میکنه که آیا قالب دادهها صحیح هست یا نه.
- ولیدیشن Semantic بررسی میکنه که دادهها در زمینه بیزینس شما معنیدار هستند.
- ولیدیشن Pragmatic بررسی میکند که آیا عمل منطقی و با توجه به وضعیت فعلی سیستم امکانپذیر هست.
- از ولیدیشن declarative برای اعمال قوانین استفاده کنید و از Tolerant Reader Pattern برای انعطافپذیری بیشتر استفاده کنید
👍4❤2
📘 Chapter 1: Getting to Know Asyncio
🔄 What is
🖥️ I/O-bound vs. CPU-bound Tasks
- I/O-bound: Tasks that wait for input/output, such as web requests or database queries. These tasks benefit greatly from
- CPU-bound: Tasks that use lots of computational power (like math calculations).
⚙️ Concurrency, Parallelism, and Multitasking
- Concurrency: When multiple tasks appear to be running at the same time by taking turns. For example, while one task is waiting for a file to download, another task can start.
- Parallelism: When tasks are literally running at the same time on multiple CPU cores.
- Multitasking: Managing several tasks at once. This can be preemptive (the OS decides when to switch between tasks) or cooperative (tasks decide when to yield control).
🔄 Processes vs. Threads
- Processes: Independent units that do not share memory. They are good for CPU-bound tasks but use more resources.
- Threads: Lighter-weight than processes and share memory within the same program. However, Python’s Global Interpreter Lock (GIL) prevents threads from running Python code at the same time (no parallel execution). Still, threads are useful for I/O-bound tasks.
🔐 Global Interpreter Lock (GIL)
- GIL is a mechanism in Python that allows only one thread to execute Python bytecode at a time, even on multi-core systems.
- This makes multithreading less useful for CPU-bound operations but still beneficial for I/O-bound tasks because I/O releases the GIL.
⚡ Single-threaded Concurrency
📬 What is a Socket?
A socket is a low-level connection to send and receive data over a network. By default, sockets are blocking, meaning they make the program wait while they communicate. Non-blocking sockets allow the program to continue executing other tasks while waiting for a response. This is key to how
🔄 Event Loop
- An event loop is a core part of how
- Tasks that involve I/O can pause and free up the loop to run other tasks, making programs more efficient.
🕹️ How It Works:
1. Tasks are submitted to the event loop.
2. The loop starts running tasks. If a task hits an I/O operation (like a web request), the task pauses.
3. The operating system watches the socket and informs the event loop when the I/O is complete.
4. The event loop resumes the paused task and continues processing.
🤔 Why Use
- Improves performance for programs that do a lot of waiting (web servers, file reading).
- Lightweight and doesn’t need multiple threads or processes to handle concurrency.
- Efficient resource utilization: While waiting for slow I/O operations, the CPU can continue working on other tasks, leading to faster overall execution.
🚀 Key Takeaways:
-
- It does not remove Python's GIL but makes it less of an issue by focusing on I/O tasks.
- Non-blocking I/O and the event loop allow tasks to pause and resume, making concurrency efficient even with just one thread.
#PythonConcurrencyWithAsyncio
#Chapter_01
#Notes #Book
🔄 What is
asyncio?asyncio is a Python library introduced in version 3.4 that allows you to run I/O-bound tasks concurrently. Rather than making your code wait for slow operations, asyncio allows multiple tasks to run "in parallel" by pausing tasks when they’re waiting for I/O. This allows Python to work on other tasks in the meantime.🖥️ I/O-bound vs. CPU-bound Tasks
- I/O-bound: Tasks that wait for input/output, such as web requests or database queries. These tasks benefit greatly from
asyncio as it allows them to pause and let other tasks run while waiting.- CPU-bound: Tasks that use lots of computational power (like math calculations).
asyncio isn't designed for CPU-heavy tasks.⚙️ Concurrency, Parallelism, and Multitasking
- Concurrency: When multiple tasks appear to be running at the same time by taking turns. For example, while one task is waiting for a file to download, another task can start.
- Parallelism: When tasks are literally running at the same time on multiple CPU cores.
- Multitasking: Managing several tasks at once. This can be preemptive (the OS decides when to switch between tasks) or cooperative (tasks decide when to yield control).
asyncio uses cooperative multitasking, meaning tasks "cooperate" by pausing when they reach I/O.🔄 Processes vs. Threads
- Processes: Independent units that do not share memory. They are good for CPU-bound tasks but use more resources.
- Threads: Lighter-weight than processes and share memory within the same program. However, Python’s Global Interpreter Lock (GIL) prevents threads from running Python code at the same time (no parallel execution). Still, threads are useful for I/O-bound tasks.
🔐 Global Interpreter Lock (GIL)
- GIL is a mechanism in Python that allows only one thread to execute Python bytecode at a time, even on multi-core systems.
- This makes multithreading less useful for CPU-bound operations but still beneficial for I/O-bound tasks because I/O releases the GIL.
⚡ Single-threaded Concurrency
asyncio achieves concurrency without needing multiple threads by using non-blocking I/O and an event loop. 📬 What is a Socket?
A socket is a low-level connection to send and receive data over a network. By default, sockets are blocking, meaning they make the program wait while they communicate. Non-blocking sockets allow the program to continue executing other tasks while waiting for a response. This is key to how
asyncio achieves concurrency with just one thread.🔄 Event Loop
- An event loop is a core part of how
asyncio works. It’s a loop that runs tasks, pausing and resuming them as necessary. The event loop checks if tasks are waiting on I/O and either runs them or pauses them until they’re ready.- Tasks that involve I/O can pause and free up the loop to run other tasks, making programs more efficient.
🕹️ How It Works:
1. Tasks are submitted to the event loop.
2. The loop starts running tasks. If a task hits an I/O operation (like a web request), the task pauses.
3. The operating system watches the socket and informs the event loop when the I/O is complete.
4. The event loop resumes the paused task and continues processing.
🤔 Why Use
asyncio?- Improves performance for programs that do a lot of waiting (web servers, file reading).
- Lightweight and doesn’t need multiple threads or processes to handle concurrency.
- Efficient resource utilization: While waiting for slow I/O operations, the CPU can continue working on other tasks, leading to faster overall execution.
🚀 Key Takeaways:
-
asyncio is ideal for I/O-bound tasks and allows you to write concurrent programs using a single thread.- It does not remove Python's GIL but makes it less of an issue by focusing on I/O tasks.
- Non-blocking I/O and the event loop allow tasks to pause and resume, making concurrency efficient even with just one thread.
#PythonConcurrencyWithAsyncio
#Chapter_01
#Notes #Book
❤4👍1