|
Do you actually wan't to modify the text, or just print it out in that format?
|
# ? Jul 19, 2010 20:03 |
|
|
# ? Jun 12, 2024 18:53 |
|
.lstrip("#").strip() lstrip will remove #s on the left hand side, and strip() will remove whitespace from both.
|
# ? Jul 19, 2010 20:07 |
|
MEAT TREAT posted:Do you actually wan't to modify the text, or just print it out in that format?
|
# ? Jul 19, 2010 20:10 |
|
[x.split('\t')[1] for x in jumble[135:145]]
|
# ? Jul 19, 2010 20:14 |
|
I saw this in an old 2008 blog post about unittesting: http://pythoscope.org/faq I'm wondering if anyone has any experience with it in the 2.6 and now 2.7 world, especially with all the 2.7 changes to unittesting.
|
# ? Jul 20, 2010 03:40 |
|
How do you upload a file-like object to a url with urllib2? Ihave a script that creates a zip file z which is a file-like object. I want to upload it to a server:code:
code:
code:
|
# ? Jul 20, 2010 09:09 |
|
nbv4 posted:according to this: http://fabien.seisen.org/python/urllib2_file/ it should just work... edit: You've opened the file already, right? code:
Haystack fucked around with this message at 15:21 on Jul 20, 2010 |
# ? Jul 20, 2010 15:01 |
|
don't you have to call urllib.urlencode on data first?
|
# ? Jul 20, 2010 15:07 |
|
quote:You've opened the file already, right? Sailor_Spoon posted:don't you have to call urllib.urlencode on data first? code:
|
# ? Jul 20, 2010 19:01 |
|
I'm trying to use Notepad++ after using IDLE for a long time. One thing I miss about IDLE though is that after I've loaded a module or whatever, I can type the name of the module and a period, then wait for IDLE to pop up a list of possible sub-categories to choose from. Is there any way to do that in Notepad++? For example, in IDLE I can type >>>import math >>>math. and it will suggest a list of everything I can choose from, without me having to remember the whole name.
|
# ? Jul 20, 2010 19:30 |
|
Looking at http://fabien.seisen.org/python/urllib2_file/ again, the point of it is that the code you pasted initially WON'T work. What's making his code work is the urllib2_file module that he wrote (the first import). Try using that.
|
# ? Jul 20, 2010 19:32 |
|
Sailor_Spoon posted:Looking at http://fabien.seisen.org/python/urllib2_file/ again, the point of it is that the code you pasted initially WON'T work. What's making his code work is the urllib2_file module that he wrote (the first import). Try using that. I came across urllib2_file before and people were saying that that module is no longer required in 2.5+ because all the functionality it provides is apparently now part of urllib2. see: http://stackoverflow.com/questions/407468/python-urllib2-file-upload-problems Anyways, I found this: http://www.doughellmann.com/PyMOTW/urllib2/#uploading-files which solves my problem.
|
# ? Jul 20, 2010 19:45 |
|
Alright so for this last piece of the dejumbler thing I'm working on, the only bit of code I could get to give me an output is really sloppy:code:
Any ideas? I know I have to do a loop somehow but I can't see it right now. Kosani fucked around with this message at 20:42 on Jul 20, 2010 |
# ? Jul 20, 2010 20:37 |
|
Kosani posted:Alright so for this last piece of the dejumbler thing I'm working on, the only bit of code I could get to give me an output is really sloppy: I think something like the last line of the below should work but it's hard to tell exactly what your snippet is doing... At least what this does is output a list of the values from 'words' that when sorted match sorted values from 'jumble_read' code:
|
# ? Jul 20, 2010 20:54 |
|
code:
b0lt fucked around with this message at 21:08 on Jul 20, 2010 |
# ? Jul 20, 2010 21:06 |
|
print is an expression, not a function. you can do that in python 3 though.
|
# ? Jul 20, 2010 21:13 |
|
Kosani posted:Alright so for this last piece of the dejumbler thing I'm working on, the only bit of code I could get to give me an output is really sloppy: You're re-doing a lot of work over and over again. You might want to use a python dictionary to create an index from the sorted word to the possible words code:
tef fucked around with this message at 21:26 on Jul 20, 2010 |
# ? Jul 20, 2010 21:18 |
|
or in normal person python, you can do from __future__ import print_function, and then use that
|
# ? Jul 20, 2010 21:20 |
|
Sailor_Spoon posted:or in normal person python, you can do from __future__ import print_function, and then use that Doesn't it work as a function by default in 2.6 at least?
|
# ? Jul 21, 2010 00:23 |
|
AzraelNewtype posted:Doesn't it work as a function by default in 2.6 at least? No.
|
# ? Jul 21, 2010 00:26 |
|
If I have a function which turns a Carrot object into a Tomato object, where do I put it and what do I call it? Do I put it in the Carrot class so I can do "t = c.getTomato()" or do I modify the constructor of Tomato so I can do "t = Tomato(c)" or do I put a separate function so I can do "t = getTomatoFromCarrot(c)"? This is more of a general programming question than a Python-specific one, but I'm writing Python and Python is known for its "There's only one way to do it" philosophy so I want to get it right.
|
# ? Jul 21, 2010 13:53 |
|
qntm posted:If I have a function which turns a Carrot object into a Tomato object, where do I put it and what do I call it? Do I put it in the Carrot class so I can do "t = c.getTomato()" or do I modify the constructor of Tomato so I can do "t = Tomato(c)" or do I put a separate function so I can do "t = getTomatoFromCarrot(c)"? Unless Carrots are the only way of making Tomatoes, having Tomato.__init__ accept a Carrot has always sat a little uneasy with me. I think a better way is to make from_carrot a class method on Tomato, so you would then say: code:
|
# ? Jul 21, 2010 14:18 |
|
I need to do what amounts to an scp from a script I'm writing. Are there any good ssh libraries or should I just use subprocess?
|
# ? Jul 21, 2010 18:17 |
|
wins32767 posted:Are there any good ssh libraries or should I just use subprocess? Ninja edit: Would fabric's .put() not do what you need? Yay fucked around with this message at 18:26 on Jul 21, 2010 |
# ? Jul 21, 2010 18:24 |
|
wins32767 posted:I need to do what amounts to an scp from a script I'm writing. Are there any good ssh libraries or should I just use subprocess? I've had some success with paramiko, although it does have some quirks. It allows you to use an sftp subsystem over an established ssh channel which is pretty much as good as scp (although I suppose you could just cat the remote files and stream data from the socket into a local file for similar effect)
|
# ? Jul 21, 2010 18:34 |
|
Yay posted:I only have Paramiko installed because Fabric depends on it, but its all I've ever heard recommended. Its pure python, if that matters to you. I think it depends on PyCrypto though; which is not. I want to pull a file down from a remote server to the local one. I just glanced at the Fabric docs, but there doesn't seem to be a "get" function and there doesn't appear to be a way to use put to pull files down.
|
# ? Jul 21, 2010 18:59 |
|
tripwire posted:I've had some success with paramiko, although it does have some quirks. It allows you to use an sftp subsystem over an established ssh channel which is pretty much as good as scp (although I suppose you could just cat the remote files and stream data from the socket into a local file for similar effect) I suspect just doing subprocess.check_call(['/path/to/scp', '-i key_file' 'remote_host:/path/to/file', '/path/to/depository']) will probably be easier to deal with, though a bit less clean.
|
# ? Jul 21, 2010 19:01 |
|
wins32767 posted:I want to pull a file down from a remote server to the local one. I just glanced at the Fabric docs, but there doesn't seem to be a "get" function [...]
|
# ? Jul 21, 2010 19:35 |
|
Yay posted:see here. Still might not be what you want or need, but the method does exist, because fabric is pretty sane. Oh cool. I didn't see it on my first pass.
|
# ? Jul 21, 2010 19:38 |
|
code:
|
# ? Jul 21, 2010 21:09 |
|
qntm posted:
Redefine __eq__ on Apple, that behavior seems totally reasonable to me FWIW.
|
# ? Jul 21, 2010 21:15 |
|
qntm posted:
http://docs.python.org/reference/datamodel.html#object.__eq__ is being inherited too
|
# ? Jul 21, 2010 21:17 |
|
But it says there that "x==y calls x.__eq__(y)". While I can override __eq__ on Apple, I can't override __eq__ on frozenset. So if I call "b == a" that will call the __eq__ method in b, which is of type Apple, which I've overridden, no problem. But if I call "a == b" that will call the __eq__ method in a, which is of type frozenset, which I can't override, so that will return True, the result I don't want. Actually I'm trying it now and the overridden __eq__ method is being called in both cases? So the documentation is inaccurate?
|
# ? Jul 21, 2010 21:33 |
|
qntm posted:But it says there that "x==y calls x.__eq__(y)". While I can override __eq__ on Apple, I can't override __eq__ on frozenset. So if I call "b == a" that will call the __eq__ method in b, which is of type Apple, which I've overridden, no problem. But if I call "a == b" that will call the __eq__ method in a, which is of type frozenset, which I can't override, so that will return True, the result I don't want. Presumably frozenset tries calling the __eq__ method of what it's being compared to at some point. By the way, why are you wanting those to not be equal anyway? They're about as same "enough" for == - I can't see any situation where you'd really want to be using == and having them not be equal.
|
# ? Jul 21, 2010 21:54 |
|
Jonnty posted:Presumably frozenset tries calling the __eq__ method of what it's being compared to at some point. By the way, why are you wanting those to not be equal anyway? They're about as same "enough" for == - I can't see any situation where you'd really want to be using == and having them not be equal. Well, they're different classes, so they're plainly not identical. Also this is the simplest example of the problem that I could construct. Not shown for brevity's sake is all the extra functionality that the Apple subclass has attached to it. Even if there were no such functionality, I can imagine plenty of situations where you'd have two objects which are functionally identical, but present different types specifically so that another function will handle them differently.
|
# ? Jul 21, 2010 23:08 |
|
qntm posted:Well, they're different classes, so they're plainly not identical. qntm posted:Even if there were no such functionality, I can imagine plenty of situations where you'd have two objects which are functionally identical, but present different types specifically so that another function will handle them differently. Plorkyeran fucked around with this message at 02:59 on Jul 22, 2010 |
# ? Jul 22, 2010 02:54 |
|
Plorkyeran posted:No, an Apple is a frozenset. That's sort of the original point of inheritance. But a frozenset isn't an Apple, and an instance of frozenset isn't an instance of Apple, so a is not equal to b. Plorkyeran posted:That would be a completely insane thing to do in OOP. You make subclasses have different behavior by overriding methods, not by making the calling code check their types. Well what if, as I mentioned above, the functions in question aren't attached to either object, but are external? Then there's nothing to override.
|
# ? Jul 22, 2010 09:20 |
|
qntm posted:But a frozenset isn't an Apple, and an instance of frozenset isn't an instance of Apple, so a is not equal to b. Welcome to the http://en.wikipedia.org/wiki/Liskov_substitution_principle You are extending the principal class, which only checks to see if something is a frozenset. Being different classes does not define equality. You're asking 'Why does this thing that acts and behaves like a frozenset equal to a frozenset of the same contents" For what it is worth frozenset([1,2,3]) == set([1,2,3]) is also true. quote:Well what if, as I mentioned above, the functions in question aren't attached to either object, but are external? Then there's nothing to override. Alternatively, try composition instead of inheritance. code:
|
# ? Jul 22, 2010 11:19 |
|
qntm posted:But a frozenset isn't an Apple, and an instance of frozenset isn't an instance of Apple, so a is not equal to b. code:
|
# ? Jul 22, 2010 14:06 |
|
|
# ? Jun 12, 2024 18:53 |
|
geera posted:Maybe you should be using 'is' to compare instead of ==? is tests object references to see if they point to the same object, which is obviously not the case here. It has nothing to do with equality.
|
# ? Jul 22, 2010 14:50 |