Open Side Menu Go to the Top
Register
** Python Support Thread ** ** Python Support Thread **

06-13-2011 , 11:58 AM
Quote:
Originally Posted by jjshabado
Do people feel the list comprehension is better than using filter here? Seems weird to me.
Code:
filter(lambda x: type(x) is int, list)
v
Code:
[x for x in list if type(x) is int]
I use the second syntax generally. List comprehension is a bit more flexible imho. Since i started working on the Euler problems i've learned quite a few things about python, so i'm not a great advocate for style.
** Python Support Thread ** Quote
06-13-2011 , 12:02 PM
The general "pythonic" way is to use list comprehension. Lots of people that have not been exposed to functional programming don't know about things like filter, map, etc.
** Python Support Thread ** Quote
06-13-2011 , 12:04 PM
What about:

Quote:
filter(is_convertible, list)
v

Quote:
[x for x in list if is_convertible(x)]
Dunno. I guess its not a big difference either way.

I mean, obviously list comprehensions are more flexible and should be used in lots of places. I'm just wondering specifically using list comprehensions for filtering.
** Python Support Thread ** Quote
06-13-2011 , 12:05 PM
Quote:
Originally Posted by TheIrishThug
The general "pythonic" way is to use list comprehension. Lots of people that have not been exposed to functional programming don't know about things like filter, map, etc.
Seems weird to call yourself a python person and not know about these functional concepts.

Edit: I feel like I'm coming across kind of belligerent here. I'm not, just curious about people's opinions.
** Python Support Thread ** Quote
06-13-2011 , 12:26 PM
i don't think of python as being particularly functional. buh-dum-chhh.

no but really, most of the python code i encounter is very procedural or OO. don't see a lot of functional programming (although your filter example is obviously more common than, i dunno, passing function pointers around like that C->Python guy up there was trying to do ).

edit: and fwiw i think sorrow's version is quite easy to read. i actually think i'd have more trouble writing it!
** Python Support Thread ** Quote
06-13-2011 , 12:32 PM
I used to program in Lisp so I am well used to map, reduce, filter, lambda, but I know they are considered "unpythonic" because they were originally scheduled to be removed in Python 3. (In the end, only reduce was removed).

jjshabado and sorrow -- none of your examples work as I believe they are expected to. They all fail to cast strings or discard zeros. Try them on a test case like

Code:
li = [1, 2, "123", "ad", 4.5, "b2", "3fdf", [1, 2], 0, "0"]
result = your_function(li)
assert result == [1, 2, 123, 4, 0, 0]
** Python Support Thread ** Quote
06-13-2011 , 12:44 PM
Peter Norvig's spelling corrector is a nice demonstration of how to use list comprehensions -- or in my case, a demonstration of how weak and feeble my own code is relative to Peter Norvig's - http://norvig.com/spell-correct.html.

Code:
import re, collections

def words(text): return re.findall('[a-z]+', text.lower()) 

def train(features):
    model = collections.defaultdict(lambda: 1)
    for f in features:
        model[f] += 1
    return model

NWORDS = train(words(file('big.txt').read()))

alphabet = 'abcdefghijklmnopqrstuvwxyz'

def edits1(word):
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
   inserts    = [a + c + b     for a, b in splits for c in alphabet]
   return set(deletes + transposes + replaces + inserts)

def known_edits2(word):
    return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)

def known(words): return set(w for w in words if w in NWORDS)

def correct(word):
    candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
    return max(candidates, key=NWORDS.get)
** Python Support Thread ** Quote
06-13-2011 , 12:58 PM
Quote:
Originally Posted by tyler_cracker
most of the python code i encounter is very procedural or OO.
Quote:
Originally Posted by RoundTower
I used to program in Lisp so I am well used to map, reduce, filter, lambda, but I know they are considered "unpythonic" because they were originally scheduled to be removed in Python 3.
Exactly, there is a big difference between Python and Lisp.
** Python Support Thread ** Quote
06-13-2011 , 01:14 PM
A working hybrid version is
Code:
filter(lambda x: x is not None, [convertable(el) for el in li])
it's annoying that you have to use a lambda function there, but you can't be any more general and write filter(None, ...) because 0 evaluates to False in Python.
** Python Support Thread ** Quote
06-13-2011 , 04:23 PM
btw since i ignited this firestorm, here's the code i ended up committing. not as elegant as some of the other examples, but it's been running in production all the while you guys were trying to figure out the how to make it elegantest :

Code:
# Attempt to cast all entries to integers. This both insures proper numeric
# (rather than lexical) sorting and throws out non-numeric, and therefore
# invalid, build dirs.
#
# Have to iterate over a copy of build_dirs because otherwise Python gets
# confused when we delete list elements.
build_dirs_copy = copy.deepcopy(build_dirs)
for dir in build_dirs_copy:
    try:
        dir_i = int(dir)
    except ValueError:
        print "WARNING: Ignoring non-numeric build dir %s" % dir
        build_dirs.remove(dir)
    else:
        build_dirs[build_dirs.index(dir)] = dir_i
build_dirs.sort()
i should just build up a new list from scratch instead of deepcopy() and then remove(), but whatevs, next problem please.
** Python Support Thread ** Quote
06-13-2011 , 05:12 PM
Quote:
Originally Posted by Neuge
Ok then. I've used the C API a lot before, and it's basically coding in C with a little gymnastics to pass values between C and python. I'm doing some project euler now to brush up on those skills now while I'm unemployed.

*snip

It's obviously the wrong type, but I don't get how you're supposed to call a function without arguments.
If anyone cares, I did solve this problem. I forgot about letting the API handle this and called it as a normal C function. Just have to build the PyObject structures for the arguments first.
** Python Support Thread ** Quote
06-14-2011 , 04:44 PM
Posted in the wrong thread at first, crossposting from LC

Kind of a noob question, but I'm pretty stumped here although it is probably simple.

I am trying to combine certain lines of xml files I have into one ordered document, and trying to search through the file with regular expressions.
Lines I'm searching for look like this or similar:
000101101000101101 -> 000101101110101101
However, I'm always looking for the second binary string as a given, and just want to also find the first string. I store the second string I'm looking for in a string, so something like:
string='000101101000101101 -> 000101101110101101'
want='001001101110101101'

What I then try to do is re.search(r'([01]+).->.'(want), string) but that isn't working. The problem here is that I want to search for that specific binary string that I would like to store in the variable, ánd that I want to store both strings in a tuple group so I can call them later for other stuff.

Any idea how I can fix this?
** Python Support Thread ** Quote
06-14-2011 , 07:32 PM
1. don't parse well-known formats with regular expressions. use a library that knows how to parse it. there are about a million and a half of them for xml.

this will seem like a pain because you have to learn the api and debug it and blah blah, but if you're doing anything other than reeeeeeeeally trivial operations, you will save yourself time and heartache by just doing it right the first time.

2. if i understand correctly, i think you're looking for re.match().
** Python Support Thread ** Quote
06-15-2011 , 02:29 AM
I was actually looking for re.findall, but the problem I was having was searching for a string and the string content of a variable at the same time. Each one on its own is working for me but I can't seem to combine them into one search?

And they are quite trivial operations, but it might be better to learn a library anyway if I ever have to do anything like this in the future. Recommendations, or is any of the 1.5M good?
** Python Support Thread ** Quote
06-15-2011 , 03:10 AM
Quote:
Originally Posted by Slick Strawberry
Posted in the wrong thread at first, crossposting from LC

Kind of a noob question, but I'm pretty stumped here although it is probably simple.

I am trying to combine certain lines of xml files I have into one ordered document, and trying to search through the file with regular expressions.
Lines I'm searching for look like this or similar:
000101101000101101 -> 000101101110101101
However, I'm always looking for the second binary string as a given, and just want to also find the first string. I store the second string I'm looking for in a string, so something like:
string='000101101000101101 -> 000101101110101101'
want='001001101110101101'

What I then try to do is re.search(r'([01]+).->.'(want), string) but that isn't working. The problem here is that I want to search for that specific binary string that I would like to store in the variable, ánd that I want to store both strings in a tuple group so I can call them later for other stuff.

Any idea how I can fix this?
Code:
import re
string='000101101000101101 -> 000101101110101101'
rexx = r'([01]+)\s->\s([01]+)'
m = re.match(rexx, string)
print m.groups()
print type(m.groups()

$ python blah.py 
('000101101000101101', '000101101110101101')
<type 'tuple'>
That sort of what you are looking for?
** Python Support Thread ** Quote
06-15-2011 , 04:21 AM
That sort of is what I was looking for, and helped me on the way to fix it. Since the second string I wanted to find was set beforehand, I had a problem putting it directly into the regex. However, if I use your method I can fix that. Here's how I did it in the end:

Code:
string='000101101000101101 -> 000101101110101101'
ideal='000101101110101101'
rexx = r'([01]+)\s->\s('+str(ideal)+')'
m=re.match(rexx,string)
That allows me to call each binary string separately with m.groups()[1] for instance, which is what I wanted. Now all I have to do is translate it to re.findall and I'm probably done, thanks
** Python Support Thread ** Quote
06-15-2011 , 05:08 AM
Quote:
Originally Posted by Slick Strawberry
That allows me to call each binary string separately with m.groups()[1] for instance, which is what I wanted. Now all I have to do is translate it to re.findall and I'm probably done, thanks
re.finditer() is damn useful too.

Code:
import re
string="""
000101101000101101 -> 000101101110101101
000101101000101101 -> 000101101110101101
000101101000101101 -> 000101101110101101
000101101000101101 -> 000101101110101101
000101101000101101 -> 000101101110101101
"""
ideal='000101101110101101'
rexx = r'([01]+)\s->\s('+str(ideal)+')'

for m in re.finditer(rexx, string):
    print m.groups()
** Python Support Thread ** Quote
06-15-2011 , 08:15 AM
Quote:
Originally Posted by RoundTower
I used to program in Lisp so I am well used to map, reduce, filter, lambda, but I know they are considered "unpythonic" because they were originally scheduled to be removed in Python 3. (In the end, only reduce was removed).
Cool, I didn't know that.

Maybe the lack of reduce is why Python 3 is having such a slow adoption rate...
** Python Support Thread ** Quote
06-15-2011 , 11:58 AM
Hi guys,

I decided to use pypoker-eval-138.0 found here:
http://download.gna.org/pokersource/sources/
(very bottom of the page)

I'm new to python. I downloaded it, opened IDLE, loaded the pokereval.py code in the script window, and pressed F5, but in the python shell I get an error:

Traceback (most recent call last):
File "C:\Users\[my name]\Desktop\pypoker-eval-138.0\pypoker-eval-138.0\pokereval.py", line 29, in <module>
_pokereval = __import__('_pokereval_' + sys.version[0] + '_' + sys.version[2])
ImportError: No module named _pokereval_3_2

Thanks.
** Python Support Thread ** Quote
06-15-2011 , 12:22 PM
If you're new to python then you'd best learn python before trying to understand a big lump of python sources.
** Python Support Thread ** Quote
06-15-2011 , 12:24 PM
Quote:
Originally Posted by :::grimReaper:::
Hi guys,

I decided to use pypoker-eval-138.0 found here:
http://download.gna.org/pokersource/sources/
(very bottom of the page)

I'm new to python. I downloaded it, opened IDLE, loaded the pokereval.py code in the script window, and pressed F5, but in the python shell I get an error:

Traceback (most recent call last):
File "C:\Users\[my name]\Desktop\pypoker-eval-138.0\pypoker-eval-138.0\pokereval.py", line 29, in <module>
_pokereval = __import__('_pokereval_' + sys.version[0] + '_' + sys.version[2])
ImportError: No module named _pokereval_3_2

Thanks.
You're getting this particular error here because your python can't find the compiled C poker-eval libraries on your computer. pypokereval is just a wrapper for poker-eval so it needs to be installed and added to the system path as well.

That being said, pypokereval is notoriously difficult to get working on Windows. I believe gimick from the fpdb project was able to get it working once. Sorrow is another good person to ask about this. GL
** Python Support Thread ** Quote
06-15-2011 , 12:48 PM
Interesting. So in other words I shouldn't try getting it to work?

Where else can I get a poker simulator/calculator?
Can I tell VBA or Java to run PokerStove, then I can parse the text from pokerstove.txt?

I know I can get my calculations off pro poker tools, but I want around 50,000 calculations just for one of my calculations. Is it reasonable to have my program go to their site 50,000 times at once? I'm not even sure how long this would take.
** Python Support Thread ** Quote
06-15-2011 , 12:54 PM
Quote:
Originally Posted by :::grimReaper:::
Interesting. So in other words I shouldn't try getting it to work?

Where else can I get a poker simulator/calculator?
Can I tell VBA or Java to run PokerStove, then I can parse the text from pokerstove.txt?

I know I can get my calculations off pro poker tools, but I want around 50,000 calculations just for one of my calculations. Is it reasonable to have my program go to their site 50,000 times at once? I'm not even sure how long this would take.
This thread has source code for a number of good evaluators written in Java, C, C++, etc. http://pokerbots.org/pf3/viewtopic.p...st=0&sk=t&sd=a
** Python Support Thread ** Quote
06-19-2011 , 09:23 PM
So....

I'm working on the mitocw courses. Using spoilers because I don't want to give out the answer to googleBots:

Spoiler:
Most of this stuff, I was able to figure out on my own. Commented for your ease of readability and my thoughts (with no cursing).

addition: Oops, all that commenting makes the code unreadable, so there are now two code tags. The first has all my thoughts (and quick cliffs after the block), and the second is the raw code.


Code:
## find the 1000th prime, which is 7919.
## This particular answer creates a full list.
## I could easily have it print out only 7919,
## but watching the numbers accumulate is like
## watching your favorite horse at the races:
##the anticipation is the fun part, IMO. :p

## The first part is logical.
## Initialize some variables and have fun.
##At first, I was using import math and using
##int(math.sqrt(pNum)
## to create a top range number. I also externalized:

##pCount = 2
##div = int(math.sqrt(pNum))
##......
##for i in range(2,div+1):

##I had issues with this one,
##and I am sure if I wasn't sick of doing this particular problem,
##I could fix it. The issue became that I could not top out the
##counter variable to 1000.
##More specifically,
## I was getting funky answers when the
## pCount variable was set at 1000.
## Regardless, there is no need to look like a super-hero
## and use the sqrt upper-limit,
## so using pNum/2 works just as well.
## I figure it wasn't as bad as using pNum
## as the upper limit: That is just too amateur for my taste.
                                            
pCount = 2

pNum = 5
while pCount < 1000:
##    It doesn't take a genius, knowing my history with programming,
##    to figure out that I have no idea why I would want to initialize
##    "test" inside of the function opposed to outside the function.
##    Regardless, this is good to know.
##    You would be shocked to know that I figured out the
##    boolean with some hard thinking.
##    I began with externalizing "test", but that didn't work
    test = True
    for i in range(2, int(pNum/2)):
        if pNum%i == 0:
            test = False
            break

##    the idea of the assignment is to figure out "flow Control"
##    or in the words of
##    the professor "introduce you to flow control."
##    Finding out to un-nest the if
##    to prevent fall-through was an amazing find
##    and probably the entire point
##    of the project.

    if test == True:
        pCount += 1
        print pNum

##        This part probably took more time to figure out than it should have
##        (all of five minutes while picking my nose).
##        At first, i commented out this region and saw a bunch of
##        '5' going down the Shell. 


##    if pCount == 1000:
##        break
##    else:
##        pNum += 1

    ## But all hope wasn't lost:
    pNum += 1
Cliffs for above: I didn't figure out why 'test' has to be inside of the while block. The ultimate revelation was learning how to un-nest if statements to prevent full code fall-through.

The unabridged version:

Code:
pCount = 2
pNum = 5

while pCount < 1000:
    test = True
    for i in range(2, int(pNum/2)):
        if pNum%i == 0:
            test = False
            break

    if test == True:
        pCount += 1
        print pNum

    pNum += 1
** Python Support Thread ** Quote
06-19-2011 , 09:45 PM
Since test is already a boolean, you don't need to compare it to True. Python, like many scripting languages, is liberal with will evaluate to true in an if clause. However, I tend to only let booleans auto evaluate for the sake of readability.

Also, loops can have an else block that executes when the loop runs to completion. I've never actually used it, but it is exactly what you are testing for.
** Python Support Thread ** Quote

      
m