<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: jairajsahgal</title>
    <description>The latest articles on DEV Community by jairajsahgal (@jairajsahgal).</description>
    <link>https://dev.to/jairajsahgal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F193821%2Faed0d08b-1a78-4e52-87a6-c6212a1b7447.jpeg</url>
      <title>DEV Community: jairajsahgal</title>
      <link>https://dev.to/jairajsahgal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jairajsahgal"/>
    <language>en</language>
    <item>
      <title>Understanding Eventual Consistency</title>
      <dc:creator>jairajsahgal</dc:creator>
      <pubDate>Mon, 01 Jan 2024 19:42:31 +0000</pubDate>
      <link>https://dev.to/jairajsahgal/understanding-eventual-consistency-59lk</link>
      <guid>https://dev.to/jairajsahgal/understanding-eventual-consistency-59lk</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the world of distributed systems, data management and synchronization are critical aspects that ensure the smooth operation of the system. One of the models that has gained popularity in this domain is &lt;strong&gt;Eventual Consistency&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Eventual Consistency?
&lt;/h2&gt;

&lt;p&gt;Eventual consistency is a model that ensures that data will eventually become consistent across all nodes or replicas in the system. It allows for temporary states of inconsistency during the propagation of updates.&lt;/p&gt;

&lt;p&gt;When a change is made to a piece of data, that change is first applied locally. The new version is then propagated to other nodes or replicas in the system over time. However, there could be a period where different nodes or replicas may have different versions of the same data due to network latency or concurrent updates.&lt;/p&gt;

&lt;p&gt;The key concept here is that, as time progresses and all updates are eventually propagated, the system will become consistent across all nodes. This model is also known as “Brewer’s Conjecture” or the CAP theorem. It’s a trade-off between consistency (always reflecting the latest data) and availability (ensuring that data is always accessible), making it suitable for applications where eventual consistency works well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is Eventual Consistency limited to NoSQL databases?
&lt;/h2&gt;

&lt;p&gt;Eventual consistency is not limited to NoSQL databases. The concept can be applied in various types of distributed systems, including both SQL and NoSQL databases, as well as other distributed applications.&lt;/p&gt;

&lt;p&gt;The choice between eventual consistency and strong consistency depends on the specific requirements of an application. Eventual consistency is often chosen when performance is a priority, the system is designed to tolerate temporary inconsistencies, or the application allows eventual consistency because it has built-in mechanisms to handle data conflicts.&lt;/p&gt;

&lt;p&gt;On the other hand, strong consistency is preferred when data accuracy and real-time consistency are essential, the application doesn’t tolerate temporary inconsistencies or data conflicts, or the system is designed to prioritize data integrity over other considerations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Let's consider a simple example of an eventual consistent system using a NoSQL database, like MongoDB. We have a collection of documents representing user profiles. Each document stores the user's&lt;br&gt;
name, email address, and profile picture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "_id": "user1",
    "name": "John Doe",
    "email": "john@example.com",
    "profilePicture": "https://example.com/john.jpg"
}
{
    "_id": "user2",
    "name": "Jane Smith",
    "email": "jane@example.com",
    "profilePicture": "https://example.com/jane.jpg"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose that we have two nodes (let's call them Node A and Node B) in our MongoDB cluster, each storing a replica of this collection. When an update is made on one node, it might not be immediately propagated&lt;br&gt;
to the other node due to network latency or other factors.&lt;/p&gt;

&lt;p&gt;Let's say that we receive an update to change Jane Smith's email address from "jane @ example.com" to "new_email @ example.com". This update is first applied on Node A, but not yet propagated to Node B. If we&lt;br&gt;
were to query the data from both nodes at this moment, we would see different versions of Jane Smith's profile:&lt;/p&gt;

&lt;p&gt;Node A:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "_id": "user2",
    "name": "Jane Smith",
    "email": "new_email@example.com",
    "profilePicture": "https://example.com/jane.jpg"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node B:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "_id": "user2",
    "name": "Jane Smith",
    "email": "jane@example.com",
    "profilePicture": "https://example.com/jane.jpg"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this situation, the system is in a state of temporary inconsistency. If we perform the same query on both Node A and Node B after some time has passed, the data will eventually be consistent:&lt;/p&gt;

&lt;p&gt;Node A (eventually):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "_id": "user2",
    "name": "Jane Smith",
    "email": "new_email@example.com",
    "profilePicture": "https://example.com/jane.jpg"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node B (eventually):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "_id": "user2",
    "name": "Jane Smith",
    "email": "new_email@example.com",
    "profilePicture": "https://example.com/jane.jpg"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Eventual consistency is a powerful model for managing data in distributed systems. It provides a balance between data consistency and system availability, making it a popular choice for many large-scale applications. However, it’s important to understand the trade-offs involved and choose the right consistency model based on the specific requirements of your application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Eventual_consistency"&gt;Wikipedia Article&lt;/a&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>tutorial</category>
      <category>performance</category>
      <category>webdev</category>
    </item>
    <item>
      <title>𝗗𝗶𝗿𝘁𝘆 𝗿𝗲𝗮𝗱, 𝗡𝗼𝗻-𝗿𝗲𝗽𝗲𝗮𝘁𝗮𝗯𝗹𝗲 𝗿𝗲𝗮𝗱, 𝗮𝗻𝗱 𝗣𝗵𝗮𝗻𝘁𝗼𝗺 𝗿𝗲𝗮𝗱 𝘄𝗶𝘁𝗵 Real 𝗹𝗶𝗳𝗲 𝗲𝘅𝗮𝗺𝗽𝗹𝗲𝘀</title>
      <dc:creator>jairajsahgal</dc:creator>
      <pubDate>Sat, 30 Dec 2023 20:10:44 +0000</pubDate>
      <link>https://dev.to/jairajsahgal/-real-15m6</link>
      <guid>https://dev.to/jairajsahgal/-real-15m6</guid>
      <description>&lt;p&gt;In the field of database systems, concurrency control is a crucial aspect to ensure that multiple transactions can execute simultaneously without causing inconsistencies or data corruption. To manage this, databases use various isolation levels and techniques like locking mechanisms to protect data integrity. However, these mechanisms may introduce certain issues known as Dirty Read, Non-repeatable Read, and Phantom Read.&lt;/p&gt;

&lt;p&gt;𝟭. 𝗗𝗶𝗿𝘁𝘆 𝗥𝗲𝗮𝗱:&lt;/p&gt;

&lt;p&gt;A dirty read occurs when a transaction reads uncommitted data from the database. In other words, it reads data that has been modified by another&lt;br&gt;
transaction but not yet committed. This can lead to incorrect results being displayed to the user. To prevent this issue, databases generally use the Two-Phase Locking (2PL) protocol, which ensures that a transaction holds locks only for the duration of the transaction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Example using SQL and Python's psycopg2 library:
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;
&lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_db&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_password&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;transaction1&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;UPDATE table SET column=column+1 WHERE condition&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# commits the changes
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;transaction2&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT * FROM table&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# reads uncommitted data
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;𝟮. 𝗡𝗼𝗻-𝗿𝗲𝗽𝗲𝗮𝘁𝗮𝗯𝗹𝗲 𝗥𝗲𝗮𝗱:&lt;/p&gt;

&lt;p&gt;A non-repeatable read occurs when a transaction re-reads data that has been modified by another concurrent transaction. This issue arises because databases do not guarantee that the data will remain unchanged between two reads of the same transaction, even if no other transactions are modifying it. To address this, 2PL is also used to prevent non-repeatable reads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Example using SQL and Python's psycopg2 library:
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;
&lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_db&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_password&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;transaction1&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT * FROM table WHERE condition&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# reads data
&lt;/span&gt;    &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;UPDATE table SET column=column+1 WHERE condition&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# modifies data
&lt;/span&gt;    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# commits the changes
&lt;/span&gt;    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# reads data again, but results can be different due to non-repeatable read
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;𝟯. 𝗣𝗵𝗮𝗻𝘁𝗼𝗺 𝗥𝗲𝗮𝗱:&lt;/p&gt;

&lt;p&gt;A phantom read occurs when a transaction retrieves multiple rows from a query based on certain conditions, but later, when it re-executes the&lt;br&gt;
same query with the same conditions, additional rows appear that were inserted by other concurrent transactions in the meantime. To address this&lt;br&gt;
issue, databases use multiversion concurrency control (MVCC) and version store, which allow multiple versions of data to coexist until a commit&lt;br&gt;
or rollback is performed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Example using SQL and Python's psycopg2 library:
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;
&lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_db&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your_password&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;transaction1&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT * FROM table WHERE condition&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# reads data
&lt;/span&gt;    &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INSERT INTO table (column) VALUES (&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;some value&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# inserts new row
&lt;/span&gt;    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# commits the changes
&lt;/span&gt;    &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT * FROM table WHERE condition&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# re-reads data, but results can be different due to phantom read
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To avoid these issues, databases use various techniques like 2PL, MVCC, and version store, depending on the isolation level chosen for the transaction. By ensuring data integrity, these mechanisms prevent dirty reads, non-repeatable reads, and phantom reads, allowing transactions to run concurrently without causing inconsistencies or data corruption.&lt;/p&gt;

&lt;p&gt;𝗥𝗲𝗮𝗹 𝗟𝗶𝗳𝗲 𝗘𝘅𝗮𝗺𝗽𝗹𝗲&lt;/p&gt;

&lt;p&gt;Imagine you are withdrawing money from your bank account. You have two options for checking the balance: Option A and Option B.&lt;/p&gt;

&lt;p&gt;Option A: Dirty Read, Non-repeatable Read&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You check the balance of your account, showing $10,000.&lt;/li&gt;
&lt;li&gt;Another transaction transfers $5,000 from your account to another account without committing yet (dirty read).&lt;/li&gt;
&lt;li&gt;You again check the balance of your account and see $5,000.&lt;/li&gt;
&lt;li&gt;The transfer transaction gets rolled back due to an error.&lt;/li&gt;
&lt;li&gt;If you try to re-check the balance using Option A, it will show $10,000, but it should ideally be $5,000 (non-repeatable read).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Option B: Phantom Read&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You check all transactions in your account from last month, showing a total of $10,000.&lt;/li&gt;
&lt;li&gt;Another transaction makes a deposit of $2,000 into your account without committing yet.&lt;/li&gt;
&lt;li&gt;You try to re-check the transactions using Option B, but it will now show an additional $2,000 deposit (phantom read).&lt;/li&gt;
&lt;li&gt;If you try to re-execute the query, it will show both the original transactions and the new $2,000 deposit (phantom read persists).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this example, Option A demonstrates dirty reads and non-repeatable reads, while Option B illustrates a phantom read. Databases employ&lt;br&gt;
isolation levels, locking mechanisms, or multiversion concurrency control to prevent these issues and maintain data integrity during concurrent&lt;br&gt;
transactions.&lt;/p&gt;

&lt;p&gt;Chapter on Concurrency Controls : &lt;a href="https://www.cs.uct.ac.za/mit_notes/database/pdfs/chp13.pdf"&gt;https://www.cs.uct.ac.za/mit_notes/database/pdfs/chp13.pdf&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Increase File Upload Size in Django Backend</title>
      <dc:creator>jairajsahgal</dc:creator>
      <pubDate>Wed, 27 Dec 2023 22:32:14 +0000</pubDate>
      <link>https://dev.to/jairajsahgal/how-to-increase-file-upload-size-in-django-backend-5bcg</link>
      <guid>https://dev.to/jairajsahgal/how-to-increase-file-upload-size-in-django-backend-5bcg</guid>
      <description>&lt;p&gt;Handling file uploads in Django is usually a breeze with the convenient request.data or request.FILES. However, there are situations where you might find yourself needing to up the ante on the maximum allowed file upload size for your Django backend server. Fear not! This guide is here to walk you through the process by tweaking the right knobs in Django's configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Memory Size Settings
&lt;/h2&gt;

&lt;p&gt;In the Django universe, two unsung heroes govern the realm of file uploads: DATA_UPLOAD_MAX_MEMORY_SIZE and FILE_UPLOAD_MAX_MEMORY_SIZE. These settings are like the unsung heroes that ensure optimal memory usage during the file upload dance. Getting to know them is key to preventing hiccups and ensuring silky-smooth file uploads on your Django backend server.&lt;/p&gt;

&lt;h3&gt;
  
  
  DATA_UPLOAD_MAX_MEMORY_SIZE
&lt;/h3&gt;

&lt;p&gt;This setting in Django dictates the maximum size in bytes that a request body can be before sounding the alarms with a SuspiciousOperation. Defaulting to 2.5 MB (2621440 bytes), this setting keeps a watchful eye on incoming data.&lt;/p&gt;

&lt;h3&gt;
  
  
  FILE_UPLOAD_MAX_MEMORY_SIZE
&lt;/h3&gt;

&lt;p&gt;This setting is the maestro that controls the maximum size (in bytes) a file can be before taking it off the memory stage and onto the file system. Imagine a user uploading a file through a Django form—the file starts off in memory. But, if it decides to be a bit too big for its digital britches and crosses the set limit, off it goes to temporary storage (a.k.a. the disk) during the upload performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fine-Tuning the Symphony: DATA_UPLOAD_MAX_MEMORY_SIZE and FILE_UPLOAD_MAX_MEMORY_SIZE
&lt;/h3&gt;

&lt;p&gt;Let's paint a picture to illustrate this harmony. Suppose DATA_UPLOAD_MAX_MEMORY_SIZE is set to 20M, and FILE_UPLOAD_MAX_MEMORY_SIZE is strutting its stuff at 15M. If someone tries to upload a file larger than 25 MB, brace yourself—error incoming. To dodge this digital bullet, simply crank up DATA_UPLOAD_MAX_MEMORY_SIZE to a safer 30M. Voila! No more errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# settings.py
&lt;/span&gt;
&lt;span class="n"&gt;DATA_UPLOAD_MAX_MEMORY_SIZE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;  &lt;span class="c1"&gt;# 30 MB
&lt;/span&gt;&lt;span class="n"&gt;FILE_UPLOAD_MAX_MEMORY_SIZE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;  &lt;span class="c1"&gt;# 15 MB
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when users upload files less than 15 MB, the data gracefully pirouettes in memory. However, if it decides to be a heavyweight contender and crosses the 15 MB threshold, it elegantly glides to the disk, ensuring a perfect balance between memory usage and efficient file handling.&lt;/p&gt;

&lt;p&gt;With these settings in your Django symphony, you can customize the upload experience, making it error-free and seamless for users interacting with your Django backend server. Happy uploading!&lt;/p&gt;

</description>
      <category>django</category>
      <category>programming</category>
      <category>backenddevelopment</category>
      <category>python</category>
    </item>
    <item>
      <title>Core Arguments of Django Rest Framework Serializer</title>
      <dc:creator>jairajsahgal</dc:creator>
      <pubDate>Sun, 17 Dec 2023 19:00:45 +0000</pubDate>
      <link>https://dev.to/jairajsahgal/core-arguments-of-django-rest-framework-serializer-4ak6</link>
      <guid>https://dev.to/jairajsahgal/core-arguments-of-django-rest-framework-serializer-4ak6</guid>
      <description>&lt;p&gt;𝗖𝗼𝗿𝗲 𝗔𝗿𝗴𝘂𝗺𝗲𝗻𝘁𝘀 𝗼𝗳 𝗦𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗲𝗿𝘀 𝗶𝗻 𝗗𝗷𝗮𝗻𝗴𝗼 𝗥𝗲𝘀𝘁 𝗙𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸&lt;/p&gt;

&lt;p&gt;In Django Rest Framework (DRF), serializers are responsible for converting complex data types such as querysets and model instances to Python data types that can then be easily rendered into JSON, XML, or other content types. They also provide deserialization, allowing parsed data to be converted back into complex types after validating incoming data. To achieve this, serializers use various field classes, each with a set of core arguments that control their behavior. &lt;/p&gt;

&lt;p&gt;Below are core arguments and their purpose:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gk6yjdVF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scoynerzen5lffwv7e8k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gk6yjdVF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scoynerzen5lffwv7e8k.png" alt="Image description" width="800" height="677"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;𝗿𝗲𝗮𝗱_𝗼𝗻𝗹𝘆&lt;br&gt;
 This argument determines whether a field is used only for serialization and not for deserialization. When set to True, the field will be included in the output representation but ignored for creating or updating an instance. By default, it is set to False.&lt;/p&gt;

&lt;p&gt;𝘄𝗿𝗶𝘁𝗲_𝗼𝗻𝗹𝘆&lt;br&gt;
 If set to True, the field can be used for creating or updating an instance but won't be included in the serialized output. This is useful for fields that should not be exposed, like passwords. Its default value is False.&lt;/p&gt;

&lt;p&gt;𝗿𝗲𝗾𝘂𝗶𝗿𝗲𝗱&lt;br&gt;
 This argument specifies whether a field must be present in the input data during deserialization. If set to False, the field is not mandatory. Additionally, the attribute or key for this field may be omitted in the serialized output if absent. By default, it is True, but if using ModelSerializer, it can default to False based on the model's field configuration (e.g., blank=True).&lt;/p&gt;

&lt;p&gt;𝗱𝗲𝗳𝗮𝘂𝗹𝘁&lt;br&gt;
This provides a default value for the field if no input is given. It can be a static value, a callable, or a function that is evaluated each time the default is accessed. If a default is provided, the field automatically becomes non-required. Note that for partial updates, the default is not used, and when serializing, it's used only if the object attribute or dictionary key is missing.&lt;/p&gt;

&lt;p&gt;𝗮𝗹𝗹𝗼𝘄_𝗻𝘂𝗹𝗹&lt;br&gt;
 By setting this argument to True, you allow the field to accept None as a valid value. This doesn't imply a default value for deserialization. The default for allow_null is False.&lt;/p&gt;

&lt;p&gt;𝘀𝗼𝘂𝗿𝗰𝗲&lt;br&gt;
 Specifies the attribute used to populate the field. You can use a method (like source='get_absolute_url') or a dotted path to navigate through attributes (source='user . email'). If using dotted notation, it is often prudent to specify a default value to avoid attribute errors during serialization. Special value source='*' means that the entire object should be passed to the field.&lt;/p&gt;

&lt;p&gt;𝘃𝗮𝗹𝗶𝗱𝗮𝘁𝗼𝗿𝘀&lt;br&gt;
A list of validator functions that are called on the field's input. They should raise serializers.ValidationError (or Django's ValidationError) if validation fails.&lt;/p&gt;

&lt;p&gt;𝗲𝗿𝗿𝗼𝗿_𝗺𝗲𝘀𝘀𝗮𝗴𝗲𝘀&lt;br&gt;
 A dictionary mapping error codes to their respective error messages, allowing for custom error messages.&lt;/p&gt;

&lt;p&gt;𝗹𝗮𝗯𝗲𝗹&lt;br&gt;
 A string that serves as a human-readable name for the field, which can be displayed in forms or other interfaces.&lt;/p&gt;

&lt;p&gt;𝗵𝗲𝗹𝗽_𝘁𝗲𝘅𝘁&lt;br&gt;
 A description of the field, typically displayed alongside it in forms or other interfaces.&lt;/p&gt;

&lt;p&gt;𝗶𝗻𝗶𝘁𝗶𝗮𝗹&lt;br&gt;
 A value used for pre-populating the field in forms. It can also be a callable, just like Django's field initial.&lt;/p&gt;

&lt;p&gt;𝘀𝘁𝘆𝗹𝗲&lt;br&gt;
 A dictionary containing rendering options for the field. This can affect how the field is displayed in the browser. Examples include setting input_type for password fields or using a different base template for choice fields.&lt;/p&gt;

&lt;p&gt;These core arguments provide the flexibility needed to define the behavior of serializer fields in DRF, ensuring that API representations are precise, secure, and beneficial for both API providers and consumers.&lt;/p&gt;

&lt;p&gt;Docs: &lt;a href="https://www.django-rest-framework.org/api-guide/fields/#core-arguments"&gt;https://www.django-rest-framework.org/api-guide/fields/#core-arguments&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  technology #careers
&lt;/h1&gt;

&lt;h1&gt;
  
  
  softwareengineering
&lt;/h1&gt;

&lt;h1&gt;
  
  
  programing
&lt;/h1&gt;

&lt;h1&gt;
  
  
  cloudcomputing
&lt;/h1&gt;

&lt;h1&gt;
  
  
  engineering
&lt;/h1&gt;

&lt;h1&gt;
  
  
  coding
&lt;/h1&gt;

&lt;h1&gt;
  
  
  django
&lt;/h1&gt;

&lt;h1&gt;
  
  
  backend
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>Core Arguments of Django Rest Framework Serializer</title>
      <dc:creator>jairajsahgal</dc:creator>
      <pubDate>Sun, 17 Dec 2023 19:00:45 +0000</pubDate>
      <link>https://dev.to/jairajsahgal/core-arguments-of-django-rest-framework-serializer-35l4</link>
      <guid>https://dev.to/jairajsahgal/core-arguments-of-django-rest-framework-serializer-35l4</guid>
      <description>&lt;p&gt;𝗖𝗼𝗿𝗲 𝗔𝗿𝗴𝘂𝗺𝗲𝗻𝘁𝘀 𝗼𝗳 𝗦𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗲𝗿𝘀 𝗶𝗻 𝗗𝗷𝗮𝗻𝗴𝗼 𝗥𝗲𝘀𝘁 𝗙𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸&lt;/p&gt;

&lt;p&gt;In Django Rest Framework (DRF), serializers are responsible for converting complex data types such as querysets and model instances to Python data types that can then be easily rendered into JSON, XML, or other content types. They also provide deserialization, allowing parsed data to be converted back into complex types after validating incoming data. To achieve this, serializers use various field classes, each with a set of core arguments that control their behavior. &lt;/p&gt;

&lt;p&gt;Below are core arguments and their purpose:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gk6yjdVF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scoynerzen5lffwv7e8k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gk6yjdVF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scoynerzen5lffwv7e8k.png" alt="Image description" width="800" height="677"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;𝗿𝗲𝗮𝗱_𝗼𝗻𝗹𝘆&lt;br&gt;
 This argument determines whether a field is used only for serialization and not for deserialization. When set to True, the field will be included in the output representation but ignored for creating or updating an instance. By default, it is set to False.&lt;/p&gt;

&lt;p&gt;𝘄𝗿𝗶𝘁𝗲_𝗼𝗻𝗹𝘆&lt;br&gt;
 If set to True, the field can be used for creating or updating an instance but won't be included in the serialized output. This is useful for fields that should not be exposed, like passwords. Its default value is False.&lt;/p&gt;

&lt;p&gt;𝗿𝗲𝗾𝘂𝗶𝗿𝗲𝗱&lt;br&gt;
 This argument specifies whether a field must be present in the input data during deserialization. If set to False, the field is not mandatory. Additionally, the attribute or key for this field may be omitted in the serialized output if absent. By default, it is True, but if using ModelSerializer, it can default to False based on the model's field configuration (e.g., blank=True).&lt;/p&gt;

&lt;p&gt;𝗱𝗲𝗳𝗮𝘂𝗹𝘁&lt;br&gt;
This provides a default value for the field if no input is given. It can be a static value, a callable, or a function that is evaluated each time the default is accessed. If a default is provided, the field automatically becomes non-required. Note that for partial updates, the default is not used, and when serializing, it's used only if the object attribute or dictionary key is missing.&lt;/p&gt;

&lt;p&gt;𝗮𝗹𝗹𝗼𝘄_𝗻𝘂𝗹𝗹&lt;br&gt;
 By setting this argument to True, you allow the field to accept None as a valid value. This doesn't imply a default value for deserialization. The default for allow_null is False.&lt;/p&gt;

&lt;p&gt;𝘀𝗼𝘂𝗿𝗰𝗲&lt;br&gt;
 Specifies the attribute used to populate the field. You can use a method (like source='get_absolute_url') or a dotted path to navigate through attributes (source='user . email'). If using dotted notation, it is often prudent to specify a default value to avoid attribute errors during serialization. Special value source='*' means that the entire object should be passed to the field.&lt;/p&gt;

&lt;p&gt;𝘃𝗮𝗹𝗶𝗱𝗮𝘁𝗼𝗿𝘀&lt;br&gt;
A list of validator functions that are called on the field's input. They should raise serializers.ValidationError (or Django's ValidationError) if validation fails.&lt;/p&gt;

&lt;p&gt;𝗲𝗿𝗿𝗼𝗿_𝗺𝗲𝘀𝘀𝗮𝗴𝗲𝘀&lt;br&gt;
 A dictionary mapping error codes to their respective error messages, allowing for custom error messages.&lt;/p&gt;

&lt;p&gt;𝗹𝗮𝗯𝗲𝗹&lt;br&gt;
 A string that serves as a human-readable name for the field, which can be displayed in forms or other interfaces.&lt;/p&gt;

&lt;p&gt;𝗵𝗲𝗹𝗽_𝘁𝗲𝘅𝘁&lt;br&gt;
 A description of the field, typically displayed alongside it in forms or other interfaces.&lt;/p&gt;

&lt;p&gt;𝗶𝗻𝗶𝘁𝗶𝗮𝗹&lt;br&gt;
 A value used for pre-populating the field in forms. It can also be a callable, just like Django's field initial.&lt;/p&gt;

&lt;p&gt;𝘀𝘁𝘆𝗹𝗲&lt;br&gt;
 A dictionary containing rendering options for the field. This can affect how the field is displayed in the browser. Examples include setting input_type for password fields or using a different base template for choice fields.&lt;/p&gt;

&lt;p&gt;These core arguments provide the flexibility needed to define the behavior of serializer fields in DRF, ensuring that API representations are precise, secure, and beneficial for both API providers and consumers.&lt;/p&gt;

&lt;p&gt;Docs: &lt;a href="https://www.django-rest-framework.org/api-guide/fields/#core-arguments"&gt;https://www.django-rest-framework.org/api-guide/fields/#core-arguments&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  technology #careers
&lt;/h1&gt;

&lt;h1&gt;
  
  
  softwareengineering
&lt;/h1&gt;

&lt;h1&gt;
  
  
  programing
&lt;/h1&gt;

&lt;h1&gt;
  
  
  cloudcomputing
&lt;/h1&gt;

&lt;h1&gt;
  
  
  engineering
&lt;/h1&gt;

&lt;h1&gt;
  
  
  coding
&lt;/h1&gt;

&lt;h1&gt;
  
  
  django
&lt;/h1&gt;

&lt;h1&gt;
  
  
  backend
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>Creating a file uploader in python</title>
      <dc:creator>jairajsahgal</dc:creator>
      <pubDate>Sat, 03 Apr 2021 10:21:33 +0000</pubDate>
      <link>https://dev.to/jairajsahgal/creating-a-file-uploader-in-python-18e0</link>
      <guid>https://dev.to/jairajsahgal/creating-a-file-uploader-in-python-18e0</guid>
      <description>&lt;h1&gt;
  
  
  About
&lt;/h1&gt;

&lt;p&gt;In this blog you will learn how to create a file uploader that runs from a terminal and asks the user to locate the file using a Graphical User Interface and uploads the file using an API from the &lt;a href="https://file.io/"&gt;File.io&lt;/a&gt;, the uploaded link is then displayed into the terminal and copied into clipboard, it also creates a QR code for the link.&lt;/p&gt;

&lt;h1&gt;
  
  
  Imports
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Requests (Posting file in API)&lt;/li&gt;
&lt;li&gt;Tkinter (For GUI)&lt;/li&gt;
&lt;li&gt;Json (To convert result in a dictionary)&lt;/li&gt;
&lt;li&gt;Pandas ( To copy the link into a clipboard)&lt;/li&gt;
&lt;li&gt;qrcode (To create a QR Code of the link with filename)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import requests
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import json
import pandas as pd
import qrcode
import time
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Program
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Structure
&lt;/h2&gt;

&lt;p&gt;The program is created in Python and frequently uses methods from different libraries. Also the proram only works in Unix only because of the &lt;code&gt;/&lt;/code&gt; and &lt;code&gt;\&lt;/code&gt; difference and I do not want to make distinction of operating system in this simple program. &lt;/p&gt;

&lt;p&gt;We hide the main window in tkinter using &lt;code&gt;Tk().withdraw()&lt;/code&gt; so that user can only use tkinter to select the file and get its path.&lt;/p&gt;

&lt;p&gt;python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Please select your file")
time.sleep(1)
filelocation = askopenfilename()
file_name=filelocation.split("/")[-1]
print(file_name)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Asks the user to select the file and waits for 1 second, then a dialog box opens and the user selects a file, user cannot select a directory. The path is stored in a file location and then the file name is extracted using string and array manipulation which is explained below.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file_name=filelocation.split("/")[-1]&lt;/code&gt;&lt;br&gt;
Remember filelocation is a variable of class type string, hence using &lt;code&gt;split("/")&lt;/code&gt; will split it into an array with elements distributed where &lt;code&gt;/&lt;/code&gt; is encountered. &lt;br&gt;
Example of filelocation string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/home/username/Downloads/Documents/file.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what a file location in unix based operating system will show when asked for a file location. Then [-1] ensures that I get the file name.&lt;/p&gt;

&lt;p&gt;python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;files = {
        'file': (file_name, open(filelocation, 'rb')),
    }

response = requests.post('https://file.io/', files=files)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we create a dictionary that will have a key of 'files' and name of the file we are uploading and an object of open() with file location with parameter of reading it as a binary file.&lt;br&gt;
Using requests library function post we post the file in file.io with the parameter as required by their API, we fill the parameter files with our created dictionary i.e. files in our syntax above.&lt;/p&gt;

&lt;p&gt;python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data=json.loads(response.text)
if data["success"]==True:
    print("One use hyperlink with your file -&amp;gt; ",data["link"]," copied to your clipboard")
    img=qrcode.make(data["link"])
    img.save(file_name.split(".")[0]+".png")
    df=pd.DataFrame([data["link"]])
    df.to_clipboard(index=False,header=False)
    print("Created QR code for link with name: "+file_name.split(".")[0]+".png")

else:
    print("Error: \n Please check your network \n Possible uploading same file too many times\n Else:\n Server problem")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We convert the text from response from the API which is in a json format into a python dictionary.&lt;br&gt;
We check if the file was uploaded successfully with &lt;code&gt;data["success"]==True&lt;/code&gt;&lt;br&gt;
If the file is uploaded successfully, we then print the link into the terminal&lt;br&gt;
python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("One use hyperlink with your file -&amp;gt; ",data["link"]," copied to your clipboard")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we make the QR code, using the following code&lt;br&gt;
python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;img=qrcode.make(data["link"])
img.save(file_name.split(".")[0]+".png")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;qrcode.make(data["link"])&lt;/code&gt; is passing the link string into make method of qrcode which returns an object of image which we stored in &lt;code&gt;img&lt;/code&gt; variable and then using &lt;code&gt;save()&lt;/code&gt; we saved the image into program directory.&lt;/p&gt;

&lt;p&gt;We then copy the link into our clipboard using below code&lt;br&gt;
python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;df=pd.DataFrame([data["link"]])
df.to_clipboard(index=False,header=False)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create a series in pandas of our link string and then convert the series into a dataframe, and then used &lt;code&gt;to_clipboard()&lt;/code&gt; to copy the link into clipboard.&lt;br&gt;
I used pandas library because it is maintained and frequently used by people who handle data.&lt;br&gt;
We tell the user that the QR code of the link has been created and that the link has been copied into computer's clipboard.&lt;/p&gt;

&lt;p&gt;Link to the github repository :&lt;br&gt;
&lt;a href="https://github.com/jairajsahgal/file_uploader"&gt;Project_Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for visiting my article.&lt;/p&gt;

</description>
      <category>python</category>
      <category>tkinter</category>
      <category>api</category>
      <category>terminal</category>
    </item>
  </channel>
</rss>
