Python

Q. Show Python operator precedence descending order.

 

Operator

Description
() Parentheses (grouping)
f(args…) Function call
x[index:index] Slicing
x[index] Subscription
x.attribute Attribute reference
** Exponentiation
~x Bitwise not
+x, –x Positive, negative
*, /, % Multiplication, division, remainder
+, – Addition, subtraction
<<, >> Bitwise shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
in, not in, is, is not, <, <=,  >,  >=,
<>, !=, ==
Comparisons, membership, identity
not x Boolean NOT
and Boolean AND
or Boolean OR
lambda Lambda expression

 

 

Q. How do you create a Python package?

There are a number of utilities that allow you to create a Python package. Python packages are not RPM’s or Debian packages, instead Python uses it’s own packages. The two most common methods to create Python packages are using a program called Setuptools or using Disutils. In this example I will demonstrate creating a package using distutils. Distutils is part of standard Python distribution, so you do not have to install any additional software. If you have Python installed you probably have distutils. The other method is to use setuptools, which is not part of standard Python and you have to download it.

* Create a directory with the package name
* Create at least the following items in the directory:

    bin/
    CHANGES.txt
    docs/
    LICENSE.txt
    MANIFEST.in
    README.txt
    setup.py
    projectname/
        __init__.py
        test/
            __init__.py

* Put in bin dir any scripts
* In CHANGES.txt write : v, — Initial release.
* Docs dir should contain docs with filename extension of *.txt
* MANIFEST.in should contain:

include *.txt
recursive-include docs *.txt

* README.txt should be written in reST, an example is below:

===========
My Project
===========

This project is totally awesome!

* Most important file is setup.py, which should be of the below format:

from distutils.core import setup

setup(
    name='myproject',
    version='0.1.0',
    author='John Doe',
    author_email='john@example.com',
    packages=['myproject'],
    scripts=['bin/myproject'],
    url='http://pypi.python.org/pypi/Tmyproject/',
    license='LICENSE.txt',
    description='Project files.',
    long_description=open('README.txt').read(),
    install_requires=[
        "Django >= 1.1.1",
        "caldav == 0.1.4",
    ],
)

Reference: http://guide.python-distribute.org/creation.html
Distutils: http://docs.python.org/2/distutils/
Setuptools: http://pythonhosted.org/an_example_pypi_project/setuptools.html

Q. How do you upload a Python package to PyPi?
The below method uses distutils to upload a package.
Test your package first:

python setup.py sdist 
python setup.py register -r https://testpypi.python.org/pypi
python setup.py sdist upload -r https://testpypi.python.org/pypi
sudo pip install -i https://testpypi.python.org/pypi xssh

Then install on PyPi:

python setup.py sdist
python setup.py register
python setup.py sdist upload

Q. What is pep8 and how would you use it?
A. Pep8 is a python coding convention that can be found here http://www.python.org/dev/peps/pep-0008/.
An example of a coding convention in pep8 is the recommended use of spaces and not tabs when indenting python.
Another example is that of using inline comments sparingly.
Below is an example of using pep8.

sudo pip install pep8
$ cat mycode.py 
name='syedali'

$ pep8 --show-source mycode.py --statistics
mycode.py:1:5: E225 missing whitespace around operator
name='syedali'
    ^
1       E225 missing whitespace around operator

$ autopep8 -i mycode.py 

$ cat mycode.py 
name = 'syedali'

$ pep8 --show-source mycode.py --statistics

In order to automatically correct for coding convention install autopep8.

sudo pip install autopep8
autopep8 -i mycode.py

Q. How do you install multiple versions of Python on CentOS?

Download, compile and install.

sudo wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
sudo tar xf Python-2.7.5.tar.bz2 
cd Python-2.7.5
./configure --prefix=/usr/local
make && make altinstall

Install setuptools.

/usr/local/bin/python2.7 ez_setup.py 

Download, compile and install.

sudo wget http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.xz
sudo tar xf Python-3.3.2.tar.xz
cd Python-3.3.2
./configure --prefix=/usr/local
make && make altinstall

Install setuptools.

/usr/local/bin/python3.3 ez_setup.py

Q. How do you install Virtual-Env?
A. pip install virtualenv
Reference: http://simononsoftware.com/virtualenv-tutorial/

Q. How do you install PIP without using yum?
A. Same as installing PIP on MacOSX which is below.

Q. How do you install PIP on MacOSX?
A. First install setuptools by downloading https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py and then running ‘python ez_setup.py’. After that download get-pip.py from here https://raw.github.com/pypa/pip/master/contrib/get-pip.py, and run ‘python get-pip.py’.
This will install pip on your system. Reference: http://www.pip-installer.org/en/latest/installing.html#python-os-support

Q. How do you install PIP on CentOS 6.x?
A. Enable EPEL repo, and then install PIP.

sudo wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
sudo vim /etc/yum.repos.d/remi.repo #set enable to 0 to 1
sudo yum -y install python-pip

Q. How do you install PIP on Fedora?
A. ‘sudo yum install python-pip’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s