Python Fast Boot Camp For Dummies , Understanding Variable , numbers, tuple , list , dictionary , python data structure.

What is variable ?

Variable is a label for a location in memory . Variable make us easy to understand as programmer.

Python has five standard data types −

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Let’s take a look on each of them.

Number Data Type in Python

Python supports integers, floating point numbers and complex numbers. They are defined as intfloat and complex class in Python.

# note variable is always at the left and we assign it with = to something like integer and float
# example we name our variable i for integer and f for float
i = 7
f = 3.5
# print the value of i and f to command prompt
print (i)
print (f)
# we can use type function to determine what is variable i and f is
print (type(i))
print (type(f))
## We can covert between the type to by using int and float function
print (float(i))
print (int(f))

Below are example of number type.

intfloatcomplex
100.03.14j
10015.2045.j
-786-21.99.322e-36j
08032.3+e18.876j
-0490-90.-.6545+0J
-0x260-32.54e1003e+26J
0x6970.2-E124.53e-7j
Number SystemPrefix
Binary‘0b’ or ‘0B’
Octal‘0o’ or ‘0O’
Hexadecimal‘0x’ or ‘0X’

Complex numbers are an extension of the familiar real number system in which all numbers are expressed as a sum of a real part and an imaginary part. Imaginary numbers are real multiples of the imaginary unit (the square root of -1), often written i in mathematics or j in engineering. Python has built-in support for complex numbers, which are written with this latter notation; the imaginary part is written with a j suffix, e.g., 3+1j. To get access to complex equivalents of the math module, use cmath. Use of complex numbers is a fairly advanced mathematical feature. If you’re not aware of a need for them, it’s almost certain you can safely ignore them.

Complex numbers are stored as a pair of machine-level double precision floating point numbers.

x = 3+1j
y = 3+2j
z = 3+3j
x = x + y + z
print (x)
print(type(x))
print(type(y))
print(type(z))

Below are Operation that we can do with number type.

OperationMeaning
<strictly less than
<=less than or equal
>strictly greater than
>=greater than or equal
==equal
!=not equal
isobject identity
is notnegated object identity

Cheat Sheet For Number

OperationResultNotesFull documentation
x + ysum of x and y
x - ydifference of x and y
x * yproduct of x and y
x / yquotient of x and y
x // yfloored quotient of x and y(1)
x % yremainder of x / y(2)
-xx negated
+xx unchanged
abs(x)absolute value or magnitude of xabs()
int(x)x converted to integer(3)(6)int()
float(x)x converted to floating point(4)(6)float()
complex(re, im)a complex number with real part re, imaginary part imim defaults to zero.(6)complex()
c.conjugate()conjugate of the complex number c
divmod(x, y)the pair (x // y, x % y)(2)divmod()
pow(x, y)x to the power y(5)pow()
x ** yx to the power y(5)

String Data Type

Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows either pair of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 to the end.

The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. For example −

#### Declare Variable str and assign string to it
str = "Welcome to Python String Tutorial"
print (len(str))     # Prints length of the str
print (str)          # Prints complete string
print (str[0])       # Prints first character of the string
print (str[2:5])     # Prints characters starting from 3rd to 5th
print (str[2:])      # Prints string starting from 3rd character
print (str * 2)      # Prints string two times
print (str + " More Text") # Prints concatenated string
print (str.lower())  # Prints to lower
print (str.upper())  # Prints all to upper

List data type in Python

list1 = [ 'Apple' , 'Banana' , "Stawberry" ] ## Declare 3 List Element
list1[0] = "Change from apple to Chili"   ## Remember to access List 0 is the first element
list2 = ['Name' , 'Fruit' , '123']
## looping list
for x in list1:
    print (x)
print (list1)          # Prints complete list
print (list1[0])       # Prints first element of the list
print (list1[1:3])     # Prints elements starting from 2nd till 3rd
print (list1[2:])      # Prints elements starting from 3rd element
print (list1 * 2)  # Prints list two times
print (list1 + list2) # Prints concatenated lists
print (len(list1))               ### print how many element in list which is 3
list1.append ("New Element")     ### append to list1
list1.insert (1, 'New Item')     ### insert in list[1] New Item
del list1[0]                     ### remove item from list[0]

Tuple Data Type in Python

While list we begin to declare it with [] , tuple we declare it with () . Unlike List , List can be change while Tuple can’t be change . It is like read only data . Let’s take a look from example below.

tuple1 = ('12345' , 'Some Text Here' , 'Final text here 12345') ## declare tuple Notice we use ()
tuple2 = ('54321', "another text here")
print (len(tuple1))           ## print tuple1 length
print (len(tuple2))           ## print tuple2 length
print (tuple1)           # Prints complete tuple
print (tuple1[0])        # Prints first element of the tuple
print (tuple1[1:3])      # Prints elements starting from 2nd till 3rd
print (tuple1[2:])       # Prints elements starting from 3rd element
print (tuple2 + tuple1)  # prints tuple 2 and follow by tuple 1
#### We try to edit tuple1 with result of error
tuple1[0] = 'Error'     # TypeError: 'tuple' object does not support item assignment

Dictionary Data Type in Python

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Key value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’.

A Dictionary in Python works similar to the Dictionary in a real world. Keys of a Dictionary must be unique and of immutable data type such as Strings, Integers and tuples, but the key-values can be repeated and be of any type.

dict1 = {
    'key1' : 'value1',
    'key2' : 'value2',
    'key3' : 'value3',
    ### Nested Dictionary
    'key4' : { 'A' : 'Welcome' , 'B' : 'To' , 'C' : 'Python'}
}
##### Let's see what we can do with Dictionary
print (dict1)                  ## print key and value of dict1
print (dict1.get('key1'))      ## print key1 value which is value1
dict1 ['key1'] = 'somevalue'   ## change key1 value to somevalue
dict1 ["key5"] = 'somevalue5'  ## Adding an item to the dictionary is done by using a new index key and assigning a value to it:
del dict1["key2"]                   ## remove key2 from dictionary
dict2 = dict1.copy()                  ## Copy Dictionary
#### We can loop over the key .
for x in dict1:                ## Print all key names in the dictionary, one by one:
  print(x)
#### Print all values in the dictionary, one by one:
for x in dict1:
  print(dict1[x])
#### You can also use the values() function to return values of a dictionary:
for x in dict1.values():
  print(x)
#### Loop through both keys and values, by using the items() function:
for x, y in dict1.items():
  print(x, y)
Source :

https://docs.python.org/3/library/stdtypes.html

https://python-reference.readthedocs.io/en/latest/docs/complex/

https://www.geeksforgeeks.org/python-dictionary/

https://www.w3schools.com/python/python_dictionaries.asp

https://www.tutorialspoint.com/python3/python_variable_types.htm

Python Fast Boot Camp , Calling Operating System Command , save result of OS command to variable.

In this bootcamp , we will look into how to get output from system call command and assign it to variable for later processing or output presentation.

We will use subprocess library

import subprocess

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions.

The subprocess.Popen() class is responsible for the creation and management of the executed process. In contrast to the previous functions, this class executes only a single command with arguments as a list. This means that you won’t be able to pipe commands.

import subprocess
process = subprocess.Popen(['ls', '-l'],
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)
stdout,stderr = process.communicate()
print (stdout.decode('utf-8'))

incode above we call Popen with command argument ls , -l .

subprocess.PIPE

Special value that can be used as the stdinstdout or stderr argument to Popen and indicates that a pipe to the standard stream should be opened.

subprocess.STDOUT

Special value that can be used as the stderr argument to Popen and indicates that standard error should go into the same handle as standard output.

communicate() method to get the standard output and error in case as a tuple.

import subprocess
process = subprocess.Popen(['ls', '-l'],
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)
stdout,stderr = process.communicate()
file = open ("test.txt" , "w")
file.write(stdout.decode('utf-8'))
#### ON TOP IS SAMPLE OF HOW TO WRITE TO FILE 

WordPress How to add color for code using prismjs. Useful for presenting Code with color.

  1. Download PrismJS from http://prismjs.com/ note: only choose language that you want to use or else your file is bigger and may be will slow your site.

2 . Download the JS and CSS do not click download from the top, it will reload the page again . scroll down to the bottom and you will see

3. once you download your prism.js and prism.css , we need to upload it to our theme directory which is at /yourwordpress/directory/wp-content/themes/yourthemehere

upload prism.js to /yourwordpress/directory/wp-content/themes/yourthemehere/
upload prism.css to /yourwordpress/directory/wp-content/themes/yourthemehere/
4. chown apache:apache /yourwordpress/directory/wp-content/themes/yourthemehere/prism.js
chown apache:apache /yourwordpress/directory/wp-content/themes/yourthemehere/prism.css
chmod 644 /yourwordpress/directory/wp-content/themes/yourthemehere/prism.js
chmod 644 /yourwordpress/directory/wp-content/themes/yourthemehere/prism.css

3. Edit your theme functions.php:

On the theme editor, click on “Theme Functions” and add this code:

Add it to the bottom of your function.php

// Function to add prism.css and prism.js to the site
function add_prism() {
    // Register prism.css file
    wp_register_style(
        'prismCSS', // handle name for the style so we can register, de-register, etc.
        get_stylesheet_directory_uri() . '/prism.css' // location of the prism.css file
    );
    // Register prism.js file
    wp_register_script(
        'prismJS', // handle name for the script so we can register, de-register, etc.
        get_stylesheet_directory_uri() . '/prism.js' // location of the prism.js file
    );
    // Enqueue the registered style and script files
    wp_enqueue_style('prismCSS');
    wp_enqueue_script('prismJS');
}
add_action('wp_enqueue_scripts', 'add_prism');

To use prism just edit your post with this at the beginning of html
example below we are using python highlighter.

<pre><code class="language-javascript">
enter your text here.
</code></pre>

Alternatively , We can Use Plugin , Which Is Call Prismatic.

https://wordpress.org/plugin/prismatic

To use prismatic see the screenshot below. Find the Button of Prismatic at your wordpress editor which is normally at Formatting Block.

Once you used it quite often it will be there in Most Used.

To use it press Prismatic button , once you add text , convert it to html and edit language to your choice language . example .

code class=”language-yourchoiceoflanguage”

source : https://crambler.com/how-to-implement-prism-js-syntax-highlighting-into-your-wordpress-site/

Regular Expression Cheat Sheet for Dummies

Regex BasicsDescription
^The start of a string
$The end of a string
.Wildcard which matches any character, except newline (n).
|Matches a specific character or group of characters on either side (e.g. a|b corresponds to a or b)
Used to escape a special character
aThe character “a”
abThe string “ab”
QuantifiersDescription
*Used to match 0 or more of the previous (e.g. xy*z could correspond to “xz”, “xyz”, “xyyz”, etc.
?Matches 0 or 1 of the previous
+Matches 1 or more of the previous
{5}Matches exactly 5
{5, 10}Matches everything between 5-10
Character ClassesDescription
sMatches a whitespace character
SMatches a non-whitespace character
wMatches a word character
WMatches a non-word character
dMatches one digit
DMatches one non-digit
[b]A backspace character
cA control character
Special CharactersDescription
nMatches a newline
tMatches a tab
rMatches a carriage return
ZZZMatches octal character ZZZ
xZZMatches hex character ZZ
A null character
vA vertical tab
GroupsDescription
(xyz)Grouping of characters
(?:xyz)Non-capturing group of characters
[xyz]Matches a range of characters (e.g. x or y or z)
[^xyz]Matches a character other than x or y or z
[a-q]Matches a character from within a specified range
[0-7]Matches a digit from within a specified range
String ReplacementsDescription
$`Insert before matched string
$’Insert after matched string
$+Insert last matched
$&Insert entire match
$nInsert nth captured group
AssertionsDescription
(?=xyz)Positive lookahead
(?!xyz)Negative lookahead
?!= or ?<!Negative lookbehind
bWord Boundary (usually a position between /w and /W)
?#Comment

Source

https://www.debuggex.com/cheatsheet/regex/python

https://www.keycdn.com/support/regex-cheatsheet

https://docs.python.org/3/library/re.html

Python For System Admin Fast BootCamp. Modifying file read and write and using regular expression.

In this tutorial fast boot camp for python for system admin , we will look into how to open , edit file and use regular expression for quickly replace text . useful for system admin.

File Modes in Python

ModeDescription
‘r’This is the default mode. It Opens file for reading.
‘w’This Mode Opens file for writing.
If file does not exist, it creates a new file.
If file exists it truncates the file.
‘x’Creates a new file. If file already exists, the operation fails.
‘a’Open file in append mode.
If file does not exist, it creates a new file.
‘t’This is the default mode. It opens in text mode.
‘b’This opens in binary mode.
‘+’This will open a file for reading and writing (updating)

example of code how to open file in python.

  • File open to write :
# we are trying to write test.txt with some line .
file = open ("test.txt" , "w")
for i in range (10):
    file.write("Print this line number = %d n" % (i+1))
file.close()
  • File open to read
# Lets try to open file test.txt that we previously wrote
file = open("test.txt" , "r")
for i in file.readlines():
    print (i)
file.close()
 Print this line number = 1
 Print this line number = 2
 Print this line number = 3
 Print this line number = 4
 Print this line number = 5
 Print this line number = 6
 Print this line number = 7
 Print this line number = 8
 Print this line number = 9
 Print this line number = 10 

Python Regular Expression Tutorial

Please see this link for quick reference of regular expression. https://dodocrypto.com/index.php/2019/11/12/regular-expression-cheat-sheet-for-dummies/

List of Python re module .

The re module offers a set of functions that allows us to search a string for a match:

FunctionDescription
findallReturns a list containing all matches
searchReturns a Match object if there is a match anywhere in the string
splitReturns a list where the string has been split at each match
subReplaces one or many matches with a string

Let’s start with findall() . The findall() function returns a list containing all matches. Note : We will be using test.txt from example that we made earlier.

import re;
file = open ("test.txt" , "r" )
for i in file.readlines():
    result = re.findall (r"d+" , i)
    print (result)
file.close()
----------------------------------------------------------------
this will print out the number 1 to 10 and d+ mean match any digit or numeric at least one or more

The search() Function

The search() function searches the string for a match, and returns a Match object if there is a match.If there is more than one match, only the first occurrence of the match will be returned:

import re
file = open ("test.txt", "r")
for i in file.readlines():
    result = re.search (r"d+" , i)
    print (result.group())
file.close()
-----------------------------------
This will print 1 - 10

The split() Function

The split() function returns a list where the string has been split at each match:

import re
file = open ("test.txt" , "r")
for i in file.readlines():
    result = re.split (r"s", i)
    print (result)
file.close()
------------------- this will return
['Print', 'this', 'line', 'number', '=', '1', '', '']
 ['Print', 'this', 'line', 'number', '=', '2', '', '']
 ['Print', 'this', 'line', 'number', '=', '3', '', '']
 ['Print', 'this', 'line', 'number', '=', '4', '', '']
 ['Print', 'this', 'line', 'number', '=', '5', '', '']
 ['Print', 'this', 'line', 'number', '=', '6', '', '']
 ['Print', 'this', 'line', 'number', '=', '7', '', '']
 ['Print', 'this', 'line', 'number', '=', '8', '', '']
 ['Print', 'this', 'line', 'number', '=', '9', '', '']
 ['Print', 'this', 'line', 'number', '=', '10', '', '']

The sub() Function

The sub() function replaces the matches with the text of your choice:

import re
file = open ("test.txt" , "r")
result = file.read()
filew = open ("test.txt" , "w")
## We replace Print in test.txt to be New Print
result = re.sub(r"Print" , "New Print" , result)
## We replace number to blank
result = re.sub(r"d+" , "" , result)
## We replace number to Number
result = re.sub(r"number", "Number" , result )
filew.write(result)
filew.close()
file.close()
 ================
this will result in file text
New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =

Remember to use readlines() in a big file instead of read()

Check out next tutorial at :

Mitre CVE database . Common Vulnerabilities and Exposures . Exploit Database.

MITRE

MITRE is a US government-sponsored organization that manages federally funded research and development centers (FFRDC). Its website emphasizes commercial publications and information related to their FFRDCs such as the National Cybersecurity program. It also maintains one of the biggest and widely referenced CVE databases currently available, searchable by the public.

https://cve.mitre.org/

Secure Coding Practice For C , C++ , Java , Perl , Android.

Welcome

This site supports the development of coding standards for commonly used programming languages such as C, C++, Java, and Perl, and the Android™ platform. These standards are developed through a broad-based community effort by members of the software development and software security communities.

For more information about this project and to see tips on how to contribute, please see the Development Guidelines.

Standards Development Area

The following development areas enable you to learn about and contribute to secure coding standards for commonly used programming languages C, C++, Java, and Perl. 

 SEI CERT C Coding Standard

SEI CERT C++ Coding Standard    

Android TM   Secure Coding Standard     

SEI CERT Oracle Coding Standard for Java

SEI CERT Perl Coding Standard

Setup Alibaba Cloud ECS Server Cloud Postfix Email Using Sendgrid

By default Alibaba Cloud Block Out going port 25 . We need to setup smtp relay for it. We choose https://sendgrid.com . Free account is available .

First register at https://sendgrid.com/ choose free plan or buy plan from them .

Next We setup postfix in our centos 7.

yum -y install postfix cyrus-sasl-plain mailx

Postfix will need to be restarted before the SASL framework will be detected.

systemctl restart postfix
systemctl enable postfix
Open the /etc/postfix/main.cf and add the following lines to the end of the file.
vi /etc/postfix/main.cf
add the configuration below
myhostname = hostname.example.com
relayhost = [smtp.sendgrid.net]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
Open /etc/postfix/sasl_passwd and add the configuration.
vi /etc/postfix/sasl_passwd
[smtp.sendgrid.net]:587 username:password
To get the username and password above : go to
https://app.sendgrid.com/guide/integrate/langs/smtp

A Postfix lookup table must now be generated from the sasl_passwd text file by running the following command.

postmap /etc/postfix/sasl_passwd

Access to the sasl_passwd files should be restricted.

chown root:postfix /etc/postfix/sasl_passwd
chmod 640 /etc/postfix/sasl_passwd
Lastly, reload the Postfix configuration.
systemctl restart postfix

Lastly test messages from mail to yourself and see /var/log/maillog

echo “This is a test.” | mail -s “test message” user@example.net

tail -f /var/log/maillog

Setup Centos 7 / RHEL 7 WordPress + FreeSSL

Since wordpress is not supporting php that come with centos 7 default package anymore , we use remi php repo , at current of this tutorial , stable php is version 7.3. Follow guide through of the following command . it just take than 5 minute before you have wordpress site running with Let’s encrypt free ssl .

# yum -y update

# hostnamectl set-hostname yourdomain.com

# systemctl restart systemd-hostnamed

# yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

# yum install yum-utils

# yum-config-manager –disable remi-php54

# yum-config-manager –enable remi-php73

# yum install httpd mod_ssl mariadb mariadb-server php php-common php-mysql php-gd php-xml php-mbstring php-mcrypt

# systemctl enable mariadb

# systemctl enable httpd

# systemctl start httpd

# systemctl start mariadb

# mysql_secure_installation

# mysql -u root -p
Enter password:
## Create database ##
CREATE DATABASE wordpress;
## Creating new user ##
CREATE USER wordpress@localhost IDENTIFIED BY "change_this";
## Grant privileges to database ##
GRANT ALL ON wordpress.* TO wordpress@localhost;
## FLUSH privileges ##
FLUSH PRIVILEGES;
## Exit ##
exit

# cd /var/www/html

# yum -y install wget

# wget http://wordpress.org/latest.tar.gz

# tar -xvzf latest.tar.gz

# chown -R apache:apache /var/www/html/wordpress

# vi /etc/httpd/conf.d/yourdomain.com.conf

<VirtualHost *:80>
   ServerAdmin admin@yourdomain.com
   DocumentRoot /var/www/html/wordpress
   ServerName yourdomain.com
   ServerAlias www.yourdomain.com
   ErrorLog /var/log/httpd/yourdomain.com-error-log
   CustomLog /var/log/httpd/yourdomain.com-acces-log common
</VirtualHost>

# systemctl restart httpd

Now we are setting up Let’s Encrypt SSL Certificate to Secure Apache on RHEL/CentOS 7. Follow the walk through

# yum install epel-release

# cd /usr/local/

# git clone https://github.com/letsencrypt/letsencrypt

# cd /usr/local/letsencrypt/

Almost Finished Hang in there . now just run ./letsencrypt-auto and follow the installation. As easy as that and restart httpd.

# ./letsencrypt-auto

# systemctl restart httpd

Verify our brand new https by clicking link below
https://www.ssllabs.com/ssltest/analyze.html
Enjoy !!!