|
sund posted:Just as the message says, you're trying to multiply single character strings together. Use the int() function to interpret the value of the string. This is exactly the sort of advice I needed. I can see where I was going wrong and now also know how to improve it. Thanks very much.
|
# ? Oct 18, 2009 18:00 |
|
|
# ? May 13, 2024 07:31 |
|
Yo-- I'm trying to learn Python and do some simple probabilistic modelling at the same time. Let's say that I want to make a function that returns "1" with a probability of 1/5, "2" with a probability of 1/4 and "3" with a probability of 11/20. How do I do it? I'd be able to work out the rest I need if you could show me how to make a function like this.
|
# ? Oct 19, 2009 08:32 |
|
You'd want to look up the random module, although it may not be random enough depending on what you're doing. This line of code will do exactly what you asked for: random.choice([1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3]) You could also do it a bunch of other ways. edit: drat, I knew I was forgetting something to make that list look better. v Tin Gang fucked around with this message at 08:52 on Oct 19, 2009 |
# ? Oct 19, 2009 08:42 |
|
Aardlof posted:random.choice([1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3]) If you're going to do this, at least do random.choice([1]*4 + [2]*5 + [3]*11)
|
# ? Oct 19, 2009 08:45 |
|
Thank you both. Much appreciated.
|
# ? Oct 19, 2009 18:17 |
|
From the python docs:code:
|
# ? Oct 20, 2009 16:42 |
|
Error doesn't have any attributes or methods but it does inherit all of the attributes and methods from the Exception class. The other two classes have their own __init__ methods because they apparently need to deviate from their base classes. I would imagine Exception itself doesn't do much of anything itself either. One important aspect of exceptions is they have a name - otherwise, how would you raise and catch them? They also have a hierarchy (through subclassing) that lets you be less specific or more specific about the kinds of exceptions you want to catch. So, the classes themselves don't necessarily do a whole lot; it's the raise/try/except functionality that handles the interruption of code flow. If that still doesn't make sense, I'd try looking for some examples of how to use exceptions, maybe from a tutorial or some existing code.
|
# ? Oct 20, 2009 19:48 |
|
Django protip: Make sure Debug is set to False in settings.py before trying to run a script that inserts 200,000 objects into the database! I just spent last night debugging why my code was running out of memory after ~13k records no matter what I did, and learning a lot about garbage collection in python.
|
# ? Oct 20, 2009 19:52 |
|
Baruch Obamawitz posted:Django protip: Django protip: Use manage.py's loaddata for bulk inserts.
|
# ? Oct 20, 2009 20:16 |
|
deimos posted:Django protip: This, fixtures rock.
|
# ? Oct 20, 2009 20:41 |
|
deimos posted:Django protip: Gotta generate the data first! I'm still working on that galaxy thing, and I'm dealing with 200,000 randomly generated stars + ~2,000,000 randomly generated planets, and gently caress if I'm figuring that all out by hand. WhiskeyJuvenile fucked around with this message at 21:30 on Oct 20, 2009 |
# ? Oct 20, 2009 21:26 |
|
Baruch Obamawitz posted:Gotta generate the data first! I'm still working on that galaxy thing, and I'm dealing with 200,000 randomly generated stars + ~2,000,000 randomly generated planets, and gently caress if I'm figuring that all out by hand.
|
# ? Oct 20, 2009 23:45 |
|
Milde posted:Error doesn't have any attributes or methods but it does inherit all of the attributes and methods from the Exception class. The other two classes have their own __init__ methods because they apparently need to deviate from their base classes. Right, but I mean why create the Error class at all? Why not just have the InputError and TransitionError inherit directly from Exception?
|
# ? Oct 21, 2009 02:42 |
|
Thermopyle posted:Right, but I mean why create the Error class at all? Why not just have the InputError and TransitionError inherit directly from Exception? What would you do when you decide that all of your errors should have some common property? Would you go and edit each one individually?
|
# ? Oct 21, 2009 03:15 |
|
Thermopyle posted:Right, but I mean why create the Error class at all? Why not just have the InputError and TransitionError inherit directly from Exception? So you can catch all of your exceptions at once, but let others' exceptions move up the call stack.
|
# ? Oct 21, 2009 06:19 |
|
BeefofAges posted:What would you do when you decide that all of your errors should have some common property? Would you go and edit each one individually? I thought of that, but if that was the point of the example, it seems like the docs should havve actually included a common property instead of "pass". So I assumed that wasn't the point.
|
# ? Oct 21, 2009 17:17 |
|
I come back to Python frequently after a short hiatus, only to find I've forgotten everything. Here's a quick newb question: I want to print a sentence that lists objects in a list, without doing "There is a ['first item', 'second item'] and a third item." How can I make it get rid of the [ and ' for the first items?code:
|
# ? Oct 21, 2009 22:35 |
|
So I'm trying to build out and draw a directed acyclic graph with PyCairo (on top of PyGTK). Eventually this is intended to be a git graph representation, but I'm just working out the kinks right now. I've come across two issues that I could use some help on: First, no line is drawn back to a second parent node (if it exists). It only draws the first line. This is fairly obvious considering how I skip over nodes that have already been drawn, but I'm not seeing how to get around that. Second, if we are in an elevated node, meaning it was forked off, the second child node is bumped graphically higher as well. Also an obvious cause, not sure how to get around it though. Code is available in a Github gist here: http://gist.github.com/215471 It's still very incomplete mind you, I'm just trying to iron out issues along the way and take baby steps to getting it finished. Also if anybody has tips on how to improve the overall code, feel free. This is a small part of my first big Python project, so I've been on one helluva learning streak lately.
|
# ? Oct 21, 2009 22:36 |
|
code:
hlfrk414 fucked around with this message at 22:50 on Oct 21, 2009 |
# ? Oct 21, 2009 22:46 |
|
Kire posted:I come back to Python frequently after a short hiatus, only to find I've forgotten everything. Here's a quick newb question: I want to print a sentence that lists objects in a list, without doing "There is a ['first item', 'second item'] and a third item." How can I make it get rid of the [ and ' for the first items? code:
|
# ? Oct 21, 2009 22:48 |
|
Kire posted:I come back to Python frequently after a short hiatus, only to find I've forgotten everything. Here's a quick newb question: I want to print a sentence that lists objects in a list, without doing "There is a ['first item', 'second item'] and a third item." How can I make it get rid of the [ and ' for the first items? Two things, you don't need to put the len call in there, [:-1] and [-1] do the same thing to what you want. But the reason it's printing out like that is because you're passing it a list to interpolate and lists that are made into strings look like ['foo', 'bar']. What you want is something more like: code:
Or if you want to steal a utility we wrote for supybot, we wrote the most terribly named function ever, commaAndify: code:
code:
|
# ? Oct 21, 2009 23:16 |
|
Thanks for your help, I hadn't thought about the join function. That works great.
|
# ? Oct 22, 2009 19:01 |
|
Scaevolus posted:Have fun living in your fantasy galaxy where every star has 10 planets. an average of 10 planets
|
# ? Oct 22, 2009 19:10 |
|
This might help inserting hundreds of thousands of recordscode:
|
# ? Oct 22, 2009 20:19 |
|
I have a question about passing (or pulling?) information from a class object into another function. I'm trying to make a text-adventure game ala Zork, but I'm realizing that even though I can set up a class for each room, and give it an __init__(self) which sets its self.room_inventory to have items, I can't reference those items in a function that lets the player choose which ones to pick up.code:
I looked on Google for some pseudo code relating to structuring a text adventure (what should I put in a class vs. put in a function vs. in the main loop?), but all I found was a broken link to the original Zork source code.
|
# ? Oct 22, 2009 21:43 |
|
Kire posted:I have a question about passing (or pulling?) information from a class object into another function. I'm trying to make a text-adventure game ala Zork, but I'm realizing that even though I can set up a class for each room, and give it an __init__(self) which sets its self.room_inventory to have items, I can't reference those items in a function that lets the player choose which ones to pick up. Ignoring your misunderstanding of classes, what do you mean? You're alreadying doing code:
|
# ? Oct 22, 2009 21:56 |
|
Kire posted:I have a question about passing (or pulling?) information from a class object into another function. Here is a version that does what i believe you intended: code:
e: Just because I am OCD, here is how I would develop your class to make it a little more universal for all potential locations (and you can derive classes for any locations that may differ) code:
Modern Pragmatist fucked around with this message at 22:43 on Oct 22, 2009 |
# ? Oct 22, 2009 22:32 |
|
Baruch Obamawitz posted:This might help inserting hundreds of thousands of records This plus removing a .objects.all() inside the for loop to a .objects.all().values_list() outside the for loop made it from 5 seconds per 1000 records to 5 seconds per 5000 records
|
# ? Oct 23, 2009 03:08 |
|
Okay, so I'm making a browser-based game powered by a django backend (ho ho ho) with a cronjob/tick-based update running every 15 minutes. I've managed to, by a whole lot of plyer-action-initiated processing stored in the database, to have only one non-.all().update() loop likecode:
(No, I can't just Player.objects.all().update(), because the update would need to refer to a F() object on a field outside the Player table) edit: edited code sample to reflect that trying to iterate over .all() gave out-of-memory errors trying to load 200,000 records into memory, and fixed __gte to __lte. edit: And the transaction commit stuff from last post won't work due to the aggregate call within the for loop; can't read after writing without committing. WhiskeyJuvenile fucked around with this message at 05:53 on Oct 23, 2009 |
# ? Oct 23, 2009 05:48 |
|
You can do Model.objects.all().iterator(), then it won't use much memory. Also, make sure your QuerySet has an ordering, otherwise you aren't guarnteed to get every result.
|
# ? Oct 23, 2009 14:04 |
|
Using iterators, would multithreading that loop by splitting it in 10 by spawning 10 threads each to iterate over 1/10th of the count speed it up? I'm not sure how mysql works in this regard.
|
# ? Oct 23, 2009 16:19 |
|
Baruch Obamawitz posted:Using iterators, would multithreading that loop by splitting it in 10 by spawning 10 threads each to iterate over 1/10th of the count speed it up? I'm not sure how mysql works in this regard. mysql is pretty lovely at using multiple cores (compared to pgsql anyways...), plus in Python you can only really use threads for IO bound ops, I'm not sure how much mysqldb drops the GIL, or whether the time it does it for is significant, if you really want to parallelize it you'd need to use multiprocessing.
|
# ? Oct 23, 2009 16:42 |
|
king_kilr posted:mysql is pretty lovely at using multiple cores (compared to pgsql anyways...), plus in Python you can only really use threads for IO bound ops, I'm not sure how much mysqldb drops the GIL, or whether the time it does it for is significant, if you really want to parallelize it you'd need to use multiprocessing. I don't want to, I just want to be able to speed this fucker up somehow.
|
# ? Oct 23, 2009 19:50 |
|
I'd bulk load the Class2 data using an annotation.
|
# ? Oct 23, 2009 22:34 |
|
king_kilr posted:I'd bulk load the Class2 data using an annotation. Python annotation or django annotation? edit: using a test case of 200,000 players, that would be on the order of 2,000,000 integers in memory... edit2: and using it seems like the values_list() method is what I'd be looking for instead of annotation w/r/t django, yes? edit3: Just for testing, I tried this: code:
That's still too long, and I'm really banging my head about trying to figure out a way to optimize this... WhiskeyJuvenile fucked around with this message at 00:38 on Oct 24, 2009 |
# ? Oct 24, 2009 00:10 |
|
Django annotation. Who cares about 2 million integers in memory, that's under 25 MB of RAM.
|
# ? Oct 24, 2009 01:06 |
|
king_kilr posted:mysql is pretty lovely at using multiple cores (compared to pgsql anyways...), plus in Python you can only really use threads for IO bound ops, I'm not sure how much mysqldb drops the GIL, or whether the time it does it for is significant, if you really want to parallelize it you'd need to use multiprocessing. I assume he's talking to mysql over a local socket; socket code releases the GIL as it's I/O bound. On the other hand, I've heard nasty things about mysqldb. Thinking about it, since you're doing a massive alter on every object in the db for this, you are going to want to farm it off someplace. You can try threads, watch out for mysqldb though. Otherwise, spawn the jobs off to a process pool (using multiprocessing) or other dedicated job-server with multiple workers. m0nk3yz fucked around with this message at 03:27 on Oct 24, 2009 |
# ? Oct 24, 2009 03:22 |
|
Objects with attributes composed of standard data types along with attributes that are other objects, how do I handle persistent storage?
|
# ? Oct 24, 2009 18:52 |
|
Thermopyle posted:Objects with attributes composed of standard data types along with attributes that are other objects, how do I handle persistent storage? Use shelve?
|
# ? Oct 24, 2009 19:14 |
|
|
# ? May 13, 2024 07:31 |
|
Thermopyle posted:Objects with attributes composed of standard data types along with attributes that are other objects, how do I handle persistent storage? Depending on your primitives either pickle or json.
|
# ? Oct 24, 2009 20:14 |