A Python nyelv

Egy rövid bevezető

Viktor Nagy / The Pulilab Kft.

Első benyomás

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Egy minta

def hello():
    print 'Hello Világ'

class Hello(object):
    '''Ez az osztály köszön neked'''

	def __init__(self, name):
		self.name = name

	def sayIt(self):
		print 'Hello %s!' % self.name

mindenkinek = Hello('mindenkinek')
mindenkinek.sayIt()
Compared to C, Python programs are much shorter, and consequently much faster to write. In comparison with Perl, Python code is easier to read, write and maintain. Relative to TCL, Python is better suited for larger or more complicated programs.

Zen of Python

import this
  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Flat is better than nested.
  • Sparse is better than dense.
  • Readability counts.
  • Special cases aren't special enough to break the rules.
  • Although practicality beats purity.
  • Errors should never pass silently.
  • Unless explicitly silenced.
  • ...

Zen of Python

  • ...
  • In the face of ambiguity, refuse the temptation to guess.
  • There should be one-- and preferably only one --obvious way to do it.
  • Although that way may not be obvious at first unless you're Dutch.
  • Now is better than never.
  • Although never is often better than *right* now.
  • If the implementation is hard to explain, it's a bad idea.
  • If the implementation is easy to explain, it may be a good idea.
  • Namespaces are one honking great idea -- let's do more of those!

Python alapok

  • nagyon könnyen olvasható
  • cPython, ironPython, Jython, ...
  • szkriptnyelv
  • nem tipizált
  • széleskörű core modulok
  • általános programnyelv
  • gyakran embedded szkriptnyelv (pl: Blender, Gimp)

Python alapok

Példa

import os

mypath = os.path.join(__file__, '..', 'valami')
if mypath:
    print mypath

try:
    open(mypath, 'r')
except IOError, e:
    print 'hiba'

odd = filter(lambda x: x%2, range(10))

def generated(x):
    yield x

mygen = generated(range(10))
						

Python verziók

2.6, 2.7 vagy 3.1?

2.7

Sage

python az oktatásban

  • numerikus számítások
  • szimbolikus algebra
  • grafikonrajzolás
  • átjárás már programok felé (maxima, R, ...)

Sage

Egy példa

Fabric

python rendszergazáknak

  • előreprogramozott helyi és távoli parancsok
  • projektspecifikus parancsok

Fabric

Egy példa

#!/usr/bin/env python
# usage: fab staging deploy

from __future__ import with_statement
from fabric.api import local, settings, abort, run, cd, env, sudo
from fabric.contrib.console import confirm

# ...

def deploy():
    """Updates the server and restarts it"""
    clone_repo_if_needed()

    with cd(env.project_root):
        run_with_failure('git pull --all', 'Updating all repos failed.')
        run_with_failure('git checkout %s' % env.branch, 'Updating %s branch failed.' % env.branch)
        run_with_failure('git pull', 'Git pull failed.')
        #update submodules
        run("git submodule init")
        run("git submodule update")
            
        if env.just_cloned:
            create_virtualenv()

        run_with_failure(env.activate + "pip install -r requirements.txt", 'Installing requirements failed.')
        run_with_failure(env.activate + "python manage.py syncdb --migrate --noinput", "Syncdb failed.")
        run_with_failure(env.activate + "python manage.py collectstatic --noinput", "Collectstatic failed.")
        run("touch wsgi/staging.py")

Django

"web framework for perfectionists with deadlines"

  • macroframework (batteries included)
  • MVC
  • reusable apps

Django

egy példa kevés lenne

Merre tovább?

Hasznos python csomagok

Merre tovább?

IDE

  • Sublime Text 2
  • PyCharm - WebStorm
  • Eclipse + pyDev
  • Komodo
  • Vim
  • Emacs
  • ...

Vége

Kérdések?