Tuesday, April 25, 2017

Swift From Scratch: Delegation and Properties

How to be an Online Session Musician: Part 1

How to be an Online Session Musician: Part 1

A session musician is someone who’s paid to add their instrumentation to a recording or live event on a casual basis. In terms of recording you’d previously go to a studio, often in a major city. 

Whilst this still happens with the advent of the internet, plus affordable recording equipment, session musicians can now work from home and with anyone anywhere in the world. 

This has opened up the industry but increased competition, In simple terms, there’s more work to go around but more people than ever with whom to compete.

In this tutorial I’ll outline the practicalities you’ll need to consider to become an online session musician. 

Qualification

This almost goes without saying, but let’s be clear: this isn’t a job for beginners. 

You either need to be completely amazing at one style or facet of playing, or a musical chameleon, the sonic equivalent of a Swiss Army knife. 

Unless you’re a specialist, someone who can build a reputation as the go-to person for a particular kind of playing, you’ll have to be prepared to turn your hand to whatever’s thrown at you. 

That said…

Skill

It’s tempting to take the scattergun approach, and offer or agree to everything that’s thrown your way, especially in the early days. However, you run the risk of either declining a lot of enquiries, or worse, delivering sub-standard work because a job didn’t really fit your skill set. Neither will enhance your reputation.

It’s better to be very clear from the start as to what you’re offering. If you’re not sure how to describe your skills, take a look at existing ads from other session players. If nothing else, you may spot a gap in the market!

Hours

If you’re working with clients all over the world, you’ll be keeping some strange hours. If your home or working life is flexible enough to accommodate this, that’s fine. 

If, however, your family already don’t see enough of you, doing this won’t improve matters. 

If you need to keep strict hours make that clear to clients at the outset. You could even set up a Google calendar, or similar, so that potential clients can see when you’re available.

It’s Not All About You

When working on any project you’re likely to form an idea of what’s best in terms of what you can provide. Happily some clients will want to be guided by you, especially if they’ve no experience of your instrument or are new to arranging music.

Other clients, however, will have a strong opinion as to what they want, which may well differ from your own. You’ll need to be comfortable with playing what they’re asking for instead of what you want. 

Respectfully offer your opinion, by all means, as the client might not have considered what you’re proposing. If they’re adamant, play what’s required of you.

Always remember: the man who pays the piper calls the tune.

Speaking of money…

Earning a Living

If you’re exceptionally talented, hard-working, network like crazy and get some lucky breaks, you might find yourself earning comfortably, thanks to some high-profile bookings. 

This is exceptional. 

Assuming that you’re not a high-profile professional, you can typically earn a few hundred a month.

Supply and Demand

It’s also worth knowing that, like a lot of jobs in the music world, there are good times and bad. Demand can be somewhat seasonal. For example, you might be rushed off your feet in the run-up to Christmas but find January extremely quiet.

Bearing all of this in mind it’s better to view this kind of work as an additional revenue stream rather than your sole income.

Declaration

When it comes to income, don’t forget to declare any such earnings on a tax return, even if you think you’re not earning much. If you’re new to this, go online, find the local tax office and contact them for advice.

Everybody Wants Some

A good way of getting work and building a reputation is through a web-based agency or marketplace. This allows you to set-up a shop front to advertise your services as well as bringing you enquiries. A typical example, and one that I use, is fiverr.com

If you go down this route, however, you need to factor a few things into your costs.

Commission

Such sites are going to want paying for helping you find work and fees are typically 10-20% per transaction.

Exchange Rates

Unsurprisingly, some of the biggest sites are US-based and work in dollars. As exchange rates will fluctuate, earnings won’t be fixed or guaranteed.

More Fees

Some sites will pay you via companies such as PayPal, so you’ll need to be registered accordingly. They in turn will take their own commission for handling your money. 

Factoring this in, plus commission fees, means that you’ll receive around 60% of what the buyer originally paid. You'll need to set your prices to offset at least some of this whilst still remaining competitive.

Waiting to Get Paid

As with a lot of financial transactions, buyers are allowed a cooling off period after a purchase, in case they change their minds. This means that you’ll probably have to wait 7-14 days before payment is released to you.

Conclusion

Online session work can become a way of earning some extra money, provided you have the time and the skills to pursue it. In summation, bear in mind the following:

  • Be good at what you do
  • Be clear as to what you’re offering
  • Regular hours can be a struggle
  • Work isn’t guaranteed
  • Play what’s required
  • Declare earnings, whatever they are
  • Factor fees into your prices

In the next tutorial, I’ll show you what you’ll need to get started, such as equipment.


How to Make Microsoft Word Text Effects and Typography Art

Updated Primer for Creating Isometric Worlds, Part 2

How to Insert, Delete & Hide New Sheets in Excel in 60 Seconds

The 3-2-1 Method: How to Store Your Digital Photos Safely

A Noob’s Guide to Three.js

Getting Started With Chart.js: Scales

Getting Started With Chart.js: Scales

In the last four tutorials, you have learned a great deal about Chart.js. After reading the first four tutorials, you should now be able to customize the tooltips and labels, change the fonts, and create different chart types. One aspect of Chart.js that has not been yet covered in this series is scales.

Scales have changed a lot since version v1.0 of the library and are now much more powerful. In this tutorial, you will learn how to manipulate scales and control their appearance using different options that the library provides.

Changing Grid Lines

All the configuration options for grid lines are nested under the scale option in the gridLines key. This key defines options to customize the grid lines that run perpendicular to the axes. Let's change the grid lines of the line chart that you created in the line and bar charts tutorial.

You can show or hide the grid lines of a chart by using the display key. Its initial value is true, so the grid lines are shown by default. The color of the grid lines can be specified by using the color key. If you want the grid lines to be composed of dashes instead of being solid lines, you can provide a value for the length and spacing of dashes as a value of the borderDash key. You can set the width and color of the lines for the first or zero index using the zeroLineWidth and zeroLineColor keys respectively.

In all the charts up to this point, the scales did not have any descriptive text to let the viewers know what a particular axis represented. This can make the charts less useful. For example, if you see a plot of car speed and can't figure out the unit in which the speed is plotted on the y-axis, the chart is pretty much useless.

You can show or hide the scale labels on a chart by using the display key. The scale labels are hidden by default. The text that should be shown on these scales can be specified using the labelString key. You can also control the font color, family, size, and style using the fontColor, fontFamily, fontSize, and fontStyle keys respectively.

Here is the same old car speed chart with a different set of chart options specified.

There is one more key called offsetGridLines. When set to true, it shifts the labels to the middle of the grid lines. This is generally useful when creating bar charts.

Configuring Linear Scales

Linear scales are used to chart numerical data. These scales can be created on either the x or y axis. In most cases, Chart.js automatically detects the minimum and maximum values for the scales. However, this can result in some confusion. 

Let's say you want to plot the marks of students in a class. If the maximum marks for the test were 200 but none of the students scored more than 180 marks, the scale will most probably max out at 180. In such cases, the readers will have no way of knowing if the maximum marks were 180 or 200.

Chart.js has multiple built-in options that let you control different keys for scales. You can specify the minimum and maximum value for scales using the min and max keys. The step size of the scales can be controlled using the stepSize property. This way, you can determine how many grid lines should be drawn on the chart. Another way to set a limit on the number of grid lines and ticks shown on a chart is to use the maxTicksLimit key.

Here is the code to specify the minimum and maximum scale values for the horizontal scale on the bar chart for a previous tutorial of this series.

Just like the linear scale, you can also create logarithmic scales to plot values on your chart. In this case, logarithmic interpolation is used to determine the position of a point on the axes. These scales can also be placed on both the x-axis and the y-axis.

Configuring Radial Linear Scales

This scale type is used for radar and polar area chart types. Unlike the regular linear scale, this one overlays the chart area instead of being positioned on the axis.

There are a lot of keys specifically targeted at radial scales. For example, you can use the lineArc key to specify if the grid lines should be circular or straight. The default value is false for radar charts and true for polar area charts.

You can control the appearance of lines that radiate from the center of the chart to its point labels using the angleLines key. It accepts an object as its value. The object can contain keys to control the color and width of angle lines. You can hide the angle lines by setting the value of display key to false. The color and width of angle lines can be controlled using the color and lineWidth keys.

The appearance of point labels can be controlled using the pointLabels key. Just like angle lines, this key also accepts an object as its value. Please note that these options only have an effect when the value of lineArc is set to false. The font color, family, size, and style can be specified using the fontColor, fontFamily, fontSize, and fontStyle keys.

You can also set a minimum and maximum value for the scale using the min and max keys. The step size and maximum number of ticks on the scale can be specified using the stepSize and maxTicksLimit keys. These options are available under the ticks key. Some other keys available inside ticks are showLabelBackdrop, backdropColor, backdropPaddingX, and backDropPaddingY. You can use them to show or hide the background behind the tick labels and control its width, height, and color.

You can also use the gridLines key that we used for the line chart to set a color and width for the grid lines in a radar chart. Here are some options to create a radar chart with customized scales.

Configuring Time Scales

You can use a time scale to display times and dates on a chart. As mentioned in the introductory Chart.js tutorial, you need to include Moment.js in your projects to display time. One restriction on using a time scale is that it can be displayed only on the x-axis. All the configuration options for the time scale are located under the time sub-option.

To create a time scale, you have to set the value of the type key to time under the xAxes sub-option. After that, you can set the value of different keys under time. The unit which should be used to draw the ticks is set using the unit key. 

The step of the unit can be specified using the unitStepSize key. The format in which the labels for the ticks should be displayed is specified using the displayFormats sub-option. You can read more about allowed string formats on the Moment.js website.

The format in which the time is displayed in the tooltips can be specified using the tooltipFormat key. 

You can also round the time before plotting it on the chart using the round key. Be careful when setting a value for round. Let's say you set its value to hour and there are two times that need to be plotted on the chart. If one time is 5:30 AM and another is 5:50 AM, they will both be plotted on the 5:00 AM tick mark. Setting its value to minute will plot them on the 5:30 and 5:50 marks respectively.

The line chart plotted in the beginning of the tutorial could be plotted using a time scale with the help of the following code.

Final Thoughts

This tutorial helped you learn about different kinds of scales in Chart.js. You can now easily customize the scales in a chart by controlling their color, minimum and maximum value, number of ticks, and other such factors.

After reading all five tutorials in this series, you should now be able to create all kinds of charts available in Chart.js. I hope you liked the tutorial and the series. 

If you’re looking for additional resources to study or to use in your work, check out what we have available in the Envato marketplace. If you have any questions, please let me know in the comments. Have you ever used this library in your own projects? Please share some tips in the comments.


15 Best Swift App Templates

How to Create a 3D Stuffed Letters Text Effect in Adobe Photoshop

Building House Using Effectors in C4D

40 Professional Real Estate Flyer Templates

Monday, April 24, 2017

Graphics Tablets for Photo Retouching: The New Buyers' Guide

Quick Tip: Introducing Android O’s Adaptive Icons and Pinned Shortcuts

How to Make a Simple One Page Website: From a Parallax WordPress Theme

15+ Best Knowledge-Base WordPress Wiki-Style Themes

New Short Course: React State Management With MobX

An Updated Primer for Creating Isometric Worlds, Part 1

Building Your Startup: Leveraging Bootstrap, Ajax, and jQuery

How to Create a Summer Picnic Community Event Flyer in Adobe InDesign

5 Important Factors to Consider When Creating Your Blog

How to Create a Modern Cottage in a Scandinavian Style in Adobe Illustrator

Friday, April 21, 2017

How To Synchronize Interview Audio and Video in Final Cut Pro X

How to Create a Spring Cleaning Illustration in Adobe Illustrator

How to Create a Laravel Helper

How to Create a Laravel Helper

To begin with, I would rather let the Laravel official site speak about helpers.

Laravel includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.

So, basically, helpers in Laravel are built-in utility functions that you can call from anywhere within your application. If they hadn't been provided by the core framework, you might have ended up developing your own helper classes.

Although the core provides a variety of helpers already, there’s always a chance that you’ll need your own and would like to develop one so you don’t have to repeat the same code here and there, thus enforcing better maintainability. You'll learn how to create a custom Laravel helper in this tutorial.

Helpers in Laravel

As we discussed earlier, there are plenty of helpers available in the core of the Laravel framework. They are grouped together based on the functionality they provide. Here’s a list of helper groups.

Arrays

Helpers in this group provide functionality to manipulate array elements. More often than not, you're in the situation where you want to perform different operations on array elements. So this is the place where you should look first to see if what you're looking for already exists.

Paths

I find helpers in this category most useful. They return the fully qualified path of different directories like app, storage, config, and the like. I bet you're using most of these helpers already in your Laravel application.

Strings

String manipulation is something inevitable in your day-to-day application development. Although there are plenty of string manipulation functions provided by PHP itself, you'll find a few more useful goodies in this section.

URLs

You'll find very few in this category, but they are used throughout the application. They are used to generate route, asset, and form action URLs.

Miscellaneous

This category contains helpers that provide a variety of functionalities ranging from logging to debugging and many more.

For a complete reference of Laravel helpers, there's no better place than the official documentation.

Create Your First Custom Helper

Now you have a basic understanding of Laravel helpers and what they are used for. In this section, I’ll go ahead and demonstrate how you can create your own custom helper that can be used globally in your Laravel application.

To keep things simple and easy to understand, it’ll be a pretty basic helper that takes a userid and returns a username as a response. Of course, that doesn’t sound fancy, but I believe it’s enough to demonstrate the concept, and you could always extend it to fulfill your complex requirements.

I assume that you have a users table in your database and it has at least two fields—userid and username.

The Skeleton of a Laravel Helper

Before we move ahead and actually create the files, let’s have a quick look at the files that we’re going to create in the rest of the article.

  • app/Helpers/Envato/User.php: It’s our helper file that holds the logic of our helper.
  • app/Providers/EnvatoServiceProvider.php: It’s a custom service provider file that loads our custom helper file.
  • config/app.php: In this file, we’ll declare our custom service provider, and it also helps us to define an alias to our helper so that we don’t have to use the fully qualified class name of our helper.
  • routes/web.php: A pretty standard Laravel route file where we’ll actually test our helper.

Create Helper Files

Although you could place your helper files anywhere in your application, the more intuitive and standard way suggests that it should go under your app directory.

So go ahead and create a Helpers/Envato directory under app and create a User.php file with the following contents. Of course, you could place it directly under the app or app/Helpers directory, but providing that extra level allows us to organize our helpers in good shape, specifically when you’re going to have plenty of them.

The file starts with a pretty standard namespace declaration:

The purpose of our custom helper is to retrieve a username based on a userid. Hence, we need to interact with the database, and that forces us to include DB Facade.

For those who are not familiar with Laravel Facades, it’s just another convenient way to access the objects in service containers. Alternatively, you could have used dependency injection.

Moving ahead, there comes the concrete implementation of our helper. As you can see, there’s a static method that defines the logic of retrieving a username based on a userid.

The $user object holds the database record with the matching userid. Finally, the method returns the username as a response in the following statement.

That’s it as far as our helper file is concerned.

Now we’ve created our helper file, but the question is how are you going to use it? Two quick solutions come to my mind:

  • You can include our helper filename in the composer.json, so that it gets auto-loaded. Then, you could straight away call the static method of our helper class.
  • You can go ahead and create a Laravel service provider that allows you to register your custom helper file so that the Laravel framework loads it along with other dependencies. Register that service provider in the Laravel configuration and create an alias to use your helper.

Of course, the first one is pretty quick and easy to implement, and you might be tempted to do so, but I would rather suggest the latter one as it looks like more of an artisan way and is more maintainable.

Move to the command line and run the following command in your application root to create a new service provider.

You should see the message that confirms that it’s created successfully under the app/Providers directory.

Open that file and you should already see two methods out there. The important one in the context of this article is the register method. Yes, it’s empty at the moment, so let’s feed in some stuff to make it more useful.

The register method is used to register your dependencies, and we’ve exactly done that. We’ve included our custom helper file.

Here’s how the app/Providers/EnvatoServiceProvider.php file should look after modifications.

So it’s all pretty good so far. We have our custom helper and service provider on the table.

Next, we need to inform Laravel about our service provider so that it can load it during bootstrapping. Let’s open the config/app.php and add the following entry in the providers array at the end.

To use our helper in a convenient way, we could create an alias as well. So let’s do that by adding the following entry in the aliases array at the end in the same file.

By defining this entry, we can call our helper by using the EnvatoUser keyword. Pretty convenient, huh? For your reference, here’s the complete config/app.php file.

We’re almost there! We’ve done all the hard work to get here, and now we can reap the benefits of our custom helper.

Your First Custom Helper in Action

Again, to keep things simple and straightforward, we’ll define a basic Laravel route and call our helper from there!

Go ahead and create a routes/web.php file with the following contents.

Does that need any explanation at all? We’ve just called the custom helper by the shorthand EnvatoUser::get_username, and it should return the username.

Of course, you can call our helper from anywhere in the application, be it a controller or view.

So that ends our story for today.

Conclusion

Helpers in Laravel are really a powerful feature, and I'm sure that as a developer you would love to extend that. And that was the topic of today—we went through the basics of the Laravel helper file structure and created a useful custom helper.

I hope you’ve enjoyed the article and it helps you create your own custom helpers in day-to-day Laravel application development.

Don’t hesitate to leave your comments and queries in the feed below. I also catch comments on my Twitter and respond to them as soon as I can!


How to Weigh All the Pros and Cons of Outsourcing

Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts

Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts

You have learned about four different chart types in Chart.js up to this point. The second tutorial of the series covered line and bar charts. The third tutorial discussed radar and polar area charts. In this tutorial, you will learn how to use Chart.js to create pie, doughnut, and bubble charts.

Creating Pie and Doughnut Charts

Pie and doughnut charts are useful when you want to show the proportion in which something is divided among different entities. For example, you can use pie charts to show the percentage of males, females, and young ones of lions in a wildlife park, or the percentage of votes that different candidates got in an election.

Pie charts are only helpful when you want to compare one specific parameter or set of data. An important thing to keep in mind is that you cannot use pie charts to plot entities whose values are zero because the angle of the sectors in a pie chart depends on the magnitude of the data points.

This means that any entity whose share is zero won't be shown on the chart at all. Similarly, you cannot plot negative values on a pie chart. You can create pie charts in Chart.js by setting the type key to pie. Similarly, you can create doughnut charts by setting the type key to doughnut. Here is an example of creating these two charts:

Let's create a pie chart which shows the oil exports data of the top five countries in 2015. The data is in US billion dollars.

You can control the appearance of the above chart using different keys like cutoutPercentage, which defines the percentage of the chart that is cut out of the middle. You can also specify the start angle of the chart using the rotation key. Similarly, you can also specify the angle that the chart sweeps while plotting the data using the circumference key.

There are two different animations that can be activated while drawing a chart. You can specify if the chart should have a rotation animation using the animateRotate key. Similarly, you can also specify if the doughnut chart should be scaled from the center using the animateScale key. The value of animateRotate is set to true by default, and the value for animateScale is set to false by default.

As usual, you can control the background color, border color, and border width of all the data points using the backgroundColorborderColor, and borderWidth keys respectively. Similarly, the hover values of all these properties can be controlled using the hoverBackgroundColorhoverBorderColor, and hoverBorderWidth keys.

Here is the code to create a doughnut chart for the above data. Setting the value for rotation equal to -Math.PI takes that starting point 180 degrees anti-clockwise. Then, setting the value of circumference to Math.PI makes the chart only span a semicircle.

Creating Bubble Charts

Bubble charts are used to plot or display three dimensions (p1, p2, p3) of data on a chart. The position and size of the bubbles determines the value of these three data points. The horizontal axis represents the first data point (p1), the vertical axis represents the second data point (p2), and the area of the bubble is used to represent the value of the third data point (p3).

One thing that you should keep in mind is that the magnitude of the third data point is not represented by the radius of the bubbles but their area. Now, the area of a circle is proportional to the square of the radius. This means that you have to make sure that the radius of the bubble that you plot is proportional to the square root of the magnitude of the third data point.

You can create bubble charts in Chart.js by setting the value of the type key to bubble. Here is an example of creating a bubble chart.

Let's plot the weight of different items kept in a room using a bubble chart. The data for the chart needs to be passed in the form of an object. The data object needs to have the following interface for it to be plotted properly.

One important difference between bubble charts and all other charts is that the bubble radius is not scaled with the chart. 

For example, the width of bars in a bar chart can increase or decrease based on the chart size. The same thing does not happen with bubble charts. The radius of the bubbles is always equal to the exact number of pixels that you specified. It makes sense because in this chart type, the radius is actually being used to represent real data.

Let's create a bubble chart to plot the population of deer at different spots in a forest. 

Since the radius here is proportional to the square root of the actual magnitude, the number of deer at (80, 80) is 100 times more than the number of deer at (0, 100).

Just like all other chart types, you can control the background color, border color, and border width of plotted points using the backgroundColor, borderColor, and borderWidth keys. Similarly, you can also specify the hover background color, hover border color, and hover border width using the hoverBackgroundColor, hoverBorderColor, and hoverBorderWidth keys. 

You can also specify the additional radius that you want to add to different bubbles on hover using the hoverRadius key. Remember that this radius is added to the original value to draw the hovered bubble. If the original bubble had a radius of 10 and hoverRadius is set to 5, the radius of the bubble on hover will be equal to 15. 

The above parameters will create the following bubble chart.

Final Thoughts

In this tutorial, you learned about three more chart types available in Chart.js. You should now be able to choose the appropriate chart type to plot your data and set specific values for different keys to control their appearance. In the next tutorial, you will learn how to manipulate the scales for different chart types.

I hope you liked this tutorial. If you have any questions, please let me know in the comments.


How to Create a Quick Holographic Text Effect in Adobe Photoshop

How to Become an Art Director

New Course: Prototyping an App With Adobe XD

Thursday, April 20, 2017

Swift From Scratch: Inheritance and Protocols

25 Stylish Vintage Fonts

Introduction to Parallel and Concurrent Programming in Python

Introduction to Parallel and Concurrent Programming in Python

Python is one of the most popular languages for data processing and data science in general. The ecosystem provides a lot of libraries and frameworks that facilitate high-performance computing. Doing parallel programming in Python can prove quite tricky, though.

In this tutorial, we're going to study why parallelism is hard especially in the Python context, and for that, we will go through the following:

  • Why is parallelism tricky in Python (hint: it's because of the GIL—the global interpreter lock).
  • Threads vs. Processes: Different ways of achieving parallelism. When to use one over the other?
  • Parallel vs. Concurrent: Why in some cases we can settle for concurrency rather than parallelism.
  • Building a simple but practical example using the various techniques discussed.

Global Interpreter Lock

The Global Interpreter Lock (GIL) is one of the most controversial subjects in the Python world. In CPython, the most popular implementation of Python, the GIL is a mutex that makes things thread-safe. The GIL makes it easy to integrate with external libraries that are not thread-safe, and it makes non-parallel code faster. This comes at a cost, though. Due to the GIL, we can't achieve true parallelism via multithreading. Basically, two different native threads of the same process can't run Python code at once.

Things are not that bad, though, and here's why: stuff that happens outside the GIL realm is free to be parallel. In this category fall long-running tasks like I/O and, fortunately, libraries like numpy.

Threads vs. Processes

So Python is not truly multithreaded. But what is a thread? Let's take a step back and look at things in perspective.

A process is a basic operating system abstraction. It is a program that is in execution—in other words, code that is running. Multiple processes are always running in a computer, and they are executing in parallel.

A process can have multiple threads. They execute the same code belonging to the parent process. Ideally, they run in parallel, but not necessarily. The reason why processes aren't enough is because applications need to be responsive and listen for user actions while updating the display and saving a file.

If that's still a bit unclear, here's a cheatsheet:

PROCESSES
THREADS
Processes don't share memory
Threads share memory
Spawning/switching processes is expensive
Spawning/switching threads is less expensive
Processes require more resources
Threads require fewer resources (are sometimes called lightweight processes)
No memory synchronisation needed
You need to use synchronisation mechanisms to be sure you're correctly handling the data

There isn’t one recipe that accommodates everything. Choosing one is greatly dependent on the context and the task you are trying to achieve.

Parallel vs. Concurrent

Now we'll go one step further and dive into concurrency. Concurrency is often misunderstood and mistaken for parallelism. That's not the case. Concurrency implies scheduling independent code to be executed in a cooperative manner. Take advantage of the fact that a piece of code is waiting on I/O operations, and during that time run a different but independent part of the code.

In Python, we can achieve lightweight concurrent behaviour via greenlets. From a parallelization perspective, using threads or greenlets is equivalent because neither of them runs in parallel. Greenlets are even less expensive to create than threads. Because of that, greenlets are heavily used for performing a huge number of simple I/O tasks, like the ones usually found in networking and web servers.

Now that we know the difference between threads and processes, parallel and concurrent, we can illustrate how different tasks are performed on the two paradigms. Here's what we're going to do: we will run, multiple times, a task outside the GIL and one inside it. We're running them serially, using threads and using processes. Let's define the tasks:

We've created two tasks. Both of them are long-running, but only crunch_numbers actively performs computations. Let's run only_sleep serially, multithreaded and using multiple processes and compare the results:

Here's the output I've got (yours should be similar, although PIDs and times will vary a bit):

Here are some observations:

  • In the case of the serial approach, things are pretty obvious. We're running the tasks one after the other. All four runs are executed by the same thread of the same process.

  • Using processes we cut the execution time down to a quarter of the original time, simply because the tasks are executed in parallel. Notice how each task is performed in a different process and on the MainThread of that process.

  • Using threads we take advantage of the fact that the tasks can be executed concurrently. The execution time is also cut down to a quarter, even though nothing is running in parallel. Here's how that goes: we spawn the first thread and it starts waiting for the timer to expire. We pause its execution, letting it wait for the timer to expire, and in this time we spawn the second thread. We repeat this for all the threads. At one moment the timer of the first thread expires so we switch execution to it and we terminate it. The algorithm is repeated for the second and for all the other threads. At the end, the result is as if things were run in parallel. You'll also notice that the four different threads branch out from and live inside the same process: MainProcess.

  • You may even notice that the threaded approach is quicker than the truly parallel one. That's due to the overhead of spawning processes. As we noted previously, spawning and switching processes is an expensive operation.

Let's do the same routine but this time running the crunch_numbers task:

Here's the output I've got:

The main difference here is in the result of the multithreaded approach. This time it performs very similarly to the serial approach, and here's why: since it performs computations and Python doesn't perform real parallelism, the threads are basically running one after the other, yielding execution to one another until they all finish.

The Python Parallel/Concurrent Programming Ecosystem

Python has rich APIs for doing parallel/concurrent programming. In this tutorial we're covering the most popular ones, but you have to know that for any need you have in this domain, there's probably something already out there that can help you achieve your goal. 

In the next section, we'll build a practical application in many forms, using all of the libraries presented. Without further ado, here are the modules/libraries we're going to cover:

  • threading: The standard way of working with threads in Python. It is a higher-level API wrapper over the functionality exposed by the _thread module, which is a low-level interface over the operating system's thread implementation.

  • concurrent.futures: A module part of the standard library that provides an even higher-level abstraction layer over threads. The threads are modelled as asynchronous tasks.

  • multiprocessing: Similar to the threading module, offering a very similar interface but using processes instead of threads.

  • gevent and greenlets: Greenlets, also called micro-threads, are units of execution that can be scheduled collaboratively and can perform tasks concurrently without much overhead.

  • celery: A high-level distributed task queue. The tasks are queued and executed concurrently using various paradigms like multiprocessing or gevent.

Building a Practical Application

Knowing the theory is nice and fine, but the best way to learn is to build something practical, right? In this section, we're going to build a classic type of application going through all the different paradigms.

Let's build an application that checks the uptime of websites. There are a lot of such solutions out there, the most well-known ones being probably Jetpack Monitor and Uptime Robot. The purpose of these apps is to notify you when your website is down so that you can quickly take action. Here's how they work:

  • The application goes very frequently over a list of website URLs and checks if those websites are up.
  • Every website should be checked every 5-10 minutes so that the downtime is not significant.
  • Instead of performing a classic HTTP GET request, it performs a HEAD request so that it does not affect your traffic significantly.
  • If the HTTP status is in the danger ranges (400+, 500+), the owner is notified.
  • The owner is notified either by email, text-message, or push notification.

Here's why it's essential to take a parallel/concurrent approach to the problem. As the list of websites grows, going through the list serially won't guarantee us that every website is checked every five minutes or so. The websites could be down for hours, and the owner won't be notified.

Let's start by writing some utilities:

We'll actually need a website list to try our system out. Create your own list or use mine:

Normally, you'd keep this list in a database along with owner contact information so that you can contact them. Since this is not the main topic of this tutorial, and for the sake of simplicity, we're just going to use this Python list.

If you paid really good attention, you might have noticed two really long domains in the list that are not valid websites (I hope nobody bought them by the time you're reading this to prove me wrong!). I added these two domains to be sure we have some websites down on every run. Also, let's name our app UptimeSquirrel.

Serial Approach

First, let's try the serial approach and see how badly it performs. We'll consider this the baseline.

Threading Approach

We're going to get a bit more creative with the implementation of the threaded approach. We're using a queue to put the addresses in and create worker threads to get them out of the queue and process them. We're going to wait for the queue to be empty, meaning that all the addresses have been processed by our worker threads.

concurrent.futures

As stated previously, concurrent.futures is a high-level API for using threads. The approach we're taking here implies using a ThreadPoolExecutor. We're going to submit tasks to the pool and get back futures, which are results that will be available to us in the future. Of course, we can wait for all futures to become actual results.

The Multiprocessing Approach

The multiprocessing library provides an almost drop-in replacement API for the threading library. In this case, we're going to take an approach more similar to the concurrent.futures one. We're setting up a multiprocessing.Pool and submitting tasks to it by mapping a function to the list of addresses (think of the classic Python map function).

Gevent

Gevent is a popular alternative for achieving massive concurrency. There are a few things you need to know before using it:

  • Code performed concurrently by greenlets is deterministic. As opposed to the other presented alternatives, this paradigm guarantees that for any two identical runs, you'll always get the same results in the same order.

  • You need to monkey patch standard functions so that they cooperate with gevent. Here's what I mean by that. Normally, a socket operation is blocking. We're waiting for the operation to finish. If we were in a multithreaded environment, the scheduler would simply switch to another thread while the other one is waiting for I/O. Since we're not in a multithreaded environment, gevent patches the standard functions so that they become non-blocking and return control to the gevent scheduler.

To install gevent, run: pip install gevent

Here's how to use gevent to perform our task using a gevent.pool.Pool:

Celery

Celery is an approach that mostly differs from what we've seen so far. It is battle tested in the context of very complex and high-performance environments. Setting up Celery will require a bit more tinkering than all the above solutions.

First, we'll need to install Celery:

pip install celery

Tasks are the central concepts within the Celery project. Everything that you'll want to run inside Celery needs to be a task. Celery offers great flexibility for running tasks: you can run them synchronously or asynchronously, real-time or scheduled, on the same machine or on multiple machines, and using threads, processes, Eventlet, or gevent.

The arrangement will be slightly more complex. Celery uses other services for sending and receiving messages. These messages are usually tasks or results from tasks. We're going to use Redis in this tutorial for this purpose. Redis is a great choice because it's really easy to install and configure, and it's really possible you already use it in your application for other purposes, such as caching and pub/sub. 

You can install Redis by following the instructions on the Redis Quick Start page. Don't forget to install the redis Python library, pip install redis, and the bundle necessary for using Redis and Celery: pip install celery[redis].

Start the Redis server like this: $ redis-server

To get started building stuff with Celery, we'll first need to create a Celery application. After that, Celery needs to know what kind of tasks it might execute. To achieve that, we need to register tasks to the Celery application. We'll do this using the @app.task decorator:

Don't panic if nothing is happening. Remember, Celery is a service, and we need to run it. Till now, we only placed the tasks in Redis but did not start Celery to execute them. To do that, we need to run this command in the folder where our code resides:

celery worker -A do_celery --loglevel=debug --concurrency=4

Now rerun the Python script and see what happens. One thing to pay attention to: notice how we passed the Redis address to our Redis application twice. The broker parameter specifies where the tasks are passed to Celery, and backend is where Celery puts the results so that we can use them in our app. If we don't specify a result backend, there's no way for us to know when the task was processed and what the result was.

Also, be aware that the logs now are in the standard output of the Celery process, so be sure to check them out in the appropriate terminal.

Conclusions

I hope this has been an interesting journey for you and a good introduction to the world of parallel/concurrent programming in Python. This is the end of the journey, and there are some conclusions we can draw:

  • There are several paradigms that help us achieve high-performance computing in Python.
  • For the multi-threaded paradigm, we have the threading and concurrent.futures libraries.
  • multiprocessing provides a very similar interface to threading but for processes rather than threads.
  • Remember that processes achieve true parallelism, but they are more expensive to create.
  • Remember that a process can have more threads running inside it.
  • Do not mistake parallel for concurrent. Remember that only the parallel approach takes advantage of multi-core processors, whereas concurrent programming intelligently schedules tasks so that waiting on long-running operations is done while in parallel doing actual computation.

How to Use the Gmail Preview Pane: To Check Emails Quickly

Improve Your Photographic Composition Skills by Using Shape

Try Our New Course: Creating Low Poly Backgrounds

Getting Started With Chart.js: Radar and Polar Area Charts

Getting Started With Chart.js: Radar and Polar Area Charts

The previous tutorial of this series focused on creating line and bar charts using Chart.js. Both these charts have their own uses and configuration options that were covered in detail in the last tutorial.

In this tutorial, you will learn about two new chart types that can be created using Chart.js: radar and polar area charts. Just like the previous tutorial, we will start with a brief overview of the chart types and then move to a more detailed discussion. 

Creating Radar Charts

Line and bar charts are useful when you want to compare only one or two properties of a large number of objects—for example, the population of all the countries in Asia or the number of different pollutants in the atmosphere.

Let's say you want to compare the density, opacity, viscosity, refractive index and boiling point of only three different liquids. Creating a bar chart for this data will be problematic as you will need to create five different columns for each liquid. It will also be hard to compare the corresponding properties of the liquids. 

In situations where you have to compare a lot of properties of only a few objects, creating a radar chart is the most efficient method of visualizing and comparing data. These charts are also known as spider charts.

From Wikipedia, a radar chart is a graphical method of displaying multivariate data in the form of a two-dimensional chart of three or more quantitative variables represented on axes starting from the same point. The relative positions and angles of the axes are typically uninformative. 

Let's create our first radar chart now. Radar charts are created by setting the type key in Chart.js to radar. Here is a very basic example.

Let's plot the marks of two students of a class in five different subjects. Here is the code to provide the data for creating the chart.

The first chart that we usually create does not have any background color specifically set by us. However, radar charts can have a lot of overlap, making it difficult to correctly identify the data points without any color coding. 

For this reason, a color has been assigned to each dataset using the backgroundColor key. The following demo shows the final result of our code. As you can see, it is now very easy to compare the performance of both students in different subjects.

Besides the background color, you can also change the border color and border width for the chart using the borderColor and borderWidth keys. It is also possible for you to create dashed borders instead of continuous lines using the borderDash key. This key accepts an array as its value. 

The first element of the array determines the length of the dashes, and the second element determines the space between them. You can also provide an offset value for drawing the dashes using the borderDashOffset key.

You can also control the border color and width for plotted points using the pointBorderColor and pointBorderWidth. Similarly, you can also set a background color for different points using the pointBackgroundColor key. The size of the plotted points can be specified using the pointRadius key. You can control the distance at which the points should start interacting with the mouse using the pointHitRadius key.

You can also control the appearance of the plotted points on hover using the pointHoverBackgroundColorpointHoverBorderColor and pointHoverBorderWidth keys. One thing that you need to remember is that these hover keys will not wait for you to actually hover over the element to get triggered. The changes will take effect as soon as you are inside the hit radius set above.

There are a lot of shapes available for plotted points. They are circular by default. However, you can change the shape to triangle, rect, rectRounded, rectRot, cross, crossRot, star, line, and dash

Let's use all these keys to redraw the previous radar chart. Here is the code to provide configuration options for the datasets and the scales.

Inside the chartOptions object, the min and max values are used to set the minimum and maximum values for the scale. The stepSize key can be used to tell Chart.js the number of steps that it should take to go from min to max. Here is the final result of the above code.

Creating Polar Area Charts

Polar area charts are similar to pie charts. Two major differences between these charts are that in polar area charts all the sectors have equal angles and the radius of each sector depends on the plotted value. These charts are used to plot cyclic phenomena. For example, you can use it to plot the number of migratory birds of a given species in your area in each season of the year.

The radius of each sector in these charts is proportional to the square root of the number of corresponding objects. In the case of migratory birds, the radius will be proportional to the square root of the number of birds in your area.

You can create polar area charts in Chart.js by setting the type key to polarArea. Here is the basic code that you need to create a polar chart.

Here is the code to plot the number of migratory birds on a polar area chart. The only data provided at this point is the number of birds and the background color for different seasons.

The above code will create the following polar area chart.

The polar area chart provides the usual options to set the backgroundColor, borderWidth, borderColor, hoverBackgroundColor, hoverBorderWidth, and hoverBorderColor. There are also some polar area chart specific keys that you can use to customize their appearance. 

For example, you can set the starting angle for the first value in the dataset using the startAngle key. Similarly, the lineArc key that can be found under scale can be used to specify if the lines drawn should be circular or not by setting its value to true or false respectively.

The sectors drawn in the polar area chart are both rotated and scaled by default. However, you can prevent the scaling animation by setting the value of the animateScale key to false. Similarly, the rotating animation can be turned off by setting the value of the animateRotate key to false. Both these keys are available under animation

The code below changes the value of a few keys to make the chart more visually appealing.

Besides rotating the chart and disabling the rotation animation, we have also moved the legend to the left of the chart by setting the value of position to left. This leaves enough space at the top of the chart to display it properly.

Final Thoughts

In this tutorial, you learned about the applications of radar and polar area charts. After that, you learned how to create basic charts and customize them with various configuration options available in Chart.js. You will learn about pie and bubble charts in the next part of the series. 

If you're working with the web, especially on the front-end, JavaScript is important to know. Of course, it's not without its learning curves, and there are plenty of frameworks and libraries to keep you busy, as well. If you’re looking for additional resources to study or to use in your work, check out what we have available in the Envato marketplace.

If you have any questions about this tutorial, please let me know in the comments.


How to Create a Bold, Cartoonish Text Effect in Adobe Illustrator

How to Work With MVPs (Minimum Viable Products)

Wednesday, April 19, 2017

How to Set Up a Project in Angular 2

How to Add Wings to a Photo in Adobe Photoshop

Quick Tip: Working With Custom Fonts in Android O

Working With PHP Arrays in the Right Way

Working With PHP Arrays in the Right Way

In this tutorial, I am going to make a list of common PHP array functions with examples of usage and best practices. Every PHP developer must know how to use them and how to combine array functions to make code readable and short.

Also, there is a presentation with given code examples, so you can download it from the related links and show it to your colleagues to build a stronger team.

The Basics

Let's start with the basic functions that work with array keys and values. One of them is array_combine(), which creates an array using one array for keys and another for its values:

You should know, that the function array_values() returns an indexed array of values, array_keys() returns an array of keys of a given array, and array_flip() exchanges keys with values:

Make Your Code Shorter

The function list(), which is not really a function, but a language construction, is designed to assign variables in a short way. For example, here is a basic example of using the list() function:

This construction works perfectly with functions like preg_slit() or  explode() . Also, you can skip some parameters, if you don't need them to be defined:

Also, list() can be used with foreach, which makes this construction even better:

With the extract() function, you can export an associative array to variables. For every element of an array, a variable will be created with the name of a key and value as a value of the element:

Be aware that extract() is not safe if you are working with user data (like results of requests), so it is better to use this function with the flags EXTR_IF_EXISTS and EXTR_PREFIX_ALL.

The opposite of the previous function is the compact() function, which makes an associative array from variables:

Filtering Functions

There is a great function for array filtering, and it is called array_filter(). Pass the array as the first param and an anonymous function as the second param. Return true in a callback function if you want to leave this element in the array, and false if you don't:

There is a way to filter not only by the values. You can use ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH as a third parameter to pass the key or both value and key to the callback function.

Also, you can call array_filter() without a callback to remove all empty values:

You can get only unique values from an array using the array_unique() function. Notice that the function will preserve the keys of the first unique elements:

With array_column(), you can get a list of column values from a multi-dimensional array, like an answer from a SQL database or an import from a CSV file. Just pass an array and column name:

Starting from PHP 7, array_column() becomes even more powerful, because it is now allowed to work with an array of objects. So working with an array of models just became easier:

Walking Through the Arrays

Using array_map(), you can apply a callback to every element of an array. You can pass a function name or anonymous function to get a new array based on the given array:

There is a myth that there is no way to pass values and keys of an array to a callback, but we can bust it:

But this looks dirty. It is better to use array_walk() instead. This function looks the same as array_map(), but it works differently. First of all, an array is passed by a reference, so array_walk() doesn't create a new array, but changes a given array. So as a source array, you can pass the array value by a reference in a callback. Array keys can also be passed easily:

Joining the Arrays

The best way to merge two or more arrays in PHP is to use the array_merge() function. Items of arrays will be merged together, and values with the same string keys will be overwritten with the last value:

To remove array values from another array (or arrays), use array_diff(). To get values which are present in given arrays, use array_intersect(). The next examples will show how it works:

Do the Math With Array Values

Use array_sum() to get a sum of array values, array_product() to multiply them, or create your own formula with array_reduce():

To count all the values of an array, use array_count_values(). It will give all unique values of a given array as keys and a count of these values as a value:

Generating Arrays

To generate an array with a given size and the same value, use array_fill():

To generate an array with a range in of keys and values, like day hours or letters, use range():

To get a part of an array—for example, just the first three elements—use array_slice():

Sorting Arrays

It is good to remember that every sorting function in PHP works with arrays by a reference and returns true on success or false on failure. There's a basic sorting function called sort(), and it sorts values in ascending order without preserving keys. The sorting function can be prepended by the following letters:

  • a, sort preserving keys
  • k, sort by keys
  • r, sort in reverse/descending order
  • u, sort with a user function

You can see the combinations of these letters in the following table:

a k r u
a asort
arsort uasort
k ksort krsort
r arsort krsort rsort
u uasort
usort

Combining Array Functions Like a Boss

The real magic begins when you start to combine array functions. Here is how you can trim and remove empty values in just a single line of code with array_filter() and array_map():

To create an id to a title map from an array of models, we can use a combination of array_combine() and array_column():

To get the top three values of an array, we can use array_count_values(), arsort(), and array_slice():

It is easy to use array_sum() and array_map() to calculate the sum of order in a few rows:

Conclusion

As you can see, knowledge of the main array functions can make your code much shorter and more readable. Of course, PHP has many more array functions, and even the given functions have many variations to use with extra parameters and flags, but I think that in this tutorial we've covered the basics that every PHP developer should know.

Please note that I've created a presentation with the given examples, so you can download it from the related links and show it to your team.

If you have any questions, don't hesitate to ask them in the comments to the article. 

Further Reading and Related Links