|
zarg posted:Awesome. Thanks for your help on the stuff above this! I was already reading the python docs stuff Tons of nice concise info there; it's great. There's a few things going on here; in general, the documentation is the best place to get answers like this. dict.items() is going to give you a shallow copy of the dict's list of key-value pairs. Since it's a copy, modifying the list returned by items() (say, by appending a new (key,value) pair) will not affect the dict in any way. But since it's a shallow copy, modifying one of the values of the dict (which are also lists) through the returned value of items() (say, by appending something to the list at my_dict_items[0][1]) will affect the contents of the dict. The sorted() call has the same effect - it generates a new shallow copy of the original list and sorts that, it doesn't sort the original list in place. If you want to sort in-place, you would want to use my_dict_items.sort() instead of sorted(my_dict_items).
|
# ? Mar 17, 2011 12:26 |
|
|
# ? Jun 11, 2024 08:54 |
|
Sailor_Spoon posted:
code:
|
# ? Mar 17, 2011 15:24 |
|
Dren posted:I believe this code does not explicitly close the file handle. I remember seeing somewhere that closing file handles by letting them go out of scope is not good practice because that behavior is a CPython implementation detail. Given that, I believe the best practice would be to do: I think I was actually the one that brought this up last time. In cpython, file handles are implicitly closed when they go out of scope. Apparently, this is not necessarily true in other implementations. So if you expect your code to ever be run on anything other than cpython, then yes, using `with` is the smart thing to do.
|
# ? Mar 17, 2011 19:29 |
|
Dren posted:I googled a bit about it but couldn't find a reference. Anyone have a link? I think I read it when I was looking at pypi.
|
# ? Mar 17, 2011 19:31 |
|
Gothmog1065 posted:Are there any "Programming Python for dumbshits" that is written for 3.2? That's the book I've been using to teach myself and it's fantastic
|
# ? Mar 17, 2011 20:02 |
|
Tufty posted:That's the book I've been using to teach myself and it's fantastic
|
# ? Mar 17, 2011 20:21 |
|
Gothmog1065 posted:Fantastic. I ordered it today and it should be in in a few days. I think I'll have an easier time using this than I have been with other ways. Yeah, I tried a few other ways first but the content of the book is just sticking with me. He treats it like he's teaching you babby's first programming language but manages to not make it patronising, and the way new concepts are introduced is nice. Chapter structure is basically this: here's what we're gonna learn this chapter; here's the programs we're gonna make to introduce the new stuff; program 1 - go through it, explaining new stuff and refreshing old stuff as appropriate; program 2 etc etc; large final chapter program that combines all the new concepts, explanation of it, refreshing all the new stuff in your mind; here are some 'challenge' programs you can try to make using the concepts you've learned this chapter. It's really fun
|
# ? Mar 17, 2011 20:44 |
|
Tuple question: I am trying to add an integer to a tuple that, to begin with, contains only one integer, but will have more, as they are added later on. code:
|
# ? Mar 20, 2011 22:12 |
|
ufarn posted:Tuple question: Short answer: because (1) isn't a tuple. The error message is telling you exactly what's happening. code:
|
# ? Mar 20, 2011 22:19 |
|
Lysidas posted:Short answer: because (1) isn't a tuple. The error message is telling you exactly what's happening. I am doing the MIT OCW, and they haven't made it to lists yet, so I wanted to do it without any future keywords and data structures.
|
# ? Mar 20, 2011 22:36 |
|
If you're curious what's up with that: tuples aren't defined by parentheses, but by the comma. A one-tuple has to have a trailing comma to be a tuple; otherwise, it's a string, integer or whatever. You probably already know this, considering the additions do have the trailing comma, but you missed it when you assigned first `primelist` in the latter example, and it was therefore made to be an integer. xtal fucked around with this message at 22:56 on Mar 20, 2011 |
# ? Mar 20, 2011 22:41 |
|
NOISIA posted:If you're curious what's up with that: tuples aren't defined by parentheses, but by the comma. A one-tuple has to have a trailing comma to be a tuple; otherwise, it's a string, integer or whatever. I went ahead and solved it using a list (), but I just replaced them with your tuple. Bonus question! I am trying to render an integer as a decimal, but failing utterly: code:
ufarn fucked around with this message at 23:36 on Mar 20, 2011 |
# ? Mar 20, 2011 23:07 |
|
NOISIA posted:tuples aren't defined by parentheses, but by the comma. Except for the empty tuple, of course: code:
|
# ? Mar 20, 2011 23:36 |
|
ufarn posted:Bonus question! I am trying to render an integer as a decimal, but failing utterly: You want %f or %g
|
# ? Mar 21, 2011 00:02 |
|
YardKGnome posted:You want %f or %g
|
# ? Mar 21, 2011 00:14 |
|
Let's say I want to create an app that keeps track of third-party game content, downloads it from the internet, installs it and handles dependencies. Basically, an apt-get for a specific antique game's mod community. They are in a poorly documented zip file hell at the moment, with nearly 20,000 pieces of legacy content. What Python libraries, or feature-complete projects, should I be looking at using? I know about WUU, and am considering pruning it down for my needs. I did search the hell out of PyPI but couldn't find anything like a generic package manager. Thanks in advance.
|
# ? Mar 21, 2011 11:09 |
|
Might be some code worth saving in yum; http://en.wikipedia.org/wiki/Yellowdog_Updater,_Modified or you could just use mercurial tef fucked around with this message at 12:32 on Mar 21, 2011 |
# ? Mar 21, 2011 12:30 |
|
SpaceReflex posted:Let's say I want to create an app that keeps track of third-party game content, downloads it from the internet, installs it and handles dependencies. Basically, an apt-get for a specific antique game's mod community. They are in a poorly documented zip file hell at the moment, with nearly 20,000 pieces of legacy content. I think you could make Ivy work for this. http://ant.apache.org/ivy/ I have written a custom dependency manager before. I strongly recommend you find a dependency tool and make it work for you instead of rolling your own. Using an existing tool will cut your work down from creating and parsing your own xml format, coding up a dependency tree parser, and organizing a dependency repository to just organizing the dependency repository. You could probably even build a yum or apt-get compatible repository.
|
# ? Mar 21, 2011 14:26 |
|
I have a list whose length is divisible by 5, and I want to apply a function to each sublist of 5 elements. Here's an example to make it clearer:code:
|
# ? Mar 21, 2011 19:47 |
|
ultrafilter posted:I have a list whose length is divisible by 5, and I want to apply a function to each sublist of 5 elements. Here's an example to make it clearer: code:
|
# ? Mar 21, 2011 20:35 |
|
ultrafilter posted:I have a list whose length is divisible by 5, and I want to apply a function to each sublist of 5 elements. Here's an example to make it clearer: code:
code:
good jovi fucked around with this message at 20:51 on Mar 21, 2011 |
# ? Mar 21, 2011 20:47 |
|
Thank you.
|
# ? Mar 21, 2011 21:30 |
|
Hmm, with two mentions, plus existing familiarity with rpm packaging, I might just try to yum it up. Yum has a nice front end too which is something else I'd like to avoid reinventing. Trying to find evidence of win32 yum standalone builds at the moment - it's gotta be a py2exe situation with no extra deps. Since yum talks to rpm libs that might be extra fun. I looked at Ivy and I like the graphical dependency chart, but it's all Java and I dun know Java outside of "ant start" "ant stop". Thanks and keep the suggestions coming guys, I will have all the content mirrored in a couple of days and be ready for repo building.
|
# ? Mar 21, 2011 22:02 |
|
PIL Question. Everybody knows PIL's support for multilayer images kind of sucks, so it may be that what I'm asking for is hopeless. But has anyone ever found a fix/workaround for the way PIL doesn't respect heterogeneous layer sizes? Typically, animated gifs are optimized by taking blocks of color which don't change between frames and turning them transparent, and resizing the layers to be as small as possible. That is to say, if a new layer only adds a change to a small region of the image, that new layer should only be as big as the changed region. When rendering the GIF, that layer will be painted at an offset to the original image (some programs like irfanview gently caress up and you see gifs rendered with regions popping around the image). PIL lets you access all of the frames in a GIF image (although you have to grab the palette from the first frame and chuck it back into the image on later frames for some reason). But PIL refuses to play nice when the layers in your gif are different sizes: if you seek to a new frame which is smaller than the image size, and save that frame, youll get a bizarre effect: the transparent regions within the boundaries of that frame are correctly saved, but rest of the image bounding it gets set to a random other color in the palette, leaving a gross border around each new frame. Heres an animated GIF I'm working with: Heres my script for dumping a series of gifs into one big image: code:
http://i.imgur.com/tObJJ.png (it may help to save it and open it up in some program where the transparency is more apparent) So what the gently caress!
|
# ? Mar 21, 2011 23:37 |
|
SpaceReflex posted:Hmm, with two mentions, plus existing familiarity with rpm packaging, I might just try to yum it up. Yum has a nice front end too which is something else I'd like to avoid reinventing. Trying to find evidence of win32 yum standalone builds at the moment - it's gotta be a py2exe situation with no extra deps. Since yum talks to rpm libs that might be extra fun. http://www.nabber.org/projects/appupdater/
|
# ? Mar 22, 2011 13:27 |
|
Dren posted:I got this when I searched for yum on windows. good suggestion! appupdater is on my short list already due to its cross platform capability and flexibility.
|
# ? Mar 22, 2011 13:54 |
Since bool('') == False, how come (False or '') == ''? Shouldn't it be equal to False?
|
|
# ? Mar 22, 2011 18:52 |
|
bool('') needs to evaluate to something, and that something has to be a bool, so it evaluates to False (because empty containers evaluate to false, to make certain idioms work). But it is not, itself, False. "(a or b)" evaluates the first argument, and then sees what that argument looks like as a bool; it returns a (NOT cast to bool) if it evaluates to True, and returns b (NOT cast to bool) otherwise. That enables other idioms. For instance: code:
Sneftel fucked around with this message at 19:07 on Mar 22, 2011 |
# ? Mar 22, 2011 19:03 |
|
I've been running a python script with no problem for weeks now, and all of a sudden it has started giving me error messages: no module named blah. For every single module it used to import with no problem. Why would this happen and how can I fix it? The only difference I can think of is that when it broke I ran it by double clicking from Explorer, rather than within IDLE.
|
# ? Mar 23, 2011 09:42 |
|
Okay, just got my "You don't know poo poo about programming" book. Been doing good so far. I'll read through the chapter, and when he comes to the game at the beginning, I try to program it before he shows how he does it. It's worked good so far, except the program I'm doing now is closing as soon as I open it. I can't figure it out. Here is the code:code:
e: I found it. I forgot the colons after "else". I find it odd that it will just not work right even before you should be getting to those areas. Gothmog1065 fucked around with this message at 19:23 on Mar 23, 2011 |
# ? Mar 23, 2011 19:19 |
|
FoiledAgain posted:I've been running a python script with no problem for weeks now, and all of a sudden it has started giving me error messages: no module named blah. For every single module it used to import with no problem. Why would this happen and how can I fix it? Something is wrong with your path. You can see your path by doing: code:
If you have more than one python interpreter or version installed on your system you may want to investigate which one is called when you run a script through explorer in order to be sure it is the one you expect.
|
# ? Mar 23, 2011 19:23 |
|
Gothmog1065 posted:Okay, just got my "You don't know poo poo about programming" book. Been doing good so far. I'll read through the chapter, and when he comes to the game at the beginning, I try to program it before he shows how he does it. It's worked good so far, except the program I'm doing now is closing as soon as I open it. Open a command prompt (Start -> Run... -> cmd) and run your program from there. The problem you had generated an error message that you never saw because windows closed your terminal as soon as the program exited.
|
# ? Mar 23, 2011 19:25 |
|
Gothmog1065 posted:
Won't this generate a number between 2 and 101?
|
# ? Mar 23, 2011 20:10 |
|
DeciusMagnus posted:Won't this generate a number between 2 and 101? No, I just double checked this (Added a print("Random number:", number) line at the bottom). The randint(1,100) creates numbers 0 - 99, not 1 - 100.
|
# ? Mar 23, 2011 20:23 |
|
quote:If you have more than one python interpreter or version installed on your system you may want to investigate which one is called when you run a script through explorer in order to be sure it is the one you expect. Multiple versions might be the problem. I have 2.6 and 2.7 installed, although I almost never use 2.6 anymore, so I wonder if that's what got called when I ran it from explorer. I'll go dig. Thanks!
|
# ? Mar 23, 2011 20:25 |
|
DeciusMagnus posted:Won't this generate a number between 2 and 101? Unless this was changed in 3.0, yes. code:
|
# ? Mar 23, 2011 21:02 |
|
Gothmog1065 posted:No, I just double checked this (Added a print("Random number:", number) line at the bottom). The randint(1,100) creates numbers 0 - 99, not 1 - 100. The documentation says otherwise. Also this was run in python 2.6 and 3.1: pre:Python 3.1.3 (r313:86834, Dec 15 2010, 17:14:42) [GCC 4.2.1 20070719 [FreeBSD]] on freebsd8 Type "help", "copyright", "credits" or "license" for more information. >>> import random >>> [random.randint(1,5) for i in range(20)] [5, 1, 2, 5, 2, 3, 1, 4, 3, 3, 5, 3, 1, 2, 3, 1, 2, 4, 2, 2] Python 2.6.6 (r266:84292, Mar 7 2011, 15:21:33) [GCC 4.2.1 20070719 [FreeBSD]] on freebsd8 Type "help", "copyright", "credits" or "license" for more information. >>> import random >>> [random.randint(1,5) for i in range(20)] [1, 5, 3, 2, 3, 1, 1, 3, 5, 4, 1, 4, 4, 1, 5, 5, 3, 2, 1, 2]
|
# ? Mar 23, 2011 21:09 |
|
DeciusMagnus posted:The documentation says otherwise. Also this was run in python 2.6 and 3.1:
|
# ? Mar 23, 2011 21:17 |
|
Okay, another day and another bit of code that's loving me up:pre:#Word Jumble game import random WORDLIST = ("jumble", "python", "word", "programming", "stephen", "computer", "plus", "game", "book", "chapter", "inventory", "programmer") print(""" Word Jumble game Try and guess the jumbled word! """) true = 1 #Set loop to infinite while the user wants to play while true: jumble = "" word = random.choice(WORDLIST) correct = word while word: position = random.randrange(len(word)) jumble += word[position] word = word[:position] + word[(position + 1):] print("\n\nThe jumble you want to guess is", jumble, ".") guess = input("Your guess: ") while guess.lower() != correct and guess.lower != "": print("\n\nSorry, that isn't right!") guess = input("\nGuess again: ") if guess == correct: end = input("You got it! Do you want to play again? (Y/N): ") if end.lower() == "n" or end.lower() == "no" or end == "": break When I run this in IDLE, it gives the error: pre:Traceback (most recent call last): File "C:/Users/Jason/Desktop/Python/word jumble", line 26, in <module> print("\n\nThe jumble you want to guess is", jumble) TypeError: 'str' object is not callable Gothmog1065 fucked around with this message at 14:10 on Mar 25, 2011 |
# ? Mar 24, 2011 17:28 |
|
|
# ? Jun 11, 2024 08:54 |
|
You've got a wayward = which is redefining "print" up near the top.
|
# ? Mar 24, 2011 17:30 |