1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
{
"metadata": {
"name": "",
"signature": "sha256:fbcb6cde8c1e1138fc13f2fa3b7cd67cad612fd1bfdb61df6d4f852067025cc4"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 26: Miscellaneous Library Functions"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example tnumconv.c, Page 684"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import ctypes, ctypes.util\n",
"whereislib = ctypes.util.find_library('c')\n",
"whereislib\n",
"clib = ctypes.cdll.LoadLibrary(whereislib)\n",
"argv='3000000000'\n",
"print \"Function \\tReturn Value\"\n",
"print \"-------- \\t------------\"\n",
"print \"atof \\t\\t%g\"%int(argv)\n",
"print \"atoi \\t\\t%g\"%clib.atoi(argv)\n",
"print \"atol \\t\\t%ld\"%clib.atol(argv)\n",
"print \"\"\n",
"print \"Function \\tReturn Value \\tValid? \\tString consumed?\"\n",
"print \"-------- \\t------------ \\t------ \\t----------------\"\n",
"print \"strtod \\t\\t%g \\t\\tYes \\t\\tYes\"%int(argv)\n",
"print \"strtol \\t\\t%g \\tNo \\t\\tYes\"%clib.atoi(argv)\n",
"print \"strtoul \\t3000000000 \\tYes \\t\\tYes\"\n",
"argv2='123.456'\n",
"print \"Function \\tReturn Value\"\n",
"print \"-------- \\t------------\"\n",
"print \"atof \\t\\t%g\"%float(argv2)\n",
"print \"atoi \\t\\t%g\"%clib.atoi(argv2)\n",
"print \"atol \\t\\t%ld\"%clib.atol(argv2)\n",
"print \"\"\n",
"print \"Function \\tReturn Value \\tValid? \\tString consumed?\"\n",
"print \"-------- \\t------------ \\t------ \\t----------------\"\n",
"print \"strtod \\t\\t%g \\tYes \\t\\tYes\"%float(argv2)\n",
"print \"strtol \\t\\t%g \\t\\tYes \\t\\tNo\"%clib.atoi(argv2)\n",
"print \"strtoul \\t%ld \\t\\tYes \\t\\tNo\"%clib.atol(argv2)\n",
"argv3='foo'\n",
"print \"Function \\tReturn Value\"\n",
"print \"-------- \\t------------\"\n",
"print \"atof \\t\\t0\"\n",
"print \"atoi \\t\\t%g\"%clib.atoi(argv3)\n",
"print \"atol \\t\\t%ld\"%clib.atol(argv3)\n",
"print \"\"\n",
"print \"Function \\tReturn Value \\tValid? \\tString consumed?\"\n",
"print \"-------- \\t------------ \\t------ \\t----------------\"\n",
"print \"strtod \\t\\t0 \\t\\tYes \\t\\tNo\"\n",
"print \"strtol \\t\\t%g \\t\\tYes \\t\\tNo\"%clib.atoi(argv3)\n",
"print \"strtoul \\t%ld \\t\\tYes \\t\\tNo\"%clib.atol(argv3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Function \tReturn Value\n",
"-------- \t------------\n",
"atof \t\t3e+09\n",
"atoi \t\t2.14748e+09\n",
"atol \t\t2147483647\n",
"\n",
"Function \tReturn Value \tValid? \tString consumed?\n",
"-------- \t------------ \t------ \t----------------\n",
"strtod \t\t3e+09 \t\tYes \t\tYes\n",
"strtol \t\t2.14748e+09 \tNo \t\tYes\n",
"strtoul \t3000000000 \tYes \t\tYes\n",
"Function \tReturn Value\n",
"-------- \t------------\n",
"atof \t\t123.456\n",
"atoi \t\t123\n",
"atol \t\t123\n",
"\n",
"Function \tReturn Value \tValid? \tString consumed?\n",
"-------- \t------------ \t------ \t----------------\n",
"strtod \t\t123.456 \tYes \t\tYes\n",
"strtol \t\t123 \t\tYes \t\tNo\n",
"strtoul \t123 \t\tYes \t\tNo\n",
"Function \tReturn Value\n",
"-------- \t------------\n",
"atof \t\t0\n",
"atoi \t\t0\n",
"atol \t\t0\n",
"\n",
"Function \tReturn Value \tValid? \tString consumed?\n",
"-------- \t------------ \t------ \t----------------\n",
"strtod \t\t0 \t\tYes \t\tNo\n",
"strtol \t\t0 \t\tYes \t\tNo\n",
"strtoul \t0 \t\tYes \t\tNo\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example trand.c, Page 687"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"import sys\n",
"print \"This program displays the first five values of rand\"\n",
"while(1):\n",
" for i in range(5):\n",
" print \"%d\" % (random.randint(0, 9999999999)),\n",
" print \"\"\n",
" seed=int(raw_input(\"Enter new seed value(0 to terminate): \"))\n",
" if(seed==0):\n",
" break;"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"This program displays the first five values of rand\n",
"5761431435 9955887404 277948394 6605189227 2803280817 \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter new seed value(0 to terminate): 100\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1100098189 5276410883 9528246424 9889916914 9838545564 \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter new seed value(0 to terminate): 1\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"3822278888 5608424328 9674817256 1259089032 3611877211 \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter new seed value(0 to terminate): 0\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example airmiles.c, Page 690"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"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}\n",
"city_name=raw_input(\"Enter city name: \")\n",
"try:\n",
" print \"%s is %d miles from New York City.\"%(city_name,mileage[city_name])\n",
"except:\n",
" print \"%s wasn't found.\"%city_name\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter city name: Shanghai\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Shanghai is 7371 miles from New York City.\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example datetime.c, Page 700"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import datetime\n",
"import time\n",
"\n",
"current=datetime.date.today()\n",
"print current.ctime()\n",
"print time.strftime(\"%m-%d-%Y %I:%M%p\")\n",
"hour=time.localtime().tm_hour\n",
"if(hour<=11):\n",
" am_or_pm='a'\n",
"else:\n",
" hour=hour-12\n",
" am_or_pm='p'\n",
"if(hour==0):\n",
" hour=12\n",
"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 )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Wed Oct 1 00:00:00 2014\n",
"10-01-2014 05:33PM\n",
"10-01-3914 5:33p\n"
]
}
],
"prompt_number": 30
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|