Categories
Python Answers

How to implement multiple constructors with Python?

Sometimes, we want to implement multiple constructors with Python.

In this article, we’ll look at how to implement multiple constructors with Python.

How to implement multiple constructors with Python?

To implement multiple constructors with Python, we can add class methods into our class that calls the constructor.

For instance, we write

class Cheese(object):
    def __init__(self, num_holes=0):
        self.number_of_holes = num_holes

    @classmethod
    def random(cls):
        return cls(randint(0, 100))

    @classmethod
    def slightly_holey(cls):
        return cls(randint(0, 33))

    @classmethod
    def very_holey(cls):
        return cls(randint(66, 100))

gouda = Cheese()
havarti = Cheese.random()

to create the Cheese class with the __init__ method.

Then we can create static methods that calls __init__ with different arguments.

Next, we have

gouda = Cheese()
havarti = Cheese.random()

to use the different methods to create Cheese objects.

Conclusion

To implement multiple constructors with Python, we can add class methods into our class that calls the constructor.

Categories
Python Answers

How to read a file in reverse order with Python?

Sometimes, we want to read a file in reverse order with Python.

In this article, we’ll look at how to read a file in reverse order with Python.

How to read a file in reverse order with Python?

To read a file in reverse order with Python, we can use the reversed function.

For instance, we write

for line in reversed(list(open("filename"))):
    print(line.rstrip())

to call open to open the 'filename' file.

Then we call list to convert the iterator to a list.

Next, we call reversed to reverse the list.

And then we use a for loop to loop through the returned lines.

Conclusion

To read a file in reverse order with Python, we can use the reversed function.

Categories
Python Answers

How to extract a floating number from a string with Python?

Sometimes, we want to extract a floating number from a string with Python.

In this article, we’ll look at how to extract a floating number from a string with Python.

How to extract a floating number from a string with Python?

To extract a floating number from a string with Python, we call the re.findall method.

For instance, we write

import re

d = re.findall("\d+\.\d+", "Current Level: 13.4db.")

to call re.findall with a regex string to get the decimal numbers from the string.

Then d is a list of numbers found in the string in the 2nd argument.

Conclusion

To extract a floating number from a string with Python, we call the re.findall method.

Categories
Python Answers

How to create dictionary of separate variables with Python?

Sometimes, we want to create dictionary of separate variables with Python.

In this article, we’ll look at how to create dictionary of separate variables with Python.

How to create dictionary of separate variables with Python?

To create dictionary of separate variables with Python, we can get the local variables in a dict with locals.

For instance, we write

blah = 1
blah_name = [k for k, v in locals().items() if v is blah][0]

to get an iterator of tuples of the local variables with

k for k, v in locals().items()

Then we get the one from the iterator that has value blah with

if v is blah

And then we use index 0 to get the first entry that matches the condition.

Conclusion

To create dictionary of separate variables with Python, we can get the local variables in a dict with locals.

Categories
Python Answers

How to test if a string contains one of the substrings in a list, in Python Pandas?

Sometimes, we want to test if a string contains one of the substrings in a list, in Python Pandas.

In this article, we’ll look at how to test if a string contains one of the substrings in a list, in Python Pandas.

How to test if a string contains one of the substrings in a list, in Python Pandas?

To test if a string contains one of the substrings in a list, in Python Pandas, we can call str.contains.

For instance, we erite

searchfor = ['og', 'at']
r = s[s.str.contains('|'.join(searchfor))]

to call s.str.contains with '|'.join(searchfor) to search for strings listed in searchfor in our series s.

Conclusion

To test if a string contains one of the substrings in a list, in Python Pandas, we can call str.contains.