Skip to main content

Posts

Showing posts with the label Python

How to debug Python code in VS Code with arguments passed from command line

 Debugging Python code in VS Code with arguments passed from the command line is a straightforward process. Here are the steps you can follow: 1. Open the Python file that you want to debug in VS Code. 2. Set breakpoints in the code where you want to pause and inspect variables. 3. Open the Debug panel in VS Code by clicking on the Debug icon in the Activity Bar or by pressing `Ctrl+Shift+D` on Windows or `Cmd+Shift+D` on Mac. 4. Click on the "create a launch.json file" button and select "Python" as the environment. 5. In the launch.json file that opens, modify the "args" attribute to include the command-line arguments you want to pass to the Python script. For example:    ```    "args": ["arg1", "arg2"]    ```    Replace "arg1" and "arg2" with the actual arguments you want to pass. NOTE: Add the below property in the launch.json file.     "purpose" : [ "debug-in-terminal" ] 6. Save the ...

Regular Expressions

Regular Expressions List of meta characters: . ---> Any one character ? ---> Zero or one + ---> One or more * ---> zero or more ^ ---> at the beginning of the string $ ---> at the end of the string [abc] ---> any one of a b c {m} ---> 'm' times {m,n} ---> at least m times, at most n times | ---> or \ ---> escape sequence character \s ---> a space \d ---> a digit \w ---> a word \b ---> a word boundary examples: \d ---> a single digit number (0 to 9) \d\d ---> a two digit number (0 to 99) \d\d\d ---> a three digit number (000 to 999) NOTE: ?, +, *, {} are used as Quantifiers (to represent quantity) \d{3} ---> same as above \d{3,5} ---> either 3 digit or 5 digit number hell?o ---> helo | hello hell+o ---> hello | helllo | helllllo | ... hrll*o ---> helo | hello | helllo | helllllo | ... he(ll)+o ---> hello | hellllo | hellllllo | ... S = "hi hello how are hello" hello ---> Yes ^hello ---> No h...

Installing pip with get-pip.py in a Python virtual environment

To create a Python virtual environment type below commands on the required console prompt: On Windows     python -m venv ./venv     .\venv\Scripts\activate On Ubuntu     python3 -m venv ./venv      source ./venv/bin/activate To install pip, securely   download   get-pip.py   by following this link:   get-pip.py . Alternatively, use   curl : curl https : // bootstrap . pypa . io / get - pip . py - o get - pip . py Then run the following command in the folder where you have downloaded  get-pip.py : python get - pip . py Warning   Be cautious if you are using a Python install that is managed by your operating system or another p ackage manager.  get-pip.py  does not coordinate with those tools, and may leave your system in an inconsistent state. Hence it is recommended to use venv (Python Virtual Environment) to isolate the working environment with the Python environment at th...

PYTHON REGULAR EXPRESSIONS

Proved to be a very useful resource for me ! http://www.tutorialspoint.com/python/python_reg_expressions.htm PYTHON REGULAR EXPRESSIONS http://www.tutorialspoint.com/python/python_reg_expressions.htm Copyright © tutorialspoint.com A  regular expression  is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world. The module  re  provides full support for Perl-like regular expressions in Python. The re module raises the exception re.error if an error occurs while compiling or using a regular expression. We would cover two important functions, which would be used to handle regular expressions. But a small thing first: There are various characters, which would have special meaning when they are used in regular expression. To avoid any confusion while dealing with regular expressions, we would use Raw Strings as  r'e...

Method argument Pass by Value & Pass by Reference behavior in Python

This is a very important concept in Python where the behavior of pass by value & pass by reference varies with lists compared to immutable data types like strings. Source  http://www.python-course.eu/passing_arguments.php Parameter Passing "call by value" and "call by name" The most common evaluation strategy when passing arguments to a function has been call by value and call by reference: Call by Value The most common strategy is the call-by-value evaluation, sometimes also called pass-by-value. This strategy is used in C and C++ for example. In call-by-value, the argument expression is evaluated, and the result of this evaluation is bound to the corresponding variable in the function. So, if the expression is a variable, a local copy of its value will be used, i.e. the variable in the caller's scope will be unchanged when the function returns. Call by Reference In call-by-reference evaluation, which is also known as pass-by-reference, a fun...