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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
{
"metadata": {
"name": "Chapter 18"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "<h1>Chapter 18: The C++ I/O System<h1>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.1, Page Number: 421<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementation of overloading << for print in python'''\n\nclass three_d:\n def __init__(self,a,b,c): #3D coordinates\n self.x=a\n self.y=b\n self.z=c\n #Display x,y,z coordinates - three_d inserter.\n def __repr__(self):\n return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n\n#Variable declaration\na=three_d(1,2,3)\nb=three_d(3,4,5)\nc=three_d(5,6,7)\n\n#Result\nprint a,b,c",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1, 2, 3\n 3, 4, 5\n 5, 6, 7\n\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.2, Page Number: 423<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Using a friend function implement overloading of << in python'''\n\nclass thrnee_d:\n def __init__(self,a,b,c): #3D coordinates\n self.x=a\n self.y=b\n self.z=c\n #Display x,y,z coordinates - three_d inserter.\n __repr__=repr \n \n#Friend function \ndef repr():\n return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)+\"\\n\"\n\n#Variable declaration\na=three_d(1,2,3)\nb=three_d(3,4,5)\nc=three_d(5,6,7)\n\nprint a,b,c",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1, 2, 3\n 3, 4, 5\n 5, 6, 7\n\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.3, Page Number: 424<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementation of overloading << and >> in python'''\n\nclass three_d:\n def __init__(self,a,b,c): #3D coordinates\n self.x=a\n self.y=b\n self.z=c\n #Display x,y,z coordinates - three_d inserter.\n def __repr__(self):\n return str(self.x)+\", \"+str(self.y)+\", \"+str(self.z)\n\n#Variable declaration\na=three_d(1,2,3)\n\nprint a\n\n#User input\nprint \"Enter X,Y,Z values:\"\na=three_d(4,5,6) \nprint a",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1, 2, 3\nEnter X,Y,Z values:\n4, 5, 6\n"
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.4, Page Number: 428<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementing some ios flags in python'''\n\n\nprint '{0:+d}'.format(123), #for ios::showpos\nif(123.23>0):\n i='{0:e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:e}'.format(123.23)\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "+123 +1.232300e+02\n"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.5, Page Number: 430<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementing some ios flags in python'''\n\nimport string\n\nprint '{0:+d}'.format(123), #for ios::showpos\nif(123.23>0):\n i='{0:e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:e}'.format(123.23)\n\n \nprint '{:10.2f}'.format(123.23), #2 digits left of decimal\nif(123.23>0):\n i='{0:.2e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:.2e}'.format(123.23)\n \n \nprint '{:#>10}'.format(str(123)), #for ios::fill\nif(123.23>0): \n i='{0:.2e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:.2e}'.format(123.23)\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "+123 +1.232300e+02\n 123.23 +1.23e+02\n#######123 +1.23e+02\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.6, Page Number: 432<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implement io manipulators in python'''\n\nprint '{0:.0e}'.format(1000.243) #for setprecision\nprint '{:>20}'.format(\"Hello There\") #to set width and right align\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1e+03\n Hello There\n"
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.7, Page Number: 433<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementing some ios flags in python'''\n\nprint '{0:+d}'.format(123), #for ios::showpos\nif(123.23>0):\n i='{0:e}'.format(123.23) #for ios::scientific \n i='+'+i\n print i\nelse :\n print '{0:e}'.format(123.23)\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "+123 +1.232300e+02\n"
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.8, Page Number: 433<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Removing leading whitespaces from a string'''\n\n#User input\ns=\" Hello\"\n\n#Result\nprint s.lstrip() #lstrip removes leading spaces",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Hello\n"
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.9, Page Number: 434<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementing some ios flags in python'''\n\ndef setup(s):\n return '{:$<10}'.format(str(s))\n\n\n#Result\nprint 10,setup(10)\n\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "10 10$$$$$$$$\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.10, Page Number: 435<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Prompting the user to enter number in hexadecimal form'''\n\ndef prompt():\n print \"Enter number using hex format:\"\n hex=0x46\n return hex\n\n\n#Result\ni=prompt()\nprint i\n\n",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.11, Page Number: 438<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Program to write to a file'''\n\n#Open a file, creating one if it does not already exist\nout=open(\"test\",'w')\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n #Write to file\n out.write(\"10 123.23\\n\")\n out.write(\"This is a short text file.\")\n #Close the file\n out.close()",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.12, Page Number: 438<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Program to read to a file'''\n\n#Open a file, creating one if it does not already exist\nIn=open(\"test\",'r')\n\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\nelse:\n #Read file\n i=In.read(2)\n ch=In.read(1)\n f=In.read(6)\n str=In.read()\n print i,f,ch\n print str\n #Close the file\n out.close()",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "10 123.23 \n\nThis is a short text file.\n"
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.13, Page Number: 439<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Program to read to a file provided on the screen'''\n#Program cannot be provided here, so error is obtained\n\nimport sys\n \nif not(len(sys.argv)==2):\n print \"Usage: PR <filename>\\n\"\nelse:\n #Open a file\n In=open(sys.argv[1],'r')\n\n #In case file cannot open\n if(not(In)):\n print \"Cannot open file.\"\n else:\n #Read file\n ch=In.read()\n print ch\n In.close()\n \n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Usage: PR <filename>\n\n"
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.14, Page Number: 440<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Program to write to a file'''\n\nimport sys\n\np=\"hello there\"\n\nout=open(\"test\",'w')\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n #Write to file\n for i in range(len(p)):\n out.write(p[i])\n",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.15, Page Number: 441<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Program to read and write in a file'''\n\nimport sys\n\nn=[1,2,3,4,5]\n\n#Open a file 'test'\nout=open(\"test\",'w')\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n #Write to file\n for i in range(5):\n out.write(chr(n[i]))\n out.close()\n \nfor i in range(5): #clear array\n n[i]=0\n \n#Open the file\nIn=open(\"test\",'r')\n\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\nelse:\n #Read file\n for i in range(5):\n n[i]=ord(In.read(1))\n\n#Result, shows value from file\nfor i in range(5):\n print n[i],\n \nIn.close()",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1 2 3 4 5\n"
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.16, Page Number: 442<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Detect end-of-file for a file on the screen'''\n#File cannot be provided here, so program wont work\nimport sys\n \nif not(len(sys.argv)==2):\n print \"Usage: PR <filename>\\n\"\nelse:\n #Open a file\n In=open(sys.argv[1],'r')\n\n #In case file cannot open\n if(not(In)):\n print \"Cannot open file.\"\n else:\n #Read file\n while True:\n ch=In.read(1)\n if not ch:\n break\n print ch,\n #Close file\n In.close()\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Usage: PR <filename>\n\n"
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.17, Page Number: 443<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Program to compare files'''\n\n#Since file names cannot be provided, two \n#files are being created test1 and test2\n\n#creating test1\nout=open(\"test1\",'w')\nout.write(\"Hello\")\nout.close()\n\n#creating test2\nout=open(\"test2\",'w')\nout.write(\"There\")\nout.close()\n\n\n#Open test1\nf1=open(\"test1\",'r')\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\n\n#Open test2\nf2=open(\"test2\",'r')\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\n \nprint \"Comparing files...\"\n\nbuf1=f1.read()\nbuf2=f2.read()\nprint buf1,buf2\n\nif len(buf1)==len(buf2):\n print \"Files are of different sizes.\"\n f1.close()\n f2.close()\nelse:\n #compare contents of buffers\n flag=1\n for i in range(len(buf1)):\n if not(buf1[i]==buf2[i]):\n print \"Files differ.\"\n f1.close()\n f2.close()\n flag=0\n break\n if flag==1:\n print \"Files are the same.\"\n \n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Comparing files...\nHello There\nFiles are of different sizes.\n"
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.18, Page Number: 445<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Read a string and print it'''\n\nprint \"Enter your name:\"\n\n#User imput\nstr=\"hello world\"\n\nprint str",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Enter your name:\nhello world\n"
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.19, Page Number: 447<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Demonstrate random access by writing X at a specified location'''\n\n#Since arguments cannot be provided file test \n#is created and X is written in the 3nd location.\n\n#Open test\nout=open(\"test\",'r+b')\nout.write(\"Hello\")\n\n\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n out.seek(2,0)\n out.write('X')\n out.close()\n \n",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.20, Page Number: 447<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Display a file from a given point'''\n\n#Here, file is displayed from 3nd position\n\n#Open test\nout=open(\"test\",'r+b')\nout.write(\"Hello\")\nout.close()\n\nIn=open(\"test\",'r')\n#In case file cannot open\nif(not(In)):\n print \"Cannot open file.\"\nelse:\n In.seek(2,0)\n ch=In.read()\n print ch\n \n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "llo\n"
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 18.21, Page Number: 450<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implement use of overloaded inserter to write three_d'''\n\nclass three_d:\n def __init__(self,a,b,c):\n self.__x=a\n self.__y=b\n self.__z=c\n def __repr__(self):\n c=(\"%d\"%self.__x)+\", \"+(\"%d\"%self.__y)+\", \"+(\"%d\"%self.__z)+\"\\n\"\n return c \n\n#Variable declaration\na=three_d(1,2,3)\nb=three_d(3,4,5)\nc=three_d(5,6,7)\n\n#Open threed\nout=open(\"threed\",'w')\n#In case file cannot open\nif(not(out)):\n print \"Cannot open file.\"\nelse:\n out.write(a.__repr__())\n out.write(b.__repr__())\n out.write(c.__repr__())\n\n ",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 18
},
{
"cell_type": "raw",
"metadata": {},
"source": ""
}
],
"metadata": {}
}
]
}
|