|
For issues like syntax and function signatures, the official docs are your best bet. I refer to them all the time. docs.python.org/3
|
# ? Aug 21, 2014 06:05 |
|
|
# ? May 9, 2024 00:18 |
|
So I was reading about test-driven development, and I'm trying it out by implementing suuuuuuper simple algorithms in Python. Initially using everybody's favorite: bubble sort. So at first I wrote this: Python code:
Python code:
|
# ? Aug 21, 2014 07:41 |
|
IMO you should've kept both tests. The one with your hardcoded sorted data and the one testing against the built-in sort. Imagine how interesting it'd be if only one of those tests fail.
|
# ? Aug 21, 2014 09:46 |
|
Symbolic Butt posted:IMO you should've kept both tests. The one with your hardcoded sorted data and the one testing against the built-in sort. I like (and have implemented) that idea. Is 7 tests for a loving numeric bubble sorter too many or too few?
|
# ? Aug 21, 2014 10:06 |
|
Also; I don't know if I'm a fan of "bubble.sort". To me it feels like it should be "sort.bubble" or similar. Because then you could have various other sort options in the same "sort" library.
|
# ? Aug 21, 2014 10:10 |
|
Literally Elvis posted:But now I'm wondering if it's a good idea at all to compare a function I wrote with a built-in function, even though it's relatively simple. One issue: Python code:
Either explicitly copy the unsorted list before sorting or use the built-in sorted which returns a new sorted list: Python code:
|
# ? Aug 21, 2014 13:45 |
|
We've always recommended against dive into python in this thread, but I don't know if the version for Python 3 has improved? To be honest, I can't remember why we recommended against it in the past...
|
# ? Aug 21, 2014 15:53 |
|
Thermopyle posted:We've always recommended against dive into python in this thread, but I don't know if the version for Python 3 has improved? http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html That page on there was pretty useful for me. Lots of syntax examples and whatnot
|
# ? Aug 21, 2014 16:44 |
|
Thermopyle posted:To be honest, I can't remember why we recommended against it in the past... A part of the problem was the book was written for Python 2.1/2 and hasn't been updated since Python 2.3, so it was missing about seven years of Python 2.x development. A version specifically for 3.x presumably wouldn't have that problem.
|
# ? Aug 21, 2014 16:45 |
|
Thermopyle posted:To be honest, I can't remember why we recommended against it in the past... It's comically out of date and tells you to do lots of terrible things as a result, and large chunks of it are entirely irrelevant now (such as the chapters on ODBC).
|
# ? Aug 21, 2014 16:57 |
|
If you're a true beginner to programming, I'm going to recommend Think Python (and start with the version adapted to be Python 3 only). It's a free PDF too.
|
# ? Aug 21, 2014 17:27 |
|
ShadowHawk posted:If you're a true beginner to programming, I'm going to recommend Think Python (and start with the version adapted to be Python 3 only). It's a free PDF too. Yes, this is good advice.
|
# ? Aug 21, 2014 18:19 |
|
Jewel posted:Also; I don't know if I'm a fan of "bubble.sort". To me it feels like it should be "sort.bubble" or similar. Because then you could have various other sort options in the same "sort" library. Yeah, I've been thinking a lot about how I'm going to organize all of these. I sort of like the idea of having one main sort.py and things like bubble_test.py and quicksort_test.py for the testing pages, but that can all be revised later. 'ST posted:One issue: I wasn't super stoked about how the code I wrote read, and this is much better. Thanks. Thermopyle posted:We've always recommended against dive into python in this thread, but I don't know if the version for Python 3 has improved?
|
# ? Aug 21, 2014 18:22 |
|
This might not cover everything you would need to know, but I really like how this site lays things out http://www.dotnetperls.com/python
|
# ? Aug 21, 2014 18:28 |
|
I don't know how I missed this, but I hadn't really paid much attention to annotations in Python 3. Well, it turns out that you can use them for type hinting in PyCharm, meaning that it makes completions and static analysis more useful. They basically replace using rtype and type in docstrings (if you're doing that just to get PyCharm to help you out more). I made a little gif showing the type warning and completions that PyCharm can do if you use annotations: http://gfycat.com/SilverGloriousBovine For those who are unaware annotations were introduced by PEP-3107. Basically, they just provide a way to attach an expression to function parameters and function return values. Python itself doesn't do anything with these annotations other than not barf out a syntax error when it encounters them. It's completely up to other tools to do something with them. PyCharm uses them to figure out types so it can give you hints and completions.
|
# ? Aug 21, 2014 20:11 |
|
Annotations are awesome. My only complaint is there's no particularly easy way to annotate a function as returning another function. You can annotate strings and ints using str and int and the like, but there's no function primitive (lambda won't work without being defined). Speaking of annotations, I'm getting curious about optimizing a bit of code using cython, but it seems to require its own special annotations rather than just being smart enough to use the Python3 annotations. Has anyone had experience compiling with cython?
|
# ? Aug 21, 2014 22:11 |
|
ShadowHawk posted:Annotations are awesome. My only complaint is there's no particularly easy way to annotate a function as returning another function. You can annotate strings and ints using str and int and the like, but there's no function primitive (lambda won't work without being defined). Have you looked at using Numba over Cython? This comparison shows that Numba is pretty similar in speed, and much simpler to use.
|
# ? Aug 21, 2014 22:42 |
|
Literally Elvis posted:Is 7 tests for a loving numeric bubble sorter too many or too few? Depends on the tests. Is everything being tested that's reasonable to test? Are they fast to run? Are they informative?
|
# ? Aug 22, 2014 03:56 |
|
Thermopyle posted:We've always recommended against dive into python in this thread, but I don't know if the version for Python 3 has improved? It has not. Somewhere on the internet is a document we compiled of all the mistakes in DIP3, archived from a Google Wave document. I couldn't find it on my phone, but search for habnabit, papna, marienz and others.
|
# ? Aug 22, 2014 04:42 |
|
Suspicious Dish posted:It has not. Somewhere on the internet is a document we compiled of all the mistakes in DIP3, archived from a Google Wave document. I couldn't find it on my phone, but search for habnabit, papna, marienz and others. I can't find those at all, nor can I seem to find any scathing criticism from any of the book's reviewers. Like, the Amazon page for the book has negative reviews of the print quality, and there are some complaints elsewhere that the book wasn't targeted towards beginners. But I haven't found anything that says "god drat this book is trash." I'm really interested to read what those complaints were, though, so if somebody could find that discussion or reiterate their points, I'd love to see it.
|
# ? Aug 22, 2014 05:02 |
|
accipter posted:Have you looked at using Numba over Cython? This comparison shows that Numba is pretty similar in speed, and much simpler to use. Someone I work with who is otherwise a very competent dude was adamant recently that numba and cython are almost identical; not in effect, but actual implementation. My understanding was that numba produces LLVM IR on the fly that gets executed in its virtual machine (or whatever), while cython is more of a static thing - producing C that is then compiled. Am I on the right track? I'm a big fan of Numba, too.
|
# ? Aug 23, 2014 02:39 |
|
accipter posted:Have you looked at using Numba over Cython? This comparison shows that Numba is pretty similar in speed, and much simpler to use. Numba, for instance, would error when I @jit that single function.
|
# ? Aug 23, 2014 02:43 |
|
ohgodwhat posted:Someone I work with who is otherwise a very competent dude was adamant recently that numba and cython are almost identical; not in effect, but actual implementation. My understanding was that numba produces LLVM IR on the fly that gets executed in its virtual machine (or whatever), while cython is more of a static thing - producing C that is then compiled. Am I on the right track? That is exactly correct. To add more context, compiling python for performance is always all about obtaining type information upon which to specialize on. Cython does it with explicit annotations and separate compilation step, PyPy does it with an instrumented interpreter that performs tracing data-flow analysis, and Numba does it by using NumPy type information and optional runtime arguments to emit LLVM byte code..
|
# ? Aug 23, 2014 03:52 |
|
BigRedDot posted:That is exactly correct. To add more context, compiling python for performance is always all about obtaining type information upon which to specialize on. Cython does it with explicit annotations and separate compilation step, PyPy does it with an instrumented interpreter that performs tracing data-flow analysis, and Numba does it by using NumPy type information and optional runtime arguments to emit LLVM byte code.. Although Cython at least has that as a todo list item.
|
# ? Aug 23, 2014 05:41 |
|
ShadowHawk posted:And yet, none of them do it via Python 3's function annotations :/ Backport function annotations to a python 2.8 and you'd probably have that next. week. That said, Numda devs are not unaware, but are waiting for the current discussions over MyPy style annotations to settle, since Guido has expressed apparent interest in them: https://github.com/numba/numba/issues/229
|
# ? Aug 23, 2014 15:58 |
|
Got a little problem with Doctest. Im wondering why my output is throwing two lists. My code for solving the problem looks right because the second list that it "got". Im thinking it has to do with my local variables and function instantiation issues(?). Not really sure. If Doctest got what it expected I shouldnt see anything in the output, not the output below. Any hints?? Bolded the part where the two lists are shown below. quote:
Heres my code: Python code:
Tacos Al Pastor fucked around with this message at 23:18 on Aug 23, 2014 |
# ? Aug 23, 2014 23:07 |
|
spiralbrain posted:Got a little problem with Doctest. Im wondering why my output is throwing two lists. My code for solving the problem looks right because the second list I found it faster to rewrite what you were trying to do rather than parse your code. Python code:
Python code:
edit: Python code:
Python code:
ShadowHawk fucked around with this message at 23:35 on Aug 23, 2014 |
# ? Aug 23, 2014 23:29 |
|
Hey, this is my first program ever, besides the paired arduino program to go with this one. For some reason I can't get the break in the loop to work. What am I missing here?code:
dirtycajun fucked around with this message at 03:45 on Aug 24, 2014 |
# ? Aug 24, 2014 03:43 |
|
You're not incrementing hour. Put the time calls in the loop to fix.
|
# ? Aug 24, 2014 03:46 |
|
Dominoes posted:You're not incrementing hour. Put the time calls in the loop to fix. I really am new to this, can you explain what you mean by that?
|
# ? Aug 24, 2014 03:50 |
|
Python code:
Here's some similar, more robust code I use. It handles timezones, and market holidays. You could also use a broker's API call to ask the current market state. Python code:
code:
Dominoes fucked around with this message at 07:29 on Aug 24, 2014 |
# ? Aug 24, 2014 03:54 |
|
In addition to the above... If this Python code:
Python code:
|
# ? Aug 24, 2014 03:58 |
|
It won't just run forever without telling it something to stop? Thanks for the help by the way, your explanation makes sense!
|
# ? Aug 24, 2014 04:00 |
|
The while condition is what stops the loop.
|
# ? Aug 24, 2014 04:08 |
|
Okay I made the changes, turns out I did need the break, and it would only work if I did another time call in the inner loop. It can probably get cleaned up some but this functions:code:
edit: yea, just needed the other time call, no break. DERP dirtycajun fucked around with this message at 04:13 on Aug 24, 2014 |
# ? Aug 24, 2014 04:09 |
|
Get rid of the outside 'While True' loop, and set the hour inside the 'while hour' loop. Btw, US trading hours generally end at 1600 L, not 1630. Dominoes fucked around with this message at 05:10 on Aug 24, 2014 |
# ? Aug 24, 2014 04:14 |
|
Thanks for the help! I'll probably have more questions once I am trying to make this project work over a wireless network and when I am trying to make the python program figure out for itself which COM the arduino is connected to instead of being told.
|
# ? Aug 24, 2014 04:18 |
|
ShadowHawk posted:I found it faster to rewrite what you were trying to do rather than parse your code. Im still learning, so a big thanks for the pointers. The whole point of this was to refactor some code that had some smell to it that was even worse than what I wrote, so any more improvements are appreciated. This was the first time that I used doctest, which I get the point of but honestly I cant wait to move on to unittest. There is something about this doctest that I dont like.
|
# ? Aug 24, 2014 05:38 |
|
dirtycajun posted:Okay I made the changes, turns out I did need the break, and it would only work if I did another time call in the inner loop. It can probably get cleaned up some but this functions: If your plan is to leave it running until you CTRL-C the process from the terminal, you should probably look into trapping signals so that you can shut the light off right before your application quits.
|
# ? Aug 24, 2014 14:59 |
|
|
# ? May 9, 2024 00:18 |
|
Anyone have any suggestions for a memory efficient 2D array of ints with reasonable indexing speed? I've gone down the list of lists route (too much memory usage) and the numpy array route (slow indexing). My next thought is wrapping the builtin array object to have a 2D like interface.
|
# ? Aug 25, 2014 19:47 |