|
drat. I mean, there are supposed to be either one or two operands, nothing else. But, yeah, if someone passes in a three-operand problem, the third operand is going to be ignored. Of course, that's not going to happen in the normal course of the website's functioning... Hm. Or maybe it can (I mean, if someone just edited the url). Anyway, should I put that in the no-return check? (Probably not, since this method hasn't received much enthusiasm in the thread.) Oh, what it does in general? Well, it's a mental math trainer for my personal use. I don't aspire to anything more than 2 operands. And at this stage I'm not sure I'm ever going to get where I could share this program online. Also, I tried to come up with something more general than what I have right now, but I wasn't sure Django would digest it. (Please don't ask me why I had to involve Django.) There are two supertypes - OneOperandProblem and TwoOperandProblem, and those are extended into all the operations I need - AdditionProblem, SubtractionProblem, MultiplicationProblem, DivisionProblem, SquareProblem, and RootProblem. Well, my shameful secret is out. What's yours?
|
# ? Jan 10, 2015 16:05 |
|
|
# ? Jun 11, 2024 18:54 |
|
superstepa posted:I just want to use beautfulsoup to scrape data from a table once every 24 hours. I want it to work on windows and I'd like to have all the code in python within the app but at the moment cron does seem like the best option If it doesn't, just have a separate cron job run a separate script. As another slightly worse alternative (eg if your script never crashes and needs relaunching), you can launch a separate thread at startup and have it loop and sleep for the amount of time needed. ShadowHawk fucked around with this message at 17:38 on Jan 10, 2015 |
# ? Jan 10, 2015 17:32 |
|
I have a program written in Java that I inherited from someone else that I am trying to add functionality to. I have written a piece of code in Python and would ideally like the following: During the course of the Java program executing, it provides two integers as input to the Python code. The Python code uses these two integers as input and returns a float to the Java program. The Java code providing input to the Python code and the Python code handing back the float result to the Java code will happen multiple times (on the order of tens of thousands). Is it possible to do this (call a piece of Python code with Java code)? If so, what should I look at to begin understanding how to accomplish this?
|
# ? Jan 10, 2015 18:45 |
|
Jose Cuervo posted:I have a program written in Java that I inherited from someone else that I am trying to add functionality to. I have written a piece of code in Python and would ideally like the following: While I haven't used it personally, are you aware of http://en.wikipedia.org/wiki/Jython ? That should be able to call between java and python with zero overhead (other than that inherent in any call in a virtual machine)
|
# ? Jan 10, 2015 19:04 |
|
I think I'm having an issue with a __getitem__ method that I made. I have this class:Python code:
But when I try to access that objects list with "mymap.objects" where mymap is a GameMap instance, I get "AttributeError: 'list' object has no attribute 'objects'". Is this caused by the __getitem__ method which always tries to return a level[index] even when I want an objects[index]? Should I somehow overload the __getitems__ method to make it possible to index this object for either of those parameters? Edit: I think I figured out my problem, I hadn't assigned the instance "mymap" to be a GameMap object yet at the point where I was trying to index it. reading fucked around with this message at 23:11 on Jan 10, 2015 |
# ? Jan 10, 2015 22:03 |
|
Errr, if you get that error from that code then I can tell you with 100% certainty that mymap is not a GameMap but is in fact a list. __getitem__ will never be invoked unless you use the square brackets. I don't know of any exceptions. E: also don't put mutable objects (e.g. an empty list) as default arguments, that will cause problems. Instead do objects=None and put an if objects is None: clause in your __init__ to assign the real default. SurgicalOntologist fucked around with this message at 22:16 on Jan 10, 2015 |
# ? Jan 10, 2015 22:14 |
|
Doesn't a default argument just get overriden with any argument passed for that parameter? What kind of problems does that cause if there's a mutable default parameter?
|
# ? Jan 10, 2015 23:00 |
|
reading posted:Doesn't a default argument just get overriden with any argument passed for that parameter? What kind of problems does that cause if there's a mutable default parameter? Yes it does, the problem comes when it doesn't get passed. If you make two GameMaps without passing the optional objects argument, their objects attribute will both reference the same list. If you append to one of them it will change both, effectively. That's not what caused the error you posted, though.
|
# ? Jan 10, 2015 23:05 |
|
SurgicalOntologist posted:Yes it does, the problem comes when it doesn't get passed. If you make two GameMaps without passing the optional objects argument, their objects attribute will both reference the same list. If you append to one of them it will change both, effectively. Ah I see, so when the GameMap object is instanced, that empty list in the default parameter is only shallow-copied, not deep-copied? That seems like poor behavior on Python's part, is there a reason for that?
|
# ? Jan 10, 2015 23:13 |
|
It's not copied at all. I believe it would actually cause a lot more strange behavior if it were copied (or at least slowdowns), but I can't come up with a good example off the top of my head. Maybe someone else can.
|
# ? Jan 10, 2015 23:22 |
|
reading posted:I think I'm having an issue with a __getitem__ method that I made. I have this class: It's good practice to not mutate variables from one type to another. If mymap is a list, then it'll be better if you declare some other variable to be the GameMap object
|
# ? Jan 11, 2015 00:01 |
|
Does anyone have any recommendations for JSON-RPC libraries? Server & Client, preferably with good quality documentation?
|
# ? Jan 11, 2015 02:32 |
|
Jose Cuervo posted:I have a program written in Java that I inherited from someone else that I am trying to add functionality to. I have written a piece of code in Python and would ideally like the following: Look into JNI/JNA on the java side and ctypes, cffi and cython for python for generic interop if you hate yourself or you find that poo poo interesting. There are a few specific options for working with Java code from within Python, though the reverse is something I've never had to do. https://wiki.python.org/moin/ScriptingJava
|
# ? Jan 11, 2015 19:42 |
|
feedmegin posted:While I haven't used it personally, are you aware of http://en.wikipedia.org/wiki/Jython ? That should be able to call between java and python with zero overhead (other than that inherent in any call in a virtual machine) salisbury shake posted:Look into JNI/JNA on the java side and ctypes, cffi and cython for python for generic interop if you hate yourself or you find that poo poo interesting. This is my code: Python code:
Python code:
Jose Cuervo fucked around with this message at 00:43 on Jan 12, 2015 |
# ? Jan 12, 2015 00:04 |
|
Jose Cuervo posted:However, even though I have 4 CPUs on my computer the parallelized code actually takes 3 times as LONG as the regular code does. Can anyone see what I am doing incorrectly? Or is this telling me that my for loop was not a good candidate for parallelization? If so, why not ( as far as I can tell it meets the requirements for parallelization)?
|
# ? Jan 12, 2015 00:35 |
|
Joblib defaults to using the multiprocessing library to bypass the Global Interpreter Lock. The first thing to check is to make sure that you are actually seeing 4 Python processes each at full CPU usage. If you don't, then it has something to do with how joblib should be used. If you do see four processes using full CPU time, then the problem is probably some sort of inefficiency with spinning up processes for each iteration. You might need to either use multiprocessing directly and set up worker processes that persist for more than one job or find that functionality in joblib.
|
# ? Jan 12, 2015 01:28 |
|
OnceIWasAnOstrich posted:Joblib defaults to using the multiprocessing library to bypass the Global Interpreter Lock. The first thing to check is to make sure that you are actually seeing 4 Python processes each at full CPU usage. If you don't, then it has something to do with how joblib should be used. If you do see four processes using full CPU time, then the problem is probably some sort of inefficiency with spinning up processes for each iteration. You might need to either use multiprocessing directly and set up worker processes that persist for more than one job or find that functionality in joblib. Actually I think I found the explanation here, where my issue is that each iteration of the for loop is relatively inexpensive, but there are a lot of iterations to do. It seems that the parallelization works best if the individual iterations are expensive compared to the overhead required to set up a new process each time. So I was wrong, and the situation I had was not ideal for parallelization in that way.
|
# ? Jan 12, 2015 02:16 |
|
I've never looked into using joblib for anything but you may consider parallelism alternatives such as MPI (e.g., mpi4py). It's used for similar things and I've used it for things that are trivially parallel (individual iterations of a loop being cheap and independent).
|
# ? Jan 12, 2015 02:42 |
|
Jose Cuervo posted:Actually I think I found the explanation here, where my issue is that each iteration of the for loop is relatively inexpensive, but there are a lot of iterations to do. It seems that the parallelization works best if the individual iterations are expensive compared to the overhead required to set up a new process each time. This is what I was trying to get at. It seems unlikely however that joblib is actually spinning up a new process for every iteration, if it is it sounds like something that should really be fixed. If you use multiprocessing.Pool directly it creates the processes at the time of pool creation, and then feeds them one at a time. I can get no slowdown and a slight increase in speed from running functions that return in as fast ~100ns each that way, so you may want to try more primitive multiprocessing tools.
|
# ? Jan 12, 2015 03:31 |
|
OnceIWasAnOstrich posted:This is what I was trying to get at. It seems unlikely however that joblib is actually spinning up a new process for every iteration, if it is it sounds like something that should really be fixed. If you use multiprocessing.Pool directly it creates the processes at the time of pool creation, and then feeds them one at a time. I can get no slowdown and a slight increase in speed from running functions that return in as fast ~100ns each that way, so you may want to try more primitive multiprocessing tools. Python code:
I was able to get a speedup with joblib by breaking the list of (bldg_id, bldg_coord) tuples into 4 equal sized lists, and then using joblib to run four processes in parallel (i.e. run the four for loops in parallel). When I run the code this way four processes do get spawned and they use 100% CPU (25% each). It just seems like a hacky way to get things done, but I suppose it is better than nothing.
|
# ? Jan 12, 2015 04:46 |
|
I've been loving with threading and multiprocess more or less for the past month. I've seen the same results too which is code that runs slower with more processes (don't use threading because of GIL). Have you considered using apply_async?
|
# ? Jan 12, 2015 05:19 |
|
Cultural Imperial posted:I've been loving with threading and multiprocess more or less for the past month. I've seen the same results too which is code that runs slower with more processes (don't use threading because of GIL). Have you considered using apply_async? Yeah I tried that as well - the code took about as long to run as with regular apply - just over 5.5 minutes (I had read the wrong output when I said 7 minutes for apply earlier). The CPU was definitely more used - 75-85% CPU being used for the 5.5 minutes with apply_async versus 35-40% CPU being used for the 5.5-ish minutes with apply. Right now my hacky version with joblib runs in 1 minute 27 seconds. There is some stuff about sharing memory/ access to objects with joblib, so I will look into that tomorrow.
|
# ? Jan 12, 2015 05:36 |
|
Jose Cuervo posted:So I looked into using just the multiprocessing library with a Pool: Try this: code:
|
# ? Jan 12, 2015 05:37 |
|
QuarkJets posted:Try this: I tried that but couldn't figure out how to pass in node_list as an additional argument. THoughts?
|
# ? Jan 12, 2015 05:51 |
|
Jose Cuervo posted:I tried that but couldn't figure out how to pass in node_list as an additional argument. THoughts? The best way to do this might be to write your data into the values of the dict code:
QuarkJets fucked around with this message at 10:10 on Jan 12, 2015 |
# ? Jan 12, 2015 10:06 |
|
If you stick all of the coordinates (for both the buildings and the graph) into numpy arrays then you can use this:code:
code:
|
# ? Jan 12, 2015 10:09 |
|
Jose Cuervo posted:Is this more along the lines of what you were saying? I am not sure I have used a Pool correctly here - multiple processes get spawned (when I look in Task Manager, but they each use <10% CPU), and the code does not terminate even after 7 minutes of running, where the unparallelized code is done after 3 minutes. You can also set chunksize to the length of the list divided by the pool_size. Someone night be able to say for sure, but I think multiprocessing is using such a small default chunksize to move data between threads that it's actually spending a ton of (wall-clock) time waiting for the OS to move tiny chunks of data between threads and the solution I found was to basically force it to move the whole dang list (or as close to that as the OS will allow) to the process at once. I wouldn't call it "hacky", but I'm biased
|
# ? Jan 12, 2015 20:00 |
|
I'm using fabric to run a single command on multiple servers. It's a big command with a for loop and multiple subshells, so I'm left with a big fat string that's being passed to the fabric run function. Does anyone know of any libraries out there that can be used to construct shell commands a little nicer? Something maybe like:code:
code:
|
# ? Jan 13, 2015 22:45 |
|
Jose Cuervo posted:Yeah I tried that as well - the code took about as long to run as with regular apply - just over 5.5 minutes (I had read the wrong output when I said 7 minutes for apply earlier). The CPU was definitely more used - 75-85% CPU being used for the 5.5 minutes with apply_async versus 35-40% CPU being used for the 5.5-ish minutes with apply.
|
# ? Jan 14, 2015 02:49 |
|
sharktamer posted:I'm using fabric to run a single command on multiple servers. It's a big command with a for loop and multiple subshells, so I'm left with a big fat string that's being passed to the fabric run function. Does anyone know of any libraries out there that can be used to construct shell commands a little nicer? If you're running the commands to do some kind of configuration management it might be wortn looking at ansible. Otherwise, unless fabric mangles the strings you give it, you could write out your script as a long string and interpolate any parameters in. code:
I find the raw, long string form useful, as you can inline the whole shell script and format it as readably as you want.
|
# ? Jan 14, 2015 10:47 |
|
I have a simple question about CSV files/list splitting with Python. I have a long list of names occupying a single column and I want to split these lists in such a way to maximize the number of names on a single page. That is, I want to create new columns from the list to fill a page and only go to the next page if necessary -- the idea is to maximize ink on the paper to reduce pages used. Are there any convenient packages for this or should I just hand code it? I am using tablib at the moment.
|
# ? Jan 14, 2015 18:36 |
|
Kudaros posted:I have a simple question about CSV files/list splitting with Python. I have a long list of names occupying a single column and I want to split these lists in such a way to maximize the number of names on a single page. That is, I want to create new columns from the list to fill a page and only go to the next page if necessary -- the idea is to maximize ink on the paper to reduce pages used. Are there any convenient packages for this or should I just hand code it? I am using tablib at the moment. If not you're just iteratively slicing the list by however many lines fit on a page: Python code:
|
# ? Jan 14, 2015 18:53 |
|
ShadowHawk posted:Is it ok to munge the order? This works just fine for my purposes, thanks for the response. I now have a separate problem EDIT: Nevermind about tablibs problem -- very basic looping out of range mistake... I think I'm representing the scientist-as-bad-programmer stereotype... Kudaros fucked around with this message at 20:44 on Jan 14, 2015 |
# ? Jan 14, 2015 20:04 |
Noob alert: I'm almost totally new to programming. I recently finished the Python course on Codecademy, and I find I really enjoy working with the language. I feel like I could use some more guided study before I try things on my own though. Would Invent with Python be a natural next step in teaching myself how to program? If it matters, my end goals would be game development and, eventually, audio-related applications.
|
|
# ? Jan 14, 2015 21:39 |
|
Start a project. Codeacademy's Python course is all you need for now.
|
# ? Jan 14, 2015 21:49 |
|
Following my previous post (it's on the previous page) and the recommendations in this thread, I came to the conclusion that learning Qt for this particular project would be a significant detour so I sticked to Tkinter and programmed the GUI using it. As suggested, I completely separated the program logic from the GUI component, and all interaction between them is done through a single "event_handler" function. The program now runs as expected, except for the fact that the GUI completely freezes while running searches or downloading files. I have looked up information on this and I understand why it is occurring. From what I gather, I have three options to deal with this: use a callback loop (which from my understanding is not the best option to handle I/O blocking in the case of Tkinter - at least I have no idea how to implement it in a simple manner), multiprocessing, or threading. I attempted the threading solution and actually managed to get the program running as expected; whenever the user presses the Search or Export buttons, the event_handler opens a new thread and runs the query/export functions within it. They do whatever they are supposed to do and the GUI doesn't freeze. However, while reading up on this I came across a substantial amount of posts warning against using threading in Python - specially for beginners like me - and to use multiprocessing instead. More alarming is the fact that most guides are written assuming you are using Queue in tandem with threading, which I am not - and nor do I understand why would I need it. Because of this, I am becoming wary of investing more time in threading, out of fear of doing something incredibly wrong but not immediately obvious. Can anyone explain - in beginner's terms - if it is truly a bad idea to use threading, and if so, why? The only code section where I am currently using threading is this one: code:
|
# ? Jan 14, 2015 22:46 |
|
Rlazgoth posted:More alarming is the fact that most guides are written assuming you are using Queue in tandem with threading, which I am not - and nor do I understand why would I need it. Because of this, I am becoming wary of investing more time in threading, out of fear of doing something incredibly wrong but not immediately obvious. Can anyone explain - in beginner's terms - if it is truly a bad idea to use threading, and if so, why? The only code section where I am currently using threading is this one: You need the Queue if you are trying to communicate with the thread. But it looks like you aren't returning anything, just running a thread. If that changes, You'll want to learn about the Tkinter method after() which will lets you check in on a queue regularly.
|
# ? Jan 14, 2015 22:57 |
|
FoiledAgain posted:You need the Queue if you are trying to communicate with the thread. But it looks like you aren't returning anything, just running a thread. Actually that answers what would be my next question. The search function does return something, an URL which is then used by an export function to save the results locally. I was wondering how to access that value and this explains the role of Queue in the middle of this. Thanks!
|
# ? Jan 14, 2015 23:04 |
|
Rlazgoth posted:Actually that answers what would be my next question. The search function does return something, an URL which is then used by an export function to save the results locally. I was wondering how to access that value and this explains the role of Queue in the middle of this. Thanks! It's fairly simple to do. Create a queue in your main Tk thread, send it as an argument to your search function, and then tell that function to .put() your result into the queue. In your main thread, you need a function something like this: code:
|
# ? Jan 15, 2015 00:19 |
|
|
# ? Jun 11, 2024 18:54 |
|
Is this gross?code:
code:
sharktamer fucked around with this message at 01:46 on Jan 15, 2015 |
# ? Jan 15, 2015 01:28 |