CyberKeeda In Social Media

Python : Print() examples with parameters

Print

Nothing more is required to explain what does the print function does, it's name is itself self explanatory.
Print function is used to display /print messages into the screen.

Within python we can use ether pair of single inverted commas '  ' or double inverted "  "  within single brackets (  )  both gives the same result.

>>> print  ('Hello World')

Hello World

Print() with Parameters.

I'm adding some of the print function usage along with various parameters.

Print() with Sep

SEP commonly a short form of separator,  Separator between the arguments to print() function in Python is space by default (softspace feature) , which can be modified and can be made to any character, integer or string as per our choice.

Default behaviour of python with softspace.

>>> print('A','B','C')
A B C

#Trick to disable softspace using sep parameter
>>> print('A','B','C',sep='')
ABC

One more example.

>>> print('13','11','2018')
13 11 2018

#Trick to format a date
>>> print('13','11','2018', sep='-')
13-11-2018

Print() with End

End parameter along with print can be used to align data into a same single line along with required character,symbol or word.
Example:

>>> print('Welcome')
>>> print('to')
>>> print('cyberkeeda.com')

Output:
Welcome
 to
Cyberkeeda.com
Now, with help of end parameter, we can allign all these three different lines by one, along with extra characters too

>>> print('Welcome', end '  ')
>>> print('to', end '  ')
>>> print('cyberkeeda.com', end ' ! ')
Output:
Welcome to Cyberkeeda.com !

One more example ( SEP with END )
>>> print(10,20,30,sep=':', end='...')
>>> print(60,70,80,sep='-')

Output:
10:20:30...60-70-80

Print Formatted strings and Variables on the same line.

Before knowing anything, please note the below abbreviations.

%i==>  int
%d==> int
%f==>  float
%s==>  str

print("formatted String" %(variable list))

Examples.

>>> print("value is :%d" %(12))
value is  : 12

>>> print("My name is :%s and age is :%d" %('Cyberkeeda',23))
My name is : Cberkeeda and age is  : 23


Read more ...

Python : How to Import Modules


Let's get to know,  what are the various ways to import a module within a python file.

We will take example of one of the famous module named as " MATH  " and a function sqrt (used to calculate the square rootwithin the module.
It provide access to mathematical functions defined by C,  to get to know more about it visit the official link

Here are the various ways we can import it.

Import the whole module.

import math

Usage.
module_name.function_name

import math
x=16
print(math.sqrt(16))

Create alias of the imported module.

In case, if you feel using such a big name of your module or nor relevant to it's behavior, you can create the module's name alias.
Let's take the example of the same math module, i will be creating alias m for it.

import math as m

Usage:

import math as m
x=16
print(m.sqrt(x))

Import only specific function, functions from a particular module.

A particular module consists of lot of functions in it, sometimes, we just need only a particular function out of entire module.
Again let's take the same module as an example.module math contains, a lot of functions within it some  are as below.
sqrt()
ceil()
floor()
pow(x,y)
factorial()
fabs()
fmod(x,y)
sin()
cos()

Now, in case we need only one function sqrt() , here is the way how we only import a function from a module.

from math import sqrt
Usage:

from math import sqrt
x=16
print(sqrt(x))

In case, if we need more then one function.
from math import sqrt, cos, sin
x=16
print(cos(x))

Now, if you just simply don't want to write math every time and want to use all of the functions within the module.
from math import *
x=16
print(cos(x))
print(sqrt(x))
print(sin(x))
Read more ...

AWK equivalent in Windows CMD




Missing AWK on Windows ?  for /f  may help you.
Lets see how can we implement the same.


Example.

C:\Users\Cyberkeeda>netsh interface show interface

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Connected      Dedicated        Wi-Fi
Enabled        Connected      Dedicated        VMware Network Adapter VMnet1
Enabled        Connected      Dedicated        VMware Network Adapter VMnet8
Enabled        Disconnected   Dedicated        Ethernet

Lets assume we have saved the same within a file in linux names as file.txt

Within Linux, to gather the State of interfaces, we use the below command.

cat file.txt| awk '{print $2}'

State
Connected
Connected
Connected
Disconnected

In Windows, it can be accomplished with FOR /F

C:\Users\cyberkeeda>for /f "tokens=2" %a in ('netsh interface show interface') do @echo %a

State
Connected
Connected
Connected
Disconnected

Now, if wish to add two values .

cat file.txt| awk '{print $1 " " $2}'

Admin State
-------------------------------------------------------------------------
Enabled Connected
Enabled Connected
Enabled Connected
Enabled Disconnected

Same can be accomplished within Windows as

C:\Users\cyberkeeda>for /f "tokens=1,2" %a in ('netsh interface show interface') do @echo %a %b
Admin State
-------------------------------------------------------------------------
Enabled Connected
Enabled Connected
Enabled Connected
Enabled Disconnected

In case, if you want to additional string along with the variable.

C:\Users\cyberkeeda>for /f "tokens=1,2" %a in ('netsh interface show interface') do @echo Boot Stats= %a Ethernet stat= %b


Now in case, if you want to use further filter within it, below one liner can help you.

C:\Users\cyberkeeda>netsh interface show interface | for /f "tokens=2" %a in ('findstr Wi-Fi') do @echo %a
Connected




Read more ...

How to Create Windows 10 Bootable USB on Ubuntu or any Linux desktop



Are you looking for any desktop application on Ubuntu to create a bootable pendrive/flash drive with Windows.

WoeUSB is a simple tool that enable you to create your own usb stick windows installer from an iso image or a real DVD.


WoeUSB  package contains two programs:
  • woeusb: A command-line utility that enables you to create your own bootable Windows installation USB storage device from an existing Windows Installation disc or disk image
  • woeusbgui: A GUI wrapper of woeusb based on WxWidgets


So, lets know how install the WoeUSB on Ubuntu 
Official & updated Installation and Github link : Here
#    sudo add-apt-repository ppa:nilarimogard/webupd8
#    sudo apt update
#    sudo apt install woeusb
In case if you wish to remove that woeusb PPA, remove it from the bellow command.
#    sudo add-apt-repository --remove ppa:nilarimogard/webupd8
#    sudo apt-update
Now, WoeUSB has installed on my Ubuntu, though the interface is self explanatory still let's move ahead and know how to use it.

Open WoeUSB application and choose the USB drive and your windows iso kept within your Ubuntu.
Once you are done click install and you are done, you can monitor the progress on screen.


In case you receive a error dialogue like below, follow the below steps

Find your USB drive's mounted file system and block by running lsblk on terminal and find your device their.
To filter, look for size of your USB or name of the USB drive, below screen shot gives the picture of my USB drive ( 8 GB ) size, and named as /dev/sdc
#    lsblk

So we will unmount that partition out of the system.
Notice there  /dev/sdc is the full size of my USB disk, but the data is witten on it's /dev/sdc1 partition.
My USB disk is  /dev/sdc, ensure your USB is unmounted with the following command. Replace /dev/sdc1 with your own partition name.
#    sudo umount /dev/sdc1
Now try again from the   WoeUSB desktop application.
In case if you are willing to do the same through woeusb CLI utility, here is the way.
#    sudo woeusb -v --device /home/kunal/windows-10.iso /dev/sdc
/home/kunal/windows-10.iso  :  Replace it with your iso location.
/dev/sdc : replace it with your USB drive ( check it from lsblk )
Read more ...

PIP SSL eeror while installing : SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:579)


If you too, have encountered SSL errors while installation/upgrading any python package from pip.
Here below is the workaround for it.


You might be flooded by this errors.
Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed 
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:579) 

Workaround.

Ignore SSL errors by setting pypi.org and files.pythonhosted.org as trusted hosts.
#  pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org <package_name>
Lets assume i want to install pika through pip 
#  pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org pika
Read more ...

How to Configure PostgreSQL user and Database

Content.

In this blog post, we will cover the below content.
  • Creating PostgreSQL users
  • Creating PostgreSQL databases
  • Adding an existing user to a database
  • Deleting PostgreSQL User
  • Deleting PostgreSQL Database.

By default PostgresSQL installation always includes the postgres superuser ( Linux Shell User) and postgresql database user too. Initially you have to use the same postgres user to coonnect to PostgreSQL until you create other users.
A PostgreSQL users are also known as role. 


Follow the steps to create a PostgreSQL user.


Use the passwd copmmand ( passwd postgres )to change the postgres linux user's password.
Just after PostgreSQL db instllation, change the password for default postgres  database superuser'spassword.
#    su - postgres
# psql -d template1 -c "ALTER USER postgres WITH PASSWORD 'newpassword';"
Now we are all set to create new database user, switch again to postgres super user
#    su - postgres
#    createuser muser --pwprompt
Above crateuser will prompt you few questions.
  • Enter password for new role: prompt, type a password for the user.
  • Enter it again: prompt, retype the password.
  • Shall the new role be a superuser? prompt, type y if you want to grant superuser access. Otherwise, type n.
  • Shall the new role be allowed to create databases? prompt, type y if you want to allow the user to create new databases. Otherwise, type n.
  • Shall the new role be allowed to create more new roles? prompt, type y if you want to allow the user to create new users. Otherwise, type n.

Follow the steps to create a PostgreSQL Database.

Open a new linux terminal from root and run the commands one by one.
#    su - postgres
One can now run commands as the PostgreSQL superuser. 

To create a database, type the following command. Replace user with the name of the user that you want to own the database, and replace dbname with the name of the database that you want to create.

PostgreSQL users that have permission to create databases can do so from their own accounts by typing the following command, replacing dbname is the name of the database to create.
#    createdb -O myuser dbname
#    createdb dbname

Follow the steps to add a PostgreSQL db user to existing  Database.

Run the psql command program as the database's owner, or as the postgres superuser.
I will be using postgres user here
#    GRANT permissions ON DATABASE dbname TO username;
Replace above dbname and username as per your requirement.

Follow the steps to delete a PostgreSQL user.

Important point to consider before deleting a db user.


If the user owns any databases or other objects, you cannot drop the user. Instead, you receive an error message " dropuser: removal of role "username" failed: ERROR:  role "username" cannot be dropped because some objects depend on it "
DETAIL:  owner of database dbname "

You should change the database's owner (or drop the database entirely), and then you can drop the user, then run the below command
#    dropuser username
Follow the steps to delete a PostgreSQL Database
#    dropdb dbname

    Read more ...
    Related Posts Plugin for WordPress, Blogger...
    Designed By Kunal Saha