skip to navigation
skip to content

Planet Python

Last update: July 26, 2016 10:49 AM

July 26, 2016


Graham Dumpleton

Installing mod_wsgi on MacOS X with native operating system tools.

Operating systems inevitably change over time, and because writing documentation is often an after thought or developers have no time, the existing instructions on how to install a piece of software can suffer bit rot and stop working. This has been the case for a while with various parts of the documentation for mod_wsgi. This post is a first step at least in getting the documentation for

July 26, 2016 10:37 AM


David MacIver

It might be worth learning an ML-family language

It’s long been a popular opinion that learning Haskell or another similar language will make you a better programmer. I think this is probably true, but I think it’s an overly specific claim because learning almost anything will make you a better programmer, and I’ve not been convinced that Haskell is a much better choice than many other things in terms of reshaping your thinking.

But… I’ve been noticing something recently when teaching Python programmers to use Hypothesis that has made me reconsider somewhat. Not so much a fundamental reshaping of the way you think as a highly useful microskill that people seem to struggle to learn in dynamically typed languages.

That skill is this: Keeping track of what the type of the value in a variable is.

That may not seem like an important skill in a dynamic language, but it really is: Although functions will tend to be more lenient about what type of value they accept (is it a list or a tuple? Who cares!), they will tend to go wrong in interesting and confusing ways when you get it too wrong, and you then waste valuable debugging time trying to figure out what you did wrong. A good development workflow will typically let you find the problem, but it will still take significantly longer than just not making the mistake in the first place.

In particular this seems to come up when the types are related but distinct. Hypothesis has a notion of a “strategy”, which is essentially a data generator, and people routinely seem to get confused as to whether something is a value of a type, a strategy for producing values of that type, or a function that returns a strategy for producing the type.

It might be that I’ve just created a really confusing API, but I don’t think that’s it – people generally seem to really like the API and this is by far the second most common basic usage error people make with it (the first most common is confusing the functions one_of and sampled_from, which do similar but distinct things. I’m still trying to figure out better names for them).

It took me a while to notice this because I just don’t think of it as a difficult thing to keep track of, but it’s definitely a common pattern. It also appears to be almost entirely absent from people who have used Haskell (and presumably other ML-family languages – any statically typed language with type-inference and a bias towards functional programming really) but I don’t know of anyone who has tried to use Hypothesis knowing an ML-family language without also knowing Haskell).

I think the reason for this is that in an ML family language, where the types are static but inferred, you are constantly playing a learning game with the compiler as your teacher: Whenever you get this wrong, the compiler tells you immediately that you’ve done so and localises it to the place where you’ve made the mistake. The error messages aren’t always that easy to understand, but it’s a lot easier to figure out where you’ve made the mistake than when the error message is instead “AttributeError: ‘int’ object has no attribute ‘kittens'” in some function unrelated to where you made the actual error.

This is probably a game worth playing. If people are making this error when using Hypothesis, they I’d expect them to be making it in many other places too. I don’t expect many of these errors are making it through to production (especially if your code is well tested), but they’re certainly wasting time while developing.

In terms of which ML-family language to choose for this, I’m not sure. I haven’t actually used it myself yet (I don’t really have anything I want to write in the space that it targets), but I suspect Elm is probably the best choice. They’ve done some really good work on making type errors clear and easy to understand, which is likely exactly what you need for this sort of learning exercise.

 

July 26, 2016 09:23 AM


Daniel Bader

How to use Python’s min() and max() with nested lists

How to use Python’s min() and max() with nested lists

Let’s talk about using Python’s min and max functions on a list containing other lists. Sometimes this is referred to as a nested list or a lists of lists.

Finding the minimum or maximum element of a list of lists1 based on a specific property of the inner lists is a common situation that can be challenging for someone new to Python.

To give us a more concrete example to work with, let’s say we have the following list of item, weight pairs2:

nested_list = [['cherry', 7], ['apple', 100], ['anaconda', 1360]]

We want Python to select the minimum and maximum element based on each item’s weight stored at index 1. We expect min and max to return the following elements:

But if we simply call min and max on that nested list we don’t get the results we expected.

The ordering we get seems to be based on the item’s name, stored at index 0:

>>> min(nested_list)
['anaconda', 1360]  # Not what we expected!

>>> max(nested_list)
['cherry', 7]  # Not what we expected!

Alright, why does it pick the wrong elements?

Let’s stop for a moment to think about how Python’s max function works internally. The algorithm looks something like this:

def my_max(sequence):
    """Return the maximum element of a sequence"""
    if not sequence:
        raise ValueError('empty sequence')

    maximum = sequence[0]

    for item in sequence:
        if item > maximum:
            maximum = item

    return maximum

The interesting bit of behavior here can be found in the condition that selects a new maximum: if item > maximum:.

This condition works nicely if sequence only contains primitive types like int or float because comparing those is straightforward (in the sense that it’ll give an answer that we intuitively expect; like 3 > 2).

However, if sequence contains other sequences then things get a little more complex. Let’s look at the Python docs to learn how Python compares sequences:

Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.

When max needs to compare two sequences to find the “larger” element then Python’s default comparison behavior might not be what we want3.

Now that we understand why we get an unexpected result we can think about ways to fix our code.

How can we change the comparison behavior?

We need to tell max to compare the items differently.

In our example, Python’s max looks at the first item in each inner list (the string cherry, apple, or anaconda) and compares it with the current maximum element. That’s why it returns cherry as the maximum element if we just call max(nested_list).

How do we tell max to compare the second item of each inner list?

Let’s imagine we had an updated version of my_max called my_max_by_weight that uses the second element of each inner list for comparison:

def my_max_by_weight(sequence):
    if not sequence:
        raise ValueError('empty sequence')

    maximum = sequence[0]

    for item in sequence:
        # Compare elements by their weight stored
        # in their second element.
        if item[1] > maximum[1]:
            maximum = item

    return maximum

That would do the trick! We can see that my_max_by_weight selects the maximum element we expected:

>>> my_max_by_weight(nested_list)
['anaconda', 1360]

Now imagine we needed to find the maximum of different kinds of lists.

Perhaps the index (or key) we’re interested in won’t always be the second item. Maybe sometimes it’ll be the third or fourth item, or a different kind of lookup is necessary all together.

Wouldn’t it be great if we could reuse the bulk of the code in our implementation of my_max? Some parts of it will always work the same, for example checking if an empty sequence was passed to the function.

How can we make max() more flexible?

Because Python allows us to treat functions as data we can extract the code selecting the comparison key into its own function. We’ll call that the key func. We can write different kinds of key funcs and pass them to my_max as necessary.

This gives us complete flexibility! Instead of just being able to choose a specific list index for the comparison, like index 1 or 2, we can tell our function to select something else entirely — for example, the length of the item’s name.

Let’s have a look at some code that implements this idea:

def identity(x):
    return x

def my_max(sequence, key_func=None):
    """
    Return the maximum element of a sequence.
    key_func is an optional one-argument ordering function.
    """
    if not sequence:
        raise ValueError('empty sequence')

    if not key_func:
        key_func = identity

    maximum = sequence[0]

    for item in sequence:
        # Ask the key func which property to compare
        if key_func(item) > key_func(maximum):
            maximum = item

    return maximum

In the code example you can see how by default we let my_max use a key func we called identity, which just uses the whole, unmodified item to do the comparison.

With identity as the key func we expect my_max to behave the same way max behaves.

nested_list = [['cherry', 7], ['apple', 100], ['anaconda', 1360]]

>>> my_max(nested_list)
['cherry', 7]

And we can confirm that we’re still getting the same (incorrect) result as before, which is a pretty good indication that we didn’t screw up the implementation completely 😃.

Now comes the cool part — we’re going to override the comparison behavior by writing a key_func that returns the second sub-element instead of the element itself:

def weight(x):
    return x[1]

>>> my_max(nested_list, key_func=weight)
['anaconda', 1360]

And voilà, this is the maximum element we expected to get!

Just to demonstrate the amount of flexibility this refactoring gave us, here’s a key_func that selects the maximum element based on the length of the item’s name:

def name_length(x):
    return len(x[0])

>>> my_max(nested_list, key_func=name_length)
['anaconda', 1360]

Is there a shorthand for this stuff?

Instead of defining the key func explicitly with def and giving it a name we can also use Python’s lambda keyword to define a function anonymously. This shortens the code quite a bit (and won’t create a named function):

my_max(nested_list, key_func=lambda x: x[1])
>>> ['anaconda', 1360]

To make the naming a little slicker (albeit less expressive) imagine we’ll shorten the key_func arg to key and we’ve arrived at a code snippet that works with the max function in vanilla Python.

This means we’ll no longer need our own re-implementation of Python’s max function to find the “correct” maximum element:

# This is pure, vanilla Python:
>>> max(nested_list, key=lambda x: x[1])
['anaconda', 1360]

The same also works for Python’s built-in min:

>>> min(nested_list, key=lambda x: x[1])
['cherry', 7]

It even works for Python’s sorted function, making the “key func” concept really valuable in a number of situations you might face as a Python developer:

>>> sorted(nested_list, key=lambda x: x[1])
[['anaconda', 1360], ['apple', 100], ['cherry', 7]]

Try it out yourself

I hope this post helped you out. What started out as a simple question ended up being a little more involved than you may have expected. But it’s often like that when you learn about new programming concepts.

Feel free to drop me a line of Twitter or over email if you got stuck anywhere. I’d love to improve this tutorial over time :)


  1. Sometimes you’ll see tuples used for the inner lists. Using tuples instead of lists doesn’t really make a difference for how min and max work, but in some cases it can bring a performance benefit. Nothing we’ll have to worry about for now. The code in this tutorial will work fine on a list of tuples. 

  2. I actually googled these for you. Apparently the average cherry sold in a super market weighs 7 grams. I’m not 100 per cent sure about anacondas though. 

  3. Note that Python strings are also sequences so when you compare two strings they will be compared character by character. 

July 26, 2016 12:00 AM

July 25, 2016


Robin Wilson

Showing code changes when teaching

A key – but challenging – part of learning to program is moving from writing technically-correct code “that works” to writing high-quality code that is sensibly decomposed into functions, generically-applicable and generally “good”. Indeed, you could say that this is exactly what Software Carpentry is about – taking you from someone bodging together a few bits of wood in the shed, to a skilled carpenter. As well as being challenging to learn, this is also challenging to teach: how should you show the progression from “working” to “good” code in a teaching context?

I’ve been struggling with this recently as part of some small-group programming teaching I’ve been doing. Simply showing the “before” and “after” ends up bombarding the students with too many changes at once: they can’t see how you get from one to the other, so I want some way to show the development of code over time as things are gradually done to it (for example, moving this code into a separate function, adding an extra argument to that function to make it more generic, renaming these variables and so on). Obviously when teaching face-to-face I can go through this interactively with the students – but some changes to real-world code are too large to do live – and students often seem to find these sorts of discussions a bit overwhelming, and want to refer back to the changes and reasoning later (or they may want to look at other examples I’ve given them). Therefore, I want some way to annotate these changes to give the explanation (to show why we’re moving that bit of code into a separate function, but not some other bit of code), but to still show them in context.

Exactly what code should be used for these examples is another discussion: I’ve used real-world code from other projects, code I’ve written specifically for demonstration, code I’ve written myself in the past and sometimes code that the students themselves have written.

So far, I’ve tried the following approaches for showing these changes with annotation:

  1. Making all of the changes to the code and providing a separate document with an ordered list of what I’ve changed and why.
    Simple and low-tech, but often difficult for the students to visualise each change
  2. The same as above but committing between each entry in the list.
    Allows them to step through git commits if they want, and to get back to how the code was after each individual change – but many of the students struggle to do this effectively in git, and it adds a huge technological barrier…particularly with Git’s ‘interesting’ user-interface.
  3. The same as above, but using Github’s line comments feature to put comments at specific locations in the code.
    Allows annotations at specific locations in the code, but rather clunky to step through the full diff view of commits in order using Github’s UI.

I suspect any solution will involve some sort of version control system used in some way (although I’m not sure that standard diffs are quite the best way to represent changes for this particular use-case), but possibly with a different interface on it.

Is this a problem anyone else has faced in their teaching? Can you suggest any tools or approaches that might make this easier – for both the teacher and students?

(This has also been cross-posted on the Software Carpentry blog)

July 25, 2016 04:07 PM


Django Weblog

Registration for Django: Under the Hood 2016 is now open!

Django: Under the Hood is back for its third edition!

DUTH is an annual Django conference that takes place in Amsterdam, the Netherlands. On 3rd - 6th November this year, we're going to see 9 deep dive talks into topics of Django channels, testing, Open Source funding, JavaScript, Django forms validation, debugging and many more.

Django: Under the Hood also gives the opportunity to bring many Django core developers to work together and shape the future of Django with a group of 300 passionate Django developers attending the conference.

This year, the registration process for the conference became a lottery to avoid mad rush and tickets selling out in minutes.

Registration

You can register now, and the lottery is only open until 26th of July at noon Amsterdam time.

If you want to make sure that tickets for your team are reserved and set aside, Django: Under the Hood still has few sponsorship opportunities open. Please get in touch on hello@djangounderthehood.com.

July 25, 2016 02:35 PM


Doug Hellmann

threading — Manage Concurrent Operations — PyMOTW 3

Using threads allows a program to run multiple operations concurrently in the same process space. Read more… This post is part of the Python Module of the Week series for Python 3. See PyMOTW.com for more articles from the series.

July 25, 2016 01:00 PM


Mike Driscoll

PyDev of the Week: Nicholas Tollervey

This week we welcome Nicholas Tollervey (@ntoll) as our PyDev of the Week. He is the author of the Python in Education booklet and the co-author of Learning jQuery Deferreds: Taming Callback Hell with Deferreds and Promises. He was one of the co-founders of the London Python Code Dojo. You should check out his website to see what he’s up to. Let’s spend some time learning more about our fellow Pythonista!

Can you tell us a little about yourself (hobbies, education, etc):

I’m a UK based freelance programmer.

I first learned to program as a kid using a BBC micro. Like many UK programmers of a certain age I go all misty-eyed when remembering the sense of excitement, adventure and possibilities that such a machine generated.

As a teenager I had music lessons and ended up getting my undergraduate degree from the Royal College of Music in London. It was a lot of fun: among other things, my composition tutor used to teach Andrew Lloyd Webber and my keyboard harmony teacher was the Queen’s personal organist (he was director of the Chapel Royal). I played professionally for a while before embarking on a career teaching music. I also went on to read for degrees in philosophy and then computing ~ my twenties were all about
teaching and learning cool stuff!

When I started a family I decided I also needed a career change and remembered back to the fun I’d had on the BBC micro so reverted back to programming. Now I spend a lot of my free time making music.

Why did you start using Python?

I was a senior .NET developer in an investment bank in London – I worked with the quants on a suite of in-house SCM related development tools. They wanted to be able to script some of my software so I looked into IronPython as a potential solution. As well as being impressed by Python-the-language, these investigations led me to the remarkably friendly UK Python community and I realised I was among friends. I took some months off to learn Python and re-started my programming career as a junior Python developer and have never looked back! That was six or seven years ago.

What other programming languages do you know and which is your favorite?

Due to the large amount of web-related programming I’ve done, JavaScript is my second language after Python. Obviously I know C# and VB (although they’re very rusty) and I’ve used Ruby, Java and Erlang at various points in my career. I tend to dip my toes into other languages because it’s interesting to get another perspective, although I always love returning to Python: it’s like putting on a favourite pair of comfortable slippers. 😉

Lisp is a beautiful language. I once wrote a version of the Lisp programming language (for fun) although that’ll never see the light of day. It proved to me how difficult it is to design a programming language and made me realise what an amazing job Guido and the core developers do with Python.

What projects are you working on now?

I proposed, coordinated and contributed to the PSF’s involvement with the new BBC micro:bit project. A micro:bit is a small programmable device aimed at 11 year-olds. A million of them have been given out to all the UK’s 11 and 12 year olds. The intention is to recreate the glory-days of the BBC micro (see above) and inspire a new generation of digital makers. My current focus is on making the fruits of these efforts sustainable.

Thanks to the amazing work of Damien George, the device runs MicroPython, it’s an open source project so anyone can contribute code.

I’ve also written many tools related to the project: Mu (a code editor for kids that’s written in Python), a child-friendly web-based Python editor for the microbit.co.uk website and various Python related utilities that make it easy to interact with the device. I’ve also been learning some C and C++ as I’ve made minor contributions to the MicroPython code that runs on the micro:bit. It’s fascinating and fun to be a complete beginner again.

Which Python libraries are your favorite (core or 3rd party)?

I love working with asyncio. I’ve written a distributed hash table with it (as a fun educational exercise). I also firmly believe that Python 3 is what everyone should be using for new projects. 😉

I also love simplicity and, happily, there are many examples of simple libraries in the Python ecosystem.

I’m especially impressed with the PyGameZero, GPIOZero and NetworkZero libraries (with more “WhateverZero” libraries coming soon). The idea is to write a simple API above an existing-yet-powerful library to make it more accessible to beginner programmers. Once beginner programmers have the basic concepts worked out they can begin the process of pulling back the curtain and exploring the underlying library. Dan Pope (PyGameZero), Ben Nuttall (GPIOZero) and Tim Golden (NetworkZero) should be showered with praise for their extraordinary efforts (all three of them spent significant amounts of time collaborating with teachers to work out how best to design their library to appeal to new programmers).

Where do you see Python going as a programming language?

What is your take on the current market for Python programmers?

There’s a huge demand for Python developers here in the UK across all sectors. This situation doesn’t look like it’ll change for the foreseeable future (see my comment above about education).

Is there anything else you’d like to say?

The best of Python is in its community: I’d like to publicly say “thank you” to the many people who have helped me as I continue to learn and use this remarkable language.

Thanks for doing the interview!

July 25, 2016 12:30 PM


Caktus Consulting Group

ShipIt Day Recap - July 2016

We finished up last week with another successful ShipIt Day. ShipIt Days are quarterly events where we put down client work for a little bit and focus on learning, stretching ourselves, and sharing. Everyone chooses to work together or individually on an itch or a project that has been on the back of their mind for the last few months. This time, we stretched ourselves by trying out new frameworks, languages, and pluggable apps. Here are some of the projects we worked on during ShipIt Day:

TinyPNG Image Optimization in Django

Kia and Dmitriy started on django_tinypng. This project creates a new OptimizedImageField in Django which uses the tinify client for the tinypng project to compress PNG files. This means that files uploaded by users can be reduced in size by up to 70% without perceivable differences in image quality. Reducing image sizes can free up disk space on servers and improve page load speeds, significantly improving user experiences.

Maintaining Clean Python Dependencies / Requirements.txts

Rebecca Muraya researched how we, as developers, can consistently manage our requirements files. In particular, she was looking for a way to handle second-level (and below) dependencies -- should these be explicitly pinned, or not? Rebecca did some research and found the pip-tools package as a possible solution and presented it to the group. Rebecca described pip-tools as a requirements file compiler which gives you the flexibility to describe your requirements at the level that makes sense to your development team, but have them consistently managed across development, testing, and production environments. Rebecca presented ideas for integrating pip-tools into our standard development workflows.

Elm

Neil and Dan each independently decided to build projects using Elm, a functional language for frontend programming.They were excited to demonstrate how they rearranged their concept of development temporarily to focus on state and state changes in data structures. And then, how these state changes would be drawn on the screen dynamically. Dan mentioned missing HTML templates, the norm in languages where everything is not a function, but loved that it forced programmers to handle all cases as a result of the strict type system (unlike Python). Neil dug not only into Elm on the frontend, but also a functional backend framework Yesod and the Haskell language. Neil built a chat app using Websockets and Yesod channels.

Firebase + React = Bill Tracking

Hunter built a bill tracking project using Google’s Firebase database and the React frontend framework. Hunter walked us through his change in thought process from writing code as a workflow to writing code that changes state and code that updates the drawing of the state. It was great to see the Firebase development tools and learn a bit more about React.

Open Data Policing Database Planning

Rebecca Conley worked on learning some new things about database routing and some of the statistics that go into the Open Data Policing project. She also engaged Caelan, Calvin’s son who was in the office during the day, to build a demonstration of what she had been working on.

Mozilla’s DXR Python Parser Contributions

DXR is a web-based code indexing and searching tool built by Mozilla. For his project, Jeff Bradberry decided to create a pull request contribution to the project that improves Python code indexing. Specifically, he used Python’s own Abstract Syntax Tree (AST), a way to introspect and consider Python code as structured data to be analyzed. Jeff’s contribution improves the analysis of nested calls like a(b(c())) and chained calls like a().b().c().

Hatrack: We all wear lots of hats, switch contexts easily

Rather than working on something completely new, Calvin decided to package up and share a project he has been working off-and-on in his free time called Hatrack. Hatrack attempts to solve a problem that web developers frequently face: changing projects regularly means switching development environments and running lots of local development web servers. Hatrack notices what projects you try to load up in your browser and starts up the development server automatically. For his project, Calvin put Hatrack up on NPM and shared it with the world. You can also check out the Hatrack source code on Github.

Software Testing Certification

Sometimes ShipIt Day can be a chance to read or review documentation. Charlotte went this route and reviewed the requirements for the International Software Testing Qualifications Board (ISTQB)’s certification programs. Charlotte narrowed down on a relevant certification and began reviewing the study materials. She came back to the group and walked us through some QA best practices including ISTQB’s seven principles of software testing.

Cross Functional Depth & Breadth

Sarah began work to visualize project teams’ cross-functional specialties with an eye towards finding skill gaps. She built out a sample questionnaire for the teams and a method of visualizing the skill ranges in specific areas on a team. This could be used in the future when team members move between teams and for long-term planning.

Demographic Data

Colin and Alex each separately investigated adding demographic data into existing project data sets using SunlightLab’s Python bindings for the Cenus API. While the Census dataset contains tens of thousands of variables in various geographic resolution levels (states, counties, down to block groups), using the Census’ API and Sunlight Lab’s bindings made it relatively quick and painless.

July 25, 2016 12:00 PM


Ilian Iliev

SQLAlchemy and "Lost connection to MySQL server during query"

... a weird behaviour when pool_recycle is ignored

Preface: This is quite a standard problem for apps/websites with low traffic or those using heavy caching and hitting the database quite seldom. Most of the articles you will find on the topic will tell you one thing - change the wait_timeout setting in the database. Unfortunately in some of the cases this disconnect occurs much earlier than the expected wait_timeout (default ot 8 hours). If you are in one of those cases keep reading.

This issue haunted our team for weeks. When we first faced it the project that was raising it was still in dev phase so it wasn't critical but with getting closer to the release data we started to search for solution. We have read several articles and decided that pool_recycle is our solution.

Adding pool_recycle: According to SQL Alchemy's documentation pool_recycle "causes the pool to recycle connections after the given number of seconds has passed". Nice, so if you recycle the connection in intervals smaller that the await_timeout the error above should not appear. Let's try it out:

import time from sqlalchemy.engine import create_engine url = 'mysql+pymysql://user:pass@127.0.0.1:3306/db' engine = create_engine(url, pool_recycle=1).connect() query = 'SELECT NOW();' while True: print('Q1', engine.execute(query).fetchall()) engine.execute('SET wait_timeout=2') time.sleep(3) print('Q2', engine.execute(query).fetchall())

So what does the code do - we create a connection to a local MySQL server and state that it should be recycled every second(line 7). Then we execute a simple query (line 12) just to verify that the connection is working.
We set the wait_timeout to 2 second and wait for 3. At this stage the connection to the server will timeout, but SQL Alchemy should recycle it, so the last query should be executed successfully and the loop should continue.
Unfortunately the results looks like:

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query') [SQL: 'SELECT NOW();']

Wait, what happened, why is not the connection recycled?

Solution: Well, as with all such problems the solution was much simpler compared to the time it took us to find it (we fought with this for days). The only change that solved it was on line 7:

engine = create_engine(url, pool_recycle=1) # the result Q1 [(datetime.datetime(2016, 7, 24, 20, 51, 41),)] Q2 [(datetime.datetime(2016, 7, 24, 20, 51, 44),)] Q1 [(datetime.datetime(2016, 7, 24, 20, 51, 44),)] Q2 [(datetime.datetime(2016, 7, 24, 20, 51, 47),)]

Have you spot the difference? We are not calling the connect() method any more.

Final words: To keep it honest, I don't know why this solved the issue. Hopefully someone more familiar with SQL Alchemy will come with a reasonable explanation for it. The bad part is that the examples in the official docs are using "connect". So it is either a bug or a bad documentation. I will send this article to SQL Alchemy's Twitter account so hopefully we will see some input from them. Till then, if any of you have an idea explanation about the behaviour I'll be happy to hear it.

July 25, 2016 10:40 AM

July 24, 2016


Weekly Python StackOverflow Report

(xxix) stackoverflow python report

These are the ten most rated questions at Stack Overflow last week.
Between brackets: [question score / answers count]
Build date: 2016-07-24 15:23:27 GMT


  1. Why and how are Python functions hashable? - [24/3]
  2. why is a sum of strings converted to floats - [12/1]
  3. Python: PEP 8 class name as variable - [11/2]
  4. How to assign member variables temporarily? - [8/5]
  5. Simplifying / optimizing a chain of for-loops - [8/4]
  6. Is there a pythonic way to process tree-structured dict keys? - [8/2]
  7. list of objects python - [8/1]
  8. Pandas rolling computations for printing elements in the window - [8/1]
  9. django-debug-toolbar breaking on admin while getting sql stats - [8/1]
  10. How to free memory of python deleted object? - [8/1]

July 24, 2016 03:24 PM


Kracekumar Ramaraju

HTTP Exception as control flow

As per Wikipedia , Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the flow of program execution.

In Python errors like SyntaxError, ZeroDivisionError are exceptions. Exception paves the way to alter the normal execution path.

While working with API, a web request goes through the following process, authentication, authorization, input validation, business logic and finally, the response is given out. Depending on complexity, few more steps can be involved.

Consider simple class based view. View looks similar to Django or Flask view.

The important point here is the number of if conditions inside post method. Though an another function can hold the code inside if validator.validate() block. Still, the if condition doesn’t go away. check_and_create_bucket returns a tuple. The caller needs to check the tuple and follow next steps. When the number of layers or functions increases, conditions also increases. These conditions are unnecessary and handled til bottom level.

On the other hand, if these function/method returns the object on success and raises exception, the code is easier to read. At a higher level, the exception can be handled.

Consider the modified code where all functions return an object or raise an exception.

In the above code, the validator raises an exception on any failure. Even if validator doesn’t raise an exception, the view will have only one if condition. At a higher level code is structured like a list of commands.

The illustrated example has only one business action post validation. When the number of business conditions increases, this method is smoother to manage.

Writing try/except code on every view is repetition. The little decorator can be handy.

wrapt is the useful library for writing decorators, wrappers, and monkey patching.

Designing code base around exceptions is highly useful. All the exceptions are handled at a higher level and reduce if/else complexity.

Django and Flask has inbuilt HTTP Exceptions support.

July 24, 2016 02:37 PM


Eli Bendersky

Drawing animated GIFs with matplotlib

This is a brief post on how to draw animated GIFs with Python using matplotlib. I tried the code shown here on a Ubuntu machine with ImageMagick installed. ImageMagick is required for matplotlib to render animated GIFs with the save method.

Here's a sample animated graph:

Animated GIF produced with matplotlib

A couple of things to note:

  1. The scatter part of the graph is unchanging; the line is changing.
  2. The X axis title is changing in each frame.

Here's the code that produces the above:

import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
fig.set_tight_layout(True)

# Query the figure's on-screen size and DPI. Note that when saving the figure to
# a file, we need to provide a DPI for that separately.
print('fig size: {0} DPI, size in inches {1}'.format(
    fig.get_dpi(), fig.get_size_inches()))

# Plot a scatter that persists (isn't redrawn) and the initial line.
x = np.arange(0, 20, 0.1)
ax.scatter(x, x + np.random.normal(0, 3.0, len(x)))
line, = ax.plot(x, x - 5, 'r-', linewidth=2)

def update(i):
    label = 'timestep {0}'.format(i)
    print(label)
    # Update the line and the axes (with a new xlabel). Return a tuple of
    # "artists" that have to be redrawn for this frame.
    line.set_ydata(x - 5 + i)
    ax.set_xlabel(label)
    return line, ax

if __name__ == '__main__':
    # FuncAnimation will call the 'update' function for each frame; here
    # animating over 10 frames, with an interval of 200ms between frames.
    anim = FuncAnimation(fig, update, frames=np.arange(0, 10), interval=200)
    if len(sys.argv) > 1 and sys.argv[1] == 'save':
        anim.save('line.gif', dpi=80, writer='imagemagick')
    else:
        # plt.show() will just loop the animation forever.
        plt.show()

If you want a fancier theme, install the seaborn library and just add:

import seaborn

Then you'll get this image:

Animated GIF produced with matplotlib, Seaborn scheme

A word of warning on size: even though the GIFs I show here only have 10 frames and the graphics is very bare-bones, they weigh in at around 160K each. AFAIU, animated GIFs don't use cross-frame compression, which makes them very byte-hungry for longer frame sequences. Reducing the number of frames to the bare minimum and making the images smaller (by playing with the figure size and/or DPI in matplotlib) can help alleviate the problem somewhat.

July 24, 2016 01:21 PM


Vasudev Ram

Control break report to PDF with xtopdf

By Vasudev Ram

Hi readers,

Control break reports are very common in data processing, from the earliest days of computing until today. This is because they are a fundamental kind of report, the need for which is ubiquitous across many kinds of organizations.

Here is an example program that generates a control break report and writes it to PDF, using xtopdf, my Python toolkit for PDF creation.

The program is named ControlBreakToPDF.py. It uses xtopdf to generate the PDF output, and the groupby function from the itertools module to handle the control break logic easily.

I've written multiple control-break report generation programs before, including implementing the logic manually, and it can get a little fiddly to get everything just right, particularly when there is more than one level of nesting (i.e. no off-by-one errors, etc.); you have to check for various conditions, set flags, etc.

So it's nice to have Python's itertools.groupby functionality handle it, at least for basic cases. Note that the data needs to be sorted on the grouping key, in order for groupby to work. Here is the code for ControlBreakToPDF.py:

from __future__ import print_function

# ControlBreakToPDF.py
# A program to show how to write simple control break reports
# and send the output to PDF, using itertools.groupby and xtopdf.
# Author: Vasudev Ram
# Copyright 2016 Vasudev Ram
# http://jugad2.blogspot.com
# https://gumroad.com/vasudevram

from itertools import groupby
from PDFWriter import PDFWriter

# I hard-code the data here to make the example shorter.
# More commonly, it would be fetched at run-time from a
# database query or CSV file or similar source.

data = \
[
['North', 'Desktop #1', 1000],
['South', 'Desktop #3', 1100],
['North', 'Laptop #7', 1200],
['South', 'Keyboard #4', 200],
['North', 'Mouse #2', 50],
['East', 'Tablet #5', 200],
['West', 'Hard disk #8', 500],
['West', 'CD-ROM #6', 150],
['South', 'DVD Drive', 150],
['East', 'Offline UPS', 250],
]

pw = PDFWriter('SalesReport.pdf')
pw.setFont('Courier', 12)
pw.setHeader('Sales by Region')
pw.setFooter('Using itertools.groupby and xtopdf')

# Convenience function to both print to screen and write to PDF.
def print_and_write(s, pw):
print(s)
pw.writeLine(s)

# Set column headers.
headers = ['Region', 'Item', 'Sale Value']
# Set column widths.
widths = [ 10, 15, 10 ]
# Build header string for report.
header_str = ''.join([hdr.center(widths[ind]) \
for ind, hdr in enumerate(headers)])
print_and_write(header_str, pw)

# Function to base the sorting and grouping on.
def key_func(rec):
return rec[0]

data.sort(key=key_func)

for region, group in groupby(data, key=key_func):
print_and_write('', pw)
# Write group header, i.e. region name.
print_and_write(region.center(widths[0]), pw)
# Write group's rows, i.e. sales data for the region.
for row in group:
# Build formatted row string.
row_str = ''.join(str(col).rjust(widths[ind + 1]) \
for ind, col in enumerate(row[1:]))
print_and_write(' ' * widths[0] + row_str, pw)
pw.close()
Running it gives this output on the screen:
$ python ControlBreakToPDF.py
Region Item Sale Value

East
Tablet #5 200
Offline UPS 250

North
Desktop #1 1000
Laptop #7 1200
Mouse #2 50

South
Desktop #3 1100
Keyboard #4 200
DVD Drive 150

West
Hard disk #8 500
CD-ROM #6 150

$
And this is a screenshot of the PDF output, viewed in Foxit PDF Reader:


So the itertools.groupby function basically provides roughly the same sort of functionality that SQL's GROUP BY clause provides (of course, when included in a complete SELECT statement). The difference is that with Python's groupby, you do the grouping and related processing in your program code, on data which is in memory, while if using SQL via a client-server RDBMS from your program, the grouping and processing will happen on the database server and only the aggregate results will be sent to your program to process further. Both methods can have pros and cons, depending on the needs of the application.

In my next post about Python, I'll use this program as one vehicle to demonstrate some uses of randomness in testing, continuing the series titled "The many uses of randomness", the earlier two parts of which are here and here.

- Enjoy.

- Vasudev Ram - Online Python training and consulting

Follow me on Gumroad to be notified about my new products:
.gumroad-follow-form-embed { zoom: 1; } .gumroad-follow-form-embed:before, .gumroad-follow-form-embed:after { display: table; line-height: 0; content: ""; } .gumroad-follow-form-embed:after { clear: both; } .gumroad-follow-form-embed * { margin: 0; border: 0; padding: 0; outline: 0; box-sizing: border-box !important; float: left !important; } .gumroad-follow-form-embed input { border-radius: 4px; border-top-right-radius: 0; border-bottom-right-radius: 0; font-family: -apple-system, ".SFNSDisplay-Regular", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; line-height: 20px; background: #fff; border: 1px solid #ddd; border-right: 0; color: #aaa; padding: 10px; box-shadow: inset 0 1px 0 rgba(0, 0, 0, 0.02); background-position: top right; background-repeat: no-repeat; text-rendering: optimizeLegibility; font-smoothing: antialiased; -webkit-appearance: none; -moz-appearance: caret; width: 65% !important; height: 40px !important; } .gumroad-follow-form-embed button { border-radius: 4px; border-top-left-radius: 0; border-bottom-left-radius: 0; box-shadow: 0 1px 1px rgba(0, 0, 0, 0.12); -webkit-transition: all .05s ease-in-out; transition: all .05s ease-in-out; display: inline-block; padding: 11px 15px 12px; cursor: pointer; color: #fff; font-size: 15px; line-height: 100%; font-family: -apple-system, ".SFNSDisplay-Regular", "Helvetica Neue", Helvetica, Arial, sans-serif; background: #36a9ae; border: 1px solid #31989d; filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#5ccfd4, endColorstr=#329ca1, GradientType=0)"; background: -webkit-linear-gradient(top, #5ccfd4, #329ca1); background: linear-gradient(to bottom, #5ccfd4, #329ca1); height: 40px !important; width: 35% !important; }



My Python posts     Subscribe to my blog by email

My ActiveState recipes



July 24, 2016 03:19 AM

July 23, 2016


Michy Alice

Fluid Dynamics: Pressure Drop Modelling [heavily revised]

Some time ago I uploaded a short script on modelling pressure drop in a circular diameter pipe. While at the time I felt I did a good job, I knew there sure was room for improvement. After having attended a (very tough) course on fluid dynamics and applied physics, I feel a lot more confident on this topic and therefore I would like to share a little model I built for a project I am working on.

3d_main_plot

I have been asked to make a study for a water pump pumping water out of a reservoir into a storage tank. While working with fluids travelling in pipes, it sometimes is important to account for pressure drop, especially if you need to make yourself an idea of what size of machine you may need. In this case, the parameter to be focused on is the pump’s head.

Let’s make some (hopefully) reasonable assumptions:
- We’ll assume that the pipes have a round constant section and are made of plastics.
- We’ll assume that the flow is stationary. This is a common assumption and usually holds true.
- The fluid used is water so we can reasonably assume it is incompressible.

Under these assumptions, you can break down pressure drop in two components (note that the pressure drop here is measured in meters since these meters will add to the minimum required pump head):

$$Y = \frac{\lambda}{2g} v^2 \frac{L}{D} + \xi \frac{v^2}{2}$$

The first component is due to friction while the second one is due to turns in the tubes, connections and change of direction of the flow. I’ll be focusing on the first one since the second one is easy to calculate once you know the velocity of the flow and you could easily look up the $\xi$ coefficient depending on the type of tube connection.

As for the first component, we are interested in modelling the pressure drop in meters as a function of the pipe’s diameter and of the volumetric flow rate. We can model the pressure drop for 1 meter of tube, for the sake of simplicity, and then multiply for how many meters we need to use.

So now we need to model $Y_d$ as follows:

$$Y_d = \frac{\lambda}{2g} v^2 \frac{L}{D}$$

Where $L=1$.

First of all we need to find the fluid velocity. Since the fluid is incompressible, it is known that velocity is given by:

$$v = \frac{4 \dot Q}{\pi D^2}$$

where $\dot Q$ is the volumetric flow rate ($\frac{m^3}{s}$) and $D$ is the pipe’s diameter ($m$). The volumetric flow rate is usually a given, since it is usually a project requirement. The pipe diameter is usually not given though.

As far as $\lambda$ is concerned, it is a little bit harder to find this parameter. However, you can usually find it by knowing the Reynold’s number and the rugosity of the tube. As far as the Reynolds number is concerned, it can be calculated using the following formula:

$$Re = \frac{\rho v D}{\mu}$$

while the rugosity of the tube can be looked up according to the material. Next, in order to find $\lambda$ you’d need to use Moody’s Diagram, but since I’m using Python, I can plug in a formula and make a better approximation. Now we have everything, let’s run the simulation!

This is the results in a contour plot and two 3D plots.

contour_plot

The contour plot looks sound. As you can see, if at a given pipe’s diameter you keep increasing the flow rate, the pressure drop increases. On the other hand if you keep the flow rate constant and increase the pipe’s diameter, the pressure drop goes down. This seems reasonable and close enough to the expected results. Of course some experiments should be done to assess whether the assumptions hold and if the simulated data matches the measured data.

This is the 3d plot from two different perspectives.

3d_plot_1

3d_plot_2

Finally, below is the gist with the code I used to make the plots and run the simulation.

If you have any observation or suggestion, please do comment.

July 23, 2016 11:31 PM


Podcast.__init__

Episode 67 - Crossing The Streams - Talk Python with Michael Kennedy

Summary

The same week that we released our first episode of Podcast.__init__, Michael Kennedy was publishing the very first episode of Talk Python To Me. The years long drought of podcasts about Python has been quenched with a veritable flood of quality content as we have both continued to deliver the stories of the wonderful people who make our community such a wonderful place. This week we interviewed Michael about what inspired him to get started, his process and experience as Talk Python continues to evolve, and how that has led him to create online training courses alongside the podcast. He also interviewed us, so check out this weeks episode of Talk Python To Me for a mirror image of this show!

Brief Introduction

Linode Sponsor Banner

Use the promo code podcastinit20 to get a $20 credit when you sign up!

sentry-horizontal-black.png

Stop hoping your users will report bugs. Sentry’s real-time tracking gives you insight into production deployments and information to reproduce and fix crashes. Use the code podcastinit at signup to get a $50 credit!

Interview with Michael Kennedy

Keep In Touch

Picks

Links

The intro and outro music is from Requiem for a Fish The Freak Fandango Orchestra / CC BY-SA

Summary The same week that we released our first episode of Podcast.__init__, Michael Kennedy was publishing the very first episode of Talk Python To Me. The years long drought of podcasts about Python has been quenched with a veritable flood of quality content as we have both continued to deliver the stories of the wonderful people who make our community such a wonderful place. This week we interviewed Michael about what inspired him to get started, his process and experience as Talk Python continues to evolve, and how that has led him to create online training courses alongside the podcast. He also interviewed us, so check out this weeks episode of Talk Python To Me for a mirror image of this show!Brief IntroductionHello and welcome to Podcast.__init__, the podcast about Python and the people who make it great.I would like to thank everyone who has donated to the show. Your contributions help us make the show sustainable. For details on how to support the show you can visit our site at pythonpodcast.comLinode is sponsoring us this week. Check them out at linode.com/podcastinit and get a $20 credit to try out their fast and reliable Linux virtual servers for your next projectWe are also sponsored by Sentry this week. Stop hoping your users will report bugs. Sentry's real-time tracking gives you insight into production deployments and information to reproduce and fix crashes. Check them out at getsentry.comVisit our site to subscribe to our show, sign up for our newsletter, read the show notes, and get in touch.To help other people find the show you can leave a review on iTunes, or Google Play Music, and tell your friends and co-workersJoin our community! Visit discourse.pythonpodcast.com for your opportunity to find out about upcoming guests, suggest questions, and propose show ideas.Your hosts as usual are Tobias Macey and Chris PattiToday we're interviewing Michael Kennedy about his work with Talk Python to Me, another podcast about Python and its community, and on-demand Python trainings. Michael has also offered to give away one of each of his Python courses to our listeners. If you would like the chance to win, then sign up for our newsletter at pythonpodcast.com, or our forum at discourse.pythonpodcast.com. If you want to double your chances, then sign up for both! Use the promo code podcastinit20 to get a $20 credit when you sign up! Stop hoping your users will report bugs. Sentry's real-time tracking gives you insight into production deployments and information to reproduce and fix crashes. Use the code podcastinit at signup to get a $50 credit!Interview with Michael KennedyIntroductionsHow did you get into programming?How did you get introduced to Python? (Chris)What is the craziest piece of software you've ever written? - TobiasYou've taken some pretty drastic steps around Python and your career lately. What inspired you to do that and how's it going?(yes, quit my job, focus only on podcast and online courses). You are basically self-taught as a developer, how did you get into this teaching / mentor role?Why did you first get started with Talk Python to Me? - TobiasDid you know when you started that it would turn into a full-time endeavor? - TobiasFor a while there weren't any podcasts available that focused on Python and now we're each producing one. What's it like to run a successful podcast? - TobiasWhat have been your most popular episodes? Tell us a bit about each - TobiasIn your excellent episode with Kate Heddleston you talked about how we tend to bash other programming languages. We've done a fair bit of Java bashing here. How can we help get ourselves and others in our community out of this bad habit? - ChrisHow do you select the guests and topics for your show? - TobiasWhat topics do you have planned for the next few episodes?How do you prepare the questions for each episode? - TobiasWhat is the most significant thing you've learned from the podcasting experience?What do you wish you did differently and h

July 23, 2016 09:13 PM


Davy Wybiral

Procedural trees with Javascript

Here's another quick web experiment, this time exploring the use of particle systems to generate art.


You can watch it generate a random forest live here: https://wybiral.github.io/code-art/projects/trees/

Or if you're one of the cool kids you can clone the Git repo or browse the code here: https://github.com/wybiral/code-art

Each tree starts as a single particle representing the trunk and moves upward with a random chance of bending or splitting as it goes. The branches are rendered using a technique similar to the spray can from MS Paint to create a charcoal look. Random pixels are faded out as it runs to create a grainy fog effect.

July 23, 2016 02:41 PM


Guido van Rossum

About spammers and comments

I'm turning off commenting for my blogs. While I've enjoyed some feedback, the time wasted to moderate spam posts just isn't worth it. Thank you, spammers! :-(

July 23, 2016 02:11 PM

July 22, 2016


Jamal Moir

[Video Series] Taking Your Python Skills to the Next Level With Pythonic Code – Stop Using Lists for Everything

Taking Your Python Skills to the Next Level With Pythonic Code – Stop Using Lists for Everything This is the 1st in a series covering Pythonic code written by Michael Kennedy of Talk Python To Me. Be sure to catch the whole series with 5 powerful Pythonic recommendations and over 45 minutes of video examples. […]

The post [Video Series] Taking Your Python Skills to the Next Level With Pythonic Code – Stop Using Lists for Everything appeared first on Data Dependence.

July 22, 2016 05:00 PM

[Video Series] Taking Your Python Skills to the Next Level With Pythonic Code – Introduction

Taking Your Python Skills to the Next Level With Pythonic Code  – Introduction This is the introduction to a series covering Pythonic code written by Michael Kennedy of Talk Python To Me. Be sure to catch the whole series with 5 powerful Pythonic concepts and over 45 minutes of video examples. WHAT IS PYTHONIC CODE? […]

The post [Video Series] Taking Your Python Skills to the Next Level With Pythonic Code – Introduction appeared first on Data Dependence.

July 22, 2016 04:59 PM


EuroPython

EuroPython 2016: Please send in your feedback

EuroPython 2016 is almost over and so it’s time to ask around for what we can improve next year. If you attended EuroPython 2016, please take a few moments and fill out our feedback form:

EuroPython 2016 Feedback Form

We will leave the feedback form online for a few weeks and then use the information as basis for the work on EuroPython 2017 and also post a summary of the multiple choice questions (not the comments to protect your privacy) on our website. Many thanks in advance.

With gravitational regards,

EuroPython 2016 Team

July 22, 2016 02:33 PM

EuroPython 2016: Financial Aid Sponsorship

We are very grateful to the Python Software Foundation (PSF) as our financial aid sponsor. Together with the budget we allocated, this has allowed us to provide finance aid to people who otherwise would not have been able to attended the conference or give a talk.

We are glad to announce that funds devoted to financial aid have risen by 30% compared to 2015.

Some statistics

With gravitational regards,

EuroPython 2016 Team

July 22, 2016 10:17 AM


Full Stack Python

Setting Up Python 3, Django & Gunicorn on Linux Mint 17.3

Linux Mint 17.3 "Rosa" is December 2015 release of the polished and widely-used Linux distribution. This Mint release includes both Python 2.7 and 3.4 by default, but in this tutorial we will download and install the latest Python 3.5.1 version to run our Django application.

If you want to use a different Linux distribution such as Ubuntu instead of Mint, check out the tutorial for Ubuntu 16.04 "Xenial Xerus". If Mint is your desired development environment though, let's get started!

Tools We Need

Our setup will use several system packages and code libraries to get up and running. Do not worry about installing these dependencies just yet, we will get to them as we progress through the tutorial. The tools and their current versions as of June 2016 are:

If you are on Mac OS X or Windows, my recommendation is to use virtualization software such as Parallels or VirtualBox with the Linux Mint Cinnamon desktop .iso.

We should see a desktop screen like this one when we boot up the operating system for the first time.

Linux Mint default desktop

Open up terminal to proceed with the configuration.

System Packages

We can see the Python version Linux Mint comes with, as well as where its executable is stored.

python3 --version
which python3

The output of those two commands should be (these are not commands to run):

Python 3.4.3
/usr/bin/python3

Output of 'python --version' and 'which python3' commands.

We really want to use the latest Python release instead of the default 3.4 when starting a new Python project, so let's download and install 3.5.1 now.

Run these commands in the terminal to download Python 3.5.1 source code:

cd ~/Downloads
wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz

wget Python source code output.

Extract the Python source code:

tar -xvf Python-3.5.1.tgz

Linux Mint is not configured by default to build the Python source code. We need to update our system package lists and install several packages to make building the Python source code possible. If you have a password on your user account, enter it when prompted to allow the installation to proceed.

sudo apt update
sudo apt install build-essential checkinstall
sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev 
sudo apt install libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
sudo apt install python3-dev

Once the packages are installed, we can configure and install Python from source.

cd Python-3.5.1
./configure
sudo make install

Test that the installation worked properly by starting up the Python REPL:

python3.5

If the REPL starts up properly with Python 3.5.1 in the output then we're good to go.

wget Python source code output.

The basic system packages we need are now installed so we can proceed to our Python-specific dependencies.

Virtual environment and pip

Python 3.5 comes with the virtual environment and pip applications so we can use them to handle our application dependencies.

Create a directory to store virtual environments then create a virtualenv for our Django project.

# the tilde "~" specifies the user's home directory, like /home/matt
cd ~
mkdir venvs
# specify the system python3 installation
python3.5 -m venv djangoproj

Activate the virtualenv.

source ~/venvs/djangoproj/bin/activate

Our virtual environment is properly activated when we see (djangoproj) prepended to our prompt.

Output from the virtualenv environment activation.

Our virtualenv with Python 3.5.1 is activated so we can install whatever dependencies we want, such as Django and Gunicorn. Our default python command is also set to use the Python 3.5.1 installation instead of the Python 2.7 version that comes with Linux Mint.

Django and Gunicorn

Now we can install Django and Green Unicorn into our virtual environment.

pip install django==1.9.7 gunicorn==19.6

If there are no errors in the pip output then that is a good sign we can proceed.

Django and Gunicorn properly install via the pip command.

Create a new Django project named djangoproj, or whatever you want to name your project. Change into the directory for the new project.

cd ~
django-admin startproject djangoproj
cd djangoproj

We can run Django using the development server with the python manage.py runserver command. However, start Django up with Gunicorn instead.

gunicorn djangoproj.wsgi

Result of running gunicorn djangoproj.wsgi on the command line.

Awesome, we can bring up our shell project in the web browser at the http://localhost:8000 or http://127.0.0.1:8000 address.

Django project running in the Firefox web browser.

Now you're ready for Django development!

Ready for Development

Those are the first few steps for beginning development with Django and Gunicorn on Linux Mint 17.3 "Rosa". If you need an even more in-depth walkthrough for deploying your Python web application to a production environment, check out the Full Stack Python Guide to Deployments book.

To figure out what to do next for your Python project, read the topics found on the table of contents page.

Questions? Contact me via Twitter @fullstackpython or @mattmakai. I'm also on GitHub with the username makaimc.

See something wrong in this post? Fork this page's source on GitHub and submit a pull request.

July 22, 2016 04:00 AM


PyCon Australia

Call for volunteers

Gain a new perspective on PyCon AU 2016, working behind the scenes to make the event happen. The team behind PyCon AU is an all volunteer team and we need your help to make the show run as smoothly as possible, so we’re now calling for on the ground volunteers to complete the team.

We aim to recruit volunteers to assist us with the following:

A/V staff - Run our video recording system (complete training provided). This is a full-time position and not suitable for attendees. It comes with a free pass to the conference.

Session Chairing - Help run a room during talks over one or more sessions (see the wiki below). Strongly recommended for first-time speakers.

Registration Desk - Handle the registration desk. This is a full-time position and not suitable for attendees. It comes with a free pass to the conference. We will also need some non-full-time folks to cover busy times.

Badge Check - Verify attendee ticket validity at the entrance doors.

Volunteers will be provided with a free T-Shirt and full-time volunteers' food and drink requirements will be looked after. This is a great opportunity to experience a Python conference first-hand and to connect with like-minded Python enthusiasts.

There will be a training session in the evening of Thursday 11th at the MCEC for all non-A/V volunteers. The A/V volunteers will have training at a time to be determined.

To sign up:

Non session chairs: just e-mail us indicating how you'd like to help out!

Session Chairs:

July 22, 2016 03:42 AM


Daniel Bader

A better Python REPL: bpython vs python

A better Python REPL: bpython vs python

A quick video that demonstrates bpython, an awesome alternative Python interpreter.

Compared to the vanilla Python interpreter bpython knows a few extra tricks like syntax highlighting, auto indent (yay!), and auto completion.

Check it out, it’s a really great tool!

If you’d like to learn more about bpython, the following links should help you out:

July 22, 2016 12:00 AM

July 21, 2016


Dataquest

How I built a Slack bot to help me find an apartment in San Francisco

I moved from Boston to the Bay Area a few months ago. Priya (my girlfriend) and I heard all sorts of horror stories about the rental market. The fact that searching for “How to find an apartment in San Francisco” on Google yields dozens of pages of advice is a good indicator that apartment hunting is a painful process.

Boston is cold, but finding an apartment in SF is scary

We read that landlords hold open houses, and that you have to bring all of your paperwork to the open house and be willing to put down a deposit immediately to even be considered. We started exhaustively researching the process, and figured out that a lot of finding an apartment comes down to timing. Some landlords want to hold an open house no matter what, but for others, being one of the first people to see the apartment usually means that you can get it. You eneed to find the listing, quickly figure out if it meets your criteria, then call the landlord to arrange a showing to have a shot.

We looked...

July 21, 2016 08:00 PM