Ir al contenido principal

Python - Basics

Download

python.org

Basics commands

dir(__builtins__)   
Show all the defined functions

help(function_name) 
Show function description

print(string_text)

(__name__)
print the scope in which the functionality is been executed

Define a function

  • Type word def follow by name function with the parameters
  • Type word return with the functionality
Ex:   def f(x):  return x**2

Testing

Inside your function description define the result expected inside the comments.

'''
>>> function_name(parameter_value)
result

>>> function_name(parameter_value_2)
result2

>>>
'''
To run the test cases type

import doctest
doctest.testmod()

You can also run the test cases after your return statement, so each time you include your function the test cases are executed.

Unittest

You can also use Unit Test 
  1. Import unittest
  2. Create a class inherits from unittest.TestCase
    • class TestMain(unittest.TestCase):
  3. Create your test case 
    • def test_name(self):
  4. Use self.assertEqual()
  5. Execute the test cases with unittest.main(exit=False)

Consideration for test cases

Take in consideration these cases:
  • Size
  • Dichotomies 
    • True/False
    • Even/Odd
  • Boundaries
  • Order

Define a class

  1. Type the name of the class and inside the parenthesis the class you want to import
  2. Define your default constructor
  3. Define your methods

class MyClass(str):

    def __init__(param1, param2):
        self.var1 = param1

    def paly(s):
        print (__name__)
        return len(s)

Define methods with default value in parameters

def method_name(param , param2=default_value):

Then you can call method in these ways:

  • method_name(2)  or 
  • method_name(2, 'aa')  

Algorithms 

Each function performs as one of the following functions
  • Lineal 
  • Exponential
  • Logarithmic
log2(n) is the number of times we divide n by 2 in order to reach 1

To evaluate a function python have the library cProfile


Exceptions 

Use the following syntaxis

try:

except  exceptionType:

except exceptionType as e:
   print (e)

As recommendation can use assert to verify preconditions 



Comentarios

Entradas populares de este blog

C# Using tabs

To use tabs in C# use the TabContainer element from AjaxControlToolkit Include AjaxControlToolkit  Include in the Web.config file, inside the tag <system.web> the following code  <pages>       <controls>         <add tagPrefix="ajaxCTK" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>       </controls>     </pages>   Include TabContainer element First  include TabContainer element that is the section where all the tabs will be displayed. <ajaxCTK:TabContainer ID="TabContainerUpdate" runat="server"                 Height="800"                 CssClass="ajax__tab_style"> </ajaxCTK:TabContainer> Second per each tab include the following code corresponding to each ...

Rails - Basic Steps III

pValidations Validations are a type of ActiveRecord Validations are defined in our models Implement Validations Go to   root_app/app/models Open files  *.rb for each model Mandatory field validates_presence_of   :field Ex:   validates_presence_of    :title Classes The basic syntax is class MyClass        @global_variable                def my_method              @method_variable        end end Create an instance myInstance = MyClass.new Invoke a mehod mc.my_method class() method returns the type of the object In Ruby, last character of method define the behavior If ends with a question -> return a boolean value If ends with an exclamation -> change the state of the object Getter / Setter method def global_variable       return @global_variable end ...

Python create package

Create a root folder Create a sub-folder "example_pkg" that contains the funtionallity packaging_tutorial/ example_pkg/ __init__.py In the root folder create the following structure  packaging_tutorial/ example_pkg/ __init__.py tests/ setup.py LICENSE README.md in the setup.py contains the configuration of the packages your package is found by find_packages() import setuptools with open ( "README.md" , "r" ) as fh : long_description = fh . read () setuptools . setup ( name = "example-pkg-YOUR-USERNAME-HERE" , # Replace with your own username version = "0.0.1" , author = "Example Author" , author_email = "author@example.com" , description = "A small example package" , long_description = long_description , long_description_content_type = "text/markdown" , url = "https://github.com/pypa/sam...