<?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: vidhi</title>
    <description>The latest articles on DEV Community by vidhi (@veedhee).</description>
    <link>https://dev.to/veedhee</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%2F188869%2Fe66b65dc-7a7d-4ca0-8876-ccc3d91b6e63.jpg</url>
      <title>DEV Community: vidhi</title>
      <link>https://dev.to/veedhee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/veedhee"/>
    <language>en</language>
    <item>
      <title>HSTS (HTTP Strict Transport Security) - A buddy to HTTPS 🛡️</title>
      <dc:creator>vidhi</dc:creator>
      <pubDate>Sat, 27 Feb 2021 12:51:19 +0000</pubDate>
      <link>https://dev.to/veedhee/hsts-http-strict-transport-security-a-buddy-to-https-576b</link>
      <guid>https://dev.to/veedhee/hsts-http-strict-transport-security-a-buddy-to-https-576b</guid>
      <description>&lt;p&gt;The first time I came upon HSTS or whatever it was supposed to mean was when I was trying to be a developer like everyone else - improving our product's Lighthouse score. We then later concluded that Lighthouse only gives us the basic few requirements and we needed something more muscled up to check for other vulnerabilities along with basic performance. This is also how we ended up updating our third-party CSS and JS libraries but that is not the point we are discussing today. &lt;/p&gt;

&lt;p&gt;We had a little warning that we did not have a specific HSTS header set. Well, I hadn't heard of it before (a point to note here would be that it is because of my lack of exposure to security headers in general). And then I did, again, what any other developer would - Google the hell out of it!&lt;/p&gt;

&lt;p&gt;This was quite some time back, but I'm revisiting this to oil the information on it. I tend to forget stuff if I don't write it down. And also, who knows, it might help someone understand something! That's always a good thing to have.&lt;/p&gt;

&lt;h4&gt;
  
  
  🛡️ It all starts with HTTPS
&lt;/h4&gt;

&lt;p&gt;Alright alright, I know the protagonist of this blog is HSTS, and not HTTPS. But it is impossible to talk about HSTS without tagging its buddy HTTPS along in the conversation. We will not go into the details of &lt;em&gt;how&lt;/em&gt; it works, but instead of &lt;em&gt;why&lt;/em&gt; it matters and its importance in the way the web works. If you're familiar with https, the next section might be your starting point.&lt;/p&gt;

&lt;p&gt;There's &lt;strong&gt;http&lt;/strong&gt; and there's &lt;strong&gt;https&lt;/strong&gt;. Both are the same protocols with the difference of an &lt;strong&gt;'s'&lt;/strong&gt; between them. Literally, and also conceptually. It is a &lt;em&gt;what-you-see-is-what-you-get&lt;/em&gt; kinda thing if you want to be funny about it. The "s" is for &lt;strong&gt;&lt;em&gt;security&lt;/em&gt;&lt;/strong&gt;. HTTPS is a secure version of HTTP. Secure in what sense, you ask?&lt;/p&gt;

&lt;p&gt;While on the internet, we transfer A LOT of data. We keep on sending requests, receiving responses for every information, for every page. And it would be much easier if the only data we were trading were cat pictures (which ultimately we'd want to share with as many people as possible), but it's not. We trade details like login details, bank information, and so much more. These are sensitive, something we don't want to share with people, but something that still has to flow through the wires and be communicated between you and the server. HTTPS is what helps us send this over securely.&lt;/p&gt;

&lt;p&gt;With https, the communication between the client (browser) and the server is encrypted such that the information being sent over the wire cannot be intercepted by someone other than the server for which the information is intended to. If anyone tries to look at the encrypted data, it will seem like a string of random characters. But because of the way encryption works, the server has some additional knowledge that lets it decrypt the information and consume the actual information. This &lt;em&gt;additional knowledge&lt;/em&gt; that the server has needs to be kept secret and private.&lt;/p&gt;

&lt;p&gt;Because of what we discussed before, the use of https becomes especially important when dealing with websites that log you to your account. Since HTTP is stateless (ie, everytime you request a page, it knows nothing about you other than what you send in the current request), the server has to somehow recognize you and determine your identity from the information you send with every request. Which also means that with every request, you're sending some sensitive data over to the server which you do not want anyone else to get hold of. (if someone gets hold of what you send with each request to authenticate/identify yourself, the attacker could also easily use that in their request to pose as you).&lt;/p&gt;

&lt;h4&gt;
  
  
  🛡️ Present Day: HSTS
&lt;/h4&gt;

&lt;p&gt;Let's continue HTTPS' story. We now know how important it is for websites to serve and receive content over https. But what if users access the site over http? &lt;/p&gt;

&lt;p&gt;One way to get around it and enforce that communications take place over https would be to redirect any request on http to https. We can permanently redirect all http requests to https and it would work for us most times. But what could be the problems?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Redirection affects performance. We first request http which redirects us to https, after which our request is processed. Let's say we care more about security than performance, and we don't mind the redirection. Any problems then?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Yes, there still can be problems - security problems. Even if we set up a redirection, the initial request goes over http. And if you're logged in or you're trying to submit a form with sensitive data, it is still available for someone in the middle to intercept or attack it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There is another way to enforce HTTPS, and that is (&lt;em&gt;finally!&lt;/em&gt;) with HSTS. &lt;/p&gt;

&lt;p&gt;HSTS (HTTP Strict Transport Security) is a security-enhancement achieved with the use of a response header (headers that are sent by the server in response to a client's request). &lt;/p&gt;

&lt;p&gt;Suppose you're trying to access a website example.com, you can do it either via http or https. Let's assume it does it over http, so this initial request is not secure. Now, the server redirects to the https endpoint like we discussed previously. &lt;/p&gt;

&lt;p&gt;At this stage, HSTS comes into picture as the response includes the HSTS header. This header instructs the browser to use HTTPS for every connection to the site and its subdomains (if &lt;em&gt;includeSubDomains&lt;/em&gt; specified in the header) from now on. This rule is valid for a period of time which is specified n the response header as well.&lt;/p&gt;

&lt;p&gt;What this means is that after receiving this header, the browser will absolutely not load any resource over http for the particular domain. If the browser is asked to load a resource over HTTP, it will try using HTTPS instead. In case HTTPS fails, the connection must be terminated.&lt;/p&gt;

&lt;p&gt;There only needs to be one request to the domain, and with the help of redirection + HSTS, we can ensure that any more connections to the domain for that browser happen via https.&lt;/p&gt;

&lt;p&gt;Note from the MDN Docs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Strict-Transport-Security header is ignored by the browser when your site is accessed using HTTP; this is because an attacker may intercept HTTP connections and inject the header or remove it. When your site is accessed over HTTPS with no certificate errors, the browser knows your site is HTTPS capable and will honor the Strict-Transport-Security header.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Is there a caveat here as well? Hmm, we'll see. ut first let's have the last piece of HSTS together, - how to set it up, and its components.&lt;/p&gt;

&lt;h4&gt;
  
  
  🛡️ HSTS! Dis-assemble!
&lt;/h4&gt;

&lt;p&gt;The HSTS header can be set in a response like you'd set u any other header. For a more technical how-to, visit your preferred backend tech documentation. The syntax for the header is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight r"&gt;&lt;code&gt;&lt;span class="n"&gt;Strict&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;Transport&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;Security&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;=&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;expiry_time&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;includeSubDomains&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;preload&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above syntax, the directives enclosed in square brackets [] are optional. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;max-age: This is what tells the browser the duration for which it has to remember to exclusively access a domain over https. This is specified in seconds and the maximum value is 2 years.&lt;br&gt;&lt;br&gt;
We usually specify the HSTS header for every response delivered over https. Thus, whenever the header is sent in the response, the browser will update the expiration time for that site, and this duration is refreshed which prevents it from timing out and expiring.&lt;br&gt;&lt;br&gt;
When the specified time elapses, the next attempt to request resource over http will proceed normally without automatically using https. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;includeSubDomains: This is optional. If this parameter is included in the header value, the security enforcement will be applied to all the subdomains of the site.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;preload: This is optional as well. This is an additional layer of protection that helps to tackle the issue that still remains after using the HSTS header. Adding this directive indicates that you want to be preloaded. We will discuss it in detail in the next section.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  🛡️ HSTS Preload List
&lt;/h4&gt;

&lt;p&gt;Let's talk about the caveat that remains. We now know that with the help of redirection and HSTS, the user only has to visit the site over https once before it expires to enforce communications over https. But it leaves open a small window of opportunity for attackers to sniff data and modify it if they want to. &lt;/p&gt;

&lt;p&gt;Suppose you visit a site for the first time using http, and then the website redirects you to the https version and sends you the HSTS header in response. You are safe for any subsequent connections, but that first connection that you had made insecurely, is what might ultimately prove to be a point of failure.&lt;/p&gt;

&lt;p&gt;HSTS preload is &lt;em&gt;not&lt;/em&gt; part of the HSTS standard spec though. The HSTS preload list is a list maintained by the Chromium project which is used by most major browsers.&lt;/p&gt;

&lt;p&gt;The browser has this internal preload list which is checked when making a connection. So suppose you have ever visited a website before but you have the domain added in the preload list, and you try to access the website over http for the initial request - it will not happen. The website then is never accessed via HTTP, and that includes the first connection which was the tiny window for an attack. &lt;/p&gt;

&lt;p&gt;In short, if your domain is added to the HSTS preload list, most browsers will never connect to your domain without https. &lt;/p&gt;

&lt;h4&gt;
  
  
  🛡️ How to add to the HSTS Preload List
&lt;/h4&gt;

&lt;p&gt;There are certain requirements that need to be fulfilled before you can request your domain to be added to the HSTS Preload List.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your site must serve a valid certificate&lt;/li&gt;
&lt;li&gt;If you are listening for HTTP requests, your site should redirect all requests to HTTPS&lt;/li&gt;
&lt;li&gt;All the subdomains must be served over HTTPS.&lt;/li&gt;
&lt;li&gt;For your base domain, serve the HSTS header with: 1) max-age of at least 1 year; 2) includeSubDomains directive; 3) preload directive.&lt;/li&gt;
&lt;li&gt;Make sure the site continues to satisfy the above requirements at all times. Removing the &lt;code&gt;preload&lt;/code&gt; directive will make your site available for the HSTS preload removal.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So after you have made sure that all your subdomains are served over HTTPS with a valid certificate, and that nothing breaks with the addition of HSTS headers (try it out with max-age of a few minutes first), add the &lt;code&gt;preload&lt;/code&gt; directive to the header. &lt;/p&gt;

&lt;p&gt;Then, head over to &lt;a href="https://hstspreload.org"&gt;hstspreload.org&lt;/a&gt; and submit your domain. You can also find more cautions listed on the page and the step for removal if anything goes wrong, which is not a process they recommend. So try to be sure and test HSTS without the preload before you request to add a domain to the preload list.&lt;/p&gt;

&lt;p&gt;This list is not immediately updated by the browser by downloading it over everyday or at intervals. It is a hard-coded list which is distributed with new browser versions, which means that it might take some time for your site to appear/remove in one of the browser's list.&lt;/p&gt;

&lt;h4&gt;
  
  
  🛡️ Tip for tiny rewinds
&lt;/h4&gt;

&lt;p&gt;When testing HSTS with smaller &lt;code&gt;max-age&lt;/code&gt; (and without the preload coming into the picture), you might occasionally want to remove the HTTPS-only restriction of your site. &lt;a href="https://appuals.com/how-to-clear-or-disable-hsts-for-chrome-firefox-and-internet-explorer/"&gt;appuals&lt;/a&gt; has a very helpful section on how to remove a domain from HSTS cache in browser. I will brief it here ut you can find more information for more browsers in the given link. Note that this will &lt;em&gt;not&lt;/em&gt; be effective if the domain is included in the preload list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Visit &lt;a href="https://dev.tochrome://net-internals/#hsts"&gt;chrome://net-internals/#hsts&lt;/a&gt; from your browser. Enter the domain name in the "Delete domain security policies" section and hit Enter. Restart Chrome.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Firefox&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
One way to delete HSTS cache for a site would be to "Forget About this Site" but that would remove a lot more data than just the HSTS restriction. For more methods, visit the above link.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Original post available @ &lt;a href="https://veedhee.com/blog/https-hsts-http-strict-transport-security"&gt;https://veedhee.com/blog/https-hsts-http-strict-transport-security&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>https</category>
      <category>security</category>
      <category>hsts</category>
      <category>preload</category>
    </item>
    <item>
      <title>Random Python Tricks for Random Moods ✨ </title>
      <dc:creator>vidhi</dc:creator>
      <pubDate>Sat, 29 Feb 2020 20:18:13 +0000</pubDate>
      <link>https://dev.to/veedhee/random-python-tricks-for-random-moods-4g82</link>
      <guid>https://dev.to/veedhee/random-python-tricks-for-random-moods-4g82</guid>
      <description>&lt;p&gt;If you have been using Python for quite a duration, you might already be familiar with all, or most of the tricks that we'll discuss below. But I guess it's always best to go over them briskly if it has been days you've used something in particular.&lt;/p&gt;

&lt;p&gt;This will also include snippets, so you can just open your Python Interactive Shell and get started along with the examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  ✨ Let's start with something small and one-liner
&lt;/h2&gt;

&lt;p&gt;One of the major concepts of code is to write as less code as possible and make the code as reusable as possible. Functions is one way, yes - the one example everybody gives when talking about reusability. Let's take it down one notch.&lt;br&gt;
Say you want a list in Python of length 5 with all elements as 1. Writing it as &lt;code&gt;lst = [1, 1, 1, 1, 1]&lt;/code&gt; would be the way to go, right? But isn't it... repeating and we're diverting from the reusability everybody talks about?&lt;br&gt;
Good news, we can cut our efforts here.&lt;br&gt;
We can write it as:&lt;br&gt;
&lt;code&gt;lst = [1]*5&lt;/code&gt;&lt;br&gt;
This is followed for strings too. If you want, for example, to hide a phone number and show only the last 3 digits of the number to the user, you can do something like:&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="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'1234567890'&lt;/span&gt;
&lt;span class="n"&gt;hidden_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"x"&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
&lt;span class="s"&gt;'xxxxxxx890'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✨ Reverse a string/ list in a snap
&lt;/h2&gt;

&lt;p&gt;If you're coming from C/C++, you would probably remember iterating from the last element explicitly and copying it over to a new variable to get the reverse of a string/array. With Python, you can:&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="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✨ Else is not only if's sibling but also for's sibling
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;else&lt;/code&gt; is something all the tutorial covers with the conditionals. It's the same old &lt;em&gt;if this, then this; else this&lt;/em&gt;. But did you know that we can use &lt;code&gt;else&lt;/code&gt; with our other favourite member of the family - &lt;code&gt;for&lt;/code&gt;?&lt;br&gt;
Here's how it goes.&lt;br&gt;
Do you remember times when you would &lt;code&gt;break&lt;/code&gt; out of the loop when you found something, or a condition satisfied, and you'd use a flag right before you broke out o the loop to let the code after the loop know that you hit a &lt;code&gt;break&lt;/code&gt; specifically and it was not the normal end of the loop? The use of &lt;code&gt;for..else&lt;/code&gt; eliminates the use o flags wth a simpler syntax.&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="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"list contains even elements"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"list has no even elements"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="n"&gt;elements&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"list contains even elements"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"list has no even elements"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt; &lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="n"&gt;elements&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✨ Revelio
&lt;/h2&gt;

&lt;p&gt;Suppose you have a Python object, it can be anything - functions/ modules/ dictionaries, and it can be created by you or fetched via an API or third-party code. Often times, we need to know the list of attributes and methods that are available on the object. Instead of looking at the code, we can simply list those out using &lt;code&gt;dir&lt;/code&gt;.&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"example"&lt;/span&gt;
    &lt;span class="n"&gt;ex_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ex_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"example instance"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'__class__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__delattr__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__dict__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__dir__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__doc__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__eq__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__format__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__ge__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__getattribute__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__gt__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__hash__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__init__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__init_subclass__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__le__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__lt__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__module__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__ne__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__new__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__reduce__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__reduce_ex__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__repr__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__setattr__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__sizeof__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__str__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__subclasshook__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'__weakref__'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'ex_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'ex_print'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above list is the list of attributes and methods of the object without the values. Notice the &lt;code&gt;ex_id&lt;/code&gt;, &lt;code&gt;ex_print&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt; in the list - this could come very handy while dealing with objects and to know what methods/attributes are available on them.&lt;/p&gt;

&lt;h2&gt;
  
  
  ✨ Passing attributes to a function for the unknown
&lt;/h2&gt;

&lt;p&gt;Let's take a function where you have a bunch of optional arguments and you need to call the function a couple of times, every time with a different set of arguments.&lt;/p&gt;

&lt;p&gt;If you've worked with Django ORM, you'd know that &lt;code&gt;filter()&lt;/code&gt; accepts a bunch of filter values. For the ones who are new to this:&lt;br&gt;
In Django, databases are made easier with an Object Relation Mapper. This also makes querying the database easier for certain records. Suppose we have a table of users who can be a staff user or not, and belong to a certain department of the organization. Long story short, we want a function that fetches the records based on different filter combinations and calculates their ranking points for the organization's benefit plans.&lt;br&gt;
One way to do this would be to fetch records separately for each filter combination we need and then calculate the ranking points, or the other would be to perform the filtering inside the function. We'll go with the latter option for this article. Let's take a look in more detail:&lt;/p&gt;

&lt;p&gt;For filtering, the syntax goes&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="n"&gt;records&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UserTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_staff&lt;/span&gt;&lt;span class="o"&gt;=&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;=&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="sb"&gt;``&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endraw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="n"&gt;Instead&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;saying&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`department=None`&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endraw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;which&lt;/span&gt; &lt;span class="n"&gt;would&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;when&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;wanting&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="nb"&gt;filter&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;we&lt;/span&gt; &lt;span class="n"&gt;could&lt;/span&gt; &lt;span class="n"&gt;just&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;pass&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="nb"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="s"&gt;'s optional! But we do need only one instruction that would handle all the combinations and cases. Maybe an example would help:
{% raw %}

```python
args1 = {'&lt;/span&gt;&lt;span class="n"&gt;is_staff&lt;/span&gt;&lt;span class="s"&gt;': True}
args2 = {'&lt;/span&gt;&lt;span class="n"&gt;is_staff&lt;/span&gt;&lt;span class="s"&gt;': True, '&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="s"&gt;': '&lt;/span&gt;&lt;span class="n"&gt;Mgmt&lt;/span&gt;&lt;span class="s"&gt;'}
args3 = {'&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="s"&gt;': '&lt;/span&gt;&lt;span class="n"&gt;Ops&lt;/span&gt;&lt;span class="s"&gt;'}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let these be the 3 filters you want to perform but you have a common function where you want to pass the filtering values:&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_ranking&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
   &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UserTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;member&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt;  &lt;span class="n"&gt;members&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# do something
&lt;/span&gt;        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, we do not need to worry about calling &lt;code&gt;get_ranking()&lt;/code&gt; with different sets of arguments. We can:&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="n"&gt;rankings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_ranking&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;rankings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_ranking&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;rankings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_ranking&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what the function actually unpacks to:&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="n"&gt;members&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UserTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UserTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_staff&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# with args1
&lt;/span&gt;&lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UserTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_staff&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Mgmt'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# with args2
&lt;/span&gt;&lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UserTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Ops'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# with args3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;That's all for this post, maybe I will come across some more exciting stuff (which I'm sure Python is full of) and continue this series for any and every mood that you might have. Let me know where and how you've used the tricks or plan to use them, so we can all bathe in the magic of Python.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tricks</category>
    </item>
    <item>
      <title>BREAKING DOWN REGULAR EXPRESSIONS[0] — ONE STEP AT A TIME</title>
      <dc:creator>vidhi</dc:creator>
      <pubDate>Sat, 20 Jul 2019 15:59:40 +0000</pubDate>
      <link>https://dev.to/bagadia/breaking-down-regular-expressions-0-one-step-at-a-time-3m2h</link>
      <guid>https://dev.to/bagadia/breaking-down-regular-expressions-0-one-step-at-a-time-3m2h</guid>
      <description>&lt;h3&gt;
  
  
  BREAKING DOWN REGULAR EXPRESSIONS — ONE STEP AT A TIME
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AetsG-pvhozsmeWDyVrXXkA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AetsG-pvhozsmeWDyVrXXkA.jpeg" alt="regular expression"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For this article, I will be building this all out right from the ground up. You might be an experienced developer reading this article, a college student with some idea of what the name means, or someone who’s not a programmer but want to know what the term &lt;strong&gt;Regular Expression&lt;/strong&gt; is. Anyone and everyone is welcome.&lt;/p&gt;

&lt;p&gt;Before proceeding, I’d like to put something to light and that is that I cannot write regular expressions for a pattern at the top of my head. At times, I forget the most basic syntax of writing a particular element in a &lt;strong&gt;regex&lt;/strong&gt; (the shorter form of regular expression). Which is why, this is more of a documentation for myself than for the public. I learn better when I read something that is written by me than looking up for distributed resources for a topic. And if I am going to write a detailed piece on regular expressions, why not write it like an article.&lt;/p&gt;

&lt;p&gt;Feel free to add suggestions or provide feedback (constructive, please).&lt;/p&gt;

&lt;h3&gt;
  
  
  The Restaurant Scenario
&lt;/h3&gt;

&lt;p&gt;Let us begin with a scenario first. You travel to a new city and you enter a very popular restaurant for lunch. So popular that they have 50 pages of menu. You take the menu in your hand and you just freeze. You have not visited the place before, you don’t know what the place serves, and you’re hungry. &lt;em&gt;Very hungry.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Lucky for you, the staff at the restaurant is super helpful. That is how they became popular in the first place, you think. Instead of going through every item from the menu, which would practically be impossible, you just ask for some basic ingredients you want in your food.&lt;/p&gt;

&lt;p&gt;You call for the waiter and ask them to bring you something that has both cheese and peas in it. Now because (a) the staff is super friendly — they accept your request which is not exactly an order (b) the staff is super talented and know their menu really well — so they return to you with a list of dishes that has both cheese and peas in it.&lt;/p&gt;

&lt;p&gt;Great! This list is relatively smaller and you can now go through the menu and decide what you want and place the order.&lt;/p&gt;

&lt;p&gt;If you understand the above scenario, you now have a basic idea of what regular expressions are. Let me draw the analogy.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Parallel
&lt;/h3&gt;

&lt;p&gt;When you first asked (&lt;em&gt;queried&lt;/em&gt;) the waiter to bring you items that had cheese and peas, you gave them a &lt;em&gt;combination&lt;/em&gt; of ingredients or a &lt;em&gt;pattern&lt;/em&gt; you wanted (you could’ve asked for food that was barbecued or roasted, and they’d still come up with a list for you). The waiter now had &lt;strong&gt;two&lt;/strong&gt; inputs. They had the very large menu they have been dealing with for years, and a combination you wanted.&lt;/p&gt;

&lt;p&gt;The waiter &lt;em&gt;matched&lt;/em&gt; your combination to find the menu you wanted.&lt;/p&gt;

&lt;p&gt;In the world of software, this isn’t so different. Here, you give a &lt;strong&gt;sequence&lt;/strong&gt; or a &lt;strong&gt;pattern&lt;/strong&gt; of characters to a system and ask them to return all the *&lt;em&gt;matching *&lt;/em&gt; results.&lt;/p&gt;

&lt;p&gt;Let’s connect this to the example above. Suppose you are being served by a new waiter. Let’s call her Lucy. Lucy doesn’t have the entire menu memorized yet and is struggling to get you the complete list. She has other tables to attend and also cannot sit and match it for you physically. Her colleagues are busy attending tables too.&lt;/p&gt;

&lt;p&gt;But she’s smart with computers.&lt;/p&gt;

&lt;p&gt;Because she works for a rich restaurant, they have their menu digitally on a computer. She writes a quick regular expression to match all the dishes that contain both cheese and peas in their name, and voila, she now has the list ready for you!&lt;/p&gt;

&lt;p&gt;And this is a real life use case of regular expressions. You take the &lt;em&gt;combination/ pattern/ sequence of characters&lt;/em&gt; &lt;strong&gt;(“cheese”, “peas”)&lt;/strong&gt;, and you match it with the entire menu &lt;strong&gt;(“peas dipped in cheese”, “cheese honey &amp;amp; peas special”, etc)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now we’ll extend the above example for another real life use case, just so we can be sure that we understand what’s going on. Lucy is going through the menu and being new to the crew, she suddenly notices something. While noting down &lt;strong&gt;“shredded cheese and peas with creem”&lt;/strong&gt; , she notices that &lt;em&gt;cream&lt;/em&gt; has been spelled wrong. Because she now has the power of regular expression, she enters a search for &lt;em&gt;“cream”&lt;/em&gt; — the actual spelling, and finds no result. But she does remember learning a few dishes with cream in them. Where are they?&lt;/p&gt;

&lt;p&gt;She then does a search for the dishes which have “&lt;em&gt;creem”&lt;/em&gt; in them and there they are. All the dishes have &lt;em&gt;cream&lt;/em&gt; spelled out the wrong way! Again, she’s good with computers. After completing her orders, she finds all the dishes with the misspelled word &lt;em&gt;“creem”&lt;/em&gt; and replaces those with the correct &lt;em&gt;“cream”&lt;/em&gt;. She later notifies the management about it and there’s a happy ending with bonuses which she later uses to buy herself a whipped cream machine.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Definition
&lt;/h3&gt;

&lt;p&gt;I love the resources of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions" rel="noopener noreferrer"&gt;&lt;strong&gt;MDN&lt;/strong&gt;&lt;/a&gt;, so let’s take a look on how they define regular expressions and see if we can understand the technical definition now that we know what it is and how it can be used.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Regular expressions are patterns used to match character combinations in strings.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And, that’s it. You now know what regular expressions are used for.&lt;/p&gt;

&lt;p&gt;Wait a minute, stop scrolling, and lift your finger off the screen or mouse. Now take a moment to think of some real life incidents where you could’ve used regular expressions. Make it as absurd as possible — finding all the blue-colored jackets in a mall, getting carpets with either a black or blue border with flowers in the center.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Where &amp;amp; Why Part of It
&lt;/h3&gt;

&lt;p&gt;Now that you’ve given it a thought, we’ll end this part by taking a look at some very concrete scenarios where regular expressions are used extensively. This will help us get a more technical perspective towards this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;While filling out online forms have you ever come across constraints that go: “&lt;em&gt;please input digits only&lt;/em&gt;”, I mean that’s a bummer, right? Well no, they are important and are achieved using regular expressions. The system checks if the value entered matches with a predefined string that goes from zero to nine [0–9]. We will check examples like these later in the series.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Did you forget where you have placed a file on your computer and the file was important? But you don’t usually panic, do you? You simply click the search option and type the few letters you are confident the file name contains. The system takes the input (&lt;em&gt;the cheese and peas&lt;/em&gt;) and tries to find the relating items in your file storage system (&lt;em&gt;the menu&lt;/em&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is also useful in web scraping — when you want to get some information from a web page using code. You can enter what pattern you are looking for — for instance, a heading which contains the word “NEWS” in it. A very very basic use of regular expression in web scraping can be found &lt;a href="https://github.com/vidhibagadia/Packt-Tracer" rel="noopener noreferrer"&gt;here&lt;/a&gt; for a small and personal project.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This article starts everything from the very beginning and will probably be not useful to people with some experience. The next article in the sequence will be dropping soon, and it will focus on turning your patterns and text to actual code we can use. So, stick around for that if you’d like to.&lt;/p&gt;

&lt;p&gt;To the ones starting out, just a note of caution, I’ve witnessed senior developers struggling with regular expressions as well. And it is &lt;em&gt;completely&lt;/em&gt; fine to not grasp it in your first, second, third or even the seventieth attempt. The more and more you (we) use it in projects, the friendlier the concept becomes and honestly that should be the end goal. Not to master it, but to befriend it.&lt;/p&gt;

&lt;p&gt;I forget the regex syntax almost for every other project that I use, but I know what to do and just have to verify and modify if the syntax works properly. I’ve a long way to go too, but it has been okay for any project that I’ve wanted to include regular expressions in.&lt;/p&gt;

&lt;p&gt;You’ll be fine too.&lt;/p&gt;

&lt;p&gt;— — — — — — — — —&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Please leave a comment if this could help you in your journey of understanding regular expressions. If you’d like to chat on topics, you can find me&lt;/em&gt; &lt;a href="https://twitter.com/vidhibagadia" rel="noopener noreferrer"&gt;&lt;em&gt;here&lt;/em&gt;&lt;/a&gt; at twitter, or you can check out my other links listed at_ &lt;a href="https://vidhibagadia.me" rel="noopener noreferrer"&gt;&lt;em&gt;vidhibagadia.me&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>regex</category>
      <category>programming</category>
      <category>regularexpressions</category>
    </item>
  </channel>
</rss>
