Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Saturday, May 15, 2021

Django - Simple Registration Form

Django - Simple Registration Form 

 


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<p>Register</p>

<form method="POST" action="">
{% csrf_token %}
{{form.username.label}}
{{form.username}}
{{form.email.label}}
{{form.email}}
{{form.password1.label}}
{{form.password1}}
{{form.password2.label}}
{{form.password2}}
<input type="submit" name="new_user">


</form>


</head>
<body>

</body>
</html>

Saturday, March 13, 2021

FastApi : Create Production ready Api

 FastApi : Create Production ready Api (faster than Flask)


https://fastapi.tiangolo.com/

Features

1. Asynchronous

2. High Perfromance

3. Less Code

4. Data Type  and Data Models auto Conversions

5. Auto Documentation

    - swagger (/docs)

    - ReDoc   (/redoc)


Pre-Req:

Install and activate virtual environment to be safe.

Steps:

  1. pip install fastapi
  2. pip install hypercorn #server
  3. touch main.py
  4. copy paste below code
  5. hypercorn main:app --reload


from fastapi import FastAPI
from pydantic import BaseModel #model
# import requests

app = FastAPI()
db = []

class Person(BaseModel):
name: str

@app.get('/')
def index():return {'person' : 'name'}

@app.get('/persons')
def get_persons():
results = []
for person in db:results.append(person)
return results

@app.get('/persons/{person_id}')
def get_person(person_id: int):return db[person_id-1]

@app.post('/persons')
def create_person(person: Person):
db.append(person)
return db[-1]

@app.delete('/persons/{person_id}')
def delete_person(person_id: int):
db.pop(person_id-1)
return {}

Sunday, November 29, 2020

Django

 Django:

What is Django ?

Web Framework
Creates pre-existing code
Can be used to create mulitple apps like - blogs , authentication etc .,
Uses MVT architecture

MVT architecture (Model View Template)

No Controller unlike MVC
Browser > Django > urls.py > View
View pulls data from model(DB) and Displays it on Template (HTML)

How to use:

  • No need to stop Django server when doing development
  • There will be only 1 settings file in the main project to handle auths , templates etc.,
  • When you want to create a new webpage do not modify the auto generated Framework.
  • Always create your own app with the command ($python manage.py startapp app_name)
 

Example Source Code

Pre-Req:
  •     make sure python is installed.
  • $pip install virtual env
  •     $virtualenv venv
  •     $source venv/bin/activate
  •     $pip install django
  •     If ur using Pycharm than goto : Preference>Select virtual Env created
Create django specific project , Create APP 
  •     django-admin startproject Main #Start django project
  •     $cd Main
  •     python manage.py startapp PersonApp #Create App
  •     python manage.py runserver
  •     open http://127.0.0.1:8000/
  •     comment - settings.py > MIDDLEWARE >
Register APP:
  •     settings.py > Installed APP ['PersonApp']
  •     settings.py > TEMPLATES > 'DIRS': [os.path.join(BASE_DIR, 'PersonApp','html')] #import os
  •     Under urls.py
from django.urls import path,include
path('', include('PersonApp.urls'))

Steps:

1)PersonApp >html>person.html
<form method="POST">
<p>{{ data }}</p>
<textarea name="fname">{{fname}}</textarea>
<textarea name="sname">{{sname}}</textarea>
<input type="submit">
</form>

2) #Under PersonApp/views.py
from django.shortcuts import render
def person(request):
if request.method == "GET":
return render(request, 'person.html', {"data":"enter ur name"})
elif request.method == "POST":
fname = request.POST.get('fname', None)
sname= request.POST.get('sname', None)
print("accepted :"+fname)
return render(request, 'person.html', {"data":"Accepted !"})


3)#Under PersonApp/urls.py
from django.urls import path
from . import views
urlpatterns= [path('',views.person,name='person')]

4) Create Model  - #PersonApp/models.py
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)

    #Under admin.py
from .models import Person
admin.site.register(Person)

5) Create a DB from Model
python manage.py makemigrations
python manage.py migrate

python manage.py createsuperuser
0.0.0.0/admin

6) Adding and Retrieving Data fro Db
from .models import Person
def person():
person=Person()
person.first_name=fname
person.last_name=sname
person.save()
print("records ="+str(len(Person.objects.all())))

Ref for HTML looping and API:

URL Dispatching

  • Copy a urls.py from base Framework into the new App folder
  • Notice line "path('admin/', admin.site.urls)" in Framework urls.py eg: path is /admin than redirect to admin page
  • Add below line in url.py in Base Framework
path('test', include('app_name.urls')) #make sure "include" is imported
  • This redirects "/test" to urls.py in the App folder.
  • Add below lines in urls.py in app_name :
from app_name import views
urlpatterns = [path('test', views.test,name="test")]

  • In views.py of app_name add below lines:
from django.shortcuts import render,HttpResponse
def test(request):
return HttpResponse("Testpage")

  • Notice here controls goes from  url.py(base) >> url.py >> views.py

StaticFolders:

  • The files inside static folders are made public
  • Can Contain CSS / JS to be used in the webpage
  • Create folder "static" inside project
  • create file "test.txt" with some contents inside and save
  • Add this to settings.py : STATICFILES_DIRS = [BASE_DIR / "static",'/var/www/static/',]
  • open link -http://127.0.0.1:8000/static/test.txt

Create Templates with paramaters

  • Create folder "templates" inside project
  • Create a home.html inside templates with following :
<html><body>My First Heading {{key1}}</body></html>
  • Goto settings.py > Under TEMPLATES > Add 'DIRS': [BASE_DIR / "templates",],
  • Goto to the app views.py > Add line to the related resource method - 
return render(request,"home.html",{"key1":"value"})
  • BootStraping
  • Used for ready made websites : https://getbootstrap.com/docs/4.5/getting-started/introduction/
  • Copy Starter Template html
  • You can use replace the html in home.html and save
  • reload page

Template Inheritance

  • Inherit the base template across the whole of the application
  • Create base.html in templates folder
  • Add templates html here , add placeholder for replacement wherever needed 
Eg:{% block body %}{% endblock body %} , {% block title %}{% endblock title %}
  • delete contents of home.html and add :
  •  In first line {% extends 'base.html' %}
  •  Add replacements for placeholder - {% block body %} HI {% endblock body %}
  •  Save

Using Images in Website:

Save any images into static folder
Rename the extension as ".jfif"
In the html file use as "/static/file.jfif"

Admin Logging Creation(We need to create Tables)

  • $python manage.py createsuperuser
  • Open http://127.0.0.1:8000/admin

To Create DB Table:

    Model is a Table
    Inside models.py
        Eg:class Person(models.Model):
                first_name = models.CharField(max_length=30)
                last_name = models.CharField(max_length=30)

    Register in admin.py  :
        from home.models import Person
        admin.site.register(Person)

    Register in Settings.py(under INSTALLED_APPS):
        'home.apps.Person'

    whenever u touch models.py , u need to run below commands
    $python manage.py makemigrations #notice any changes and store in a DB.
    $python manage.py migrate #Apply changes 

API

1. Create a model
2. migrate it to create a DB
3. Create a serializer  : Converts models to Json 
4. Create a view and use the serializer 
5. Create url.py and use the view

#https://www.youtube.com/watch?v=263xt_4mBNc
#https://www.youtube.com/watch?v=QB9gGEwxxM4

Pre-Req

    pip install djangorestframework
    pip freeze > requirements.txt
    python manage.py startapp api 
  •       settings.py > INSTALLED_APPS = 'rest_framework'
  •       url.py > urlpatterns=[path('api/', include('api.urls')),]

create  api/serializers.py : Converts models to Json 

from rest_framework import serializers
from .models import Person

class personSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Person
fields = ("url","first_name","last_name")

api/views.py

from rest_framework import viewsets
from .models import Person
from .serializers import personSerializer


#make sure Person model is created in models.py and migrated
class personView(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = personSerializer

create  api/url.py : Handles all API endpoint

from django.urls import path , include
from . import views
from rest_framework import routers


router = routers.DefaultRouter()
router.register('person',views.personview)
urlpatterns = [path('', include(router.urls))]

Ref:

https://www.youtube.com/watch?v=JxzZxdht-XY&t=5368s

Python Virtual Environment (venv)

 Python Virtual Environment (venv)


Pre-Req :

  • This is on Mac
  • Make sure Python3 is installed in the system
  • $pip3 install virtualenv #--upgrade
  • $virtualenv #help

Steps:

  1. create folder
  2. cd inside folderName
  3. $virtualenv env_name  #-p /folder_path_of_python_to_clone/bin/python
  4. $source ./env_name/bin/activate
    1. #if u get permission denied "$chmod -R 777 *"
    2. #Note u can also have multiple environments and activate which ever u want
  5. Notice "env_name" in the Terminal line meaning it is activated.
  6. Try:
    1. $python
    2. import requests # you will get error
    3. exit()
  7. Try2 (make sure virtual env is still activated)
    1. $pip install requests
    2. $python
    3. import requests
    4. exit()
  8. $pip freeze > requirements.txt
  9. To install requirements from above file #pip install -r requirements.txt
  10. $deactivate #Deactivate the env

Note:

  1. To use Virtual Environment in Pycharm
  2. Preferences> Settings > Add > Virtualenv 
  3. Make sure it is pointing to venv

Sunday, January 19, 2020

Scala vs Python

Scala vs Python

----------------VAR / CONST ------------
val a:String="123" // Scala
var a:String="123" // Scala

a="123"# Python
--------------COMMENTS------------------
// this is a commented line in Scala
/* and this is a multiline comment,
...just choose! */

#Hi # Python
""" # Python
multi
"""
or
''' # Python
multi Line
'''
---------------PRINT-----------------
print("whatever you want") #  Python
println("whatever you want") // Scala

a="hi"
print(f"{a}")

val a="hi"
println(s"$a")

---------------LIST/ARRAY-----------------
Declaration :
var myArray = Array(2, 5, "apple", 78) // Scala
my_list = [2, 5, 'apple', 78] # Python

Indexing
myArray(1) // Scala 
my_list[1] # Python 

Slicing
myArray.slice(0,3) // Scala
my_list[0:3] # Python 


myArray.last    // Scala last element
myArray.max     // Scala maximum element
myArray.min     // Scala minimum element

my_list[-1]    # Python last element
max(my_list)   # Python maximum element
min(my_list)   # Python minimum element

sum(my_list) # Python- sum

myArray.sum    // scala sum
myArray.product  // multiplying elements in array

my_list.append('last words') # Python
myArray :+= "last words" 

---------------LOOP-----------------
for i in my_list:print(i)# Python
for (i <- myArray){ println(i)}// Scala
val list = List(1, 2, 3)
list.foreach(x => println(x)) 


--------------MAP------------------
list(map(lambda x:x*3,my_list)) # map in Python
myArray.map(i => i*3) // map in Scala

---------------FILTER-----------------
list(filter(lambda x:x%3==0,my_list)# filter in Python
myArray.filter(i => i%3 == 0) // filter in Scala

---------------REDUCE-----------------
from functools import reduce # Python
print(reduce(lambdax,y:x+y,(1,2,3))) #Python

val lst = List(1, 2, 3) // Scala
list.reduce((x, y) => x + y) // => 6 // Scala

---------------LIST COMPREHENSION-----------------
[i*3 for i in my_list if i%2 == 0] # list comprehension
myArray.filter(i => i%2 == 0).map(i => i*3) // Scala

---------------SORT-----------------------
sortBy(f: T => K): Seq[T]

---------------DICTIONARY-----------------
Declaration:
my_dict = {'first_name': 'Emma','age': 18}# Python
var myMap = (("firstName", "Emma"),("age", 18)) // Scala

New key:
my_dict['country_of_origin'] = 'Italy' # Python
myMap += ("countryOfOrigin" -> "Italy") // Scala

Access:
my_dict['first_name'] # Python
myMap("firstName") // Scala

Loop:
for key, value in my_dict.items(): # Python
    print(key)
    print(value)

for ((key, value) <- myMap){ // Scala
    println(key)
    println(value)
}
--------------TUPLE------------------
my_tup = (1, 2, 3) # Python
my_tup[0] # Python


myTup = (1, 2, 3) // Scala
myTup._1 // Scala
// the indexing is way different than arrays!

-------------SET-------------------
my_set = {1, 3, 5, 1} # Python
mySet = Set(1, 3, 5, 1) // Scala

-------------FUNCTION-------------------
def chop_string(input_string): # Python
    return input_string[0:5]

def chopString(inputString: String): String = { // Scala
    inputString.slice(0, 5)
}
--------------NAMED TUPLE/CASE CLASS------
from collections import namedtuple
n=namedtuple("n",["a","b"])
n2=n(10,20)
print(n2.a)

case class n(a:String,b:Int)
val n2: n = n("name", 1)
println(n2.a)
----------------SORTED 1---------------------
List(10, 5, 8, 1, 7).sortWith(_ < _) // Scala asc
sorted([10, 5, 8, 1, 7] , reverse =false) # Python

----------------SORTED 2---------------------
val list = List(('b',30),('c',10),('a',20))// Scala
println(list.sortBy(_._2))

l=[[1,2],[2,1]] # Python
sorted(l,key=lambdax:x[1])

----------------SORTED 3---------------------
List("banana", "pear", "apple", "orange").sortWith(_.length < _.length) // Scala
sorted(["banana", "pear", "apple", "orange"],key=lambda x:len(x)) # Python

output:(pear, apple, banana, orange)

-----------------Switch / Case ----------------
val result =1
result match {
case -1 =>{println("-1")}
case 0 => { println("asd")}
case x:Int if(x>0) =>
{ println(x)}
}

------------------OPTION = Some/getorelse/match---------------------
To eliminate NullPointerExceptions.

val age:Option[Integer] = None //Some(28)
val x=age.getOrElse(throw new Exception("error"))

Friday, December 27, 2019

Python : SOLID Principles


SOLID principles:


  • S Single Responsibility - Should have single reason to change and should be related to its primary objective.
  • O Open Closed - add features by extension, not by modification
  • L Liskov Substitution
  • I Interface Segregation
  • D Dependecny Inversion

AntiPattern:
    God Object : Reverse of Single Responsibility ie., 1 class doing everything

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Single Responsibility : A class should have only one job
Example :
class User:
def __init__(self, name: str): self.name = name
def get_name(self) :pass
def save(self, user: User): pass

Problem: User class is taking responsibility of Saving as well .
Solution :
class User:
def __init__(self, name: str): self.name = name
def get_name(self): pass

class UserDB:
def save(self, user: User):pass
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Open Closed : Classes should be open for extension, not for modification.
Example :
class bottle:
def __init__(self, season, price):
self.season = season
self.price = price

def give_discount(self):
if self.season == 'normal': return self.price
if self.season == 'festival': return self.price * 0.1

Problem : Let’s imagine you have a store, and you give a discount of 20% to your favorite customers using this class:
When you decide to offer double the 20% discount to VIP customers. You may modify the class

Solution:
class bottle:
def __init__(self, season, price):
self.season = season
self.price = price
def give_discount(self):return self.price

class Festival_bottle_price(bottle):
def give_discount(self): return self.price * 0.1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Liskov Substitution Principle:  A sub-class can assume the place of its super-class without errors.
Example :
class Bird():
def fly(self):pass

class Duck(Bird):
def fly(self):pass

class penguin(Bird):
def fly(self):pass

Problem : This breaks LSP.As penguin cannot fly hence this causes error
Solution :
class Bird():pass
class FlyingBirds(Bird) :
def fly(self):pass

class Duck ( FlyingBirds):pass
class Ostrich ( Bird): pass
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Interface Segregation:Make interfaces that are client specific Clients should not be forced to depend upon interfaces that they do not use
Example :
class Machine:
def print(self):raise NotImplementedError()
def call(self):raise NotImplementedError()

class Printer(Machine):
def print(self, document): pass

Problem : Printer object throws exception when Printer().call() is performed as it should not have been exposed in the first place.
Solution:
class Machine:
def perform_operation(self):raise NotImplementedError()

class Printer(Machine):
def perform_operation(self, document): pass
class call_phone(Machine):
def perform_operation(self, document): pass
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Dependency Inversion  :A: High-level modules should not depend on low-level level modules. It should depend upon its abstraction.
Example :
class Http:
def __init__(self, xml_http_service: XMLHttpService):self.xml_http_service = xml_http_service

def get(self, url: str, options: dict):self.xml_http_service.request(url, 'GET')

def post(self, url, options: dict):self.xml_http_service.request(url, 'POST')

class XMLHttpService(XMLHttpRequestService):pass

Problem:Http =high-level component , XMLHttpService= low-level component.
The Http class should care less the type of Http service you are using

Solution:
class Connection:
def request(self, url: str, options: dict):
raise NotImplementedError

class Http:
def __init__(self, http_connection: Connection):
self.http_connection = http_connection

def get(self, url: str, options: dict):self.http_connection.request(url, 'GET')

def post(self, url, options: dict):self.http_connection.request(url, 'POST')

class NodeHttpService(Connection):def request(self, url: str, options:dict):pass

class MockHttpService(Connection):def request(self, url: str, options:dict):pass






Python :open Closed principle



"""OCP - Open Closed PrincipleOpen for expansion but closed for modification 
Goal : Instead of modifing existing class , your are encouraged to create a new class to solve the problem
Summary : To select object from a list of objects based on the properties
Description :Assume you have a box containing objects .You job is to select an object based on the properties like size and color . Here number of objects , size and color can be of any number .You have to printTrue for object which satisfies all the properties mentioned using Open Closed Principle.
Solution:1. you can select an object by size or color .Eg: small2. You can select object by both size and color . Eg: small and red
Steps:1. Start by creating classes which selects an object based on 1 property2. If u have 2 property create 2 classes3. For combination create last class which takes above 2 class objects as input  
"""
from enum import Enum
class COLOR(Enum):
    black="BLACK"    white="WHITE"
class SIZE(Enum):
    big="BIG"    small="small"
class Product():
    def __init__(self,color,size):
        self.color=color
        self.size=size

class match():
    def test(self,obj1,obj2):
        pass
class color_match(match):
    def __init__(self,color):
        self.color=color

    def test(self,target):
        return self.color==target.color

class size_match(match):
    def __init__(self,size):
        self.size=size

    def test(self,target):
        return self.size==target.size

class And_operation():
    def __init__(self,match1,match2):
        self.match1=match1
        self.match2=match2

    def test(self,target):
        return self.match1.test(target) and self.match2.test(target)

pen=Product(COLOR.black.value, SIZE.small.value)
paper=Product(COLOR.white.value,SIZE.small.value)
book=Product(COLOR.white.value,SIZE.big.value)

Products=[pen,paper,book]
Color_match=color_match(COLOR.white.value)
Size_match=size_match(SIZE.small.value)

combination=And_operation(Color_match,Size_match)
for product in Products:
    print(combination.test(product))
Output :
False
True
False

Enum vs Named tuple in Python

Enum vs Named tuple

  1. enums are a way of aliasing values, 
  2. namedtuple is a way of encapsulating data by name. 
  3. The two are not really interchangeable, 
 Example :

from collections import namedtuple
from enum import Enum

class HairColor(Enum):
    blonde = 1
    brown = 2
    black = 3
    red = 4

Person = namedtuple('Person', ['name','age','hair_color'])
bert = Person('Bert', 5, HairColor.black)

 you can use enums as values in  namedtuple.


Python Setter and getter :

Python Setter and Getter :

Without Setter and getter , in the below example when the users first name or last name is changed individually his full name is not getting changed : 

Method 1 : Without Decorator

class  a():
   _value=0
    def __init__(self,value):
         self._value = value
    
    def get_value(self):
        return self._value
     
    def set_value(self, x):
        self._value = x


---------------------------------------------------------------------------------
Method 2  : With Decorator 
class a():
    _value=0
    def __init__(self,value):
        self._value = value

    @property
    def value(self):
        return self._value

    @value.setter       # should match the method name of @property
    def value(self, a):
        self._value = a


A=a("hi")
A.value=10
print(A.value)




Thursday, December 26, 2019

Python Refactor

Python Refactor :


Rules:

    General :
  1.         Follow PEP8 Standard
  2.         DRY - Dont Repeat Yourself ie., not more than 2 times
  3.         1 Function / method should perform only 1 thing
  4.         Use helper functions (small functions)
  5.         Simplicity (logic , naming etc )
    Functions/Method:
  1.         big functions are bad
  2.         group related methods / variables in a file
  3.         split or merge methods / functions using refactor feature in IDE
    Classes/Objects:
  1.         Hide/Expose logic using middeleman classes
  2.         Separate general and specific code in a class
  3.         objects should not talk to strangers
  4.         Use parent-child class whenever method/variables is repeated across different classes
    Others:
  1.         breaking 1 big expression in conditional statements into simple statments
  2.         Use named tuple to mini classes for data segregation
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Tools:

  1.     Linters (Quality Checker):             Pylint , PEP8 and McCabe
  2.     Formatters (Formatter):                 Autopep8
  3.     Style Checkers(recommendations):    pycodestyle , pydocstyle Eg: pycodestyle file.py
  4.     Pycharm(Refactor):                    In Editor > Rt ck >refactor

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Anti Patterns :    

    1. Too much code in a function/method
    2. Implementation of OOP / Not correct application of OOP /Too much OOP
    3. Duplicate Code
    4. Avoiding intertwine code
    5. Rename variables ie., do not use v1 ,v2 etc.,
    6. Avoid useless /non-operational code
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Design Patterns in OOP:

    1. If subclasses need not be modifiedby others , than instantion of child object inside parent class and make parent method static.
     2. Instead of having checks to returm certain value , you can create a null class which returns null /empty/relavent value and instantiate that instead.Instead of actual classs.
    3.Replace conditional with subclasses
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Law of Demeter

 the Law of Demeter for functions requires that a method m of an object O may only invoke the methods of the following kinds of objects:

    O itself
        public class ClassOne {
            public void method1() {method2();}
            public void method2() {}
        }


    m's parameters
        public class ClassOne {
            public void method1(ClassTwo classTwo) {
                classTwo.method2();
            }
        }
        class ClassTwo {
            public void method2() {   }
        }


    Any objects created/instantiated within m
        public class ClassOne {       
            public void method1() {
                ClassTwo classTwo = new ClassTwo();
                classTwo.method2();
            }
        }
        class ClassTwo {       
            public void method2() {  }
        }


    O's direct component objects
        public class ClassOne {
            private ClassTwo classTwo;   
            public void method1() {
                classTwo = new ClassTwo();
                classTwo.method2();
            }
        }
        class ClassTwo {
            public void method2() { }
        }

    A global variable, accessible by O, in the scope of m
        public class ClassOne {
            public void method1() {
                classTwo.STATIC_INSTANCE.method2();
            }
        }
        class ClassTwo {
            public static final ClassTwo STATIC_INSTANCE = ...;
            public void method2() { }
        }

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1. OOP is mainly used to organize your business logic while  
  2. AOP helps to organize your non-functional things like Auditing, Logging, Transaction Management , Security etc. This way you can decouple your business logic with non-fictional logic that makes code cleaner.







Wednesday, September 18, 2019

Linux / Mac : Get latest of chromeDriver

Linux / Mac : Get latest of chromeDriver



LINUX
currdir=$(pwd)/misc
latest_version=$(wget https://chromedriver.storage.googleapis.com/LATEST_RELEASE -O -) && wget https://chromedriver.storage.googleapis.com/${latest_version}/chromedriver_linux64.zip -P $currdir
unzip -q $(pwd)/misc/chromedriver_mac64.zip && /bin/rm $(pwd)/misc/chromedriver_linux64.zip


MAC
#mac make sure "wget" is installed
#brew install wget
currdir=$(pwd)/misc
latest_version=$(wget https://chromedriver.storage.googleapis.com/LATEST_RELEASE -O -) && wget https://chromedriver.storage.googleapis.com/${latest_version}/chromedriver_mac64.zip -P $currdir
unzip -q $(pwd)/misc/chromedriver_mac64.zip && /bin/rm $(pwd)/misc/chromedriver_mac64.zip

Thursday, July 4, 2019

Python : Config /Properties/ ini file



Python : Config/ini/Properties file

-----------credentials.config-----------

[db]
dbname= dbname
hostname= host_anme
port= 1234
username= usr_name
password= pass

----------------------

Python : Config /Properties/ ini file


from configparser import RawConfigParser

config_path = os.getcwd()+'/properties/credentials.config'
confparser = RawConfigParser()
with open(config_path, "r") as config_file:
    confparser.read_file(config_file)

KWARGS = {
    "dbname": confparser["db"]["dbname"],
    "hostname": confparser["db"]["hostname"],
    "port":confparser["db"]["port"],
    "username": confparser["db"]["username"],
    "password": confparser["db"]["password"]
        }
"""

Monday, May 27, 2019

pandas : Lambda ,Map and Eval Conditions

Lambda ,Map and eval in Pandas


1. Simple 


import pandas as pd

a=pd.DataFrame({"a":["deea","123","deep","deepak"]})
b=pd.DataFrame({"b":["deeap","1234","deepak","deepa"]})
c=pd.DataFrame(map(lambda x,y:x in y,a['a'],b['b']))
print(c)
 
 
result : 
       0
0   True
1   True
2   True
3  False 
 

2. Using Dynamic Formula :

import pandas as pd

a=pd.DataFrame({"a":["deea","123","deep","deepak"]})
b=pd.DataFrame({"b":["deeap","1234","deepak","deepa"]})
c=pd.DataFrame(map(eval(input("enter -lambda x,y:x in y")),a['a'].values,b['b'].values))
print(c)

       0
0   True
1   True
2   True
3  False
 

3)Using Dynamic Formula and 1st columns

import pandas as pd

a=pd.DataFrame({"a":["deea","123","deep","deepak"]})
b=pd.DataFrame({"b":["deeap","1234","deepak","deepa"]})
c=pd.DataFrame(map(eval(input("enter -lambda x,y:x in y")),a.iloc[:,0],b.iloc[:,0]))
print(c)
  
Result: 
  0
0 True
1 True
2 True
3 False 
 
"""

"""

Column Names

data.iloc[:,0] # first column of data frame (first_name)
data.iloc[:,1] # second column of data frame (last_name)
data.iloc[:,-1] # last column of data frame (id)

Rows and Columns
data.iloc[0:5] # first five rows of dataframe
data.iloc[:, 0:2] # first two columns of data frame with all rows
data.iloc[[0,3,6,24], [0,5,6]] # 1st, 4th, 7th, 25th row + 1st 6th 7th columns.
data.iloc[0:5, 5:8] # first 5 rows and 5th, 6th, 7th columns of data frame
"""

Sunday, April 21, 2019

pytest : All about pytest

pytest : All about pytest




Index :
  1.  Installation x 2
  2.  Project structure
  3. Sample code
  4. Important commands x 7
  5. Run specific test case
  6. Raise pytest exception (pytest.raises)
  7. labels (pytest.mark.label_name ,pytest.mark.skip,pytest.mark.skip_if)
  8. Parameterize (pytest.mark.parametrize('x,y,res',[(1,2,3),(0,3,3)]) )
  9. Fixture (pytest.fixture(scope="module") )
  10. conftest
  11. pdb
Installation:
Using pip
pip3 install pytest
pip install selenium

Using requirements.txt : project>requirements.txt
selenium >= 3.0
pytest >= 3.10.1
pytest-html >= 1.20.0

pip install -r requirements.txt
or
pipenv install -r requirements.txt #Inside virtual env
Project structure

add file : - project>tests>test_filename.py

Sample Code
from selenium import webdriver
def test_1():
 driver=webdriver.Chrome("chromedriverpath")
 driver.get(url)
 objs=driver.find_elements_by_xpath("//button")
 for i in objs:
  i.click()
  1. open terminal
  2. cd to project
  3. excute command "pytest"
Important commands:
1.    pytest --pdb #runs the debugger when an err is encountered
2.    pytest -s #to "print" result in console
3.    pytest -v #verbose 
4.    pytest --maxfail=2 # limit no of fails
5.    py.test --html=1.html
6.    pipenv run pytest
7.    pipenv run py.test --html=1.html

pytest -v -k "add or something" # runs only methods with add ,something
pytest -h # help
pytest -v -x #to stop after 1st fail
pytest -v -x --tb=no # do not display stack trace
pytest -q # quiet mode
pytest -rsx #report skipped tests 
pytest --lf #runs only tests that failed on last attempt
# in eclipse Window > Preferences > Pydev> PyUnit = Py.testRunner and add above Parameters 

Execute specific method of class
pytest test_filename.py::test_method

Exception
def test_mytest():
with pytest.raises(ZeroDivisionError):
  1/0
  
Markers(labels)
@pytest.mark.label_name" .To run "pytest -v -m label_name"
@pytest.mark.skip(reason="any reason") #skip methods
@pytest.mark.skip_if(sys.version_info < (3,3),reason="some reason")

Parametrize
import pytest
@pytest.mark.parametrize('x,y,res',[(1,2,3),(0,3,3)])
def test_add(x,y,res):
    assert classname.add(x,y,res)

Fixtures(similar to @Before annotations in Junit test used for - db connection , etc.,)

@pytest.fixture(scope="module") #scope =module/session/function def fix():
    pass    #setup code
    yield   #run till here
    print("done") # tear down code

def fn(fix):
    #fix runs 1st

example:
from selenium import webdriver
@pytest.fixture()
def test_setup():
    global driver
    driver =webdriver.Chrome(executable_path="C:/Users/driver.exe")# Windows
    driver.implicitly.wait(5)
    driver.maximise()
    yield()

    driver.close()
    driver.quit()

conftest.py
You can put all fixtures inside this file and this file will execute before test starts


Debugger :
import pdb;
pdb.set_trace() # to break

Others 

Eclipse Setup :
 (if u are using eclipse : Window->Preferences --> pydev --> PyUnit --> Change the Test runner to "Py.test runner".)
Right Click over the file. Run As --> Python Unit-Test
Or press Ctrl+F9:-
It will prompt you to select the test

User can use fixture or setup-teardown
setup
import pytest
def setup_module(module):


tiredown
 
import pytest
def teardown_module(module):

Allure report
pip install allure-pytest
pip list | grep -i allure


pytest --alluredir=/Users/Documents/reports
allure generate /Users/Documents/reports

setup.py
#Add this to setup.py file:
from setuptools import setup
setup(
    # ...,
    setup_requires=["pytest-runner", ...],
    tests_require=["pytest", ...],
    # ...,
)


#And create an alias into setup.cfg file:
[aliases]
test=pytest
[tool:pytest]
addopts = --verbose
python_files = testing/*/*.py


#If you now type:
python setup.py test



Links 
#https://docs.pytest.org/en/latest/goodpractices.html#goodpractices
https://docs.pytest.org/en/latest/parametrize.html
https://docs.pytest.org/en/latest/example/parametrize.html#paramexamples
https://pypi.org/project/pytest-html/
https://pypi.org/project/pytest-csv/

 important read
 https://bytes.yingw787.com/posts/2018/12/10/data_driven_testing_three/
 https://gitlab.com/qalabs/blog/pytest-parametrize-example

important watch **
https://www.youtube.com/watch?v=o9pEzgHorH0
https://www.youtube.com/watch?v=RdE-d_EhzmA

hooks
https://stackoverflow.com/questions/21930858/pytest-parameterize-row-from-csv-as-a-testcase?rq=1 

Running pytest inside eclipse ide
Ref :https://stackoverflow.com/questions/37985589/how-to-debug-or-run-pytest-scripts-using-eclipse