<?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: RedouaneSarouf</title>
    <description>The latest articles on DEV Community by RedouaneSarouf (@red2019cubic).</description>
    <link>https://dev.to/red2019cubic</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%2F1082326%2F7bbe9db6-42a0-4c90-ab75-eccff585b5cc.jpg</url>
      <title>DEV Community: RedouaneSarouf</title>
      <link>https://dev.to/red2019cubic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/red2019cubic"/>
    <language>en</language>
    <item>
      <title>Python Magic Methods</title>
      <dc:creator>RedouaneSarouf</dc:creator>
      <pubDate>Mon, 09 Oct 2023 00:08:27 +0000</pubDate>
      <link>https://dev.to/red2019cubic/python-magic-methods-5jj</link>
      <guid>https://dev.to/red2019cubic/python-magic-methods-5jj</guid>
      <description>&lt;p&gt;In this post i will be demonstrating the use of some of python magic magic methods also known as "dunder" methods and how to implement them in your code. &lt;br&gt;
Magic methods are special methods in Python that start and end with a double underscore, such as &lt;strong&gt;init&lt;/strong&gt;, &lt;strong&gt;str&lt;/strong&gt;, &lt;strong&gt;add&lt;/strong&gt;, and many more. These methods have specific names and purposes, and they enable you to define how instances of your custom classes interact with Python's built-in functions and operators.&lt;br&gt;
Lets explore some of these magic methods.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;1. The Constructure method &lt;strong&gt;init&lt;/strong&gt;: *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It is used to initialize objects attributes.&lt;br&gt;
coding example:&lt;/p&gt;

&lt;p&gt;class MyClass:&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self, value):&lt;br&gt;
        self.value = value&lt;/p&gt;

&lt;p&gt;obj = MyClass(42)`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.The string method &lt;strong&gt;str&lt;/strong&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;the string method define string representation of  an object.&lt;/p&gt;

&lt;p&gt;class MyClass:&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self, value):&lt;br&gt;
        self.value = value&lt;br&gt;
def &lt;strong&gt;str&lt;/strong&gt;:&lt;br&gt;
   return f'{self.value}"&lt;br&gt;
obj = MyClass(42)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Length method &lt;strong&gt;len&lt;/strong&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method allows you to get the length of an object of your class making them compatible function like len().&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
class MyList:&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self, items):&lt;br&gt;
        self.items = items&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def __len__(self):
    return len(self.items)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;my_list = MyList([1, 2, 3, 4, 5])&lt;br&gt;
print(len(my_list))  # Output: 5&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;4. Equal method &lt;strong&gt;eq&lt;/strong&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method allows to customize equalities for objects comparison.&lt;/p&gt;

&lt;p&gt;`class MyClass:&lt;br&gt;
        def &lt;strong&gt;init&lt;/strong&gt;(self, x, y):&lt;br&gt;
        self.x = x&lt;br&gt;
        self.y = y&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def __eq__(self, other):
    if isinstance(other, MyClass):
        return self.x == other.x and self.y == other.y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;c1 = MyClass(5, 6)&lt;br&gt;
c2 = MyClass(2, 3)&lt;br&gt;
c3 = MyClass(5, 6)&lt;br&gt;
print(c1==c2)  # False&lt;br&gt;
print(c1==c3)  # True`&lt;/p&gt;

&lt;p&gt;In Conclusion Python magic methods are the very powerful tool you can use to customize your classes and making them more flexible. They enable you to define how objects behave in various contexts, from initialization to string representation, arithmetic operations, and more. Magic methods empower you to write clean, readable, and powerful Python code, elevating your programming skills to the next level.&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Python Bitwise Operators Explained in Detail</title>
      <dc:creator>RedouaneSarouf</dc:creator>
      <pubDate>Mon, 28 Aug 2023 03:05:26 +0000</pubDate>
      <link>https://dev.to/red2019cubic/python-bitwise-explained-in-detail-25o2</link>
      <guid>https://dev.to/red2019cubic/python-bitwise-explained-in-detail-25o2</guid>
      <description>&lt;p&gt;This post explains python bitwise in detail with easy to understand code example. Python provides several bitwise operators that allow you to work directly with the binary representations of integers. &lt;br&gt;
We'll explore the world of Python bitwise operators and their functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Bitwise Operators with code example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bitwise operators work on individual bits of integers, treating them as binary values (0s and 1s). Python provides the following bitwise operators:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Bitwise AND (&amp;amp;): &lt;br&gt;
The &amp;amp; means both bits have to be on&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int1 = 10     # 00001010 in binary
                  # 00008020
    int2 = 6      # 00000110 in binary
                  # 00000420

    # int1        = 00001010 = 10
    # int2        = 00000110 = 6
    # int1 &amp;amp; int2 = 00000010 = 2

    print(10 &amp;amp; 6) # Output: 2 (00000010 in binary)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Bitwise OR (|): &lt;br&gt;
The (|) means at least 1 bit has to be on or both.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    8bits(256 128 64 32 16 8 4 2 1)
    int1 = 10     # 00001010 in binary
                  # 00008020
    int2 = 6      # 00000110 in binary
                  # 00000420

    # int1        = 00000100 = 4
    # int2        = 00001000 = 8
    # int1 | int2 = 00001100 = 12

    print(4 &amp;amp; 8) # Output: 12 (00001100 in binary)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Bitwise XOR (^): &lt;br&gt;
The ^ means both bits have to be different either (1 0) or (0 1)&lt;br&gt;
8bits(256 128 64 32 16 8 4 2 1)&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int1 = 10     # 00001010 in binary
                  # 00008020
    int2 = 6      # 00000110 in binary
                  # 00000420
    # int1        = 00000100 = 4
    # int2        = 00001000 = 8
    # int1 ^ int2 = 00001100 = 12

    print(4 &amp;amp; 8) # Output: 12 (00001100 in binary)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Bitwise NOT (~): Flips the bits of an integer. It changes all 0s to 1s and all 1s to 0s.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    n = ~10  #  0000 1010
              #  1111 0101        
    # the most significant bit is reserved for sign bit 
    # in this case the result will be negative 
    print(n)  # Output: -11 (Negative of (1010 + 1))
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Bitwise Left Shift (&amp;lt;&amp;lt;): Shifts the bits of the left operand to the left by a specified number of positions. This is equivalent to multiplying the number by 2 raised to the power of the shift count.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    5 &amp;lt;&amp;lt; 2   
    # binary representation for 5 is 0000 0101
    # number 2 means shifting by 2 bits
    # &amp;lt;&amp;lt; 2 means we shift 2 bits to the left 
    # 0001 0100
    print(5 &amp;gt;&amp;gt; 2)  # Output: 2 (0001 0100 in binary)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Bitwise Right Shift (&amp;gt;&amp;gt;): Shifts the bits of the left operand to the right by a specified number of positions. This is equivalent to dividing the number by 2 raised to the power of the shift count, while discarding the remainder.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    5 &amp;gt;&amp;gt; 2   
    # binary representation for 5 is 0000 0101
    # number 2 means shifting by 2 bits
    # &amp;gt;&amp;gt; 2 means we shift 2 bits to the right 
    # 0000 0001
    print(5 &amp;gt;&amp;gt; 2)  # Output: 2 (0000 0001 in binary)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>React CORS</title>
      <dc:creator>RedouaneSarouf</dc:creator>
      <pubDate>Sun, 02 Jul 2023 20:05:58 +0000</pubDate>
      <link>https://dev.to/red2019cubic/react-cors-23nl</link>
      <guid>https://dev.to/red2019cubic/react-cors-23nl</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;Introduction:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Cross-Origin Resource Sharing (CORS) is an essential security mechanism that allows web applications to access resources from different domains. As a React developer, it is crucial to have a solid understanding of CORS and how it can affect your application's functionality. In this blog post, we will explore what CORS is, how it works, and how to handle CORS-related issues in your React application.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;What is CORS?&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
CORS is a browser-based mechanism that enables controlled access to resources located on a different domain. It is implemented as a set of HTTP headers exchanged between the client (browser) and the server. These headers determine whether a request from a particular origin (domain) should be allowed or denied access to server resources.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;How CORS Works:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
When a client makes a cross-origin request, the browser sends an HTTP OPTIONS preflight request to the server, asking for permission to make the actual request. The server responds with the appropriate CORS headers, indicating whether the request is allowed. If the server approves the request, the browser proceeds with the actual request. Otherwise, it blocks the request, preventing the client from accessing the server resources.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Handling CORS in React:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;em&gt;&lt;strong&gt;1. Enabling CORS on the Server:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
To allow cross-origin requests, the server must be configured to send the appropriate CORS headers. This can be done by adding specific response headers, such as Access-Control-Allow-Origin, Access-Control-Allow-Methods, and Access-Control-Allow-Headers, to the server's HTTP responses. Consult your server's documentation to find out how to enable CORS.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;2. CORS in Development Environment:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
During development, React applications typically run on a different port than the server's API. To avoid CORS issues, you can utilize a proxy server or a package like &lt;code&gt;http-proxy-middleware&lt;/code&gt; to proxy API requests from the client-side to the server-side. This way, the requests appear to be originating from the same origin, bypassing CORS restrictions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;3. Using Fetch or Axios:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
When making API requests in React, it is common to use libraries like Fetch or Axios. These libraries automatically handle CORS-related headers and options for you, making it easier to interact with APIs that are on different domains. However, it is still important to ensure that the server's CORS configuration allows the specific request methods and headers required by your application.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;4 CORS Error Handling:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
If your React application encounters a CORS error, it is crucial to handle it gracefully. You can display an error message to the user or redirect them to a fallback page. Additionally, you can implement error logging to capture CORS-related issues and track them for future troubleshooting.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Understanding CORS is essential for React developers, as it allows for secure communication between web applications and different domains. By properly configuring the server, handling CORS in the development environment, and utilizing appropriate libraries, you can ensure that your React application works seamlessly with cross-origin resources. Remember to handle CORS errors gracefully to provide a better user experience.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JavaScript var keyword usage</title>
      <dc:creator>RedouaneSarouf</dc:creator>
      <pubDate>Sun, 14 May 2023 19:18:11 +0000</pubDate>
      <link>https://dev.to/red2019cubic/javascript-var-keyword-usage-4mjh</link>
      <guid>https://dev.to/red2019cubic/javascript-var-keyword-usage-4mjh</guid>
      <description>&lt;p&gt;&lt;em&gt;JavaScript var keyword:&lt;/em&gt; &lt;strong&gt;Usage and Best Practices&lt;/strong&gt;&lt;br&gt;
In JavaScript, variables are used to store data values. The 'var' keyword is used declare a variables of any type without initializing them and it stands for variable.&lt;br&gt;
here is an example of declaring a variable using 'var':&lt;br&gt;
var [variable name];&lt;br&gt;
In this blog post we will discuss the usage and how it differs from other type of variable declaration.&lt;br&gt;
Difference between var, let and const. let is used in situation where you may want to reassign the variable again. &lt;/p&gt;

&lt;p&gt;let dog = "Bella";&lt;br&gt;
dog = "Tracy" //reassigning the dog value&lt;/p&gt;

&lt;p&gt;const is used to declare variables that can not be changed once declared similar to final keyword in java language. &lt;/p&gt;

&lt;p&gt;const cat = "Brent";&lt;br&gt;
cat = "jay"// error;&lt;/p&gt;

&lt;p&gt;Both keywords let and const can not escape block scope. On the other hand, 'var' can escape block scope but can not escape function block.&lt;br&gt;
here is an example of var escaping:&lt;/p&gt;

&lt;p&gt;for(var i = 0; i &amp;lt; 4; i++){&lt;br&gt;
    console.log(i);// 0 1 2 3&lt;br&gt;
}&lt;br&gt;
console.log(i) // the value of i is 4;&lt;/p&gt;

&lt;p&gt;Best practices is to avoid creating global variables and use let and const instead of var. var can cause issues with naming conflict and make your code difficult to read.  &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
