Search

Scala-The Complete Guide

Introducing ScalaDoes Scala have a better future than any other programming language? Why Java Developer should learn Scala? What is the demand for Scala? Don’t worry we’ll help you understand why Scala has emerged as one of the most powerful Java alternatives in recent times and what made Developers think about Scala?Scala is a high-caliber programming language that changed the world of big data. It is a general-purpose, functional and object-oriented programming language that helps both object-oriented and functional styles of programming on a larger scale. Scala is a strong and static type language. If you are looking for a technique that can simplify your code easily then Scala is one of the great choices you can opt for. One of the best advantages of Scala for developers is, it reduces the burden by reducing the code snippets to two to three times compared to other application platforms.Let’s understand how Scala came into existenceHistory of ScalaScala is a programming language created by developed by  Martin Odersky in the year 2001.The language was first released internally in the year 2003 later it was officially released on January 20, 2004. Scala language was formally released on the platforms of Java, and .Net in the month of June 2004. Scala 2.0 version was released in Mach 2006. In the year 2011 Typesafe launched to provide commercial support training, services for Scala. These developments led towards the formal cessation of the .Net support in the year 2012. The team Scala has won a five-year research grant of over €2.3 million from the European Research Council.Features of ScalaLike other languages, even Scala has its own features. Its operational characteristics are the same as Java’s. Let’s understand the different features of Scala.Scala is Object-OrientedScala is functionalMarketableScala is ExtensibleExpressivenessScala runs on the JVMScala can execute Java CodeLet’s understand each of these clearly1. Scala is Object-OrientedScala is an object-oriented programming language like Python, Ruby, Java, Smalltalk, etc. It is a pure object-oriented language, it means that every value is an object. The Behavior of objects and Types and are described by classes and traits.2. Scala is functionalIt means that every function in Scala is a value and every value is an object, which makes every function an object. Scala provides a lightweight syntax for defining anonymous functions.3. MarketableYes, Scala is Marketable.  Learning Scala will surely make you more marketable. These days many top companies are moving to Scala.4. ExpressivenessScala is inherently very expressive. There are tons and tons of useful code in Scala. This is attracting more and more Java developers to learn Scala who is willing to write beautiful and clean code.5. Multi-Paradigm LanguageScala supports two programming paradigms like Object-oriented programming (OOP) and Functional programming (FP). It provides you a chance to explore both the languages. This combination of features will help you write Scala programs that are quite compact and simple.6. Scala runs on the JVMJava is compiled into Java Byte Code which is executed by the Java Virtual Machine (JVM). It means that both Java and Scala have a common runtime platform.7. Scala can execute Java CodeJust simple, it allows you to use all the classes of the Java SDK and also your favorite java open source projects.Frameworks for ScalaScala is a powerful language that has gained popularity among many developers. It enables developers to develop cutting edge applications that are less costly to maintain and easier to scale. Scala frameworks will help you in selecting the right technologies for solving your project tasks.Following is the list of different Scala frameworks:PlayFinchAkka HTTPChaosLiftScalaQueryBlueEyesSlickLet’s understand a few main frameworks1. PlayPlay is an open-source web application framework released in 2007. This high-velocity framework will allow developers to build highly scalable web applications written in Scala. Applications built with Play deliver all of the benefits of Reactive systems including a high degree of responsiveness, resilience, and elasticity. It is currently used by some large websites like Samsung’s IoT Artik platform, LinkedIn, etc.2. FinchFinch is a modular system of HTTP primitives centered upon the concept of composability. Its purpose is to provide developers simple and robust HTTP primitives. Finch is very fast and highly functional for a variety of systems.3. Akka:Akka is a free and open-source toolkit and runtime, good with distributed processing. It was created for building distributed and resilient message-driven applications. Since the Akka ecosystem is immense it provides a wide range of tools for various purposes like Akka Actors, Akka HTTP, etc.4. Akka HTTPAkka HTTP is a modern toolkit for building integration layers. It provides tools for building integration layers rather than application cores. This Akka HTTP tool is mostly used in high load environments.5. LiftLift is a highly secure and scalable framework designed to address a multitude of security concerns including code injection and cross-site scripting. It was created by David Pollak since he was dissatisfied with some aspects of Ruby on Rails.6. SlickSlick is a modern database query and access library for Scala. It is designed to connect to databases and sources. Slick helps in simplifying the process of building next-generation distributed applications using Scala.Applications of ScalaScala is a general-purpose computer language that widely supports the object and functional style of programming. Using Scala, you can create any type of application in less time. It has the capability to support multiple functionalities Below are the few areas where Scala can be applied.It supports both front and back ends of a web application with scala.jsBig Data and data analysis with Apache SparkScala can be used to create Android ApplicationsHighly concurrent things, like messaging apps, with AkkaScala is also applied in HadoopParallel batch processingDesktop Applications can be created using ScalaConcurrency and distributed application1. Front and back ends of a web application with scala.jsScala is used for designing Web Applications and Web Pages. Scala.js is an implementation of Scala which gathers Scala code to JavaScript. It supports full bilateral interoperability between Scala and Javascript code and hence it allows us to develop front-end web applications in Java using Javascript libraries and frameworks.2. Android ApplicationsScala can be used to build Android Applications. In order to develop Android applications in Scala, you need a minimum set of tools like Android SDK and Homebrew. Scala is easy to use for existing and newly created Android projects and the apps written using Scala is lightweight and start at 30kb.3. Desktop ApplicationsWhen you want to develop desktop applications you need to make sure you have the right framework that is going to work for you. There are several options out there, Scala is one among them.4. Scala is used with HadoopScala is used in some of the Hadoop ecosystem components like Apache Kafka, Apache Spark. Obviously, it will be really useful for someone to develop applications using Scala that uses Hadoop and ecosystem projects.We use Scala due to the factor that it is faster than Java. The central drive behind Scala is to make life easier and more productive for the programmer.Technologies built using ScalaApache SmazaFinagleAkkaLichessScaldingApache SparkLearn more about different technologies built using Scala  here.Career in ScalaScala has been gradually gaining popularity in recent times, some of the world’s best-known web platforms have switched to Scala. According to  Indeed, Scala is in the 12th place in terms of the most demanded languages. Very few developers know how to work with Scala. It is the best-suited technology for aspiring software professionals like Data Scientists, Data Engineers, etc. People who know Scala can enjoy a great career as Scala Developer.Different Scala Job RolesSoftware EngineerSenior Software EngineerSoftware DeveloperApplication DeveloperIT ConsultantScala Developers SalaryAccording to  PayScale the average salary for a Software Engineer with Scala Skills is RS 1019K. Let’s see the salary for different job roles.Job RolesMinimum Salary in RSMaximum Salary  in RSSoftware Engineer296K1.8MSenior Software Engineer452K2.9MSoftware Developer296K1MApplication Developer708K1.1MIT Consultant670K2.3MSenior Software Engineer/ Developer/ Programmer528K2.3MSoftware Engineer/ Developer/ Programmer240K550KSource: PayScaleScala Developers Salary in  different countriesAs per  PayScale the average salary for a Software Engineer with Scala Skills is $107,633. Scala Developer's salary differs from country to country. Let’s see the salary for a Software Engineer with Scala Skills in other countries.CountryMinimumMaximumSourceUSA$97195-Indeed.comFrance€33000 (Junior)€52000StackOverflowAustraliaAUS65000 (Specialist)AUS 146000PayScaleGermany€43000€81000PayScaleUkraine$12000$46000PayScaleAre you still pondering about the future of your career in Scala? Confused about whether to make a career shift in Scala or not? What will be the next career option available? Don’t worry you can earn a Scala Certification and grab a chance to get your dream job. Taking up   Scala Certification Course will definitely give you an advantage while hiring. Scala Certification is the best way to tell your hiring manager that you belong to the category of niche professionals who can make a difference. Taking up this certification course will help you understand the basics of how functional programming work. Hence, learning Scala programming language will definitely take your career to the new career avenues.

Scala-The Complete Guide

8K
Scala-The Complete Guide

Introducing Scala

Does Scala have a better future than any other programming language? Why Java Developer should learn Scala? What is the demand for Scala? Don’t worry we’ll help you understand why Scala has emerged as one of the most powerful Java alternatives in recent times and what made Developers think about Scala?

Scala is a high-caliber programming language that changed the world of big data. It is a general-purpose, functional and object-oriented programming language that helps both object-oriented and functional styles of programming on a larger scale. Scala is a strong and static type language. If you are looking for a technique that can simplify your code easily then Scala is one of the great choices you can opt for. One of the best advantages of Scala for developers is, it reduces the burden by reducing the code snippets to two to three times compared to other application platforms.

Let’s understand how Scala came into existence

History of Scala

Scala is a programming language created by developed by  Martin Odersky in the year 2001.
The language was first released internally in the year 2003 later it was officially released on January 20, 2004. Scala language was formally released on the platforms of Java, and .Net in the month of June 2004. Scala 2.0 version was released in Mach 2006. In the year 2011 Typesafe launched to provide commercial support training, services for Scala. These developments led towards the formal cessation of the .Net support in the year 2012. The team Scala has won a five-year research grant of over €2.3 million from the European Research Council.

History of Scala

Features of Scala

Like other languages, even Scala has its own features. Its operational characteristics are the same as Java’s. Let’s understand the different features of Scala.

Features of Scala

  1. Scala is Object-Oriented
  2. Scala is functional
  3. Marketable
  4. Scala is Extensible
  5. Expressiveness
  6. Scala runs on the JVM
  7. Scala can execute Java Code

Let’s understand each of these clearly

1. Scala is Object-Oriented

Scala is an object-oriented programming language like Python, Ruby, Java, Smalltalk, etc. It is a pure object-oriented language, it means that every value is an object. The Behavior of objects and Types and are described by classes and traits.

2. Scala is functional

It means that every function in Scala is a value and every value is an object, which makes every function an object. Scala provides a lightweight syntax for defining anonymous functions.

3. Marketable

Yes, Scala is Marketable.  Learning Scala will surely make you more marketable. These days many top companies are moving to Scala.

4. Expressiveness

Scala is inherently very expressive. There are tons and tons of useful code in Scala. This is attracting more and more Java developers to learn Scala who is willing to write beautiful and clean code.

5. Multi-Paradigm Language

Scala supports two programming paradigms like Object-oriented programming (OOP) and Functional programming (FP). It provides you a chance to explore both the languages. This combination of features will help you write Scala programs that are quite compact and simple.

6. Scala runs on the JVM

Java is compiled into Java Byte Code which is executed by the Java Virtual Machine (JVM). It means that both Java and Scala have a common runtime platform.

7. Scala can execute Java Code

Just simple, it allows you to use all the classes of the Java SDK and also your favorite java open source projects.

Frameworks for Scala

Scala is a powerful language that has gained popularity among many developers. It enables developers to develop cutting edge applications that are less costly to maintain and easier to scale. Scala frameworks will help you in selecting the right technologies for solving your project tasks.

Following is the list of different Scala frameworks:

Frameworks for Scala

  1. Play
  2. Finch
  3. Akka HTTP
  4. Chaos
  5. Lift
  6. ScalaQuery
  7. BlueEyes
  8. Slick

Let’s understand a few main frameworks

1. Play

Play is an open-source web application framework released in 2007. This high-velocity framework will allow developers to build highly scalable web applications written in Scala. Applications built with Play deliver all of the benefits of Reactive systems including a high degree of responsiveness, resilience, and elasticity. It is currently used by some large websites like Samsung’s IoT Artik platform, LinkedIn, etc.

2. Finch

Finch is a modular system of HTTP primitives centered upon the concept of composability. Its purpose is to provide developers simple and robust HTTP primitives. Finch is very fast and highly functional for a variety of systems.

3. Akka:

Akka is a free and open-source toolkit and runtime, good with distributed processing. It was created for building distributed and resilient message-driven applications. Since the Akka ecosystem is immense it provides a wide range of tools for various purposes like Akka Actors, Akka HTTP, etc.

4. Akka HTTP

Akka HTTP is a modern toolkit for building integration layers. It provides tools for building integration layers rather than application cores. This Akka HTTP tool is mostly used in high load environments.

5. Lift

Lift is a highly secure and scalable framework designed to address a multitude of security concerns including code injection and cross-site scripting. It was created by David Pollak since he was dissatisfied with some aspects of Ruby on Rails.

6. Slick

Slick is a modern database query and access library for Scala. It is designed to connect to databases and sources. Slick helps in simplifying the process of building next-generation distributed applications using Scala.

Applications of Scala

Scala is a general-purpose computer language that widely supports the object and functional style of programming. Using Scala, you can create any type of application in less time. It has the capability to support multiple functionalities Below are the few areas where Scala can be applied.

  • It supports both front and back ends of a web application with scala.js
  • Big Data and data analysis with Apache Spark
  • Scala can be used to create Android Applications
  • Highly concurrent things, like messaging apps, with Akka
  • Scala is also applied in Hadoop
  • Parallel batch processing
  • Desktop Applications can be created using Scala
  • Concurrency and distributed application

1. Front and back ends of a web application with scala.js

Scala is used for designing Web Applications and Web Pages. Scala.js is an implementation of Scala which gathers Scala code to JavaScript. It supports full bilateral interoperability between Scala and Javascript code and hence it allows us to develop front-end web applications in Java using Javascript libraries and frameworks.

2. Android Applications

Scala can be used to build Android Applications. In order to develop Android applications in Scala, you need a minimum set of tools like Android SDK and Homebrew. Scala is easy to use for existing and newly created Android projects and the apps written using Scala is lightweight and start at 30kb.

3. Desktop Applications

When you want to develop desktop applications you need to make sure you have the right framework that is going to work for you. There are several options out there, Scala is one among them.

4. Scala is used with Hadoop

Scala is used in some of the Hadoop ecosystem components like Apache Kafka, Apache Spark. Obviously, it will be really useful for someone to develop applications using Scala that uses Hadoop and ecosystem projects.

We use Scala due to the factor that it is faster than Java. The central drive behind Scala is to make life easier and more productive for the programmer.

Technologies built using Scala

Technologies built using Scala

  • Apache Smaza
  • Finagle
  • Akka
  • Lichess
  • Scalding
  • Apache Spark

Learn more about different technologies built using Scala  here.

Career in Scala

Scala has been gradually gaining popularity in recent times, some of the world’s best-known web platforms have switched to Scala. According to  Indeed, Scala is in the 12th place in terms of the most demanded languages. Very few developers know how to work with Scala. It is the best-suited technology for aspiring software professionals like Data Scientists, Data Engineers, etc. People who know Scala can enjoy a great career as Scala Developer.

Different Scala Job Roles

  • Software Engineer
  • Senior Software Engineer
  • Software Developer
  • Application Developer
  • IT Consultant

Scala Developers Salary

According to  PayScale the average salary for a Software Engineer with Scala Skills is RS 1019K. Let’s see the salary for different job roles.

Job RolesMinimum Salary in RSMaximum Salary  in RS
Software Engineer296K1.8M
Senior Software Engineer452K2.9M
Software Developer296K1M
Application Developer708K1.1M
IT Consultant670K2.3M
Senior Software Engineer/ Developer/ Programmer528K2.3M
Software Engineer/ Developer/ Programmer240K550K

Source: PayScale

Scala Developers Salary in  different countries

As per  PayScale the average salary for a Software Engineer with Scala Skills is $107,633. Scala Developer's salary differs from country to country. Let’s see the salary for a Software Engineer with Scala Skills in other countries.

CountryMinimumMaximumSource
USA$97195-Indeed.com
France€33000 (Junior)€52000StackOverflow
AustraliaAUS65000 (Specialist)AUS 146000PayScale
Germany€43000€81000PayScale
Ukraine$12000$46000PayScale

Are you still pondering about the future of your career in Scala? Confused about whether to make a career shift in Scala or not? What will be the next career option available? Don’t worry you can earn a Scala Certification and grab a chance to get your dream job. Taking up   Scala Certification Course will definitely give you an advantage while hiring. Scala Certification is the best way to tell your hiring manager that you belong to the category of niche professionals who can make a difference. Taking up this certification course will help you understand the basics of how functional programming work. Hence, learning Scala programming language will definitely take your career to the new career avenues.

KnowledgeHut

KnowledgeHut

Author

KnowledgeHut is an outcome-focused global ed-tech company. We help organizations and professionals unlock excellence through skills development. We offer training solutions under the people and process, data science, full-stack development, cybersecurity, future technologies and digital transformation verticals.
Website : https://www.knowledgehut.com

Join the Discussion

Your email address will not be published. Required fields are marked *

SPECIAL OFFER Upto 20% off on all courses
Enrol Now

Trending blog posts

Suggested Blogs

Six Tips To Improve As Android Developer

Mobile applications are increasing day by day and that is all because of increasing craze and trend of Android development course among individuals. Evolution of these mobile applications has given people an interesting and innovative ways to stay connected with each other. The statistics say it all, Android market leads the position with 2.2 million apps available for download (as of June 2016). With the numbers only we can tell the increased demand for Android developers. Digital era has already started and we are already witnessing the emerging market for these new age jobs. Android development surely has a bright future but with proper Android development training and certification will take you to new heights. As the market for Android development is booming, it is mandatory for an Android developers to have the proper skill set. Let’s explore some basic and essential tips to become a good Android developer. Design does matter One of the primary areas to concentrate on will be your app design. You might be having a great idea and proper skill sets to become an android developer, but if you fail in to attract people just because of bad execution of your ideas, it does not matter how creative your app is because people are not going to turn their heads towards poorly designed applications. Android platform should be your primary concern We know that this article is primarily for android developers, but this point is worth noting. iOS gets more premium quality apps than android (at least sooner than android) because iOS users do not worry about spending a few bucks on their apps, android users, on the other hand, are less targeted towards purchasing the apps. This should not stop you from developing applications for android, the reason being there are more than 1000 million android devices out there and you will be missing out millions of customers. Releasing applications for free “How do I make my money back then?” is the first question that pops into your mind. Well as said before, android users want their applications to be free and you can make your money back by using ads in your application. If this does not work for you, develop two different applications with slightly different features (premium and regular). The premium app should have slightly more features than the normal version and you can release it as paid version while as the normal app can be released for free with slightly fewer features or with ads. But remember this, DO NOT compromise on quality on either of the applications. Being passionate leads to better development Building something that can solve real life problems can help App developers to kick start the early traction. It requires passion and persistence which lacks in many developers. Developing better applications requires some persistence in the work. Treating yourself as the most important user of the application will work in your favour. If you are really passionate about creating something out of the box, persistence is the only way. Top App developers around the globe would still create apps even if they are not getting paid. Teaming Up We all know that having a team makes work better. Developing android applications is nothing but working in a small core team and creating it bit by bit. Technically sound people working together will faster the work. Applications have many aspects which can’t be dealt by a single person. Having a small technical team with well-coordinated people can make things really easy. Languages to Focus On: A successful android developer needs to be proficient in few programming languages which will help in developing better application. These languages consist of Java, SQL and XML. Developers should be well versed in Java as it is the most in-demand language. Creating an android application requires the database and he should be well-versed in SQL. XML works along with SQL and Java as it performs tasks like parsing data feeds, designing UI and more. The above-mentioned points should be given priority before developing any android apps, remember that there are several other ways for you to become a good android developer. Intensive research on developing applications is strongly recommended.
Six Tips To Improve As Android Developer

Mobile applications are increasing day by day and ... Read More

What are Python KeyError Exceptions and How to Handle Them

There are times when you have written your code but while you execute, it might not run. These types of situations occur when the input is inappropriate or you try to open a file with a wrong path or try to divide a number by zero. Due to some errors or incorrect command the output will not be displayed. This is because of errors and exceptions which are a part of the Python programming language. Learn about such concepts and gain further knowledge by joining Python Programming Course.What is Exception Handling?Python raises exceptions when it encounters errors during execution. A Python Exception is basically a construct that signals any important event, such as a run-time error.Exception Handling is the process of responding to executions during computations, which often interrupts the usual flow of executing a program. It can be performed both at the software level as part of the program and also at hardware level using built-in CPU mechanisms.Why is Exception Handling Important?Although exceptions might be irritating when they occur, they play an essential role in high level languages by acting as a friend to the user.An error at the time of execution might lead to two things— either your program will die or will display a blue screen of death. On the other hand, exceptions act as communication tools. It allows the program to answer the questions — what, why and how something goes wrong and then terminates the program in a delicate manner.In simple words, exception handling protects against uncontrollable program failures and increases the potency and efficiency of your code. If you want to master yourself in programming, the knowledge of exceptions and how to handle them is very crucial, especially in Python.What are the Errors and Exceptions in Python?Python doesn’t like errors and exceptions and displays its dissatisfaction by terminating the program abruptly.There are basically two types of errors in the Python language-Syntax Error.Errors occuring at run-time or Exceptions.Syntax ErrorsSyntax Errors, also known as parsing errors, occur when the parser identifies an incorrect statement. In simple words, syntax error occurs when the proper structure or syntax of the programming language is not followed.An example of a syntax error:>>> print( 1 / 0 )) File "", line 1 print( 1 / 0 ))   ^SyntaxError: invalid syntaxExceptionsExceptions occur during run-time. Python raises an exception when your code has a correct syntax but it encounters a run-time issue which it is not able to handle.There are a number of defined built-in exceptions in Python which are used in specific situations. Some of the built-in exceptions are:ExceptionCause Of ErrorArithmeticErrorRaised when numerical computation fails.FloatingPointErrorRaised when floating point calculation fails.AssertionErrorRaised in case of failure of the Assert statement.ZeroDivisionErrorRaised when division or modulo by zero takes place for all numerical values.OverflowErrorRaised when result of an arithmetic operation is very large to be represented.IndexErrorRaised when an index is not found in a sequence.ImportErrorRaised when the imported module is not found.IndentationErrorRaised when indentation is not specified properly.KeyboardInterruptRaised when the user hits interrupt key.RuntimeErrorRaised when a generated error does not fall into any category.SyntaxErrorRaised when there is an error in Python syntax.IOErrorRaised when Python cannot access a file correctly on disk.KeyErrorRaised when a key is not found in a dictionary.ValueErrorRaised when an argument to a function is the right type but not in the right domain.NameErrorRaised when an identifier is not found in the local or global namespace.TypeErrorRaised when an argument to a function is not in the right type.There are another type of built-in exceptions called warnings. They are usually issued in situations where the user is alerted of some conditions. The condition does not raise an exception; rather it  terminates the program.What is a Python KeyError?Before getting into KeyError, you must know the meaning of dictionary and mapping in Python. Dictionary (dict) is an unordered collection of objects which deals with data type key. They are Python’s implementation of data structures and are also known as associative arrays. They comprise key-value pairs, in which each pair maps the key to its associated value.Dictionary is basically a data structure that maps one set of values into another and is the most common mapping in Python.Exception hierarchy of KeyError:->BaseException              ->Exception                         ->LookupError                                       ->KeyErrorA Python KeyError is raised when you try to access an invalid key in a dictionary. In simple terms, when you see a KeyError, it denotes that the key you were looking for could not be found.An example of KeyError:>>> prices = { 'Pen' : 10, 'Pencil' : 5, 'Notebook' : 25} >>> prices['Eraser'] Traceback (most recent call last): File "", line 1, in prices['Eraser'] KeyError: 'Eraser'Here, dictionary prices is declared with the prices of three items. The KeyError is raised when the item ‘Eraser’ is being accessed which is not present in prices.Whenever an exception is raised in Python, it is done using traceback, as you can see in the example code above. It tells why an exception is raised and what caused it.Let’s execute the same Python code from a file. This time, you will be asked to give the name of the item whose price you want to know:# prices.py prices = { 'Pen' : 10, 'Pencil' : 5, 'Notebook' : 25} item = input('Get price of: ') print(f'The price of {item} is {prices[item]}')You will get a traceback again but you’ll also get the information about the line from which the KeyError is raised:Get price of: Eraser Traceback (most recent call last): File "prices.py", line 5, in print(f'The price of {item} is {prices[item]}') KeyError: 'Eraser'The traceback in the example above provides the following information:A KeyError was raised.The key ‘Eraser’ was not found.The line number which raised the exception along with that line.Where else will you find a Python KeyError?Although most of the time, a KeyError is raised because of an invalid key in a Python dictionary or a dictionary subclass, you may also find it in other places in the Python Standard Library, such as in a zipfile. However, it denotes the same semantic meaning of the Python KeyError, which is not finding the requested key.An example of such:>>> from zipfile import ZipFile >>> my_zip_file = ZipFile('Avengers.zip') >>> my_zip_file.getinfo('Batman')Traceback (most recent call last): File "", line 1, in File "myzip.py", line 1119, in getinfo 'There is no item named %r in the archive' % name) KeyError: "There is no item named 'Batman' in the archive"In this example, the zipfile.ZipFile class is used to derive information about a ZIP archive ‘Batman’ using the getinfo() function. Here, the traceback indicates that the problem is not in your code but in the zipfile code, by showing the line which caused the problem. The exception raised here is not because of a LookUpError but rather due to the zipfile.ZipFile.getinfo()function call.When do you need to raise a Python KeyError?In Python Programming, it might be sensible at times to forcefully raise exceptions in your own code. You can usually raise an exception using the raise keyword and by calling the KeyError exception:>>> raise KeyError('Batman')Here, ‘Batman’ acts as the missing key. However, in most cases, you should provide more information about the missing key so that your next developer has a clear understanding of the problem.Conditions to raise a Python KeyError in your code:It should match the generic meaning behind the exception.A message should be displayed about the missing key along with the missing key which needs to be accessed.How to Handle a Python KeyError?The main motive of handling a Python KeyError is to stop unexpected KeyError exceptions to be raised. There are a number of number of ways of handling a KeyError exception.Using get()The get()is useful in cases where the exception is raised due to a failed dictionary LookupError. It returns either the specified key value or a default value.# prices.py prices = { 'Pen' : 10, 'Pencil' : 5, 'Notebook' : 25} item = input('Get price of: ') price = prices.get(item) if price:   print(f'The price of {item} is {prices[item]}')   else:   print(f'The price of {item} is not known')This time, you’ll not get a KeyError because the get() uses a better and safer method to retrieve the price and if not found, the default value is displayed:Get price of: EraserThe price of Eraser is not knownIn this example, the variable price will either have the price of the item in the dictionary or the default value ( which is None by default ).In the example above, when the key ‘Eraser’ is not found in the dictionary, the get() returns  None by default rather than raising a KeyError. You can also give another default value as a second argument by calling get():price = prices.get(item,0)If the key is not found, it will return 0 instead of None.Checking for KeysIn some situations, the get() might not provide the correct information. If it returns None, it will mean that the key was not found or the value of the key in Python Dictionary is actually None, which might not be true in some cases. In such situations, you need to determine the existence of a key in the dictionary. You can use the if and in operator to handle such cases. It checks whether a key is present in the mapping or not by returning a boolean (True or False) value:dict = dictionary() for i in range(50):   key = i % 10     if key in dict: dict[key] += 1 else: dict[key] = 1In this case, we do not check what the value of the missing key is but rather we check whether the key is in the dictionary or not. This is a special way of handling an exception which is used rarely.This technique of handling exceptions is known as Look Before You Leap(LBYL).Using try-exceptThe try-except block is one of the best possible ways to handle the KeyError exceptions. It is also useful where the get() and the if and in operators are not supported.Let’s apply the try-except block on our earlier retrieval of prices code:# prices.py prices = { 'Pen' : 10, 'Pencil' : 5, 'Notebook' : 25} item = input('Get price of: ') try: print(f'The price of {item} is {prices[item]}') except KeyError: print(f'The price of {item} is not known')Here, in this example there are two cases— normal case and a backup case. try block corresponds to the normal case and except block to the backup case. If the normal case doesn’t print the name of the item and the price and raises a KeyError, the backup case prints a different statement or a message.Using try-except-elseThis is another way of handling exceptions. The try-except-else  has three blocks— try block, except block and else block.The else condition in a try-except statement is useful when the try condition doesn’t raise an exception. However, it must follow all the except conditions.Let us take our previous price retrieval code to illustrate try-except-else:# prices.py prices = { 'Pen' : 10, 'Pencil' : 5, 'Notebook' : 25} item = input('Get price of:') try: print(f'The price of {item} is {prices[item]}') except KeyError: print(f'The price of {item} is not known') else: print(f'There is no error in the statement')First, we access an existing key in the try-except block. If the Keyerror is not raised, there are no errors. Then the else condition is executed and the statement is displayed on the screen.Using finallyThe try statement in Python can have an optional finally condition. It is used to define clean-up actions and is always executed irrespective of anything. It is generally used to release external sources.An example to show finally:# prices.py prices = { 'Pen' : 10, 'Pencil' : 5, 'Notebook' : 25} item = input('Get price of: ') try: print(f'The price of {item} is {prices[item]}') except KeyError: print(f'The price of {item} is not known') finally: print(f'The finally statement is executed')Remember, the finally statement will always be executed whether an exception has occurred or not.How to raise Custom Exceptions in Python?Python comprises of a number of built-in exceptions which you can use in your program. However, when you’re developing your own packages, you might need to create your own custom exceptions to increase the flexibility of your program.You can create a custom Python exception using the pre-defined class Exception:def square(x): if x
8279
What are Python KeyError Exceptions and How to Han...

There are times when you have written your code bu... Read More

How to Work With a PDF in Python

Whether it is an ebook, digitally signed agreements, password protected documents, or scanned documents such as passports, the most preferred file format is PDF or Portable Document Format. It was originally developed by Adobe and is a file format used to present and transfer documents easily and reliably. It uses the file extension .pdf. In fact, PDF being the most widely used digital media, is now considered as an open standard which is maintained by the International Standards Organization (ISO). Python has relatively easy syntax which makes it even easier for the ones who are in their initial stage of learning the language. The popular Python libraries are well suited and integrated which allows to easily extract documents from a PDF, rotate pages if required, split pdf to make separate documents, or add watermarks in them.Now an important question rises, why do we need Python to process PDFs? Well, processing a PDF falls under the category of text analytics. There are several libraries and frameworks available which are designed in Python exclusively for text analytics. This makes it easier to play with a PDF in Python. You can also extract information from PDF and use into Natural Language Processing or any other Machine Learning models. Get certified and learn more about Python Programming and apply those skills and knowledge in the real world.History of  pyPDF, PyPDF2, pyPDF4The first PyPDF package was released in 2005 and the last official release in 2010. After a year or so, a  company named Phasit sponsored a branch of the PyPDF called PyPDF2 which was consistent with the original package and worked pretty well for several years.A series of packages were released later on with the name of PyPDF3 and later renamed as PyPDF4. The biggest difference between PyPDF and the other versions was that the later versions supported Python3. PyPDF2 has been discarded recently. But since PyPDF4 is not fully backward compatible with the PyPDf2, it is suggested to use PyPDF2. You can also use a substitute package - pdfrw. Pdfrw was created by Patrick Maupin and allows you to perform all functions which PyPDF2 is capable of except a few such as encryption, decryption, and types of decompression.Some common libraries in PythonLet us look into some of the libraries Python offers to handle PDFs:PdfMiner It is a tool used to extract information from PDF documents. PDFMiner allows the user to analyze text data and obtain the definite location of a text. It provides information such as fonts and lines. We can also use it as a PDF transformer and a PDF parser.PyPDF2PyPDF2 is purely a Python library which allows users to split, merge, crop, encrypt, and transform PDFs. You can also add customized data, view options, and passwords to the documents. Tabula-pyIt is a Python wrapper of tabula-java which can read tables from PDF files and convert into Pandas Dataframe or into CSV/TSV/JSON file formats.SlateIt is a Python package which facilitates the extraction of information and is dependent on the PdfMiner package.PDFQueryA light Python wrapper which uses minimum code to extract data from PDFs.xPDFIt is an open source viewer of PDF which also includes an extractor, converter and other utilities. Out of all the libraries mentioned above, PyPDF2 is the most used to perform operations like extraction, merging, splitting and so on.Installing PyPDF2If you're using Anaconda, you can install PyPDF2 using pip or conda. To install PyPDF2 using pip, run the following command in the command line:pip install PyPDF2The module is case-sensitive. So you need to make sure that proper syntax is followed. The installation is really quick since PyPDF2 is free of dependencies.Extracting Document Information from a PDF in PythonPyPDF2 can be used to extract metadata and all sorts of texts from PDF when you are performing operations on preexisting PDF files. The types of data you can extract are:AuthorCreatorProducerSubjectTitleNumber of PagesTo understand it better, let us use an existing PDF in your system or you can go to Leanpub and download a book sample.The code for extracting the document information from the PDF—# get_doc_info.py from PyPDF2 import PdfFileReader def getinfo(path):     with open(path, 'rb') as f:         PDF = PdfFileReader(f)         information = PDF.getDocumentInfo()         numberofpages = PDF.getNumPages()     print(information)     author = information.author     creator = information.creator     producer =information .producer     subject = information.subject     title = information.title if __name__ == '__main__':     path = 'reportlab-sample.pdf'     getinfo(path)The output of the program above will look like—Here, we have firstly imported PdfFileReader from the PyPDF2 package. The class PdfFileReader is used to interact with PDF files like reading and extracting information using accessor methods. Then, we have created our own function getinfo with a PDF file as an argument and then called the getdocumentinfo(). This returned an instance of DocumentInformation. And finally we got extract information like the author, creator, subject or title, etc.getNumPages() is used to count the number of pages in the document. PdfMiner can be used when you want to extract text from a PDF file. It is potent and particularly designed for extracting text from PDF.We have learned to extract information from PDF. Now let’s learn how to rotate a PDF. Rotating pages in PDFA lot of times we receive PDFs which contain pages in landscape orientation instead of portrait. You may also find certain documents to be upside down, which happens while scanning a document or mailing. However, we can rotate the pages clockwise or counterclockwise according to our choice using Python with PyPDF2.The code for rotating the article is as follows—# rotate_pages.py from PyPDF2 import PdfFileReader, PdfFileWriter def rotate(pdf_path):     pdf_write = PdfFileWriter()     pdf_read = PdfFileReader(path)     # Rotate page 90 degrees to the right     page1 = pdf_read.getPage(0).rotateClockwise(90)     pdf_write.addPage(page1)     # Rotate page 90 degrees to the left     page2 = pdf_read.getPage(1).rotateCounterClockwise(90)     pdf_write.addPage(page2)     # Add a page in normal orientation     pdf_write.addPage(pdf_read.getPage(2))     with open('rotate_pages.pdf', 'wb') as fh:         pdf_write.write(fh) if __name__ == '__main__':     path = 'mldocument.pdf'     rotate(path)The output of the code will be as follows—Here firstly we imported the PdfFileReader and the PdfFileWriter so that we can write out a new PDF file. Then we declared a function rotate with a path to the PDF that is to be modified. Within the function, we created a read object pdf_read and write object pdf_write.Then, we used the getPage() to grab the pages. Two pages page1 and page2 are taken and rotated to 90 degrees clockwise and 90 degrees counterclockwise respectively using rotateClockwise() and rotateCounterClockwise().We used addPage() function after each rotation method calls. This adds the rotated page to the write object. The last page we add is page3 without any rotation.Lastly, we have used write() with a file-like parameter to write out the new PDF. The final PDF contains three pages, the first two will be in the landscape mode and rotated in reversed direction and the third page will be in normal orientation.Now we will learn to merge different PDFs into one.Merging PDFsIn many cases, we need to merge two PDFs into a single one. For example, suppose you are working on a project report and you need to print it and bind it into a book. It contains a cover page followed by the project report. So you have two different PDFs and you want to merge them into one PDF. You can simply use Python to do so. Let us see how can we merge PDFs into one.The code for merging two PDF documents using PyPDF in mentioned below:# pdf_merging.py from PyPDF2 import PdfFileReader, PdfFileWriter def pdfmerger(paths, output):     pdfwrite = PdfFileWriter()     for path in paths:         pdfread = PdfFileReader(path)         for page in range(pdfread.getNumPages()):             # Add each page to the writer object             pdfwrite.addPage(pdfread.getPage(page))     # Write out the merged PDF     with open(output, 'wb') as out:         pdfwrite.write(out) if __name__ == '__main__':     paths = ['document-1.pdf', 'document-2.pdf']     pdfmerger(paths, output='merged.pdf')Here we have created a function pdfmerger() which takes a number of inputs and a single output. Then we created a PdfFileReader() object for each PDF path and looped over the pages, added each page to the write object. Finally, using the write() function the object’s contents are written to the disk.PyPDF2 makes the process of merging simpler by creating the PdfFileMerger class.Code for merging two documents using PyPDF2—# pdf_merger2.py import glob from PyPDF2 import PdfFileMerger def merger(output_path, input_paths):     pdfmerge = PdfFileMerger()     file_handles = []     for path in input_paths:         pdfmerge.append(path)     with open(output_path, 'wb') as fileobj:         pdfmerge.write(fileobj) if __name__ == '__main__':     paths = glob.glob('d-1.pdf')     paths.sort()     merger('d-2.pdf', paths)The PyPDF2 makes it simpler in the way that we don’t need to loop the pages of each document ourselves.  Here, we created the object pdfmerge and looped through the PDF paths. The PyPDF2 automatically appends the whole document. Finally, we write it out.Let’s perform the opposite of merging now!Splitting PDFsThe PyPDF2 package has the ability to split up a single PDF into multiple PDFs. It allows us to split pages into different PDFs. Suppose we have a set of scanned documents in a single PDF and we need to separate the pages into different PDFs as per requirement, we can simply use Python to select pages we want to split and get the work done.Code for splitting a single PDF into multiple PDFs—# pdf_splitter.py import os from PyPDF2 import PdfFileReader, PdfFileWriter def splitpdf(path):     fname = os.path.splitext(os.path.basename(path))[0]     pdf = PdfFileReader(path)     for page in range(pdf.getNumPages()):         pdfwrite = PdfFileWriter()         pdfwrite.addPage(pdf.getPage(page))         outputfilename = '{}_page_{}.pdf'.format(             fname, page+1)         with open(outputfilename, 'wb') as out:             pdfwrite.write(out)         print('Created: {}'.format(outputfilename)) if __name__ == '__main__':     path = 'document-1.pdf'     splitpdf(path)Here we have imported the PdfFileReader and PdfFileWriter from PyPDF2. Then we created a function called splitpdf() which accepts the path of PDF we want to split. The first line of the function takes the name of the input file. Then we open the PDF and create a read object. Using the read object’s getNumPages(), we loop over all the pages.In the next step, we created an instance of PdfFileWriter inside the for loop. Then, we created a PDF write instance and added each page to it for each of the pages in the PDF input. We also created a unique filename using the original filename + the word ‘page’ + the page number + 1.Once we are done with running the script, we will have each of the pages of the input PDF split into multiple PDFs. Now let us learn how to add a watermark to a PDF and keep it secured.Adding Overlays/WatermarksAn image or superimposed text on selected pages in a PDF document is referred to as a Watermark. The Watermark adds security features and protects our rational property like images and PDFs. Watermarks are also called overlays.The PyPDF2 allows us to watermark documents. We just need to have a PDF which will consist of our watermark text, image or signature.Code for adding a watermark in a PDF—# watermarker.py from PyPDF2 import PdfFileWriter, PdfFileReader def watermark(inputpdf, outputpdf, watermarkpdf):     watermark = PdfFileReader(watermarkpdf)     watermarkpage = watermark.getPage(0)     pdf = PdfFileReader(inputpdf)     pdfwrite = PdfFileWriter()     for page in range(pdf.getNumPages()):         pdfpage = pdf.getPage(page)         pdfpage.mergePage(watermarkpage)         pdfwrite.addPage(pdfpage)     with open(outputpdf, 'wb') as fh:         pdfwrite.write(fh) if __name__ == '__main__':     watermark(inputpdf='document-1.pdf',               outputpdf='watermarked_w9.pdf',               watermarkpdf='watermark.pdf')The output of the code will look like— There are three arguments of the function watermark(): inputpdf: The path of the PDF that is to be watermarked. outputpdf: The path where the watermarked PDF will be saved. watermarkpdf: The PDF which contains the watermark.Firstly, we extract the PDF page which contains the watermark image or text and then open that PDF page where we want to give the desired watermark.Using the inputpdf, we create a read object and using the pdfwrite, we create a write object to write out the watermarked PDF and then iterate over the pages.Next, we call the page object’s mergePage and apply the watermark and add that to the write object pdfwrite.When the loop terminates, the watermarked PDF is written out to the disk and it’s done!Encrypting a PDFIn the PDF world, the PyPDF2 package allows an owner password which gives the user the advantage to work as an administrator. The package also provides the user password which allows us to open the document upon entering the password.The PyPDF2 basically doesn’t permit any allowances on any PDF file yet it allows the user to set the owner password and user password.Code to add a password and add encryption to a PDF—# pdf_encrypt.py from PyPDF2 import PdfFileWriter, PdfFileReader def encryption(inputpdf, outputpdf, password):     pdfwrite = PdfFileWriter()     pdfread = PdfFileReader(inputpdf)     for page in range(pdfread.getNumPages()):         pdfwrite.addPage(pdfread.getPage(page))     pdfwrite.encrypt(user_pwd=password, owner_pwd=None,                       use_128bit=True)     with open(outputpdf, 'wb') as fh:         pdfwrite.write(fh) if __name__ == '__main__':     encryption(inputpdf='document-1.pdf',                   outputpdf='document-1-encrypted.pdf',                   password='twofish')We declare a  function named encryption() with three arguments—the input PDF path, the output PDF path and the password that we want to keep. Then we create one read object pdfread and one write object pdfwrite. Now we loop over all the pages and add them to the write object since we need to encrypt the entire document.Finally, we call the encrypt() function which accepts three parameters—the user password, the owner password and the whether or not to use 128-bit encryption. The PDF  will be encrypted to 40-bit encryption if the argument use128bit is set to false. Also if the owner password is set to none, then it will be set to user password automatically.Reading the Table data from PDFSuppose you want to work with the Table data in Pdf, you can use tabula-py to read tables in a PDF. To install tabula-py, run:pip install tabula-pyCode to extract simple Text from pdf using PyPDF2:import tabula # readinf the PDF file that contain Table Data # you can find the pdf file with complete code in below # read_pdf will save the pdf table into Pandas Dataframe df = tabula.read_pdf("document.pdf") # in order to print first 5 lines of Table df.head()If you PDF file contains Multiple Tabledf = tabula.read_pdf("document.pdf",multiple_tables=True)If you want to extract Information from the specific part of any specific page of PDFtabula.read_pdf("document.pdf", area=(126,149,212,462), pages=1)If you want the output into JSON Formattabula.read_pdf("offense.pdf", output_format="json")Exporting PDF into ExcelSuppose you want to export a PDF into Excel, you can do so by writing the following code and convert the PDF Data into Excel or CSV.tabula.convert_into("document.pdf", "document_testing.xlsx", output_format="xlsx")Let us sum up what we have learned in the article:Extraction of data from a PDFRotate pages in a PDFMerge PDFs into one PDFSplit a PDF into many PDFsAdd watermarks or overlays in a PDFAdd password or encryption to a PDFReading table from PDFExporting PDF into Excel or CSVAs you have seen, PyPDF2 is one of the most useful tools available in Python. The features of PyPDF2 makes life easier whether you are working on a large project or even when you quickly want to make some changes to your PDF documents. Learn more about such libraries and frameworks as KnowledgeHut offers Python Certification Course for Programmers, Developers, Jr./Sr Software Engineers/Developers and anybody who wants to learn Python.
8368
How to Work With a PDF in Python

Whether it is an ebook, digitally signed agreement... Read More