« first day (3231 days earlier)      last day (1720 days later) » 

12:17 AM
I'm writing a bunch of python functions for a Jenkins job. Should I put the small separate functions into different python files. Or keep them in one script and parse input arguments to determine which function to call?
 
> keep them in one script and parse input arguments to determine which function to call
Do that ^
 
ok thanks
 
 
3 hours later…
3:14 AM
Question: What dunder method is called when unpacking a string like this [*'ABCD']?
 
3:32 AM
@ScottBoston For the unpacking operator and its implementation, I believe this might help answer your question: stackoverflow.com/a/19526400/1832539
 
3:53 AM
cabbage
 
4:18 AM
cbg
 
Hey cbg @ParitoshSingh
 
4:39 AM
cbg
 
5:01 AM
@wim I answered a very similar thing recently. In MATLAB you get the rounded value due to magic
 
 
2 hours later…
6:41 AM
do you guys know any generic SQL CLI client, like psql but written in Python (please) and preferably sth that works with SQLAlchemy connection uris :D
 
6:54 AM
does this fit the bill or nah?
 
7:06 AM
well it is for mssql but yea probably ok
 
recbg
 
the problem is I need oracle too : ...................................... (
 
cbg guys o/
Ubuntu is going mad. On Windows User.py never complain on executing like: python User.py However same execution on Ubuntu throwing the below error:
 File "User.py", line 4
    name: str
        ^
SyntaxError: invalid syntax
Should I ask on main site ?
 
type hinting for variables was added in .. 3.6 i think. Check your python version
 
eek my internet connection sucks
 
7:12 AM
@Arne Daymn! Mine is 3.5.2 on Ubuntu!
on Windows it is 3.7 :O
But situation like this can happen, right? What would be best way to code ?
 
just download a newer python version and run your code with that instead
 
ugh, one solution would be to upgrade on ubuntu
 
maybe top of your file, check sys version
 
why would it be bad?
 
@Arne Haha we said the same thing at the same time
@Arne True edited it out
 
7:15 AM
everyone really should be on 3.6 or greater as of now, it's stable and safer compared to the previous versions of python 3.
 
@Arne Won't that break existing code? Lets say if some API got removed in newer version.
 
There is a 3.8 now
 
i barely can use type hints
as i am in 3.6.0
 
@TheLittleNaruto nah, system uses python, which points to python2
 
7:16 AM
and 3.6.1
 
Sorry I am not able to relate.
@Arne It's not system it's my code which might be using some APIs which is present in 3.5.2 and got removed in 3.6. I mean this could be a case some day right ?
 
maybe, who knows. just try it.
 
Okay if that's what the universe want me to do; I'll do it.
 
it's just me, but sure =)
 
trying stuff and getting the results are always 100% correct results whereas guessing is much less
 
7:20 AM
B-)
 
It does look like it's a rough ride if you use new style syntax for typehints. here and here
Although that last one seems weird, it seems to imply the code should work without issues
 
7:39 AM
@TheLittleNaruto the solution is this:
always run python3
that is the name of the executable.
 
@TheLittleNaruto as a side note, python doesn't just randomly change its public api, deprecations are communicated beforehand. optimally, you read the what's new for your current version and ctrl-f for "deprecat". as long as you're not using anything listed there, you're save to upgrade to the next version
 
hello
I'm looking for more information about this issue bugs.python.org/issue34226
 
8:19 AM
cbg
 
@AnttiHaapala Oh I installed 3.7 but using python3 gives me same version i.e. 3.5.2
 
cbg y'all
 
Hello guys, I am using pytest and I get this error:
requests_mock.exceptions.NoMockAddress: No mock address: POST https://api.stripe.com/v1/charges
Is it there a way to skip stripe.com from being mocked?
 
@Arne Thanks for the link. Will add it to my bookmark. Do we have it added in the list of feeds of this room ?
 
I can't find it
 
8:29 AM
What is correct way to upgrade python on Ubuntu btw ?
 
Isn't the recommended approach to install a new env with the version of python you want to use?
 
True but prior to that we should have the version installed, right ?
 
apt-get update && apt-get upgrade?
 
Not working as in not upgrading Python to latest
 
then you'll have to compile it manually
 
8:38 AM
Okay
virtualenv is removed from python 3.7 ?
 
8:49 AM
You don't upgrade the system python
You will install them aside if needed
Then refer to exact version number
python3.7
My remark was generic in that the Unix command python should always refer to python 2 on the system, at least for now
 
I'm a fan of pyenv
 
I am not
 
@TheLittleNaruto you can link python3 to link to python3.7 .. or just use python3.7 to run your program
@TheLittleNaruto virtualenv is still there in 3.7
 
@Arne Yeah it's there, However when I create one env using this command: virtualenv -p /usr/bin/python3.7 sample_env it throws this error: gist.github.com/TheLittleNaruto/… Same is not the case for other versions.
 
how about python3.7 -m venv sample_env?
 
9:01 AM
That's what I did no ? Ok lemme try
Yeah it worked :O
Yay \o/
Watermelon Arne _/\_
 
9:39 AM
you're welcome =)
 
9:57 AM
@Arne you can't
 
@AnttiHaapala I remembered the same, but now that I reread pep 394 it sounds a lot more liberal which version python should refer to.
 
@Arne I am talking about practice.
@Arne Ubuntu uses Python3 only by default, and no Python 2 is installed by default. There has been 0 serious talk ever about changing what /usr/bin/python points to, because it wouldn't work.
no one except clueless users expect /usr/bin/python to be python 3 but there has been too many years of precedent of having /usr/bin/python to be compatible with Python 2.
 
wait, i might be misunderstanding. are you saying that usr/bin/python will in practice not refer to python3 because it needs to be compatible with python2?
@AnttiHaapala I only meant putting an alias into .bashrc
 
that is not a link
 
TIL =)
 
10:03 AM
@Arne yes that's exactly what I am saying. The PEP only exists because ARGH! linux decided to make /usr/bin/python point to python3
 
They should've just called it threethon
 
notice that prior to that pep, there were countless systems that do not even have /usr/bin/python2 or anything else besides /usr/bin/python.
 
Or pythree
 
If it does change, i imagine only a few distros would pick it up, and the transition would be slow
 
@RobertGrant or Phyton
 
10:04 AM
That's dangerous
Actually, calling it py3 would've maybe been genuinely better anyway
 
@RobertGrant What could go wrong... Muphry's law.
 
And only switched python to alias to it after the 2020 deadline
 
@AnttiHaapala is the tl;dr of the pep then something like "distro maintainers, don't install python2 or python3 and call it python. instead, install it and call it python2 or python3, and let python point to whichever you want"?
I'm having a hard time reading it, it just seems to repeat itself over and over
 
@Arne no.
 
=/
 
10:12 AM
It says:
Distributors may choose to set the behavior of the python command as follows:

        python2,
        python3,
        not provide python command,
        allow python to be configurable by an end user or a system administrator.
 
It says "if there's python it shoukd be 2"?
 
which means that "distributors are allowed to make the choice between these 4 and no one can assume anything."
 
Hmm, didn't remember that bottom line :P
 
but out of these 4 only items 1 and 4 work with legacy
thus, every single user who expects python to launch Python 3 is wrong.
... on certain platforms they might be "pleasantly surprised" however.
 
Aye, i think that pretty much sums it up best.
 
10:15 AM
ok, i get it
but I don't know what item 4 would look like
 
... and anyone who says "I am going to use Python 3 only when /usr/bin/python will launch Python 3" will be using EOL'ed Python in 4 months 2 weeks.
is the Debian universe mechanism
but it wouldn't really do any good in case of Python.
 
alright, that was interesting. thanks for the lecture
 
euphemism "whatever, go away" :D
 
ahhh, no!
sincerity just carries as bad as sarcasm over text
I was actually sincere
 
sarcasm: The ability to turn words into the exact opposite meaning, puzzling humans and machines for eons to come.
 
10:37 AM
I do not agree with the Related part at all. Commenting against the closure should be enough. Neither do I agree with the close vote reason needing a consensus. 3 close votes is enough. At first it can be that there is no [mcve] and it gets 2 close votes with that reason - and when it is provided, it becomes evident that there is [mcve] but the entire problem arises from a tpyo at which point closing as brainf4rt is the correct close reason. But you suggest we should leave these worst of the lot to wait for 5 close votes?! — Antti Haapala 1 min ago
 
The bottom line is, surely (re-cbg) that Python users should control their $PATH ro be sure they know what python means in their command line.
But still we see users who don't know which pip will run, and that python -m pip is more reliable anyway.
It's tough to watch once these things have become second nature, but I'm not sure people will ever stop tripping over these problems.
 
@holdenweb except with python -mpip it will run whatever pip it finds in the current working directory :'D
 
If you have modules that shadow important system-related packages that's a whole different set of problems.
 
@AndrasDeak this is the first time I agree with SOCORP rather than with Lundin :D
 
11:11 AM
Just spent half an hour with my first ever encounter with a pdf that "looks" correct but gets garbled text out in tika. Fun!
 
There are a lot of text pdfs where on highlight you get gibberish
 
@idjaw Thanks.
 
Aye, now Im just surprised that either i never came across one or never looked that closely. But it was quite interesting reading about PDF internals to finally make sense of it.
 
my most smartass C answer ever to these stupid homework assignments stackoverflow.com/a/57590305/918959
 
gives a standing ovation
 
11:27 AM
Stick it to the man
 
> Allowing the use of scanf and printf is a deliberate trap set by your teacher - there is no way of using them in an useful way without resorting to use of arrays, explicit or implied
 
12:27 PM
cbg
 
wim
@AndrasDeak does it mean the list of close voters is hidden from everyone except OP? that's weird.
 
12:43 PM
i have a for loop that runs 3 commands c1,c,2,c3 I need to run c1,c2,c3 serially but for loop iterations in parallel
for i in things:
  proc1 = Popen([c1], shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
  proc2 = Popen([c2], shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
  proc3 = Popen([c3], shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
But this does everything in parallel
 
@wim stackoverflow.blog/2019/08/20/upcoming-on-stack-overflow/… >3k rep users should be able to see it too
 
Is it possible to return the type hints for a class?
 
@Dodge type hints for its constructor? class variables?
 
>>> class Thing:
...     a: int
...     b: str
...     def __init__(self):
...         self.a = 1
...         self.b = 'Hello world!'
for a class like that
 
you can get rid of the type hints for Thing.a and Thing.b, they have nothing to do with the instance variables self.a and self.b
 
12:52 PM
Ok, I'm trying to answer a question where the user wants a dict with a given class attribute as the key and the type as the value
But they are probably as confused about this as I am :)
 
they can get it with Thing.__annotations__, but they should be told that their types/names only incidentally relate to the instance attributes
 
Got it
 
hmm, looking at pep 526 I might be a little off target with my strict distinction of class variable annotations and instance variable annotations. I can't find a question on SO, maybe I should ask one
 
1:08 PM
@Peilonrayz Awesome, thanks @Arne That might be a good idea, type hints became a thing with python 3.5, right? So there is not a ton of great content on the subject that I was easily able to find
 
function parameter annotation in 3.5, variable annotation in 3.6. I still can't manage to find out how to get the annotation for a variable that was type hinted like this:
class Thing:
  def __init__(self):
    self.a: int = 1
 
@Arne I don't think that's quite right.
 
yeah, I agree by now
 
The first answer that has been offered sort of highlights the difference between type hints for the instance attributes vs the class attributes but does not explain how to get the type for the class attribute (which the asker has not mentioned)
 
cbg
 
1:15 PM
cbg
 
@Dodge What do you mean by this? As in class Foo: bar: int = 1?
 
@Peilonrayz I mean that the first person to answer has given a snippet from the PEP to clarify:
class BasicStarship:
    captain: str = 'Picard'               # instance variable with default
    damage: int                           # instance variable without default
    stats: ClassVar[Dict[str, int]] = {}  # class variable
 
Ok, I don't see what the problem is cause get_type_hints seems to work fine for me. I'ma just go back to work.
 
@Peilonrayz but I now see that getting the type from that instance variable can be done with .__annotations__() but getting the type from the class variable (like in the example above) I'm not sure how to do. I apologize if I am being confusing, I really don't know much about this at all. I might be better served at this point to spend some time reading up on it.
@Peilonrayz Oh yeah, have not tried that, it probably works fine. I got lost in the weeds for a sec
 
> At runtime there is no way, annotations are never even stored at local scope. If you move it to class scope you can use typing.get_type_hints(Thing)
 
1:27 PM
cbg
 
cbg
 
OOf
-4
Q: "Could not convert string to float" when training my CNN

Gilbertohttps://imgur.com/MHCbFYZ << Here's the problem/code When I run my code the RAM goes up and the console shows error. What I have to do?

 
@Dodge chat.stackoverflow.com/transcript/message/47097701#47097701 that'll be the final word, by Ivan Levkivskyi himself
@BlackThunder feel free to post posts like these with a [tag:cv-pls]. or what was the point?
 
@Arne Its not 10 min. I thought the OP may edit their question
 
@Arne Thanks. I kind of get it now, if you have an unsinstantiated class you need to use `.__annotations__' but if you want the type hint info for an instantiated class you need to use what Peilonrayz suggested (it actually works for both the instantiated class and the corresponding class instance)
 
1:32 PM
@BlackThunder fair enough. although it doesn't look too promising
@Dodge and it seems that hinting types for instances should also be done in the class body and not __init__: stackoverflow.com/a/44962662/962190
I'll skip posting a question then, the answer is already out there and the problem was just me being too narrow minded ¯\_(ツ)_/¯
 
cabbage
 
cabbage folks!
 
Nice picture on a sunny day for your avatar @Arne
 
thanks =)
 
wim
1:58 PM
This needs more votes: stackoverflow.com/a/18090853/674039
 
@wim sounds like it
 
Does any have an idea why stackoverflow.com/questions/57593294/… print(f()+f()+f()) # -> [0, 1, 0, 1, 0, 1, 2] ? I know that's there mutable default argument, but why the [0, 1...] at the beginning?
 
2:16 PM
I suspect it's because f() + f() + f() is parsed as (f() + f()) + f(), so then the first two calls are evaluated before any addition happens, and the 3rd call is evaluated later
basically f() + f() + f() turns into a = f(); b = f(); c = a + b; d = f(); result = c + d
and I think I'm too lazy to post an answer, so if someone wants to post it in my stead...
 
I knew it was something how operators work
 
@Aran-Fey someone else was faster, they're now getting all that sweet HNQ rep
 
perfect. That's how I like my SO experience to be, upvote a question, upvote an answer and then return to watching youtube
 
and drop some knowledge in a chatroom every now and then, just to show that you could reap more rep if you wanted to =D
9
 
haha, I had to star that @Arne
 
2:27 PM
huh, SO down?
 
Yep, down for me
 
here too: Stack Overflow is currently offline for maintenance
 
Time to make a dinner :)
 
wim
3:05 PM
@AndrejKesely could have been a nice riddle
 
@wim Definitely something more interesting than the Javascript's banana thing from last time... I upvoted OP and the answer
 
[tag: cv-pls] - duplicate: changing list item seen in other lists stackoverflow.com/questions/57594419/…
 
wim
the question is somewhat contrived, I wonder if OP already knew answer :)
 
hey does anyone know how to use the .join function on a dataframe column that has some floats in it?
 
3:22 PM
POLL: (and potentially polarizing). Recently, someone I know IRL pronounce regex as rejects. I had always assumed to pronounce it as the first syllable of regular and the first syllable of expression (ray - hard G as in "gate" - ex). So the poll is Do you pronounce it as 1) ray - hard G - ex 2) rejects
 
3) regex phonetically
/regeks/ probably
 
ok, so we're exposing my lack of knowledge to convey phonetics... hmmm. I'll be back
 
See. But I do it with a soft G out of habit.
 
Note that I don't say "ray" in "regular"
 
for me re as in regular, and soft G for no good reason. and ex as in expression.
 
3:27 PM
for me, it's "reg" (as in "Reggie" - think back to the Archie comics) + "ex"
 
it's just like I say su dough in stead of su doo
even though I'm well aware of what sudo is short for lmao.
 
Yeah, I shouldn't have written "ray". Maybe "rƏ"
 
@Dair I can't in all good conscience say "sudo make me a sandwich" pronouncing it as "su dough"
 
@piRSquared That's fair, but it reminds me of pseudo...
 
3:31 PM
I think Randall Munroe says sue-dough. I know I do.
 
And now that I wrote that "out loud" I can't justify my feelings.
 
however, I knew what sudowoodo was before the pseudo...
 
@mrnovice what are you trying to do?
 
It's kind of a shame that the de-evolution of Sudowoodo is Bonsly. Missed opportunity to call it Woodo.
 
morning cbg
 
3:43 PM
cbg o/
 
@piRSquared 1
to be fair, I don't get thrown by the alternate pronunciation when others us it.
 
@piRSquared i have a dataframe. There is a column which has entries which are mainly strings, and there are a couple of floats
I am trying to join chunks of this column into single strings based on another column
but I get an error due to the presence of the floats
which cant be handled by the .join function
I am thinking a possible solution would be to run some code which forces all the entries in my column to be a string
not sure how to do that though
 
wim
@piRSquared more like "rejecks"
 
Hi all. Don't know if this is the right place for this or not, but here goes. I have an open Python question stackoverflow.com/questions/57589480/… which has been marked as a duplicate. I've flagged up why I think that is incorrect. If anyones free to take a look I'd appreciate it. I'm just as happy with a comment as to why it solves my problem as I would be with a vote to reopen.
Many thanks!
 
3:59 PM
how can you change every entry of a column in a dataframe to be a string
 
@MatthewBaker If your question was closed by a gold badge holder, you can ping them in the comments (like @davidism) - it won't autocomplete, but it'll work regardless
 
@Aran-Fey I didn't know that! Thanks for the help already!
Thanks @Aran-Fey I've sent added a comment with him on there.
 
4:28 PM
@mrnovice Make an MCVE with test data and you will be more likely to get useful feedback, specifically with pandas stuff
 
cbg
 
@mrnovice df['mycolumn'].astype(str)
... then df['mycolumn'].astype(str).str.join(', ') if you wanted them joined by commas. However, it may be quicker to use python functions rather than pandas ', '.join(map(str, df['mycolumn']))
 
recbg!
 
@pi
@piRSquared hi thanks for the help, I found the issue, it turns out there were some NaN values
in the column
so I removed those rows and now its working fine
 
5:18 PM
@ReblochonMasque stackoverflow.com/questions/57589480/… is absolutely not ready to be reopened, the duplicate about getting traceback information and not masking errors still applies
please re-hammer it, since you undid mine
 
5:35 PM
^ closed. (it's currently closed as no mcve though)
 
5:49 PM
@piRSquared since you don't seem to be offended, i'll be on your behalf
( ಠ_ಠ)–† rejecks
 
6:04 PM
@piRSquared I either pronounce it reg-eks or rej-eks
but never 'rejects'
 
hammer'd!
 
Thanks
Oh man, one of the 5 alter-egos around the world that manage to use my email address has done something really stupid and now my mailbox is getting rammed with junk :/
 
6:36 PM
"one of the 5 alter-egos around the world that manage to use my email address" yes, i can spot something really stupid already :P
 
My consolation is that I still potentially have their job contract and their mortgage application, depending on which cretin it was that used my email and not theirs. Some consolation
I've only managed to track 2 of them down. One by their phone number on an order sent to me, and the other with a declined mortgage application + credit score which allowed me to track her down on facebook. The latter wasn't actually her fault, her email ended in .ca and the office managed to use .com. Hopefully she sued them
 
I bet everything aside, this must feel kinda exciting to deduce :P
Sherlock would be impressed.
 
It has its ups and downs
I've been invited to a stag do in Vegas, for example
Secret Santa with people I don't even know, which I call Super Secret Santa. I've been asked, as a carpenter, to beat a church cross with a chain to give it a weathered look, invited to help lead a literacy class somewhere in the US, which I found the most hilarious considering either the sender or the person being invited to lead the class can't actually get an email address right
And in the case of the one I tracked down from a phone number, I apparently like buying women's clothes from time to time
 
6:53 PM
@roganjosh reminds me of the story of the guy who got invited to a stag by email-typo and actually went and then got featured on Jimmy Kimmel, I think
 
@inspectorG4dget yep, the similarity is spot-on... right to the point that they don't end up inviting me :P
 
ahhh, nuts
 
wim
@inspectorG4dget I was just about to ask if rj was that guy
 
rofl.
Sidenote: I've missed this channel. Glad to be back :)
 
The new me is decidedly in a worse position. I once apparently subscribed to a mailing list that had "dog of the week", which was apparently the "spunky terrier". The new me has debt and a fungal nail infection, based on the 50+ junk emails I received today. Like I said, ups and downs :P
 
7:00 PM
I'd deal with the fungal nail infection first. take care of yourself :P
 
It's a curve-ball. I mean, I can imagine companies sitting around waiting for you to land on the wrong site with debt issues.... the nail infection is niche :P
Companies was the wrong word. I'm legitimising the model in my head :/
Anyway, now that I've got over that, I can go download the code I dumped on OneDrive to create my embarrassing MCVE
 
wim
7:24 PM
"dog of the week" sounds ok tbh, where can I sign up
reminds me of that cat facts troll
 
See, I totally wish these things were real, but they clearly wouldn't bait a troll like that, but clearly people these days can do really dumb stuff... it just becomes a Möbius strip of thinking in my head :/
 
wim
def f(a, *b, c=1):
    pass
^ how do you do that in Python 2.7 ?
 
hack your way around it by manually checking kwargs
(or you know, drop support for python 2.7 :P Take the plunge wim!)
 
wim
the first step to dropping support for python 2.7 is making the code work cross-compat in the first place
 
Touche
Im afraid this one has nothing clean for python 2, quite sure on that.
 
wim
7:38 PM
bummer
and kinda weird that it's a syntax error in the first place, it doesn't seem to be ambiguous at all
 
heres an SO question on it
Aye, agreed. I'm glad they introduced it in python 3 in any case, I've actually made use of it in my own code a few times now
 
Hmmm, something isn't going right in making my MCVE in the translation from flask-sqlalchemy to just sqlalchemy.
 
Does the error go poof?
 
It's not an error, it's a really basic thing I'm trying to do, but I think I might have mistranslated now
 
Ah, i see.
 
7:44 PM
I'm starting to find it more and more frustrating to translate between the two. I have seen the criticism in here before of flask-sqlalchemy and I'm starting to agree
 
8:02 PM
I don't know if I've had any issues, but I've also not used it in a larger codebase where I didn't have complete control
 
Now my issue is coming from using a beta of Spyder so it's confusing me. When I dumped to CSV rather than trying to view a df in a console, it's fine
 
So, maybe your issue is Spyder then :P
 
As always
Irritatingly Detrimental Environment
 
Nah, I've found that issue now that threw me off. My question is super-basic, but I've nearly done with it now
The beta I have is hopeless at autocomplete (but hey, it was broken for me and many others in the previous version, with a statement that a fix was basically abandoned in that version) and borks dataframe prints. This is not an upgrade so far :P
Ok, the gist is here. I'm trying to get the child "name" attribute as an attribute on the parent object. I can grab the value with product_name = item.product.name but I don't think it's necessary to do that. I've gone through this that uses AS from different tables and tied myself in knots
Well, it's not a child, it's a one-to-one, and I just want the name of the product when I query the forecast. I don't want to have to use item.product.name. I've tried so many combinations from that documentation and I don't seem to be able to get it. It seems really basic but I can't seem to search for a solution or find it :/
Sorry, many-to-one. The forecast is on the many side since it's per-week
 
8:22 PM
You just want the forecast or the product name?
 
The forecast is per week, it has a foreign key to the product ID and I just want to pull over the product name each time that product ID appears in the forecast
 
or are you trying to create a df from the query? Cause that's also kind of a different thing
 
The docs I linked show addresses_1.id AS addresses_1_id,etc, as though it should be an attribute of the returned object... but it's not. I still have to do product_name = item.product.name (in my equivalent) and that doesn't make sense to me. Maybe I'm fighting the ORM. I'd expect something like item_product_name as an attribute of my query result
 
8:42 PM
I don't know whether the silence is because you're thinking about the problem or whether I'm totally approaching this the wrong way. If it's the latter, I'd be more than happy to hear it btw if there's a better setup
 
9:13 PM
It's because there are multiple ways to query a thing in sqlalchemy + flask-sqlalchemy
@roganjosh no I was just doing other things :P
 
Sure, but it screams to me that I am missing something fundamental
@WayneWerner I'll allow it... this time :P
 
What it's showing there is just the query that's actually being executed. It doesn't have anything to do that I can tell with how you actually pull stuff out of there.
What is it that you actually want to get?
It's not very clear from your gist :P
 
At the bottom, I have product_name = item.product.name. I want product_name as an attribute on what is returned from forecast = session.query(Forecast).join(Product).all()
@WayneWerner I definitely fumbled my words at the start, but go from this:
1 hour ago, by roganjosh
The forecast is per week, it has a foreign key to the product ID and I just want to pull over the product name each time that product ID appears in the forecast
And line 64 in my example
 
What's wrong with item.product.name?
 
That I have to loop to get it
 
9:27 PM
No you don't. You just have to get it from a forecast item.
 
Ok, maybe we are getting to the source of the issue
 
That loop stinks to me, it tells me I'm missing some understanding
 
L35 is what gives you the ability to access the product off of a Forecast item
 
Ok, so is there a way to configure the relationship or the join to avoid that loop, or is it actually best practice?
 
9:29 PM
I'm just really confused about why you're confused about the loop
you're looping over forecast items
do you realize that?
 
Yes, of course
 
okay, so what are you confused about when it comes to the loop?
 
it seems to me that the join itself, which could be executed in SQL could do the work of that loop
 
it is. But I think that I might understand what you're missing now...
 
So that I could have an object at the end that had a name without looping in Python
 
9:32 PM
You are asking for something like....
session.query((Forecast.c.id, Forecast.c.name, Product.c.name))
 
AttributeError: type object 'Forecast' has no attribute 'c', I don't know how to apply this
I thought that was for join criteria
 
I think you return a partial set of columns by adding a with_entities call.
 
Mmmm, I'm thinking of the core bits
 
The answer can span from "you're doing the right thing" to "you're bonkers" but I'm pretty sure my gist gives a repeatable example. I seriously just want the name for each product_id in the forecast, that's it.
It's a dumb question. A couple of times I've asked recently, it's been a bit over-thought. I'm not coming from a solid foundation of understanding.
 
9:48 PM
Ah, here we go
    forecast = session.query(Forecast.id, Forecast.product_id, Product.name).all()

    expanded = []
    for item in forecast: # <-- I want rid of this loop
        item_dict = item._asdict()
        expanded.append(item_dict)
   import pprint; pprint.pprint(expanded)
@roganjosh I think that's what you're after?
Obviously you'll want the rest of the columns in there
 
@WayneWerner in a way it is because I'll tentatively take it in your word that I'm doing the right thing despite the code comment on for item in forecast: # <-- I want rid of this loop :)
 
I mean... what else would you possibly do to convert a bunch of rows to a list?
you can use a list comprehension but that's not a different thing
I mean, fundamentally it seems like you're asking how to get a bunch of rows and turn them into a list without having to turn them into a list?
 
No
Well, yes
 
I'm really confused now
 
9:56 PM
By specifying the columns you don't have to do the item.product.id bit
the query is getting the list of things for you
add , echo=True to your create_engine
 
@WayneWerner great suggestion. I'm apologise if I've been frustrating through this
 
Ah! That's what I was looking for
 
I think I have a mental model that doesn't quite gel with how SQL works. Your responses will make me review that
 
forecast = session.query(*Forecast.__table__.columns, Product.name).all()
I mean, I think your model is right about how SQL works...
but fundamentally you have to get results from SQL into Python
you are either going to get back a scalar (i.e. 1 result)
or you're going to get back a "list" of results
 
^^ Which is what I want
 
10:01 PM
But they're not lists by themselves - if you want them to be lists you have to make them lists
whether that's forecasts = [item._asdict() for item in session.query(...).all()] or a for loop or something else
 
.all() makes a list, though?
I need to go to bed, I'll have a go at the raw SQL tomorrow because I think there is something missing. But I really thank you for your time, I hope it wasn't too frustrating
 
 
1 hour later…
11:37 PM
@Kevin Thanks for your help, but I'm using imap_unordered rather than apply_async, which doesn't have error_callback. That made me think, if apply_async has error_callback but imap_unordered, it might mean that the last one already kill its child process inside the pool, so the problem should be in other section of my code, and effectively it was.
Another process outside the Pool was not dying correctly when an exception inside the Pool happened, so I wrapped the Pool inside a try statement, and when on exception I manually kill the process, and then raise the error to the console.
I've learned several new things with this conversation, I'm glad and I thank you.
 

« first day (3231 days earlier)      last day (1720 days later) »