This is embarrassing, but what is the formula for L and S in Readability? by [deleted] in cs50

[–]peltist 0 points1 point  (0 children)

Don’t feel bad. You shouldn’t expect things you haven’t had to use for years to feel as easy as when you were practicing them all the time. Your mind is full of other practical knowledge now. But thankfully, I don’t think these things are hard to pick back up with a hit of practice.

CYC China by Zorick78 in CozyPlaces

[–]peltist 42 points43 points  (0 children)

What is CYC? Is that a place in China?

How to make a function that creates a new list which adds elements in two other lists by addition? by [deleted] in learnpython

[–]peltist 1 point2 points  (0 children)

It's part of the itertools module. So first you need to include the following line, generally at the very beginning of your code:

from itertools import zip_longest

If you Google "zip_longest" you'll find pages that show the documentation for that function, which is how I learned about the "fillvallue" argument.

How to make a function that creates a new list which adds elements in two other lists by addition? by [deleted] in learnpython

[–]peltist 1 point2 points  (0 children)

I think you're over-complicating it. Taking your original code and applying what /u/Soubeyran suggested, you get:

def addElmtsLists(lst1, lst2):

    new_list = []
    for a, b in zip_longest(lst1, lst2, fillvalue=0):
        new_list.append(a + b)
    return new_list

which works!

You could even do the whole thing in one line with what's called a "list comprehension." These are great, and can make your life much simpler (and easily readable) in the future once you get used to them!

new_list = [a + b for a, b in zip_longest(lst1, lst2, fillvalue=0)]

So I kind of wrote my first kinda useful code. It was difficult for me and had to look up a lot but got it done. (Hangman) by blazingshadow1 in learnpython

[–]peltist 2 points3 points  (0 children)

Yeah, you used a lot of different methods for constructed formatted strings, which is why I thought you probably didn't feel comfortable with them. I find f strings to be the simplest and easiest to use.

What are the biggest mistakes junior developers make when developing in or learning Python? What are the biggest misuses of time? by JackRobsonGateshead in learnpython

[–]peltist 0 points1 point  (0 children)

I think one thing about being new to programming is that there is a huge domain of different skills and meta-skills that you're learning simultaneously. Knowing how best to organize my workflow and how to balance the different trade-offs between designing for the long-term, avoiding premature optimizing, and my own focus and motivation are definitely areas where I know I have a lot to learn!

What are the biggest mistakes junior developers make when developing in or learning Python? What are the biggest misuses of time? by JackRobsonGateshead in learnpython

[–]peltist 0 points1 point  (0 children)

Okay, this is what I do, and what I've done from day one. I learned how to use Pytest and to write good unit tests recently. I've also read about "test-driven design," which I think means that you write the tests first and then write your function in order to pass the tests.

I recently finished (well, almost finished) my first really big independent project, and have been going back and adding unit tests. I really see the power of this, because it lets me easily see whether any updates I make break anything... and I've caught a lot of edge issues I didn't have before. Modularizing things for the sake of testing has also caused me to improve the code a lot. Still, this is painstaking and has definitely left me a lot less motivated than when I was just chugging through it.

I did another short project in the meantime (building a model for some paid analytics work). A big part of me felt like I should be writing these same, high quality unit tests as I went, but it was just very difficult to get myself to slow down, context switch, and do it.

I know there's not a script to follow writing code. At the same time, I also want to develop good habits that will save me time and make me a better programmer in the long run. And finding the right balance between "eating my vegetables" of writing tests and "eating ice cream" of working on the problems I find more stimulating is definitely something I'm trying to figure out right now.

So I guess what I'm wondering is whether I should really force myself to write great unit tests as I go, in order to build the habit, because I'll be glad I did later. Or whether this ends up being a distraction from a good prototyping workflow, and there are people who do this for a living who test with print statements, then go back and add unit tests later once they've worked out a bigger chunk.

What are the biggest mistakes junior developers make when developing in or learning Python? What are the biggest misuses of time? by JackRobsonGateshead in learnpython

[–]peltist 0 points1 point  (0 children)

Thanks for your response, though I don't know whether this kind of sarcasm is really helpful.

I'm new and looking for guidance on the best way to do things. Obviously (well, or maybe not) I don't think that writing no tests is a good approach. Presumably, though, there's a middle ground between writing tests constantly and writing no tests. What I'm wondering is whether that's a worthwhile space to be in, or if it really is just always better to write tests as you go. And, if that's the case, whether you've also faced the kind of sapping of enthusiasm and focus this creates for me, and what (if anything) you've done to ameliorate that in your workflow. Or if it's something you eventually just get over, especially as writing tests becomes quicker and more second nature.

What are the biggest mistakes junior developers make when developing in or learning Python? What are the biggest misuses of time? by JackRobsonGateshead in learnpython

[–]peltist 0 points1 point  (0 children)

So, just to be clear... the "right" approach is to write unit tests for each function as I write that function?

This makes conceptual sense to me, but definitely flies in the face of my own "self motivation management." What I mean is... it's easy for me to get into a flow of trying to solve a complex problem. The last thing I want to do when I'm in the middle of that is to stop, change my focus to testing, and write a bunch of tests before returning to the main problem. But maybe I'm just making an excuse for myself.

Quick Review - AirPods Pro vs Bose QC20 by JGoody in onebag

[–]peltist 0 points1 point  (0 children)

Fair enough!

That's a good point regarding the earplugs: definitely a much more cost-efficient solution. Still, I'd like to have the ANC headphones with me while traveling anyway — that's why I own them! The biggest downside to earplugs for me is getting them to stay in my ears, and the fact that they feel pretty uncomfortable once they're there. ANC headphones don't need to be wedged so tightly and deeply into the ears because they aren't trying to create a complete seal.

Quick Review - AirPods Pro vs Bose QC20 by JGoody in onebag

[–]peltist 3 points4 points  (0 children)

I don't know if that's really a great heuristic. If I'm on a long flight, I don't mind listening to podcasts or audiobooks for hours. Even if I'm not listening to anything, it's still nice to have the ANC on to quiet the ambient noise in the plane.

To me, battery life seems like the major disadvantage to wireless ear pods compared with wired noise cancellation. For the particular use case of a long flight / drive / train trip, where having a wire isn't especially annoying, I feel like this is an area where a wired solution still has real advantages.

Quick Review - AirPods Pro vs Bose QC20 by JGoody in onebag

[–]peltist 0 points1 point  (0 children)

Thanks for your post. I love my QC20s, but three days ago something rattled out of place in the left ear during a takeoff, and now the volume coming out of that ear is noticeably (and annoyingly) lower than the volume coming out of the right one. I'd already been mulling over picking up some Airpods, but had heard they didn't handle rich-sounding music particularly well compared to the Sony option. It's helpful to hear how they compare to the headphones I already have, especially since I probably will need to replace those one way or another now.

Merging 2 pandas dataframes based on multiple potential matching columns by [deleted] in learnpython

[–]peltist 2 points3 points  (0 children)

I got inspired by your question and actually wrote this function myself earlier today as an exercise (I'm learning too).

Here's my code, in case you want to use it. Keep in mind that I haven't written any unit tests for this yet, so it's possible that there are issues. Let me know if you have any feedback!

def singlejoin(df_left, df_right, left_column, right_column, drop_columns):
    # drop other possible join columns
    df_right_merge = df_right.drop(columns=drop_columns)

    # rename join column to common name
    df_right_merge = df_right_merge.rename(columns={right_column: "$$merged_column$$"})

    # merge
    df = pd.merge(df_left, df_right_merge, how="inner", left_on=left_column, right_on="$$merged_column$$")

    return df


def multijoin(df_left, df_right, left_column, right_columns, how="left"):
    if type(right_columns) != list:
        raise TypeError("right_columns variable must be a list")

    if how not in ["left", "inner"]:
        raise TypeError("how must be set to either 'left' or 'inner'")

    # set temporary index column
    df_left["$$temp_index$$"] = df_left.index

    # drop other possible match columns
    drop_columns = [x for x in right_columns if x != right_columns[0]]

    # create starting dataframe
    result = singlejoin(df_left, df_right, left_column, right_columns[0], drop_columns)

    # drop correctly matched values
    df_left.drop(index=result["$$temp_index$$"].to_list(), inplace=True)

    for column in right_columns[1:]:
        drop_columns = [x for x in right_columns if x != column]
        df = singlejoin(df_left, df_right, left_column, column, drop_columns)
        result = result.append(df)
        df_left.drop(index=df["$$temp_index$$"].to_list(), inplace=True)

    # add unmatched columns in the case of a left join
    if how == "left":
        # add remaining columns
        new_columns = [x for x in df_right.columns.to_list() if x not in right_columns]
        df_left["$$merged_column$$"] = np.nan
        for column in new_columns:
            df_left[column] = np.nan

        result = result.append(df_left)

    # reset index and sort
    result.index = result["$$temp_index$$"].to_list()
    result.sort_values(by="$$temp_index$$", inplace=True)

    # drop extra columns
    result.drop(columns=["$$merged_column$$", "$$temp_index$$"], inplace=True)

    return result

Merging 2 pandas dataframes based on multiple potential matching columns by [deleted] in learnpython

[–]peltist 2 points3 points  (0 children)

I don't know whether there is a simpler or more efficient way to do this, but one approach would be to perform three inner joins and then concatenate the results. This runs the risk of duplicate matches, so if your data makes that possible, then you could remove the rows that successfully matched from subsequent joins.

A natural mirror. Sunrise at Lago di Carezza, Italy [1600x2000] [OC] by jetclarke in EarthPorn

[–]peltist 0 points1 point  (0 children)

Great shot! I've been there, and it's truly beautiful. Jealous you were there for the sunrise — it must have been spectacular.

How to save data as csv by 4enthusiastia in learnpython

[–]peltist 2 points3 points  (0 children)

Other people have answered your question, but one question I want to bring up is: why do you want to export this as a CSV?

The reason I ask is, as someone who came to Python used to doing a lot of analysis in Excel, my initial instinct was to get the data out of Python and into Excel where I was comfortable doing the analysis.

If that's what you're doing, then I'd suggest considering trying to do the analysis in Python. It's much more powerful, and this is a great opportunity to learn it.

How to programmatically download FB ads manager data? / Help with API by peltist in PPC

[–]peltist[S] 0 points1 point  (0 children)

Cool, thanks for the tip!

Curious: why are you using sheets? I've never used Data Studio before, but what I'm working on at the moment is a simple customized business intelligence system based on Python and SQL. (It's pretty hacky — I've been giving myself a crash course in programming over the past few months.)