Wednesday, June 30, 2021

STX Next: FastAPI vs. Flask: Comparing the Pros and Cons of Top Microframeworks for Building a REST API in Python

Creating web applications such as REST APIs is the bread and butter of backend developers. Therefore, working with a web framework should be quick and easy.



from Planet Python
via read more

Python Anywhere: Glastonbury: a new system image, with Python 3.9 and Ubuntu 20.04

If you signed up for an account on PythonAnywhere after 21 June 2021, you’ll have Python 3.9 available – you can use it just like any other Python version. Additionally, the underlying operating system for your account will be Ubuntu 20.04, rather than the 16.04 used by older accounts.

If you signed up before that date, you’ll be on an older “system image” – essentially the version of the operating system and the set of installed packages that you have access to. You can switch to the new system image from the “Account” page, but you may need to make changes to your code and/or virtualenvs to make everything work – there’s more information on the linked page.

This post has more details on what’s new in the glastonbury system image. There’s a lot!



from Planet Python
via read more

Model Debugging Strategies – Machine Learning Guide

You can’t deploy software based on a buggy model. Debugging is a big part of model development and deployment in machine learning.  Is there one silver-bullet strategy that works for all types of models and applications? Not really, there are plenty of different strategies for model debugging, and we’re going to explore 10 of them […]

The post Model Debugging Strategies – Machine Learning Guide appeared first on neptune.ai.



from Planet SciPy
read more

PyCharm: Early Access PyCharm: PyCharm and Data Science

For a very long time, PyCharm’s Data Science tooling has not been a feature set that we’ve talked at length about. We’ve got a lot wrong, but this time around, we’ve changed the way we approach building tools for Data Scientists. I sat down with Andrey Cheptsov, the Product Manager of the newly created DataSpell IDE and asked him how this time, it’s going to be different.

Transcript

Nafiul: Hey everybody. This is early access PyCharm. I’m your host Nafiul Islam. Today we have a special treat for you. I’m here with Andrey Cheptsov who is the product manager for a new IDE that we’re about to release called DataSpell. That’s going to be targeted towards data scientists, but a lot of the features of DataSpell, in fact, if not all of them, are going to be ported into PyCharm and I’m just super excited about it. So without much further ado, let’s dive in.
So Andrey, you’re the guy that everybody talks to when it comes to the data science features of PyCharm. What have you been up to this past year in terms of data science?
Andrey Cheptsov: All right. I think it’s actually a little bit more than a year. I think we’ve started working on that on October 2019 and yeah, many things to talk about. So where do I start?
Nafiul: You can start talking about the fact that, our data science offering has had a few ups and downs. So what have we been up to in trying to fix that?
Andrey Cheptsov: Awesome. Indeed it’s a good starting point to discuss the data science supported by PyCharm. PyCharm is an IDE for Python developers. It’s known for its let’s say intelligent coding assistance, which people like, like refactoring, code completion and quick fixes.
And if you look at the community of Python developers, we see that there are totally different, let’s say job titles and mindsets who use the IDE, but what is prevailing? What is the if I say? If I can say the main one is probably developers. So PyCharm is very well known within the developer community.
Nafiul: Oh, absolutely. Yeah, it is.
Andrey Cheptsov: And I think it’s for a good reason, because it really nails the core development experience, which is the good support for Git, built-in tools, such as terminal. Of course the editor is there is the heart of the IDE. And the workflow, it takes well some time to set up your project, but then you work with that project for quite significant amount of time, and then you commit all your work through Git, and then, that’s how it works.
And however, Python is a lot more than just development and there are many other things going on, and data science is for sure one of, one of the other main things taking place. And if we take a look at the some of the survey, surveys asking like how people use Python, at Stack Overflow, for example, or JetBrains own DevEcosystem survey, we’ll see that a lot of people use Python for data analysis and also for machine learning. It’s it’s, it’s quite interesting because there are different ways of using Python for data analysis and machine learning. So for example, there are ways, there are software developers which use or somehow involved in data analysis. It could be either they write software for data analysis.
So basically they automate things around data. Yeah. And there are also people who apart from software development are involved in ad hoc data analysis, which is a very interesting thing. And also was probably a starting point for us to look differently at the support for data science within PyCharm.
I think that’s where it started and basically…
Nafiul: But here’s the thing, we’ve added different kinds of support for data science in PyCharm, again and again, we did it not just a year ago, but years ago. And we… something happened. It wasn’t what people expected. So what are we doing different this time around to make sure that we nail that data science story?
Andrey Cheptsov: Yeah. That’s a good one. Thanks for asking. I think if I would if I would say what has changed in their approach we are taking the main thing would be to look from the perspective of the people who are involved in data analysis. And maybe talk a little bit about what is it, what is data analysis? What are we talking about and how it looks like. If I can describe very briefly and on a primitive level, like what data analysis is and how it’s different from development, probably I would describe it the following way. There are certain tools which one can use to look at the data.
And when I say, look at the data, I really mean it. Literally staring at the data.
Nafiul: At rows and rows of random numbers and fields, huh?
Andrey Cheptsov: Yeah, exactly. And so to to make sense out of data, you have to use certain tools which we’ll talk about. And then in the end you have to look at them yourself and it cannot be automated to a certain way.
You have to look at the, either the raw data at the processed data or at visualized data. And this is how data analysis is done. You process data, and then do you visualize data? And there’s a lot of different tools used for that.
Nafiul: And so the approach of this time around is to really focus on what data scientists wanted to do and coming at it from their perspective, instead of just saying, making it an offshoot of what we offer software engineers in general.
So it’s more about what they need instead of how we can retrofit some kind of a data science support into the IDE.
Andrey Cheptsov: Yeah. One thing that is changing now. That’s what we changed for some time is a PyCharm is very good at providing a way to read and write code and then also run code.
And while it’s also still very important part of the data analysis since to do data analysis, you still have to write case and a lot of things I’ve done through code. Yet, there’s another key thing, which is indirective way of working with data and maybe…
Nafiul: So that means visualizations and just getting quick feedback when you input something.
Andrey Cheptsov: Yeah, absolutely. And that’s maybe a good point of time to talk about the tools which make data analysis or so, so efficient, let’s say. There are many different tools data scientists use today. And you can, you can get a rough idea by looking at these, let’s say surveys, for example, Stack Overflow or DevEco, like what tools data scientists actually use and what is… and what you can immediately see, for example, there’s a NumPy library, right?
Which let you work efficiently with data and process this data. And when you process data you have to analyze what you get, like basically look at this data some way, and this is where there are two things come very handy. One is the interactive Python. Or,
Nafiul: Yeah.
Andrey Cheptsov: a tool, like IPython, for example, which implementes this REPL mode when you write something, then you run it and then you look at it and then you can run through it again, and then you see the results again.
So this is a very different from what typically a code editor offers to you.
Yes, absolutely.
And the other thing is of course, the Jupiter notebooks, which are very famous and there are people who love them, and there are people who hate them. And with all of that you cannot deny, one cannot deny that Jupyter Notebooks is probably the best way today to interact, work with the data.
Nafiul: Okay. So in terms of design decisions you went out there and you just saw the different ways in which data scientists were interacting with data, were working with data, were playing with data, and I’m guessing that in the new thing that we’re going to do the most important thing is going to be Jupyter support and top-notch Jupyter support inside of the IDE.
Am I correct in assuming that?
Andrey Cheptsov: Oh, yeah. So the, one of the main things is a better support for Jupyter notebooks. It’s not the only thing I think we’ll cover the other things too. But if we start somewhere, Jupyter notebooks support is totally the right place to start. If it, if I can describe very briefly what we do now, basically do in terms of the better support for Jupyter notebooks we… or what we do now, the approach was to try to see if we can make something like Jupyter notebook out of the code editor, which PyCharm already has.
And this didn’t seem to work really well to a great number of reasons. One reason is that notebooks are so handy because they can, they let you see their results immediately in line with your let’s say code. So you’ve…
Nafiul: They’re also quite portable. You can share them with a friend and they can also run the code and they can also see the visualizations or they can see a rendered version of that.
So going back to the old WolframAlpha and Mathematica days where you would have like a notebook with different equations being solved by your CAS system. Okay. So your main focus is going to be Jupyter notebooks. You do want to add other features, of course, that’s not the only thing. But what are we working on that makes this offering in PyCharm just really amazing to use? What have we done? What was special sauce that we added to make this top-notch?
Andrey Cheptsov: Right. Yeah. I think the secret sauce probably would be something very well obvious here. So we gonna take the best parts of the Jupyter notebook. We’re going to keep the best parts of this Jupyter notebook.
And make sure that we don’t miss any of the advantages Jupyter notebooks offers. So we are talking about the inline cell outputs, we are talking about the command mode, which makes it easy to navigate over the cells and also apply commands in there, familiar shortcuts. We’re also talking about JavaScript outputs, which was a problem previously, if you’re using some interactive library like Plotly or what care or widgets it didn’t work due to the poor support, the interoperability between the IDE and JavaScript. This is also something that we’ve been up within a change. Basically, we were going to make notebooks work exactly as you expect them to work with all of the nice things, which you like in, in Jupyter notebook and at the same time, we also want to keep some of the things PyCharm is typically good at and make sure that it applies to this new Jupyter notebook support.
Nafiul: So in terms of performance, are we working on making this as fluid and as fast and as smooth as possible?
In, in, in terms of just working with it is it going to be a similar experience to writing in your editor where every, where the code completion pops up very well and you can debug and you get all the IDE features that you love about PyCharm, but you now have it in like a notebook format.
Andrey Cheptsov: Yeah. So performance is a is one of the main, let’s say aspects. And this was a big problem as well with the previous support for Jupyter Notebook. Especially when you start to import a lot of data and then you start to visualize all of it. We used to have problems and now we addressed most of it, which prevented the, like the way of working with notebooks.
So we want to expect at least the same experience which you typically have with Jupyter notebooks except that you also get coding assistance on top of that without any, um, delays.
Nafiul: You know, Python isn’t, this is of course a PyCharm show, but Python isn’t the only language t hat is prominent in data science. So what other plans do we have for people who are using R, people who are using Julia or whatever other language that they want to use for data science?
Andrey Cheptsov: I think what is worth mentioning here is we bring a better support for Jupyter notebooks for Python developers. A big chunk of the work here is to make just language-independent notebook support with the IntelliJ platform. And while of course the notebook support is super important for Python developers at the point of, at this point of time, more than, for example, R or Julia or other languages we, we think it is super important to make it language-independent. And …
Nafiul: That, that has to be the case because if you take a look at like the notebook, the IPython kernel, this notebook format is being adopted by Scala. It’s been adopted by Julia. It’s been adopted by other languages because it helps, it… Because it’s portable.
It helps you transfer that idea and that ended processing work from one place to another, from one data scientist or one data analysts to another. So building this thing as something that works across languages, I would assume is going to pay out dividends when we support more languages.
Yeah, absolutely. Um, and I actually look at it this even of course, it’s a higher return, let’s say on investments on our end now that we support Python, if other people can use it for other languages, we don’t have to implement it again. That’s one thing.
The other one is, which I think is super important as well, is Python is a super great language for data science. Um, it doesn’t mean that other languages shouldn’t also improve the support for data science workflows. And I see a lot of potential in other languages as well. And um. Of course Scala is one case, but I think, I personally see here a trend that the data science is going to be a big thing also for other languages as well. And if you look for example, what’s going on with JavaScript, there is a lot of things what’s going on and like in the end everybody can benefit out of better support for notebooks in other languages.
I can absolutely imagine, but I’m assuming that building that support across libraries, across languages, across frameworks is going to be difficult. But is that made a little bit easier now that we can embed a web browser into the IDE?
Andrey Cheptsov: Yeah, absolutely. And I think you’ve just nailed it.
We can make it possible because we’ve integrated, we found let’s say a working way of integrating of JavaScript and the IDE in order to make it interactive. And probably now that we support Jupiter notebooks, it’s already very easy to support known Python kernels of Jupyter notebooks.
So that’s the most easy, let’s say step here. More difficult steps will be to support non-Jupyter notebooks. Which of course they are there.
Nafiul: So Andrey last question, before we wrap this up When are we going to get all this goodness, when can we actually get our hands on working with all these goodness that you’re talking about and how can we get it?
Andrey Cheptsov: Yeah, sure. So I think it depends on um, If, whether you are open to, to try one of their unstable builds, for example, or try some early previews and eager to also share a feedback, if that’s the case I would strongly suggest you to join the private beta and sign up for that. And we are send new builds. And basically in September we are going to make it public and available for everyone.
So you don’t really have to register it’s still going to be an EAP quality which means there are bugs we fix that.
Yeah. Yeah. And yeah. And speaking of the release of the DataSpell currently most likely that it’s going to be there the spring 2022.1 release train of IntelliJ-based IDEs.
Nafiul: So when is PyCharm going to see all of us?
Andrey Cheptsov: It’s very likely that PyCharm will get this once DataSpell is released which is most likely to happen, yeah, in spring. Still chances that it might happen even sooner, but most likely, yeah, spring of the next year.
Nafiul: Awesome.
Thank you very much, Andrey for dropping by and we’ll see you again soon.
Andrey Cheptsov: Yeah. Thanks for having me.



from Planet Python
via read more

PyCharm: PyCharm 2021.1.3 Is Out

The PyCharm 2021.1.3 release fixes some major issues in the IDE performance, UI, and specific features. Support for Django, frontend, and databases is only available in PyCharm Professional.

Thank you for all the feedback you’ve shared with us in our issue tracker!

You can get the latest build from inside the IDE, with the Toolbox App, or using snaps for Ubuntu. It is also available for download from our website.

Download PyCharm 2021.1.3

Here are the major bug fixes:

  • Fixed the problems behind several UI freezes. [JBR-2755]
  • Fixed the infinite indexing issue. [IDEA-266989]
  • User interface: Ctrl+Shift+Arrow shortcut selects text in a dialog window rather than stretching the window. [IDEA-267294]
  • It is now possible to work with KWallet without errors. [IDEA-261634]
  • Quick documentation: .jpeg and .svg images are rendered correctly. [IDEA-256001]
  • Plugins: disabled plugins that have been updated to the latest version don’t cause a false notification saying a plugin update is required. [IDEA-267878]
  • VCS: Fixed an unexpected closure of the “Compare branch” dialog. [IDEA-251381]
  • Projector: PyCharm doesn’t crash on restart when Projector is used. [IDEA-269591]
  • Python Console: “Run Cell and Select Below” shortcut (Ctrl+Enter) in SciView now moves the caret to the next cell after execution. [PY-48157]
  • Django: Code Completion for Django Models now works correctly. [PY-48252]
  • Debugger: when the “Attach to Node.js/Chrome” Run/Debug configuration is used, the debugger reconnects automatically upon node restart. [WEB-50455]
  • Databases: code completion for table synonyms works correctly for databases in MS SQL. [DBE-4531]

For the full list of resolved issues, please see the release notes.



from Planet Python
via read more

Zero to Mastery: Python Monthly πŸ’»πŸ June 2021

19th issue of Python Monthly! Read by 20,000+ Python developers every month. This monthly Python newsletter is focused on keeping you up to date with the industry and keeping your skills sharp, without wasting your valuable time.

from Planet Python
via read more

Beautiful Soup: Build a Web Scraper With Python

The incredible amount of data on the Internet is a rich resource for any field of research or personal interest. To effectively harvest that data, you’ll need to become skilled at web scraping. The Python libraries requests and Beautiful Soup are powerful tools for the job. If you like to learn with hands-on examples and have a basic understanding of Python and HTML, then this tutorial is for you.

In this tutorial, you’ll learn how to:

  • Inspect the HTML structure of your target site with your browser’s developer tools
  • Decipher data encoded in URLs
  • Use requests and Beautiful Soup for scraping and parsing data from the Web
  • Step through a web scraping pipeline from start to finish
  • Build a script that fetches job offers from the Web and displays relevant information in your console

Working through this project will give you the knowledge of the process and tools you need to scrape any static website out there on the World Wide Web. You can download the project source code by clicking on the link below:

Let’s get started!

What Is Web Scraping?

Web scraping is the process of gathering information from the Internet. Even copying and pasting the lyrics of your favorite song is a form of web scraping! However, the words “web scraping” usually refer to a process that involves automation. Some websites don’t like it when automatic scrapers gather their data, while others don’t mind.

If you’re scraping a page respectfully for educational purposes, then you’re unlikely to have any problems. Still, it’s a good idea to do some research on your own and make sure that you’re not violating any Terms of Service before you start a large-scale project.

Reasons for Web Scraping

Say you’re a surfer, both online and in real life, and you’re looking for employment. However, you’re not looking for just any job. With a surfer’s mindset, you’re waiting for the perfect opportunity to roll your way!

There’s a job site that offers precisely the kinds of jobs you want. Unfortunately, a new position only pops up once in a blue moon, and the site doesn’t provide an email notification service. You think about checking up on it every day, but that doesn’t sound like the most fun and productive way to spend your time.

Thankfully, the world offers other ways to apply that surfer’s mindset! Instead of looking at the job site every day, you can use Python to help automate your job search’s repetitive parts. Automated web scraping can be a solution to speed up the data collection process. You write your code once, and it will get the information you want many times and from many pages.

In contrast, when you try to get the information you want manually, you might spend a lot of time clicking, scrolling, and searching, especially if you need large amounts of data from websites that are regularly updated with new content. Manual web scraping can take a lot of time and repetition.

There’s so much information on the Web, and new information is constantly added. You’ll probably be interested in at least some of that data, and much of it is just out there for the taking. Whether you’re actually on the job hunt or you want to download all the lyrics of your favorite artist, automated web scraping can help you accomplish your goals.

Challenges of Web Scraping

The Web has grown organically out of many sources. It combines many different technologies, styles, and personalities, and it continues to grow to this day. In other words, the Web is a hot mess! Because of this, you’ll run into some challenges when scraping the Web:

  • Variety: Every website is different. While you’ll encounter general structures that repeat themselves, each website is unique and will need personal treatment if you want to extract the relevant information.

  • Durability: Websites constantly change. Say you’ve built a shiny new web scraper that automatically cherry-picks what you want from your resource of interest. The first time you run your script, it works flawlessly. But when you run the same script only a short while later, you run into a discouraging and lengthy stack of tracebacks!

Unstable scripts are a realistic scenario, as many websites are in active development. Once the site’s structure has changed, your scraper might not be able to navigate the sitemap correctly or find the relevant information. The good news is that many changes to websites are small and incremental, so you’ll likely be able to update your scraper with only minimal adjustments.

However, keep in mind that because the Internet is dynamic, the scrapers you’ll build will probably require constant maintenance. You can set up continuous integration to run scraping tests periodically to ensure that your main script doesn’t break without your knowledge.

An Alternative to Web Scraping: APIs

Some website providers offer application programming interfaces (APIs) that allow you to access their data in a predefined manner. With APIs, you can avoid parsing HTML. Instead, you can access the data directly using formats like JSON and XML. HTML is primarily a way to present content to users visually.

When you use an API, the process is generally more stable than gathering the data through web scraping. That’s because developers create APIs to be consumed by programs rather than by human eyes.

The front-end presentation of a site might change often, but such a change in the website’s design doesn’t affect its API structure. The structure of an API is usually more permanent, which means it’s a more reliable source of the site’s data.

However, APIs can change as well. The challenges of both variety and durability apply to APIs just as they do to websites. Additionally, it’s much harder to inspect the structure of an API by yourself if the provided documentation lacks quality.

Read the full article at https://realpython.com/beautiful-soup-web-scraper-python/ »


[ 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 ]



from Real Python
read more

Real Python: Beautiful Soup: Build a Web Scraper With Python

The incredible amount of data on the Internet is a rich resource for any field of research or personal interest. To effectively harvest that data, you’ll need to become skilled at web scraping. The Python libraries requests and Beautiful Soup are powerful tools for the job. If you like to learn with hands-on examples and have a basic understanding of Python and HTML, then this tutorial is for you.

In this tutorial, you’ll learn how to:

  • Inspect the HTML structure of your target site with your browser’s developer tools
  • Decipher data encoded in URLs
  • Use requests and Beautiful Soup for scraping and parsing data from the Web
  • Step through a web scraping pipeline from start to finish
  • Build a script that fetches job offers from the Web and displays relevant information in your console

Working through this project will give you the knowledge of the process and tools you need to scrape any static website out there on the World Wide Web. You can download the project source code by clicking on the link below:

Get Sample Code: Click here to get the sample code you’ll use for the project and examples in this tutorial.

Let’s get started!

What Is Web Scraping?

Web scraping is the process of gathering information from the Internet. Even copying and pasting the lyrics of your favorite song is a form of web scraping! However, the words “web scraping” usually refer to a process that involves automation. Some websites don’t like it when automatic scrapers gather their data, while others don’t mind.

If you’re scraping a page respectfully for educational purposes, then you’re unlikely to have any problems. Still, it’s a good idea to do some research on your own and make sure that you’re not violating any Terms of Service before you start a large-scale project.

Reasons for Web Scraping

Say you’re a surfer, both online and in real life, and you’re looking for employment. However, you’re not looking for just any job. With a surfer’s mindset, you’re waiting for the perfect opportunity to roll your way!

There’s a job site that offers precisely the kinds of jobs you want. Unfortunately, a new position only pops up once in a blue moon, and the site doesn’t provide an email notification service. You think about checking up on it every day, but that doesn’t sound like the most fun and productive way to spend your time.

Thankfully, the world offers other ways to apply that surfer’s mindset! Instead of looking at the job site every day, you can use Python to help automate your job search’s repetitive parts. Automated web scraping can be a solution to speed up the data collection process. You write your code once, and it will get the information you want many times and from many pages.

In contrast, when you try to get the information you want manually, you might spend a lot of time clicking, scrolling, and searching, especially if you need large amounts of data from websites that are regularly updated with new content. Manual web scraping can take a lot of time and repetition.

There’s so much information on the Web, and new information is constantly added. You’ll probably be interested in at least some of that data, and much of it is just out there for the taking. Whether you’re actually on the job hunt or you want to download all the lyrics of your favorite artist, automated web scraping can help you accomplish your goals.

Challenges of Web Scraping

The Web has grown organically out of many sources. It combines many different technologies, styles, and personalities, and it continues to grow to this day. In other words, the Web is a hot mess! Because of this, you’ll run into some challenges when scraping the Web:

  • Variety: Every website is different. While you’ll encounter general structures that repeat themselves, each website is unique and will need personal treatment if you want to extract the relevant information.

  • Durability: Websites constantly change. Say you’ve built a shiny new web scraper that automatically cherry-picks what you want from your resource of interest. The first time you run your script, it works flawlessly. But when you run the same script only a short while later, you run into a discouraging and lengthy stack of tracebacks!

Unstable scripts are a realistic scenario, as many websites are in active development. Once the site’s structure has changed, your scraper might not be able to navigate the sitemap correctly or find the relevant information. The good news is that many changes to websites are small and incremental, so you’ll likely be able to update your scraper with only minimal adjustments.

However, keep in mind that because the Internet is dynamic, the scrapers you’ll build will probably require constant maintenance. You can set up continuous integration to run scraping tests periodically to ensure that your main script doesn’t break without your knowledge.

An Alternative to Web Scraping: APIs

Some website providers offer application programming interfaces (APIs) that allow you to access their data in a predefined manner. With APIs, you can avoid parsing HTML. Instead, you can access the data directly using formats like JSON and XML. HTML is primarily a way to present content to users visually.

When you use an API, the process is generally more stable than gathering the data through web scraping. That’s because developers create APIs to be consumed by programs rather than by human eyes.

The front-end presentation of a site might change often, but such a change in the website’s design doesn’t affect its API structure. The structure of an API is usually more permanent, which means it’s a more reliable source of the site’s data.

However, APIs can change as well. The challenges of both variety and durability apply to APIs just as they do to websites. Additionally, it’s much harder to inspect the structure of an API by yourself if the provided documentation lacks quality.

Read the full article at https://realpython.com/beautiful-soup-web-scraper-python/ »


[ 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 ]



from Planet Python
via read more

Python for Beginners: Introduction to cmath module in Python

While working on data science, machine learning or scientific calculations, we often need to perform calculations on numeric data types including  complex numbers. In this article, we will use the cmath module in python to perform operations on complex numbers using different methods provided in the module.

Calculate phase of a complex number

The phase of a complex number is defined as the angle between the real axis and the vector representing the complex number. Using the cmath module, we can find the phase of a complex number using the phase() method. The phase method takes a complex number as input and returns a floating point number representing the phase of the complex number as follows.

import cmath
myNum=3+2j
print("The complex number is:",myNum)
myPhase= cmath.phase(myNum)
print("Phase of  the complex number is:",myPhase)

Output:

The complex number is: (3+2j)
Phase of  the complex number is: 0.5880026035475675

Polar coordinates of a complex number

In polar coordinates, a complex number is defined as a tuple consisting of the modulus of the complex number as the first element and phase of the complex number as the second element. We can find the polar coordinates of a complex number using polar() method in python. The polar() method takes a complex number as input and returns a tuple representing the polar coordinates as follows.

import cmath
myNum=3+2j
print("The complex number is:",myNum)
myPol= cmath.polar(myNum)
print("Polar coordinates of  the complex number are:",myPol)

Output:

The complex number is: (3+2j)
Polar coordinates of  the complex number are: (3.605551275463989, 0.5880026035475675)

If we know the modulus and phase of a complex number i.e. If we know the polar coordinates of a complex number, we can obtain the  complex number using the rect() method. The rect()  method takes the modulus as the first argument and phase of the complex number as the second argument and returns the corresponding complex number as follows.

import cmath
myPol= (3.605551275463989, 0.5880026035475675)
print("Polar coordinates of  the complex number are:",myPol)
print("Modulus of the complex number is:",myPol[0])
print("Phase of the complex number is:",myPol[1])
myRec=cmath.rect(myPol[0],myPol[1])
print("Complex number in rectangular form is:",myRec)

Output:

Polar coordinates of  the complex number are: (3.605551275463989, 0.5880026035475675)
Modulus of the complex number is: 3.605551275463989
Phase of the complex number is: 0.5880026035475675
Complex number in rectangular form is: (3+1.9999999999999996j)

Constants in cmath module

The cmath module also provides certain mathematical constants like infinity, NaN and pi which are useful in mathematical calculations. Some of the constants are given in the following example.

import cmath
print("Given below are some of the constants defined in cmath module.")
print("e:",cmath.e)
print("Infinity (real axis):",cmath.inf)
print("Infinity (Imaginary axis):",cmath.infj)
print("NaN (real):",cmath.nan)
print("NaN (imaginary):",cmath.nanj)
print("Pi:",cmath.pi)
print("Tau:",cmath.tau)

Output:

Given below are some of the constants defined in cmath module.
e: 2.718281828459045
Infinity (real axis): inf
Infinity (Imaginary axis): infj
NaN (real): nan
NaN (imaginary): nanj
Pi: 3.141592653589793
Tau: 6.283185307179586

Trigonometric functions in cmath module

For mathematical calculations on complex numbers, the cmath module provides a set of trigonometric functions.All of the trigonometric functions take the complex number as input and also return a complex number which represents the corresponding output for the trigonometric functions. Examples are discussed below.

import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Sine of the complex number is:",cmath.sin(myNum))
print("Cosine of the complex number is:",cmath.cos(myNum))
print("Tangent of the complex number is:",cmath.tan(myNum))
print("Inverse Sine of the complex number is:",cmath.asin(myNum))
print("Inverse Cosine of the complex number is:",cmath.acos(myNum))
print("Inverse Tangent of the complex number is:",cmath.atan(myNum))

Output:

Complex number is: (3+2j)
Sine of the complex number is: (0.5309210862485197-3.59056458998578j)
Cosine of the complex number is: (-3.7245455049153224-0.5118225699873846j)
Tangent of the complex number is: (-0.009884375038322495+0.965385879022133j)
Inverse Sine of the complex number is: (0.9646585044076028+1.9686379257930964j)
Inverse Cosine of the complex number is: (0.6061378223872937-1.9686379257930964j)
Inverse Tangent of the complex number is: (1.3389725222944935+0.14694666622552977j)

Hyperbolic functions in cmath module

Just like trigonometric functions, cmath module also provides hyperbolic functions and inverse hyperbolic trigonometric functions for mathematical calculations in python. All these functions take a complex number as input and return a complex number representing the hyperbolic or inverse hyperbolic trigonometric output as per their nature. Examples are given below.

import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Hyperbolic Sine of the complex number is:",cmath.sinh(myNum))
print("Hyperbolic Cosine of the complex number is:",cmath.cosh(myNum))
print("Hyperbolic Tangent of the complex number is:",cmath.tanh(myNum))
print("Inverse Hyperbolic Sine of the complex number is:",cmath.asinh(myNum))
print("Inverse Hyperbolic Cosine of the complex number is:",cmath.acosh(myNum))
print("Inverse Hyperbolic Tangent of the complex number is:",cmath.atanh(myNum))

Output:

Complex number is: (3+2j)
Hyperbolic Sine of the complex number is: (-4.168906959966565+9.15449914691143j)
Hyperbolic Cosine of the complex number is: (-4.189625690968807+9.109227893755337j)
Hyperbolic Tangent of the complex number is: (1.00323862735361-0.003764025641504249j)
Inverse Hyperbolic Sine of the complex number is: (1.9833870299165355+0.5706527843210994j)
Inverse Hyperbolic Cosine of the complex number is: (1.9686379257930964+0.6061378223872937j)
Inverse Hyperbolic Tangent of the complex number is: (0.22907268296853878+1.4099210495965755j)

Logarithmic functions in cmath module

The cmath module provides two methods namely log() and log10() for logarithmic calculations on complex numbers. The log() function takes a complex number as first input and an optional argument representing the base of the logarithmic function. When we pass only the complex number as input to the log() function, it returns the natural log of the complex number with base “e”. When we also pass the second argument i.e. base to the log() function, it calculates the logarithm of the complex number with the provided base. This can be seen in the following example.

import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Natural log of the complex number is:",cmath.log(myNum))
print("Logarithm of the complex number with base 5 is:",cmath.log(myNum,5))

Output:

Complex number is: (3+2j)
Natural log of the complex number is: (1.2824746787307684+0.5880026035475675j)
Logarithm of the complex number with base 5 is: (0.7968463205835412+0.36534655919610926j)

The log10() method calculates the logarithm of a complex number with base 10 as follows.

import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Logarithm of the complex number with base 10 is:",cmath.log10(myNum))

Output:

Complex number is: (3+2j)
Logarithm of the complex number with base 10 is: (0.5569716761534184+0.255366286065454j)

Power functions in cmath module

The cmath module provides two power functions namely exp() and sqrt() for calculations in python. The exp() function takes a complex number as input and returns  a complex number representing the exponential value of the input. This can be seen in the following example.

import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Exponential of the complex number is:",cmath.exp(myNum)) 

Output:

Complex number is: (3+2j)
Exponential of the complex number is: (-8.358532650935372+18.263727040666765j)

The sqrt() function also takes a complex number as input and returns the complex number representing the square root of the input as follows.


import cmath
myNum=3+2j
print("Complex number is:",myNum)
print("Square root of the complex number is:",cmath.sqrt(myNum)) 

Output:

Complex number is: (3+2j)
Square root of the complex number is: (1.8173540210239707+0.5502505227003375j)

Conclusion

In this article, we have studied the functions and methods in the cmath module to perform mathematical operations on complex numbers in Python.We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way. Stay tuned for more informative articles.

The post Introduction to cmath module in Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

Stack Abuse: Graphs in Python: Minimum Spanning Trees - Prim's Algorithm

Introduction

Graphs are a great tool for modeling relationships between objects. Graphs can be used to model complex relationships between any number of objects in a system, which makes them ideal for representing interactions between concepts/items in areas like physics, economics, chemistry.

Thanks to constantly increasing computational power, graphs can be manipulated easier than ever. The study of data structures and algorithms has given us better insight into the use of graphs, thanks to algorithms created by great minds over the years. One of these algorithms is Prim’s Algorithm. It was designed by Vojtech Jarnik in 1930, then redesigned by Robert C. Prim in 1957. This algorithm is used to find the Minimum Spanning Tree in a graph.

In this guide, you'll learn how to implement Prim's algorithm in Python - we'll cover the steps used in the algorithm as well as implement it through a functional example. Before that, we'll dive into the definitions of trees, graphs, adjacency matrtices and minimum spanning trees.

What is a Graph?

Graphs are used to illustrate complex systems and the relationships that exist between points of data. They simplify the visualization of these relationships and their relative importance, and have countless applications, but we commonly study them in discrete mathematics, where they can lead to optimal solutions for a wide variety of problems.

You have probably seen graphs when working with Git repositories. Consulting the commits log of a project often reveals a hierarchical graph.

The vertices and the edges are the principal components of the graph’s fundamental structure. We usually represent the vertices as circles or dots, while the edges are lines or curves that connect the vertices, forming relationships. According to the purpose of the graph, the edges and the vertices can have weights, which are numeric values assigned to these elements.

Weights are an abstraction and can be defined by various measures. On a map (graph) of bus stops (nodes), the streets (edges) can have a traffic jam. The amount of time it takes to go through this jam is the weight of that edge.

The edges that connect two or more vertices are called paths. Here's an example:

graph illustration

If a graph is undirected (all edges are bidirectional) and acyclic (isn't cyclic, doesn't run in a circle), we call it a tree - the graph above is a tree. Let's take a closer look at this.

What is a Tree?

Trees are a type of graph, though not all graphs are trees. The main feature of a tree is that every pair of vertices is connected by one and only one path, so unlike other types of graphs there cannot be cycles in the graph - they're acyclic.

Additionally, trees are undirected - there are no strict directions between nodes that you have to follow.

This is a tree:

tree illustration

Although this might not look much like a tree that you're used to seeing in the park - a lot of trees actually do resemble them, having a hierarchy from the root node with many branches and leaves.

What is an Adjacency Matrix?

A graph’s relationships can be represented in many ways, and one of them is an Adjacency Matrix. This structure shows how the vertices in a graph are connected to other vertices. An adjacency matrix is a square matrix with dimensions n x n, where n is the number of vertices in the graph. Once the matrix structure is defined, its fields define which vertices have paths connected to other vertices.

For example, the vertex N1 is connected to the vertex N2, so you fill the field (N1, N2) in the matrix with the number one, showing that a relationship exists between them. If there is no relationship between the two vertices, fill the field in with a zero. If you're working with a graph with weights, you can fill the fields with the weights of the edges instead of the number one when there is a relationship.

This is how an Adjacency Matrix can look like:

adjacency matrix

Minimum Spanning Trees

With the terminology sorted out, we can take a look at Minimum Spanning Trees (MST). These are a common occurrence in the real world where we'd like to find, say, the "cheapest" coverage of a certain area. For example, a pizza restaurant chain might want to know where to open up their restaurants to cover the largest area of delivery, within their guaranteed time, with the minimum number of open restaurants.

The time it takes to traverse a street is its weight, so the goal is to create a tree within the city, that connects all of the nodes (delivery areas/houses), without any cycles (inefficient), and have the sum of all the weights be the lowest it can be.

The result is a Minimum Spanning Tree of their restaurants.

For every graph with weights, you can calculate a Minimum Spanning Tree. Each of these trees fulfills the following conditions:

  • Is a subgraph (this means the MST contains some or all the relationships from the original graph, no more).
  • Is a tree.
  • The MST weight (sum of weights) is the minimum weight possible of the different potential spanning trees in the graph.
  • The general graph must be undirected.

A Spanning Tree (Maximum or Minimum) connects all of the general graph's vertices, but not necessarily all the edges of the graph - some are avoided to reduce cost and even if not, using all the edges may turn the path into a cyclic one, which then defeats the purpose of a tree.

With this summary, we can get into Prim's Algorithm.

Prim's Algorithm Intuition

In 1957 Robert C. Prim designed (or rather, redesigned) a sequence of steps to find a graph's Minimum Spanning Tree using path weights.

The algorithm's steps are these:

  1. Select a random vertex.
  2. Choose the path with the minimum weight connected to the chosen vertex.
  3. The path will lead you to a new vertex, position yourself there.
  4. Once you have formed/updated the initial tree, choose the path with the minimum weight that is connected to the whole tree. You must avoid creating cycles.
  5. Repeat the steps 3 and 4 until you have covered all the vertices.

Let's get some more intuition for how this works with this gif:

prim's algorithm gif

Implementing Prim's Algorithm in Python

Creating the Adjacency Matrix

We'll use two adjacency matrices - one for the general graph and another for the MST, and we'll define them using list comprehensions. Since we are creating a matrix, we'll create one list full of other lists. Every row will be another list and there we'll store the weight values.

Note: Every column and row of the matrix represents a vertex.

def primsAlgorithm(vertices):

    # Creating the adjacency Matrix with n x n dimensions filled with zeros, where n is the graph number of vertices:
    adjacencyMatrix = [[0 for column in range(vertices)] for row in range(vertices)]

    # Creating another adjacency Matrix for the Minimum Spanning Tree:
    mstMatrix = [[0 for column in range(vertices)] for row in range(vertices)]

One useful attribute of an undirected graph's adjacency matrix is that it is symmetric. This means the upper half (above the graph's diagonal) is a mirror of the lower half. Knowing this means we don't have to fill the whole matrix because there will be repeated values. Taking advantage of this property leads us to this:

    # Filling the adjacency matrix:
    for i in range(0,vertices):
        # Since the adjacency matrix is Symmetric we don't have to fill the whole matrix, only the upper half:
        for j in range(0+i,vertices):
            # Asking for the edges weights:
            adjacencyMatrix[i][j] = int(input('Enter the path weight between the vertices: ({}, {}):  '.format(i,j)))

            # Again, we use the Symmetric Matrix as an advantage:
            adjacencyMatrix[j][i] = adjacencyMatrix[i][j]

We've filled in the general graph adjacency matrix. Let's get to filling in the matrix's values.

As we use Prim's algorithm, we are going to be comparing the weights and looking for the minimum one, so we should define a really large number that works as a temporary minimum. Python will help us with that task:

    # Defining a really big number:
    positiveInf = float('inf')

We also have to track the selected vertices to learn which vertices are included in the MST. Once all the vertices are part of the subgraph, we can stop looking. To achieve this, we are going to create another comprehension list with Boolean values. Every column in this new comprehension list will represent a vertex and if the vertex was chosen as part of the MST the field will be True, and False otherwise:

    # This is a list showing which vertices are already selected, so we don't pick the same vertex twice and we can actually know when stop looking
    selectedVertices = [False for vertex in range(vertices)]

Now, let's see how all of the elements we've covered so far come together.

Prim's Algorithm

In the code below, we'll be repeating the searching process (using a while loop) until all the fields in the selectedVertices list are made true. As we said, the positiveInf values will act as a temporary minimum since every weight we intact with will be less than that value. We'll define two variables: start and end. These variables will act as the vertices to be connected and we are going to use them to fill in our MST matrix.

After defining these variables, we have to create two loops, so that we can move through the adjacency matrix of the general graph (the first loop is for the x-axis or the rows, and the second loop for the y-axis or columns). Before entering the second loop, we have to validate that the vertex given by the first loop is selected, which ensures it's part of the MST graph. We handle this with the if selectedVertices[i]: block in the code below.

When we start building the tree, none of the vertices will be selected, they are all False, so the loop will end before we even enter the second loop. For this reason, the variables start and end are initially defined as 0, and when we exit from the loop the Boolean value assigned to the end position will become True. As a result, one field of the mstMatrix will be filled with the existing minimum, and since the mstMatrix is symmetrical we can use the same trick on the adjacency matrix to fill in another field.

Now that we have a selected vertex, which is Step 1of Prim's Algorithm, let's discuss the second loop.

First, we'll use the symmetric matrix trick to move through the y-axis and examine the relationships between our selected vertex and other vertices. Our selected vertex will be compared with the other n vertices according to the following parameters:

  • The vertex given by i must have a path that connects it with vertex j (this means the weight in the (i,j) position of the adjacency matrix must be greater than zero).
  • The vertex j must be not selected (if it's already selected this can lead to a cycle, which is something we don't want since we're trying to construct a tree).

Given these two conditions, we can compare the edge weight of a given relationship with the general minimum of the MST. If the weight is less than the minimum, then it will become the new minimum and the variables start and end will receive the i and j values. If the weight is more than the minimum, then we keep searching through the remaining columns.

The start and end will populate the MST matrix, creating the tree we are looking for. We then have to repeat the process until all the vertices are selected.

Let's take a look at the code:

# While there are vertices that are not included in the MST, keep looking:
    while(False in selectedVertices):
        # We use the big number we created before as the possible minimum weight
        minimum = positiveInf

        # The starting vertex
        start = 0

        # The ending vertex
        end = 0

        for i in range(0,vertices):
            # If the vertex is part of the MST, look its relationships
            if selectedVertices[i]:
                # Again, we use the Symmetric Matrix as an advantage:
                for j in range(0+i,vertices):
                    # If the vertex analyzed have a path to the ending vertex AND its not included in the MST to avoid cycles)
                    if (not selectedVertices[j] and adjacencyMatrix[i][j]>0):  
                        # If the weight path analyzed is less than the minimum of the MST
                        if adjacencyMatrix[i][j] < minimum:
                            # Defines the new minimum weight, the starting vertex and the ending vertex
                            minimum = adjacencyMatrix[i][j]
                            start, end = i, j
        
        # Since we added the ending vertex to the MST, it's already selected:
        selectedVertices[end] = True

        # Filling the MST Adjacency Matrix fields:
        mstMatrix[start][end] = minimum
        
        # Initially, the minimum will be Inf if the first vertex is not connected with itself, but really it must be 0:
        if minimum == positiveInf:
            mstMatrix[start][end] = 0
            
        # Symmetric matrix, remember
        mstMatrix[end][start] = mstMatrix[start][end]

    # Show off:
    print(mstMatrix)
    
    # Here the function ends

Now, we just have to call the function we created. The line below prompts the user to enter a chosen number of vertices and then fills the adjacency matrix:

#Call to the function:
primsAlgorithm(int(input('Enter the vertices number: ')))

Let's test the code by running it, which will create our number of chosen vertices and fill in the graph adjacency matrix:

running prim's algorithm

We have 5 vertices and the edges weights, our input will look like this:

Enter the vertices number: 5
Enter the path weight between the vertices: (0, 0):  0
Enter the path weight between the vertices: (0, 1):  15
Enter the path weight between the vertices: (0, 2):  0
Enter the path weight between the vertices: (0, 3):  0
Enter the path weight between the vertices: (0, 4):  0
Enter the path weight between the vertices: (1, 1):  0
Enter the path weight between the vertices: (1, 2):  7
Enter the path weight between the vertices: (1, 3):  10
Enter the path weight between the vertices: (1, 4):  0
Enter the path weight between the vertices: (2, 2):  0
Enter the path weight between the vertices: (2, 3):  30
Enter the path weight between the vertices: (2, 4):  3
Enter the path weight between the vertices: (3, 3):  0
Enter the path weight between the vertices: (3, 4):  0
Enter the path weight between the vertices: (4, 4):  0

And for the output we got:

[[0, 15, 0, 0, 0], [15, 0, 7, 10, 0], [0, 7, 0, 0, 3], [0, 10, 0, 0, 0], [0, 0, 3, 0, 0]]

That's our adjacency matrix for the Minimum Spanning Tree. Producing the graph will give us this:

prim's algorithm output

And that's it! That's our MST for that graph. Remember that we can start with a random vertex, we don't necessarily need to start with the first one. If you want to challenge yourself, you can modify the code so that it takes a random number (in the correct range of course) as the starting vertex.

Conclusion

Prim's algorithm is not only efficient, but flexible when it comes to finding the Minimum Spanning Tree of a graph. The Python implementation is also really simple. MSTs are useful structures that can be applied in a wide variety of fields, making Prim's algorithm an incredibly important one.

You can access the source code from the guide on GitHub



from Planet Python
via read more

Tuesday, June 29, 2021

Podcast.__init__: Lightening The Load For Deep Learning With Sparse Networks Using Neural Magic

Deep learning has largely taken over the research and applications of artificial intelligence, with some truly impressive results. The challenge that it presents is that for reasonable speed and performance it requires specialized hardware, generally in the form of a dedicated GPU (Graphics Processing Unit). This raises the cost of the infrastructure, adds deployment complexity, and drastically increases the energy requirements for training and serving of models. To address these challenges Nir Shavit combined his experiences in multi-core computing and brain science to co-found Neural Magic where he is leading the efforts to build a set of tools that prune dense neural networks to allow them to execute on commodity CPU hardware. In this episode he explains how sparsification of deep learning models works, the potential that it unlocks for making machine learning and specialized AI more accessible, and how you can start using it today.

Summary

Deep learning has largely taken over the research and applications of artificial intelligence, with some truly impressive results. The challenge that it presents is that for reasonable speed and performance it requires specialized hardware, generally in the form of a dedicated GPU (Graphics Processing Unit). This raises the cost of the infrastructure, adds deployment complexity, and drastically increases the energy requirements for training and serving of models. To address these challenges Nir Shavit combined his experiences in multi-core computing and brain science to co-found Neural Magic where he is leading the efforts to build a set of tools that prune dense neural networks to allow them to execute on commodity CPU hardware. In this episode he explains how sparsification of deep learning models works, the potential that it unlocks for making machine learning and specialized AI more accessible, and how you can start using it today.

Announcements

  • Hello and welcome to Podcast.__init__, the podcast about Python’s role in data and science.
  • 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 the launch of their managed Kubernetes platform it’s easy to get started with the next generation of deployment and scaling, powered by the battle tested Linode platform, including simple pricing, node balancers, 40Gbit networking, dedicated CPU and GPU instances, and worldwide data centers. Go to pythonpodcast.com/linode and get a $100 credit to try out a Kubernetes cluster of your own. And don’t forget to thank them for their continued support of this show!
  • Your host as usual is Tobias Macey and today I’m interviewing Nir Shavit about Neural Magic and the benefits of using sparsification techniques for deep learning models

Interview

  • Introductions
  • How did you get introduced to Python?
  • Can you describe what Neural Magic is and the story behind it?
  • What are the attributes of deep learning architectures that influence the bias toward GPU hardware for training them?
    • What are the mathematical aspects of neural networks that have biased the current generation of software tools toward that architectural style?
  • How does sparsifying a network architecture allow for improved performance on commodity CPU architectures?
  • What is involved in converting a dense neural network into a sparse network?
  • Can you describe the components of the Neural Magic architecture and how they are used together to reduce the footprint of deep learning architectures and accelerate their performance on CPUs?
    • What are some of the goals or design approaches that have changed or evolved since you first began working on the Neural Magic platform?
  • For someone who has an existing model defined, what is the process to convert it to run with the DeepSparse engine?
  • What are some of the options for applications of deep learning that are unlocked by enabling the models to train and run without GPU or other specialized hardware?
  • The current set of components for Neural Magic is either open source or free to use. What is your long-term business model, and how are you approaching governance of the open source projects?
  • What are the most interesting, innovative, or unexpected ways that you have seen Neural Magic and model sparsification used?
  • What are the most interesting, unexpected, or challenging lessons that you have learned while working on Neural Magic?
  • When is Neural Magic or sparse networks the wrong choice?
  • What do you have planned for the future of Neural Magic?

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



from Planet Python
via read more

Development containers in VS Code: a quick start guide

If you’re building production ML systems, dev containers are the killer feature of VS Code. Dev containers give you full VS Code functionality inside a Docker container. This lets you unify your dev and production environments if production is a Docker container. But even if you’re not targeting a Docker ... Read More

The post Development containers in VS Code: a quick start guide appeared first on Sparrow Computing.



from Planet SciPy
read more

Ben Cook: Development containers in VS Code: a quick start guide

If you’re building production ML systems, dev containers are the killer feature of VS Code. Dev containers give you full VS Code functionality inside a Docker container. This lets you unify your dev and production environments if production is a Docker container. But even if you’re not targeting a Docker deployment, running your code in a container prevents you from fighting with local Python environments across development machines: if you configure your dev container correctly, it will work exactly the same for every member of your team.

In this post, I’ll give you a quick look at how development containers work in VS Code so you can try them for yourself. There are other ways to go about this, but for a basic workflow, I recommend a few things:

  • A Dockerfile in the root folder to define the development container
  • A configuration file at .devcontainer/devcontainer.json
  • Whatever project files you need

This means, for a new project, the folder structure will look something like the following:

.
├── .devcontainer
│   └── devcontainer.json
├── Dockerfile
├── LICENSE
├── README.md
├── main.py
├── requirements-dev.txt
└── requirements.txt

The Dockerfile can be very flexible. Typically you’ll want to install dependencies unless your Docker image already has them installed. Here’s a very basic one you can use to start:

FROM python:3.9

COPY requirements.txt .
RUN pip install requirements.txt

The devcontainer.json file is also highly customizable, but a few things are particularly important:

  • The build definition
  • Workspace settings for VS Code (things like the path to the Python interpreter, what linter to use, etc)
  • Extensions to use
  • Additional commands to run inside the container

Here’s what a basic devcontainer.json looks like:

{
  "name": "Python 3",
  "build": {
    "dockerfile": "../Dockerfile",
  },
  // Set *default* container specific settings.json values on container create.
  "settings": {
    "python.pythonPath": "/usr/local/bin/python",
    "python.languageServer": "Pylance",
    "python.linting.enabled": true,
    "python.formatting.blackPath": "/usr/local/bin/black",
    "python.formatting.provider": "black",
    "editor.formatOnSave": true,
  },
  // Add the IDs of extensions you want installed when the container is created.
  "extensions": [
    "ms-python.python",
    "ms-python.vscode-pylance"
  ],
  "postCreateCommand": "pip install -r requirements-dev.txt"
}

Once you have these files setup in your VS Code session, you can open the project in a container. Open the Command Palette with F1 and run Remote-Containers: Open Folder in Container... From there, you can open a terminal and run your code.

Notice if you modify print("hello world") to print('hello world') (with a single quotation) Black will automatically change it for you on save. Nifty!

One other note: the community seems to be switching to Poetry for dependency management in Python and I think this is a good trend. Version conflicts are a huge pain in the regular pip toolchain. I will save that for another post, but just know that you can swap out the pip requirements files for Poetry.

If you want to play around with this, feel free to clone my git repo.

The post Development containers in VS Code: a quick start guide appeared first on Sparrow Computing.



from Planet Python
via read more

PyCoder’s Weekly: Issue #479 (June 29, 2021)

#479 – JUNE 29, 2021
View in Browser »

The PyCoder’s Weekly Logo


Subclassing in Python Redux

Have you ever heard someone say that composition is better than inheritance? It’s an age-old conflict in object-oriented programming that’s been the impetus for numerous online debates. Author Hynek Schlawack, who describes himself as “firmly in the composition-over-inheritance camp” explains in this article how “Python is designed in a way that you can’t write idiomatic code without subclassing sometimes.”
HYNEK SCHLAWACK

Correlation Analysis 101 in Python

Correlation analysis is a useful part of exploratory data analysis. It can help you identify potential relationships between various features of your data. In this helpful guide, you’ll learn how to do correlation analysis in a pandas DataFrame. You’ll see how to display a correlation matrix as a heatmap and explore some guidelines for identifying when correlation might imply causation.
OLGA BEREZOVSKY • Shared by Olga Berezovsky

Learn How to Scale Python with Dask

alt

If you’ve taken your data skills from zero to one with PyData (e.g. Pandas, Scikit-Learn) then this two-day, interactive class will help you use larger data sets that won’t fit in memory and will help you distribute your workloads to accelerate your code with Dask. Limited seats. Register now →
COILED COMPUTING, INC sponsor

Reverse Python Lists: Beyond .reverse() and reversed()

In this step-by-step tutorial, you’ll learn about Python’s tools and techniques to work with lists in reverse order. You’ll also learn how to reverse your list by hand.
REAL PYTHON

CPython Internals: Paperback Now Available!

With CPython Internals: Your Guide to the Python 3 Interpreter, you’ll unlock the inner workings of the Python language, learn how to compile the Python interpreter from source code, and cover what you’ll need to know to confidently start contributing to CPython yourself.
REAL PYTHON

Discussions

Tkinter… Not Bad

Tkinter. Sometimes it seems like you either love it or hate it. While Tkinter doesn’t check all the boxes for every GUI, it definitely has its use cases. And the fact that it comes built into the Python standard library is a huge plus. This Reddit thread spreads some much-needed love for the library.
REDDIT

Python Jobs

Backend Software Engineer (Remote)

Tessian

Python Web Developer (Los Angeles, CA, USA)

Premiere Digital Services

Senior Software Engineer (Remote)

Truveris

Senior Software Engineer (Washington D.C., DC, USA)

Truth Initiative

More Python Jobs >>>

Articles & Tutorials

Built-in Permission Classes in Django Rest Framework

Django REST Framework (DRF) is a full-featured framework built on top of Django for creating REST APIs. This article covers built-in permissions classes in DRF that you can use to restrict permissions on an API view. You’ll learn how to use each of the seven built-in classes, as well as how to restrict permissions globally or per object.
Ε PELA GIACOMELLI • Shared by GirlLovesToCode

A From-Scratch Tour of Bitcoin in Python

This in-depth article from Tesla’s head of AI shows you how to create, digitally sign, and broadcast a Bitcoin transaction in pure Python, from scratch, and with zero dependencies. Along the way, you’ll learn quite a bit about how Bitcoin works under the hood.
ANDREJ KARPATHY

One API to Manage Transactional Messages From Your Python App: Email, SMS, Push, In-App, Slack

alt

Trigger event-based messages from your Python app in just a few lines of code. One API with endpoints for white-label branding, sophisticated automation rules, and an in-app inbox. Product can design messages with our drag and drop editor. 10k notifications free /mo with Courier →
COURIER sponsor

Practicing Python With CSV Files and Extracting Values With filter()

Are you ready to practice your Python skills some more? There is a new set of practice problems prepared for you to tackle, and this time they’re based on working with CSV files. This week on the Real Python Podcast, David Amos is back, and he’s brought another batch of PyCoder’s Weekly articles and projects.
REAL PYTHON podcast

Measuring Memory Usage in Python: It’s Tricky!

Measuring memory usage in Python is tricky. In this article, you’ll learn a simplified yet informative model for how memory works. You’ll explore two memory-resident and allocated memory and how to measure them with Python. Finally, you’ll explore the tradeoffs between the two methods for measuring memory.
ITAMAR TURNER-TRAURING

Functools: The Power of Higher-Order Functions in Python

The functools module is one of the “hidden gems” of the Python standard library. This article takes you on a tour of everything in functools. You’ll learn how to implement caching, function overloading, and a whole lot more.
MARTIN HEINZ • Shared by Martin Heinz

Python Inner Functions

In this step-by-step course, you’ll learn what inner functions are in Python, how to define them, and what their main use cases are.
REAL PYTHON course

Projects & Code

Events

PyHEP 2021

July 5 – 10, 2021
CERN.CH

EuroPython 2021 (Virtual)

July 26 – August 1, 2021
EUROPYTHON.EU

PyCon India 2021 (Virtual)

September 17 – 20, 2021
PYCON.ORG


Happy Pythoning!
This was PyCoder’s Weekly Issue #479.
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 ]



from Planet Python
via read more

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...