|
Senso posted:I never really used threads before, with the reputation of being more complex and hard to debug. I'll give threads a look tomorrow. Is sharing objects between threads much easier than with multiprocessing? multiprocessing is a drop in replacement for threading that uses processes instead. you should have little problem using similar constructs (threading.Queue iirc)
|
# ? Nov 1, 2011 18:33 |
|
|
# ? May 9, 2024 09:54 |
|
tef posted:multiprocessing is a drop in replacement for threading that uses processes instead. you should have little problem using similar constructs (threading.Queue iirc) True, I was able to switch to threads in a few minutes. I don't even need a Queue since, for now, I have one thread writing and one thread reading and global items can be accessed by both threads. It's dead simple like that, should have looked into that in the first place...
|
# ? Nov 2, 2011 09:45 |
|
I don't normally go out of my way to mention this, but for what you're talking about (multiple threads accessing the same global items) multiprocessing is NOT a drop in replacement. Don't worry about the specifics for now, but the subtleties definitely tripped me up when going from threads to processing.
|
# ? Nov 2, 2011 15:55 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:12 on Mar 13, 2017 |
# ? Nov 4, 2011 01:25 |
|
It'd be a lot easier to use a dictionary for that, or even better, the Counter class in the collections module. You can pass it a list and it will count each element's occurrences. Although it's not very thoroughly tested, this should work. It uses a list comprehension to create a list of number pairs and the Counter class to get each pair's occurrence. code:
|
# ? Nov 4, 2011 01:57 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:25 on Mar 13, 2017 |
# ? Nov 4, 2011 02:00 |
|
well Counter is new in 2.7/3.1 I believe, so that may be why? Not sure if you mentioned all the particulars of your environment
|
# ? Nov 4, 2011 02:08 |
|
GregNorc posted:Cool, thanks. What is the if name == main bit doing? Last part is kind of irrelevant to the issue, just habit. Executes that section only if you're running the file itself, rather than importing it. I think your code needs a slight change on line 42: follow = data[i + gap + 1] Not sure on the error.
|
# ? Nov 4, 2011 02:10 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:25 on Mar 13, 2017 |
# ? Nov 4, 2011 02:12 |
|
Ok, that makes sense. If you don't have the Counter class you can get the same effect using dictionaries:code:
|
# ? Nov 4, 2011 02:18 |
|
Metroid48 posted:Ok, that makes sense. If you don't have the Counter class you can get the same effect using dictionaries: This is a touch shorter, and defaultdict should be available in GregNorc's version of Python. code:
|
# ? Nov 4, 2011 02:34 |
|
FoiledAgain posted:This is a touch shorter, and defaultdict should be available in GregNorc's version of Python. code:
|
# ? Nov 4, 2011 02:36 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:25 on Mar 13, 2017 |
# ? Nov 4, 2011 02:37 |
|
GregNorc posted:What I need to do is store how often X follows Y with a gap of N. So if the gap is 0, then how often does X directly follow y? Should 233 have two entries for 2->3 or just one?
|
# ? Nov 4, 2011 02:41 |
|
GregNorc posted:This is a cool idea, but it doesn't do exactly what I'm trying to do. It's a replacement for the collections.Counter part from the first code snippet Using the defaultdict edit FoiledAgain posted: code:
|
# ? Nov 4, 2011 02:54 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:25 on Mar 13, 2017 |
# ? Nov 4, 2011 03:04 |
|
GregNorc posted:If we had 1234567890 with a gap of 2 Looks like a gap of 1! GregNorc posted:Edit: And I ran the code Metroid48 posted, and I still get an error: Make sure you have "from collections import defaultdict" at the top of the file. If it's still giving that error even with the line, replace count_elements with the first version I posted since it's the simplest (albeit least concise) version: code:
|
# ? Nov 4, 2011 03:26 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:25 on Mar 13, 2017 |
# ? Nov 5, 2011 23:53 |
|
The error in your code is line 42, which should read: follow = data[i+gap+1] The +1 is missing from your version, so with a gap size of 0 you are always getting the same number for prior and follow. Note that you'll also need to add a +1 to the loop condition on line 37 to avoid an index out of range error. As for the code sample I posted: Metroid48 posted:If it's still giving that error even with the line, replace count_elements with the first version I posted since it's the simplest (albeit least concise) version: That isn't the full file, that's just an adjustment to make it work on your version of python. Here's the full file you should have: code:
|
# ? Nov 6, 2011 08:56 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:25 on Mar 13, 2017 |
# ? Nov 6, 2011 23:22 |
|
I've recently been attempting to jump into Python to script my webservice testing. I've run into a BadStatusLine error that I just can't get my head around. I created a question over at StackOverflow: http://stackoverflow.com/questions/8031636/python-badstatusline-error-on-certain-delete-requests. Any thoughts?
|
# ? Nov 7, 2011 04:01 |
|
Edit: Double Post
maskenfreiheit fucked around with this message at 20:36 on Mar 13, 2017 |
# ? Nov 10, 2011 02:10 |
|
GregNorc posted:Is there anything I can do? Maybe use some special, larger datatype? The numbers involved in that code are way too large to be computed by anything. That formula also won't help you with your original question. I recommend you follow the advice in ultrafilter's post, or, if you're desperate for a crash course in precisely what you need to solve your problem make a thread in SAL for your original problem and people (possibly even me) might be willing to help you out. Edit: Fixed link to ultrafilter's post. dirby fucked around with this message at 03:01 on Nov 10, 2011 |
# ? Nov 10, 2011 02:51 |
|
When writing modules, where is it best to put the exception handling? For example, I've written three modules; One that describes a Publication class with a title, date, author etc, One that handles database connectivity and CRUD operations, One that connects to an API and queries for XML documents and creates a Publication object. I then have a main script that imports the modules, and does what it needs to do (i.e. get the publications and put them in the database). Should I be putting all the exception handling within the functions in the modules themselves? Or should I let the exceptions fall through to the main script and handle them all there? A module that doesn't have any exception handling seems a bit strange to me, but then when unit testing you expect invalid data to be raising exceptions. What's the "pythonic" way of going about this?
|
# ? Nov 10, 2011 12:48 |
|
A module is just python code like any other. Catch the errors inside your code, then push an appropriate exception upwards towards whatevers calling it, so it can deal with it, unless you want to handle it yourself then dont. Just define the exceptions you want to describe at the start of your code, then raise your FooBarException where it needs to be, and then when your unit testing , make sure your code raises that exception you defined where the failure conditions are met. Exceptions are a pain in the rear end, but at least we're not doing that @who_gives_a_fuck_if_our_code_sucks() poo poo that the php people do. Every time I see code that just lazily swallows exceptions in PHP I want to pound some coders loving head in. duck monster fucked around with this message at 14:23 on Nov 10, 2011 |
# ? Nov 10, 2011 14:20 |
|
What's the best way to remove items with a certain character in them from a list? What I've tried so far has been weird with one of the elements not getting deleted. code:
code:
Still pretty new at this but my searches have yielded stuff that I don't quite grasp yet as far as answers so it's hard to tell if that's what I need or not. Edit: Well, this seemed to work instead of all that stuff above, though I'm not quite sure what it does but I can figure that out in more depth on my own: sorted_sources_list = [x for x in sorted_sources_list if "-" not in x] fnordcircle fucked around with this message at 16:45 on Nov 10, 2011 |
# ? Nov 10, 2011 16:39 |
|
I don't think you can modify the list you are looping through.
|
# ? Nov 10, 2011 16:47 |
|
rule number 1 for working with lists: never ever delete elements of the list while you are iterating over it. It will do unpredictable things because you're changing the list as you're iterating.quote:
This is a list comprehension. You are applying logic statements to construct a new list while iterating over the original list. It is exactly the same as: code:
|
# ? Nov 10, 2011 16:48 |
|
taqueso posted:I don't think you can modify the list you are looping through. You can but I don't know of a good reason ever to do so.
|
# ? Nov 10, 2011 16:59 |
|
No Safe Word posted:You can but I don't know of a good reason ever to do so. Ya, I really should have used the word "should" in place of "can".
|
# ? Nov 10, 2011 17:26 |
|
Is there a reason that this:code:
Can someone more in tune with Python culture tell me if that's a wart or an intentional feature?
|
# ? Nov 10, 2011 17:38 |
|
Munkeymon posted:Can someone more in tune with Python culture tell me if that's a wart or an intentional feature? wart, no longer a feature in python 3 quote:Python 3.0 has simplified the rules for ordering comparisons:
|
# ? Nov 10, 2011 17:49 |
|
fnordcircle posted:What's the best way to remove items with a certain character in them from a list? Internally, python keeps a counter as it progresses through the list. When you remove the current item, the counter isn't changed, so it effectively skips one item in the list. This is why that one item keeps slipping through. Do not rely on this behaviour. If you mutate the list, you have to keep an explicit count yourself, and only increment it if you don't remove the current item. code:
|
# ? Nov 10, 2011 17:53 |
|
tef posted:Internally, python keeps a counter as it progresses through the list. When you remove the current item, the counter isn't changed, so it effectively skips one item in the list. This is why that one item keeps slipping through. Do not rely on this behaviour. Awesome, that's what I was thinking was happening. That 'for each in list' means 'each' is a pointer, in essence, so when I was using the remove method I was screwing up my list index. I dunno how close I was but that at least makes sense to me. On the flip side I do modify another list by appending to it as I go along so I can sort it for real values when some of the values are nested. I haven't seen any problems like this with appending should I expect to? Example: code:
|
# ? Nov 10, 2011 18:47 |
|
fnordcircle posted:Example: Some quick style tips - as a guideline, not a doctrine. Good names should reflect intent, not implementation. Avoid suffixes like dict - i.e prefer groups/hosts over group_dict/hosts_dict. Use the right data structures. Don't store lists of data within strings, but of lists of strings. i.e groups = {'group1': [ "host1", "host2"] } (There are a lot of arguments about naming - with python, just use PEP8 or /lots/ of people will whinge at you) Do one thing at a time - Instead of mutating the list and implicitly not breaking the iterator, collect the names first, before doing the lookup. (and if you must mutate a list in a for-loop, use *explicit* iteration, rather than relying on internal behaviour) Programming is not a game of outwitting the compiler, but an exercise of explaining something to other programmers, as well as the computer. It isn't worth confusing either of them - write dumb code that is very straight forward and you will be surprised how little you come back to it. Write clever things and you waste immense amount of time coming back to fix edge and boundary conditions. Relying on append not to break is a bad idea. It makes your code harder to follow. code:
|
# ? Nov 10, 2011 19:11 |
|
Thanks for all the feedback, Tef, that's some awesome stuff there. Not all of it fully sinks in on the first read but I'm sure I'll understand it eventually.
|
# ? Nov 10, 2011 20:38 |
|
tef posted:wart, no longer a feature in python 3 Excellent - thanks
|
# ? Nov 10, 2011 22:27 |
|
I have 10,000 objects or so and I basically want to group them into categories based on the value of a specific property. The property that I care about is a list of 6 floats. My initial thought on how to approach this was to create a dict() where the keys are the 6 element list, but this isn't possible. What is the best approach for this? code:
|
# ? Nov 11, 2011 17:47 |
|
Lists are mutable and thus cannot be used as keys. Tuples, however, can.code:
|
# ? Nov 11, 2011 18:03 |
|
|
# ? May 9, 2024 09:54 |
|
TasteMyHouse posted:Lists are mutable and thus cannot be used as keys. Tuples, however, can. Oh awesome. That makes perfect sense now that I think about it. Thank you.
|
# ? Nov 11, 2011 18:05 |