|
ArcticZombie posted:I'm doing something wrong/misunderstanding how to use asyncio. I want to read from stdin, start a task based on that input and be able to receive more input and start more tasks while waiting for that task to complete. But the first task seems to be blocking and I don't understand why. Here is an distilled example (with load simulating a task taking some time): A coroutine blocks every time you use await. Although they allow other coroutines to run at those times, they nevertheless themselves run top-to-bottom. So, your while loop never returns to the top before the second await returns. For your desired behavior, the call to readline must be the only await within the loop. You need to schedule the execution of a coroutine without awaiting it. This can be done with futures. Something like: Python code:
|
# ? Jan 31, 2018 18:32 |
|
|
# ? Jun 5, 2024 19:55 |
|
The "tasks" are network IO, I wanted to fire them off as quickly as input came in. I was under the mistaken belief that at the await, the event loop would start another listen coroutine or do some other magic to allow the loop to continue processing input while the original coroutine was blocked (thinking about how this would work now, this sounds dumb), so thanks for putting me straight on that.
|
# ? Jan 31, 2018 20:57 |
|
I am trying to understand what I am doing wrong with a Pandas merge of two data frames:Python code:
|
# ? Feb 3, 2018 04:09 |
|
Jose Cuervo posted:I am trying to understand what I am doing wrong with a Pandas merge of two data frames: Is df_two unique on ID, Date?
|
# ? Feb 3, 2018 04:28 |
|
vikingstrike posted:Is df_two unique on ID, Date? Are you asking if all the (ID, Date) tuple combinations in df_two are unique? If so, yes. df_two was generated using groupby where on=['ID', 'Date'].
|
# ? Feb 3, 2018 15:52 |
|
Jose Cuervo posted:Are you asking if all the (ID, Date) tuple combinations in df_two are unique? If so, yes. df_two was generated using groupby where on=['ID', 'Date']. Er, sorry. Unique was a bad word. In df_two does (ID, Date) ever include multiple rows?
|
# ? Feb 3, 2018 16:06 |
|
vikingstrike posted:Er, sorry. Unique was a bad word. In df_two does (ID, Date) ever include multiple rows? No, because df_two was generated using a groupby statement where on=['ID', 'Date'] - so every row in df_two corresponds to a unique ('ID', 'Date') tuple.
|
# ? Feb 3, 2018 16:49 |
|
I have another newbie question. I have to read a text file for an activity selection problem and am having trouble getting the data from the file. The file format lists the number of activities, followed the the activity id number, start time, and finish time. In this instance there are two sets of activities:code:
code:
|
# ? Feb 3, 2018 18:55 |
|
wasey posted:How do I assign each line element to an object in Python
|
# ? Feb 3, 2018 19:03 |
|
Jose Cuervo posted:No, because df_two was generated using a groupby statement where on=['ID', 'Date'] - so every row in df_two corresponds to a unique ('ID', 'Date') tuple. Try the indicator flag on the merge and then use it to see if it might lead you to where the extra rows are coming.
|
# ? Feb 3, 2018 19:03 |
|
Cingulate posted:First of all, and apologies if this is a dumb question, but are you perfectly sure you actually do want that? Why? I'm the dumb one here, trying to learn Python on the fly for a class and it has been rough. I'm not sure that I want to do that, I just want to make sure that an activity's start and end time are not separated after I sort them by start time. I'm able to put the number of elements in a set in one list(11, 3) and the elements themselves into another list, but I don't know how to proceed from there
|
# ? Feb 3, 2018 19:14 |
|
wasey posted:but I can't seem to properly assign the info. Thanks for helping out How have you been trying to do it so far?
|
# ? Feb 3, 2018 19:14 |
|
wasey posted:
You need to work out your plan first. Basically (if I'm reading this right) what you want to do is:
and the activity parsing bit goes
hopefully that makes it a bit clearer it's the "do something with that data" where you need to store your activities You might not want to use classes, but only because it's not 'pythonic' - there's nothing wrong with using them while you get your head around the problem. It's just there are simpler tools you can use that will do the same job here (better, even) edit- it might help to explain exactly what you want to have at the end of this. How should it be organised? Do you need to store the 11 and 3, what do they represent? baka kaba fucked around with this message at 19:24 on Feb 3, 2018 |
# ? Feb 3, 2018 19:22 |
|
wasey posted:I'm the dumb one here, trying to learn Python on the fly for a class and it has been rough. I'm not sure that I want to do that, I just want to make sure that an activity's start and end time are not separated after I sort them by start time. I'm able to put the number of elements in a set in one list(11, 3) and the elements themselves into another list, but I don't know how to proceed from there Once you have the data in either format, the sorting will be absolutely trivial (literally df.sort()), but it would be a bit more complicated to get the data in there due to the lone "3" in the 4th to last line. So really, it depends a bit on what exactly you want to do.
|
# ? Feb 3, 2018 19:38 |
|
Cingulate posted:What do you want to do with the data? is it literally only sorting? Because if you want to do anything more with that, you'll probably want to use Pandas, or at least Numpy. It looks like the data is such that any line with 1 integer is just a count of the number of subsequent rows until the next single-integer line wasey if you wanted to use classes you could do something like this: Python code:
|
# ? Feb 4, 2018 01:11 |
|
wasey posted:poo poo code I took the advice of everyone in the thread and went with the select-all -> delete method and started over. I made things overly complicated and was trying to manipulate a list of list of lists (without fully realizing it) and things went downhill from there. Thanks to all to chimed in
|
# ? Feb 5, 2018 17:33 |
|
vikingstrike posted:Try the indicator flag on the merge and then use it to see if it might lead you to where the extra rows are coming. Just wanted to say you got me thinking about having duplicates, and df_one actually had non-unique ('ID', 'Date') tuples, so that is where the problem was. Thanks for the help.
|
# ? Feb 5, 2018 20:20 |
|
If I have a list of numbers, like so: [0, 14, 18, 20, 36, 41, 62, 70, 72] And I want to extract all longest possible lists from it where no two consecutive items are more than, say, 5 apart: [[14, 18, 20], [36, 41], [70, 72]] It's not hard to construct a loop to do this with a few conditionals, but is there an elegant python solution?
|
# ? Feb 8, 2018 17:32 |
|
I think you'll have to post your code to know if there's a more elegant way, but I don't think Python really encourages anything besides the ol' for loops in this case Some languages have some kind of window function, which you could use to produce a sequence of each element and its successor (so in your case (0, 14), (14, 18), (18, 20) etc). That way you get a stream of items and the thing you need to compare them to, and you can start to group them based on whether they meet your rule or not. Python doesn't have this though, for some reason It does have a reduce function but again, I'm not sure I'd exactly call it elegant here - it's definitely not as Pythonic (i.e. readable to general Python users) as a bunch of loops and conditionals doing the same thing Python code:
|
# ? Feb 8, 2018 21:29 |
|
Wallet posted:If I have a list of numbers, like so: Unless that first term is always supposed to be ignored, it would seem that your output list should start with a [0]. e: No 62 either. Are you just ignoring all singleton lists? Dr Subterfuge fucked around with this message at 23:57 on Feb 8, 2018 |
# ? Feb 8, 2018 23:53 |
|
baka kaba posted:I think you'll have to post your code to know if there's a more elegant way, but I don't think Python really encourages anything besides the ol' for loops in this case baka kaba posted:
Python code:
Dr Subterfuge posted:Unless that first term is always supposed to be ignored, it would seem that your output list should start with a [0]. Wallet fucked around with this message at 00:23 on Feb 9, 2018 |
# ? Feb 9, 2018 00:15 |
|
The more pythony way might be to have a generator that yields lists (basically consumes the iterator, adding each item to a list it's building until the item is too big, yields the old list and creates a new one for the item). That way you can feed that into another generator that filters out the single lists, or have the first generator just not yield a single item list
|
# ? Feb 9, 2018 00:28 |
baka kaba posted:The more pythony way might be to have a generator that yields lists (basically consumes the iterator, adding each item to a list it's building until the item is too big, yields the old list and creates a new one for the item). That way you can feed that into another generator that filters out the single lists, or have the first generator just not yield a single item list I agree. I would do it like this: Python code:
IN: Python code:
[[14, 18, 20], [36, 41], [70, 72]] Eela6 fucked around with this message at 00:36 on Feb 9, 2018 |
|
# ? Feb 9, 2018 00:32 |
|
Eela6 posted:
Now using a modified version of this that self-filters (the numbers are indexes and can't be negative, though that's a good catch). I had no idea yield or generators were a thing. I did a little reading which left me slightly puzzled, but stepping through it made it a lot clearer what's going on. That was really interesting, and will probably be useful in a lot of other contexts. Thanks!
|
# ? Feb 9, 2018 01:41 |
|
I was thinking more likePython code:
You can do a length check before you yield (so you only get lists with 2 or more items), but there's two problems - one is that you have to put that code in there twice (you're yielding in two different places), and it also ties that implementation detail into the grouping function. It doesn't just split your list into groups where each item is close to the last, it also does another job of removing lists below a certain size. Personally I'd argue those are two different things (you might not even want the filtering all the time, or you might want to change the size limit), so it makes sense to do that separately, running it on the results of the grouping function The nice thing about generators it's they're on-demand, so you can put them together like a pipeline. Instead of producing a full list of groups, and then producing another list where some of those groups are missing, that code up there iterates over big_groups, asking it to produce a single group each time. (big_groups is a generator comprehension, like a list comprehension but in ( ) so it produces a generator.) So in turn, big_groups asks grouped for an item, which it yields when it's built one. If big_groups discards it (because it doesn't meet the 'big enough' condition) then it asks for another one, until it has something it wants to yield That doesn't necessarily matter, but you can see how you can set up a big chain that yielded items have to pass through, getting tweaked or dropped or whatever, creating a machine that produces single, finished items on demand. And that can be good for working with very large amounts of data, or something slow where you want to drop things as early as possible if you're not going to be using them later in the pipeline baka kaba fucked around with this message at 02:36 on Feb 9, 2018 |
# ? Feb 9, 2018 02:28 |
|
Your approach is much more readable, at least to me.baka kaba posted:You can do a length check before you yield (so you only get lists with 2 or more items), but there's two problems - one is that you have to put that code in there twice (you're yielding in two different places), and it also ties that implementation detail into the grouping function. It doesn't just split your list into groups where each item is close to the last, it also does another job of removing lists below a certain size. Personally I'd argue those are two different things (you might not even want the filtering all the time, or you might want to change the size limit), so it makes sense to do that separately, running it on the results of the grouping function I take your point about them being different functions, though in this case there isn't really a context where you'd want length 1 groups. You can do the filtering without duplicating the code twice with a little tweaking (only one extra line of code, I think?): Python code:
baka kaba posted:That doesn't necessarily matter, but you can see how you can set up a big chain that yielded items have to pass through, getting tweaked or dropped or whatever, creating a machine that produces single, finished items on demand. And that can be good for working with very large amounts of data, or something slow where you want to drop things as early as possible if you're not going to be using them later in the pipeline Yeah, there's some combinatorics stuff I was messing with the other day that this approach will probably make possible, where before it just ate all of my memory and poo poo its pants.
|
# ? Feb 9, 2018 03:39 |
|
Wallet posted:I take your point about them being different functions, though in this case there isn't really a context where you'd want length 1 groups. Sure, that works! I feel like you're introducing a lot of complexity just to have a single yield statement in there though, and it's less clear what job it's actually doing. Personal preference obviously And the thing with putting the filtering outside... I guess it's more of a functional approach, where you're encouraged to write smaller, simpler single-task functions and then compose them together - not necessarily because you're gonna use them in isolation too, just that it's really readable when you look at each one or the code that chains them together It's great for writing pipeliney stuff, especially if you name your functions well, because you can see exactly what it's doing as a set of steps - like I had to infer you were dropping those groups by the example result you posted, but adding a line that filters them out makes it explicit, and you don't need to rely on comments either. Personal preference again of course!
|
# ? Feb 9, 2018 05:14 |
|
I got to exercise 49 of Learn Python the Hard Way before deciding to ditch it and skip the last 3 exercises. I've picked up Python Crash Course and now up to chapter 8 (functions). Just a very quick question, is there a more Pythonic way of doing this, given my beginner level? I'm especially thinking of the way that I added "The Great" to each string in make_great(). Python code:
Sad Panda fucked around with this message at 17:55 on Feb 9, 2018 |
# ? Feb 9, 2018 17:36 |
|
Sad Panda posted:I got to exercise 49 of Learn Python the Hard Way before deciding to ditch it and skip the last 3 exercises. Python code:
Also, don't do this when looping over the elements of a list: Python code:
Python code:
|
# ? Feb 9, 2018 17:41 |
|
If that's not clear, instead of replacing each string in the list with a new "great" version, you're just creating a new list by taking elements from the original, doing something with them and sticking the results in your new list. LikePython code:
Also this is a little advanced, but you can print all your magicians in one line: Python code:
Not saying this is better in this case, but it's good to know!
|
# ? Feb 9, 2018 17:58 |
|
Thermopyle posted:Also, don't do this when looping over the elements of a list: Thanks! Using 3.6 and f-strings are the best thing in the world. What is the reason for enumerate() instead of the iterating through i like I did it? More efficient algorithm? Just more Pythonic? Sad Panda fucked around with this message at 18:06 on Feb 9, 2018 |
# ? Feb 9, 2018 18:02 |
|
baka kaba posted:Also this is a little advanced, but you can print all your magicians in one line: Fab! That definitely is better and a wonderful use of *args. Saves a line as that function gets shortened to Python code:
|
# ? Feb 9, 2018 18:05 |
|
Sad Panda posted:What is the reason for enumerate() instead of the iterating through i like I did it? More efficient algorithm? Just more Pythonic? More pythonic because it's more explicit. You usually see programming beginners and programmers who are used to C-ish languages using the range way to iterate through an iterable.
|
# ? Feb 9, 2018 18:07 |
|
Another way you can look at it is, usually you do for thing in stuff to get a sequence of items, easy. If for whatever reason you want to number those items too, just call enumerate and it'll handle the counter and pass you the number with each item. The work's already done for you, there's less chance of making a mistake, it's clear what's happening, and if you don't unpack it as count, item then you have a tuple ready to pass into something else, you don't need to build it yourself
|
# ? Feb 9, 2018 18:33 |
|
When I was learning Python basics, it took me a long time to realise that when using a list comprehension, you can use any name in the for part. I saw examples of [letter.lower() for letter in name] and assumed you hadto use letter if you were iterating through a string. I always try to avoid doing callable/in-place stuff in a list comprehension eg [print(letter) for letter in name]. Is there any particular reason to avoid this? It seems to be more concise than the indentation of a for-loop.
|
# ? Feb 9, 2018 19:43 |
|
Is Python packaging really a shitshow of deprecated and inconsistent standards? I was trying to figure out the right want to conditionally install module dependencies in a wheel based on OS and Python version. I got to this: https://hynek.me/articles/conditional-python-dependencies/ According to this, all the ways of expressing stuff in extras_require is, like, wrong. I found a means that works with what we are using internally now, but this smells like it'll all fall apart 6 months from now. Over the past few years, my general experience has been: 1. Use easy_install! 2. Don't use easy_install! Use pip! 3. Use wheel! 4. Wait, no, use pip to install wheels! 5. Create wheels using this standard. 6. No! Create wheels using that standard!
|
# ? Feb 9, 2018 19:53 |
Slimchandi posted:I always try to avoid doing callable/in-place stuff in a list comprehension eg [print(letter) for letter in name]. Is there any particular reason to avoid this? It seems to be more concise than the indentation of a for-loop. Yes. You shouldn't use a list comprehension for it's side effects. A list comprehension should be used to create a list. In fact, generally speaking, any function which had side effects shouldn't be used in a comprehension; they're explicitly for a functional style of programming; What you're talking about is the equivalent of these lines of code: Python code:
IN: Python code:
pre:w o r d [None, None, None, None]
|
|
# ? Feb 9, 2018 19:57 |
|
Rocko Bonaparte posted:Is Python packaging really a shitshow of deprecated and inconsistent standards? Yes. Blame history. Eela6 posted:Yes. Everything you said is correct, but I just wanted to point out that the poster said "callable", and using a callable is fine in a list comprehension, it's just that the particular example the poster used wasn't great. (of course, it could be the reverse and the example was on-point and the word "callable" wasn't exactly what he meant)
|
# ? Feb 9, 2018 20:28 |
|
Anybody had any issues with PyCharm skipping over breakpoints when debugging? My Google searches have failed me, and it's getting super annoying because I can't figure out how to replicate the issues.
|
# ? Feb 9, 2018 20:32 |
|
|
# ? Jun 5, 2024 19:55 |
Thermopyle posted:Yes. Blame history. They used 'callable' as a synonym for in-place, so I assumed they meant 'mutable', given the example.
|
|
# ? Feb 9, 2018 20:49 |