Planet Python

Subscribe to Planet Python feed
Planet Python - http://planetpython.org/
Updated: 2 hours 53 min ago

PyBites: Fail. Fail better. A change in attitude towards Imposter Syndrome

Thu, 2023-01-26 14:05

I’m afraid my introduction to you all will quickly turn into somewhat of a cliche.

Let me explain. You know what it’s like. You are watching a YouTube video on the latest and greatest tutorials (Yeah, I was also engulfed in the flames of  tutorial hell).

Then it happens… commercial! Arghhhh! There is one I can’t bear to click away from (enter cliche here). 

Master Class. Not just any Master Class. This one is a trailer for Margaret Atwood Teaches Creative Writing.

If you don’t know who she is, then I’m sure you would have heard of the popular Netflix Series “The Handmaid’s Tale”. Well she wrote it. Cliche?

I’m a Canadian Expat, living in Europe. Suddenly memories came flooding back of studying Canadian Literature in University almost 25 years ago. Countless hours of reading her novels, books of poetry, even going to a reading of hers in person. So what does this have to do with coding and imposter syndrome?? 

Despite watching the trailer over and over and over, sometimes even googling it to watch it from time to time, it was only after I decided to go back into coding after a 20 year break that I really got the message.

So, about six months ago, there I was, coding along when boom! Commercial time. I immediately seized the opportunity for a coffee refill whilst intently listening, taking for granted that I knew Atwood’s words of wisdom by heart. Then, what she said stopped me in my tracks:

If you really do want to write and you’re struggling to get started, you’re afraid of something.

– Margaret Atwood

What did she say? It was only then that I realised that this is exactly the reason why I spend too much time watching tutorials instead of coding.

What am I afraid of then? Looking stupid? Failure? Being discovered as a fake? Basically all of the above. And what do all of the above have in common? 

You guessed it! Fear

Fear is crippling and it fuels Imposter Syndrome.

We set out with good intentions. For example: “Today, I’m going to start that bigger project and get out of my comfort zone. I really am this time.” Then you code part way, run into some obstacles and it ends up as a partial project, stowed away in your private repositories on  GitHub.

For those of you who don’t really follow what I am getting at here, I have a challenge for you:

Look at your repositories, “What percentage of your repos are private?”

Look at them and ask yourself: “Why are they private? “Why didn’t I finish them?”

Go ahead and write it down. Make a list. Take inventory of your fears.

Atwood continues with:

Any form of human creativity is a process of doing it and getting better at it.

Just do it.

Imagine if you could sit in front of your screen without fear and just code. It doesn’t matter if there are syntax errors, etc. You just code.

Atwood explains creativity as a process. We just don’t wake up one day and become an expert. Neither did she. She wrote and wrote for years and years before getting to the level she is at today. 

Atwood continues: 

You become a writer by writing. There is no other way.

Coding, coding and more coding.

There really is no other way is there? Practice, practice and more practice is the only way to get better.

Last Atwood quote I promise (you can tell I’m a fan right? Or should I say eh?):

So do it, do it more. Do it better. Fail. Fail better.

Fail better? Ahh yes, perfectionism. The other fuel for Imposter Syndrome. 

Well should I say the other component of the fuel for Imposter Syndrome. A fear of Failure.

Think about it: we are all good at something, perhaps even an expert. We have no  problem executing those tasks. In fact, we enjoy them.

Why? We don’t fear failure when it comes to taks we are good at. It’s the learning something new and the process of it that we fear the most. 

I’ll make a confession. This is my first blog post!

Bob and Julian graciously invited me to write on and I am honoured to say the least.

This has been at least two weeks in the making. Why? Fear. I can’t write a blog. What if I look stupid? What if no one likes or reads it?

Every single word written in this blog was written by overcoming fear and Imposter Syndrome.

Every. Single. Word.

I had to face my fear and perfectionism head on. “Fail better”, according to my interpretation, means not being afraid to make mistakes. In fact, the” better” part is referring to learning from those mistakes. How? 

A change of attitude towards mistakes. 

Let’s change the vocabulary here: 

Mistakes/Failures == Learning Opportunities

Fail Better == I’ve chosen to learn from each of my mistakes/failures. 

The bottom line is: we all struggle with this. I’m actually a trained Therapist.

So what has helped me in the last two weeks to actually complete this blog?  The 20 minute confidence training video by Bob and Julian you can check out at the end of this blog. 

Hope you enjoyed this and perhaps next time, I’ll share an interesting anecdote about the elephant … 

-Sherry Andrews Bhutia

Pybites Instagram Content Creator and Pythonista 

Bonuses:

Categories: FLOSS Project Planets

PyCharm: PyCharm 2022.3.2 Is Out!

Thu, 2023-01-26 13:51

The second minor release brings a list of improvements, and fixes for the debugger, test runner, and Python Console.

You can update to version 2022.3.2 by using the Toolbox App, installing it right from the IDE, or downloading it from our website.

Download PyCharm 2022.3.2

  • The behavior of the Settings Sync plugin has been updated. Settings synchronization across different IDE products is now turned off by default and can be enabled via a checkbox in Settings/Preferences | Settings Sync. [IDEA-233535]
  • Prisma ORM plugin: relationMode property can now be used in the data source configuration; previewFeatures values are autocompleted in lower camelcase; binaryTargets completion now works for all supported operating systems. [WEB-58853], [WEB-58852], [WEB-58854]
  • ESLint: flat configuration files are now detected and ESLint is executed without the --config flag. [WEB-57661]
  • TypeScript: accessor keyword is now recognized. [WEB-58320]
Notable fixes:
  • We’ve fixed the issue causing private PKCS #8 keys to malfunction when the IDE couldn’t locate the corresponding public key. [IDEA-307924]
  • Search Everywhere once again works as expected, providing all text search results. [IDEA-307142]
  • Test runner: Running pytest on Windows doesn’t cause a UnicodeEncodeError with non-UTF8 locales. [PY-55463]
  • Debugger: Running pytest in debug mode no longer fails with a missing `pydev_tests_python` module. [PY-53913]
  • The local interpreter in PyCharm can again be set up via the environment variable. [PY-57453]
  • Python 3.11: The IPython console doesn’t break with non-ASCII characters in tracebacks. [PY-57408]
  • Debug Console: Output encoding is now correct for Windows. [PY-57964]
  • Debug Console no longer changes the output after execution. [PY-56839]
  • Windows users whose usernames contain spaces can load a Conda environment in the Terminal. [PY-57644]
  • The Rich package works as expected in the Python Console when the Emulate terminal in output console option is used. [PY-57770]
  • Quick Documentation works in the Python Console with the Ctrl + j shortcut. [PY-57642]
  • Python Console: Parameters info works with Python 3.11. [PY-58002]
  • Databases: introspecting database objects or external schemas in Amazon Redshift doesn’t cause an error. [DBE-16887]

To see the full list of improvements, check out the release notes. Found a bug or have an idea for a new feature or implementation? Share your feedback with us in the comments, on Twitter, or in our issue tracker.

The PyCharm team

Categories: FLOSS Project Planets

Awesome Python Applications: Moa Bridge

Thu, 2023-01-26 12:04

Moa Bridge: A service to cross post [Mastodon](https://github.com/mastodon/mastodon) toots to Twitter and Instagram.

Links:

Categories: FLOSS Project Planets

Python Software Foundation: The PSF is hiring a Security Developer-in-Residence!

Thu, 2023-01-26 09:58
The Python Software Foundation (PSF) is happy to announce the launch of a year-long security enhancement initiative that will include a security audit and the creation of a new Security Developer-in-Residence role. Generous funding by the OpenSSF’s Alpha-Omega Project has made this work possible.

Recent open source supply chain security attacks on open source projects and infrastructure have increased awareness of the critical role projects like Python and the Python Package Index (PyPI) play in providing a safe and secure ecosystem for millions of open source users. Historically, the Python Software Foundation’s ability to make key security improvements has only been realized when dedicated teams of volunteers or its existing infrastructure staff make time, or when it has received occasional grants, such as the introduction of 2FA and other security improvements to PyPI in 2019.

The Security Developer-in-Residence will work full-time during the initiative to formalize existing security practices and become more proactive in Python-related security improvements. The new role will be responsible for addressing security issues across PSF projects such as CPython and PyPI, and applying knowledge and expertise and working with volunteers to implement key improvements in a timely manner. They will also establish new processes and features that make it easier to prevent, detect, and respond to security risks to lay a foundation that makes it easier and more sustainable for the community to identify and address security issues going forward.

The Security Developer-in-Residence job is posted HERE. Please take a look and and share with your friends and colleagues.

This role is funded by a substantial investment from the Open Software Security Foundation’s Alpha-Omega Project. The OpenSSF is a non-profit cross-industry collaboration that brings together leaders to improve the security of open source software by building a broader community, targeted initiatives, and best practices. The OpenSSF brings together open source security initiatives under one foundation to accelerate work through cross-industry support.

The PSF is a non-profit whose mission is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers. The PSF supports the Python community using corporate sponsorships, grants, and donations. Are you interested in sponsoring or donating to the PSF so it can continue supporting Python and its community? Check out our sponsorship program, donate directly here, or contact our team!
Categories: FLOSS Project Planets

Codementor: Shortest Program in Language C

Thu, 2023-01-26 04:55
lets learn the shortest program in C, the absolute basics in Language C
Categories: FLOSS Project Planets

Codementor: C Programming in 2023 ?

Thu, 2023-01-26 02:54
is it worth to learn language C
Categories: FLOSS Project Planets

PyCharm: In Conversation With the Reloadium Team: Hot Reload and a Future Webinar

Wed, 2023-01-25 09:18

PyCharm is working hard on Python developer experience (DX). There’s a project with a very promising DX boost using “hot reloading”: Reloadium. It really speeds up turnaround time on working with your code, and with the PyCharm plugin, brings fresh new ideas to running, debugging, and profiling.

On January, 27 we have a webinar with Reloadium to show it in action. As an intro, we did a Q&A with the team.

Register now!

Quick Hit: Why should people care about Reloadium?

DK: Reloadium is a valuable tool for developers that offers hot reloading, shortening development cycle and preserving application state for debugging. Its PyCharm plugin integration makes Reloadium easy to use and offers additional features for efficient debugging. It’s a powerful tool for improving workflow and streamlining development.

Now, some introductions, starting with Sajan, who will be presenting.

ST: Thank you for the opportunity to be a part of this, Paul, we are very excited to share a glimpse into the problem we are solving for Python developers. My name is Sajan, I am a Business Psychologist specialising in the areas of business development, change management, and human factors. I have been working closely with Damian to create and improve Reloadium by identifying the gaps that exist in terms of productivity with Python and finding ways to close these gaps. 

DK: My name is Damian and I am the creator of Reloadium. I am currently employed as a Technical Lead and Full-Stack Developer on a full-time basis. The concept for Reloadium originated from a project I was working on, where I found that I had to frequently deal with long restart times. I created an initial version of Reloadium and used it to optimize my workflow, cutting down the development time significantly. That experience motivated me to continue to develop the tool and make it available to other developers. I am dedicated to providing a reliable and user-friendly hot reloading solution that can improve the development experience and increase efficiency in the programming process.

Explain hot reloading, especially from its use in frontend dev.

ST: Hot reloading is a valuable tool for developers. It allows developers to make updates to an application’s source code and see the resulting changes in real-time. This feature streamlines the development process by eliminating the need for manual application restarts, thus saving time and improving the efficiency of the development cycle. Additionally, one of the key benefits of hot reloading is the ability to preserve the state of the application, which can be particularly useful during bug fixing and troubleshooting.

In a front-end development context, hot reloading enhances the development experience by providing instant feedback on the effect of code changes, allowing developers to make adjustments and iterate quickly without the need for manual page refreshes. 

Hot reloading is a powerful feature that enables developers to work more efficiently, improve their workflow and deliver better-quality software.

Others worked on this. Damian, you then went off for a long time. What are the subtle, hard parts?

DK: Implementing hot reloading in Python has proven to be a challenging task, as previous attempts have not been able to consistently provide a reliable and user-friendly experience. One of the main difficulties in this process is maintaining compatibility with subtle variations in different versions of Python and operating systems. To ensure consistent quality between releases, it has been necessary to implement rigorous unit and end-to-end testing procedures. This has been a crucial step in identifying and addressing any issues that may arise during the development process, and in ensuring that the final product is stable and reliable for users. 

What are some of the fresh ideas Reloadium brings to DX?

ST: Reloadium brings several cutting-edge features to the developer experience, perhaps the most notable of which is Frame Reloading. This feature allows developers to hot reload the current call stack frame, enabling them to modify code that is currently being executed. This is a significant innovation in the field of software development, as it allows developers to make changes while debugging, which can save a significant amount of time. Another innovative feature is Hot Reloading of Unhandled Exceptions. Reloadium will break at unhandled exceptions and allow developers to fix the error by hot reloading changes. This feature is particularly useful when debugging non-deterministic bugs, as it can save countless hours in the reproducing and resolving of these issues.

How does the PyCharm plugin help improve the DX part?

DK: The PyCharm plugin provides a seamless integration of Reloadium, making it an invaluable tool for improving the development experience. In addition to integrating the core hot reloading functionality of Reloadium, the plugin also includes several advanced debugging features, such as time and memory profiling, call stack frame dropping, restarting frames, and hot reloading of unhandled exceptions. These features provide developers with more robust tools for identifying and resolving issues, thus increasing the efficiency of the development process. Furthermore, the plugin provides visual feedback on hot reloading and code execution, which greatly improves the user experience. Overall, the PyCharm plugin’s integration of Reloadium and advanced debugging features make it a powerful tool for enhancing the development experience.

Another fresh idea: you’ve formed a company to make this sustainable. Tell us about that.

ST: We are in the process of forming a company that focuses on developer tools like Reloadium to improve productivity, and ultimately helps in making programming easier. We are committed to making programming more developer-friendly and accessible so that people can feel free to design, create, and innovate. Through this company, we can keep improving and managing products like Reloadium so that it stays relevant, and continues to help improve productivity for developers.

Give us a teaser for what folks will see in the webinar.

ST:  In this webinar, we will explore:

  • how developers can experience Reloadium’s out-of-the-box experience on PyCharm
  • Reloadium’s hot-reloading feature in action during front-end and back-end development scenarios 
  • The means of further optimising your workflow by using Reloadium’s time-profiling and memory-profiling features. 

Register now!

We urge you to join us in this journey to learn about this pioneering DevTool so that you can make more time to design, create, and innovate.  

Categories: FLOSS Project Planets

Real Python: The Python Standard REPL: Try Out Code and Ideas Quickly

Wed, 2023-01-25 09:00

The Python standard shell, or REPL (Read-Eval-Print Loop), allows you to run Python code interactively while working on a project or learning the language. This tool is available in every Python installation, so you can use it at any moment.

As a Python developer, you’ll spend a considerable part of your coding time in a REPL session because this tool allows you to test new ideas, explore and experiment with new tools and libraries, refactor and debug your code, and try out examples.

In this tutorial, you’ll learn how to:

  • Run the Python standard REPL, or interactive shell
  • Write and execute Python code in an interactive session
  • Quickly edit, modify, and reuse code in a REPL session
  • Get help and introspect your code in an interactive session
  • Tweak some features of the standard REPL
  • Identify the standard REPL’s missing features

You’ll also learn about available feature-rich REPLs, such as IDLE, IPython, bpython, and ptpython.

To get the most out of this tutorial, you should be familiar with your operating system’s command line, or terminal. You should also know the basics of using the python command to run your code.

Free Sample Code: Click here to download the free sample code that you’ll use to explore the capabilities of Python’s standard REPL.

Getting to Know the Python Standard REPL

In computer programming, you’ll find two kinds of programming languages: compiled and interpreted languages. Compiled programming languages like C and C++ will have a compiler program, which takes care of translating the language’s code into machine code.

This machine code is typically saved into an executable file. Once you have an executable file, you can run your program on any compatible computer system without needing the compiler or the source code.

In contrast, interpreted languages like Python need an interpreter program. This means that you need to have a Python interpreter installed to run Python code on your computer. Some may consider this characteristic a drawback because it can make your code distribution process much more difficult.

However, in Python, having an interpreter offers one significant advantage that comes in handy during your development and testing process. The Python interpreter allows for what’s known as an interactive REPL (Read-Eval-Print Loop), or shell, which reads a piece of code, evaluates it, and then prints the result to the console in a loop.

Note: In this tutorial, you’ll learn about the CPython standard REPL, which is available in all the installers of this Python distribution. If you don’t have CPython yet, then check out Python 3 Installation & Setup Guide for detailed instructions.

The Python interpreter can execute Python code in two modes:

  1. Script, or program
  2. Interactive, or REPL

In script mode, you use the interpreter to run a source file as an executable program. In this case, Python loads the file content and runs the code line by line, following the script or program’s execution flow. Alternatively, interactive mode is when you launch the interpreter and use it as a platform to run code that you type in directly.

Note: The name Python is commonly used to denote two different things: the language itself, and the interpreter. In this tutorial, you’ll find the explicit term Python interpreter only in situations where ambiguity can arise.

In this tutorial, you’ll learn how to use the Python standard REPL to run code interactively, which allows you to try ideas and test concepts when using and learning Python. Are you ready to take a closer look at the Python REPL? Keep reading!

What Is Python’s Interactive Shell or REPL?

When you run the Python interpreter in interactive mode, you open an interactive shell, also known as an interactive or a REPL session. In this shell, your keyboard is the input source, and your screen is the output destination.

Note: In this tutorial, you’ll find the terms interactive shell, interactive session, interpreter session, and REPL session used interchangeably.

The input consists of Python code, which the interpreter parses and evaluates. After that’s done, the interpreter automatically displays the result on your screen, and the process starts again as a loop.

So, Python’s REPL is an interactive way to talk to your computer using the Python language. It’s like live chat. The whole process is known as a REPL because it goes through four steps that run under the hood:

  1. Reading your input, which consists of Python code as expressions and statements
  2. Evaluating your Python code, which generates a result or causes side effects
  3. Printing any output so that you can check your code’s results and get immediate feedback
  4. Looping back to step one to continue the interaction

This feature of Python is a powerful tool that you’ll wind up needing in your Python coding adventure, especially when you’re learning the language or when you’re in the early stages of a development process. That’s because the REPL offers several benefits, which you’ll learn about next.

Read the full article at https://realpython.com/python-repl/ »

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

Categories: FLOSS Project Planets

Python for Beginners: Convert Epoch to Datetime in Python

Wed, 2023-01-25 09:00

Most of the software applications log date and time values as UNIX timestamps. While analyzing the logged data, we often need to convert the Unix timestamp to date and time values. In this article, we will discuss different ways to convert UNIX timestamps to datetime in python. We will also discuss how to convert a negative epoch to datetime in Python.

Table of Contents
  1. Unix Epoch to Datetime in Python
  2. Unix Timestamp to Datetime String
  3. Datetime to UNIX Timestamp in Python
  4. Convert Negative Timestamp to Datetime in Python
  5. Conclusion
Unix Epoch to Datetime in Python

To convert epoch to datetime in python, we can use the fromtimestamp() method defined in the datetime module. The fromtimestamp() method takes the epoch as its input argument and returns the datetime object. You can observe this in the following example. 

from datetime import datetime epoch=123456789 print("The epoch is:") print(epoch) datetime_obj=datetime.fromtimestamp(epoch) print("The datetime object is:") print(datetime_obj)

Output:

The epoch is: 123456789 The datetime object is: 1973-11-30 03:03:09

In this example, we have converted the epoch 123456789 to datetime value using the fromtimestamp() method.

The above approach will give you time according to the time zone on your machine. If you want to get the UTC time from the timestamp, you can use the utcfromtimestamp() instead of the fromtimestamp() method as shown below.

from datetime import datetime epoch=123456789 print("The epoch is:") print(epoch) datetime_obj=datetime.utcfromtimestamp(epoch) print("The datetime object is:") print(datetime_obj)

Output:

The epoch is: 123456789 The datetime object is: 1973-11-29 21:33:09

In this example, you can observe that the datetime output show the time approx 5 hours and 30 minutes before the time in the previous example. However, we have used the same epoch value. This difference is due to the reason that the time zone of my computer is set to +5:30 hours.

Unix Timestamp to Datetime String

To convert the Unix timestamp to a datetime string in python, we will first create a datetime object from the epoch using the fromtimestamp() method or the utcfromtimestamp() method. Then, you can use the strftime() method to convert the datetime object to a string.

The strftime() method, when invoked on a datetime object, takes the format of the required datetime string as its input argument and returns the output string. You can observe this in the following example.

from datetime import datetime epoch=123456789 print("The epoch is:") print(epoch) datetime_obj=datetime.utcfromtimestamp(epoch) print("The datetime object is:") print(datetime_obj) datetime_string=datetime_obj.strftime( "%d-%m-%Y %H:%M:%S" ) print("The datetime string is:") print(datetime_string)

Output:

The epoch is: 123456789 The datetime object is: 1973-11-29 21:33:09 The datetime string is: 29-11-1973 21:33:09

In this example, the format specifiers used in the strftime() method are as follows.

  • %d is the placeholder for date.
  • %m is the placeholder for month.
  • %Y is the placeholder for year.
  • %H is the placeholder for hour.
  • %M is the placeholder for minutes.
  • %S is the placeholder for seconds.

You can also change the position of the placeholders in the string to change the date format.

Datetime to UNIX Timestamp in Python

To convert a datetime object to UNIX timestamp in python, we can use the timestamp() method. The timestamp() method, when invoked on a datetime object, returns the UNIX epoch for the given datetime object. You can observe this in the following example.

from datetime import datetime datetime_obj=datetime.today() print("The datetime object is:") print(datetime_obj) epoch=datetime_obj.timestamp() print("The epoch is:") print(epoch)

Output:

The datetime object is: 2023-01-24 00:34:40.582494 The epoch is: 1674500680.582494

In this example, we first obtained the current datetime using the datetime.today() method. Then, we used the timestamp() method to convert datetime object to timestamp.

Convert Negative Timestamp to Datetime in Python

The UNIX timestamp or epoch is basically the number of seconds that elapsed after UTC 1st January 1970, 0 hours, 0 minutes, 0 seconds. So, if we represent a date before 1970 using an epoch, the value is negative. For example, the if we represent 31 December 1969 using epoch, it will evaluate to -86400 i.e 24 hours*3600 seconds before 01 Jan 1970. You can observe this in the following example.

from datetime import datetime epoch=-86400 print("The epoch is:") print(epoch) datetime_obj=datetime.fromtimestamp(epoch) print("The datetime object is:") print(datetime_obj)

Output:

The epoch is: -86400 The datetime object is: 1969-12-31 05:30:00

To convert a negative UNIX timestamp to datetime, you can directly pass it to the fromtimestamp() method as shown above. Here, we have specified the timestamp as -86400. Hence, the fromtimestamp() method returns the datetime 86400 seconds before the datetime 01 Jan 1970 +5:30.

Again, the above approach will give you time according to the time zone on your machine. If you want to get the UTC time from the timestamp, you can use the utcfromtimestamp() instead of the fromtimestamp() method as shown below.

from datetime import datetime epoch=-86400 print("The epoch is:") print(epoch) datetime_obj=datetime.utcfromtimestamp(epoch) print("The datetime object is:") print(datetime_obj)

Output:

The epoch is: -86400 The datetime object is: 1969-12-31 00:00:00

In this example, you can observe that the utcfromtimestamp() method returns the datetime 1969-12-31 00:00:00 which is exactly 86400 seconds before Jan 1, 1970 00:00:00.

Instead of using the above approach, you can also use the timedelta() function to convert the negative epoch to datetime.

The timedelta() function takes the negative epoch as its input argument and returns a timedelta object. After calculating the timedelta object, you can add it to the datetime object representing Jan 1, 1970. This will give you the datetime object for the negative epoch that was given as input. You can observe this in the following example.

from datetime import datetime from datetime import timedelta epoch=-86400 print("The epoch is:") print(epoch) datetime_obj=datetime(1970,1,1)+timedelta(seconds=epoch) print("The datetime object is:") print(datetime_obj)

Output:

The epoch is: -86400 The datetime object is: 1969-12-31 00:00:00 Conclusion

In this article, we have discussed different ways to convert a UNIX timestamp to datetime in python. We also discussed how to convert a negative epoch to a datetime object in python.

To learn more about python programming, you can read this article on python simplehttpserver. You might also like this article on python with open statement.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

The post Convert Epoch to Datetime in Python appeared first on PythonForBeginners.com.

Categories: FLOSS Project Planets

PyBites: 10 things that hamper your Python career progress

Wed, 2023-01-25 08:42

We all know that becoming a Python developer is hard.

There’s the “10,000-hour” principle which means there’s a significant amount of effort and time you’re going to have to invest.

More important though is how you’ll spend that time. Are you working on the right things and tackling increasingly challenging goals?

We talk with a lot of people about their Python career goals and here are some common things that are holding them back:

  1. I do not have a clear end goal of what I wanted to do for a career.
  2. I don’t know what options there are to break into the tech industry and what niche I want to be in.
  3. I feel as though I have learned the foundations of Python but am struggling with understanding how to bridge the gap from where I am now to building a production-ready application.
  4. I am completely self-taught, I have taken numerous courses from xyz, but have not been able to feel confident enough to develop an application end-to-end. Getting to this point will give me the confidence to apply for developer type jobs.
  5. Bad time management. I often find myself caught up in other “urgent” tasks. It can be very tough to work on a programming project on and off (in an unfocused manner), a lot of time gets lost having to re-learn skills, never reaching consistent fluency.
  6. Tutorial hell. And it’s hard when I’m getting stuck in dead ends, not being able to find the answers on the Internet.
  7. Impostor syndrome, not feeling confident enough and not knowing where my skill/experience level fits compared to other developers.
  8. I just haven’t got the ‘developer mindset‘ yet e.g., “How would a dev approach a particular problem?”
  9. Sites with simple coding tasks tend to focus on little parts of potentially big things so I do not see a good way to learn without building a bigger project, with a mentor, gaining more holistic developer skills. I lack feedback and code reviews from more experienced developers.
  10. My main obstacle is a lack of direction (not having anybody to bounce questions or ideas off of) and a sort of decision paralysis given the sheer number of options, resources, libraries and “best practices” available.

Do one or more of these things resonate with you?

Do you want to get to the next level as a Python developer but you feel these types of reasons are holding you back too?

Then we have good news. You can resolve all of these things in just 3 months by putting in ~10 hours of consistent effort a week.

Our team of expert coaches can show you the right way, and once exposed to it, you won’t look back to your old way of doing things.

Working with us:

  • You will get a crystal clear understanding of your goals, what will (and won’t) matter in your career moving forward.
  • You will ship code, specifically two or three fully-fledged applications of your choosing (!) – a unique approach we take which teaches you Python, common tools and libraries, while at the same time you’ll build up your portfolio (people that showcase their projects land jobs!)
  • Code reviewing, pair programming, design reviewing… it’s like working in a dev team before you officially land such a job (fake it before you make it in a safe environment)
  • You’ll learn to push through doubts and fears, embracing imposter syndrome. Pybites is unique in the industry by offering a solution that teaches both the tech and the mindset side of things. This is something we’re proud of and do with passion every single day. People we work with often recognize towards the end that the mindset was the unexpected hero, and THE ingredient they actually needed the most!
  • No more tutorial hell (yes that’s possible!) – with us you’ll embrace JIT (“just in time”) learning. You’ll learn things as the need arises and only use courses and books as reference materials. A weight will fall off your shoulders and this alone will make you a more effective developer. You’ll constantly have to learn new things, and you’re expected to pick things up fast too. Working with us you’ll learn this from the get go and it’s a career transforming skill.

If you’re excited at this point at the prospect of taking your Python journey to the next level in an effective way, it’s time to take action:

1) Check out our Pybites Developer Mindset (PDM) program.

2) If you got excited about the sample projects and watching / reading what PDM alumni have gained from working with us, then apply on the page. Please provide us with as much detail so we can be prepared when we reach out to you.

Looking forward to hearing from you.

– Bob & Julian

Categories: FLOSS Project Planets

Talk Python to Me: #400: Ruff - The Fast, Rust-based Python Linter

Wed, 2023-01-25 03:00
Our code quality tools (linters, test frameworks, and others) play an important role in keeping our code error free and conforming to the rules our teams have chosen. But when these tools become sluggish and slow down development, we often avoid running them or even turn them off. On this episode, we have Charlie Marsh here to introduce Ruff, a fast Python linter, written in Rust. To give you a sense of what he means with fast, common Python linters can take 30-60 seconds to lint the CPython codebase. Ruff takes 300 milliseconds. I ran it on the 20,000 lines of Python code for our courses web app at Talk Python Training, and it was instantaneous. It's the kind of tool that can change how you work. I hope you're excited to learn more about it.<br/> <br/> <strong>Links from the show</strong><br/> <br/> <div><b>Charlie on Twitter</b>: <a href="https://twitter.com/charliermarsh" target="_blank" rel="noopener">@charliermarsh</a><br/> <b>Charlie on Mastodon</b>: <a href="https://hachyderm.io/@charliermarsh" target="_blank" rel="noopener">@charliermarsh@hachyderm</a><br/> <b>Ruff</b>: <a href="https://github.com/charliermarsh/ruff" target="_blank" rel="noopener">github.com</a><br/> <br/> <b>PyCharm Developer Advocate Job</b>: <a href="https://talkpython.fm/pycharm-advocate-job" target="_blank" rel="noopener">jetbrains.com/careers</a><br/> <b>Watch this episode on YouTube</b>: <a href="https://www.youtube.com/watch?v=LCva0NOM2-o" target="_blank" rel="noopener">youtube.com</a><br/> <b>Episode transcripts</b>: <a href="https://talkpython.fm/episodes/transcript/400/ruff-the-fast-rust-based-python-linter" target="_blank" rel="noopener">talkpython.fm</a><br/> <br/> <b>--- Stay in touch with us ---</b><br/> <b>Subscribe to us on YouTube</b>: <a href="https://talkpython.fm/youtube" target="_blank" rel="noopener">youtube.com</a><br/> <b>Follow Talk Python on Mastodon</b>: <a href="https://fosstodon.org/web/@talkpython" target="_blank" rel="noopener"><i class="fa-brands fa-mastodon"></i>talkpython</a><br/> <b>Follow Michael on Mastodon</b>: <a href="https://fosstodon.org/web/@mkennedy" target="_blank" rel="noopener"><i class="fa-brands fa-mastodon"></i>mkennedy</a><br/></div><br/> <strong>Sponsors</strong><br/> <a href='https://talkpython.fm/cox'>Cox Automotive</a><br> <a href='https://talkpython.fm/userinterviews'>User Interviews</a><br> <a href='https://talkpython.fm/training'>Talk Python Training</a>
Categories: FLOSS Project Planets

Codementor: Mistakes which beginners make while choosing a Programming Language

Wed, 2023-01-25 01:39
Try avoiding this mistakes...
Categories: FLOSS Project Planets

PyCoder’s Weekly: Issue #561 (Jan. 24, 2023)

Tue, 2023-01-24 14:30

#561 – JANUARY 24, 2023
View in Browser »

How to Get and Use the Current Time in Python

In this tutorial, you’ll be getting the current time in Python. You’ll get your hands on a datetime object that represents the current time. You’ll see how to format it according to international standards, and you’ll even check out how computers represent time.
REAL PYTHON

Property-Based Testing With Hypothesis and Pytest

This tutorial is a gentle guide to property-based testing. Property-based testing is a testing philosophy; a way of approaching testing that will bring you many benefits that other types of testing don’t give you.
RODRIGO GIRÃO SERRÃO • Shared by Rodrigo Girão Serrão

Building an IoT App with InfluxDB Cloud, Python, and Flask

Step-by-step tutorial for building a monitoring app. The prototype is serverless, with zero infrastructure to manage, and free—all built on the free tier of InfluxDB time series platform →
INFLUXDATA sponsor

Boost Your Python Performance Using Continuous Profiling

Learn how to use Grafana Phlare and continuous profiling to discover bottlenecks in you code and boost the performance of your Python applications
MARTIN HEINZ

CircleCI Security Incident: Rotate Your Keys

CircleCI says hackers stole encryption keys and customers’ secrets.
TECHCRUNCH.COM

Discussions What Is Your Favorite Tech Podcasts These Days?

HACKER NEWS

Python Jobs Python Video Course Instructor (Anywhere)

Real Python

Python Tutorial Writer (Anywhere)

Real Python

More Python Jobs >>>

Articles & Tutorials Analyzing Labor Markets in Python With LODES Data

This article shows step-by-step instructions on how to use pandas and pygris to analyze geographical data. The example uses the LODES (LEHD Origin-Destination Employment Statistics) data set, a synthetic data set with US Census block and job workplace data, to map the commute flow to Apple headquarters in Cupertino, California.
KYLE WALKER

Using the Terminal on macOS

In this Code Conversation video course, you’ll learn how to use the terminal on macOS. You’ll navigate the file system with Philipp and Martin and perform common tasks like creating files and folders. If you’ve never used the terminal before, then this video course will help you get started.
REAL PYTHON course

Get Personalized Insight from Python Experts

We are performance tuners, Django and Python experts, infrastructure and scaling architects. We’ve worked with over a hundred companies and teams of all types. We know what works best and which roads lead to failure. Let our experience guide you, your code, and your processes to smoother waters →
REVSYS sponsor

A Not So Unfortunate Sharp Edge in Pipenv

Pipenv keeps hashes of installed projects to help protect from supply chain attacks. Something interesting happened though when a project didn’t change version numbers but did change hashes, learn just what the edge case was and how it complicated build repeatability.
DADE

One a Day One Liners With Python

Three weeks ago Jeremy started writing a mini-blog posting each day about a Python one-liner. This week’s theme is vector and matrix processing. Last week was on text processing.
JEREMY BROWN • Shared by Jeremy Brown

Awesome Design Patterns

This is a curated list of software and architecture design patterns. It has language specific sections as well as general architecture sections such as cloud, big data, devops, IOT, serverless, micro-services, and more.
GITHUB.COM/DOVAMIR

4 Tips for Structuring Your Research Group’s Python Packages

This opinion piece by David Aaron Nicholson gives some great advice about how to name and structure your packages. Life lessons learned by coming across modules that weren’t so elegantly structured.
DAVID AARON NICHOLSON

Authentication With Django and Single Page Apps

Single Page Applications (SPA) often use passed tokens for authenticated pages. This article shows you how to use Django’s session based auth mechanism in conjunction with an SPA.
MIKE SUKMANOWSKY

Using pytest to Test PySpark Code

This article shows you how to run automated tests on your PySpark code using the pytest. It delves into some of the pitfalls and how to make your tests performant.
GARY BAKE • Shared by Gary Bake

Data is the Lifeblood of Modern Businesses

What are the most common mistakes companies make when it comes to managing and protecting sensitive user data? Read the guide now to learn how to move fast without breaking privacy.
SKYFLOW sponsor

Improving Python Packaging: Why 14 Tools Are 12 Too Many

A journey to the world of Python packaging, a visit to the competition, a hopeful look at the future, and highlights from a disappointing discussion.
CHRIS WARRICK

Why Polars Uses Less Memory Than Pandas

Polars is an alternative to Pandas than can often run faster—and use less memory! This article shows you how to go from Pandas to Polars.
ITAMAR TURNER-TRAURING

Projects & Code Trace Your Python Process Line by Line

GITHUB.COM/FURKANONDER • Shared by Furkan Onder

Finds All 3rd-Party Imports in Your Python Project

GITHUB.COM/SAADMK11 • Shared by Maksudul Haque

socketify.py: High Performance Python Web Server

GITHUB.COM/CIROSPACIARI

pybboxes: Light Weight Toolkit for Bounding Boxes

GITHUB.COM/DEVRIMCAVUSOGLU

CustomTkinter: Python UI-library Based on Tkinter

GITHUB.COM/TOMSCHIMANSKY

Events PyKla Monthly Meetup

January 25, 2023
MEETUP.COM

101 Django Rest Framework

January 25, 2023
MEETUP.COM

Heidelberg Python Meetup

January 25, 2023
MEETUP.COM

Weekly Real Python Office Hours Q&A (Virtual)

January 25, 2023
REALPYTHON.COM

SPb Python Drinkup

January 26, 2023
MEETUP.COM

PyDelhi User Group Meetup

January 28, 2023
MEETUP.COM

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

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

Categories: FLOSS Project Planets

Real Python: Python Basics: Object-Oriented Programming

Tue, 2023-01-24 09:00

OOP, or object-oriented programming, is a method of structuring a program by bundling related properties and behaviors into individual objects.

Conceptually, objects are like the components of a system. Think of a program as a factory assembly line of sorts. At each step of the assembly line, a system component processes some material, ultimately transforming raw material into a finished product.

An object contains both data, like the raw or preprocessed materials at each step on an assembly line, and behavior, like the action that each assembly line component performs.

In this video course, you’ll learn how to:

  • Create a class, which is like a blueprint for creating an object
  • Use classes to create new objects
  • Instantiate classes with attributes and methods

This video course is part of the Python Basics series, which accompanies Python Basics: A Practical Introduction to Python 3. You can also check out the other Python Basics courses.

Note that you’ll be using IDLE to interact with Python throughout this course. If you’re just getting started, then you might want to check out Python Basics: Setting Up Python before diving into this course.

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

Categories: FLOSS Project Planets

Python Bytes: #320 The Bug Is In The JavaScript

Tue, 2023-01-24 03:00
<a href='https://www.youtube.com/watch?v=uy04vHILEtY' style='font-weight: bold;'>Watch on YouTube</a><br> <br> <p><strong>About the show</strong></p> <p>Sponsored by us! Support our work through:</p> <ul> <li>Our <a href="https://training.talkpython.fm/"><strong>courses at Talk Python Training</strong></a></li> <li><a href="https://testandcode.com/"><strong>Test &amp; Code</strong></a> Podcast</li> <li><a href="https://www.patreon.com/pythonbytes"><strong>Patreon Supporters</strong></a></li> </ul> <p><strong>Connect with the hosts</strong></p> <ul> <li>Michael: <a href="https://fosstodon.org/@mkennedy"><strong>@mkennedy@fosstodon.org</strong></a></li> <li>Brian: <a href="https://fosstodon.org/@brianokken"><strong>@brianokken@fosstodon.org</strong></a></li> <li>Show: <a href="https://fosstodon.org/@pythonbytes"><strong>@pythonbytes@fosstodon.org</strong></a></li> </ul> <p>Join us on YouTube at <a href="https://pythonbytes.fm/stream/live"><strong>pythonbytes.fm/stream/live</strong></a> to be part of the audience. Usually Tuesdays at 11am PT. Older video versions available there too.</p> <p><strong>Brian #1:</strong> <a href="https://github.com/executablebooks/markdown-it-py"><strong>markdown-it-py</strong></a></p> <ul> <li>Yes. another markdown parser.</li> <li><a href="https://github.com/Textualize/rich/releases/tag/v13.2.0">Rich recently switched markdown parsers</a>, from <code>commonmark</code> to <code>markdown-it-py</code>.</li> <li>Let’s look at those a bit.</li> </ul> <p><strong>Michael #2:</strong> <a href="https://github.com/approximatelabs/sketch"><strong>Sketch</strong></a></p> <ul> <li>via Jake Firman</li> <li>Sketch is an AI code-writing assistant for pandas users that understands the context of your data</li> <li>A Natural Language interface that successfully navigates many tasks in the data stack landscape. <ul> <li>Data Cataloging: <ul> <li>General tagging (eg. PII identification)</li> <li>Metadata generation (names and descriptions)</li> </ul></li> <li>Data Engineering: <ul> <li>Data cleaning and masking (compliance)</li> <li>Derived feature creation and extraction</li> </ul></li> <li>Data Analysis: <ul> <li>Data questions</li> <li>Data visualization</li> </ul></li> </ul></li> <li>Watch the video on the GitHub page for a quick intro</li> </ul> <p><strong>Brian #3:</strong> <a href="https://pythontest.com/fix-circular-import-python-typing-protocol/"><strong>Fixing Circular Imports in Python with Protocol</strong></a></p> <ul> <li>Built on <a href="https://hynek.me/articles/python-subclassing-redux/"><strong>Subclassing in Python Redux</strong></a> from Hynek <ul> <li>We covered this in the summer of 2021, <a href="https://pythonbytes.fm/episodes/show/240/this-is-github-your-pilot-speaking">episode 240</a></li> <li>However, I re-read it recently due to a typing problem</li> <li>Problem is when an object passes itself to another module to be called later. <ul> <li>This is common in many design patterns, including just normal callback functions.</li> <li>Normally not a problem with Python, due to duck typing.</li> <li>But with type hints, suddenly it seems like both modules need types from the other.</li> </ul></li> <li>So how do you have two modules use types from each other without a circular import.</li> <li>Hynek produces two options <ul> <li>Abstract Data Types, aka Interfaces, using the <code>abc</code> module <ul> <li>Requires a third interface class</li> </ul></li> <li>Structural subtyping with <code>Protocol</code> <ul> <li>This is what I think I’ll use more often and I’m kinda in love with it now that I understand it.</li> <li>Still has a third type, but one of the modules doesn’t have to know about it.</li> </ul></li> <li>"<strong>Structural Subtyping :</strong> <em>Structural subtyping</em> is <em>duck typing</em> for types: if your class fulfills the constraints of a <code>[Protocol](https://docs.python.org/3/library/typing.html#typing.Protocol)</code>, it’s automatically considered a subtype of it. Therefore, a class can implement <em>many</em> <code>Protocol</code>s from all kinds of packages without knowing about them!”</li> </ul></li> </ul></li> <li>The <a href="https://pythontest.com/fix-circular-import-python-typing-protocol/">Fixing Circular Imports in Python with Protocol</a> article walks through one example of two classes talking with each other, typing, circular imports, and fixing them with Protocol</li> </ul> <p><strong>Michael #4:</strong> <a href="https://github.com/salabim/unrepl"><strong>unrepl</strong></a></p> <ul> <li>via/by Ruud van der Ham</li> <li><p>We’ve seen the code samples:</p> <div class="codehilite"> <pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="n">board</span> <span class="o">=</span> <span class="p">[]</span> <span class="o">&gt;&gt;&gt;</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span> <span class="o">...</span> <span class="n">row</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;_&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span> <span class="o">...</span> <span class="n">board</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">row</span><span class="p">)</span> <span class="o">...</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">board</span> <span class="p">[[</span><span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">]]</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">board</span>\<span class="p">[</span><span class="mi">2</span>\<span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;X&#39;</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">board</span> <span class="p">[[</span><span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;X&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="p">]]</span> </code></pre> </div></li> <li><p>But you cannot <em>really</em> run this code. You can’t paste it into a REPL yourself nor can you put it into a .py file.</p></li> <li>So you unrepl it: <strong>Copying the above code to the clipboard and run unrepl</strong>.</li> <li>Paste the result and now you can.</li> <li>Unrepl can be used as a command line tool but also as a module.</li> <li>The REPL functionality of underscore (_) to get access to the last value is also supported.</li> </ul> <p><strong>Extras</strong> </p> <p>Michael:</p> <ul> <li>You'll want to update your git ASAP.</li> <li>Get course releases at Talk Python <a href="https://training.talkpython.fm/courses/rss">via RSS</a></li> <li><a href="https://fosstodon.org/@cobler/109718505946298680">Gist</a> for using Turnstile with Python + Pydantic</li> </ul> <p><strong>Joke:</strong> <a href="https://www.youtube.com/watch?v=jxi0ETwDvws">there's a bug in the js</a></p> <ul> <li>You’ve checked all your database indexes, </li> <li>You’ve tuned all your API hooks,</li> <li>You’re starting to think</li> <li>That you might need a drink,</li> <li>Because there’s only one place left to look:</li> <li>…</li> <li>There must be a bug in the javascript</li> <li>Because everything else was built properly</li> <li>But the frontend’s a pile of crap ;)</li> </ul>
Categories: FLOSS Project Planets

Will Kahn-Greene: Bleach 6.0.0 release and deprecation

Mon, 2023-01-23 11:55
What is it?

Bleach is a Python library for sanitizing and linkifying text from untrusted sources for safe usage in HTML.

Bleach v6.0.0 released!

Bleach 6.0.0 cleans up some issues in linkify and with the way it uses html5lib so it's easier to reason about. It also adds support for Python 3.11 and cleans up the project infrastructure.

There are several backwards-incompatible changes, hence the 6.0.0 version.

https://bleach.readthedocs.io/en/latest/changes.html#version-6-0-0-january-23rd-2023

I did some rough testing with a corpus of Standup messages data and it looks like bleach.clean is slightly faster with 6.0.0 than 5.0.0.

Using Python 3.10.9:

  • 5.0.0: bleach.clean on 58,630 items 10x: minimum 2.793s

  • 6.0.0: bleach.clean on 58,630 items 10x: minimum 2.304s

The other big change 6.0.0 brings with it is that it's now deprecated.

Bleach is deprecated

Bleach sits on top of html5lib which is not actively maintained. It is increasingly difficult to maintain Bleach in that context and I think it's nuts to build a security library on top of a library that's not in active development.

Over the years, we've talked about other options:

  1. find another library to switch to

  2. take over html5lib development

  3. fork html5lib and vendor and maintain our fork

  4. write a new HTML parser

  5. etc

With the exception of option 1, they greatly increase the scope of the work for Bleach. They all feel exhausting to me.

Given that, I think Bleach has run its course and this journey is over.

What happens now?

Possibilities:

  1. Pass it to someone else?

    No, I won't be passing Bleach to someone else to maintain. Bleach is a security-related library, so making a mistake when passing it to someone else would be a mess. I'm not going to do that.

  2. Switch to an alternative?

    I'm not aware of any alternatives to Bleach. I don't plan to work on coordinating the migration for everyone from Bleach to something else.

  3. Oh my goodness--you're leaving us with nothing?

    Sort of.

I'm going to continue doing minimal maintenance:

  1. security updates

  2. support for new Python versions

  3. fixes for egregious bugs (begrudgingly)

I'll do that for at least a year. At some point, I'll stop doing that, too.

I think that gives the world enough time for either something to take Bleach's place, or for the sanitizing web api to kick in, or for everyone to come to the consensus that they never really needed Bleach in the first place.

Bleach. Tired. At the end of its journey.

Where to go for more

For more specifics on this release, see here: https://bleach.readthedocs.io/en/latest/changes.html#version-6-0-0-january-23rd-2023

Documentation and quickstart here: https://bleach.readthedocs.io/en/latest/

Source code and issue tracker here: https://github.com/mozilla/bleach

Categories: FLOSS Project Planets

Python Morsels: Reading a CSV file in Python

Mon, 2023-01-23 11:00

You don't need third-party libraries to read CSV file in Python! Python's csv module includes helper functions for reading CSV files, tab-delimited files, and other delimited data files.

Table of contents

  1. Reading a CSV file with csv.reader
  2. Skipping the header row in a CSV file
  3. Mapping CSV headers to columns
  4. Reading tab-delimited data files
  5. Use csv.reader or csv.DictReader to read CSV files in Python

Reading a CSV file with csv.reader

The Python Standard Library has a csv module, which has a reader function within it:

>>> import csv >>> csv.reader <built-in function reader>

We can use the reader function by passing it an iterable of lines. This usually involves passing reader a file object, because files are iterables in Python, and as we loop over them, we'll get back each line in our file (see reading a file line-by-line in Python).

Here we have a CSV file called penguins_small.csv:

species,island,bill_length_mm,bill_depth_mm,flipper_length_mm,body_mass_g,sex Adelie,Torgersen,39.1,18.7,181,3750,MALE Adelie,Dream,39.5,16.7,178,3250,FEMALE Adelie,Biscoe,39.6,17.7,186,3500,FEMALE Chinstrap,Dream,46.5,17.9,192,3500,FEMALE Gentoo,Biscoe,46.1,13.2,211,4500,FEMALE

Let's use Python's built-in open function to [open our file for reading][reading from a file].

>>> penguins_file = open("penguins_small.csv")

Now we can call pass the file object we got back to csv.reader:

>>> penguins_file = open("penguins_small.csv") >>> reader = csv.reader(penguins_file)

When we call csv.reader we'll get back a reader object:

>>> reader <_csv.reader object at 0x7fd34c861930>

We can loop over that reader object to get the rows within it:

>>> for row in reader: ... print(row) ...

When we loop over a csv.reader object, the reader will loop over the file object that we originally gave it and convert each line in our file to a list of strings:

>>> for row in reader: ... print(row) ... ['species', 'island', 'bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', 'body_ mass_g', 'sex'] ['Adelie', 'Torgersen', '39.1', '18.7', '181', '3750', 'MALE'] ['Adelie', 'Dream', '39.5', '16.7', '178', '3250', 'FEMALE'] ['Adelie', 'Biscoe', '39.6', '17.7', '186', '3500', 'FEMALE'] ['Chinstrap', 'Dream', '46.5', '17.9', '192', '3500', 'FEMALE'] ['Gentoo', 'Biscoe', '46.1', '13.2', '211', '4500', 'FEMALE']

Each list represents one row in our file, and each string in each list represents the data in one column in that row.

Skipping the header row in a CSV file

Note that csv.reader doesn't know …

Read the full article: https://www.pythonmorsels.com/csv-reading/
Categories: FLOSS Project Planets

Real Python: Linear Algebra in Python: Matrix Inverses and Least Squares

Mon, 2023-01-23 09:00

Linear algebra is an important topic across a variety of subjects. It allows you to solve problems related to vectors, matrices, and linear equations. In Python, most of the routines related to this subject are implemented in scipy.linalg, which offers very fast linear algebra capabilities.

In particular, linear models play an important role in a variety of real-world problems, and scipy.linalg provides tools to compute them in an efficient way.

In this tutorial, you’ll learn how to:

  • Study linear systems using determinants and solve problems using matrix inverses
  • Interpolate polynomials to fit a set of points using linear systems
  • Use Python to solve linear regression problems
  • Use linear regression to predict prices based on historical data

This is the second part of a series of tutorials on linear algebra using scipy.linalg. So, before continuing, make sure to take a look at the first tutorial of the series before reading this one.

Free Source Code: Click here to download the free code and dataset that you’ll use to work with linear systems and algebra in Python with scipy.linalg.

Now you’re ready to get started!

Getting Started With Linear Algebra in Python

Linear algebra is a branch of mathematics that deals with linear equations and their representations using vectors and matrices. It’s a fundamental subject in several areas of engineering, and it’s a prerequisite to a deeper understanding of machine learning.

To work with linear algebra in Python, you can count on SciPy, which is an open-source Python library used for scientific computing, including several modules for common tasks in science and engineering.

Of course, SciPy includes modules for linear algebra, but that’s not all. It also offers optimization, integration, interpolation, and signal processing capabilities. It’s part of the SciPy stack, which includes several other packages for scientific computing, such as NumPy, Matplotlib, SymPy, IPython, and pandas.

scipy.linalg includes several tools for working with linear algebra problems, including functions for performing matrix calculations, such as determinants, inverses, eigenvalues, eigenvectors, and the singular value decomposition.

In the previous tutorial of this series, you learned how to work with matrices and vectors in Python to model practical problems using linear systems. You solved these problems using scipy.linalg.

In this tutorial, you’re going a step further, using scipy.linalg to study linear systems and build linear models for real-world problems.

In order to use scipy.linalg, you have to install and set up the SciPy library. Besides that, you’re going to use Jupyter Notebook to run the code in an interactive environment. SciPy and Jupyter Notebook are third-party packages that you need to install. For installation, you can use the conda or pip package manager. Revisit Working With Linear Systems in Python With scipy.linalg for installation details.

Note: Using Jupyter Notebook to run the code isn’t mandatory, but it facilitates working with numerical and scientific applications.

For a refresher on working with Jupyter Notebooks, take a look at Jupyter Notebook: An Introduction.

Next, you’ll go through some fundamental concepts of linear algebra and explore how to use Python to work with these concepts.

Understanding Vectors, Matrices, and the Role of Linear Algebra

A vector is a mathematical entity used to represent physical quantities that have both magnitude and direction. It’s a fundamental tool for solving engineering and machine learning problems. So are matrices, which are used to represent vector transformations, among other applications.

Note: In Python, NumPy is the most used library for working with matrices and vectors. It uses a special type called ndarray to represent them. As an example, imagine that you need to create the following matrix:

With NumPy, you can use np.array(), to create it, providing a nested list containing the elements of each row of the matrix:

>>>In [1]: import numpy as np In [2]: np.array([[1, 2], [3, 4], [5, 6]]) Out[2]: array([[1, 2], [3, 4], [5, 6]])

NumPy provides several functions to facilitate working with vector and matrix computations. You can find more information on how to use NumPy to represent vectors and matrices and perform operations with them in the previous tutorial in this series.

A linear system or, more precisely, a system of linear equations, is a set of equations linearly relating to a set of variables. Here’s an example of a linear system relating to the variables x₁ and x₂:

Here you have two equations involving two variables. In order to have a linear system, the values that multiply the variables x₁ and x₂ must be constants, like the ones in this example. It’s common to write linear systems using matrices and vectors. For example, you can write the previous system as the following matrix product:

Comparing the matrix product form with the original system, you can notice the elements of matrix A correspond to the coefficients that multiply x₁ and x₂. Besides that, the values in the right-hand side of the original equations now make up vector b.

Read the full article at https://realpython.com/python-linear-algebra/ »

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

Categories: FLOSS Project Planets

Python for Beginners: Iterate Rows in a Pandas Dataframe

Mon, 2023-01-23 09:00

We use pandas dataframes to handle tabular data in python. In this article, we will discuss different ways to iterate rows in a pandas dataframe.

Table of Contents
  1. Iterate a Pandas Dataframe Using iloc Attribute
  2. Iterate Rows in a Pandas Dataframe Using the loc Attribute
  3. Iterate Rows Using iterrows() Method in Pandas
  4. Conclusion
Iterate a Pandas Dataframe Using iloc Attribute

The iloc attribute in a pandas dataframe is used to select a row at a specified position from the dataframe. The iloc attribute contains the  _iLocIndexer object. Using the _iLocIndexer object, we can select any row from the dataframe using the position of the row and python indexing

To iterate rows in the pandas dataframe using the iloc attribute, we will first find the number of rows in the dataframe using the len() function. The len() function takes the dataframe as its input and returns the number of rows. 

After finding the number of rows, we will use a for loop and _iLocIndexer object to iterate rows of the dataframe as shown below.

import pandas as pd myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}, {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90}, {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70}] df=pd.DataFrame(myDicts) print("The input dataframe is:") print(df) print("The rows are:") num_rows=len(df) for position in range(num_rows): print(df.iloc[position])

Output:

The input dataframe is: Roll Maths Physics Chemistry 0 1 100 80 90 1 2 80 100 90 2 3 90 80 70 The rows are: Roll 1 Maths 100 Physics 80 Chemistry 90 Name: 0, dtype: int64 Roll 2 Maths 80 Physics 100 Chemistry 90 Name: 1, dtype: int64 Roll 3 Maths 90 Physics 80 Chemistry 70 Name: 2, dtype: int64

In this example, we first converted a list of dictionaries to a dataframe. Then, we calculated the number of rows in the dataframe using the len() function. After this, we used a for loop and the iloc attribute to iterate over the dataframe.

Iterate Rows in a Pandas Dataframe Using the loc Attribute

The loc attribute in a pandas dataframe is used to select a row at a specified index from the dataframe. The loc attribute contains a _LocIndexer object. Using the _LocIndexer object, we can select any row in a dataframe using the index of the row and the indexing operator. 

To iterate through rows in the pandas dataframe using the loc attribute, we will first get the list containing the index values using the index attribute of the dataframe. Then, we will use a for loop and the loc attribute to iterate rows as shown in the following example.

import pandas as pd myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}, {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90}, {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70}] df=pd.DataFrame(myDicts) print("The input dataframe is:") print(df) df.set_index("Roll",inplace=True) print("The rows are:") indices=df.index for index in indices: print(df.loc[index])

Output:

The input dataframe is: Roll Maths Physics Chemistry 0 1 100 80 90 1 2 80 100 90 2 3 90 80 70 The rows are: Maths 100 Physics 80 Chemistry 90 Name: 1, dtype: int64 Maths 80 Physics 100 Chemistry 90 Name: 2, dtype: int64 Maths 90 Physics 80 Chemistry 70 Name: 3, dtype: int64

In this example, we have used the loc and the index attribute of the dataframe to iterate through the rows.

The loc attribute can be helpful when we have to iterate through the rows of a dataframe at specific indices. You can select a list of indices and iterate over the rows having those indices using the loc operator without knowing the rows’ position in the dataframe. On the other hand, If you want to iterate through a dataframe using the iloc attribute, you need to know the position of the required rows in the dataframe.

Iterate Rows Using iterrows() Method in Pandas

Instead of using the iloc and loc attributes, you can use the iterrows() method to iterate through rows in a pandas dataframe. 

The iterrows() method, when invoked on a dataframe, returns an iterator object to the rows of the dataframe. After creating the iterator object, you can use a for loop to iterate through the rows of the pandas dataframe as shown below.

import pandas as pd myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90}, {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90}, {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70}] df=pd.DataFrame(myDicts) print("The input dataframe is:") print(df) print("The rows are:") for row in df.iterrows(): print(row)

Output:

The input dataframe is: Roll Maths Physics Chemistry 0 1 100 80 90 1 2 80 100 90 2 3 90 80 70 The rows are: (0, Roll 1 Maths 100 Physics 80 Chemistry 90 Name: 0, dtype: int64) (1, Roll 2 Maths 80 Physics 100 Chemistry 90 Name: 1, dtype: int64) (2, Roll 3 Maths 90 Physics 80 Chemistry 70 Name: 2, dtype: int64) Conclusion

In this article, we have discussed different ways to iterate through rows of a dataframe in python. 

To learn more about python programming, you can read this article on how to sort a pandas dataframe. You might also like this article on how to drop columns from a pandas dataframe.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

The post Iterate Rows in a Pandas Dataframe appeared first on PythonForBeginners.com.

Categories: FLOSS Project Planets

Mike Driscoll: PyDev of the Week: Fabio Pliger

Mon, 2023-01-23 08:25

This week we welcome Fabio Pliger (@b_smoke) as our PyDev of the Week! Fabio is the creator and tech lead of PyScript. You can see what else Fabio is working on over at GitHub.

Let’s spend a few moments getting to know about Fabio!

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

I was born in Rio de Janeiro – Brasil, from a big family of Italian immigrants, where I stayed until I was 15 when I moved to Italy, finished my studies, and graduated in Computer Science.

Videogames were one of my biggest passions growing up and probably the main trigger for my interest in computers even if, unfortunately, I don’t have much time to play these days. Other than programming, computers, and video games, photography is one of my current major hobbies these days.

Why did you start using Python?

I started using Python around 2001/2002 when I was working at my very first gig as a programmer. A friend of my boss at the time had a son who was very passionate (and talented) about Python, so my manager asked me to check it out. We became friends, I fell in love with the language and a few years later we founded the Python Italia Association (with other friends) to promote Python and Pycon in Italy.

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

These days, if I’m not using Python, I mostly program in Javascript or TypeScript. I must say that I definitely enjoy TypeScript more than Javascript. Needless to say, my favorite language remains Python.

In the past, I’ve also worked with Visual Basic, C#, C, and a bit of Caml (which I quite enjoyed) and Java in college but none of them really made me enjoy programming as Python does. I’d definitely enjoy having some time to learn Rust and Go.

What projects are you working on now?

At the moment, I spend most of my time on PyScript and related projects inside Anaconda. More specifically, in PyScript, I’m currently working on its Plugin system that will allow anyone to create pure Python plugins that can extend PyScript functionality and even create new tags. For instance, it’d be really easy to create a new tag (let’s say ) that can interpret Markdown content and translate it to HTML and that can be done in pure Python and in just about 10 lines of code.

Another feature I’m currently starting to work on is a PyScript higher-level interface to the DOM (in Python). It’s currently already possible to interact with the DOM through a very simple API and the native Javascript API (accessible through Pyodide) but we want to create an API that is powerful and yet, very Pythonic and simple to use.

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

Oh.. that’s a really hard question

I can’t name one favorite library. But will name some of my favorites:

  • Conda: when I started using Python (many many maaaaany years ago ) it was such a pain to create Python environments. Conda magic makes it all feel so easy but I still remember the hours and days spent trying to get Python packages and C dependencies to compile on Windows machines, back in the day.
  • Pytest: definitely love writing tests with Pytest. It has a brilliant API and its plugin system is really good. Highly recommend it to anyone who cares about testing their software (which should be everyone
  • Pandas and Numpy: there’s a reason Pandas is so much loved by the data science community. It’s a great framework that allows users to manipulate data and access the power of Numpy with a very accessible interface.
  • Typer and Rich: if you ever had to write a CLI application you probably will love these libraries. They are a mix of simple API that just makes sense and powerful features that would usually require a lot of boilerplate code.
  • FastAPI: from the same author of Typer, FastAPI is a high-performant and modern web framework on top of Starlette and Pydantic (Oh, yeah, I also love Pydantic ) that focuses on user experience alongside performance.

There are many more that I’d love to mention but I realize this list is becoming “way bigger than your one favorite package”. The one common thread you can notice across all these libraries is how easy and user-focused they are. They just make developers’ life much easier with great design and performance.

What’s the origin story of PyScript?

Peter Wang [co-parent of PyScript] and I have been entertaining the idea of democratizing programming and data science for quite some time. A lot of what we do at Anaconda revolves around the idea of making tools more accessible and easier to use. We also often have conversations about current technology, trends, and whatnot.

Python, the most popular programming language in the world today, doesn’t have a very compelling story on the 2 most popular HW and SW systems in the world: phones/tablet devices and the browser. We started to look into it and actually, the eco-system was quite ready for a platform to enable Python on the browser with a simple yet rich interface (with all the progress on browsers, today they are basically a very secure and portable VM and WASM is a very interesting “Virtual OS”). After a few weeks/months of talking about it and doing some research, I started developing a prototype (that got accelerated after Peter was invited to Keynote at Pycon). The rest is history

What are the top 3 things you have learned so far while working on PyScript?

It’s probably a mix of things I’ve learned and confirmation of some things I knew already

  1. If you build something useful, people will come, use and contribute; even if it’s not perfect. It was especially true for when we released the first version of PyScript very quickly for the PyCon announcement. I made several trade-offs that I wouldn’t normally allow (and was extremely worried about) but we were hoping users would love the concept and vision and be forgiven while we paid our tech debt, and that’s what has been happening.
  2. “The web” and Python are 2 very different things. Python in the browser is (and will be for a long time) different: in good and less good ways. WASM (and Python on the browser) are very young technologies and we have a lot to build and learn.
  3. Technology is not the only hard challenge in software development. People and naming things can often be harder. There are lots of great projects out there and we often choose to build yet another version of the same thing instead of building on top of them. If we start communicating more and finding ways to build on top of them we can achieve great things! PyScript is intentionally trying to be additive to the ecosystem rather than competitive.
Is there anything else you’d like to say?

As cheesy as it may sound, I’d really like to thank the Python community. My personal experience is definitely one of these stories that you hear around: “came for the language, stayed for the community”. Yes, Python is great, but its community is just what makes it unique.

Also, WebAssembly is a really interesting and powerful technology. CPython now builds to WASM but there’s so much more we can do to increase support and performance. I really believe that WebAssembly can be the next-gen container technology and it can be what can bring Python to the browser and mobile. As a community Python community should probably start paying a lot of attention to it. That’s why we created PyScript and one of the reasons I invite everyone to try it!

The post PyDev of the Week: Fabio Pliger appeared first on Mouse Vs Python.

Categories: FLOSS Project Planets

Pages