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
FoiledAgain
May 6, 2007

Did you remember to strip the newlines away before making the comparison?

Adbot
ADBOT LOVES YOU

Hughmoris
Apr 21, 2007
Let's go to the abyss!

FoiledAgain posted:

Did you remember to strip the newlines away before making the comparison?

Yes, I think I'm doing it correctly but still no luck.

spankweasel
Jan 4, 2006

Hughmoris posted:

Ok, I am stumped. I want this function to take a value, search through a text file of dictionary words and tell me if the value is a word.
code:
def dictLook(word):
        f = open('C:/Python32/programs/dictionary.txt', 'r')
        for line in f:
                if word in line:
                        print(word + ' is a word.')
                        break
        f.close()
If i run: dictLook('oklo')
it spits out that it is a word. What am I missing?

Here is the list of words I use:
http://download.oracle.com/javase/tutorial/collections/interfaces/examples/dictionary.txt

*I think I might have stumbled upon the issue. 'oklo' is in the word 'booklore', so maybe it registers that. Is there a way to make it match word specifically and not register false words like that?

I understand that you're just doing this for learning Python but the developer in me cringes at what that function does.

Every single time you call dictLook(), it re-opens and re-reads *every* line in that file. Every time.

Do yourself a favor and just read in the entire file once. Keep it in memory for everything you need to do.

Odds are also extremely likely that the reason "line == word" isn't working is because there's a trailing newline character. Try this:

code:
with open('C:/Python32/programs/dictionary.txt', 'r') as fh:
    wordlist = [line.strip() for line in fh.readlines()]

def dictLook(word, wordlist):
    if word in wordlist:
        print(word + ' is a word.')
line 1 is a context manager (the with keyword). To keep this brief, it handles closing the file descriptor after you're done with the indented block of code.

line 2 is a list comprehension which constructs a single list of each line in the file, after its been stripped of any leading or trailing whitespace.

If you're using Python 3.2 you can do even more fun:

code:
from functools import lru_cache

with open('C:/Python32/programs/dictionary.txt', 'r') as fh:
    wordlist = [line.strip() for line in fh.readlines()]

@lru_cache(maxsize=100)
def dictLook(word, wordlist):
    if word in wordlist:
        return True
    return False

word = 'oklo'
if dictLook(word, wordlist):
    print(word + ' is a word.')
That lru_cache thing is a function decorator to cache results of the function it is currently decorating. What that means is, if you look up 'oklo' the first time, the lru_cache will check for a mapping of 'oklo' to True or False in a cache dictionary. If it finds a mapping, it will return the cached answer.

If it can't find 'oklo' in the cache, it'll proceed with the rest of the function which searches the wordlist in memory.

Before True or False returns, the answer will be cached in the lru_cache. That way, the second time you call the function with the same word, it'll simply return the cache entry instead of scanning the entire list.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

spankweasel posted:

Great stuff

Thanks for taking the time to write all of that up. I'll see what I can do with it.

MaberMK
Feb 1, 2008

BFFs
e: already answered, that'll teach me to look on the next page :eng99:

FoiledAgain
May 6, 2007

Hughmoris posted:

Yes, I think I'm doing it correctly but still no luck.

Hmm. I copied your code and the dictionary and it worked for me. What tripped me up all the time when I started Python is that strip functions don't have side effects. Did you make the mistake I use to:

code:
lines = [line for line in somefile]
for line in lines:
    line.rstrip('\n')
It has to be line=line.rstrip('\n')

MaberMK
Feb 1, 2008

BFFs
Just do line.strip()

Mopp
Oct 29, 2004

I'm a complete beginner at Python, and I want to read values incoming from a serial port and list them in some sort of dynamic view so that I can see them as come in to the port. I'm curious what the best way is to approach this. First thought is something like the "top" program in unix, a console program that updates all information instead of printing it out all the time, but I don't know how to do or what this type of "GUI" is called. Is it harder to do than to use Tkinter? Are there easier ways for a beginner?



edit: solved original but question above still remains

Mopp fucked around with this message at 14:43 on Mar 31, 2011

Stabby McDamage
Dec 11, 2005

Doctor Rope

Mopp posted:

I'm a complete beginner at Python, and I want to read values incoming from a serial port and list them in some sort of dynamic view so that I can see them as come in to the port. I'm curious what the best way is to approach this. First thought is something like the "top" program in unix, a console program that updates all information instead of printing it out all the time, but I don't know how to do or what this type of "GUI" is called. Is it harder to do than to use Tkinter? Are there easier ways for a beginner?



edit: solved original but question above still remains

What is the nature of the data you want to show? The simplest solution is just to print stuff as it passes by, maybe with a few newlines in between.

Next, you could look at faking a console GUI by running "clear" before each update, so that the data always appears by itself in the console window. In addition, you can use the carriage-return character ('\r') to move the cursor to the front of a line and overwrite that line -- that's how things like the wget progress bar work.

Moving up, a proper console GUI is usually built out of something like ncurses, which I have very little knowledge of. That's how things like 'top' work.

If you want an actual windows GUI, then I guess Tkinter or wxWindows is your best bet, but I suspect you can get by with one of the above.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



What is
code:
'str' in 'string'
Sugar for?
code:
'string'.find('str') > -1
?
At least I assume it's syntactic sugar for something string-specific because it's not working on other sequence types, but I can't find anything with Google, probably because the stupid keyword is 'in'

Thermopyle
Jul 1, 2003

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

Munkeymon posted:

because it's not working on other sequence types

code:
>>> a_list = ["one", "two", "three"]
>>> "two" in a_list
True
Did I misunderstand what you meant?

Stabby McDamage
Dec 11, 2005

Doctor Rope
Well, to be honest, "str1 in str2" means something different than "item1 in list". The former really means "is a substring of"...if it literally meant "is an element of", then only individual chars would be possible to be in the string sequence.

Thermopyle
Jul 1, 2003

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

Stabby McDamage posted:

Well, to be honest, "str1 in str2" means something different than "item1 in list". The former really means "is a substring of"...if it literally meant "is an element of", then only individual chars would be possible to be in the string sequence.

Right. I assumed he meant that syntax didn't work on other sequence types, not that the ... intention or whatever ... didn't work on other sequence types.

I sit on the edge of my seat waiting to find out what he really meant!

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Thermopyle posted:

code:
>>> a_list = ["one", "two", "three"]
>>> "two" in a_list
True
Did I misunderstand what you meant?

Yep, I was thinking
code:
[2,3] in [1,2,3,4]

Reformed Pissboy
Nov 6, 2003

I believe that in general x in y is syntactic sugar for y.__contains__(x). This page has some details.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

FoiledAgain posted:

Hmm. I copied your code and the dictionary and it worked for me. What tripped me up all the time when I started Python is that strip functions don't have side effects.


I got it working! Thanks for the help.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Space Prostitute posted:

I believe that in general x in y is syntactic sugar for y.__contains__(x). This page has some details.

I guess it's just defined differently on strings and lists.

Yakattak
Dec 17, 2009

I am Grumpypuss
>:3

Does anybody have any resources for working with a SOAP based API? Specifically with Python but I know absolutely nothing about SOAP. I am trying to communicate with the FedEx API and it's proving to be very difficult as I know nothing about SOAP.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
Probably the best resources for working with SOAP APIs are a bottle of rye, a revolver and a bullet.

Yakattak
Dec 17, 2009

I am Grumpypuss
>:3

Otto Skorzeny posted:

Probably the best resources for working with SOAP APIs are a bottle of rye, a revolver and a bullet.

This is the conclusion I have come to as well.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
It's not any easier trying to write them and test them with soapui. :suicide:

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

Yakattak posted:

Does anybody have any resources for working with a SOAP based API? Specifically with Python but I know absolutely nothing about SOAP. I am trying to communicate with the FedEx API and it's proving to be very difficult as I know nothing about SOAP.

My work recently released Scio, which I think is a pretty decent SOAP library (relatively speaking). It won't make SOAP any less painful, but it will let you experience that pain more directly, without any other, more pythonic pains getting in the way.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Any recommendations on an elegant way for the following problem:

I have 7 letters, and I want to print out every possible combination of those letters(think Scrabble). I've done this in a long-handed way for 4 letters but with 7 letters it would be a pain to write all that out. Is there a clever way to tackle this?

Stabby McDamage
Dec 11, 2005

Doctor Rope

Hughmoris posted:

Any recommendations on an elegant way for the following problem:

I have 7 letters, and I want to print out every possible combination of those letters(think Scrabble). I've done this in a long-handed way for 4 letters but with 7 letters it would be a pain to write all that out. Is there a clever way to tackle this?

itertools.permutations should do it.

Yakattak
Dec 17, 2009

I am Grumpypuss
>:3

Yeah gently caress it. I found a package for talking with FedEx, just gonna use that instead.

FoiledAgain
May 6, 2007

I'm so confused. This looks like really simple code:

code:
    treelist = list()
    for vectorlist in vectors:
        vectorlist = vectorlist.split(' ')
        for vector in vectorlist:
            if float(vector) <=0.5 and float(vector) >0:
                treelist.append(vector+'\t')
So why is this happening?

code:
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    treeline.append('vector+'\t')
AttributeError: 'NoneType' object has no attribute 'append'
>>>type(treelist)
<type 'NoneType'>
edit: I even put a print type(treelist) after *every* line of code and it spits out <type 'list'> until it hits the error, and then magically turns into NoneType.

FoiledAgain fucked around with this message at 05:32 on Apr 1, 2011

boak
Aug 17, 2010

im gay
is there a simple cross-platform method of determining the height and width of the console with python?

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Thanks to everyone in the thread, I finished my first real project! The program is for scrabble players. It will take any amount of letters, and let you know every possible word that could be used with them. It is ungodly slow if you choose to use all 7 letters but it works. Now I will try to optimize it a little bit and maybe add in a "points" feature.


Stabby McDamage posted:

itertools.permutations should do it.

This is what I needed, thank you.

tripwire
Nov 19, 2004

        ghost flow

boak posted:

is there a simple cross-platform method of determining the height and width of the console with python?

Nope, because consoles are different on different platforms. On windows I doubt that theres anything out of the box thats going to tell you terminal size, but it may be hidden somewhere in pywin32.

On unixey systems you want the tty and/or termios modules.

edit:
code:
def terminal_size():
    import fcntl, termios, struct
    h, w, hp, wp = struct.unpack('HHHH',
        fcntl.ioctl(0, termios.TIOCGWINSZ,
        struct.pack('HHHH', 0, 0, 0, 0)))
    return w, h

tripwire fucked around with this message at 10:44 on Apr 1, 2011

DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah

Yakattak posted:

Does anybody have any resources for working with a SOAP based API? Specifically with Python but I know absolutely nothing about SOAP. I am trying to communicate with the FedEx API and it's proving to be very difficult as I know nothing about SOAP.
You should wash up with some suds.

It's easy as dirt to use, presuming you're using their web service.

EDIT: I read thread good. :downs: Oh well, if anybody's using SOAP web services in the future, I'm pretty sure suds is the easiest way to go.

Met48
Mar 15, 2009

FoiledAgain posted:

I'm so confused. This looks like really simple code:

code:
    treelist = list()
    for vectorlist in vectors:
        vectorlist = vectorlist.split(' ')
        for vector in vectorlist:
            if float(vector) <=0.5 and float(vector) >0:
                treelist.append(vector+'\t')
So why is this happening?

code:
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    treeline.append('vector+'\t')
AttributeError: 'NoneType' object has no attribute 'append'
>>>type(treelist)
<type 'NoneType'>
edit: I even put a print type(treelist) after *every* line of code and it spits out <type 'list'> until it hits the error, and then magically turns into NoneType.

Assuming treeline is a typo in copying the error message, could you post a larger sample? It's not clear what could make treelist be None.

By the way, for convenience you can declare an empty list like this:
code:
treelist = []
And your if statement can be:
code:
if 0.0 < float(vector) <= 0.5:

Stabby McDamage
Dec 11, 2005

Doctor Rope

Hughmoris posted:

Thanks to everyone in the thread, I finished my first real project! The program is for scrabble players. It will take any amount of letters, and let you know every possible word that could be used with them. It is ungodly slow if you choose to use all 7 letters but it works. Now I will try to optimize it a little bit and maybe add in a "points" feature.


This is what I needed, thank you.

That's great. Making it fast could be a tricky problem, but there are some coarse-grained things you could do to get started. First, are you loading your word list in a lookup-friendly way (a dictionary or set)? If your words are in a list, then "x in wordlist" has to walk through the whole list to see if x is there. If it's a dictionary ("wordlist[x]") or a set ("x in wordlist"), it runs a lot faster. (Technically, we say it runs in "constant time"* -- i.e. adding more stuff to the list doesn't make it take proportionally longer.) For this project, you probably want a set, which you can get just by saying "wordlist = set( <<whatever was here before >> )".

If you want to go faster than that, you'll probably need to get into some sophisticated algorithms (or reuse the right piece of existing code).

* I'm assuming python dictionaries and sets use something like extensible hashing to achieve O(1) performance. Let me know if this isn't the case.

king_kilr
May 25, 2007
dicts and sets are both backed by hash tables, if you're interested in the implementation: http://hg.python.org/cpython/file/default/Objects/dictnotes.txt

6174
Dec 4, 2004

Hughmoris posted:

Thanks to everyone in the thread, I finished my first real project! The program is for scrabble players. It will take any amount of letters, and let you know every possible word that could be used with them. It is ungodly slow if you choose to use all 7 letters but it works. Now I will try to optimize it a little bit and maybe add in a "points" feature.

This is probably beyond what kind of thing you are interested in attempting at the moment, but there are two main techniques used to generate a list of possibilities for Scrabble:

  • DAWGs/CDAWGs described in The world’s fastest scrabble program by Andrew W. Appel and Guy J. Jacobson. Commun. ACM, 31:572–578, May 1988.
  • GADDAGs described in A faster scrabble move generation algorithm by Steven A. Gordon. Softw. Pract. Exper., 24:219–232, February 1994.

The DAWGs/CDAWGs are conceptually simpler. GADDAGs run in about half the time of DAWGs/CDAWGs, but are much more complicated. There is also more literature about DAWG/CDAWG generation, but you'd want to start with Appel and Jacobson either way.

You may also be interested in the following paper (which is a nice paper even if you have no intention of making a full-blown AI):

World-championship-caliber scrabble. by Brian Sheppard. Artif. Intell., 134:241–275, January 2002.

These should all be easy to find (particularly with Google Scholar), but if you're having problems, feel free to PM me.

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

I'm reacquainting myself with an old project of mine, and noticing that there's very few method docstrings, and the ones that are there are inconsistent and potentially unclear. Is there a nice clean standard that it'd be good to stick to? I don't necessarily want to go out of my way to support an automated documentation system, but if that's a good idea or easy then I guess I may as well.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Jonnty posted:

I'm reacquainting myself with an old project of mine, and noticing that there's very few method docstrings, and the ones that are there are inconsistent and potentially unclear. Is there a nice clean standard that it'd be good to stick to? I don't necessarily want to go out of my way to support an automated documentation system, but if that's a good idea or easy then I guess I may as well.
Use reStructuredText and Sphinx

bbq
Jan 21, 2006

get funky.
If someone has time, I would love to get some feedback on the tetris clone I wrote. Style, program structure, anything would be great.

https://github.com/mcmt/disappeartheblocks

FoiledAgain
May 6, 2007

n/m

Profane Obituary!
May 19, 2009

This Motherfucker is Dead
Question for a program i'm writing. For what basically amounts to a web spider that needs to be able to spider multiple sites at a time would it be better to use something event based like Twisted, Circuits or Gevent, MultiProcessing or threads?

Adbot
ADBOT LOVES YOU

Lurchington
Jan 2, 2003

Forums Dragoon
I used something similar to gevent (eventlet) and it seemed like the best approach compared to threads/processe/twisted. Never used circuits.

  • Locked thread