Categories
Python Answers

How to write a Python module or package?

Sometimes, we want to write a Python module or package.

In this article, we’ll look at how to write a Python module or package.

How to write a Python module or package?

To write a Python module or package, we just create a file with the .py extension.

For instance, we create hello.py and write

def hello_world():
   print("hello")

to add the hello_world function in it.

Then in another .py file in the same folder, we write

import hello

hello.hello_world()

to import hello.py as hello and call the hello_world function in it with

hello.hello_world()

If we have more than one .py file in a folder, then we need to put an init.py file inside the folder to let us import all the files in the folder.

Conclusion

To write a Python module or package, we just create a file with the .py extension.

Categories
Python Answers

How to generating permutations with repetitions with Python?

Sometimes, we want to generating permutations with repetitions with Python.

In this article, we’ll look at how to generating permutations with repetitions with Python.

How to generating permutations with repetitions with Python?

To generating permutations with repetitions with Python, we can compute the cartesian product with itertools.product.

For instance, we write

import itertools

x = [1, 2, 3, 4, 5, 6]
prod = [p for p in itertools.product(x, repeat=2)]

to call itertools.product with list x and repeat set to 2 to get all permutations of any 2 items in list x with repetition.

Then we put the items in a list and assign it to prod.

Conclusion

To generating permutations with repetitions with Python, we can compute the cartesian product with itertools.product.

Categories
Python Answers

How to do equation parsing in Python?

Sometimes, we want to do equation parsing in Python.

In this article, we’ll look at how to do equation parsing in Python.

How to do equation parsing in Python?

To do equation parsing in Python, we can use the compiler module.

For instance, we write

import compiler

eq = "sin(x)*x**2"
ast = compiler.parse(eq)

to call compiler.parse with eq to return an abstract syntax tree object of the given expression in string eq.

Conclusion

To do equation parsing in Python, we can use the compiler module.

Categories
Python Answers

How to extract part of a regex match with Python?

Sometimes, we want to extract part of a regex match with Python.

In this article, we’ll look at how to extract part of a regex match with Python.

How to extract part of a regex match with Python?

To extract part of a regex match with Python, we can use the := operator.

For instance, we write

pattern = '<title>(.*)</title>'
text = '<title>hello</title>'

if match := re.search(pattern, text, re.IGNORECASE):
  title = match.group(1)

to call re.searcg with the pattern to find the pattern in the text.

We use re.IGNORECASE to find case insensitive matches.

Then we get the match object by using the := with the results returned by re.search.

And we call match.group to find the match for the capturing group.

Conclusion

To extract part of a regex match with Python, we can use the := operator.

Categories
Python Answers

How to do Timezone conversion with Python?

Sometimes, we want to do Timezone conversion with Python.

In this article, we’ll look at how to do Timezone conversion with Python.

How to do Timezone conversion with Python?

To do Timezone conversion with Python, we can use the pytz module.

For instance, we write

import datetime
import pytz

dt_today = datetime.datetime.today()
dt_london = dt_today.astimezone(pytz.timezone('Europe/London'))

to call datetime.datetime.today to get today’s datetime.

Then we convert the datetime to the London time zone with the astimezone method called with the time zone returned by pytz.timezone called with the time zone string.

Conclusion

To do Timezone conversion with Python, we can use the pytz module.