|
The guide that I'm using is telling me that I can use the 'sort' and 'item' functions to alphabetize my output, but since it's not working AT ALL, I guess I'm messing something up. Your code, however, makes much more sense than mine ever did (and I was not aware of the 'sorted' function or defaultDict class, obviously, so thanks for teaching me that!).
|
# ? Jun 26, 2011 17:39 |
|
|
# ? May 9, 2024 15:35 |
|
Clandestine! posted:The guide that I'm using is telling me that I can use the 'sort' and 'item' functions to alphabetize my output, but since it's not working AT ALL, I guess I'm messing something up. Your code, however, makes much more sense than mine ever did (and I was not aware of the 'sorted' function or defaultDict class, obviously, so thanks for teaching me that!). code:
this page explains how all the dictionary methods work.
|
# ? Jun 26, 2011 17:47 |
|
The dict itself can't be sorted, but it can be represented as two lists (keys and/or items) and sorted thereby. e: Didn't realize tripwire's post included the exact same code I posted! e: V xtal fucked around with this message at 20:33 on Jun 26, 2011 |
# ? Jun 26, 2011 20:29 |
|
NOISIA posted:Couldn't that be accomplished with this? It's absolutely not Pythonic, but. what exactly is not "Pythonic" in this code? it's perfectly fine.
|
# ? Jun 26, 2011 20:31 |
|
German Joey posted:what exactly is not "Pythonic" in this code? it's perfectly fine. I'd say it'd be more pythonic to use an iterator whenever you're doing a for-loop, and in general, It's not super pythonic to iterate over the indexes/keys of something and just use them to access the values of that key/index. It's not bad code, but that's my take. But yeah, on 2.7 and above, it's all about collections.Counter (which I think is what tripwire meant): http://docs.python.org/dev/library/collections.html#collections.Counter
|
# ? Jun 26, 2011 21:32 |
|
German Joey posted:what exactly is not "Pythonic" in this code? it's perfectly fine. There's no comments with quotes from Monty Python.
|
# ? Jun 28, 2011 17:05 |
|
So I've got this function:code:
code:
|
# ? Jun 30, 2011 22:59 |
|
Plastic Snake posted:So I've got this function: code:
|
# ? Jun 30, 2011 23:10 |
|
I realize i'm being overly nitpicky, but it always irks me to see camelcase in python. Also nitpicky (and I realize you are probably just writing a small one off script), but I would be careful about your choice of keyword argument defaults- usually you try to write functions like the one you are talking about so that they're completely agnostic about everything not immediately related to their role. In other words, you should avoid whenever possible having the behaviour of a function definition depend on module level variables, especially things like the position of a command line argument. The function really doesn't need to know anything about the position of module level variables like sys.argv- if you want it to use those values, just feed them in explicitly in your function call. Another related note, be especially wary of constructions like this: code:
The Counter class in python2.7 and newer simplifies it even further: code:
tripwire fucked around with this message at 00:05 on Jul 1, 2011 |
# ? Jun 30, 2011 23:48 |
|
tripwire posted:I realize i'm being overly nitpicky, but it always irks me to see camelcase in python. This is all true, and I'm glad you said it. I was going to but figured I was just being overly nitpicky.
|
# ? Jun 30, 2011 23:50 |
|
Posted this in the Minecraft thread, but I felt my efforts may be appreciated here too.
|
# ? Jul 1, 2011 06:57 |
|
Captain Capacitor posted:Posted this in the Minecraft thread, but I felt my efforts may be appreciated here too. Someone already implemented a beanshell repl, which integrates more easily with minecraft itself, but this is still neat.
|
# ? Jul 1, 2011 07:02 |
|
Scaevolus posted:Someone already implemented a beanshell repl, which integrates more easily with minecraft itself, but this is still neat. Ah, neat. I'm building scriptable blocks and adapting ModLoader to support Jython classes.
|
# ? Jul 1, 2011 07:10 |
|
What does it mean when the Python Shell GUI for Windows writes a line of "---------------------------------", and then just stops doing anything, although the program is still running? When this happened the program would stop using the CPU, but the RAM consumption didn't fall until I killed the pythonw.exe process (which was after I killed the shell process). I was not into swap file territory. EDIT: I just realized that it probably is because the Python Windows implementation I'm running is 32-bit. EDIT: Yep, 64-bit cleared that right up. Jarl fucked around with this message at 14:41 on Jul 1, 2011 |
# ? Jul 1, 2011 11:31 |
|
tripwire posted:The Counter class in python2.7 and newer simplifies it even further: code:
|
# ? Jul 1, 2011 15:39 |
|
Lysidas posted:
Even better.
|
# ? Jul 1, 2011 19:51 |
|
Lysidas posted:
code:
|
# ? Jul 2, 2011 17:01 |
|
yaoi prophet posted:
code:
|
# ? Jul 2, 2011 17:50 |
|
Im a newb to Python, been working in it for the past 2 months, pleased with it getting things done, but ive got a couple of newb questions which this might be the place for. Imports. If ive got say 2 modules which both import 're' (regular expressions module) for example is it clever enough to ... you know what while typing that i think realised the answer, the pyc in the python install dir will be be the one both modules access yeah? Imports again, lots of sample code examples use 'Import [object] from [library/module]'. Is there any disadvantage to just saying 'Import [library/module]' and having it all unless you are making something that has to be mega small? Error handling. While I really do love working in Python I find the feedback from it can be next to useless. Simple example: code:
code:
Thanks guys if you got anything on those questions.
|
# ? Jul 2, 2011 17:52 |
|
Profane Obituary! posted:
lol, for all the complaints about "Pythonic" code, y'all sure are acting like a bunch of Perl geeks.
|
# ? Jul 2, 2011 18:18 |
|
Graviton v2 posted:Im a newb to Python, been working in it for the past 2 months, pleased with it getting things done, but ive got a couple of newb questions which this might be the place for. it's not that it didn't give you feedback for an error, its that code:
code:
as far as imports go, i'm not sure what you're talking about. a module that imports a file into its namespace still keeps its own namespace as sacred, of course. conflicts can unfortunately occur if you're trying to import two things into the same module that have the same name (although surely not with a builtin module like 're' ????), but you can resolve them by renaming them in the import like this: code:
German Joey fucked around with this message at 18:34 on Jul 2, 2011 |
# ? Jul 2, 2011 18:31 |
|
German Joey posted:lol, for all the complaints about "Pythonic" code, y'all sure are acting like a bunch of Perl geeks. You take that back !
|
# ? Jul 2, 2011 18:32 |
|
Profane Obituary! posted:
No, because now if you want to count something other than characters you still have to use character_tally.
|
# ? Jul 2, 2011 18:38 |
|
German Joey posted:stuff
|
# ? Jul 2, 2011 18:39 |
|
German Joey posted:
Something I just thought of while reading this and I can't seem to find from googling is: Given all of that, how do you specify the repr of a method? (IE: Implement/override __repr__(), __str__(), or __unicode__()?
|
# ? Jul 2, 2011 18:52 |
|
Graviton v2 posted:I was going to respond in an angry fashion to your smugness but after reading your post a couple of times it was actually very helpfull. So thanks (smartarse). well, i mean, i was just responding in kind. the tone of your post sounded to me like "Wow, can't believe I'm stuck in this backwater hick town. You guys seriously poo poo in holes... outside? Back in the city, where I'm from, we got these 'thangs' called 'toilets', can't believe you've never heard of 'em. How are you supposed to live like this?" there's all kinds of crazy rear end IDEs for python if you want something more like you're used to with .net or whatever you meant. i think there's even an eclipse plugin if you don't mind your computer throbbing and near-bursting at the seams just from editing text. you can also use the interactive shell to test code that you might be unfamiliar with; i just learned python myself about a year ago, coming from a background in Perl and Java where, (especially compared to Perl), everything is The Same But Different, and still find myself using the shell to test out simple python mechanics.
|
# ? Jul 2, 2011 18:59 |
|
German Joey posted:well, i mean, i was just responding in kind. the tone of your post sounded to me like "Wow, can't believe I'm stuck in this backwater hick town. You guys seriously poo poo in holes... outside? Back in the city, where I'm from, we got these 'thangs' called 'toilets', can't believe you've never heard of 'em. How are you supposed to live like this?" And I only discovered the fact that you could use the shell to try stuff out a couple of weeks ago, that really should have been one of the first things my boss told me about! In praise of the language I have to say that amazed how easy it was to implement RPC-XML stuff, ive had a fear of network programming since I did a contract 5'ish years ago where I had to use MS DCOM. There is no loving question at all about which is easier.
|
# ? Jul 2, 2011 19:11 |
|
Hughlander posted:Something I just thought of while reading this and I can't seem to find from googling is: Given all of that, how do you specify the repr of a method? (IE: Implement/override __repr__(), __str__(), or __unicode__()? well, that's not so straightforward. it's pretty crazy to even want to do this, so you better believe the solution is gonna be pretty crazy too. so, the obvious way would be to do this is this: code:
code:
http://forums.somethingawful.com/showthread.php?threadid=2675400&pagenumber=134&perpage=40#post385293739 anyways, good luck! German Joey fucked around with this message at 19:20 on Jul 2, 2011 |
# ? Jul 2, 2011 19:16 |
|
why there isn't a module offering a threading pool which behaves this way:code:
I had to resort to a custom made thread Pool, which is way more confortable: http://pastebin.com/xZ0j5cfi
|
# ? Jul 3, 2011 02:34 |
|
TK422 posted:why there isn't a module offering a threading pool which behaves this way: uh, just use Queue, its exactly for this. or maybe i'm confused as to what you're trying to do?
|
# ? Jul 3, 2011 02:51 |
|
My interpretation is that the desire is to have the guarantee that as soon as a job is submitted, it was be worked immediately, or it will block until it's worked immediately. So even if Queue has a maxsize set (link), it won't block unless it hits that max size, which doesn't necessarily guarantee the kind of thing outline above. That said, Queue is definitely how this (this being having multiple workers attack things) kind of thing is normally done
|
# ? Jul 3, 2011 05:14 |
|
You are looking for a mutex then i think? e: looks like they have it: http://docs.python.org/library/mutex.html Graviton v2 fucked around with this message at 07:01 on Jul 3, 2011 |
# ? Jul 3, 2011 06:44 |
|
Disclaimer: I just started learning Python and PyQt from a background of C++ and Java, with no graphical applications to speak, so I'm pretty dumb about everything. I'm making a simple little program for checking the validity of XML files with a chosen XML Schema as an exercise for myself. The program is pretty much working fine right now, with one little annoyance: If I try to validate a valid XML with a faulty Schema, or validate a faulty XML with a valid Schema, I get an error printed into the console about what is wrong with the Schema and/or XML. This is a problem because I already have a pop-up window notifying of any errors and I don't know how to remove the console messages - the errors don't seem to be Python exceptions at all so a try block doesn't do anything. I googled around a bit and it seems a possible fix would involve hooks into sys.excepthook or something complicated like that, and would possibly screw up other error messages, and I didn't really understand the fix proposed. It seems like it's a fairly simple thing that should have a simple fix, since why would PyQt have random stuff printed into the console and no way to stop it? I am using Python 2.7 with PyQt4, on Windows 7. The specific methods that make the errors are QtXmlPatterns.QXmlSchema.isValid and QtXmlPatterns.QXmlSchemaValidator.validate.
|
# ? Jul 3, 2011 15:31 |
|
Cukel posted:Disclaimer: I just started learning Python and PyQt from a background of C++ and Java, with no graphical applications to speak, so I'm pretty dumb about everything.
|
# ? Jul 3, 2011 17:04 |
|
Graviton v2 posted:Are you using the 'simplexml parsing' lib, im not on the work box atm so my terminology might be off but that lib deffo hates any non-readable ascii, base64.encode/base64.decode might help. Maybe.
|
# ? Jul 3, 2011 17:55 |
|
Cukel posted:I'm using PyQt libraries pretty much exclusively, QtXml and QtXmlPatterns. And sorry if I was unclear, the problem isn't that it detects something being wrong with the XML or the Schema, since that's what I want it to do. I just don't want it to print the problem to console, since I'd like to make my own implementation how to deal with problems or exceptions, and there doesn't seem to be any simple way to stop it from printing to console.
|
# ? Jul 3, 2011 22:06 |
|
Cukel posted:there doesn't seem to be any simple way to stop it from printing to console. Is this what you want to do maybe? code:
I also have my own question. I'm working on an agent-based simulation, and I'm stuck at how to write a particular function. Here's the scenario: I have two agents, call them Sender and Reciever. The Sender chooses two items x,y from a set of items and combines them into z, which is sent to the Reciever. On the basis of z, the Receiver should be able to work out what x and y are. It would be ideal if this could be done imperfectly - I actually want the Receiver to occasionally deduce x or y incorrectly. The items x and y are generated from a set of binary features, they could be represented as a sequence of 1s and 0s. I have no formal background in CS, so maybe this is something standard (it seems like a common problem anyway). I'd really like to solve this myself, I'm just not sure where to start: any useful imports or terms I should google?
|
# ? Jul 5, 2011 20:17 |
|
There is a library/module called pickle I noticed the other day which seems to be basically an object serializer.
|
# ? Jul 6, 2011 00:20 |
|
FoiledAgain posted:I also have my own question. I'm working on an agent-based simulation, and I'm stuck at how to write a particular function. Here's the scenario: Hm, that's closely related to en-/decryption: "Given a message encrypted with a key, try to guess the message (and the key)". The chance of successfully guessing starts off at 0% if the key is at least as long as the message, rising towards but not hitting 100% as it gets shorter. I'm not sure what specific terms apply here, though.
|
# ? Jul 6, 2011 03:01 |
|
|
# ? May 9, 2024 15:35 |
|
FoiledAgain posted:I have two agents, call them Sender and Reciever. The Sender chooses two items x,y from a set of items and combines them into z, which is sent to the Reciever. On the basis of z, the Receiver should be able to work out what x and y are. It would be ideal if this could be done imperfectly - I actually want the Receiver to occasionally deduce x or y incorrectly. The items x and y are generated from a set of binary features, they could be represented as a sequence of 1s and 0s. "Error correcting code" is probably a good search term to start with. I'm imagining a process like this: 1. Sender combines x and y in a reversible way to get z (e.g. concatenate their bitstring representations) 2. Sender encodes z using an error correcting code. 3. Receiver gets z' which is z + some flipped bits. Choose the number of flipped bits randomly around the error correction threshold of the code you're using. If you pick below the threshold then Receiver can recover z from z' and thus decode x and y correctly, if you pick above the threshold then z will not be recovered and the x and y Receiver sees will not be correct. Adjust the amount of noise to control how often Receiver makes mistakes.
|
# ? Jul 6, 2011 04:07 |