Python‎ > ‎keywords‎ > ‎

from

http://www.adilwaseem.com/

Definition
Keyword assert value, module from sources.

Syntax
from

Parameters
None

Examples
Here in this example, the CharField  is used to store the data into the field.  The data is recorded and saved in models.py and retrieved and displayed through the views.py. 
 Steps for creating virtual environment, startproject, startapp, creating database, and migrating the data, and running server to check the app.
  • virtualenenv djangoschool
  • cd djangoschool
  • .\Scripts\activate
  • pip install django
  • python .\Scripts\django-admin.py startproject mysite
  • python manage.py migrate
  • python manage.py startapp dbapp
  • python manage.py makemigrations dbapp


mysite/settings,py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles','dbapp',
)

mysite/urls.py

from django.conf.urls import include, url

from dbapp.views import Display

urlpatterns = [
    url(r'^', Display),
]

dbapp/models.py


    groups=(('male','0-40'),('female','0-40'))
    genderfield=models.CharField(max_length=50,choices=groups)

v=LongLife(genderfield="Casey")
v.save()

dbapp/views.py



from . models import LongLife

def Display (request):
    return HttpResponse(LongLife.objects.filter(genderfield="Casey").values())







Here, this examples, mydb.db database is created and in that database Table3 is created with Fields, name, email, telephone and income. The values are displayed through for loop by order of income in html file.
Steps for creating virtual environment, startproject, startapp, creating database, and migrating the data, and running server to check the app.
  • virtualenenv pythonlake
  • cd pythonlake
  • .\Scripts\activate
  • pip install django
  • python .\Scripts\django-admin.py startproject mysite
  • python manage.py migrate
  • python manage.py startapp mydb
  • python manage.py makemigrations mydb




mysite/settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles','mydb',
)

mysite/urls.py


from mydb.views import DataBase
urlpatterns = [
    url(r'^$', DataBase),
]

mydb/views.py

from django.shortcuts import render

import sqlite3

def DataBase (request):
    conn=sqlite3.connect('mydb.db')
    cursor=conn.cursor()
    cursor.execute('''CREATE TABLE table3(name text, phone text, email text, income text)''')
    cursor.execute('''INSERT INTO table3 VALUES(' Erica ', ' 571 402 2992 ', ' erica.steve@ibm.com ', '  180000 ')''')
    cursor.execute('''INSERT INTO table3 VALUES(' Strube ', ' 571 402 2212 ', ' marya_strube@care.com ', ' 890000 ')''')
    conn.commit()

    for row in cursor.execute('SELECT * FROM table3 ORDER BY income'):
        response=row

    return render(request, 'mydb\index.html', {'response':response} )

mydb/index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Table3</title>
</head>
<body>

{{response}}

</body>
</html>






The django web framework returns with hyperlink created through html5 integrated with django Scripts.
Steps for creating virtual environment, startproject, startapps, migration, & running server:
    • virtualenv string
    • cd  string
    • .\Scripts\activate
    • pip install django
    • python .\Scripts\django-admin.py startproject str
    • cd str
    • python manage.py runserver
    • key ctr c to break the server
    • python manage.py startapp htmlapp
    • python manage.py runserver

    File structure 




    settings.py

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles','htmlapp',
    )

    urls.py

    from django.conf.urls import url, patterns
    from htmlapp.views import html_links

    urlpatterns = patterns('',

        url(r'^html_links/', html_links),
        )

    httpapp/views.py


    def html_links(request):
        link='<div style="background-color:pink; color:white; padding:20px;">'\
             '<a href="http://www.pythonlake.com/">Python Lake</a> <hr>' \
             '<a href="http://www.pythonlake.com/python/django">Django</a> <hr> ' \
             '<a href="http://www.pythonlake.com/python/django/djangohttp">django.http</a> <hr> ' \
             '<a href="http://www.pythonlake.com/python/django/djangohttp/httpresponse">HttpResponse</a> <hr> ' \
             '</div>'
        return HttpResponse(link)


    Running the server through Window CMD command.






    The django web framework is used to create the application which convert the string into uppercase using str.upper method. The string_method_upper.py calls the string_method.views.py file to render the function.
    Windows CMD commands for creating virtualenv, startproject, startapp, and running server.
    • virtualenv string
    • cd string
    • .\Scripts\activate
    • pip install django
    • python .\Scripts\django-admin.py startproject str
    • cd str
    • python manage.py runserver
    • python manage.py startapp string_methods
    • python manage.py startapp string_method_upper
    • python manage.py runserver



    string_method_upper/views.py


    def string_method_upper (request):
        return string_upper(request)

    string_methods/views.py


    def string_upper(request):
        return HttpResponse("Python is a simple, powerful and high-level programming language "
                            "and the logic and structure is consistent with the way we "
                            "reasons. ".upper())


    urls.py

    from string_methods.views import*
    from string_method_upper.views import *

    urlpatterns=patterns('', url(r'^string_method_upper/', string_method_upper),)

    settings.py

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles','string_methods', 'string_method_upper',
    )




    The equal to:

    from fractions import Fraction
    a=Fraction(5,16)
    b=Fraction(9,16)
    c=a+b
    print(c)

    >>> 
    7/8
    >>>


    The returns are:
     >>> from fractions import Fraction
    >>> a=Fractions(1,3)
    >>> a=Fraction(1,4)
    >>> b=Fraction(3,4)
    >>> a+b
    Fraction(1, 1)
    >>> a-b
    Fraction(-1, 2)
    >>> c=Fraction(1,4)
    >>> c+a
    Fraction(1, 2)
    >>> 



    The method computed the int(value) of the decimal and then run loop through range() function.
    from decimal import Decimal


    for i in range(a,10):
        print(i)
        print(i/Decimal(1.1).log10())
            
    >>>
    0
    0E+29
    1
    24.15885792809678504023429545
    2
    48.31771585619357008046859090
    3
    72.47657378429035512070288634
    4
    96.63543171238714016093718179
    5
    120.7942896404839252011714772
    6
    144.9531475685807102414057727
    7
    169.1120054966774952816400681
    8
    193.2708634247742803218743636
    9
    217.4297213528710653621086590
    >>>

    from decimal import Decimal
    The method test the equality tow Decimal equations.
    from decimal import Decimal

    x=(Decimal(1).logical_and(Decimal(1))).__sub__(Decimal(0).logical_and(Decimal(1)))
    y=(Decimal(1).logical_and(Decimal(1))).__mul__(Decimal(0).logical_and(Decimal(1)))
    print(x.__eq__(y))

    >>> 
    False
    >>>


    from decimal import Decimal
    The method return with the difference in the set items:
    The tkinter.Entry is used to check the string characteristics through string methods.

    master=Tk()
    master.title("String methods")

    def cap():
        try:
            c=str.capitalize(s.get())
            s.set(c)
            
            pass

    def cfold():
        try:
            c=str.casefold(s.get())
            s.set(c)
            
        except ValueError:
            pass

    def ifier():
        try:
            if str.isidentifier(s.get())==True:
                s.set("Yes")
            
            else:
                s.set("No")
            
        except ValueError:
            pass

    def ipace():
        try:
            if str.isspace(s.get())==True:
                s.set("Yes")
            
            else:
                s.set("No")

        except ValueError:
            pass

    def iric():
        try:
           if str.isnumeric(s.get())==True:
               s.set("Yes")
           else:
               s.set("No")        
                    
        except ValueError:
            pass

    def ower():
        try:
            if str.islower(s.get())==True:
                s.set("Yes")
            
            else:
                s.set("No")        
            
        except ValueError:
            pass

    def inum():
        try:
            if str.isalnum(s.get())==True:
                s.set("Yes")
            
            else:
                s.set("No")        
        except ValueError:
            pass

    def imal():
        try:
            if str.isdecimal(s.get())==True:
                s.set("Yes")
            
            else:
                s.set("No")
            
        except ValueError:
            pass

        

    def igit():
        try:
            if str.isdigit(s.get())==True:
                s.set("Yes")
            
            else:
                s.set("No")
            
        except ValueError:
            pass


    def clr():
        try:
            s.set("")
            
                  
        except ValueError:
            pass



    entry=Entry(master, textvariable=s)
    entry.grid(row=0, columnspan=4, sticky=W+E)


    button_capitalize=Button(master, text="capitalize", width=10, command=cap)
    button_capitalize.grid(row=2, column=0)

    button_casefold=Button(master, text="casefold",width=10, command=cfold)
    button_casefold.grid(row=2, column=1)

    button_isidentifier=Button(master, text="isidentifier",width=10,command=ifier)
    button_isidentifier.grid(row=2, column=2)


    button_isspace=Button(master, text="isspace",width=10, command=ipace)
    button_isspace.grid(row=3, column=0)

    button_endswith=Button(master, text="endswith", width=10, command=iric)
    button_endswith.grid(row=3, column=1)

    button_islower=Button(master, text="islower",width=10, command=ower)
    button_islower.grid(row=3, column=2)



    button_isalnum=Button(master, text="isalnum",width=10, command=inum)
    button_isalnum.grid(row=4, column=0)

    button_isdecimal=Button(master, text="isdecimal",width=10, command=imal)
    button_isdecimal.grid(row=4, column=1)

    button_isdigit=Button(master, text="isdigit",width=10, command=igit)
    button_isdigit.grid(row=4, column=2)

    button_clear=Button(master, text="Clear", command=clr )
    button_clear.grid(row=5, columnspan=3,sticky=W+E)











    Comments