Skip to content
Chetabahana edited this page Apr 30, 2019 · 53 revisions

Table of Contents

Syntax

If-Else

Trap

myfunction(x, y):
    return x + y
else:
    print("Hello!")

if mark >= 50
    print("You passed!")

if flag:
    print("Flag is set!")
esle:
    print("Bye!")

Checks

# with checks

n = None
while n is None:
    s = input("Please enter an integer: ")
    if s.lstrip('-').isdigit():
        n = int(s)
    else:
        print("%s is not an integer." % s)

Try-Except

Block

def main():
    Do this
    Do that
    try:
        dividend = int(input("Please enter the dividend: "))
        divisor = int(input("Please enter the divisor: "))
        print("%d / %d = %f" % (dividend, divisor, dividend/divisor))
    except ValueError:
        print("The divisor and dividend have to be numbers!")
    except ZeroDivisionError:
        print("The dividend may not be zero!")

Specific

def main():
    Do this
    Do that
    try:
        dividend = int(input("Please enter the dividend: "))
    except ValueError:
        print("The dividend has to be a number!")

    try:
        divisor = int(input("Please enter the divisor: "))
    except ValueError:
        print("The divisor has to be a number!")

    try:
        print("%d / %d = %f" % (dividend, divisor, dividend/divisor))
    except ZeroDivisionError:
        print("The dividend may not be zero!")

Raise

def main():
    Do this
    Do that
    try:
        file = open('input-file', 'open mode')
    except:
        # In case of any unhandled error, throw it away
        raise

Finally

def main():
    Do this
    Do that
    try:
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Hey, that wasn't a number!")
    else:
        print("I see that you are %d years old." % age)
    finally:
        print("It was really nice talking to you.  Goodbye!")

Exceptions

def main():
    Do this
    Do that
    try:
        age = int(input("Please enter your age: "))
        if age < 0:
            raise ValueError("%d is not a valid age. Age must be positive or zero.")
    except ValueError as err:
        print("You entered incorrect age input: %s" % err)
    else:
        print("I see that you are %d years old." % age)

Logging

Console

Stackdriver

import logging

# log messages to a file, ignoring anything less severe than ERROR
logging.basicConfig(filename='myprogram.log', level=logging.ERROR)

# these messages should appear in our file
logging.error("The washing machine is leaking!")
logging.critical("The house is on fire!")

# but these ones won't
logging.warning("We're almost out of milk.")
logging.info("It's sunny today.")
logging.debug("I had eggs for breakfast.")

Debug Tools

Offline

Pdb

Pep8

Pylint

PyDev

PyDev adalah plug-in pihak ketiga untuk Eclipse yang digunakan untuk pemrograman dengan Python yang mendukung refactoring kode, debugging grafis, analisis kode di antara fitur-fitur.

Pyflakes

PyCharm

PyChecker

Online

Snyk

https://app.snyk.io

Travis

https://travis-ci.org/mirumee/saleor

Sentry

$ pip install --upgrade sentry-sdk==0.7.14

import sentry_sdk
sentry_sdk.init("https://xxxxxx@sentry.io/XXXXXX")

Codecov

https://codecov.io/gh/mirumee/saleor

Codeclimate

https://codeclimate.com/github/mirumee/saleor/pull/2626

Sourcegraph

Project Tutorial

You are on the wiki of our repo

Chetabahana Project

Clone this wiki locally