Friday 30 April 2021

Getting Started with Git - part one

Git is a version control system that allows you to track changes to your code and collaborate with others on software development projects. Here are the basic steps to get started with Git:

  1. Install Git on your computer. You can download the latest version of Git from the official website (https://git-scm.com/downloads).
  2. Open a command line or terminal window and navigate to the folder where you want to create your Git repository.
  3. Use the git init command to initialize a new repository in the current folder. This command creates a new subfolder called ".git" that contains the necessary files for the repository.
  4. Use the git add command to stage the files that you want to include in the next commit. For example, git add file1.txt will stage the file "file1.txt" for the next commit.
  5. Use the git commit command to save the staged changes to the repository. The m option allows you to add a message describing the changes. For example, git commit -m "Initial commit" will create the first commit with the message "Initial commit".
  6. Use the git status command to check the current status of the repository. This command will show which files have been modified, which files have been staged for the next commit, and which branch you are currently working on.
  7. Use the git log command to see a list of all commits in the repository, along with their corresponding messages.
  8. Use the git branch command to manage branches in your repository. You can use git branch to list existing branches, git branch newbranch to create a new branch, and git checkout branch to switch to a different branch.
  9. Finally, the git push origin <branch_name> (ex git push origin main) command to push the changes to remote repository.
These are the basic steps to get started with Git. As you become more familiar with Git, you can start using more advanced features such as branching, merging, and working with remote repositories.

It's important to note that Git is distributed version control system so you can work on a local repository and then push/pull changes to a remote repository. Most developers use Github, Gitlab, and Bitbucket as remote repositories.

Do comment if you're facing any issue on git.

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.