|
Suspicious Dish posted:Do not read Dive into Python. It was built for a version of Python that's over 10 years old, and the creator has not updated it. Thanks, that potentially saved me a lot of time.
|
# ? Sep 5, 2012 01:21 |
|
|
# ? May 9, 2024 19:00 |
|
Lysidas posted:There's no special syntax. I did not know that d[2, 1] would be treated as a dictionary lookup on a tuple. I assumed you'd have to do d[(2, 1)].
|
# ? Sep 5, 2012 20:12 |
|
Lysidas posted:There's no special syntax. Numpy can't just be doing something with tuple keys, because you can do slicing right?
|
# ? Sep 5, 2012 20:45 |
|
Dren posted:I did not know that d[2, 1] would be treated as a dictionary lookup on a tuple. I assumed you'd have to do d[(2, 1)]. It's the same reason why a, b = b, a works.
|
# ? Sep 5, 2012 21:05 |
|
Emacs Headroom posted:Numpy can't just be doing something with tuple keys, because you can do slicing right? You can't use slices as dictionary keys because they're unhashable Python code:
Python code:
|
# ? Sep 5, 2012 22:54 |
|
KARMA! posted:It's the same reason why a, b = b, a works. To explain in little more detail, comma is what actually creates a tuple, not parentheses. That's also why you need a comma at the end of singleton tuple. Only exception is the empty tuple, which is literally (). code:
|
# ? Sep 5, 2012 23:10 |
|
PiotrLegnica posted:Only exception is the empty tuple, which is literally () This has always made me inexplicably angry. I know its unnecessarily complicated and doesn't make sense to have the empty tuple be (,) but damnit, the empty tuple should be a butt.
|
# ? Sep 6, 2012 00:48 |
|
Is there a way to ignore (not update) a file with pysvn on a svn update? I've gotten it to checkout/update to a temp directory then using rsync to copy with ignore/exclude X files. Would like to get rid of that rsync and just do it on svn update via pysvn.
|
# ? Sep 6, 2012 05:23 |
|
If I'm working on a module that is python2 and python3 compatible, I'd like to force the user to specify string values as unicode. This is easy enough in python3 since unicode strings are very commonplace but for people using python2, they haven't necessarily thought about unicode vs. bytestrings in their programs. Here is my thought on how to handle them: Python code:
Is it acceptable to recommend that python2.x users add from __future__ import unicode_literals to their scripts to improve compatibility?
|
# ? Sep 6, 2012 16:02 |
|
TIL about the specifics of tuple syntax. It is a shame that (,) is invalid so you can't have butts as empty tuples if you want. If you're going to try to write a compatible library put the compatibility in a separate layer. Make the library the user imports a bunch of stub functions that do nothing but convert string arguments if necessary then call the real functions. Other solutions include:
|
# ? Sep 6, 2012 16:29 |
|
This thread should be renamed "Python: empty tuples aren't butts "
|
# ? Sep 6, 2012 20:29 |
|
Hi there, python newbie coming in again. I've been trying to practice coding by making a simple test-based game (I'm really new at this programming thing.) But, my game has a problem. The code is pretty long, but here's the gist: I define the introduction to the game, then define what happens when the player inputs a direction they want to go. The directions are: north, south, east and west. Then I define a menu that should should ask the player for a direction, and based on that input execute the code for that particular direction. There is also some randomization within the directions (by that I mean I use randint and randomchoice). However, when I go to execute the game, no matter what I enter at the menu, the game executes the north direction, and and always in the same way. Is there some obvious thing I'm doing wrong? Should I just post the code? I'm using Python 3 if that makes any difference.
|
# ? Sep 7, 2012 01:35 |
|
Post the code. At least post the function or the class that contains the parts with the movement commands. Gothmog1065 fucked around with this message at 01:46 on Sep 7, 2012 |
# ? Sep 7, 2012 01:43 |
|
All right, I guess the description was too vague.code:
|
# ? Sep 7, 2012 02:17 |
|
When you write if selection == 'n' or 'north': because the "==" operator has higher precedence than the "or" operator, it is equivalent to this: if (selection == 'n') or 'north': So you are asking: is it the case that either selection == 'n' is true, or 'north' is true? The string 'north' always counts as True, so the value returned by "or" will always be True here. You could change the line to if selection in ['n', 'north']:
|
# ? Sep 7, 2012 02:35 |
|
Thank you! That'll teach me to not skip the basic boring stuff. Of course, now new problems are popping up, but that's just the joy of programming~!
|
# ? Sep 7, 2012 02:44 |
|
Dren posted:
Emphasis mine. This is inaccurate, Writing code that is 2 and 3 compatible is the easier and more widespread method of handling the 2 -> 3 shift. It's really not very difficult, especially with something like six and supporting only python 2.6+. It also avoids a lot of the issue of differing code bases (namely that either you have 2 seperate projects with vastly similar code that you need to mirror the changes in, or you use 2to3 and deal with slow tests on 3.x, tracebacks that don't point to the right lines of code, etc. Using six and Python 2.6+ you can write mostly idiomatic Python 3 code, and include support for Python 2 still.
|
# ? Sep 7, 2012 03:34 |
|
Coming from a c++ background, serialization is so much easier using the pickle module.
|
# ? Sep 7, 2012 04:23 |
|
I'm making a basic scrabble solver and want it to use the letter positions to see if a word fits from a word list. I have a long way of doing this and its not that processor intensive but I was wondering if there is a better more pythonic way I'm missing. First we have the 7 letters someone has in their rack lets say these: code:
Lets say the middle squares of a row look like this: code:
But it would be nice if there was some kind of pattern recognition way of doing this but I can't think of anything beyond a series of for loops and other checks. Anyone have a clever idea?
|
# ? Sep 7, 2012 13:38 |
|
Steampunk Hitler posted:Emphasis mine. This is inaccurate, Writing code that is 2 and 3 compatible is the easier and more widespread method of handling the 2 -> 3 shift. It's really not very difficult, especially with something like six and supporting only python 2.6+. It also avoids a lot of the issue of differing code bases (namely that either you have 2 seperate projects with vastly similar code that you need to mirror the changes in, or you use 2to3 and deal with slow tests on 3.x, tracebacks that don't point to the right lines of code, etc. Using six and Python 2.6+ you can write mostly idiomatic Python 3 code, and include support for Python 2 still. The 2to3 approach was the one that we were going for. I hadn't really run into any issues with tracebacks being messed up yet. I think for better backwards compatibility we're going to stick to native strings (str) for both Python 3 and 2. The module only supports 2.6+ so I may look into using six.
|
# ? Sep 7, 2012 14:52 |
|
bc87 posted:Coming from a c++ background, serialization is so much easier using the pickle module. Don't use it for data a malicious user could access, though http://nadiana.com/python-pickle-insecure
|
# ? Sep 7, 2012 14:53 |
|
Sylink posted:I'm making a basic scrabble solver and want it to use the letter positions to see if a word fits from a word list. You should attempt to create a better data structure for your dictionary. The dictionary has the following characteristics: 1. It doesn't particularly matter how much space you devote to it unless it gets so big you start having to restructure your code so that the whole thing isn't in memory at once. Even then it's not that bad of a problem. 2. It never changes so you can do as much analysis/collection of additional information/organization of it as you want. This is all frontend work that doesn't happen at runtime so you can access the data faster at runtime. You should create a data structure where looking up all of the words with a letter at position x is really fast. You could have something like this: code:
Once you have your list of words that can fit with certain letters, such as 'g' and 't' with two spaces in between, perhaps you could re-run the algorithm on that word list to get something that's faster to use for fitting the pieces the player has. There's probably a more elegant/faster way to solve the problem but this seems like a pretty easy and straightforward optimization that would get you down to an acceptable runtime. I'd also figure out a data structure for storing the available locations to play along with their constraints and bonuses. When a word is played it would only need to update the parts that change. The point of such a data structure would not be just to avoid reanalyzing parts of the board that were already looked at. It would also be to have a nice, concise list of possible positions to go through while solving the board given the player's current tileset.
|
# ? Sep 7, 2012 16:45 |
|
Never mind.
ufarn fucked around with this message at 21:29 on Sep 8, 2012 |
# ? Sep 7, 2012 16:58 |
|
Modern Pragmatist posted:The 2to3 approach was the one that we were going for. I hadn't really run into any issues with tracebacks being messed up yet. I think for better backwards compatibility we're going to stick to native strings (str) for both Python 3 and 2. The module only supports 2.6+ so I may look into using six. Tracebacks being messed up tends to be line numbers changing or module imports changing so you'll get a traceback that says line XXX and you can't just jump to that line, you have to run 2to3, find the problem, reverse that to find where the problem exists in your existing codebase fix it, run 2to3, make sure it's fixed now, etc.
|
# ? Sep 8, 2012 05:22 |
|
If I assign values to each of the letters such that a=1, b=2, c=3, etc, etc, how could I go about converting a string to a numerical value such as cat = 3 + 1 + 20? I would prefer a push in the right direction instead of a solution if possible. My thoughts so far is to set a=1 b=2 ... z=26 Then take a name such as "cat" and break it up and treat c a t as variables and sum these values. I'm just not sure how to identify each of the strings as variables. huhu fucked around with this message at 18:03 on Sep 8, 2012 |
# ? Sep 8, 2012 17:56 |
|
huhu posted:Then take a name such as "cat" and break it up and treat c a t as variables and sum these values. I'm just not sure how to identify each of the strings as variables. Use a dictionary that maps letters to their values. Iterating over string will yield individual letters.
|
# ? Sep 8, 2012 18:23 |
|
Try the ord function.
|
# ? Sep 8, 2012 18:23 |
|
As a general rule, you almost never want to go from the name of a variable to a variable itself; it's a sign that you should be using something like a dict.
|
# ? Sep 8, 2012 18:25 |
|
Ended up with a surprisingly fast code which went through about 5000 names in less than a second. I will check out dictionaries and ord now though thanks.code:
huhu fucked around with this message at 18:47 on Sep 8, 2012 |
# ? Sep 8, 2012 18:33 |
|
huhu posted:Ended up with a surprisingly fast code which went through about 5000 names in less than a second. I will check out dictionaries and ord now though thanks. I guess that works, but you're not really using the strengths of the language to cut down on the amount of code you have to write. See how it is possible to do the same thing in four lines of Python code, with less repetition: Python code:
|
# ? Sep 8, 2012 20:41 |
|
Just to add to that, there's a module with everything you need in it. This is the magic that makes Python useful.Python code:
|
# ? Sep 8, 2012 21:03 |
|
huhu posted:Ended up with a surprisingly fast code which went through about 5000 names in less than a second. I will check out dictionaries and ord now though thanks. If you're gonna do it this way, you might as well at least put elifs instead of ifs
|
# ? Sep 9, 2012 01:24 |
|
I've got a strange Python/matplotlib issue going on. I've been using matplotlib in the past few days to re-do all the graphs for my job market paper since I love how nice they look relative to MATLAB/R/etc. However, I'm trying to make some simple scatter plots with least squares lines and something strange is happening -- matplotlib only draws the lines partially, if it draws them at all. Here's some code:code:
Note how the regression lines for the first figure (which is also the first one in the multi-figure plot) and the third subplot in the second figure get truncated. And the line for the second subplot in the second figure doesn't show up at all. Anybody have any idea how to fix this? Also, you can probably figure out who I am based on these figures, so I'd appreciate it if you would refrain from doing so (or at least posting my contact info). Edit: And these pictures do a very poor job of capturing how good python figures look.
|
# ? Sep 9, 2012 03:46 |
|
huhu posted:If I assign values to each of the letters such that a=1, b=2, c=3, etc, etc, how could I go about converting a string to a numerical value such as cat = 3 + 1 + 20? I would prefer a push in the right direction instead of a solution if possible. Treating the letters of the alphabet as sequential numbers is a special case of the generic problem of mapping a key to a value since the ord() function easily supplies the mapping for you. The generic tool to use for solving this problem is the dictionary. code:
You can then use functional operators to translate your strings into lists of integers that can be summed. code:
For starters, look at map(). http://docs.python.org/library/functions.html#map Note the functional style of Hammerite's solution. He has a function to do the mapping/translation, score_character() (analogous to the dictionary I created). Then, in score(), he translates the original values to the desired values using his mapping function and sums the resulting list. Functional style in Python can be given to some inefficiency. Sometimes computations that could be combined into one loop are instead spread over two loops. However, what it sometimes lacks in efficiency it makes up for in readability.
|
# ? Sep 10, 2012 16:10 |
|
Howdy! Just found this subforum and have been working my way through Udacity classes hard and fast. I'm currently looking at making a quick program that produces a ding whenever a web page updates, and I for the life of me can't find a concise way to produce sound in Python on a contemporary installation of Ubuntu. The closest answer I've found is old as dirt, making calls to the depreciated /dev/dsp to initiate sound through an oss module. The other options I can follow are installing pretty sizable modules like pygame, but I really just want a 1 second sound clip to play every thirty minutes or so, no UI necessary. Should I bite the bullet and install Snack, or is there some os-specific import that I'm missing to make simple .wav playback easy?
|
# ? Sep 10, 2012 17:23 |
|
Cheekio posted:Howdy! Just found this subforum and have been working my way through Udacity classes hard and fast. I'm currently looking at making a quick program that produces a ding whenever a web page updates, and I for the life of me can't find a concise way to produce sound in Python on a contemporary installation of Ubuntu. Pick your favorite command-line audio player (VLC using cvlc, mpg123, sox with play) and use the subprocess module. Python code:
OnceIWasAnOstrich fucked around with this message at 18:14 on Sep 10, 2012 |
# ? Sep 10, 2012 18:06 |
|
OnceIWasAnOstrich posted:Pick your favorite command-line audio player (VLC using cvlc, mpg123, sox with play) and use the subprocess module. "subprocess + aplay" works great, exactly what I was hoping for! Finally I can (sort of) put my attention back to constructive uses. Pythagoras a trois fucked around with this message at 19:10 on Sep 10, 2012 |
# ? Sep 10, 2012 19:03 |
|
I just found out that Python chokes if you do this:code:
Why does it do this? I thought trailing commas in argument lists, etc. were fine.
|
# ? Sep 11, 2012 04:59 |
|
Python code:
|
# ? Sep 11, 2012 07:28 |
|
|
# ? May 9, 2024 19:00 |
|
Alright, so I kept working on my scrabble program and ran into a problem. Consider that a row in scrabble contains 15 tiles which could have any reasonable combination of letters on them based on how the game is going. I represent a row like this, roughly: code:
For example, at the 'den' section a simple search would turn up denied as an option, but in this case doesn't fit because you would have a 'c' in the way. Can anyone think of a straightforward way to check? right now I have a complicated way that still doesn't come up with every possibility (though it does come up with MOST of them). here is the full code example with sample output: code:
code:
EDIT: oh and I have a class based version of this to do other things, but I separated this out to test it.
|
# ? Sep 11, 2012 21:30 |