Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > BOOK: Python Projects
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Python Projects
This is the forum to discuss the Wrox book Python Projects Laura Cassell, Alan Gauld; ISBN: 978-1-118-90866-2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Python Projects section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 17th, 2014, 11:54 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 1 - bits.py problems

I must say, starting off the book with bitwise operations is a bit difficult for someone just past the beginner level. A lot is presumed about knowing binary operations and how to count in binary. Aside from that rant I'm having trouble getting my code to match with the book output. I was trying this on a fresh Ubuntu 14.04 VPS, using python3 interpreter

In my bits.py file:

Code:
def bit(val, idx):
    mask = 1 << idx # all 0 except idx
    return bool(val & 1)
I'm supposed to get:

>>> bin.bits(0b0101, 0)
True
>>> bin.bits(0b0101, 1)
False

But I get True returned for both!

I think the 'bit' function is also messing up my 'listbits' function. Here's my code:

Code:
def listbits(val):
    num = len(bin(val)) - 2
    result = []
    for n in range(num):
        result.append( 1 if bit(val, n) else 0 )
    return list( reversed(result) )
Now, the following operation is supposed to occur:
>>> bits.listbits(0b10111)
[1, 0, 1, 1, 1]

However, I get back:
[1, 1, 1, 1, 1]

I think this is something to do with the 'bit' function call in 'listbits' but I can't figure out what's going on because I don't understand binary too well. Any ideas?
Reply With Quote
  #2 (permalink)  
Old December 17th, 2014, 12:02 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Typo

I've discovered that it is actually a typo in the book's code. I figured this out by downloading the Chapter 1 code and viewing the same function for 'bit'. It should read:

Code:
def bit(val, idx): 
    mask = 1 << idx # all 0 except idx
    return bool(val & mask)
Where '1' in the last line should have been 'mask'. This fixes all the issues I was having. Hope this helps others who were as confused as I was!
Reply With Quote
  #3 (permalink)  
Old February 27th, 2015, 01:27 PM
Wrox Author
Points: 109, Level: 2
Points: 109, Level: 2 Points: 109, Level: 2 Points: 109, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2015
Location: Stirling, Scotland
Posts: 22
Thanks: 0
Thanked 3 Times in 3 Posts
Default bits.py bug

My apologies, there is indeed a bug in the code in the book.

The download files are (or should be!) correct because they are
the actual files I used in testing. The code in the book has been
copy/pasted/reformatted/reviewed and then reformatted again.
There are lots of opportunities for things to go astray.

Its still my fault however, for not spotting it in the final copy so
apologies once more.

Alan G.
Reply With Quote
  #4 (permalink)  
Old February 27th, 2015, 01:34 PM
Wrox Author
Points: 109, Level: 2
Points: 109, Level: 2 Points: 109, Level: 2 Points: 109, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2015
Location: Stirling, Scotland
Posts: 22
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
Originally Posted by saltycraig View Post
I must say, starting off the book with bitwise operations is a bit difficult for someone just past the beginner level. A lot is presumed about knowing binary operations and how to count in binary.
Sorry if it seems a bit (!) hard but I wanted the module/package to be
genuinely useful and because bitmasks crop up all over the OS stuff in
chapter 2, I thought providing an option that was slightly easier to use
than the standard bitwise operators would be worthwhile.

Alan G.

PS. I'm a little late on the scene as I only just discovered this forum.
Reply With Quote
  #5 (permalink)  
Old October 16th, 2015, 07:44 PM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2015
Posts: 3
Thanks: 2
Thanked 2 Times in 2 Posts
Default

I just started reading the book recently and became curious as to whether other readers had commented on the section about bitwise operators, because it was indeed a bit of a challenge. Like saltycraig I'm just (barely) past beginner's level. (I got an introduction to some Java programming many years ago). Now I am in a situation where I have very specific and relatively simple programming needs for a business, and felt it would be nice to use that need to learn some more programming. I purchased this book for three reasons: I immediately identified with "WHO THIS BOOK IS FOR" in the introduction, I liked the structure, and, most importantly, I was looking for a book with practical exercises (including solutions!).

Fortunately I started reading during a holiday and had plenty of time to google around to learn more about bitwise operators. In particular, the following example in step 4 on page 37 was difficult to understand (despite the How-it-works comments on the next page):
Code:
>>> bin(bits.NOT(0b0101))
'-0b110'
But I agree that after I had figured out the details, it was a nice example that immediately gives the reader some pretty fundamental insights into programming basics.

However, as a reader I would have appreciated very much if there had been a short explanation, only a few sentences, to explain 1) how byte size is related to the bit representation of numbers, 2) the concept of two's complement for representing negative numbers, and 3) that there is a difference between the computer's physical bit representation of the number and Python's binary representation of that number. Once this is figured out, it's easy to see why 0b0101 in the example is represented (in 8 bits for simplicity) as 00000101. Performing bitwise NOT (switching the ones and zeros) on this trivially gives 11111010, which is (by definition) the two's complement representation of the number -6. And in Python the binary representation of 6 is '0b110', so -6 is simply '-0b110'.
Code:
>>> 0b110
6
>>> -0b110
-6

In the next step of the example it is necessary to understand the conversion between binary and hexadecimal numbers, which is pretty straightforward, but some readers might appreciated a little nudge in the right direction. Likewise, the concept of a bitmask deserves an extra sentence to explain it to the newcomers.

I learned a lot from studying this example, but I spent several hours over the course of a couple of days before it made sense.
Reply With Quote
  #6 (permalink)  
Old October 17th, 2015, 12:39 PM
Wrox Author
Points: 109, Level: 2
Points: 109, Level: 2 Points: 109, Level: 2 Points: 109, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2015
Location: Stirling, Scotland
Posts: 22
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Your points are all valid and well made, however the purpose of the example was primarily to produce a module/package and I decided I wanted it to be useful rather than just a toy. I considered giving more theoretical background but decided it would distract from the actual focus of packaging the code.

As I stated in an earlier reply, I chose this functionality because I knew it could be useful for dealing with the bitmasks encountered in the OS section of Chapter 2. Perhaps, coming from an electronics background, I underestimated just how foreign a concept bitmasks and binary logic are to programming beginners. If that's the case I apologize in advance to all future readers! Hopefully Wikipedia can compensate... :-)

Alan g.
Reply With Quote
The Following User Says Thank You to Alan G For This Useful Post:
Egalth (October 19th, 2015)
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating an array of size 2^16 bits scoobie C++ Programming 2 November 9th, 2005 07:59 PM
Turning strings into bits...help patpicos BOOK: Beginning Java 2 2 March 29th, 2005 05:09 AM
inverting bits sdilucca Visual C++ 0 July 2nd, 2004 02:14 PM



All times are GMT -4. The time now is 02:25 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.