Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
SurgicalOntologist
Jun 17, 2004

rjmccall posted:

Most of what you're saying makes sense, but this specifically is really weird. You're being blasé about, basically, introducing an arbitrary and unnecessary source of data corruption to your clients, who may or may not care about the sensors having nice, small identifying numbers but definitely care a lot about reliably identifying a single stream of data. If the renumbering thing is really important for some reason you haven't explained, then yes, you almost certainly should just delay/suppress sending any data to the client for the first cycles until you've figured out how many sensors there are.

You're right, I was thinking of interacting with on-screen objects where a 5 ms blip will be imperceptible (actually given that that the screen's refresh rate is a much slower 60 Hz I'm not sure what will happen), or doing timeseries analysis on movement data in which case you'd be discarding the first few seconds of data anyway. But those are assumptions I probably shouldn't be making, plus there may be other applications I'm not anticipating.

(The renumbering thing is definitely important. My lab does human subjects research, if we were doing a development study with, say, a child interacting with an adult, we don't want to have to say "oh, looks like you're controlling the wrong avatars, can you switch sensors?" Instead we just give the lower-numbered sensor, of whatever two we happened to grab, to the adult)

Adbot
ADBOT LOVES YOU

nielsm
Jun 1, 2009



Instead of trying to insert the sensor data into an input data map of some sort directly, I would suggest instead inserting them into a list of (sensor-id, data-value) tuples in the received order, and then after one cycle process that list at once. (Putting tuples into a list should also have slightly more predictable performance than updating a dict.)

tef
May 30, 2004

-> some l-system crap ->

seiken posted:

vvv that's ridiculous, naming has nothing to do with it. Control flow exceptions are poo poo no matter what the language is or what they're called

I love hearing this argument because I imagine a programmer going LA LA LA I CAN’T HEAR YOU when they discover that exception handling is a form of control flow.

qntm
Jun 17, 2009
I like the argument that exceptions should only be used in exceptional circumstances "because that's what the name says".

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

qntm posted:

I like the argument that exceptions should only be used in exceptional circumstances "because that's what the name says".

You should use goto instead of functions for your code. This makes your code more faster.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
Which is why exceptions, a sort of non-local goto, are such an unalloyed good. We've come full circle.

..btt
Mar 26, 2008

qntm posted:

I like the argument that exceptions should only be used in exceptional circumstances "because that's what the name says".

This is exactly why they are called exceptions. You think they were named that because they're supposed to be used in non-exceptional circumstances?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
How is StopIteration an exceptional circumstance?

..btt
Mar 26, 2008
I did mention I wasn't a python programmer. Nor did I design the language. I'm not going to defend their choice of exception implementation since it makes no sense to me :)

omeg
Sep 3, 2012

Suspicious Dish posted:

How is StopIteration an exceptional circumstance?

It's an exceptionally poo poo design. :v:

Polio Vax Scene
Apr 5, 2009



Exception exception exception, that word has no meaning to me now...

Whenever someone asks if they should learn Python, just say it's an exceptional language.

tef
May 30, 2004

-> some l-system crap ->

..btt posted:

This is exactly why they are called exceptions. You think they were named that because they're supposed to be used in non-exceptional circumstances?

Perhaps they mean such an exceptional circumstance as, not following the normal return path, or expected return path. c.f the semi-predicate problem

Maybe exceptions just aren't goodenough

..btt
Mar 26, 2008
It's a nice idea, but not the reason. Have a read up on the history of exception handling (it starts with hardware).

Of course this doesn't mean it's what the Python devs mean by "exception", but the concept predates Python by probably 50 years or so.

E: oh, now I get it. Ignore me :)

..btt fucked around with this message at 17:36 on Jan 17, 2014

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
It turns out all the possible solutions to the semipredicate problem are bad in their own ways, especially when the error is recoverable or generally non-fatal :(

tef
May 30, 2004

-> some l-system crap ->

..btt posted:

It's a nice idea, but not the reason. Have a read up on the history of exception handling (it starts with hardware).

(I have and no-one else gets my crummy jokes about it)

quote:

(a) to permit dealing with an operation's impending or actual failure. Two types of failure are of interest: range failure, and domain failure;
(b) to indicate the significance of a valid result or the circumstances under which it was obtained.
(c) to permit an invoker to monitor an operation, e.g. to measure computational progress or to provide additional information and guidance should certain conditions arise.

from goodenough's paper in '75

fritz
Jul 26, 2003

Suspicious Dish posted:

How is StopIteration an exceptional circumstance?

If you have an iterable that's a million elements long, finding yourself at the end is a one-in-a-million event, which was agreed upthread to be pretty exceptional.

evensevenone
May 12, 2001
Glass is a solid.
clearly, the classic C design pattern is better: Functions return whether or not they worked. Then if it didn't work, you check a global to see what was wrong. If your function actually needs to return data, you provide a pointer to where it should go as a parameter, and hope that you've understood the documentation and allocated enough memory for the results.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Guido van Rossum's coroutine library for AppEngine uses exceptions as the normal way to return values (due to limitations in Python 2's generators). He clearly does not feel that the name of a thing should dictate how it can be used.

Python code:
@ndb.tasklet
def callback(msg):
  acct = yield msg.author.get_async()
  raise ndb.Return('On %s, %s wrote:\n%s' % (msg.date, acct.nick(), msg.content))

qry = Messages.query().order(-Message.when)
outputs = qry.map(callback, limit=20)
for output in outputs:
  print output

JawnV6
Jul 4, 2004

So hot ...

SurgicalOntologist posted:

(The renumbering thing is definitely important. My lab does human subjects research, if we were doing a development study with, say, a child interacting with an adult, we don't want to have to say "oh, looks like you're controlling the wrong avatars, can you switch sensors?" Instead we just give the lower-numbered sensor, of whatever two we happened to grab, to the adult)

The bit I'm not following is how you can simultaneously know you're at the start of a session and NOT know that you might need to change sensor mappings. Seems like you should have a bit at the beginning that maps out the first sensors seen and doesn't need to be handled every time you're checking the mapping?

Zombywuf
Mar 29, 2008

tef posted:

I love hearing this argument because I imagine a programmer going LA LA LA I CAN’T HEAR YOU when they discover that exception handling is a form of control flow.

Only if by control flow you mean the (╯° °)╯︵ ┻━┻ operator.

Alien Arcana
Feb 14, 2012

You're related to soup, Admiral.
I just spent an hour trying to figure out why my fractal was coming out all wrong, and well...

code:
map[L, M] = GenerateValue(rng, damping, (map[U, L] + map[D, L]) / 2);
map[R, M] = GenerateValue(rng, damping, (map[U, R] + map[D, R]) / 2);
map[C, U] = GenerateValue(rng, damping, (map[U, L] + map[U, R]) / 2);
map[C, D] = GenerateValue(rng, damping, (map[D, L] + map[D, R]) / 2);
map[C, M] = GenerateValue(rng, damping, (map[U, L] + map[U, R] + map[D, L] + map[D, R]) / 4);
Let's play "Spot The Dumb Mistake."

:ssh:Look at the order of the indices.

For bonus points, explain what went wrong in my brain there.

SurgicalOntologist
Jun 17, 2004

JawnV6 posted:

The bit I'm not following is how you can simultaneously know you're at the start of a session and NOT know that you might need to change sensor mappings. Seems like you should have a bit at the beginning that maps out the first sensors seen and doesn't need to be handled every time you're checking the mapping?

Yeah you're right. The issue I originally had with that is how to know when that initial mapping period is over. I don't trust the data coming in to be right on time, or to perfectly cycle between the sensors. But I just figured out a way to ask the device how many sensor's to expect (there's a function in the device's C API that was never mapped into the Python API) so this is now a moot point and I will explicitly wait until I get the expected number of sensors.

seiken
Feb 7, 2005

hah ha ha

tef posted:

I love hearing this argument because I imagine a programmer going LA LA LA I CAN’T HEAR YOU when they discover that exception handling is a form of control flow.

what? Of course they're a form of control flow. They're a really bad form of control flow that you shouldn't use unless you run out of memory or can't open a file, that's the whole point

QuarkJets
Sep 8, 2008

Suspicious Dish posted:

How is StopIteration an exceptional circumstance?

New iterator control mechanisms were developed because making users use an exception to control iteration is stupid. The use of StopIteration is exceptional because no one really uses it anymore

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
What newer control mechanisms were that?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

tef posted:

quote:

(a) to permit dealing with an operation's impending or actual failure. Two types of failure are of interest: range failure, and domain failure;
(b) to indicate the significance of a valid result or the circumstances under which it was obtained.
(c) to permit an invoker to monitor an operation, e.g. to measure computational progress or to provide additional information and guidance should certain conditions arise.

But two and a half of these points relate to resumable exceptions, which hardly any languages have ever implemented (Smalltalk! and also amusingly supported by libUnwind), and which very few people would consider superior to just passing down a callback.

QuarkJets
Sep 8, 2008

Suspicious Dish posted:

What newer control mechanisms were that?

Python code:
for item in my_iterator:
  code
While iterating, the StopIteration exception catching is done for you by the for loop. You can still write a loop that calls next() on an iterator and catches StopIteration, but no one really does because it's unnecessary

Dren
Jan 5, 2001

Pillbug
Python code:
for item in my_iterator:
Catches StopIteration. It's hidden from the user but it still happens.

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

QuarkJets posted:

Python code:
for item in my_iterator:
  code
While iterating, the StopIteration exception catching is done for you by the for loop. You can still write a loop that calls next() on an iterator and catches StopIteration, but no one really does because it's unnecessary

:what:

StopIteration is the newer iteration protocol, as defined in PEP 234 and implemented in Python 2.2. Before that, iterating over sequences with e.g. for item in seq was defined in terms of repeated calls to seq.__getitem__: seq[0], seq[1], and so on. This is obviously terrible if the sequence doesn't provide you efficient random access, so for instance it's O(n2) for a linked list like a collections.deque.

The for loop predates StopIteration, not the other way around.

No Safe Word
Feb 26, 2005

I mean if you just look at the built-in Exception hierarchy of Python it's clear that StopIteration is just weird and should have triggered some code smell alarms:

pre:
BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StandardError
      |    +-- BufferError
      |    +-- ArithmeticError
      |    |    +-- FloatingPointError
      |    |    +-- OverflowError
      |    |    +-- ZeroDivisionError
      |    +-- AssertionError
      |    +-- ... others ...
      .
      .
      .
      |
      +-- Warning
           +-- DeprecationWarning
           +-- PendingDeprecationWarning
           +-- ... others ...
And GeneratorExit is weird too, it would seem.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
It's neat to see that ES6, which originally followed a model similar to Python, discovered a simple solution:

JavaScript code:
// Old specification
function myOldIterator() {
    var number = 0;
    return function() {
        if (number > 10)
            throw new StopIteration();

        return ++number;
    };
}

// New specification
function myNewIterator() {
    var number = 0;
    return function() {
        if (number > 10)
            return { done: true };

        return { done: false, value: ++number };
    };
}
Yeah, yeah, it isn't as good as option types, I know. Shut up.

Zombywuf
Mar 29, 2008

The C++ in me is rolling it's eyes so hard it hurts right now.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Suspicious Dish: You can do the same basic idea pretty cleanly in a concatenative language by using a variable stack effect. Something like this will return a curried function (generator), and then every time you evaluate it you'll get either a "true" left on the stack with the value underneath or a "false" on the stack with no value.

code:
: count-to-10 ( -- generator )
	{ dup inc @ dup 11 > if drop false else true then } 0 with
;

Tesseraction
Apr 5, 2009

Suspicious Dish posted:

How is StopIteration an exceptional circumstance?

I guess the principle is that in large arrays it's the most optimal way of iterating (i.e. keep going until something goes horribly wrong) and in smaller arrays the overhead is negligible compared to iterating over an array in the first place.

Basically the idea that

time = (x * y) + z

where x is the length of the array, y is the time to access an element, and z is the exception handler evaluation time.

For small values of x, z is massive, but the handling time is still small on modern processors. For larger values of x, z gets comparatively minute compared to checking the range of x versus the current position.

I guess it's an optimisation of sorts?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
It doesn't seem all that optimal - you're still checking every iteration whether the end has been reached yet, only now the library throws an exception once that happens which gets propagated up. I guess it's marginally better than checking twice whether you've reached the end (once inside the data structure and once outside), assuming that one of those checks can't be elided at any point.

It seems like the "optimal" mechanism would be the functional-style "pass a callback which gets called on every element", where the end of the iteration implicitly happens when the function returns to the caller.

HORATIO HORNBLOWER
Sep 21, 2002

no ambition,
no talent,
no chance
Exceptions are great, quit being afraid of them and quit pretending anyone gives half a poo poo that they're "expensive." This isn't 1995 and unless you're doing real-time work it literally does not matter at all.

Doctor w-rw-rw-
Jun 24, 2008

HORATIO HORNBLOWER posted:

Exceptions are great, quit being afraid of them and quit pretending anyone gives half a poo poo that they're "expensive." This isn't 1995 and unless you're doing real-time work it literally does not matter at all.

Except they're also poo poo for debugging. Audio on iOS is done using C++ exceptions for flow control, which can severely hamper debugging legitimate or rare-firing C++ exceptions, since the debugger will halt on playing sound.

Exceptions create basically an alternate stack unwinding system and if you aren't doing garbage collection can also gently caress up memory management unless you know what you're doing, which is certain to never *always* be the case.

Exceptions as flow control is sufficiently wrong to warrant careful use, not wanton sprinkling around a codebase.

That Turkey Story
Mar 30, 2003

Suspicious Dish posted:

How is StopIteration an exceptional circumstance?

I really, really, really, hate this. I don't know why they use exceptions for this kind of stuff. Exceptions make sense for functions that can't reach their post-conditions and not much else. It's embarassing that so many languages and libraries use exceptions for basic control flow.

evensevenone
May 12, 2001
Glass is a solid.
If the post-condition for iterable.next() is that the iterator is moved and the next item is returned, and then you call it when there is no next item, an exception seems kind of appropriate.

Adbot
ADBOT LOVES YOU

Jewel
May 2, 2009

evensevenone posted:

If the post-condition for iterable.next() is that the iterator is moved and the next item is returned, and then you call it when there is no next item, an exception seems kind of appropriate.

So you're saying the item after the current item is nothing? So why not return None?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply