Two Plus Two Publishing LLC Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > Other Topics > Programming

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 06-13-2011, 11:58 AM   #76
old hand
 
sorrow's Avatar
 
Join Date: Apr 2008
Location: Perth, Western Australia
Posts: 1,531
Re: ** Python Support Thread **

Quote:
Originally Posted by jjshabado View Post
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.
sorrow is offline   Reply With Quote
Old 06-13-2011, 12:02 PM   #77
Pooh-Bah
 
TheIrishThug's Avatar
 
Join Date: Jan 2005
Location: Belligerent and numerous
Posts: 5,223
Re: ** Python Support Thread **

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.
TheIrishThug is offline   Reply With Quote
Old 06-13-2011, 12:04 PM   #78
Carpal Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 20,077
Re: ** Python Support Thread **

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.
jjshabado is offline   Reply With Quote
Old 06-13-2011, 12:05 PM   #79
Carpal Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 20,077
Re: ** Python Support Thread **

Quote:
Originally Posted by TheIrishThug View Post
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.
jjshabado is offline   Reply With Quote
Old 06-13-2011, 12:26 PM   #80
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 15,800
Re: ** Python Support Thread **

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!
tyler_cracker is offline   Reply With Quote
Old 06-13-2011, 12:32 PM   #81
ɹǝʍoʇpunoɹ
 
RoundTower's Avatar
 
Join Date: Feb 2005
Location: soah made my profile
Posts: 14,542
Re: ** Python Support Thread **

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]
RoundTower is offline   Reply With Quote
Old 06-13-2011, 12:44 PM   #82
adept
 
ChazDazzle's Avatar
 
Join Date: Aug 2005
Location: Casino Night Zone
Posts: 749
Re: ** Python Support Thread **

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)
ChazDazzle is offline   Reply With Quote
Old 06-13-2011, 12:58 PM   #83
Pooh-Bah
 
TheIrishThug's Avatar
 
Join Date: Jan 2005
Location: Belligerent and numerous
Posts: 5,223
Re: ** Python Support Thread **

Quote:
Originally Posted by tyler_cracker View Post
most of the python code i encounter is very procedural or OO.
Quote:
Originally Posted by RoundTower View Post
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.
TheIrishThug is offline   Reply With Quote
Old 06-13-2011, 01:14 PM   #84
ɹǝʍoʇpunoɹ
 
RoundTower's Avatar
 
Join Date: Feb 2005
Location: soah made my profile
Posts: 14,542
Re: ** Python Support Thread **

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.
RoundTower is offline   Reply With Quote
Old 06-13-2011, 04:23 PM   #85
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 15,800
Re: ** Python Support Thread **

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.
tyler_cracker is offline   Reply With Quote
Old 06-13-2011, 05:12 PM   #86
veteran
 
Join Date: Jul 2004
Posts: 2,414
Re: ** Python Support Thread **

Quote:
Originally Posted by Neuge View Post
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.
Neuge is offline   Reply With Quote
Old 06-14-2011, 04:44 PM   #87
journeyman
 
Slick Strawberry's Avatar
 
Join Date: Nov 2010
Posts: 349
Re: ** Python Support Thread **

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?
Slick Strawberry is offline   Reply With Quote
Old 06-14-2011, 07:32 PM   #88
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 15,800
Re: ** Python Support Thread **

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().
tyler_cracker is offline   Reply With Quote
Old 06-15-2011, 02:29 AM   #89
journeyman
 
Slick Strawberry's Avatar
 
Join Date: Nov 2010
Posts: 349
Re: ** Python Support Thread **

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?
Slick Strawberry is offline   Reply With Quote
Old 06-15-2011, 03:10 AM   #90
old hand
 
sorrow's Avatar
 
Join Date: Apr 2008
Location: Perth, Western Australia
Posts: 1,531
Re: ** Python Support Thread **

Quote:
Originally Posted by Slick Strawberry View Post
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?
sorrow is offline   Reply With Quote
Old 06-15-2011, 04:21 AM   #91
journeyman
 
Slick Strawberry's Avatar
 
Join Date: Nov 2010
Posts: 349
Re: ** Python Support Thread **

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
Slick Strawberry is offline   Reply With Quote
Old 06-15-2011, 05:08 AM   #92
old hand
 
sorrow's Avatar
 
Join Date: Apr 2008
Location: Perth, Western Australia
Posts: 1,531
Re: ** Python Support Thread **

Quote:
Originally Posted by Slick Strawberry View Post
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()
sorrow is offline   Reply With Quote
Old 06-15-2011, 08:15 AM   #93
Carpal Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 20,077
Re: ** Python Support Thread **

Quote:
Originally Posted by RoundTower View Post
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...
jjshabado is offline   Reply With Quote
Old 06-15-2011, 11:58 AM   #94
old hand
 
:::grimReaper:::'s Avatar
 
Join Date: Jul 2010
Posts: 1,867
Re: ** Python Support Thread **

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.
:::grimReaper::: is online now   Reply With Quote
Old 06-15-2011, 12:22 PM   #95
King of the sidebar
 
Neil S's Avatar
 
Join Date: Sep 2004
Location: Northern Virginia
Posts: 16,585
Re: ** Python Support Thread **

If you're new to python then you'd best learn python before trying to understand a big lump of python sources.
Neil S is offline   Reply With Quote
Old 06-15-2011, 12:24 PM   #96
adept
 
ChazDazzle's Avatar
 
Join Date: Aug 2005
Location: Casino Night Zone
Posts: 749
Re: ** Python Support Thread **

Quote:
Originally Posted by :::grimReaper::: View Post
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
ChazDazzle is offline   Reply With Quote
Old 06-15-2011, 12:48 PM   #97
old hand
 
:::grimReaper:::'s Avatar
 
Join Date: Jul 2010
Posts: 1,867
Re: ** Python Support Thread **

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.
:::grimReaper::: is online now   Reply With Quote
Old 06-15-2011, 12:54 PM   #98
adept
 
ChazDazzle's Avatar
 
Join Date: Aug 2005
Location: Casino Night Zone
Posts: 749
Re: ** Python Support Thread **

Quote:
Originally Posted by :::grimReaper::: View Post
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
ChazDazzle is offline   Reply With Quote
Old 06-19-2011, 09:23 PM   #99
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 20,422
Re: ** Python Support Thread **

So....

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

Spoiler:
daveT is offline   Reply With Quote
Old 06-19-2011, 09:45 PM   #100
Pooh-Bah
 
TheIrishThug's Avatar
 
Join Date: Jan 2005
Location: Belligerent and numerous
Posts: 5,223
Re: ** Python Support Thread **

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.
TheIrishThug is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -4. The time now is 12:51 PM.


Powered by vBulletin®
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.
Copyright © 2008-2010, Two Plus Two Interactive
 
 
Poker Players - Streaming Live Online