Python Programming Cookbook

所需积分/C币:10 2018-01-17 10:47:24 2.04MB PDF
收藏 收藏

新出来的python cookbook类学习书籍~~内容非常的好,唯一的缺点是英文的,估计国内翻译完出版这个也要好久了
Python Programming Cookbook i Contents 1 CSV Reader/Writer Example 1.I The basics 1.2 Reading and writing dictionaries 1.3 Download the Code Project 2 Decorator Tutorial 7 2.1 Understanding Functions 2.2 Jumping into decorators 8 2. 3 The Practice 2. 4 Download the Code project 14 3 Threading /Concurrency Example 15 3.1 Python thread module 15 3.2 Python threading module 16 3.2.1 Extending Thread 6 3.2.2 Getting Current Thread Information 17 3.2.3 Daemon threads l8 3.2.4 Joining threads 20 3.2.5 Time Threads 22 3.2.6 Events: Communication between Threads 3.2.7 Locking resources,,,..,.. 24 3.2.8 Limiting concurrent access to resources 3.2.9 Thread-Specific Data 4 Logging Example 31 4.1 The Theorv 4.1.1 Log Levels 41.2 Handlers 32 4.1.3 Format 2 4.2 The Practice 4.3 Download the Code project 39 Python Programming Cookbook s Django Tutorial 40 5.1 Creating the project 5.2 Creating our application 5.3 Database Setup 5. 4 The Public Page 5.5 Style sheets 47 5.6 Download the Code project 49 6 Dictionary Example 50 6.1 Define 50 6.2 Read 50 6.3 Write 52 6. 4 Useful operations 6.4.1 In(key word) 6.4.2 Len(built-in function) 6.4.3 keys and values 6.4.4 items 55 6.4.5 upd 6.4.6 copy... 6.5 Download the Code Project 57 7 Sockets Example 58 7. I Creating a Socket ....... .,58 7.2 USing a Socket 7.3 isconnecting 7.4 A Little Example Here 7.5 Non-blocking Sockets 7.6 Download the Code project 8 Map Example 8.1 Map Implementation 64 8.2 Pythons Map 65 8.3 Map object 68 8. 4 Download the Code Project g Subprocess Example 70 9.1 Convenience Functions 9.1.1 70 9.1.2 subprocess. check call 9.1.3 subprocess. check_output 9.2 Popen 72 9.3 Download the Code project 74 Python Programming Cookbook 10 Send Email example 10.1 The basics of smtplib 75 10. 2 SSL and authentication 76 10.3 Sending HTML 10.4 Sending Attachments 78 10.5 Download the Code Project 80 Python Programming Cookbook V Copyright (c) Exelixis Media P.C., 2016 All rights reserved. Without limiting the rights under copyright reserved above, no part of this publication may be reprcauced, stored cr introduced into a retrieval system, or transmitted, in any form or by any means (electronic, mechanical, photocopying, recording or otherwise), without the pricr written permission cf the copyright owner Python Programming Cookbook Preface ython is a widely used high-level, general-purpose, interpreted, dynamic programming language. Its design philosophy empha- sizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or proce dural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.(source:https:/en.wikipediaorg/wiki/python_%28programMIng_language%629) In this ebook, we provide a compilation of Python examples that will help you kick-start your own projects. We cover a wide range of topics, fron multi-threaded progranming to web development with Django. With our straightforward tutorials, you will be able to get your own projects up and running in minimum time Python Programming Cookbook About the author Sebastian is a full stack programmer, who has strong experience in Java and Scala enterprise web applications He is currently studying Computers Science in UBA (University of Buenos Aires)and working a full time job at a. com company as a Semi-Senior developer, involving architectural design, implementation and monitoring He also worked in automating processes(such as data base backups, building, deploying and monitoring applications) Python Programming Cookbook 1/80 Chapter 1 CSV Reader /Writer Example CSV(comma-separated values)is a standard for exporting and importing data across multiple formats, such as My sQL and excel It stores numbers and text in plain text. Each row of the file is a data record and every record consists of one or more fields which values are separated by commas. The use of the comma to separate every record's fields is the source of the name given to this standard Even with this very explicit name, there is no official standard CS Vs, and it may denote some very similar delimiter-separated values, which use a variety of field delimiters(such as spaces and tabs, which are both very popular), and are given thecSv extension anyway. Such lack of strict terminology makes data exchange very difficult some times RFC 4180 provides some rules to this format It's plain text · Consists of records Every record consists of fields separated by a single character delimiter Every record has the same sequence of fields But unless there is additional information about the provided file(such as if the rules provided by rFc were followed), data exchange through this format can be pretty annoying 1.1 The Basics Python has native support for CsV readers, and it's configurable (which, as weve seen, is necessary). There is a module csv which holds every thing you need to make a CsV reader/writer, and it follows RFC standards(unless your configuration overrides then, so by default it should read and write valid CSV files. So, let's see how it works sv-readerpy import Csv with open('my.Csv',/r+', newline-)as csv_File reader csv reader(csv file) for row in reader print (s=r(row) Here, we are importing csv and opening a file called my csv, then we call csv. reader passing our file as a parameter and then we print each row in our reader. If mv. csv looks like this. my. CSV Python Programming Cookbook 2/80 y irst column, my second column, my third column y =irst column. 2, my second column 2, y third column 2 Then, when you run this script, you will see the following output [ my first column',′ my secona CO1umn,’ my third column’] 「 my first column2′, my second column2′; my third column21 And writing is just as simple as reading CSV-reader py with open('my. csv,/w+', newline=')as csv_file: riter (csv file for r writer. writerow(row) i th open(′ new Ine as Csv fil reader Csv. reader (csv file) for row in reader print (s=r(row)) Then, in your csv file you' ll see my. CSV And in your output It's pretty easy to see what is going on in here. We are opening a file in write mode, getting our writer from csv giving our file to it. and writing each row with it Making it a little smarter. ⊥ed (filemon fil reader Csv. reader(csv file) 「 ow for o der I def write(file locati with open(filelocation,wt, newline=) as csvfile ter(cav file) for row in def raw test ( columns int(input (" How many columns do you want to write? " True while k

试读 88P Python Programming Cookbook
立即下载 身份认证后 购VIP低至7折
Python Programming Cookbook 10积分/C币 立即下载
Python Programming Cookbook第1页
Python Programming Cookbook第2页
Python Programming Cookbook第3页
Python Programming Cookbook第4页
Python Programming Cookbook第5页
Python Programming Cookbook第6页
Python Programming Cookbook第7页
Python Programming Cookbook第8页
Python Programming Cookbook第9页
Python Programming Cookbook第10页
Python Programming Cookbook第11页
Python Programming Cookbook第12页
Python Programming Cookbook第13页
Python Programming Cookbook第14页
Python Programming Cookbook第15页
Python Programming Cookbook第16页
Python Programming Cookbook第17页
Python Programming Cookbook第18页

试读结束, 可继续读2页

10积分/C币 立即下载