Posted on Leave a comment

6 Best Python NLP Libraries

If you are a data scientist or aspire to be one investing your time in learning natural language processing (NLP) will be an investment in your future. 2020 saw a surge in the field of natural language processing. In this blog post you will discover 5 popular NLP libraries, and it’s applications.

Preprocessing Libraries

Preprocessing a crucial step in any machine learning pipeline. If you are building a language model you would have to create a word vector which involves removing stop words, and converting words to its root form.

#1 Spacy

Spacy is a popular Python library for sentence tokenization, lemmatization, and stemming. It is an industry grade library which can be used for text preprocessing and training deep learning based text classifiers.

Getting started with Spacy: Named Entity Recognition is an important task in natural language processing. NER helps in extracting important entities like location, organization names, etc.

import spacy # python -m spacy download en_core_web_sm
nlp = spacy.load('en_core_web_sm') sentences = ['Stockholm is a beautiful city', 'Mumbai is a vibrant city' ] for sentence in sentences: doc = nlp(sentence) for entity in doc.ents: print(entity.text, entity.label_) print(spacy.explain(entity.label_))

The above code processes the two sentences and extracts the location in both sentences.

Let us now see the output

As seen from the output the code was able to extract Stockholm and Mumbai and associated them with the GPE label which indicates countries, cities, or states.

#2 NLTK

NLTK is another popular Python library for text preprocessing. It was started as an academic project and soon became very popular amongst researchers and academicians.

Let us see how we can do Part of Speech Tagging using NLTK. Part of speech tagging is used to extract the important part of speech like nouns, pronouns, adverbs, adjectives, etc.

import nltk
import os sentence = "Python is a beautiful programming language."
tokens = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokens)
entities = nltk.chunk.ne_chunk(tagged)
print(entities)

The parts of speech that were extract from the above sentence are

 (S   (GPE Python/NNP)   is/VBZ   a/DT   beautiful/JJ   programming/NN   language/NN   ./.) 

Applications

A popular application of NLP is to categorize a document into a given set of labels. There are a number of Python libraries which can help you to train deep learning based models for topic modeling, text summarization, sentiment analysis etc. Let us have a look at some of these popular libraries

Most deep learning based NLP models rely on pretrained language models using a process called transfer learning. A huge corpus of document is trained and then this model can be fine-tuned for a specific domain. Some popular libraries which help in using pretrained models and building industry grade NLP applications are

#3 FARM

Farm is a popular open source package developed by a Berlin based company. It is used to make the life of developers easier by providing some nice functionalities like experiment tracking, multitask-learning and parallelized processing of documents.

#4 Flair

Flair is a popular PyTorch based framework which helps developers to build state of the NLP applications like named entity recognition, part-of-speech tagging, sense disambiguation and classification.

#5 Transformers

Transformers is a popular Python library to easily access pretrained models and has support for both PyTorch and TensorFlow. If you want to build an entire NLP pipeline by using pretrained models for Natural language understanding and generation tasks transformers will make your life easier.

#6 Gensim

Gensim is another popular Python library widely used for topic modelling and provides an easy-to-use interface for popular algorithms like word2vec to find synonymous words.

The post 6 Best Python NLP Libraries first appeared on Finxter.

Posted on Leave a comment

Python compile()

If you’re like me, you love those TLDR; overviews to grasp the big picture quickly. Here is mine about Python’s compile() function:

Python’s built-in compile() method returns an executable code object as an “Abstract Syntax Tree” represented as an ast object. By passing this code object into the exec() or eval() functions, you can run it dynamically in your Python code. This way, you can programmatically create source code and execute it at runtime. To use the function, pass the string code to be executed, the filename, and the execution mode. For example compile('print("hi")', '<string>', 'exec') creates a code object consisting of one line print("hi").

This article shows you how to use Python’s built-in compile() method.

Python compile() built-in function -- Illustrated Explanation

Usage compile()

Learn by example! Let’s say, you have a code file with the following content:

# some_code.py file
customers = {"Alice": 40000, "Bob": 33000}
print(f"Bob: {customers['Bob']}")

Here’s an example on how to use the compile() built-in function.

# Read the code from the file or define it explicitly:
code = 'customers = {"Alice": 40000, "Bob": 33000}\nprint(f"Bob: {customers[\'Bob\']}")' # Create the ast code object
code_obj = compile(code, '<string>', 'exec') # Execute the ast code object
exec(code_obj) # Result:
# Bob: 33000

First, you create the code as a string. Second, you pass the string into the compile() function to create an ast object. You can then pass this object into the exec() function and run it dynamically.

Video compile()

Why Using compile() Instead of exec() With a String?

Before you dive into the syntax, you may not be motivated to use the compile() function in the first place. Why? Because you can also use the exec() function on a string instead of a code object.

Python’s exec() function executes the Python code you pass as a string or executable object argument. This is called dynamic execution because, in contrast to normal static Python code, you can generate code and execute it at runtime. This way, you can run programmatically-created Python code.

Here’s the same code without compile():

code = 'customers = {"Alice": 40000, "Bob": 33000}\nprint(f"Bob: {customers[\'Bob\']}")'
exec(code)
# Bob: 33000

The output is the same. However, there are two advantages of using a code object:

  • You make it explicit. Without compiling the string explicitly, the exec() method would do the same work implicitly. Thus, if you need to run the same code multiple times, you can save significant compilation overhead by just compiling it once in advance.
  • You can use the powerful ast object otherwise. This gives you access to some helper methods and additional information about the code such as the names defined in the code:
>>> code_obj.co_names
('customers', 'print')

You obtain this information from a compiled string which is very convenient!

Syntax compile()

You can use the compile() method with a number of different arguments.

Syntax: 
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) 
Arguments source A string to be compiled into a code object.
filename The file from which the code given in source was read. If this was a string, use '<string>'.
mode The execution mode—must be one of the following:
'exec' — If source is a sequence of statements
'eval' — If source is a single expression
'single' —If source is a single interactive statement
Optional: flags=0 Controls which compiler options should be activated and which future features should be allowed.
Optional: dont_inherit=False Do you want to avoid inheritance of the compiler options and future features?
Optional: optimize=-1 Optimization level of the compiler:
-1 selects the optimization level of the interpreter as given by -O options.
0 selects no optimization and __debug__ to True.
1 specifies that asserts are removed and sets __debug__ to False.
2 additionally removes docstrings.
Return Value ast Returns an AST object that represents the code as an abstract syntax tree.

Interactive Shell Exercise: Understanding compile()

Consider the following interactive code:

Exercise: Print the number associated to Alice in the dictionary!


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


How to Read and Compile Code from a File

Say, you have the following code in a code file:

# filename.py file
customers = {"Alice": 40000, "Bob": 33000}
print("Bob: " + str(customers['Bob']))

How to read the code from a file, compile it, and execute it at runtime?

Here’s the code:

# 1. Read code from file
f = open('filename.py', 'r')
code = f.read()
f.close() # 2. Compile code string
code_obj = compile(code, 'filename.py', 'exec') # 3. Run the code object (ast)
exec(code_obj)

Alternatively, you can also use the following one-liner to read the code from a file:

code = open('filename.py').read()

Frankly, I don’t see a huge problem with not closing the file if you have a small script and you don’t access the file anywhere else. Python will close it automatically after the code terminates.

Summary

The Python compile() method returns an executable code object as an “Abstract Syntax Tree” that is represented as an ast object.

There are many applications of an ast such as the following: You can pass this code object into the exec() or eval() functions and run it dynamically in your Python code.

This way, you can programmatically create source code and execute it at runtime.

To use the function, you must pass the string code to be executed, the filename, and the execution mode.

For example compile('print("hi")', '<string>', 'exec') would be a valid call that creates a code object consisting of one line print("hi").


I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python compile() first appeared on Finxter.

Posted on Leave a comment

Python eval()

This tutorial shows you how to use Python’s built-in eval() function.

Why Using It? The main application of eval() is to take user input at runtime and run it as a Python expression. This way, you can create a calculator or allow users to perform custom computations on a computing cluster. However, this use also poses the biggest security risk: the user can run byzantine (=harmful) code on your server environment!

How does it work? TLDR;

Python eval(s) parses the string argument s into a Python expression, runs it, and returns the result of the expression. This poses a security risk because a user can use it to run code on your computer. For example, if you allow eval(input()), a user could type os.system('rm -R *') to delete all files in your home directory.

Usage Examples

Learn by example! Here are some examples of how to use the eval() built-in function:

>>> eval('2+2')
4
>>> eval('[1, 2, 3]*3')
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> eval('[x for x in range(10)]')
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> eval('"Alice".replace("e", "")') 'Alic'

You can run any Python code that has a return value within the eval() code. You can even create your own function and run it within eval():

>>> def f(): return 42 >>> eval('f()')
42

This gives you great flexibility in how you use the function to run any string expression you may encounter in Python and it allows you to create Python code programmatically and evaluate it at runtime.

Python eval() - Visual Explanation

Syntax eval()

You can use the eval() method with three different argument lists.

Syntax: 
eval(string)
eval(string, globals)
eval(string, globals, locals) 
Arguments string A string to be evaluated.
globals Optional, default None. A dictionary in which you can define variables that should be globally accessible by the executed object (local namespace).
locals Optional, default None. A dictionary in which you can define variables that should be locally accessible by the executed object (global namespace).
Return Value object Returns the result of parsing the string argument and running it as a Python expression.

Python eval() Return Value

The return value of eval() is a Python object that is the result of parsing the string argument and running it as a Python expression. The code can have side effects which means that it may change the state of your program or even your computer!


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners

[Danger Zone] Python eval(input()) User Input

You can use the eval() function to run code that is typed in dynamically by the user:

def dangerous_function(): # Do nasty stuff like removing files # or creating trojan horses print('You were hacked!') return 42 eval(input())

This is how the user may interact with your code at runtime:

dangerous_function()
You were hacked! 42

You see that the dangerous_function() was executed which could contain all kinds of dangerous code. If you run this on your server, the user may attempt to remove all files on your server! For example, the user may use the command os.system('rm -rf *') to remove all files and folders.

Interactive Jupyter Notebook eval()

Exercise: Run the following interactive code and try to run the dangerous function in the interactive Jupyter notebook!


Python exec() vs eval()

Python’s exec() function takes a Python program, as a string or executable object, and runs it. The eval() function evaluates an expression and returns the result of this expression. There are two main differences:

  • exec() can execute all Python source code, whereas eval() can only evaluate expressions.
  • exec() always returns None, whereas eval() returns the result of the evaluated expression.
  • exec() can import modules, whereas eval() cannot.
exec('x=2;y=2;z=x*y;print(z)')
# 4 print(eval('2*2'))
# 4

Python eval() import

Can you import a Python library within the eval() function? No, you can’t! The import statement is a statement, not an expression. But eval() can only execute expressions. A simple workaround is to create a function with side effects that imports the module within the function body:

def f(): import random return random.randint(0, 9) print(eval('f()'))
# 4

Per default, the eval() function has access to all names in the dir() namespace, so you can also import the library globally and use it within the eval() function:

import random
print(eval('random.randint(0, 9)'))

How to Restrict the Use of Built-in Functions Within eval()

If you don’t want to allow users to access built-in functions, you can restrict this by providing the globals argument as follows:

eval(expression, {'__builtins__': None})

For example:

>>> eval('sum([1, 2, 3])')
6
>>> eval('sum([1, 2, 3])', {'__builtins__': None})
Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> eval('sum([1, 2, 3])', {'__builtins__': None}) File "<string>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable

After restricting the built-in functions in the second call, Python raises an error NoneType object is not subscriptable. This reduces the security risks of your application.

Summary

Python eval(s) parses the string argument s into a Python expression, runs it, and returns the result of the expression.

>>> eval('2+2')
4

This poses a security risk because a user can use it to run code on your computer. For example, if you allow eval(input()), a user could type import os; os.system('rm -R *') to delete all files in your home directory.


I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python eval() first appeared on Finxter.

Posted on Leave a comment

A Guide to Python’s pow() Function

Exponents are superscript numbers that describe how many times you want to multiply a number by itself. Calculating a value raised to the power of another value is a fundamental operation in applied mathematics such as finance, machine learning, statistics, and data science. This tutorial shows you how to do it in Python!

Definition

For pow(x, y), the pow() function returns the value of x raised to the power y. It performs the same function as the power operator ** , i.e. x**y, but differs in that it comes with an optional argument called mod.

Examples without mod

>>> pow(5, 2)
25
>>> pow(-3, 3)
-27
>>> pow(2, -2)
0.25

Parameters and Syntax

pow(base, exp, mod=None)

The pow() function includes two compulsory arguments, base and exp, and one optional argument, mod, whose default value is None. All arguments must be of numeric data type.

Parameter Description
exp A number that represents the base of the function, whose power is to be calculated.
base A number that represents the exponent of the function, to which the base will be raised.
mod A number with which the modulo will be computed.

Return value: The output of base raised to the power exp and will be a numeric data type, int, float or complex, depending on what you input.

Using the pow() function without the mod argument

When using the pow(x, y) function without the optional mod argument, it will perform the same operation as the power operator x**y, raising x to the power y.

Comparison of the two methods

>>> pow(6, 4)
1296
>>> 6 ** 4
1296

The pow() function accepts all numeric data types, i.e. int, float and even complex numbers. In general the return value will depend on what data types you input. The example above shows that both arguments are type int, therefore, an int type is returned. However, if you were to instead use a float number as one or both of the arguments, the function will automatically return a float type.

Examples using float types

>>> pow(3.0, 4)
81.0
>>> pow(4.5, 2.3)
31.7971929089206

As with float type inputs leading to float outputs, the same reasoning applies to complex numbers. If you enter a complex number as one or both of the arguments, a complex number will be returned.

Example using complex numbers

>>> pow(4+2j, 3)
(16+88j)

The return type will also depend on whether your arguments are non-negative or negative, as is shown in the below table.

base exp Return type
Non-negative Non-negative int
Non-negative Negative foat
Negative Non-negative int
Negative Negative float

Examples of return values with different input types

>>> pow(7, 2)
49
>>> pow(4, -5)
0.0009765625
>>> pow(-6, 3)
-216
>>> pow(-9, -4)
0.00015241579027587258

Using the pow() function with a mod argument

What sets the pow() function apart from the ** operator is its third optional argument, mod, which gives you the ability to do a modulo operation within the function. 

The process of operations when using the mod argument is as follows:

If we have pow(x, y, z), the function first performs the task of raising x to the power y and then that result is used to perform the modulo task with respect to z. It would be the equivalent of (x**y) % z .

Examples using mod

>>> pow(14, 7, 5)
4
>>> pow(-8, 3, 5)
3
>>> pow(2, 4, -3)
-2

The general rule for using the mod argument is that all values must be of integer type, the exp argument must be non-negative and the mod argument must be non-zero. However, Python 3.8 now comes with the functionality of computing modular inverses. In this case, the exp argument may be negative, on the condition that base is relatively prime to mod, i.e, the only common integer divisor of base and mod is 1.

So, when using the pow() function with negative exp, the function will perform as follows:

pow(inv_base, -exp, mod)

In other words, the function will compute the modular inverse of base and mod first and then that result will be used in the pow() function as base to be computed as normal with the  exp argument being converted to its non-negative counterpart.

Example of modular inverse

>>> pow(87, -1, 25)
23

In this example, the straight modular inverse is calculated because inv_base will be raised to the power 1.

Example of modular inverse when exp is not -1

>>> pow(34, -5, 19)
10
# The modular inverse of 34 mod 19 is 14, therefore, we end up with the function pow(14, 5, 19)
>>> pow(14, 5, 19)
10

Calculating the nth root of a number using pow()

Unfortunately, Python does not have a built-in function to calculate the nth root of a number. The math module only has a function to calculate square roots, math.sqrt(), therefore, we have to get creative in order to calculate nth roots.

We know that nx is equivalent to x1n. Thus, using this knowledge we can calculate the nth root in Python by using either pow(x, (1/n)) or x**(1/n)

Examples of calculating nth roots

>>> pow(625, (1/4))
4.0
>>> 729**(1/3)
8.999999999999998

Note that performing an nth root calculation will always return a float when not using complex numbers. Since Python’s float type works on approximations, it will often return the approximation rather than the exact number, even when an exact answer is possible. This is demonstrated in the second example above.

When calculating the nth root of a negative number, the return value will be a complex number whether an integer number is possible or not. 

Examples of calculating nth roots of negative bases

>>> pow(-16, (1/2))
(2.4492935982947064e-16+4j)
>>> pow(-27, (1/3))
(1.5000000000000004+2.598076211353316j)

We would expect the second example above, the cubed root of -27, to result in -3, but instead we get a complex number. This is because Python returns the principal root rather than the real root. For an explanation of these different types of roots, you can look up the Fundamental theorem of algebra.

math.pow() Function

In the math module of Python, there is a similar function called math.pow(). To use this we first need to import the math function, thus, the built-in pow() function will be very slightly faster. The main differences between the two functions is that math.pow() does not allow for the optional mod argument and it will always return a float. So if you want to ensure that you get a float result, math.pow() is a better option.

Example of using math.pow()

>>> import math
>>> math.pow(9, 5)
59049.0

When to use the pow() function vs when to use the ** operator

When deciding between using the pow() function or the ** operator, the most important factor to consider would be the efficiency of your code. We can use the timeit.timeit() function from the timeit module to find out how fast Python executes our code.

Examples of using timeit with simple numbers

>>> import timeit
>>> timeit.timeit('pow(5, 2)')
0.25059129999863217
>>> timeit.timeit('5**2')
0.008814800001346157

When performing a simple power computation, the ** operator appears to be much faster.

Examples using modulo

>>> timeit.timeit('pow(52, 2, 4)')
0.7482693000001746
>>> timeit.timeit('52**2 % 4')
0.012026999998852261

The same is true even when we include a modulo operation. 

However, when we want to perform power operations with very large numbers, the pow() function is much quicker, showing that the power of the pow() function lies in executing longer computations.

Examples using large numbers

>>> timeit.timeit('pow(5234, 2341, 124)')
0.9020593000004737
>>> timeit.timeit('5234**2341 % 124')
152.56075580000106

Here the pow() function is extremely fast compared to the ** operator. Therefore, we can generalize these findings by saying that when you want to perform short, simple calculations, the ** operator is the better option, however, if your operations involve very large numbers, the pow() function is much more efficient.

The post A Guide to Python’s pow() Function first appeared on Finxter.

Posted on Leave a comment

How to Get The Current Reference Count of an Object in Python?

The reference count is the number of times an object is referenced by a variable. If an object isn’t referenced anymore, it can be safely removed from the memory—what’s the use of an object nobody cares for anyway? This article shows you how to count the number of references to a Python object.

What’s the Current Reference Count of Python Object

Question: How to get the current reference count of a Python object?

Get Reference Count Object Python

Answer: You can get the current reference count with the sys module’s getrefcount() method. For example, to get the number of times variable x is referenced, run sys.getrefcount(x).

Here’s the general strategy for an arbitrary object:

import sys
print(sys.getrefcount(object))

The following code snippet shows an example of an object x that has a reference count of 13. After creating a new reference y to the object x, the reference count increases by one to 14.

import sys x = 42 print(sys.getrefcount(x))
# 13 y = x print(sys.getrefcount(x))
# 14

You may not have expected that the initial reference count is 13. So, why is that? Let’s see.

Why is the Reference Count Higher Than Expected?

Reason 1: Implicit Helper Variables

Python is a high-level programming language. It provides you many convenience functions and memory management. You don’t have to care about the concrete memory locations of your objects and Python automatically performs garbage collection—that is—removing unused objects from memory. To do all of this work, Python relies on its own structures. Among those, are helper variables that may refer to an object you’ve created. All of this happens under the hood, so even if you don’t refer an object in your code, Python may still have created many references to that object. That’s why the reference count can be higher than expected.

Reason 2: Temporary Reference in getrefcount()

Note that by calling the function getrefcount(x), you create an additional temporary reference to the object x. This alone increases the reference counter by one.

The post How to Get The Current Reference Count of an Object in Python? first appeared on Finxter.

Posted on Leave a comment

Captive User Interfaces — Why You Should Avoid Them

This tutorial shows you the meaning of captive user interfaces and why they’re discouraged under the Unix philosophy. I’ve written this as a first chapter draft for my upcoming book “From One to Zero” to appear in 2020 with San Francisco-based publisher NoStarch.

What’s a Captive User Interface (CUI)?

A captive user interface is a way of designing a program that requires the user to interact with the program in a session before they’ll be able to proceed with their main execution flow. If you invoke a program in your terminal (Windows, MacOS, or Linux), you must communicate with the program before you can go back to the terminal. Examples are mini programs such as SSH, top, cat, vim—as well as programming language features such as Python’s input() function.

Python Example Captive User Interface

Say you create a simple life expectancy calculator in Python. The user must type in their age and it returns the expected number of years left based on a straightforward heuristic. This is a fun project found here.

"If you’re under 85, your life expectancy is 72 minus 80% of your age.
Otherwise it’s 22 minus 20% of your age."

Your initial, bad, Python code is shown here:

def your_life_expectancy(): age = int(input('how old are you? ')) if age<85: exp_years = 72 - 0.8 * age else: exp_years = 22 - 0.2 * age print(f'People your age have on average {exp_years} years left - use them wisely!') your_life_expectancy()

Listing 8-8: Life-expectancy calculator – a simple heuristic – implemented as a captive user interface.

Here are some runs of the code:

>>> how old are you? 10
People your age have on average 64.0 years left - use them wisely!
>>> how old are you? 20
People your age have on average 56.0 years left - use them wisely!
>>> how old are you? 77
People your age have on average 10.399999999999999 years left - use them wisely!

Interactive Jupyter Notebook to Calculate Your Life Expectancy Using a Captive User Interface

In case you want to try it yourself, I’ve created an interactive Jupyter notebook you can run in your browser to calculate your own life expectancy. But, please, don’t take it too serious! Here’s the notebook:

The code makes use of Python’s input() function that blocks the program execution and waits for user input. Without user input, the code doesn’t do anything. This seriously limits the usability of the code.

What if I wanted to calculate the life expectancy for every age from 1 to 100 based on the heuristic and plot it? I’d have to manually type 100 different ages and store the results in a separate file. Then, you’d have to copy&paste the results into a new script to plot it.

The function really does two things: process the user input and calculate the life expectancy. This already violates rule number 3: Make Every Program Do One Thing Well.

But it also violates our rule: don’t use captive user interfaces if possible.

Non-Captive User Interface Python Example

Here’s how the function could’ve been implemented more cleanly:

def your_life_expectancy(age): if age<85: return 72 - 0.8 * age return 22 - 0.2 * age age = int(input('how old are you? '))
exp_years = your_life_expectancy(age)
print(f'People your age have on average {exp_years} years left - use them wisely!')

Listing: Life-expectancy calculator – a simple heuristic – without captive user interface.

The code is functionally identical to the code with captive user interface. However, it has a big advantage: now, you can use the function in different and unexpected—by the initial developer—ways:

import matplotlib.pyplot as plt def your_life_expectancy(age): '''Returns the expected remaining number of years.''' if age<85: return 72 - 0.8 * age return 22 - 0.2 * age # Plot for first 100 years
plt.plot(range(100), [your_life_expectancy(i) for i in range(100)]) # Style plot
plt.xlabel('Age')
plt.ylabel('No. Years Left')
plt.grid() # Show and save plot
plt.savefig('age_plot.jpg')
plt.savefig('age_plot.pdf')
plt.show()

Listing: Code to plot the life expectancy for years 0-99.

The resulting plot is shown in the following figure:

Figure: How the heuristic works for input years 0-99.

Let’s not talk too much about the flaws of this heuristic—it’s crude by design—but focus on how the rule of avoiding captive user interface has helped us produce this plot. Without the rule, we’d have to write a new function, add redundancies and unnecessary complexity. By considering the rule, we’ve simplified the code and opened up all kinds of future programs to use and built-upon the heuristic. Instead of optimizing for one specific use case, we’ve written the code in a general way that can be used by hundreds of different applications.

The post Captive User Interfaces — Why You Should Avoid Them first appeared on Finxter.

Posted on Leave a comment

Python complex() — A Useless Python Feature?

The Python complex() method returns a complex number object. You can either pass a string argument to convert the string to a complex number, or you provide the real and imaginary parts to create a new complex number from those.

This article shows you how to use Python’s built-in complex() constructor. You’ll not only learn how to use it—but also why it is useless and what you should do instead in newer Python versions.

Usage

Learn by example! Here are some examples of how to use the complex() built-in function:

>>> complex(1, -2)
(1-2j)
>>> complex(2, -1)
(2-1j)
>>> complex(2, 2)
(2+2j)
>>> complex(1)
(1+0j)
>>> complex(2)
(2+0j)
>>> complex('42-21j')
(42-21j)

Syntax Complex()

You can use the complex() method with three different argument lists.

Syntax: 
complex(real) # Imaginary Part is 0j
complex(real, img) # Both real and imaginary part are given
complex(string)  # String has format 'x+yj' for real part x and imaginary part y. 
Arguments real The real part of the complex number
img The imaginary part of the complex number
string A string defining the real and imaginary part in the form 'x+yj' or 'x+yJ' where x and y are integers for the real and imaginary parts.
Return Value complex Returns a complex number.

Video Complex()

Interactive Shell Exercise: Understanding Complex()

Consider the following interactive code:

Exercise: Guess the output before running the code.


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


How to Create Complex Number Without complex()

Interestingly, you don’t need the complex() constructor to create a complex number! Instead, newer version of Python have built-in complex number support—just use the the syntax x+yj for real part x and imaginary part y to obtain a complex number.

a = 1+1j
b = 4+42j
c = 0+0j print('Complex Numbers:')
print(a, b, c) print('Types:')
print(type(a), type(b), type(c))

The output is:

Complex Numbers:
(1+1j) (4+42j) 0j
Types:
<class 'complex'> <class 'complex'> <class 'complex'>

Summary

The Python complex() method returns a complex number object. To create a complex number:

  • Pass a string argument to convert the string to a complex number, or
  • Provide the real and imaginary parts to create a new complex number from those.

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:


Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python complex() — A Useless Python Feature? first appeared on Finxter.

Posted on Leave a comment

Python staticmethod()

Static methods are special cases of class methods. They’re bound to a class rather than an instance, so they’re independent on any instance’s state. Python’s built-in function staticmethod() prefixes a method definition as an annotation @staticmethod. This annotation transforms a normal instance method into a static method. The difference between static (class) methods and instance methods is that they don’t require an instance to be callable.

In this tutorial, I’ll show you one of Python’s little-known secrets that separate the intermediates from the experts: static methods. A static method is a special case of a class method. You may know the difference between an instance method and a class method conceptually. (If you don’t, this tutorial is for you.) But do you also know how to create a static method in Python?

If not, read on, because this tutorial will show you!

Syntax Class Method

Syntax: staticmethod(function) # <--- This is considered unpythonic
@staticmethod # <--- As a prefix before the used method

You can declare a static method is the following decorator syntax—this is the most Pythonic way:

class C: @staticmethod def f(arg1, arg2, ...): None C.f(arg1, arg2, ...)

How to Call a Static Method?

There are two ways of calling a static method:

  • You can call it on a class such as C.f(arg1, arg2, ...), or
  • You can call it on an instance such as C().f(arg1, arg2, ...).

In contrast to a class method, Python doesn’t implicitly pass a reference to the class itself as a first argument.

Static Method Application — Factory Pattern

You can use the static method to allow people to create different variants of a Coffee class:

class Coffee: def __init__(self, milk, beans): self.milk = milk # percentage self.coffee = 100-milk # percentage self.beans = beans def __repr__(self): return f'Milk={self.milk}% Coffee={self.coffee}% Beans={self.beans}' @staticmethod def cappuccino(): return Coffee(80, 'Arrabica') @staticmethod def espresso_macchiato(): return Coffee(30, 'Robusta') @staticmethod def latte(): return Coffee(95, 'Arrabica') print(Coffee.cappuccino())
print(Coffee.espresso_macchiato())
print(Coffee.latte())

This is called the factory pattern: the static methods are instance factories—they produce new instances according to their implementations. For example, the Coffee.cappuccino() method creates a special type of Coffee with an initial selection of 80% milk and Arrabica beans.

The output of this code snippet is:

Milk=80% Coffee=20% Beans=Arrabica
Milk=30% Coffee=70% Beans=Robusta
Milk=95% Coffee=5% Beans=Arrabica

Interactive Example Static Method

The following interactive code shell allows you to play with this example and deepen your skills.

Exercise: Can you create another coffee specialty?

Static Method is a Function Decorator

Decorators help to add functionality to existing code without having to modify the code itself. Decorators are so-called because they decorate code, they do not modify the code, but they make the code do different things using decoration. Now that we have understood closures, we can work our way step by step to understanding and using decorators.

The @staticmethod is a function decorator. It’s short for calling staticmethod(m) for the method m that you would decorate.

Here’s an example without using a decorator and by using staticmethod() instead:

class Coffee: def __init__(self, milk, beans): self.milk = milk # percentage self.coffee = 100-milk # percentage self.beans = beans def __repr__(self): return f'Milk={self.milk}% Coffee={self.coffee}% Beans={self.beans}' def cappuccino(): return Coffee(80, 'Arrabica') cappuccino = staticmethod(cappuccino) print(Coffee.cappuccino())

The output is the same:

Milk=80% Coffee=20% Beans=Arrabica

However, this is not the recommended way—use a decorator with the @ annotation instead!

Related Article: Decorators

Static Method vs Instance Method

If you don’t use the @staticmethod annotator, you obtain an instance method per default. The instance method requires that the first argument self is a reference to the instance itself on which the method is called. The static method doesn’t pass any implicit argument. Thus, the difference between static methods and instance methods is that Python passes nothing in the first case while passing the instance (object) as a first implicit argument in the second case.

Here’s a minimal example of a static and an instance method:

class C: @staticmethod def f(): print('hi') # instance method def g(self): None # call static method:
C.f() # call instance method:
C().g()

Static Method vs Class Method

You may know static methods from programming languages such as C++ or Java. They’re methods that exist independently of whether or not you created an instance of the class. That’s why they don’t use any instance variable in the method body. If you want to use a static method in Python, you need to use the @staticmethod annotation rather than the @classmethod annotation. The difference is that static methods don’t expect a reference to either the instance or the class as an implied first argument.

Here’s an example comparing class methods, instance methods, and static methods:

class C: @classmethod def f(cls): None # instance method def g(self): None @staticmethod def h(): None # call class method:
C.f() # call instance method:
C().g() # call static method:
C.h()

Static Method vs Class Method vs Instance Method

To summarize, here’s the difference between the three different types of methods:

  • Static Methods,
  • Class Methods, and
  • Instance Methods.
Instance Method Class Method Static Method
Definition def f(self, arg1, arg2): ... def f(cls, arg1, arg2): ... def f(arg1, arg2): ...
First Argument Reference to Instance Reference to Class No Reference
Usage On instance: C().f(arg1, arg2) On class: C.f(arg1, arg2) On class: C.f(arg1, arg2)
Application Work on data of specific instance Work independently of instance data—but depends on class (e.g., factory). Work independent of instance data and class data (e.g., general computation)

Related Video: Python Class Method

Summary

Static methods are special cases of class methods. They’re bound to a class rather than an instance, so they’re independent on any instance’s state.

Python’s built-in function staticmethod() prefixes a method definition as an annotation @staticmethod. This annotation transforms a normal instance method into a static method.


Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python staticmethod() first appeared on Finxter.

Posted on Leave a comment

Python classmethod()

Python’s built-in function classmethod() prefixes a method definition in a class as an annotation @classmethod. This annotation transforms a normal instance method into a class method. The difference between class and instance method is that Python passes the class itself as a first implicit argument of the method rather than the instance on which it is called.

In this tutorial, I’ll show you one of Python’s little-known secrets that separate the intermediates from the experts: class methods. You may know the difference between an instance method and a class method conceptually. (If you don’t, this tutorial is for you.) But do you also know how to create a class method in Python? If not, read on, because this tutorial will show you!

Syntax Class Method

Syntax: classmethod(function) # <--- This is considered unpythonic
@classmethod # <--- As a prefix before the used method

The most Pythonic way to declare a class method is the following decorator syntax:

class C: @classmethod def f(cls, arg1, arg2, ...): None

How to Call a Class Method?

There are two ways of using a class method:

  • You can call it on a class such as C.f(arg1, arg2, ...), or
  • You can call it on an instance such as C().f(arg1, arg2, ...).

Note that Python implicitly passes the class itself—in Python, everything including a class is an object—as a first argument. Roughly speaking, if you call C.f(arg1, arg2, ...), Python really runs f(C, arg1, arg2, ...) with the class object passed as a first argument.

Example Class Method

When to use a class method? You’ll find that class methods are often used in a factory pattern. A factory pattern is one of the most popular software development patterns: a so-called factory method creates new instances of a certain type. It is responsible for actually creating the instances from a given class definition.

In the following example, you define a class Coffee that represents a real coffee serving in a coffee bar. There are many specialities such as Cappuccino (80% milk, 20% coffee), Espresso Macchiato (30% milk, 70% coffee), and Latte Macchiato (95% milk, 5% coffee).

You use the class methods to allow users to conveniently call factory methods instead of setting the milk level themselves. Instead of calling Coffee(80, 20, 'Arrabica'), a user can just call cappuccino() to create the same instance of the class Coffee. This simplifies the creation of new instances and can improve the readability and usability of your code:

class Coffee: def __init__(self, milk, beans): self.milk = milk # percentage self.coffee = 100-milk # percentage self.beans = beans def __repr__(self): return f'Milk={self.milk}% Coffee={self.coffee}% Beans={self.beans}' @classmethod def cappuccino(cls): return clf(80, 'Arrabica') @classmethod def espresso_macchiato(cls): return cls(30, 'Robusta') @classmethod def latte(cls): return cls(95, 'Arrabica') print(Coffee.cappuccino())
print(Coffee.espresso_macchiato())
print(Coffee.latte())

The three lines of output are:

Milk=80% Coffee=20% Beans=Arrabica
Milk=30% Coffee=70% Beans=Robusta
Milk=95% Coffee=5% Beans=Arrabica

You can run this example in our interactive Jupyter Notebook:

Interactive Example Class Method

Exercise: Can you create another coffee specialty?

Class Method Naming Convention: cls vs self

In the previous example, you’ve seen the naming convention of the first argument of a class method: it’s the three characters cls—short for “class”, but we cannot take this because class is a reserved keyword. You can use any name you want but using the name cls is the expected convention and it’s highly recommended. The naming convention of the first implicit argument of instance methods is the name self.

Related Article: Self in Python

Class Method is a Function Decorator

Decorators help to add functionality to existing code without having to modify the code itself. Decorators are so-called because they decorate code, they do not modify the code, but they make the code do different things using decoration. Now that we have understood closures, we can work our way step by step to understanding and using decorators.

The @classmethod is a function decorator. It’s short for calling classmethod(m) for the method m that you would decorate.

Here’s the same simplified coffee example without using a decorator and by using classmethod() instead:

class Coffee: def __init__(self, milk, beans): self.milk = milk # percentage self.coffee = 100-milk # percentage self.beans = beans def __repr__(self): return f'Milk={self.milk}% Coffee={self.coffee}% Beans={self.beans}' def cappuccino(cls): return cls(80, 'Arrabica') Coffee.cappuccino = classmethod(Coffee.cappuccino)
print(Coffee.cappuccino())

The output is the same:

Milk=80% Coffee=20% Beans=Arrabica

However, this is not the recommended way—use a decorator with the @ annotation instead!

Related Article: Decorators

Class Method vs Instance Method

If you don’t use the @classmethod annotator, you obtain an instance method per default. The instance method requires that the first argument self is a reference to the instance itself on which the method is called. The class method expects a reference to the class, not the instance, as a first argument cls. Thus, the difference between class and instance methods is that Python passes the class rather than the instance (object) as a first implicit argument.

Here’s a minimal example of a class and an instance method:

class C: @classmethod def f(cls): None # instance method def g(self): None # call class method:
C.f() # call instance method:
C().g()

There are two ways of using a class method:

  • You can call it on a class such as C.f(arg1, arg2, ...), or
  • You can call it on an instance such as C().f(arg1, arg2, ...).

But there’s only one way of using an instance method:

  • You can call it on an instance such as C().g().

Class Method vs Static Method

You may know static methods from programming languages such as C++ or Java. They’re methods that exist independently of whether or not you created an instance of the class. That’s why they don’t use any instance variable in the method body. If you want to use a static method in Python, you need to use the @staticmethod annotation rather than the @classmethod annotation. The difference is that static methods don’t expect a reference to either the instance or the class as an implied first argument.

Here’s an example comparing class methods, instance methods, and static methods:

class C: @classmethod def f(cls): None # instance method def g(self): None @staticmethod def h(): None # call class method:
C.f() # call instance method:
C().g() # call static method:
C.h()

Summary

Python’s built-in function classmethod() prefixes a method definition in a class as an annotation @classmethod.

class C: @classmethod def f(cls, arg1, arg2, ...): None

This annotation transforms a normal instance method into a class method.

The difference between class and instance method is that Python passes the class itself as a first implicit argument of the method rather than the instance on which it is called:

class C: @classmethod def f(cls, arg1, arg2, ...): None # instance method def g(self, arg1, arg2, ...): None

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python classmethod() first appeared on Finxter.

Posted on Leave a comment

What’s the Most Pythonic Way to Alias Method Names?

In contrast to a normal Python method, an alias method accesses an original method via a different name—mostly for programming convenience. An example is the iterable method __next__() that can also be accessed with next(). You can define your own alias method by adding the statement a = b to your class definition. This creates an alias method a() for the original method b().

Example Alias

Here’s a minimal example:

class Car: def change_oil(self): print('oil changed') def drive_to_cinema(self): print('movie watched') # Alias Method Names oil = change_oil cinema = drive_to_cinema # Create new car object
porsche = Car() # Test original and alias method calls
porsche.change_oil()
# oil changed porsche.oil()
# oil changed porsche.cinema()
# movie watched porsche.drive_to_cinema()
# movie watched

You create one Car object porsche. The original method change_oil() may be too lengthy, so you decide to add an alias method to the class definition oil = change_oil. Now, you can access the same method in two different ways: porsche.change_oil() or simply porsche.oil().

Interactive Notebook

You can run this code interactively in our Jupyter Notebook:

Just click to the code and run it in a new tab!

Are Alias Methods Pythonic at All?

You should note, however, that using an alias at all is not very Pythonic! The Zen of Python clearly states that there should be one, and only one, way to accomplish a thing.

>>> import this
The Zen of Python, by Tim Peters  Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!

What is Aliasing Anyway?

Aliasing happens if you access the same memory location via different symbolic names. If you change the data through one name, the other name pointing to the same data will see the change as well!

Alias Example

In the graphic, both variables a and b point to the same imaginary object with value 42. You delete the object with a.delete(). Now, both variables a and b point to the empty object. Variable b sees a change—even though it didn’t change anything!

“In computing, aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program. Thus, modifying the data through one name implicitly modifies the values associated with all aliased names, which may not be expected by the programmer. As a result, aliasing makes it particularly difficult to understand, analyze and optimize programs. Aliasing analysers intend to make and compute useful information for understanding aliasing in programs.” — Wikipedia

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post What’s the Most Pythonic Way to Alias Method Names? first appeared on Finxter.