Friday, January 4, 2019

Python Standard Library.

Batteries Included
datetime
import datetime
datetime.date
datetime.time
datetime.datetime
datetime.datetime (year=2019, month=1, day=1, hour=0, minute=00, second=0)
# Click for more...
Python datetime library
decimal
# for floating point arithmetic

import decimal
decimal.Decimal
decimal.Decimal ('0.001')
d = decimal.Decimal ('0.001')
0.1+0.1+0.1
0.1+0.1+0.1 == 0.3
d + d + d == 0.003
d + d + d == decimal.Decimal('0.003')

decimal.getcontext()
dd = decimal.Decimal
four = dd('4')
four.sqrt()
dd('4').sqrt()
dd('3').sqrt()
decimal.getcontext().prec=50
dd('3').sqrt()
glob
# same as wildcard * in shell that expand names
glob.glob ('path_name' + '/*')
itertools
import itertools
for i in itertools.product ('xyz','1234'):
    print (i)
for i in itertools.permutations ([1,2,3]):
    print (i)
math, cmath and random
import math
math.e
math.pi
math.sqrt(25)
math.sqrt(55)
math.log(1024)
math.log2(64)
math.sin(math.pi/2)

# cmat support complex number and complex roots beyond quadroot.py
math.sqrt(-39)
cmath.sqrt(-39)
type (cmath.sqrt(-39))
cmath.polar (4+8j)

random.randrange (-10,1)
x = [ 1,2,3,4,5 ]
random.shuffle (x)
random.choice (x)
random.choice ('string')
os and shutil
statistics
import statistics as st
mylist = [ random.randrange(-5,6) for i in range(10) ]
mylist
# do it again and see what happens
st.mean (mylist)
st.median (mylist)
st.stdev (mylist)
sys
import sys
sys.modules
dir(sys)
sys.argv
for i, argv in enumerate (sys.argv):
    print ("arg {} is {!r}".format (i, argv))
sys.stdin
sys.stdout
sys.stderr
sys.stdout.writelines (sorted (sys.stdin.readlines () ))
# to stop ctrl-D
# or run sysstd.py < my.txt
subprocess
import subprocess as sp
p1 = sp.Popen (['cat','my.txt'], stdout=sp.PIPE)
p2 = sp.Popen (['sort'], stdin=p1.stdout, stdout=sp.PIPE)
pout, perr = p2.communicate()
out
err
turtle
import turtle
turtle.forward (100)
urllib, urllib.request
import urllib.request

x = urllib.request.urlopen('https://www.google.com/')
print(x.read())
webbrowser
import webbrowser
webbrowser.open ("http://www.google.com")
More...
Coming up...

No comments:

Post a Comment