ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory

While installing the scikit-learn in python or a few other python packages, you may encounter an error like this in Windows machines.

ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'C:\\Users\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python38\\site-packages\\sklearn\\datasets\\tests\\data\\openml\\292\\api-v1-json-data-list-data_name-australian-limit-2-data_version-1-status-deactivated.json.gz'

To fix this error, modify the registry value, Long install path

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem

Modify the value of LongPathsEnabled from 0 to 1

Python 3 – Learn it quick – Strings

Introduction to Strings

In this lesson, we are discussing about Strings and String operations in Python. The strings are sequences of one or more characters, which are variable or a constant. Strings are are surrounded by either single quotation marks, or double quotation marks and they are immutable in nature, which means, the value will not change.

The print finction can be used to print a string. One example is given in this lab.

samplestr = "This is a sample string" print(samplestr) This is a sample string

String Indexing and Slicing in Python

In python, each character in string is mapped to an index and index start with 0, like in the case of arrays.

Positive String Indexing

Please see the below table,

The top row in the below table is a sample string with 16 characters,

THIS IS A STRING

T h i s   i s   a   s t r i n g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

In this example, the index starts with the letter with index number 0 and last index of the string is G with index number 15. The indexing is applicable to space and any special characters present in the string. Space between THIS and IS is assigned with index number 4 and is the case with other spaces.

Any character present in the string can be accessed by using the index number associated with it.

Example :

 string = "Is this a string?"
string[6]
s

Excercise to identify the postive index of string is below.

string = "Is this a string?" string[16] ?
Negative String Indexing

Negative string indexing is used, if the character needs to be read backwards, which means, if the indexing needs to be done from the last character of a string.

Sample String : IS THIS A STRING?

This will be represented in negative indexing as below,

I S   T H I S   A   S T R I N G ?
-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

In negative indexing method, the value will be read from last character. Below given is an example,

test = "IS THIS A STRING ?"
test[-6]
T

Let us try it in the below lab,

test = "IS THIS A STRING?" test[-6] T

String Slicing

Portion of a string can be sliced out from the complete string using slicing. Consider a string “Some Apples and Peaches”. Using the slicing, the substring “Apples” can be sliced out. The Slicing is done by creating a range of index numbers separated by a colon [i:j]. The letter i stands for the first character of the slice and letter j stands for the last character of the slice.

To slice “Apples”,

str = "Some Apples and Peaches"
str[5:10]

To create a slice, everything after “and”, from character “a” to form a string “and Peaches”, the indexing will be like this below. The below index will create a subsctring from the character 14 till the end of original string.

 
string = "Some Apples and Peaches"
string[14:]
d Peaches

To create a slice by including all characters from the start till the end of the word “and”, while removing the space and “Peaches”, which means, create a substring “Some Apples an”, the indexing needs to be done like below.

 
string = "Some Apples and Peaches"
string[:14]
Some Apples an

Slicing can also be done using the negative indexing or reverse indexing, which means from the last character of original string. To create a slice “Peaches” using the negative indexing, the code will be like below

 
string = "Some Apples and Peaches"
string[-1:-7]

String Striding

The third character in string denotes a stride. The stride denotes, how many characters need to move forward after the first character from the string. By default, for a string, the stride value is 1.

The below code will print “Apples”.

 
string = "Some Apples and Peaches"
string[5:10:1]

If the stride value is set as two, the output string will change to “pls”,

 
string = "Some Apples and Peaches"
string[5:10:2]

If stride needs to be applied against the complete string and stride value needs to be 2, the output will be “Sm plsnadPahs”. Please see the below example,

 
string = "Some Apples and Peaches"
string[::2]
Sm plsnadPahs

Stride can be done using negative or reverse indexing,

 
string = "Some Apples and Peaches"
string[::-1]
sehcaeP dna selppA emoS

String Functions

Python provides the below string functions, to perform different string operations.

    1. len()
    2. count()
    3. find()
String len Function

The string len function is used to find the length of a string, which means, count number of characters present in the string.

Example :

 
string = "Some Apples and Peaches"
print(string(len))
23
String Count Function

The string count function is used to find the count of or the number of times, a character or a substring or a slice is present in the string.

Example :

Count the number of times the character ‘m’ is present in the below string,

 
string = "Some Apples and Peaches"
print(string.count("m"))
1

Count the number of times the slice ‘Apples’ present in the below string,

 
string = "Some Apples and Peaches"
print(string.count("Apples"))
1

Count the number of times the slice ‘Oranges’ present in the below string,

 
string = "Some Apples and Peaches"
print(string.count("Oranges"))
0
String Find Function

The string find function is used to find the first index or the position of a character or a substring or a slice, present in the string.

Example :

Find the first postion of character ‘p’ in the below string,

 
string = "Some Apples and Peaches"
print(string.find("p"))
6

Find the index of slice ‘Apples’ in the below string. The output will return 5, which is the index of the first character of matching slice.

 
string = "Some Apples and Peaches"
print(string.find("Apples"))
5

If the requirement is to look for a matching substring after nth index position, it can be done like below. In the below example, we are trying to look for the next index position or the presence of slice “Apples”, after it was first found. In the below example, the interpreter will start looking for “Apples” after the index position 10 from the start of string.

 
string = "Some Apples are green and some Apples are red"
print(string.find("Apples",10))
30

The negative or reverse indexing can also be used to find the presence of a string from the last character of a string. To do this, a third value needs to be passed.

 
string = "Some Apples are green and some Apples are red"
print(string.find("Apples",10,-9))
36

Now lets us practice the above lessons in our interactive lab,

def stringcalc(): #Declare the string as global global str str = "Some Apples and Peaches" #Call stringcalc() function stringcalc() #Print string slice print(str[5:10]) #Print string slice after the index 14 print(str[14:]) #Print string slice before index position 14 print(str[:14]) #Print string slice using negative slicing print(str[-8:-1]) #Print string slice using stride value 1 print(str[5:10:1]) #Print string slice using stride value 2 print(str[5:10:2]) #Apply stride value 2 to entire string print(str[::2]) #Apply stride value 2 to entire string print(str[::-1]) #Print the length of string print(len(str)) #Count the presence of character 'm' in the string print(str.count("m")) #Count 'Apples' in the string print(str.count("Apples")) #Count 'Oranges' in the string print(str.count("Oranges")) #Find the first postion of character 'p' in the below string print(str.find("p")) #Find the index of slice 'Apples' in the below string print(str.find("Apples")) #Look for a matching substring after nth index position print(str.find("Apples",10)) #Using negative indexing, find the presence of a string from the last character of a string print(str.find("Apples",10,-9))

PowerShell ValidateSet

The Powershell validate set can be used to validate the powershell parameters that are getting supplied to the script. This is useful, when you want to validate a boolean value like True or False, or when you want to ensure that, user is selecting the right choice, when supplying the input values to a function or a script.

 
Function Sample-Validation {
    [cmdletbinding()]
    Param(
          [Parameter(Position=0,Mandatory=$True, HelpMessage="Select the choice, True or False")]
          [ValidateSet("True","False" )]
          [string]$Choice = "False",
          [ValidateNotNullorEmpty()]
         )
      $Choice
}
  
# Sample Choices
Sample-Validation -Choice "True"
Sample-Validation -Choice "False"
Sample-Validation -Choice "Falseeeee"
Sample-Validation -Choice $null
Sample-Validation -Choice ""

When the choice is True

When the choice is False


When the choice is having spelling mistake

When the choice is null

When the choice is empty


Similare write ups can be found in the Microsoft dev blogs here

Python 3 – Learn it quick – Logical and Bitwise Operators

In python, there are mainly, three boolean operators and seven bitwise operators

Operator Name Symbol Sample Operation Description
logical AND and age == 23 and name == “john”
logical OR or age == 23 or name == “john”
logical NOT not age == 23 not name == “john”
bitwise left shift >>
bitwise right shift <<
bitwise and & Return 1 if both of the bits are 1 or else it will return 0
bitwise xor ^ return 1 if one of the bit is 1 and other one is 0 or else it will return false
bitwise or | Return 1 if either of the bit is 1 or else 0
bitwise not ~ Return its one’s complement

logical AND

The logical AND operator validate two conditions. If both conditions are True, then it will return True or it will be False.

Lab Excercise

name = "Isaac" year = 2 if name == "Isaac" and year == 2: print("Student name is Isaac, and Isaac studies in year 2.")

logical OR

The logical AND operator validate two conditions. If one of the condition is True, then it will return True.

year = 3 if year == 3 or year == 2: print("Isaac studies in year 2 or 3 ")

logical NOT

Lab Excercise

year = 3 if not year: print("Year is empty") else: print("Year is ",year)

Bitwise OR

The Bitwise OR operator will return the value 1 if either of the bit is 1 or else it will return 0

Example :

a = 12, Binary of 12 is 1100
b = 14, Binary of 14 is 1110
         1100
         1110
a | b =  1110

Result in binary	1110
Result in decimal	14
Result in hexadecimal	e
Bit Value
0|0 0
0|1 1
1|0 1
1|1 1

Lab Excercise

i = 12 j = 14 # Print bitwise OR operation print("i | j =", i | j)

Bitwise AND

The Bitwise AND operator will return the value 1 if both of the bits are 1 or else it will return 0

Example :

a = 12, Binary of 12 is 1100
b = 14, Binary of 14 is 1110
         1100
         1110
a & b =  1100

Result in binary	1100
Result in decimal	12
Result in hexadecimal	c
Bit Value
0|0 0
0|1 0
1|0 0
1|1 1

Lab Excercise

i = 12 j = 14 # Print bitwise AND operation print("i & j =", i & j)

Bitwise XOR

Bitwise XOR will return 1 if one of the bit is 1 and other one is 0 or else it will return false

a = 12, Binary of 12 is 1100
b = 14, Binary of 14 is 1110
         1100
         1110
a ^ b =  0010

Result in binary	0010
Result in decimal	2
Result in hexadecimal	2

Lab Excercise

i = 12 j = 14 # Print bitwise XOR operation print("i ^ j =", i ^ j)

Bitwise NOT

Bitwise NOT will return its one’s complement. So, it flips the bits until it reaches the first 0 from right. ~a is the same as -a-1.

a = 12, Binary of 12 is 1100
         1100
~a = ~1100
   = -(1100 + 1)
   = -(1101)
   = -13, Decimal Equivalent

It can also be calculated by,

~a =  -12-1 = binary(-13) = -(1101)

Lab Excercise

i = 12 # Print bitwise NOT operation print("~i = ", ~i)

Bitwise Right Shift

Right shift operator, shifts the bits of the number to the right by the number of bits mentioned by the left operand and it will fills 0 on empty slots remained as a result of this change. It is similar to dividing the number with a power of two.

Example : If a = 12 and bit shift value is 2, then result will be, 12 /(2 ^ 2) = 12 / 4 = 3

a = 12, Binary of 12 is 1100
         1100
a >> 2 =  0011

Result in binary	0011
Result in decimal	3
Result in hexadecimal	3

Lab Excercise

i = 12 # Print Bitwise Right Shift operation print("i >> 2 =", i >> 2)

Bitwise Left Shift

The Left shift operator, shifts the bits of the number to the left by the number of bits mentioned by the right operand and it will fills 0 on empty slots remained as a result of this change. It is similar to multiplying the number with a power of two.

Example :

If the number is 12 and shift bit is 2, then result is 12 * 2^2 = 48

a = 12, Binary of 12 is 1100
         1100
a << 2 =  110000

Result in binary	110000
Result in decimal	48
Result in hexadecimal	30

Lab Excercise

Python 3 – Learn it quick – Comparison Operators

In this lesson, we are learning about comparison operators. In Python, the below comparison operators are supported,

1. Equal to
2. Not Equal To or Different
3. Higher than
4. Lower than
5. Higher than or equal to
6. Lower than or equal to

Operator Name Symbol Sample Operation Description
Equal To "==" 5==5.0, ‘str1’ == ‘str1’ If two operands are equal, then the condition is true
Not Equal To or Different != 3 != 4 If two operands are not equal, then the condition is true
Higher than > 8 > 6 If Operand A is greater than operand B, then the condition is true
Lower than < 3 < 37 If operand A is less than operand B, then the condition is true
Higher than or equal to >= 6 >= 6 If the operand A is greater than or equal to the operand B, then the condition is true
Lower than or equal to <= 4 <= 5 If the operand A is less than or equal to operand B, then condition is true

Equal to

x = 55 y = 55 if x == y: print("x is equal to y...") else: print("x is not equal to y...")

Not Equal To

x = 45 y = 55 if x == y: print("x is equal to y...") else: print("x is not equal to y...")

Greater than

x = 76 y = 55 if x > y: print("x is greater than y...") print("Print the value of x :", x) else: print("x is not greater than y...")

Greater than or equal to

x = 76 y = 55 if x >= y: print("x is greater than or equal to y...") print("Print the value of x :", x) else: print("x is not greater than or equal to y...")

Less than

x = 76 y = 55 if x < y: print("x is less than y...") print("Print the value of x :", x) else: print("y is less than x...") print("Print the value of y :", y)

Less than or equal to

x = 76 y = 55 if x <= y: print("x is less than or equal to y...") print("Print the value of x :", x) else: print("y is less than or equal to x...") print("Print the value of y :", y)

Python 3 – Learn it quick – Numbers

Working with Numbers

In this lesson we are working with numbers and different arithmetic operations. Python support different types of arithmetic operations like,

    1. Addition
    2. Subtraction
    3. Division
    4. Multiplication
    5. Exponentiation
    6. Modulo
    7. Floor Division

Addition

You can add numbers by simply mentioning two or more numbers, connected by addition operator “+”

Example :

 3 + 3
6

Subtraction

You can subtract one number from another number by connecting the subtraction operator “-“

Example :

 6 - 3
3

Multiplication

To multiply one number with another, both needs to be connected by multiplication operator “*”

Example :

 6 * 3
18

Division

To perform the division of two numbers, the division operator “/” can be used.

Example :

 6 / 3
2

Exponentiation

Exponentiation stands for the multiplying the number n number of times and exponentiation operator is represented by **

Example :

 6 ** 2
36

Modulo

The modulo operator is represented using the symbol “%”. The modulo operator returns the remainder of dividing the left hand operand by right hand operand.

Example :

 37 % 3
1

Floor Division

The floor division operator is represented using the symbol “//”. Floor division returns the quotient, answer or the result of division, in which the digits after the decimal point will be removed. But if one of the operands(dividend and divisor) is negative, then the result is floored, means rounded away from zero, which means towards the negative of infinity.

Example :

 37 // 3
12
Operator Name Symbol Sample Operation Output
Addition + 5 + 5 10
Subtraction 10 – 6 4
Multiplication * 8 * 6 48
Division / 37 / 3 12.3333
Exponentiation ** 6 ** 2 36
Modulo % 37 % 3 1
Floor Division // 37 // 3 12

In the below excercise, we performing the operations using the operators mentioned in this lesson.

#Print the sum of values 5 and 6 sum = 5 + 6 print(sum) #Subtract 4 from 10 and print the value diffr = 10 - 4 print(diffr) #Multiple 3 with 9 and print the value mult = 3 * 9 print(mult) #Divide 56 with 2 and print the value div = 56 / 2 print(div) #Combination of arithmetic operation and print the value total = 21 + 45 * 78 / 67.6 print(total) # Test the Exponentiation operator. Perform exponentiation 8 to the power of 6 exp = 8 ** 6 print(exp) # Modulo operator. Obtain the remainder of division modl = 37 % 3 print(modl) # Floor Division operator. Obtain the remainder of floor division flr = 37 // 3 print(flr) # Print the sum of values 5 and 6 11 # Subtract 4 from 10 and print the value 6 # Multiply 3 with 9 and print the value 27 # Divide 56 with 2 and print the value 28.0 #Combination of arithmetic operation and print the value 72.92307692307693 # Test the Exponentiation operator. Perform exponentiation 8 to the power of 6 262144 # Modulo operator. Obtain the remainder of division 1 # Floor division operator. Obtain the remainder of floor division 12 msg = "Do not remove the other arithmetic examples!"

Python 3 – Learn it quick – Introduction to Variables

Introduction to Variables

In this lesson, we are learning, how to declare a variable in python and how to print the values. In python, the command, print can be used to display the value of a variable.

Below given is an example on how to declare a variable, assigning a value and printing its value. In this example, you need to try declaring a variable with letter k, assign a value 10 to it and then print the value of k. You can use the below work space to do this exercise.

Lab Excercise
# Create a variable k, equal to 10 # Print out k # This will get executed each time the exercise gets initialised j = 15 # Create a variable k, equal to 10 k = 10 # Print out k print(k) test_object("k") test_function("print") success_msg("Great job!")
Use the assignment operator (<-) to create the variable k.

Python 3 – Learn it quick – DataTypes

DataTypes in Python

In Python there are 8 basic data types namely integers, floating point, strings, lists, dictionaries, tuples, sets and Boolean.

The integer is represented using int and they are whole numbers, like 23, 60 etc.

The Floating point data types or Floats are represented using float, and are fractional numbers like 3.17, 56.78 etc.

The strings are represented using str and they are a string of characters, like a letter, group of letters or a backslash. Python recognises single and double quotes as the same thing, the beginning and end of the strings.

Example :

 “This is a string” 

Lists are represented by list and they are containers for holding the values.

Example :

names = ['joseph','nas', 'jas','ram'] 

Dictionary is represented by dict and they are unordered key value pairs.

Example :

 dictionary = { key1:value1, key2:value2, …}

Tuples are represented by tup and they are immutable sequence of objects.

Example :

 (78,”apple”,99.23)

Sets are represented by set and they are a collection which are unordered and unindexed.

Example :

{“str1″,”str2”}

Boolean are represented by bool and they represent logical value.

Example :

True/False
DataType Represented By Value Description
Integer int 24 Whole number values
Float float 45.89 Decimal numbers
String str “this is a sample string” Sequence of characters
List list [‘pencil’,’ink’,’paper’] Ordered sequence of objects
Dictionary dict {‘grapes’: ‘orange’,’apple’: ‘banana’} Unordered key value pairs
Tuples tup (24,”orange”,89.62) It is an immutable sequence of objects
Sets set {“str1″,”str2”} It is a collection which is unordered and unindexed
Boolean bool True/False Logical value

Install Node.js and NPM on Windows 10

To install Node.js, in a web browser, navigate to nodejs.org/en/download.

Click the Windows Installer (.msi).

1. Once the installer is downloaded to your machine, click the installer file to launch the installation.

2. A popup window will be displayed and ask if you want to run the software. Click ‘Run’

3. In the Node.js Setup Wizard, click the Next button.

4. On the next screen, click Next button to agree with the terms of conditions and install the software.

5. The installer will ask you to confirm on the installation location. You can choose the default location and then click Next.

6. In the next page, the installation wizard will let you to select the options. You can opt to accept the defaults by clicking Next button once again.

7. In the next page, click the Install button to complete the installation process. Once done, click Finish and exit.

Alternatively, node.js can be installed using choco and it will be a better option, for the server users.

Before proceeding with the node.js installation, the below command needs to be executed from the elevated powershell.

choco feature enable -n allowGlobalConfirmation

The output will be something similar below,

Once the feature enabling permission is set, we can proceed with the choco installation, by running the below command,

choco install nodejs.install 

Once the installation is finished, you will see the below powershell output,


To verify the installation status,

Search for a specific string pattern in files in linux

To search for a matching patter in files can be done by executing the below commands in linux

[snippet slug=720 lang=bash]

You can do pattern search based on specific file extensions. In the below example, we are trying to lookup .php and .jsp files for the pattern “Sample Pattern”

[snippet slug=grep-pattern2 lang=bash]

In a similar way, you can use the exclude parameter to exclude certain file extensions

[snippet slug=file_pattern lang=bash]

Specific directories can also be excluded

[snippet slug=grep_dir_pattern lang=bash]

Alternate method to perform the pattern search,

[snippet slug=grep_recurs lang=bash]