|
trekdanne posted:I ran into this "problem" with Python scoping. I know that Python uses statical scoping but that doesn't really work that well with the lambda keyword. the i is stored in the outer scope, and not copied into the lambda, so i keeps the value from exiting the loop. code:
tef fucked around with this message at 11:49 on Jul 20, 2013 |
# ? Jul 20, 2013 11:47 |
|
|
# ? May 18, 2024 15:12 |
|
tef posted:
|
# ? Jul 20, 2013 11:59 |
pop(i)
|
|
# ? Jul 20, 2013 12:06 |
|
trekdanne posted:I ran into this "problem" with Python scoping. I know that Python uses statical scoping but that doesn't really work that well with the lambda keyword. these loop 4 times so i will he 4 after the loop completes code:
code:
|
# ? Jul 20, 2013 13:02 |
|
tef posted:the i is stored in the outer scope, and not copied into the lambda, so i keeps the value from exiting the loop. code:
ok I'm happy now.
|
# ? Jul 20, 2013 22:01 |
|
tef posted:the i is stored in the outer scope, and not copied into the lambda, so i keeps the value from exiting the loop. to be honest, this makes sense anonymous functions should not store unless explicitly declared but it also means that function and statefulness do not mix
|
# ? Jul 20, 2013 22:11 |
|
code:
i don't have to stealth edit
|
# ? Jul 20, 2013 22:15 |
|
|
# ? Jul 21, 2013 23:27 |
|
tef posted:happy now ? I am still programming in Python, so the awswer is NOOO.
|
# ? Jul 22, 2013 01:18 |
|
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) this first one takes a qa flag layer like this: code:
Python code:
code:
Python code:
|
# ? Jul 23, 2013 23:11 |
|
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:
|
# ? Jul 23, 2013 23:19 |
|
PleasingFungus posted:
i think that looks pretty cool, but i am also fond of perl, so you might want to reconsider that code
|
# ? Jul 23, 2013 23:22 |
|
prefect posted:i think that looks pretty cool, but i am also fond of perl, so you might want to reconsider that code 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:
|
# ? Jul 23, 2013 23:25 |
|
seems like you could use a map and unpack if you want to go with new-skool self documenting, as few lines as possible, but i dont use python
|
# ? Jul 23, 2013 23:28 |
|
PleasingFungus posted: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. can you just use a set comprehension on that first line?
|
# ? Jul 23, 2013 23:32 |
|
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:
Java code:
Java code:
|
# ? Jul 24, 2013 00:33 |
|
PleasingFungus posted: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. no, calling set() on a list comprehension is not a set comprehension
|
# ? Jul 24, 2013 00:36 |
|
ps should i make a smart dog book thread in CoC and then argue about stuff?
|
# ? Jul 24, 2013 00:41 |
|
Jerry SanDisky posted:no, calling set() on a list comprehension is not a set comprehension (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)
|
# ? Jul 24, 2013 00:42 |
|
just noticed something else:Python code:
actually, now that I'm looking at it Python code:
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
|
# ? Jul 24, 2013 00:42 |
|
tef posted:ps should i make a smart dog book thread in CoC and then argue about stuff? yes even if it's exhausting for you, it'll be educational for the rest of us
|
# ? Jul 24, 2013 00:45 |
|
tef posted:ps should i make a smart dog book thread in CoC and then argue about stuff? make one here
|
# ? Jul 24, 2013 00:45 |
|
tef posted:(ps technically he called it on a generator comprehension). I thought that at first, but then I noticed this bit Python code:
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
|
# ? Jul 24, 2013 00:45 |
|
tef posted:(ps technically he called it on a generator comprehension). why am i so loving bad at python anyways, i'm sure you will explain why, but the set comprehension is reliably faster on my machine given these fake functions Python code:
code:
|
# ? Jul 24, 2013 00:51 |
|
i'm gonna write some python and dig into python source cuz im just embarassing myself
|
# ? Jul 24, 2013 00:53 |
|
switch 2 java or c# ftw
|
# ? Jul 24, 2013 00:55 |
|
Jerry SanDisky posted:why am i so loving bad at python 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
|
# ? Jul 24, 2013 00:56 |
|
PleasingFungus posted:you got that backwards, the set comprehension is orders of magnitude slower per your numbers foo_comp uses the set comprehension, foo_set uses the set() builtin. they both ran 100k times and foo_comp took 13 seconds less??
|
# ? Jul 24, 2013 00:58 |
|
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.
|
# ? Jul 24, 2013 01:08 |
|
PleasingFungus posted:I think I might just be really bad.
|
# ? Jul 24, 2013 01:09 |
|
PleasingFungus posted:honestly, it looks generally fine. not familiar with arcpy, but nothing there looks obviously wrong. yeah i like that a lot better, like you said it's a lot easier to read too. i think i did it the first way because i thought valuelist was the name of the thing the function returned (because i'm new to this whole programming thing) PleasingFungus posted: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?
|
# ? Jul 24, 2013 01:09 |
|
i'm guessing that the set-comprehension is mostly implemented in C, so python can go woosh. with the generator comprehension, some extra work is done, and a bit of indirection to build the set.
|
# ? Jul 24, 2013 01:16 |
|
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. ) 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:
|
# ? Jul 24, 2013 01:38 |
|
tef posted:ps should i make a smart dog book thread in CoC and then argue about stuff? prefect posted:i think that looks pretty cool, but i am also fond of perl, so you might want to reconsider that code Python code:
everybody is exactly like "I don't need this perl poo poo in my python"
|
# ? Jul 24, 2013 04:09 |
|
Symbolic Butt posted:yes 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
|
# ? Jul 24, 2013 04:33 |
|
Shinku ABOOKEN posted:as someone who doesn't use python that syntax looks awful 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.
|
# ? Jul 24, 2013 04:40 |
|
I think it's God's own code you can tell because it's ineffable
|
# ? Jul 24, 2013 04:42 |
|
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 |
# ? Jul 24, 2013 04:50 |
|
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:
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!
|
# ? Jul 24, 2013 05:15 |
|
|
# ? May 18, 2024 15:12 |
|
oh well it looks good to me and understandable for those kinds of brute-force things and also very convenient to tinker and reorder the loops. it's not very natural at first but I guess I got used with the bottom-up flow from comprehensions? something huge like this rarely came up for me besides challenge problems though but yeah I guess I gotta rethink this stuff
|
# ? Jul 24, 2013 05:25 |