Subscribe: Planet Python
http://www.planetpython.org/rss20.xml
Added By: Feedage Forager Feedage Grade B rated
Language: English
Tags:
app  bylaws  code  cython  def  file  files  import  info  library  line  list  make  new  package  packages  python 
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: Planet Python

Planet Python



Planet Python - http://planetpython.org/



 



PyCharm: PyCharm 2017.2 Out Now: Docker Compose on Windows, SSH Agent and more

Wed, 26 Jul 2017 10:11:45 +0000

PyCharm 2017.2 is out now! Get it today for Docker Compose support on Windows, SSH Agent, Azure Databases, and Amazon Redshift support.


Get it from our website

  • We’ve added some small improvements for editing Python files: a quick fix to change the signature of a function you’re calling, an inspection to make sure your Python 3.6-style format strings work correctly, and auto-completion for type hints
  • Docker Compose is additionally supported on Windows (this feature is available only in PyCharm Professional Edition)
  • PyCharm 2017.2 supports using SSH Agent to handle your SSH private keys. Compatible tools like Pageant on Windows are also supported. (only in Professional Edition)
  • Database tools fully support connecting to Amazon Redshift and Azure Databases (only in Professional Edition)
  • Run inline SQL on multiple data source (only in Professional Edition)
  • Improvements for Version Control, JavaScript, and HiDPI support (JavaScript support is available only in Professional Edition)
  • And more, see our what’s new page for details

Get PyCharm 2017.2 now from our website!

Please let us know what you think about PyCharm! You can reach us on Twitter, Facebook, and by leaving a comment on the blog.

PyCharm Team
-The Drive to Develop

(image)



Stefan Behnel: What's new in Cython 0.26?

Tue, 25 Jul 2017 14:33:15 +0000

Cython 0.26 has finally been released and it comes with some big and several smaller new features, contributed by quite a number of non-core developers.

Probably the biggest addition, definitely codewise, is support for Pythran as a backend for NumPy array expressions, contributed by Adrien Guinet. Pythran understands many usage patterns for NumPy, including array expressions and some methods, and can directly be used from within Cython to compile NumPy using code by setting the directive np_pythran=True. Thus, if Pythran is available at compile time, users can avoid writing manual loops and instead often just use NumPy in the same way as they would from Python. Note that this does not currently work generically with Cython's memoryviews, so you need to declare the specific numpy.ndarray[] types in order to benefit from the translation. Also, this requires the C++ mode in Cython as Pythran generates C++ code.

The other major new feature in this release, and likely with an even wider impact, is pickling support for cdef classes (a.k.a. extension types). This is enabled by default for all classes with Python compatible attribute types, which explicitly excludes pointers and unions. Classes with struct type attributes are also excluded for practical reasons (such as a high code overhead), but can be enabled to support pickling with the class decorator @cython.auto_pickle(True). This was a long-standing feature for which users previously had to implement the pickle protocol themselves. Since Cython has all information about the extension type and its attributes, however, there was no technical reason why it can't also generate the support for pickling them, and it now does.

As always, there are several new optimisations including speed-ups for abs(complex), comparing strings and dispatching to specialised function implementations based on fused types arguments. Particularly interesting might be the faster GIL re-entry with the directive fast_gil=True. It tries to remember the current GIL lock state in the fast thread local storage and avoids costly calls into the thread and GIL handling APIs if possible, even when calling across multiple Cython modules.

A slightly controversial change now hides the C code lines from tracebacks by default. Cython exceptions used to show the failing line number of the generated C code in addition to the Cython module code line, now only the latter is shown, like in Python code. On the one hand, this removes clutter that is irrelevant for most users. On the other hand, it hides information that could help developers debug failures from bug reports. For debugging purposes, the re-inclusion of C code lines can now be enabled with a runtime setting as follows:

import cython_runtime
cython_runtime.cline_in_traceback=True



Stefan Behnel: What's new in Cython 0.25?

Tue, 25 Jul 2017 14:33:05 +0000

Cython 0.25 has been released in October 2016, so here's a quick writeup of the most relevant new features in this release.

My personal favourites are the call optimisations. Victor Stinner has done a great job throughout the year to optimise and benchmark different parts of CPython, and one of the things he came up with was a faster way to process function calls internally. For this, he added a new calling convention, METH_FASTCALL, which avoids tuple creation by passing positional arguments as a C array. I've added support for this to Cython and also ported a part of the CPython implementation to speed up calls to Python functions and PyCFunction functions from Cython code.

Further optimisations speed up f-string formatting, cython.inline() and some Python long integer operations (now also in Py2.7 and not only Py3).

The next big feature, one that has been on our list essentially forever, was finally added in 0.25. If you declare a special attribute __dict__ on a cdef class (a.k.a. extension type), if will have an instance dict that allows setting arbitrary attributes on the objects. Otherwise, extension types are limited to the declared attributes and trying to access undeclared ones would result in an AttributeError.

The C++ integration has received several minor improvements, including support for calling subclass methods from C++ classes implemented in Cython code, iterating over std::string with Cython's for-loop, typedef members in class declarations, or the typeid operator.

And a final little goodie is redundant declarations for pi and e in libc.math, which makes from libc cimport math pretty much a drop-in replacement for Python's import math.




PyCharm: Interview: Paul Craven on Python Gaming and Teaching

Tue, 25 Jul 2017 09:38:46 +0000

Writing games in Python is fun, so how about using it to teach computer programming? Paul Craven is both a professor and the creator of Arcade, a 2d game library for Python. He’s doing a webinar with us next week, so we talked to him about teaching Python, using Python 3 type hints, why another Python game library, and how IDEs fit into teaching. Thanks a bunch for doing the webinar next week. First, can you tell us a little bit about yourself? I worked in the IT industry for 15 years before switching to teaching at Simpson College, a small 4-year college in Iowa. My main interest has been getting first-time programmers to realize that programming can be fun. That moment when a student cheers out loud because they finally figured out how to get sprites to move correctly on the screen? It is a beautiful thing to see. You teach programming, and you created the Arcade game library to help. Before talking about Arcade, can you explain the motivation behind having a framework that you can teach? Teaching is like engineering. Each semester you work to improve how you teach students. I had been using the Pygame library. But I wanted a library that I could improve on based on what I saw from students. For example: Function and parameter names that students intuitively understand. Each year I had to teach them “set_mode” opens a window. Why not just have the function called “open_window”? Support for functions students which ask for. In Pygame drawing ellipses with thick borders always had a moire pattern because of a bug in the library. And you can’t tilt ellipses when you draw them. Every year I have a student that wants to draw a football. And each year they were frustrated that it looked awful. I wanted a library where it just worked. Students would download a graphic for their sprite. But it would be too large and there was no easy way to scale the image. That always resulted in hours of wasted time explaining how to do the scaling. What if the library just supported scaling? After a while I collected such a long list of things like that, I decided to create a game library that where I wouldn’t have to teach “around” these issues. Beyond using it for teaching, can you talk a bit about Arcade? What is it, how is it different, who might want to use it? Arcade is great for sprite-based 2D games. It is simple and easy to get started with the library. There is no need to learn a complex framework. If you’ve wanted to create a game for fun, but Kivy, Unity, Unreal would just take more time to learn than what you’ve got, Arcade is a better choice. If you want to quickly create a scientific visualization without a lot of overhead code, Arcade can help you there too. Arcade uses OpenGL and Pyglet. With OpenGL acceleration, it can draw a lot of sprites fast. I use Arcade for my PyCharm tutorials and Arcade’s Python 3.6 type hinting is a big reason. Can you talk about your experience, as a library author and teacher, with type hinting? New programmers often get confused when calling functions and methods. What data does it expect? And when the program doesn’t run, the students aren’t yet experts in reading stack traces. So they are stuck with a 500 line program that doesn’t work and they don’t know why. Frustrating. Type hinting can sometimes tell students that they are passing unexpected data to the function. It does this before they run the program. Before they’ve even moved to writing the next line of code. It seems trivial, but it’s not. I found students able to create better, more complex programs because PyCharm and type hinting kept them from that error and allowed them to move on. You also use PyCharm CE with your students. What’s been your experience having beginners start with an IDE? I’ve taught students with an IDE and without an IDE. The biggest advantage is how the IDE can help students catch errors early. PyCharm’s built-in PEP[...]



Python Insider: Python 3.5.4rc1 and Python 3.4.7rc1 are now available

Tue, 25 Jul 2017 04:40:14 +0000

Python 3.5.4rc1 and Python 3.4.7rc1 are now available for download.

You can download Python 3.5.4rc1 here, and you can download Python 3.4.7rc1 here.(image)



Peter Bengtsson: Find static files defined in django-pipeline but not found

Tue, 25 Jul 2017 00:12:58 +0000

If you're reading this you're probably familiar with how, in django-pipeline, you define bundles of static files to be combined and served. If you're not familiar with django-pipeline it's unlike this'll be of much help. The Challenge (aka. the pitfall) So you specify bundles by creating things in your settings.py something like this: PIPELINE = { 'STYLESHEETS': { 'colors': { 'source_filenames': ( 'css/core.css', 'css/colors/*.css', 'css/layers.css' ), 'output_filename': 'css/colors.css', 'extra_context': { 'media': 'screen,projection', }, }, }, 'JAVASCRIPT': { 'stats': { 'source_filenames': ( 'js/jquery', 'js/d3', 'js/collections/*', 'js/aplication', ), 'output_filename': 'js/stats', } } } You do a bit more configuration and now, when you run ./manage.py collectstatic --noinput Django and django-pipeline will gather up all static files from all Django apps installed, then start post processing then and doing things like concatenating them into one file and doing stuff like minification etc. The problem is, if you look at the example snippet above, there's a typo. Instead of js/application it's accidentally js/aplication. Oh noes!! What's sad is it that nobody will notice (running ./manage.py collectstatic will exit with a 0). At least not unless you do some careful manual reviewing. Perhaps you will notice later, when you've pushed the site to prod, that the output file js/stats actually doesn't contain the code from js/application. Or, you can automate it! A Solution (aka. the hack) I started this work this morning because the error actually happened to us. Thankfully not in production but our staging server produced a rendered HTML page with which was an actual file but it was 0 bytes. It wasn't that hard to figure out what the problem was because of the context of recent changes but it would have been nice to catch this during continuous integration. So what we did was add an extra class to settings.STATICFILES_FINDERS called myproject.base.finders.LeftoverPipelineFinder. So now it looks like this: # in settings.py STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'pipeline.finders.PipelineFinder', 'myproject.finders.LeftoverPipelineFinder', # the new hotness! ) And here's the class implementation: from pipeline.finders import PipelineFinder from django.conf import settings from django.core.exceptions import ImproperlyConfigured class LeftoverPipelineFinder(PipelineFinder): """This finder is expected to come AFTER django.contrib.staticfiles.finders.FileSystemFinder and django.contrib.staticfiles.finders.AppDirectoriesFinder in settings.STATICFILES_FINDERS. If a path is looked for here it means it's trying to find a file that none of the regular staticfiles finders couldn't find. """ def find(self, path, all=False): # Before we raise an error, try to find out where, # in the bundles, this was defined. This will make it easier to correct # the mistake. for config_name in 'STYLESHEETS', 'JAVASCRIPT': config = settings.PIPELINE[config_name] for key in config: if path in config[key]['source_filenames']: raise ImproperlyConfigured( 'Static file {!r} can not be found anywhere. Defined in ' "PIPELINE[{!r}][{!r}]['source_filenames']".format( path, [...]



Daniel Bader: How to Install and Uninstall Python Packages Using Pip

Tue, 25 Jul 2017 00:00:00 +0000

How to Install and Uninstall Python Packages Using Pip A step-by-step introduction to basic Python package management skills with the “pip” command. Learn how to install and remove third-party modules from PyPI. Python is approaching its third decade of good old age, and over the years many people have contributed to the creation of Python packages that perform specific functions and operations. As of this writing, there are ~112K packages listed on the PyPI website. PyPI is short for “Python Package Index”, a central repository for free third-party Python modules. This large and convenient module ecosystem is what makes Python so great to work with: You see, most Python programmers are really assemblers of Python packages, which take care of a big chunk of the programming load required by modern applications. Chances are that there is more than one Python package ready to be unleashed and help you with your specific programming needs. For instance, while reading dbader.org, you may notice that the pages on the site render emoji quite nicely. You may wonder… I’d like to use emoji on my Python app! Is there a Python package for that? Let’s find out! Here’s what we’ll cover in this tutorial: Finding Python Packages What to Look for in a Python Package Installing Python Packages With Pip Capturing Installed Python Packages with Requirements Files Visualizing Installed Packages Installing Python Packages From a requirements.txt File Uninstalling Python Packages With Pip Summary & Conclusion Finding Python Packages Let’s use the emoji use case as an example. We find emoji related Python packages by visiting the PyPI website and searching for emoji via the search box on the top right corner of the page. As of this writing, PyPI lists 94 packages, of which a partial list is shown below. Notice the “Weight*” header of the middle column. That’s a key piece of information. The weight value is basically a search scoring number, which the site calculates for each package to rank them and list them accordingly. If we read the footnote it tells us that the number is calculated by “the occurrence of search term weighted by field (name, summary, keywords, description, author, maintainer).” Does that mean that the top one is the best package? Not necessarily. Although uncommon, a package maintainer may stuff emoji into every field to try to top rank the package, which could well happen. Conversely, many developers don’t do their homework and don’t bother filling out all the fields for their packages, which results in those packages being ranked lower. You still need to research the packages listed, including a consideration for what your specific end use may be. For instance, a key question could be: Which environment do you want to implement emoji on? A terminal-based app, or perhaps a Django web app? If you are trying to display emoji on a django web app, you may be better off with the 10th package down the list shown above (package django-emoji 2.2.0). For our use case, let’s assume that we are interested in emoji for a terminal based Python app. Let’s check out the first one on our list (package emoji 0.4.5) by clicking on it. What to Look for in a Python Package The following are characteristics of a good Python package: Decent documentation: By reading it we can get a clue as to whether the package could meet our need or not; Maturity and stability: It’s been around for some time, proven by both its age and its successive versions; Number of contributors: Healthy packages (especially complex ones) tend to have a healthy number of maintainers; Maintenance: It undergoes maintenance on a regular basis (we live in an ever-evolving world). Although I would check it out, I wouldn’t rely too much on the development status listed for each package, that is, whether it’s a 4 - Beta or 5 - Production/Stable package. That c[...]



Python Anywhere: Outage report: 20, 21 and 22 July 2017

Mon, 24 Jul 2017 16:44:38 +0000

We had several outages over the last few days. The problem appears to be fixed now, but investigations into the underlying cause are still underway. This post is a summary of what happened, and what we know so far. Once we've got a better understanding of the issue, we'll post more. It's worth saying at the outset that while the problems related to the way we manage our users' files, those files themselves were always safe. While availability problems are clearly a big issue, we regard data integrity as more important. 20 July: the system update On Thursday 20 July, at 05:00 UTC, we released a new system update for PythonAnywhere. This was largely an infrastructural update. In particular, we updated our file servers from Ubuntu 14.04 to 16.04, as part of a general upgrade of all servers in our cluster. File servers are, of course, the servers that manage the files in your PythonAnywhere disk storage. Each server handles the data for a specific set of users, and serves the files up to the other servers in the cluster that need them -- the "execution" servers where your websites, your scheduled tasks, and your consoles run. The files themselves are stored on network-attached storage (and mirrored in realtime to redundant disks on a separate set of backup servers); the file servers simply act as NFS servers and manage a few simple things like disk quotas. While the system update took a little longer than we'd planned, once everything was up and running, the system looked stable and all monitoring looked good. 20 July: initial problems At 12:07 UTC our monitoring system showed a very brief issue. From some of our web servers, it appeared that access to one of our file servers, file-2, had very briefly slowed right down -- it was taking more than 30 seconds to list the specific directory that is monitored. The problem cleared up after about 60 seconds. Other file servers were completely unaffected. We did some investigations, but couldn't find anything, so we chalked it up as a glitch and kept an eye out for further problems. At 14:12 UTC it happened again, and then over the course of the afternoon, the "glitches" became more frequent and started lasting longer. We discovered that the symptom from the file server's side was that all of the NFS daemons -- the processes that together make up an NFS server -- would all become busy; system load would rise from about 1.5 to 64 or so. They were all waiting uninterruptably on what we think was disk I/O (status "D" in top). The problem only affected file-2 -- other file servers were all fine. Given that every file server had been upgraded to an identical system image, our initial suspicion was that there might be some kind of hardware problem. At 17:20 UTC we got in touch with AWS to discuss whether this was likely. By 19:10 our discussions with AWS had revealed nothing of interest. The "glitches" had become a noticeable problem for users, and we decided that while there was no obvious sign of hardware problems, it would be good to at least eliminate that as a possible cause, so we took a snapshot of all disks containing user data (for safety), then migrated the server to new hardware, causing a 20-minute outage for users on that file server (who were already seeing a serious slowdown anyway), and a 5-minute outage for everyone else, the latter because we had to reboot the execution servers After this move, at 19:57 UTC, everything seemed OK. Our monitoring was clear, and the users we were in touch with confirmed that everything was looking good. 21 July: the problem continues At 14:31 UTC on 21 July, we saw another glitch on our monitoring. Again, the problem cleared up quickly, but we started looking again into what could possibly be the cause. There were further glitches at 15:17 and 16:51, but then the problem seemed to clear up. Unfortunately at 22:44 it fl[...]



Will Kahn-Greene: Soloists: code review on a solo project

Mon, 24 Jul 2017 16:00:00 +0000

Summary

I work on some projects with other people, but I also spend a lot of time working on projects by myself. When I'm working by myself, I have difficulties with the following:

  1. code review
  2. bouncing ideas off of people
  3. peer programming
  4. long slogs
  5. getting help when I'm stuck
  6. publicizing my work
  7. dealing with loneliness
  8. going on vacation

I started a #soloists group at Mozilla figuring there are a bunch of other Mozillians who are working on solo projects and maybe if we all work alone together, then that might alleviate some of the problems of working solo. We hang out in the #soloists IRC channel on irc.mozilla.org. If you're solo, join us!

I keep thinking about writing a set of blog posts for things we've talked about in the channel and how I do things. Maybe they'll help you.

This one covers code review.

Read more… (10 mins to read)




Doug Hellmann: hmac — Cryptographic Message Signing and Verification — PyMOTW 3

Mon, 24 Jul 2017 13:00:43 +0000

The HMAC algorithm can be used to verify the integrity of information passed between applications or stored in a potentially vulnerable location. The basic idea is to generate a cryptographic hash of the actual data combined with a shared secret key. The resulting hash can then be used to check the transmitted or stored message … Continue reading hmac — Cryptographic Message Signing and Verification — PyMOTW 3(image)



Python Software Foundation: 2017 Bylaw Changes

Mon, 24 Jul 2017 11:35:16 +0000

The PSF has changed its bylaws, following a discussion and vote among the voting members. I'd like to publicly explain those changes. For each of the changes, I will describe  1.) what the bylaws used to say prior to June 2017 2.) what the new bylaws say and 3.) why the changes were implemented. Certification of Voting Members What the bylaws used to say Every member had to acknowledge that they wanted to vote/or not vote every year. What the bylaws now say The bylaws now say that the list of voters is based on criteria decided upon by the board. Why was this change made? The previous bylaws pertaining to this topic created too much work for our staff to handle and sometimes it was not done because we did not have the time resources to do it. We can now change the certification to something more manageable for our staff and our members. Voting in New PSF Fellow Members What the bylaws used to say We did not have a procedure in place for this in the previous bylaws. What the bylaws now say Now the bylaws allow any member to nominate a Fellow. Additionally, it gives the chance for the PSF Board to create a work group for evaluating the nominations. Why was this change made? We lacked a procedure. We had several inquiries and nominations in the past, but did not have a policy to respond with. Now that we voted in this bylaw, the PSF Board voted in the creation of the Work Group. We can now begin accepting new Fellow Members after several years. Staggered Board Terms What the bylaws used to say We did not have staggered board terms prior to June 2017. Every director would be voted on every term. What the bylaws now say The bylaws now say that in the June election, the top 4 voted directors would hold 3 year terms, the next 4 voted-in directors hold 2 year terms and the next 3 voted-in directors hold 1 year terms. That resulted in: Naomi Ceder (3 yr) Eric Holscher (3 yr) Jackie Kazil (3 yr) Paul Hildebrandt (3 yr) Lorena Mesa (2 yr) Thomas Wouters (2 yr) Kushal Das (2 yr) Marlene Mhangami (2 yr) Kenneth Reitz (1 yr) Trey Hunner (1 yr) Paola Katherine Pacheco (1 yr) Why was this change made? The main push behind this change is continuity. As the PSF continues to grow, we are hoping to make it more stable and sustainable. Having some directors in place for more than one year will help us better complete short-term and long-term projects. It will also help us pass on context from previous discussions and meetings. Direct Officers What the bylaws used to say We did not have Direct Officers prior to June 2017. What the bylaws now say The bylaws state that the current General Counsel and Director of Operations will be the Direct Officers of the PSF. Additionally, they state that the Direct Officers become the 12th and 13th members of the board giving them rights to vote on board business. Direct Officers can be removed by a.) fail of an approval vote, held on at least the same schedule as 3-year-term directors; b) leave the office associated with the officer director position; or c) fail a no-confidence vote. Why was this change made? In an effort to become a more stable and mature board, we are appointing two important positions to be directors of the board. Having the General Counsel and Director of Operations on the board helps us have more strength with legal situations and how the PSF operates. The two new Direct Officers are: Van Lindberg Ewa Jodlowska Delegating Ability to Set Compensation What the bylaws used to say The bylaws used to state that the President of the Foundation would direct how compensation of the Foundation’s employees was decided. What the bylaws now say The bylaws have changed so that the Board of Directors dec[...]



A. Jesse Jiryu Davis: Vote For Your Favorite PyGotham Talks

Mon, 24 Jul 2017 10:57:44 +0000

(image)

We received 195 proposals for talks at PyGotham this year. Now we have to find the best 50 or so. For the first time, we’re asking the community to vote on their favorite talks. Voting will close August 7th; then I and my comrades on the Program Committee will make a final selection.

Your Mission, If You Choose To Accept It

We need your help judging which proposals are the highest quality and the best fit for our community’s interests. For each talk we’ll ask you one question: “Would you like to see this talk at PyGotham?” Remember, PyGotham isn’t just about Python: it’s an eclectic conference about open source technology, policy, and culture.

You can give each talk one of:

  • +1 “I would definitely like to see this talk”
  •  0 “I have no preference on this talk”
  • -1 “I do not think this talk should be in PyGotham”

You can sign up for an account and begin voting at vote.pygotham.org. The site presents you with talks in random order, omitting the ones you have already voted on. For each talk, you will see this form:

(image)

Click “Save Vote” to make sure your vote is recorded. Once you do, a button appears to jump to the next proposal.

Our thanks to Ned Jackson Lovely, who made this possible by sharing the talk voting app “progcom” that was developed for the PyCon US committee.

So far, about 50 people have cast votes. We need to hear from you, too. Please help us shape this October’s PyGotham. Vote today!


Image: Voting in Brisbane, 1937




Catalin George Festila: Fix Gimp with python script.

Mon, 24 Jul 2017 10:24:40 +0000

Today I will show you how python language can help GIMP users.
From my point of view, Gimp does not properly import frames from GIF files.
This program imports GIF files in this way:
(image)
Using the python module, you can get the correct frames from the GIF file.
Here's my script that uses the python PIL module.
import sys
from PIL import Image, ImageSequence
try:
img = Image.open(sys.argv[1])
except IOError:
print "Cant load", infile
sys.exit(1)

pal = img.getpalette()
prev = img.convert('RGBA')
prev_dispose = True
for i, frame in enumerate(ImageSequence.Iterator(img)):
dispose = frame.dispose

if frame.tile:
x0, y0, x1, y1 = frame.tile[0][1]
if not frame.palette.dirty:
frame.putpalette(pal)
frame = frame.crop((x0, y0, x1, y1))
bbox = (x0, y0, x1, y1)
else:
bbox = None

if dispose is None:
prev.paste(frame, bbox, frame.convert('RGBA'))
prev.save('result_%03d.png' % i)
prev_dispose = False
else:
if prev_dispose:
prev = Image.new('RGBA', img.size, (0, 0, 0, 0))
out = prev.copy()
out.paste(frame, bbox, frame.convert('RGBA'))
out.save('result_%03d.png' % i)
Name the python script with convert_gif.py and then you can use it on the GIF file as follows:
C:\Python27>python.exe convert_gif.py 0001.gif
The final result has a smaller number of images than in Gimp, but this was to be expected.
(image)



Kevin Dahlhausen: Using Beets from 3rd Party Python Applications

Sun, 23 Jul 2017 20:47:55 +0000

I am thinking of using Beets as music library to update a project. The only example of using it this way is in the source code of the Beets command-line interface. That code is well-written but does much more than I need so I decided to create a simple example of using Beets in a 3rd party application. The hardest part turned out to be determining how to create a proper configuration pro grammatically. The final code is short:         config["import"]["autotag"] = False         config["import"]["copy"] = False         config["import"]["move"] = False         config["import"]["write"] = False         config["library"] = music_library_file_name         config["threaded"] = True This will create a configuration that keeps the music files in place and does not attempt to autotag them. Importating files requires one to subclass importer.ImportSession. A simple importer that serves to import files and not change them is:     class AutoImportSession(importer.ImportSession):         "a minimal session class for importing that does not change files"         def should_resume(self, path):             return True         def choose_match(self, task):             return importer.action.ASIS         def resolve_duplicate(self, task, found_duplicates):             pass         def choose_item(self, task):             return importer.action.ASIS That’s the trickiest part of it. The full demo is: # Copyright 2017, Kevin Dahlhausen # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. from beets import config from beets import importer from beets.ui import _open_library class Beets(object):     """a minimal wrapper for using beets in a 3rd party application        as a music library."""     class AutoImportSession(importer.ImportSession):         "a minimal session class for importing that does not change files"         def should_resume(self, path):             return True         def choose_match(self, task):             return importer.action.ASIS        &n[...]



Mike Driscoll: Python is #1 in 2017 According to IEEE Spectrum

Sun, 23 Jul 2017 18:53:59 +0000

It’s always fun to see what languages are considered to be in the top ten. This year, IEEE Spectrum named Python as the #1 language in the Web and Enterprise categories. Some of the Python community over at Reddit think that the scoring of the languages are flawed because Javascript is below R in web programming. That gives me pause as well. Frankly I don’t really see how anything is above Javascript when it comes to web programming.

Regardless, it’s still interesting to read through the article.

Related Articles







Trey Hunner: Craft Your Python Like Poetry

Sun, 23 Jul 2017 17:00:00 +0000

Line length is a big deal… programmers argue about it quite a bit. PEP 8, the Python style guide, recommends a 79 character maximum line length but concedes that a line length up to 100 characters is acceptable for teams that agree to use a specific longer line length. So 79 characters is recommended… but isn’t line length completely obsolete? After all, programmers are no longer restricted by punch cards, teletypes, and 80 column terminals. The laptop screen I’m typing this on can fit about 200 characters per line. Line length is not obsolete Line length is not a technical limitation: it’s a human-imposed limitation. Many programmers prefer short lines because long lines are hard to read. This is true in typography and it’s true in programming as well. Short lines are easier to read. In the typography world, a line length of 55 characters per line is recommended for electronic text (see line length on Wikipedia). That doesn’t mean we should use a 55 character limit though; typography and programming are different. Python isn’t prose Python code isn’t structured like prose. English prose is structured in flowing sentences: each line wraps into the next line. In Python, statements are somewhat like sentences, meaning each sentence begins at the start of each line. Python code is more like poetry than prose. Poets and Python programmers don’t wrap lines once they hit an arbitrary length; they wrap lines when they make sense for readability and beauty. I stand amid the roar Of a surf-tormented shore, And I hold within my hand Grains of the golden sand— How few! yet how they creep Through my fingers to the deep, While I weep—while I weep! O God! can I not grasp Them with a tighter clasp? O God! can I not save One from the pitiless wave? Is all that we see or seem But a dream within a dream? Don’t wrap lines arbitrarily. Craft each line with care to help readers experience your code exactly the way you intended. I stand amid the roar Of a surf-tormented shore, And I hold within my hand Grains of the golden sand— How few! yet how they creep Through my fingers to the deep, While I weep—while I weep! O God! can I not grasp Them with a tighter clasp? O God! can I not save One from the pitiless wave? Is all that we see or seem But a dream within a dream? Examples It’s not possible to make a single rule for when and how to wrap lines of code. PEP8 discusses line wrapping briefly, but it only discusses one case of line wrapping and three different acceptable styles are provided, leaving the reader to choose which is best. Line wrapping is best discussed through examples. Let’s look at a few examples of long lines and few variations for line wrapping for each. Example: Wrapping a Comprehension This line of code is over 79 characters long: 1 employee_hours = [schedule.earliest_hour for employee in self.public_employees for schedule in employee.schedules] Here we’ve wrapped that line of code so that it’s two shorter lines of code: 1 2 employee_hours = [schedule.earliest_hour for employee in self.public_employees for schedule in employee.schedules] We’re able to insert that line break in this line because we have an unclosed square bracket. This is called an implicit line continuation. Python knows we’re continuing a line of code whenever there’s a line break inside unclosed square brackets, curly braces, or parentheses. This code still isn’t very easy to read because the line break was inserted arbitrarily. We simply wrapped th[...]



Kay Hayen: Nuitka Release 0.5.27

Sun, 23 Jul 2017 15:42:00 +0000

This is to inform you about the new stable release of Nuitka. It is the extremely compatible Python compiler. Please see the page "What is Nuitka?" for an overview. This release comes a lot of bug fixes and improvements. Bug Fixes Fix, need to add recursed modules immediately to the working set, or else they might first be processed in second pass, where global names that are locally assigned, are optimized to the built-in names although that should not happen. Fixed in 0.5.26.1 already. Fix, the accelerated call of methods could crash for some special types. This had been a regress of 0.5.25, but only happens with custom extension types. Fixed in 0.5.26.1 already. Python3.5: For async def functions parameter variables could fail to properly work with in-place assignments to them. Fixed in 0.5.26.4 already. Compatability: Decorators that overload type checks didn't pass the checks for compiled types. Now isinstance and as a result inspect module work fine for them. Compatiblity: Fix, imports from __init__ were crashing the compiler. You are not supposed to do them, because they duplicate the package code, but they work. Compatiblity: Fix, the super built-in on module level was crashing the compiler. Standalone: For Linux, BSD and MacOS extension modules and shared libraries using their own $ORIGIN to find loaded DLLs resulted in those not being included in the distribution. Standalone: Added more missing implicit dependencies. Standalone: Fix, implicit imports now also can be optional, as e.g. _tkinter if not installed. Only include those if available. The --recompile-c-only was only working with C compiler as a backend, but not in the C++ compatibility fallback, where files get renamed. This prevented that edit and test debug approach with at least MSVC. Plugins: The PyLint plug-in didn't consider the symbolic name import-error but only the code F0401. Implicit exception raises in conditional expressions would crash the compiler. New Features Added support for Visual Studio 2017. Issue#368. Added option --python2-for-scons to specify the Python2 execute to use for calling Scons. This should allow using AnaConda Python for that task. Optimization References to known unassigned variables are now statically optimized to exception raises and warned about if the according option is enabled. Unhashable keys in dictionaries are now statically optimized to exception raises and warned about if the according option is enabled. Enable forward propagation for classes too, resulting in some classes to create only static dictionaries. Currently this never happens for Python3, but it will, once we can statically optimize __prepare__ too. Enable inlining of class dictionary creations if they are mere return statements of the created dictionary. Currently this never happens for Python3, see above for why. Python2: Selecting the metaclass is now visible in the tree and can be statically optimized. For executables, we now also use a freelist for traceback objects, which also makes exception cases slightly faster. Generator expressions no longer require the use of a function call with a .0 argument value to carry the iterator value, instead their creation is directly inlined. Remove "pass through" frames for Python2 list contractions, they are no longer needed. Minimal gain for generated code, but more lightweight at compile time. When compiling Windows x64 with MinGW64 a link library needs to be created for linking against the Python DLL. This one is now cached and re-used if already done. Use common code for NameError and UnboundLocalError exception code raises. In some c[...]



Patricio Paez: Concatenating strings with punctuation

Sun, 23 Jul 2017 11:45:24 +0000

Creating strings of the form “a, b, c and d” from a list [‘a’, ‘b’, ‘c’, ‘d’] is a task I faced some time ago, as I needed to include such strings in some HTML documents. The “,” and the “and” are included according to the amount of elements. [‘a’, ‘b’] yields “a and b“, [‘a’] yields “a” for example. In a recent review to the code, I changed the method from using string concatenation:

if len(items) > 1:
    text = items[0]
    for item in items[1:-1]:
        text += ', ' + item
    text += ' and ' + items[-1]
else:
    text = items[0]

to the use of slicing of the items list, addition of the resulting sublists and str.join to include the punctuation:

first = items[:1]
middle = items[1:-1]
last = items[1:][-1:]
first_middle = [', '.join(first + middle)]
text = ' and '.join(first_middle + last)

The old method requires an additonal elif branch to work when items is an empty list; the new method returns an empty string if the items list is empty. I share this tip in case it is useful to someone else.




Full Stack Python: How to Add Hosted Monitoring to Flask Web Applications

Sun, 23 Jul 2017 04:00:00 +0000

How do you know whether your application is running properly with minimal errors after building and deploying it? The fastest and easiest way to monitor your operational Flask web application is to integrate one of the many available fantastic hosted monitoring tools. In this post we will quickly add Rollbar monitoring to catch errors and visualize our application is running properly. There are also many other great hosted monitoring tools, which you can check out on the monitoring page. Our Tools We can use either Python 2 or 3 to build this tutorial, but Python 3 is strongly recommended for all new applications. I used Python 3.6.2 to execute my code. We will also use the following application dependencies throughout the post: Flask web framework, version 0.12.2 pyrollbar monitoring instrumentation library, version 0.13.12 blinker for signaling support in Flask applications so pyrollbar can report on all errors A free Rollbar account where we will send error data and view it when it is captured pip and the virtualenv virtual environment library, which come packaged with Python 3, to install and isolate the Flask and Rollbar libraries from other Python projects you are working on If you need help getting your development environment configured before running this code, take a look at this guide for setting up Python 3 and Flask on Ubuntu 16.04 LTS. All code in this blog post is available open source under the MIT license on GitHub under the monitor-flask-apps directory of the blog-code-examples repository. Use and abuse the source code as you desire for your own applications. Installing Dependencies Change into the directory where you keep your Python virtualenvs. Create a new virtual environment for this project using the following command. python3 -m venv monitorflask Activate the virtualenv. source monitorflask/bin/activate The command prompt will change after activating the virtualenv: Remember that you need to activate the virtualenv in every new terminal window where you want to use the virtualenv to run the project. Flask, Rollbar and Blinker can now be installed into the now-activated virtualenv. pip install flask==0.12.2 rollbar==0.13.12 blinker==1.4 Our required dependencies should be installed within our virtualenv after a short installation period. Look for output like the following to confirm everything worked. Installing collected packages: blinker, itsdangerous, click, MarkupSafe, Jinja2, Werkzeug, Flask, idna, urllib3, chardet, certifi, requests, six, rollbar Running setup.py install for blinker ... done Running setup.py install for itsdangerous ... done Running setup.py install for MarkupSafe ... done Running setup.py install for rollbar ... done Successfully installed Flask-0.12.2 Jinja2-2.9.6 MarkupSafe-1.0 Werkzeug-0.12.2 blinker-1.4 certifi-2017.4.17 chardet-3.0.4 click-6.7 idna-2.5 itsdangerous-0.24 requests-2.18.1 rollbar-0.13.12 six-1.10.0 urllib3-1.21.1 Now that we have our Python dependencies installed into our virtualenv we can create the initial version of our application. Building Our Flask App Create a folder for your project named monitor-flask-apps. Change into the folder and then create a file named app.py with the following code. import re from flask import Flask, render_template, Response from werkzeug.exceptions import NotFound app = Flask(__name__) MIN_PAGE_NAME_LENGTH = 2 @app.route("//") def show_page(page): try: valid_length = len(page) >= MIN_PAGE_NAME_LENGTH [...]






Catalin George Festila: About py-translate python module.

Sat, 22 Jul 2017 11:46:03 +0000

This python module is used for translating text in the terminal.
You can read and see examples with this API on this web page.
Features

  • Fast! Translate an entire book in less than 5 seconds.
  • Made for Python 3 but still works on Python 2
  • Fast and easy to install, easy to use
  • Supports translation from any language
  • Highly composable interface, the power of Unix pipes and filters.
  • Simple API and documentation

Installation 
C:\>cd Python27

C:\Python27>cd Scripts

C:\Python27\Scripts>pip install py-translate
Collecting py-translate
Downloading py_translate-1.0.3-py2.py3-none-any.whl (61kB)
100% |################################| 61kB 376kB/s
Installing collected packages: py-translate
Successfully installed py-translate-1.0.3

C:\Python27\Scripts>
Let's test it with a simple example:
>>> import translate
>>> dir(translate)
['TestLanguages', 'TestTranslator', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', 'accumulator', 'coroutine', 'coroutines', 'languages', 'print_table', 'push_url', 'set_task', 'source', 'spool', 'tests', 'translation_table', 'translator', 'write_stream']
>>> from translate import translator
>>> translator('ro', 'en', 'Consider ca dezvoltarea personala este un pas important')
[[[u'I think personal development is an important step', u'Consider ca dezvoltarea personala este un pas important', None, None, 0]], None, u'ro']
>>>



Catalin George Festila: Make one executable from a python script.

Sat, 22 Jul 2017 11:12:05 +0000

The official website of this tool tells us:PyInstaller bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules. PyInstaller supports Python 2.7 and Python 3.3+, and correctly bundles the major Python packages such as numpy, PyQt, Django, wxPython, and others.PyInstaller is tested against Windows, Mac OS X, and Linux. However, it is not a cross-compiler: to make a Windows app you run PyInstaller in Windows; to make a Linux app you run it in Linux, etc. PyInstaller has been used successfully with AIX, Solaris, and FreeBSD, but is not tested against them.The manual of this tool can be see it here.C:\Python27>cd ScriptsC:\Python27\Scripts>pip install pyinstallerCollecting pyinstaller Downloading PyInstaller-3.2.1.tar.bz2 (2.4MB) 100% |################################| 2.4MB 453kB/s....Collecting pypiwin32 (from pyinstaller) Downloading pypiwin32-219-cp27-none-win32.whl (6.7MB) 100% |################################| 6.7MB 175kB/s...Successfully installed pyinstaller-3.2.1 pypiwin32-219Also this will install PyWin32 python module.Let's make one test python script and then to make it executable. I used this python script to test it:from tkinter import Tk, Label, Buttonclass MyFirstGUI: def __init__(self, master): self.master = master master.title("A simple GUI") self.label = Label(master, text="This is our first GUI!") self.label.pack() self.greet_button = Button(master, text="Greet", command=self.greet) self.greet_button.pack() self.close_button = Button(master, text="Close", command=master.quit) self.close_button.pack() def greet(self): print("Greetings!")root = Tk()my_gui = MyFirstGUI(root)root.mainloop()The output of the command of pyinstaller:C:\Python27\Scripts>pyinstaller.exe --onefile --windowed ..\tk_app.py92 INFO: PyInstaller: 3.2.192 INFO: Python: 2.7.1393 INFO: Platform: Windows-10-10.0.1439393 INFO: wrote C:\Python27\Scripts\tk_app.spec95 INFO: UPX is not available.96 INFO: Extending PYTHONPATH with paths['C:\\Python27', 'C:\\Python27\\Scripts']96 INFO: checking Analysis135 INFO: checking PYZ151 INFO: checking PKG151 INFO: Building because toc changed151 INFO: Building PKG (CArchive) out00-PKG.pkg213 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9247)2120 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.2251 INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Windows-32bit\runw.exe2251 INFO: checking EXE2251 INFO: Rebuilding out00-EXE.toc because tk_app.exe missing2251 INFO: Building EXE from out00-EXE.toc2267 INFO: Appending archive to EXE C:\Python27\Scripts\dist\tk_app.exe2267 INFO: Building EXE from out00-EXE.toc completed successfully.Then I run the executable output: C:\Python27\Scripts>C:\Python27\Scripts\dist\tk_app.exeC:\Python27\Scripts>...and working well.The output file come with this icon:Also you can make changes by using your icons or set the type of this file, according to VS_FIXEDFILEINFO structure.You need to have the icon file and / or version.txt file for VS_FIXEDFILEINFO structure. Let's see the version.txt file:# UTF-8## For more details about fixed file info 'ffi' see:# http://msdn.microsoft.com/en-us/library/ms646997.aspxVSVersionInfo( ffi=FixedFileInfo( # filevers and prodvers should be always a tuple with four items: (1[...]



Catalin George Festila: Python Qt4 - part 001.

Sat, 22 Jul 2017 11:11:54 +0000

Today I started with PyQt4 and python version :Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32To install PyQt4 I used this link to take the executable named: PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32.exe.The name of this executable shows us: can be used with python 2.7.x versions and come with Qt4.8.7 for our 32 bit python.I start with a default Example class to make a calculator interface with PyQt4. This is my example:#!/usr/bin/python# -*- coding: utf-8 -*-import sysfrom PyQt4 import QtGui"""Qt.Gui calculator example"""class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): title = QtGui.QLabel('Title') titleEdit = QtGui.QLineEdit() grid = QtGui.QGridLayout() grid.setSpacing(10) grid.addWidget(title, 0, 0) grid.addWidget(titleEdit,0,1,1,4) self.setLayout(grid) names = ['Cls', 'Bck', 'OFF', '/', '.', '7', '8', '9', '*', 'SQR', '3', '4', '5', '-', '=', '0', '1', '2', '+'] positions = [(i,j) for i in range(1,5) for j in range(0,5)] for position, name in zip(positions, names): if name == '': continue button = QtGui.QPushButton(name) grid.addWidget(button, *position) self.move(300, 250) self.setWindowTitle('Calculator') self.show() def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_())if __name__ == '__main__': main()The example is simple.First you need a QGridLayout - this make a matrix. I used labels, line edit and buttons all from QtGui: QLabel, QLineEdit and QPushButton.First into this matrix - named grid is: Title and edit area named titleEdit.This two is added to the grid - matrix with addWidget.The next step is to put all the buttons into one array.This array will be add to the grid matrix with a for loop. To make this add from array to matrix I used the zip function. The zip function make an iterator that aggregates elements from each of the iterables.Also I set the title to Calculator with setWindowTitle.I have not implemented the part of the events and the calculation.The main function will start the interface by using the QApplication.The goal of this tutorial was the realization of the graphical interface with PyQt4.This is the result of my example: [...]



Catalin George Festila: The pyquery python module.

Sat, 22 Jul 2017 11:11:12 +0000

This tutorial is about pyquery python module and python 2.7.13 version.
First I used pip command to install it.
C:\Python27>cd Scripts

C:\Python27\Scripts>pip install pyquery
Collecting pyquery
Downloading pyquery-1.2.17-py2.py3-none-any.whl
Requirement already satisfied: lxml>=2.1 in c:\python27\lib\site-packages (from pyquery)
Requirement already satisfied: cssselect>0.7.9 in c:\python27\lib\site-packages (from pyquery)
Installing collected packages: pyquery
Successfully installed pyquery-1.2.17
I try to install with pip and python 3.4 version but I got errors.
The development team tells us about this python module:
pyquery allows you to make jquery queries on xml documents. The API is as much as possible the similar to jquery. pyquery uses lxml for fast xml and html manipulation.
Let's try a simple example with this python module.
The base of this example is find links by html tag.
from pyquery import PyQuery

seeds = [
'https://twitter.com',
'http://google.com'
]

crawl_frontiers = []

def start_crawler():
crawl_frontiers = crawler_seeds()

print(crawl_frontiers)

def crawler_seeds():
frontiers = []
for index, seed in enumerate(seeds):
frontier = {index: read_links(seed)}
frontiers.append(frontier)

return frontiers

def read_links(seed):
crawler = PyQuery(seed)
return [crawler(tag_a).attr("href") for tag_a in crawler("a")]

start_crawler()
The read_links function take links from seeds array.
To do that, I need to read the links and put in into another array crawl_frontiers.
The frontiers array is used just for crawler process.
Also this simple example allow you to understand better the arrays.
You can read more about this python module here .