<?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: Hamdy Abou El Anein</title>
    <description>The latest articles on DEV Community by Hamdy Abou El Anein (@hamdyaea).</description>
    <link>https://dev.to/hamdyaea</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%2F315649%2Fa70dee30-8974-46ea-86e5-6b6d6ea47a03.jpeg</url>
      <title>DEV Community: Hamdy Abou El Anein</title>
      <link>https://dev.to/hamdyaea</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hamdyaea"/>
    <language>en</language>
    <item>
      <title>Mastering Bash arguments with getopts</title>
      <dc:creator>Hamdy Abou El Anein</dc:creator>
      <pubDate>Sun, 06 Mar 2022 10:48:34 +0000</pubDate>
      <link>https://dev.to/hamdyaea/mastering-bash-arguments-with-getopts-iac</link>
      <guid>https://dev.to/hamdyaea/mastering-bash-arguments-with-getopts-iac</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;In this article, I show you how to mastering the Bash arguments with getopts to have a software who run with professional arguments like &lt;/p&gt;

&lt;pre&gt;
mysoft.bsh -f [arg1] -g [arg2]
&lt;/pre&gt;

&lt;p&gt;I also show you how to add a default value if the argument is not given.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's an example of code :&lt;/p&gt;

&lt;pre&gt;
f=10  
g=5  
while getopts ":f:g:" option; do  
    case "${option}" in  
        f)  
            f=${OPTARG}  
            ((f == 15 || f == 75)) || usage  
            ;;  
        g)  
            g=${OPTARG}  
            ;;  
        *)  
            usage
            ;;
    esac  
done  
shift $((OPTIND-1))  
if [ -z "${v}" ] || [ -z "${g}" ]; then  
    echo "info"  
fi  
echo "f = ${f}"  
echo "g = ${g}"
&lt;/pre&gt;

&lt;p&gt;Now we can look at the code more closely :&lt;/p&gt;

&lt;p&gt;Here we declare a default value to the variable -v and -g&lt;/p&gt;

&lt;pre&gt;
f=10  
g=5
&lt;/pre&gt;

&lt;p&gt;Here we use getopts with the arguments -f -g. If the user write, software.bsh -f value1 and/or -g value 2.&lt;/p&gt;

&lt;pre&gt;
while getopts ":f:g:" option; do  
    case "${option}" in  
        f)  
            f=${OPTARG}  
            ((f == 15 || f == 75)) || usage  
            ;;  
        g)  
            g=${OPTARG}  
            ;;  
        *)  
            usage
            ;;
    esac  
done
&lt;/pre&gt;

&lt;p&gt;That makes the arguments in Bash more professional.&lt;/p&gt;

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

</description>
      <category>bash</category>
      <category>linux</category>
      <category>arg</category>
      <category>getopts</category>
    </item>
    <item>
      <title>Create a login page in Python with flask and/or gunicorn   💻 🐍 </title>
      <dc:creator>Hamdy Abou El Anein</dc:creator>
      <pubDate>Sat, 05 Feb 2022 18:59:48 +0000</pubDate>
      <link>https://dev.to/hamdyaea/create-a-login-page-with-in-python-with-flask-andor-gunicorn-1g5a</link>
      <guid>https://dev.to/hamdyaea/create-a-login-page-with-in-python-with-flask-andor-gunicorn-1g5a</guid>
      <description>&lt;p&gt;In this post I will show you how to create a simple login page in Python with the flask library and finally with gunicorn.&lt;/p&gt;

&lt;p&gt;let's begin!&lt;/p&gt;

&lt;p&gt;Create the virtual env:&lt;/p&gt;

&lt;p&gt;in the project directory :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python3 -m venv myenv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then the linux command to activate the new env:&lt;br&gt;
&lt;code&gt;source /myenv/bin/activate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we are in our dev env.&lt;/p&gt;

&lt;p&gt;From here install the flask library :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install flask&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we can start to develop our flask app.&lt;/p&gt;

&lt;p&gt;Create a file that you can call &lt;code&gt;mylogin.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env python3

from flask import Flask, render_template, redirect, url_for, request

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] == 'Admin' and request.form['password'] == 'Admin1234$':
            return render_template("adminpage.html")
        elif request.form['username'] == 'User' and request.form['password'] == 'User1234$':
            return render_template("adminpage.html")
        else:
            return 'Invalid login or password, please try again'

    return render_template('login.html', error=error)


if __name__=='__main__':
    app.run(host='0.0.0.0')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have created two users :&lt;/p&gt;

&lt;p&gt;name: &lt;strong&gt;Admin&lt;/strong&gt;&lt;br&gt;
password : &lt;strong&gt;Admin1234$'&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;and &lt;/p&gt;

&lt;p&gt;name: &lt;strong&gt;User&lt;/strong&gt;&lt;br&gt;
password: &lt;strong&gt;User1234$&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir templates&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;in templates, we will create 3 files :&lt;/p&gt;

&lt;p&gt;adminpage.html  login.html  userpage.html&lt;/p&gt;

&lt;p&gt;&lt;code&gt;login.html&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;title&amp;gt;Login page&amp;lt;/title&amp;gt;
&amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
        &amp;lt;h1&amp;gt;Please login&amp;lt;/h1&amp;gt;
        &amp;lt;br&amp;gt;
        &amp;lt;form action="" method="post"&amp;gt;
            &amp;lt;input type="text" placeholder="Username" name="username" value="{{  request.form.username }}"&amp;gt;
            &amp;lt;input type="password" placeholder="Password" name="password" value="{{  request.form.password  }}"&amp;gt;
            &amp;lt;input class="btn btn-default" type="submit" value="Login"&amp;gt;
        &amp;lt;/form&amp;gt;
        {% if error %}
            &amp;lt;p class="error"&amp;gt;&amp;lt;strong&amp;gt;Error:&amp;lt;/stong&amp;gt;  {{  error }}
        {% endif %}
    &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;adminpage.html&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;This is the admin private page&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;userpage.html&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;&amp;gt;This is the user private page&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can test it with flask:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python mylogin.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The app is running...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; * Serving Flask app 'mylogin' (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://YOUR.IP:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 521-493-757
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now open a web browser and connect to your ip : &lt;a href="http://YOUR.IP:5000/" rel="noopener noreferrer"&gt;http://YOUR.IP:5000/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have a firewall installed, the port 5000 must be open.&lt;/p&gt;

&lt;p&gt;You can now try to login with the admin the with the user account.&lt;/p&gt;

&lt;p&gt;Now we can make it run for production with gunicorn :&lt;/p&gt;

&lt;p&gt;install gunincorn:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install gunicorn&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;in the main directory, we need to create a file called :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;wsgi.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from mylogin import app

if __name__=='__main__':
    app.run()

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

&lt;/div&gt;



&lt;p&gt;Here mylogin is our mylogin.py Python/Flask app.&lt;/p&gt;

&lt;p&gt;Now we can run it with gunicorn :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gunicorn --bind 0.0.0.0:5000 wsgi:app&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It's running well...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2022-02-05 19:51:48 +0100] [6545] [INFO] Starting gunicorn 20.1.0
[2022-02-05 19:51:48 +0100] [6545] [INFO] Listening at: http://0.0.0.0:5000 (6545)
[2022-02-05 19:51:48 +0100] [6545] [INFO] Using worker: sync
[2022-02-05 19:51:48 +0100] [6546] [INFO] Booting worker with pid: 6546
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open again your web browser at the same address and it's running.&lt;/p&gt;

&lt;p&gt;The project directory is like this :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;----Project-Directory-&amp;gt;    &lt;br&gt;
           |    &lt;br&gt;
           |    &lt;br&gt;
           --mylogin.py    &lt;br&gt;
           --wsgi.py    &lt;br&gt;
           |     &lt;br&gt;
           |     &lt;br&gt;
            ----Project-Directory-&amp;gt;----templates-&amp;gt;     &lt;br&gt;
                 |     &lt;br&gt;
                 |    &lt;br&gt;
                 --login.html    &lt;br&gt;
                 --userpage.html    &lt;br&gt;
                 --adminpage.html&lt;/code&gt;     &lt;/p&gt;

&lt;p&gt;Finally the library requirements in this project :&lt;/p&gt;

&lt;p&gt;requirements.txt&lt;/p&gt;

&lt;p&gt;&lt;code&gt;click==8.0.3&lt;br&gt;
Flask==2.0.2&lt;br&gt;
gunicorn==20.1.0&lt;br&gt;
itsdangerous==2.0.1&lt;br&gt;
Jinja2==3.0.3&lt;br&gt;
MarkupSafe==2.0.1&lt;br&gt;
Werkzeug==2.0.2&lt;/code&gt;&lt;/p&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhe3ao48k5i52kkthozgk.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhe3ao48k5i52kkthozgk.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>flask</category>
      <category>gunicorn</category>
      <category>login</category>
    </item>
    <item>
      <title>How to find the N most present element in a list in Python 💻 🐍</title>
      <dc:creator>Hamdy Abou El Anein</dc:creator>
      <pubDate>Wed, 26 Jan 2022 10:53:11 +0000</pubDate>
      <link>https://dev.to/hamdyaea/how-to-find-the-n-most-present-element-in-a-list-in-python-5fej</link>
      <guid>https://dev.to/hamdyaea/how-to-find-the-n-most-present-element-in-a-list-in-python-5fej</guid>
      <description>&lt;p&gt;If you have a list in Python 🐍 and you need to find for example the 3 elements that are the must present in the list. &lt;/p&gt;

&lt;p&gt;You can do it like this :&lt;/p&gt;

&lt;p&gt;Create a list&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mylist = [1,2,3,4,4,4,4,5,5,6,7,8,8,8,9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create two variables, maxcount is the number N of elements that we are searching.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;count = 1
maxcount = 3

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

&lt;/div&gt;



&lt;p&gt;Use a while loop to find what we are looking for :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while count &amp;lt;= maxcount:
    num = max(set(mylist), key=mylist.count)
    print(num)
    count+=1
    while num in mylist:
        mylist.remove(num) #Here we remove the element that was the max
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the full code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mylist = [1,2,3,4,4,4,4,5,5,6,7,8,8,8,9]

count = 1
maxcount = 3

while count &amp;lt;= maxcount:
    num = max(set(mylist), key=mylist.count)
    print(num)
    count+=1
    while num in mylist:
        mylist.remove(num)

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

&lt;/div&gt;



&lt;p&gt;Now the result is :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4
8
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can even add them to a new list...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jb50Mx1R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tlxjxrq036qhd4nte4o2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jb50Mx1R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tlxjxrq036qhd4nte4o2.jpg" alt="Image description" width="300" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
    </item>
  </channel>
</rss>
