I keep getting an error line 14 needing to be unicode.
when ever I wrap the line iwith the atttrbute .decode('utf-8') i get the error that the function doesn't have that attribute.
I was working with asda earlier today and I got most of the errors fixed, this is the alst one i am having an issue getting past.
njs
Guest65574: I think your problem on line 14 is that you have unicode, and need to have bytes
Guest65574
So on line 14 when i try to write.
njs
Guest65574: generally data that's being transmitted over the network needs to be in bytes, so when you receive data you have to decode from bytes→unicode, and when you send data you first encode it from unicode→bytes
Guest65574: so try self.transport.write(self.getQuote().encode("utf-8")) # note "encode", not "decode"
itamarst has quit
Guest65574
File "C:/Users/Twisted/quoteserver.py", line 14, in dataReceived self.transport.write(self.getQuote().encode("utf-8")) builtins.AttributeError: 'bytes' object has no attribute 'encode'
clokep_tb has quit
@Guest65574
energizer joined the channel
this is the error i get when i try that.
njs
Guest65574: well, I'm confused. that's saying that self.getQuote() is returning a 'bytes' object. Which it definitely isn't in the code you pastebinned. On the other hand, if it *is* somehow returning a 'bytes' object, then self.transport.write(self.getQuote()) should have worked, and your complaint was that it didn't. So not sure what's going on :-)
Guest65574
hmmm okay.
idk if that is saying that it is returning a bytes object as mch as it might be saying it just doesn't have the ability to use teh function encode?
efphe has quit
clokep_tb joined the channel
EACFreddy
Guest65574: njs: I think it's because of the self.updateQuote(data) in dataRecieved()
njs
EACFreddy: ah! so without the call to .encode(), it crashes the first time that it reaches line 14. With the call to .encode(), the first time it reaches line 14 it works, then it calls updateQuote(data), which replaces the saved quote with a bytes object, and then the *next* time it reaches line 14 it crashes because *now* getQuote() is returning bytes instead of a str
EACFreddy
using a simple function like this can do the trick: ensure_bytes = lambda s: s if isinstance(s, bytes) else bytes(s, "utf-8")
njs
Guest65574: the error "builtins.AttributeError: 'bytes' object has no attribute 'encode'" is Python telling you exactly that: (a) you tried to do 'someobj.encode', (b) this failed because 'someobj' didn't have an 'encode' attribute, (c) also by the way, 'someobj' was a 'bytes' object (which makes sense, since bytes objects never have an encode method)
EACFreddy: that's a terrible habit though :-(
Guest65574
interesting.
okay let me see i understand this right.
so in quote server my data is in byte?
where do i put the cod e you wrote?
EaC freddy?
EACFreddy
if you want clean code, nowhere and instead make it a habit to only use bytes everywhere
Guest65574
How do i ensure I am using bytes?
EACFreddy
b"something" instead of "something"
Guest65574
where would i put the b? The weird thing is that the program is printing what I want it to print. so idk the point of the error.
njs
if you want clean code, make it a habit to decode text on input and encode it on output :-P
Guest65574
I FIXED IT Yay.
so I needed a "b" on line 30 in the quote factory class.
not in line 14.
can someone expalin to me why it was throwing out the error in such an odd place?
I'm writing a small oneshot client which shall print its output to stdout. However the client use twisted.logger for its output. This is fine for the server, since it captures stdout, but for the client this isn't how I want this. How can I relate to the logger in a client application?
Can I configure twister.logger.Logger() not to capture stdout, so print could work as-is?