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

Thursday, 4 March 2021

Python Debugging Techniques for Beginners

Debugging is an important part of the software development process, and Python provides several techniques for beginners to debug their code. Here are some common techniques:

  1. Print statements: One of the simplest and most widely used techniques is to insert print statements in your code to check the values of variables and expressions at different points in the execution. This can help you identify where the problem is occurring and what the state of the program is at that point.
  2. The pdb library: Python includes a built-in library called pdb that provides a command-line interface for debugging. You can insert the statement import pdb; pdb.set_trace() at the point where you want to start debugging, and the program will enter the pdb interactive mode, allowing you to step through the code and inspect variables.
  3. The ipdb library: ipdb is an improved version of pdb library, it allows you to use the same interface as pdb but with some added features like syntax highlighting and tab completion.
  4. The breakpoint() function: Python 3.7 introduces a built-in function breakpoint() that is similar to pdb.set_trace() but it does not require importing pdb, it is built-in to the interpreter.
  5. IDEs and text editors with debugging support: Many IDEs and text editors have built-in support for debugging Python code, such as PyCharm, VSCode, and Sublime Text. These tools provide a graphical user interface that allows you to set breakpoints, step through code, and inspect variables.
  6. Assert statements: Using assert statements you can check if a certain condition is true during runtime and if it isn't it will raise an AssertionError, you can use this to check if the code is working as expected.
  7. logging: Logging is a way to record information about your code's execution. It can be used to record messages that can help you understand what is happening in your code.
  8. try-except block: Using try-except blocks to catch and handle specific errors, can help you isolate and fix the issue, and also can provide the user with a meaningful message.

While debugging can be a frustrating process, these techniques can help you quickly identify and fix errors in your code, allowing you to move on to the next step in your development process. It's important to try different techniques to find the one that works best for you and your specific needs.

 Do Comment what's your favorite?