« first day (3827 days earlier)      last day (1138 days later) » 

12:15 AM
hey, using sqlalchemy, what is the right way to call a foreign key class from a foreign key.
I'm working on an example to explain what I mean , just hold on a sec.
class bag(db.Model):
 id = db.Column(db.Integer, primary_key=True)
 name = db.Column(db.String(), nullable=True)
 box= db.relationship("box", backref='bag')

 class box(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(), nullable=True)
  bag_id = db.Column(db.Integer, db.ForeignKey('bag.id'))
  item_id = db.Column(db.Integer, db.ForeignKey('item.id'), nullable=True)


 class item(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(), nullable=True)
When we query the bag, It also queried the "box" classes that were in it, but even thought the box classes had item foreign key in it , it didn't query it, instead, just printed it's value
maybe that's good not to overload the server and the query for no reason but what If i want to pull value from the item class while pulling the bag object , for example, I want to know each item's name.
 
12:33 AM
Has anyone here used the CTRL + ] syntax to show/hid Colab notebook cells?
 
my current solution is to loop through the whole list ( box list ) and query each item in it, but i'm not sure if that is the best way of doing that.
 
Username checks out :)
 
 
11 hours later…
user13727121
11:21 AM
Is it still a constant if I use a string method on it in a function? Example: dpaste.com/9H2Z383SY
 
did COIN_SIDES ever change throughout that code?
 
@CoreVisional alternatively, if you didn't use it anywhere why would it need to exist at all? constant = doesn't change. not "never used or accessed"
so yes, it's still a constant
 
 
1 hour later…
12:53 PM
@CoreVisional strings are immutable
 
1:18 PM
Unfortunately the string method in question is random.choice and it's being called on a list :P
 
I think CoreVisional is worried about .title
 
Oh, that makes a lot more sense
 
In a way
 
user13727121
@ParitoshSingh No, it just kinda stays there, I only capitalized the first letter using .title().
 
user13727121
@Aran-Fey I thought the seq can be a list, string, tuple, etc.. I changed it to strings instead of a list
 
1:36 PM
Any sequence should work as an argument to random.choice. But they have to be numerically indexable.
>>> random.choice("abc")
'a'
>>> random.choice(["a", "b", "c"])
'b'
>>> random.choice(("a", "b", "c"))
'b'
>>> random.choice({"a", "b", "c"})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/anaconda3/lib/python3.6/random.py", line 261, in choice
    return seq[i]
TypeError: 'set' object does not support indexing
>>>
 
user13727121
1:51 PM
@holdenweb Okay I see now, no wonder I got an error the first time I used it, it was not about indexing though, but object is not scriptable
 
Not _sub_scriptable?
(subscripting is another name for indexing with [index])
>>> object[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
 
@holdenweb How did you manage to get this error? It's TypeError: 'set' object is not subscriptable in all the versions I have (CPy3.9, CPy3.8, PyPy3.6).
 
user13727121
@holdenweb yeah that was the error when I tried to put the value in a set, it didn't mention about set not being indexable, but I guess it's another name for indexing as you pointed out
 
@MisterMiyagi Python 3.6.10 |Anaconda, Inc.| :)
 
@CoreVisional exactly, it doesn't matter whether you make new things out of it, COIN_SIDES itself never changes
so, is it still a constant? :)
 
2:01 PM
@holdenweb interesting. Guess I need some more test versions. ^^
 
@MisterMiyagi Plus, I was indexing object, whose type is indeed type(type).
 
Just wondering why it says "indexing" instead of "subscriptable"
 
user13727121
@ParitoshSingh Well, from my understanding, it is still a constant, the values in COIN_SIDES never changes, I only capitalize the first letters of the two values.
 
It appears sets get their own message:
>>> {1}[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
 
@CoreVisional you're correct
 
2:03 PM
:shrug:
 
more importantly, you never capitalize and "update" the COIN_SIDES variable
your capitalization (by title) are making new strings, which are then used, but the original data remains untouched.
 
@holdenweb it feels like it should support indexing
 
user13727121
@ParitoshSingh Ahh I forgot that in the String Method lesson I learned, it says that string methods can only create new strings, they do not change the original string.
 
user13727121
I was using .title(), so yeah, COIN_SIDES is still constant, thank you by the way
 
@CoreVisional what about list.sort()?
 
user13727121
2:10 PM
what about it? Is the variable still a constant if I use sort() on it?
 
i thought that list.sort() would sort the list without having to set the list like list = list.sort()
also list is a list (e.g. [1, 2, 5 ,3 ,4])
 
user13727121
wait what, but I'm not sorting anything here. Also, wouldn't x = [3, 2, 5, 6] then x.sort() works? new_lst = x.sort() will obviously returns None, why not new_lst = sorted(x)?
 
2:41 PM
I want to simultaneously perform two operations which is enqueue and dequeue. How can I do that?
 
do them one after another, call it good
 
Terminology nitpick: a "constant" is a variable that the programmer has no intention of reassigning or mutating. An "immutable object" is a value that the programmer literally cannot change.
 
if you have a maxlen for a deque you automatically dequeue when you enqueue
 
DIRECTIONS = ["N", "E", "S", "W"] is both constant (if you remember not to reassign or mutate it) and mutable (you could call .sort() on it to mutate it if you really wanted, and Python wouldn't stop you)
@MohdAbdulAzeem If you need guaranteed atomicity, use a Lock
I guess my DIRECTIONS example is a bit unrealistic because, where practical, you should use only immutable types when creating a constant. In this case, you'd prefer a tuple over a list: ("N", "E", "S", "W")
 
@MohdAbdulAzeem With threads or asyncio
 
2:56 PM
A dict are more permissible to use in constants because it doesn't have immutable equivalents in the builtin module. I can't be bothered to pip install import frozendict just to potentially prevent a mistake I'm likely not going to make
 
frozenset is built in right?
nvm, I read it wrong
 
You read it right, before I edited it :-P
"Or, hmm" is my code word for "let's see if I can edit out my factual error before anyone notices"
 
The worst part is that frozendict is unmaintained and throws a DeprecationWarning, so you have to use immutabledict instead, which doesn't sound quite as catchy
 
blech
 
3:17 PM
cabbage
 
user13727121
3:45 PM
@Kevin so what you're saying is that it is better to assign immutable data types to a constant since we are not permitted to change the value of a constant throughout the code, right? EDIT: This is referring to your DIRECTIONS example.
 
Yes, although "not permitted" is only by convention. In other words, it's your job to ensure your constants stay constant. Python won't do it for you, and not even code analysis tools can verify it
*can verify it in all cases. obviously a code analysis tool is capable of telling you there's something suspicious about FOOBAR += 23
 
user13727121
I also notice that I don't really need to parenthesize the values in my tuple like my code here COIN_SIDES = "heads", "tails". Must I enclose it for some special reasons? Or is it about readability?
 
Looks like actual enforceable checkable constants were proposed but the big man said "this would be really hard and also probably annoying to use", so I guess that will be that for the forseeable future
@CoreVisional Just readability.
PEP 8 doesn't have a stance on whether parentheses are preferred, so leave them in if you like, or leave them out if you like.
Keeping in mind that parens can't be stripped out of a tuple in all contexts. x = (1, 2) is functionally identical to x = 1, 2. But f((1,2)) is not functionally identical to f(1,2)
 
user13727121
4:03 PM
@Kevin I see, thank you for clarifying that with me.
 
user13727121
@Kevin Seems like a tuple inside a tuple to me. Nvm, it's only 1 tuple, unless f((1, 2), (3, 4))
 
4:15 PM
Continuing my Koch Curve experiments from yesterday. This is a Voronoi diagram for a curve of degree 3.
This experimentally confirms the ideas Andras and I were kicking around yesterday - the area of space closest to the curve's central peak is represented here as a dark blue region in the lower right. As suspected, it diverges as it moves away from the curve, and its sides are (AFAICT) perpendicular to the curve's convex hull. And bonus, it's a little wider than I thought it would be because it doesn't narrow to a single point.
But perhaps it does narrow more sharply for higher degrees. I'll try my code on degree 4, but it will take upwards of 20 minutes. Gotta love O(4^N) algorithms.
*dark blue region in the lower left
 
@LoopingDev That's where you need to look into joins
Also, the additional subquery was launched because you didn't set the lazy parameter on your relationship
 
4:34 PM
I couldn't find any examples to decode the Gzip results in python. — Adam Smith - Microsoft Azure Jan 22 '19 at 23:55
does that make sense to anyone? Doesn't requests automatically decode any gzip compression? And doesn't Stack Overflow send API results uncompressed anyway?
 
@escr Why? Sets are explicitly unordered. You can iterate over them perfectly well, but how would set indexing even work?
 
@LoopingDev What do you mean by "it didn't query it" and "just printed it's value"? Will you be more specific please?
@PaulMcG bemoans? why? that seems like a good way to learn about recursion.
 
recursing over a linked list is bad design in professional code, so it shouldn't be taught as an exercise either
 
oh...I guess I don't do much with linked lists in my professional code, so I hadn't considered it.
What do you do instead?
and why is recursing over a linked list bad?
 
while True:
    do_thing(node.value)
    if node.next is not None:
        break
    node = node.next
 
4:42 PM
any language that doesn't implement tail recursion will drop its stack at some point
 
If you recurse, then you'll hit the maximum recursion depth after 999 elements, or get a stack overflow after a large yet plausible number of elements
 
and lists with 1000 elements aren't unusual at all
 
oh okay, I see what you mean. For large lists, it's easy to recurse too deep
 
Perhaps a better exercise in recursion would be to walk a balanced binary tree. You still have the possibility of hitting max recursion depth... If the tree contains 2^999 nodes.
 
@tripleee I don't think requests does do that or, at least, not if you stream the download
 
4:44 PM
Which may or may not be larger than the number of atoms in the universe, so let's call that one "implausible"
 
@Kevin Is there some way to cast that as a generator so it reduces to for node in some_function_of(rootnode): do_thing(node.value())?
 
Indeed
while True:
    yield node
    if node.next is not None:
        break
    node = node.next
 
better yet, override __iter__() and __next__().
 
@Kevin yield from iter(lambda: (node := node.next), None)?
 
veto
 
4:48 PM
while (node := node.next) is not None: yield node?
 
Ok, I'll let that one through, but the Supreme Court may strike it down later.
The constitutionality of walrus operators is not yet thoroughly explored in case law
 
I'll have a pattern matching version ready by then.
 
"I know that we usually just decide whether to reject or accept an appeal of an existing judgment, but given Miyagi's clear lack of remorse, I'm going to go ahead and banish him to Skull Island"
It has wifi, don't worry
 
The path to Skull Island is paved with good PEPs.
 
Skull Island is where I hunt the most dangerous game, so obviously I need a good connection for livestreaming
 
4:57 PM
Kudos for putting pattern matching on the list of endangered syntax.
 
I consider myself quite the conservationist... I have quite a lot of specimens stored safely on the wall of my trophy room
Another voronoi diagram. There's evidently a lot of symmetry and similarity going on here, which gives me hope that I can find a more efficient rendering method that's practical for larger degrees.
 
@Kevin I read this as "I consider myself quite the conversationist"
 
@roganjosh I think I have verified that it does this transparently, like I expected, but my http(s) skillz are way rusty
 
5:15 PM
@Code-Apprentice If anything, I put my summer lodge on Skull Island so I could avoid conversation
 
and yet you are here
@Kevin So I read this story in high school and I am just now realizing the double meaning in the word "game" here.
 
I think I got halfway through the story before the same occurred to me
 
@tripleee ah, but I suspect that's because of the headers that were set. For example, the code I inherited is: dpaste.org/3xyX and that really does require the unzipping step (which I suspect will behave the same with gszip)
 
@roganjosh zip is an archive format, gzip is just streaming compression
 
5:30 PM
But in my example, I didn't tell requests what it was streaming anyway (I didn't set any headers) so I don't think it'd know unless I did set the headers? In which case, that might be what that commenter had done. I'm not saying it's the correct way to do things, only that it might explain how they got into the situation they are in?
 
according to the Stack API documentation they send everything gzipped api.stackexchange.com/docs/compression ... but the code with that comment was using requests which to my understanding has been doing this transparently for a long time (but maybe they were just asking in general and found a weird place to ask)
I was able to verify with curl that it does indeed send content-encoding: gzip even if you don't explicitly send Accept-Encoding: gzip
 
I think PEP8 should be changed to allow no spaces between operators when using them to increment positional indices. myarray[i, j+1] vs myarray[i, j + 1]
 
6:10 PM
@Code-Apprentice I find it misleading to teach recursion as an alternative to iteration. Iteration is straightforward, and does not require extra mental gymnastics. I have always bristled at the classic recursive example of computing factorial(n) - iteration makes so much more sense to me. Find a recursive case that actually reflects some hierarchy, like walking a graph, or a directory of files, or pages on craigslist, or game choices in tic-tac-toe or nim.
 
@PaulMcG that's interesting. Those examples are definitely all more interesting than computing factorial. I think we have different backgrounds because a recursive implementation of factorial makes sense to me coming directly from a precise mathematical definition. But at the same time, I realize the pitfalls and that iteration is a better way to go.
 
6:53 PM
@PaulMcG ohh, I didn't hear about nim in ages. I wrote a player for that game in prolog, recursive of course.
I'm not even sure prolog is able to do iteration
 
7:21 PM
@Code-Apprentice To me, factorial is repeated multiplication, just like exponentiation is or multiplication is repeated addition. I'm sure all could also be implemented recursively instead of iteratively, but why?
 
@piRSquared AFAIK PEP8 already allows omitting it between arithmetic operators, e.g. 2*3 + 4*5 is valid PEP8
 
@ThiefMaster Well I'll be ... I didn't know that. I feel a lot better today.
 
brief cbg
@PaulMcG my thoughts on it exactly.. :)
 
If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your own judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator:

# Correct:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
PEP 8: "Use your own judgment". piR2: ">:)"
 
@Andras and stuff like 3 -4 just looks odd
 
7:32 PM
"always have the same amount of whitespace on both sides of a binary operator"
 
3-4 looks like one's trying a range syntax of some... but 3 -4 almost looks I'm missing an operator between 3 and -4...
 
the -- operator
hmmmm
# Wrong:
[...]
c = (a + b) * (a - b)
that "wrong" is at odds with "consider adding whitespace"
 
I think that's part of PEP8 I'm not going to worry about... as long as things are readable and as clear as they can be for tired eyes... shrug
 
I tend to omit whitespace for multiplications (including division). So I'd write that as (a + b)*(a - b).
 
@ThiefMaster But black will reformat that as 2 * 3 + 4 * 5
 
7:38 PM
the point of black is to remove formatting bikeshedding, so you can just accept that :P
 
I don't mind it reformatting other peoples' code. I just don't want it reformatting my code.
 
@inspectorG4dget I've found a way out for you: apparently with annotated function parameters one should use whitespace around the = of default values
# Correct:
def munge(sep: AnyStr = None): ...
def munge(input: AnyStr, sep: AnyStr = None, limit=1000): ...

# Wrong:
def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...
so you just have to annotate your parameters with Any and it will be PEP 8 compliant :P
 
7:55 PM
@PaulMcG yup, I understand. I'm just explaining I how recursion makes sense here, too.
and as you say, recursion is much more than just another way to do iteration.
 
@Code-Apprentice it also depends how clever the language is/how intrinsic its recursion abilities are and its stack/etc handling is... as you can always write your own mini stack for things like graph traversal etc...
sometimes though, you don't even want that language to be handling it, as you'd rather push an item into a queue and have an external "thing" (be it disk file/db) worry about sorting out the mechanics so you can focus on the logic rather than the implementation
 
8:12 PM
Do you want to get declarative languages? Because that's how you get declarative languages.
(I think :P)
 
@AndrasDeak oooh! interesting. Thanks :)
 
of course adding type hints will make it overall less readable than any unannotated choice of whitespace
yes, including def foo( bar =default1 , baz= default2):
 
8:31 PM
hahaha. Very true
 
 
2 hours later…
10:34 PM
@JonClements I've always admired your wisdom and intellect.
 

« first day (3827 days earlier)      last day (1138 days later) »