|
shrike82 posted:Which is a roundabout way of saying that Boris was right with his initial solution. well i said he was directly right with my first reply, that last one is just using __get__ instead of methodtype syntax since it's new to me and reads a bit cleaner imo, since it just does that under the hood
|
# ? Apr 19, 2017 03:02 |
|
|
# ? May 16, 2024 17:26 |
|
Boris Galerkin posted:The way I thought of it was that everything is being passed around by reference/pointer. So when I assign test.foo = foo what I'm actually doing is saying "hey test object, your memory location corresponding to "foo" is now pointing to this other memory location which happens to have information on how to execute the foo function. It is basically correct. As some of the previous posts were getting at, the Python function type implements the descriptor protocol. When you look up an attribute located in the class's namespace on a class or an instance of a class, if the object has a __get__ method, that's called and it's return value is what you get from the attribute lookup instead of the object itself. This is explained in more detail in the docs on the descriptor protocol. But point being, when you do Class.foo = some_function and then Class.foo or Class().foo, you are getting a newly created wrapper around the function and not the function itself. Another fuckin edit to add: Today I also learned that this is changed from Python 2->3. In Python 2 you get an unbound method for the Class.foo lookup and in Python 3 you just get the function itself. But the descriptor mechanics are the same, what __get__ is returning is different. Though again, unless this is just an experiment for learning about Python rather than actual in-use code, I think you are better off just calling the function with the instance as a parameter rather than doing the thing that we've had a 20-post discussion about the exact mechanics of. More funny stuff: code:
Sorry I edited this a few times and search/replaced "c" with "inst" in the code because it was hard to read. breaks fucked around with this message at 08:57 on Apr 19, 2017 |
# ? Apr 19, 2017 05:05 |
|
As always, 'Fluent Python' has something to say about this with chapters on descriptors and meta-programming. Boris, I'd recommend you get it if you haven't, if you really want to delve into the languages.
|
# ? Apr 19, 2017 05:26 |
|
Nippashish posted:This is sort of weird, but what you want is also sort of weird. "I want a function that does different things depending on which instance I call it on" really screams "I want a base class with a bunch of different subclasses" to me. You're not wrong, but I wanted to avoid having to explicitly make new subclasses and instead just have them all represented by one class called "NavierStokes" in this case. So that's why I asked about metaclass vs methodtypes. Dex posted:
This is essentially what I had in my first post: Boris Galerkin posted:
The question was just: should I metaclass or use the types.MethodType module? I didn't really see or understand a practical difference. shrike82 posted:Which is a roundabout way of saying that Boris was right with his initial solution. I'm not sure I understand what this one is doing. Thanks for all the help and discussion. I don't want to sound dismissive, but it looks like the original method I proposed works and Dex's solution is more or less the same, so I'm just going to stick with what I have.
|
# ? Apr 20, 2017 08:02 |
|
Basically, and to your earlier question, when you use dot notation in Python (Class.attribute or Instance.attribute), you're not directly accessing the attribute. You're calling the __setattr__ or __getattribute__ (and potentially __getattr__) magic functions and passing the attribute to those functions. Attributes that implement the Descriptor protocol (i.e. implement __get__ and/or __set__ functions) affect the earlier mentioned magic functions. Because functions implement the __get__ function, there's some funkiness. Your thought about using MethodTypes to manually bind the function is right.
|
# ? Apr 20, 2017 09:24 |
|
I'm sorry-not-sorry to reiterate it again but keep in mind that doing self.something = types.MethodType(some_func, self) in __init__ or whatever does not exactly replicate the behavior of a normal method and with enough usage there will eventually be a situation where the fact that you have a MethodType in the instance namespace instead of a function in the class namespace becomes a problem even though it "works like a method." If you don't care you don't care but do enough of this stuff and it will eventually bite you in the rear end in a very painful way unless you are really taking pains to exactly replicate the normal behavior. It is genuine Bad Python to drop a MethodType on an instance like that because it is subverting the expectations of the language implementation. I'll stop harping on it because I think I've said it too many times already. breaks fucked around with this message at 10:29 on Apr 20, 2017 |
# ? Apr 20, 2017 10:18 |
|
Again, from Hettinger's Descriptor HowTo code:
https://twitter.com/raymondh/status/771628923100667904 Which is effectively the same I'm very well aware of the issues with playing with metaprogramming, but just saying it's bad without providing a feasible alternative kinda sucks. shrike82 fucked around with this message at 10:35 on Apr 20, 2017 |
# ? Apr 20, 2017 10:30 |
|
The descriptor howto is a great read and generally correct but has some simplifications, imprecise language, and outdated info. That code throws an exception on 3.6.1, and if you fix that the Class.Function lookup returns a bound method, etc etc; it isn't fully equivalent to how it actually works, at least not at this point. Partial is fine. It's not pretending to be a method and then actually behaving in slightly different ways. But passing in the function and adding a method to call it with self is basically the same thing, and has already been suggested two or three times and the code written out for it at least once. The guy doesn't seem interested because it's not "injecting a method."
|
# ? Apr 20, 2017 13:37 |
|
I still think subclassing is the better option, if anything because it is the simplest way.
|
# ? Apr 20, 2017 13:43 |
|
There's nothing odd about talking about dependency injection, outside of DI/IoC being slightly less 'popular' in Python-land due to dynamic typing making it trivial to handle.
|
# ? Apr 20, 2017 14:33 |
|
breaks posted:The guy doesn't seem interested because it's not "injecting a method." No. I wasn't interested in passing in a literal function into the constructor of the class because I wanted the class constructor to do it for me. I understand that I could have still written my class to "attach" the proper function to itself and then call it with explicitly passing in "self" into the function to replicate the same thing. But my "there has to be a better way" lightbulb lit up since it seemed silly and I found metaclass and MethodType. And also breaks posted:If you don't care you don't care but do enough of this stuff and it will eventually bite you in the rear end in a very painful way unless you are really taking pains to exactly replicate the normal behavior. It is genuine Bad Python to drop a MethodType on an instance like that because it is subverting the expectations of the language implementation. I'll stop harping on it because I think I've said it too many times already. This is literally the first response from anybody with substantial information on why what I'm doing may be considered "bad." Everyone else was either helpful and suggested methods to do what I wanted to do or tell me "no don't" without offering why.
|
# ? Apr 20, 2017 15:14 |
|
I'm wanting to build a controller program for some physical computing equipment of mine, but in trying to set up Kivy, I've run into a problem where Windows 10 only shows that it has OpenGL 1.1. Kivy is saying it needs OpenGL 2.0 in order to run. Has anyone a) found themselves in the same position, and b) found a workaround? Barring that, does anyone have a suggestion for packages that build a GUI with about the same learning curve as Kivy?
|
# ? Apr 20, 2017 16:10 |
|
i was going to suggest looking at GLEW but it seems kivy already has it as a dependency:https://kivy.org/docs/installation/installation-windows.html posted:python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew have you installed all those packages already?
|
# ? Apr 20, 2017 16:49 |
|
Dex posted:i was going to suggest looking at GLEW but it seems kivy already has it as a dependency: I have -- next time I'm at the computer I'll screen shot the error codes. I think it has more to do with Windows insisting on using the generic PnP display driver (which is set on OpenGL 1.1)
|
# ? Apr 20, 2017 17:03 |
|
We were talking about Zappa and Lambda a few pages ago, and Thermopyle had mentioned that a lack of Python 3 support was the real thing holding him back from starting new projects with it. That's now been remedied: https://aws.amazon.com/about-aws/whats-new/2017/04/aws-lambda-supports-python-3-6/.
|
# ? Apr 20, 2017 17:44 |
Boris Galerkin posted:Thanks for all the help and discussion. I don't want to sound dismissive, but it looks like the original method I proposed works and Dex's solution is more or less the same, so I'm just going to stick with what I have. You took a good look at alternative suggestions and then made an informed decision. That's not dismissive in the least. You brought up an interesting question in an area that's relatively niche/complex within python. If nothing else, I learned a lot from the discussion! Just because Metaclasses and method injection are the wrong solution most of the time doesn't mean they're the wrong solution all of the time.
|
|
# ? Apr 20, 2017 17:52 |
|
Gene Hackman Fan posted:I have -- next time I'm at the computer I'll screen shot the error codes. I think it has more to do with Windows insisting on using the generic PnP display driver (which is set on OpenGL 1.1) Yeah, here's what it gave me the last time I tried to run a basic "hello world:" edit: I know I've heard other people having issues with this and added a line or two to the actual program to force... something, but it hasn't worked for me: code:
code:
Gene Hackman Fan fucked around with this message at 20:09 on Apr 20, 2017 |
# ? Apr 20, 2017 20:07 |
|
shrike82 posted:There's nothing odd about talking about dependency injection, outside of DI/IoC being slightly less 'popular' in Python-land due to dynamic typing making it trivial to handle. As a relative newbie, if dependency injection is less popular in Python than it is in other languages, how do you handle cases in Python that you'd typically use dependency injection for in, say, Java?
|
# ? Apr 20, 2017 20:10 |
|
Epsilon Plus posted:As a relative newbie, if dependency injection is less popular in Python than it is in other languages, how do you handle cases in Python that you'd typically use dependency injection for in, say, Java? DI isn't exactly uncommon in Python. It's just that because of Python's dynamic nature you don't really need a lot of scaffolding that you need in other languages so it often doesn't go by the Official Pattern Name Dependency Injection. You just write stuff in Python in a pythonic way and then later someone is talking about Python and DI and you go "you know I guess I just did use DI!" edit: Forgot to add that super() is an effective and easy way of doing DI in python. Thermopyle fucked around with this message at 21:51 on Apr 20, 2017 |
# ? Apr 20, 2017 20:41 |
|
Gene Hackman Fan posted:edit: I know I've heard other people having issues with this and added a line or two to the actual program to force... something, but it hasn't worked for me: seems to be a pretty common problem with windows 10 users: https://github.com/kivy/kivy/issues/3576 - this one is fixed by the multisampling config you posted, so it's not that https://github.com/kivy/kivy/issues/5071 - this one is pyinstaller specific https://buffered.com/support/solve-opengl-error/ - this is just some random app that uses kivy if you have the latest graphics drivers installed already, i'd say open an issue on kivy's github and see if they have anything helpful to add
|
# ? Apr 20, 2017 20:57 |
|
Thermopyle posted:DI isn't exactly uncommon in Python. It's just that because of Python's dynamic nature you don't really need a lot of scaffolding that you need in other languages so it often doesn't go by the Official Pattern Name Dependency Injection. You just write stuff in Python in a pythonic way and then later someone is talking about Python and DI and you go "you know I guess I just did use DI!" I've tried to figure out super() in Python a few times and everything I look at makes no goddamn sense; do you ? have any good resources covering it? I don't mind doing regular old DI for the project I'm doing in Python it would be nice to properly understand how super() works.
|
# ? Apr 21, 2017 01:20 |
|
Epsilon Plus posted:As a relative newbie, if dependency injection is less popular in Python than it is in other languages, how do you handle cases in Python that you'd typically use dependency injection for in, say, Java? Sorry, I meant that it's so trivial in Python that people don't tend to think "Oh, I need to use a DI/IoC framework here". More generally, and imho, people spend less time thinking about design patterns when building stuff in Python. This is a good read if you want to learn more about Python design patterns http://www.aleax.it/gdd_pydp.pdf shrike82 fucked around with this message at 01:28 on Apr 21, 2017 |
# ? Apr 21, 2017 01:21 |
|
shrike82 posted:Sorry, I meant that it's so trivial in Python that people don't tend to think "Oh, I need to use a DI/IoC framework here". The main use I see of IOC containers is magically getting constructor args on creation by registering classes and interfaces which is v unpythonic Otherwise just pass them in as arguments
|
# ? Apr 21, 2017 01:37 |
Epsilon Plus posted:I've tried to figure out super() in Python a few times and everything I look at makes no goddamn sense; do you ? have any good resources covering it? I don't mind doing regular old DI for the project I'm doing in Python it would be nice to properly understand how super() works. Super() considered super by Raymond Hettinger is a good resource. Raymond did this as talk at PyCon a couple years back so it's available as a video presentation too. Raymond is a fantastic presenter. As always, Fluent Python covers this in some detail.
|
|
# ? Apr 21, 2017 02:40 |
|
I've forgotten most of my Java but doesn't it have the super keyword which is used roughly in the same way?
|
# ? Apr 21, 2017 02:43 |
|
I'm not sure what this would be called but, is there anything for python gui making that is similar to what VB.net has where you can place all the widgets on a template? I've been slamming my face into tkinter with little to no progress for long enough that I'm ready to try something new.
|
# ? Apr 21, 2017 19:45 |
|
Azuth0667 posted:I'm not sure what this would be called but, is there anything for python gui making that is similar to what VB.net has where you can place all the widgets on a template?
|
# ? Apr 21, 2017 20:02 |
|
I've seen PyQT recommended before for making GUIs. How does it compare to tkinter?
|
# ? Apr 21, 2017 21:13 |
|
Azuth0667 posted:I've seen PyQT recommended before for making GUIs. How does it compare to tkinter? I've built GUIs with both. PyQt is considerably more powerful and I prefer using it. You get much nicer looking widgets too. At the same time, PyQt is more complex, and I found it harder to learn (harder even than Tkinter which was my first ever attempt at GUI programming). The model-view system and the signal-slot system are the usual stumbling blocks for learners. One downside is that there's practically no official documentation of PyQt. What exists is mainly for the C++ version of Qt. However, there is some good documentation for PySide, which is extremely similar to PyQt. Although I use PyQt, I find myself reading the PySide docs more often.
|
# ? Apr 21, 2017 21:58 |
|
Well crap the big stumbling block I'm still having with tkinter is that none of the example code I can find is current so all these tutorials I attempt get hung up at some point because something has changed.
|
# ? Apr 21, 2017 23:31 |
|
I don't know what your specific application is but nine times out of ten when someone is asking for a GUI toolkit the right answer for their use case is HTTP.
|
# ? Apr 22, 2017 14:32 |
|
Thanks for the Fluent Python suggestion, thread. Started reading it last week and it's been really good.
|
# ? Apr 22, 2017 14:41 |
|
Newbie question: I'm trying to build a web scraper with Beautiful Soup that pulls table rows off of the roster pages for a variety of college sports teams. I've got it working with regular HTML pages, but it doesn't seem to work with what appear to be Angular pages. Some quick googling makes it seem like I will need to use a python library like selenium to virtually load the page in order to scrape the html tables on the page. Would a work around be to first use Beautiful Soup, but if a table row returns as None, then call a function to try scraping the page using something like selenium. Or, should I just try to scrape all of the pages with selenium or a similar library?
|
# ? Apr 22, 2017 16:44 |
|
KernelSlanders posted:I don't know what your specific application is but nine times out of ten when someone is asking for a GUI toolkit the right answer for their use case is HTTP. I assume you mean HTML. And I agree with you.
|
# ? Apr 22, 2017 17:51 |
|
I have an interview next week where I'll have to demonstrate working knowledge of Python. I do have a basic understanding of the language and what to do with it, but I wondered if there were any good online resources for intermediate Python practice. Unfortunately I don't have a lot of clarity about what I'll be tested on and I'm not working on any special project at the moment, so I'm not sure how to practice.
|
# ? Apr 22, 2017 22:42 |
The best thing you can do to get basic fluency is to take a project you've written before in another language and re-inplement it in Python.
|
|
# ? Apr 22, 2017 22:57 |
|
laxbro posted:Newbie question: I'm trying to build a web scraper with Beautiful Soup that pulls table rows off of the roster pages for a variety of college sports teams. I've got it working with regular HTML pages, but it doesn't seem to work with what appear to be Angular pages. Some quick googling makes it seem like I will need to use a python library like selenium to virtually load the page in order to scrape the html tables on the page. Would a work around be to first use Beautiful Soup, but if a table row returns as None, then call a function to try scraping the page using something like selenium. Or, should I just try to scrape all of the pages with selenium or a similar library? In my experience Selenium isn't exactly fast, but really it depends on how many pages you need to scrape and how often, and also how heavy they are (like does it take a full 20 seconds for all the jabbascripts to render). In the worst case you'll have a browser window literally opening and loading the page, and you'll have code waiting and checking if it's ready yet Honestly it depends on the pages you're loading, unless they all happen to share the same structure (these are separate sites right) you'll have to hand craft some code to pull out the relevant table on each page. Ideally that's as simple as writing a selector tailored to that page's html, but if you're doing that anyway, you might as well explicitly set the type of scraping while you're at it I don't remember the syntax but you could make a class that takes a selector and handles normal parsing (normal GET, pulls out selector using Beautiful Soup). Then you could do a similar one that does the same, but loads the page with selenium, keeps trying to find the element described by the selector (might not be in the DOM yet), and eventually times out That way you have two basic kinds of behaviour, and you can list all your sites as Normal(url, '#cool-table') or Webdev(url2, '.stuff > table') and iterate over them calling the parse method on each. This might not be the best design but it's pretty simple if it works for you?! Also I forget but you might be able to just get away with using Selenium and running its selector functions (which are a lot like Beautiful Soup's) on plain http responses, so no need for BS in that case. Also Selenium was a real pain last time I used it, they updated something so the Firefox webdriver wasn't included anymore and everything broke. There might be a better scraper out there?
|
# ? Apr 23, 2017 00:03 |
|
Thermopyle posted:I assume you mean HTML. And I agree with you. I meant HTTP, HTML, CSS, and maybe a tiny bit of Javascript, but just picked one. Thought about saying "Flask" but thought that would be too indirect to be helpful. Vivian Darkbloom posted:I have an interview next week where I'll have to demonstrate working knowledge of Python. I do have a basic understanding of the language and what to do with it, but I wondered if there were any good online resources for intermediate Python practice. Unfortunately I don't have a lot of clarity about what I'll be tested on and I'm not working on any special project at the moment, so I'm not sure how to practice. Do some of the coding problems from https://www.careercup.com/page?pid=coding-interview-questions. Actually implement them, don't just think through it. Practice both on a whiteboard and in a text editor/python cli.
|
# ? Apr 23, 2017 00:31 |
|
Actually, go to platforms like hackerrank and codewars. They have algo problems as well as Python specific problems with a built-in interpreter that runs your program and compares its output for a bunch of test cases.
|
# ? Apr 23, 2017 00:43 |
|
|
# ? May 16, 2024 17:26 |
|
Those are some useful suggestions, thanks. I've been using hackerrank and it seems to be helping a bit.
|
# ? Apr 23, 2017 05:43 |