« first day (4733 days earlier)      last day (64 days later) » 

7:16 AM
What even is the point of an abstract base class with only one subclass though? How can that possibly make the code simpler?
I really don't see how this is supposed to help, but here's my attempt
Oops, I botched the _start_process_and_read method a little
 
 
1 hour later…
8:49 AM
@Aran-Fey Not much you can do if you want to have the class like that. The _process is optional by your own design, and the _process.stdout/.stderr are broken in typeshed. I'd make the _process non-optional; instead of a _setup to override, have subclasses provide public alternate constructor that creates the process.
 
Let's say I replace the process: Optional[Process] argument with a start_process: Callable[[], Awaitable[Process]] argument, which would be invoked the first time you call read(). Would that help at all?
What I'm trying to get at is, I don't think it's my fault, I think the type system is simply unable to handle mutable objects
 
Yeah, mutable types aren't really a thing.
Yet I have to say that not having mutable types has made my code easier to use.
 
That doesn't sound realistically achievable, especially if asyncio is involved. You constantly have to close() things
By the way, I forgot to ask. You recently said you don't have issues with circular imports. Do you have a class-heavy code base I could take a look at?
 
@Aran-Fey I usually have that hidden behind context managers. Yes, those are inherently broken in theory (you can book my rant on this topic for just USD 999.99 today!) but it in practice not using contexts after the context (?!?) is well accepted.
 
But isn't the typing still a mess, internally? Since the object can be in a "open" or a "closed" state
Or do you just pretend it's always "open"?
Hmm. I guess it's a non-issue if you're just wrapping another closable object. You just call self._session.close() or whatever, which doesn't change the type of anything
 
9:06 AM
@Aran-Fey I've pondered that a bit since then. I do have one case of circular imports here because it describes some inherently circular abstraction.
If I have time I'm just going to get rid of the Java-style organisation and pull all those classes into one file. Then it's forward refs which are allowed to be circular.
@Aran-Fey This. IMO context managers are inherently broken but no one cares so I don't care either. :D
 
Haha, I wish I could have that attitude :D
 
For user-facing code I usually just have a flag that says "I'm open" and throw errors strategically.
@Aran-Fey Practicality definitely beats purity if the latter just isn't possible. ;)
 
All things considered, OOP is surprisingly poorly supported in all existing programming languages
One more question. Is there a reasonable way to override a parent class's method without lots of WET code or losing static typing? Example
 
9:23 AM
@Aran-Fey IMO this works a bit better in RAII languages such as C++ or Rust where you can veritably rely on object lifetime. The main problem we have here is that things can live on after closing, which RAII handles natively.
@Aran-Fey Not quite sure what you are looking for here. Initialisation/Construction isn't covered by LSP or other subclassing constraints.
Are you asking about repeating the default_headers type or loosing the session argument?
 
I want my IDE to understand that the signature of HtmlScraper isn't (*args: Unknown, default_headers: Mapping[str, str] = { "User-Agent": "my scraper" }, **kwargs: Unknown) -> None, without having to copy-paste all the parameters from the base class
I'm fine with repeating the default_headers (including type and default value), but I don't want to repeat the other parameters
 
I've never figured out how to do that properly. :/
I think you asked that before and again my initial thought was "ParamSpec" before again realising that's not it.
 
Programming sucks :)
 
 
5 hours later…
2:50 PM
@Aran-Fey I unfortunetly agree
 
 
1 hour later…
4:06 PM
@Aran-Fey apologies, I've have some family stuff come up and not looked at this yet. I haven't ignored you on this
 
4:17 PM
No worries. I'm not sure if I understood the assignment correctly, so just let me know if that wasn't what you were looking for
 
 
3 hours later…
6:48 PM
Forgive my barging in @Aran-Fey and @roganjosh - this discussion of "I have an object which may or may not be in a configured state" feels like a State pattern kind of thing. I'm not a super fan of that pattern, as it is a bit on the WET side, but could you use that here? I put together a gist just as a self-refresher on State, you can view it here if it may jog some ideas. At least there are a minimum of asserts.
 
7:00 PM
How do you typically initialize a member variable which contains a class instance?
My situation:
class myClass:
    someClassInstance = ???

    foo(self):
        self.someClassInstance = createObj()
 
7:39 PM
@LandonZeKepitelOfGreytBritn Can you clarify what you mean? Every object is an instance of some class, so every attribute ("member variable") refers to a class instance. You typically initialise them the same way you initailise everything – in __init__ by assignment.
 
@MisterMiyagi Not quite sure how I could clarify... So, I m more used to write C++ which is why I like to have all my variables declared at beginning/end of my class. Having an __init__ is not possible as this is a class used by PyTest
I am debugging smething and m worried that if I just do this:self.someClassInstance = createObj() the variable becomes unknown once I exit the function
You probably have guessed it, I am still learning Python...
 
if you use classes with pytest there's a very good chance you're doing something wrong ;)
 
@ThiefMaster PLease elaborate? I put everything into a class because I thought that's what the cool kids do nowadays
 
if you want singleton-like logic in pytest, have a look at the scope of pytest's fixtures: docs.pytest.org/en/stable/how-to/…
 
Pytest uses classes primarily as namespaces. It does provide separate instances per test run, so "the variable becomes unknown once I exit the function" by design.
 
7:53 PM
"put everything in a class or you're doing it wrong" is a java-ism, and a philosophy from people who believe OOP is the only valid paradigm :)
 
@ThiefMaster I am one of these people, yes
* shrug *
but admittedly, I still have loads to learn in that field
 
Your unittests should generally be independent of each other – especially their ordering – so relying on shared state between tests is a bad idea by default, no matter if your test framework of choice makes it simple or hard.
pytest's primary means of initialising tests consistently are fixtures which work orthogonal to classes.
 
Agreed.
Actually the issue is that the current pytest configuration spawns all tests in parallel and I can only have one connection to the server at once
meaning that whenever my eg second test is launched ant tries to create a socket to connect, it obviously fails
so I either have to be able to share the socket somehow between the different running tests (yay...) or run all tests sequentially
oh LOL!
assigning an execution order with that framework seems to be much easier then I thought
 
8:44 PM
@PaulMcG Hmm, maybe I'm misunderstanding but it sounds similar to the strategy pattern? Interesting idea. I gave it a shot, and I'm unsure how I feel about it. It felt very verbose, but somehow it's only 7 lines longer than my original code. Monkey brain is confused
It has a reference cycle though, and getting rid of that would probably add some 10 lines or so
It's definitely type safe though. No complaints on that front
 
9:03 PM
It straight out of the original Design Patterns book, although the use of getattr() to delegate to the internal state object is Python-specific, and so would deviate from what you would read in the book.
The verbosity of this pattern (and maintenance toil in the face of adding new methods or states) are its drawbacks. There are some Python packages that simplify this part of using this pattern, and I have a pyparsing example that I've been meaning to promote to being its own PyPI package. I had a co-worker who used State pattern every chance he got.
I guess the getattr parts just give us a short cut to avoid additional repetition of functions that explicitly delegate to the state attribute. You could put all the behavior in the xxxState classes, but I like keeping those classes focused on tracking state and which functions are valid for a given state, and keeping the actual behavior in the containing Job class.
 
9:37 PM
I can't find any related modules on pypi, do you know some?
 
I thought for sure I had seen a presentation at PyTexas a few years ago. Looking...
I think transitions is the package that was presented. A quick google also turns up pysm and python-statemachine.
 
ty
 
The pyparsing example I wrote generates and then compiles the classes to implement the various states, including a mixin class to add to the state container class. It parses a class-like description of "from_state_class -transition-> to_state_class" transitions. I've held off on uploading to PyPI because I have rethought the generated class code approach.
 

« first day (4733 days earlier)      last day (64 days later) »