|
Falcon2001 posted:So I have a bit of a best practices question. In the event that you have a function that mutates a data structure like a list/etc, is it best practice to return the mutated version, or to just make it clear in the docstring that the function mutates the object? For the sake of argument, let's assume that this isn't a library or other externally facing function and is pretty tightly bound business logic specific to a situation, where you're separating code out into functions to improve readability/etc. My opinion is that the first is better over the second example. All things being the same (documented mutation behavior, etc) returning a value from a function immediately makes me think away from mutation. In your second example its fine since you're reassigning to the same variable, but if you weren't (or your peer didn't read the docstring and assumed the return was a copy) and assigned it to buttless_big_dict, you now have two references to the same dict. The first example is less ambiguous in that it doesn't return anything (or if it does, you don't care because you're not capturing it in a variable), and looks closer to a function that just does something with or to the arguments you passed. As a bonus, here's a third option I'd prefer: Python code:
|
# ¿ Mar 22, 2022 20:52 |
|
|
# ¿ May 11, 2024 10:59 |
|
Falcon2001 posted:Thanks for the feedback! I think the biggest takeaway is that it should be clear, and I properly type hint all my functions/etc, so it's clear that this doesn't return anything (-> None), which means I think I'm going in the right direction. Godspeed goon
|
# ¿ Mar 22, 2022 21:01 |
|
Protocol7 posted:Thought someone would get a kick out of this terrible fstring I wrote today. Apparently this works Python code:
|
# ¿ Mar 23, 2022 23:58 |
|
Today I learned that the Ellipsis ... in python is an actual object that can be used to mean I don't care
|
# ¿ Mar 24, 2022 12:28 |
|
worms butthole guy posted:Hey all, I have another simple question that i'm hoping someone can point me in the right direction. When you say a JSON key in python do you mean a dict? Assuming HubspotData is a dict (or at least easily convertible to one) the preferred way to look-before-getting a key is HubspotData.get(“key”, default_value). If expressions (the ternary operator equivalent in python) will also work with a caveat: if the value is falsy you won’t get what you expect. ef;b
|
# ¿ Apr 27, 2022 18:01 |
|
12 rats tied together posted:Other people have been doing a good job of fielding these questions, but I wanted to try and provide a zoomed-out explanation for you real quick because virtualenvs are one of those things that are easiest to understand if you start at the bottom (IMO), but it's hard to find resources online that actually start at the bottom and don't presume some foreknowledge on your part. Good info here. I use VSCode personally and can expand a bit here. To activate a virtual environment in vscode you need the python extension installed. Then CMD/Ctrl + Shift + P to pull up the command palette and select Python: Select Interpreter… then open your virtual environment which should be listed there. If it isn’t try the Developer: Reload Window command first from the command palette. Your vscode should now recognise your virtual env and give you nice autocomplete etc for your locally installed packages.
|
# ¿ Apr 27, 2022 20:33 |
|
D34THROW posted:Probably more too much docstring. I use numpydoc-style docstrings because I have an easier time visually parsing them, so I have parameters, returns, notes, and examples sections. It's a template function to generate the class attribute for an HTML tag that depends on the session["ADMIN_MODE"], essentially a shortcut for a big jinja if/else block that exists to assign the proper class for admin or user mode. Python is pretty flexible when it comes to how exhaustive the docstring needs to be. I tend to use the 6 month/peer rule: how complicated would it be to use this function as me 6 months from now/as another user or peer? If there are a fair amount of gotchas or lots of parameters, you’ll want a good exhaustive docstring, there’s no real upper limit to how helpful you want to be here. Otherwise, you’ll find that a succinct passage is enough, especially for functions that take none or one or two params and are like 5 lines in length. If you have empathy for your fellow users then you’ll find the right balance no problem. (e: docstrings in a lot of pythons own api can be pretty succinct and short if you take a look)
|
# ¿ May 19, 2022 14:42 |
|
SirPablo posted:Someone explain or direct me somewhere... What does the "-> str" do when defining the function? -> at the end of the function hints at the return type. -> str means it returns a string.
|
# ¿ May 19, 2022 14:44 |
|
SirPablo posted:Ah interesting. So it's almost a comment of sorts, doesn't actually do anything functionally by itself. Yeah, python is still ducktyped so you can go against your own hints. Like D34THROW says, they can be used by libs like mypy to actually enforce types by giving you errors on a pre-build.
|
# ¿ May 19, 2022 17:01 |
|
Hughmoris posted:Rookie question as I try to wrap my head around Docker and containers: The easiest way is to skip building images everytime with new versions of your script. To do that you bind the dir of your script with -v into a volume the container can reach. The entrypoint command will always be the same too. Then when you spin the container up it will always be the same image just running the script you bound from your host machine.
|
# ¿ Jul 4, 2022 16:55 |
|
Gangsta Lean posted:You’re probably missing out on a ton of stuff by assuming that there’s an interior list. This form is probably way more common. i cannot wrap my head around this form e: well this is understandable actually. last time i came across this double for was for zipping two things or whatever and didn’t get it at the time
|
# ¿ Jul 6, 2022 12:37 |
|
SirPablo posted:Is there a way to scrape threads? Sure, you can just make the same requests as your browser does with requests and scrape the content with beautifulsoup. Id drop a line to astral first though.
|
# ¿ Jul 31, 2022 19:59 |
|
Falcon2001 posted:Is there a simpler way to handle this interaction? I'm finding I'm doing this a reasonable amount. You can use the built in getattr, similar syntax to dict.get("key", default). Python code:
Armitag3 fucked around with this message at 19:14 on Aug 31, 2022 |
# ¿ Aug 31, 2022 19:09 |
|
The March Hare posted:I've been in OPs position before when writing systems to ingest absolutely insane API data. I'm getting flashbacks to ingesting SOAP requests - that of course could have been any of dozens of operations - in a single endpoint, which meant a conga line of null checks because of course every single param was optional to support any arbitrary combination. We couldn't count on anything.
|
# ¿ Aug 31, 2022 21:13 |
|
FISHMANPET posted:I'm writing a Python module, and I've gotten myself stuck into some circular dependencies. I've found some "basic" stuff about fixing that, but I seem to have gotten myself into a deeper pickle. Basically, I have a User class, and that User class has methods that will return a Group. And I have a Group class that has methods that will return a User. And I'm trying to define these in separate files, otherwise this is gonna be some massive 2000 line single-file module. I've finally just "thrown in the towel" so to speak and stopped importing at the top of the file, and instead am importing only when a function is called, but that doesn't feel great either. look up lazy loading or lazy importing. Basically in this kind of circular dependency you’d want to only import the symbols when actually needed, i.e. when you call the methods.
|
# ¿ Apr 10, 2023 22:21 |
|
rowkey bilbao posted:Anyone familiar with Pydantic v2 ? Try adding the @classmethod decorator to your validator function, then you should have access to cls as you're expecting. Python code:
|
# ¿ Sep 4, 2023 18:34 |
|
It's called fluent interface design, and it's not a bad idea at all, especially applied to situations where the flow can read logically from one operation to the next.Python code:
|
# ¿ Sep 25, 2023 23:36 |
|
from them import frustration
|
# ¿ Sep 27, 2023 00:22 |
|
LochNessMonster posted:Is anyone doing the advent of code by any chance? Yeah this is a common trip up right on day one that got me too. Without giving the game away here’s two things you should be aware: * some words might overlap (“twone”) * regex-replacing, given the above, might give you two results depending on which direction you do it from (“2ne” vs “tw1”) Don’t give up the elves need you
|
# ¿ Dec 12, 2023 16:46 |
|
LochNessMonster posted:Thanks, I didn’t take the overlap into account. Coming up with a solution is going to be challenging, but interesting. My solution involved reversing the string (I’m doing it in C++ this year). When I ran it with the string already reversed, I got different results and that’s when i realised that it matters which way you parse the string from first.
|
# ¿ Dec 12, 2023 17:04 |
|
Zoracle Zed posted:Maybe check out the 'sliding_window' iterator from the itertools recipes, which continues to drive me insane for not being importable but instead requires copy-pasting. Hey now thanks for this
|
# ¿ Dec 13, 2023 00:20 |
|
Vulture Culture posted:Other than the input variable shadowing a builtin, which has the potential to create gnarly bugs later on, these aren't the kinds of things new devs, or arguably any devs, really need to spend cognitive energy on. (IMO, all internal utility function names are bullshit no matter how much you pretend they aren't. Beyond a small handful of developers, none of them will ever see actual reuse.) I still avoid using id as an attribute of a model because of that best practice, but yeah I don't think I've ever seen id() being used in the wild.
|
# ¿ Feb 16, 2024 13:46 |
|
|
# ¿ May 11, 2024 10:59 |
|
Deadite posted:Perfect, thank you. And I'm sure that team doesn't do any unit testing. I got pulled in to help out because they were running behind. At the end of the month this program will not be my problem again until an ironic reorg forces me to maintain it. What version of Python are you running this on? (Lol I know, it's probably going to be 3.8) I ask just on the offchance you might be able to leverage pattern matching to make this less of a rats nest. (https://docs.python.org/3.10/reference/compound_stmts.html#the-match-statement)
|
# ¿ Feb 25, 2024 17:59 |