Python Archives - Stackify Thu, 16 May 2024 11:45:19 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.4 https://stackify.com/wp-content/uploads/2023/02/favicon.png Python Archives - Stackify 32 32 Python Tips: 10 Tricks for Optimizing Your Code https://stackify.com/python-tips-10-tricks-for-optimizing-your-code/ Thu, 04 May 2023 18:10:10 +0000 https://stackify.com/?p=40214 Python is a universal language that is widely used in various industries. In this post, we’ll look at 10 tricks that can help you optimize your Python code, regardless of your Python framework. From following coding conventions and meaningful names to using exceptions for error handling and taking advantage of built-in functions, these tips will help you write cleaner, faster and more maintainable code.

Let’s dive deep into each tip with code snippets to help coders working on important projects or side gigs:

Tips for Writing Clean Code in Python

1. Use Built-in Functions and Libraries

Python has a wide range of built-in functions and libraries that can be used to optimize code. For example, use the built-in sorted function instead of writing your implementation of a sorting algorithm.

2. Avoid Using Global Variables

Global variables can lead to unwanted changes in the state of your program and make it difficult to debug. Instead, use local variables whenever possible.

3. Use List Comprehensions 

List comprehensions are a concise way of creating lists in Python. They are faster and more memory-efficient than using loops.

4. Avoiding ‘for’ Loops for Iterating Over a Range of Numbers

Use the range function instead. The range function is faster and generates a sequence of numbers that can be iterated more efficiently.

5. Use Generators 

Generators are a way to create lazy iterators. They can improve the performance of your code by reducing the amount of memory required.

6. Use the ‘map’ and ‘filter’ Functions

These functions can apply a process to each element of a list and filter elements, respectively. They are faster and more memory-efficient than using loops.

7. Use the ‘itertools’ Library

The itertools Library provides a range of functions for working with iterators. It can optimize code by reducing the number of loops and conditional statements.

8. Use ‘numpy’ Library 

A numpy Library is a powerful tool for numerical computing in Python. It provides optimized functions for numerical operations, which can improve the performance of your code.

9. Use ‘Cython’ 

Cython is a Python-like language that can be compiled into C code. Cython provides a way to write code that runs faster than pure Python code.

10. Profile and Optimize 

Use the cProfile module to profile your code and identify bottlenecks. Then, use the techniques outlined in this article to optimize your code and make it run faster.

Following these tips, you can optimize your Python code and make it run faster and more efficiently.

Now, when you get the basics right, you get clean code. When you get the hang of coding in a particular style that’s clean, even if someone else picks up from where they left off, they understand what’s been coded. That’s pretty much what clean code means.

Tricks to Make your Python Code Clean and Precise

Follow PEP 8 

PEP 8 is the official style guide for Python code, and following its guidelines will help you write clean and readable code. Some approaches include using four spaces for indentation, limiting lines to 79 characters and using lowercase with words separated by underscores for variable names.

Use Meaningful Variable and Function Names

Choose descriptive names for your variables and functions, making it easier for others (or you) to understand what they are used for. For example, instead of “a” for a variable, you could use “age.” This makes your code easier to understand and reduces the chance of bugs due to typos or misunderstandings.

Avoid Using try-except blocks for Control Flow

While exceptions are helpful for error handling, they should not be used in your code. This can lead to confusing and hard-to-debug code.

Use docstrings

Document your code with docstrings to describe what your functions, classes and modules do. Docstrings help others understand what your code does and how to use it, and automated tools can also use docstrings to generate documentation.

Keep Functions and Classes Small

Large functions and classes can be challenging to understand and maintain, so keeping them small and focused is best. If a part or type gets too large, consider breaking it into smaller, more concentrated functions or categories.

Use Exceptions for Error Handling

Exceptions provide a transparent and flexible way to handle errors in your code. When an error occurs, raise an exception, and catch it in a try-except block. This separates the error handling code from the normal flow of your program, making it easier to understand and maintain.

Avoid Using Complex Expressions

Complex expressions can be challenging to read and debug, so break them into smaller, more straightforward statements. For example, you could use a for loop instead of a problematic list comprehension. This makes your code easier to understand and reduces the chance of bugs due to typos or misunderstandings.

Use List Comprehensions

List comprehensions are a concise and readable way to generate lists in Python. They can make your code more efficient by avoiding the overhead of loops and make it more readable by expressing the transformation of one list into another.

Test Your Code

Write tests for your code to ensure it behaves as expected and catch bugs early. Tests also make it easier to refactor your code, as you can quickly tell if you’ve broken something by running the tests.

Keep Your Code Organized

Keep your code organized by breaking it into smaller functions and modules and using descriptive names. This makes it easier to understand and maintain and reduces the chance of bugs due to typos or misunderstandings.

Ending Note

In conclusion, these 10 tricks have provided a solid foundation for writing optimized Python code. You can write efficient, readable, maintainable code by following coding conventions, using meaningful names, avoiding global variables and taking advantage of built-in functions and suitable data structures. By using the above coding tricks, you can overcome the python challenges.

Take advantage of list comprehensions and generator expressions, as they are powerful tools for processing and transforming data in Python. Additionally, profiling your code to identify performance bottlenecks and using memory-efficient data structures and algorithms can further improve the performance of your code.

Finally, remember the importance of writing simple, clean and maintainable code. These tips are just the beginning, and there is always room for improvement. So stay curious, and keep learning and exploring new techniques and best practices to become a better Python programmer.

]]>
Best Python IDEs to Use in 2020 https://stackify.com/best-python-ides-to-use-in-2020/ Wed, 15 Jan 2020 15:30:14 +0000 https://stackify.com/?p=27500 According to the 2019 StackOverFlow Developer survey, Python is the fastest growing major programming language, with 41% of software engineers using Python.  With the continuing popularity of Python, choosing the best IDE (or Integrated Development Environment) is important.

Development and testing is becoming increasingly complicated with multiple files and unit tests involved. Life is easier for programmers when they can write, run, and wrangle their code all in the same place. That’s why IDEs or code editors can be so helpful. 

In a nutshell, an IDE is a toolkit designed for handling different components of software development in one place. For example, usually, an IDE includes:

  • Code editor with syntax highlighting, code formatting, or auto-completion
  • Assembly automation tools
  • Testing & debugging tools

To execute all functions and make developers’ jobs easier, Python IDEs need to be fast, clear, and logical. With a wide range of IDEs, it’s often difficult to pick the best tool for your specific solutions. 

So, we compiled a list of the most powerful Python IDEs for various purposes and complexities to help you choose the IDE for your use case. 

Atom

OS: macOS, Windows, Linux

First release date: 2014

Developed by GitHub, Atom is a versatile text editor which uses an electron framework.  It can be utilized cross-platform with a considerable memory footprint. Programmers who want their apps to be lightweight won’t like the 400MB (including its dependencies) install footprint. However, on a modest system, it runs fine. 

With the last Atom update, there have been improvements made to reduce latency time.

Advantages:

Atom’s success is in part due to its highly customizable interface. It is a novice-friendly comprehensive package manager with built-in Git and GitHub integration. Easy to use auto-completion and syntax highlight tools while coding.  Atom also offers almost everything that VC Code does. Atom boasts a well-documented support library and community oriented user experience.

Disadvantages:

The real disadvantages are the sizable memory footprint and excessive RAM consumption. It’s not suitable for handling large code files, and further improvements must be made in optimization and system latency. 

IDLE

OS: macOS, Windows, Linux
First release date: 1998

It is easy for programmers to overlook IDLE. Still, it is one of the best Python IDEs. IDLE is Python’s integrated development environment and, based on that fact alone, worth considering. IDLE is stripped-down compared to some of the other offerings around. It has everything you need, with easy to master features. It could be considered a Python IDE for beginners, yet more experienced coders also utilize it. 

IDLE is coded in Python and uses the lightweight Tkinter toolkit to draw its GUI. 

Advantages:

It has a practical Python shell, where you can play around with code snippets. The keywords and output are nicely colored, which many programmers appreciate.

IDLE supports using both tabs or spaces for indentation. It can automatically convert between the two, and indents multiple lines at a time.

Disadvantages:

IDLE does not have any project management facilities. Though, that isn’t a problem, if your project requires only a handful of files. It has an excellent debugger that allows single-stepping through code. You can also over-step through each high-level function should your project require.

Visual Studio Code

OS: macOS, Windows, Linux

First release date: 2015

Last release date: December 2019

Visual Studio Code works with several platforms, including Linux, macOS, and Windows. It’s Microsoft’s code editor, and the most popular choice among devs with over 50% of engineers having used it for Python programming.

The code features its own debugger and supports linking.  With different source control integrations, it has a built-in terminal and well stocked extensions marketplace.  It can run and debug any project’s unit tests through the unittest, pytest, or nose frameworks.

Advantages:

Visual Studio Code is quite configurable with a settings panel. Each highlighted section unfolds the corresponding section of the settings.json file.

This IDE is a bit bulky. It does have support for Intellisense, Microsoft’s take on code completion, which many coders will appreciate. For Python, in addition to suggesting completions, this setup provides on-the-fly popups showing the documentation for methods and classes.

Visual Studio Code includes more than 4,700 usable extensions On-demand import of keyboard shortcuts from other Python editors, including Atom and Sublime Text make Visual Studio Code is appropriate for experts and amateurs alike. 

Disadvantages:

VS Code is not suitable for handling large code files. It is also difficult to find the right extension for you needs due to the availability of so many.

Eclipse and Pydev

OS: macOS, Windows, Linux

First release date: 

  • 2001 – for Eclipse
  • 2003 – for Pydev

Last release date: 

  • December 2019 – for Eclipse (2019-12)
  • April 2019 – for Pydev

Eclipse is a Python development environment designed for programming in Java. With a system of plugins and extensions, it can be used with other programming languages including Python. 

Pydev is the plugin that lets Eclipse be used as a Python IDE.  It also supports Jython and IronPython. Pydev uses advanced inference techniques, allowing for code completion and analysis. Pydev offers more features such as a debugger, an interactive console, code coverage, and basic syntax highlighting.

Advantages:

Eclipse and Pydev when used as a Python IDE are easy to get started. Moreover, it’s free, and has the code completion and analysis capabilities programmers love. 

Disadvantages:

The real disadvantage is that the user interface is not very enriching. Still, this IDE has so much going for it that this might be forgivable for some coders.

PyCharm

OS: Windows, Linux, MacOS

First release date: 2010

Last release date: October 2019

Developed by JetBrains, PyCharm stands out from the competition with its productivity tools that allow for quick fixes. It is available in three versions – Apache-licensed Community version, the Educational (Edu) version, and a proprietary Professional version. The first two are open source and free, while you must pay for the Professional one.

Advantages:

The Community version has some useful features, such as syntax highlighting, auto-completion, and live code verification. The paid ones have more advanced features, including full database management and a multitude of vital Frameworks.

It works with Linux, macOS, and Windows, and the most recent version was the 2019.2.3. In 2020, look for the much-anticipated PyCharm 2020.1.

The greatest advantage of the PyCharm setup is the active community support. The live code verification and syntax highlighting are excellent features as they can execute edits and debugs Python code without any external requirements. 

Disadvantages:

Most coders don’t like the slow loading time. The default settings can also require adjustments before existing products can be used, which might add extra time to a project.

Sublime Text

OS: Windows, OS X, Linux

First release date: 2008

Last release date: October 2019

First conceived as an extension for Vim, Sublime Text is a generic text editor coded in both C++ and Python. Since the release of version 2.0, this software supports 44 programming languages. 

Advantages:

The first advantage is its discreet and minimal interface allowing you to focus on the text and not a myriad of toolbars. The text is not hidden by windows and uses as much space as possible so you can work with it in full-screen, multi-screen, and side-by-side file editing.

Once installing Sublime Text as a Python IDE, different plug-ins and packages make it a high-quality and powerful tool at your disposal. You can use Sublime Text to develop code easily within Python and  incorporate many of the features of a basic text editor, including customizable syntax highlighting. 

The main advantage of Sublime Text is that it is very fast and has few bugs. You can use it to open large files with support for many languages. 

Disadvantages:

The real disadvantage is that everything has to go through JSON and is difficult to modify. Learning the shortcuts can also be time-consuming.

Vim

OS: Windows, Unix, MacOS

First release date: 1991

Last release date: December 2019

Vim is the 5th most popular IDE online with over 25% of devs using it. It’s an IDE that allows for the manipulation of text files. 

Vim differs from many other Python text editors in its modal mode of operation. It has three basic modes: Insert mode, Normal or Command mode, and Command-Line mode.

Advantages:

Its main advantage is that the software is feature-rich. The automatic indentation is adapted to the language and it offers a highly-regarded user experience with community support. The recognition, support, and conversion of file formats, such as Mac, MS-DOS, and UNIX are also practical.

Disadvantages:

ost programmers agree that the main disadvantage of Vim is its lack of innovative features. It seems rather basic compared to some of the other IDEs on the market.  

Spyder

OS: macOS, Windows, Linux

First release date: 2009

Last release date: December 2019

Originally named Pydee, Spyder is a cross-platform IDE. Under a non-copyleft license, the software is released as open source by the author with permission to redistribute and modify it. Since 2012, the Python community has maintained Spyder through their collective contributions. 

Spyder’s basic features include syntax highlighting and auto-completion. It integrates with many scientific use libraries, such as Matplotlib, NumPy, IPython, and SciPy. Installation is quite easy, due to the Python package manager.

Advantages:

This IDE offers a unique combination of advanced analysis, editing, debugging, interactive execution, and in-depth inspection. It has the visualization capabilities of a scientific software package including interactive console, documentation viewer, variable explorer, and various development tools.

The greatest advantage of Spyder is the community support you get when you use it with development tool features and complete documentation.

Disadvantages:

Execution and optional dependencies aren’t appealing to some programmers.

Rodeo Python

OS: macOS, Windows, Linux

First release date: 2016

Last release date: January 2017

Rodeo python is thought to be the best IDE for machine learning as ML programming scales up in the coming years. Developed by Yhat, Rodeo could be considered an integrated development environment for Python. It was built expressly for data science and machine learning.

Rodeo IDE helps to interact and explore with data and plots. The Rodeo IDE has many similar features to Spyder, and it runs on the IPython Kernel.

Advantages:

What’s ideal about Rodeo IDE is that it comes with built-in IPython support that helps programmers write code faster with features like auto-completion and syntax highlighting.

Rodeo comes with cheat sheets and Python tutorials for reference – perfect for Python beginners. 

Disadvantages:

The main cons of Rodeo are the lack of its memory and overheating problems. However the more significant problem is that this IDE has been abandoned by its developers and will not be updated over time.

]]>
A Look At 5 of the Most Popular Programming Languages of 2019 https://stackify.com/popular-programming-languages-2018/ Fri, 30 Aug 2019 13:02:57 +0000 https://stackify.com/?p=14766 If you’re a software developer, then you probably—every now and then—feel overwhelmed by the super-fast pace at which our industry evolves, and that’s fine. I certainly feel that way sometimes, especially when trying to keep up with the latest trends.

But it’s possible to be well-informed about what’s going on out there, and use that information to your advantage, by being economical about your learning.

Sure, there are lots of programming languages. Sure, new ones keep being created every week—and don’t even get me started on JavaScript frameworks.

Do you need to learn all of them? Of course not.

First, learn about a number of the most popular programming languages. Then, narrow that list down, by picking the ones that make the most sense for your current knowledge level, employment status and other criteria that might make sense for your scenario.

For instance, if you intend to learn the functional paradigm, then pick a functional language from the list. Rinse and repeat.

That’s what this post is about. We’ve done the legwork for you, compiling a list of five of the most popular programming languages for 2019. Now you only have to read it and put it to good use.

A Look At Our Sources

You might be wondering where have we found our data for this post. There are hundreds of metrics to look at when considering the top programming languages, but we focused on a few of the most authoritative, namely Tiobe and GitHub.

Tiobe Programming Index

For decades, Tiobe (the software quality company) has generated an index of the most popular programming languages. They update this list monthly, pulling in data from hundreds of sources around the world.

For more on how the Tiobe Index is calculated, see here.

GitHub

GitHub is one of the largest code repositories in use today. Every year they create a Year in Review report, sharing statistics about their programmer community. We use this data as another indicator of language popularity.

In this post, we also attempt to predict the future of programming languages.

We use a wide variety of resources to predict the fastest growing languages and the most influential languages. Of course, all of these insights are up for debate, but they are worth considering if you want to stay ahead of the curve.

Let’s get into it. Below are the most popular programming languages of 2019, and predictions about the future of code.

Most Popular Programming Languages

The table above shows the top 20 most popular programming languages as of August 2019. The Tiobe Index works like market share; the percentage is the amount of “market share” a language holds.

Tiobe factors in variables like the number of professional developers worldwide, training courses, and third-party vendors.

Most of this information comes from analyzing search engine results. Here is an explanation of how the Tiobe Index is produced.

Last, but not least, we take data from The GitHub Year in Review—which is a report published annually by GitHub.

Using the data from GitHub we can see not only the most used languages on the platform during the year, but also the ones which are growing the fastest.

The chart above shows GitHub’s top languages over time.

The Top Programming Languages, Explained

What makes the top programming languages so popular? We’ll take a deeper look at five of the top languages to learn how they’re used and why people love them.

1. Java

According to Tiobe, Java has been the number 1 or 2 most popular language basically since its creation in the mid-90s. Many of the world’s biggest companies use Java to build desktop apps and backend web systems.

If you know Java, chances are you won’t be desperate for work!

There are a number of factors that make Java so popular:

  1. Portability: Thanks to the platform-agnostic Java Virtual Machine (JVM), Java can run on nearly every system. Java is also the most popular Android language, so the vast majority of Android apps are built in Java.
  2. Scalability: James Governor has a saying: “When web companies grow up, they become Java shops”.Java is built for scalability in mind, which is why it is so popular among enterprises and scaling startups (Twitter moved from Ruby to Java for scaling purposes). Since Java is a statically-typed language, it’s faster and easier to maintain with less bugs. It is also backwards compatible, which means old versions of the language will still run perfectly even after new versions are released. This is a big relief for businesses who would otherwise worry about rewriting their code every time a new version comes out.
  3. Large community: The popularity of Java helps to ensure its future popularity, thanks to a huge community of users. With massive Stack Overflow and GitHub communities, developers can find help on virtually any problem they might encounter. Coupled with its portability, developers know that investing in Java will pay dividends for a long, long time.

If you’re a Java developer, check out the Stackify Retrace and Prefix tools so you know exactly what’s going on with your code. If you’re still learning, check out some of the web’s best Java courses here.

2. The C Programming Language

C is one of the oldest, most popular programming languages, thanks to its near universal portability and early adoption by Tech’s biggest brands, including Microsoft, Apple, Linux, and Oracle.

C is also the most popular language for embedded systems in cars, electronics, and other devices.

Nearly everything that we touch today, from our cell phones to alarm clocks, is influenced by—if not directly written in—the C language.

Why is it still a popular programming language to learn today? First, it’s essentially a portable assembly language. It works with nearly every system and operates about as low to the machine as you can get.

C also has features that make is perfectly qualified for operating systems and embedded systems (like your car’s dashboard). Thanks to its relatively small runtime, C is perfect for keeping these systems lean.

Any programmer will benefit from learning the C language.

Many algorithms written and shared online are done in C. It’s essentially the “universal language” of programming languages. C spinoffs like C++ and C# are also among the top 5 most popular languages, again emphasizing the influence C still has today.

3. Python

The popularity of Python has risen steadily over the past 15 years, finally breaking the top 5 on the Tiobe Index a few years ago. This is because Python is a major language in some of the most exciting technologies today.

Machine learning, artificial intelligence (AI), Big Data, and Robotics all rely heavily on Python (Robotics also relies on C for its use in systems programming). Cyber Security, one of the top software challenges of our time, is also driven by Python.

It’s surprising how simple Python is to learn.

It’s now the most popular introductory language taught in universities and often picked up by experienced developers as a second or third language.

4. JavaScript

Thanks to the ubiquity of web browsers, JavaScript has become one of the most popular programming languages in the world, and number 1 on GitHub in terms of pull requests.

There are notable complaints with JavaScript (more on that in a bit), but JavaScript has held its own against newer languages and will continue to play a significant role on the web.

JavaScript allows developers to add interactive effects to web pages. It often works alongside HTML, but it’s becoming more common for web apps to be built entirely in JavaScript.

Because of its simplicity and speed, more startups and tech businesses are starting to use JavaScript on the backend via the Node.js framework.

5. Ruby

Ruby is one of the most popular languages among tech startups.

Many Silicon Valley unicorns have been built on Ruby, including Airbnb, Twitch, GitHub, and Twitter. Its popularity is bolstered (and perhaps dependent) on Ruby on Rails, a full-stack web application framework that runs Ruby.

Ruby is beloved by developers for a number of reasons.

This explains why startups are so fond of the language: it enables the famous startup mantra, “move fast and break things.”

The downside of Ruby is its scalability.

Ruby is a dynamically-typed language, which makes it very flexible and great for prototypes, but difficult to maintain at scale. As a Ruby app grows, the dynamic nature of the language obscures the source of code errors and eats up computing resources. This is why Twitter switched from Ruby to Java.

Twitter Headquarters Building
“Twitter was originally built using Ruby, but switched to Java so they could scale more easily”. Photo by Aaron Durand

2020 Programming Language Predictions

Now that we’ve looked at the most popular programming languages right now, we’re going to take the liberty of predicting what’s to come in 2020 and beyond.

Based on trends from previous years, we’re confident that the list of top programming languages won’t change that much from year-to-year.

But where are the winds headed? Let’s try to take a glimpse into the future.

Fastest-Growing Languages

Every year, the Tiobe Index crowns the fastest growing language as “Language of the Year”. Recent winners have been Python (2018), C (2017), Go (2016), Java (2015), and JavaScript (2014).

Tiobe will crown a new Language of the Year in the next few weeks, but their website says the candidates are Kotlin and C. It’s an interesting dichotomy with C being of the oldest languages (1987) and Kotlin being one of the newest (2011).

Let’s look at each language.

Kotlin

The couple last years have been great for Kotlin, the statically-typed programming language from JetBrains.

On top of receiving loads of good press, in 2017 Kotlin was also named an official development language for the Android platform.

This is an enormous boost; not only is Android the most popular mobile development platform, but it’s also the 3rd most popular development platform behind Windows desktop and Linux, according to StackOverflow.

And, as you can see in the image above, GitHub’s report indicates that Kotlin was the fastest growing language in 2018.

There are several reasons for Kotlin’s rise in popularity, not least of which is its 100-percent interoperability with Java and the fact that IT runs on Java Virtual Machine (Java is another official Android language).

Kotlin also compiles down into JavaScript, making it extremely versatile for both front and back-end development.

Expect to hear a lot more about Kotlin in the years to come, and if possible, take some time to add it to your repertoire (it’s surprisingly simple).

The C Programming Language

We’ve already discussed why C practically runs the world, but what’s behind this latest boost in popularity?

Like we mentioned earlier, C is the perfect language for embedded systems, and frankly, everything is becoming an embedded system these days.

C is one of the top programming languages for IoT devices, including wearables and car dashboards. As more products become “smart”, we’ll see C’s use continue to expand.

[adinserter block=”33″]

Most Influential Programming Languages

The popularity of a programming language is one thing, but which languages will have the greatest influence in the years to come?

One way to determine whether a language is “influential” is to look at the technologies built on top of it (see Python and C).

Another way is to look at a language’s ability to solve intrinsic software problems. For the sake of treading new ground, let’s focus on the latter definition.

I turned to Jake Ehrlich, a software engineer and programming language enthusiast, for his thoughts on the most influential programming languages.

“Individual problems tend to be more influential than any one specific language,” said Ehrlich. “Right now the biggest problem we’re facing is end of Moore’s law.

Moore’s Law says that computing power will double every 18 months, and for the first time in decades, computer chip makers are not keeping pace. That means software developers need to figure out a way to make powerful web applications with the same amount of computing processing power.

Another hardware issue Ehrlich references is power consumption.

“Batteries just aren’t getting better,” said Ehrlich, despite the fact that more and more of our devices run on them. “So now we need to make hardware and software that is as power-efficient as possible.”

One solution to both of these challenges, Ehrlich suggests, is the use of native languages. “It turns out that the same sort of features that improve speed and responsiveness also allow us to write more power-efficient code.”

Ehrlich thinks we will see a move towards native languages like Go, Swift, Rust as hardware attempts to catch up.

Learn the Most Popular Programming Languages

The world’s biggest challenges and opportunities are driven by programming languages discussed in this article.

While this post is all about the code, it is really an ode to the people who create that code day-in and day-out. From machine learning to cybersecurity and web apps to battery power, these technologies will only go as far as software developers take them.

Try Stackify’s free code profiler, Prefix, to write better code on your workstation. Prefix works with .NET, Java, PHP, Node.js, Ruby, and Python.

Want to learn more about the most popular languages? Here are a few resources to get you started:

Java

C

C++

Python

C#

JavaScript

]]>
Python Geocoder: A Guide to Managing Locations in Your Apps https://stackify.com/python-geocoder-a-guide-to-managing-locations-in-your-apps/ Fri, 28 Jun 2019 19:02:10 +0000 https://stackify.com/?p=25523 A great thing about building applications for the internet is that people from all around the world can benefit from your effort. You can gather new users from Taiwan to Colorado and meet their needs just as effectively. In this global context, it can be good to provide your users with local flavor to help them feel connected to you and your applications. It can also be useful for you to know where your users are coming from to make sure that your infrastructure is configured in the best way.

In today’s post, we show you how to get started with Python geocoder using free services and the requests module to gather and leverage data.

What is geolocation?

Geolocation is the process of identifying the geographical location of a person or device by means of digital information. You use some data you have to gather more data for the benefit of your users. This can be important when dealing with information for users from the EU, for example—it can help you comply with laws around data protection. Identifying where your users are can also help to provide a more bespoke and effective service.

A quick note: this is a Python guide, so I’m going to make a few assumptions. I’ll assume that you’re using Python 3, you have it installed, and  you know the basic syntax. Nothing we’ll cover here is very complicated, but hopefully it’ll be quite interesting. If you’re having trouble getting started, have a look at the  Python site.

Where are they?

One of the key pieces of information you can use to find out where your users are is their IP address. It’s possible for users to use a proxy service to seem to be from somewhere else. Having said that, for most users this is a helpful approach.

I’ve looked into a number of applications that provide this service and have decided to use  ipstack for this tutorial. If you sign up for their free account, you get 10,000 calls per month, which is more than enough to get started.

I think the API for this service is quite straightforward. Once you’ve signed up, you’ll be taken to a dashboard and get an API key. This key is used to identify your application to the service, allowing you to make the calls. Keep this information safe and private, because anyone can use it and pretend to be you. That might not be a huge deal for this free service,  but it’s a good habit to get into.

Let’s see how good the service is at finding you. Get your IP address (get it in plain text by  going here), open a new tab in your browser, and paste the link below. Of course, make sure you swap out the relevant details.

http://api.ipstack.com/YOUR-IP-HERE?access_key=YOUR-API-KEY-HERE

If everything has gone correctly, you should get a response that shows the location of your IP address—great stuff! Now you know where you are, although hopefully this wasn’t news to you. It isn’t going to be practical for you to do this manually in the browser for every user. Given that, we’re going to use the Python requests module to make the same call and collect the information for us.

Using Python to call APIs

import requests
key = "YOUR-API-KEY"
ip = "YOUR-IP-ADDRESS"
url = "http://api.ipstack.com/" + ip +"?access_key=" + key
response = requests.get(url).json()
print(response)

Here, I’m declaring the key and IP address as variables and building up the URL using those components. I then use the requests module to carry out an HTTP request on that URL and convert the response to a JSON array. In Python, this is a dictionary, so you’ll be able to access any of the individual pieces of information by adding the key name. For example, response[‘country_name’] will tell you what country the IP address is based in.

If this was part of a larger application, I’d want to declare the key in a configuration file instead of cluttering up this code. Also, I’d like a function to pass the IP address to and get some information back. You can see from the response all of the information that you could get, but for my purposes, I’m going to get the longitude and latitude of the city closest to the IP address. I’ll improve the code above by using a function.

def lng_lat_from_ip(ip):
url = "http://api.ipstack.com/" + ip + "?access_key=" + key 
response = requests.get(url).json()
return (response['longitude'], response['latitude']) 
longitude, latitude = lng_lat_from_ip(ip)

Awesome! Now I have a function that can take the IP address and return the map coordinates. You can alter this function to return any of the keys that you’d prefer to discover or use.

How far is that from you?

Once you have the user’s location, you can decide how you might adapt their experience. You may store their user data differently, or you may want to do something more fun.

I’m going to work out how far my users are from me. To do that, I’m going to use a module called geopy. Let’s install it!

pip install geopy --user

Once you’ve installed the module, you can use it straight away. You don’t need to sign up for a service to access this functionality. I’m based in Brighton, England. I’m going to use my details to find out how far my users are from here.

from geopy.distance import geodesic
Brighton = (longitude, latitude) # I used our function above to get this.
cleveland_oh = (41.499498, -81.695391) # I looked this one up.
print(geodesic(Brighton, cleveland_oh).miles)

This will print the distance in miles from Brighton to Cleveland. You can change the units to meters or feet if that’s more relevant. I can now let a user from Cleveland know that I am 8,336 miles away from them. You can read more about the other possibilities in the  documentation. There are two ways to measure distance implemented—geodesic and great-circle—and the documentation discusses them both. In my use case, there was no difference, but you may find that one is better than the other and you need the increased accuracy.

What’s going on there?

Another free and useful service that you can sign up for,  GeoNames, allows you to access a wide variety of location-based data. The daily call limit to this service is 20,000, so this should be more than enough for any use case. One thing to look out for is that once you’ve registered, you need to enable “Free Web Services.” This is a link at the bottom of the login page.

I find this to be a really useful service and have used it to tell the weather where my users are and even find some interesting local  Wikipedia articles.

Let’s find out what the weather is like. We’ll start, as before, with testing in our browser, and then we can write the Python function to do the heavy lifting for us.

The good thing is, you have a function to find your latitude and longitude from your IP address, so you can use that to gather the information. According to the weather information, there are few clouds where I am.

By now, you can probably write the function yourself to carry this out for you. I’m going to construct the URL, use the requests module to make the call, and then return the output.

import requests
username = "YOUR USERNAME"
def weather_from_lat_lng(lat, lng):
url="http://api.geonames.org/findNearByWeatherJSON?lat=" + lat + "&lng=" + lng + "&username=" + username
response = requests.get(url).json()
return response
print(weather_from_lat_lng("41.499498", "-81.695391"))

As before, I’m declaring my username in code here, which isn’t a best practice. You could use separate configuration files or even environment variables to keep your keys safe. You can call this function and give a visual indication of cloud cover on your site. Or, less frivolously, you could track if there’s any correlation between the weather and use of your application.

Where next?

I’ve just scratched the surface of the things you can do with geolocation in this guide. The GeoNames library has a lot of  free services I haven’t mentioned here, but the documentation is good and you’ve got everything you need in this guide to be able to leverage them in your applications.

If you’re collecting logs and metrics with a service such as Retrace, this location data would be useful to gather. You can see if there’s a spike in location-specific traffic at unusual times. This can allow you to track down problems or security issues more quickly.

Start Free Trial
]]>
AWS Lambda with Python: A Complete Getting Started Guide https://stackify.com/aws-lambda-with-python-a-complete-getting-started-guide/ Sat, 22 Jun 2019 14:15:39 +0000 https://stackify.com/?p=25386 In this post, we’ll learn what Amazon Web Services (AWS) Lambda is, and why it might be a good idea to use for your next project. For a more in-depth introduction to serverless and Lambda, read AWS Lambda: Your Quick Start Guide to Going Serverless.

In order to show how useful Lambda can be, we’ll walk through creating a simple Lambda function using the Python programming language. We’ll test it out, as well as take a look at what Lambda provides for metrics and logging.

By the end of this tutorial, you’ll be ready to start integrating other AWS serverless frameworks using Python Lambda functions as the glue to bind them all together. Note that the usage of the term Lambda here is not related to anonymous functions in Python, which are also known as lambda functions.

Serverless, or Why you don’t need a server!

In a traditional cloud usage model, you provision servers, deploy code to the server, and manage resource usage and scaling, along with other traditional server activities.

While this is still the right way to handle a lot of scenarios, sometimes you just need to run a bit of code to handle some kind of event. For example, let’s say you have an application that allows users to upload a photo for a profile image. The application stores these images in Amazon S3. Now, say you want to resize the image to a max resolution. You could most definitely handle this task using the traditional model, but since this code runs based on an event (the file upload), Amazon S3 can fire an event and trigger the execution of code to handle the image resize.

Where does this piece of code live, if we aren’t deploying a server? Well, that’s where AWS Lambda comes into play.

AWS Lambda is the glue that binds many AWS services together, including S3, API Gateway, and DynamoDB. Alexa Skill Kits and Alexa Home also have events that can trigger Lambda functions!

Using a serverless architecture also handles the case where you might have resources that are underutilized, since with Lambda, you only pay for the related execution costs. In many cases, this approach can be cheaper than provisioning and running code in the traditional cloud model. Amazon also handles all the resource scaling and load balancing!

At this point, I’m sure you’re ready start writing your first Lambda function. So let’s get to it.

Start by creating your first Python Lambda function

Create the Lambda function on the AWS Lambda homepage by clicking the Create a Function button.

AWS Lambda Create Function

This brings us to the function creation screen where we have a few items to configure, before our function is created:

  • Author from scratch
  • Function name
  • Runtime

For Permissions, Lambda will automatically create a basic execution role so the Lambda function can access CloudWatch for logs. We’ll just leave it at that for now.

Go ahead and name the function something glorious, and then we’ll be all set to create our function.

No, wait! What? Node.js? This is a Python tutorial! No Node allowed.

Set this drop-down to the correct value of Python 3.7. Now go ahead and click the Create function button, which will bring you to the function configuration screen. This can be a bit overwhelming at first, but we’ll briefly cover a few of the essentials here.

Also Read-https://stackify.com/aws-lambda-with-node-js-a-complete-getting-started-guide/

Understanding the Lambda function configuration screen

This view shows two tabs: Configuration and Monitoring. We’ll get into the monitoring section later in this article, when we learn about viewing activity and CloudWatch logs. For now, we’ll focus on the first portion of the configuration screen.

The first area that you’ll see is the Designer view. This view is a handy visual representation of which triggers are utilizing this function, as well as the resources that the function has access to. By default, Lambda adds a resource for Amazon CloudWatch for function logging.

We aren’t using any triggers for this simple function, but triggers can be added by clicking on the trigger in the left column. The designer view will update to show the trigger.

As an example, in the following screenshot, I’ve added triggers for the API Gateway and Alexa Skills Kit.

myFirstLambdaFunction

If you’re curious about what a layer is, here’s what you need to know: a layer essentially allows you to add additional dependencies or frameworks for your function to use. We won’t be using any layers, but for more information, refer to the AWS Lambda Layers documentation.

The next portion of the configuration screen is where the real action here is, and that’s the Function code section. One of the beautiful things about Lambda is the ability to write our function without needing to install anything locally. We can do it all in the browser. Later in this article, you’ll learn how to test the function as well.

lambda function code

If writing the function in the browser isn’t your cup of tea, you can change the Code entry type drop-down and upload a zip file containing the code or give it a URL to the file on Amazon S3. For our purposes, we can stick with the inline code editor.

The code that Lambda generates for us is its version of the venerable Hello, World! program.

import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

This code imports the JSON Python package and defines a function named lambda_handler. This is important, as when an event trigger occurs, Lambda needs to know what to execute.

This function entry point is defined in the Handler field. The format is filename.handler_name. In our example, the filename our code resides in is lambda_function.py. The function name within this file is lambda_handler.

You can name these whatever you want, but the value in the Handler field needs to be updated to match; otherwise, Lambda has no idea how to execute our function.

As an example, if our function is called handler and it’s in the file named main.py, the Handler value needs to be set to main.handler. The below screenshot shows how this ties together:

lambda code entry type

While you can run this example now as is, there’s one more change to make, for illustration purposes. Add a print statement to the code as in this example:

import json

def lambda_handler(event, context):
    print("Hello from Lambda!")
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Okay. Now we’re ready to run our function.

Next, take the Python Lambda function for a spin

I lied. There’s one last thing we need to do before running, and that’s to save the function. At the top of the screen, click the orange Save button.

Once saved, the function will have a green check next to it in the Designer window:

lambda function designer

Since our function doesn’t take any input, running it is straightforward using the built-in test functionality. Click the Test button to the left of the Save button and Lambda will execute the function.

After the function completes, the screen will update to show the result of the execution, along with additional information such as the execution duration, maximum memory used, and the billing duration.

log output

At the bottom of the window, it also shows the log output from this execution. Let’s introduce an error in our code to see what this screen looks like when things don’t go so well.

Update the print statement in the code so it’s no longer correct by leaving off the closing parenthesis:

import json

def lambda_handler(event, context):
    print("Hello from Lambda!"
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Save the function and click the test button again.

This time, when the function executes, it fails. You’ll once again see information about the execution—and this time, you’ll see the error message and stack trace as well.

lambda execution failed

Next, let’s take a look at the Monitoring section mentioned earlier in this post.

View your function logs using CloudWatch

Let’s free up a bit of screen real estate by removing the error message from the previous run. You’ll do that by clicking the X in the upper right part of the window.

Click on the Monitoring tab, next to the Configuration tab, to view this function’s CloudWatch metrics:

lambda CloudWatch

On this screen, we can see a lot of information, such as the function invocation count, error count, and success rate.

We don’t currently have a lot of data to look at here, so let’s take a look at the CloudWatch logs. Click the View logs in CloudWatch button above the charts to see the log streams for this function. Click on the first log stream in the table to see the log from our recent failure test case.

CloudWatch Logs

Each time our function runs, it creates a new log stream. This makes it easy to analyze what went wrong in a particular failure scenario. The most recent log stream shows the log output that the error message uses.

CloudWatch Log Groups

To see more information for each log event, click the arrow in the left to expand the message:

CloudWatch Log Groups

There’s a lot of other great information that CloudWatch tracks for us that we can’t cover here, but I suggest perusing the Getting Started With Amazon CloudWatch user guide. Now that we have a test function written and working, let’s add the ability to access environment variables so we can connect to a database.

Configure and access environment variables

If you recall from earlier in the article, Lambda allows us to set environment variables when configuring the function. In this section, we’ll set up an environment variable to indicate what release environment our function is executing in, for example, DEVSTAGE, or PROD. We’ll then look at how to encrypt sensitive information in environment variables, which AWS makes very easy with the Key Management Service (KMS).

First, scroll down to the Environment variables section on the function configuration screen. Environment variables are sets of key-value pairs, and we’ll use the ENV key to hold the release environment information.

Your environment variables should look like the following:

KMS environment variables

Now that the environment variables are set, let’s update our function to access the value of ENV.

You access environment variables in Lambda functions the same way as you would anywhere else. In Python, this is through the os.environ module.

Scroll back up and update the Python function to retrieve the values we set, and log the DB_HOST and DB_USER to CloudWatch:

import os

def lambda_handler(event, context):
    DB_HOST = os.environ["DB_HOST"]
    DB_USER = os.environ["DB_USER"]
    DB_PASS = os.environ["DB_PASS"]
    print("Connected to %s as %s" % (DB_HOST, DB_USER))
    return None

The above code first imports the os module. Then, in the lambda_handler function (our main entry point), it uses the os.environ function to access the environment variables, and it stores them in local variables.

Go ahead and save the function. Now, test it out, as we did earlier, by clicking the Test button. After it runs, expand the Execution result section, and you’ll see the log message containing the environment variables DB_HOST and DB_USER:

Lambda execution

If you’re as security conscious as I am, there’s something about this example that doesn’t sit right with you: the setting for the DB_PASS value. It’s in plaintext!

Let’s add a bit of security here and encrypt the environment variable.

Encrypt environment variables? Yes, please!

AWS makes it very easy to encrypt the environment variables using KMS. In order to save time, the example below uses a pre-existing key with the name myLambdaFunctionKey. If you don’t have a key yet, refer to AWS’s Creating Keys guide to create one before moving on.

Now that you have a key, return to the Environment variables configuration screen and expand the Encryption configuration section:

Check the Enable helpers for encryption in transit box, which displays a search box beneath it to select the key to use. Once done, you’ll have something similar to the following:

encryption in transit

Now there’s just one last thing to do, and that’s to actually encrypt the environment variable.

After selecting the key, you’ll notice that a couple of new buttons appear where you define the environment variables:

environment variables

Clicking the Encrypt button for any of these will encrypt the values and mask it from view on the screen. Note that you need to remove the value first and add it back in before the Encrypt button will perform the encryption.

Since this password wasn’t encrypted, we can consider it to be exposed. So let’s change it and then encrypt the new one:

Now our database password is safe and secure. But how do we use it in our function?

AWS provides a KMS client as part of the AWS software development kit. Let’s use that to handle our decryption.

Decrypt environment variables with the AWS SDK

Since the DB_PASS value is encrypted, we need to decrypt it before we use it.

Once again, AWS comes to our aid with the Boto 3 library. Boto is the Python version of the AWS software development kit (SDK). AWS also provides us with an example snippet, which can be seen by clicking the Code button. This will display example code showing how to decrypt the environment variable using the Boto library.

Secrets Snippet

Let’s work the above example in our function and clean it up a bit:

import os
import boto3
from base64 import b64decode

DB_HOST = os.environ["DB_HOST"] 
DB_USER = os.environ["DB_USER"]
 
DB_PASS_ENCRYPTED = os.environ["DB_PASS"]
cipherTextBlob = b64decode(DB_PASS_ENCRYPTED)
DB_PASS_DECRYPTED = boto3.client('kms').decrypt(CiphertextBlob=cipherTextBlob)['Plaintext']

def lambda_handler(event, context): 
    print("Connected to %s as %s" % (DB_HOST, DB_USER)) 
    return None

First, we import the b64decode function from the Python base64 library. We move the retrieval of the environment variables outside of the lambda_handler function since we don’t want the decryption routines running every time the handler is called. Putting the other environment variables outside of the function isn’t necessary, but it just keeps things tidy to have it all in one place.

The meat of this example is lines 11 and 12. The encrypted environment variable is stored in base64, so this is decoded and stored as binary in the cipherTextBlob variable. A KMS client is instantiated through the boto3.client interface, and the decrypt function is called on the cipherTextBlob. The decrypted environment variable is stored in the DB_PASS_DECRYPTED variable just like before.

Finally, we just print the host and user again. Save and run this, and then you can bask in the warm green light of knowing that you’ve made your function much more secure!

Troubleshooting

When initially setting this example up, I ran into a few configuration issues that I had to resolve. Those issues went places there’s just not room for in this article.

If you run into problems, here are some things to do and to know:

  • Check to make sure the function role has VPC access. The execution role set in the function configuration needs to have Amazon Virtual Private Cloud (VPC) access, or the function will time out. Check the role in the AWS Identify Access and Management (IAM) console to add the correct policies.
  • Check to ensure that the correct VPC is selected. The function needs to be in the right network, so double check that the correct VPC (and security group) is selected under the Network section of the function configuration screen.
  • It’s never the codeWell, sometimes it is. When adjusting the code for the last section, I left out the import statement for the b64decode function. I was also looking at the wrong error messages in CloudWatch and wasted a bit of time hunting down the issue. Such is life. Sometimes (most of the time?) it’s the simple things that get overlooked.

At this point, you’re probably wondering where to go next. Or maybe you already have a lot of great ideas. So let’s discuss next steps.

Where to go next?

Now that you’re here, you’ve successfully written your first Lambda Python function and can understand what happens when the function works according to your expectations—and also what happens when it fails. Like most things, however, this barely scratches the surface of the power of Lambda. So what now?

For starters, here are just a few ideas:

Once your applications get more complex, you’ll need additional tooling to help understand performance bottlenecks, as well as to troubleshoot harder-to-identify issues. Stackify created Retrace to address these exact scenarios. Retrace is an application performance monitoring tool that allows you to monitor, profile, and troubleshoot your code.

Also, try Stackify’s free code profiler, Prefix, to write better code on your workstation. Prefix works with .NET, Java, PHP, Node.js, Ruby, and Python.

Since Lambda ties so many different pieces of the AWS ecosphere together, there is no limit with what you can do. So go create something awesome, and welcome to the world of serverless computing!

Start Free Trial
]]>
Node.js vs Python for a Beginner’s Web App https://stackify.com/node-js-vs-python-for-a-beginners-web-app/ Fri, 29 Mar 2019 20:57:34 +0000 https://stackify.com/?p=24330 Learning to build webapps is an exciting process, but it comes with its own set of challenges. As a newer developer, deciding what programming language will bring your big idea to life is a common challenge. There are lots of terrific choices for building webapps on the market. Today, we’ll focus on two of 2019’s most popular options: Node.js vs Python. Spending a bit of time going through these competing options will help you figure out where each excels so you can make the smartest choice for your app.

One language for your project or two?

Let’s get the obvious comparison between Node.js and just about every other programming language out of the way to start. Node.js allows you to code in JavaScript on both the client (in the browser) and the server. This is a serious advantage for Node.js that’s difficult to replicate in other languages. JavaScript is the primary language of the web browser, so you’ll be writing client-side code in JavaScript or a language like it. Coding your app’s server in JavaScript simplifies your learning experience and unlocks the ability to share some code between the client and server. Sharing that code between the client and server can help you manage the complexity of your codebase and app.

Recent years have brought forward projects in Python that compile to JavaScript, aiming to provide this same flexibility to Python developers. One example is Transcrypt, which is actively developed. Young developers should be wary of jumping straight into a program like this while they’re learning. Learn to write standard JavaScript before writing code that will be compiled to JavaScript so you can spot common errors and learn to troubleshoot issues in your client-side code.

Scoreboard: Node.js 1, Python 0

Node.js vs Python

Installing your runtime

Installing Node.js is a simple task, whether you use macOSWindows, or Linux. For most users, it’s as simple as downloading an executable and double-clicking.

Installing Python is often more difficult. Python 2 comes pre-installed on macOS, but you shouldn’t use the installed version for your coding because it’s used by system libraries. Instead, you’ll need to install a new version alongside the existing version. Installing Python on Windowscan also be somewhat tricky. Many tutorials recommend installing a package manager like Chocolatey before getting started, which can be a bit of extra work. If you’re working on Linux, installing Python is no sweat.

Node.js is easier to use out of the box on two out of three platforms, so it scores another point here.

Scoreboard: Node.js 2, Python 0

Library management

Library management is a category in which both languages excel. Pip (which stands for “Pip installs Python”) provides tremendous library management for Python. The maintainers of Pip make sure that it’s fast, stable, and easy to learn in just a few minutes.

NPM (the Node Package Manager) provides package management for Node.js. NPM is one of the largest repositories of software libraries in the world. Like Pip, NPM is fast, well-documented, and a breeze to learn in an afternoon or less.

As your project grows, dependency counts usually grow as well. A good dependency manager saves hours of time and countless headaches.NPM and Pip are two of the best. Both languages score a point in this category.

Scoreboard: Node.js 3, Python 1

Environment Management

Environment management

If you only ever work on one project, environment management isn’t a concern. Most developers find that between work and personal projects, they need to support more than one code repository. When working with a package manager like Pip or NPM, managing more than one codebase can cause serious headaches. What if the version of a library you use for work is different from the version of a library that you use for your personal website? This is where environment managers come into play.

The lead environment manager for Python is virtualenv. Virtualenv is a powerful tool that can manage different Python projects, up to and including installing different versions of the Python interpreter itself. Virtualenv is one of the original environment managers, and it’s been actively maintained for more than a decade. As you’d expect, it’s rock-solid and very easy to learn.

Node.js has a similar tool to Virtualenv called nodeenv. Nodeenv is developed and supported by the same people who make Pip, which is a great recommendation for a software package. Nodeenv comes with a big caveat, though: you need to install Python to make it work. If you’re deciding on Node.js to avoid having to install Python on your computer, using nodeenv would defeat the whole purpose.

Scoreboard: Node.js 3, Python 2

Which is easier to learn?

Learn Node.js vs Python

On the surface, both JavaScript and Python are similarly structured. Both are imperative languages with object-oriented features. Both have terrific IDE support. Python has a barer syntax than JavaScript; the lack of curly braces around classes or functions stands out to most new developers. Python is also whitespace sensitive, meaning that how you indent your code matters. Whitespace sensitivity is a double-edged sword for newer developers. Making sure that your code is always well-indented and organized is an important skill to learn for a new developer, and Python requires you to do that. The dangerous side is that improperly indented code can easily break for no obvious reason, and as a new developer, troubleshooting an issue like this is challenging.

While JavaScript’s syntax is likely to be familiar to many inexperienced developers, Node.js introduces additional advanced topics. Node.js web apps are written in what’s known as an asynchronous, event-driven style, which can be difficult to wrap your head around at first. A new developer learning Node.js has a tall hill to climb to understand advanced concepts like why a call to the database might not have information when the developer expects it to. The event-driven architecture is a very powerful performance boost (we’ll talk about why in the next section), but it poses a real challenge for newer developers.

Additionally, JavaScript has a notoriously thin standard library. NPM is popular in the JavaScript landscape because many things that are standard in other programming languages aren’t included in a normal install of JavaScript. As a new developer, this poses a challenge. You’ll commonly be faced with tasks that are simple in other languages but have no similar tool in JavaScript. Python edges JavaScript slightly here, so it wins a point.

Scoreboard: Node.js 3, Python 3

Which one builds faster websites?

Asking which programming language is faster is a dangerous exercise on the internet! In this comparison, you’ll find little dissension, though. Node.js web apps are mostly faster than Python web apps. The reason is related to that event-driven architecture we talked about in the last section. If you haven’t read the link in the previous section (here it is again so you don’t have to scroll back), you should do that now to truly understand why. Node.js code doesn’t spend as much time waiting on network requests and database queries. This means that it does more work in less time, which means faster server responses and page renders.

Python does have the ability to build asynchronous, event-driven web apps. Modules like asyncio make writing asynchronous Python code similar to writing that kind of code in Node.js. Python doesn’t win a point here, though, because most major Python frameworks don’t have this built in.

Another performance advantage for Node.js is that it runs on the V8 JavaScript engine, which is the same engine used in the Chrome web browser. Google devotes a large number of developers to making V8 as fast as possible, which is good news for Node devs. Node.js is likely to continue to get faster over the next few years. Your web app could be faster in a year without you changing a single line of code! Node.js also has powerful tuning capabilities that boost your already good performance to another level.

Finally, it’s important to remember that just because you choose one language or framework, you’re not guaranteed a fast web app. Monitoring the real-world performance of your app through a tool like Retrace is critical to understanding how your app is actually performing for your users.

Retrace Performance

Scoreboard: Node.js 4, Python 3

Tallying it up

Looking at the scoreboard, Node.js holds a slight lead over Python, and I think that’s accurate. In 2019, both Node.js and Python are attractive options for building a new web app. As someone who’s just learning, you may find that Node.js has a pile of excellent frameworks to choose from. Python has similarly excellent frameworks in Django and Flask. Neither choice is a wrong one, and both languages have thriving communities. If it were up to me to choose which language I’d use to start a new web project, I would lean toward Node.js. But the most important thing is to start turning your dream web app into reality today. Get out there and get coding!

]]>
Comparison: Ruby vs. Python https://stackify.com/ruby-vs-python/ Thu, 03 Jan 2019 15:23:43 +0000 https://stackify.com/?p=23572 Around 1996, when I attended my first programming classes, C++ was the language of choice if you wanted to have a job in this industry. The Internet was young and not as widely available as it is now. Ruby and Python were still in their infancy.

Now, in 2018, both have evolved and matured well enough to be in the top 10 most in-demand programming languages. In this article, I’m going to highlight features and contrasts between Python and Ruby.

Let’s start by listing similarities and differences between the two languages.

Ruby vs Python Similarities

  • They’re available under OSI- and FSF-approved licenses, respectively, so you don’t need to pay license fees to use them or to distribute software made with them.
  • They’re also cross-platform, which is convenient for distributed teams where people might use Windows or Linux on their computers.
  • Ruby and Python are high-level scripting languages; their programs don’t need to be compiled.
  • Both languages are dynamically typed, meaning that you can use a variable without declaring it first.
  • They also support object-oriented programming (OOP) out of the box.
  • Both are available via Lambda functions at Amazon Web Services (AWS).

Ruby vs Python Differences

Use cases

Ruby is common in organizations that create web applications. Programming with Ruby on Rails allows teams to rapidly develop and focus on business processes instead of coding functions from scratch. Rails provides an elegant model–view–controller (MVC) structure, so you can strictly separate your data, user interface, and business functions. Further down the stack, the configuration management software Chef is written in Ruby. So if you need to create a new Chef module, it would be in Ruby.

Django is Python’s most popular MVC framework, but Python is also famous beyond the domain of web applications. An example is the Pandas library, which is useful for data munging and preparation, along with other math libraries such as stats-model and numpy. TensorFlow is popular for machine learning tasks, and Matplotlib is powerful for data visualization. Also worth mentioning is SciPy, which has resources to solve math functions that used to make engineering students sweat.

Flexibility

In this aspect, Ruby has inherited Perl’s philosophy: “There’s more than one way to do it.” So, you will always find many different methods to achieve a task in Ruby. Depending on who’s writing the code, this might lead to unnecessary complexity and obfuscation.

On the other hand, Python follows an approach where simplicity has more value than complexity (see “The Zen of Python“). Hence, its philosophy is, “There should be one—and preferably only one—obvious way to do it.” So, although Python code probably won’t be the most flexible, it has a good chance of being more readable to an inexperienced programmer.

Popularity

Looking at Octoverse 2018 from GitHub, we can see Ruby’s use slowly decaying in the top 10 languages, from fifth place in 2014 to tenth place in 2018. However, Python has steadily kept its popularity, moving from the fourth most used language in 2014 up to the third spot in 2015, where it remained through 2018.

Let’s check out job offers as another criterion: searching remote jobs at Stack Overflow returns around twice as many results when you search for Python instead of Ruby. If you prefer LinkedIn, Python leads by 20% over Ruby. Therefore, if you want a skill set in high demand, Python should be your choice. On the other hand, if you prefer a niche job with higher pay, in the long run, Ruby shows a better salary range at PayScale in the U.S. market.

Reusable code

Publicly available and ready-to-use code is a relevant factor when you need to decide on a programming language. Python calls them “modules,” and they’re available via PyPI, where you can search more than 150,000 modules. On the other hand, reusable code in Ruby is called Gems, and there are close to 150,000 gems. But the differentiating factor is filtering; PyPI allows filtering by categories like “development status,” which is more straightforward than comparing many libraries and manually evaluating their code.

When it comes to packaging and automatic dependency resolution, both systems offer painless installation of dependencies in your environment. An advantage on the Python side is how virtualenv enables you to isolate different versions of the Python interpreter. Ruby has a similar feature through rbenv or rvm, but I found this a bit cumbersome. You can gauge this better considering your particular case and how much reusable code there is for your purpose.

python coiled on a branch

Learning curve

I won’t beat around the bush here: Python is hands-down quicker to learn. The syntax is easier to understand, and it’s more readable to the beginner. But beyond that, you’re going to need to make decisions on what framework to use beyond bare-bones Python.

Ruby might take more time to get used to, but Ruby on Rails has built-in features—like scaffolding and Active Record—to accelerate your development. As soon as you know them, you’ll be able to build a CRUD application with API access in a matter of minutes.

Team and audience skills

Take a few minutes to answer these questions:

  • Are you going to distribute as open-source, or is this codebase going to be private?
  • Do people involved know either of the languages already?
  • Is this going to be delivered as a product or service?
  • Do you plan to hire local or remote teams?

Your answers will determine who is going to operate, contribute to, and maintain this code in the long term. You need to decide how convenient each language is, depending on these answers and your plans with this code you’re going to develop.

Testing and troubleshooting

Testing code as early as possible is important, even more since we learned how shift-left increases teams’ performance. That’s why test-driven development (TDD) is one strategy you could use to your advantage. Although PyTest and unittest exist, I’ve not been able to find a mature framework for behavior-driven development (BDD) in Python. RSpec has this role in Ruby, and behave seems to follow suit, but it doesn’t look as mature as RSpec.

When it comes to troubleshooting and tracing, both languages have capable toolsets, but as an operator, I need to recognize that reading errors are more evident than the unforgiving stack traces in Ruby. There’s another learning curve that’s not obvious to most decision makers. However, if you want to close that gap, Stackify’s Retrace is starting to support Ruby and will start supporting Python in Q1 of 2019.

What about performance?

I didn’t include performance as a deciding factor for several reasons:

  • The initial application code needs to solve problems; optimizations come later.
  • The CPU time billed per hour in the cloud is cheaper than developer time in the form of salaries.
  • If you choose an open source language based on your purpose, odds are there’s already an optimized library for your goals.
  • Optimization problems surface when you already have a product working in the market and you’re already tied to your codebase.

I hope these guidelines allow you to make a well-informed decision, either for your career path or for that project you want to start.

In conclusion, I don’t think you’d make a bad choice picking Python in most cases. But if you’re going to build a web application with minimum time to market, and your team already knows Ruby, then Ruby is the way to go.

]]>
Top 11 Python Frameworks in 2018 https://stackify.com/python-frameworks/ Tue, 28 Aug 2018 14:48:55 +0000 https://stackify.com/?p=21809 Python is an interpreted, dynamically-typed, and high-level language. It also implements the basic concepts of an object-oriented programming language (OOP). It has a unique syntax, which makes it different from other programming languages such as Java, C++, and C. Hence, it is simple and easy to learn the language. Most developers use frameworks to create code and develop applications. The framework provides a defined structure to the developers so that they can focus on the core logic of the application rather than on other elements.

In order to start development with Python, you will require a platform or framework to code. While choosing a framework, remember to consider the size and complexity of your application or project. In this article, we will discuss some commonly used Python frameworks.

Python provides support for a wide range of frameworks. Generally, there are two types of Python framework used while developing applications.

Python Frameworks

Full-Stack Frameworks

The full-stack frameworks provide complete support to developers, including necessary elements such as form validation, form generators, and template layouts. Some of the common full-stack frameworks are:

1. Django

Django, developed by Django Software Foundation, is a full-stack Python web framework. It is an open source and free-to-use framework, released officially in July 2005. It helps developers to create complex code and applications in an easier way, and requires much less time compared to other frameworks. It follows the principle of DRY (don’t repeat yourself) and a model-view-template architectural pattern.

It is widely popular among developers as it has a vast collection of libraries written in the Python language. It emphasizes efficiency, the reusability of components, and less code. Some of the main features of Django are URL routing, object-relational mapper (ORM), authentication mechanism, template engine, and database schema migrations.

Django implements ORM to map its instances to database tables. It provides support for multiple databases such as PostgreSQL, MySQL, SQLite, and Oracle. Hence, it becomes easier for developers to transfer the code from one database to another. In addition, it also provides support for web servers. Because of its amazing features, Django is widely used by most of the renowned companies such as Instagram, Pinterest, Disqus, Mozilla, The Washington Times, and Bitbucket.

2. Web2py

Web2py, developed by Massimo De Pierro, is a cross-platform web application framework written in Python programming language. It is an open source and free-to-use Python web framework, released in September 2007. It enables users to create dynamic web content in Python. The Web2py framework comes with a code editor, debugger, and deployment tool with which you can develop and debug code, as well as test and maintain applications. It incorporates a ticketing system, which issues a ticket to the user whenever an error occurs. This ticket helps the user to track the status of the error.

Some of the main features of the Web2py Python framework are:

  • Cross-platform framework that provides support for Windows, Unix/Linux, Mac, Google App Engine, and many other platforms.
  • No additional installation and configuration.
  • Built-in components to handle HTTP requests, HTTP responses, cookies, and sessions as well.
  • Ability to read multiple protocols.
  • Security to data against all possible threats such as cross-site scripting, injection flaws, and execution of infected files.
  • Follows model-view-controller (MVC) pattern.
  • Support for role-based access control and internationalization.
  • Allows users to embed jQuery for Ajax and UI effects.

3. TurboGears

TurboGears, developed by KevinDangoor and Mark Ramm, is a full-stack web application framework. It is a data-driven, open source and free-to-use Python web framework. With the help of components such as WebOb, SQLAlchemy, Genshi, and Repoze, you can easily develop applications that require database connectivity much faster as compared to other existing frameworks.

Some of the main features of TurboGears are:

  • Support for multiple databases.
  • Follows an MVC pattern.
  • Support for web servers such as Pylons.
  • Numerous libraries.
  • WSGI (Web Server Gateway Interface) components. For example, it uses ToscaWidgets, which enable developers to embed any complex widget in their application.

4. CubicWeb

CubicWeb, developed by Logilab, is an open source, semantic, and free-to-use Python web framework. This framework is based on the data model. You are required to define the data model in order to get a functional application. It uses the cube in place of using separate views and models. Multiple cubes are joined together to create an instance with the help of some configuration files, a web server, and a database.

Some of the main features of CubicWeb are:

  • Multiple databases, security workflows, and reusable components.
  • Support for Web Ontology Language (OWL) and Resource Description Framework (RDF).
  • Embeds Relational Query Language (RQL) in order to simplify the queries related to data.

5. Giotto

Giotto is a Python framework that is based on the MVC (Model View Controller) pattern. It separates Model, View, and Controller elements in order to ensure that the web designers, web developers, and system administrators can perform their functions independently and effectively.

Apart from this, it also incorporates controller modules, which enables users to create applications on top of the web, irc, or command line.

6. Pylon

Pylon, developed in December 2010, is a lightweight Python web framework. It places emphasis on the rapid development of applications. It is developed with some of the best ideas taken from languages such as Ruby, Python, and Perl. Hence, it provides a highly flexible structure for web development.

Note: The Pylon framework is in maintenance mode, after being merged with Pyramid to make Pylons project in order to develop Python-based web technologies.

Non-full-stack Frameworks

The non-full-stack frameworks do not provide additional functionalities and features to the users. Developers need to add a lot of code and other things manually. Some commonly used Python frameworks are:

7. Bottle

Bottle, developed by Marcel Hellkemp, is a microframework. It is an easy-to-use lightweight framework generally used to build small web applications. It creates a single source file of every project or application. It has no other dependency than Python Standard Library.

Some of the basic features of the Bottle framework are:

  • Built-in HTTP server.
  • Adapters for third-party template engines and WSGI/HTTP servers.
  • Allows users to access form data, file uploads, cookies, and other HTTP-related metadata in a much simpler way.
  • Provides request-dispatching routes having URL-parameter support.
  • Support for plugins of different databases.

8. CherryPy

CherryPy is an open source object-oriented Python framework. Remi Delon is known as the founder of the CherryPy project. The CherryPy framework is widely implemented by developers to create Python web applications. It has its own multi-threaded web server.

You can create applications using CherryPy that will run on any Python-supporting operating systems such as Windows, Linux/Unix, and macOS.

Some of the common features of CherryPy are:

  • Contains an HTTP/1.1-compliant, WSGI threaded-pooled web server. It provides support for different web servers as well, for example, Apache and IIS.
  • Allows you to run several HTTP servers simultaneously.
  • Contains some tools for events such as caching, encoding, authorization, etc. by default.
  • Support for profiling, testing, and coverage by default.
  • Built-in plugin system.

9. Flask

Flask, developed by Armin Ronacher, is a powerful Python web application framework. It is generally termed a microframework because it does not have the following elements:

  • Specific tools and libraries
  • No database abstraction layer
  • No form validation

The functionalities provided by the above-mentioned elements are now provided by third-party libraries. It depends on the Werkzeug WSGI toolkit and Jinja2template. Some of the common features of the Flask framework are:

  • Built-in development server and debugger also.
  • Support for unit testing.
  • Incorporates RESTful request dispatching.
  • Establishes secure client-side sessions.
  • Compatible with Google App Engine.

10. Sanic

Sanic is an easy, open source, and simple Python framework. This framework is similar to Flask in function but it is much faster comparatively. It was specially designed for quick HTTP responses with the help of asynchronous request handlers.

A remarkable record was made during a benchmark test performed using the Sanic framework. It was recorded that Sanic framework has the potential to process 33,342 requests in a second. This statistic is enough to display how fast Sanic is.

11. Tornado

Tornado, developed by Ben Darnell and Bret Taylor, is a Python web application framework. Initially, it was developed for a company named FriendFeed, which was later taken over by Facebook in 2009. Tornado is an open source framework and is generally known for its high performance. It uses non-blocking network I/O with the ability to handle more than 10,000 connections at a single time.

Some of the main features of the Tornado framework are:

  • Support for user authentication by default.
  • Provides high-quality output.
  • Non-blocking HTTP client.
  • Allows you to implement third-party authentication and authorization schemes, such as Google OpenID/OAuth, Facebook login, Yahoo BBAuth, and Twitter OAuth.

Which Python framework is the best?

Each framework has pros and cons and every developer has different coding styles and preferences. We hope you find a framework on our list that is valuable to you and your team.

Keep your Python applications running smoothly with Stackify’s APM tool Retrace.  Download your free two week trial today!

]]>
Java vs. Python: Coding Battle Royale https://stackify.com/java-vs-python/ Tue, 12 Jun 2018 13:34:13 +0000 https://stackify.com/?p=19279 While we all started to learn how to code with HTML, developing a sophisticated app requires a more advanced language. Java and Python are two of the hottest programming languages in the market right now because of their versatility, efficiency, and automation capabilities. Both languages have their merits and their flaws, but the main difference is that Java is statically typed and Python is dynamically typed.

They have similarities, as they both adopt the “everything is an object” design, have great cross-platform support, and use immutable strings and deep standard libraries. However, they have plenty of differences that steer some coders towards Java and others towards Python. Java has always had a single large corporate sponsor, while Python is more distributed.

Here’s how the two languages are different and how to decide which one better fits your goals.

Pros and cons

The phrase “dynamically typed” means that Python performs type checking at runtime, while statically typed languages like Java perform type checking at compile time. Python can compile even if they contain errors that would prevent the script from running properly. On the other hand, when Java contains errors, it will not compile until the errors have been fixed.

when Java contains errors, it will not compile until the errors have been fixed

Java also requires you to declare the data types of your variables before using them, while Python does not. Because it is statically typed, it expects its variables to be declared before they can be assigned values. Python is more flexible and can save you time and space when running scripts. However, it can cause you issues at runtime.

Choosing a language boils down to what you’re trying to achieve with your code. Performance is not of the essence in software at all times, but it is always worth keeping in mind. Java is more efficient when it comes to performance speed thanks to its optimizations and virtual machine execution.

You can add Python implementations without this restriction, but they can negatively affect portability assumptions within the Python code. Therefore, when it comes to sheer speed performance, Java has the edge.

However, Python is more effective when it comes to adapting legacy systems. The language is more suited to make changes to an existing legacy system. Python can make gradual shifts instead of fully rewriting and revamping the system like Java does.

Java in the enterprise world is a more verbose coding style, which means that these systems are often larger and more numerous than Python legacy. The latter language is more common among enterprise code that glues their IT infrastructure together, making it more effective in adapting legacy systems.

As far as practical agility goes, both languages have their pros and cons. Recent advancements in DevOps have benefited both as Java thrives on more consistent refactoring support. This is due to the language’s static type system, which makes automated refactored more predictable and reliable.

Meanwhile, Python’s dynamic system relies on brevity, fluidity, and experimentation in code, giving it more versatility than Java’s rigid style. Python has also been adapting to automated testing in modern development, but this happens more frequently in integration instead of unit testing.

Choosing which language to goes with depends on your company’s needs, and which setbacks you’re willing to accept. While Java churns out higher performance speed, Python is more suited to evolve legacy systems. When it comes to practical agility, Java is a more proven option, while Python has more flexibility for experimentation.

Python Is easier for beginners

Java is still king in the coding space as it remains the most popular language. It has traditionally been the language to teach beginners, but this is quickly changing as Python is gaining steam. A recent survey from the Association for Computing Machinery (ACM) found that Python has surpassed Java as the most popular language to introduce students to programming.

The survey found that eight of the top 10 computer science departments in the U.S. now use Python to teach coding, as well as 27 of the top 39 schools. Python’s becoming more popular in the academic world for only three or four years as it is being taught as early as high school and through college.

There are several reasons for this shift, including that Python is a general-purpose language, which means that it can be used to build almost anything. The language is great for backend web development, data analysis, artificial intelligence and scientific computing. In addition to its professional uses, it can also be used to create games, productivity tools, and desktop apps.

Python programming language example

Since its inception, Python was created so it would be easy to understand and easy to use as the name came from Monty Python. It is more beginner-friendly because it reads like English, making it a more intuitive learning experience from a syntactical standpoint. The language takes a load off coders by taking care of a lot of the language’s complexity.

Plus, Python is very flexible because it is dynamically typed. While Java has hard rules on how to build features, Python offers different methods to solve the same problem. The language is also more forgiving of errors, allowing you to compile and run your program easily.

One major downside for beginners is that Python can be difficult to maintain as errors are hard to track down and fix. The rigid nature of Java ensures that you fix every problem in real time, saving you time fixing code later down the line. Python is also slower because of how flexible it is, slowing its performance down, making Java more attractive in that sense.

While Java certainly has its benefits and it can do wonders for experienced coders, Python makes the most sense for beginners. It is more flexible, has a more intuitive user interface and it creates a more enjoyable coding experience.

Is the future with Java or Python?

Both languages have large communities surrounding them and they’re both open source. This means that coders are constantly fixing bugs with the languages and updating them, making both viable coding options for the future. As things stand, Java is the most popular programming language in the world, while Python is top-five.

Java programmers have Java User Groups (JUGs), which are some of the most popular coding communities in the world. They also have JavaOne, a high-profile programming event that shows no signs of slowing down. Meanwhile, Python has 1,637 user groups in 191 cities and 37 countries with more than 860,000 members. The language has events as well, including PyCon and PyLadies for women to meet and code together.

Learning either language will help you get a job in computer science, but predicting which trend will go further in the future is hard to do. There will always be coders with different preferences, with Java attracting those who prefer a more straightforward language. Coders who wish to have more flexibility coding, such as data scientists on a machine learning project, will prefer Python.

There are different jobs for each of these languages, but it’s worth noting that Python may be making more strides than Java at the moment. Python tools such as GREENLETS and GEVENT allow asynchronous IO capabilities with a threading-like programming style. This means that the language can be written in twisted code without hurting the brains of its users by relying on stack-switching assembly code for greenlets.

There’s also Kivy, a Python tool that makes it easier to write mobile applications. The language moves away from traditional web technologies, making it an exciting option for the future. With the language, you could talk to telco gear through a custom C extension. Python’s recent update fixes error messages, the ability to modify PATH in the Windows installer, and other features to make things easier for coders.

Python has the slight edge over Java as far as the future goes, but neither language is perfect and Java adopters will continue to try to perfect the language moving forward.

Try Stackify’s free code profiler, Prefix, to write better code on your workstation. Prefix works with .NET, Java, PHP, Node.js, Ruby, and Python.

The best language for you

We can’t tell you which language to choose, but rest assured that both languages will be relevant for years to come. Python is a good choice for beginners as the language is more intuitive and its syntax is similar to the English language. It’s also in the midst of a revolution as its open source nature is paving the way for a slew of new tools to improve it.

Java has plenty to offer as open source as well as it deals with performance issues more resolutely. Choosing a language ultimately comes down to preference as Java is geared more towards perfectionists looking to build clear, consistent code using complex syntax. Some will prefer this system, while others prefer to have the flexibility, brevity and fluidity of Python.

With support for both Python and Java, improve your application performance with Stackify Retrace.  Try your free two week trial today

]]>