|
I've spent the last week or so writing a battle simulator for the board game Axis & Allies. I'm fairly new to programming, and it's my first program that uses classes. I'd appreciate any constructive criticism, or any other advice or opinions. http://pastebin.com/f741d6759
|
# ? Jun 30, 2008 04:38 |
|
|
# ? May 10, 2024 14:01 |
|
Flea110 posted:I've spent the last week or so writing a battle simulator for the board game Axis & Allies. I'm fairly new to programming, and it's my first program that uses classes. Line 112, you want to use the "copy" module. You should be using dicts instead of tuples and lists. That would make line 137 read something like units['battleship']['count'] *= 2. But really, you should be doing proper OOP-- this sort of thing is the textbook example of it. Don't use range, it creates an entire list. Use xrange, it creates an iterator. The main problem is that your code reads like C, not like Python
|
# ? Jun 30, 2008 05:40 |
|
I work for the Systems group at my school and I'm supposed to write an Account Request Form where students sign up for a UNIX account so they can access our servers and use our lab computers. Originally the sign up process was done as follows: 1. Run an account program on one of our lab machines with requested username, and other bits of information like first and last name. 2. Fill out and sign a paper form with all sorts of information like your name, school ID number, phone number, which type of account you're requesting (are you a CS undergraduate, graduate student, just taking a CS course but not a major?), etc. I'm supposed to move that second form online, and I was originally doing it in Perl using CGI.pm. My friend then told me that I should be using templates and that the HTML should be separate from the logic, so I tried to go and learn how to use HTML::Template. Now I'm realizing that this code is probably going to be a big unreadable mess as I do not have much experience in CGI or Perl. I'd appreciate some opinions on how you guys think this should be done. Is there an HTML template module for Python? Should I be doing this with CGI?
|
# ? Jun 30, 2008 19:47 |
|
Yeah there is. It seems like the best solution here would be Django. It's a web framework for Python. It features a very good templating engine, database abstraction and is generally very easy to use. I'd go so far as to say it's one of the most well planned out and best programming tools today. There's a Django thread here in CoC, so I'd suggest starting there.
|
# ? Jun 30, 2008 19:55 |
|
If it's a simple enough page, web.py with mako is also suitable and simple.
|
# ? Jun 30, 2008 20:00 |
|
I may be an idiot (most likely scenario) but it seems that the csv module prints "\r\r\n" for new lines in Windows and I don't see anyway to change this functionality. Edit: Further research shows I'm an idiot. I wasn't opening the file with "wb". DTMTCM fucked around with this message at 20:55 on Jun 30, 2008 |
# ? Jun 30, 2008 20:51 |
|
checkeredshawn posted:I'd appreciate some opinions on how you guys think this should be done. Is there an HTML template module for Python? Should I be doing this with CGI? Django is overkill for this. Web.py is nice and simple (read the tutorial), although it doesn't have the most extensive documentation. Web.py has a templating system, allowing you to write stuff like this: templates/index.html: code:
code:
If you have any difficulties, talk to us on #cobol at irc.synirc.net.
|
# ? Jun 30, 2008 23:14 |
|
Here's an oddity I can't seem to figure out. I have a list of objects that I can interate through but, how to I operate on them? This is a very simplistic example but, demonstrates what I want code:
Caveat: without the obvious iterating via Index. I want to maintain using it by objects? Is that possible or do I have to use indices? Not* for x in range(len(k)): k[x] = 0 ATLbeer fucked around with this message at 00:24 on Jul 1, 2008 |
# ? Jul 1, 2008 00:17 |
|
You can try code:
code:
|
# ? Jul 1, 2008 00:24 |
|
code:
|
# ? Jul 1, 2008 00:25 |
|
|
# ? Jul 1, 2008 00:27 |
|
Well.. I guess I should have used my complete example. Simplifying it kind of changes the complexity. This is my current function. code:
It's a multi-dimensional table of unknown dimensions and each cell needs to be slightly massaged to eliminate some unneeded cruft I would rather code:
ATLbeer fucked around with this message at 00:33 on Jul 1, 2008 |
# ? Jul 1, 2008 00:27 |
|
Watch out .... in your first example, you're modifying the arguments in place and then returning a copy. So if someone passes a list to that function and tries to use the same list later on, they'll find it's been mysteriously changed. Why not just do code:
Also, it's generally not cool to just use catch-all except clauses because then they also catch KeyboardInterrupt exceptions and whatnot. hey mom its 420 fucked around with this message at 00:35 on Jul 1, 2008 |
# ? Jul 1, 2008 00:32 |
|
Bonus posted:Watch out .... in your first example, you're modifying the arguments in place and then returning a copy. So if someone passes a list to that function and tries to use the same list later on, they'll find it's been mysteriously changed. Can I get the Python man pages that explain that syntax
|
# ? Jul 1, 2008 00:34 |
|
Sure, it's section 5.1.4 here http://docs.python.org/tut/node7.html It's quite simple actually, this is how list comprehensions behave: code:
|
# ? Jul 1, 2008 00:39 |
|
Bonus posted:Sure, it's section 5.1.4 here http://docs.python.org/tut/node7.html Are there enough of these for a Python tips and tricks thread?
|
# ? Jul 1, 2008 00:44 |
|
I think Python's standard library surprises me the most when it comes to discovering new things. PyMOTW is great for learning about parts of the library you've never even noticed. I also like using my browser's search on the standard library's index. Just search for whatever you're dealing with, you'll probably find something relevant.
|
# ? Jul 1, 2008 00:51 |
|
Recommending Django for writing simple cgi scripts is a bit overkill don't you think? I've used a variety of methods for writing web applications and I like mod_python the most. web.py and other cgi modules feel clunky to me. I guess it's personal preference, but mod_python makes writing cgi in python feel more like python.
|
# ? Jul 1, 2008 12:33 |
|
I just set up mod_wsgi and I like it a lot, it's super easy to get going and I imagine it would be pretty easy to invoke scripts using subpress.Popen and pipe their stdout back to the user.
|
# ? Jul 1, 2008 21:23 |
|
Bonus posted:def cleanse_table(table): when using multidimensional lists, is there a way to simply iterate over all elements regardless of depth? Maybe this is a little too matlab-y, but in m code you can do code:
edit: put spaces around the ":" index to remove emoticons
|
# ? Jul 1, 2008 22:39 |
|
I don't think there's a really idiomatic way of doing that. But you can do it recursively in this manner. - For every element in the list, check if it's a list. --- If it is a list, call the function recursively on it. --- If it's not a list, apply whatever transformations you want to it.
|
# ? Jul 1, 2008 23:13 |
|
HatfulOfHollow posted:Recommending Django for writing simple cgi scripts is a bit overkill don't you think? I've used a variety of methods for writing web applications and I like mod_python the most. web.py and other cgi modules feel clunky to me. I guess it's personal preference, but mod_python makes writing cgi in python feel more like python. I would personally never ever ever use mod_python if I could avoid it. Its poorly documented code caching (which can be inconsistent across the processes it loads) is probably the most frustrating thing I've ever had to deal with when testing code. And to make matters worse you can't really test applications developed for mod_python outside of mod_python. It's a loving framework too. I absolutely love mod_wsgi, and for quick one-off development I just write WSGI apps directly and use wsgiref.simple_server.
|
# ? Jul 2, 2008 01:03 |
|
Milde posted:I would personally never ever ever use mod_python if I could avoid it. Its poorly documented code caching (which can be inconsistent across the processes it loads) is probably the most frustrating thing I've ever had to deal with when testing code. And to make matters worse you can't really test applications developed for mod_python outside of mod_python. It's a loving framework too. I've run into the caching problem a few times, but it's really only been an issue if I'm developing on a live environment and constantly changing the code. Usually I develop on another box, deploy the app and hup apache. As for not being able to test applications outside of mod_python, you can't really test PHP apps outside of mod_php either or JSP's outside of a java application server so I don't really understand where you're going with that one.
|
# ? Jul 2, 2008 11:22 |
|
That doesn't say anything positive about mod_python, only something negative about JSP and PHP. If you develop for WSGI, you can deploy your application on any WSGI-compilant server. There's also a WSGI wrapper for mod_python that makes it possible to run WSGI apps on mod_python.
|
# ? Jul 2, 2008 11:49 |
|
I have a really retarded question for you guys... What advantage does coding a web application in Python offer over using a more widely used language such as PHP? I mean aside from the obvious shared hosting issues with Python, does it offer a faster page load times or is literally just the nicer syntax?
|
# ? Jul 2, 2008 12:18 |
|
Hanpan posted:I have a really retarded question for you guys... In terms of development time, I couldnt really say. I do know that php is notoriously hard to make secure, and python tends to be more maintainable in the long term because the language promotes legibility and easy to understand/debug/refactor code, whereas in php you have to kind of fight against the language if you want to accomplish these things. In terms of speed, python isn't particularly fast in comparison to most other compiled languages. I think in comparison to php you will likely see slightly less throughput for many common web applications, but then again maybe python will be faster on other tasks as well. Python also has the ability to include C code through the swig, psyco, or pyrex modules, which can yield significant boosts in speed if that is a big issue for you. tripwire fucked around with this message at 12:29 on Jul 2, 2008 |
# ? Jul 2, 2008 12:24 |
|
tripwire posted:In terms of development time, I couldnt really say. I do know that php is notoriously hard to make secure, and python tends to be more maintainable in the long term because the language promotes legibility and easy to understand/debug/refactor code, whereas in php you have to kind of fight against the language if you want to accomplish these things. Ahh, thanks. I was worried because I thought there was some kind of huge performance issues between the two.
|
# ? Jul 2, 2008 12:27 |
|
I'm looking for a nice imaging/drawing api/toolkit for python. The problem is that I need to produce real time (well, frequently updates) visualisations (mostly charts) on linux/python (although preferably cross platform) I've been looking at using a number of chart APIs but have avoided them as they produce ugly as sin. NodeBox is Pretty but it's OSX only. Processing is also pretty but in Java. I could use. I could also use something like pyglet or even pyqt, and I'm looking into the possibility of pycairo. Any suggestions ?
|
# ? Jul 3, 2008 14:07 |
|
Cairo is often a good choice there. I haven't used a lot of such tools but it was not that difficult to use when I tried and it's cross platform and flexible in terms of what you can draw with it.
|
# ? Jul 3, 2008 18:23 |
|
Another one for the OP: Painless Python for Competent developers 1 of 2 and 2 of 2
|
# ? Jul 3, 2008 21:12 |
|
What the hell is going on herecode:
3 6 9 12 So why is this happening? I thought every time I call m=MenuContainer() it creates a new MenuContainer object and assigns it to m? Why is it the same object over and over again?
|
# ? Jul 4, 2008 04:16 |
|
ATLbeer posted:What the hell is going on here def __init__(self, Enabled=False, Items=[]): That line, change it to Items=None then code:
edit: wow I totally overlooked the actual problem, I should stop posting here when sleepy. deimos fucked around with this message at 05:16 on Jul 4, 2008 |
# ? Jul 4, 2008 04:55 |
|
tef posted:Processing is also pretty but in Java. I could use. You could try using this with Jython.
|
# ? Jul 4, 2008 05:05 |
|
deimos posted:def __init__(self, Enabled=False, Items=[]): Didn't make a difference
|
# ? Jul 4, 2008 05:07 |
|
ATLbeer posted:Didn't make a difference You're using the class level variable midmenu. When you access an attribute on an instance and Python doesn't find the attribute on the instance, it looks for it in its class too before it raises AttributeError. What you need to do is instantiate all of those variables inside __init__ - they don't belong inside the class.
|
# ? Jul 4, 2008 05:13 |
|
ATLbeer posted:What the hell is going on here This creates sidemenu, midmenu, topmenu and footer as class variables. All instances of MenuContainer will share them. You want code:
|
# ? Jul 4, 2008 05:14 |
|
JoeNotCharles posted:This creates sidemenu, midmenu, topmenu and footer as class variables. All instances of MenuContainer will share them. You want That was it... Working 12 hours today and my brain is fried... I need to blow something up tomorrow.
|
# ? Jul 4, 2008 05:17 |
|
ATLbeer posted:
You're throwing all your parameters away and even with the scope issuse resolved it's not going to do anything. It should be self.Text = Text, etc. Also, the official and idiomatic style is to only start names with capitals iff they are the names of classes.
|
# ? Jul 4, 2008 06:41 |
|
Has anyone gotten PayPal IPN working with Python? For the life of me I can't figure out what I am doing wrong when I have simply taken the idea from another (working) script and used it in my own. I am not getting errors, it just seems PayPal cannot verify my payment and therefore nothing is happening unless the user explicitly clicks on "Return to Merchant" which then everything is grand, but not every user clicks that link This is a mix of django/python but the relevant stuff is in Python PP_URL = https://www.paypal.com/cgi-bin/webscr code:
|
# ? Jul 4, 2008 18:01 |
|
|
# ? May 10, 2024 14:01 |
|
What are people using for general HTML form generation? A lot of the more popular modules out there are tied to particular frameworks (e.g. zope.form, newforms). I really like formencode's validation and conversion but it doesn't generate forms (for good reasons) and has some oddities when handling arrays of fields. formbuild is supposed is okay, but again has a couple of odd design decisions.
|
# ? Jul 4, 2008 18:38 |