In Python regular expressions, one of the common tasks is matching either one character or another. we can done this easily by using the re module along with the pipe symbol |, which acts as an OR operator. In this article, we’ll explore how to match either 'a' or 'b' in a string using regex. The following are the various methods included. Using re.search() with the | symbol The re.search() combined with the | (OR) symbol allows us to search for multiple patterns within a single string. It returns a match object if any of the patterns are found. The ... Read More
A timestamp represents a specific point in time as a numerical value. It typically measures the number of seconds that have elapsed since the Unix epoch, which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC). The main reasons behind timestamp variations on different machines include differences in time zones, system clocks, locale settings, and the use of UTC and local time (such as IST, PST, or CET). In this article, we will explore timestamp differences across different machines by considering the following key factors. Different System Time Zones If each system were set to different time zones like ... Read More
A millisecond UNIX timestamp is a number that shows how many milliseconds have elapsed since the beginning of the Unix epoch, which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC) up to the current moment or specified date and time. Instead of counting time in days, hours, or minutes, it counts in milliseconds (1 second = 1000 milliseconds). In Python, the common way to convert a Datetime object to a milliseconds timestamp involves using the strptime() function (to parse a string into a Datetime object), then converting this datetime object into a UNIX timestamp by using the timestamp() method, ... Read More
Python's re module provides various tools for pattern matching using regular expressions (regex). With the help of regex, we can define flexible patterns that match or exclude particular characters or sequences. In this article, we will focus on how to match everything except spaces and newlines using regular expressions. The following are the methods involved to match the regex pattern, except for space and new line. Using re.findall() Method Using re.split() Method Replace Spaces and Newlines with Empty String Match Words Without Space/Newline Using re.findall() Instead ... Read More
Both Python and JavaScript have unique ways of representing date and time data. To compare Python datetime objects with JavaScript Date objects, we must ensure that both are converted to a common format, such as ISO 8601 strings or Unix timestamps (milliseconds since epoch). The following are two major differences between Python (datetime) and JavaScript (date) objects. Month Representation: JavaScript uses a 0-indexed month (0 for January, 11 for December), while Python uses a 1-indexed month (1 for January, 12 for December). Default Time Zone: Python defaults to UTC, while JavaScript defaults to ... Read More
Python's regular expressions provide various ways to search and manipulate strings. They are used to define search patterns that can be matched in text data. These patterns are defined using a set of characters known as metacharacters, which carry special meaning in regex. Their behaviour can change when used inside character classes (also known as character sets or square brackets '[]'). Understanding how metacharacters are interpreted within character classes is crucial for writing accurate and effective regular expressions. Understanding Character Classes In Python regex, character classes are denoted by square brackets '[ ]', which define a set of characters or ... Read More
While working with databases like MySQL, it’s necessary to store dates in a numeric format, especially for timestamps. MySQL commonly uses Unix timestamps, which are 10-digit numbers representing the number of seconds since January 1, 1970 (known as the epoch). The following are the different methods from the time and datetime modules to convert Python date formats into a 10-digit format (Unix timestamp) suitable for use with MySQL. Using mktime() Method Using strptime() + mktime() Combination Using timestamp() Method Using mktime() Method The mktime() method from ... Read More
In Python, while working on date and time, converting time seconds to h:m:s (Hours: Minutes: Seconds) format can be done using simple arithmetic operations and built-in modules like datetime and time. The following are several ways to convert time seconds to h:m:s format. Using arithmetic operations (Naive Method) Using the timedelta class of the datetime module Using time.strftime() with time.gmtime() Using arithmetic operations (Naive method) This is the simplest way to convert seconds into time in H:M:S format. We use basic mathematical operations like division and modulo ... Read More
In Python, measuring the execution time of a function or block of code can be done using several functions and methods provided by built-in modules like time and datetime. The following are the methods to measure the execution time of Python functions. Using time.time() Method Using time.process_time() Function Using datetime.now() Function Using time.time() Method The time.time() method (returns the current UTC) can measure how long a function takes to run. It returns the current time in seconds since January 1, 1970 (known as the Unix epoch). To ... Read More
Python provides the time and datetime modules to convert a DateTime string into integer milliseconds. Key functions include time.time(), which gives the current time in seconds, and datetime.timestamp(), which converts datetime objects directly into seconds since the epoch. By multiplying these values by 1000, we can get the time in milliseconds. Using time.time() Method The time module in Python provides various methods and functions related to time. Here we use the time.time() method to get the current CPU time in seconds. The time is calculated since the epoch, which returns a floating-point number expressed in seconds. This value is multiplied ... Read More