Saturday, 17 April 2021

Introduction to Python re module

The re module in Python is used to work with regular expressions. It provides functions to search for patterns in strings, and to perform substitutions and splits. Some common functions include:

  • search(): searches for a match to a pattern in a string
  • findall(): returns all non-overlapping matches of a pattern in a string
  • sub(): replaces all occurrences of a pattern in a string with a replacement string
  • split(): splits a string by a specified pattern

The re module also includes several functions for compiling and working with regular expression patterns, including:

  • compile(): compiles a regular expression pattern into a pattern object
  • match(): attempts to match a pattern at the start of a string
  • fullmatch(): attempts to match a pattern against all of a string

Regular expressions are a powerful tool, They are mostly used to match or find the pattern in the string, You can use special characters and sets to define patterns, and you can use groups and flags to modify the behavior of the match.

It's important to note that regular expressions can be quite complex and hard to read, So, It's always a good idea to use comments in the pattern.

here are a few examples of how the re module can be used in Python:

  1. Finding all occurrences of a pattern in a string:

     import re
    
     text = "The cat is in the hat"
    
     # Find all occurrences of "at" in the text
     matches = re.findall("at", text)
    
     print(matches) 
     # Output: ['at', 'at']
    
  2. Replacing all occurrences of a pattern in a string:

     import re
    
     text = "The cat is in the hat"
     # Replace all occurrences of "cat" with "dog"
     new_text = re.sub("cat", "dog", text)
     print(new_text) 
     # Output: "The dog is in the hat"
    
  3. Splitting a string by a pattern:

     import re
    
     text = "The,cat,is,in,the,hat"
    
     # Split the text by ","
     parts = re.split(",", text)
    
     print(parts) 
     # Output: ['The', 'cat', 'is', 'in', 'the', 'hat']
    
  4. Matching a pattern at the start of a string:

     import re
    
     text = "The cat is in the hat"
    
     # Check if the text starts with "The"
     match = re.match("The", text)
    
     if match:
         print("Text starts with 'The'")
     else:
         print("Text does not start with 'The'")
    
     # Output: Text starts with 'The'
    
  5. Using groups to extract parts of a match:

     import re
    
     text = "The cat is in the hat"
    
     # Find all occurrences of "at" preceded by a word
     matches = re.findall(r"(\w+)at", text)
    
     print(matches) 
     # Output: ['cat', 'hat']
    
  6. Using a flag to make the search case-insensitive:

     import re
    
     text = "The Cat is in the Hat"
    
     # Find all occurrences of "cat" or "Cat"
     matches = re.findall("cat", text,re.IGNORECASE)
    
     print(matches) 
     # Output: ['Cat']
    
  7. Using the search() function to find a match:

     import re
    
     text = "The cat is in the hat"
    
     # Search for the first occurrence of "cat"
     match = re.search("cat", text)
    
     if match:
         print("Found a match:", match.group())
     else:
         print("No match found.")
    
     # output: Found a match: cat
    
  8. Using the compile()function to create a pattern object:

     import re
    
     text = "The cat is in the hat"
    
     # Compile a regular expression pattern
     pattern = re.compile("cat")
    
     # Search for the first occurrence of the pattern in the text
     match = pattern.search(text)
    
     if match:
         print("Found a match:", match.group())
     else:
         print("No match found.")
    
  9. Using the finditer() function to find all matches and iterate over them:

     import re
    
     text = "The cat is in the hat. The bat is in the mat."
    
     # Find all occurrences of "at"
     matches = re.finditer("at", text)
    
     # Iterate over the matches
     for match in matches:
         print("Found a match:", match.group())
    
     #Output:
     # Found a match: at
     # Found a match: at
     # Found a match: at
     # Found a match: at
    
  10. Using the escape()function to escape special characters in a string:

     import re
    
     text = "The .*+?^$[]{}\|() cat is in the hat"
    
     # Escape special characters in the text
     escaped_text = re.escape(text)
    
     print(escaped_text) 
     # Output: "The \.\*\+\?\^\$\[\]\{\}\|\(\) cat is in the hat"
    
  11. Using the purge()function to clear the regular expression cache.

     import re
    
     re.purge()
    

These are just a few examples of how the remodule can be used in Python. There are many more functions and options available in the remodule, so I recommend reading the official documentation for more information and examples.https://docs.python.org/3/library/re.html

I hope these examples help you understand the basics of working with regular expressions in Python.

Sunday, 4 April 2021

Getting Started with Linux command line

The Linux command line is a powerful tool for interacting with your computer and performing various tasks. Here are the basic steps to get started with the Linux command line:

  1. Open a terminal window. On most Linux distributions, you can open the terminal by pressing the key combination Ctrl + Alt + T.
  2. Use the ls command to list the files and directories in the current directory.

     ls
    
  3. Use the cd command to change the current directory. For example, to change to the home directory, you would use the command:

     cd /home/ubuntu/
    
  4. Use the pwd command to display the current working directory.

     pwd
    
  5. Use the mkdir command to create a new directory. For example, to create a new directory called "myfolder", you would use the command:

     mkdir myfolder
    
  6. Use the touch command to create a new empty file. For example, to create a new file called "myfile.txt", you would use the command:

     touch myfile.txt
    
  7. Use the rm command to delete a file or directory. For example, to delete the file "myfile.txt", you would use the command: (run this carefully)

     rm myfile.txt
    
  8. Use the cp command to copy a file or directory. For example, to copy the file "myfile.txt" to a new file called "myfile_backup.txt", you would use the command:

     cp myfile.txt myfile_backup.txt
    
  9. Use the mv command to move or rename a file or directory. For example, to rename the file "myfile.txt" to "mynewfile.txt", you would use the command:

     mv myfile.txt mynewfile.txt
    

Other:

  1. "cat" - Displays the contents of a file.
  2. "grep" - Searches for a specific string in a file or multiple files.
  3. "find" - Searches for files based on various criteria such as name, size, and time modified.
  4. "man" - Displays the manual pages for a command.
  5. "chmod" - Changes the permissions of a file or directory.
  6. "sudo" - Allows a user to run a command with administrative privileges.

These are just a few basic examples of how to use the Linux command line. There are many more commands and options available, and it's worth exploring and learning more about the command line.

It's important to note that the command line uses case-sensitive, so be careful of the case when you type the command.

Saturday, 20 March 2021

Python strptime() vs Python strftime()

The strftime() method returns a string representing date and time using date , time or datetime object. So it is used to convert the date-time object into string format.

Let’s take a look at an example:

    from datetime import datetime
    t = datetime.now()
    print('current-time--->', t)
 #output
    current-time---> 2023-01-10 23:16:42.511274

now let's play around strftime
 
    print(t.strftime('%y-%m-%d'))
output:-->  
    23-01-10
    print(t.strftime('%Y-%m-%d'))
output:--> 
    2023-01-10
    print(t.strftime('%y-%m-%d %H:%M'))
output:--> 
    23-01-10 23:16

So basically we can extract any kind of data from date-time object in string format.

The strptime() method creates a datetime object from the given string. It used to convert a given string into python datetime object.

In the same way, let’s look at an example for strptime.

    from datetime import datetime
    t_date = '2023-01-10'
    t_date_obj = datetime.strptime(t_date, '%Y-%m-%d')
output:-->
    datetime.datetime(2023, 1, 10, 0, 0)
 lets add a time as well.
    t_date_time = '2023-01-10T10:10'
    t_date_time_obj = datetime.strptime(t_date, '%Y-%m-%dT%H:%M')
output:-->
    datetime.datetime(2023, 1, 10, 10, 10)

Here t_date_obj and t_date_time_obj now became regular python datetime object and you can start treating them as datetime object further in your program.

It’s really helpful in time formatting, ingesting data in different time format, date-to-string conversion etc.

Do check out the official python documentation for the full list of abbreviations.

Directive Meaning Example
%a Weekday as locale’s abbreviated name. Sun, Mon, …, Sat (en_US);So, Mo, …, Sa (de_DE)
%A Weekday as locale’s full name. Sunday, Monday, …, Saturday (en_US);Sonntag, Montag, …, Samstag (de_DE)
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 0, 1, …, 6
%d Day of the month as a zero-padded decimal number. 01, 02, …, 31

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior