Tuesday, December 29, 2020

Rat-Maze problem - Yew app - PART2

Rust Yew app:


Last week I wrote about the  Rat-Maze problem - Yew app.

Writing code  in Rust programming language is always fun and a learning.So thought of making some enhancements to the yew app 

1. add colors to the cells

2. after solving the path, change the color of the cells in the path. ..etc


This gave an opportunity to work with CSS and Yew. I think still there is no full support for CSS from Yew as you might have seen in React. But I was managed to change the color with "class" property in CSS.

index.css


.game-celluley {
display: inline-block;
width: 20px;
height: 20px;
border: 1px solid #ccc;
background-color: yellow;
text-align: center;

}


.game-celluleg {
display: inline-block;
width: 20px;
height: 20px;
border: 1px solid #ccc;
background-color: green;
text-align: center;

}



lib.rs

html!{
<div key=x class=format!("game-cellule{}",cell_color)>{ cell.value } </div>
}


Source Code:

github- Yew app


How to compile and run:




output:




Saturday, December 26, 2020

Rat-Maze problem - Yew app built with rustwasm

 Backtracking Algorithms

I just started loving the algorithms , especially backtracking algorithms.This geekforgeeks page gives more details on backtracking 

https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-2/

Solving this problem in Rust programming  will be  a little more interesting. Even more fun if we can build this as  Rust Wasm app. Webassembly (wasm) is one of the latest technology that web developers are interested in. So thought of building  the rat-maze as a Yew app.

Yew is a modern Rust framework for creating multi-threaded front-end web apps with WebAssembly

Read mroe about Yew here - https://yew.rs/docs/en/


I used following dependencies - 

yew="0.17"
wasm-bindgen="0.2.67"
web-sys = "0.3.46"
wasm-logger = "0.2.0"
log = "0.4.6"


Backtracking -

This is one of the algorithm every programmer should try and understand. It will involve recursion and push and pop values from Results.

In my program, "solve" is the method actually implemented this backtracking

///Actual method where the backtracking algorithm is implemented
/// this method will be recursively called based the choices
/// we will be adding the "path" in this algorithm, before calling the method recursively
/// if we couldn't solve the Maze, pop the added "path" and next choice we have.


Yew Component:



///Grid is our component which displays the maze
/// Its also contains the path , once we solve it
struct Grid{
link:ComponentLink<Self>,
cell:[[u8;4];4],
path:Vec<(usize,usize)>,

}


The full program is below for your reference:

Full Code for Rat Maze


Output:




Wednesday, December 9, 2020

How to make dereference for your struct in Rust

 Dereference operator ( *)  in Rust


Dereference operator we have seen in C programming language. Yes , I am talking about the ' * ' operator that we use for dereferencing a pointer in C.

I am not sure how * operator works behind the seen, but the it dereference the pointer and give us the value in that address. 

But in Rust implementation is more obvious  that the type has to implement the trait 'Deref".

https://doc.rust-lang.org/std/ops/trait.Deref.html 


So let's see how should be convert our struct to dereference the value whatever way that we like.


Rust  Deref example

This is simple program to use * operator on MyStruct. And it is only to demonstrate the capabilities of Deref trait, not an idiomatic code.

Here we are derefencing the second field value when we use * operator on MyStruct instance.

use std::ops::Deref; struct MyStruct{ x: i32, y:String, } impl Deref for MyStruct{ //the Deref trait implementation should have Target //associated type type Target = String; // trait mandotory function implementation fn deref(&self) -> &Self::Target{ &self.y } } fn main(){ let my = MyStruct { x:4,y:"hello".to_string() }; //derefencing our struct with * operator println!("{}",*my); }



Conclusion:

Even though references and dereferencing are essential part of most of the system programming languages, how they are implemented  are always hidden behind the language implementations ( in my knowledge) . But Rust gives that insight and flexibility to use them on custom references.



Saturday, November 14, 2020

Callbacks in Rust and Python



Callbacks

Following is the definition of callbacks from wiki https://en.wikipedia.org/wiki/Callback_(computer_programming)

callback in Rust

Whenever I read any web code, callbacks are important part of the code.
So tried to build a basic callback in rust to understand what is it .


I believe , callbacks are basically storing functions/closures in array and calling them whenever required. Actually it is a little difficult to build in Rust because of the syntax and type checks.
But it’s work , type of the functions will be verified in compile time.
For example , if I give x+2 , instead of x +=2 in the below code, rust comiplier gives compilation error.
below code


Rust callback


//create a callbacks list and make some basic operations like
//register a function and calling it etc.

//callbacks struct to store the vector( list of functions)
struct CallbackV1{
    callbacks:Vec<Box<FnMut(i32)>>
}

//implemenation of the CallbackV1
impl CallbackV1{

    fn new() ->Self{

        CallbackV1 {
            callbacks : Vec::new()
        }
    }
	
	//method to register functions in the callbacks vector
    fn register(&mut self,f:Box<FnMut(i32)>){
        self.callbacks.push(f)
    }

	//method to call the functions in the callbacks
    fn call(&mut self,p:i32){

		//iterate over the callbacks vector and take a mutable reference
        for callback in self.callbacks.iter_mut(){
			
			//dereference the callback function and call with passed argument.
            (&mut *callback)(p);
        }
    }
}

fn add(x:i32){

    println!("{}",x+1);
}

fn main() {
//   let mut x = 2;
   let mut  mycallbacks = CallbackV1::new();
   let f1 = |mut x:i32| { 
                            x += 2
                            // println!("{}",x) ;
            };
   let f2 = add;

   mycallbacks.register(Box::new(f1));
   mycallbacks.register(Box::new(f2));

   mycallbacks.call(4);


}


Callback in Python

Built the same code with Python as well. Its quite easy to build the same in python. But the type checks are not there . Its simply call the functions registered. I believe this can lead to vulnerabilities .

Python Callback code


#create a callbacks list and make some basic operations like
#register a function and calling it etc.

class CallbacksV1:
    
    def __init__(self):
        self.callbacks = []
    
    def register(self,f):
        self.callbacks.append(f)
    
    def call(self,value):
        
        callback =  self.callbacks.pop()
            
        callback(value)

    
def add(x):
    print(x+1)

def add_two(x):
    print(x+2)
    return (x+2) 

c = CallbacksV1()
c.register(add)
c.register(add_two)

c.call(3)
c.call(5)

Saturday, July 25, 2020

Why tokio tasks are run-time dependent ?


Tokio and async-std 


I was going through two major crates for async programming in Rust. I believe Tokio and async-std are predominant crates now available for async programming. 

But when I tried to run tokio tasks with async-std run-time, but default it won't allow. I need to change run-time as tokio using #[tokio::main] that's not nice.

But when  I tried to run async-std tasks with tokio run-time, I am able to run.  Looks like async-std is more compatible with other Rust eco-systems.



error:
thread 'main' panicked at 'must be called from the context of Tokio runtime configured with either `basic_scheduler` or `threaded_scheduler


There would be libraries/crates to make it happen, but I think by default this should be available.


Friday, July 24, 2020

Where is the infinite loop in the below Rust Program ?

Have you ever wonder how the below  TcpListener Code run infinitely looking for new connections/clients.

Code:
When you run this below program with a Rust compiler or Cargo run, you will see that the program is not ending. How ?


As you noticed , the for loop doesn't have a explicit condition to exit. As far as the Iterator can return values, the "for loop" construct  execute the  statements inside the body. The Iterators can behave like  an infinite loop.But can return "None"(which can be used to exit from "for") , or can return Some values from next method .

Read more to understand how "for loop"  decompose in Rust - https://doc.rust-lang.org/reference/expressions/loop-expr.html

Ok..we understood that there is something called Iterator which can yield for values infinite.

But where is the Iterator here ?

When you look at the TcpListener , it is just a struct. But it has one particular method which implements Iterator trait. 
The connections can be accepted from TcpListener through a  struct called  std::net::Incoming  which is an Iterator.It is hidden(abstracted) behind the method incoming  in this case. As I said earlier  Iterators can yield and ask for values infinite times.





Iterator Trait is implemented on the struct "Incoming"



As you noticed the next method on the Iterator is not going to end. It just keep iterate as far as we are calling incoming method.




Incoming struct accepts connections of TcpListener:



Incoming implementing Iterator trait:




Saturday, July 18, 2020

compiled a Rust program for Wasm target without wasmbind-gen

It was not that hard to write a function and build for wasm target. I was using the #![no_std] without knowing much on it.The Rust compiler was just guiding through errors to fill the code.
I did not use the wasmbind-gen. But I did use wee_alloc

The program is below:

Note: just comment each line and see the error message compiler is giving to you.

Sunday, June 14, 2020

Pointers in Rust

pointers in Rust

Pointers

Pointers - what is the definition of pointer?
As per wikipedia - In computer science, a pointer is a programming language object that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware.

https://en.wikipedia.org/wiki/Pointer_(computer_programming)

Historically pointers become a tough topic for students, even though the definition looks simple. The loop holes behind the pointers make programmers life hard with them.
If I write a program in C with pointers, its end up in “Segmentation Fault”

Pointers in Rust

I have seen Computer languages started introducing smart pointers recently for safety . So since Rust is new language, Rust has really smart pointers .But it may looks a bit more syntax around them. Once you know the specification of them its quite safe to use them.

1.Raw pointers in Rust

Let starts with dirty one(it’s not dirty,just not smart). Raw, unsafe pointers, *const T, and *mut T.
As name suggests they are raw or unsafe pointers. You can assign a raw address to a raw pointer.

These pointers are very useful when you need to assign some port address to a variable in Rust.

fn main() {
    let p: *mut i32;
    p = 0x10 as *mut i32 ;
    
}

But if you try to deference and assign a value , it throws error in Rust. Rust error shows you why it is risky…

```rust_errors
|
5 |     *p= 10;
  |     ^^^^^^ dereference of raw pointer
  |
  = note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

But if you explicitly tell the compiler that I am sure this is safe to use and so am tagging this snippet under “unsafe” , the compiler compile the code successfully
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=792e09bd5485af4ceca96e59ba80848b

Read more: https://doc.rust-lang.org/std/primitive.pointer.html

2.references - &T

References are generally called as pointers. In Rust,&T type a ‘reference’. So rather than calling this as pointers ,Rust consider this as a separate Type.
&T means reference to a type value T. You can dereference it with * operator.

fn main() {
    let a = 10;
    let p = &a;
    println!("{}",*p);
    
}

3.References - &mut T

Since it is a Type , &T and &mut T are different. The first one is simple reference and we can access the value that points ,but can’t modify the value.
You can modify the value with &mut T reference
Rust treats different variation(mutable or immutable) of references as different types
eg:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=776367d50a05cc1af60c90b31d8ac0d7

But mutable references have one big restriction: you can have only one mutable reference to a particular piece of data in a particular scope

The Rules of References

  • At any given time, you can have either one mutable reference or any number of immutable references.
  • References must always be valid.
fn main() {

    let mut a = 10;
    
    let p1 = &mut a;
    
    *p1 = 11;
   
    let p2 = &a;
    
    println!("{},{}",*p1,*p2)
    
}

Above code won’t compile with error message

rust_errors
error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable
  --> src/main.rs:9:14
   |
5  |     let p1 = &mut a;
   |              ------ mutable borrow occurs here
...
9  |     let p2 = &a;
   |              ^^ immutable borrow occurs here
10 |     
11 |     println!("{},{}",*p1,*p2)
   |                      --- mutable borrow later used here

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1a975610b3d5d380e57dea9cc1c286b4

If you use 2 mutable reference, the program won’t compile successfully.

fn main() {

    let mut a = 10;
    
    let p1 = &mut a;
    
    *p1 = 11;
   
    let p2 = &mut a;
    
    println!("{},{}",*p1,*p2)
    
}

error message is below, hope that is self explanatory.

error[E0499]: cannot borrow `a` as mutable more than once at a time
  --> src/main.rs:9:14
   |
5  |     let p1 = &mut a;
   |              ------ first mutable borrow occurs here

9  |     let p2 = &mut a;
   |              ^^^^^^ second mutable borrow occurs here
10 |     
11 |     println!("{},{}",*p1,*p2)
   |                      --- first borrow later used here

4.Box - A pointer type for heap allocation.

A Box allows heap allocations similar to “malloc” in C programming.
You can mutate the heap value using the keyword ‘mut’ in front of the variables.

fn main() {

   let mut heap_pointer = Box::new(10);
    *heap_pointer += 1;
   println!("{}",*heap_pointer);
   
}

The above code gives you the output as 11.

 fn main() {

    let mut heap_pointer = Box::new(10);
    
    let heap_pointer2 = &mut heap_pointer ;

   // let heap_pointer3 = &mut heap_pointer;
   // heap_pointer2 = *heap_pointer2 + 1;
   
   match *heap_pointer {
       x => { println!("{}",x+34)},
   }
    
 //   let heap_pointer3 = &mut heap_pointer;
  //   *heap_pointer2 = *heap_pointer + 1;
    println!("{}",*heap_pointer);
 //   println!("{}",x);
 //   println!("{}",*heap_pointer2);
    
}

How to get the value from Box:
You can use * operator to dereference the address.

#![allow(unused)]
fn main() {
let x = Box::new(String::from("Hello"));
println!("{}",*x);
}

Using Raw pointer -

You have to use “unsafe” block for creating a raw pointer for a boxed value.

#![allow(unused)]
fn main() {
let x = Box::new(String::from("Hello"));

//taking a raw pointer
let ptr = Box::into_raw(x);

let x = unsafe { Box::from_raw(ptr) };

println!("{}",x);
}

5.Rc - Reference Counted

We are getting into the world of smart pointers. Reference counting is not a new concept, it is there in Python. If you see below program in python tutor.

a = [12,3,4]
b = a
c = b

a = 0
b= 1
c =2

The python visualizer shows you that the object (list object) remains in the memory until all the references to the object get removed.
Note: when we assign a =0 , the value assigned to ‘a’ become zero .

http://www.pythontutor.com/visualize.html#code=a %3D [12,3,4] b %3D a c %3D b a %3D 0 b%3D 1 c %3D2 &cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=[]&textReferences=false

In Rust ‘Rc’ is a single-threaded reference-counting pointer. ‘Rc’ stands for ‘Reference Counted’.

Below program creates an Rc reference and get the value using * operator (dereferencing )



fn main() {
use std::rc::Rc;

let x = Rc::new("hello".to_owned());

let y = Rc::clone(&x);
let z = Rc::clone(&x);

println!("{}",*x);
println!("{}",*y);

println!("{}",Rc::strong_count(&y));

}

Below Rust program generates number of references from a heap allocated memory . You might have noticed that we drop ‘x’ in the middle. But still we have 2 Rc references. If you have multiple normal reference &T for a heap allocation, compiler won’t allow you to drop the owned reference.

fn main() {
use std::rc::Rc;
let x = Rc::new("hello".to_owned());

let y = Rc::clone(&x);
let z = Rc::clone(&x);

drop(x);

println!("{}",Rc::strong_count(&x));
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=12748f50761fb40ae36bead095f64a65

6.Recell

A mutable memory location with dynamically checked borrow rules.
Refcell gives you a mutable reference for an immutable instance.
if you do two or more ‘borrow_mut’ in the same scope, the program panic in the run time.

But below program , we are passing borrow_mut to the function, so it is not in the same scope.

#![allow(unused)]
use std::cell::RefCell;
use std::cell::*;

fn main() {

//c is not declared as mutable.
let  c = RefCell::new(5);

//but we are creating a mutable borrow and passing to the function.
fun (c.borrow_mut());
fun (c.borrow_mut());

println!("{}",c.into_inner());

}

//fun function receive a mutable cell
fn fun(mut x:RefMut<i32>){
    
// changing the value inside the cell, even though cell 'c' was not declared as mutable.
    *x = 3;
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=02b0ed5116b1abada7160c6ee0fbb436

You can wrap RefCell(not declared mutable) with Rc and mutate the object with Rc reference . If you are using immutable Rc for another objects other than RefCell, it won’t allow to mutate .

#![
allow(unused)]
use std::cell::RefCell;
use std::cell::*;
use std::rc::Rc;
fn main() {

//c is not declared as mutable.
let  c = RefCell::new(5);

//wrap Refcell with Rc

let r = Rc::new(&c);
//wrap Refcell with Rc, even though r1 is not mutable , we use it mutating cell value
let r1 = Rc::clone(&r);

let r2 = Rc::clone(&r);



//but we are creating a mutable borrow and passing to the function.
fun (r1.borrow_mut());

//let d = c.borrow();
fun( r2.borrow_mut());


println!("{}",c.into_inner());

}

//fun function receive a mutable cell
fn fun(mut x:RefMut<i32>){
    
   // changing the value inside the cell, even though cell 'c' was not declared as mutable.
    *x += 3;
} 

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=75d5ccbb4c6c9d62d5df4a92a8f7137e

Conclusion

You might need to try these pointers and write some small code snippets and see to understand how Rust compiler behaves on these pointers. The smart pointers really safe to use in Rust. You can avoid bugs that are hard to debug like race condition, double free,free after use etc…

This is just my notes and experiments with Rust pointers.There could be some missing or incorrect information. Happy to correct it !!

Monday, June 8, 2020

Rust - Test driven program development

Cargo supports test driven developmentin Rust programming. There are lot of options comes with cargo test command.


Writing test functions with #[test] in your program makes the code more readable as well as its makes easy to understand the functionalities of the program for others . I have seen that even I can start running the complex projects and see outputs of some of the modules or functions in those projects.
code repository - https://github.com/davnav/parser_try.git

Thursday, April 23, 2020

Traverse through 2 dimensional list


A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list
Slicing the list and manipulating the list is easy in python. Since its a sequence is iterable, we can use for loop on list. "Sequence in Python, sequence is the generic term for an ordered set.Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable - they can be changed. Elements can be reassigned or removed, and new elements can be inserted" python two dimensional list

Wednesday, April 22, 2020

Python TRIE


Python implementation for TRIE


TRIE is very interesting data structure and its very much used in word suggestion algorithms. Python implementation for this would easiest way to learn it.



Python Trie example 1

Tuesday, April 21, 2020

Python Generator


Have you ever  think through how to build an function that generate infinite series ?
You might be  thinking why can't we use an infinite loop! the problem is you can't come of the loop and again resume from the position/state where you stopped .

But generators helps in this case, they are just like functions but the difference is they have yield keyword in the definition which is like a return wrapped with generator-iterator object

"When you call either a function or a generator, a stackframe is created. It has the local variables
(including the arguments passed into the function),a code pointer to the active opcode, and a stack for pending try-blocks,with-blocks, or loops.

In a regular function, execution begins immediately.When return is encountered, the final result is kept and the stackframe is freed along with everything it referenced.

In a generator function, the stackframe is wrapped in a generator-iterator object and returned immediately. The code in the generator function only runs when called by next(g) or g.send(v). Execution is suspended when yield is encountered. "



Python generator infinite sequence example

Python generator things to be known ( exhausted generator )


Sunday, April 19, 2020

How to deploy python programs to Docker

docker python

Running python programs in Docker

It is quite easy to run your python programs in docker. For getting started with python in docker, we need to install docker in our system.

I am using Ubuntu and followed the instruction details beginners commands to start with Docker.

Once you have installed docker, we can start building our docker. We can create a separate folder/directory for this in our system.

FROM tells Docker which image you base your image on (in the example, Python 3).

RUN tells Docker which additional commands to execute.

CMD tells Docker to execute the command when the image loads.

Dockerfile:

#base image
FROM python:3

#adding our first program/application to docker image
ADD app.py /
ADD app2.py /

#this time we are using a script to run our applications.
ADD script.sh /
#make the script executable
RUN ["chmod", "+x", "./script.sh"]
#changed the command to run the script
CMD ./script.sh 
#you can read more about commands in docker at https://docs.docker.com
#add the command instruction
#CMD ["python","./app.py"]

script file:

You can specify your python programs in the script.
Note: I have explicitly used python3 to run the programs.

#!bin/bash

#first process
python3 app.py

#second process
python3 app2.py

Python applications

print("this is my first pythong docker program")

python function arguments


Python function arguments 

You might have seen *, ** (asterisks ) in functions definition -arguments in python. Don't confuse them with C pointers.They are actually totally different.

Below examples take you through  defaults ,keyed and VarArgs parameter passing in python.


Reference: https://python.swaroopch.com/functions.html






python function arguments Example 1

python function arguments Example 2

Python function arguments Example 3



Python arguments with tuple and dictionary

Friday, April 17, 2020

6 ways to run python


Python language interpreter

Python interpreter can be used many ways from command line.

Beginners guide: https://wiki.python.org/moin/BeginnersGuide

Developers guide: https://devguide.python.org/


  •  Call a Python interactive shell (REPL):
           python
  •   Execute script in a given Python file:
           python script.py
  • Execute script as part of an interactive shell:
          python -i script.py
  •  Execute a Python expression:
          python -c "expression"
  •  Run library module as a script (terminates option list):
          python -m module arguments
  •  Interactively debug a Python script:
         python -m pdb script.py

     
8 beginners commands to start with Docker


Installation guides:


windows - https://docs.docker.com/docker-for-windows/

linux - https://docs.docker.com/engine/install/

mac - https://docs.docker.com/docker-for-mac/install/


docker basic
  1. List currently running docker containers:
    docker ps
  2. List all docker containers (running and stopped):
    docker ps -a
  3. Start a container from an image, with a custom name:
    docker run --name container_name image
  4. Start or stop an existing container:
    docker start|stop container_name
  5. Pull an image from a docker registry:
    docker pull image
  6. Open a shell inside of an already running container:
    docker exec -it container_name sh
  7. Remove a stopped container:
    docker rm container_name
  8. Fetch and follow the logs of a container:
    docker logs -f container_name

Thursday, April 16, 2020

gdb debugging - PART 2

gdb part2

gdb debugging techniques continutaion

This is a continuation from gdb part1 post -
http://naveendavisv.blogspot.com/2020/02/gdb-tips-part1.html

 - Debug an executable:
   gdb executable

 - Attach a process to gdb:
   gdb -p procID

 - Debug with a core file:
   gdb -c core executable

 - Execute given GDB commands upon start:
   gdb -ex "commands" executable

 - Start gdb and pass arguments:
   gdb --args executable argument1 argument2

Wednesday, April 15, 2020

Python data science libraries that you should know

8 python libraries

8 python libraries for data science

1. NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

In python interpreter

>>> import numpy as np
>>> a = np.arange(15).reshape(3,5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

2.scikit-learn - is an open source machine learning library that supports supervised and unsupervised learning. It also provides various tools for model fitting, data preprocessing, model selection and evaluation, and many other utilities.features various algorithms like support vector machine, random forests, and k-neighbours, and it also supports Python numerical and scientific libraries like NumPy and SciPy.


>>> clf = RandomForestClassifier(random_state=0)
>>> x=[[1,2,3],
... [11,12,13]]
>>> y = [0,1]
>>> clf.fit(x,y)
RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None,
                       criterion='gini', max_depth=None, max_features='auto',
                       max_leaf_nodes=None, max_samples=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, n_estimators=100,
                       n_jobs=None, oob_score=False, random_state=0, verbose=0,
                       warm_start=False)
>>> clf.predict(x)
array([0, 1])
>>> clf.predict([[4,5,6],[13,14,15]])
array([0, 1])


3.pandas
When working with tabular data, such as data stored in spreadsheets or databases, Pandas is the right tool for you. Pandas will help you to explore, clean and process your data. In Pandas, a data table is called a DataFrame.

https://pandas.pydata.org/getting_started.html

The primary two components of pandas are the Series and DataFrame

>>> import pandas as pd
>>> data = {
...     'naveen':[50,40,30,20],
...     'John':[23,50,34,22]
... }
>>> marks = pd.DataFrame(data)
>>> marks
   naveen  John
0      50    23
1      40    50
2      30    34
3      20    22
>>> marks = pd.DataFrame(data,index=['English','Maths','Science','History'])
>>> marks
         naveen  John
English      50    23
Maths        40    50
Science      30    34
History      20    22



4.Sympy -
SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible

>>> from sympy import solve,Eq,symbols
>>> x, y, z, d = symbols('x y z d') 
>>> eq1 = Eq(x+y,8) 
>>> eq2 = Eq(x+z,13)  
>>> eq3 = Eq(z+d,6)  
>>> eq3 = Eq(z-d,6)  
>>> eq4 = Eq(y+d,8)
>>> solve(eq1,eq2,eq3,eq4,(x,y,z,d))

5.mathplotlib -Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy

>>> import matplotlib.pyplot as plt
>>> import numpy as nu
>>> x = nu.linspace(0,10,100)
>>> plt.plot(x,x,label='linear')
[<matplotlib.lines.Line2D object at 0x7fe300680910>]
>>> plt.legend()
<matplotlib.legend.Legend object at 0x7fe2f38f6450>
>>> plt.show()

enter image description here

6. Tensorflow
TensorFlow is a free and open-source software library for dataflow and differentiable programming across a range of tasks. It is a symbolic math library, and is also used for machine learning applications such as neural networks.

https://www.tensorflow.org/api_docs/python

https://machinelearningmastery.com/introduction-python-deep-learning-library-tensorflow/

7. Keras: The Python Deep Learning library
Keras is an open-source neural-network library written in Python. It is capable of running on top of TensorFlow, Microsoft Cognitive Toolkit, R, Theano, or PlaidML.
There are two main types of models available in Keras: the Sequential model, and the Model class used with the functional API.

https://keras.io/

8.Scipy -

https://www.scipy.org/getting-started.html

The SciPy ecosystem

Scientific computing in Python builds upon a small core of packages:

  • Python, a general purpose programming language. It is interpreted and dynamically typed and is very well suited for interactive work and quick prototyping, while being powerful enough to write large applications in.

  • NumPy, the fundamental package for numerical computation. It defines the numerical array and matrix types and basic operations on them.

  • The SciPy library, a collection of numerical algorithms and domain-specific toolboxes, including signal processing, optimization, statistics, and much more.

  • Matplotlib, a mature and popular plotting package that provides publication-quality 2-D plotting, as well as rudimentary 3-D plotting.

Sunday, April 12, 2020

10 commands that you should know in Linux networking

networking

1. ifconfig - configure a network interface


examples : -

- View network settings of an ethernet adapter:

ifconfig eth0



- Display details of all interfaces, including disabled interfaces:

ifconfig -a



- Disable eth0 interface:

ifconfig eth0 down



- Enable eth0 interface:

ifconfig eth0 up



- Assign IP address to eth0 interface:

ifconfig eth0 ip_address


2. traceroute - to trace route or path of the packets to the destination machine

examples :

- Traceroute to a host:

traceroute host

  

- Disable IP address and host name mapping:

traceroute -n host

  

- Specify wait time for response:

traceroute -w 0.5 host

  

- Specify number of queries per hop:

traceroute -q 5 host

  

- Specify size in bytes of probing packet:

traceroute host 42

  

3.telnet - The telnet command is used for interactive communication with another host using the TELNET protocol.


- Telnet to the default port of a host:

telnet host



- Telnet to a specific port of a host:

telnet ip_address port



- Exit a telnet session:

quit



- Emit the default escape character combination for terminating the session:

Ctrl + ]



- Start telnet with "x" as the session termination character:

telnet -e x ip_address port

4.nslookup - nslookup is a program to query Internet domain name servers.


- Query your system's default name server for an IP address (A record) of the domain:

nslookup example.com



- Query a given name server for a NS record of the domain:

nslookup -type=NS example.com 8.8.8.8



- Query for a reverse lookup (PTR record) of an IP address:

nslookup -type=PTR 54.240.162.118



- Query for ANY available records using TCP protocol:

nslookup -vc -type=ANY example.com



- Query a given name server for the whole zone file (zone transfer) of the domain using TCP protocol:

nslookup -vc -type=AXFR example.com name_server



- Query for a mail server (MX record) of the domain, showing details of the transaction:

nslookup -type=MX -debug example.com



- Query a given name server on a specific port number for a TXT record of the domain:

nslookup -port=port_number -type=TXT example.com name_server

5. netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.


- List all ports:

netstat -a



- List all listening ports:

netstat -l



- List listening TCP ports:

netstat -t



- Display PID and program names:

netstat -p



- List information continuously:

netstat -c



- List routes and do not resolve IP to hostname:

netstat -rn



- List listening TCP and UDP ports (+ user and process if you're root):

netstat -lepunt



- Print the routing table:

netstat -nr


6. ip - show / manipulate routing, devices, policy routing and tunnels


- List interfaces with detailed info:

ip a



- Display the routing table:

ip r



- Show neighbors (ARP table):

ip n



- Make an interface up/down:

ip link set interface up/down



- Add/Delete an ip address to an interface:

ip addr add/del ip/mask dev interface



- Add a default route:

ip route add default via ip dev interface


7. nmap - Network exploration tool and security / port scanner

- Try to determine whether the specified hosts are up and what are their names:

nmap -sn ip_or_hostname optional_another_address

  

- Like above, but also run a default 1000-port TCP scan if host seems up:

nmap ip_or_hostname optional_another_address

  

- Also enable scripts, service detection, OS fingerprinting and traceroute:

nmap -A address_or_addresses

  

- Assume good network connection and speed up execution:

nmap -T4 address_or_addresses

  

- Scan a specific list of ports (use -p- for all ports 1-65535):

nmap -p port1,port2,…,portN address_or_addresses

  

- Perform TCP and UDP scanning (use -sU for UDP only, -sZ for SCTP, -sO for IP):

nmap -sSU address_or_addresses

  

- Perform TLS cipher scan against a host to determine supported ciphers and SSL/TLS protocols:

nmap --script ssl-enum-ciphers address_or_addresses -p 443

  

8.ping - send ICMP ECHO_REQUEST to network hosts

- Ping host:

ping host

  

- Ping a host only a specific number of times:

ping -c count host

  

- Ping host, specifying the interval in seconds between requests (default is 1 second):

ping -i seconds host

  

- Ping host without trying to lookup symbolic names for addresses:

ping -n host

  

- Ping host and ring the bell when a packet is received (if your terminal supports it):

ping -a host

  

- Also display a message if no response was received:

ping -O host

9.ip link - network device configuration


ip link add [ link DEVICE ] [ name ] NAME



ip link set { DEVICE | group GROUP } { up | down | arp { on | off } } etc...



10. docker network command - Manage networks. You can use subcommands to create, inspect, list, remove, prune, connect, and disconnect networks.




docker network connect  Connect a container to a network

docker network create  Create a network

docker network disconnect  Disconnect a container from a network

docker network inspect  Display detailed information on one or more networks

docker network ls  List networks

docker network prune  Remove all unused networks

docker network rm  Remove one or more networks

Tuesday, April 7, 2020

Understanding hashing and applications of hashing - Rust

Rust hashing

Rust Hashing

I am seeing hashing everywhere like block chain, load balancing ,Cryptographic hash functions,Password Verification,key-value pair data structures in programming languages.

So thought of checking how to do hashing Rust and looks like its pretty easy to do in Rust as well.

#![allow(unused)]
fn main() {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

#[derive(Hash)]
struct Person {
    id: u32,
    name: String,
    phone: u64,
}

let person1 = Person {
    id: 5,
    name: "Janet".to_string(),
    phone: 555_666_7777,
};
let person2 = Person {
    id: 5,
    name: "Bob".to_string(),
    phone: 555_666_7777,
};

assert!(calculate_hash(&person1) != calculate_hash(&person2));

fn calculate_hash<T: Hash>(t: &T) -> u64 {
    let mut s = DefaultHasher::new();
    t.hash(&mut s);
    println!("{:?}",s.finish());
    s.finish()
}
}

hashing code example in rust

Sunday, April 5, 2020

How do C and Rust programs differs in memory safety -Example 3

memory-safety 2

Memory safety example 3

Dangling Pointers in C

If you try to free a pointer and then try to access it, the C compiler won’t complains it. But you will be come to know that bug in the run time.

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 int main(){
  5 
  6   int* ptr = (int*) malloc(2*sizeof(int));
  7 
  8   *ptr= 10;
  9    ptr++;
 10   *ptr = 20;
 11 
 12   free(ptr);
 13 
 14   printf("pointer values are %d",*ptr);
 15 
 16 }

This is the runtime error: I know that you hate runt ime errors . But that is what happens when we try to access pointers that are already freed. We won’t any clue until we encounter this error in C.

======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f97ccd4e7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f97ccd5737a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f97ccd5b53c]
./a.out[0x4005f1]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f97cccf7830]
./a.out[0x4004e9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:06 6554103                            /home/naveen/rustprojects/mar2020/C_Rust_$omp/a.out
00600000-00601000 r--p 00000000 08:06 6554103                            /home/naveen/rustprojects/mar2020/C_Rust_$omp/a.out
00601000-00602000 rw-p 00001000 08:06 6554103                            /home/naveen/rustprojects/mar2020/C_Rust_$omp/a.out
020fa000-0211b000 rw-p 00000000 00:00 0                                  [heap]
7f97c8000000-7f97c8021000 rw-p 00000000 00:00 0 

But Rust save us here.

Rust:

 1 fn main() {
  2 
  3     let a = vec!(10,11,14); //  vector 'a' is initialized.
  4     let p = &a ; // reference to the value in 'a'.
  5 
  6     drop(a);   //free the memory allocated for 'a'
  7     
  8     //we can try to access  values in 'a' through reference 'p'
  9     println!(" values in a = {:?}",*p);
 10 }
                


The famous error comes in compile time itself

Rust complains “borrow later used here” means we dropped the value and but still trying to access it.

borrow means: when we create a reference to the value, we are just borrowing the value.In this case the ownership of the value still remains with ‘a’.

So when we dropped the value ‘a’. The borrowed reference is also become invalid and we can’t use it later point in the program.

error[E0505]: cannot move out of `a` because it is borrowed
  --> src/main.rs:9:10
   |
7  |     let p = &a ; // reference to the value in 'a'.
   |             -- borrow of `a` occurs here
8  |     
9  |     drop(a);   //free the memory allocated for 'a'
   |          ^ move out of `a` occurs here
...
12 |     println!(" values in a = {:?}",*p);
   |                                    -- borrow later used here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0505`.
error: could not compile `dangling`.

To learn more, run the command again with --verbose.

Saturday, April 4, 2020

sqlite is beautiful

I got amazed seeing the byte code instructions listing from the explain command in sqlite. It is such a beautiful design that opcodes are listed from the underlying virtual machine. This helps easy to learn more on the virtual machine design and opcodes.

sqlite> explain select * from tbl1 where a > 20;
addr  opcode         p1    p2    p3    p4             p5  comment     
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     10    0                    00  Start at 10 
1     OpenRead       0     2     0     2              00  root=2 iDb=0; tbl1
2     Rewind         0     9     0                    00             
3       Column         0     0     1                    00  r[1]=tbl1.a 
4       Le             2     8     1     (BINARY)       51  if r[1]<=r[2] goto 8
5       Column         0     0     3                    00  r[3]=tbl1.a 
6       Column         0     1     4                    00  r[4]=tbl1.b 
7       ResultRow      3     2     0                    00  output=r[3..4]
8     Next           0     3     0                    01             
9     Halt           0     0     0                    00             
10    Transaction    0     0     1     0              01  usesStmtJournal=0
11    Integer        20    2     0                    00  r[2]=20     
12    Goto           0     1     0                    00             


Wednesday, April 1, 2020

How do C and Rust programs differs in memory safety - Example 2

memory-safety 2

Memory safety example 2

This is one of the well known problem in C programming language - Array Overflow.
C compiler really won’t care the boundary of arrays , you can even point to the value beyond array length using a pointer as if you are traversing through it.

C Program

  1 
  2 int main(){
  3 
  4     int a[3] =  {1,2,3 };
  5     char c = 'a';
  6     char d = 'b';
  //pointer to the array, 
  //usually array itself is a pointer
  // to the first address of the array 
  7     printf("array = %d " , *a );
  8     printf("array = %d " , *(a+1) );
  9     printf("array = %d " , *(a+2) );
 10     
 11     
 12     //memory overflow , we are trying to access beyond array's length
 13     //but compiling is not complaining
 14 
 15     printf("array = %d " , *(a+3) );
 16     printf("array = %d " , a[5] );
 17 
 18 }

Output is

array = 1 array = 2 array = 3 array = 0 array = 32764 

We will see how Rust program restricts this vulnerability .

Rust Program

trying to create pointer and dereferencing it below, but compiler catches it

 1 
  2 fn main() {
  3 
  4     let a = [1,2,4];
  5 
  6     let p = &a;
  7     
  8     println!("array ={:?}",*p+1);
  9 
 10 }

error is

--> src/main.rs:8:31
  |
8 |     println!("array ={:?}",*(p+1)); 
  |                              -^- {integer}
  |                              |
  |                              &[{integer}; 3]


if you try to access it through index, as a[3] , below is the error


error: this operation will panic at runtime
 --> src/main.rs:8:26
  |
8 |    println!("array = {}",a[3]);
  |                          ^^^^ index out of bounds: the len is 3 but the index is 3
  |
  = note: `#[deny(unconditional_panic)]` on by default