Python

From DreamsteepWiki

Jump to: navigation, search

Contents

Python Guides & Tutorials

http://diveintopython.org/ - Dive into Python Book

http://www.poromenos.org/tutorials/python - Learn Python in 10 Minutes

http://docs.python.org/tut/ - Python Tutorial by Guido van Rossum

http://wiki.python.org/moin/BeginnersGuide - Beginner's Guide to Python

http://www.awaretek.com/tutorials.html - Python Tutorials

http://www.blendernation.com/2006/09/22/learning-python/ - Blender Nation Learning Python

http://www.ibiblio.org/obp/thinkCSpy/ - How to Think Like a Computer Scientist

http://www.pythonware.com/daily/ - Daily Python URL

http://swaroopch.info/text/Byte_of_Python:Main_Page - Byte of Python


Python Script Examples

Human Readable Random Password

## Generate a human readable 'random' password
## password  will be generated in the form 'word'+digits+'word'
## eg.,nice137pass
## parameters: number of 'characters' , number of 'digits'
## Pradeep Kishore Gowda <pradeep at btbytes.com >
## License : GPL
## Date : 2005.April.15
## Revision 1.2
## ChangeLog:
## 1.1 - fixed typos
## 1.2 - renamed functions _apart & _npart to a_part & n_part as zope does not allow functions to
## start with _

def nicepass(alpha=6,numeric=2):
    """
    returns a human-readble password (say rol86din instead of
    a difficult to remember K8Yn9muL )
    """
    import string
    import random
    vowels = ['a','e','i','o','u']
    consonants = [a for a in string.ascii_lowercase if a not in vowels]
    digits = string.digits

    ####utility functions
    def a_part(slen):
        ret = ''
        for i in range(slen):
            if i%2 ==0:
                randid = random.randint(0,20) #number of consonants
                ret += consonants[randid]
            else:
                randid = random.randint(0,4) #number of vowels
                ret += vowels[randid]
        return ret

    def n_part(slen):
        ret = ''
        for i in range(slen):
            randid = random.randint(0,9) #number of digits
            ret += digits[randid]
        return ret

    ####
    fpl = alpha/2
    if alpha % 2 :
        fpl = int(alpha/2) + 1
    lpl = alpha - fpl

    start = a_part(fpl)
    mid = n_part(numeric)
    end = a_part(lpl)

    return "%s%s%s" % (start,mid,end)

if __name__ == "__main__":
    print nicepass(6,2)


Print Float with 2 Decimal Places in Python

You need to format the string twice - first, to generate the float formatting string, and then to format the string.


Like this:

num = 7.12345678901234567

for i in xrange(3,7):

print ("%%.%if" % i) % num


Note the %% that produces a single % for the second string interpolation.


>>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN)
Decimal('7.32')

>>> Decimal('7.325').quantize(Decimal('1.'), rounding=ROUND_UP)
Decimal('8')


Adding a File Extension to Files with Python

#!/usr/local/bin/python
# add extention to my files
# Author: mayia pi 2009

import os

extention = raw_input('enter the extention u want added (without the dot):\n')
ext = '.' + extention
print 'adding ' + ext + ' to everything...but .py'

n = 0
for fname in os.listdir(os.getcwd()):
    if fname[-2:] <> 'py' :
        newfname = fname + ext
        os.rename(fname, newfname)
        print fname + ' renamed ' + newfname
        n +=1
print ' --- ' + str(n) + ' files renamed '
Personal tools