|
Example:code:
|
# ? Aug 28, 2012 19:12 |
|
|
# ? May 8, 2024 23:34 |
|
PiotrLegnica posted:Try these: http://docs.scipy.org/doc/numpy/reference/c-api.array.html#array-scalars Cheers. Those appear to be the correct things (PyArray_Scalar() in particular) but the fact that the documentation refers to parameters which aren't even in the prototype (and has done so since at least 2008 according to git log) and the intermittent references to whether a function needs you to worry about refcounting is freaking me out and leaving me without any confidence in using these functions reliably. I stumbled across this mailing list discussion which says to use the PyArrayScalar_New and PyArrayScalar_ASSIGN macros defined in <numpy/arrayscalars.h>. These things call macros down about 7 layers, so I'm not really sure what the hell they're doing or whether they're handling refcounting correctly. I just return a PyObject pointer to the return value of the _New macro and the calling Python code appears to be a numpy.float32 and sys.refcount says it has a refcount of 2, so I assume everything is correct. These macros aren't documented in the source code and don't appear in the generated docs at all, but aren't called by anything else in the include directory so I don't think they're meant for internal use only. C++ code:
Python code:
|
# ? Aug 29, 2012 02:52 |
|
That is the correct way to do it.code:
BigRedDot fucked around with this message at 04:08 on Aug 30, 2012 |
# ? Aug 30, 2012 04:04 |
|
I learned something today about python coding that is probably very durr to the rest of you but actually will save my rear end in the future. When making arrays from data files, it's not uncommon of me to do something like: code:
code:
Anyway, I don't know how much time this saves, but if it even saves 1 second for a decent sized array, it means that for my latest project, which has ~2000 of said data files, I'll save easily half an hour. I didn't think the coding I did was intensive enough for memory management to mater, but here I am.
|
# ? Aug 30, 2012 22:31 |
|
JetsGuy posted:I learned something today about python coding that is probably very durr to the rest of you but actually will save my rear end in the future. You can pre-allocate numpy arrays with something like numpy.zeros if you know the size of the data, which it appears you do, and it saves you have having to copy everything from a list to a numpy array. Depending on your input format you could also use numpy.loadtxt, which is also quite quick. edit: Also using numpy.arange as your iterable in a for loop is...a strange thing to do, just use xrange so you aren't allocating an entire array just to hold your indices. OnceIWasAnOstrich fucked around with this message at 23:37 on Aug 30, 2012 |
# ? Aug 30, 2012 23:30 |
|
I'm working on Ex. 15 of LearnPythonTheHardWay and I'm a bit stuck. This is the exercise I am trying to complete. http://learnpythonthehardway.org/book/ex15.html I'm running this exercise with Powershell in my C:\Python27 folder. Both my .py file and sample text file I am trying to open and read with this program are in the same C:\python27 folder. When I run python ex15.py ex15_sample.txt I don't get an error, the shell just returns to my C:\python27 > prompt. I read all the comments for the exercise page already and I couldn't figure out the exact problem I'm causing. I think I don't understand something about directing python to the correct folder to open my text file, but I don't believe I'm making the obvious mistakes other people did on the exercise. code:
AmericanBarbarian fucked around with this message at 20:13 on Aug 31, 2012 |
# ? Aug 31, 2012 15:04 |
|
JetsGuy posted:I learned something today about python coding that is probably very durr to the rest of you but actually will save my rear end in the future. I would something like this: code:
code:
accipter fucked around with this message at 15:53 on Aug 31, 2012 |
# ? Aug 31, 2012 15:50 |
|
Dr.Spaceman posted:I'm working on Ex. 15 of LearnPythonTheHardWay and I'm a bit stuck. This is the exercise I am trying to complete. http://learnpythonthehardway.org/book/ex15.html This might be a bit much but you should check to see if C:\Python27\ is in your path by doing 'echo %PATH%' (don't include the quotes). If your path includes C:\Python27\ then you can call python.exe from any location and it will find python.exe located in C:\Python27\. Note that if you have other installations of Python that are included in the PATH and they are listed earlier they will be selected first. You can modify your path using Control Panel .. Advanced System Settings (google for more detail), or by creating a batch file to set a custom path prior to loading PowerShell. Once you solve your path issue you can move your Python code to a more sane location. Is that code block the text in your file? If so you shouldn't have the >>> in there.
|
# ? Aug 31, 2012 15:59 |
|
No, the >>> wasn't in my code. That was an artifact from a copy past I did somewhere else. I added python to my Environmental Variable so now I don't have to abbreviate .\python in powershell to run it. I was aware of how to change that but just lazy. I ran python exp15.py ex15_sample.txt again with the change and got the same result. To be extra clear this is what I get when I run it in Powershell. code:
|
# ? Aug 31, 2012 16:40 |
|
accipter posted:I would something like this: The list comprehensions are probaly the cleanest way to do exactly what your code segments did but again need extra memory as you are reading the entire data array into a list and then reading the list into a numpy array. The records are the fastest but I don't know if they are any slower to access than an array, and they obviously change what kind of format you are dealing with. If you really care how fast this one operation takes. data = 2x1000 array of random ints Numpy append - 1.5s Native append - 151ms List comprehensions - 112ms Preallocated numpy arrays with np.zeros() and direct assignment - 63ms (records) - 24ms
|
# ? Aug 31, 2012 16:46 |
|
Dr.Spaceman posted:No, the >>> wasn't in my code. That was an artifact from a copy past I did somewhere else. I added python to my Environmental Variable so now I don't have to abbreviate .\python in powershell to run it. I was aware of how to change that but just lazy. Try running with -v flag. Try running outside of PowerShell, too. Do you use standard console or something like Console2?
|
# ? Aug 31, 2012 18:11 |
|
I tried it in Console2 and got the same results. Would I use -v flag like this? I've tried this in Powershell and it returns the same results as before.code:
AmericanBarbarian fucked around with this message at 19:26 on Aug 31, 2012 |
# ? Aug 31, 2012 19:06 |
|
A silly question, but are you sure "exp15.py" is the one that contains your code?
|
# ? Aug 31, 2012 19:20 |
|
Suspicious Dish posted:A silly question, but are you sure "exp15.py" is the one that contains your code? No worries, no questions are silly when learning something from scratch. And yes, I'm very sure exp15.py is the correct file with my code, and ex15_sample.txt is the sample text file I am trying to open. At one point I thought I was saving the example file as ex15_sample.txt.txt , but I checked and that wasn't it. When I ran it in Console2 it looks like a file opens and then closes very quickly in the top of the window , but I can't tell anything more than that.
|
# ? Aug 31, 2012 19:30 |
|
Dr.Spaceman posted:I don't know if I am using the -v flag correctly here. code:
|
# ? Aug 31, 2012 19:40 |
|
When you ask if I can run the REPL with no arguments do you mean just running python? That works fine but I'm not sure what you are exactly asking. I tried the proper -v flag code:
|
# ? Aug 31, 2012 19:55 |
|
Well, I thought it might be a bigger problem with Python being unable to output anything, but if -v and REPL work, then that is out of the window. Do type exp15.py (and/or try running python full\path\to\exp15.py), because running an empty file is the only other explanation that I've got — it would error out on non-existing file, it would error out on opening non-existing file, with no errors your prints would output. If this is really the file with the exact code you've posted, I'm out of ideas.
|
# ? Aug 31, 2012 20:03 |
|
Thanks for looking into my problem, I might give up on it for now. I tried an alternative solution that teaches the same idea (opening and looking at data in other files) like so code:
|
# ? Aug 31, 2012 20:13 |
|
Do other sorts of errors get printed out? PowerShell could be doing something stupid with stderr (does Windows even have a stderr?)
|
# ? Aug 31, 2012 20:14 |
|
Suspicious Dish posted:(does Windows even have a stderr?) It does. I've tried running that script in a PowerShell and it works fine (errors are displayed correctly, too). I don't really use PS, maybe there's a magic switch to make it behave in a weird way?
|
# ? Aug 31, 2012 20:24 |
|
I have this code:code:
|
# ? Sep 1, 2012 21:13 |
|
huhu posted:I have this code: continue edit: continue would be useful in restarting a loop, but just leave the whole line out as there are no more lines after the if block. it will continue automatically. yippee cahier fucked around with this message at 21:26 on Sep 1, 2012 |
# ? Sep 1, 2012 21:24 |
|
sund posted:continue Any suggestions for making this more efficient though? It took somewhere between two and five minutes to solve. Also, is making coding more efficient in regards to math problems mostly done by knowing more advanced theories? Like for example, here is another method for solving this problem: http://ca.answers.yahoo.com/question/index?qid=20100123221401AA7q4lR code:
|
# ? Sep 1, 2012 22:00 |
|
huhu posted:Any suggestions for making this more efficient though? It took somewhere between two and five minutes to solve. Also, is making coding more efficient in regards to math problems mostly done by knowing more advanced theories? Like for example, here is another method for solving this problem: Often the best way to make a program more efficient is to find a strategy for solving the same problem that requires fewer steps, rather than trying to make each step faster. This applies pretty much everywhere, not just to coding in regards to math.
|
# ? Sep 1, 2012 22:21 |
|
huhu posted:I think I didn't explain myself properly. I want to break out of the if statement and start the while loop over again. I ended up redoing my coding and got it to work a different way though by moving 'test' around. Your code probably can't be significantly improved, but as Nippashish stated, your algorithm is very sub-optimal. You're counting up from 1 and checking whether that number is divisible by each of 1 through 20. A better way to do this (the best that I can think of) is to factorize each of the integers from 1 to 20, take the union of those factor counts, and then multiply to find your number. For example:
I've hacked up a version of the Python collections.Counter class that overrides update to use the max instead of the sum of values. Assuming that I didn't screw anything up, it took 53 milliseconds to find the least common multiple of all integers from 1 to 100 inclusive (69720375229712477164533808935312303556800). EDIT: oh hey that's what the Yahoo Answers link says. Oops. Lysidas fucked around with this message at 06:24 on Sep 2, 2012 |
# ? Sep 1, 2012 23:22 |
|
Thanks again for all of your input. Can anyone point me to a basic introduction to matrices and interacting with rows, columns, changing elements, adding, rows/columns, etc? I must be searching the wrong thing because I've been looking for twenty minutes and can't find anything useful.
|
# ? Sep 2, 2012 22:23 |
|
huhu posted:Thanks again for all of your input. Do you want to know about doing these things in code or are you asking where you can learn linear algebra? Assuming the former (since you're in the python thread) you should look at numpy.
|
# ? Sep 3, 2012 03:04 |
|
Python experts! I'm trying to get started with Python via Udacity. It has been a humbling experience. I'm already stumped on one of Udacity's lessons on extracting links. The whole course has gone from "Okay, I'm following. This is do-able!" to . I feel as if there's some additional material I should be reviewing before I move on in this course (CS101). The OP has an amazing wealth of information, but is there any particular starting point where a novice coder like myself should start? \/So I guess I'm not imagining things. Not even kidding you, the course went from easy to "What the heck are they asking for?" I'm not even brand new to coding languages. I was just hoping to get better acquainted with Python. Thanks a lot for those two resources, Suspicious Dish! Any other great starting points you guys would recommend? melon cat fucked around with this message at 05:15 on Sep 4, 2012 |
# ? Sep 4, 2012 04:56 |
|
um, this course looks not very well put together. The chapter on Backus Naur Form comes before the chapter on Python Expressions, which comes before the chapter on Variables. If you have any specific question, feel free to ask, but my very biased and rushed opinion is that it doesn't look like a proper introduction for new programmers. They apparently want you to have a search engine by the end of this. The two go-to things that I like to refer people to for new programmers are Think Python and Learn Python the Hard Way. Both are free resources. Suspicious Dish fucked around with this message at 05:05 on Sep 4, 2012 |
# ? Sep 4, 2012 05:03 |
|
JetsGuy posted:I learned something today about python coding that is probably very durr to the rest of you but actually will save my rear end in the future. What is this data[i,0] syntax?
|
# ? Sep 4, 2012 19:58 |
|
melon cat posted:Any other great starting points you guys would recommend? I'm using Learn Python the Hard Way as my main resource, with supplements from the interactive tutorials on the other two sites below. http://learnpythonthehardway.org/book http://www.diveintopython.net/getting_to_know_python/index.html http://www.learnpython.org/ Big List - http://programming-motherfucker.com/become.html#Python
|
# ? Sep 4, 2012 20:44 |
|
Dren posted:What is this data[i,0] syntax? There's no special syntax. Python code:
|
# ? Sep 4, 2012 20:51 |
|
Random Python question: I was playing around with the Python shell and noticed that you could extend Python's built-in types: Python code:
Python code:
|
# ? Sep 4, 2012 21:34 |
|
Python code:
|
# ? Sep 4, 2012 21:58 |
|
Optimus Prime Ribs posted:I don't actually plan on doing this in real code, but I'm just wondering why constructing an int works, but using a literal value does not. Does the literal value use a primitive type that is not affected by that goofy class or something? Pretty much Python code:
|
# ? Sep 4, 2012 21:59 |
|
There are really two issues:
EDIT: Beaten with #1; it took me too long to research #2 Lysidas fucked around with this message at 22:03 on Sep 4, 2012 |
# ? Sep 4, 2012 22:01 |
|
Lysidas posted:A bit of whitespace makes a huge difference: It's better to do (42).stuff(), if you really have a need to call something on an integer literal. I don't recall ever seeing it in the wild, though.
|
# ? Sep 4, 2012 22:09 |
|
Dr.Spaceman posted:I'm using Learn Python the Hard Way as my main resource, with supplements from the interactive tutorials on the other two sites below. Do not read Dive into Python. It was built for a version of Python that's over 10 years old, and the creator has not updated it.
|
# ? Sep 4, 2012 23:17 |
|
Optimus Prime Ribs posted:Random Python question: Python code:
|
# ? Sep 4, 2012 23:20 |
|
|
# ? May 8, 2024 23:34 |
|
The part about int and __main__.int makes sense; I had a feeling it was something like that.Lysidas posted:
Never knew that about Python. Thanks for the tip. Suspicious Dish posted:
I don't really understand this, but that's a neat trick.
|
# ? Sep 4, 2012 23:49 |