Monday, 27 November 2017

converting word to number in python


pip install word2number


from word2number import w2n
print(w2n.word_to_num('ten'))
10
print(w2n.word_to_num('one hundred twenty three'))
123
print(w2n.word_to_num('one thousand two hundred twenty three'))
1223

convert number to word in python




pip install n2w

import n2w

print(n2w.convert(12))

'twelve'

print(n2w.convert(123))

'one hundred twenty three'

Tuesday, 14 November 2017

underscores _ in Python

Understanding the underscore( _ ) of Python :

The underscore ( _ ) is special in Python.

There are 5 cases for using the underscore in Python.

  • For storing the value of last expression in interpreter.
  • For ignoring the specific values. (so-called “I don’t care”)
  • To give special meanings and functions to name of variables or functions.
  • To use as ‘Internationalization(i18n)’ or ‘Localization(l10n)’ functions.
  • To separate the digits of number literal value.


When used in interpreter :

  • The python interpreter stores the last expression value to the special variable called ‘_’.


Example:

>>> x=10

>>> x

10

>>> _

10

>>> _*2

20

>>> _

20

>>> _+3

23

>>> _

23

>>> y=_/3

>>> y

7.666666666666667


For Ignoring the values:


  • The underscore is also used for ignoring the specific values. 
  • If you don’t need the specific values or the values are not used, just assign the values to underscore.


Example:

Ignore a value when unpacking:

>>> x, _, y = (1, 2, 3)

>>> x

1

>>> y

3

>>> 

Ignore the multiple values:


  • Note:which is available in only Python 3.x.


>>> x, *_, y = (1, 2, 3, 4, 5)

>>> x

1

>>> y

5

>>> 

Ignore the index:

for _ in range(10):

print("hai")

hai

hai

hai

hai

hai

hai

hai

hai

hai

hai

def do_something( ):

print("hello")

for _ in range(10):

do_something()

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

Ignore a value of specific location:

>>> x=(5,10,15,20,25)

>>> for _,val in x:

do_something()



Give special meanings to name of variables and functions:

  • The underscore may be most used in ‘naming’.
  • The PEP8 which is Python convention guideline introduces the following 4 naming cases.


single underscore( _ ):

  • leading underscore( _ )

example:

_variablename or _functionname or _methodname or _classname


  • This convention is used for declaring protected variables, functions, methods and classes in a module.



  • tailing underscore( _ )

example:

variablename_ or functionname_ or methodname_ or classname_


  • This convention could be used for avoiding conflict with Python keywords or built-ins.


Tkinter.Toplevel(master, class_='ClassName')
# Avoid conflict with 'class' keyword

list_ = List.objects.get(1)
# Avoid conflict with 'list' built-in type


double underscore( _ _ ):


  • leading double underscore(_ _)

example:
 _ _varaiblename or _ _functionname or _ _methodname or _ _classname


  • double underscore will mangle the attribute names of a class to avoide the conflicts of attribute names between classes.
  • (so-called “mangling” that means that the compiler or interpreter modify the variables or function names with some rules, not use as it is). 
  • The mangling rule of Python is adding the “_ClassName” to front of attribute names are declared with double underscore. 
  • That is, if you write method named “__method” in a class, the name will be mangled in “_ClassName__method” form.



  • leading and tailing double underscores(_ _)

example:
_ _variablename_ _ or _ _functionname_ _ or _ _classname_ _ or __methodname__



  • This convention is used for special variables or methods (so-called “magic method”) such as   _ _init_ _  ,  _ _len__  ,          _ _new_ _ ,_ _del_ _,......etc.
  • These methods provides special syntactic features or does special things.


example:
class A:
    def __init__(self, a):
          self.a = a
    def __custom__(self):
               pass


As Internationalization(i18n)/Localization(l10 n) functions :


  • The built-in library gettext which is for i18n/l10n uses this convention, and Django which is Python web framework supports i18n/l10n also introduces and uses this convention.


# see official docs : https://docs.python.org/3/library/gettext.html import gettext
gettext.bindtextdomain('myapplication','/path/to/my/language /directory') gettext.textdomain('myapplication') _ = gettext.gettext
# ...
print(_('This is a translatable string.'))



To separate the digits of number literal value:

  • This feature was added in Python 3.6. It is used for separating digits of numbers using underscore for readability.


>>> x=1_00

>>> x

100

>>> y="siva_krishna"

>>> y

'siva_krishna'

>>> z=0b_101

>>> bin(z)

'0b101'
>>> 

Friday, 3 November 2017

manual debugging in python

python debugging


import  pdb

for i in range(0,10):

            x=i*3

           print(i,x)

           pdb.set_trace()

Note:

n  means next

c  means continue

x  print current x value

i  print current i value


output:

0 0

> d:\siva krishna\python_practice\python tricks\debuggin.py(3)<module>()

-> for i in range(0,10):

(Pdb) i

0

(Pdb) x

0
(Pdb) c

1 3

> d:\siva krishna\python_practice\python tricks\debuggin.py(3)<module>()

-> for i in range(0,10):

(Pdb) i

1

(Pdb) x

3

(Pdb) c

2 6

> d:\siva krishna\python_practice\python tricks\debuggin.py(3)<module>()

-> for i in range(0,10):

(Pdb) i

2

(Pdb) x

6

(Pdb) n

> d:\siva krishna\python_practice\python tricks\debuggin.py(4)<module>()

-> x=i*3

(Pdb) c

3 9

> d:\siva krishna\python_practice\python tricks\debuggin.py(3)<module>()

-> for i in range(0,10):

(Pdb) i

3

(Pdb) x

9


(Pdb) 

Thursday, 2 November 2017

clear the python command window screen



how to clear the python command window screen


C:\Users\siva>python

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> print("hai siva")
hai siva
>>> print("welcome to my blog")
welcome to my blog
>>> x=10
>>> y=20
>>> z=x+y
>>> z
30
>>> import  os
>>> def  cls( ):
...     os.system("CLS")
...
>>> cls( )

>>>