1 / 27

ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ

ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ. ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ ΕΠΛ37 1 – ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΣΥΣΤΗΜΑΤΩΝ Python ΕΑΡΙΝΟ ΕΞΑΜΗΝΟ 2009. Αγαθοκλέους Μάριος Γιάλλουρος Μιχάλης Γιαννή Παναγιώτα. Ιστορική Αναδρομή. Δημιουργός: Guido van Rossum Ανάπτυξη : 1990 , CWI του Άμστερνταμ και συνεχίζεται στο CNRI του Reston

chaela
Download Presentation

ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ ΕΠΛ371 – ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΣΥΣΤΗΜΑΤΩΝ Python ΕΑΡΙΝΟ ΕΞΑΜΗΝΟ 2009 Αγαθοκλέους Μάριος Γιάλλουρος Μιχάλης Γιαννή Παναγιώτα

  2. Ιστορική Αναδρομή • Δημιουργός: GuidovanRossum • Ανάπτυξη : 1990 , CWI του Άμστερνταμ και συνεχίζεται στο CNRI του Reston • Πρώτη εμφάνιση :1991 • Γλώσσα υψηλού επιπέδου • Διερμηνευόμενη (interpreted) • Χρήση interpreted, αντί για μεταγλωττιστή,  γρήγορη και ευέλικτη • Αλληλεπιδραστική (interactive) • Χρήση interpreted αλληλεπιδραστικά για δοκιμή ενός μέρους του κώδικα ή για εξερεύνηση της γλώσσας γράφοντας απλές εντολές στη γραμμή εντολών • Αντικειμενοστρεφής (object-oriented). Όπως Java ή C++ • Ανοικτό λογισμικό(freesoftware) • Αρχικά : γλώσσα scripting για το λειτουργικό σύστημα Amoeba

  3. Συνδυάζει σημαντική ισχύ με πολύ σαφή σύνταξη. • Χρήση: modules, classes, exceptions και πολύ υψηλού επιπέδου δυναμικούς τύπους δεδομένων. • Επέκταση : προσθέτοντάς καινούργια modules που να είναι γραμμένα σε μια γλώσσα που μεταγλωττίζεται, όπως C ή C++ • Τα modules επέκτασης μπορούν να ορίσουν νέα functions και μεταβλητές και καινούργιους τύπους δεδομένων. • Συγκρίνεται μ’ άλλες διερμηνευόμενες γλώσσες προγραμματισμού: Java, JavaScript, Perl, Tcl , Ruby και Smalltalk, αλλά συγκρίσεις μπορούν να γίνουν και με C++, CommonLisp και Scheme • Πλατφόρμες : Windows, Linux/Unix, OS/2, Mac, Amiga… Υπάρχουν εκδόσεις πού τρέχουν σε .NET,Javavirtualmachine και σε κινητά Nokia • Επίσημηιστοσελίδα Python : http://www.python.org • Καλύτερη scripting language 2008

  4. Πλεονεκτήματα • Διατίθεται δωρεάν και είναι Opensource • Υποστηρίζονται threads • Υποχρεωτικά στοιχισμένος κώδικας - χρήση τεσσάρων κενών διαστημάτων (whitespace) για τον διαχωρισμό των συντακτικών δομών του προγράμματος προγράμματα ευανάγνωστα • Δεν κάνει έλεγχο τωv τύπων στα ορίσματα που περνάμε στις συναρτήσεις και μπορούμε να χρησιμοποιήσουμε την ίδια συνάρτηση για διαφορετικά ορίσματα • Απλή • Εύκολη στη μάθηση • Ανεξάρτητη λειτουργικού συστήματος. Ο κώδικας πού γράφεται τρέχει χωρίς αλλαγές πάνω σε διάφορες πλατφόρμες

  5. Μεταβλητές : Δεν έχουν τύπους και δεν χρειάζονται δήλωση • Μπορεί να γίνει καταχώρηση σε αρκετές μεταβλητές ταυτόχρονα x, y, z = 1, 2, 3 • Χρήση standard library • Χρήση Exceptions αντιμετώπιση προβλημάτων που μπορούν να προκύψουν κατά την εκτέλεση του προγράμματος • Ένας προγραμματιστής σε Python μπορεί να είναι πολύ πιο αποδοτικός/παραγωγικός όταν γράφει σε Python παρά πχ σε Java • Dynamically-typed γλώσσα , κάθε μεταβλητή γίνεται bound μόνο σε ένα αντικείμενο παραγωγικός προγραμματισμός

  6. Μειονεκτήματα • Πιο αργή από τη C • Μεταβλητές : δεν έχουν τύπους οι μεταβλητές  δύσκολη κατανόηση προγράμματος • Θεωρείται συντακτικό λάθος η λανθασμένη στοίχιση του κώδικα δυσκολία γραφής κώδικα • Δεν είναι ιδιαίτερα αποδοτική/γρήγορη σε προγράμματα πολύ απαιτιτκά σε υπολογισμό

  7. Ποιοι οργανισμοί χρησιμοποιούν python • BitTorrent Client • Google • Yahoo! • NASA • YouTube

  8. Σχόλια για Python YouTube.com • "Python is fast enough for our site and allows us to produce maintainable features in record times, with a minimum of developers," said Cuong Do, Software Architect, YouTube.com. Google • "Python has been an important part of Google since the beginning, and remains so as the system grows and evolves. Today dozens of Google engineers use Python, and we're looking for more people with skills in this language." said Peter Norvig, director of search quality at Google, Inc.

  9. Εγκατάσταση Python • Ανάλογα με το λειτουργικό σύστημα χρειάζεται να γίνει download μόνο το κατάλληλο binary code για τηνπλατφόρμα. • http://www.python.org/download/ Windows Installation • Δύο επιλογές: 1. ActivePythonτο οποίο είναι ολοκληρωμένο version και περιέχει IDE with a Python-aware code editor, και κάποια Windows extensions για την Python που της επιτρέπουν να έχει complete access σε Windows-specific services, APIs, and the Windows Registry. 2. Οfficial Python installerπου αποτελεί και την πιο απλή έκδοση του installer • Μετά την εγκατάσταση Setting path at Windows: • Στο command prompt  path %path%;C:\Python

  10. Unix & Linux Installation • Για την πλατφόρμα Unix & Linux ακολουθούμε τα βήματα για το windows, κάνοντας Download κάποια επιπρόσθετα files • Εκτελούμε: ./configure script make make install • Μετά την εγκατάσταση  Setting path at Unix/Linux • Στο bash shell (Linux) export PATH="$PATH:/usr/local/bin/python" and press Enter

  11. Hello world σε Python Παράδειγμα 1: #python >>> print "Hello World!!!" Hello World!!! >>> Παράδειγμα 2: #!/usr/bin/python x=“Hello World!!!” print x

  12. Που μπορεί να χρησιμοποιηθεί η Python • Web and Internet Development • Database Access • Desktop GUIs • Network Programming(Socket) • Game and 3D Graphics • Software Development • Scientific and Numeric • Education • Information Security Industry

  13. HTTPConnection Objects • Υποστηρίζει εγγενώς τα πρωτόκολλα HTTP, POP3,SMTP, FTP και Telnet. • import httplib • >>> conn = httplib.HTTPConnection("www.python.org") • >>> conn.request("GET", "/index.html") • >>> r1 = conn.getresponse() • >>> print r1.status, r1.reason • 200 OK • >>> data1 = r1.read() • >>> conn.request("GET", "/parrot.spam") • >>> r2 = conn.getresponse() • >>> print r2.status, r2.reason • 404 Not Found • >>> data2 = r2.read() • >>> conn.close()

  14. Υποστήριξη threads • import thread #κλήση της βιβλιοθήκης thread import threadingimport timedef run_thread (threadname, count, sleeptime): """This is the thread function to be invoked."""print "I am thread", threadnamefor i in range (1, count + 1):print "%s: count = %s" % (threadname, i)time.sleep(sleeptime)thread.interrupt_main()if __name__ == "__main__":thread.start_new_thread(run_thread, ("Thread1", 5, 1))try:while 1:passexcept:print "Thread1 exited...." Output: I am thread Thread1Thread1: count = 1Thread1: count = 2Thread1: count = 3Thread1: count = 4Thread1: count = 5Thread1 exited....

  15. Database Access • Επιτρέπει την σύνδεση και επικοινωνία με βάσειςδεδομένων • MySQLdb : interface για σύνδεση στο MySQL database server • #!/usr/bin/python • import MySQLdb • # Open database connection • db = MySQLdb.connect( host = "localhost", • user = "testuser", • passwd ="test123", • db ="TESTDB") • # prepare a cursor object using cursor() method • cursor = db.cursor() • # execute SQL query using execute() method. • cursor.execute("SELECT VERSION()") • # Fetch a single row using fetchone() method. • data = cursor.fetchone() • print "Database version : %s " % data • # disconnect from server • db.close() Output: Database version : 5.0.45

  16. Python - GUI Programming (Tkinter) • Δημιουργία GUI applications εύκολα και γρήγορα. • Tkinter standard GUI library for Python. • Import the Tkinter module. • Create the GUI application main window. • #!/usr/bin/python • import Tkinter • top = Tkinter.Tk() • # Code to add widgets will go here... • top.mainloop() Output:

  17. Υλοποίηση Προγράμματος Web Crawler • Ανάκτηση urls από μία δοσμένη ιστοσελίδα • Κατά πλάτος διερεύνηση(Breadth First Traversal) του γράφου που εκφράζει τη δοσμένη ιστοσελίδα • Επίσκεψη ιστοσελίδων επίπεδο-επίπεδο με βάση το δοσμένο βάθος • Τύπωμα ιστοσελίδων που ανακτήθηκαν

  18. Ευκολίες/Δυσκολίες • Δυσκολίες • Πρώτη επαφή με γλώσσα – Δυσκολία υλοποίησης κώδικα • Δυσκολία υλοποίησης κώδικα λόγω στοίχισης • Αντικειμενοστρεφής γλώσσα • Ευκολίες • Χρήση έτοιμων συναρτήσεων • Online documentation για εύρεση έτοιμων βιβλιοθηκών

  19. Κώδικας • import sys • import re • import urllib2 • import urlparse • import optparse • import htmllib, formatter • import time • from cStringIO import StringIO • def parse_options(): • parser = optparse.OptionParser() • parser.add_option("-d", "--depth", • action="store", type="int", default=30, dest="depth", • help="Maximum depth to traverse") • opts, args = parser.parse_args() • if len(args) < 1: • parser.print_help() • raise SystemExit, 1 • return opts, args

  20. def log_stdout(msg): • print msg • def get_page(url, log): • try: • page = urllib2.urlopen(url) • except urllib2.URLError: • log("Error retrieving: " + url) • return '' • body = page.read() • page.close() • return body • def find_links(html): • writer = formatter.DumbWriter(StringIO()) • f = formatter.AbstractFormatter(writer) • parser = htmllib.HTMLParser(f) • parser.feed(html) • parser.close() • return parser.anchorlist

  21. class Spider: • def __init__(self,startURL, log=None): • self.URLs = set() • self.URLs.add(startURL) • self.include = startURL • self._links_to_p = set() • if log is None: • self.log = log_stdout • else: • self.log = log

  22. def run(self,urls): • for url in urls: • print url • self.process_page(url) • def process_page(self, url): • html = get_page(url, self.log) • self.URLs.add(url) • for link in find_links(html): • link = urlparse.urljoin(url, link) • if link[-1] == '/': • if link not in self.URLs: • self.URLs.add(link) • self._links_to_p.add(link) • else: • temp = link.replace('/', '.') • if temp[-5:] == ".html" or temp[-4:] == ".htm": • if link not in self.URLs: • self.URLs.add(link) • self._links_to_p.add(link) • else: • continue

  23. def clear(url): • while not len(url) == 0: • url.pop() • def main(): • opts, args = parse_options() • depth = opts.depth • startURL = args[0] • spider = Spider(startURL) • count = 0 • urls = set() • urls.add(startURL) • sTime = time.time() • while count <= depth: • spider.run(urls) • clear(urls) • while not len(spider._links_to_p) == 0: • urls.add(spider._links_to_p.pop()) • count=count+1 • eTime = time.time() • tTime = eTime - sTime • print "time taken %f" % tTime • if __name__ == "__main__": • main()

  24. Στατιστικά • Κώδικας: 113 γραμμές • Χρόνος Υλοποίησης: 24 ώρες • Χρόνος Εκτέλεσης: 5.156 sec

  25. Screenshot

  26. Συμπεράσματα • Μπορεί να χρησιμοποιηθεί για μεγάλες εφαρμογέςαφού υποστηρίζει modules, classes και είναι ευανάγνωστη • Η σύνταξή της είναι πολύ απλή άρα μπορεί κάποιοςνα προσαρμοστεί εύκολα • Έχει αρκετές ιδιοτροπίες, όπως την στοίχισης της • Μπορεί να συνδέσει κώδικες από διαφορετικέςγλώσσες • Μπορεί να αντικαταστήσει οποιαδήποτε γλώσσα προγραμματισμού, εάν η απόδοση δεν έχει σημασία

  27. Πηγές • http://docs.python.org/ • www.tutorialspoint.com/ • http://docs.python.org/tutorial/ • http://spinning-yarns.org/michael/misc/python-intro.pdf • http://www.stat.ucla.edu/~rosario/classes/07F/202a/python/index.html • http://www.ferg.org/projects/python_java_side-by-side.html#typing • http://www.daniweb.com/forums/thread20774.html • en.wikipedia.org • Learning Python‎Mark Lutz - 2007

More Related