background preloader

Full Stack Python

Full Stack Python

The Python I Would Like To See written on Saturday, August 16, 2014 It's no secret that I'm not a fan of Python 3 or where the language is currently going. This has led to a bunch of emails flying my way over the last few months about questions about what exactly I would prefer Python would do. So I figured I might share some of my thoughts publicly to maybe leave some food for thought for future language designers :) Python is definitely a language that is not perfect. However I think what frustrates me about the language are largely problems that have to do with tiny details in the interpreter and less the language itself. I want to take you on a journey that starts with a small oddity in the interpreter (slots) and ends up with the biggest mistake in the language design. In general though these posts will be an exploration about design decisions in the interpreter and what consequences they have on both the interpreter and the resulting language. Language vs Implementation Slots So what's a slot? If we do however a.

使用模块 - 廖雪峰的官方网站 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用。 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env python # -*- coding: utf-8 -*- ' a test module ' __author__ = 'Michael Liao' import sys def test(): args = sys.argv if len(args)==1: print 'Hello, world!' elif len(args)==2: print 'Hello, %s!' 第1行和第2行是标准注释,第1行注释可以让这个hello.py文件直接在Unix/Linux/Mac上运行,第2行注释表示.py文件本身使用标准UTF-8编码; 第4行是一个字符串,表示模块的文档注释,任何模块代码的第一个字符串都被视为模块的文档注释; 第6行使用__author__变量把作者写进去,这样当你公开源代码后别人就可以瞻仰你的大名; 以上就是Python模块的标准文件模板,当然也可以全部删掉不写,但是,按标准办事肯定没错。 后面开始就是真正的代码部分。 你可能注意到了,使用sys模块的第一步,就是导入该模块: import sys 导入sys模块后,我们就有了变量sys指向该模块,利用sys这个变量,就可以访问sys模块的所有功能。 sys模块有一个argv变量,用list存储了命令行的所有参数。 运行python hello.py获得的sys.argv就是['hello.py']; 运行python hello.py Michael获得的sys.argv就是['hello.py', 'Michael]。 最后,注意到这两行代码: if __name__=='__main__': test() 当我们在命令行运行hello模块文件时,Python解释器把一个特殊变量__name__置为__main__,而如果在其他地方导入该hello模块时,if判断将失败,因此,这种if测试可以让一个模块通过命令行运行时执行一些额外的代码,最常见的就是运行测试。 我们可以用命令行运行hello.py看看效果: $ python hello.py Hello, world! 如果启动Python交互环境,再导入hello模块: 别名 作用域

The Python I Would Like To See This post is surprisingly confused, it is phrased as a complaint about the language, then immediately degrades into CPython implementation specifics that have little bearing on the usability of the language itself. Ronacher should also know better than to post microbenchmarks like the one provided here, especially without corresponding (C) profiler output. At the C level, slots allow the implementation constant-time access to the most common code paths for an object, and especially when you have C code calling other C code via the type system (IMHO the primary use for Python, and still its strongest use case), "interpreter overhead" is reduced to a few extra memory indirection operations. In the alternative world, sure, perhaps some microbenchmark may behave faster, but now systemically, and for e.g. Python is all about providing a lightweight way to compose bits of fast code (the kernel, network stack, NumPy, MySQL, whatever).

Python操作Mysql实例代码教程(查询手册) | 疯狂的蚂蚁 本文介绍了Python操作MYSQL、执行SQL语句、获取结果集、遍历结果集、取得某个字段、获取表字段名、将图片插入数据库、执行事务等各种代码实例和详细介绍,代码居多,是一桌丰盛唯美的代码大餐。 实例1、取得MYSQL的版本 在windows环境下安装mysql模块用于python开发,请见我的另一篇文章: MySQL-python Windows下EXE安装文件下载 # -*- coding: UTF-8 -*- # 安装MYSQL DB for python import MySQLdb as mdb con = None try: # 连接mysql的方法:connect('ip','user','password','dbname') con = mdb.connect('localhost', 'root', 'root', 'test'); # 所有的查询,都在连接con的一个模块cursor上面运行的 cur = con.cursor() # 执行一个查询 cur.execute("SELECT VERSION()") # 取得上个查询的结果,是单个结果 data = cur.fetchone() print "Database version : %s " % data finally: if con: # 无论如何,连接记得关闭 con.close() 执行结果: Database version : 5.5.25 实例2、创建一个表并且插入数据 主要还是在cursor上面执行execute方法来进行,请见源码: 运行结果(在phpmyadmin中查看,在mysql命令行查看结果是一样的): 实例3、python使用slect获取mysql的数据并遍历 这个恐怕是用的最多的了,请速看代码: 运行结果: (1L, ‘Jack London’) (2L, ‘Honore de Balzac’) (3L, ‘Lion Feuchtwanger’) (4L, ‘Emile Zola’) (5L, ‘Truman Capote’) 上面的代码,用来将所有的结果取出,不过打印的时候是每行一个元祖打印,现在我们使用方法,取出其中的单个数据: 实例4、使用字典cursor取得结果集(可以使用表字段名字访问值) 实例5、获取单个表的字段名和信息的方法 结果: 实例7、把图片用二进制存入MYSQL

Minecraft: Pi Edition- How to Use Python @Raspberry_pi #piday #raspberrypi August 15, 2014 AT 2:00 am This tutorial from MakerSpace UK is a great intro into using python and your Raspberry Pi with Minecraft: First, we have to do a bit of setting up. To keep our code separate to the original code in the MCPI folder, we will make a new directory to keep all of our code in. In LXTerminal, type mkdir python This will make a directory called python. To use the MCPI API we will have to copy that folder over to our new directory. Read more. Each Friday is PiDay here at Adafruit! Related No comments yet. Adafruit has a "be excellent to each other" comment policy.

python之import机制_AncyLQ_新浪博客 1. 标准 import Python 中所有加载到内存的模块都放在 sys.modules 。 一个模块不会重复载入。 2. 1)顺序嵌套 例如:本模块导入 A 模块(import A),A 中又 import B,B 模块又可以 import 其他模块…… 这中嵌套比较容易理解,需要注意的一点就是各个模块的 Local 名字空间是独立的。 2)循环嵌套 例如:文件[ A.py ] from B import D class C:pass 文件[ B.py ] from A import C class D:pass 为什么执行 A 的时候不能加载 D 呢? 如果将 A.py 改为:import B 就可以了。 这是怎么回事呢? RobertChen:这跟Python内部 import 的机制是有关的,具体到 from B import D,Python 内部会分成几个步骤: (1)在 sys.modules 中查找符号 “B” (2)如果符号 B 存在,则获得符号 B 对应的 module 对象。 从 <modult B> 的 __dict__ 中获得符号 “D” 对应的对象,如果 “D” 不存在,则抛出异常。 (3)如果符号 B 不存在,则创建一个新的 module 对象 <module B>,注意,此时,module 对象的 __dict__ 为空。 执行 B.py 中的表达式,填充 <module B> 的 __dict__。 从 <module B> 的 __dict__ 中获得 “D” 对应的对象,如果 “D” 不存在,则抛出异常。 所以这个例子的执行顺序如下: 1、执行 A.py 中的 from B import D 由于是执行的 python A.py,所以在 sys.modules 中并没有 <module B> 存在, 首先为 B.py 创建一个 module 对象 (<module B>) , 注意,这时创建的这个 module 对象是空的,里边啥也没有, 在 Python 内部创建了这个 module 对象之后,就会解析执行 B.py,其目的是填充 <module B> 这个 __dict__。 3、再次执行A.py中的from B import D 这时,由于在第1步时,创建的ZQ:图解 3.

Numerical Methods With Python MOOC Starts Today An interesting sounding MOOC that will help students implement numerical solution methods in well-designed Python programs starts on August 18. The course was announced at last months SciPy (Scientific Python) conference where Professor Lorena A Barba, who will be one of four instructors for the MOOC, MAE6286: Practical Numerical Methods with Python, delivered using Open edX software. The MOOC is taking place in conjunction with four on-campus credit bearing classes for first-year graduate students at George Washington University (Washington, DC, USA) King-Abdullah University of Science and Technology (Saudi Arabia) University of Southampton (UK) Pontifical Catholic University of Chile (Santiago, Chile) It is described as: a groundbreaking example of inter-institutional (across four continents!) The decision not to go with Coursera, edX or any similar MOOC providers is a deliberate one, in keeping with its ideals of open education: Over the course of the MOOC, students will learn to:

Django documentation Everything you need to know about Django. Performance and optimization There are a variety of techniques and tools that can help get your code running more efficiently - faster, and using fewer system resources. Performance and optimization overview Geographic framework GeoDjango intends to be a world-class geographic Web framework. Having trouble? Try the FAQ — it's got answers to many common questions. The History of Python 比较详细Python正则表达式操作指南(re使用)_python_脚本之家 Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式。Python 1.5之前版本则是通过 regex 模块提供 Emecs 风格的模式。Emacs 风格模式可读性稍差些,而且功能也不强,因此编写新代码时尽量不要再使用 regex 模块,当然偶尔你还是可能在老代码里发现其踪影。 就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现。 正则表达式模式被编译成一系列的字节码,然後由用 C 编写的匹配引擎执行。 正则表达式语言相对小型和受限(功能有限),因此并非所有字符串处理都能用正则表达式完成。 简单模式 我们将从最简单的正则表达式学习开始。 有关正则表达式底层的计算机科学上的详细解释(确定性和非确定性有限自动机),你可以查阅编写编译器相关的任何教科书。 字符匹配 大多数字母和字符一般都会和自身匹配。 这个规则当然会有例外;有些字符比较特殊,它们和自身并不匹配,而是会表明应和一些特殊的东西匹配,或者它们会影响到 RE 其它部分的重复次数。 这里有一个元字符的完整列表;其含义会在本指南馀下部分进行讨论。 我们首先考察的元字符是"[" 和 "]"。 元字符在类别里并不起作用。 你可以用补集来匹配不在区间范围内的字符。 也许最重要的元字符是反斜杠"""。 一些用 """ 开始的特殊字符所表示的预定义字符集通常是很有用的,象数字集,字母集,或其它非空字符集。 \d 匹配任何十进制数;它相当于类 [0-9]。 \S 匹配任何非空白字符;它相当于类 [^ "t"n"r"f"v]。 \w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。 这样特殊字符都可以包含在一个字符类中。 本节最後一个元字符是 . 。 重复 正则表达式第一件能做的事是能够匹配不定长的字符集,而这是其它能作用在字符串上的方法所不能做到的。 我们讨论的第一个重复功能的元字符是 *。 举个例子,ca*t 将匹配 "ct" (0 个 "a" 字符), "cat" (1 个 "a"), "caaat" (3 个 "a" 字符)等等。 象 * 这样地重复是“贪婪的”;当重复一个 RE 时,匹配引擎会试着重复尽可能多的次数。 一步步的示例可以使它更加清晰。 RE 的结尾部分现在可以到达了,它匹配 "abcb"。 还有更多的限定符。 使用正则表达式 #!

Probably Overthinking It: Regression with Python, pandas and StatsModels I was at Boston Data-Con 2014 this morning, which was a great event. The organizer, John Verostek, seems to have created this three-day event single-handedly, so I am hugely impressed. Imran Malek started the day with a very nice iPython tutorial. And Imran very kindly let me use his laptop to project slides for my talk, which was next. Regression is a powerful tool for fitting data and making predictions. As an example, I will use data from the National Survey of Family Growth to generate predictions for the date of birth, weight, and sex of an expected baby. This talk is appropriate for people with no prior experience with regression. And here are my slides: The material for this talk is from the second edition of Think Stats, which is in production now and scheduled for release in early November. As I expected, I prepared way more material than I could present. The nice people at O'Reilly Media sent over 25 copies of my book, Think Python, so we had a book signing after the talk.

Related: