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
PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
singletons are to globals as flow-control exceptions are to GOTO


nice stealth edit

Adbot
ADBOT LOVES YOU

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Dirk Pitt posted:

Didn't mean to ghost edit. I agree with your post. Just frustrated my team sees anything more advanced than a 'systemcontext' singleton as "too complicated."

Edit: I thought this was a safe zone :smith:

it's all right, I stealth-edit pretty much every post I make. just thought it was funny that I spotted yours.

everyone is welcome here :glomp:

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

OBAMA BIN LinkedIn posted:

c++ owns you rear end burgers and if you disagree you need to get the gently caress out forever. jk this is the terrible programmer safe zone, you're welcome to post here.

some sick burns itt

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

dur posted:

this client has terrible data so today i wrote a bad script to count annotations with bad geometry

code:
mxd = arcpy.mapping.MapDocument('Current')
layers = arcpy.mapping.ListLayers(mxd)

zerolength = 0

for lyr in layers:
	if lyr.isGroupLayer:
		if arcpy.Describe(lyr).featureType == "Annotation":
			arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", ' "SHAPE_Length" = 0 ')
			result = int(arcpy.GetCount_management(lyr).getOutput(0))
			zerolength = zerolength + result
		
arcpy.AddMessage(zerolength)
it works, sort of

backseat scriptin'

code:
mxd = arcpy.mapping.MapDocument('Current')
layers = arcpy.mapping.ListLayers(mxd)

def layer_length(layer):
    arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", ' "SHAPE_Length" = 0 ')
    return int(arcpy.GetCount_management(layer).getOutput(0))

layer_lengths = sum(layer_length(layer) for layer in layers
                    if layer.isGroupLayer and "Annotation" == arcpy.Describe(layer).featureType)
arcpy.AddMessage(layer_lengths)
haven't worked with arcpy so not sure what's going on with the ' "SHAPE_Length" = 0 ' line, looks horrifying though

hth :tipshat:

fakeedit: this is a bad post but w/e :justpost:

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

polpotpi posted:

java is the only language versatile enough to be used on the fronted and backend. the goddamn internet runs on java. you can get paid to write code in java.

i dunno how anyone can not like shaggaring

dur posted:

python owns

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
yeah basically that behavior is to discourage people from using/modifying nonlocal variables without being very explicit about it, to help avoid confusion about where exactly symbols are coming from (confusion on the part of maintainers, not the machine)

the same thing happens if you try to assign to a global variable within a function (but lol if you use global variables)

in both cases, python (3, the one true python) provides a way to explicitly specify nonlocal variables that you're going to modify within an inner scope: "nonlocal foo[, bar[, baz...]]"

(or in the case of globals, 'global foo' etc)

it's slightly counterintuitive when you first run into it, but I can see why they did it



re the lambda example: we actually had a thread about closures & such a few months back. might have had a discussion in this very thread, I forget. anyway, if memory serves, you can get the behavior you want by doing something like

code:
a = [(lambda x: lambda y: x + y)(i) for i in xrange(5)]
or more verbosely

code:
def gen_func(x):
   def out_func(y):
       return x + y
a = [gen_func(i) for i in xrange(5)]
which reserves a separate scope to store the value ('i') you want for each generated function



I like closures

I like python

PleasingFungus fucked around with this message at 17:34 on Jul 18, 2013

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

dur posted:

i wrote some python today, can y'all tell me how bad it is (it's bad because it's python but i mean besides that)

honestly, it looks generally fine. not familiar with arcpy, but nothing there looks obviously wrong.

I'd probably rewrite getValueList() as:

Python code:
def getValueList(flag, field):
    return list(set(row.getValue(field) for row in arcpy.SearchCursor(flag)))
and that's about it.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

prefect posted:

i think that looks pretty cool, but i am also fond of perl, so you might want to reconsider that code :D

eh, it's pretty idiomatic. grab an iterator, map a single function on it, feed that into a set and then into a list. it's admittedly borderline - one more operation, and I'd definitely want to split it onto multiple lines - but I think it's ok.

actually, on further consideration, this is probably about right:

Python code:
def getValueList(flag, field):
    valueSet = set(row.getValue(field) for row in arcpy.SearchCursor(flag))
    return list(valueSet)

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Jerry SanDisky posted:

can you just use a set comprehension on that first line?

yes, I literally did two posts up from the one you quoted. but then I decided that it was too many things going on in one line, and split it up to make it easier to read.

dur's original code was

Python code:
def getValueList(flag, field):
    values = set()
    rows = arcpy.SearchCursor(flag)
    for row in rows:
        values.add(row.getValue(field))
    valueslist = list(values)
    return valueslist
but that was on too many lines, and actually made it harder to read. like writing

Java code:
int i;
i = 0;
int bound = arr.length - 1
while (i < bound) {
    arr[i] = arr[i+1];
    i++;
}
instead of
Java code:
for (int i = 0; i < arr.length - 1; i++)
    arr[i] = arr[i+1];
it's a balance, and admittedly something of a personal judgment - different people will have different opinions on what's easy to read. (some people still believe in the 80-character limit!) but I think I'm right, and that's what matters.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
just noticed something else:

Python code:
    for feature in featureclass:
        oidlist.append(feature.attrib.get('oid'))
        if len(oidlist) > 1:
            qry = "OBJECTID in " + str(tuple(oidlist)).replace("'", "")
        else:
            qry = "OBJECTID = " + str(oidlist[0]).replace("'", "")
    arcpy.SelectLayerByAttribute_management(currentfeature, "NEW_SELECTION", qry)
the 'if len(oidlist)/else' block should probably be decremented one setp; no reason to do it until you've finished building oidlist. also... are you turning oidlist into a tuple so you get parens instead of square brackets? :allears:

actually, now that I'm looking at it

Python code:
...
    def feature_to_oidstr(feature):
        oid = feature.attrib.get('oid')
        return str(oid).replace("'", '')
    oidstrlist = [feature_to_oidstr(feature) for feature in featureclass]

    if len(oidstrlist) > 1:
        qry = "OBJECTID in ({})".format(','.join(oidstrlist))
    else:
        qry = "OBJECTID = " + oidstrlist[0] #this will throw an exception if there are no oids in the featureclass

    arcpy.SelectLayerByAttribute_management(currentfeature, "NEW_SELECTION", qry)
more lines, but clears up some silliness with the strings slightly, maybe.

honestly I just like playing with python and list comprehensions. I admit it.

Jerry SanDisky posted:

no, calling set() on a list comprehension is not a set comprehension

o

oops

yeah, that's a reasonable tweak

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

tef posted:

(ps technically he called it on a generator comprehension).

(pps you don't need to return a list, a set would be fine I think)

I thought that at first, but then I noticed this bit

Python code:
    featoidlist = getValueList(QAFeature, "FEAT_OID")
    if len(featoidlist) > 1:
        qry = "OBJECTID in " + str(tuple(featoidlist))
    else:
        qry = "OBJECTID = " + str(featoidlist[0])
the last line being the relevant one

you could rewrite that bit, but I was trying for a minimum-impact change.


tef posted:

ps should i make a smart dog book thread in CoC and then argue about stuff?

yes

then link it here or in the other programming thread

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Jerry SanDisky posted:

why am i so loving bad at python :eng99:

anyways, i'm sure you will explain why, but the set comprehension is reliably faster on my machine given these fake functions

you got that backwards, the set comprehension is orders of magnitude slower per your numbers

gently caress if I know why, though

Shaggar posted:

switch 2 java or c# ftw

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Jerry SanDisky posted:

foo_comp uses the set comprehension, foo_set uses the set() builtin. they both ran 100k times and foo_comp took 13 seconds less??

oh, I was misreading it.

I think I might just be really bad at reading your posts.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

PleasingFungus posted:

I think I might just be really bad.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

dur posted:

well the selectlayerbyattributes query needs things to be in parenthesis, not square brackets. that's how someone else on the arcgis.com forums did it so i stole it.

that is extremely silly, and certainly bad practice, but I can't bring myself to be upset about it. (the teddy in your av probably helps. :3:)

the post you quoted suggests one way to be a little more explicit about what you're doing vis-a-vis string formatting:

Python code:
            qry = "OBJECTID in ({})".format(','.join(oidstrlist))
build a comma-separated list of elements, then drop 'em between parens. it's the same result as the tuple approach, just expresses your intent more clearly in code, rather than indirectly indicating it by switching data structures.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Shinku ABOOKEN posted:

as someone who doesn't use python that syntax looks awful

i can't tell what i'm looking at (did you mess up the indentation or something?)

can someone explainn to me

as someone who uses python & loves list comprehensions, I am baffled and faintly horrified. that code is incomprehensible.

I'll second the question re indentation.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
googling finds context: it's a fragment of a program that solves the Zebra Puzzle

specifically, it's a fragment of one of these programs; though notably, these two are also both incomplete.

still looking at indentation. I tried a few different variants, but they were clearly both (a) wrong and (b) broke tables.

PleasingFungus fucked around with this message at 04:54 on Jul 24, 2013

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
okay, got it, maybe. it iterates over assignments of (house number) permutations to elements, filtering out impossibilities at each stage; at the end, an iterator is returned that only yields one element, the solution.

Python code:
return next(
	(
		(
			(
				(
					(WATER, ZEBRA) for (red, green, ivory, yellow, blue) in c(orderings) if imright(green, ivory)
				) for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in c(orderings) if Englishman is red if Norwegian is first if nextto(Norwegian, blue)
			) for (coffee, tea, milk, oj, WATER) in c(orderings) if coffee is green if Ukranian is tea if milk is middle
		) for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments) in c(orderings) if Kools is yellow if LuckyStrike is oj if Japanese is Parliaments
	) for (dog, snails, fox, horse, ZEBRA) in c(orderings) if Spaniard is dog if OldGold is snails if nextto(Chesterfields, fox) if nextto(Kools, horse)
)
flow proceeds first from bottom to top (the bottom/outermost iterator requesting a yield from the one above, etc), then top to bottom repeatedly (as the top/innermost iterator yields a permutation, which proceeds downward, matching with other permutations until a filter rejects, at which point control steps up a level, etc); the top iterator yields many times, the bottom iterator only once.

in the end, a pruned subset of P(5)^5 = ~25E9 possibilities are examined; the code is from an attempt to minimize on possibilities examined.

that only took 35 minutes to figure out. good code!

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
sorry :smith:

it's worth noting that the explanation in my last post is still wrong, I... think. the conditionals are definitely being evaluated top-to-bottom, but something is traveling bottom-to-top; otherwise how did (WATER, ZEBRA) get into the innermost loop?

functional programming is hard

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

dur posted:

holy gently caress dude

i changed it to work this way (though it needs to be "OBJECTID in ({0})" because python 2.6 (that's what arc10 supports :( )) and it now takes 4 seconds to select 371000 features, instead of the hour and a half it took the original way

this owns, thanks!

and here I thought I was just sperging about Proper Usage! super glad I could help. :)

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

MeruFM posted:

This is definitely the most complex a single list comprehension should be
code:
for user in [post.user() for post in thread if poo poo(post) is True]:
    user.ban()

I don't understand. why include the conditional check if it's always going to return True?

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
some day I will work in a company with a rich testing environment

that day is not today

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

wait, you're the guy who claims that java/python/etc aren't "real programming languages" (as opposed to esoteric academic functional languages, aka, all functional languages)

your opinions are a very strange mix.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

FamDav posted:

gently caress all of you

hey

hey

hey

hey




rude

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Symbolic Butt posted:

I think I use way too much breaks and continues in my code.

this seems improbable

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Shaggar posted:

altho that's some serious p-langer syntax so whatever it is probably cant even do transactions.

the style is unusual for the language, but I think it might actually be c++ syntax. don't know any other language that has both arrows and double-colon operators.

JewKiller 3000 posted:

he does it once in a while to make the other posts more believable

lol if you haven't realized shaggar is the best poster in yospos

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Mr SuperAwesome posted:

like C works great when you create a load of structures so you can use dot notation and stuff and basically make it like a normal language, apart from everything being really linuxy and stupid and lets stick some control characters all over the place to read and write poo poo thats a good idea.

and all this reinventing the wheel to use list/etc style structures in a sensible way


gently caress c basically

c is portable assembly

set your expectations accordingly

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Shaggar posted:

nothing soothes my autism like good data design

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
I never worked with SQL long enough to develop a good intuition for joins. that'll probably bite me in the rear end someday.

rip.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Bloody posted:

you wanna know an awful lang?

object-oriented matlab. why am i writing this. someone kill me pleas

oh my god

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Zlodo posted:

its not "worrying about stack allocation details", its being able to say "pass this thing by value because i know its small" and even if at high level you dont necessarily have to care being able to pass value types around efficiently is the difference between "wee lets use option types everywhere bc its neat and almost free" and "better be careful not to overuse those because theyre pointlessly slow"

also yeah i can tell java and c# developers dont give a gently caress about performances whenever things lock up on me to crap the bed while doing garbage collection
you can tell more and more of visual studio is replaced with slow c# code with each version bc that thing is becoming comically slow

and in fact i'm not saying "you sholuld worry about cache locality" but perhaps the people who design the language / vm / libraries that you use should

'why isn't Java C?'

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Zlodo posted:

profiling is useful when considering an optimization that comes at a cost of your program's readability/maintainability/flexibility/stability which is not what is being discussed here

"ok i'll do a new on that struct and pass a smart pointer to it and then i'll profile and if necessary replace it with passing it directly by value which is both much more readable and faster" - nobody ever

except that, in java, allocating objects on the heap is both natural, easy to read, and correct. this is because Java is not C.

like any optimization, arguing that objects should be allocated on the stack is a decision that should be handled case-by-case and justified by profiling. it is not a default. because Java is not C.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

FamDav posted:

are you telling me that this mostly java

code:

class MyClass
{
	public int x;
}

void swap(MyClass a, MyClass b)
{
	int tmp = a.x;
	a.x = b.x;
	b.x = tmp;
}
doesn't swap the contents of a and b

that's correct. the contents of variables a are b are not swapped by that code.

going back to jewkiller's post:

code:
a = x
b = y
swap(a, b)
assert(a == y && b == x)
and modifying it to fit your example

code:
MyClass x = new MyClass(1);
MyClass y = new MyClass(2);
MyClass a = x;
MyClass b = y;
MyClass.swap(a, b);
assert(a == y && b == x)
the assert will fail. after the 'swap', a.x will be set to 2, and b.x will be set to 1, but which objects are held in which variables doesn't change. ("assert(a == x && b == y)" will be true on either side of the swap() call.)

this is something that fucks beginners up pretty often.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Pollyanna posted:

ummmm idk :( i guess its frustrating trying to get a gui going in python cause there's no great choice out there but also i dunno what to do next, sure there's django n poo poo but im not really a web developer/database admin. most things seem oriented to web development this days in fact

I always used tkinter back when I was doing python gui stuff. anything you make with tkinter will be incredibly ugly but it's pretty easy to get going iirc. (IDLE is a fine tkinter product, if you want an example of tkinter-ugly.)

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Pollyanna posted:

okay, that makes sense. i get that objects n methods are good for simulating irl circumstances but how does that translate to a broader programming perspective, like for example how would you use oop for abstract things like an IM client? what would a typical class be in that regard? something like class Message?

and yeah its prolly just that im inexperienced which is why i dont know what the gently caress


but those are funny

sure. let's look at an im client. say it has a class Window, which represents an open chat window. Let's give it a TextInputField, a Scrollbar, and a list of Messages. The Messages have an author and some text for the actual message...

basically, the idea is that you break things down into natural components, and nest them inside each other, so that you can more easily abstract what's going on. the ConversationDisplay doesn't really know anything about what's going on inside the Messages; all it does is ask them how much space they need & then position them accordingly. likewise, the Messages don't know what they're being used for; for all they know, they could be displayed in a corner-of-the-screen notification bubble. everything is split up to be as simple as possible so you don't have to worry how everything combines with everything else; each class only has to deal with its own area of concern.

classes are, in the end, a tool for making programs easier to understand.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
sulk rackin' up the wrongposts like there's no tomorrow

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Nomnom Cookie posted:

java will complain if you do stuff with null (nil). objc is more laid back and just lets the nil propagate to somewhere a user can see it.

whaaaa

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

lolllll

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Soricidus posted:

You can't even just implement a well-known algorithm yourself because you can't trust the compiler.

this is a funny troll

Adbot
ADBOT LOVES YOU

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Shinku ABOOKEN posted:

is azure a comedy option or legit good?

http://www.techweekeurope.co.uk/news/microsoft-azure-fail-security-certificates-109344?ModPagespeed=noscript

quote:

Everything went according to plan for the Azure storage service – except for one thing. On 7 January, the team renewed the three certificates, for Windows Azure Storage Blobs, Tables and Queues, and created a new build of the services, which they then deployed.

However, they forgot to flag that updated build as an urgent release which contained certificate updates.

That meant the update got delayed behind updates which were apparently more time critical. On 22 February, when the certificates expired, it had still not actually been implemented.

To make matters worse, there was no warning of impending doom. Although the storage service was running with an expiring certificate, no alarm bells rang, because the certificate had actually been updated, and it all looked fine in the central secret store.

“Because the certificate had already been updated in the Secret Store, no additional alerts were presented to the team, which was a gap in our alerting system,” says Neil.

result: entire azure service goes down for twelve hours, for the second year in a row.

idk if it's any good but it'd def. the comedy option.

  • Locked thread