|
Man you might as well make a useful decorator, like one that adds TCO to functions. It was pretty fun to write.
|
# ? Sep 7, 2009 18:36 |
|
|
# ? May 9, 2024 18:35 |
|
Habnabit posted:Man you might as well make a useful decorator, like one that adds TCO to functions. Wow, this is really cool! Do you think people would get pissed off if I used this decorator freely in projects like it was a built-in?
|
# ? Sep 7, 2009 18:57 |
|
Yes. And you can re-write most code to not depend on tco quite easily.
|
# ? Sep 7, 2009 19:07 |
|
Aw Quit ruining my fun.
|
# ? Sep 7, 2009 19:08 |
|
tripwire posted:Wow, this is really cool! Do you think people would get pissed off if I used this decorator freely in projects like it was a built-in? Yes, people would get pissed. Oh god it burnses us.
|
# ? Sep 7, 2009 19:19 |
|
Well, the resulting bytecode really isn't that bad! The hack itself isn't too pretty, and it has a few restrictions on how the function has to be defined and called, but I like it.
|
# ? Sep 7, 2009 20:31 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:19 on Mar 13, 2017 |
# ? Sep 7, 2009 20:50 |
|
Habnabit posted:Well, the resulting bytecode really isn't that bad! The hack itself isn't too pretty, and it has a few restrictions on how the function has to be defined and called, but I like it. I thought it was cool too. While python is a breath of fresh air compared to many lower level languages, I don't know whether I agree 100% with every aspect the python ideology, specifically in the case of tail call recursion. I agree that python's legibility and ease of use are among its strongest points and as Guido van Rossum says, adding in tail call optimization at the very least complicates backtraces, but even if that were fixed it would still be unacceptable to Guido: ideologically it prioritizes extended functionality over legibility and ease of use. His words on multi-line lambdas indicated he felt pretty strongly about this principle: Guido van Rossum posted:And there's the rub: there's no way to make a Rube Goldberg language feature appear simple. Features of a programming language, whether syntactic or semantic, are all part of the language's user interface. And a user interface can handle only so much complexity or it becomes unusable. This is also the reason why Python will never have continuations, and even why I'm uninterested in optimizing tail recursion. But that's for another installment. I still think though that if you are going to call the language multi-paradigm, you still provide some minor facilities to ease the use of things people might want to experiment with, regardless of whether or not that paradigm is deemed "unpythonic". I admit there might be a risk of the language becoming a bit more like perl, in that there would be multiple ways of accomplishing the same thing with similar efficiency as opposed to the pythonic maxim of only having one obvious best way to do things. So what though? If it increases the number of people using python by making it more accommodating to a wide variety of styles and backgrounds its better for everyone, as far as I can see.
|
# ? Sep 7, 2009 20:51 |
|
GregNorc posted:
You're using py3k, right? Use range(), not xrange(). Previously, range() returned a list and xrange() returned an iterator, but they simplified it to just having range() which returns an iterator.
|
# ? Sep 7, 2009 20:51 |
|
Habnabit posted:Well, the resulting bytecode really isn't that bad! The hack itself isn't too pretty, and it has a few restrictions on how the function has to be defined and called, but I like it. I think it's awesome, but I wouldn't use bytecode hacking in production code
|
# ? Sep 7, 2009 21:32 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:19 on Mar 13, 2017 |
# ? Sep 7, 2009 21:33 |
|
GregNorc posted:that solved it but printing the list gives me an error You do realize that one of the most visible changes in py3k is that print is a function, right?
|
# ? Sep 7, 2009 21:34 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:19 on Mar 13, 2017 |
# ? Sep 7, 2009 21:37 |
|
GregNorc posted:Oops, no I didn't. create_deck() just returns the deck, but you have to assign it to something in the module namespace. So instead of just calling create_deck(), you should say something like: code:
|
# ? Sep 7, 2009 21:43 |
|
tripwire posted:... and as Guido van Rossum says, adding in tail call optimization at the very least complicates backtraces, but even if that were fixed it would still be unacceptable to Guido: ideologically it prioritizes extended functionality over legibility and ease of use. If it didn't have that ideology it would be a remarkably different language. Accepting a feature like (automatic) TCO sets a precedent for more complex language features, as well as bloating the cpython interpreter. quote:I still think though that if you are going to call the language multi-paradigm, you still provide some minor facilities to ease the use of things people might want to experiment with, regardless of whether or not that paradigm is deemed "unpythonic". At this point it stops being python though. Python isn't about experimentation with language facilities but being able to write and maintain simple readable code. quote:I admit there might be a risk of the language becoming a bit more like perl, in that there would be multiple ways of accomplishing the same thing with similar efficiency as opposed to the pythonic maxim of only having one obvious best way to do things. Except maintenance programmers. Software is frequently read and maintained, and so having a small feature set ensures alleviates the burden. By adding spurious features replicating existing functionality you dramatically increase the amount of knowledge required to maintain existing programs. I'm not saying I agree with the choices python has made, but python is not the sort of language to bloat for the sake of expressiveness at the cost of complexity.
|
# ? Sep 7, 2009 21:47 |
|
As a fairly novice programmer who's beginning to write more complex pieces of code, I'd like more advanced programmers to look at my code and tell me how badly I've hosed up. I've currently got a class I'm writing that takes a filename of a downloaded TV show and tries to parse out the episode and season number. It's about 100 lines long so far. Is there a good site out there focusing on hooking up programmers who are willing to look at other people's code? Would someone here like to look at it and make some comments for me? edit: Hell, i'll throw it up on pastebin and if someone wants to look they can just do it.... http://pastebin.com/ma24c43 sample use: code:
Thermopyle fucked around with this message at 22:13 on Sep 7, 2009 |
# ? Sep 7, 2009 22:06 |
|
Recently I discovered the wonderfilness of "import pdb; pdb.set_trace()" to help debug my code. The only problem is that, for some reason it always screws up my terminal. If I exit the debug prompt with "exit", it'll be OK, but if I use ctrl+c or if I save a file in my django folder (and thus triggering a development server restart), it renders my terminal unusable. When I type something, the text doesn't show up on the screen. The text is there, I just can't see it. This is very annoying. The only way to fix it is to crtl+d, then relaunch gnome-terminal. This happens with urxvt as well as gnome-terminal. Is this a known issue, and if so are there work arounds?
|
# ? Sep 8, 2009 06:10 |
|
I've found pdb's postmortem pm() to be pretty handy as well, and you don't even have to instrument your code ahead of time necessarily.
|
# ? Sep 8, 2009 06:29 |
|
nbv4 posted:When I type something, the text doesn't show up on the screen. The text is there, I just can't see it. This is very annoying. The only way to fix it is to crtl+d, then relaunch gnome-terminal. This happens with urxvt as well as gnome-terminal. Is this a known issue, and if so are there work arounds? I can't help with the particulars of pdb, but you can type 'reset' and the terminal should... reset.
|
# ? Sep 8, 2009 07:27 |
|
Thermopyle posted:As a fairly novice programmer who's beginning to write more complex pieces of code, I'd like more advanced programmers to look at my code and tell me how badly I've hosed up. Woah. Actually, given the number of datums you're trying to parse out of the string, 100 lines may not be unreasonable. Your code isn't too bad. A few general comments: * Take the regexes out of the class and express them as constants. This will make your class leaner and easier to read. They could be written more simply as: code:
* Maybe break out code blocks like the one that tests for a valid show name or the bit that parses episodes numbers into separate methods or functions. Again, it makes things more readable. * You're using a few magic numbers in there, i.e. naked constants like '2' and '3'. These would be better phrased as (say) 'removal_cutoff' and 'max_ep_index' for readability (again). * You're not catching the problem that occurs when no regex is matched. Not necessarily bad - maybe you want a TVShow object with no fields filled in - but alternatively throwing an exception might be called for.
|
# ? Sep 8, 2009 11:53 |
|
That and there is almost no reason to make it a class over a function that returns a string
|
# ? Sep 8, 2009 12:02 |
|
tef posted:That and there is almost no reason to make it a class over a function that returns a string I'd assumed the developer may have wanted to keep the class around, to query specific fields and so on, but yes. If the only necessary return value is the generated name, then a class is overkill.
|
# ? Sep 8, 2009 13:26 |
|
Thermopyle posted:As a fairly novice programmer who's beginning to write more complex pieces of code, I'd like more advanced programmers to look at my code and tell me how badly I've hosed up. I've been fooling around with TV Show name parsing for a LONG time, and depending on how much you delve into it, you can see all my work at Keasby's google code repo.
|
# ? Sep 8, 2009 14:24 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:19 on Mar 13, 2017 |
# ? Sep 8, 2009 22:48 |
|
You really need to step back and read about some fundamental programming concepts such as how subroutines and variable scopes work.
|
# ? Sep 8, 2009 23:16 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:19 on Mar 13, 2017 |
# ? Sep 8, 2009 23:45 |
|
GregNorc posted:Everything I've seen either assumes no prior knowledge and spends page after page explaining really simple things like the difference between int and float, or assume I'm an expert at programming who's just never used python before. No offense, but looking at what you pasted in there, I'm not sure you actually have any prior knowledge to speak of. Read the tutorial listed in the OP. Let's go through the problem bits to illustrate exactly why we're being so harsh. code:
|
# ? Sep 9, 2009 00:20 |
|
GregNorc posted:ATLbeer's solution was pretty cool but I seperated creating the deck and shuffling it into two functions so it's a bit easier to read (right now I wanna work on the basics before I try anything too complex) You are correct, shuffle only requires one argument. But on line 16, you are calling it without any arguments. There is no introspection done into the names of functions or variables by the compiler. Calling a function "shuffle_deck" does not link it in any way to objects or concepts that may be named "shuffle" or "deck". Have you read through the standard tutorial? If there are parts of it that feel too remedial, then by all means at least skim them, but it sounds like there is still going to be some useful stuff in there. And as mentioned earlier, Dive Into Python is a great resource. It does bill itself as a "Python book for experienced programmers", but I think that's a bit of an exaggeration.
|
# ? Sep 9, 2009 00:20 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:19 on Mar 13, 2017 |
# ? Sep 9, 2009 02:06 |
|
GregNorc posted:File "hackjack.py", line 17, in <module>
|
# ? Sep 9, 2009 05:02 |
|
How exactly would I implement ping in 2.6? I've found this example: http://code.activestate.com/recipes/409689/ But it doesn't actually work (I get a socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions). I was going to read through that example and try to understand it, but if it doesn't work, I'm not sure what to do. A bit more googling suggests that the socket I was trying to use was already bound, but surely I don't need to do this for PING??
|
# ? Sep 9, 2009 11:53 |
|
You need to be at least in the local admins group on NT to use raw sockets.
|
# ? Sep 9, 2009 11:57 |
|
And root on unix.
|
# ? Sep 9, 2009 12:03 |
|
outlier posted:Woah. Thank you! Exactly the sort of information I was hoping for. tef posted:That and there is almost no reason to make it a class over a function that returns a string You know, I started to write a function and then I decided I'm going to stop dicking around and make myself learn how the hell classes work. Besides, in the future I may expand this into a more full-featured program where I'd like to be able to query each bit of info. I was under the impression that a class would would facilitate that better. Lurchington posted:I've been fooling around with TV Show name parsing for a LONG time, and depending on how much you delve into it, you can see all my work at Keasby's google code repo. Thanks, I'll look it over.
|
# ? Sep 9, 2009 18:56 |
|
What's a good way to learn GUI programming? For the last little app I wrote (a disk benchmark - http://sourceforge.net/apps/mediawiki/copytest/), I used TkInter, and managed to get an ugly but functional GUI working. However, it had some issues, like the fact that the GUI froze every time the program was actually doing anything. Does this just mean that I should have created new threads for things besides the GUI, or was I doing something else wrong? I want to learn how to use PyQt, because it actually has a utility for building GUIs instead of having to hand-code them. However, there don't seem to be any good PyQt tutorials out there, and the documentation I can find is a confusing mix of Python and C++. BeefofAges fucked around with this message at 20:33 on Sep 9, 2009 |
# ? Sep 9, 2009 20:28 |
|
BeefofAges posted:However, it had some issues, like the fact that the GUI froze every time the program was actually doing anything. Does this just mean that I should have created new threads for things besides the GUI, or was I doing something else wrong? Yes. You should spawn new threads to handle any processing that takes any decent amount of time. This separates your CPU-crushing calculations from hogging the resources required to render the GUI properly. BeefofAges posted:I want to learn how to use PyQt, because it actually has a utility for building GUIs instead of having to hand-code them. However, there don't seem to be any good PyQt tutorials out there, and the documentation I can find is a confusing mix of Python and C++. I personally like to build a GUI by hand just because I feel like I have a lot more control over it's behavior, also it allows for better understanding of the GUI library that you are using. I am preferential to wxPython because it has great cross-platform performance, uses native widgets, and is documented very well. It also has a demo available that demonstrates the usage of all widgets. Modern Pragmatist fucked around with this message at 22:02 on Sep 9, 2009 |
# ? Sep 9, 2009 21:57 |
|
BeefofAges posted:I want to learn how to use PyQt, because it actually has a utility for building GUIs instead of having to hand-code them. However, there don't seem to be any good PyQt tutorials out there, and the documentation I can find is a confusing mix of Python and C++. Unfortunately, the tutorials are that way because PyQt is a confusing mix of Python and C++. For example, to connect a slot, you have to declare the slot's signature in C++ syntax as a string literal. You might try PyGTK, using Glade through the gtk.glade package.
|
# ? Sep 10, 2009 01:58 |
|
With any luck Nokia's PySide, a recreation of PyQT, will ameliorate those horrors, as QT itself is fantastic and it would be nice to use in Python without dumb poo poo like that, but PySide is new and it's going to be a while before they even start to make the interface pythonic.
|
# ? Sep 10, 2009 02:28 |
|
Thanks for the comments and suggestions, I'll check out those other toolkits.
|
# ? Sep 10, 2009 03:20 |
|
|
# ? May 9, 2024 18:35 |
|
Hey guys, I've been working on parsing (tailing) a named pipe. Essentially it's the haproxy traffic logs for a pair of servers that I have configured syslog to output to a named pipe. Obviously it's a fair bit of traffic (upto 3k hits/s per server), but I am finding that simply tailing the file, without any processing, is taking up 15% of a core. In contrast HAProxy takes 25% and syslogd takes 5% with the same load. Is this normal and am I gonna have to go get my WD-40 and K&R? Or am I just stupid and doing things horribly wrong. Here is my code (this is what I tested to get the above CPU percents, there's more code that barely raises the CPU which runs some regexes using coroutines but this bit of code consumes 10-15% CPU): code:
deimos fucked around with this message at 22:02 on Sep 10, 2009 |
# ? Sep 10, 2009 21:58 |