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.
 
  • Locked thread
cinci zoo sniper
Mar 15, 2013




ty all for answers, was just worried, out of a sudden, that im doing something outright inferior with my strict checks for iterable types and nones

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
dunno about python specifically, but in a lot of languages/runtimes the exception-based version is slightly faster in the normal case (and pays for it by being horrendously slower if the exception is actually thrown).

Soricidus
Oct 21, 2010
freedom-hating statist shill

cinci zoo sniper posted:

code:
try:
    bar = foo["x"]
except TypeError:
    print("baz")
this is wrong because it will catch any TypeError thrown by foo's implementation of __getitem__, which may be caused by something other than foo being the wrong type.

cinci zoo sniper posted:

code:
if isinstance(foo, dict):
    bar = foo["x"]
else:
    print("baz")
this is wrong because foo might be a dictionary-like object that does not inherit from dict. i mean there's nothing wrong with nominative typing but if you're going to use python then people will expect your code to use structural typing.

both of these are situations i have encountered in real life, leading to bugs that were non-trivial to fix. have fun!

other wrong solutions include hasattr tests (which guarantee nothing about the behavior of the method).

(the correct solution is just to write whatever and pray really hard that it works, because if you cared about correctness you wouldn't be using python.)

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

c tp s: one of the products i work on just went open source this week so some of my bad code, including literally the first lines of shipped code i ever wrote in my career, is now open to the public

luckily it came from a different source control so someone else's name is on it :toot:

cinci zoo sniper
Mar 15, 2013




Soricidus posted:

this is wrong because it will catch any TypeError thrown by foo's implementation of __getitem__, which may be caused by something other than foo being the wrong type.

this is wrong because foo might be a dictionary-like object that does not inherit from dict. i mean there's nothing wrong with nominative typing but if you're going to use python then people will expect your code to use structural typing.

both of these are situations i have encountered in real life, leading to bugs that were non-trivial to fix. have fun!

other wrong solutions include hasattr tests (which guarantee nothing about the behavior of the method).

(the correct solution is just to write whatever and pray really hard that it works, because if you cared about correctness you wouldn't be using python.)

well i understand that it's abstract example, i was just drawing examples from the most recent thing i wrote. no non-standard types or whatever fancy poo poo, just good old mangled as hell json that was converted form xml disregarding the schema so all i really need to know if if an x thing is dict or not, and thus in generaly i have combinations such as

code:
if food is not none:
    if isinstance(foo, dict):
        ...
    elif isinstance(foo, l;ist) and len(foo) > 0:
        ...
else:
    print("baz")
e.g. to paraphrase the question before so that there are no 50 shades of overthiking, are there practical advantages for using try-except over if-isinstance when im working with known in advance python base types

Sapozhnik
Jan 2, 2005

Nap Ghost
trying to second-guess the type of the object like that is a code smell

if you have two different kinds of thing passing through your function that both quack in the same way then you shouldn't have to care about whether you're dealing with a duck or a goose.

if you do have two fundamentally different things passing through your function where the difference between them matters then why are they both passing through that function? your abstraction is broken, fix it.

Powerful Two-Hander
Mar 10, 2004

Mods please change my name to "Tooter Skeleton" TIA.


Sapozhnik posted:

trying to second-guess the type of the object like that is a code smell

if you have two different kinds of thing passing through your function that both quack in the same way then you shouldn't have to care about whether you're dealing with a duck or a goose.


honked again

Luigi Thirty
Apr 30, 2006

Emergency confection port.

iirc try/catch in python is a lot slower than if/then/else and should only be used when you expect to actually throw an exception

i could be confusing it with C though

tef
May 30, 2004

-> some l-system crap ->

cinci zoo sniper posted:

even in cases like these below?
code:
try:
    bar = foo["x"]
except TypeError:
    print("baz")
code:
if isinstance(foo, dict):
    bar = foo["x"]
else:
    print("baz")

bar = foo.get('x', 'baz')

cinci zoo sniper
Mar 15, 2013




Sapozhnik posted:

trying to second-guess the type of the object like that is a code smell

if you have two different kinds of thing passing through your function that both quack in the same way then you shouldn't have to care about whether you're dealing with a duck or a goose.

if you do have two fundamentally different things passing through your function where the difference between them matters then why are they both passing through that function? your abstraction is broken, fix it.

"good old mangled as hell json that was converted form xml disregarding the schema so all i really need to know if if an x thing is dict or not, and thus in generaly i have combinations such as"

it's not like i've been moaning about this in the thread for 3 weeks straight now that i unfortunately do have to loving second guess our troglodyte web developers fulfilling dba/etl duties, for obvious reasons

this is absolutely out of my control besides e-mail the responsible dev who is a lazy, sorry sack of poo poo

cinci zoo sniper
Mar 15, 2013




tef posted:

bar = foo.get('x', 'baz')

code:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'get'

cinci zoo sniper
Mar 15, 2013




sorry i become enraged the instant i remember that casually dropped mention "oh yeah we convert xml to json so it can be stored in database, fam"

tef
May 30, 2004

-> some l-system crap ->
if you're doing list['x'] what

cinci zoo sniper
Mar 15, 2013




tef posted:

if you're doing list['x'] what

typeerror, cant slice list with strings because python lists don't have named members

cinci zoo sniper fucked around with this message at 18:59 on Sep 23, 2017

Arcteryx Anarchist
Sep 15, 2007

Fun Shoe
there's probably enough structure that you can define a type hierarchy that would make things less error prone

cinci zoo sniper
Mar 15, 2013




lancemantis posted:

there's probably enough structure that you can define a type hierarchy that would make things less error prone

im not sure what you mean by defining type hierarchy. in general, i have a fairly good idea about possible abnormalities in the dataset, which is why i posted that isintance(x, dict) example earlier, since in this garbage i am forced to work with:

1) a value is always a string
2) a missing value is a 0-length string
3) a variable may be randomly placed in a dictionary containing just it (cause by some json (or mongo?) type thing (?extended types? i think?)
4) a collection of variables (json array?) is a dictionary
5) if there is just a single matching collection, there is a dictionary
6) if multiple collections match the record, they are contained in a list
7) if there are no collections matching the record, there is an empty list
8) a collection may randomly be missing one or more variables/sub-collections in it
9) a collection "tree stub" (?node?) may be missing as such

and so i have bunch of static methods covering each problematic variable where in each i have hardcoded substructure path housed inside at most 3 levels deep nested if condition tree to account for the mix of the factors above specific to data subset im extracting

hence the earlier question, if doing if isinstance, if foo in bar, etc - given known variability of the situation and having no way to affect it - is not inferior in some way to try-cath patterns or equivalent

now if you want actually embarassing stuff, let me tell you how i zip bunch of lists, sort by sub value, and then unzip them before passing further since i need homgeneous lists as final outputs :v:

cinci zoo sniper fucked around with this message at 20:01 on Sep 23, 2017

hackbunny
Jul 22, 2007

I haven't been on SA for years but the person who gave me my previous av as a joke felt guilty for doing so and decided to get me a non-shitty av

Luigi Thirty posted:

iirc try/catch in python is a lot slower than if/then/else and should only be used when you expect to actually throw an exception

i could be confusing it with C though

C has no exceptions :eng101: C++ does and try/catch has zero execution time overhead for normal execution in most ABIs

Luigi Thirty
Apr 30, 2006

Emergency confection port.

okay i feel dumb thanks to geometry and i'm not even sure if I can explain my problem. the jaguar blitter draws a series of Y horizontal lines with pixel length X.

After each pixel is blitted, a 16.16 number held in the increment register is added to the draw position. this is the inner draw loop.
After each line is blitted, a 16.16 number held in the step register is added to the draw position. this is the outer draw loop.

To draw a line, I need to specify a starting coordinate, an increment, a step, and a length.

example: Draw a line from (32,32) to (64,64). The slope is 32/32 = 1. The increment is (1,0) and the step is (0,1). The line length is 32px.

what about different slopes though?

Draw a line from (0,0) to (5,60).

ooooooh okay nevermind I think I got it. I need to apply the logic that applies to regular bresenham drawing here where step depends on the octant we are drawing in?



increment by (1/15,0), step by (0,1), length of 60? that sounds right. so if the slope is over 1, increment X by its inverse and step Y by 1 or -1 depending on positive/negative slope?

i hate geometry

i will leave this here as a monument to thought and go play rimworld instead for a while

Luigi Thirty fucked around with this message at 23:38 on Sep 23, 2017

Hunter2 Thompson
Feb 3, 2005

Ramrod XTreme

hackbunny posted:

C has no exceptions :eng101: C++ does and try/catch has zero execution time overhead for normal execution in most ABIs

Correct, C has no exceptions. However, you can use setjmp and longjmp to roll your own exceptions in C... but like everything C, you've gotta remember to handle all the details of deallocating resources yourself

I've never done it myself but some older coworkers did it in the 80s

Sapozhnik
Jan 2, 2005

Nap Ghost

meatpotato posted:

Correct, C has no exceptions. However, you can use setjmp and longjmp to roll your own exceptions in C... but like everything C, you've gotta remember to handle all the details of deallocating resources yourself

I've never done it myself but some older coworkers did it in the 80s

https://www.youtube.com/watch?v=dOV5WXISM24

HoboMan
Nov 4, 2010

FamDav posted:

I guess I did make some assumptions, like maybe you never tear the old environment down and you just sit on double capacity. what part was confusing?

the parts about vms and monitoring and scaling. i was just gonna swap staging into prod and vice-versa

HoboMan
Nov 4, 2010

like actually swap the boxes instead of running a bunch of scripts in a particular order to properly clone it

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

goddamn if someone could replace that ferret with the goland gopher it would be :perfect:

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
[quote="“meatpotato”" post="“476696241”"]
Correct, C has no exceptions. However, you can use setjmp and longjmp to roll your own exceptions in C... but like everything C, you’ve gotta remember to handle all the details of deallocating resources yourself

I’ve never done it myself but some older coworkers did it in the 80s
[/quote]

note that setjmp/longjmp is like the epitome of what people talking about when they say non-zero-cost exception ABIs, though

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

rjmccall posted:

note that setjmp/longjmp is like the epitome of what people talking about when they say non-zero-cost exception ABIs, though

hello, NS_DURING, my old friend

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?
something something volatile again

redleader
Aug 18, 2005

Engage according to operational parameters

cinci zoo sniper posted:

im not sure what you mean by defining type hierarchy. in general, i have a fairly good idea about possible abnormalities in the dataset, which is why i posted that isintance(x, dict) example earlier, since in this garbage i am forced to work with:

1) a value is always a string
2) a missing value is a 0-length string
3) a variable may be randomly placed in a dictionary containing just it (cause by some json (or mongo?) type thing (?extended types? i think?)
4) a collection of variables (json array?) is a dictionary
5) if there is just a single matching collection, there is a dictionary
6) if multiple collections match the record, they are contained in a list
7) if there are no collections matching the record, there is an empty list
8) a collection may randomly be missing one or more variables/sub-collections in it
9) a collection "tree stub" (?node?) may be missing as such

and so i have bunch of static methods covering each problematic variable where in each i have hardcoded substructure path housed inside at most 3 levels deep nested if condition tree to account for the mix of the factors above specific to data subset im extracting

hence the earlier question, if doing if isinstance, if foo in bar, etc - given known variability of the situation and having no way to affect it - is not inferior in some way to try-cath patterns or equivalent

now if you want actually embarassing stuff, let me tell you how i zip bunch of lists, sort by sub value, and then unzip them before passing further since i need homgeneous lists as final outputs :v:

you should probably find another job

cinci zoo sniper
Mar 15, 2013




redleader posted:

you should probably find another job

nah, its a consultancy-style position with sweet pay. ill just escalate past the dev responsible for this garbage if he won't pull head out of his arse within a month, since then i will need the data myself, and in a considerably greater breadth than what i fiddle with now

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

eschaton posted:

something something volatile again

yeah, the volatile thing is also something that people just completely forget about when they're using sj/lj

also sj/lj are just terrible for code efficiency because compiler writers have basically gone "well we could invest some serious amount of time into making this work better orrrr we could just disable this optimization completely when we see a setjmp and go work on something that matters to code that doesn't suck" in a thousand different places

LinYutang
Oct 12, 2016

NEOLIBERAL SHITPOSTER

:siren:
VOTE BLUE NO MATTER WHO!!!
:siren:
Opengl is truly hell

A programming language that can only be debugged by literally throwing poo poo at the screen

hackbunny
Jul 22, 2007

I haven't been on SA for years but the person who gave me my previous av as a joke felt guilty for doing so and decided to get me a non-shitty av

meatpotato posted:

Correct, C has no exceptions. However, you can use setjmp and longjmp to roll your own exceptions in C... but like everything C, you've gotta remember to handle all the details of deallocating resources yourself

I've never done it myself but some older coworkers did it in the 80s

setjmp/longjmp has higher overhead than c++ exceptions but lower than error code checking. on the other hand, C has no standard stack unwinding so you'll leak resources and invalid states like crazy if you aren't extremely careful

I have used setjmp/longjmp to roll my own exceptions, both academically and in a real world project. it's no worse than the rest of the C language

Deep Dish Fuckfest
Sep 6, 2006

Advanced
Computer Touching


Toilet Rascal

LinYutang posted:

Opengl is truly hell

A programming language that can only be debugged by literally throwing poo poo at the screen

i'm incredibly thankful every day of my existence for visual studio's graphics debugger. it's not the clean, line by line debugging you get when stepping through cpu code because gpu compilers don't really seem to have a "no optimizations" setting, but it's miles ahead of anything else

that's probably directx only though so lol ur hosed

Powerful Two-Hander
Mar 10, 2004

Mods please change my name to "Tooter Skeleton" TIA.


ok I've got a fresh blank slate graduate minion who knows no SQL, what's the least bad resource for teaching them poo poo?


unfortunately the approach I had of "get given all the dbo passwords by the lead developer and go hog wild" is no longer viable

cinci zoo sniper
Mar 15, 2013




Powerful Two-Hander posted:

ok I've got a fresh blank slate graduate minion who knows no SQL, what's the least bad resource for teaching them poo poo?


unfortunately the approach I had of "get given all the dbo passwords by the lead developer and go hog wild" is no longer viable

1) https://www.w3schools.com/sql/default.asp
2) docs of your specific database server

further than that idk, especially without knowing if you want him to be analyst, dba, etl, or whatever

crazypenguin
Mar 9, 2005
nothing witty here, move along

hackbunny posted:

setjmp/longjmp has higher overhead than c++ exceptions

normal implementation have no real overhead (literally setjmp just saves registers and longjmp restores them) except on windows, where microsoft's implementation does some extra stuff to deal with the windows exception handler mechanism. (Which is probably what you have experience with?)

There was a funny paper recently about implementing algebraic effects as a C library, using setjmp/longjmp: http://lambda-the-ultimate.org/node/5457

akadajet
Sep 14, 2003

cinci zoo sniper posted:

1) https://www.w3schools.com/sql/default.asp
2) docs of your specific database server

further than that idk, especially without knowing if you want him to be analyst, dba, etl, or whatever

w3schools is super lovely for anything web related, I'd have to imagine that extends to their sql tutorials. I've found a lot of the info on w3schools to be just plain incorrect.

cinci zoo sniper
Mar 15, 2013




akadajet posted:

w3schools is super lovely for anything web related, I'd have to imagine that extends to their sql tutorials. I've found a lot of the info on w3schools to be just plain incorrect.

i only know w3schools for the very basics of sql, and that seems to be alright there, at least when i skimmed through them this june or so

GraceGarland
Jul 4, 2003
Code Wars has SQL poo poo if you want to push him to do more than just `SELECT * FROM whatevers WHERE`.
SQL Antipatterns is great, especially if he's going to be designing the DB structure.

hackbunny
Jul 22, 2007

I haven't been on SA for years but the person who gave me my previous av as a joke felt guilty for doing so and decided to get me a non-shitty av

crazypenguin posted:

normal implementation have no real overhead (literally setjmp just saves registers and longjmp restores them) except on windows, where microsoft's implementation does some extra stuff to deal with the windows exception handler mechanism. (Which is probably what you have experience with?)

c++ exceptions have zero overhead... in terms of code that's executed. in terms of code size and extra metadata, though, they can be on the heavy side. you need extra code for unwinding when your stack frame is skipped, but in many (not all cases) you can recycle it for regular scope exit unwinding. but the real overhead is in all the metadata: for each instruction of a function that may be involved in unwinding, what objects need to be unwound at that point of the functions and how (not literally for each instruction, the metadata stores ranges of instructions instead); for each function, how to pop its frame off the stack (often in the form of bitcode that tells you what registers you should pop a stack slot into and so on, basically how to run the function prolog in reverse); enough runtime type information to make a copy of the exception object and match it to the catch statements; etc. if no exceptions are thrown (the expectation), then all of this is completely invisible

setjmp has fixed, guaranteed overhead: you need to save registers and add a conditional to your code. you have to do this every time, regardless of whether exceptions will be thrown or not

windows exception handling is a whole another matter. first of all, it only has overhead on x86, where a parallel exception handler stack is maintained (with the related extra push/pop operations). on all other platforms, the regular stack contains most of the required information, and exception handling metadata supplies the rest (minus local unwinding information - i.e. what objects are in scope for each instruction in a function - which is managed by each compiler on its own, and of course any kind of language- or compiler-specific logic on what exceptions are caught and where). windows exception handling is also asynchronous (i.e. it assumes any instruction can raise an exception) and it has restartable exceptions (exceptions can be "fixed" without being caught so that execution can resume); as a consequence of restartable exceptions, its unwinding is virtual, i.e. it's performed on a shadow stack, and the "hardware" stack is lowered all at once at the end of the virtual unwind. it's a very different beast from the "soft" exception handling featured in c++ and imo it was very stupid of microsoft to base c++ exceptions on windows exceptions: not only the restartability adds a lot of overhead to unwinding for no benefit (c++ exceptions are unrecoverable), but you can't even reliably use c++ exception handling to catch hardware exceptions (c++ does not assume any instruction can throw, and to assume as much - you can enable it as an option in all major compilers - means goodbye zero-overhead and welcome to a lot more bookkeeping). it was based on the flawed old-school view of c and c++ as "fancy macro assemblers" which hasn't been true in a long time

for the record, I have used setjmp/longjmp, implemented setjmp/longjmp, used windows exception handling, implemented windows exception handling and implemented setjmp/longjmp-based windows exception handling

hackbunny fucked around with this message at 18:36 on Sep 24, 2017

Adbot
ADBOT LOVES YOU

FamDav
Mar 29, 2008

HoboMan posted:

the parts about vms and monitoring and scaling. i was just gonna swap staging into prod and vice-versa

HoboMan posted:

like actually swap the boxes instead of running a bunch of scripts in a particular order to properly clone it

oh ok i drive-through window'd you. yeah if you're at the stage where you have two literal boxes and you just want to treat them like a double buffer go hogwild. just make sure none of your tooling assumes that one particular box is always staging or production :)

  • Locked thread