|
PiotrLegnica posted:Do you know what functools.partial does? Don't use it, just bind the signal to a no-arg method and use instance variables from there. Ah, that works perfectly. Thanks. I was only using partial because I couldn't figure out how to use a method with arguments as a PyQT slot, and that's what Google turned up.
|
# ? Sep 25, 2012 20:24 |
|
|
# ? May 9, 2024 07:05 |
|
Here's two rules:
And here's some specializations of those rules:
Suspicious Dish fucked around with this message at 20:54 on Sep 25, 2012 |
# ? Sep 25, 2012 20:27 |
|
Suspicious Dish posted:Here's two rules: I think I'm broken, I can't translate it into a simple comparison. I know how I want them to end up, but even comparing two random values from my set just makes my brain stop. Ugh. Thanks, though. Maybe if I walk away from it one more time and look again.
|
# ? Sep 25, 2012 21:26 |
|
Carthag posted:How would you guys go about sorting open-ended "ranges"? I have a bunch of numbers in tuples that represent ranges (start, end) and I want to sort these in a specific way. Let me check whether I understand this. You're trying to sort open intervals, and None is a stand-in for minus infinity or infinity? And expected is the ordering you want to end up with? (I'm not sure whether this is a correct understanding. In particular, if these really are meant to be open intervals, then for example (2, 2) and (3, 3) both represent the empty set, and so given that they're equal they should surely be placed adjacent in expected.) I can't deduce any logic behind the ordering given in expected. Is this where you are running into difficulty, or is it something else that is the problem.
|
# ? Sep 25, 2012 21:38 |
|
My data consists of both single values and ranges between values, so actually the original data set is more like this (in sorted order):Python code:
|
# ? Sep 25, 2012 22:36 |
|
Suspicious Dish posted:Here's two rules: Python code:
|
# ? Sep 25, 2012 23:08 |
|
Thanks, but that's not my comparison; I guess I'm bad at explaining. After some fiddling, I've come up with this horror which sorts as expected, but holy moly is it ever ugly.Python code:
|
# ? Sep 26, 2012 00:38 |
|
Try just using the tuples like (3, 3). It should work.
|
# ? Sep 26, 2012 00:43 |
|
Yeah but the comparison shouldn't put the unbounded ranges on the outside, they need to hug the numbers, like in my previous post, (None,3) means "less than 3" and should sort right before 3, not at the beginning of the list. Also the one I just posted doesn't work either, argh. Carthag Tuek fucked around with this message at 00:57 on Sep 26, 2012 |
# ? Sep 26, 2012 00:53 |
|
The usage of cmp is deprecated (and removed in Python 3), so I would suggest using a class for this:Python code:
Edit: bonuspoints if you derive from namedtuple and replace None with infinity. M31 fucked around with this message at 13:44 on Sep 26, 2012 |
# ? Sep 26, 2012 13:29 |
|
Thanks that sorts like I wanted
|
# ? Sep 26, 2012 15:17 |
|
I was going to suggest implementing a class with a comparison operator and using @total_ordering but M31 already did. So I am seconding his suggestion.
|
# ? Sep 26, 2012 19:57 |
|
Very basic question that I can't work out.Python code:
code:
Is it not OK to modify a list by iterating over it like that? davebees fucked around with this message at 22:22 on Sep 26, 2012 |
# ? Sep 26, 2012 22:17 |
|
Starno posted:Very basic question that I can't work out. Yeah, you have to put objects into the list directly. What you're doing now is looping over the list, and saying let 'x' be the name of this element. Now let 'x' be the name for the number '1'. Python assignment works a lot like "naming" things, rather than being a reference or pointer like in java or c.
|
# ? Sep 26, 2012 22:27 |
|
Emacs Headroom posted:Yeah, you have to put objects into the list directly. What you're doing now is looping over the list, and saying let 'x' be the name of this element. Now let 'x' be the name for the number '1'. Cool OK. So what would be the typical method for doing something to every item in a list? Something like this? Python code:
|
# ? Sep 26, 2012 22:41 |
|
Starno posted:Cool OK. So what would be the typical method for doing something to every item in a list? Something like this? You can use something like Python code:
OnceIWasAnOstrich fucked around with this message at 00:45 on Sep 27, 2012 |
# ? Sep 26, 2012 22:53 |
|
Starno posted:Cool OK. So what would be the typical method for doing something to every item in a list? Something like this? Create a new one with a list comprehension (or map/filter). And be careful with using [something] * n — you get n references to that object, so if it's mutable, modifying one is visible in the entire list (which usually surprises people). And try not to overshadow built-in names (list). Python code:
|
# ? Sep 26, 2012 23:36 |
|
Starno posted:Cool OK. So what would be the typical method for doing something to every item in a list? Something like this? Yeah that's a normal thing to do. Couple of quick notes: 1) You should in general not name things "list", since that's already in the default namespace and you never know when you might have to call it -- e.g., getting only the unique elements in a list l can be accomplished easily by calling Python code:
Python code:
|
# ? Sep 27, 2012 00:27 |
|
How does one write a python module in C?
|
# ? Sep 28, 2012 15:19 |
|
Well, step 1 is reading this. The detailed API reference is here.
|
# ? Sep 28, 2012 15:26 |
|
And if you're just wrapping some C code, swig is extremely easy and straightforward. edit: vvvvv worksforme Emacs Headroom fucked around with this message at 15:31 on Sep 28, 2012 |
# ? Sep 28, 2012 15:27 |
|
Emacs Headroom posted:swig is extremely easy and straightforward. Might want to double check your facts there, buddy.
|
# ? Sep 28, 2012 15:28 |
|
I've found ctypes works great for the system programming I do (I work on an OS installer and have to do a ton with C libraries)
|
# ? Sep 28, 2012 17:15 |
|
I've decided that 79 characters per line is too restrictive for me on the project I'm working on, and that 89 characters per line is more appropriate. But I'm loath to make an exception for docstrings, so I'm most likely just going to wrap them at the 89 character line as well. So some of the lines in the docstrings are going to end up longer than 79 characters after being treated by the automatic formatter. I think it is reasonable enough just to dismiss this as NMFP, but does the idea make anyone froth with rage?
|
# ? Sep 28, 2012 23:43 |
|
Hammerite posted:I've decided that 79 characters per line is too restrictive for me on the project I'm working on, and that 89 characters per line is more appropriate. Special cases aren't special enough to break the rules.
|
# ? Sep 29, 2012 00:59 |
|
Hammerite posted:I've decided that 79 characters per line is too restrictive for me on the project I'm working on, and that 89 characters per line is more appropriate. But I'm loath to make an exception for docstrings, so I'm most likely just going to wrap them at the 89 character line as well. So some of the lines in the docstrings are going to end up longer than 79 characters after being treated by the automatic formatter. I think it is reasonable enough just to dismiss this as NMFP, but does the idea make anyone froth with rage? I stick to PEP8 on almost everything except line length. I've got a ton of monitor resolution to work with and sometimes longer lines just work better. (Almost always 79 is enough, though) Plus, I get a lot of enjoyment out of thinking about someone in several years working on my code and ranting about the line length.
|
# ? Sep 29, 2012 01:02 |
|
Yeah, what is that 10 characters buying you that you need so much? Is it because you're nesting too heavily? Are you not breaking up long line statements to make them more readable, such as chained methods?
|
# ? Sep 29, 2012 01:02 |
|
Sylink posted:How does one write a python module in C? If you're willing to use C++, boost::python is actually pretty awesome for this once you figure out how to set it up properly (which is needlessly hard).
|
# ? Sep 29, 2012 03:25 |
|
Sylink posted:How does one write a python module in C? People have mentioned SWIG, but even the original author Dave Beazely does not advocate SWIG anymore. Boost::Python is fine if you don't mind depending on Boost. Rolling your own with the Python C API is also fine, if you don't mind tons of boilerplate and explicit refcount management. Some other options to consider are cython and Numba.
|
# ? Sep 29, 2012 05:35 |
|
BigRedDot posted:Boost::Python is fine if you don't mind depending on Boost.
|
# ? Sep 29, 2012 06:15 |
|
Cython is cool. Writing a C module "from scratch" isn't that hard. There's also gobject-introspection, but that's a whole other can of worms that I shall not go into.
|
# ? Sep 29, 2012 06:17 |
|
Depending on what you want to do, can't ctypes work as well? And speaking of boost::python, does anyone have a good guide to it? All the documentation I've ever found has been old or incredibly confusing. Or both.
|
# ? Sep 29, 2012 06:33 |
|
Maluco Marinero posted:Yeah, what is that 10 characters buying you that you need so much? Is it because you're nesting too heavily? That is difficult for me to answer because I don't know what "nesting too heavily" means (to you or to anyone else). I am breaking up long statements into multiple lines, but I was finding that I had to do so a bit more frequently than I was entirely happy with. However, I have concluded that I quite like the width and readability of my code at 89 characters, so I am going to stick to it.
|
# ? Sep 29, 2012 06:51 |
|
Hammerite posted:That is difficult for me to answer because I don't know what "nesting too heavily" means (to you or to anyone else). I am breaking up long statements into multiple lines, but I was finding that I had to do so a bit more frequently than I was entirely happy with. However, I have concluded that I quite like the width and readability of my code at 89 characters, so I am going to stick to it. By nesting I just mean nested for and if loops, so you end up a long way across the screen before you even have a character on the line. Usually in these cases you can greatly improve the readability of your code by extracting a few methods or using something from itertools to better handle the iteration. Up to you of course, if you only have yourself to look after then go for it, but personally I prefer to do my best to stick to the standard.
|
# ? Sep 29, 2012 06:59 |
|
Last time I used Boost.Python I just ended up using handle/object for automatic refcounting and CPython API directly. The documentation really is terrible. Cython is much better.Thermopyle posted:I stick to PEP8 on almost everything except line length. I've got a ton of monitor resolution to work with and sometimes longer lines just work better. (Almost always 79 is enough, though) I use PEP8 very selectively. I don't like its advice on several things (no one-line imports, no spaces around = in keyword arguments, weird aligning rules and probably some other things I don't remember right now). I can fit at least 120 characters on my smallest screen, and I don't care.
|
# ? Sep 29, 2012 20:21 |
|
This might not be exactly Python related, but - I just installed EasyEclipse for Python after a reformat and sure enough it was easy to configure for Python. Today I discovered something super annoying though - using raw_input() in the Console appears to append an extra linebreak at the end of each input. I suppose it simply thinks the linebreak that is used to send whatever is typed into the Console should also be appended to the return value. Running the programs from Windows's command line works as expected, without no extra linebreaks. Sure strip() or something works, but that's dumb as it might cause some problems when I don't really want to use it. Does anyone have any clue how to unfuck EasyEclipse? Or any other IDE recommendations for Windows?
|
# ? Sep 29, 2012 21:12 |
|
Python 3.3.0 is out python.org posted:New syntax features:
|
# ? Sep 30, 2012 23:59 |
|
Hed posted:Python 3.3.0 is out indeed. It looks like almost everything I use is compatible with 3.3, with the notable exception of SciPy. I'm using development versions of a lot of packages, but I'm okay with that for the moment. It also looks like Pygments was finally updated for the new yield from syntax; this section of the docs didn't have any syntax highlighting when I downloaded a copy of the documentation on Saturday EDIT: Damnit, there's a package that doesn't work with 3.3 for this reason and I'm the one who did the 3.x compatibility changes. I'm not looking forward to fixing this. Lysidas fucked around with this message at 18:59 on Oct 1, 2012 |
# ? Oct 1, 2012 18:22 |
|
JOHN SKELETON posted:Or any other IDE recommendations for Windows? I've got a huge boner for PyCharm + vim plugin, personally. However, it's a pretty steep price at $99 . I use it for work though, and I've had nothing but positive things to say about it. You can do horizontal or vertical splits, the IDE tries to autodetect your calls and resolve them for you, you can autooptimize code with one button...list goes on and on . Otherwise, if you want to go the free route, I'd go Vim + good 'ol Python command line interpreter.
|
# ? Oct 3, 2012 11:14 |
|
|
# ? May 9, 2024 07:05 |
|
pliable posted:I've got a huge boner for PyCharm + vim plugin, personally. However, it's a pretty steep price at $99 . I use it for work though, and I've had nothing but positive things to say about it. You can do horizontal or vertical splits, the IDE tries to autodetect your calls and resolve them for you, you can autooptimize code with one button...list goes on and on . I bought the student version of PyCharm for $29 and love it, although I mostly use it on OSX. I'm assuming its pretty similar for Windows since its all Java-based, and if you are using it for some open-source project you can get a free license.
|
# ? Oct 3, 2012 13:53 |