|
Better make sure it is callable, too:Python code:
|
# ? Aug 8, 2013 18:44 |
|
|
# ? May 9, 2024 15:58 |
|
NtotheTC posted:I'm not going to get into a community best practice slap fight. I just view this in a similar light to: As the last two posts point out, it's not that simple. Yes, it's more explicit to verbosely test every possible thing x might be, or you could just use Python's rules for boolean types and do if x:. It's not as simple as a black and white readability and explicitness versus how verbose you are. If you can cut the amount of verboseness by 50% while damaging explicitness by 5%, is it worth it? Most python programmers have some inate ratio in that question that they're willing to accept, and most python programmers have accepted that using if x: and the simple rules for boolean coercion are a fine tradeoff. If your sensitivities on that ratio are such that you don't want to do it, that's fine, but just be aware of the tradeoffs you're making when you make these kinds of decisions. (I have no idea if I'm right when I say "most", I can just go by the small percentage of existing python code that I have read and assume that my sample size is large and representative enough)
|
# ? Aug 8, 2013 18:56 |
|
NtotheTC posted:I'd avoid doing this. Using the plain if statement on variables that aren't a bool strikes me as a hideous pattern despite it being perfectly legal for strings/dicts/lists/etc. You'll occasionally get nasty unexpected behaviour, not to mention it's very un-pythonic in terms of explicity. I dislike the "if len()" one too, however you'll never convince people not take shortcuts like this with dynamic-typed languages. Apparently pep8 suggests using the fact that sequences are False if their length is 0 rather than explicitly checking for 0 length. I know that pep8 is not the end-all for Python style and you should do whatever the gently caress you want, but this seems like a good feature with no consequences aside from subjective differences in readability. Brushing up, apparently code:
code:
QuarkJets fucked around with this message at 19:06 on Aug 8, 2013 |
# ? Aug 8, 2013 19:01 |
|
The way I read the requirements the program only needs to care about the first character of the word.quote:The program is supposed to ask for a word and if it start with a vowel it should print vowel. If the word doesn't start with a vowel it should print consonant. The problem I'm having is no matter what word I put in it, it print 'consonant'. Also, raw_input always returns a string unless there's an EOF character (and I don't care to worry about that) so no need to check for None. Here's a version of the program that takes care to make sure the user enters something but only cares about the first letter of the input. Python code:
Dren fucked around with this message at 19:07 on Aug 8, 2013 |
# ? Aug 8, 2013 19:02 |
|
QuarkJets posted:Brushing up, apparently is tests identity. It tests if two things are the same thing. == tests equality. It tests if two things evaluate to the same value. code:
Secondly, and I'm not as sure about myself here, I think the fact that a == True and a is True both give you the same result is implementation-specific. In other words, I think, that the next version of CPython could change that, or another implementation, like IronPython for example, might return False when you ask it to evaluate a is True, because conceptually a with the value True is a different object from True.
|
# ? Aug 8, 2013 19:18 |
What do you guys typically use for a production stack? apache2? gunicorn? nginx out in front of those? How do you handle deployments to a production environment?
|
|
# ? Aug 8, 2013 19:28 |
|
fletcher posted:What do you guys typically use for a production stack? apache2? gunicorn? nginx out in front of those? How do you handle deployments to a production environment? @jesseenoller (the OP iirc) asked exactly this on twitter today, general answer seems to be: varnish -> nginx -> gunicorn/uwsgi -> pypy if possible -> pgbouncer -> postgres All being automated by Chef/Puppet/Salt/Ansible/Shell Scripts/Trained Monkeys. Or to just use a PaaS provider (Heroku). edit: It just struck me how many giants of Python have actually posted in this thread. deimos fucked around with this message at 19:50 on Aug 8, 2013 |
# ? Aug 8, 2013 19:46 |
|
Thermopyle posted:Secondly, and I'm not as sure about myself here, I think the fact that a == True and a is True both give you the same result is implementation-specific. In other words, I think, that the next version of CPython could change that, or another implementation, like IronPython for example, might return False when you ask it to evaluate a is True, because conceptually a with the value True is a different object from True. IronPython does have single global True and False objects which surprises me a bit because it doesn't do that with strings and numbers like CPython will. I do think you're right about it being platform specific, though, because I can easily imagine an interpreter with a braindead-simple bool class that doesn't memoize itself.
|
# ? Aug 8, 2013 20:44 |
|
Thermopyle posted:
I always just thought that True and False were immutable. So if you assign a to five and then ask if a is 5, the result should be true, because 5 is an immutable object. Same with strings.
|
# ? Aug 8, 2013 21:52 |
|
keyvin posted:I always just thought that True and False were immutable. So if you assign a to five and then ask if a is 5, the result should be true, because 5 is an immutable object. Same with strings. 5 might be immutable but it doesn't make it a reference to the same object, you can have many ints with the same value, this is strictly implementation dependent.. This is only true for -5 to 256 in CPython btw, try it with anything above and is will fail. ej. code:
deimos fucked around with this message at 22:03 on Aug 8, 2013 |
# ? Aug 8, 2013 21:59 |
|
keyvin posted:I always just thought that True and False were immutable. So if you assign a to five and then ask if a is 5, the result should be true, because 5 is an immutable object. Same with strings. Under that logic, 0 would always equal True (0 is immutable!) and lists would always equal false.
|
# ? Aug 8, 2013 22:03 |
|
Thermopyle posted:Or just do it the way the whole Python community does. I'd argue that code:
If len(None) = 0 , thats a convenience but its not necessarily coherent (It SHOULD raise an exception).
|
# ? Aug 9, 2013 01:05 |
|
Haystack posted:Under that logic, 0 would always equal True (0 is immutable!) and lists would always equal false. well, no. I didn't realize larger numbers didn't work, but I thought python just created one of an immutable object and tried to not make duplicates in memory, because if an object can't change then why would you create multiple string objects that all contained the same data. But now I see it doesn't work with tuples at all so I learned something neat! How does python recognize that two string objects are the same with the is operator? Does it actually compare the address (sorry don't know the right term as I was a C programmer) or does it just do like a strcmp for equality? Edit: Using ID on different string objects show that they do have the same address. Is this also implementation specific? Edit: The above should say that assigning two different variables to the same string at different points of execution results in them pointing to the same string object SYSV Fanfic fucked around with this message at 02:04 on Aug 9, 2013 |
# ? Aug 9, 2013 01:50 |
|
keyvin posted:I always just thought that True and False were immutable. So if you assign a to five and then ask if a is 5, the result should be true, because 5 is an immutable object. Same with strings. True and False are immutable in sane versions of Python. Python code:
Python code:
keyvin posted:How does python recognize that two string objects are the same with the is operator? Does it actually compare the address (sorry don't know the right term as I was a C programmer) or does it just do like a strcmp for equality? If they have the same ID, they aren't different string objects. Where did these strings come from? Note that the Python interpreter interns string literals and reuses objects if possible, even in the interactive interpreter. Python code:
Python code:
Lysidas fucked around with this message at 02:04 on Aug 9, 2013 |
# ? Aug 9, 2013 01:53 |
deimos posted:@jesseenoller (the OP iirc) asked exactly this on twitter today, general answer seems to be: I've been meaning to learn how to use chef. Been playing around with it for the past couple hours and it is really neat. One question though, and this may not be the right thread for it, but how do I get the code for my app onto the server using chef? The code is in a remote git repo...should I use git to download the code in my chef recipe? I'm sure there's a million different ways this could be answered, just looking for something basic and sensible for now. edit: This sounds like the answer to my question: http://docs.opscode.com/resource_deploy.html fletcher fucked around with this message at 06:37 on Aug 9, 2013 |
|
# ? Aug 9, 2013 04:48 |
|
Taking advantage of "empty sequences are false" goes right out the door if you use numpy:code:
|
# ? Aug 9, 2013 07:11 |
|
For a personal project I need to be able to do some basic linear algebra (like solving systems of linear equations), but it needs to be symbolic, at least up to a point. So for example, it can't require that everything is a floating point number, or anything like that. I might need to write a class whose members act like elements of the field \mathbb{Q}[\sqrt{2}] (the set of all numbers of the form p + q√2 where p and q are rational numbers) and apply it to that. I started writing my own basic Matrix class. Is there a significantly better way of doing things I should be looking at? If the alternatives are all large and ponderous systems that I'd have to learn then I'll probably just finish my own Matrix class (it is quite fun) but I thought I would check. QuarkJets posted:Brushing up, apparently You shouldn't use if somebool == True: because that has the same effect as if somebool: which is more straightforward and idiomatic. There is a case for if somebool is True because True and False, like None, are singleton objects and you might sometimes want to use True as a special value (e.g. for function arguments) (there are good arguments against doing this too though)
|
# ? Aug 9, 2013 14:03 |
|
Hammerite posted:For a personal project I need to be able to do some basic linear algebra (like solving systems of linear equations), but it needs to be symbolic, at least up to a point. So for example, it can't require that everything is a floating point number, or anything like that. I might need to write a class whose members act like elements of the field \mathbb{Q}[\sqrt{2}] (the set of all numbers of the form p + q√2 where p and q are rational numbers) and apply it to that. Have you considered using SymPy? http://docs.sympy.org/0.6.7/tutorial.html#linear-algebra http://docs.sympy.org/0.6.7/modules/mpmath/matrices.html
|
# ? Aug 9, 2013 16:06 |
|
duck monster posted:I'd argue that I agree. I was saying to do if x:, which while also not completely coherent, is not completely incoherent and is also less verbose and it's widely used. Really, though, the whole issue is a molehill that is already worth less than the amount of words I've typed about it.
|
# ? Aug 9, 2013 18:26 |
Trying to set up a new machine but I'm running into an error when I try to run the south migration. It seems to work fine on my dev machine running Mint, but when my chef recipe gets to this step on Ubuntu Server 12.04 it hits an error. Any ideas?code:
fletcher fucked around with this message at 20:46 on Aug 9, 2013 |
|
# ? Aug 9, 2013 20:29 |
|
fletcher posted:Trying to set up a new machine but I'm running into an error when I try to run the south migration. It seems to work fine on my dev machine running Mint, but when my chef recipe gets to this step on Ubuntu Server 12.04 it hits an error. Any ideas? For future reference, there's a Django thread.
|
# ? Aug 9, 2013 21:47 |
|
Omnomnomnivore posted:Taking advantage of "empty sequences are false" goes right out the door if you use numpy: Arrays support broadcasting, so it really is ambiguous. Whether an array is True is ambiguous; you might be asking whether the array has some nonzero length or you might be asking for the operation to return an array of True/False values. You also might be asking whether any of the elements are true, or all of them, as the suggestion in the exception implies. This kind of thing isn't a problem for strings or lists, since they don't support broadcasting. Hammerite posted:You shouldn't use if somebool == True: because that has the same effect as if somebool: which is more straightforward and idiomatic. I'm aware of that, and it's irrelevant to my question of why PEP8 says that 'is' is worse than '==' if you compare a bool to True. PEP8 also suggests that you should always just do if somebool, but that's for obvious reasons. QuarkJets fucked around with this message at 09:42 on Aug 10, 2013 |
# ? Aug 10, 2013 09:32 |
|
The other reason to use is rather than == when testing against None is operator overloading.Python code:
e: woops this was so last page
|
# ? Aug 10, 2013 16:20 |
|
I want to write a thing which will do a thing when a remote server tells it to do it. Probably over HTTP. Nothing complex. One server telling another server "take this string and The Thing with it". What's my options for something light weight and simple and secure (so not just anyone can say "hey, do The Thing")?
|
# ? Aug 10, 2013 17:39 |
|
Thermopyle posted:I want to write a thing which will do a thing when a remote server tells it to do it. Probably over HTTP. Try using a simple web server framework, like web.py or Flask. More info at http://stackoverflow.com/questions/713847/recommendations-of-python-rest-web-services-framework
|
# ? Aug 10, 2013 18:16 |
|
Crosscontaminant posted:The other reason to use is rather than == when testing against None is operator overloading. Is it possible to overload 'is'?
|
# ? Aug 10, 2013 19:47 |
|
No.
|
# ? Aug 10, 2013 19:52 |
|
BeefofAges posted:Try using a simple web server framework, like web.py or Flask. Yeah, I'll just do that if there's nothing more purpose-built for this.
|
# ? Aug 10, 2013 20:32 |
|
xmlrpclib is built in and standard and easy as gently caress to use. You can even use it inside apache if you don't want to write your own daemon.
deedee megadoodoo fucked around with this message at 21:34 on Aug 10, 2013 |
# ? Aug 10, 2013 21:30 |
|
Doing some more lessons and I have run into another problem.code:
|
# ? Aug 10, 2013 21:39 |
|
digitalcamo posted:Doing some more lessons and I have run into another problem. I copy/pasted what you have there and it worked fine. I forget, what text editor are you using? PS: A better way of testing for different capitalization is to do it like: Python code:
|
# ? Aug 10, 2013 21:45 |
|
digitalcamo posted:Doing some more lessons and I have run into another problem. I don't know about that error, but this code won't do what you are expecting. For example: code:
|
# ? Aug 10, 2013 21:48 |
|
digitalcamo posted:Doing some more lessons and I have run into another problem. Dominoes fucked around with this message at 22:03 on Aug 10, 2013 |
# ? Aug 10, 2013 22:00 |
|
I've found the solution to a lot of my problems is using Codecademy. I can put in the exact code they are looking for but I get some kind of error. When I use my own text editor it works just fine. So a lot of times once I KNOW my code is correct I have to search online to find out what the problem is. Oh and I'm using Notepad++.
|
# ? Aug 10, 2013 22:13 |
|
digitalcamo posted:I've found the solution to a lot of my problems is using Codecademy. I can put in the exact code they are looking for but I get some kind of error. When I use my own text editor it works just fine. So a lot of times once I KNOW my code is correct I have to search online to find out what the problem is. Oh and I'm using Notepad++. Codeacademy is probably testing different strings for your function. As someone posted above, your function will return 'Shutting down.' no matter what the passed string contains. So if it is passing 'no', it will still return 'Shutting down.'. The problem is the test in your if/elif block. Python's or keyword is a boolean operator, meaning it checks whether or not one or more of the objects being tested is true, then returns In Python, non-empty strings will return True when doing boolean operation. With that in mind, what is going on here is in your if condition: code:
You are not making a single test here. You are testing the equivalency of s and 'Yes', which will return False. Equivalent code: code:
code:
To translate what you were trying to do in a single test, you could either do: code:
As said above, the other solution to this test is to use the 'lower()' method string objects have. Keep in mind, the method does not assign the lowercase string to the variable s. It creates an instance for that test, and discards it. So for every test you need to remember to use the 'lower()' method. What you could do is: code:
double edit: heres a quick reference for what I'm talking about. Pretty much the basic python cheat sheet, tis invaluable when learning: http://docs.python.org/2/library/stdtypes.html (scroll down to boolean operations and comparisons) Are you using any other resources than Codeacademy? Two space indent supremacy~~ salisbury shake fucked around with this message at 01:21 on Aug 11, 2013 |
# ? Aug 10, 2013 22:49 |
|
digitalcamo posted:I've found the solution to a lot of my problems is using Codecademy. I can put in the exact code they are looking for but I get some kind of error. When I use my own text editor it works just fine. So a lot of times once I KNOW my code is correct I have to search online to find out what the problem is. Oh and I'm using Notepad++. Do not ever, ever mix tabs and spaces for indentation. code:
|
# ? Aug 10, 2013 23:30 |
|
What version of Python emits IndentationError here? Both Python 2.7.5 with the -tt flag and Python 3.3.2 emit TabError when given your code sample, and I don't have other versions on hand to test.
|
# ? Aug 10, 2013 23:47 |
|
I'm having issues with scipy.optimize.curve_fit. Sometimes instead of returning a covariance matrix, it just returns inf (I guess if you're giving it data that is really really hard to fit to). Obviously this fucks everything up if I'm treating the returned variable as a 2D array when it's actually a float sometimes. I could use isinstance to make sure that it's not a float, but common wisdom seems to be to avoid isinstance. I could use a try/except block, but that seems sloppy. (basically curve_fit should have been designed to return a matrix of infs instead of just inf when it has trouble fitting to data, but since that's not the case what should I do to compensate?)
|
# ? Aug 11, 2013 04:34 |
|
accipter posted:Have you considered using SymPy? 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.
|
# ? Aug 11, 2013 18:03 |
|
|
# ? May 9, 2024 15:58 |
|
PYQT signals/slots question: I'm getting crashes with QT threads. After troubleshooting and internet searching, it appears that the crashes are caused by updating the GUI from a thread other than its own. The solution I've read is to be to never 'update' the GUI directly, but use signals/slots instead. I think I'm misunderstanding signals/slots, and what the difference is between using them and the 'updating GUI' behavior that causes crashes. I've been using commands such as: Python code:
I've been using slots the following way: code:
Python code:
I found some examples that uses formats such as: Python code:
Dominoes fucked around with this message at 16:42 on Aug 12, 2013 |
# ? Aug 12, 2013 04:47 |