Skip to main content

Posts

Installation of Python 3 in Linux

Beliefs As our belief, every Linux flavor has Python as one of the Shell. but that shell was created long back based on the OS release time repositories. In the most common situation where built-in Python might be at Python 2.7.x version but the latest version is on Python 3.7.x. The big challenge here is 'how do I upgrade or install Python3 on Linux?' So I've chased this challenge and completed the latest version installed on my Oracle Linux box. Python 3 installation on RHELflavors How do I install Python3 latest version on Linux? Python Programming is simple and kids can learn by doing! It's capabilities to interact with machine internals and the simple structure makes easy to write and understand it. The latest Python version for download you can find from the Python official site . Here I'm installing Python3 on the Oracle Linux same steps will be followed on any RHEL flavors as well. yum -y groupinstall development yum -y install zlib-devel Note: I...

Control your PC from Python Code

Hey guys, One of the easiest programming languages in the World is our Python. Here this post is intended for those who want to learn the hacking techniques! Here I would like to share simple 4lines of Python hack tricks that make shutdown or restart your PC. The logic here is that have the function contains the CMD Command line 'shutdown' with forceful(/s) timeout as 1 minute. Shutdown logic is here... import os def shutdown(): os.system("shutdown /s /t 1") shutdown() Restart logic is here import os def restart(): os.system("shutdown /r /t 1") restart() Jump to Linux VM and same thought to execute the same script then it failed! "Must be root." The solution there you need to be superuser to shutdown your Linux machine. To have better understand about the shutdown command in Linux operating system, used the --help option. [vagrant@mydev ~]$ sudo shutdown --help shutdown [OPTIONS...] [TIME] [WALL...] Shut down the system. ...

Program Controls and loops in Python

Python Programming controls The flow of program will be controlled with conditional statements where this flow  if condition if-else condition if-elif condition while loop while - else loop for loop for with if- else loop if condition Here I would like to work on program control statements, wherein relate some operating system functionalities so that, this would give some basic prototype for the sys admin scripts. #!/usr/bin/python # This illustrate if condition # Filename : ifex.py import os d=os.listdir('/home/pavanbsd/pybin') print d f='ifex.py' if f in d: print "File exists" else: print "Not found..." if-elif-else ladder No worries, coming soon... When you need the task that need to be done repeatedly then your choice is using loops. Python provides two simple loop constructs easy to use! More powerful then other scripting compare to, it has 'else' block. while loop Understand the power of while loop,...

Python Sets and Dictionaries

Python Sets Python Maps examples A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. Dictionaries consist of pairs (called items) of keys and their corresponding values. Dictionaries can be created by placing a comma-separated list of key: value pairs within curly braces Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. Python Set Operations and methods Python Set methods continueed >>> SET={'new','old','list','new'} >>> SET set(['new', 'old', 'list']) Lets play with set operations, it is more like school math here... >>> s={1,2,3,4} >>> k={3,4,5,6} >>> len(s) 4 >>> len(k) 4 >>> 1 in s True >>...

Advance Object Oriented programming in Python

The following program will illustrates the method overriding concept onf OOP. Here parent and child classes have defined with the same method name and signature. According to context the method would be executed. The actual meaning of overriding means ignoring the parent method when both have same method signature, and executing the derived class method. ===== METHOD OVERRIDING SAMPLE PROGRAM ===== class FirstClass: #define the super class def setdata(self, value): # define methods self.data = value # ‘self’ refers to an instance def display(self): print self.data class SecondClass(FirstClass): # inherits from FirstClass def display(self): # redefines display print 'Current Data = %s' % self.data x=FirstClass() # instance of FirstClass y=SecondClass() # instance of SecondClass x.setdata('Before Method Overloading') y.setdata('After Method Overloading') x.display() y.display() ===== Static and Class Method Sample Prog...

Regular Expressions in Python

This session about Python Regular expressions how we can work with patterns and the specific methods available in re module. match() group(), groups() search() compile() sub() Regular expression sample in Python Here is the sample for match function use. #!/usr/bin/python """ This program illustrates the usage of match, group functions in re module This also shows how to use re flags """ import re line = "Python Orientation course helps professionals fish best opportunities" m = re.match( r'(.*) helps (.*?) .*', line, re.I) if m: print "m.group() : ", m.group() print "m.group(1) : ", m.group(1) print "m.group(2) : ", m.group(2) else: print "Don't have match!!" The output >>> execfile('c:/pybin/rematch.py') m.group() : Python Orientation course helps professionals fish best opportunities m.group(1) : Python Orientation course m.group(2) : ...

Functions in Python

Heere we have experimenting with functions in Python, Functions can be defined for specific task, functions are prepared to reuse them. modular programming can be defined as breaking the bigger task into chunks of blocks this can be achived with the Python functions. Over all we are going to know in depth knowledge on how Python function is powerful then other structure programs. We have seen some built-in functions which you don't need to import any modules. str(), int(), float(), bool() -- type definitions type(), id(), dir() --  introspecting the functions len(), range() -- sequance collection related functions print(), input(), raw_input() -- input output functions Python Function overview Function definition  Calling Function in assignment Adding DocString to Function  Function Execution – Scope of variables The import and reload Defining inner functions Lambda functions Function definition structure 1. Procedure - which do not ha...