Download presentation
Presentation is loading. Please wait.
1
CS2911 Week 2, Class 2 Today Review Muddiest Point Quiz
12 July 2019 CS2911 Week 2, Class 2 Today Review Muddiest Point Quiz Supplementary Python app Encoding in Python Muddiest Point Parsing in Python 17q ,2,4,6,7,24 17q1 v (started 1_1) Recall the raw representations of the ASCII characters '\r' (CR) and '\n' (LF) v(started 1_1) Recall the raw representations of the ASCII characters A, B, C, D, E, a, b, c, d, e, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 v(started 1_2) Convert a many-digit binary number to its big-endian and little-endian formats v(started 1_2) Give examples of common systems using the big-endian and little-endian formats v Describe the difference between a compiled an an interpreted language v Write a list literal in Python v Write a tuple literal in Python v Index into a tuple or list in Python v Describe the difference between lists and tuples v Write if-statements in Python v Write a counting (for) loop in Python v Write a sentinel-value (while) loop in Python Write code that manipulates raw bytes in Python using ord(), chr(), int.from_bytes(), and i.to_bytes() Write code that interprets and formats strings using str() and int() (Write exceptions in Python) Declare a Python method Write a string literal in Python Write a bytes literal in Python CS2911 Dr. Yoder Dr. Josiah Yoder
2
Muddiest Point 2-1 and Lab 2
CS2911 12 July 2019 Muddiest Point 2-1 and Lab 2 It's very unclear what we actually have to code in Lab 3. Lab 3 So what exactly is ord ord/chr should the pre-lab be printed? And today wasnt too muddy. Im getting better at converting between binary and hex Lab 2 everything was clear for me none Using the keyword arguments keyword arguments Do you want our predictions for the lab printed out, or is a digital format sufficent Why is 5 stored as 53? ASCII decimal Dr. Josiah Yoder
3
CS2911 12 July 2019 Quiz 1 Dr. Josiah Yoder
4
Supplementary Python materials
CS2911 12 July 2019 Supplementary Python materials Learn Python app from SoloLearn Available for Android and iPhone Supplementary – If you learn something here that is missing in class, please tell me Quizzes and interactive environment may be helpful Alternative: Redo an SE1011 lab with Python Note: We are not doing OO in Python this quarter Thanks to a student for recommending this app Dr. Josiah Yoder
5
Questions about Lab 3? Design process How to implement in Python?
CS2911 12 July 2019 Questions about Lab 3? Design process How to implement in Python? Protocol definition … Dr. Josiah Yoder
6
Exercise: Brainstorm steps (large or small) needed to complete Lab 3
CS2911 12 July 2019 Exercise: Brainstorm steps (large or small) needed to complete Lab 3 Dr. Josiah Yoder
7
Python 3 types int 123 integer (any size) str '123' Unicode string
CS2911 12 July 2019 Python 3 types int 123 integer (any size) str '123' Unicode string bytes b'123' raw bytes or ASCII string UPDATED NEXT LECTURE Dr. Josiah Yoder
8
How integers are stored conceptually in Python
CS2910 12 July 2019 How integers are stored conceptually in Python i = 5 101 i = 255 i = 1000 i = (Length is also stored, and bigger numbers are internally broken up into chunks) = 2^31-1 is the Java's Integer.MAX_VALUE. But Python 3 integers can be as large as you want them to be. class 1-2: Slide 21 Dr. Josiah Yoder
9
How strings are conceptually stored in in Python
CS2910 12 July 2019 How strings are conceptually stored in in Python s = '0123' (Actually, 32 bits are stored for each character, but only the first 21 bits are used.) class 1-2: Slide 27 Dr. Josiah Yoder
10
Arbitrary byte literals
CS2910 12 July 2019 Arbitrary byte literals 5 = 0x05 b'\x05' # = 0x0e b'\x0e' # = 0b = 0x64 b'\x64' # These really do store just one byte, even though you use four characters to type them in your code: b = '\x05' len(b) # 1 Dr. Josiah Yoder
11
How bytes are stored in Python
CS2910 12 July 2019 How bytes are stored in Python (Length is also stored) s = b'ABC' s = b'0123' s = b'\r\n' Dr. Josiah Yoder
12
ord() and chr() i = 5 # int literal s = chr(i) # 101
CS2910 12 July 2019 ord() and chr() i = 5 # int literal # 101 s = chr(i) # s = 'a' # str literal # i = ord(s) # Strings are actually stored with 32 bits, but only the first 2 bits are used for a Unicode code-point, because so far, those are the only Unicode code-points defined. Class 1-2: Slide 28 Dr. Josiah Yoder
13
bytes can be sliced like strings
CS2910 12 July 2019 bytes can be sliced like strings b = b'ABC' # b[1:3] # # b'BC' But not indexed like strings! b[0] # … # 65 (an integer) Dr. Josiah Yoder
14
Converting between strings and bytes
CS2910 12 July 2019 Converting between strings and bytes # Use UTF-8 for the byte string # If a non-ASCII characters is used in s, # multiple bytes in b will replace it. s = bytes.decode(b) b = s.encode() # Use ASCII for the byte string # Only ASCII characters can be used in s s = bytes.decode(b,'ASCII') b = s.encode('ASCII') Dr. Josiah Yoder
15
CS2910 12 July 2019 int() and str() s = b'1234' # # # # i = int(s) # Dr. Josiah Yoder
16
CS2910 12 July 2019 int() and str() i = 999 # s = str(i) # Dr. Josiah Yoder
17
CS2910 12 July 2019 int(…,16) s = '1AFF' # # # i = int(s,16) # Dr. Josiah Yoder
18
CS2910 12 July 2019 format(…,'x') i = 0xAFF # s =format(i,'04x') # # # # ('0aff') hex(i)[2:] is popular, but doesn't have a way to include padding zeros. It also doesn't work for negative numbers. format(0xAFF,'+05x') is quite predictable. format(0xAFF,'x') also works nicely if you don't care how many digits there are. Note that you can also use 'b', 'o' bases Dr. Josiah Yoder
19
CS2910 12 July 2019 .to_bytes() i = 0xAFF = # b = i.to_bytes(2,'big') # # b'\n\xff' i2 = int.from_bytes(b,'big') # class method Dr. Josiah Yoder
20
CS2910 12 July 2019 Keyword Arguments You can include the names of the parameters as "keywords" on your arguments: b = i.to_bytes(length=2, byteorder='big') # length – number of bytes to produce in b i2 = int.from_bytes(bytes=b, byteorder='big') # This can help make code self-documenting Dr. Josiah Yoder
21
Review b'\xbe\xef' = 1011 1110 1110 1111 (as bytes)
CS2910 12 July 2019 Review b'\xbe\xef' = (as bytes) 0xbeef = (as int) (…).to_bytes(…,byteorder='big') int.from_bytes(…,byteorder='big') ord(…) chr(…) int(…) format(…,'x') Dr. Josiah Yoder
22
CS2910 12 July 2019 Question Why might int(…) and format(…) be less efficient than the others for creating a raw encoding? Why are int(…) and chr(…) less natural? Dr. Josiah Yoder
23
https://www.polleverywhere.com/free_text_polls/zIp2tH2IVWjXv4H CS2910
12 July 2019 What was the muddiest point? Dr. Josiah Yoder
24
Exercise: Work on Lab 3 Write method headers with documentation
CS2911 12 July 2019 Exercise: Work on Lab 3 Write method headers with documentation Decide who will write each method Try to figure out what the "hard" parts will be Dr. Josiah Yoder
25
Python byte lists and strings
CS2910 12 July 2019 Python byte lists and strings Python 2.7 Python 3.x str bytearray unicode bytes # ASCII string, array of bytes bytearray # Mutable list of bytes str # Unicode string Dr. Josiah Yoder
26
CS2911 12 July 2019 Acknowledgement This course is based on the text Computer Networking: A Top Down Approach 7th edition Jim Kurose, Keith Ross Addison-Wesley Dr. Josiah Yoder
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.