|
trashmatic posted:If you mean that you can't call 'continue' inside a function and expect it to abort the loop it was called from... well, yes. You can't do that. I think this is coming in Python 3 though, could swear I read that somewhere Not that it helps the poster any, of course. I'd second everything you said, at any rate. EDIT: reading comprehension Can't do what you said, but Python3000 is adding multi-level continue statements, I think - i.e. if you're 3 nested for loops deep, you'd be able to have a single continue that says to break out of all 3. Regarding the original assertion, yea, it's not possible for a function to have any real idea about the state of things when it's being called - it is solely concerned with taking input and giving output, and that's it. bitprophet fucked around with this message at 16:59 on Feb 25, 2008 |
# ? Feb 25, 2008 16:57 |
|
|
# ? May 11, 2024 16:39 |
|
Ok, thanks for the advice trashmatic. This is what I've got after working on the program some more (before reading the reply) code:
I think I should be able to draw a flow chart now and think it through, but I better study for the midterm on wednesday first.
|
# ? Feb 26, 2008 01:03 |
I'm very curious about Python. I've never used it, but can't help but see everybody praising it, while everybody puts down PHP. I figured I might as well give it a whirl, but I was wondering if anybody had a good comparison of how something is written in PHP vs what the equivalent would look like in Python, specifically geared towards web development.
|
|
# ? Feb 26, 2008 02:13 |
|
Kresher posted:This is what I've got after working on the program some more (before reading the reply)
|
# ? Feb 26, 2008 02:18 |
|
fletcher posted:I'm very curious about Python. I've never used it, but can't help but see everybody praising it, while everybody puts down PHP. I figured I might as well give it a whirl, but I was wondering if anybody had a good comparison of how something is written in PHP vs what the equivalent would look like in Python, specifically geared towards web development. PHP isn't really as bad as its made out, at least not v5 onwards. The latest Object model is actually pretty close , but not quite (in a few key ways) to python. PHP is however a pretty focused little web language, but not really that good for much else, in comparison with other languages. I think the more interesting comparison is Perl v Python. I had it described like this. Perl is a chainsaw, Python is the surgeons scalpel. A Chainsaw will amputate a leg much faster than a scalpel, but the scalpel will do a cleaner job of it. Now contrast and compare with cutting down a tree. Python is elegant, and conservative. Its simple minded as well, and thats a good thing. You'll quickly come to apreciate how python achieves so much, in so small an amount a code with such a small but highly powerful number of primatives. But its a bit more of a bitch if you want to write 'line noise' regex heavy stuff.
|
# ? Feb 26, 2008 02:43 |
|
trashmatic posted:Try to clarify with your teacher what the "top level programming model" (I found nothing on Google) means with respect to your code. I think it's leading you further away from a solution. Functions are generally to take input data, perform a specific task, and return data -- not act as cutely named containers for fragments of your program's control structures. The following is what my prof showed us as an example of "top level" programming. code:
|
# ? Feb 26, 2008 04:38 |
duck monster posted:PHP is however a pretty focused little web language, but not really that good for much else, in comparison with other languages. What makes it not as good as python for a command line application? I've only done very basic things on the command line with php, but Python looks like it would be a lot more useful since it has GTK support.
|
|
# ? Feb 26, 2008 05:27 |
|
A few things I noticed about your code: First, you used "continue". It's occasionally acceptable to use "continue", but most of the time, you shouldn't. Instead, use an else clause. Pass parameters to your functions! You're using "global variables" now; everyone gets access to your variables. This quickly leads to maintainability problems. Instead of: code:
code:
There's another problem with your "user_win_loss" function: it doesn't return anything, even though it's supposed to. You call it thusly: code:
|
# ? Feb 26, 2008 05:40 |
|
I'm trying to put together a simple file-section app using wxpython, and I'm running into the same problem whether I run the test app on a Linux machine or under Windows. Basically, the wx.GenericDirCtrl control that I'm using isn't receiving the keyboard focus on app startup, which makes it impossible to control using anything but mouse clicks. This is annoying for the intended purpose, which is to let people select a movie file a directory using a remote control's arrow keys. The really annoying thing is that after adding "style=wx.DIRCTRL_SELECT_FIRST | wx.WANTS_CHARS" to the control, I can then use window.FindFocus() to see where the keyboard focus is supposed to end up, and it says that it's on the only GenericDirCtrl control in the app. But even so, it won't accept keyboard input unless I click on a filename in the control first. Scrolling the control doesn't do the trick; a filename remains selected but it's greyed out and no keyboard input is accepted. If I click on a filename first, everything works as intended, including the keyboard focus going back after it returns from the subprocess.check_call() that spawns the media player. It's just the initial keyboard focus on application startup that seems broken. I have used SetFocus() and all that, and it doesn't help. All it lets me do is pull the "focus" back to the window level. Am I missing something obvious here? I apologize for the terrible quality of this code. A lot of this was ripped from wxpython tutorials and example code while I tried to test out what I was doing. code:
Heresiarch fucked around with this message at 14:24 on Feb 26, 2008 |
# ? Feb 26, 2008 13:57 |
|
fletcher posted:What makes it not as good as python for a command line application? I've only done very basic things on the command line with php, but Python looks like it would be a lot more useful since it has GTK support. Its just better focused on the task. Pythons got a very clean model, and its designed all sharp like for that sort of thing. Truth is, its a generally better language. Not to say PHP can't do it. But there stuff php simply can't do as well, like threading , or pythons funky itterators and generators, and so on. But PHP is a fine web language as far as I'm concerned.
|
# ? Feb 26, 2008 16:48 |
|
Kresher posted:The following is what my prof showed us as an example of "top level" programming. To elaborate, this code is completely abusing global variables in order to make a so-called subroutine out of a function which should logically be taking two arguments (i.e. check_if_user_guessed(user_number, the_number)). The way it is now, if for whatever reason those variables are not in the global scope, it will fail. Terrible programming practice. Further, it's stupidly named because of course the user guessed, we want to know whether they guessed correctly! So we should write something more like check_if_guess_correct(users_guess, correct_answer). But wait, we can generalize here and just call it check_if_equal(a, b). Brilliant! OH WAIT THAT IS BUILT INTO THE LANGUAGE IT IS CALLED THE == OPERATOR. Your prof is doing a piss-poor job of teaching functional abstraction. He/she seems to have created the perception that Python functions work like assembly language subroutines or C preprocessor macros, where the code in the function is simply copy-pasted into your "top level" code at runtime. This is completely wrong. Try think of a function as a its own box, if you will, that is connected to its caller only by a thin straw. Nothing inside one box can directly alter anything happening inside the other box, it can only send things down the straw as either function arguments or return values and hope the other box does something intelligent with it. That's why you can't use a 'continue' statement inside a function and expect it to bust out of your main program's 'while' loop -- because your function is operating in its own little universe. Data in, data out. Of course, using global variables completely fucks with this picture because now you have these things that are visible and alterable by both boxes, so you can "cheat" that way. But the picture is still very different from the copy-n-paste idea. I dunno, hope this helps a little. trashmatic fucked around with this message at 19:58 on Feb 26, 2008 |
# ? Feb 26, 2008 19:29 |
|
trashmatic posted:There is no excuse for a professor to be showing code like this in an intro class. The rest of the program is pretty bad, too, but drat..... To be fair it does say in the code comments at the top ## 1st look at USAGE OF FUNCTIONS AND SUBROUTINES (THAT WE CREATE): ## FUNCTIONS WITH NO PASSAGE OF PARAMETERS ## (notice the parenthesis with no variables inside) I can still remember attending required basic programming classes in python when I'd already taught myself C. In all seriousness some students *really* struggled with functions after writing simple python programs without them. It wasn't until they'd grasped functions and parameter passing that the whole "don't use global variables" stuff was introduced.
|
# ? Feb 26, 2008 20:31 |
|
Yes, my class will be learning about local&global variables on friday after tomorrow's midterm. Also, my prof told us that she usually codes/teaches in Java, and this is her first time teaching the introductory CS course.
|
# ? Feb 26, 2008 23:46 |
|
The completely elegant solution to my earlier problem (as provided by a guy on #wxpython):code:
Heresiarch fucked around with this message at 00:37 on Feb 28, 2008 |
# ? Feb 28, 2008 00:34 |
|
bitprophet posted:EDIT: reading comprehension Can't do what you said, but Python3000 is adding multi-level continue statements, I think - i.e. if you're 3 nested for loops deep, you'd be able to have a single continue that says to break out of all 3. I just looked into that because I thought it sounded interesting. Apparently it is not being included; the PEP for it was rejected. http://www.python.org/dev/peps/pep-3136/
|
# ? Feb 28, 2008 01:32 |
|
Thanks for the correction, goes to show that I don't exactly follow the mailing list very carefully
|
# ? Feb 28, 2008 01:39 |
|
I just made thiscode:
code:
Also the order it puts elements is MAGICAL just like harry potter or a unicorn.
|
# ? Feb 28, 2008 04:56 |
|
duck monster posted:I just made this That's neat, is this a question? I've read the last two pages and I'm not sure. Dictionaries/hashes don't store ordering information and need sorting for keys or definitions.
|
# ? Feb 28, 2008 07:33 |
|
duck monster posted:Also the order it puts elements is MAGICAL just like harry potter or a unicorn. The ordering is quite consistent for being magical. Dictionaries are order by the generated index from the hash key, not the order in which the items were placed in the hash. 'job' apparently generates a lower index than any of the other values you've provided. If you need to retrieve values from the dict in a specific order, you're probably going to have to use a pre-sorted list of keys. Edit: Edit2: I'm surprised the reason behind this behavior is not discussed in the documentation. oRenj9 fucked around with this message at 17:55 on Feb 28, 2008 |
# ? Feb 28, 2008 09:35 |
|
oRenj9 posted:The ordering is quite consistent for being magical. Dictionaries are order by the generated index from the hash key, not the order in which the items were placed in the hash. 'job' apparently generates a lower index than any of the other values you've provided. Oooor you could wrap a sorted around it. for item in sorted(struct): ... e: Also, string formatting is your friend. code:
|
# ? Feb 28, 2008 15:16 |
|
or you can browse django's source and grab SortedDict. edit: Habnabit posted:Oooor you could wrap a sorted around it. for item in sorted(struct): ... that should be for key in sorted(dict): because sorted() on a dict only returns the keys also: oRenj9 posted:The ordering is quite consistent for being magical. It's consistent, but it differs accross implementations, which is exactly what the docs says. Python Library Docs posted:Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary's history of insertions and deletions. If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of (value, key) pairs using zip(): "pairs = zip(a.values(), a.keys())". The same relationship holds for the iterkeys() and itervalues() methods: "pairs = zip(a.itervalues(), a.iterkeys())" provides the same value for pairs. Another way to create the same list is "pairs = [(v, k) for (k, v) in a.iteritems()]". deimos fucked around with this message at 15:26 on Feb 28, 2008 |
# ? Feb 28, 2008 15:17 |
|
deimos posted:or you can browse django's source and grab SortedDict. Though they're still having issues, I believe the ordering of dicts is what causes the following lovely bug (I believe): http://code.djangoproject.com/ticket/4193 http://code.djangoproject.com/ticket/6374
|
# ? Feb 28, 2008 20:56 |
|
Wedge of Lime posted:Though they're still having issues, I believe the ordering of dicts is what causes the following lovely bug (I believe): Isn't that because they use regular dicts instead of the sorted alternative? I know that's why they're having some tests fail on PyPy. The tests expect CPython ordering.
|
# ? Feb 28, 2008 21:11 |
|
Newbie Python question: Say I have a CSV file like so: code:
I know there are different ways to read in the CSV file, and I would like to manipulate the resulting input so that it will work with pyChart. pyChart has its own way to read in CSV data using art_data.read_csv('file', '%d,%s:%d'): http://home.gna.org/pychart/doc/module-chart-data.html But it wants a specific format that my CSV file does not have. So, I ask, what is a good way to read in the CSV input and manipulate it to be as pyChart wants, either as a comma separated list [5, 10, 15] or as a tuple (5, 10, 15). I would like to come up with a method rather than just fixing the CSV data manually because that is exactly how it comes out of the simulation program that I use. So far, when I read it in using: adata = [] for line in csv.reader(open("hrrnum.csv").readlines()[1:]): adata.append(line) I get: code:
Thank you for any help in kicking me in the right direction.
|
# ? Feb 28, 2008 22:10 |
|
VirtuaSpy posted:Newbie Python question: Instead of copy/pasting, read the docs, try: chart_data.read_csv('file','%f,') heck I think chart_data.read_csv('file',',') should work.
|
# ? Feb 28, 2008 22:30 |
|
I knew that I was being an idiot somehow. Thank you.
|
# ? Feb 28, 2008 22:53 |
|
Oh I'm aware that the expectation of ordering is not a good one, but its annoying, since a handy little guide to ordering is provided by the structure provided.
|
# ? Feb 29, 2008 03:41 |
|
deimos posted:that should be for key in sorted(dict): I was trying to use the same variable names: duck monster posted:
|
# ? Feb 29, 2008 06:57 |
|
Habnabit posted:I was trying to use the same variable names: My bad. But still, it was worth clarifying.
|
# ? Feb 29, 2008 19:25 |
|
I'm using python with pySerial and wxWidgets to do some pretty sweet stuff with GPS devices. I just have to say that wx for python is amazingly simple to use. I love it to pieces.
|
# ? Feb 29, 2008 20:31 |
|
How do I install a .egg on python in os x? I have installed "easy_install" or at least I think I didcode:
The module is matplotlib and I just renamed the thing since it was driving me crazy.
|
# ? Mar 1, 2008 23:32 |
|
Help me not be retarded with regular expressions >>print re.search('\bword\b','something donkey1431 ; ,word , ; ,cat]') None I want to use a regular expression to check for the existence of a word in a sentence but I want to make sure it is not part of another word '\bword\b' has not been working for this purpose.
|
# ? Mar 2, 2008 00:19 |
|
Use r'\bword\b' or '\\bword\\b'
|
# ? Mar 2, 2008 00:57 |
|
Marzzle posted:How do I install a .egg on python in os x? I have installed "easy_install" or at least I think I did Don't execute it in python's interactive prompt, execute it into a command line (ie Terminal).
|
# ? Mar 2, 2008 01:07 |
|
Marzzle posted:As said before, easy_install is a command. It should be /System/Library/Python.framework/Versions/Current/bin/ unless you manually upgraded your python. So, $ easy_install [...].egg schzim posted:I want to use a regular expression to check for the existence of a word in a sentence but I want to make sure it is not part of another word '\bword\b' has not been working for this purpose. Raw strings were designed for things like this. Don't escape the backslashes, just do r'\bword\b'. Raw strings don't parse escape characters like \b or \n, but they will still parse \\' or \" like expected.
|
# ? Mar 2, 2008 05:25 |
|
PnP Bios posted:I'm using python with pySerial and wxWidgets to do some pretty sweet stuff with GPS devices. I just have to say that wx for python is amazingly simple to use. I love it to pieces. track down boa constructor. its a bit rusty, which is a shame because it would be loving fantastic otherwise. However its a really competent gui designer and generates nice and clean code. But due to its buggyness I'd recomend once designing the gui, using something else to finish it off. Crashing and losing code sucks. If the guy working on it wasnt so drat insular and let it open up a bit, it could be a 'killer app' in the python world as a full delphi-like system.
|
# ? Mar 2, 2008 14:18 |
|
Here's something basic, but it might represent a gap in my Python-fu. I have a bunch of floating point numbers of indeterminate size. Say, 3.02340, 0.8, 0.0051234. Now with the string formatting, it's fairly easy to specify the number of digits before the decimal points and the number of digits after: '%2f' % x => ['3.023400', '0.800000', '0.005123'] '%0.2f' % x => ['3.02', '0.80', '0.01'] What I'd like to do is something a bit different, show 3 digits precision overall. So it would look like: ['3.02', '0.800', '0.00512'] Any ideas on the most robust way to do this?
|
# ? Mar 2, 2008 19:40 |
|
outlier posted:Here's something basic, but it might represent a gap in my Python-fu. You can do this with the decimal module and setting precision to 3 on the context I guess. You're probably best served by casting it to an engineering string. ed: forgot link
|
# ? Mar 2, 2008 20:57 |
|
^^^ I'd have to do a speed test to see, but if I remember correctly, doing it with Decimals is a little slower.outlier posted:Here's something basic, but it might represent a gap in my Python-fu. There's not really an easy way to do this. Here's a small function I found in ASPN's python cookbook that I tidied up a little. code:
|
# ? Mar 2, 2008 20:59 |
|
|
# ? May 11, 2024 16:39 |
|
mantaworks posted:Don't execute it in python's interactive prompt, execute it into a command line (ie Terminal). I have run the "ez_setup.py" in IDLE's prompt, this was right yes? Now when I try easy_install in a terminal I just get code:
|
# ? Mar 3, 2008 00:59 |