|
We’re pretty good about hinting in first party code but it all falls down as soon as you use a third party library for which there are no types. Fine for Flask/Django/whatever other popular library but just lol if I try to get anyone to write our own type packages for third party deps we use.
|
# ? Sep 3, 2023 02:26 |
|
|
# ? May 28, 2024 16:21 |
|
Good third party packages like numpy tend to use good type consistency Good groups too
|
# ? Sep 3, 2023 02:35 |
|
Generic Monk posted:Good point Using pycharm I dropped a breakpoint on the line where it is meant to query the user from the database but it never actually pauses the execution, so it looks like that isn't even running. So I would assume it's a problem with the form validators because of the validate_on_submit conditional, but those seem to be working ok. Am I missing a trick?
|
# ? Sep 3, 2023 18:57 |
|
Keep dropping in breakpoints until you find where the execution path goes. Might have to dive into framework/library code to understand it.
|
# ? Sep 3, 2023 20:01 |
|
this is probably an extremely dumb question but what's the preferred approach to install a new package with pip-tools? I have my requirements.in where I have my pinned packages, and my requirements.txt that's autogenerated, but I'm not sure how to install a new package with the command line and get it in the requirements.in Do i just have to manually add that line? teen phone cutie fucked around with this message at 20:16 on Sep 3, 2023 |
# ? Sep 3, 2023 20:13 |
|
teen phone cutie posted:this is probably an extremely dumb question but what's the preferred approach to install a new package with pip-tools? According to the documentation you would manually add that new package to requirements.in
|
# ? Sep 3, 2023 23:51 |
|
Anyone familiar with Pydantic v2 ? You used to be able to do this: Python code:
Python code:
How do can I create instances like in my first examples, while being able to set values in the Pet instance based on the PetOwner.person_id value ? I've been trying field_validator, but I run into similar issues.
|
# ? Sep 4, 2023 17:41 |
|
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 |
|
That doesn't seem to do much, at least on my install. It still goes code:
Python code:
rowkey bilbao fucked around with this message at 22:05 on Sep 4, 2023 |
# ? Sep 4, 2023 22:02 |
|
Have you tried setting the mode to "after"? I would have guessed that you want a normal method, not a class method, since you're validating the values attached to an instance of the class. But you want the factory function to run and assign a default value to the missing attribute before this validator runs, so I'd think you'd want this validator to be in "after" mode QuarkJets fucked around with this message at 22:15 on Sep 4, 2023 |
# ? Sep 4, 2023 22:11 |
|
Pretty sure the v2 equivalent you're looking for is FieldValidationInfo, which is passed into each validated field like the values dict was in 1.x, it just has a slightly different shape.Python code:
|
# ? Sep 5, 2023 05:30 |
|
Python code:
|
# ? Sep 5, 2023 05:50 |
|
Yeah, fair. I was mucking about with the rest of the code to play with v2 a bit more and didn't include half of the things that would make this better. So far v2 seems good, though I need to spend some time with their automated upgrade tool.
|
# ? Sep 5, 2023 05:55 |
QuarkJets posted:
It's like wasted spark ignition
|
|
# ? Sep 5, 2023 10:46 |
|
QuarkJets posted:
Similarly I've had to point out to people you don't need to do index-based list iteration a bunch of times: Python code:
|
# ? Sep 5, 2023 17:11 |
|
Cursed comedy option: use for i, _ in enumerate(list), then access elements via the index
|
# ? Sep 5, 2023 18:16 |
|
QuarkJets posted:Have you tried setting the mode to "after"? I did, iirc it expects all fields (ex Pet.owners_id) to be set when creating a PetOwner instance. nullfunction posted:Pretty sure the v2 equivalent you're looking for is FieldValidationInfo, which is passed into each validated field like the values dict was in 1.x, it just has a slightly different shape. This was the one, thanks !
|
# ? Sep 5, 2023 21:53 |
|
Zugzwang posted:Cursed comedy option: use for i, _ in enumerate(list), then access elements via the index for x in list(<generator>) or things like {max, all, any}([<generator expression>]) drive me insane. You were so close! Why did it have to be a list?!
|
# ? Sep 6, 2023 14:55 |
|
During code review I noticed a routine that modifies a dict while iterating over it. I've always understood this to be a no-no. But this code seems to work in practice. It's not removing or adding keys, just re-assigning the value for some keys. Here's a simplified example to show what I mean:code:
|
# ? Sep 6, 2023 18:46 |
|
Olly the Otter posted:Is this type of thing guaranteed to work under Python standards? Mutating an item linked to a key ought to be safe. Replacing the object a key points to with a different one like that code does is fuzzier. CPython probably doesn't actually rebalance the backing tree so previous iterators are staying valid. But it also doesn't seem that unreasonable for some hypothetical future version to consider replacing a value as a time that it could rebalance
|
# ? Sep 6, 2023 20:31 |
|
Foxfire_ posted:There aren't any Python standards besides "what does CPython do?" combined with vibes/occasional comments about what is only an implementation detail. I still can't bring myself not to use OrderedDict. But yeah, why not make that a dict comprehension? Just never modify the thing you are iterating over.
|
# ? Sep 6, 2023 20:48 |
|
The big problem with mutating collections is that you can't add or remove things as you go, mutating them is perfectly fine. Agree that a dict comprehension is cleaner though.
|
# ? Sep 6, 2023 20:52 |
|
The gray area is that this code isn't mutating things, it's replacing the value associated witha key with a completely new unrelated one (python integers are immutable like strings are) A well optimized dictionary implementation probably doesn't mess with any structure for that, but you could also implement it with "delete the old (key, value)" operation followed by "insert the new (key, value)", which could plausibly restructure a backing tree/hash table
|
# ? Sep 6, 2023 22:08 |
|
Foxfire_ posted:There aren't any Python standards besides "what does CPython do?" combined with vibes/occasional comments about what is only an implementation detail. Why would you rebalance a tree when modifying the value for some key? Neither the size of the tree nor the bucket of the value changes. To answer the original question, I agree there probably isn't anything specifying this behavior, but modifying or replacing the value of some existing key should be fine, adding or removing keys would break iteration. Pie Colony fucked around with this message at 00:22 on Sep 7, 2023 |
# ? Sep 7, 2023 00:18 |
|
Foxfire_ posted:The gray area is that this code isn't mutating things, it's replacing the value Yeah, this gray area is why I posted -- if it were just mutating an object then I wouldn't be concerned. (The original code does stuff that's more obviously replacing the value, such as mydata[k] = UUID(d), but the principle is the same.) If it were my own code then I'd avoid stuff like this altogether, but it's trickier when it comes to asking someone else to change their code. I suppose I was hoping there were clear standards somewhere that I could point to.
|
# ? Sep 7, 2023 00:51 |
|
Olly the Otter posted:During code review I noticed a routine that modifies a dict while iterating over it. I've always understood this to be a no-no. But this code seems to work in practice. It's not removing or adding keys, just re-assigning the value for some keys. Here's a simplified example to show what I mean: Views are dynamic, so changes to the values in a dictionary are permitted even while you're iterating over them. If keys were being added or removed then I'd expect issues https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects Per the docs, this is a-okay This is okay too: Python code:
Python code:
QuarkJets fucked around with this message at 07:26 on Sep 7, 2023 |
# ? Sep 7, 2023 07:14 |
|
Pie Colony posted:Why would you rebalance a tree when modifying the value for some key? Neither the size of the tree nor the bucket of the value changes. Essentially: Python code:
Python code:
Requiring Change in the interface isn't unreasonable, but it doesn't feel so fundamental to the concept of a mapping type that I'd confidently infer a "The Python language requires that all Dict and Dict-compatible objects support a Change operation" rule instead of it just being an implementation detail that CPython dict's happen to do this Nothing in the documentation says anything definitively either way, there's nothing talking about changes at all. It just says: quote:d[key] = value quote:Iterating views while adding or deleting entries in the dictionary may raise a RuntimeError or fail to iterate over all entries.
|
# ? Sep 8, 2023 04:44 |
|
The definition of the dict type also says:quote:Note that updating a key does not affect the order. Keys added after deletion are inserted at the end. quote:Keys and values are iterated over in insertion order. This is explicit and defines the behavior that we've each shown
|
# ? Sep 8, 2023 07:41 |
|
Yeah, I'd take that as sufficient to say the language is requiring update/change as a distinct thing. Looks like it got promoted from being an implementation detail to language requirement at the same time as requiring dict's to be ordered did. The text for it is just off with the stuff about order in the main dict documentation instead of the part about dictviews.
|
# ? Sep 8, 2023 09:24 |
|
I've got a bit of a design question. I'm working on a project that involves a lot of infrastructure tech clients and has to do with various forms of infrastructure validation across a variety of infra types. To keep the backstory short: the APIs that these services expose have a fairly poor Python integration; for this reason and wanting to glue stuff together we have a package that does a lot of this. For example, you might have an ClusterPlus object, which recreates the hierarchical data of the Cluster object from the API, as well as loading things like ClusterResources/etc. These classes are pretty heavy, but they make it a lot easier to iterate quickly because a lot of the weird interplay is handled for you. However, now we have a problem: We want to be able to freeze these objects so we can use them in a situation where you cache first and then call later, and currently they have a lot of lazy loading functions etc involved that check if the object is loaded and grab it if it isn't. There's a few options I'd like to suggest moving forward for refactoring these, and I'm not sure which one would be better or if it's even workable. Note that these classes are all in active use by other major projects so ideally we'd like these refactors to be as drop-in as possible. One option would be to have all these objects take in an optional argument that disables all API calls; this requires that it's implemented properly but is (at least from a high level perspective) fairly straightforward. Another option would be to separate the data structure from the functions - For example, having a FrozenClusterPlus class that is either inherited from (probably bad) or passed into the ClusterPlus object, so something like this: Python code:
Any suggestions on how to approach this?
|
# ? Sep 11, 2023 20:36 |
|
Have you looked at frozen dataclasses? Python code:
To store off intermediate state you could have a `to_json` method, and then you could have a class method `from_json` that also handles initialization Without knowing what your data model looks like it hard to know how drop-in this would be. Frozen dataclasses are as close to "immutable" as things tend to get in python, and you get all of the other advantages of dataclasses (a nice __repr__, trivial conversion to dictionary, etc).
|
# ? Sep 12, 2023 05:42 |
|
Hey all, getting back into some basic Python scripting and my smooth brain finally hit a wall. The basic is that the script is parsing a file, putting it all in a dictionary, then pulling specific elements of that out and creating a list of lists. However, I want the functionality to sort that list. That all came out well and sorting was working. Then I wanted to be able to add the sort ability to the command line with an argument, so in comes argparser. The issue I'm probably having, however is scoping. Here's what I've got so far: Python code:
If I attempt to add a variable of 'args' in the global area, I get TypeErrors, no matter what I attempt to set it to (string, list, tuple, etc). If I global another variable (sort) and put sort = args.sort inside the main function, it doesn't update the global variable (without having to put 'global sort' in there which is silly). So I'm probably doing something really dumb right now and completely missing an easy solution.
|
# ? Sep 13, 2023 21:36 |
|
This is maybe more of a DB question than a Python question, but I'm using SQL Alchemy so I'll ask it here... I've got a FastAPI project that's storing some stuff in a DB (SQLite locally for development, MySQL for production). I have a few columns that are strings, but are actually lists of data. So to read them I read the string and split on the comma to get my list of strings. I know I could setup a relationship and store these items in the list in another table, to do it in a more "native" way, but that adds complexity to the DB, and there's never a reason why I would want only one of these values in the list: I always want only a single one. These entries are rather "static", they don't get changed by user input, and in fact once created they rarely if ever change, only get read. I'm thinking I could do something like this but I'm not sure if there's any better options these days.
|
# ? Sep 13, 2023 21:39 |
|
Gothmog1065 posted:Hey all, getting back into some basic Python scripting and my smooth brain finally hit a wall. The basic is that the script is parsing a file, putting it all in a dictionary, then pulling specific elements of that out and creating a list of lists. However, I want the functionality to sort that list. That all came out well and sorting was working. Then I wanted to be able to add the sort ability to the command line with an argument, so in comes argparser. The issue I'm probably having, however is scoping. Here's what I've got so far: args is a local to the main function. Pass it in as an argument to data_display.
|
# ? Sep 13, 2023 22:16 |
|
you could also define data_display inside main's scope. which is not usually recommended but is sometimes a useful pattern
|
# ? Sep 13, 2023 22:19 |
|
QuarkJets posted:Have you looked at frozen dataclasses? Familiar with Frozen dataclasses, but this solves a problem I'm not actually trying to fix. The issue isn't 'you can reassign/update attributes after creation', the problem is 'certain attributes make calls out to clients to populate data, and we need to ensure that doesn't happen.' The idea is that all the dependencies/etc of a Cluster might be very complex and big, and span multiple other systems. This framework was built as an accelerator to other projects so it basically works a glue between multiple infrastructure technologies - when you ask a cluster about network details, instead of just giving you a pointer, it actually calls and loads the object from that other client, then it's available to you cached locally from there on out. (See the example of the cluster_children note above) The problem is that I basically want a way of saying 'hey just return a version of yourself that just contains the data and won't try and fetch anything else - we either initialized it correctly, or datasets will be empty. One approach would be to construct complex dataclasses and have each of these objects basically turn themselves into those objects, but that's a huge amount of basically copy/paste work there, so the idea is how to build it into the class itself in the least weird way possible. There's some really long ways of going about it - basically writing two almost identical classes that both fulfill the same interface - so you have a ClusterPlus object and then a ClusterPlusButThisOneDoesntMakeCalls object - the second of which is basically just a dataclass representation of what the first one looks like after you fetched all the data you need. But that's a ton of duplicate code, not to mention the entire problem of drift.
|
# ? Sep 14, 2023 03:17 |
|
You should copy-paste whatever Python spits out when the issue occurs because that's very helpful, Gothmog1065. I can see that dataDict isn't defined when main() runs, that would be one problem
|
# ? Sep 14, 2023 03:51 |
|
Falcon2001 posted:Familiar with Frozen dataclasses, but this solves a problem I'm not actually trying to fix. The issue isn't 'you can reassign/update attributes after creation', the problem is 'certain attributes make calls out to clients to populate data, and we need to ensure that doesn't happen.' Have you looked at the cached property decorator?
|
# ? Sep 14, 2023 03:55 |
|
Falcon2001 posted:Familiar with Frozen dataclasses, but this solves a problem I'm not actually trying to fix. The issue isn't 'you can reassign/update attributes after creation', the problem is 'certain attributes make calls out to clients to populate data, and we need to ensure that doesn't happen.' Assuming there's a naming convention for the underlying properties, you could make a class that just dynamically returns those when fetching a property. Something like: Python code:
|
# ? Sep 14, 2023 04:22 |
|
|
# ? May 28, 2024 16:21 |
|
Zugzwang posted:Yeah I really wish Python had something like "type hinting is purely optional, but they will be enforced if you specify them." As opposed to "the Python interpreter does not give the slightest gently caress about type hints." loving VB6 had a solution for this. "Option Explicit On" "Option Strict On" It would be a highly good thing to have the option to just stick something like that at the top of a python file, so that it explodes at import if it fails a sanity linting.
|
# ? Sep 14, 2023 05:22 |