Talk Python to Me: #248 Climate change and your Python code
Codementor: Python 3 Functions - Learn Python Programming Tutorial
Tryton News: Release 1.1.0 of python-sql
@ced wrote:
We are proud to announce the release of the version 1.1.0 of python-sql.
python-sql is a library to write SQL queries in a pythonic way. It is mainly developed for Tryton but it has no external dependencies and is agnostic to any framework or SQL database.
In addition to bug-fixes, this release contains the following improvements:
- Add ORDER BY clause to aggregate functions
- Add support for Python 3.8
- Add distinct on Select
python-sql is available on PyPI: https://pypi.org/project/python-sql/1.1.0/
Posts: 1
Participants: 1
Python Insider: Python 3.9.0a3 now available for testing
This is an early developer preview of Python 3.9
Python 3.9 is still in development. This releasee, 3.9.0a3 is the third of six planned alpha releases. Alpha releases are intended to make it easier to test the current state of new features and bug fixes and to test the release process. During the alpha phase, features may be added up until the start of the beta phase (2020-05-18) and, if necessary, may be modified or deleted up until the release candidate phase (2020-08-10). Please keep in mind that this is a preview release and its use is not recommended for production environments.Major new features of the 3.9 series, compared to 3.8
Many new features for Python 3.9 are still being planned and written. Among the new major new features and changes so far:- PEP 602, Python adopts a stable annual release cadence
- BPO 38379, garbage collection does not block on resurrected objects;
- BPO 38692, os.pidfd_open added that allows process management without races and signals;
- A number of standard library modules (audioop, ast, grp, _hashlib, pwd, _posixsubprocess, random, select, struct, termios, zlib) are now using the stable ABI defined by PEP 384.
- (Hey, fellow core developer, if a feature you find important is missing from this list, let Łukasz know.)
The next pre-release of Python 3.9 will be 3.9.0a4, currently scheduled for 2020-02-17.
Go Deh: Sharing another way?
Tickled!
Sharing came up in something I was reading that got me revisiting the Wikipedia page on the Thue Morse Sequence. Tucked away to the right is a static image of ones and zeroes with the caption:"When counting in binary, the digit sum modulo 2 is the Thue-Morse sequence"Now I had already blogged about the Thue-Morse sequence with respect to sharing fairly:
If you had two people, Asia and Bain chosing from different cash bounties then if Asia went first she'd choose the largest cash bounty, then Bain would chose the next larget, ... if they alternate like this then Asia, who went first, would always get more than Bain; furthermore, Asias total accumulated bounty would increase over Bains over time like this:
In my earlier blog entry: Sharing Fairly, I looked into the Thue-Morse sequence for who gets to pick next and showed that using that sequence, and itsextension for more than two people sharing, the person with the most accumulated wealth switches over time and does not diverge like in the simple taking of turns shown above.
I showed how using a Thue-Morse sequence for multiple people sharing gave a "fairer" distribution of wealth, for example this graph of accumulated wealth for three people:
Old Code
My code to generate the Thue Morse sequence for sharing between multiple people was this function:defthue_morse(persons='ABC', mx=20, tutor=True)
Its algorithm works, and was straight forward to describe as a natural extension of the two-person case, but could generate more terms than requested that are then trimmed.
Back to the tickle
"When counting in binary, the digit sum modulo 2 is the Thue-Morse sequence"Now that method might be coded to only give the number of terms required. I might code it as a generator. But as it stands it applies only to two people (binary).
I thought that an equivalent statement for many people might be:
"When counting base b, the digit sum modulo b is the Thue-Morse sequence of fairer sharing between b people"I set out to code it.
Code Journey
"When counting base b" ... "digit sum" ...I am going to need to express an integer in different bases, and sum the digits.
Base changing
Normally when expressing an integer in a different base, b, it is for printout and the routine returns characters: The columns in the string denote, by position, the multiples of successive powers ob b.For example:
"123" base 10 == 3 * 10**0 + 2 * 10**1 + 1 * 10**2
If the base goes beyond 10 such as in hexadecimal, then letters A... are used to represent numbers beyond 9 as single characters.
We don't want that!
We need to sum the digits so in the _basechange_int function a list is returned of integers to make for easier summation later.
Integer 123 base 10 would return the list [1, 2, 3]
People representation
I am used to generating the Thue-Morse sequence as a string ofupper-case characters where each individual characters corresponds to one persons turn in sharing. as in my original, I will code my new function to have a persons parameter which is a string of different characters, each representing a person.The number of persons becomes the base, b, of my altered statement above.
The algorithm will generate an int that will then need to be represented as a corresponding character from the persons string.
Generate
I decided to code the algorithm as a generator of successive terms from the given persons. islice is my friend :-)Thue-Morse sequence generator from digit counts
1 | # -*- coding: utf-8 -*- |
Example runs:
In [24]: b = 3; print(b, ''.join(islice(fairshare(PERSONS[:b]), 44)))
3 ABCBCACABBCACABABCCABABCBCABCACABABCCABABCBC
In [25]: b = 2; print(b, ''.join(islice(fairshare(PERSONS[:b]), 44)))
2 ABBABAABBAABABBABAABABBAABBABAABBAABABBAABBA
In [26]:
Checks
I can't do the maths to formally proove it, but I have compared outputs between the old and the new and they generate the same sequences.Weekly Python StackOverflow Report: (ccxii) stackoverflow python report
Between brackets: [question score / answers count]
Build date: 2020-01-25 14:48:31 GMT
- Read-the-docs build fails with "cannot import name 'PackageFinder' from 'pip._internal.index'" - [15/1]
- Sort list from index to index with Python? - [11/5]
- How to convert pandas dataframe to hierarchical dictionary - [11/3]
- pandas overwrite values in multiple columns at once based on condition of values in one column - [10/3]
- Unmelt only part of a column from pandas dataframe - [6/3]
- keep duplicates by key in a list of dictionaries - [6/2]
- Why define create_foo() in a Django models.Manager instead of overriding create()? - [6/1]
- Counting mode occurrences for all columns in a dataframe - [5/4]
- Select columns in a DataFrame conditional on row - [5/2]
- Identify leading and trailing NAs in pandas DataFrame - [5/1]
Tryton News: Release of Relatorio 0.9.1
@ced wrote:
We are glad to announce the release of Relatorio version 0.9.1.
Relatorio is a templating library mainly for OpenDocument using also OpenDocument as source format.
This is a bug-fix release which:
- Do not guess type of cell if directive is not alone
- Add support for Python 3.8
- Support file-magic as fallback to python-magic
The package is available at https://pypi.org/project/relatorio/0.9.1/
The documentation is available at https://relatorio.readthedocs.io/en/0.9.1/
Posts: 1
Participants: 1
Will McGugan: Prettier logging with Rich
If you are a Python developer you may spend a large part of your day reading log output that looks like this:
Front end tools like the Chrome developer console have far superior rendering for logs, but in the back-end we're stuck with an ancient technology that is older than most of the readers of this blog. I'm talking of course of the terminal which is likely to be the primary interface to back-end development until I retire.
Still I think we can do a little better. I wanted to use my terminal rendering library, Rich to render log output that is easier on the eye. The results are promising:
There are a few things going on here. Important fields are rendered in their own column to make it easier to scan. To reduce visual clutter, the time field is only rendered if it changes and I've set the date format to time only, which is fine for local development (if you forget what day it is you need a vacation). The message column has some syntax highlighting applied to it, tuned for web development, but more importantly it is word-wrapped. Finally there is a column for the python file and line that called the log method.
This would be my ideal logging for web-development, your mileage may vary and you may want to tune it for your domain.
You can try this yourself by configuring logging to use rich.logging.RichHandler
.
Rich is still in active development and not ready for a 1.0 release. If you want to use it anger, best pin the current version for now.
Made With Mu: Announcing Mu version 1.0.3
The latest stable version of Mu is here!
Version 1.0.3 is a bug-fix release with no new features.
We didn’t intend to cut this release but changes in the way the latest OSX works meant that code highlighting didn’t work correctly. We also managed to apply a fix to an annoying bug relating to where Mu set the current working directory for scripts run in Python3 mode.
OSX Catalina has posed a number of problems, from the incorrect rendering mentioned above, to the way the application should be installed and problems with permissions when flashing a BBC micro:bit.
The simple answer to the installation story is, once you’ve installed Mu in
your Applications
folder, you should first open it with CTRL-click (not a
double click) and select the “Open” button in the resulting pop-up. Subsequent
runs of Mu can be started in the usual “double click” way. If you don’t do the
“CTRL-click” trick you’ll see a pop-up complaining about Mu not being checked
for malicious software.
Because OSX Catalina also interferes with disk access permissions that relate to the BBC micro:bit, then flashing won’t work unless you run Mu as an administrator with the following commands in the terminal (you may also need to supply your password too):
$ cd /Applications
$ sudo open mu-editor.app
If you spot any bugs, please don’t hesitate to let us know.
Once again, many thanks to all the people in our community who have made such important and helpful contributions: Tim Golden for the bug fix for setting the current working directory and the many folks who reported and helped triage and fix the OSX related problems.
Please accept our apologies for not releasing a fix sooner. In our defence, Mu is a volunteer led project and sometimes it takes a while to find the time and space to triage problems, updates things, cut a release and announce it.
Finally, if you find Mu useful or would like to support the goals of this project, you might want to think about sponsoring the creator and lead maintainer Nicholas Tollervey.
Podcast.__init__: Simplifying Social Login For Your Web Applications
Summary
A standard feature in most modern web applications is the ability to log in or register using accounts that you already own on other sites such as Google, Facebook, or Twitter. Building your own integrations for each service can be complex and time consuming, distracting you from the features that you and your users actually care about. Fortunately the Python social auth library makes it easy to support third party authentication with a large and growing number of services with minimal effort. In this episode Matías Aguirre discusses his motivation for creating the library, how he has designed it to allow for flexibility and ease of use, and the benefits of delegating identity and authentication to third parties rather than managing passwords yourself.
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, Corinium Global Intelligence, ODSC, and Data Council. Upcoming events include the Software Architecture Conference in NYC, Strata Data in San Jose, and PyCon US in Pittsburgh. 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 Matías Aguirre about Python social auth and the complexities of third-party authentication
Interview
- Introductions
- How did you get introduced to Python?
- Can you start by describing what the Python social auth project is and your motivation for starting it?
- Why might someone want to integrate with or rely on a third-party identity provider in their projects?
- What are some of the tradeoffs or drawbacks of implementing
- Can you describe the current architecture of the library and how it has evolved since you first began working on it?
- There are a number of pre-built integrations with different web frameworks in the social auth github organization, but Django is the only one that has seen any commits recently. What are the contributing factors for that state of affairs?
- There are a number of authentication protocols that you support. What are the common capabilities that they each support and what are some of the more challenging differences between them?
- How have you implemented the interface for plugging different authentication mechanisms to allow for the variation between them while keeping the library code maintainable?
- What is involved in adding support for a new authentication provider or protocol?
- Many times authorization and authentication are conflated or used interchangeably. How does Python social auth address those concerns and what are the limitations of different mechanisms for defining permissions?
- For someone who is using Python social auth, what is the workflow for integrating it with their application as a consumer?
- What are some of the most interesting/unexpected/innovative ways that you have seen Python social auth used?
- What are some of the most interesting/useful/unexpected lessons that you have learned in the process of building and maintaining Python social auth?
- When is Python social auth more effort than it’s worth?
- What do you have planned for the future of the project?
Keep In Touch
- omab on GitHub
- Website
- @linuxaddict on Twitter
Picks
- Tobias
- Joker movie
- Matías
- Sanic asynchronous web framework
- Star Trek Picard TV series
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
- Python Social Auth
- Uruguay
- Django
- Ruby on Rails
- MonkeyLearn
- Social Authentication
- Django Social Auth
- Salted and hashed passwords
- Magic Link Authentication
- OAuth
- OpenID
- SAML
- FastAPI
- Sanic
- ASGI
- WSGI
- AsyncIO
The intro and outro music is from Requiem for a Fish The Freak Fandango Orchestra / CC BY-SA
Mike Driscoll: PyDev of the Week: Thomas Wouters
This week we welcome Thomas Wouters (@Yhg1s) as our PyDev of the Week! Thomas is a core developer of the Python language. He is very active in open source in general and has been a director of the Python Software Foundation in the past. Let’s spend some time getting to know him better!
Can you tell us a little about yourself (hobbies, education, etc):
Part of why I kept programming is that I found out how much fun it was to work on CPython itself. I had worked on a number of different C code bases at the time, and CPython’s was the cleanest, most readable, most enjoyable by far. I learned a lot from just reading it, and implementing small features that people asked for. I took a proof-of-concept patch from Michael Hudson to add augmented assignment (+=, *=, etc) and ran with it, getting guidance from Guido himself on a lot of the details. It took a lot longer than I expected, but that ended up becoming PEP 204, and made me a core Python developer. I was just in time to help found the Python Software Foundation as well, which we did in 2003, and I was on its Board of Directors the first three years (and again later).
My involvement with Python also meant I got offered a job at Google, working remotely from Amsterdam, to help maintain Python internally. The rest of my team is in California, and I get to visit them regularly. The work at Google is complex and diverse and challenging enough that after 13 years I’m still not bored.
I know C intimately, and C++ and Java fairly well. I use all three (along with Python) at my day job. I’m also somewhat familiar with Haskell, D, Objective C, and Perl. I used to use Perl a lot at my previous job, but I never enjoyed it and I don’t remember much of it now. My favourite language by far is Python, but C is in a firm second place. I’m familiar enough with its pitfalls that I know when I don’t know something, and where to look it up. I’m also under no illusions about its drawbacks, and would be quite happy if everybody moved to more memory-safe languages. Modern C++ — at least the set of features we’re encouraged to use at work — is also growing on me. The main issue I have with C++ is that it has so many features you *shouldn’t* use. At work we have a lot of tooling to help us make those choices, which greatly improves the C++ experience.What projects are you working on now?
Most of my projects are internal to Google, where we have a somewhat unusual environment. My main project is similar to Gregory Szorc’s PyOxidizer, although a couple years older and tightly coupled with Google’s build and deployment infrastructure. The open-source work I’m most excited about is my experimentation with replacing CPython’s refcounting with a traditional mark-and-sweep GC. I haven’t had much time in the last year to write the actual code for it, but I keep thinking it through, and I think it’s a realistic way forward for CPython. It would allow Larry Hasting’s Gilectomy to happen without too much extra overhead, and while it’ll mean radically changing the CPython C API, I think we can do that gradually in a good way. It’ll probably take ten years before it’s all done, but it’ll be worth it.
Which Python libraries are your favorite (core or 3rd party)?
I’ll have to shamelessly plug Google’s pytype here. Like mypy, it’s a PEP-484 type checker, but it actually infers types. You don’t have to annotate any of your code for it to be useful, and it can generate type annotations for your code (so that you can then also check the code with mypy, for example). I’m not directly involved with its development, but it’s one of the products of my team at Google and I had front-row seats at all stages of it. The people working on it are awesome — everyone in my team is — and pytype has caught lots of runtime errors during static analysis at Google. Getting the benefits of PEP-484 type annotations without having to figure out all the annotations is a huge win.
As a core developer of Python, what parts of the language do you maintain?
The post PyDev of the Week: Thomas Wouters appeared first on The Mouse Vs. The Python.
Mike Driscoll: PyDev of the Week: Thomas Wouters
This week we welcome Thomas Wouters (@Yhg1s) as our PyDev of the Week! Thomas is a core developer of the Python language. He is very active in open source in general and has been a director of the Python Software Foundation in the past. Let’s spend some time getting to know him better!
Can you tell us a little about yourself (hobbies, education, etc):
Part of why I kept programming is that I found out how much fun it was to work on CPython itself. I had worked on a number of different C code bases at the time, and CPython’s was the cleanest, most readable, most enjoyable by far. I learned a lot from just reading it, and implementing small features that people asked for. I took a proof-of-concept patch from Michael Hudson to add augmented assignment (+=, *=, etc) and ran with it, getting guidance from Guido himself on a lot of the details. It took a lot longer than I expected, but that ended up becoming PEP 204, and made me a core Python developer. I was just in time to help found the Python Software Foundation as well, which we did in 2003, and I was on its Board of Directors the first three years (and again later).
My involvement with Python also meant I got offered a job at Google, working remotely from Amsterdam, to help maintain Python internally. The rest of my team is in California, and I get to visit them regularly. The work at Google is complex and diverse and challenging enough that after 13 years I’m still not bored.
I know C intimately, and C++ and Java fairly well. I use all three (along with Python) at my day job. I’m also somewhat familiar with Haskell, D, Objective C, and Perl. I used to use Perl a lot at my previous job, but I never enjoyed it and I don’t remember much of it now. My favourite language by far is Python, but C is in a firm second place. I’m familiar enough with its pitfalls that I know when I don’t know something, and where to look it up. I’m also under no illusions about its drawbacks, and would be quite happy if everybody moved to more memory-safe languages. Modern C++ — at least the set of features we’re encouraged to use at work — is also growing on me. The main issue I have with C++ is that it has so many features you *shouldn’t* use. At work we have a lot of tooling to help us make those choices, which greatly improves the C++ experience.What projects are you working on now?
Most of my projects are internal to Google, where we have a somewhat unusual environment. My main project is similar to Gregory Szorc’s PyOxidizer, although a couple years older and tightly coupled with Google’s build and deployment infrastructure. The open-source work I’m most excited about is my experimentation with replacing CPython’s refcounting with a traditional mark-and-sweep GC. I haven’t had much time in the last year to write the actual code for it, but I keep thinking it through, and I think it’s a realistic way forward for CPython. It would allow Larry Hasting’s Gilectomy to happen without too much extra overhead, and while it’ll mean radically changing the CPython C API, I think we can do that gradually in a good way. It’ll probably take ten years before it’s all done, but it’ll be worth it.
Which Python libraries are your favorite (core or 3rd party)?
I’ll have to shamelessly plug Google’s pytype here. Like mypy, it’s a PEP-484 type checker, but it actually infers types. You don’t have to annotate any of your code for it to be useful, and it can generate type annotations for your code (so that you can then also check the code with mypy, for example). I’m not directly involved with its development, but it’s one of the products of my team at Google and I had front-row seats at all stages of it. The people working on it are awesome — everyone in my team is — and pytype has caught lots of runtime errors during static analysis at Google. Getting the benefits of PEP-484 type annotations without having to figure out all the annotations is a huge win.
As a core developer of Python, what parts of the language do you maintain?
The post PyDev of the Week: Thomas Wouters appeared first on The Mouse Vs. The Python.
Mike Driscoll: PyDev of the Week: Thomas Wouters
This week we welcome Thomas Wouters (@Yhg1s) as our PyDev of the Week! Thomas is a core developer of the Python language. He is very active in open source in general and has been a director of the Python Software Foundation in the past. Let’s spend some time getting to know him better!
Can you tell us a little about yourself (hobbies, education, etc):
Part of why I kept programming is that I found out how much fun it was to work on CPython itself. I had worked on a number of different C code bases at the time, and CPython’s was the cleanest, most readable, most enjoyable by far. I learned a lot from just reading it, and implementing small features that people asked for. I took a proof-of-concept patch from Michael Hudson to add augmented assignment (+=, *=, etc) and ran with it, getting guidance from Guido himself on a lot of the details. It took a lot longer than I expected, but that ended up becoming PEP 204, and made me a core Python developer. I was just in time to help found the Python Software Foundation as well, which we did in 2003, and I was on its Board of Directors the first three years (and again later).
My involvement with Python also meant I got offered a job at Google, working remotely from Amsterdam, to help maintain Python internally. The rest of my team is in California, and I get to visit them regularly. The work at Google is complex and diverse and challenging enough that after 13 years I’m still not bored.
I know C intimately, and C++ and Java fairly well. I use all three (along with Python) at my day job. I’m also somewhat familiar with Haskell, D, Objective C, and Perl. I used to use Perl a lot at my previous job, but I never enjoyed it and I don’t remember much of it now. My favourite language by far is Python, but C is in a firm second place. I’m familiar enough with its pitfalls that I know when I don’t know something, and where to look it up. I’m also under no illusions about its drawbacks, and would be quite happy if everybody moved to more memory-safe languages. Modern C++ — at least the set of features we’re encouraged to use at work — is also growing on me. The main issue I have with C++ is that it has so many features you *shouldn’t* use. At work we have a lot of tooling to help us make those choices, which greatly improves the C++ experience.What projects are you working on now?
Most of my projects are internal to Google, where we have a somewhat unusual environment. My main project is similar to Gregory Szorc’s PyOxidizer, although a couple years older and tightly coupled with Google’s build and deployment infrastructure. The open-source work I’m most excited about is my experimentation with replacing CPython’s refcounting with a traditional mark-and-sweep GC. I haven’t had much time in the last year to write the actual code for it, but I keep thinking it through, and I think it’s a realistic way forward for CPython. It would allow Larry Hasting’s Gilectomy to happen without too much extra overhead, and while it’ll mean radically changing the CPython C API, I think we can do that gradually in a good way. It’ll probably take ten years before it’s all done, but it’ll be worth it.
Which Python libraries are your favorite (core or 3rd party)?
I’ll have to shamelessly plug Google’s pytype here. Like mypy, it’s a PEP-484 type checker, but it actually infers types. You don’t have to annotate any of your code for it to be useful, and it can generate type annotations for your code (so that you can then also check the code with mypy, for example). I’m not directly involved with its development, but it’s one of the products of my team at Google and I had front-row seats at all stages of it. The people working on it are awesome — everyone in my team is — and pytype has caught lots of runtime errors during static analysis at Google. Getting the benefits of PEP-484 type annotations without having to figure out all the annotations is a huge win.
As a core developer of Python, what parts of the language do you maintain?
The post PyDev of the Week: Thomas Wouters appeared first on The Mouse Vs. The Python.
Stack Abuse: Text Translation with Google Translate API in Python
Unless you have been hiding under a rock, you have probably used Google Translate on many occasions in your life. Whenever you try to translate a word or a sentence from a certain language to another, it is the Google Translate API which brings you the desired results in the background. Though you can translate anything by simply going to the Google Translate web page, you can also integrate Google Translate API into your web applications or desktop programs. The best thing about the API is that it is extremely easy to set up and use.
You can actually do a lot of things with the help of the Google Translate API ranging from detecting languages to simple text translation, setting source and destination languages, and translating entire lists of text phrases. In this article, you will see how to work with the Google Translate API in the Python programming language.
Google Translate API Installation
Before you can work with the Google Translate API in Python, you will have to install it. There are two different methods of installing the API. The first method is straight forward. Simply go to terminal and use the pip
installer to install the API, as you would for any other Python library. To do this, type the following command in your terminal:
$ pip install googletrans
Press Enter and the Python module for Google Translate API will be installed on your system.
If you have installed an Anaconda distribution of Python, you can install the API using the Anaconda Prompt. In this particular method, you will replace pip
in the above command with conda
, as shown in the following code snippet:
$ conda install googletrans
Now that you have installed the API, we will see it in action with the help of some examples.
Listing Supported Languages
The Google Translate API supports a variety of languages. To list all the supported languages, run the following script:
import googletrans
print(googletrans.LANGUAGES)
In the above example, you use the keyword import
to import the googletrans
module. Subsequently, you can list all the language names by printing the LANGUAGES
attribute of the googletrans
module.
When executed, the above piece of code will list all the supported languages names along with their shorthand notation. Here is how the output will look:
{'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu', 'fil': 'Filipino', 'he': 'Hebrew'}
Basic Use
The most basic use of the Google Translate API is, of course, translating words or sentences from one language into another. To do so, we have to import the Translator
class from googletrans
module.
from googletrans import Translator
Next, you have to create an object of the Translator
class.
translator = Translator()
Once the Translator class object is created, you will pass the text in source language as a parameter to the translate()
method of the Translator()
class object, as shown below:
result = translator.translate('Mitä sinä teet')
In the script above, we pass Finnish text to the translate()
method.
The translate()
method returns an object that contains information about the translated text, the source and destination languages and the pronunciation of the text. By default, the translate()
method returns the English translation of the text passed to it. In our case, the object returned by the translate()
method is stored in the result
variable.
The object returned by the translate()
method has the following attributes:
src
: The source languagedest
: Destination language, which is set to English (en)origin
: Original text, that is 'Mitä sinä teet' in our exampletext
: Translated text, that will be 'what are you doing?' in our casepronunciation
: Pronunciation of the translated text
Let's print all the above attributes and see what output we get:
print(result.src)
print(result.dest)
print(result.origin)
print(result.text)
print(result.pronunciation)
Output:
fi
en
Mitä sinä teet
What are you doing
What are you doing
The output shows that the source language is Finnish (fi) and the destination language is English (en). The translated sentence can be printed via the text
attribute.
In the above example, we did not specify the source language. Therefore, Google Translate API tries to detect source language itself. Similarly, we did not specify any destination language as well and thus, the API translated the source language into the default language that is English. But, what if you want to specify both the source and destination languages?
Specifying Source and Destination Languages
It is in fact, very easy to specify both the destination and source languages in the Google Translate API. Here is the code you'll use to pass only the source language:
result = translator.translate('Mikä on nimesi', src='fi')
For adding destination language only, you have to add dest
attribute, followed by the language code:
result = translator.translate('Mikä on nimesi', dest='fr')
You can also pass the source and destination languages at the same time:
result = translator.translate('Mikä on nimesi', src='fi', dest='fr')
Let's now translate a Finnish sentence into French and then print the source and destination languages, as well as the translated text. This time we will specify the source and destination languages.
from googletrans import Translator
translator = Translator()
result = translator.translate('Mikä on nimesi', src='fi', dest='fr')
print(result.src)
print(result.dest)
print(result.text)
The above piece of code will produce the following result.
fi
fr
Quel est votre nom
Translating List of Phrases
It is also possible to translate a list of textual phrases with the help of the Google Translate API. The basic process is the same as discussed above. You just have to pass the list containing the phrases as a parameter to the translate()
method. This is useful for having a batch of phrases translated separately, but all in one API call.
Let's create a list of strings containing some phrases from the French language.
sentences = ['Bienvenu', 'Comment allez-vous', 'je vais bien']
Now it is time to call the translate()
method and pass the list, the source language, and the destination language as parameters.
result = translator.translate(sentences, src='fr', dest='sw')
In the above script, the source language is French and the destination language is Swahili.
The translate()
method returns a list of objects if you pass a list of phrases to it. Each object in the list returned by translate()
method corresponds to each phrase in the input list that has to be translated. The best way to find the translation of each input phrase in the list is to iterate over the list of output objects. You can then use the text
, origin
, src
, and other attributes of the individual objects to see the translation of individual phrases in the input list.
In the script below, we iterate over the list of objects returned by the translate()
method and then print the origin and translated text:
for trans in result:
print(f'{trans.origin} -> {trans.text}')
The following will be the result displayed on the screen.
Bienvenu -> karibu
Comment allez-vous -> Vipi wewe
je vais bien -> Niko sawa
Translating Text Documents
You can also translate text documents via Google Translate API. All you have to do is to read the text file in Python using the open
method, read the text and pass it to the translate()
method.
The first step is to open the file in the "read" mode:
f = open('C:\\Users\\Khurram\\Desktop\\test.txt', 'r')
You can also check whether or not the file is in "read" mode using the mode
property:
if f.mode == 'r':
Next, you can use the f.read()
method to read the contents of the file. The contents of the file can be stored in any variable. In our case, the name of the variable will be contents.
We will also print the contents
variable to check whether or not Python is properly reading the text file:
contents = f.read()
print(contents)
Here is the output of the file contents:
We are going to translate this text file using Python.
Subsequently, we will also translate it into French.
Make sure you have the above content in your text file if you want to follow along with our example.
We have ascertained that Python is accessing and reading the text file. Now, we will translate the result by importing the same old Translate
class from before.
from googletrans import Translator
file_translate = Translator()
The next step is to pass the contents
variable containing the input text to the translate()
function. Finally, print the text
attribute of the object returned by the translate()
method and you will get the translated string.
result = translator.translate(contents, dest='fr')
print(result.text)
The output should look like the following:
Nous allons traduire ce fichier texte en Python.
Par la suite, nous le traduirons également en français.
To write the translated text to the same file, or a different text file, you will simply open the file in the write mode ("w"). Next, you need to call the write()
method and pass it your translated text, as shown below:
with open('C:\\Users\\Khurram\\Desktop\\test_2.txt', 'w') as f:
f.write(result.text)
In the above example, we have used the context managerwith
to automatically open and close the write stream. Secondly, we have opened the file in the write mode. Lastly, we have used the write()
method to write the translated string to a new file.
Conclusion
Google Translate is a tool with an API that helps you perform a lot of different translation-related functions. We have only scratched the surface with the above examples. You are encouraged to practice the API, as well as learn and understand how to use it in real-life applications.
Codementor: Dividing Deep Into Enhancing Photos With Python
Real Python: Python Community Interview With Kelly and Sean of Teaching Python
This week I’m joined by Kelly Paredes and Sean Tibor, the hosts of the Teaching Python podcast. Join us as we discuss the benefits of learning Python outside of the code itself, and what it’s like to learn Python when you’re not planning to become a professional developer. So, without further ado, let’s meet Kelly and Sean!
Ricky:Welcome to Real Python, Kelly and Sean. I’m glad you could join me for this interview. Let’s start the same way we do with all our guests. How’d you get into programming, and when did you start using Python?
Kelly: I am probably your least typical coder/programmer. I didn’t really get into programming until about a year and a half ago. I played a little with MS-DOS as a kid, but I never went further than copying things from a manual. Also, I was a pre-med student in college but never really had a coding class.
During graduate school, I had a few web design classes, and I liked playing around on the web, but that’s the most coding I did in my youth. I later taught students how to make websites with HTML in Dreamweaver, back when it was a huge thing to do in education. Then, later I programmed Lego Robots with the EV3 Mindstorm software and played with Block Code a lot with students.
However, Python is my first real coding language, and I’ve been teaching myself how to code ever since. I think Python lends itself to newbies because of its readability and its logically organized structure.
Sean: I grew up around computers, and I think my earliest coding experiences were with Apple II computers in the classroom. I remember typing in BASIC programs from magazines and books to solve puzzles and decode cryptograms.
My serious programming started in college, where I studied information systems and had to learn about databases and web programming with Java and PHP. I fell in love with the ability to make things more efficient and elegant through practical code. I applied this to my career in software development and marketing by always finding a way to make things a little bit better with code, even when I became a manager and stopped coding as part of my day job.
About a year and a half ago, I decided to make a career change and start teaching computer science full time. We wanted to use Python as our foundational text-based language, so I began teaching myself all about it. It’s been one of the most satisfying languages to learn, use, and teach to others because of its elegant implementation and wide range of uses.
Ricky:You are the co-hosts of the Teaching Python Podcast, which started in December 2018. For those that have not yet listened, why did you start the podcast? Also, who was your target listener when you started and has this changed as you’ve progressed?
Kelly: Sean and I work together in the same classroom. We spend most of our nine hours together talking about really cool things like coding, pedagogy, classroom management, curriculum design, how to get cool projects into the classroom, and whatever else piques our interest that day. We often hang out while one of us is teaching, and we tend to work together instructing when we can.
We realized that we had something unique together. I was his teaching mentor because it was his first time teaching, and he was my Python coding mentor because, well, he’s just really smart when it comes to coding! We started this podcast because we noticed it’s hard to find trained software engineers who are willing to teach, and teachers who are willing to learn how to code.
If you’re a programmer, you typically don’t want to start teaching on a reduced salary, and there aren’t many experienced teachers who have strong coding experience. It’s hard to start a computer science curriculum if you don’t have quality teachers who know both the code and the pedagogy.
We strongly believe that good teachers from other subject areas can become great computer science teachers and that all it takes is a little mentoring and a lot of perseverance. At the same time, we want to help other teachers learn how to code so that we can all help develop students who can think critically, solve problems, and develop the social and emotional skills that will help them in their future.
Sean: We originally thought that many of our listeners would be teachers who specialize in computer science or a related STEM field. It’s been interesting to find that there are so many Python developers that have a passion for education and for showing others how to begin applying computational thinking and problem solving to the world around them!
One of our favorite things about this podcast has been meeting people from across the Python world who are doing such interesting and important work. Whether it’s English teachers that use code to explore literary concepts, college-level instructors teaching data science principles to graduate students in the sciences, or busy professionals who teach after school Python programs to underprivileged or under-represented groups, it’s inspiring to see how Python can be used to make the world a little bit better.
Ricky:You teach Python to middle school-aged kids. I’m curious to hear about any challenges it has presented for you personally. How do you approach teaching that age group differently, as opposed to adults?
Kelly: I’ve taught middle school children for over twenty years. I love this age group, especially 7th graders—they’re sponges! If they’re given the opportunity and the right amount of support, then they can accomplish amazing things.
In my opinion, teaching middle school students is the easy part. Sometimes, explaining to their parents that coding is hard and that we’re “pushing” their children to do what they believe they can’t do is the hard part. Parents don’t typically know how to let their child struggle. And most parents don’t understand coding themselves, and so they feel lost trying to help their child.
However, Sean and I believe that finding the right desirable difficulties is what helps students to learn more than just how to code. In the end, parents are super pleased with what their children accomplish!
Sean: Even after working in business with adults for so many years, it was a little intimidating to teach middle school students for the first time, because they usually look to the teacher as the primary source of knowledge. We have really great students in our school, and middle school is often the time where they’re seeking a huge amount of knowledge and understanding about themselves and the world around them.
I’ve found that one of the most important things I can do as a teacher is to show them that I don’t know everything. Then, I can guide them through the process of acquiring new information and applying it to the problem they’re trying to solve or the program they’re trying to create. I have to thank Kelly for helping me understand the importance of this approach so quickly as a new teacher.
Ricky:I think it’s fair to say that not all of your students are going to end up as software engineers or similar in the future. What benefits have your students gained in learning something like Python programming and completing the projects set?
Kelly: I think it’s fair to say that most computer science teachers do not expect their students to become software engineers, nor do we seek out that goal for them. What I’ve learned through my own process of learning is that “learning how to code” is so much more than developing a cool app, or another version of a “Guess the Number” game.
I’ve learned so much about myself and how to solve problems that I knew nothing about or had no background knowledge about, or even a reference to start from. However, I’ve developed skills that help me build my confidence, improve my research skills, allow me to read content where I know less than 80% of the vocabulary, and persevere in solving a problem that has multiple solutions.
I want my students to have the same experience. I want to build their confidence and let them understand that getting an “A” on a test is great, but being able to solve a problem, to think critically about the implications and results, and to have the dedication to face the unknown is more important than getting an “A” or being a “coder.” We really hope to develop lifelong learners who are eager to take on any challenge.
Sean: I often tell the students that we’re lucky to be coding in Python because it’s such a great language for beginners, and it also grows with you to solve the problems at hand.
I think it’s fair to say that the least important thing we’re teaching right now is the language, syntax, and vocabulary of Python. What we’re really teaching is research, problem solving, persistence, dealing with setbacks and failure, and how to develop true competence in something. These traits and skills are durable across many different disciplines, not just computer science.
To echo Kelly, only a small fraction of our students are going to pursue a traditional computer science education, but all of them are going to need to think through and solve problems, learn and master topics, and develop true competency in their chosen fields. Our wager is that knowing how to solve problems through code and technology will serve them well in the career paths of the 21st century.
Ricky:Now, for my last few questions. What else do you get up to in your spare time? What other hobbies and interests do you have, aside from teaching Python and coding?
Kelly: Between teaching, learning how to code better, and recording Teaching Python, I don’t do too much else! I love spending time with my two young boys and enjoying the South Florida life. We like swimming, going to the beach, fishing, and playing sports—really, anything outdoors. However, I really want to write a book one day and have started the process, but it’s hard to get it going with the time I have leftover in the day.
I also want to get to a point in my coding where I can make something really useful that helps teachers get a better understanding of their students’ academic, emotional, and social progress in school. Both of these latter goals are “near” future hopes and dreams that I try to keep within my grasp.
Sean: If I’m not teaching Python or writing code, then you can usually find me playing with my kids, consulting with a few marketing clients, or finding ways to make my house smarter. I also try to stay healthy by distance running and kickboxing at least a few mornings a week before school.
We’re blessed in South Florida with warm weather year-round, so you may also see me out fishing in the canals and lakes, flying my drone, or experimenting with photography. Sometimes, I even get to play video games or work on side projects, like Pandas-based data science analysis of CRM systems for my clients.
Thank you, Kelly and Sean, for joining me for this interview! It’s been great hearing from you both.
You can check out the Teaching Python podcast on their website, or by searching for it in your favorite podcast player. You can also follow Kelly and Sean on Twitter. As always, if there’s someone you’d like me to interview in the future, then leave a comment down 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 ]
Python Bytes: #166 Misunderstanding software clocks and time
Roberto Alsina: Episodio 19: Small Data (I)
Jugando con datos de verdad! Cómo podemos usar esta pila de datos que sacamos de algún lado para hacer algo interesante? Empecemos!
Para ver qué hice yo con estos mismos datos: https://nombres.ralsina.me
tryexceptpass: Episode 2 - Writing README files
Summary
- What’s a README?
- Modern day added formatting. Mostly Markdown, sometimes Restructured Text.
- What to include in a README?
- Name the project
- Intro / Summary / Overview
- Prerequisites
- Installation instructions
- Usage instructions
- Contribution instructions.
- Credit for important contributors.
- Acknowledgments of projects you based yours on.
- Ways to communicate with you or the community.
- License information.
- Project status information in the form of badges.
PyCharm: PyCharm 2020.1 EAP starts now
There are two types of people in the world: those who can wait to open a package they’ve received, and people like me, who need to see what’s inside this very second.
PyCharm isn’t delivered in the mail though, and that’s why we have something even better for impatient people. The early access program (EAP) shows you what’s in the package a couple months before you get it. Take a sneak peek, and get PyCharm’s first EAP now!
New in PyCharm
JetBrains Mono
We recently announced our font designed especially for programming. This means that we’ve optimized the font for reading vertically, and other optimizations like code-specific ligatures. If you haven’t seen it yet, be sure to check out the page where we introduce JetBrains Mono, it clearly shows how it makes programming a more enjoyable experience.
From PyCharm 2020.1 onward, this font is chosen as a default in the editor. If you had previously configured another font, be sure to try out JetBrains Mono to see if you like it. You can configure the font in Settings | Editor | Font.
Further Improvements
- Some Version Control features just got better! On the Branches popup (Find action, Ctrl/Cmd-Shift-A, and then look for ‘Branches’), there’s now a refresh icon that will fetch changes from the git remote and a search box that allows you to quickly find your branch.
- We’ve updated the ‘Python Debug Server’ run configuration. It used to be called ‘Python Remote Debug’, and we think this name makes clearer what it does. When you run this configuration, PyCharm will listen to an incoming connection from your script. To use it, you need to modify your script, and ensure that the parameters to the
settrace
call are correct: it should be the hostname how the script (from where it’s running) can access the host where the IDE is running, and the appropriate port. You need to ensure yourself that the hostname correctly resolves for the script, and that all firewalls are appropriately configured for the connection to succeed. A common way to punch through firewalls is to use this feature together with SSH remote forwarding. If you want PyCharm to do this for you, a remote interpreter can handle all the details in the background. - To see everything that’s new in this release, check out the release notes.
Interested?
Download this EAP from our website. Alternatively, you can use the JetBrains Toolbox App to stay up to date throughout the entire EAP.
If you’re on Ubuntu 16.04 or later, you can use snap to get PyCharm EAP, and stay up to date. You can find the installation instructions on our website.