Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
Do you actually wan't to modify the text, or just print it out in that format?

Adbot
ADBOT LOVES YOU

king_kilr
May 25, 2007
.lstrip("#").strip()

lstrip will remove #s on the left hand side, and strip() will remove whitespace from both.

Kosani
Jun 30, 2004
Ni ni.

MEAT TREAT posted:

Do you actually wan't to modify the text, or just print it out in that format?
I don't want to modify the text I just want it to read/print it in that format. Basically the "input" for my dejumbler program is going to be 10 words embedded on lines 135-145 of a big long text file of which 99% I don't need.

b0lt
Apr 29, 2005
[x.split('\t')[1] for x in jumble[135:145]]

Lurchington
Jan 2, 2003

Forums Dragoon
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.

nbv4
Aug 21, 2002

by Duchess Gummybuns
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:
data = {'artist': 'artist',
        'album': 'album'}
   
urllib2.urlopen(url=url, data=data)
works fine

code:
data = {'artist': 'artist',
        'album': 'album',
        'file': z}
   
urllib2.urlopen(url=url, data=data)
but does not work, it raises this error:

code:
Traceback (most recent call last):
  File "/home/chris/bin/upload.py", line 349, in <module>
    urllib2.urlopen(url=url, data=data)
  File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.6/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 1161, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.6/urllib2.py", line 1133, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/lib/python2.6/httplib.py", line 910, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.6/httplib.py", line 950, in _send_request
    self.send(body)
  File "/usr/lib/python2.6/httplib.py", line 755, in send
    self.sock.sendall(str)
  File "<string>", line 1, in sendall
TypeError: sendall() argument 1 must be string or buffer, not dict
according to this: http://fabien.seisen.org/python/urllib2_file/ it should just work...

Haystack
Jan 23, 2005





nbv4 posted:

according to this: http://fabien.seisen.org/python/urllib2_file/ it should just work...

According to that link, you should open the file in the data dict

edit: You've opened the file already, right?

code:
data = {'artist': 'artist',
        'album': 'album',
        'file': open(z)}
   
urllib2.urlopen(url=url, data=data)

Haystack fucked around with this message at 15:21 on Jul 20, 2010

good jovi
Dec 11, 2000

don't you have to call urllib.urlencode on data first?

nbv4
Aug 21, 2002

by Duchess Gummybuns

quote:

You've opened the file already, right?
yes, z is a file-like object

Sailor_Spoon posted:

don't you have to call urllib.urlencode on data first?
when I do that and print out the result, I get this:

code:
>>> data = {'artist': 'artist',
        'album': 'album',
        'file': z}

>>> data = urlib.urlencode(data)
>>> print data
album=The+Chronic+%28Re-Lit+and+Remastered%29&preset=-V2&file=%3Czipfile.ZipFile+instance+at+0x1c7ffc8%3E&artist=Dr.+Dre

Kire
Aug 25, 2006
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.

good jovi
Dec 11, 2000

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.

nbv4
Aug 21, 2002

by Duchess Gummybuns

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.

Kosani
Jun 30, 2004
Ni ni.
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:
for y in words[:]:
    if sorted(y) == sorted(jumble_read[0]):
        print y
    if sorted(y) == sorted(jumble_read[1]):
        print y
    if sorted(y) == sorted(jumble_read[2]):
        print y
    if sorted(y) == sorted(jumble_read[3]):
        print y
    if sorted(y) == sorted(jumble_read[4]):
        print y
    if sorted(y) == sorted(jumble_read[5]):
        print y
    if sorted(y) == sorted(jumble_read[6]):
        print y
    if sorted(y) == sorted(jumble_read[7]):
        print y
    if sorted(y) == sorted(jumble_read[8]):
        print y
    if sorted(y) == sorted(jumble_read[9]):
        print y
        
I mean it works in that it gives as a 10 line output all 10 words successfully dejumbled, but I feel like there has to be a waaay simpler way to accomplish the same task and print the output in standard list form ('word1, word2, word3...)

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

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



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:

code:
for y in words[:]:
    if sorted(y) == sorted(jumble_read[0]):
        print y
    if sorted(y) == sorted(jumble_read[1]):
        print y
    if sorted(y) == sorted(jumble_read[2]):
        print y
    if sorted(y) == sorted(jumble_read[3]):
        print y
    if sorted(y) == sorted(jumble_read[4]):
        print y
    if sorted(y) == sorted(jumble_read[5]):
        print y
    if sorted(y) == sorted(jumble_read[6]):
        print y
    if sorted(y) == sorted(jumble_read[7]):
        print y
    if sorted(y) == sorted(jumble_read[8]):
        print y
    if sorted(y) == sorted(jumble_read[9]):
        print y
        
I mean it works in that it gives as a 10 line output all 10 words successfully dejumbled, but I feel like there has to be a waaay simpler way to accomplish the same task and print the output in standard list form ('word1, word2, word3...)

Any ideas? I know I have to do a loop somehow but I can't see it right now.

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:
>>> jumble_read = ['abc', 'def', 'ghi']
>>> words = ['cba', 'fed', 'bfs', 'ala']
>>> [y for y in words if sorted(y) in [sorted(jr) for jr in jumble_read]]
['cba', 'fed']

b0lt
Apr 29, 2005
code:
for y in words[:]:
    print filter(lambda x: (lambda c,d: c == d)(sorted(x),sorted(y)), jumble_read)
As a sidenote, why does lambda x: print(x) not work <:mad:>

b0lt fucked around with this message at 21:08 on Jul 20, 2010

tef
May 30, 2004

-> some l-system crap ->
print is an expression, not a function.


you can do that in python 3 though.

tef
May 30, 2004

-> some l-system crap ->

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 mean it works in that it gives as a 10 line output all 10 words successfully dejumbled, but I feel like there has to be a waaay simpler way to accomplish the same task and print the output in standard list form ('word1, word2, word3...)

Any ideas? I know I have to do a loop somehow but I can't see it right now.


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:
# completely untested

# create a new dictionary
jumbled_dict = {}
# the keys of this dict will be the sorted version of the word
# the values of this dict will be a list of words

# i.e    'eilv' ---> [ 'evil' , 'live', ....]

for word in word_list:

    ordered_word = "".join(sorted(word))
    
    if ordered_word not in jumbled_dict:
        # if we have not encountered this sorted word before
        # then we store a list containing the original word
        jumbled_dict[ordered_word] = [word]        
    else:
        # we have encountered this sorted word before, so we add it to the list
        # of possibilities
        jumbled_dict[ordered_word].append(word)
        
        

for jumbled_word in jumbled_list
    possible_words = jumbled_dict[sorted(jumbled_word)]
    print jumbled_word, "could be", possible_words

tef fucked around with this message at 21:26 on Jul 20, 2010

good jovi
Dec 11, 2000

or in normal person python, you can do from __future__ import print_function, and then use that

AzraelNewtype
Nov 9, 2004

「ブレストバーン!!」

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?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

AzraelNewtype posted:

Doesn't it work as a function by default in 2.6 at least?

No.

qntm
Jun 17, 2009
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.

good jovi
Dec 11, 2000

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)"?

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.

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:
tomato = Tomato.from_carrot(carrot)

wins32767
Mar 16, 2007

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?

Yay
Aug 4, 2007

wins32767 posted:

Are there any good ssh libraries or should I just use subprocess?
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.

Ninja edit: Would fabric's .put() not do what you need?

Yay fucked around with this message at 18:26 on Jul 21, 2010

tripwire
Nov 19, 2004

        ghost flow

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)

wins32767
Mar 16, 2007

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.

Ninja edit: Would fabric's .put() not do what you need?

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.

wins32767
Mar 16, 2007

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.

Yay
Aug 4, 2007

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 [...]
see here. Still might not be what you want or need, but the method does exist, because fabric is pretty sane.

wins32767
Mar 16, 2007

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.

qntm
Jun 17, 2009
code:
>>> class Apple(frozenset):
...     pass
...
>>> a = frozenset("blah")
>>> b = Apple("blah")
>>> a == b
True
Why are these two objects of different classes comparing as equal, and how can I stop this from happening?

king_kilr
May 25, 2007

qntm posted:

code:
>>> class Apple(frozenset):
...     pass
...
>>> a = frozenset("blah")
>>> b = Apple("blah")
>>> a == b
True
Why are these two objects of different classes comparing as equal, and how can I stop this from happening?

Redefine __eq__ on Apple, that behavior seems totally reasonable to me FWIW.

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

qntm posted:

code:
>>> class Apple(frozenset):
...     pass
...
>>> a = frozenset("blah")
>>> b = Apple("blah")
>>> a == b
True
Why are these two objects of different classes comparing as equal, and how can I stop this from happening?

http://docs.python.org/reference/datamodel.html#object.__eq__

is being inherited too

qntm
Jun 17, 2009
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?

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

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.

Actually I'm trying it now and the overridden __eq__ method is being called in both cases? So the documentation is inaccurate?

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.

qntm
Jun 17, 2009

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.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

qntm posted:

Well, they're different classes, so they're plainly not identical.
No, an Apple is a frozenset. That's sort of the original point of inheritance.

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.
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.

Plorkyeran fucked around with this message at 02:59 on Jul 22, 2010

qntm
Jun 17, 2009

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.

tef
May 30, 2004

-> some l-system crap ->

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:
class Apple(object):
    def __init__(self, args):
        self.set = frozenset(args)

geera
May 20, 2003

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.
Maybe you should be using 'is' to compare instead of ==?

code:
>>> class Apple(frozenset):
...     pass
... 
>>> a = frozenset("blah")
>>> b = Apple("blah")
>>> a == b
True
>>> a is b
False

Adbot
ADBOT LOVES YOU

MaberMK
Feb 1, 2008

BFFs

geera posted:

Maybe you should be using 'is' to compare instead of ==?

code:
>>> class Apple(frozenset):
...     pass
... 
>>> a = frozenset("blah")
>>> b = Apple("blah")
>>> a == b
True
>>> a is b
False

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.

  • Locked thread