Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all 9596 articles
Browse latest View live

reduce() in Python

$
0
0

The reduce(fun,seq) function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.This function is defined in “ functools ” module.

Working : At first step, first two elements of sequence are picked and the result is obtained. Next step is to apply the same function to the previously attained result and the number just succeeding the second element and the result is again stored. This process continues till no more elements are left in the container. The final returned result is returned and printed on console. # python code to demonstrate working of reduce()
# importing functools for reduce()
import functools
# initializing list
lis = [ 1 , 3, 5, 6, 2, ]
# using reduce to compute sum of list
print ("The sum of the list elements is : ",end="")
print (functools.reduce(lambda a,b : a+b,lis))
# using reduce to compute maximum element from list
print ("The maximum element of the list is : ",end="")
print (functools.reduce(lambda a,b : a if a>b else b,lis))

Output:

The sum of the list elements is : 17
The maximum element of the list is : 6 Using Operator Functions

reduce() can also be combined withoperator functions to achieve the similar functionality as with lambda functions and makes the code more readable.

# python code to demonstrate working of reduce()
# using operator functions
# importing functools for reduce()
import functools
# importing operator for operator functions
import operator
# initializing list
lis = [ 1 , 3, 5, 6, 2, ]
# using reduce to compute sum of list
# using operator functions
print ("The sum of the list elements is : ",end="")
print (functools.reduce(operator.add,lis))
# using reduce to compute product
# using operator functions
print ("The product of list elements is : ",end="")
print (functools.reduce(operator.mul,lis))
# using reduce to concatenate string
print ("The concatenated product is : ",end="")
print (functools.reduce(operator.add,["geeks","for","geeks"]))

Output

The sum of the list elements is : 17
The product of list elements is : 180
The concatenated product is : geeksforgeeks reduce() vs accumulate()

Both reduce() and accumulate() can be used to calculate the summation of a sequence elements. But there are differences in the implementation aspects in both of these.

reduce() is defined in “functools” module, accumulate() in “itertools” module. reduce() stores the intermediate result and only returns the final summation value. Whereas, accumulate() returns a list containing the intermediate results. The last number of the list returned is summation value of the list. reduce(fun,seq) takes function as 1st and sequence as 2nd argument. In contrast accumulate(seq,fun) takes sequence as 1st argument and function as 2nd argument. # python code to demonstrate summation
# using reduce() and accumulate()
# importing itertools for accumulate()
import itertools
# importing functools for reduce()
import functools
# initializing list
lis = [ 1, 3, 4, 10, 4 ]
# priting summation using accumulate()
print ("The summation of list using accumulate is :",end="")
print (list(itertools.accumulate(lis,lambda x,y : x+y)))
# priting summation using reduce()
print ("The summation of list using reduce is :",end="")
print (functools.reduce(lambda x,y:x+y,lis))

Output:

The summation of list using accumulate is :[1, 4, 8, 18, 22]
The summation of list using reduce is :22

This article is contributed by Manjeet Singh(S.Nandini) . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Python 101 Now FREE on Leanpub Permanently

$
0
0

After the amazing response I had when I made python 101 free for a few days a couple of months ago (see here , I have decided to make it free (or Pay What You Want) permanently on Leanpub (PDF, mobi and epub):

https://leanpub.com/python_101

Note: I am still selling it on Amazon, Gumroad and Lulu

Now you can check it for free any time you like. If you happen to like the book, I would appreciate it if you could drop a review of it over on Amazon or contact me about doing a Reader Testimonial on Leanpub since they don’t have an automatic way to leave one of those.

When I originally wrote the book, I noticed that there were few or no books available that described how to create executables of your code or distribute your code via Python’s Package Index (PyPI). Python 101 covers these topics as well as introducing the reader to Python’s standard library, how to install 3rd party packages and an introduction to some of the most popular 3rd party packages for Python, such as SQLAlchemy, requests and virtualenv.

Python 101 has 5 sections, 44 chapters and 295 pages .

Here’s some coupons for my other books in case anyone is interested:

Python 201: Intermediate Python http://leanpub.com/python201/c/reddit wxPython Cookbook http://leanpub.com/wxpythoncookbook/c/reddit

Error Descriptions for System Calls

$
0
0

Working with FUSE to build file systems means inevitably you have to deal with (or return) system call errors. The Go FUSE implementation includes helpers and constants for returning these errors, but simply wraps them around the syscall error numbers. I needed descriptions to better understand what was doing what. Pete saved the day by pointing me towards the errno.h header file on my Macbook. Some python later and we had the descriptions:

So that’s a good script to have on your local machine, since now I can just do the following:

$ syserr.py | grep EAGAIN 35: EAGAIN: Resource temporarily unavailable

To get descriptions for the various errors. However for Google reference, I’ll also provide them here:

数据分析必备书单,先马再说!

$
0
0
数据分析必备书单,先马再说!

一点号互联网早读课2017.1.24

欢迎到早读课投稿,投稿邮箱:mm@zaodula.com

有不少人留言希望我推荐数据分析的书单,刚好即将春节,无论是假日学习还是年后,都值得充电。读书最好的时候是学生时期,其次是现在。内容按照如何七周成为数据分析师的顺序。

数据分析是一门专业且跨越多个领域的学科,虽然我每篇公众号都足够篇幅(乃至我自己觉得Up嗦),可我还是得承认存在缺漏。如果有好书作为参考,对数据分析能力的成长更有帮助。

这份书单权作入门级推荐,如果大家有更好的欢迎留言说明。我不能保证全部看过,毕竟基础书没必要看几本,但我尽量做到客观。建议大家根据自己基础挑选,不要贪多。

大家多支持正版。

Excel《谁说菜鸟不会数据分析》
php?url=0FWYYGWsqp" alt="数据分析必备书单,先马再说!" />

知名度比较高的一套书,适合新手,优点是它和数据分析结合,而不是单纯地学习函数。学会函数适用的场景和过程比它本身更重要。

是否需要学习VBA是仁者见仁的答案。我个人不建议。Excel VBA的最大优势是适用性广,哪怕去其他行业其他职位,都离不开Excel,这时候它就是一个工作加分的亮点。但是在互联网行业,对数据分析师,VBA的性价比就不高了。

这里只推荐一本,因为我就翻过上面这本,还没全看…

数据可视化

数据可视化的书不多。市面上多以编程为主,面向新手和设计的教程寥寥无几。 如果只是了解图表,看Excel的书籍也管用。

《鲜活的数据》


数据分析必备书单,先马再说!

内容很丰富,涉及可视化的方方面面,也囊括更类编程语言和设计软件:python+JS+R+Excel。作者还有另外一本书《数据之美》。

可视化是一门侧重灵感的学科,有一种入门技巧是从他人设计中学习,从模仿开始,了解他人是如何设计的,这个网络上有大量的信息图可以参考。当然数据分析师更需要的是如何发现,别只学习展示。

英文足够好,可以看Edward Tufte的著作:《The Visual Display of Quantitative Information》、《Envisioning Information》、《Beautiful Evidence》。他是数据可视化的领军人物,他的理念是反对为艺术效果而混淆或者简化数据。暂时没有中文版。


数据分析必备书单,先马再说!

分析思维首推《金字塔原理》,金字塔原理有些人说它晦涩难懂,我认为是芭芭拉这个老太有骗稿费之嫌,本书包含了报告、写文、演讲等诸多内容。可以细看可以快看。另外还有一本同名案例集,有兴趣可以买。

另外麦肯锡相关的书籍还有《麦肯锡意识》《麦肯锡工具》《麦肯锡方法》等。

《深入浅出数据分析》


数据分析必备书单,先马再说!

深入浅出系列是对新手非常友好的丛书,用生动但Up嗦的语言讲解案例。厚厚的一本书翻起来很快。本书涉及的基础概念比较广,包含一点统计学知识,学下来对数据分析思维会有一个大概了解。

《精益数据分析》


数据分析必备书单,先马再说!

国外的精益系列一直以互联网创业作内容导向,本书也属于此类。如果是互联网行业相关,可以看看。它介绍了不同领域的指标,以及产品不同时期的侧重点。案例都是欧美,这部分做参考用。

接下来的几本,是兴趣向读物。《黑天鹅》能拓展思维,讲叙了不确定性。《思考的技术》,大前研一的著作,也是咨询类经典。如果对咨询向的分析感兴趣,还可以看BCG系列,或者刷CaseBook。《批判性思维》,则是教你如何形成理性思维。

SQL

数据库有很多种,常见有Oracle,mysql,SQL Server等。我推荐学习MySQL,这是互联网公司的主流数据库。以后学习Hadoop生态时,MySQL也是最接近Hive语法的语言。

MySQL不需要专门看书学习,因为数据分析师以查询为主,不需要考虑数据性能、数据安全和架构的问题。使用搜索引擎能解决90%的问题,我就是w3cschool学的。

《MySQL必知必会》


数据分析必备书单,先马再说!

如果真想买书看,可以看这本,适合新手向的学习,看基础概念和查询相关的章节即可。网络上大部分MySQL都是偏DBA的。

如果想深入,可以看《高性能MySQL》,对分析师没啥用。至于另外一个方向NoSQL,对入门者还是小众了些。

如果有余力,就学习正则表达式吧,清洗数据的工作就靠它了。

统计学

统计学是比较大的范围,分析师往后还需要学线性代数和矩阵、关系代数等。初学者不需要掌握所有公式定理的数学推导,懂得如何应用就行用。


数据分析必备书单,先马再说!

大概是最Up嗦的深入浅出系列,从卖橡皮鸭到赌博机的案例,囊括了常用的统计分析如假设检验、概率分布、描述统计、贝叶斯等。书本注重应用和趣味性,数学推理一般。


数据分析必备书单,先马再说!

国外的经典教材,已经出到第十二版了。国外教材都有丰富有趣的案例,所以读起来会比国内的轻松不少。如果你还在读书,不妨买这本看一看。

名字既然有商务与经济,所以书中辅以了大量的相关案例。书内容很多,看起来不会快,适合细读。

《The Elements of Statistical Learning》


数据分析必备书单,先马再说!

稍微有一些难度的英文书籍,属于进阶版统计学,国外很推崇。如果要往机器学习发展,这本书可以打下很好的基础。

以上书籍的难度是逐步递增的。统计学是机器学习的基础,是概率、矩阵等实际应用。现在已经有很多统计工具,Excel的分析工具库、传统行业的SPSS、SAS以及R、Python等,使用过程都不用计算推导,大学考试才会考,现在都是计算机解决,轻松不少。

业务知识

不同领域的业务知识都不一样,这里以互联网举例。


数据分析必备书单,先马再说!

增长黑客的概念就是随着这本书的畅销传播开来。增长黑客在国内即是数据分析+运营/产品的复合型人才。这本书好的地方在于拓展思路,告诉我们数据能够做什么,尤其是连AB测试都不清楚的新人。

实际涉及的业务知识不多,我推荐,是希望新人能够了解数据驱动的概念,这本算是我走上数据化运营的启蒙读物了。

《从零开始做运营》


数据分析必备书单,先马再说!

知乎亮哥的书籍,互联网所有的数据都是和运营相关的,如果是新手,就以此学习业务知识。如果已经工作很多,就略过吧。

《网站分析实战》


数据分析必备书单,先马再说!

互联网不再是网站的天下,但是移动端依旧有Web,我们在朋友圈看到的所有H5活动、第三方内容等,都是依托网页实现。网站的数据分析依旧有存在空间,网站的数据指标还是能够指导我们运营。

《数据挖掘与数据化运营实战》


数据分析必备书单,先马再说!

这本书涉及了数据挖掘,但是比较浅,可以作为数据分析师视野的承上启下,了解数据化运营的高级应用。特点是以阿里的实际工作相结合,可又因为保密原则不够详尽。

《数据实践之美》


数据分析必备书单,先马再说!

是各领域专家众筹完成的书本,比起传统的书籍,囊括范围更广。虽然没有深度讲解技术,但是各领域的案例都是一手资料,对业务的触类旁通理解有帮助。

业务知识我不再多推荐,以后我会通过公众号文章的形式讲解。因为从我看来,市面上也没有详尽介绍数据角度下的用户行为、产品运营的书籍,都是点到为止。这一块内容,尽量从工作中去学,收获才是最大的。

Python/R

欢迎来到数据分析的最后殿堂,Python和R都是大分支,基本是前面所有内容的实现。Python的学习以PY3为前提,毕竟2017年了,我实在想不出不用Python3的理由。

除了书籍,Python/R更多依靠博客和文档学习。Python的学习路径不陡峭,新手水平取决于查询能力,所以也请学会如何高效搜索。

《深入浅出Python》


数据分析必备书单,先马再说!

还是深入浅出系列,完全适合零基础的新人。需要注意的是,编程学习不同于其他知识,如果计算机基础不稳固,在使用中会遇到各类问题。知其然不知其所以然,这是本书缺点:能掌握,但是Bug比较多。

《Python学习手册》


数据分析必备书单,先马再说!

对于拥有编程基础的人,这本书系无巨细的有些Up嗦,不过对新人,可以避免不必要的坑。把它当作一本工具文档吧,当遇到不理解的内容随时翻阅。这是纸质书比电子书好的优势之一。

《利用Python进行数据分析》


数据分析必备书单,先马再说!

非新手向的书籍,成书较早,部分内容比较老旧。虽然学习中不会有问题,但很多Pandas函数已经有更优雅的写法了,例如df.query。每段代码都敲打一遍,千万行的数据清洗基本不会有大问题了。

《Python Cookbook》


数据分析必备书单,先马再说!

Python的进阶书,如果想要掌握更好的编程能力,这是一本经典,值得时时翻阅。注意,它更偏向程序员。

《R语言实战》


数据分析必备书单,先马再说!

R语言的入门书籍,从数据读取到各类统计函数的使用。虽然没有涉及机器学习,依靠这本书入门R是绰绰有余了。

《统计学:从数据到结论》


数据分析必备书单,先马再说!

这本书是将R语言和统计学结合的教材,可以利用这本书再复习一遍统计知识。缺点是书本后面的内容质量不如前部分。

到这里,入门书籍推荐完毕,当然好书不嫌多,例如《数学之美》、《集体智慧编程》、《统计学习方法》等,有兴趣不妨阅读。

上面的内容都吃透,不论是成为一名数据分析师,还是往后向机器学习、数据科学家、数据产品发展、都有了良好的基础。

Generate PDF from HTML in django using weasyprint

$
0
0

In most of the web development projects you might want to automate file generation, like for example placeorder confirmation receipts, payment receipts, that can be based on a template you are using.

The library we will be using is Weasyprint. WeasyPrint is to combine multiple pieces of information into an HTML template and then converting it to a PDF document.

The supported version are python 2.7, 3.3+

WeasyPrint has lot of dependencies, So this can be install it with pip.

pip install Weasyprint

Once you have installed WeasyPrint, you should have a weasyprint executable. This can be as simple:

weasyprint --version

This will Print WeasyPrint's version number you have installed.

weasyprint <Your_Website_URL> <Your_path_to_save_this_PDF> Eg: weasyprint http://samplewebsite.com ./test.pdf

Here i have converted "http://samplewebsite.com" site to an test.pdf.

Let we write sample PDF Generation: from weasyprint import HTML, CSS
HTML('http://samplewebsite.com/').write_pdf('/localdirectory/test.pdf',
stylesheets=[CSS(string='body { font-size: 10px }')])

This will also converts the page in to PDF, Here the change is we are writting custom stylesheet(CSS) for the body to change the font size using the "string" argument.

You can also pass the CSS File, This can be done using:

from django.conf import settings
CSS(settings.STATIC_ROOT + 'css/main.css')
Ex: HTML('http://samplewebsite.com/').write_pdf('/localdirectory/test.pdf',
stylesheets=[CSS(settings.STATIC_ROOT + 'css/main.css')])

You can also pass multiple css files to this stylesheets array

Generating PDF Using Template:

Let we create a basic HTML file, that we will use as a template to generate PDF:

templates/home_page.html
<html>
<head>
Home Page
</head>
<body>
<h1>Hello !!!</h1>
<p>First Pdf Generation using Weasyprint.</p>
</body>
</html>

Lets write a django function to render this template in a PDF:

from weasyprint import HTML, CSS
from django.template.loader import get_template
from django.http import HttpResponse
def pdf_generation(request):
html_template = get_template('templates/home_page.html')
pdf_file = HTML(string=html_template).write_pdf()
response = HttpResponse(pdf_file, content_type='application/pdf')
response['Content-Disposition'] = 'filename="home_page.pdf"'
return response

Here, we have used the get_template() function to fetch the HTML template file in the static root.

Finally, You can download your home_page.pdf

New Django Admin with DRF and EmberJS... What are the news?

$
0
0

A couple months ago, I published a post titled "Yes I want a new admin" . Now that more than a few weeks have gone by, I want to bring you up to speed on the progress.

In the original post, it was mentioned that, in order to achieve this goal, several libraries would be needed and that some of these libraries were already published.

Among them were DRF-auto-endpoint and djember-model .

During Django Under The Hood 's sprints, we got together with several people interested by the concepts of those two libraries, merged them in a signle library ( DRF-schema-adapter ) and added some functionalities. Other than being a merge of the two "old" libraries DRF-schema-adapter also brings the concept of adapters which make it possible to use the same library for different frontends.

DRF-schema-adapterpursues 3 main goals:

Making it as easy, fast and straight-forward to define an API endpoint as it would be to generate a ModelAdmin class. Letting developpers be DRYer by generating frontend code (models, resources, stores, ...) based on your DRF endpoints. Providing enough information to the client to be entirely dynamic.

Although there are still some improvements planned for the library, all 3 of these goals have been achieved.

As far as the DRF backend is concerned, another library has been introduced: DRF-Base64 which allows for base64-encoded files to be uploaded to DRF.

Regarding the frontend, last time I mentioned ember-cli-dynamic-model (to dynamically load frontend models and metadata generated by the backend) and even if I still consider this library in alpha stage, I have been using it successfully for a few months now, the next steps will be to cleanup the code and add some more tests before it can be considered stable.

Still on the frontend, another library has also been introduced: ember-cli-crudities .

Ember-cli-cruditiesis mostly a set of widgets as well as a change-list and a change-form that behave pretty closely to the original Django admin change-list and change-form .

So... where does it stand compared to the original goals? "Themability" of the admin

The new admin is based on bootstrap 3 and is therefore pretty easily skinnable. For the future, there are also plans to support material design . But a few pictures are better than words so let me share with you, a screenshot of the same form rendered with 3 different themes:

LevIT theme
New Django Admin with DRF and EmberJS... What are the news?
Bootstrap theme
New Django Admin with DRF and EmberJS... What are the news?
Django admin theme
New Django Admin with DRF and EmberJS... What are the news?

On those screenshots, you might also have noticed a few perks like the form-disposition (which doesn't require any templating) or the translatable fields (compatibility with django-modeltranslation ).

Multi-level nesting

This goal is also fully achieved out-of-the-box, here is another sample screenshot demontrating the capability (Company has a OneToMany relationship towards Employee which, in turn has a OneToMany relationship towards ContactMechanism):


New Django Admin with DRF and EmberJS... What are the news?

Once again, in this screenshot, you might have noticed a couple extra perks like tabular content and sortable inlines, both available out-of-the-box.

Dynamic update

This goal has also been achieved.


New Django Admin with DRF and EmberJS... What are the news?
New Django Admin with DRF and EmberJS... What are the news?
New Django Admin with DRF and EmberJS... What are the news?

As you can see on the first screenshot, on a brand new record, the employee I just added to the company is already available as "Default contact".

On the second and third screenshot, you can see an extra field dynamically added to the form depending on the selected "Sale Type".

And... what about all the functionalities of the existing admin?

Django's admin is packed with functionalities and there are probably some of that I've never heard of but as far as I can tell this DRF-EmberJS admin comes pretty close, from the top of my head, here are a few things that I know are still missing/should be improved:

i18n support for the interface better filter widgets not provided by django admin - frontend validation (right now form validation happens on the backend) "Save and add another" "Save as new" More field widgets python package installable through pip for the whole admin. Where can I try it?

All of the screenshots on this post have been taken from a sample application available at https://djembersample.pythonanywhere.com (Thanks PythonAnywhere ), you are more than welcome to try it out and play with it (as well as report bugs if you happen to find some).

You can also clone the git repository for the sample app and run it locally where you'll be able to make changes to the exposed models, create new ones or change the active theme.

If you are interested in knowing more about DRF-schema-adapter or ember-cli-crudities stay tuned for more posts about those libraries in a near future.

Have a great day!

Django Weblog: Django 1.11 alpha 1 released

$
0
0

Django 1.11 alpha 1 is now available. It represents the first stage in the 1.11 release cycle and is an opportunity for you to try out the changes coming in Django 1.11.

Django 1.11 has a medley of new features which you can read about in the in-development 1.11 release notes .

This alpha milestone marks a complete feature freeze. The current release schedule calls for a beta release in about a month and a release candidate about a month from then. We'll only be able to keep this schedule if we get early and often testing from the community. Updates on the release schedule schedule are available on the django-developers mailing list .

As with all alpha and beta packages, this is not for production use. But if you'd like to take some of the new features for a spin, or to help find and fix bugs (which should be reported tothe issue tracker), you can grab a copy of the alpha package fromour downloads page or on PyPI.

The PGP key ID used for this release is Tim Graham: 1E8ABDC773EDE252.

Django 1.10中文文档-第一个应用Part3-视图和模板

$
0
0

本教程上接 Django 1.10中文文档-第一个应用Part2-模型和管理站点 。我们将继续开发网页投票这个应用,主要讲如何创建一个对用户开放的界面。

概览

视图是Django应用中的一“类”网页,它通常使用一个特定的函数提供服务,并且具有一个特定的模板。例如,在博客应用中,可能有以下视图:

博客首页 ―― 显示最新发表的博客;

博客“详细”页面 ―― 每博客的链接页面;

基于年份的归档页面 ―― 显示特定年内所有月份发表过的博客;

基于月份的归档页面 ―― 显示特定月份内每天发表过博客;

基于日期的归档页面 ―― 显示特定日期内发表过的所有博客;

评论:处理针对某篇博客发布的评论。

在我们的投票应用中,我们将建立下面的四个视图:

Question首页 ―― 显示最新发布的几个Question;

Question“详细”页面 ―― 显示单个Question的具体内容,提供一个投票的表单,但不显示该议题的当前投票结果;

Question“结果”页面 ―― 显示特定的Question的投票结果;

投票功能 ―― 处理对Question中Choice的投票。

在Django中,网页的页面和其他内容都是由视图(views.py)来传递的(视图对WEB请求进行回应)。每个视图都是由一个python函数(或者是基于类的视图的方法)表示。Django通过对比请求的URL地址来选择对应的视图。

在你平时的网页上,你可能经常会碰到类似“ME2/Sites/dirmod.asp?sid=&type=gen&mod=Core+Pages&gid=A6CD4967199A42D9B65B1B”的url。庆幸的是Django支持使用更加简介的URL模式(patterns),而不需要编写上面那种复杂的url。

URL模式就是一种URL的通用模式 ―― 例如: /newsarchive/<year>/<month>/ 。

Django使用‘URLconfs’的配置来为URL匹配视图函数。 URLconf使用正则表达式将URL匹配到视图上。

本教程提供URLconfs基本使用,更多信息请参考 django.url

编辑视图

下面,让我们打开polls/views.py文件,添加下列代码:

# polls/views.py
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)

然后,在polls/urls.py文件中加入下面的url模式,将其映射到我们上面新增的视图。

# polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

现在去浏览器中访问“/polls/34/”它将运行detail()方法,然后在页面中显示你在url里提供的ID。访问“/polls/34/results/”和“/polls/34/vote/”,将分别显示预定义的伪结果和投票页面。

上面访问的路由过程如下:当有人访问“/polls/34/”地址时,Django将首先加载mysite.urls模块,因为它是settings文件里设置的ROOT_URLCONF配置文件。在模块里找到urlpatterns变量,按顺序对各项进行正则匹配。当它匹配到了^polls/,就剥离出url中匹配的文本polls/,然后将剩下的文本“34/”,传递给“polls.urls”进行下一步的处理。在polls.urls,又匹配到了 r’^(?P<question_id>[0-9]+)/$’ ,最终结果就是调用该模式对应的detail()视图,将34作为参数传入: detail(request=<HttpRequest object>, question_id='34') question_id=’34’的部分来自 (?P <question_id> [0-9]) 。使用模式周围的括号“捕获”该模式匹配到的文本,并将其作为参数发送到视图函数; ?P<question_id> 定义一个名字用于标识匹配的模式; [0-9]+ 是匹配一串数字的正则表达。

因为URL模式是正则表达式,你如何使用它们没有什么限制。 不需要添加像.html这样繁琐的URL ―― 除非你执意这么做,在这种情况下你可以这样做:

url(r'^polls/latest\.html$', views.index),

但是,不要这样做。这比较愚蠢。

编写拥有实际功能的视图

每个视图函数只负责处理两件事中的一件:返回一个包含所请求页面内容的HttpResponse对象,或抛出一个诸如Http404异常。该如何去做这两件事,就看你自己的想法了。

您的视图可以从数据库读取记录,也可以不读取。它可以使用模板系统:如Django的或第三方Python模板系统 或不。可以生成PDF文件,输出XML,即时创建ZIP文件,任何你想要的,使用任何你想要的Python库。Django只要求返回的是一个 HttpResponse 。 或者抛出一个异常。

为了方便,让我们使用 Part1 中介绍的Django自己的数据库API。 下面是一个新的index()视图,它显示系统中最新发布的5条questions记录,并用逗号分隔:

# polls/views.py
from django.http import HttpResponse
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)
# 保持其他的视图 (detail, results, vote) 不变

这里有一个问题:页面的设计被硬编码在视图中。 如果你想更改页面的外观,就得编辑这段Python代码。 因此,我们使用Django的模板系统,通过创建一个视图能够调用的模板,将页面的设计从Python中分离出来。

首先,在你的polls目录下创建一个叫做 templates的目录。Django将在这里查找模板。

项目的settings.py中的templates配置决定了Django如何加载渲染模板。将APP_DIRS设置为True。DjangoTemplates将在INSTALLED_APPS所包含的每个应用的目录下查找名为”templates”子目录。

在刚刚创建的templates目录中,创建另一个名为polls的目录,并在其中创建一个名为index.html的文件。换句话说,你的模板应该是polls/templates/polls/index.html。由于app_directories模板加载器如上所述工作,因此您可以在Django中简单地引用此模板为polls/index.html(省掉前面的路径)。

模板命名空间: 如果我们把模板直接放在polls/templates中(而不是创建另一个polls子目录),但它实际上是一个坏主意。 Django将选择它找到的名字匹配的第一个模板,如果你在不同的应用程序中有一个相同名称的模板,Django将无法区分它们。我们需要能够将Django指向正确的一个,确保这一点的最简单的方法是通过命名空间。也就是说,将这些模板放在为应用程序本身命名的另一个目录中。

将以下的代码放入模板文件:

# polls/templates/polls/index.html
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}

现在更新polls/views.py中的index视图来使用模板:

# polls/views.py
from django.http import HttpResponse
from django.template import RequestContext, loader
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = RequestContext(request, {
'latest_question_list': latest_question_list,
})
return HttpResponse(template.render(context))

该代码加载名为polls/index.html的模板,并传给它一个context。Context是一个字典,将模板变量的名字映射到Python对象。

然后你可以通过浏览器打开 http://127.0.0.1:8000/polls 查看效果。

快捷方式:render()

常见的习惯是载入一个模板、填充一个context 然后返回一个含有模板渲染结果的HttpResponse对象。Django为此提供一个快捷方式。 下面是重写后的index()视图:

# polls/views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)

注意,一旦我们在所有这些视图中完成这个操作,我们不再需要import loader和HttpResponse(如果您仍然有detail, results, and vote方法,您将需要保留HttpResponse)。

render()函数接受request对象作为其第一个参数,模板名称作为其第二个参数,字典作为其可选的第三个参数。它返回一个HttpResponse对象,含有用给定的context 渲染后的模板。

404错误

现在,让我们处理Question 详细页面的视图 ―― 显示Question内容的页面:

# polls/views.py
from django.http import Http404
from django.shortcuts import render
from .models import Question
# ...
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, 'polls/detail.html', {'question': question})

这里的新概念:如果具有所请求的ID的问题不存在,则该视图引发Http404异常。

我们将在以后讨论你可以在polls/detail.html模板文件里放些什么代码,但如果你想快点运行上面的例子,仅仅只需要:

# polls/templates/polls/detail.html
{{ question }} 快捷方式:get_object_or_404()

一种常见的习惯是使用get()并在对象不存在时引发Http404。Django为此提供一个快捷方式。 下面是重写后的detail()视图:

# polls/views.py
from django.shortcuts import get_object_or_404, render
from .models import Question
# ...
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})

get_object_or_404() 函数将一个Django模型作为它的第一个参数,任意数量的关键字参数作为它的第二个参数,它会将这些关键字参数传递给模型管理器中的get() 函数。如果对象不存在,它就引发一个 Http404异常。

为什么我们要使用一个辅助函数get_object_or_404()而不是在更高层自动捕获ObjectDoesNotExist异常,或者让模型的API 引发 Http404 而不是ObjectDoesNotExist?

因为那样做将会使模型层与视图层耦合在一起。 Django最重要的一个设计目标就是保持松耦合。 一些可控的耦合将会在django.shortcuts 模块中介绍。

还有一个get_list_or_404()函数,它的工作方式类似get_object_or_404() ―― 差别在于它使用filter()而不是get()。如果列表为空则引发Http404。

使用模板系统

回到我们投票应用的detail()视图。 根据context 变量question,下面是polls/detail.html模板可能的样子:

# polls/templates/polls/detail.html
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

模板系统使用点查找语法访问变量属性。在{{question.question_text}}的示例中,首先Django对对象问题进行字典查找。如果没有,它尝试一个属性查找 - 在这种情况下工作。如果属性查找失败,它将尝试列表索引查找。

方法调用发生在{% for %}循环中:question.choice_set.all被解释为Python的代码question.choice_set.all(),它返回一个由Choice对象组成的可迭代对象,并将其用于{% for %}标签。访问 模板指南 来了解更多关于模板的信息。

移除模板中硬编码的URLs

我们在polls/index.html模板中编写一个指向Question的链接时,链接中一部分是硬编码的:

<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

这种硬编码、紧耦合的方法有一个问题,就是如果我们想在拥有许多模板文件的项目中修改URLs,那将会变得非常麻烦。 但是,因为你在polls.urls模块的url()函数中定义了name 参数,所以你可以通过使用{% url %}模板标签来移除对你的URL配置中定义的特定的URL的依赖:

<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

它的工作原理是在polls.urls模块里查找指定的URL的定义。你可以看到名为‘detail’的URL的准确定义:

...
# the 'name' value as called by the {% url %} template tag
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
...

如果你想把polls应用中detail视图的URL改成其它样子比如 polls/specifics/12/,就可以不必在该模板(或者多个模板)中修改它,只需要修改 polls/urls.py:

...
# added the word 'specifics'
url(r'^specifics/(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
... URL name的命名空间

教程中的这个项目只有一个应用polls。在真实的Django项目中,可能会有五个、十个、二十个或者更多的应用。 Django如何区分它们URL的名字呢? 例如,polls 应用具有一个detail 视图,相同项目中的博客应用可能也有这样一个视图。当使用模板标签{% url %}时,人们该如何做才能使得Django知道为一个URL创建哪个应用的视图?

答案是在你的主URLconf下添加命名空间。 在mysite/urls.py文件中,添加命名空间将它修改成:

# mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]

现在将你的polls/index.html改为具有命名空间的详细视图:

# polls/templates/polls/index.html
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

欢迎关注微信公众号: Pythoner每日一报


Django 1.10中文文档-第一个应用Part3-视图和模板

Django Debug Toolbar

$
0
0

Django debug toolbar is a nifty little utility to allow you to examine what’s going on under the hood. It’s a fairly easy install and gives quite a lot of info.

Installation

I’m not going to waste your time (or mine) with details of how to install the debug toolbar. The instructions are here .

I will, however, point out that the “tips” page starts with “ The toolbar isn’t displayed! “, which helped me get running. My problem was a lack of <body> </body> tags on my template. (side note: I’m wondering if something like bootstrap would provide those surrounding tags automatically.)

Using The Toolbar

The use of the toolbar is pretty obvious. The information is pretty clearly laid out on each of the sections.

The section I found the most interesting was the SQL tab (shown below), which not only shows which queries were done for the given page, but also how long each took.
Django Debug Toolbar

The page I instrumented has a task which updates several fields in the database the first time it is loaded on any given date. Using this tab it was clear how much of the page load time was taken up in this update process.

Not only would this be handy for performance troubleshooting, but it’s also instructional to see which python statements turn into queries and how.

Conclusion

As a fan of development tools, Django Debug Toolbar certainly makes me happy not only for its features, but also its simplicity in use and design. I would definitely recommend it

Ship It Day Q1 2017

$
0
0

Last Friday, Caktus set aside client projects for our regular quarterly ShipIt Day. From gerrymandered districts to Rpython and meetup planning, the team started off 2017 with another great ShipIt.

Books for the Caktus Library

Liza uses Delicious Library to track books in the Caktus Library. However, the tracking of books isn't visible to the team, so Scott used the FTP export feature of Delicious Library to serve the content on our local network. Scott dockerized Caddy and deployed it to our local Dokku PaaS platform and serves it over HTTPS, allowing the team to see the status of the Caktus Library.

Property-based testing with Hypothesis

Vinod researched using property-based testing in Python. Traditionally it's more used with functional programming languages, but Hypothesis brings the concept to Python. He also learned about new Django features, including testing optimizations introduced with setupTestData .

Caktus Wagtail Demo with Docker and AWS

David looked into migrating a Heroku-based Wagtail deployment to a container-driven deployment using Amazon Web Services (AWS) and Docker. Utilizing Tobias' AWS Container Basics isolated Elastic Container Service stack, David created a Dockerfile for Wagtail and deployed it to AWS. Down the road, he'd like to more easily debug performance issues and integrate it with GitLab CI.

Local Docker Development

During Code for Durham Hack Nights, Victor noticed local development setup was a barrier of entry for new team members. To help mitigate this issue, he researched using Docker for local development with the Durham School Navigator project. In the end, he used Docker Compose to run a multi-container docker application with PostgreSQL, NGINX, and Django.

Caktus Costa Rica

Daryl, Nicole, and Sarah really like the idea of opening a branch Caktus office in Costa Rica and drafted a business plan to do so! Including everything from an executive summary, to operational and financial plans, the team researched what it would take to run a team from Playa Hermosa in Central America. Primary criteria included short distances to an airport, hospital, and of course, a beach. They even found an office with our name, the Cactus House. Relocation would be voluntary!

Improving the GUI test runner: Cricket

Charlotte M. likes to use Cricket to see test results in real time and have the ability to easily re-run specific tests, which is useful for quickly verifying fixes. However, she encountered a problem causing the application to crash sometimes when tests failed. So she investigated the problem and submitted a fix via a pull request back to the project . She also looked into adding coverage support.

Color your own NC Congressional District

Erin, Mark, Basia, Neil, and Dmitriy worked on an app that visualizes and teaches you about gerrymandered districts . The team ran a mini workshop to define goals and personas, and help the team prioritize the day's tasks by using agile user story mapping. The app provides background information on gerrymandering and uses data from NC State Board of Elections to illustrate how slight changes to districts can vastly impact the election of state representatives. The site uses D3 visualizations, which is an excellent utility for rendering GeoJSON geospatial data. In the future they hope to add features to compare districts and overlay demographic data.

Releasing django_tinypng

Dmitriy worked on testing and documenting django_tinypng , a simple Django library to allows optimization of images by using TinyPNG. He published the app to PyPI so it's easily installable via pip .

Learning Django: The Django Girls Tutorial

Gerald and Graham wanted to sharpen their Django skills by following the Django Girls Tutorial . Gerald learned a lot from the tutorial and enjoyed the format, including how it steps through blocks of code describing the syntax. He also learned about how the Django Admin is configured. Graham knew that following tutorials can sometimes be a rocky process, so he worked together with Graham so they could talk through problems together and Graham was able to learn by reviewing and helping.

Planning a new meetup for Digital Project Management

When Elizabeth first entered the Digital Project Management field several years ago, there were not a lot of resources available specifically for digital project managers. Most information was related to more traditional project management, or the PMP. She attended the 2nd Digital PM Summit with her friend Jillian, and loved the general tone of openness and knowledge sharing (they also met Daryl and Ben there!). The Summit was a wonderful resource. Elizabeth wanted to bring the spirit of the Summit back to the Triangle, so during Ship It Day, she started planning for a new meetup, including potential topics and meeting locations. One goal is to allow remote attendance through Google Hangouts, to encourage openness and sharing without having to commute across the Triangle. Elizabeth and Jillian hope to hold their first meetup in February.

Kanban: Research + Talk

Charlotte F. researched Kanban to prepare for a longer talk to illustrate how Kanban works in development and how it differs from Scrum. Originally designed by Toyota to improve manufacturing plants, Kanban focuses on visualizing workflows to help reveal and address bottlenecks. Picking the right tool for the job is important, and one is not necessarily better than the other, so Charlotte focused on outlining when to use one over the other.

Identifying Code for Cleanup

Calvin created redundant , a tool for identifying technical debt. Last ShipIt he was able to locate completely identical files, but he wanted to improve on that. Now the tool can identify functions that are almost the same and/or might be generalizable. It searches for patterns and generates a report of your codebase. He's looking for codebases to test it on!

RPython Lisp Implementation, Revisited

Jeff B. continued exploring how to create a Lisp implementation in RPython, the framework behind the PyPy project project. RPython is a restricted subset of the Python language. In addition to learning about RPython, he wanted to better understand how PyPy is capable of performance enhancements over CPython. Jeff also converted his parser to use Alex Gaynor's RPLY project.

Streamlined Time Tracking

At Caktus, time tracking is important, and we've used a variety of tools over the years. Currently we use Harvest , but it can be tedius to use when switching between projects a lot. Dan would like a tool to make this process more efficient. He looked into Project Hampster , but settled on building a new tool. His implementation makes it easy to switch between projects with a single click. It also allows users to sync daily entries to Harvest.

Why does Django not email me the 500 internal server error?

$
0
0

Why does Django not email me the 500 internal server error?

You’ve set your EMAIL_* settings correctly, and when you try to send emails with django.core.mail.sendmail() it works. However, Django still does not send you internal server errors. Why?

The sender’s email address matters

SERVER_EMAIL is the email address from which emails with error messages appear to come from. It is set in the “From:” field of the email. The default is “root@localhost”, and while “root” is OK, “localhost” is not, and some mail servers may refuse the email. The domain name where your Django application runs is usually OK, but if this doesn’t work you can use any other valid domain. The domain of your email address should work properly.

→ Set SERVER_EMAIL → When testing with django.core.mail.sendmail(), use the same sender email address as the one you’ve specified in SERVER_EMAIL. The recipient’s email address matters

Because of spam, mail servers are often very picky about which emails they will accept. It’s possible that even if your smarthost accepts the email, the next mail server may refuse it. For example, I made some experiments using EMAIL_HOST = 'mail.runbox.com' and this command:

send_mail('Hello', 'hello, world', 'noreply@example.com', ['anthony@itia.ntua.gr'])

In that case, Runbox accepted the email and subsequently attempted to deliver it to the mail server of ntua.gr, which rejected it because it didn’t like the sender (noreply@example.com; I literally used “example.com”, and ntua.gr didn’t like that domain). When something like this happens, send_mail() will appear to work, because send_mail() manages to deliver the email to the smarthost, and the error occurs after that; not only will we never receive the email, but it is also likely that we will not receive the failure notification (the returned email), so it’s often hard to know what went wrong and we need to guess.

One thing you can do to lessen the probability of error is to make sure that the recipient has an email address served by the provider who provides the smarthost. In my case, the smarthost is mail.runbox.com , and the recipient is antonis@djangodeployment.com, and the email for domain djangodeployment.com is served by Runbox. It is unlikely that mail.runbox.com would accept an email addressed to antonis@djangodeployment.com if another Runbox server were to subsequently refuse it. If something like this happened, I believe it would be a configuration error on behalf of Runbox. But it’s very normal that mail.runbox.com will accept an email which will subsequently be refused by ntua.gr or Gmail or another provider downstream.

→ At least one of the ADMINS should have an email address served by the provider who runs the smarthost. → When testing with django.core.mail.sendmail(), use the same recipient email address as the one of the ADMINS.
Commas can matter

This will work:

ADMINS = [('John', 'john@example.com')]

This is a common error; it won’t work:

ADMINS = (('John', 'john@example.com'))

You can make it work by adding a comma, like this:

ADMINS = (('John', 'john@example.com'),)

Despite the fact that I have a very sharp eye, I once forgot the comma and the site worked for months without notifying me of errors. Therefore, use the foolproof way:

→ Specify ADMINS as a list, not as a tuple. Test whether it sends errors

A favorite way of mine is to temporarily rename a template file and make a related request, which will raise a TemplateDoesNotExist exception. Your browser should show the “server error” page. Don’t forget to rename the template file back to what it was. By the time you finish doing that, you should have received the email with the full trace.

→ Temporarily rename a template file and make a related request in order to test whether errors are emailed OK. Django can’t notify you that it can’t notify you

While Django attempts to send an error email, if something goes wrong, it fails silently. This behaviour is appropriate (the system is in error, it attempts to email its administrator with the exception, but sending the email also results in an error; can’t do much more). Suppose, however, that when you try to verify that error emails work, you find out they don’t work. What has gone wrong? Nothing is written in any log. Intercepting the communication with ngrep won’t work either, because it’s usually encrypted. Inmy book, I recommend to use a locally installed mail server. If you do so, you will at least be able to look at the local mail server’s logs.

I don’t recommend using exim or postfix, as they are quite complicated. Instead, I recommend dma . To make it work, you’ll also need django-sendmail-backend .

→ Use a local mail server

使用Python的paramiko模块实现ssh与scp功能

$
0
0

#1. 介绍

这篇文章简单地介绍了python的paramiko模块的用法,paramiko实现了SSH协议,能够方便地与远程计算机交互。简单的说,就是你在terminal下执行的如下语句,现在可以通过python的paramiko实现了。

# 执行shell语句
ssh -i ~/.ssh/id_rsa -p 1098 rds@12.164.145.21-e 'ls -al'# 拷贝数据到远程计算机
scp -i ~/.ssh/id_rsa -P 1098-r data rds@12.164.145.21:~/data

这里不讨论shell与python实现的优缺点,如果你没有需求,也不会看到这篇博客了。我个人使用paramiko是为了使用python的多线程,并发地对多台远程计算机执行相同的操作。

这篇博客虽然篇幅不大,但是,可能是目前网络上最好的中文入门教程了。那就开始吧!

2. 安装

安装非常简单,直接使用pip安装即可:

sudo pip instal paramiko 3. 建立SSH连接

使用密码连接:

import paramiko
ssh = paramiko.SSHClient()#这行代码的作用是允许连接不在know_hosts文件中的主机。
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("IP", port,"username","password")

使用私钥连接:

ssh = paramiko.SSHClient()
ssh.connect('10.120.48.109', port,'用户名',
key_filename='私钥')

连接以后可以执行shell命令:

In[8]: ssh.exec_command('ls')Out[8]:(<paramiko.ChannelFilefrom<paramiko.Channel1(open) window=2097152-><paramiko.Transport at 0x377c690L(cipher aes128-ctr,128 bits)(active;2 open channel(s))>>>,<paramiko.ChannelFilefrom<paramiko.Channel1(open) window=2097152-><paramiko.Transport at 0x377c690L(cipher aes128-ctr,128 bits)(active;2 open channel(s))>>>,<paramiko.ChannelFilefrom<paramiko.Channel1(open) window=2097152-><paramiko.Transport at 0x377c690L(cipher aes128-ctr,128 bits)(active;2 open channel(s))>>>)

执行shell命令以后,并不会立即打印命令的执行结果,而是返回几个Channel, 只能像下面这样获取输出:

In[9]: stdin, stdout, stderr = ssh.exec_command('ls')In[10]:print stdout.readlines()['AgentBackkup_2015-06-11\n','AgentBackup\n','log\n','mysql.sh\n','rdsAgent\n']

注意:命令执行出错并不会抛出异常,所以,对于命令出错需要根据自己的需求进行相应的处理:

In[54]: stdin, stdout, stderr = ssh.exec_command('cat file_not_found')In[55]:print stdout.readlines()[]In[56]:print stderr.readlines()[u'cat: file_not_found: No such file or directory\n']In[57]: stdin, stdout, stderr = ssh.exec_command('ls')In[58]:print stderr.readlines()[]

API文档: https://paramiko-docs.readthedocs.org/en/1.15/api/client.html

4. SCP vs SFTP

通过paramiko还可以传输文件,这是我这篇博客的主要原因。搜了很多博客,都没有说明白如何通过paramiko在计算机之间传输文件,通过阅读官方文档,发现有如下两种方式:

sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
sftp = ssh.open_sftp()

即新建一个SFTPClient对象,该对象复用之前的SSH连接,因此,我们使用sftp传输文件时,不需要再次进行用户认证。

文件上传

In[59]: sftp.put('memory.py','memory.py')Out[59]:<SFTPAttributes:[ size=288 uid=1000 gid=1000 mode=0100644 atime=1435391914 mtime=1435391914]>

文件下载

In[60]: sftp.get('memory.py','backup.py')

执行命令

paramiko并没有提供一个叫做scp的子模块,如果我们希望在计算机之间传输数据,可以通过sftp(sftp实现了scp所有的功能,也就没有必再实现一个scp)传输文件,还可以通过sftp执行命令,如下所示:

In[44]: sftp.listdir()Out[44]:['.viminfo','.bash_logout','.bash_history','AgentBackkup_2015-06-10','AgentBackup','rdsAgent']In[45]: sftp.rename('AgentBackkup_2015-06-10','AgentBackkup_2015-06-11')In[46]: sftp.listdir()Out[46]:['AgentBackkup_2015-06-11','.viminfo','.bash_logout','.bash_history','AgentBackup','rdsAgent']

sftp提供了很多命令,具体内容可以参考 官方文档 。

零基础如何入门Python http://www.linuxidc.com/Linux/2016-10/136485.htm

Ubuntu 14.04安装Python 3.3.5 http://www.linuxidc.com/Linux/2014-05/101481.htm

CentOS上源码安装Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

Ubuntu 14.04下Python数据处理环境搭建 http://www.linuxidc.com/Linux/2017-01/139568.htm

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm 《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

在CentOS 6.5上安装Python2.7 http://www.linuxidc.com/Linux/2016-10/136206.htm

在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htm

本文永久更新链接地址 : http://www.linuxidc.com/Linux/2017-01/139972.htm

PyTennessee: PyTN Profiles:James Dozier and PyOhio

$
0
0

PyTennessee: PyTN Profiles:James Dozier and PyOhio
Speaker Profile: James Dozier ( @thinkcodemake )

James is a Data Analyst for RePublic Schools in Nashville, TN and a self-taught python Developer. Before moving to Nashville this year, he ran a monthly programming meetup in Jackson, TN, where he shared his love of Python and other programming languages.

James will be presenting “Robot Fight: A Study of Genetic Algorithms” at 2:00PM Sunday (2/5) in room 200. Gronk is a master Goblin Robot Builder. His bot, ZapPow, is the undefeated champion of Robot Fight!, the ongoing Goblin robot tournament. (Goblins aren’t great at naming things.) Where Gronk is a master builder, the Dorklings aren’t that great. When they first build their bots, they just throw random pieces together. But they have a scheme, a way to work together to eventually overthrow Gronk.


PyTennessee: PyTN Profiles:James Dozier and PyOhio

Sponsor Profile: PyOhio (@PyOhio)

PyOhio is a free annual conference for Python programmers in and around Ohio and the entire Midwest. The Tenth Annual PyOhio will be July 29-30, 2017! They’re model, advice and guidance are the reason we are here.

使用Python的Paramiko模块登陆SSH

$
0
0

paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。

python的paramiko模块可以方便的实现ssh登录,并执行命令。

pycrypto 与 paramiko下载到linux公社资源站下载:

------------------------------------------分割线------------------------------------------

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是 www.linuxidc.com

具体下载目录在/2017年资料/1月/26日/使用Python的Paramiko模块登陆SSH/

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

------------------------------------------分割线------------------------------------------

1. paramiko模块安装 1.1安装pycrypto库

pycrypto库的主页在这里:https://www.dlitz.net/software/pycrypto/

pycrypto库安装方法

python setup.py build && python setup.py install 1.2安装paramiko

paramiko库的主页在这里: https://pypi.python.org/pypi/paramiko

paramiko库的安装方法超级简单。

python setup.py build && python setup.py install 2.简单使用 2.1 执行远程命令 #!/usr/bin/python
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("某IP地址",22,"用户名", "口令")
stdin, stdout, stderr = ssh.exec_command("你的命令")
print stdout.readlines()
ssh.close() 2.2 上传文件到远程 #!/usr/bin/python
import paramiko
t = paramiko.Transport(("某IP地址",22))
t.connect(username = "用户名", password = "口令")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.put(localpath,remotepath)
t.close() 2.3 上传文件到远程 #!/usr/bin/python
import paramiko
t = paramiko.Transport(("某IP地址",22))
t.connect(username = "用户名", password = "口令")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.get(remotepath, localpath)
t.close() 3.高级用法

通常需要对多个服务器或者虚拟机进行管理,可以采用批量的方式进行。

#-*- coding: utf-8 -*-
#!/usr/bin/python
import paramiko
import threading
def ssh2(ip,username,passwd,cmd):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,username,passwd,timeout=5)
for m in cmd:
stdin, stdout, stderr = ssh.exec_command(m)
out = stdout.readlines()
for o in out:
print o, #屏幕输出
print '%s\tOK\n'%(ip)
ssh.close()
except :
print '%s\tError\n'%(ip)
if __name__=='__main__':
cmd = ['echo hello!']#需要执行的命令列表
username = "root" #用户名
passwd = "root" #密码
threads = [] #多线程
print "Begin excute......"
for i in range(1,254):
ip = '192.168.1.'+str(i)
a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
a.start()

零基础如何入门Python http://www.linuxidc.com/Linux/2016-10/136485.htm

Ubuntu 14.04安装Python 3.3.5 http://www.linuxidc.com/Linux/2014-05/101481.htm

CentOS上源码安装Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

Ubuntu 14.04下Python数据处理环境搭建 http://www.linuxidc.com/Linux/2017-01/139568.htm

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm 《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

在CentOS 6.5上安装Python2.7 http://www.linuxidc.com/Linux/2016-10/136206.htm

在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htm

本文永久更新链接地址 : http://www.linuxidc.com/Linux/2017-01/139977.htm

使用python编写vim插件

$
0
0

Use a single editor well. The editor should be an extension of your hand; make sure your editor is configurable, extensible, and programmable. - Andrew Hunt

平常总是用那些大神们写的vim插件,今天闲来无事学写了一下vim插件,顺便写一个简单的教程。

先来看下我一个旺盛的 需求 :平常总会遇到分享本机或者服务器代码的情况,我经常使用 paste.ubuntu.com 网站粘贴代码(我记性不好总是忘记scp咋使的),然后把链接分享给别人。类似github的gist服务,不过比较简单,而且gist国内用户访问有时候会出现奇奇怪怪的问题,所以一般我用ubuntu的pastebin多一些。但是步骤麻烦点,我写这个插件就是要一个命令帮我完成在vim里直接贴代码到paste.ubuntu.com的功能。

步骤如下:

看了一个youtube视频讲怎么用python写vim插件的,笔者以前的经验只限于使用vim,并不会vim脚本。现学现卖,同想写插件的可以看看,即使不用深入理解,照葫芦画瓢也不难。简单的插件从学习到实现大概只会花你半天到一天左右的时间,以后你就可以定制你的vim编辑器了。

Writing Vim plugins with Python 让vim支持python函数

首先确认你的vim编译时支持python或者python3,用 vim --version | grep +python 查看输出,否则你可能要重新编译安装vim。 笔者之前没有写过vim脚本,由于对python比较熟悉,现在用python实现。这里举个简单的例子,vim example.vim

function! HelloVim() echo "Hello Vim" endfunction

然后在vim里执行 :source % ,之后在 :call HelloVim() 就能看到函数执行结果了。

怎么在vim脚本里使用python函数呢?这里有个简单的文档 Vim documentation: if_pyth

来用python实现一个类似的Hello函数

" vim sript function function! HelloVim() echo "Hello Vim" endfunction " use python function, vim compiled with +python or +python3 function! HelloPython() python3 << endOfPython print('Hello Python') endOfPython endfunction

我的vim使用+python3编译的,你可能需要把上边的python3换成python。然后在vim里执行 :source % ,之后在 :call HelloVim() 就能看到函数执行结果了。

vim插件的目录结构

我们直接偷懒使用项目生成框架starter kit(就和我直接偷懒用flask-cookitecutter一样),项目仓库在这里 vim-plugin-starter-kit 如果你有兴趣也可以看看这个项目的wiki,有一些教程。 直接克隆项目,进入文件夹以后执行python create_plugin_scaffold.py然后填写一下项目名称就好了,直接帮助你生成一个项目模板。我的项目树如下:

vim-ubuntu-pastebin ├── README.md ├── doc │ └── vim-ubuntu-pastebin.txt └── plugin ├── __init__.py ├── test.txt ├── tests │ ├── __init__.py │ └── vim_ubuntu_pastebin_tests.py ├── vim_ubuntu_pastebin.py └── vim_ubuntu_pastebin.vim 编写简单的vim插件

我的需求有两个,一个是把当前的文本发送到 paste.ubuntu.com ,还有一个是把该功能集成到vim里。第一个功能我们使用python完成,功能也比较简单。我们去网站上随便粘贴一段文本用浏览器开发者工具观察发送的post请求就好了,之后用requests模拟提交(也可以用python内置的urllib,不过这里还要兼容python2和python3,所以直接用requests方便很多),编辑vim_ubuntu_pastebin_tests.py文件如下:

# -*- coding: utf-8 -*- import os import requests import webbrowser try: from urllib import urlencode # py2 except ImportError: from urllib.parse import urlencode # py3 PASTEBIN_SUPPORT_TYPE = frozenset([ 'text', 'Cucumber', 'abap', 'ada', 'ahk', 'antlr', 'antlr-as', 'antlr-cpp', 'antlr-csharp', 'antlr-java', 'antlr-objc', 'antlr-perl', 'antlr-python', 'antlr-ruby', 'apacheconf', 'applescript', 'as', 'as3', 'aspx-cs', 'aspx-vb', 'asy', 'basemake', 'bash', 'bat', 'bbcode', 'befunge', 'blitzmax', 'boo', 'c', 'c-objdump', 'cfm', 'cfs', 'cheetah', 'clojure', 'cmake', 'coffee-script', 'common-lisp', 'console', 'control', 'cpp', 'cpp-objdump', 'csharp', 'css', 'css+django', 'css+erb', 'css+genshitext', 'css+mako', 'css+myghty', 'css+php', 'css+smarty', 'cython', 'd', 'd-objdump', 'delphi', 'diff', 'django', 'dpatch', 'duel', 'dylan', 'erb', 'erl', 'erlang', 'evoque', 'factor', 'felix', 'fortran', 'gas', 'genshi', 'genshitext', 'glsl', 'gnuplot', 'go', 'gooddata-cl', 'groff', 'haml', 'haskell', 'html', 'html+cheetah', 'html+django', 'html+evoque', 'html+genshi', 'html+mako', 'html+myghty', 'html+php', 'html+smarty', 'html+velocity', 'hx', 'hybris', 'ini', 'io', 'ioke', 'irc', 'jade', 'java', 'js', 'js+cheetah', 'js+django', 'js+erb', 'js+genshitext', 'js+mako', 'js+myghty', 'js+php', 'js+smarty', 'jsp', 'lhs', 'lighty', 'llvm', 'logtalk', 'lua', 'make', 'mako', 'maql', 'mason', 'matlab', 'matlabsession', 'minid', 'modelica', 'modula2', 'moocode', 'mupad', 'mxml', 'myghty', 'mysql', 'nasm', 'newspeak', 'nginx', 'numpy', 'objdump', 'objective-c', 'objective-j', 'ocaml', 'ooc', 'perl', 'php', 'postscript', 'pot', 'pov', 'prolog', 'properties', 'protobuf', 'py3tb', 'pycon', 'pytb', 'python', 'python3', 'ragel', 'ragel-c', 'ragel-cpp', 'ragel-d', 'ragel-em', 'ragel-java', 'ragel-objc', 'ragel-ruby', 'raw', 'rb', 'rbcon', 'rconsole', 'rebol', 'redcode', 'rhtml', 'rst', 'sass', 'scala', 'scaml', 'scheme', 'scss', 'smalltalk', 'smarty', 'sourceslist', 'splus', 'sql', 'sqlite3', 'squidconf', 'ssp', 'tcl', 'tcsh', 'tex', 'text', 'trac-wiki', 'v', 'vala', 'vb.net', 'velocity', 'vim', 'xml', 'xml+cheetah', 'xml+django', 'xml+erb', 'xml+evoque', 'xml+mako', 'xml+myghty', 'xml+php', 'xml+smarty', 'xml+velocity', 'xquery', 'xslt', 'yaml', ]) def post_buffer_to_ubuntu_pastebin(buffer, filetype, open_in_borwser=True): """ post current file buffer content to http://paste.ubuntu.com/ and return url. Args: buffer (vim.current.buffer) filetype (str) Returns: url (str) """ lines = os.linesep.join(list(buffer)) url = 'http://paste.ubuntu.com/' data = urlencode( { 'poster': os.environ.get('USER', 'anonymous'), 'syntax': filetype if filetype in PASTEBIN_SUPPORT_TYPE else 'text', 'content': lines } ).encode('utf-8') r = requests.post(url, data=data, allow_redirects=True) if open_in_borwser: webbrowser.open_new_tab(r.url) return r.url

这样发送文本内容到 paste.ubuntu.com 的需求就完成了,还差一步就是怎么在vim里能够使用这段python代码。接下来编辑 vim_ubuntu_pastebin.vim文件,看着也挺简单:

" -------------------------------- " Add our plugin to the path " -------------------------------- if !(has('python3') || has('python')) echo "Error: Required vim compiled with +python or +python3" finish endif if has('python3') python3 import sys python3 import vim python3 sys.path.append(vim.eval('expand("<sfile>:h")')) function! Pastebin() python3 << endOfPython from vim_ubuntu_pastebin import post_buffer_to_ubuntu_pastebin filetype = vim.eval('&filetype') " 获取文件类型 url = post_buffer_to_ubuntu_pastebin(vim.current.buffer, filetype) print(url) endOfPython endfunction endif " 下边代码仅仅是'python3'替换成了'python' if has('python') python import sys python import vim python sys.path.append(vim.eval('expand("<sfile>:h")')) function! Pastebin() python << endOfPython from vim_ubuntu_pastebin import post_buffer_to_ubuntu_pastebin filetype = vim.eval('&filetype') url = post_buffer_to_ubuntu_pastebin(vim.current.buffer, filetype) print(url) endOfPython endfunction endif " 定义一个命令Pastebin就可以在终端 command! -nargs=0 Pastebin call Pastebin()

我们直接在这个vim脚本里调用上边的python函数就可以了,使用list(vim.current.buffer)就能获取当前buffer里每行的内容,用换行符拼起来,之后把内容发送出去就好了。这样就实现了一个简单的vim插件了,是不是挺容易的。把当前项目推到github上,别人就可以使用Bundle等工具使用你的插件了。亲测有效,以后贴代码方便多了,在你的vim里头执行命令 :Pastebin 当前文件就自动贴到 paste.ubuntu.com 上并且输出url了,如果是桌面版操作系统还会自动帮你打开页面。

Ref:

vim-ubuntu-pastebin - 上边小项目的代码

Vim documentation: if_pyth - 你需要看一下python操作vim buffer的接口


Ian Ozsvald: Introduction to Random Forests for Machine Learning at the London P ...

$
0
0

Last night I had the pleasure of returning to London python to introduce Random Forests (this builds on myPyConUK 2016 talk from September). My goal was to give a pragmatic introduction to solving a binary classification problem (Kaggle’s Titanic) using scikit-learn. The talk ( slides here ) covers:

Organising your data with Pandas Exploratory Data Visualisation with Seaborn Creating a train/test set and using a Dummy Classifier Adding a Random Forest Moving towards Cross Validation for higher trust Ways to debug the model (from the point of view of a non-ML engineer) Deployment Code for the talk is a rendered Notebook on github

I finished with a slide on Community ( are you contributing? do you fulfill your part of the social contract to give back when you consume from the ecosystem?) and another pitching PyDataLondon 2017 (May 5-7th). My colleague Vincent is over from Amsterdam he pitched PyDataAmsterdam (April 8-9th). The Call for Proposals is open for both, get your talk ideas in quickly please.

I’m really happy to see the continued growth of the London Python meetup , this was one of the earliest meetups I ever spoke at. The organisers are looking for speakers do get in touch with them via meetup to tell them what you’d like to talk on.

Ian applies Data Science as an AI/Data Scientist for companies inModelInsight, sign-up for Data Science tutorials in London . Historically Ian ran Mor Consulting . He also founded the image and text annotation API Annotate.io , co-authored SocialTies , programs Python, authored The Screencasting Handbook , lives in London and is a consumer of fine coffees.

Python basics for data analysis:

$
0
0

When I was asked to write a post, I decided to share with the NLP community how learning a programming language can help Computational Linguists to optimize their everyday work.

Since I started working as a Computational Linguist at Bitext, every day I have to deal with large amounts of data and information and I wanted to share my favorite tool to process, analyze, and extract information from large files: python.

Python, as some of you might know, is a freely accessible programming language and it offers a lot of functionalities for NLP and data analysis. I prefer it over other programming languages because it is widely used in academia, research, and industry, and it also gives the user access to libraries like NLTK, which can be very useful when the data you are analyzing is linguistic.

Let me show you a couple functions of Python for data analysis I use on a daily basis, you will see that even some Python basics can help us greatly.

Information Retrieval:

A Python script can help you to extract from a big file the specific information that you are interested in. This eases the process, reduces the time, and avoids tedious manual work.

As an example, let’s imagine I have a big text file in Russian. With just over 10 lines of simple code, I can extract all the masculine singular forms of the past participle active of Russian verbs (not counting irregular verbs).


Python basics for data analysis:

This script would iterate through millions of lines and give back a result in seconds, optimizing the process and saving us a lot of time.

Text processing:

Imagine that in the above-mentioned file, we discover that those forms we have extracted were not meant to be masculine (e.g: ‘слышавший’), but instead they should be the feminine form (e.g: ‘слышавшая’). With this simple Python script, we can change the masculine forms into feminine ones.


Python basics for data analysis:

As well as the fast processing time, using a script ensures that there will not be manual errors (if we make sure we have the correct code, of course). The script will go through the whole file and change all the instances in the same way.

Text normalization:

This is very useful when we get a file full of great information, but to actually use it we need to give it a different format. This formatting may be necessary to make it usable by some other software or simply to make it more readable.

For example, let’s imagine we get the flexion of Spanish verbs from a source that has this messy format:

+o;bailar;1sg;present;indicative;-ar

But in order to use it we want it presented following this format:

Conjugated-form Lemma Person Number Tense Aspect

This Python script will read any number of lines and return them in the desired format.


Python basics for data analysis:

This type of change would be impossible to make with most text editors if the file is several million lines long, but with a Python script, we change the whole file uniformly in seconds.

These are only simple examples of what you can do with Python to process and analyze your data, however, they show the great potential this programming language has. At Bitext, we use Python to easily interact with Language Processing APIs, deal with big language databases, generate or process the inflection and derivation of any language and many other tasks. If you are interested in more complex examples complete the form and we will send to you a presentation!

PyTennessee: PyTN Profiles:Cindy Sridharan and the Python Software Foundation

$
0
0

PyTennessee: PyTN Profiles:Cindy Sridharan and the Python Software Foundation
Speaker Profile: Cindy Sridharan ( @copyconstruct )

I’m a developer based out of San Francisco with a passion for python, Go, Rust, operations, systems programming and infrastructure. I’ve been writing Python for the last 6 years. I’ve spoken at conferences like OSCON, PyBay, GothamGo as well as at local meetups in San Francisco, I organize the SF Python Twisted meetups.

Cindy will be presenting “The Python Deployment Albatross” at 3:00PM Saturday (2/4) in room 100. The Python packaging ecosystem has evolved a lot in the recent years, with better packaging formats and tools available to us that didn’t exist 5 years ago. In fact, it’s a bit of an embarrassment of riches compared to what we had 5 years ago.Distutils, setuptools, pip, virtualenv, eggs, wheels, pex … there’s absolutely no shortage of tools available to package and distribute Python artifacts. Furthermore, the advent of Docker in the recent years has brought a renewed focus on containerization and how Docker and other technologies in the ecosystem can be best leveraged to package and ship Python applications. Most Python deployments in the wild use pip with virtualenv or more recently Docker, but there appears to be a lot of misconceptions around swapping out one for the other. There’s even talk about how Docker is well-suited to replace existing tools in the ecosystem like virtualenv.


PyTennessee: PyTN Profiles:Cindy Sridharan and the Python Software Foundation

Sponsor Profile: Python Software Foundation (@thepsf)

The mission of the Python Software Foundation is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers. The majority of the PSF’s work is focused on empowering and supporting people within the Python community. The PSF has active grant programs that support sprints, conferences, meetups, user groups, and Python development efforts all over the world. In addition, the PSF underwrites and runs PyCon US, the primary Python community conference. Being part of the PSF means being part of the Python community. Recently we changed the PSF to an open membership organization, so that everyone who uses and supports Python can join. To learn more, visit https://www.python.org/psf/membership .

Uncover machine learning's potential with this Python training collection

$
0
0

Uncover machine learning's potential with this Python training collection

Machine learning is revolutionizing the way we use information. From programming self-driving cars to crunchingstock market data, machine learning and its related fields are opening the doors to new technologies as well as high-paying professions.

Pick up the Machine Learning with python Course and E-Book Bundle , and you can get an inside look at both as you access five courses and four e-books full of machine learning expertise.

You’ll lay the foundation as you work with Python. Using this versatile programming language, you’ll dive into deep learning and discover how data science, statistics, and artificial intelligence come together to advance image and speech recognition as well as search engine technology.

Make your way through the entire collection, and you’ll master the ins and outs of Python, TensorFlow, and a myriad of data analysis tools to prepare you for a career in one of tech’s most talked-about fields.

For a limited time, you can get the Machine Learning with Python Course and E-Book Bundle for only $49, saving more than 90% off its usual $649 retail price.

How to install Django on Ubuntu (the right way) for novice users

$
0
0

How to install Django on Ubuntu (the right way) for novice users
About Django

Django is a great web application development framework, especially for the perfectionist with deadlines built into python programming language. It provides a very good structure and easy methods that help to do the heavy lifting when writing web applications.

If you have never used Django before and are looking for an installation guide on your Ubuntu machine then you are in the right place. In this article we will teach you how to set up a very simple Django development environment, step by step.

Some Tools We Need

There are many different ways to install the Django framework in Ubuntu. In order to stay updated with the latest python development industry standards we need to install a few tools before getting Django up and running it on our machine.

The first tool is called pip which is a very practical python package management system widely used by python developers all over the world. I mainly use pip to install packages for my python projects, generate dependencies of a project and also uninstall different python packages from my machine when they are no longer required.

Before installing pip we highly recommend updating the package index on your machine with the help of the command shown below.

sudo apt-get update

The following command can be used to install the pip python package manager inside Ubuntu:

sudo apt-get install python-pip

Now, you can easily install python packages on your Ubuntu machine by following the syntax shown here:

pip install name-of-python-package-here

But we do not recommend installing packages yet! Python developers use another tool during the development of their python projects…

This tool is called virtualenv . It is really useful as it helps to manage dependency conflicts between different python projects on the same machine. Essentially, this tool helps to create isolated virtual environments where you can develop without any worries about package conflicts.

To install virtualenv on Ubuntu the following command can be used:

sudo pip install vitualenv Create A Virtual Environment For Your Project

Virtualenv can be easily used to create isolated virtual environments for your python projects.

For example, to create a virtual environment under the name of venv1 the following command can be used:

virtualenv venv1

In order to work on an isolated environment it needs to be activated. The following command will make that happen:

source venv1/bin/activate

But working with virtualenv is a bit annoying as there are many different commands you need to remember and type on your terminal emulator.

The best solution is to install and use virtualenvwrapper which makes working with python virtual environments very easy. Once you have finished installing and configuring virtualenvwrapper working on a virtual environment is as easy as typing the following command:

workon venv1

Now that pip and virtualenv tools are available on your machine it is time to install and configure virtualenvwrapper. To install virtualenvwrapper use pip, as shown below:

pip install virtualenvwrapper

There are a few steps you need to follow in order to do this properly. On your command prompt run the following command:

source /usr/local/bin/virtualenvwrapper.sh

All virtual environments you create will be available inside the directory ~/.virtualenvs.

If you want to keep your virtual environments inside another directory then use the following commands (as shown in the official documentation of the virtualenvwrapper):

export WORKON_HOME=~/Name-Of-DIrectory-Here mkdir -p $WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh

I like to keep my virtual environments inside ~/.virtualenvs. My projects are inside ~/projects.

To create a virtual environment just use the command mkvirtualenv as shown below:

mkvirtualenv linuxtuts

The following output is displayed on my terminal when executing the above command.

New python executable in linuxtuts/bin/python
Installing setuptools, pip...done.

To work on the virtual environment linuxtuts use the following command:

workon linuxtuts

Once the virtual environment has been activated your command prompt will change. Mine looks like this:

(linuxtuts)[email protected]

:~/Desktop$

As you can see from the output shown above, linuxtuts is the name of the virtual environment we created.

Make the changes permanent

In order for the commands offered by virtualenvwrapper to work every time you open the terminal you will need to make some changes in the .bashrc file.

The .bashrc file is used to set up environment variables, function aliases and lots of other information you will want to have when opening a new terminal window. Read more on .bashrc.

Open the .bashrc file with your text editor, and then copy/paste the following into it and save the file.

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh Install Django

Inside the virtual environment use the command ‘which python’ to see the python executable you are using.

which python

The above command produces the following output on my machine. Depending on the name of your directory for the virtual environments you will get a different output, but structured in a very similar way.

/home/oltjano/.virtualenvs/linuxtuts/bin/python

The above output is telling us that the python executeable being used is inside the virtual environment, which in this case is linuxtuts.

Deactivate the virtual environment with the command deactivate.

deactivate

Remove the virtual environment linuxtuts with the help of the following command:

rmvirtualenv linuxtuts

The reason we decided to remove linuxtuts is that we did not choose the version of python we wanted to use inside the virtual environment we created.

It can be done with the following command:

mkvirtualenv -p /usr/bin/python-version-here

In a project I am working on we use python3.2. To run the project I create a special environment for it:

mkvirtualenv -p /usr/bin/python3.2 linuxtuts

The above command produces the following output:

Running virtualenv with interpreter /usr/bin/python3.2
New python executable in linuxtuts/bin/python3.2
Installing setuptools, pip...done
Also creating executable in linuxtuts/bin/python

The command prompt will look similar to mine.

(linuxtuts)[email protected]

:~/Desktop$

You can use any python version required by your project. The installation of Django is very easy. Just run the following command:

pip installl django

The above command produces the following output:

(linuxtuts)[email protected]

:~/Desktop$ pip install django You are using pip version 6.0.7, however version 6.0.8 is available. You should consider upgrading via the 'pip install --upgrade pip' command. 100% |################################| 7.4MB 40kB/s Collecting django Downloading Django-1.7.6-py2.py3-none-any.whl (7.4MB) Successfully installed django-1.7.6 Installing collected packages: django

To
Viewing all 9596 articles
Browse latest View live




Latest Images