Pages

Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Python Class: A simple and clear explanation.

I have been trying to understand class in python and failed to use it any useful purpose. Then, I came across this  webage where I got a clear understanding of Class in Python. To understand this, I went through so many pages and blogposts. But I didn't get it. 

https://micropyramid.com/blog/understand-self-and-__init__-method-in-python-class/

See the code blow (taken from above link). 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Car(object):
  """
    blueprint for car
  """

  def __init__(self, model, color, company, speed_limit):
    self.color = color
    self.company = company
    self.speed_limit = speed_limit
    self.model = model

  def start(self):
    print("started")

  def stop(self):
    print("stopped")

  def accelarate(self):
    print("accelarating...")
    "accelarator functionality here"

  def change_gear(self, gear_type):
    print("gear changed")
    " gear related functionality here"

This code is an example for a class in python language.

As you can see, this is a collection of functions (def) grouped in a class named Car. 

What is the use of Class?

Class is used to collect list of definitions and provide a functionality to crosscommunicate with all other definitions inside the Class. 

What is the best explanation for Class? Do you have any link? 

Comment and provide link here. 

Thanks. 

RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility in jupyter notebook python

/usr/lib/python3.5/importlib/_bootstrap.py:222:
RuntimeWarning: numpy.dtype size changed, 
may indicate binary incompatibility.

Expected 96, got 88 return f(*args, **kwds)
This warning is harmless. 
This is due to the Numpy version 1.15.0 or 1.15.1

You can ignore this. 
Or you can go back to Numpy version 1.14.5 

You can do this by

conda install numpy1.14.5

Or, you can wait until Numpy1.15.2 is released.








Useful Python modules for Physics/Chemists

Numpy

pymatgen : Pymatgen (Python Materials Genomics) is a Python library for materials analysis. The features of this module are: Classes for the representation of Element, Site, Molecule, Structure objects, extensive input/output support, including support for VASP (http://cms.mpi.univie.ac.at/vasp/), ABINIT (http://www.abinit.org/), CIF, Gaussian, XYZ, and many other file formats. Also powerful analysis tools, including generation of phase diagrams, Pourbaix diagrams, diffusion analyses, reactions, etc.
Electronic structure analyses, such as density of states and band structure.

Scipy

Sympy

Pythran
Pythoran is a module to covert python code to C++ code. Highly useful for computational chemistry and physics calculations.





Related links
To know whether a particular module is installed or not, follow this procedure.



Everything you need to know about Jupyter Notebook

To start a notebook 
In terminal, run

jupyter-notebook 

Now, the notebook will be opened in your default browser.

To change the current line as comment

Ctrl + /   (this will change the current line in to a comment. To convert multiple lines of code in to a comment, select the lines that you want to convert and then hit Ctrl + /

Running different programs in Jupyter-Notebook

You can run Bash comments in jupyter-notebook. In any cell, type %%bash then that shell can be used to to bash comments. Similarly, other programming languages also can be run. A detailed post will be written on this topic.   

Check if a Python module installed or not

For a beginner, it may be confusing whether a particular module in Python (either 2.* or 3.*). The simple way to check (in terminal is ) to import the module you want.

In the terminal, first go to python2 or python3 prompt. Then import the module you want to check. For example, if I you want to check if Numpy is installed or not, type "import numpy". If installed, the python interpreter will import. Otherwise, error message will be displayed.

Now example.

For python2, try:

~$ python     [in the terminal]
Python 2.7.12 (default, Oct  8 2019 time stamp)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy     [Enter]
>>>
Here note that you get a new >>> prompt and this means that numpy is already installed.

Now, I want to check a module called pythran is installed or not.

~$ python
Python 2.7.12 (default, Oct  8 2019, time stamp)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> pythran       [Enter]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'pythran' is not defined
>>>
This error means that this particular module is not installed.

We are here to help you what we learn. Comment here if you need any help on this or any question related to this post.



You may be interested in these posts

Error in image file conversion: convert-im6.q16: not authorized `test.eps' @ error/constitute.c/WriteImage/1037.

This error is because of the vulnerability. This allows remote execution of code using image formats. So, some Linux distributions by defaul...