|
Hammerite posted:Thanks for this pointer. This looks like it might at least have most of what I need, I'll have to decide whether to use this or go on with my own thing. Taking a longer (but still brief) look at this, unless I'm missing something, it still uses floating point numbers internally to speed things up or whatever. I need something that will do things symbolically (even if that implies that things will be slower) and that will cooperate if I decide to use my own subclass of numbers.Real. I'll probably carry on with my own class. I'm having fun with it, anyway. I never learnt basic linear algebra as well as I should have.
|
# ? Aug 12, 2013 15:50 |
|
|
# ? Jun 13, 2024 05:47 |
If you install something like uWSGI in a virtualenv, should any configuration files also live inside the virtualenv?
|
|
# ? Aug 13, 2013 07:40 |
|
Hammerite posted:Taking a longer (but still brief) look at this, unless I'm missing something, it still uses floating point numbers internally to speed things up or whatever. I need something that will do things symbolically (even if that implies that things will be slower) and that will cooperate if I decide to use my own subclass of numbers.Real. I'll probably carry on with my own class. I'm having fun with it, anyway. I never learnt basic linear algebra as well as I should have. Years ago as an exercise I implemented gaussian elimination over fields like that using the fractions library coupled with customized classes. For example in the Q[sqrt(2)] case the objects would be just pairs of fractions (a, b) where the inverse would be (a/(a^2 + 2b^2), -b/(a^2 + 2b^2)).
|
# ? Aug 13, 2013 08:34 |
|
Anyone have a one-liner for the for loop here so that the function is not needed?Python code:
|
# ? Aug 13, 2013 15:42 |
|
fletcher posted:If you install something like uWSGI in a virtualenv, should any configuration files also live inside the virtualenv? Config files should live with the project. Virtualenvs do have a directory, but that's just an implementation detail, and that directory could be anywhere (for instance, I use virtualenvwrapper, so all my env directories are in ~/.virtualenvs).
|
# ? Aug 13, 2013 15:48 |
|
Symbolic Butt posted:Years ago as an exercise I implemented gaussian elimination over fields like that using the fractions library coupled with customized classes. For example in the Q[sqrt(2)] case the objects would be just pairs of fractions (a, b) where the inverse would be (a/(a^2 + 2b^2), -b/(a^2 + 2b^2)). Yeah, this is exactly the sort of thing I was thinking of doing (not very far into it right now). I had been thinking of making the matrix objects mutable in a limited way - it would be possible to edit entries in a matrix, but not to change the dimensions of the matrix. Now, though, I'm thinking of making them immutable. There are some things I'm not sure about there, though.
|
# ? Aug 13, 2013 16:00 |
|
Dren posted:Anyone have a one-liner for the for loop here so that the function is not needed? There's the standard zip/iter trick, but that seems readable enough to me. Only thing I'd do is use next(i) instead of i.next().
|
# ? Aug 13, 2013 16:17 |
|
Hammerite posted:Yeah, this is exactly the sort of thing I was thinking of doing (not very far into it right now). Make them Immutable, or better yet, use an existing Matrix Library quote:How exactly do I create a sensible __hash__() for my class? If your __eq__ method checks .bar and .foo, use them in the __hash__ method. code:
quote:Am I allowed to have mutable internal state if the object is immutable? It isn't really mutable if it can't change. You can have lazy properties that are calculated and then fixed without being made fun of by python celebrities. quote:If I do an operation that potentially involves lots of intermediate matrices, like reducing a big matrix to echelon form, then it might be wasteful to generate new objects for each step, so maybe I need a mutable companion class (MutableMatrix or BaseMatrix, say) to reduce that expense by doing things in-place. Is this a thing that people do? Write the slow version first, then replace it with an immutable version if it isn't fast enough. If you're worried about speed, using a library with native code may be the best way to handle things, rather than fiddling around in python. http://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html
|
# ? Aug 13, 2013 16:25 |
|
Suspicious Dish posted:There's the standard zip/iter trick, but that seems readable enough to me. Only thing I'd do is use next(i) instead of i.next(). Yeah, the helper function seems the cleanest way. If you're wanting to do it without one, you can use this horror code:
|
# ? Aug 13, 2013 16:37 |
|
Suspicious Dish posted:There's the standard zip/iter trick. I assume you mean this ? code:
|
# ? Aug 13, 2013 16:40 |
|
Hammerite posted:Yeah, this is exactly the sort of thing I was thinking of doing (not very far into it right now). If you implement __hash__ you don't need to implement __eq__ do you? In any case, equality (and hash equality) are supposed to be equal-by-value, so yes, usually you'd implement __hash__ as hash((self.internal_a,self.internal_b,....)) or somesuch. If you want identity the user would use the "is" operator. There are times when you only want equality by identity (usually when the objects represent some external state, like a row or a file or something) in which case just don't implement __hash__ (or __eq__ etc) as that is how objects work by default. As for mutability, there are no hard and fast semantics (that I know of) in python regarding mutability for your own classes. If you call your object immutable I'm going to assume it's immutable, and what you do internally is none of my business. If the abstraction leaks and an object I assume is immutable starts mutating from under me though, I will try to hate you to death over the internet. But yes, you can definitely do it, just make sure the public presentation of your class is consistent. Having said all that, tef is quite correct in saying it is best not to sweat performance at first. It's not actually that obvious trying to predict where mutability vs immutability will perform better.
|
# ? Aug 13, 2013 16:45 |
|
tef posted:I assume you mean this ? I got... Python code:
NtotheTC fucked around with this message at 16:48 on Aug 13, 2013 |
# ? Aug 13, 2013 16:46 |
|
NtotheTC posted:I got... Yep, that's the standard one. It relies on the fact that list multiplication returns the same iter object.
|
# ? Aug 13, 2013 16:51 |
|
Mr. Wynand posted:If you implement __hash__ you don't need to implement __eq__ do you? The way I think I understand it, hashable objects' hashes have to be the same if the objects are the same. But two different objects could happen to have the same hash. It's really really unlikely because hashes are integers with lots of digits, like 3713084879518070856 (that was the hash of the tuple (4, 5) when I fired up the Python interpreter just now). But it could happen, and it would be bad if two different matrices could, entirely by chance, test as being the same on a certain occasion because they chance to have the same hash. Thanks for advice and thanks also to tef. I know there are existing matrix libraries but I am preoccupied with having one that prefers using Fractions over floats*. I'm not sure what search terms I'd use to find something like that. Finding matrix library implementations on Google is easy, finding ones that satisfy idiosyncratic requirements is less easy. * For example, the following snippets should be equivalent: code:
code:
|
# ? Aug 13, 2013 17:14 |
|
Hammerite posted:The way I think I understand it, hashable objects' hashes have to be the same if the objects are the same. But two different objects could happen to have the same hash. It's really really unlikely because hashes are integers with lots of digits, like 3713084879518070856 (that was the hash of the tuple (4, 5) when I fired up the Python interpreter just now). But it could happen, and it would be bad if two different matrices could, entirely by chance, test as being the same on a certain occasion because they chance to have the same hash. Objects don't test as the same if their hashes are equal (unless for some reason you define __eq__ in that way). Hash values are mostly used to select hash table buckets when objects are used as dict keys or set items, and a good __hash__ function will try to minimize collisions so that items are distributed more-or-less uniformly in these data structures. Everything still "works" correctly even if you use a degenerate hash function: Python code:
|
# ? Aug 13, 2013 17:47 |
|
Lysidas posted:Objects don't test as the same if their hashes are equal (unless for some reason you define __eq__ in that way). Hash values are mostly used to select hash table buckets when objects are used as dict keys or set items, and a good __hash__ function will try to minimize collisions so that items are distributed more-or-less uniformly in these data structures. Oh, I see. My understanding was wrong. It is still appropriate to define __eq__() for a matrix class, though. The condition for two matrices to be equal is simple to state and to code. They are equal if they have equal dimensions and elements are pairwise equal. Object identity would not be appropriate, because if you construct two matrices in two different ways or at two different times but they are the same matrix, they should still compare equal.
|
# ? Aug 13, 2013 20:00 |
|
Doh, yes, you need __eq__ separately as you need to test type (or interface, if applicable) as well. I think I was getting confused with ruby... What I said about equality vs identity still stands though - you (usually) want your objects to be equal by value, and if anyone needs an identity comparison they'll just use "is" explicitly.
|
# ? Aug 13, 2013 20:12 |
|
tef posted:I assume you mean this ? This is really nice. I hadn't thought to use two references to the same iterator but I figured there had to be some trick like this. I think I'll use this one. Suspicious Dish, why do you suggest next(i) instead of i.next()?
|
# ? Aug 13, 2013 21:30 |
|
Dren posted:This is really nice. I hadn't thought to use two references to the same iterator but I figured there had to be some trick like this. I think I'll use this one. .next() becomes .__next__() in Python 3. next() is a convenience function that calls whichever one is appropriate in the version you're using.
|
# ? Aug 13, 2013 21:46 |
|
Dren posted:Suspicious Dish, why do you suggest next(i) instead of i.next()? Same reason I'd suggest you use str(i) instead of i.__str__().
|
# ? Aug 13, 2013 22:13 |
|
Dominoes posted:PYQT signals/slots question: Python code:
Dominoes fucked around with this message at 03:30 on Aug 14, 2013 |
# ? Aug 14, 2013 03:22 |
|
I just want to see if I'm understand this code correctly. I'm a little confused by it's mechanics I'd guess you'd call it. I get bottle_cokes is not defined in this section of code.code:
code:
|
# ? Aug 14, 2013 03:51 |
|
digitalcamo posted:I just want to see if I'm understand this code correctly. I'm a little confused by it's mechanics I'd guess you'd call it. I get bottle_cokes is not defined in this section of code. In your first example, 'bottles_coke', 'crates_of_coke' etc are local variables, accessible only by the box_of_coke() function. In your second example, you assigned them to global variables, which can be accessed outside box_of_coke(). quote:I also do not understand how the variable coke gets assigned to bottle_cokes. Does it just go through coke, crates, trucks and assigns the first variable coke to the first variable of box_of_coke? quote:And I'm assuming since I have three variables in box_of_coke I must declare three variables to box_of_coke? Technique only: print "There is {0} bottles of coke, {1} crates, and {2} trucks.".format(bottle_cokes, crates_of_cokes, trucks_of_coke) Dominoes fucked around with this message at 04:07 on Aug 14, 2013 |
# ? Aug 14, 2013 04:03 |
|
The answer is scope. http://en.m.wikipedia.org/wiki/Scope_(computer_science)
|
# ? Aug 14, 2013 04:05 |
|
Another way to think about it: You're expecting the functions to dump its names into the outer scope. What actually happens is a tuple value is created and dumped into the outer scope; since in the first code sample it's not being assigned to a name, it gets silently garbage-collected, after which bottle_cokes throws a NameError because it's not defined in the outer scope. In the second code sample, the tuple is created and returned from the function, and then each element of the tuple is assigned to one of the three names you've just declared in the outer scope. Dominoes posted:It depends. If you tried to assign it to one variable, you'd get a tuple, with size three. If you tried two, you'd raise this exception: 'ValueError: too many values to unpack (expected 2)' Dominoes posted:Technique only: print "There is {0} bottles of coke, {2} crates, and {3} trucks.".format(bottle_cokes, crates_of_cokes, trucks_of_coke)
|
# ? Aug 14, 2013 04:08 |
|
You need to read up on scope. The simplest explanation is that the variables within a certain scope (in this case the function) only exist within that scope. However, the function has access to outside variables in the greater scope. So for example: code:
however code:
There is a lot more to it, but hopefully that makes sense.
|
# ? Aug 14, 2013 04:18 |
|
When you write something like "return a, b, c" you are really doing "return (a, b, c)". That is, you are returning a tuple with three elements. The comma-separated sequence of values is implicitly a tuple. You might find it helpful to write it with the brackets until you are comfortable with this. When you return just one value (really you always return just one object, it's just that it's sometimes a tuple, but I mean when you don't use two or more values separated by commas) there's no comma and it's not made into a tuple. (But if for some reason you want to return a tuple with one element x, it is possible to do that by writing "return (x,)") When you assign to more than one thing, as in "a, b, c = func()" it means you are expecting func() to return a sequence with a length of 3. The names in the "sequence of names" on the left are assigned the respective values from the sequence returned by func().
|
# ? Aug 14, 2013 13:57 |
|
Hammerite posted:When you assign to more than one thing, as in "a, b, c = func()" it means you are expecting func() to return a sequence with a length of 3. The names in the "sequence of names" on the left are assigned the respective values from the sequence returned by func(). For future Googlin' this is called "unpacking".
|
# ? Aug 14, 2013 17:20 |
|
Anyone know of a good sound recognition library for python or C? I want a library that can compare two audio samples and determine how close of a match they are. I don't remember enough math to write my own (lol laplace and fourier transforms) and I can't find anything. I am currently messing around with echoprint's codegen library to shorten the sample length from 20 seconds down to ~3-5 seconds but I have no idea how well fingerprinting designed for music is going to work for generic sound recognition, especially if there is any background noise involved.
|
# ? Aug 14, 2013 17:32 |
|
Maybe LibXtract?
|
# ? Aug 14, 2013 18:16 |
|
Unit testing question. I decided to write an IRC bot using this lightweight bot framework as a starting point. I wanted to write some unit tests for it before I started messing around with it, but I'm having trouble wrapping my head around the idea of mocking. For example, I'd like to make a test to check whether it can connect to an IRC server, so I obviously need to mock an irc server, or at least the part of it that would respond when the bot tries to connect. I'm not quite getting how you can mock something like that to behave correctly. Does anyone have any pointers?
|
# ? Aug 14, 2013 20:04 |
|
I'm racking my brain here trying to figure out how to quickly load data from into numpy arrays. I have a single .gz file with 86,000 individual files in it, each one comprised of daily weather data for a single station through its existence. The data in a file looks like this:code:
code:
code:
|
# ? Aug 14, 2013 20:05 |
|
SirPablo posted:I'm racking my brain here trying to figure out how to quickly load data from into numpy arrays. I have a single .gz file with 86,000 individual files in it, each one comprised of daily weather data for a single station through its existence. The data in a file looks like this: I would probably skip numpy.genfromtxt unless profiling the code showed that it was required. Python code:
|
# ? Aug 14, 2013 20:50 |
|
Yea I'm hating to go line-by-line but I think I have to. I also think I'll need the flexibility of genfromtxt since there are no defined character delimiters (it is very likely numbers will run into each other, thus the need for defined columns). My brain finally farted out this option which seems to work.code:
|
# ? Aug 14, 2013 20:56 |
|
SirPablo posted:Yea I'm hating to go line-by-line but I think I have to. I also think I'll need the flexibility of genfromtxt since there are no defined character delimiters (it is very likely numbers will run into each other, thus the need for defined columns). My brain finally farted out this option which seems to work. There is this simplification you could make. I guess I don't understand the need to process lines without 'TMAX' in them. Also, is '6' a separator? Python code:
|
# ? Aug 14, 2013 21:21 |
|
NtotheTC posted:Unit testing question. I decided to write an IRC bot using this lightweight bot framework as a starting point. Basically, you want any place in your code where the test needs to interact with the irc server to instead interact with your own "mock" server. This can be easy or hard, depending on how you go about it. It took me a few tries to end up with something decent on my own project. Here's what I came up with for my own lightweight bot framework. Seeing as how you're testing writing a bot with the framework, I don't think mocking the IRC server will actually be necessary though. From what I skimmed through in your choice of framework, you'll have to do two things: For any lines of data you want sent towards the IRC server, they should pass through a mock say() (or a raw_send if there is one such function, that sends raw data, instead of saying to a channel). This say() should log, per-test-case, if the information that was sent to the IRC server is the correct one, in the correct order, etc. For any lines of data you want sent from the IRC server, you should call the methods on the bot directly, by which I mean the callbacks you add. You can also somehow call __processLine with the raw data, but that means testing the framework instead of the bot, which you shouldn't really be doing.
|
# ? Aug 14, 2013 21:37 |
|
My analysis is only looking at maximum temperatures (TMAX) so anything else is data I don't need in the analysis. The number 6 is not a delimiter, it is a data quality flag value (just happens to be a lot of 6s in that example). I'll give the enumerate a spin, thanks!
|
# ? Aug 14, 2013 21:44 |
|
Two servers, remote from each other. ServerA wants ServerB to run some fairly simple code (less than 30 lines) that ServerB doesn't have. How do I get the code from ServerA to ServerB? I already have a REST API. I can't guarantee that they will be running the exact same version of Python. In fact, right now I know one is on 2.7.x and the other is on 2.6.x, so I'm not sure about binary formats being compatible between the two, but we can assume the code itself is compatible and that both have the same 3rd-party libraries available.
|
# ? Aug 14, 2013 22:08 |
|
Unless you have a very, very good reason, you should never execute code from anybody as a service.
|
# ? Aug 14, 2013 22:14 |
|
|
# ? Jun 13, 2024 05:47 |
|
Thermopyle posted:Two servers, remote from each other. ServerA wants ServerB to run some fairly simple code (less than 30 lines) that ServerB doesn't have. How do I get the code from ServerA to ServerB? Sounds like a job for ZeroRPC Suspicious Dish posted:Unless you have a very, very good reason, you should never execute code from anybody as a service. I assumed he wanted something like RPC, but I might be wrong. Depending on the context Thermopyle, you might want to look into something like salt stack for this, otherwise you're looking at something far more complex like Apache Mesos. deimos fucked around with this message at 22:19 on Aug 14, 2013 |
# ? Aug 14, 2013 22:15 |