<?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: Ravi Kumar Gupta</title>
    <description>The latest articles on DEV Community by Ravi Kumar Gupta (@kravigupta).</description>
    <link>https://dev.to/kravigupta</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%2F673498%2F30e2ee4d-7d1e-4a1f-a7a7-24ad1ca4bbd0.png</url>
      <title>DEV Community: Ravi Kumar Gupta</title>
      <link>https://dev.to/kravigupta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kravigupta"/>
    <language>en</language>
    <item>
      <title>Encode Decode JWT</title>
      <dc:creator>Ravi Kumar Gupta</dc:creator>
      <pubDate>Sun, 25 Jul 2021 15:58:27 +0000</pubDate>
      <link>https://dev.to/kravigupta/encode-decode-jwt-4ck5</link>
      <guid>https://dev.to/kravigupta/encode-decode-jwt-4ck5</guid>
      <description>&lt;p&gt;JWT stands for JSON Web Tokens.&lt;/p&gt;

&lt;p&gt;A simple function to encode the content -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'''
Encode the given text with given secret key. The default number of seconds for token validity is 600 seconds.
'''
def encode_token(text, secret_key, validity_seconds = 600):
    import datetime, jwt
    try:
        payload = {
            'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=validity_seconds),
            'iat': datetime.datetime.utcnow(),
            'secret': text
        }
        return jwt.encode(
            payload,
            secret_key,
            algorithm='HS256'
        )
    except Exception as e:
        return e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to decode -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'''
Decode the encoded token with given secret_key
'''
def decode_token(auth_token, secret_key):
    import jwt
    try:
        payload = jwt.decode(auth_token, secret_key, algorithms='HS256')
        return {'auth': True, 'error': '', 'decoded': payload}
    except jwt.ExpiredSignatureError:
        return {'auth': False, 'error': 'Token expired'}
    except jwt.InvalidTokenError:
        return {'auth': False, 'error': 'Invalid token'}
    return {'auth': False, 'error': 'Some error'}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's get to work - &lt;/p&gt;

&lt;h3&gt;
  
  
  Define a secret
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;secret = 'This-is-my-super-secret'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Encode the content
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;encoded_data = encode_token('Something to encode', secret)
print(encoded_data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This outputs as -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjcyMjY4NDUsImlhdCI6MTYyNzIyNjI0NSwic2VjcmV0IjoiU29tZXRoaW5nIHRvIGVuY29kZSJ9.CombVr-757PXau8yeXtyjCLn54E3pGNntlnpoADnPRI'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If You copy this to &lt;a href="https://jwt.io"&gt;https://jwt.io&lt;/a&gt; you will see - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dYpOQDQB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acvmnb47lwyvb3v4ckqm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dYpOQDQB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acvmnb47lwyvb3v4ckqm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Decode the token
&lt;/h3&gt;

&lt;p&gt;To decode the data you need the same secret&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;decoded_data = decode_token(encoded_data, secret)
print(decoded_data['decoded']['secret'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This outputs to -&lt;br&gt;
&lt;code&gt;'Something to encode'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you try to decode using some other secret key, the data won't be decoded correctly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;decoded_data = decode_token(encoded_data, 'some-other-secret')
print(decoded_data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This output as -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{'auth': False, 'error': 'Invalid token'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope these simple functions help you :)&lt;/p&gt;

&lt;p&gt;You can follow me on Twitter — &lt;a class="mentioned-user" href="https://dev.to/kravigupta"&gt;@kravigupta&lt;/a&gt;
 . You can also connect on LinkedIn — kravigupta.&lt;/p&gt;

</description>
      <category>python</category>
      <category>jwt</category>
      <category>encode</category>
      <category>decode</category>
    </item>
    <item>
      <title>Setting up Python Development Environment on Apple M1</title>
      <dc:creator>Ravi Kumar Gupta</dc:creator>
      <pubDate>Sun, 25 Jul 2021 05:57:07 +0000</pubDate>
      <link>https://dev.to/kravigupta/setting-up-python-development-environment-on-apple-m1-4o4l</link>
      <guid>https://dev.to/kravigupta/setting-up-python-development-environment-on-apple-m1-4o4l</guid>
      <description>&lt;p&gt;I recently moved to Apple M1. It was an awesome experience for my NodeJs, Java projects.&lt;/p&gt;

&lt;p&gt;But a poor experience for python projects. I could not even install numpy properly.&lt;/p&gt;

&lt;p&gt;I did not want to use Rosetta much. So, I had to install numpy using source. But then pandas did not install, then scipy has problems… Tired.&lt;/p&gt;

&lt;p&gt;If you’ve reached here, you would have got a lot of articles where people tried to install by source or other ways. In this story, I am jumping straight to the solution which worked for me using miniforge&lt;/p&gt;

&lt;p&gt;Install miniforge&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install miniforge
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setup conda with your shell(zsh or bash, whatever you use)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;conda init zsh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create your venv using desired python version&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;conda create -n .venv python=3.9.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a virtual env for python 3.9.2. All the packages you install will go inside this environment.&lt;/p&gt;

&lt;p&gt;Now you can just go your python code directory and activate virtual env.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /path/to/my/python/source/dir
conda activate .venv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the packages. Few packages I was not able to find in conda so if I install using pip that worked fine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;conda install numpy
conda install pandas
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or using Pip&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install celery
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it, now you can simply run your app.&lt;/p&gt;

&lt;p&gt;Hope this helps you. :)&lt;/p&gt;

&lt;p&gt;You can follow me on Twitter — &lt;a class="mentioned-user" href="https://dev.to/kravigupta"&gt;@kravigupta&lt;/a&gt;
. You can also connect on LinkedIn — kravigupta.&lt;/p&gt;

&lt;p&gt;Cheers!!&lt;/p&gt;

</description>
      <category>python</category>
      <category>applem1</category>
      <category>conda</category>
    </item>
    <item>
      <title>MongoDB Start with no-indexing</title>
      <dc:creator>Ravi Kumar Gupta</dc:creator>
      <pubDate>Sun, 25 Jul 2021 05:53:44 +0000</pubDate>
      <link>https://dev.to/kravigupta/mongodb-start-with-no-indexing-25i1</link>
      <guid>https://dev.to/kravigupta/mongodb-start-with-no-indexing-25i1</guid>
      <description>&lt;p&gt;Using MongoDB?&lt;/p&gt;

&lt;p&gt;Ever faced this problem when your app is collecting huge data every day that any moment it may completely fill your storage and leave you with zero bytes.&lt;/p&gt;

&lt;p&gt;After that, you can’t even login to the machine to fix the problem.&lt;/p&gt;

&lt;p&gt;In your app, you can keep monitoring the free space and delete the old MongoDB data. But.. what if -&lt;/p&gt;

&lt;p&gt;You have free space left, and MongoDB is re-building or building indexes and before your app checks again, the free space goes to zero?&lt;/p&gt;

&lt;p&gt;But, you’re lucky to observe this before it happens. You go to MongoDB prompt and try to delete the data, but alas, MongoDB is building the indexes on the same collection you want to delete data from.&lt;/p&gt;

&lt;p&gt;You won’t be able to do that.&lt;/p&gt;

&lt;p&gt;You restart MongoDB hoping it would stop indexing and you would be able to delete.&lt;/p&gt;

&lt;p&gt;That, doesn’t work either.&lt;/p&gt;

&lt;p&gt;Okay, so you need to do this — start MongoDB with &lt;code&gt;—-noIndexBuildRetry&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;Edit the &lt;code&gt;/etc/init.d/mongod&lt;/code&gt; file and replace the line —&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- OPTIONS=" -f $CONFIGFILE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- OPTIONS=" -f $CONFIGFILE --noIndexBuildRetry"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart MongoDB after the update. MongoDB will not attempt to fix or rebuild indexes.&lt;/p&gt;

&lt;p&gt;Delete what you want from MongoDB prompt. Ensure to keep safe free space considering the indexes size.&lt;/p&gt;

&lt;p&gt;Edit the &lt;code&gt;/etc/init.d/mongod&lt;/code&gt; file again to remove the &lt;code&gt;--noIndexBuildRetry&lt;/code&gt; and save.&lt;/p&gt;

&lt;p&gt;Restart MongoDB.&lt;/p&gt;

&lt;p&gt;Did it save your time? Let me know in the comments.&lt;/p&gt;

&lt;p&gt;You can follow me on Twitter — &lt;a class="mentioned-user" href="https://dev.to/kravigupta"&gt;@kravigupta&lt;/a&gt;
. You can also connect on LinkedIn — kravigupta.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>database</category>
    </item>
  </channel>
</rss>
