Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
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.

Adbot
ADBOT LOVES YOU

Titan Coeus
Jul 30, 2007

check out my horn

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.

Instead of doing
Python code:
dict_keys = [] #it's a lot of work to iteratively create a tuple so I just make a list and turn it into a tuple with tuple()
Put the comment on it's own line and break after 80 characters
Python code:
# Create a list and convert it to a tuple with tuple() to avoid constantly
# making new tuples for each change
dict_keys = []
As far as Python specific stuff, the biggest thing I am noticing is your use of for loops:

Python code:
for x in range(len(data1_dict["key"])): dict_keys.append(data1_dict["key"][x])
In Python, think of a for loop as something that iterates over a collection, not something that increments a loop variable (like you would be used to in C).

Python code:
for value in data1_dict["key"]:
    dict_keys.append(value)

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
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:
for value in data1_dict["key"]:
    dict_keys.append(value)

Python code:
dict_keys = tuple(data1_dict["key"])
http://divmod.readthedocs.org/en/latest/philosophy/potato.html

Pangolin Poetry
Jan 1, 2008

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:
dict["Great Dane"]
>>> [ ["John", 6, "Great Dane", "Brown"], 
>>> ["Alfie", 2, "Great Dane", "White"],
>>> ["Alonzo", 3, "Great Dane", "Black"] ]
So that way if I want to know about the Great Danes (or whatever) in my data I can see all of them, and if I want to know about a specific Great Dane I can do dict["Great Dane"][1] and get Alfie, for example. Creating the first two dictionaries is easy: just add a column of data to its appropriate key and viola. Creating the new dictionary is "easy", too, but like you guys have noted it's super ugly. I don't want to be a potato programmer :( but thinking "Pythonically" is not something I'm very good at yet! Thanks for helping me improve.

Pangolin Poetry fucked around with this message at 09:38 on Sep 18, 2012

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
Do you want the breed's info accessible in the dict as well? IE

Python code:
dict = {
  "Great Dane": {
     "dogs" : [
       ["John", 6, "Great Dane", "Brown"], 
       ["Alfie", 2, "Great Dane", "White"],
       ["Alonzo", 3, "Great Dane", "Black"]
     ],
     "avg_lifespan": 10,
     "common_health_problems": "Bad Gas",  # Heh I dunno :)
  },
  # and so on
} 

Civil Twilight
Apr 2, 2011

edit: never mind, didn't see new page

Comrade Gritty
Sep 19, 2011

This Machine Kills Fascists

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:

Python code:
my_sh = sh.DoYourMagicPlease(path=["/opt/apache/bin", "/opt/apache/sbin"])
my_sh.apachectl("restart")

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.

Nippashish
Nov 2, 2005

Let me see you dance!

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.

Pangolin Poetry
Jan 1, 2008

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

Winkle-Daddy
Mar 10, 2007
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!

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
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.

FoiledAgain
May 6, 2007

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:
KL = sum(
[(probserve(c,C,seg1_context,seg1.freq)*log(probserve(c,C,seg1_context,seg1.freq)/
probserve(c,C,seg2_context,seg2.freq)))
+(probserve(c,C,seg2_context,seg2.freq)*log(probserve(c,C,seg2_context,seg2.freq)/
probserve(c,C,seg1_context,seg1.freq)))
for c in C])
It doesn't fit across, so I cut it at those places because it helps me make the most sense out of what that says.


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:
var = really + long + complicated.formula - that.just \
+wont - fit.across.one + line

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
I tend to use it for chained querysets in Django my self, ie

Python code:
logs = Log.objects.filter(value__in=filter_vals).filter(value2__gt=next_val).distinct()

logs = Log.objects.filter(value__in=filter_vals)\
                  .filter(value2__gt=next_val)\
                  .distinct()
Contrived statement there obviously, I know others do that with brackets round the whole statement, but this tends to be the style I've settled on. I like being explicit about the line break with single statements rather than using brackets, personally anyhow.

Pangolin Poetry
Jan 1, 2008
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

Emacs Headroom
Aug 2, 2003

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:
In [1]: s = "foo foo foo\
   ...: foo foo foo"

In [2]: s
Out[2]: 'foo foo foofoo foo foo'

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
You can write long string literals in sequence and they are automatically concatenated together.

Python code:
mylongstring = (
    'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod '
    'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim '
    'veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea '
    'commodo consequat. Duis aute irure dolor in reprehenderit in voluptate '
    'velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint '
    'occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit '
    'anim id est laborum.'
)

ufarn
May 30, 2009

Hammerite posted:

You can write long string literals in sequence and they are automatically concatenated together.

Python code:
mylongstring = (
    'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod '
    'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim '
    'veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea '
    'commodo consequat. Duis aute irure dolor in reprehenderit in voluptate '
    'velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint '
    'occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit '
    'anim id est laborum.'
)
Long time since I did this, but something like this would probably work:

Python code:
mylongstring = """
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
    veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
    commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
    velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
    occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
    anim id est laborum.
    """
Same thing you do when you write docstrings for your scripts and functions - which I'm sure you do already. :colbert:

Emacs Headroom
Aug 2, 2003

ufarn posted:

Same thing you do when you write docstrings for your scripts and functions - which I'm sure you do already. :colbert:

The triple quote will preserve the line breaks though, which is exactly what he didn't want.

Jewel
May 2, 2009

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:
mylongstring = """
lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.
"""
	
mylongstring = "".join(mylongstring.split("\n"))
Or if you want to keep in the tabbed text for some reason:

code:
mylongstring = "".join(mylongstring.split("\n")).replace("\t", "").replace("    ", "")

Emacs Headroom
Aug 2, 2003
Sure why not

Python code:
strings = ["let's", "all", 'stop', """bikeshedding""",
    'over simple', "built-in string functionality"]
print ' '.join(strings)

The Gripper
Sep 14, 2004
i am winner
If you're really aching for horizontal space you could try:
Python code:
x = ('h'
     'e'
     'l'
     'l'
     'o'
     ' '
     'w'
     'o'
     'r'
     'l'
     'd'
     '!')

Winkle-Daddy
Mar 10, 2007

Suspicious Dish posted:

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.

Holy poo poo, this is great! Thanks!

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
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

BigRedDot
Mar 6, 2008

GrumpyDoctor posted:

I have a question that might be more appropriate for the scientific computing megathread, but I think I should start here.

I'm pretty new to Python. I whipped a short little thing up for a project I'm working on. It's pretty simple - it just calculates some simple normal distributions, samples from them, fiddles around with the samples a little, and plots the result.

I didn't think this would be that complicated, but the performance is much worse than I'd expected. It takes over a minute to run. Is there some glaring thing I'm doing wrong? Like I said, I'm pretty new to Python in general and this is my first time doing anything with NumPy or SciPy. (Also, if there are Python conventions I'm not following it'd be great to hear about those too.)

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

raminasi
Jan 25, 2005

a last drink with no ice

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.

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)

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).

BigRedDot
Mar 6, 2008

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.

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).

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:
hourly_availability_samples = map(ds*fs for ds, fs in zip(demand_samples, flex_samples))
As for the other question, in:
code:
flex_samples = np.vectorize(partial(calculate_flex, shed_length=args.shed_length))(dsf_samples)
If dsf_smaples is a list of arrays, then I think we may have found the problem. I would expect that the vectorized ufunc would first convert the list of arrays into larger numpy array, with lots of expensive copying. There are a few things to try: combine the arrays explicitly with hstack or vstack. Then you might even be able to do away with your vectorized function in favor or masks and built in ufuncs. But even just stacking should avoid the extra copying. Other possibilities are to look into other tools like cython and Numba for speeding things up.

raminasi
Jan 25, 2005

a last drink with no ice

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?

Nippashish
Nov 2, 2005

Let me see you dance!

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:
def calculate_flex(X, shed_length):
    Y = (24 / shed_length) * ((X-1)/X)
    Y[X > 24 / (24 - shed_length)] = 1
    Y[X < 1] = 0
    return Y
assuming I actually understand what np.vectorize does.

BigRedDot
Mar 6, 2008

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).

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

I hope NumPyPy exists soon because people have apparently donated $45,000 to fund its development

Titan Coeus
Jul 30, 2007

check out my horn

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

BigRedDot
Mar 6, 2008

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

A Fistful of Dicks
Jan 8, 2011
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?

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

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.

raminasi
Jan 25, 2005

a last drink with no ice

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.

For example calculate_flex might look something like this:
code:
def calculate_flex(X, shed_length):
    Y = (24 / shed_length) * ((X-1)/X)
    Y[X > 24 / (24 - shed_length)] = 1
    Y[X < 1] = 0
    return Y
assuming I actually understand what np.vectorize does.

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!

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

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.

huhwhat
Apr 22, 2010

by sebmojo
Python code:

======================================================================

FAIL: test_asum (test_blas.TestFBLAS1Simple)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/lib/blas/tests/test_blas.py", line 58, in test_asum

    assert_almost_equal(f([3,-4,5]),12)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 468, in assert_almost_equal

    raise AssertionError(msg)

AssertionError:

Arrays are not almost equal to 7 decimals

 ACTUAL: 0.0

 DESIRED: 12



======================================================================

FAIL: test_dot (test_blas.TestFBLAS1Simple)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/lib/blas/tests/test_blas.py", line 67, in test_dot

    assert_almost_equal(f([3,-4,5],[2,5,1]),-9)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 468, in assert_almost_equal

    raise AssertionError(msg)

AssertionError:

Arrays are not almost equal to 7 decimals

 ACTUAL: 0.0

 DESIRED: -9



======================================================================

FAIL: test_nrm2 (test_blas.TestFBLAS1Simple)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/lib/blas/tests/test_blas.py", line 78, in test_nrm2

    assert_almost_equal(f([3,-4,5]),math.sqrt(50))

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 468, in assert_almost_equal

    raise AssertionError(msg)

AssertionError:

Arrays are not almost equal to 7 decimals

 ACTUAL: 0.0

 DESIRED: 7.0710678118654755



======================================================================

FAIL: test_basic.TestNorm.test_overflow

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest

    self.test(*self.arg)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/tests/test_basic.py", line 581, in test_overflow

    assert_almost_equal(norm(a), a)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 452, in assert_almost_equal

    return assert_array_almost_equal(actual, desired, decimal, err_msg)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 800, in assert_array_almost_equal

    header=('Arrays are not almost equal to %d decimals' % decimal))

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 636, in assert_array_compare

    raise AssertionError(msg)

AssertionError:

Arrays are not almost equal to 7 decimals



(mismatch 100.0%)

 x: array(-0.0)

 y: array([  1.00000002e+20], dtype=float32)



======================================================================

FAIL: test_basic.TestNorm.test_stable

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest

    self.test(*self.arg)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/tests/test_basic.py", line 586, in test_stable

    assert_almost_equal(norm(a) - 1e4, 0.5)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 468, in assert_almost_equal

    raise AssertionError(msg)

AssertionError:

Arrays are not almost equal to 7 decimals

 ACTUAL: -10000.0

 DESIRED: 0.5



======================================================================

FAIL: test_basic.TestNorm.test_types

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest

    self.test(*self.arg)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/tests/test_basic.py", line 568, in test_types

    assert_allclose(norm(x), np.sqrt(14), rtol=tol)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 1168, in assert_allclose

    verbose=verbose, header=header)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 636, in assert_array_compare

    raise AssertionError(msg)

AssertionError:

Not equal to tolerance rtol=2.38419e-06, atol=0



(mismatch 100.0%)

 x: array(1.0842021724855044e-19)

 y: array(3.7416573867739413)



======================================================================

FAIL: test_asum (test_blas.TestFBLAS1Simple)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/tests/test_blas.py", line 99, in test_asum

    assert_almost_equal(f([3,-4,5]),12)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 468, in assert_almost_equal

    raise AssertionError(msg)

AssertionError:

Arrays are not almost equal to 7 decimals

 ACTUAL: 0.0

 DESIRED: 12



======================================================================

FAIL: test_dot (test_blas.TestFBLAS1Simple)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/tests/test_blas.py", line 109, in test_dot

    assert_almost_equal(f([3,-4,5],[2,5,1]),-9)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 468, in assert_almost_equal

    raise AssertionError(msg)

AssertionError:

Arrays are not almost equal to 7 decimals

 ACTUAL: 0.0

 DESIRED: -9



======================================================================

FAIL: test_nrm2 (test_blas.TestFBLAS1Simple)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/tests/test_blas.py", line 127, in test_nrm2

    assert_almost_equal(f([3,-4,5]),math.sqrt(50))

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 468, in assert_almost_equal

    raise AssertionError(msg)

AssertionError:

Arrays are not almost equal to 7 decimals

 ACTUAL: 0.0

 DESIRED: 7.0710678118654755



----------------------------------------------------------------------

Ran 5101 tests in 62.751s



FAILED (KNOWNFAIL=12, SKIP=31, failures=9)

<nose.result.TextTestResult run=5101 errors=0 failures=9>

Scipy was behaving weirdly. Running some code I found in StackOverflow that lets me filter out duplicates changed my array (e.g. [1, 2.9, 0, 0] turns into [1, 2.89999999, 0, 0]).
Python code:
array_with_dupes = np.array([np.array(x) for x in set(tuple(x) for x in array_with_dupes)]
So I ran scipy.test() and found that my scipy build is buggy.

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.

Masa
Jun 20, 2003
Generic Newbie
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:
class MyGui(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent=None)
        self.ui = Ui_TemperatureConverter()
        self.ui.setupUi(self)  
        self.temp = 0.0
        self.input_type = "Fahrenheit"
        self.output_type = "Celsius"
        self.ui.input_scale.setCurrentIndex(2)
        self.ui.output_scale.setCurrentIndex(1)
        self.ui.input_temp.textChanged.connect(self.temp_changed)               
        self.ui.input_scale.activated[str].connect(self.input_activated)        
        self.ui.output_scale.activated[str].connect(self.output_activated)
        QtCore.QObject.connect(self.ui.convert_button, 
                               QtCore.SIGNAL("clicked()"),
                               functools.partial(self.convert_temperatures,
                                                 self.temp, self.input_type,
                                                    self.output_type))   
        
    def input_activated(self, text):
        self.input_type = text  
        
    def output_activated(self, text):
        self.output_type = text
        
    def temp_changed(self, num):
        self.temp = float(num)

    .......
    and some other stuff that works properly
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?
It always uses the values I give them in __init__, and if I don't define them there, I get an Attribute Error.

Cat Plus Plus
Apr 8, 2011

:frogc00l:

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?
It always uses the values I give them in __init__, and if I don't define them there, I get an Attribute Error.

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:
class MyGui(QtGui.QMainWindow):
    def __init__(self, parent = None):
        # ... the stuff up to QObject.connect ...
        # I haven't used PyQt in who knows how long, so I'm following your syntax
        self.ui.convert_button.clicked.connect(self.convert_temperatures)

    def convert_temperatures(self):
        # do whatever you want with self.input_type, self.output_type and self.temp

    # ...

Adbot
ADBOT LOVES YOU

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



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:
#!/usr/bin/env python

def compare(a, b):
	"""
	order should be:
	
	closed-before:	(0,3)
	open-before:	(None,3)
	same-number		(3,3)
	open-after		(3,None)
	closed-after	(3,5)
	"""
	res = 0
	# do magic
	print 'compare %s with %s: %d' % (str(a), str(b), res)
	return res

values = [ (1,1), (2,None), (None,1), (2,2), (3,3), (0,0), (None,0), (2,3), (0,2) ]

sortedvals = sorted(values, cmp=compare)

expected = [
	(None,0),
	(0,0),
	(0,2),
	(None,1),
	(1,1),
	(2,2),
	(2,None),
	(2,3),
	(3,3),
]

print sortedvals
print sortedvals == expected
I guess I'm growing old, I can't wrap my head around it.

  • Locked thread