code logs -> 2016 -> Fri, 23 Sep 2016< code.20160922.log - code.20160924.log >
--- Log opened Fri Sep 23 00:00:44 2016
00:27 catalyst [catalyst@Nightstar-bt5k4h.81.in-addr.arpa] has quit [[NS] Quit: Leaving]
00:27 catadroid` is now known as catadroid
02:39
<&ToxicFrog>
Hmm. There are people here who python.
02:39
<&ToxicFrog>
Is there a convenient way to turn a function that returns a different value each time it's called into a generator?
02:39
<&ToxicFrog>
Like, I have next_frame(fd), which returns the next ttyrec frame from the given fd
02:40
<&ToxicFrog>
(or None at eof)
02:40
<&ToxicFrog>
I want to iterate over all the frames
02:40
<&ToxicFrog>
In lua this is just: for frame in next_frame,fd do
02:40
<~Vornicus>
def iterize(f):
02:40
<&ToxicFrog>
In python it doesn't seem to be that simple
02:40
<&ToxicFrog>
I tried wrapping it in a while true yield, but that blows up at the end of iteration because it just yields the None
02:41
<~Vornicus>
result = f(); while result is not None: yield result; result = f()
02:44
<&ToxicFrog>
Thanks
02:45
<&ToxicFrog>
I think the sticking point is that I was expecting returning None to end the iteration
02:45
<&ToxicFrog>
And that returning nothing and returning None would be eqv
02:46
<&ToxicFrog>
And apparently neither of those are true
02:48
<~Vornicus>
returning nothing is equivalent to returning None in all situations I can think of
02:49
<~Vornicus>
to end iteration you need to raise StopIteration, technically, or reach the end of the generator function
02:49
<&ToxicFrog>
Wait
02:49
<&ToxicFrog>
So falling off the end of the function and returning nothing are not the same thing?
02:50
<~Vornicus>
okay okay so
02:50
<~Vornicus>
For regular functions, the following three are equivalent: def f(): blah blah blah; def f(): blah blah blah return; def f(): blah blah blah return None;
02:52
<~Vornicus>
For generators, you don't use *return* at all; yielding at all -- including yielding None -- waits there and continues where it left off next time you (or the loop, implicitly) calls generator.next()
02:52
<~Vornicus>
falling off the end is equivalent to raising StopIteration
02:53
<&ToxicFrog>
Aah.
02:55
<~Vornicus>
oh, you can use return. Prior to 3.3, it had to be empty, and was equivalent to raise StopIteration(); 3.3+ has return something equivalent to raiseStopIteration(something)
03:21 * ToxicFrog befuddles
03:21
<&ToxicFrog>
I have a bunch of Command subclasses implementing the various user commands. I want to split them into their own files.
03:22
<&ToxicFrog>
So I take RegisterCommand and shuffle it off into commands/RegisterCommand.py
03:22
<&ToxicFrog>
And then call 'import commands.RegisterCommand'
03:22
<&ToxicFrog>
And it dies with "TypeError: module.__init__() takes at most 2 arguments (3 given)"
03:22
<&ToxicFrog>
And then points to this line as the cause:
03:22
<&ToxicFrog>
File "/home/ben/devel/doomrl-server/commands/RegisterCommand.py", line 3, in <module>
03:22
<&ToxicFrog>
class RegisterCommand(Command):
03:23
<&ToxicFrog>
I think I need to read more about how python modules work, because it's clearly nothing like what I'm used to.
03:23
<&Derakon>
Does RegisterCommand.py know what Command is?
03:23
<&Derakon>
Specifically, is the Command symbol a class or a module?
03:24
<&Derakon>
(See http://stackoverflow.com/questions/14583761/typeerror-module-init-takes-at-most- 2-arguments-3-given )
03:27
<&ToxicFrog>
Oh, I think I see what's happening
03:27
<&ToxicFrog>
When I "import command.Command" to get the superclass, I actually get the module containing the superclass; I want command.Command.Command
03:27
<&ToxicFrog>
There has got to be a better way to do this
03:27
<&Derakon>
Yes.
03:28
<&Derakon>
You could add a __init__.py in the command directory that imports the Command class.
03:28
<&Derakon>
Then you could import command.Command and get the class.
03:28
<&Derakon>
You should probably rename Command.py if you do that, though.
03:28
<&Derakon>
Any symbols declared in an __init__.py...or is it __module__.py? I forget...
03:28
<&ToxicFrog>
__init__.py, I think
03:29
<&Derakon>
Anyway, any symbols declared or imported in that specially-named file will be available when you import the package.
03:29
<&Derakon>
Many libraries have a root-level __init__.py that imports a whole bunch of crap, so you can e.g. do "import numpy" and get access to numpy.zeros, numpy.float32, etc.
03:30 * ToxicFrog nods
03:32
<&Derakon>
More generally, any module that imports X can also access anything that X imports, which is part of why "import *" is such a bad idea. I once had to trace a function across five modules, none of which imported it explicitly by name and one of which renamed it for added fun.
03:32
<&Derakon>
Like, I was searching for "func2", and one of the modules imported * from some other module, and then had a line somewhere in there saying "func2 = func".
03:32
<&ToxicFrog>
Lovely.
03:33
<&Derakon>
And that was the only reference to func in the entire module!
03:33
<&ToxicFrog>
In this case, I just need the classes to be defined; they have a metaclass that registers them in the global command table.
03:33
<&Derakon>
Ahh, reflection.
03:33
<&ToxicFrog>
Then I just need to be able to import the command table so main() can find them.
03:48 catadroid` [catadroid@Nightstar-fk3dgo.dab.02.net] has joined #code
03:50 catadroid [catadroid@Nightstar-eopd2k.dab.02.net] has quit [Ping timeout: 121 seconds]
05:29 Derakon is now known as Derakon[AFK]
07:02 Kindamoody[zZz] is now known as Kindamoody
07:34 catadroid` is now known as catadroid
07:58 catadroid [catadroid@Nightstar-fk3dgo.dab.02.net] has quit [[NS] Quit: Bye]
08:10 Vornicus [Vorn@ServerAdministrator.Nightstar.Net] has quit [Operation timed out]
08:48 Pink [user1@Nightstar-g7hdo5.dyn.optonline.net] has quit [Ping timeout: 121 seconds]
08:48 Pink [user1@Nightstar-g7hdo5.dyn.optonline.net] has joined #code
08:56 celticminstrel [celticminst@Nightstar-f4fn3d.dsl.bell.ca] has quit [[NS] Quit: And lo! The computer falls into a deep sleep, to awake again some other day!]
09:44 You're now known as TheWatcher[lab]
10:27
<@gnolam>
... phew
10:27
<@gnolam>
Thought I would have to roll back everything I did the last couple of days there.
10:30
<@gnolam>
Crisis averted. Some of the old cargo cult code got cleaned up in the process.
10:47
<@abudhabi>
Cargo cult code?
10:48
<@abudhabi>
Did it try to imitate something that worked, but failed to understand how and why it worked, and therefore didn't?
11:27 gizmore [kvirc@Nightstar-ofjk5v.dip0.t-ipconnect.de] has quit [Operation timed out]
11:28 gizmore [kvirc@Nightstar-k7s4tb.dip0.t-ipconnect.de] has joined #code
11:45 Vornicus [Vorn@ServerAdministrator.Nightstar.Net] has joined #code
11:45 mode/#code [+qo Vornicus Vornicus] by ChanServ
12:01 You're now known as TheWatcher
12:19 You're now known as TheWatcher[afk]
14:47 You're now known as TheWatcher
15:03 catadroid [catadroid@Nightstar-ri2h4v.dab.02.net] has joined #code
16:05 Vornicus [Vorn@ServerAdministrator.Nightstar.Net] has quit [Ping timeout: 121 seconds]
16:48 gizmore [kvirc@Nightstar-k7s4tb.dip0.t-ipconnect.de] has quit [Ping timeout: 121 seconds]
16:48 catadroid` [catadroid@Nightstar-qj54qa.dab.02.net] has joined #code
16:51 catadroid [catadroid@Nightstar-ri2h4v.dab.02.net] has quit [Ping timeout: 121 seconds]
17:51 catadroid` is now known as catadroid
18:15 Derakon[AFK] is now known as Derakon
18:27 celticminstrel [celticminst@Nightstar-f4fn3d.dsl.bell.ca] has joined #code
18:27 mode/#code [+o celticminstrel] by ChanServ
18:41 catadroid [catadroid@Nightstar-qj54qa.dab.02.net] has quit [Ping timeout: 121 seconds]
19:35
<&McMartin>
abudhabi: That is indeed exactly the image intended by that phrase
19:35
<&McMartin>
Except unlike the old cults, sometimes it actually works, because the Words turn out to have power after all
19:35
<&McMartin>
When something about the environment changes, though, everything breaks and nobody knows how to fix it~
21:03 catalyst [catalyst@Nightstar-bt5k4h.81.in-addr.arpa] has joined #code
21:49
<&ToxicFrog>
Hmm.
21:50
<&ToxicFrog>
I definitely dislike cone less than any other mailreader I've tried so far
21:50
<&ToxicFrog>
But it's missing some kind of important features
21:50 Emmy [Emmy@Nightstar-9p7hb1.direct-adsl.nl] has quit [Ping timeout: 121 seconds]
21:50
<&ToxicFrog>
Like "reply to destination address", "put reply in same folder", and a search command that actually lets you look at the search results
22:07
<@celticminstrel>
Hmm... it is guaranteed that long can hold an unsigned short, right?
22:08
< catalyst>
yes
22:09
<&McMartin>
... is that "signed long", "unsigned short"?
22:09
<@celticminstrel>
Yes.
22:10
<&McMartin>
I know short <= int <= long, but I'm not sure if short < long.
22:10
<&ToxicFrog>
McMartin: I think it's guaranteed that the signed and unsigned versions fit in the same space
22:10
<@celticminstrel>
Maybe I should use int64_t just to be safe. (Plus it'll be promoted to that regardless by the library.)
22:11
<&ToxicFrog>
It's definitely the case on ia32 and amd64 machines.
22:12
<&McMartin>
ToxicFrog: What I mean is, if sizeof(short) == sizeof(long), than an unsigned short may lose its top bit once it is interpreted as a sign
22:12
<&ToxicFrog>
Hmm.
22:12
<&McMartin>
But then, I interpreted the question as "can it hold it without distortion"
22:12
<@celticminstrel>
Yes.
22:12
<@celticminstrel>
The value must not change.
22:13
<@celticminstrel>
(Admittedly the actual numbers stored are unlikely to approach the limits, but still.)
22:13 catalyst [catalyst@Nightstar-bt5k4h.81.in-addr.arpa] has left #code ["Leaving"]
22:13
<&ToxicFrog>
Hmm. I think it just gets reinterpreted, not truncated, but I'm rusty on the minutiae of C casting
22:14
<&McMartin>
Yeah, I'm digging on this
22:14
<&McMartin>
catalyst's word is usually good enough and digging into it more...
22:14
<&McMartin>
... long is guaranteed to be at least 32 bits
22:14
<&McMartin>
short is guaranteed to be at least 16 bits
22:14
<&McMartin>
It is technically compliant for short and long to both tbe 32 bits
22:15
<&McMartin>
In which case this is not the case
22:15
<&McMartin>
But if you have a short you should be sticking to the 16-bit range
22:15
<&McMartin>
That *will* fit in a long, guaranteed
22:15
<&McMartin>
If exact bit widths matter, it is The Year Of Our Lord 2016 and stdint.h should exist everywhere
22:15
<&McMartin>
And you can straight-up use uint16_t and sint32_t.
22:15
<&McMartin>
Er, just int32_t for the latter
22:16
<@celticminstrel>
I think I only used a short because I don't expect to store abnormally large numbers in it.
22:16
<&McMartin>
In all seriousness, use int, not short, that's the one that's probably register width and thus the fastest to operate on
22:16
<@celticminstrel>
And I need to cast because the library I'm using chokes on unsigned integers (though I suppose I could patch the library instead).
22:23
<@celticminstrel>
Next question: How safe is it to cast between int64_t and uint64_t?
22:24
<&McMartin>
Going from one to the other and back is lossless
22:24
<&McMartin>
You'll get garbage in the "other" if the int64_t was negative or if the uint64_t value is larger than the maximum positive representative number
22:24
<@celticminstrel>
That seems good enough for me.
22:25
<&McMartin>
But if it's in the part of their range where they overlap (0 through 2^63-1, basically), then the value is the same on both sides
22:25
<&McMartin>
If your architecture is 2's Complement (spoiler alert: it is) then you can even do math with the "garbage" values and the overflow will make it work out.
22:28
<@celticminstrel>
Okay, so given this, I think I'm just going to patch the library.
22:30
<@celticminstrel>
...why is this library separately checking for long and int32_t?
22:30
<@celticminstrel>
And also short separately.
--- Log closed Sat Sep 24 00:01:00 2016
code logs -> 2016 -> Fri, 23 Sep 2016< code.20160922.log - code.20160924.log >

[ Latest log file ]