Rajendra Dharmkar has Published 345 Articles

What are the best practices to organize Python modules?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 28-Apr-2025 12:30:19

512 Views

Organizing Python modules efficiently is important to maintain scalability and collaboration. Following best practices can make your project easier to understand and use. In this article, we will discuss about an structure approach to organize Python modules with a sample project and some of the best practices to organize. Sample ... Read More

How to import other Python files?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 31-May-2024 12:20:05

96K+ Views

The task or process of importing other files in Python allows you to use functions, classes, or variables defined in those files within your current Python script. In this article, we will explore different ways to import other Python files, including built-in modules, user-defined modules, and importing specific functions or ... Read More

How can I apply an offset on the current time in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 04-Nov-2023 01:13:51

4K+ Views

Whenever you want to add or subtract(apply an offset) to a date/time, use a datetime.datetime(), then add or subtract datetime.timedelta() instances. A timedelta object represents a duration, the difference between two dates or times. The timedelta constructor has the following function signature − datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]) ... Read More

How to simulate scanf() method using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 13:26:24

7K+ Views

According to Python documentationPython does not currently have an equivalent to scanf(). Regular expressions are generally more powerful, though also more verbose, than scanf() format strings. The table below offers some more-or-less equivalent mappings between scanf() format tokens and regular expressions.scanf() TokenRegular Expression%c.%5c.{5}%d[-+]?\d+%e, %E, %f, %g[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?%i[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)%o[-+]?[0-7]+%s\S+%u\d+%x, %X[-+]?(0[xX])?[\dA-Fa-f]+To extract the filename and ... Read More

How to write a regular expression to match either a or b in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 13:22:22

5K+ Views

The following code uses a regular expression '(a|b)' to match a or b in the given Python string.We are also using the flag re.I to ignore case of a or b while matching. Example import re s = 'Bank of Baroda' print(re.findall(r'(a|b)', s, re.I))OutputThis gives the output['B', 'a', 'B', ... Read More

How to subtract Python timedelta from date in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 13:16:07

11K+ Views

You can subtract a day from a Python date using the timedelta object. You need to create a timedelta object with the amount of time you want to subtract. Then subtract it from the date. Example from datetime import datetime from datetime import timedelta today = datetime.today() yesterday = today ... Read More

How to convert unix timestamp string to readable date in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 13:13:20

5K+ Views

You can use the fromtimestamp() function from the datetime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the datetime object corresponding to the timestamp.  Exmaple import datetime timestamp = datetime.datetime.fromtimestamp(1500000000) print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))OutputThis will give the output −2017-07-14 08:10:00Read More

How to use variables in Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 06:51:00

10K+ Views

The following code demonstrates the use of variables in Python regex.The variable cannot contain any special or meta characters or regular expression. We just use string concatenation to create a string.Example import re s = 'I love books' var_name = 'love' result = re.search('(.+)'+var_name+'(.+)', s) print result var_name = 'hate' ... Read More

How can we do date and time math in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 02:07:05

3K+ Views

It is very easy to do date and time maths in Python using timedelta objects. Whenever you want to add or subtract to a date/time, use a datetime.datetime(), then add or subtract datetime.timedelta() instances. A timedelta object represents a duration, the difference between two dates or times. The timedelta constructor ... Read More

How to convert Python date in JSON format?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 01:50:21

3K+ Views

There is no standard JSON format for dates. Although JavaScript does have a standard date format that is human readable, sorts correctly, includes fractional seconds(which can help re-establish chronology) and  conforms to ISO 8601. You can convert a Python date to the JS date format using the strftime function and ... Read More

1 2 3 4 5 ... 35 Next
Advertisements