Categories
Python Answers

How to check whether a file is empty or not with Python?

Sometimes, we want to check whether a file is empty or not with Python.

In this article, we’ll look at how to check whether a file is empty or not with Python.

How to check whether a file is empty or not with Python?

To check whether a file is empty or not with Python, we can use the os.stat method.

For instance, we write

import os

is_empty = os.stat("file.txt").st_size == 0

to get the size of file.txt with os.stat.

We get the size in bytes with st_size.

And if it’s 0, then the file is empty.

Conclusion

To check whether a file is empty or not with Python, we can use the os.stat method.

Categories
Python Answers

How to use method overloading in Python?

Sometimes, we want to use method overloading in Python.

In this article, we’ll look at how to use method overloading in Python.

How to use method overloading in Python?

To use method overloading in Python, we can use the pythonlangutil module.

To install it, we run

pip install pythonlangutil==0.1

Then we can use it by writing

from pythonlangutil.overload import Overload, signature

class A:
    @Overload
    @signature()
    def foo(self):    
        print('first method')

    @foo.overload
    @signature("int")
    def foo(self, i):
        print('second method', i)

to define the class A with 2 foo methods.

This is possible since we have the decorators provided by pythonlangutil called to modify the methods.

The original foo method has the Overload and signature decorators called on it.

And then 2nd foo method had the foo.overload decorator and the signature decorators called it.

We call signature with 'int' to make sure i is an int.

Conclusion

To use method overloading in Python, we can use the pythonlangutil module.

Categories
Python Answers

How to test if lists share any items in Python?

Sometimes, we want to test if lists share any items in Python.

In this article, we’ll look at how to test if lists share any items in Python.

How to test if lists share any items in Python?

To test if lists share any items in Python, we can use the & operator after converting the lists to sets.

For instance, we write

bool(set(a) & set(b))

to converts lists a and b to sets with set.

Then we use the & operator to get the intersection of the sets.

And then we call bool to check if the intersection is empty or not.

Conclusion

To test if lists share any items in Python, we can use the & operator after converting the lists to sets.

Categories
Python Answers

How to write output in same place on the console with Python?

Sometimes, we want to write output in same place on the console with Python.

In this article, we’ll look at how to write output in same place on the console with Python.

How to write output in same place on the console with Python?

To write output in same place on the console with Python, we call print with the end argument set to an empty string.

For instance, we write

import time

for i in range(100):
    time.sleep(0.1)
    print('Downloading File foo.txt [%d%%]\r'%i, end="")

to call print with end set to an empty string to print the text in the same line by overwriting the existing output.

Conclusion

To write output in same place on the console with Python, we call print with the end argument set to an empty string.

Categories
Python Answers

How to include non-Python files with setup.py?

Sometimes, we want to include non-Python files with setup.py.

In this article, we’ll look at how to include non-Python files with setup.py.

How to include non-Python files with setup.py?

To include non-Python files with setup.py, we can call the setup function with the package_data argument.

For instance, we write

from setuptools import setup, find_packages

setup(
    name='your_project_name',
    version='0.1',
    description='A description.',
    packages=find_packages(exclude=['ez_setup', 'tests', 'tests.*']),
    package_data={'': ['license.txt']},
    include_package_data=True,
    install_requires=[],
)

to call setup with package_data set to {'': ['license.txt']} to include license.txt in our package.

Having empty string as the key means include the file in all packages.

Conclusion

To include non-Python files with setup.py, we can call the setup function with the package_data argument.