Chapter 26: Miscellaneous Library Functions

Example tnumconv.c, Page 684

In [3]:
import ctypes, ctypes.util
whereislib = ctypes.util.find_library('c')
whereislib
clib = ctypes.cdll.LoadLibrary(whereislib)
argv='3000000000'
print "Function \tReturn Value"
print "-------- \t------------"
print "atof \t\t%g"%int(argv)
print "atoi \t\t%g"%clib.atoi(argv)
print "atol \t\t%ld"%clib.atol(argv)
print ""
print "Function \tReturn Value \tValid? \tString consumed?"
print "-------- \t------------ \t------ \t----------------"
print "strtod \t\t%g \t\tYes \t\tYes"%int(argv)
print "strtol \t\t%g \tNo \t\tYes"%clib.atoi(argv)
print "strtoul \t3000000000 \tYes \t\tYes"
argv2='123.456'
print "Function \tReturn Value"
print "-------- \t------------"
print "atof \t\t%g"%float(argv2)
print "atoi \t\t%g"%clib.atoi(argv2)
print "atol \t\t%ld"%clib.atol(argv2)
print ""
print "Function \tReturn Value \tValid? \tString consumed?"
print "-------- \t------------ \t------ \t----------------"
print "strtod \t\t%g \tYes \t\tYes"%float(argv2)
print "strtol \t\t%g \t\tYes \t\tNo"%clib.atoi(argv2)
print "strtoul \t%ld \t\tYes \t\tNo"%clib.atol(argv2)
argv3='foo'
print "Function \tReturn Value"
print "-------- \t------------"
print "atof \t\t0"
print "atoi \t\t%g"%clib.atoi(argv3)
print "atol \t\t%ld"%clib.atol(argv3)
print ""
print "Function \tReturn Value \tValid? \tString consumed?"
print "-------- \t------------ \t------ \t----------------"
print "strtod \t\t0 \t\tYes \t\tNo"
print "strtol \t\t%g \t\tYes \t\tNo"%clib.atoi(argv3)
print "strtoul \t%ld \t\tYes \t\tNo"%clib.atol(argv3)
Function 	Return Value
-------- 	------------
atof 		3e+09
atoi 		2.14748e+09
atol 		2147483647

Function 	Return Value 	Valid? 	String consumed?
-------- 	------------ 	------ 	----------------
strtod 		3e+09 		Yes 		Yes
strtol 		2.14748e+09 	No 		Yes
strtoul 	3000000000 	Yes 		Yes
Function 	Return Value
-------- 	------------
atof 		123.456
atoi 		123
atol 		123

Function 	Return Value 	Valid? 	String consumed?
-------- 	------------ 	------ 	----------------
strtod 		123.456 	Yes 		Yes
strtol 		123 		Yes 		No
strtoul 	123 		Yes 		No
Function 	Return Value
-------- 	------------
atof 		0
atoi 		0
atol 		0

Function 	Return Value 	Valid? 	String consumed?
-------- 	------------ 	------ 	----------------
strtod 		0 		Yes 		No
strtol 		0 		Yes 		No
strtoul 	0 		Yes 		No

Example trand.c, Page 687

In [2]:
import random
import sys
print "This program displays the first five values of rand"
while(1):
    for i in range(5):
        print "%d" % (random.randint(0, 9999999999)),
    print ""
    seed=int(raw_input("Enter new seed value(0 to terminate): "))
    if(seed==0):
        break;
This program displays the first five values of rand
5761431435 9955887404 277948394 6605189227 2803280817 
Enter new seed value(0 to terminate): 100
1100098189 5276410883 9528246424 9889916914 9838545564 
Enter new seed value(0 to terminate): 1
3822278888 5608424328 9674817256 1259089032 3611877211 
Enter new seed value(0 to terminate): 0

Example airmiles.c, Page 690

In [3]:
mileage={"Berlin": 3965, "Buenos Aires": 5297, "Cairo": 5602, "Calcutta": 7918,"Cape Town": 7764, "Caracas": 2132,"Chicago": 713, "Hong Kong": 8054,"Honolulu": 4964, "Istanbul": 4975, "Lisbon": 3354,"London": 3458,"Los Angeles": 2451, "Manila": 3498,"Mexico City": 2094,  "Montreal": 320,"Moscow": 4665, "Paris": 3624,"Rio de Janeiro": 4817, "Rome": 4281,"San Francisco": 2571, "Shanghai": 7371,"Stockholm": 3924, "Sydney": 9933,"Tokyo": 6740, "Warsaw": 4344,"Washington": 205}
city_name=raw_input("Enter city name: ")
try:
    print "%s is %d miles from New York City."%(city_name,mileage[city_name])
except:
    print "%s wasn't found."%city_name
    
Enter city name: Shanghai
Shanghai is 7371 miles from New York City.

Example datetime.c, Page 700

In [30]:
import datetime
import time

current=datetime.date.today()
print current.ctime()
print time.strftime("%m-%d-%Y %I:%M%p")
hour=time.localtime().tm_hour
if(hour<=11):
    am_or_pm='a'
else:
    hour=hour-12
    am_or_pm='p'
if(hour==0):
    hour=12
print "%.2d-%.2d-%d %2d:%.2d%c"%((time.localtime().tm_mon),time.localtime().tm_mday,( time.localtime().tm_year+1900),hour,time.localtime().tm_min, am_or_pm )
Wed Oct  1 00:00:00 2014
10-01-2014 05:33PM
10-01-3914  5:33p
In [ ]: