pangstrom posted:Probably sounds like faux outrage but I'm really just curious: is starting bullet lists for human consumption with element zero something programmers routinely do? I do it, because otherwise my mind will snap back to one-based indexing and my code will start breaking on me. Years of MATLAB left permanent damage.
|
|
# ¿ Jun 1, 2017 15:15 |
|
|
# ¿ May 15, 2024 23:28 |
Cocoa Crispies posted:In Ruby and Elixir, I got in the habit of chaining up operations on collections, with chained Enumerable methods in the former and pipes in the latter: You can chain comprehensions, re-using the same variable name(s) each time. The interpreter will know what you mean. Chaining generator comprehensions in this way is considered idiomatic python, and is fast & efficient! IN Python code:
code:
Python code:
Eela6 fucked around with this message at 06:34 on Jun 6, 2017 |
|
# ¿ Jun 6, 2017 06:22 |
Malcolm XML posted:fluent python owns
|
|
# ¿ Jun 7, 2017 21:39 |
accipter posted:You can with py3redirect!
|
|
# ¿ Jun 9, 2017 17:10 |
Thermopyle posted:This thread has always been open to beginners. The poster who has been asking questions is obviously a beginner. Floating point stuff obviously is weird to beginners. Numerical Analysis is not an easy subject and it frustrates me when people pretend it's simple & intuitive.
|
|
# ¿ Jun 11, 2017 19:21 |
Nippashish posted:In the vast (vast) majority of cases they are in fact very simple and intuitive. It's extremely rare to need a mental model of floating point that is more sophisticated than "they're like real numbers except that they get funny when they're really big or really small" even if you work with them every day. Python code:
|
|
# ¿ Jun 11, 2017 19:45 |
Nippashish posted:That's why I'm suggesting a very simple mental model that is intuitive and also sufficient for even non-beginners. Teaching people that floating point numbers are dark and spooky and complicated isn't very productive, because very few people need to care about that level of detail. I'm not against teaching people rules of thumb, but pretending that they're more than just rules of thumb is dumb. An important part of being a professional is knowing the limits of your knowledge.
|
|
# ¿ Jun 11, 2017 20:05 |
As a note, the fact that approximate equality is often useful when comparing floats comes up often enough that isclose was added to the math and cmath modules in Python 3.5. Python code:
|
|
# ¿ Jun 11, 2017 20:16 |
By the way, you shouldn't store currency in floats.
|
|
# ¿ Jun 11, 2017 20:57 |
baka kaba posted:I mean yeah, the other extreme is the 'you can't trust floats' one, where people pretend they're full of gremlins adding random inaccuracy when your back is turned, but that's not the same as being wary and knowing what does and doesn't work You can't trust them, though. They seem to stay still when your back is turned, but I can feel the heat of their hideous eyes. Watching. Waiting. Ready to pounce. They may not be Real, but their claws are sharp enough. funny Star Wars parody posted:I'm assuming that you should use doubles but please expand on this Stack Overflow has a reasonable answer. You should not use doubles - you should use Decimals. To quote the relevant part: Zneak from Stack Overflow posted:The problem with floats and doubles is that the vast majority of money-like numbers don't have an exact representation as a integer times a power of two. In fact, the only fractions of a hundred between 0/100 and 100/100 (which are significant when dealing with money because they're integer cents) that can be represented exactly as an IEEE-754 binary floating-point number are 0, 0.25, 0.5, 0.75 and 1. All the others are off by a small amount. I mostly posted the point about currency to tweak Nippanish's nose, but I'm actually glad I did. More people should know. Eela6 fucked around with this message at 21:19 on Jun 11, 2017 |
|
# ¿ Jun 11, 2017 21:08 |
I am on that side. That said, I learned how to program in a numerical analysis class, so I recognize I am a bit of an outlier here.
|
|
# ¿ Jun 11, 2017 23:20 |
Thermopyle posted:Async basically only helps with IO-bound tasks. If you are mostly CPU-bound you should probably use multiprocessing. I agree with thermopyle. If you're actually pegging one of your CPUs asyncio is probably not the solution.
|
|
# ¿ Jun 15, 2017 14:52 |
Loving Africa Chaps posted:Hello thread looking for some advice: You should do this with linear algebra and numpy. There's no need to use PyPy or anything like that - this is the exact use case for numpy's numeric computing tools. Don't wrap it in a class. If you give me the actual math or more context about you're trying to do, I can show you how.
|
|
# ¿ Jun 17, 2017 16:44 |
Loving Africa Chaps posted:It's a series of differential equations Would you mind giving me the differential equations? My skills are a little rusty but this is something I used to be pretty good at. I have an easier time with the mathematical notation. (Funnily enough, I had my old Numerical Methods for Ordinary Differential Systems book on my lap as you posted this - I'm putting all my books in cardboard boxes before I move.)
|
|
# ¿ Jun 17, 2017 17:46 |
I agree. Differential equations are prone to all sorts of numerical analysis problems. Don't roll your own solutions - use a known stable algorithm.
|
|
# ¿ Jun 17, 2017 18:41 |
Really? I had no idea. (I rolled my own back when I was in academia, but that was part of my numerical analysis course work. I figured there had to be good libraries out there.)
|
|
# ¿ Jun 17, 2017 19:18 |
Philip Rivers posted:What's the best way to store dynamic values in Python do y'all think? I have objects with elements that are constantly changing and need to be updated and then checked against each timestep and I'm not really very good at optimization! This is a very vague question. Can you be more specific?
|
|
# ¿ Jun 20, 2017 19:20 |
Depending on the size of your simulation, I would worry about it too much. Use whatever you find most readable. If you find that it's too inefficient for the calculations you're trying to do, start looking into the numpy / scipy packages.
|
|
# ¿ Jun 20, 2017 19:36 |
Philip Rivers posted:Yeah, I'm currently having efficiency issues. The naive implementation is pulling the endpoint positions from a list of the line objects but it's not fast enough. I don't know if a numpy array would be useful because since the positions are always updating I would need to rebuild the array every timestep. Rebuilding numpy arrays is very fast, especially if you properly vectorize.
|
|
# ¿ Jun 20, 2017 19:49 |
Philip Rivers posted:Do you have any resources on this? I'm very out of my depth with a lot of this and I'm just fumbling in the dark. This video by Jake Vanderplass is a good start. https://www.youtube.com/watch?v=EEUXKG97YRw
|
|
# ¿ Jun 20, 2017 21:05 |
NaN == n is False, even if n is NaN. It's in the IEEE specificiations for floating point numbers. This is occasionally inconvenient but it's the way it is - the spec is older than Python! This is true both for numpy and Python's built-in float type, though you're unlikely to encounter NaN in that case, since most operations that produce it will raise an exception first. Eela6 fucked around with this message at 16:14 on Jul 4, 2017 |
|
# ¿ Jul 4, 2017 16:06 |
Learning to program is an iterative process. You're constantly going to go back to code you wrote a month ago and shudder at the quality of the code you wrote then - it's a natural part of the process. It's good to want to improve your code, but don't stress out too much. Every single good programmer was once a terrible programmer. There's no single answer as to how best to structure your code. What is best depends on context. However, generally speaking, deeply nested dictionaries are a 'code smell' - it might be time to refactor and start working in classes.
|
|
# ¿ Jul 5, 2017 15:37 |
You may find this talk by Brett slatkin educational. https://m.youtube.com/watch?v=D_6ybDcU5gc
|
|
# ¿ Jul 5, 2017 15:48 |
QuarkJets posted:It looks like your Fighter class defines ages for various Races. It might be better if a single Age value is defined under CClass (because all characters should have an age), and then Fighter and Race can modify it, maybe using if statements to check the race and class type and then adjusting age appropriately. To be more specific, you probably want to factor this out into a function something like this: Python code:
Eela6 fucked around with this message at 21:50 on Jul 5, 2017 |
|
# ¿ Jul 5, 2017 21:45 |
Philip Rivers posted:So here's a quick question, I got a bunch of nodes connected in a graph and I wanna store info and the chirality of those connections. Is there a pretty/Pythonic way I could assemble/sort lists by chirality? If you just want to sort the list once, that's easy as pie: Python code:
Python code:
Eela6 fucked around with this message at 01:53 on Jul 7, 2017 |
|
# ¿ Jul 7, 2017 01:50 |
You want to implement the magic method __eq__. __eq__ is implemented like this: Python code:
Python code:
here's an example. Python code:
Eela6 fucked around with this message at 21:03 on Jul 21, 2017 |
|
# ¿ Jul 21, 2017 20:48 |
onionradish posted:I'd considered __eq__, and actually used it during the "upgrade" from the namedtuple to the class. Then, once the class __init__ changed, that broke and I needed to rethink. (I've been looking at the code for too long, so likely not thinking clearly.) I'm fine with it, but I like dynamic magic. I would make the following changes Python code:
Python code:
If you want to go crazy, you can do this: Python code:
Python code:
code:
Eela6 fucked around with this message at 22:57 on Jul 21, 2017 |
|
# ¿ Jul 21, 2017 22:32 |
As far as NotImplemented vs NotImplementedError goes, read the docs at https://docs.python.org/3/library/constants.html or Ch. 13 of Fluent Python: Operator Overloading, or see this post I just made where I explained how the interpereter uses NotImplemented
Eela6 fucked around with this message at 22:59 on Jul 21, 2017 |
|
# ¿ Jul 21, 2017 22:54 |
onionradish posted:I'd never seen that before. I learned something new! Thanks again. Check this out. 0. self.__eq__(other) -> self._public_attribs == other 1. self._public_attribs.__eq__(other) is NotImplemented. 2. Fallback to other.__eq__(self._public_attribs) -> other._public_attributes == self.public_attribs. 3. These are both dictionaries, so they compare 'normally'. Python is cool. Eela6 fucked around with this message at 23:40 on Jul 21, 2017 |
|
# ¿ Jul 21, 2017 23:36 |
onionradish posted:
I agree with your decision, actually. Explicit is better than implicit. I just wanted to show off
|
|
# ¿ Jul 21, 2017 23:55 |
Boris Galerkin posted:Is it ok to do this? This seems like a take on lazy properties; i.e, calculate once, then store. I personally like the lazyproperty decorator described in the Python Cookbook by Beazley and Jones: Python code:
Python code:
Python code:
|
|
# ¿ Jul 27, 2017 18:36 |
Thermopyle posted:Say i have a routine that searches for and gets a thing. You can search for a thing via a regex, via an int, or via a tuple. Which of the following do you prefer? I would combine the first two approaches. The first approach is easiest for the user, the second is easiest for the developer. code:
|
|
# ¿ Aug 6, 2017 03:53 |
I completely forgot about singledispatch. I don't think I've ever used it, but maybe I should have.
|
|
# ¿ Aug 6, 2017 21:15 |
Jose posted:i've started playing around with python for work mostly so i can make charts that can be tweeted out quickly or whatever. All i really want to do is import csv's so i can start using matplotlib stuff with them but finding an easy guide of doing this is really annoying me. I've spent a while playing with anaconda and i can import the csv using a couple of methods but they've all added stuff to the output. I'm just after either a decent guide using unicodecsv which seems to have replaced the csv package or the short bit of code that will get me an array or whatever where the first row of the csv is saved as column headers so I can stick things onto an x and y axis. The csv module in the python standard library will work just fine for you. Here's an example to get you started. Suppose we have a CSV of wedding guests, that contain their name, their relationship to the bride and groom, and their food preferences. Here's our CSV: code:
We want to process the list to find out which guests need what food. We can do so as follows: IN Python code:
code:
|
|
# ¿ Aug 14, 2017 17:39 |
Nippashish posted:Seriously, do this. Do not even consider the csv reader in the stdlib. Pandas is infinitely superior. I don't feel that way. I don't particularly like pandas, and I prefer using the stdlib where possible. You have to consider the audience of your code, too. Jose is very new to python; the last thing he needs is a thousand different APIs to understand. Cingulate posted:Situations where I can see the hand-crafted option make sense: I am no fan of masochism. I just don't like solutions that are 'well, first install this dependency' for simple problems. I think you should use the level of tool that's appropriate for your problem. If I'm using CSVs, I'm probably going to be doing it by hand. If I have a big dataset that requires the big guns, 1. I'm going to use xarray , not pandas 2. Why the hell am I using CSVs? Eela6 fucked around with this message at 20:24 on Aug 14, 2017 |
|
# ¿ Aug 14, 2017 20:19 |
Thermopyle posted:Probably, most people who need to read csv's don't need pandas, but it certainly seems like this person could use it. Fair enough.
|
|
# ¿ Aug 14, 2017 22:16 |
I've now been working strictly in golang for about three months. Having stepped away from Python, here the three things I miss the most: 1. Set type as a primitive 2. Comprehension literals (especially generator comprehensions!) 3. Context managers ( with statement) Python is a good language.
|
|
# ¿ Aug 28, 2017 16:24 |
Munkeymon posted:Anaconda would do all of that This sounds like the perfect use case for Anaconda. Spyder isn't great, but it will work just fine for students. It's as good of an 'IDE' for beginners as any, in my opinion. (Spyder has the advantage of being being configured by default so that you can slam F5 and see the results of your script in a live IPython session in the bottom right corner. This is very helpful for beginners, especially in a math/scientific computing context. As a developer IDE, it's not great, but I think its just right for this.)
|
|
# ¿ Sep 14, 2017 19:48 |
QuarkJets posted:IIRC the people who made Spyder were actively trying to mimic the Matlab IDE, to act as an entry point to former Matlab users who aren't already fully consumed by Stockholm's Syndrome Huh. That explains why I took to Spyder immediately as a new Python programmer (I learned to program in MATLAB) numpy-in-spyder vs. MATLAB are remarkably similar, with the exception that numpy is good Eela6 fucked around with this message at 00:22 on Sep 15, 2017 |
|
# ¿ Sep 15, 2017 00:20 |
|
|
# ¿ May 15, 2024 23:28 |
Daviclond posted:Why does this one-liner: Let's look at this in a little more detail. list.reverse() is a method which reverses the list in place. It returns None. IN: Python code:
pre:[3, 2, 1] None Python code:
pre:<list_reverseiterator at 0x2f46d9ef0f0> [1, 2, 3] [3, 2, 1] Python code:
pre:[3, 2, 1] [1, 2, 3] Eela6 fucked around with this message at 01:08 on Sep 16, 2017 |
|
# ¿ Sep 15, 2017 23:02 |