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 April 4th, 2017, 11:28 AM
Registered User
Points: 20, Level: 1
Points: 20, Level: 1 Points: 20, Level: 1 Points: 20, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2017
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default BitMask Try It Out pg40 error

I'm getting the following error for the bitmask project:
>>> bm1==bm3.shiftright(3)
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
bm1==bm3.shiftright(3)
AttributeError: 'int' object has no attribute 'shift right'

I've checked my spelling and checked my code against the supplied code.
I've even imported the supplied file and got the same error.

I'm guessing that it is related to the next step for "bit".


class BitMask(int):
def AND(self, bm):
return BitMask(self & bm)
def OR(self, bm):
return BitMask(self | bm)
def XOR(self, bm):
return(self ^ bm)
def NOT(self):
return(~self)
def shiftleft(self, num):
return BitMask(self << num)
def shiftright(self, num):
return BitMask(self >> num)
def bit(self, num):
mask = 1 << num
return bool(self & mask)
def setbit(self, num):
mask = 1 << num
return BitMask(self | mask)
def zerobit (self, num):
mask = ~(1 << num)
return BitMask(self & mask)
def listbits(self, start=0, end=-1):
end = end if end < 0 else end+2
return [int(c) for c in bin(self) [start+2:end]]
Reply With Quote
  #2 (permalink)  
Old April 4th, 2017, 09: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

The error is saying that bm3 is an integer.
Did you carry out the steps immediately prior to the error:
Code:
...
>>> bm3 = bm1.shiftleft(3)
>>> bin(bm3)
'0b110000'     # did you get this result?
>>> bm1 == bm3.shiftright(3)
However, there is one other very strange feature of the error message:
Code:
File "<pyshell#76>", line 1, in <module>
bm1==bm3.shiftright(3)
AttributeError: 'int' object has no attribute 'shift right'
Notice the last line says there is no attribute 'shift right' - with a space...
But your code seems not to have a space? Are you sure you cut 'n paste
exactly what the interpreter said?


Hope that helps,

Alan G.
Reply With Quote
The Following User Says Thank You to Alan G For This Useful Post:
xynzee (April 5th, 2017)
  #3 (permalink)  
Old April 5th, 2017, 10:06 AM
Registered User
Points: 20, Level: 1
Points: 20, Level: 1 Points: 20, Level: 1 Points: 20, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2017
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Alan,

The code works fine except for the one command.
Got the expected result with the shiftleft.

The space in the error, was this text box trying to be "helpful" and added the space.

Traceback (most recent call last):
File "<pyshell#97>", line 1, in <module>
bm1 == bm3.shiftright(3)
AttributeError: 'int' object has no attribute 'shiftright'

Code:
class BitMask(int):
    def AND(self,bm):
        return BitMask(self & bm)
    def OR(self,bm):
        return BitMask(self | bm)
    def XOR(self,bm):
        return BitMask(self ^ bm)
    def NOT(self):
        return BitMask(~self)
    def shiftleft(self, num):
        return BitMask(self << num)
    def shiftright(self, num):
        return BitMask(self >> num)
    def bit(self, num):
        mask = 1 << num
        return bool(self & mask)
    def setbit(self, num):
        mask = 1 << num
        return BitMask(self | mask)
    def zerobit(self, num):
        mask = ~(1 << num)
        return BitMask(self & mask)
    def listbits(self, start=0,end=None):
        if end: end = end if end < 0 else end+2
        return [int(c) for c in bin(self)[start+2:end]]
I've tried it with both the code in the book and the supplied same error.

I'm using IDLE Python Shell 3.5.2 Tk version 8.5.9
and
Eclipse Platform
Version: Neon (4.6)
Build id: I20160606-1100

On Mac OS-X 10.9.5

If that helps.

Regards,
G*
Reply With Quote
  #4 (permalink)  
Old April 5th, 2017, 11:00 AM
Registered User
Points: 20, Level: 1
Points: 20, Level: 1 Points: 20, Level: 1 Points: 20, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2017
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

It appears it was the version of Python I was using.

I updated to 3.6.1 and it gave me the result.
Reply With Quote
  #5 (permalink)  
Old April 5th, 2017, 12:32 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 xynzee View Post
It appears it was the version of Python I was using.

I updated to 3.6.1 and it gave me the result.
That's really weird; the book was originally written using v3.3 and then retested with 3.4
when it became available, so Python 3.5 should not have made any difference.

Glad you got it working, but there is definitely something strange there.
Reply With Quote
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
Error Number: 1064 You have an error in your SQL syntax; check the manual that corre rousseauu BOOK: Professional CodeIgniter ISBN: 978-0-470-28245-8 0 March 30th, 2014 05:43 AM
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING ngchpg BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 July 16th, 2012 04:28 AM
SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified soufya BOOK: Beginning ASP.NET 4 : in C# and VB 7 September 14th, 2011 09:08 AM
HTTP error 500: Internal Server Error in Load Testing sherin Visual Studio 2008 0 May 19th, 2010 10:02 AM
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 12:17 PM



All times are GMT -4. The time now is 11:15 PM.


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