Quantcast
Channel: Planet Python
Viewing all 22412 articles
Browse latest View live

Mike C. Fletcher: Updating PyOpenGL Sample Code References

$
0
0

Continuing to prepare for a PyOpenGL 3.1.4 release.

I've worked through getting the auto-generated reference documentation updated. We now use the upstream git repository for the docbook base files (gl, glu and glx) while still using the legacy files for glut and gle.

I've had to disable the sample-source projects still using SourceForge CVS, as SourceForge doesn't provide web-based viewing of the CVS repositories any more. If you know of projects that should be added to the set of sample-code projects, let me know.

I got all of the OpenGLContext demos running in python3, PyOpenGL 3.1.4, current numpy, and current PIL. No bugs really showing up within PyOpenGL for that, which is a good thing for considering the release working.

I ported the OpenGLContext wxPython context to run with current wxPython this weekend.

I've moved all of my PyOpenGL related projects over to GitHub as their primary repositories.

The tutorial documentation, a bunch of details regarding moving to github (e.g. updating urls), and of course platform testing for mac/windows machines is all I'm really planning to get done before the release. If you are a PyOpenGL user on one of those two platforms and would like to do some testing, I'd love to know about problems before the release goes out the door. I expect this will likely be the last Python 2 supporting release for PyOpenGL, so it would be nice if it was relatively stable and reliable.


Mike Driscoll: PyDev of the Week: Miguel Grinberg

$
0
0

This week we welcome Miguel Grinberg (@miguelgrinberg) as our PyDev of the Week! Miguel is the author of Flask Web Development and the very popular Flask Mega-Tutorial. You can find out more about Miguel by checking out his blog or his Github profile. Let’s spend some time getting to know Miguel better!

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

I was born in Buenos Aires, Argentina. Shortly after graduating from college with a Masters degree in Computer Science I was lucky to be offered a job in the United States, so I relocated to Portland, Oregon with my wife. We raised a family there and lived happily for several years. In 2018 we relocated once again, this time to Ireland. We plan to spend a few years on this side of the pond to be closer to my wife’s family and to be able to travel through Europe, but Portland is still our home and I’m pretty sure we will eventually return to America.

In terms of hobbies I have to say that by all standards I’m a fairly boring person. Outside of coding (which I do professionally and also as a hobby), what I enjoy the most is playing the Ukulele. I have a small collection of them, and I have recently expanded it with a Mandolin, which seemed appropriate now that I’m in Ireland. Everyone here seems to be in a band of some sort, so maybe one day I’ll join one as well, who knows!

Why did you start using Python?

This was around 2008 or 2009, I think. I was working at a company in which my team maintained a large library written in C++ that was used by several products, both internal and from partners. This was a big company, with a proper Quality Assurance department, but the QA engineers complained that they did not have an easy way to test our library, since it was C++ code. We had a homegrown unit testing suite written in a combination of bash, make, C++ and diff that was painful to maintain, and that was it in terms of testing. So I came up with the idea of creating bindings for our library in a scripting language that our QA people felt comfortable using. After a survey, the two contenders were Python and Ruby. At the time I knew very little about Python, and I had some knowledge of Ruby, so funny enough my personal choice would have been Ruby. But as it happens, one of the engineers in my team was actually very experienced in Python from a previous job, so strategically we thought it would be to our advantage to go with Python because we had an expert in the team. So I have to thank my teammate for getting me into Python!

I always approach the learning of new things through personal projects, so as soon as the decision to go with Python was made I started to play with the language at home just for fun. A few years after my initial introduction to the language I was thinking in starting a software blog and was having trouble finding a blogging platform that I liked, so I’ve got the idea of writing my own blog. By then the Python bindings we created at work were a success and Python had won me over 100%. So I naturally decided to use a Python web framework to make my blog, and looking through the available options I finally selected this minimalistic framework that at the time wasn’t that popular, called Flask. That turned out to be one of the best decisions I’ve made in my life.

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

I have a fair amount of experience with C and C++. I often work with JavaScript, HTML and CSS, since they are unavoidable if you do any sort of web development work. I learned to code back when I was a teenager hacking away on my TI-99/4A Home Computer, and in those days Basic was pretty much the only option to write code in. I remember I was frustrated back then because I was writing video games in Basic and they were pretty slow, so I taught myself Assembly to write faster games, as this was the only other available programming language on the TI computer. I have also done some projects here and there in other languages such as Ruby, PHP, Smalltalk and Java. So it’s a little bit of everything for me.

The language that allows me to get things done faster and without creating an unmaintainable code mess is hands down Python, so given a new project this is always my first choice. Python is also the language that is the most fun to use for me.

What projects are you working on now?

The open source project that takes most of my time these days is my Socket.IO client and server, which is split in two repositories, python-socketio and python-engineio. Socket.IO is a standard communication protocol built on top of HTTP and WebSocket that allows clients and servers to establish long lived bi-directional connections, avoiding some of the limitations of HTTP, and also with very low latency. My project allows Python to act as a client or as a server and connect to Socket.IO applications built in other languages, or of course, you can also use Python on both ends if that’s what you like. My Socket.IO server is commonly paired with the JavaScript client running on web browsers to build highly dynamic applications that are an alternative to the more traditional option of building the server as a REST or HTTP API.

I’ve mentioned above that at some point was trying to get into blogging. It’s now been more than seven years since I started to blog and I still find it very rewarding, so the blog is always high on the list of current projects. I recently finished a six part tutorial for MicroPython, and I’m about to publish the second part of an in-depth series on Python decorators.

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

Two libraries that I wish were more popular because they are fantastic are gevent and eventlet. These are small frameworks that allow you to write asynchronous applications, pretty much like you would with Python’s own asyncio. The difference is that gevent and eventlet do not require a new syntax for asynchronous code, so you can get code that was written for regular Python to magically work asynchronously. Packages such as Requests or even Flask become asynchronous without any changes. I gave a talk about using these frameworks alongside Flask at PyCon UK 2019.

Another package that I like a lot is pudb, a console based GUI debugger that you can run on a remote server through an SSH connection. I also like arrow as a nicer date and time library than Python’s datetime package. Last but not least, httpie is a great HTTP client that runs from the CLI with a much cleaner syntax than curl or wget.

How did you get started with MicroPython?

When I moved to Ireland last year I rented a house that had a last generation heating controller, one of those that you can control from a smartphone app or the browser. My issue with this controller was that the thermostat could not be programmed, the only thing that was programmable was to start and stop the heating at the given times. Since the weather outside is constantly changing, the schedule required constant adjusting, and that was a tedious task. What I would have preferred is to program a target temperature for the day and another one for the night, and then have the controller figure out when to start and stop the heating so that my desired temperatures are reached, but this device, and most others I’ve seen here in Ireland, do not have that capability for some reason. So the idea came to write a little thermostat app and load it into a microcontroller connected to a thermometer running MicroPython. I’ve used a ESP8266 board for this project, a nice and cheap microcontroller board that comes with Wi-Fi and has a vast array of supported sensors, including a few for temperature. I built two of these devices, one for downstairs and one for upstairs. The MicroPython boards would then read the temperature every 5 minutes and compare it against my chosen temperature for the current time of day. The script would then decide on the best course of action to reduce the difference between the current and target temperatures. The action could be one of three options: start the heating, stop the heating, or keep the heating in its current state. Controlling the heating from MicroPython was actually the easiest part, it just required sending a couple of HTTP requests. It was a super fun project to work on!

What was the motivation behind the Flask Mega-Tutorial?

The Flask Mega-Tutorial was actually my first writing task for the blog. Once I completed my blog project using Python and Flask and had the blog deployed, I had to find a first topic to write about. At the time Flask was starting to be noticed, but wasn’t in most people’s radars yet. For example, there were no books about it at all. So the most logical thing seemed to start blogging about my experience writing the blog itself with Flask. I called it the “Mega” tutorial because I knew I wanted to cover a lot of different topics, all sourced from my experience in getting the blog project going. I started writing articles without doing so much planning, to be honest. Every 2-3 weeks I would decide on a topic and write an article for the series on it, plus fully working code to go with it. I finally decided to end the tutorial after 18 chapters, about a year after I started it.

I never liked that improvised nature of the original tutorial, but regardless, a lot of people liked it and it became very popular, enough for an O’Reilly editor to notice it and eventually give me the chance to write an Animal book on the Flask framework, now in its second edition. The years passed, and I kept feeling that the original Flask Mega-Tutorial could have been better organized. About five years after I started it, I decided to give it a major makeover, sponsored by a Kickstarter campaign. I feel much better about the new version of the tutorial, now expanded to 23 chapters and with a more thought out structure. I have also shot video versions of all the chapters, where I go through the tutorial step by step, and this was a lot of fun.

Do you have any words of wisdom for other aspiring bloggers / content creators?

This sounds like a cheesy bit of advice, but I think the most important thing you can do is to start writing right away! It’s easy enough to create a free blog, so there is no reason to delay it. What should you write about? You should write about what you know. I’ve found that for me, the easiest way to get going was to write step by step tutorials. You may find that this works for you as well, or maybe it comes easier to you to write in a different style, or maybe to record a video instead. The important thing is that you get used to produce content a little bit every day, because then without even realizing it you will be getting better at it over time. If you think you are not at expert at anything so there’s no topic you can talk about, then you should know that most bloggers (me included) are not experts at what they write about. It actually works the other way around, you become more experienced on a topic after you go through the effort of explaining it in detail to others. If you are concerned about making mistakes in your writing due to inexperience, you should know that blog posts can be edited after published, so as soon as you learn of a mistake you’ve made you can correct it easily. I constantly revise, edit and improve my old posts, often as a result of someone pointing out mistakes or inconsistencies I’ve made. No shame in that. So if you want to become a blogger or content creator you really have no excuse. You should start now!

Is there anything else you’d like to say?

I’d like to encourage everyone to work on personal projects. The things that I do on the side of my day job have a special place in my heart, because unlike work, where there are a lot of things outside of my control, when I embark on a personal project I can control every aspect of it and make it 100% to my liking. I have heard often the argument that if you are working 40+ hours a week the last thing you want is to give yourself more work for your nights and weekends. I think the people who think this way are choosing the wrong side projects! Your personal projects should be all about enjoyment and fun, if they feel too much like work then you are doing it wrong.

If I convinced you that spending some time on personal projects can be a good thing, then I hope you’ll agree that showing those projects off is also important. The interaction with the community will give you feedback and new ideas. You will likely become a role model for others who will decide to also embark on their own projects after seeing yours. You may also notice that over time, what you do at your day job will be positively affected by what you do on your own time, often in little ways, but potentially in big ways as well. A software project that I built on my own time while I was a college student in Argentina was key in getting that job offer to relocate to the United States, for example.

Thanks for doing the interview, Miguel!

The post PyDev of the Week: Miguel Grinberg appeared first on The Mouse Vs. The Python.

Jaime Buelta: Interviewed about microservices

$
0
0
I got interviewed about Microservice and talk a bit about my last book, Hands-on Docker for Microservices with Python. I was an interesting view on what are the most important areas of Microservices and when migrating from Monolith architecture is a good idea. And also talking about related tools like Python, Docker or Kubernetes. Check … Continue reading Interviewed about microservices

Real Python: Python Community Interview With Brian Okken

$
0
0

This week, it’s my pleasure to interview Brian Okken. Brian is perhaps best known as the author of Python Testing with pytest, as well as being the host of two podcasts. Read on to find out more about the man behind the voice, his new meetup in Portland, and the advice he has for anyone new to testing software.

Ricky:Let’s start with the same questions I ask all my guests. How’d you get into programming? When did you start using Python?

Brian Okken

Brian: When I was probably 14 I received a TRS-80 as a gift from my parents. I didn’t really learn to code on that, but I did type in BASIC program listings for small applications and games from the backs of magazines. Then I took a BASIC class in high school, but I didn’t take it much further than that. I didn’t know you could make a career out of it.

I didn’t come back to programming until 2 to 3 years into university when I switched from math and fine arts to CS. I’d used Scheme, Perl, Tcl, C, C++, and ksh by the end of my Bachelor’s and Master’s, both in Computer Science. Then, I joined a test and measurement company building electronic test systems and instruments.

In the early 2000s, a Python-based test framework was introduced to our team. Even though I was developing embedded C++ code, my use of Python to drive the testing of the system has grown and grown over the years. Since I started blogging and podcasting in 2012 and 2015, I’ve expanded my use of Python, both inside projects and on the job, to the point that it’s now where I spend most of my time.

Ricky:What drew you into testing software as a profession? And what do you enjoy most about it?

Brian: I kinda love that question and cringe at the same time. I’ve never been in “software testing” as a profession. But I guess that’s a lot of what I do, anyway.

Successful, effective, efficient software development includes software testing. We rely on solid, dependable, thorough, yet fast test suites to give us the green light for merges during CI/CD pipelines. More and more, we lean on the quality of automated testing to ensure the quality of our software. This enables us to develop faster, ship faster, iterate faster, pivot faster, make mistakes and learn from them faster, and so much more.

And yet, very few developers know how to efficiently and effectively write automated tests that deliver on all of those responsibilities that we put on automated test suites. Many are told (or believe) that they need to write unit tests, without really knowing what that means. Most believe in the test pyramid in theory, but don’t really use it or know what it implies. So, my work has gradually turned to beating the drum and sounding the horn and standing on the podium, singing the praises of automated tests and trying to teach people how to do it.

I like to learn about problems from the first implementation of some code, the first draft. And I really love to write second drafts, third drafts, and so on. I like to tinker with software projects. I like to rip out ugly parts and try to make it readable, or at least less ugly. And in order to do that, I need to rely on tests to tell me if I’ve mucked something up.

That’s the core of it. I want to play with software, so I want to spend as little time as possible writing tests and debugging and triaging issues. That way, I can spend as much time as possible writing new stuff and refactoring old stuff. I want to develop software that I’m proud of. Automated software tests allow that. I don’t know of any other way. That’s what I enjoy the most from testing.

Ricky:For our readers that are new to testing (and perhaps programming in general), what’s the one piece of advice you would give them?

Brian Don’t put off testing till the end. You don’t have to do it first. You don’t have to do Test Driven Development. But also don’t do it last. Build up tests as you build up code, and build up knowledge.

Your automated tests should allow you to develop faster because you aren’t having to go back and manually check all the stuff that used to work. If it feels like adding automated tests is increasing your workload, a few things may be going on. Maybe you’re just new at it, and learning a new skill takes time. You may be writing too many tests. Testing at higher levels, or bigger chunks, or parametrization, or all of the above may make your test writing time more efficient.

Testing isn’t easy. But it’s also not complicated. Allow yourself time to learn. Testing allows you to learn about your software. The learning will help you develop better software. That learning is valuable, so be very careful about being willing to give that learning to someone else. Teams that have engineers develop their own tests will build better engineers than teams that toss the testing to a separate team.

I guess that’s way more than one bit of advice. :)

Ricky:If you could predict the future, what would you predict will be the biggest challenge you see in how we’ll test software in the future?

Brian: Unless something is changing right now, the future will have the same challenges we have now. People that teach programming are not teaching testing hand in hand with development. Compare that with electrical engineering. An EE graduating with a Bachelor’s or Master’s will have spent tons of time with oscilloscopes, logic analyzers, spectrum analyzers, frequency counters, power meters, and other pieces of equipment to validate that their circuits and systems work as expected.

Yet for CS, we don’t spend very much time at all, if any, making sure graduates know how to validate the software they build. And the internet is a mess of conflicting information. It’s really not surprising that testing is so misunderstood. I’m trying to make a dent in it. But really, the biggest challenge is education and countering old assumptions.

Ricky:I would be remiss if I didn’t mention the podcasts you host (Test and Code) and co-host (Python Bytes). Have there been any lessons or learning experiences from podcasting that have transferred over to your code? And has it helped you level up your programming in any way?

Brian: There are so many lessons! But I think the most important thing I’ve learned is the importance of developing the skill of listening and practicing it often. Asking questions is a great way to learn things, but it only works if you let yourself listen and hear the answer. I’ve had to edit out follow-up questions from episodes when, during editing, it was obvious that I hadn’t really listened to what the interviewee had already told me.

Careful listening is harder than we give it credit for, and so freaking powerful. Being a better listener definitely carries over into all other parts of my life, including management, programming, testing, and even family.

Ricky:Last but not least, when you’re not writing code, what else do you get up to in your spare time? What other hobbies and interests do you have? Anything you’d like to plug?

Brian: I most like spending time with my wife and kids. My wife and I have always been collectors of beautiful antique and vintage items, and we’ve tried to build a home full of eclectic, theatrical elements. We treat our living space as a giant shadow box art installation. More recently, that interest has moved into retail and we sell through a local antique mall. Feel free to see pics on Instagram, where we are FrivolityPDX.

I just started up the Python PDX West Meetup for West Portland with my Python Bytes co-host Michael Kennedy. I also wrote a book. Not only will you learn to use pytest very well, but you’ll also get lots of testing strategy advice in a book short enough to read in a weekend. Python Testing with pytest can be found at Pragmatic or on Amazon. And please, listen to Test & Code. Testing is not just about testing, it’s about development. Same goes for the podcast. That’s why there’s an “& Code” part in the name. :)


Thank you, Brian, for joining me for this interview! It’s been great having you on the other side of the interview mic (so to speak).

As always, if there’s someone you’d like me to interview in the future, then reach out to me in the comments below or send me a message on Twitter.


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Codementor: Creating Dashboard to Visualise Data In Python

$
0
0
List of the various dashboard libraries to allow the creation of a website dashboard in Python.

Catalin George Festila: Python 3.7.5 : Python and SQL, a fast approach.

$
0
0
Today he had to solve a task that required declaring a series of consecutive days to read from several SQL tables. As you know, this task would require a complex query to include statements. I solved it quite simply by using a python script that would automatically generate the days and then concatenate them into a query. It may not be the best solution but it is very fast and does not require

NumFOCUS: Astropy Receives $900k Grant from Moore Foundation

Programiz: Python CSV

$
0
0
In this tutorial, we will learn how to read and write into CSV files in Python with the help of examples.

Mike Driscoll: Black Friday Deals on Python Books

$
0
0

Interesting in learning Python? Well you will be happy to know that I am running a Black Friday / Cyber Monday sale of my Python books. But I am starting the sale early so that you have plenty of time to decide if you’d like to buy one of my books. Check them out below!

All My Python Books

Note that Python 101 is free. You can bump the amount to pay all the way down to $0 if you’d like a free copy.

Also note that all my books have free sample chapters so you can check those out before you purchase.


Creating GUI Applications with wxPython

Creating GUI Applications with wxPython is my latest book. In it you will learn how to create cross-platform desktop applications using wxPython. Use this link or click the image above to get a discount.


Jupyter Notebook 101

The Jupyter Notebook is a great teaching tool and it’s a fun way to use and learn Python and data science. I wrote a nice introductory book on the topic called Jupyter Notebook 101.


ReportLab – PDF Processing with Python

Creating and manipulating PDFs with Python is fun! In ReportLab – PDF Processing with Python you will learn how to create PDFs using the ReportLab package. You will also learn how to manipulate pre-existing PDFs using PyPDF2 and pdfrw as well as a few other handy PDF-related Python packages.


Python 201: Intermediate Python

Python 201: Intermediate Python

Python 201: Intermediate Python is a sequel to my first book, Python 101 and teaches its readers intermediate to advanced topics in Python.

The post Black Friday Deals on Python Books appeared first on The Mouse Vs. The Python.

Robin Wilson: Using SQLAlchemy to access MySQL without frustrating library installation issues

$
0
0

This is more a ‘note to myself’ than anything else, but I expect some other people might find it useful.

I’ve often struggled with accessing MySQL from Python, as the ‘default’ MySQL library for Python is MySQLdb. This library has a number of problems: 1) it is Python 2 only, and 2) it requires compiling against the MySQL C library and header files, and so can’t be simply installed using pip.

There is a Python 3 version of MySQLdb called mysqlclient, but this also requires compiling against the MySQL libraries and header files, so can be complicated to install.

The best library I’ve found as a replacement is PyMySQL which is a pure Python library (so no need to install MySQL libraries and header files). It’s API is basically exactly the same as MySQLdb, so it’s easy to switch across.

Right, that’s the introduction – and we’re really at the actual point of this post, which is how to go about using the PyMySQL library ‘under the hood’ when you’re accessing databases through SQLAlchemy.

The weird thing is that I’m not actually using SQLAlchemy by choice in my code – but it is used by pandas to convert between SQL and data frames.

For example, you can write code like this:

from sqlalchemy import create_engine
eng = create_engine('mysql://user:pass@127.0.0.1/database')
df.to_sql('table', eng, if_exists='append', index=False)

which will append the data in df to a table in a database running on the local machine.

The create_engine call is a SQLAlchemy function which creates an engine to handle all of the complex communication to and from a specific database.

Now, when you specify a database connection string with the mysql:// prefix, SQLAlchemy tries to use the MySQLdb library to do the underlying communication with the MySQL database – and fails if it can’t be found.

So, now we’re at the actual solution: which is that you can give SQLAlchemy a ‘dialect’ to use to connect to a database – and this can be used to change the underlying library that is used to talk to the database.

So, you can change your connection string to mysql+pymysql://user:pass@127.0.0.1/database and it will use the PyMySQL library. It’s as simple as that!

There are other dialects that you can use to connect to MySQL using different underlying libraries – although these aren’t recommended by the authors of SQLAlchemy. You can find a list of them here.

_I do data science work – including processing data in MySQL databases – as part of my freelance work. Please contact me for more details._

Podcast.__init__: Faster And Safer Software Development With Feature Flags

$
0
0
Any software project that is worked on or used by multiple people will inevitably reach a point where certain capabilities need to be turned on or off. In this episode Pete Hodgson shares his experience and insight into when, how, and why to use feature flags in your projects as a way to enable that practice. In addition to the simple on and off controls for certain logic paths, feature toggles also allow for more advanced patterns such as canary releases and A/B testing. This episode has something useful for anyone who works on software in any language.

Summary

Any software project that is worked on or used by multiple people will inevitably reach a point where certain capabilities need to be turned on or off. In this episode Pete Hodgson shares his experience and insight into when, how, and why to use feature flags in your projects as a way to enable that practice. In addition to the simple on and off controls for certain logic paths, feature toggles also allow for more advanced patterns such as canary releases and A/B testing. This episode has something useful for anyone who works on software in any language.

Announcements

  • Hello and welcome to Podcast.__init__, the podcast about Python and the people who make it great.
  • When you’re ready to launch your next app or want to try a project you hear about on the show, you’ll need somewhere to deploy it, so take a look at our friends over at Linode. With 200 Gbit/s private networking, scalable shared block storage, node balancers, and a 40 Gbit/s public network, all controlled by a brand new API you’ve got everything you need to scale up. And for your tasks that need fast computation, such as training machine learning models, they just launched dedicated CPU instances. Go to pythonpodcast.com/linode to get a $20 credit and launch a new server in under a minute. And don’t forget to thank them for their continued support of this show!
  • You listen to this show to learn and stay up to date with the ways that Python is being used, including the latest in machine learning and data analysis. For even more opportunities to meet, listen, and learn from your peers you don’t want to miss out on this year’s conference season. We have partnered with organizations such as O’Reilly Media, Dataversity, Corinium Global Intelligence, Alluxio, and Data Council. Go to pythonpodcast.com/conferences to learn more about these and other events, and take advantage of our partner discounts to save money when you register today.
  • Your host as usual is Tobias Macey and today I’m interviewing Pete Hodgson about the concept of feature flags and how they can benefit your development workflow

Interview

  • Introductions
  • How did you get introduced to Python?
  • Can you start by describing what a feature flag is?
    • What was your first experience with feature flags and how did it affect your approach to software development?
  • What are some of the ways that feature flags are used?
    • What are some antipatterns that you have seen for teams using feature flags?
  • What are some of the alternative development practices that teams will employ to achieve the same or similar outcomes to what is possible with feature flags?
  • Can you describe some of the different approaches to implementing feature flags in an application?
    • What are some of the common pitfalls or edge cases that teams run into when building an in-house solution?
    • What are some useful considerations when making a build vs. buy decision for a feature toggling service?
  • What are some of the complexities that get introduced by feature flags for mantaining application code over the long run?
  • What have you found to be useful or effective strategies for cataloging and documenting feature toggles in an application, particularly if they are long lived or for open source applications where there is no institutional context?
  • Can you describe some of the lifecycle considerations for feature flags, and how the design, implementation, or use of them changes for short-lived vs long-lived use cases?
  • What are some cases where the overhead of implementing and maintaining a feature flag infrastructure outweighs the potential benefit?
  • What advice or references do you recommend for anyone who is interested in using feature flags for their own work?

Keep In Touch

Picks

Closing Announcements

  • Thank you for listening! Don’t forget to check out our other show, the Data Engineering Podcast for the latest on modern data management.
  • Visit the site to subscribe to the show, sign up for the mailing list, and read the show notes.
  • If you’ve learned something or tried out a project from the show then tell us about it! Email hosts@podcastinit.com) with your story.
  • To help other people find the show please leave a review on iTunes and tell your friends and co-workers
  • Join the community in the new Zulip chat workspace at pythonpodcast.com/chat

Links

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

PyCharm: Webinar: “React+TypeScript+TDD Part 2” with Paul Everitt

$
0
0

Last month we did a webinar on using PyCharm to write React+TypeScript apps using TDD. We teased a “Part 2” that was more in-depth. Thanks to the good folks at WebStorm, we’ll be doing a Part 2, Tuesday, December 10th, 6:00 PM – 7:00 PM CET (12:00 – 1:00 PM EST).

  • Tuesday, December 10th
  • 6:00 PM – 7:00 PM CET (12:00 – 1:00 PM EST)
  • Register here
  • Aimed at intermediate frontend developers

DSGN-8345_Webinar_Weact+TypeScript+TDD_in_WebStorm_Part2_1300x880_mail_button_register

About Part 2

In the second part, we’ll dive a little deeper into how to be more productive when building React applications using TypeScript and cover the following topics:

  • Sharing props using type information
  • Class-based components with props
  • Modeling class state with TypeScript interfaces
  • Rich events and testing

In the first part we went slow, presuming less knowledge of both the React frontend stack and how to drive it in the IDE. This session will focus more on driving React and TypeScript features from a testing perspective.

As a reminder, both Part 1 and Part 2 are based on the tutorial in the PyCharm Guide.

Real Python: Python KeyError Exceptions and How to Handle Them

$
0
0

Python’s KeyError exception is a common exception encountered by beginners. Knowing why a KeyError can be raised and some solutions to prevent it from stopping your program are essential steps to improving as a Python programmer.

By the end of this course, you’ll know:

  • What a Python KeyError usually means
  • Where else you might see a KeyError in the standard library
  • How to handle a KeyError when you see it
  • When to raise a Python KeyError in your code

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Reuven Lerner: Black Friday: All of my Python courses are 50% off!

$
0
0

This coming Friday is “Black Friday,” when many stores offer big discounts on their products. I’m happy to say that from Friday through Monday, every course in my online store will be 50% off.

This includes all eight of the video courses in my online store:

There’s a new course in there — my brand-new “Intro Python: Functions” course tells you everything you need to understand writing and using Python functions. It’s aimed at people with programming experience but without a lot of experience with Python.

Oh, and you might also have noticed that my Pandas course is now complete, weighing in at 12.5 hours of videos (!), along with a large number of exercises.

But wait, there’s more: In 2020, I’ll be offering all six versions of Weekly Python Exercise (3 for beginners, and 3 for more experienced developers). If you buy them during this sale, you’ll save 50%. The cohorts might not be starting for several months, but you can lock in this price, and then begin the course along with the other students when it begins.

I’ll have more information about my Black Friday sale later this week. And I hope that this Black Friday will be an additional milestone as you improve your Python fluency.

Questions? Comments? Thoughts? Contact me at reuven@lerner.co.il, or on Twitter as @reuvenmlerner .

The post Black Friday: All of my Python courses are 50% off! appeared first on Reuven Lerner.

Paolo Amoroso: Revisiting Building Android Apps in Python Using Kivy with Android Studio

$
0
0
One of the books I read on Kivy, a Python cross-platform GUI framework, is Building Android Apps in Python Using Kivy with Android Studio: With Pyjnius, Plyer, and Buildozer by Ahmed Fawzy Mohamed Gad (Apress, 2019). My comments on the book, which focused on it not being a good match for my learning needs, sounded negative. Perhaps unnecessarily so.

Cover of Building Android Apps in Python Using Kivy with Android Studio in Google Play Books on a Pixel 2 XL
The cover of Building Android Apps in Python Using Kivy with Android Studio in Google Play Books on my Pixel 2 XL.

The author, Ahmed Fawzy Mohamed Gad, emailed me concerning my feedback. He provided more context on how he researched the market to position the book, planned the content, and selected the topics to cover. This is valuable information, so I’m publishing it here with his permission. Here’s what Ahmed wrote:

At first, thanks for the feedback you posted considering my book titled "Building Android Apps in Python Using Kivy with Android Studio". I read your feedback carefully and managed to send this e-mail to thank you for the time spent reading the book. 
As you said, my book does not cover many widgets about Kivy compared to the other book you mentioned which is titles "Kivy - Interactive Applications and Games in Python". Before preparing my book, I read this book and found that it discusses many Kivy widgets and some aspects of Kivy and the author really did a great job. 
Based on your feedback, I have some comments:
Publishing a book on a topic that someone touched before makes you think about something new to do. Talking about more widgets again in my book might not be a good idea because this book did that well. I preferred to continue where others stopped. Thus, I preferred to add some projects that make use of the widgets. So, my target is not "what are the widgets" but it is "how to use the widgets". 
Regarding using Android Studio to building the Android apps, the motivation behind this is that Kivy not currently supporting everything in Android and has some limitations. Plyer is not able to do everything at the current time. Thus, editing the generated Android Studio projects allows the developers to use Java for making some edits.  
You mentioned that developers use Kivy to avoid using Java but there are some situations that developers have to use Java. For example, you have to edit the Android Studio project to edit the presplash screen. As long as Kivy is not able to do everything in Android, then developers have to edit the project in Android Studio. Just using Kivy will build apps with limited features. 
Again I thank you for the time spent reading the book and leaving your feedback and hope you have time to read this e-mail.

First off, I’d like to thank Ahmed again for reaching out to me.

It’s an opportunity to clarify that, although I was looking for an introductory learning resource on Kivy covering the basic widgets and layouts, his goal was to differentiate from existing books and the official documentation. So Building Android Apps in Python Using Kivy with Android Studio may be more appropriate as a second book on Kivy rather than an introductory guide.

Another way my learning needs differ from Ahmed’s goals is in using only Python for developing Kivy apps for Android.

Java is too verbose and low-level for me. So I prefer to use only Python and, at least initially, work within the limits of what the language can do with Kivy on Android. But Java is the main language for Android developers, who are already familiar with Android Studio and may want to extend Kivy projects to add platform-specific features Python alone doesn’t support. It makes sense for a Kivy book to cover this.

There are parts of Ahmed’s book I did find valuable but didn’t elaborate on much in my earlier comments.

For example, one of the projects the book presents is an app for capturing images with the camera of an Android device and streaming a live feed to a Flask web app that visualizes them. This project is especially relevant to Android development as many apps have a remote backend for storing or processing data in the cloud.

Stack Abuse: Insertion Sort in Python

$
0
0

Introduction

If you're majoring in Computer Science, Insertion Sort is most likely one of the first sorting algorithms you have heard of. It is intuitive and easy to implement, but it's very slow on large arrays and is almost never used to sort them.

Insertion sort is often illustrated by comparing it to sorting a hand of cards while playing rummy. For those of you unfamiliar with the game, most players want the cards in their hand sorted in ascending order so they can quickly see which combinations they have at their disposal.

The dealer deals out 14 cards to you, you pick them up one by one and put them in your hand in the right order. During this entire process your hand holds the sorted "subarray" of your cards, while the remaining face down cards on the table are unsorted - from which you take cards one by one, and put them in your hand.

Insertion Sort is very simple and intuitive to implement, which is one of the reasons it's generally taught at an early stage in programming. It's a stable, in-place algorithm, that works really well for nearly-sorted or small arrays.

Let's elaborate these terms:

  • in-place: Requires a small, constant additional space (no matter the input size of the collection), but rewrites the original memory locations of the elements in the collection.
  • stable: The algorithm maintains the relative order of equal objects from the initial array. In other words, say your company's employee database returns "Dave Watson" and "Dave Brown" as two employees, in that specific order. If you were to sort them by their (shared) first name, a stable algorithm would guarantee that this order remains unchanged.

Another thing to note: Insertion Sort doesn't need to know the entire array in advance before sorting. The algorithm can receive one element at a time. Which is great if we want to add more elements to be sorted - the algorithm only inserts that element in it's proper place without "re-doing" the whole sort.

Insertion Sort is used rather often in practice, because of how efficient it is for small (~10 item) data sets. We will talk more about that later.

How Insertion Sort Works

An array is partitioned into a "sorted" subarray and an "unsorted" subarray. At the beginning, the sorted subarray contains only the first element of our original array.

The first element in the unsorted array is evaluated so that we can insert it into its proper place in the sorted subarray.

The insertion is done by moving all elements larger than the new element one position to the right.

Continue doing this until our entire array is sorted.

Keep in mind however that when we say an element is larger or smaller than another element - it doesn't necessarily mean larger or smaller integers.

We can define the words "larger" and "smaller" however we like when using custom objects. For example, point A can be "larger" than point B if it's further away from the center of the coordinate system.

We will mark the sorted subarray with bolded numbers, and use the following array to illustrate the algorithm:

8, 5, 4, 10, 9

The first step would be to "add" 8 to our sorted subarray.

8, 5, 4, 10, 9

Now we take a look at the first unsorted element - 5. We keep that value in a separate variable, for example current, for safe-keeping. 5 is less than 8. We move 8 one place to the right, effectively overwriting the 5 that was previously stored there (hence the separate variable for safe keeping):

8, 8, 4, 10, 9 (current = 5)

5 is lesser than all the elements in our sorted subarray, so we insert it in to the first position:

5, 8, 4, 10, 9

Next we look at number 4. We save that value in current. 4 is less than 8 so we move 8 to the right, and do the same with 5.

5, 5, 8, 10, 9 (current = 4)

Again we've encountered an element lesser than our entire sorted subarray, so we put it in the first position:

4, 5, 8, 10, 9

10 is greater than our rightmost element in the sorted subarray and is therefore larger than any of the elements to the left of 8. So we simply move on to the next element:

4, 5, 8, 10, 9

9 is less than 10, so we move 10 to the right:

4, 5, 8, 10, 10 (current = 9)

However, 9 is greater than 8, so we simply insert 9 right after 8.

4, 5, 8, 9, 10

Implementation

As we have previously mentioned, Insertion Sort is fairly easy to implement. We'll implement it first on a simple array of integers and then on some custom objects.

In practice, it's much more likely that you'll be working with objects and sorting them based on certain criteria.

Sorting Arrays

def insertion_sort(array):

    # We start from 1 since the first element is trivially sorted
    for index in range(1, len(array)):
        currentValue = array[index]
        currentPosition = index

        # As long as we haven't reached the beginning and there is an element
        # in our sorted array larger than the one we're trying to insert - move
        # that element to the right
        while currentPosition > 0 and array[currentPosition - 1] > currentValue:
            array[currentPosition] = array[currentPosition -1]
            currentPosition = currentPosition - 1


        # We have either reached the beginning of the array or we have found
        # an element of the sorted array that is smaller than the element
        # we're trying to insert at index currentPosition - 1.
        # Either way - we insert the element at currentPosition
        array[currentPosition] = currentValue

Let's populate a simple array and sort it:

array = [4, 22, 41, 40, 27, 30, 36, 16, 42, 37, 14, 39, 3, 6, 34, 9, 21, 2, 29, 47]
insertion_sort(array)
print("sorted array: " + str(array))

Output:

sorted array: [2, 3, 4, 6, 9, 14, 16, 21, 22, 27, 29, 30, 34, 36, 37, 39, 40, 41, 42, 47]

Note: It would have been technically incorrect if we reversed the order of conditions in our while loop. That is, if we first checked whether array[currentPosition-1] > currentValue before checking whether currentPosition > 0.

This would mean that if we did indeed reach the 0th element, we would first check whether array[-1] > currentValue. In Python this is "fine", although technically incorrect, since it wouldn't cause the loop to end prematurely or continue when it shouldn't.

This is because if we had reached the zeroth element, the second condition of currentPosition > 0 would fail regardless of the first condition, and cause the loop to break. In Python array[-1] > currentValue is equivalent to array[len(array) - 2] > currentValue and the interpreter wouldn't complain, but this is not a comparison we actually want to happen.

This reversed order of conditions is a bad idea. In a lot of cases it can lead to unexpected results that can be hard to debug because there is no syntactic or semantic error. Most programming languages would "complain" for trying to access the -1st element, but Python won't complain and it's easy to miss such a mistake.

The takeaway advice from this is to always check whether the index is valid before using it to access elements..

Sorting Custom Objects

We've mentioned before that we can define "greater than" and "lesser than" however we want - that the definition didn't need to rely solely on integers. There are several ways we can change our algorithm to work with custom objects.

We could redefine the actual comparison operators for our custom class and keep the same algorithm code as above. However, that would mean that we'd need to overload those operators if we wanted to sort the objects of our class in a different way.

Arguably the best way to use Insertion Sort for custom classes is to pass another argument to the insertion_sort method - specifically a comparison method. The most convenient way to do this is by using a custom lambda function when calling the sorting method.

In this implementation, we'll sort points where a "smaller" point is the one with a lower x coordinate.

First we'll define our Point class:

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return str.format("({},{})", self.x, self.y)

Then we make a slight change to our insertion_sort method to accommodate custom sorting:

def insertion_sort(array, compare_function):
    for index in range(1, len(array)):
        currentValue = array[index]
        currentPosition = index

        while currentPosition > 0 and compare_function(array[currentPosition - 1], currentValue):
            array[currentPosition] = array[currentPosition - 1]
            currentPosition = currentPosition - 1

        array[currentPosition] = currentValue

Finally we test the program:

A = Point(1,2)
B = Point(4,4)
C = Point(3,1)
D = Point(10,0)

array = [A,B,C,D]

# We sort by the x coordinate, ascending
insertion_sort(array, lambda a, b: a.x > b.x)

for point in array:
    print(point)

We get the output:

(1,2)
(3,1)
(4,4)
(10,0)

This algorithm will now work for any type of array, as long as we provide an appropriate comparison function.

Insertion Sort in Practice

Insertion sort may seem like a slow algorithm, and indeed in most cases it is too slow for any practical use with its O(n2) time complexity. However, as we've mentioned, it's very efficient on small arrays and on nearly sorted arrays.

This makes Insertion Sort very relevant for use in combination with algorithms that work well on large data sets.

For example, Java used a Dual Pivot Quick Sort as the primary sorting algorithm, but used Insertion Sort whenever the array (or subarray created by Quick Sort) had less than 7 elements.

Another efficient combination is simply ignoring all small subarrays created by Quick Sort, and then passing the final, nearly-sorted array, through Insertion Sort.

Another place where Insertion Sort left its mark is with a very popular algorithm called Shell Sort. Shell Sort works by calling Insertion Sort to sort pairs of elements far apart from each other, then incrementally reducing the gap between elements to should be compared.

Essentially making a lot of Insertion Sort calls to first small, and then nearly-sorted larger arrays, harnessing all the advantages it can.

Conclusion

Insertion Sort is a very simple, generally inefficient algorithm that nonetheless has several specific advantages that make it relevant even after many other, generally more efficient algorithms have been developed.

It remains a great algorithm for introducing future software developers into the world of sorting algorithms, and is still used in practice for specific scenarios in which it shines.

Paolo Amoroso: Two Books About the Kivy GUI Framework

$
0
0
The Kivy Python GUI framework is intriguing.

Not only it’s cross-platform but also supports Android. Java is too verbose and low level for me and Kivy is an opportunity for developing native Android apps without leaving Python.

Outside of the Kivy project documentation, there are few third-party advanced tutorials that go in more depth than the official tutorials. So, before diving into the code of the Kivy demos, I wanted some books to explore more features and get a broader picture of the framework and what it can do.

I found two potentially interesting books: Building Android Apps in Python Using Kivy with Android Studio: With Pyjnius, Plyer, and Buildozer by Ahmed Fawzy Mohamed Gad (Apress, 2019), and Kivy - Interactive Applications and Games in Python - Second Edition by Roberto Ulloa (Packt, 2015).

I read both and here are my impressions.

Building Android Apps in Python Using Kivy with Android Studio

I had much hope for Building Android Apps in Python Using Kivy with Android Studio as its focus is Android. And it was published just a few weeks ago, so it may be the most up to date of the books about the framework.

But I don’t recommend the book.

Cover of Building Android Apps in Python Using Kivy with Android Studio in Google Play Books on a Pixel 2 XL
The cover of Building Android Apps in Python Using Kivy with Android Studio in Google Play Books on my Pixel 2 XL.

It’s poorly edited, often repetitive, and, despite the size, covers only a handful of widgets, layouts, and Kivy features. The few interesting tidbits here and there aren't worth the price.

As a non-native English speaker and author I face the same difficulties in writing about complex technical topics in an unfamiliar language. Had the book been self-published, I wouldn’t have minded some editing issues. But I hold a large publishing house to higher standards.

The book presents a few sample apps it progressively builds on and enhances. In most cases, each new version includes the full source code of the previous versions, which sometimes makes it difficult to spot new or changed features. This, along with the poor editing, makes reading bumpy.

Kivy with Android Studio is an oxymoron. The point of using Python to develop Android apps is to not have to use Java, so this part of the book is useless to me. I’d prefer more examples of what Plyer can do without Java.

Despite all this, the author knows his stuff. I hope for a new, improved edition that keeps the focus on Android and Python, tells how to make apps responsive on a variety of screens, expands on Plyer, and drops Java.

Update

The author of Building Android Apps in Python Using Kivy with Android Studio, Ahmed Fawzy Mohamed Gad, emailed me to provide more context on how he researched and planned the book. His comments are interesting and explain the rationale behind his content selection decisions.

Thanks to Ahmed for sharing his thoughts.

Kivy - Interactive Applications and Games in Python

Kivy - Interactive Applications and Games in Python is the learning resource I was looking for.

At over half the size of Gad’s book, Ulloa’s is much better. It covers many more Kivy features that help develop complex, attractive, responsive apps with a wide variety of layouts and widgets.

Cover of Kivy - Interactive Applications and Games in Python in Google Play Books on a Pixel 2 XL
The cover of Kivy - Interactive Applications and Games in Python in Google Play Books on my Pixel 2 XL.

Aside from a game, the book discusses apps that showcase several advanced widgets and features of desktop and mobile user interfaces. The sample apps display fairly complex data, have a variety of user interface controls, and showcase different interaction techniques.

Ulloa anticipates the learning roadblocks and difficulties and clarifies some subtleties of Kivy. For example, code like this in the KV file of Kivy’s reStructured Text editor demo had me puzzled as it has the same identifier at both sides of a statement:

Root:
    text_input: text_input

Ulloa explains the left one is a widget attribute, the right one a widget ID. To avoid confusion, he uses a different style in which IDs start with an underscore, like this in the previous example:

Root:
    text_input: _text_input

According to the Kivy blog’s review of Kivy – Interactive Applications and Games in Python, the chapter on events and event binding is dense and difficult to grok. As a Kivy and Python beginner this wasn’t my impression. However, the coordinate transformations that event handlers often perform may be difficult to visualize.

The code fragments are often short and easy to follow but the book has a formatting issue that interferes with reading the code. In the ePub version of the ebook I bought at Google Play Books, the width of source code blocks is too narrow and most of the lines wrap.

Ulloa’s book covers enough building blocks to bootstrap my understanding of Kivy and continue on my own.

Chris Moffitt: Tips for Selecting Columns in a DataFrame

$
0
0

Introduction

This article will discuss several tips and shortcuts for using iloc to work with a data set that has a large number of columns. Even if you have some experience with using iloc you should learn a couple of helpful tricks to speed up your own analysis and avoid typing lots of column names in your code.

Why Do We Care About Selecting Columns?

In many standard data science examples, there are a relatively small number of columns. For example, Titanic has 8, Iris has 4, and Boston Housing has 14. Real-life data sets are messy and often include a lot of extra (potentially unnecessary) columns.

In data science problems you may need to select a subset of columns for one or more of the following reasons:

  • Filtering the data to only include the relevant columns can help shrink the memory footprint and speed up data processing.
  • Limiting the number of columns can reduce the mental overhead of keeping the data model in your head.
  • When exploring a new data set, it might be necessary to break to task into manageable chunks.
  • In some cases, you may need to loop through columns and perform calculations or cleanups in order to get the data in the format you need for further analysis.
  • Your data may just contain extra or duplicate information which is not needed.

Regardless of the reason, you may not need these techniques all the time. When you do, though, the tricks outlined below can reduce the amount of time you spend wrangling columns of data.

Also, if you like this type of content, I encourage you to check out Kevin Markham’s pandas tricks which served as an inspiration for a couple of the tips below.

The Data

In order to illustrate some examples, I’m going to use a quirky data set from the Central Park Squirrel Census. Yes, apparently there was an effort to count and catalog squirrels in Central Park. I thought this would be a fun example to work through. It also gave me a chance to include a squirrel image (credit: GeorgeB2) in my post :) .

This data set includes 3,023 rows of data and 31 columns. While 31 columns is not a tremendous number of columns, it is a useful example to illustrate the concepts you might apply to data with many more columns.

If you want to follow along, you can view the notebook or pull it directly from github.

Let’s get started by reading in the data.

importpandasaspdimportnumpyasnpdf=pd.read_csv('https://data.cityofnewyork.us/api/views/vfnx-vebw/rows.csv?accessType=DOWNLOAD&bom=true&format=true')

Sometimes it gets tricky to remember each column name and where it is by index. Here is a simple list comprehension to build up a reference list of all columns and their index.

col_mapping=[f"{c[0]}:{c[1]}"forcinenumerate(df.columns)]

Which creates a list like this:

['0:X','1:Y','2:Unique Squirrel ID','3:Hectare','4:Shift','5:Date',...'33:Borough Boundaries','34:City Council Districts','35:Police Precincts']

In some cases if you might want to rename a bunch of columns, you can use a dictionary comprehension to create a dictionary view of the data:

col_mapping_dict={c[0]:c[1]forcinenumerate(df.columns)}

Which creates this dictionary:

{0:'X',1:'Y',2:'Unique Squirrel ID',3:'Hectare',4:'Shift',5:'Date',...33:'Borough Boundaries',34:'City Council Districts',35:'Police Precincts'}

Having these variables defined can be useful as you progress through your analysis. Instead of repeatedly looking at your original file, you can just double check the variable name during your analysis.

One other common task I frequently have is to rename a bunch of columns that are inconsistently named across files. I use a dictionary to easily rename all the columns using something like df.rename(columns=col_mapping) Typing all the column names can be an error prone task. A simple trick is to copy all the columns in excel and use pd.read_clipboard() to build a small DataFrame and turn the columns into a dictionary. I can then manually type in the new names, if need be.

Here is a quick example with this data set:

df_cols=pd.read_clipboard()col_mapping={c[1]:''forcinenumerate(df_cols.columns)}

Which creates a dictionary that is relatively easy to populate with new names:

{'X':'','Y':'','Unique':'','Squirrel':'','ID':'','Hectare':'','Shift':'',...'Police':'','Precincts':''}

As an added bonus, you could even use an Excel file to set up the column renaming and automate the whole process. That tip is outside the scope of this article. If you are interested though, let me know in the comments.

Using iloc

The primary function we will walk through is panda’s iloc which is used for integer-location based indexing. New users may be slightly confused because iloc and loc can take a boolean-array which leads to more powerful indexing. Since both functions can a boolean array as input, there are times when these functions produce the same output. However, for the scope of this post, I will focus only on iloc column selection.

Here’s a simple graphic to illustrate the primary usage for iloc :

iloc illustrated

For instance, if you want to look at just the Squirrel ID column of data for all rows:

df.iloc[:,2]
0       37F-PM-1014-03
1       37E-PM-1006-03
2        2E-AM-1010-03
3        5D-PM-1018-05
4       39B-AM-1018-01
             ...
3018    30B-AM-1007-04
3019    19A-PM-1013-05
3020    22D-PM-1012-07
3021    29B-PM-1010-02
3022     5E-PM-1012-01
Name: Unique Squirrel ID, Length: 3023, dtype: object

If you want to look at the X and Y location as well as the ID, you can pass in a list of integers [0,1,2] :

df.iloc[:,[0,1,2]]
XYUnique Squirrel ID
0-73.95613440.79408237F-PM-1014-03
1-73.95704440.79485137E-PM-1006-03
2-73.97683140.7667182E-AM-1010-03
3-73.97572540.7697035D-PM-1018-05
4-73.95931340.79753339B-AM-1018-01
3018-73.96394340.79086830B-AM-1007-04
3019-73.97040240.78256019A-PM-1013-05
3020-73.96658740.78367822D-PM-1012-07
3021-73.96399440.78991529B-PM-1010-02
3022-73.97547940.7696405E-PM-1012-01

3023 rows × 3 columns

Typing all the columns is not the most efficient, so we can use slicing notation to make this a little easier to understand:

df.iloc[:,0:3]

Which will generate the same output as above.

If you have some experience with python lists, and have used pandas a bit; all of this usage should make sense. These points are pandas 101 concepts but we will build up from here.

While both of these approaches are straightforward, what if you want to combine the list of integers with the slice notation? You might try something like this:

df.iloc[:,[0:3,15:19]]
  File "<ipython-input-56-6f5b3426f412>", line 1
    df.iloc[:, [0:3,15:19]]
                 ^
SyntaxError: invalid syntax

Or, you could try something like this:

df.iloc[:,0:3,15:19]
IndexingError: Too many indexers

Hmmm. That obviously doesn’t work but seems like it would be useful for selecting ranges as well as individual columns.

Fortunately there is a numpy object that can help us out. The r_ object will “Translate slice objects to concatenation along the first axis.” It might not make much since from the documentation but it does exactly what we need.

Here’s a slightly more elaborate example to show how it works on a combination of individual list items and sliced ranges:

np.r_[0:3,15:19,24,25]
array([ 0,  1,  2, 15, 16, 17, 18, 24, 25])

That’s kind of cool. This object has converted the combination of integer lists and slice notation into a single list which we can pass to iloc :

df.iloc[:,np.r_[0:3,15:19,24,25]]
YUnique Squirrel IDDateHectare Squirrel NumberAgeRunningChasingClimbingEatingForagingOther ActivitiesKuksQuaasMoansTail flagsTail twitchesApproachesIndifferentRuns fromOther Interactions
040.79408237F-PM-1014-03101420183NaNFalseFalseFalseFalseFalseNaNFalseFalseFalseFalseFalseFalseFalseFalseNaN
140.79485137E-PM-1006-03100620183AdultTrueFalseFalseFalseFalseNaNFalseFalseFalseFalseFalseFalseFalseTrueme
240.7667182E-AM-1010-03101020183AdultFalseFalseTrueFalseFalseNaNFalseFalseFalseFalseFalseFalseTrueFalseNaN
340.7697035D-PM-1018-05101820185JuvenileFalseFalseTrueFalseFalseNaNFalseFalseFalseFalseFalseFalseFalseTrueNaN
301940.78256019A-PM-1013-05101320185AdultFalseFalseFalseFalseTrueNaNFalseFalseFalseFalseFalseFalseTrueFalseNaN
302040.78367822D-PM-1012-07101220187AdultFalseFalseFalseTrueTrueNaNFalseFalseFalseFalseFalseFalseTrueFalseNaN

3023 rows × 20 columns

Here is another tip. You may also use this notation when reading data using read_csv :

df_2=pd.read_csv('https://data.cityofnewyork.us/api/views/vfnx-vebw/rows.csv?accessType=DOWNLOAD&bom=true&format=true',usecols=np.r_[1,2,5:8,15:30],)

I find this notation helpful when you have a data set where you want to keep non-sequential columns and do not want to type out the full names of all the columns.

One caveat I would make is that you need to be careful when using the slice notation and keep in mind that the last number in the range will not be included in the generated list of numbers.

For example, if we specify the range 2:4 , we only get a list of 2 and 3:

np.r_[2:4]
array([2, 3])

If you want to include column index 4, use np.r_[2:5] .

One final comment on np.r_ is that there is an optional step argument. In this example, we can specify that this list will increment by 2:

np.r_[2:10:2]
array([2, 4, 6, 8])

This is a bit of a more advanced option and is not going to be intuitively obvious to a new pandas user. However if you ever find yourself parsing a lot of columns by index, this might be a useful tool to navigate a tricky scenario.

iloc and boolean arrays

One of the most powerful ways to filter columns is to pass a boolean array to iloc to select a subset of columns. This sounds a little complex but a couple of examples should make this understandable.

The most important concept is that we don’t generate a boolean array by hand but use the output from another pandas function to generate the array and feed it to iloc .

In this case, we can use the str accessor on a column index just like any other column of pandas data. This will generate the necessary boolean array that iloc expects. An example should help make this clear.

If we want to see which columns contain the word “run”:

run_cols=df.columns.str.contains('run',case=False)print(run_cols)
array([False, False, False, False, False, False, False, False, False,
    False, False, False, False, False, False,  True, False, False,
    False, False, False, False, False, False, False, False, False,
    False,  True, False, False, False, False, False, False, False])

Then we can pass this new array of boolean values to select only two columns:

df.iloc[:,run_cols].head()
RunningRuns from
0FalseFalse
1TrueTrue
2FalseFalse
3FalseTrue
4FalseFalse

In practice, many people will use a lambda function to do this in one line:

df.iloc[:,lambdadf:df.columns.str.contains('run',case=False)]

The benefits of using str functions are that you can get sophisticated with the potential filter options. For instance, if we want all the columns with “district,” “precinct” or “boundaries” in the name:

df.iloc[:,lambdadf:df.columns.str.contains('district|precinct|boundaries',case=False)].head()
Community DistrictsBorough BoundariesCity Council DistrictsPolice Precincts
01941913
11941913
21941913
31941913
41941913

We can even combine all these concepts together by using the results of the boolean array to get the index then use np.r_ to combine these lists together.

Here is an example where we want to get all the location related columns as well as the squirrel ID into a DataFrame:

location_cols=df.columns.str.contains('district|precinct|boundaries',case=False)location_indices=[ifori,colinenumerate(location_cols)ifcol]df.iloc[:,np.r_[0:3,location_indices]].head()
XYUnique Squirrel IDCommunity DistrictsBorough BoundariesCity Council DistrictsPolice Precincts
0-73.95613440.79408237F-PM-1014-031941913
1-73.95704440.79485137E-PM-1006-031941913
2-73.97683140.7667182E-AM-1010-031941913
3-73.97572540.7697035D-PM-1018-051941913
4-73.95931340.79753339B-AM-1018-011941913

This code is a little complicated since we are using a conditional list comprehension and might be overkill for selecting 7 columns. The important concept is that you know it is possible and can refer back to this article when you need it for your own analysis.

Caveats

One item to keep in mind when dealing with numerical indexing of columns is that you need to understand where your data comes from. If you expect you ID column to always be ain a specific location and it changes order in the data, you could face problems with your subsequent data processing. This situation is where your domain knowledge and expertise comes into play to make sure the solution is robust enough for the given situation.

Summary

Most of my data analysis involves filtering and selecting data at the row level. However there are times when it is helpful to work with data in a column-wise fashion. Pandas iloc can be a useful tool for quickly and efficiently working with data sets that have many columns of data. I hope this article provided a couple of tips that will help you with your own analysis.

PyCoder’s Weekly: Issue #396 (Nov. 26, 2019)

$
0
0

#396 – NOVEMBER 26, 2019
View in Browser »

The PyCoder’s Weekly Logo


PyCon 2020 Registration Is Now Open

PyCon is the largest annual gathering for the community that uses and develops the open-source Python programming language. April 15–23 in Pittsburgh. Pro-tip: Get your early bird tickets today.
PYCON.ORG

Python KeyError Exceptions and How to Handle Them

KeyError exceptions are often caused by a bad key lookup in a dictionary, but there are a few other situations when a KeyError can be raised as well. Knowing how to handle these exceptions is essential to writing good Python code.
REAL PYTHONvideo

Automate & Standardize Code Reviews For Python

alt

Take the hassle out of code reviews - Codacy flags errors automatically, directly from your Git workflow. Customize standards on coverage, duplication, complexity & style violations. Use in the cloud or on your servers for 30 different languages. Get started for free →
CODACYsponsor

Pragmatic Unicode (2012)

Timeless tutorial that teaches the basics of Unicode, and how both Python 2 and Python 3 work, as well as some key tips on solving Unicode problems.
NED BATCHELDER

Guido van Rossum on How Python Makes Thinking in Code Easier

A conversation with the creator of the world’s most popular programming language on removing brain friction for better work.
DROPBOX.COM

What’s New in Python 3.9a1

The first draft changelog for Python 3.9 alpha 1 is out, if you want to stay on top of the latest CPython developments.
PYTHON.ORG

Raise Better Exceptions in Python

How to include contextual information in your exceptions for easier root cause analysis.
MOSHE ZADKA

Testing Django Migrations

A web app outage post-mortem with useful tips and tools for testing Django migrations.
NIKITA SOBOLEV

Python Jobs

Senior Python Engineer (Munich, Germany)

Stylight GmbH

Senior Python/Django Developer (Eindhoven, Netherlands)

Sendcloud

Django Full Stack Web Developer (Austin, TX)

Zeitcode

More Python Jobs >>>

Articles & Tutorials

Python Community Interview With Brian Okken

Brian Okken is perhaps best known as the author of Python Testing with pytest, as well as being the host of two Python-related podcasts. Find out more about the man behind the voice, his new meetup in Portland, and the advice he’d like to give to anyone who’s new to testing software.
REAL PYTHON

Issues With Python 3 byte/str Separation

The original title of the article is The Incredible Disaster of Python 3 so this was probably written with some frustration still fresh on someone’s mind. Nevertheless I think it’s important to look at aspects of Python that developers might be struggling with and that could be improved. Related discussion on Hacker News.
JOHN GOERZENopinion

Python 2 EOL: Are You Prepared? Take ActiveState’s Survey For a Chance to Win a Drone

alt

Python 2 End of Life is coming soon. Take ActiveState’s 5-minute survey to let them know how you’re preparing for the change. You’ll get the final results, plus the chance to win a camera drone. Take the Survey →
ACTIVESTATEsponsor

Invalid Syntax in Python: Common Reasons for SyntaxError

In this step-by-step tutorial, you’ll see common examples of invalid syntax in Python and learn how to resolve the issue. If you’ve ever received a SyntaxError when trying to run your Python code, then this is the guide for you.
REAL PYTHON

Teaching Python Episode 33: Interview With Eric Matthes

Kelly and Sean interview Eric Matthes, author of Python Crash Course, about how he began programming, what led him to teaching, and the important lessons from Python to be learned both inside and outside of the classroom.
TEACHINGPYTHON.FMpodcast

Detecting “Thinking Intensity” in the Human Brain, With Python

How to get started with fNIRS sensing data specifically oxygenated hemoglobin “HbO2/HbO” data for analyzing a data stream from a sensor.
JOHN DAVID CHIBUK

Measure and Improve Python Code Performance With Blackfire.io

Profile in development, test/staging, and production, with no overhead for end users! Blackfire supports any Python version from 2.7.x and 3.x. Find bottlenecks in wall-time, I/O, CPU, memory, HTTP requests, and SQL queries.
BLACKFIREsponsor

Counting FLOPS and Other CPU Counters in Python

How the python-papi package can be used to measure the FLOP requirements of any section of a Python program.
BOJAN NIKOLIC

Projects & Code

Events


Happy Pythoning!
This was PyCoder’s Weekly Issue #396.
View in Browser »

alt

[ Subscribe to 🐍 PyCoder’s Weekly 💌 – Get the best Python news, articles, and tutorials delivered to your inbox once a week >> Click here to learn more ]

Codementor: Awesome Full Stack Python Resources

$
0
0
A list of Python Full Stack resources for aspiring Python web developer looking to learn web development using Python.
Viewing all 22412 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>