|
Any chance of a sample of the output and use case, maybe a little more detail on what kind of information it's iterating over. Be a bit easier to follow and offer suggestions with a better idea of what's being passed around, for me anyway.
|
# ? Sep 18, 2012 07:02 |
|
|
# ? May 8, 2024 04:41 |
|
Pangolin Poetry posted:Python code A couple things make this code tricky to read. In particular, the long length of lines caused by comments. This is true for any language, but Python programs in particular tend to keep their line length to 80 characters or less, but no one Instead of doing Python code:
Python code:
Python code:
Python code:
|
# ? Sep 18, 2012 07:50 |
|
Pangolin, your code won't work. You reference temp_promotion_list, even though you never set it anywhere. You have improper indentation. Also, it's also super ugly. I would give you something a bit more Pythonic, but the thing I would say is that your data structures are hard to work with. Where do data1_dict, data2_dict and data2_columns come from? Give me the code that creates those, and I'll show you a better way to do things. Titan Coeus posted:
Python code:
|
# ? Sep 18, 2012 08:15 |
|
Suspicious Dish posted:Pangolin, your code won't work. You reference temp_promotion_list, even though you never set it anywhere. You have improper indentation. Oops. Gosh, this is hard - I have to transcribe my code onto my phone to post on SA and some times I miss things. The code does work, I just forgot to change that variable name and the indentation must have gotten lost in the shuffle. Anyway, I can't sleep so I figured I'd keep the ball rolling here. Thanks to everybody who's commented - now, then... Titan Coeus posted:A couple things make this code tricky to read. In particular, the long length of lines caused by comments. This is true for any language, but Python programs in particular tend to keep their line length to 80 characters or less, but no one will should be outraged if you go up to 100. I'll definitely do that from now on. A question, though: what do I do about an actual line of code that's longer than 80 chars? Or do line breaks not break the code integrity? Maluco Marinero posted:Any chance of a sample of the output and use case, maybe a little more detail on what kind of information it's iterating over. Be a bit easier to follow and offer suggestions with a better idea of what's being passed around, for me anyway. So here's what I've got. I have two sets of data in the form of tables in a database. One set contains information about dog breeds, and the other about individual dogs. I put each set in a dictionary keyed to the column names of the tables - so for example, the breed dictionary has keys like "breed name", "avg lifespan", "common health problems", etc. and the individual dog one has keys like "name of dog", "age", "breed name", "color" etc. The values attached to each key are lists containing all the values in that column. So if I call BreedDictionary["avg lifespan"] it gives me this: [2, 3, 5, 6, 7, 10, 11, 13, 13, 14, 9], or if I called DogDictionary["DogNames"] I'd get a list of strings, etc. which is a column of data; its place in the list corresponds to the row from the original database table. Obviously this organization isn't particularly useful and there's not much I can do about that since the databases aren't mine. The rows aren't related across the databases, and the dogs aren't in any particular order either. The name of the dog breed column is shared between the two dictionaries as a key. What I want to have at the end is this: in my new dictionary, I can do the following code:
Pangolin Poetry fucked around with this message at 09:38 on Sep 18, 2012 |
# ? Sep 18, 2012 09:35 |
|
Do you want the breed's info accessible in the dict as well? IEPython code:
|
# ? Sep 18, 2012 10:08 |
|
edit: never mind, didn't see new page
|
# ? Sep 18, 2012 13:09 |
|
Suspicious Dish posted:Why would the environment of the process that you're trying to exec affect path resolution in the current process? Are they double forking and doing path resolution in a subchild in that environment? That's the way the underlying Python implementation works with the os.exec*() functions, and because the subprocess module also uses os.exec*() functions, it also uses an env var to modify the PATH to determine the lookup. The only difference here between sh and subprocess is that subprocess calls the kwarg env and sh calls it _env. Suspicious Dish posted:And I think you read my API issue wrong. What I'd like is: Yea I did, having never actually used the module I don't see an immediate way of handling this, maybe baking but that appears to need to know the command ahead of time. Suspicious Dish posted:They could still be building a string for convenience, so sh.sudo("sync /my-directory/") will work along with sh.sudo("sync", "/my-directory/"). Yea they could be, but they arn't. They are just passing the values along to os.exec*() (which is what subprocess does with shell=False). Suspicious Dish posted:How do I read output from a "background process"? Send input? You can pass a callback via _out that will get called with the output from a backgrounded process. I don't see any sort of read() type of method but if there isn't one there should be imo. You can pass in input via the _in kwarg. It accepts any iterable including Queue.
|
# ? Sep 18, 2012 15:20 |
|
Pangolin Poetry posted:I have two sets of data in the form of tables in a database. If you're going to be wrangling around data like this you might want to look at pandas.
|
# ? Sep 18, 2012 17:25 |
|
Maluco Marinero posted:Do you want the breed's info accessible in the dict as well? IE Sure, that would be useful. Right now in order to get that data I'd have to pass the index value for "Great Dane" back to my DogDict and iterate through its keys, so having it all condensed into a single dictionary would be good stuff! If it's not super nasty to do, that is. Dictionaries within dictionaries isn't something that occurred to me! Edit: pandas eh... Time to read up
|
# ? Sep 18, 2012 17:41 |
|
Not sure if this is the right place for this question, but the python guys have steered me right before. I recently decided that Panda3D was too robust for some simple 2D roguelike stuff. I heard some good things about pyglet so I decided to get started with it. Working in 3D, I'm used to building my world, then placing the camera at certain locations. 2D is far less intuitive to me. Anyone have a link to a tutorial/methodology for how to only display part of a level on screen and make the camera follow around the player? It's a very basic concept but the last 2D stuff I did was in GameMaker and in that you just set up a view with the correct window size and make it follow the player. Any advice on setting up a similar system with pyglet? The documentation doesn't seem to cover this use case, which leads me to believe I need to write my own class to handle displaying the level. Some ideas on methodology there would be a huge help!
|
# ? Sep 18, 2012 18:44 |
|
Our very own Red Mike wrote an article on this. http://codingden.net/camera-tutorial-take-2/ EDIT: the images seem to be broken. Swap out new.codingden.net for codingden.net.
|
# ? Sep 18, 2012 22:31 |
|
Pangolin Poetry posted:I'll definitely do that from now on. A question, though: what do I do about an actual line of code that's longer than 80 chars? Or do line breaks not break the code integrity? Python doesn't care about line breaks inside of brackets. Here's an example from some code I have up right now: code:
You can also use "\" to signal a line continuation. I don't have any real examples of this on hand because I don't use it very much, so here's a fake one code:
|
# ? Sep 19, 2012 01:31 |
|
I tend to use it for chained querysets in Django my self, iePython code:
|
# ? Sep 19, 2012 01:57 |
|
Right on. What about very long strings? Is there a way to make them continuous, as if on one line, but in actuality split on multiple lines, without actually inserting line breaks? The style guide doesn't have an entry for that
|
# ? Sep 19, 2012 02:07 |
|
Pangolin Poetry posted:Right on. What about very long strings? Is there a way to make them continuous, as if on one line, but in actuality split on multiple lines, without actually inserting line breaks? The style guide doesn't have an entry for that Just one single or double quote dog. I'm pretty sure it is mentioned in the string section of the Python docs Python code:
|
# ? Sep 19, 2012 02:09 |
|
You can write long string literals in sequence and they are automatically concatenated together.Python code:
|
# ? Sep 19, 2012 10:37 |
|
Hammerite posted:You can write long string literals in sequence and they are automatically concatenated together. Python code:
|
# ? Sep 19, 2012 13:45 |
|
ufarn posted:Same thing you do when you write docstrings for your scripts and functions - which I'm sure you do already. The triple quote will preserve the line breaks though, which is exactly what he didn't want.
|
# ? Sep 19, 2012 13:51 |
|
Emacs Headroom posted:The triple quote will preserve the line breaks though, which is exactly what he didn't want. Why not this: Python code:
code:
|
# ? Sep 19, 2012 15:13 |
|
Sure why notPython code:
|
# ? Sep 19, 2012 16:46 |
|
If you're really aching for horizontal space you could try:Python code:
|
# ? Sep 19, 2012 17:06 |
|
Suspicious Dish posted:Our very own Red Mike wrote an article on this. Holy poo poo, this is great! Thanks!
|
# ? Sep 19, 2012 21:25 |
|
If you want to give your camera zooming or bank rotation, the easiest thing to do is to model your camera as a rectangle with a transformation matrix. Then, when you draw your objects, apply the inverse of the camera's transformation matrix to the object's transformation matrix. EDIT: simple example. If your game window is 640x480, you should probably model your rectangle to be 640x480 as well. So you have a rectangle at 640x480, with translation 0,0, rotation 0, and scale 1,1. When you want to zoom your camera in to 2x, that means reducing the size of the rectangle, so that you're really displaying a 320x240 view into the world. Keep the rectangle's size the same, but put your scale at 0.5, 0.5. Then, when you draw, all objects will become 2x bigger, because you applied by the inverse of the matrix. Suspicious Dish fucked around with this message at 21:45 on Sep 19, 2012 |
# ? Sep 19, 2012 21:43 |
|
GrumpyDoctor posted:I have a question that might be more appropriate for the scientific computing megathread, but I think I should start here. np.vectorize is usually not worth much. Numpy ufunc kernels are written in C as are the loop harnesses. So when you use a numpy ufunc all the iteration and comptuation over your data is all at the C level. np.vectorize might be convenient to write but it requires a call back into python space for every element of your data which kills performance. Same thing when you use map the way you did with np.multiply. In fact it's even worse than a map over a plain python list because numpy arrays do extra work to support slicing. Long story short: pretend you are in matlab, use numpy ufuncs everywhere, and avoid explicit loops at all cost. If there isn't a numpy ufunc that does what you want, there is a newish open source project Numba that generates machine code for inner loops directly from python using LLVM. So you can basically create new ufuncs that can be used effectively with np.vectorize. (Disclaimer: I work with Travis Oliphant at Continuum) BigRedDot fucked around with this message at 06:07 on Sep 20, 2012 |
# ? Sep 20, 2012 06:04 |
|
BigRedDot posted:np.vectorize is usually not worth much. Numpy ufunc kernels are written in C as are the loop harnesses. So when you use a numpy ufunc all the iteration and comptuation over your data is all at the C level. np.vectorize might be convenient to write but it requires a call back into python space for every element of your data which kills performance. Same thing when you use map the way you did with np.multiply. In fact it's even worse than a map over a plain python list because numpy arrays do extra work to support slicing. Long story short: pretend you are in matlab, use numpy ufuncs everywhere, and avoid explicit loops at all cost. Okay, you're the second person who seems to think I was doing something different than what I thought I was doing, so I guess I should ask this. The various x_sample variables are lists of numpy arrays. That's why I used map. Should I have shoved the whole thing into a 2d numpy array (are they still arrays in two dimensions?) and figured out how to slice it properly? And with respect to vectorize - how do I do what I want in this case if not with that? I understand that it's not for performance (it says so in the documentation) but I couldn't find any other way to map a numpy array (other than just map).
|
# ? Sep 20, 2012 22:50 |
|
GrumpyDoctor posted:Okay, you're the second person who seems to think I was doing something different than what I thought I was doing, so I guess I should ask this. You're right. I noticed what you describe in the mapping over mu's, and that is fine. I was thrown off by map(np.multiply,...) I think the lists of arrays are fine but you might consider using list comprehensions instead of map. Something like: code:
code:
|
# ? Sep 20, 2012 23:55 |
|
BigRedDot posted:As for the other question Let me ask a more generic question. If I have numpy array, and I need to apply some weird-rear end function (in this case it was calculate_flex) to each element of it to get a result array, how should I do it?
|
# ? Sep 21, 2012 00:03 |
|
GrumpyDoctor posted:Let me ask a more generic question. If I have numpy array, and I need to apply some weird-rear end function (in this case it was calculate_flex) to each element of it to get a result array, how should I do it? Instead of writing a scalar function to operate on each element of the array it's usually better to write a function that operates on the whole array elementwise with operations that are implemented in C. For example calculate_flex might look something like this: code:
|
# ? Sep 21, 2012 03:21 |
|
GrumpyDoctor posted:Let me ask a more generic question. If I have numpy array, and I need to apply some weird-rear end function (in this case it was calculate_flex) to each element of it to get a result array, how should I do it? A few options. If possible use masks and split the wierd-rear end function into regular numpy ufuncs (or even better, NumExpr expressions) on different views with those masks. Otherwise, look into Cython or Numba. Some day NumPyPy might exist but I am not holding my breath. That said, my guess is that your main problem was not np.vectorize, but that you used np.vectorize on a list of arrays (resulting in big copies).
|
# ? Sep 21, 2012 03:27 |
|
I hope NumPyPy exists soon because people have apparently donated $45,000 to fund its development
|
# ? Sep 21, 2012 04:57 |
|
MeramJert posted:I hope NumPyPy exists soon because people have apparently donated $45,000 to fund its development There was a commit 10 days ago that commented out some code... I'm not sure this was the best investment
|
# ? Sep 21, 2012 05:16 |
|
I can only offer my own opinion on the matter. The base of the scientific python stack is numpy, but the scientific python stack comprises much more than numpy alone. The stack is so valuable precisely because of CPython's ability to be a glue language, to be able to easily wrap lapack, linpack, MKL, ATLAS, blas, scalapack, whatever. PyPy as a tracing jit-compiler is incredibly interesting, and in many cases useful and worth investigating. But in regards to NumpPyPy and the rest of the scientific stack, I don't see how they are going to ignore five decades of tuned and optimized numerical libraries and succeed. (disclaimer again: I may be occupationally biased in my views!)
BigRedDot fucked around with this message at 05:27 on Sep 21, 2012 |
# ? Sep 21, 2012 05:17 |
|
I'm going through the reading list, and I'm checking out the built-in functions part of the Python Reference Library and idkwtf dude...it's incomprehensible for a nooblet like me. What do I need to read in order for this to make sense?
|
# ? Sep 21, 2012 15:28 |
|
A Fistful of Dicks posted:I'm going through the reading list, and I'm checking out the built-in functions part of the Python Reference Library and idkwtf dude...it's incomprehensible for a nooblet like me. What do I need to read in order for this to make sense? When I learn how to do something with a programming language, I generally like to do it by using the language in anger, i.e. I have something I want to achieve and I learn the tools I need to do it. Is there anything you are trying to work towards, or are you just trying to learn the language "in general"? Perhaps it would help to focus on a particular topic.
|
# ? Sep 21, 2012 15:57 |
|
Nippashish posted:Instead of writing a scalar function to operate on each element of the array it's usually better to write a function that operates on the whole array elementwise with operations that are implemented in C. This is the piece I was missing. I don't come from a Matlab background so doing things this way isn't immediately intuitive to me. Thanks a bunch!
|
# ? Sep 21, 2012 23:14 |
|
BigRedDot posted:I can only offer my own opinion on the matter. The base of the scientific python stack is numpy, but the scientific python stack comprises much more than numpy alone. The stack is so valuable precisely because of CPython's ability to be a glue language, to be able to easily wrap lapack, linpack, MKL, ATLAS, blas, scalapack, whatever. PyPy as a tracing jit-compiler is incredibly interesting, and in many cases useful and worth investigating. But in regards to NumpPyPy and the rest of the scientific stack, I don't see how they are going to ignore five decades of tuned and optimized numerical libraries and succeed. (disclaimer again: I may be occupationally biased in my views!) Those can all exist. The problem with NumPy is that it's not a Python module, it's a CPython module. PyPy is not CPython, and doesn't share any code. All of those other things, because they wrap C modules, can work with PyPy in a similar manner, especially if they use Cython or ctypes.
|
# ? Sep 22, 2012 04:20 |
|
Python code:
Python code:
I've tried cleaning out my macports folder and reinstalling everything. Testing again returned the same results. Google search suggested that it may be because different compilers were used to build my Python modules, and the solution is to build everything from source by hand. I'd rather not go that route and stick with package managers, especially after recently going through the nightmare of getting Python 2.7.3, numpy and scipy onto CentOS 5.4.
|
# ? Sep 22, 2012 04:28 |
|
I'm new to programming and Python and I'm trying to make a simple GUI with PyQT. I made a layout with QT Designer, now I'm trying to connect everything to make it functional. Right now I have this:code:
It always uses the values I give them in __init__, and if I don't define them there, I get an Attribute Error.
|
# ? Sep 25, 2012 19:52 |
|
Masa posted:How do I get it to pass the changed values for temp, input_type and output_type to the convert_temperatures method when I click on convert_button? Do you know what functools.partial does? Don't use it, just bind the signal to a no-arg method and use instance variables from there. Python code:
|
# ? Sep 25, 2012 19:58 |
|
|
# ? May 8, 2024 04:41 |
|
How would you guys go about sorting open-ended "ranges"? I have a bunch of numbers in tuples that represent ranges (start, end) and I want to sort these in a specific way.Python code:
|
# ? Sep 25, 2012 20:21 |