|
JHVH-1 posted:Is there a decent IDE out there for windows that would let me work on python code from a remote machine. Right now I just use WinSCP, then edit the file in windows VIM and run it from putty. Then I also have to do all the git checkins/outs. mount your ssh endpoint via (win-)sshfs and just edit directly in any editor
|
# ? Jun 19, 2014 14:34 |
|
|
# ? Jun 11, 2024 04:13 |
|
Symbolic Butt posted:This is not true. a == b is just a.__eq__(b) http://codepad.org/taSgse1T
|
# ? Jun 19, 2014 14:36 |
|
Oh god, my life is a lie.
|
# ? Jun 19, 2014 14:49 |
|
To be fair, I think that's true for every non-base type. Base types are usually weird non-standard things that don't conform to other conventions like overloading or inheritance. Edit: Though; is the python interpreter source up anywhere? Probably a dumb question but if it is it could be worth it to take a look what "==" does. I'd be interested.
|
# ? Jun 19, 2014 15:22 |
|
Jewel posted:To be fair, I think that's true for every non-base type. Base types are usually weird non-standard things that don't conform to other conventions like overloading or inheritance. https://docs.python.org/2/reference/datamodel.html quote:There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, __lt__() and __gt__() are each other’s reflection, __le__() and __ge__() are each other’s reflection, and __eq__() and __ne__() are their own reflection.
|
# ? Jun 19, 2014 20:50 |
|
The python documentation seems to be really unclear about what happens here. http://docs.sympy.org/dev/python-comparisons.html#method-resolution
|
# ? Jun 19, 2014 21:42 |
|
Figure I'll ask here as well as on SO. I've got a lil' app I've been working on that is my first foray into doing UI poo poo of any kind and also distribution of python code of any kind. It all works just fine if I run it as a python script, but once I package it into a .app and run it on os x it crashes every time I call qApp.quit(). I have no idea where to even start reading the stack trace or anything, and google has been basically no help at all. http://pastebin.com/upxeBKbx <-- stack trace. https://github.com/LarryBrid/glimmer-client <-- is the whole client, but the 'client.py' file is the main qt loop. Do note that if you try to actually use the thing right now it won't actually function, but the client will run and let you close it no problem. I've been redoing the API over the past few days though so all of the actual functionality is broken. In addition to a fix to this problem I'm open to having people tell me how dumb I am for doing anything dumb that I have done, and would really appreciate any time given to general code-quality crits because I would like to get better.
|
# ? Jun 19, 2014 23:15 |
|
Jewel posted:To be fair, I think that's true for every non-base type. Base types are usually weird non-standard things that don't conform to other conventions like overloading or inheritance. https://github.com/python/cpython/blob/master/Objects/object.c#L633 It's a bit confusing because of the "richcompare" vfunc, but that's just a C convenience API. Python code:
|
# ? Jun 19, 2014 23:57 |
Is PyCharm supposed to be generating CSS files for LESS files that start with an underscore? Would that be a bug?
|
|
# ? Jun 20, 2014 00:29 |
|
Is there a resource that really breaks down python class and objects, for dummies? I'm having a really hard time wrapping my head around them.code:
|
# ? Jun 20, 2014 02:52 |
|
Hughmoris posted:Is there a resource that really breaks down python class and objects, for dummies? I'm having a really hard time wrapping my head around them. OOP is something everyone struggles with learning, and then once they get it it's hard to understand in retrospect why it was so hard to get. I could write a bunch of words, but I don't feel like it, so I'll write very few words that gloss over details and then you can ask for clarification... When you Song(["this is a song", "this song sucks"]) you're basically calling the __init__ function. self "contains" all the names that make up the class, like lyrics and sing_me_a_song. The only way you can access those things within a method is by passing self to it. You put it in the parameter list, and Python automatically passes it to the method when it's called.
|
# ? Jun 20, 2014 03:24 |
|
To expand on Thermopyle's explanation: __init__ is a function that runs whenever an instance of the class is created. 'self' in __init__'s arguments is boilerplate. The other arguments are variables you send to an instance you're creating. 'self.lyrics=lyrics' turns your argument into a variable built into the instance. 'def sing_me_a_song(self):' is a function that has easy access to the instance's 'self.' variables and methods. The 'self' argument is boilerplate, and other arguments are normal function arguments. 'CrappySong = Song(["this is a song", "this song sucks"])' creates an instance of Song, and sets its self.lyrics to the lyrics argument. 'CrappySong.sing_me_a_song()' runs sing_me_a_song(), which now automatically has access to ["this is a song", "this song sucks"]. This highlights why this class might be useful instead of just using a function - you don't have to provide lyrics every time you want to sing a song. You could have a bunch of Song instances. You'd set up the lyrics once for each, and could have each sing their own song whenever. Dominoes fucked around with this message at 09:09 on Jun 20, 2014 |
# ? Jun 20, 2014 08:58 |
|
Also dont start instance variable name with Capital letter.
|
# ? Jun 20, 2014 12:08 |
|
let's say I have a list of salaries like this:code:
edit: thanks \/\/, I saw an example like that on a page, but I couldn't figure out what the lambda was supposed to indicate the fucked around with this message at 17:11 on Jun 20, 2014 |
# ? Jun 20, 2014 17:04 |
|
Python code:
|
# ? Jun 20, 2014 17:10 |
|
the posted:let's say I have a list of salaries like this: code:
|
# ? Jun 20, 2014 17:11 |
|
the posted:edit: thanks \/\/, I saw an example like that on a page, but I couldn't figure out what the lambda was supposed to indicate Using Thermopyle's example, sorted(sal, key=lambda x: x[1]) is the same as Python code:
Python code:
Python code:
Dominoes fucked around with this message at 17:36 on Jun 20, 2014 |
# ? Jun 20, 2014 17:27 |
|
Thermopyle posted:OOP is something everyone struggles with learning, and then once they get it it's hard to understand in retrospect why it was so hard to get. Dominoes posted:To expand on Thermopyle's explanation: Thanks for this. I'll try and break the parts down and see if I can grasp things a little better. Dominoes, I'm still a bit confused on how a class is more useful than just defining a function. Wouldn't it be easier to just define a function called sing_a_song(lyrics) and whenever I want it to sing a song, I just pass it whatever variable contains the song I want to sing? Ex: sing_a_song(big_booty_hoes) I realize this example is poor, I just pulled it from a text I was reading.
|
# ? Jun 20, 2014 17:44 |
|
Sure, but if you have a lot of things to sing (a song might have an artist, a title, the lyrics, and how long each lyric takes to sing), it can be painful to have to write out all the arguments every time. You can put all the arguments in one little object and then just refer to it later by one name later. That said, there's no right way or wrong way to use a class. When to use a class and when to just write a function is something you'll pick up as your experience and tastes develop.
|
# ? Jun 20, 2014 17:53 |
|
Hugh, your example works for learning how to structure classes, but it's too simple to show why they're useful. Try writing most of your code without classes. Take a look at the more complicated and messy bits, and try turning them into classes. See if it makes them easier to read and maintain.
|
# ? Jun 20, 2014 18:03 |
|
Hughmoris posted:Thanks for this. I'll try and break the parts down and see if I can grasp things a little better. That might be easier with this simple example because it ignores all the other things classes bring to the table. In fact, people tend to overuse classes when functions would work just fine. Anyway, the use for objects becomes more apparent in more complex cases. See if this helps: Python code:
|
# ? Jun 20, 2014 18:10 |
|
Thermopyle posted:Ask questions about what you don't understand. Would you do it in this case because slicing a string is a simple, cheap operation, so it doesn't need to be shown as explicitly running a method every time it's called? Would it be if it returned the second line instead of printed it, so you could do my_song.secondline = "My dog got run over by a truck" ? Guides I've read talk about replacing get and set, using instance variables as examples, but I've never found a use for a get or set method. Dominoes fucked around with this message at 18:25 on Jun 20, 2014 |
# ? Jun 20, 2014 18:16 |
|
Design question for you guys. My department (all SDETs) is trying to design a library to wrap and simplify interaction with several dozen internal REST API endpoints. The two competing strategies right now are to either have one endpoint per file, with just plain functions for doing GETs/POSTs/etc and building payloads and so on, or to put multiple endpoints in each file and have a class per endpoint, with the GETs/POSTs/etc in static methods (because there's no reason for an endpoint wrapper to be stateful or require initialization). I favor the first option because it seems simpler, but my coworkers favor the 2nd, and I don't really have any good pros/cons for either approach. Thoughts?
|
# ? Jun 20, 2014 18:23 |
|
I don't understand decorators or why they're useful. Can someone explain them to me?
|
# ? Jun 20, 2014 19:44 |
|
Thermopyle posted:Ask questions about what you don't understand. That is a really bad example.
|
# ? Jun 20, 2014 19:46 |
Suspicious Dish posted:That is a really bad example. It doesn't seem so bad, why not elaborate?
|
|
# ? Jun 20, 2014 20:04 |
|
fletcher posted:It doesn't seem so bad, why not elaborate? Because it's suspicious dish. edit: Now that I'm at my PC... I certainly wouldn't ever actually write those classes, but I think it quickly expands upon the original class to demonstrate the mechanics of classes just fine to show some ways a more complex thingamajig makes classes more useful. As such, it is a good example. It's just not something you'd actually write for real software. edit2: inserted important not into last sentence. Thermopyle fucked around with this message at 20:53 on Jun 20, 2014 |
# ? Jun 20, 2014 20:23 |
|
Thermopyle posted:Because it's suspicious dish. Yeah, the reason I didn't write anything before was because I was phone posting. I wanted to write a longer reply, but forgot about it for a bit while I did some real work. I'll try not to do that in the future, sorry. The exact reason I don't like it is because it's not how I'd ever write the code myself, and examples that tell you "here's a thing that you can do but don't do it this way!!!" are bad examples. I really wouldn't expect a Song class to have any methods, but it depends on the system we're building. If I'm building a karaoke system that plays a bad instrumental track synced up to words on a monitor, I'd structure it like this: Python code:
( I went looking for the "Goodbye, lovely "Car extends Vehicle" object-orientation tutorial" article in the initial post, but it seems the server went down. It has most of the same ideas. Just remembered to check the Wayback Machine, though, so: https://web.archive.org/web/20140416021831/http://lists.canonical.org/pipermail/kragen-tol/2011-August/000937.html )
|
# ? Jun 20, 2014 20:46 |
|
QuarkJets posted:I don't understand decorators or why they're useful. Can someone explain them to me? Decorators are really simple. Python code:
Python code:
Python code:
Another thing you can do is registration for a dispatch table. Frameworks like Flask use them for this purpose. Python code:
While decorators can lead to cleaner and more declarative code with higher-level structures, they can also lead to crazy meta-programming shenanigans.
|
# ? Jun 20, 2014 20:55 |
|
Suspicious Dish posted:Yeah, the reason I didn't write anything before was because I was phone posting. I wanted to write a longer reply, but forgot about it for a bit while I did some real work. I'll try not to do that in the future, sorry. I get what you're saying, but I just don't agree that it's always not useful for teaching the mechanics of how inheritance works. Especially not to the point where a person would be justified in saying it's "really bad". I think that, while there's an overlap between the two, teaching the mechanics of how OO works and how to design OO code are not exactly the same thing. Particularly in this instance where we're discussing an already-existing example and talking about how __init__ and self work.
|
# ? Jun 20, 2014 21:05 |
|
Dominoes posted:When would you use @property? My take is that in this case, it would allow you to write mysong.second_line instead of mysong.second_line(). I see a lot of people using @property a lot of different ways. Really, you could get away with never using them. However, I try to use them for every method without side effects. Sometimes you have regular attributes and then something that needs computed but is related to your regular attributes in some way and you can just pretend it is a regular attribute by using @property. It helps with refactoring. If you change from needing a function to not, you don't have to change any other code because everything just refers to the previously-@property-decorated method via the normal attribute syntax. It just makes sense. If you're getting a side-effect-free value from an object, why should you have to think about if it's a method or an attribute? In this sense, properties help with information hiding...you don't worry about implementation details. Alex Martelli talks some more about it here.
|
# ? Jun 20, 2014 21:19 |
|
Another good example of a useful decorator is functools.lru_cache, which is used to memoise functions. If you don't know what that means, basically it means caching the results of calling a function with a particular set of arguments, which is useful if you have a (side-effect-free) function that takes a lot of resources to execute and you don't want to dance around making sure you only call it when really necessary. You just decorate it with lru_cache and you get Python to check for you whether the function really needs to be called with the specified arguments or whether you already did it.
|
# ? Jun 20, 2014 22:10 |
|
Hammerite posted:Another good example of a useful decorator is functools.lru_cache, which is used to memoise functions. If you don't know what that means, basically it means caching the results of calling a function with a particular set of arguments, which is useful if you have a (side-effect-free) function that takes a lot of resources to execute and you don't want to dance around making sure you only call it when really necessary. You just decorate it with lru_cache and you get Python to check for you whether the function really needs to be called with the specified arguments or whether you already did it. I love this one. I'm always writing little scripts with sometimes long-running functions so I wrote my own that used memcached as a backend with month-long expiry times and decorate the poo poo out of any function that takes longer than a few milliseconds to run and have memcached running on all our lab computers.
|
# ? Jun 20, 2014 22:23 |
|
So what exactly does @property do, then?
|
# ? Jun 21, 2014 02:29 |
|
@property turns something into a "getter property": http://codepad.org/mUALdwe7 It does this through secret arcane magic known as "descriptors".
|
# ? Jun 21, 2014 02:35 |
|
Suspicious Dish posted:@property turns something into a "getter property": Everything to do with objects in python is done through descriptors: properties, slots, passing self, static and class methods
|
# ? Jun 21, 2014 05:38 |
|
I'm working on a GUI, but my question might have a more general answer. I want to check if a user has modified a document before they do something like quit, or open another document (because only one can be open at a time). I have a "global" self.warn_about_changes that tracks when changes have been made. There are 5 different functions where I need to check this and then possibly issue a warning, and this number could potentially go up in the future. At the moment what I have done is to paste this code into each of the functions:code:
At first I thought this would be a case where decorators might come in handy. I have never used them before, because I never saw the need, but this idea of wanting to first call another function seemed appropriate. But I cannot get this work properly. Are decorators appropriate here? If not, what's a better idea? If so, I don't understand how/where to define them.
|
# ? Jun 21, 2014 09:31 |
|
FoiledAgain posted:... I don't feel that I can confidently tell you whether this is a good idea design-wise or not (it seems like a decent enough idea to me, but I have limited experience designing big projects), but it is definitely something that you can use decorators for. It would be something like: code:
|
# ? Jun 21, 2014 10:58 |
|
I'd just write a method. The more I encounter metaprogramming like decorators, the more I appreciate clear and straightforward code.Python code:
|
# ? Jun 21, 2014 14:00 |
|
|
# ? Jun 11, 2024 04:13 |
|
I have to do something in python 2.6, apparently. Is there a preferred way to do code:
|
# ? Jun 21, 2014 16:30 |