Skip to main content

Posts

Python Interview Questions

Recent posts

getattrib, Setattrib, hasattrib and delattrib in python

# `getattr(object, name[, default])` Function in Python The `getattr(object, name[, default])` function returns the value of a named attribute of an object, where `name` must be a string. If the object has an attribute with the specified `name`, then the value of that attribute is returned. On the other hand, if the object does not have an attribute with `name`, then the value of `default` is returned, or `AttributeError` is raised if `default` is not provided. ```python >>> t = ('This', 'is', 'a', 'tuple') >>> t.index('is') 1 >>> getattr(t, 'index') <built-in method index of tuple object at 0x10c15e680> >>> getattr(t, 'index')('is') 1 ``` when the attribute is not defined, ```python >>> getattr(t, 'len') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'len

Python tips

python tips: In [1]: os.listdir(os.getcwd())==os.listdir(os.curdir) Out[1]: True In [2]: os.listdir(os.getcwd())==os.listdir('.') Out[2]: True     ******* WAP for print a character its position times in a word. word=raw_input("Enter the word:") for i in word:      print i*word.index(i) Output: ****** WAP for print a character its ASCII values times in a word. word=raw_input("Enter the word:") for i in word: print i*ord(i) #alternatively # print [i*ord(i) for i in word]

Initiating static local HTTP Server using different scripting languages

Initiating HTTP Server using different scripting languages Python     python 2.x         - python -m SimpleHTTPServer 8000     python 3.x         - python -m http.server 8000 http-server (Node.js)     npm install -g http-server # install dependency     http-server -p 8000 node-static (Node.js)     npm install -g node-static # install dependency     static -p 8000 Ruby     ruby -rwebrick -e’WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => Dir.pwd).start’     Ruby 1.9.2+         - ruby -run -ehttpd . -p3000     adsf (Ruby)         - gem install adsf # install dependency         - adsf -p 8000 Perl ----     cpan HTTP::Server::Brick # install dependency         - $ perl -MHTTP::Server::Brick -e ‘$s=HTTP::Server::Brick->new(port=>8000); $s->mount(“/”=>{path=>”.”}); $s->start’     Plack (Perl)         - cpan Plack # install dependency         - plackup -MPlack::App::Directory -e ‘Plack::App::Dire

AWS CLI installation procedure in centos/ RHEL

1)  Make sure you have the epel repository installed sudo yum install -y epel-release  2)  Install needed packages sudo yum install -y python2-pip 3)  Install awscli sudo yum install -y awscli 4) Configure awscli aws configure --profile <profile_name> 4) Configure awscli. If you do not add --profile <profile_name> , it will use the default profile. aws configure --profile <profile_name> Using the --profile allows you to create profiles for other accounts using different keys. You can also create profiles for different regions.

Python Programming: Interview Questions

Python Programming :: Interview Question 1