|
Suspicious Dish posted:Inheriting from object in Python 2 gives you a new-style class. It's a fancy system, but if you're using Python 2, always inherit from object. Ok, so I'm confused, when I declare a class, I'll do something like: Python code:
|
# ? Mar 26, 2013 19:48 |
|
|
# ? May 8, 2024 06:44 |
|
Nothing. You're just getting an old-style class (which is generally fine). If you do class hurf(Object): then you get a new-style class which has some advantages (basically the new object model makes a lot more sense and is more powerful). And if you're using Python 3 you're always getting the new-style classes. There's a lot of resources out there comparing the two so I won't go into it all.
|
# ? Mar 26, 2013 20:00 |
|
Suspicious Dish posted:So, you can't make objects threaded. Threads are like additional programs that run in the same memory space (in Linux, that's literally the only difference from forked processes — they get their own PID and everything) This is all easy to understand, from the way you've described it. Thanks. My project consists of a step in which specific files are read in and then a second step in which calculatios are performed using data from those files, but the files and the data in the files is not being changed (IE results are stored in new variables and saved elsewhere on the disk, MySQL insertions are performed, etc). So I could set up a Queue for the read-in (which shouldn't benefit much due to a file I/O bottleneck) and another queue for the operations, and before queuing operations I would just need to wait for the read-in queue to empty, right? Alternatively, if the file I/O queue really doesn't improve speed at all, then I could just read everything in normally and then setup a queue for processing the data, so long as I'm careful about not changing the data that is being operated on. I'm very comfortable with coding in Python and using Python, I've just never done Computer Science, which I see as "understanding what the code is making the computer actually do." I have a lot of experience in C++ as well, so I know all about memory management and reference passing and how memory management in Python works differently (IE basically Python uses something analogous to smart pointers, a chunk of memory can only cleaned up after the number of active references to that chunk of memory becomes 0, all done automatically), I'm just really clueless in threading and multiprocessing, and I'm not well-versed in the "guts" of Python, just the high-level stuff. AKA there is a gap between what many of the hard science programs teach (a bare-bones introduction to scientific computing) and what is actually needed in the real world of hard science (actual computer science knowledge for real and efficient scientific computing), and individually overcoming this gap is what I've been trying to do for the last few years. This July I'll become eligible for free remote-learning university courses through my employer, I already have plans to get some intro-level CS courses under my belt in the hope of building a better understanding of what is going on under the hood. Thern posted:You can release the GIL? I need to investigate this further as I have something that is very I/O bound. And Multiprocessing is a bit of hack I feel. According to this page the GIL is always unlocked when doing I/O. I don't know whether this helps you. QuarkJets fucked around with this message at 20:58 on Mar 26, 2013 |
# ? Mar 26, 2013 20:55 |
|
The relationship between "Computer Science" and "Programming" is really vague and everybody has some different understanding of what they mean. I just meant more low-level programming more than anything, but it seems you have a decent handle on that. So, I'm going to step it up to a higher level. What you want isn't threading, but parallelism. Threading is a technology that can be used to implement parallelism. I don't know enough details about your problem (your problem statement is an extremely generic, common problem), but it sounds like there's no reason that you couldn't run ten programs on four or so different computers that crunch the numbers, and then you put them into a database at the end. There's of course no reason that you couldn't just have one program and a bunch of threads, but that design makes it harder to distribute to different computers in the end. Or one computer and multiple processes, etc. There's of course trade-offs to all of these designs, and understanding all of these trade-offs is something that you'll come to learn as an engineer, but it's important to think of threading as a technology that can solve lots of problems, parallelization being one of them, not a solution. There is so much to explore in this topic, but to pick one example, try Googling for "MapReduce". We've expanded this topic enough so that this might be a better question for the Ask General Questions, or perhaps it's time for a new General Concurrency/Parallelism thread.
|
# ? Mar 26, 2013 21:26 |
|
Emacs Headroom posted:I would think about splitting off all the classes into their own files, maybe inside of a package. For instance you could have I tried playing around with this, and I guess I'm not understanding something because I'm still running into import problems. For instance, virtually all my modules need import itertools. I thought I could import that once in my main.py module, but if I do that all my other modules throw NameError at me. It's not an effective solution to do import itertools in every module, because then why split them apart in the first place. Everything is sitting in /Lib/site-packages/LanguageFuckery so the problem is not in locating the modules.
|
# ? Mar 26, 2013 22:06 |
|
You need to do it in every module you write, because each module has its own namespace and can't mess with the namespace of whatever imported it. This is actually a good thing, otherwise one module could cause bugs in another module. Also, you shouldn't be putting your own code in /Lib/site-packages, or need to.
|
# ? Mar 26, 2013 22:16 |
|
evensevenone posted:You need to do it in every module you write, because each module has its own namespace and can't mess with the namespace of whatever imported it. This is actually a good thing, otherwise one module could cause bugs in another module. I have a pretty naive understanding of how importing works, so let me ask this: if module A imports module B and C, and module B needs things from module C, will I always have to write import C in module B? In other words, can B access stuff that A imported by virtue of it having been imported into A, or does it have to import that stuff itself? (I hope this makes sense.) edit: my original question for context. quote:Also, you shouldn't be putting your own code in /Lib/site-packages, or need to. I did some googling, and came across some forum/blog posts that suggested this. Is it a bad idea stylistically, or is there something more serious that could go wrong? (I'd actually be glad to know this is a bad idea since I found it really inconvenient) FoiledAgain fucked around with this message at 22:47 on Mar 26, 2013 |
# ? Mar 26, 2013 22:41 |
|
FoiledAgain posted:I did some googling, and came across some forum/blog posts that suggested this. Is it a bad idea stylistically, or is there something more serious that could go wrong? (I'd actually be glad to know this is a bad idea since I found it really inconvenient) Add the directory with all your work to your PYTHONPATH (or to sys.path) when you're working on it. When it's mature and ready to distribute / put on github, you can use setuptools or whatever to make a package that will go into site-packages.
|
# ? Mar 27, 2013 00:07 |
|
FoiledAgain posted:I have a pretty naive understanding of how importing works, so let me ask this: If module b wants to use C directly, you need an import C in B. Now, you could import B and C in A, and make an object in A that is of a class from C, and pass that object to B, and B could use it. But you couldn't make an object in B that is of a class in C, without importing B. If you think about the full names of the objects as they are created/instantiated/passed it makes sense.
|
# ? Mar 27, 2013 00:19 |
|
evensevenone posted:Now, you could import B and C in A, and make an object in A that is of a class from C, and pass that object to B, and B could use it. But you couldn't make an object in B that is of a class in C, without importing B. This is a really helpful explanation. Thank you!
|
# ? Mar 27, 2013 00:24 |
|
evensevenone posted:Now, you could import B and C in A, and make an object in A that is of a class from C, and pass that object to B, and B could use it. But you couldn't make an object in B that is of a class in C, without importing B. Well, sure you can. If A passes an object of C to B, then you can use type(obj_C)() in B. Importing is about names, not objects or anything else like that. If you want to access the value at some name in another module, you have to import it.
|
# ? Mar 27, 2013 00:47 |
|
That's true. Although, you'd have to be an rear end in a top hat.
|
# ? Mar 27, 2013 01:17 |
|
spankweasel posted:Assigning a card from the deck to a player is akin to moving one element from a list to another. evilentity posted:I was bored so I did this for you. Try to use this as guidelines for your own work and dont just copy verbatim. At least try to understand why and how it does things. Ive commented things that I thought might be hard. There is no error checking so you could add that. Popper posted:This is basically how people are thought classes. Sorry that this was last page, but I just wanted to say thanks for your help and advice. I haven't looked at anything you guys posted because I've been so busy studying for a math test over the weekend, Spanish, and catching up on this computer sciency stuff. I already turned in what I have, but thanks for helping me out so I can get some inspiration and figure out how all this code stuff works. Thanks!
|
# ? Mar 27, 2013 14:05 |
|
JetsGuy posted:What is wrong with doing that? If you're going to use inheritance in 2.x, you will at some point need to inherit from object. Classes work fine without it but you can't use super(). At the end of the day I do it out of habit, but it's a good habit.
|
# ? Mar 27, 2013 15:04 |
|
Anyone know a python 2.7 lib that can give me a file like object handle to the binary data in an mp3 file? I want to md5sum it.
|
# ? Mar 27, 2013 15:08 |
|
Use the mmap module.Python code:
code:
|
# ? Mar 27, 2013 15:17 |
|
Dren posted:Anyone know a python 2.7 lib that can give me a file like object handle to the binary data in an mp3 file? Python code:
e; I guess this doesn't give you a file-like object though, but it can probably be shoehorned in to something like it. The Gripper fucked around with this message at 15:21 on Mar 27, 2013 |
# ? Mar 27, 2013 15:18 |
|
PS - If anyone wants to do some interesting reading about the GIL here's a python 3 ticket where some valiant efforts were made to try to replace the GIL with a scheduler. http://bugs.python.org/issue7946 One of the topics that comes up is that the current GIL really sucks at task switching when you use the threading model of I/O bound thread + computationally bound thread. That is, under the current GIL either the I/O bound thread or the computationally bound thread tends to hog execution instead of evenly splitting time. Solving this task switching problem would go a long way toward removing the stigma surrounding python threading. Unfortunately, the scheduler implementations in that ticket were rejected for 3.2.
|
# ? Mar 27, 2013 15:26 |
|
Lysidas, thank you for your suggestion. Perhaps I wasn't clear but I actually wanted the solution The Gripper suggested. I want to compare file data (using a broken hash function) without taking the metadata into account.The Gripper posted:You could try PyMedia, I think all you'd need to do is: Thanks, I should be able to shoehorn that into something that the md5sum module will accept.
|
# ? Mar 27, 2013 15:34 |
|
If you have two different MP3 files that were gotten from two different sources, it's extremely unlikely that they'll compare equal, even after removing metadata. There are too many MP3 encoders out there, with too many encoder options and things to tweak. Even if you have two different uncompressed audio tracks ripped from the same album, minor variations in CDs, CD disc drives and rippers can produce wildly different results. You probably want to build an audio fingerprint instead to compare similar audio tracks.
|
# ? Mar 27, 2013 16:24 |
|
Suspicious Dish posted:If you have two different MP3 files that were gotten from two different sources, it's extremely unlikely that they'll compare equal, even after removing metadata. There are too many MP3 encoders out there, with too many encoder options and things to tweak. Even if you have two different uncompressed audio tracks ripped from the same album, minor variations in CDs, CD disc drives and rippers can produce wildly different results. Yeah, I'm doing this for someone else. I told him the same thing. However, he's concerned about honest duplicate data rather than stuff that fingerprints the same. pymedia is loving me. Doesn't build with gcc 4.x and doesn't find any of the libraries it wants on ubuntu.
|
# ? Mar 27, 2013 16:30 |
|
Suspicious Dish posted:You probably want to build an audio fingerprint instead to compare similar audio tracks. Plus the audio fingerprinting will be more fun to write. You'll have to decide on a good feature space based on the spectrogram or something, which will be interesting (you could also use PCA to find features or employ some machine learning if you have enough examples).
|
# ? Mar 27, 2013 16:31 |
|
In case anyone is interested, here is a "good enough" solution using audioread (https://github.com/sampsyo/audioread). You can get audioread with pip.Python code:
It seems most music file libraries are coded to do some specific task but there are no good general purpose ones. E.g. mutagen has support for python native metadata reading with a ton of container types but doesn't give you a handle to the music datastream. pyglet can play lots of different audio file types but that's all it can do, play them. There is no hook between the decode step and the play step. pymedia is a piece of a larger project called pycar "a media center for a car based PC( personal companion )". As such, it hasn't been updated since 2006, only builds on GCC 3.x, and fails to detect the existence of installed dev libraries it depends on. If mutagen would mmap and return back the music datastream it'd be nearly a one stop shop. For the returned music to be generally useful there would need to be a hook to decode the datastream to a common format like PCM, the way audioread does. I saw some fingerprinting libraries while I was poking around but since I'm not particularly interested in them I'm not gonna do a write up about them.
|
# ? Mar 27, 2013 18:19 |
|
Here's a program to generate a pickled version a dictionary that maps file paths to hashes of audio data. http://pastebin.com/7kbARcgN It uses coroutines. Also, you can interrupt it with ctrl+c then resume from where you left off the next time you execute. edit: If anyone wants to get nuts hook up multiprocessing or multithreading to the coroutine stuff so that multiple files are processed at a time. edit 2: I wrote a mt version. GIL is limiting me to 130%-225% cpu while processing with 4 producer threads and 1 consumer but that's better than 100%. edit 3: fixed some bugs in the mt version and I'm up to 400%-450% cpu w/ 4 producers and 1 consumer Dren fucked around with this message at 22:02 on Mar 27, 2013 |
# ? Mar 27, 2013 19:41 |
|
Man, I tried to install pyqt today, and easy_install bitched and moaned that there wasn't a setup script. This isn't a great start for my exploration... :/ EDIT: And the source straight from riverbank is all screwy. You need to install sip first, and the README there says to use a file that doesn't exist. EDIT2: Figured out part of it, still getting fun errors out the rear end... For the life of me, I can't figure out what the poo poo this is bitching about. The only thing google says is to have XTools with command line support, which I do... quote:clang++ -c -pipe -mmacosx-version-min=10.6 -fno-strict-aliasing -O2 -fPIC -Wall -W -DQT_DISABLE_DEPRECATED_BEFORE=0x040900 -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/Users/JetsGuy/Qt5.0.1/5.0.1/clang_64/mkspecs/macx-clang -I. -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I../../QtCore -I. -I/Users/JetsGuy/Qt5.0.1/5.0.1/clang_64/include -I/Users/JetsGuy/Qt5.0.1/5.0.1/clang_64/include/QtGui -I/Users/JetsGuy/Qt5.0.1/5.0.1/clang_64/lib/QtGui.framework/Versions/5/Headers -I/Users/JetsGuy/Qt5.0.1/5.0.1/clang_64/include/QtCore -I/Users/JetsGuy/Qt5.0.1/5.0.1/clang_64/lib/QtCore.framework/Versions/5/Headers -I. -I/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/System/Library/Frameworks/AGL.framework/Headers -o qpycore_qabstracteventdispatcher.o qpycore_qabstracteventdispatcher.cpp JetsGuy fucked around with this message at 05:07 on Mar 28, 2013 |
# ? Mar 27, 2013 22:30 |
|
The error you posted seems to indicate that you don't have the clang++ compiler installed, have a screwy makefile, or some combination of the two. Try installing the clang++ compiler.
|
# ? Mar 27, 2013 23:39 |
|
PySide has pre-compiled binaries for OS X.
|
# ? Mar 27, 2013 23:57 |
|
Learning Qt with no programming experience is proving to be a PITA. The Zetcode tutorial has some great example code that I've been able to use and manipulate, but is fairly limited in what it demonstrates. There are several other tutorials online, all of which seem to use very different syntax - and I can't get any of them to work. I setup Qt Designer, and have been creating windows, then analyzing the code using puic, but can't get the gui to display, even when trying wrapper styles from several tutorials. Can anyone explain how to get QT Designer 5 code ->puic to run in Python 3? I'm not getting any errors, just an output of >>>. What style do you recommend as the best way to learn Qt? I'm planning to stick with the Zetcode style, but am having some issues doing things not directly described in it: For example, how can I have two separate layout widgets in a main window? This seems dramatically more difficult than learning basic Python - it might be due to lack of strong tutorials, like Codeacademy's. Dominoes fucked around with this message at 04:29 on Mar 29, 2013 |
# ? Mar 29, 2013 04:07 |
|
I can't help you with Designer, but you can use QFrames to nest layouts.
|
# ? Mar 29, 2013 05:50 |
|
wwb posted:I'm starting to get into python a bit. I've done a few tutorials, learned the hard way, etc. But I'm running into an issue my google-fu fails at -- how does one properly structure a non-trivial python project. Bumping this a bit as it got subsumed by a much more exciting conversation about multithreaded python. Anyhow, any advices guys?
|
# ? Mar 29, 2013 12:43 |
|
Why does packaging in Python have to be so weird I am following instructions but stuff doesn't work. My package structure is: code:
Python code:
edit: I used PyDev for Eclipse to convert the main package folder to a source folder because it wasn't for some reason and it works?? Doesn't make sense to me though. edit again: It only works in Eclipse Smarmy Coworker fucked around with this message at 17:18 on Mar 29, 2013 |
# ? Mar 29, 2013 14:46 |
|
I'm giving this a shot because this confuses me in Python sometimes to.code:
|
# ? Mar 29, 2013 17:51 |
|
If the package/ folder isn't in your PYTHONPATH then the interpreter doesn't know how to find those, I don't think it will automatically just search parent directories to find other packages, there is nothing that indicates to python that what you have is a subpackage necessarily. You can only import things in the same package or from things in PYTHONPATH. Presumably converting it to a source folder causes Eclipse to add the folder to your PYTHONPATH for whatever method it uses to run the script.
OnceIWasAnOstrich fucked around with this message at 17:59 on Mar 29, 2013 |
# ? Mar 29, 2013 17:55 |
|
Hi, I'm trying the exercises in chapter 9 of How to Think Like a Computer Scientist. I'm going through this whole book that was recommended for the intro CompSci class that I'm currently taking. Book: http://openbookproject.net/thinkcs/python/english2e/ch09.html I have a few questions about some of the exercises. Exercise #1 Write a loop that traverses: code:
This is what I wrote: code:
EXERCISE #2 Open a file named ch09e02.py and with the following content: code:
Here's one of the doctests it wants me to pass. code:
code:
*** DocTestRunner.merge: '__main__.test' in both testers; summing outcomes. *** DocTestRunner.merge: '__main__' in both testers; summing outcomes. Now I'll go through the examples in the chapter listed above and get it to say that it's passed(at least I remember doing so about an hour ago), so why am I getting this now? Question About a Piece of Code I wrote for an Exercise code:
list3 = [i for i in u] I don't know how to translate i for i in u in an English statement. I know what it does, but I don't know how to say it if I were having a conversation with someone. I get to the assignment to list3 is the list with as many indices... and then I can't really think of how to say it. Thanks for your help!
|
# ? Mar 29, 2013 19:17 |
|
[i for i in u] will create a list that is just all of the elements in u. If u is list1, then list3 will be filled with the same values as list1 with that line. You don't appear to be using enumerate, so instead of keeping track of an index and incrementing it you could just:code:
code:
This isn't great though because if b is shorter than u then the code will bomb out
|
# ? Mar 29, 2013 21:08 |
|
OnceIWasAnOstrich posted:If the package/ folder isn't in your PYTHONPATH then the interpreter doesn't know how to find those, I don't think it will automatically just search parent directories to find other packages, there is nothing that indicates to python that what you have is a subpackage necessarily. You can only import things in the same package or from things in PYTHONPATH. Presumably converting it to a source folder causes Eclipse to add the folder to your PYTHONPATH for whatever method it uses to run the script. why is Python so weird?? What if I did something with site or added the main package location to sys.path in every module? I don't really want to dick around with PYTHONPATH right now because I'm only doing test stuff.
|
# ? Mar 29, 2013 22:02 |
|
It's actually pretty standard -- it's known as "the search path problem". Eclipse just sets up the search path for you. Don't mess around with sys.path or the site module. Those are bad ideas and will break your environment.
|
# ? Mar 29, 2013 22:58 |
|
ARACHNOTRON posted:why is Python so weird?? What is so weird about it? It will search folder the file is in, subfolders, path and libs. It doesnt magically know wheres your stuff located. Try structuring your project in different way. Path is hardly magic. If you really must you can use this: Python code:
Mad Pino Rage posted:Write a loop that traverses: Python code:
Mad Pino Rage posted:Open a file named ch09e02.py and with the following content: Mad Pino Rage posted:Well, I wrote this to pass a doctest in one of the exercises, but I don't understand a piece of code. I actually copied it from another project I wrote earlier in this semester.
|
# ? Mar 29, 2013 23:28 |
|
ARACHNOTRON posted:why is Python so weird?? C and other languages work in a similar way; would you prefer it if Python/C/etc recursively searched your entire file system every time that you tried to import something? Setting your PYTHONPATH only takes a second. Just point it to the directory where you're currently testing things. It would also be way faster than any of the workarounds that you're considering
|
# ? Mar 29, 2013 23:29 |
|
|
# ? May 8, 2024 06:44 |
|
It would be nice if it searched back until it didn't find an init file. I don't know. I have only done Java packaging and it kinda just works, especially with JARs
|
# ? Mar 30, 2013 03:37 |