Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, August 23, 2021

how to use timer to debug the performance issue in python?

 it is very common to encounter the performance issue. it is very easy to identfiy the function with such issue by adding the timer inside the function or put the timer before and after the function call.

here is a sample snippet to show how to use the time to track the function execution

import time

# @cache
def fib(n):
    if n<=1:
        return n
    else:
        return fib(n-1) +fib(n-2)

def main():
    start=time.perf_counter()
    for i in range(40):
        print(ifib(i))
    print("done")
    elapsed= time.perf_counter() -start
    print(f"completed in {elapsed} seconds")
        

if __name__ == '__main__':
    main()

the output is showing that the function was completed in 69.8057579 seconds.  it indicated that we should opitmize this function for better performance.


PS C:\code\python\python> & C:/Python39/python.exe c:/code/python/python/tutorial/cache/fibonacci_sequnce.py

0 0

1 1

2 1

3 2

4 3

5 5

6 8

7 13

8 21

9 34

10 55

11 89

12 144

13 233

14 377

15 610

16 987

17 1597

18 2584

19 4181

20 6765

21 10946

22 17711

23 28657

24 46368

25 75025

26 121393

27 196418

28 317811

29 514229

30 832040

31 1346269

32 2178309

33 3524578

34 5702887

35 9227465

36 14930352

37 24157817

38 39088169

39 63245986

done

completed in 69.8057579 seconds

PS C:\code\python\python>

how can we improve the performance of this function? Cache will dramatically improve the execution time.

here is the improved version 

from functools import cache
import time

@cache
def fib(n):
    if n<=1:
        return n
    else:
        return fib(n-1) +fib(n-2)

def main():
    start=time.perf_counter()
    for i in range(40):
        print(ifib(i))
    print("done")
    elapsed= time.perf_counter() -start
    print(f"completed in {elapsed} seconds")
        

if __name__ == '__main__':
    main()

done

completed in 0.0112849 seconds

the execution time is less than 1 seocond which is huge change comparing to the previous function will cost 89 second to complete.







Saturday, July 31, 2021

how to setup template folder for Django development?

 there are two ways to setup the template folder for django development.

first we can setup the path that Django framework can automatically recognize it..

under the component folder, we have to create the a template folder, then create a folder with name matching the component name.

for example we have a lead component, then the structure of the project should look like as following








in the view.py, we can implement with the code snippet below

from django.shortcuts import render

# Create your views here.

def home_page(request):
    return render(request"leads/home_page.html")

second. we can modify the DIRS attribute in  TEMPLATES setting section in the settings.json to map to the templates path.

TEMPLATES = [
    {
        'BACKEND''django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS'True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

then we only have to create a templates folder under the current component folder. we can add the template html in the template folder.







in the view.py we use the template page directly.

def home_page(request):
    return render(request"home_page.html")








Friday, July 30, 2021

how to fix "Import "django.contrib.auth" could not be resolved from sourcePylance" in Django application development?

 when I try to import a library to the project. I spot a curly underline for the library that i just imported.

I hovered the mouse to the underneath, the warming message as following

    "Import "django.contrib.auth" could not be resolved from sourcePylance"


the root cause of this issue is related to the VS Code workspace configuration. we have to add this line to get ride of the error message.

{
    "python.pythonPath""/venv/bin/python",
}

Since I did not have the .vscode folder when I setup my django project with command line window.

I have to manually create a .vscode folder under the project and add the settings.json file inside it.

I put the above setting in the settings.json file.  The warming message finally resolved.










update: if the above solution still doesn't work

you can change the python interpreter for the current project by doing the following

  • use Ctrl-Shift-p to open command palette
  • type python interpreter in the command palette
  • select Enter interpreter path
  • find the python.exe file under your venv\scripts folder


Monday, July 26, 2021

how to implement dynamic routing in Flask

 When we want to pass into a variable on routing. we can use the following code to pass the variable <routeVariable> in the route path.

@app.route("/mypage/<username>"

def  my_page(username):

        return f"<h1>Hello {username}'s page<h1>"


the output is 

hello dan's page

how to force a flask web app to run on specific port in local development?

 when I execute flask run to launch the flask web app in my laptop. I usaully encoutered an error as following.

    "OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions " 

since the windows firewall had block this port.

I can assign the port during the launch to void this issue. i assing 300 for this app.

flask run -h localhost -p 3000 

Saturday, July 24, 2021

Python Quick Reference

It is a quick reference for seasoned developer, who already master at least one programming language. for myself, I have worked with C# for more than 10+ years. this reference can help you easily translate from other language to python

Naming Variables : camel Case

    myVariable

Data Types: 

    Text Type: 

         str "hello world"

    Numeric Types: 

        int : (10),

        long :(101L)

        float: (10.01), 

        complex: (3j)

    Sequence Types:

        list: ["a","b","c","d"], 

        tuple: immutable 

        myTuple = ("a","b","c","d")  

       tuple[0]="New assignment"  TypeError: 'tuple' object does not support item assignment

        range: generate a sequence of integer

        range(5) : 0 1 2 3 4

        range(0, 5, 2) : 0 2 4

    Mapping Type: 

        dictionary: list of key value pairs

        myDictionary={"key1:value1, "key2:value2 }

    Set Types:

    set 

    frozenset

Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

Comments

    # single comment

    """

    multiple line comments

    another line comments

    """

Strings

Multiline and Formatting Strings

Indentation

Arithmetic Operators

Comparison Operators

    <, >, <=, >=, ==, !=, <>

Logical Operators

    and, or, not

Assignment Operators

    a=b assign value from right to left

    a +=b a=a+b

    a -=b a=a-b

   a *=b a=a*b

    a /=b a=a/b

    a **=b a=a**b

    a //=b   a=a//b   10 //=3   => 10//3=3

 If Statements

    if condition:

    elif condition:

    else:

Ternary If Statements

    message= "true condition" if condition else "false condition"

Lists

    operators:

        Length: len([1, 2, 3] => 3

        Concatenation: ["a", "b", "c"] + [1, 2, 3]  => ["a", "b", "c", 1, 2, 3]

        Repetition: [a, b] * 3 => ["a", "b", "a", "b", "a", "b"]

        Slicing [1: ]   myList=[1,  2,  3] myList[1:] => [2, 3]

        Indexing :

        myList=[1,  2,  3]  

        offsets starting from zero:  myList[1] => 2

         if negative counting from the right side:  myList[-1] => 3

      Built in List methods 

          max(myList) =>3

          min(myList) => 1

         list: convert  iterable object into a list

         list("abcd")=>[a, b, c, d]

         list(123456) TypeError: 'int' object is not iterable

         list("123456") => [1, 2, 3, 4, 5 ,6]

         list({12, "ab", [1, 3]}) => [12, "ab", [1, 3]]

Useful List Methods

  • append : add item the end of the list
  • count: find the number of occurrence of object
  • extend: append iterable object to the list
  • insert: add item to the offset index
  • pop: return the item from the list
  • remove: remove item from the list
  • reverse: reverse the order of items in the list
  • sort: sort items in the list

Deleting Items from Lists

    del list(obj) exactly like list.remove(obj)

Sets

    not allow to have duplicate item

Set Union Intersection & Difference

    union : setA | setB

    intersect: setA & setB

    differece: setA - setB

    symmetric_difference: setA ^ setB

Dictionaries

    key value pairs data structure

   myDict ={

        "key1" : value1, 

        "key2" : value2,

        "key3" : value3

}

value1=myDict["key1"]

    Functions:

        len:

        

    Methods:

For Loops

    for item in list or set:

        print(item)

Loop Through Dictionaries

    for key in dict: 

        print(f"key: {key} value: {dict[key]}")

While Loop

    number=0

    while number < 3

         number +=1

Break and Continue

    break: exist the the loop if condition meets

              number=0

                while number < 3

                     if number==2

                        break;

                     number +=1

    continue: continue the loop if the condition is true

                       number=0

                            while number < 3

                     if number <=2

                            continue;

                            number +=1

Functions 

    def myfunction():

        print("this  is my first function")

Built in Functions and Import Statement

    import python module

        import math

    import specific method from the module

        from math import floor

Creating Modules

        create a python file myModule.py

        in another file use import myModule to reference the moudule

         printing all variables and function names of the "myModule" module

          dir(myModule)

Classes and Objects

    classes are the blueprints of objects

    objects are the instances of classes

Creating Classes and Objects

     class person

        #define the constructor

        def __init__(self, firstName,lastName):

                self.fistName=firstName

                self.lastName=lastName 

     myObject=person("first name", "last name")

 Inheritance

    class driver(person)  #driver is inherited from person

Printing Objects

    # override method

    def __str__(self) -> str:

           return f"last name: {self.firstName}, last name: {self.lastName}"

Working With Dates

    from datetime import datetime #work with datetime

    from datetime import date    #work with actual date

Formatting Dates

    now=datetime.now()

    now.strftime("datetime string formatter") 

Creating Files

    file =open("./myfile.txt", "w")      # create a text file myfile.txt in the current working dir

    flags: a -> append

              r -> read

              r+ -> read and write

              w -> write

    file.close() # to properly close the file being read or write    

Reading From Files

     file =open("./myfile.txt", "r") 

     file.read()

     file.close()

A Better Way To Work With Files

    import os.path

    if(os.path.isfile(myfile.txt):  #ensure the file exists before read write operations

            with open("./myfile.txt", "r")  as file

                file.read()

Fetching Data From Internet

     import json

    from urllib import request

    response = request.urlopen(("https://fakerapi.it/api/v1/companies?_quantity=5"))

    resjson = json.loads(response.read())

    print(resjson["data"])

Pip & Modules

    pip3: python package manager

    pip3 install requests

 Request Module

    import json

    import requests

    response = requests.get(("https://fakerapi.it/api/v1/companies?_quantity=5"))

    print(response.text)

    jsonData = json.loads(response.text)

    print(jsonData["data"])

Text To Speech

  pip3 install pyttsx3

   import pyttsx3

   pyttsx3.speak


Asterik * 

 * is argument-unpacking and ** keyword-argument-unpacking operators

The single star * unpacks the sequence/collection into positional arguments

The double start ** uses a dictionary and thus named arguments.



Thursday, February 27, 2020

Python 101

i joined a Harvard Online Introduction course using Python for Research.

it is really good to get to know a new programming language. here is some 101 fact for Python

1. [:]  is for Array slice which is a shadow copy of the array content

for example a=[1 , 2 , 3 , 4]

b=a   #a and b reference to the same object. however
b=a[:]   #a and b have same content. but reference to two different object due to shadow copy of array a.

2. global value vs local variable

  x=1 # which is a global value

   def printx();
     x=2  # x is a local variable which different from the global value above
    print(x) # print function will output 2 since the print function execute inside the printx fucntion.

print(x) # output will be 1 since global x is 1

another example shows that global and local variable do not interference each other

x=1
while x<5:
 x *=2

the final value of x is 4

3. # is the inline comment character, use Ctrl + / to comment out or uncomment highlight rows or multi line delimiter (""")


4. len([]) will only count distinct value only
len[1, 1, 2, 3, 3, 4] =4

5. a=["Hello, World"]
a[5:] will be equal to a.slice(5) which will be result in ", World"

6. the function block is identified the indentation
for example
def myFunc():
      #line 1
      #line 2
      #line 3 the function is end here

#this line is  out of function. same applies to for loop and while loop.
python use indentation for to indicate a block of code

Happy programming. enjoy the fun on learning a new language.