Two Plus Two Poker Forums

Two Plus Two Poker Forums (https://forumserver.twoplustwo.com/)
-   Computer and Technical Help (https://forumserver.twoplustwo.com/48/computer-technical-help/)
-   -   ** Python Support Thread ** (https://forumserver.twoplustwo.com/48/computer-technical-help/python-support-thread-1007515/)

JackHighFlop 03-12-2012 08:15 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by Xhad (Post 32020026)
you mean parameters[1] is a string and you want to call a function by that name? Sounds like something that could be done with eval:

Code:

instring = 'f'
print eval(instring)(1,2,3)


If I try this, I get that an error that says name 'f' is not defined.

Yes, parameters is a string and I want to call a function by that name. If I use eval, it either says the syntax is wrong or the there is no module eval defined. :\

Xhad 03-12-2012 08:17 PM

Re: ** Python Support Thread **
 
sorry, I forgot to mention I was using the same 'f' from last post. Try this in an interpreter:

Code:

eval('sum')((1,2,3))

JackHighFlop 03-12-2012 08:22 PM

Re: ** Python Support Thread **
 
Why is this syntax invalid then? :\

scipy.stats.(eval('norm')).rvs()

which is normally

scipy.stats.norm.rvs()

Edit: Is it because 'norm' is not directly a function after all?

Xhad 03-12-2012 08:30 PM

Re: ** Python Support Thread **
 
Hrm. Yeah, that exact syntax won't work. This does work, however:

Code:

scipy.stats.__dict__['norm'].rvs()
Another possible option is to build the entire string 'scipy.stats.norm.rvs()' somehow and then eval the entire thing.

JackHighFlop 03-12-2012 08:37 PM

Re: ** Python Support Thread **
 
Thanks. Both method works.

Colombo 03-21-2012 09:26 PM

Re: ** Python Support Thread **
 
Is there a quick way to join two files on a common field(s)? Similar to UNIX join.

Only way I'm aware of is to use a nested loop, which I'd rather avoid.

kerowo 03-21-2012 09:51 PM

Re: ** Python Support Thread **
 
There is a way to run unix commands in a script.

Xhad 03-21-2012 10:05 PM

Re: ** Python Support Thread **
 
Is this what you mean? Or something else?

http://docs.python.org/library/os.html#os.system

kerowo 03-21-2012 10:08 PM

Re: ** Python Support Thread **
 
Probably, I'm on the couch with my iPad so didn't do more than give a Google trail to start on...

Colombo 03-22-2012 08:56 AM

Re: ** Python Support Thread **
 
os.system or subprocess will definitely be my last resort, but I'm hoping to use something that is purely python

Neko 03-22-2012 05:28 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by Colombo (Post 32186387)
Is there a quick way to join two files on a common field(s)? Similar to UNIX join.

Only way I'm aware of is to use a nested loop, which I'd rather avoid.

Read the files into two in memory sqlite database tables and then run an SQL query?

edit: pandas (nice library for handling data sets) can do this to I think.

kerowo 03-22-2012 05:31 PM

Re: ** Python Support Thread **
 
Couldn't you just read them in one at a time and write them to the same output file?

Colombo 03-22-2012 06:10 PM

Quote:

Originally Posted by Neko (Post 32199908)
Read the files into two in memory sqlite database tables and then run an SQL query?

edit: pandas (nice library for handling data sets) can do this to I think.

Interesting. Ill check your recommendations out. I was going to use postgresql for this but I'm having a very difficult time installing it and getting it to work properly (OS X Lion). Is sqlite free?

Thanks

Colombo 03-22-2012 06:26 PM

Quote:

Originally Posted by kerowo (Post 32199974)
Couldn't you just read them in one at a time and write them to the same output file?

But to execute the join, wouldnt i need a nested loop? E.g.:
Code:

for line in open("file1):
    cols = line.split()
    uid = cols[0]
    for line in open("file2"):
        # compare uid to this uid
        # write all columns if uid matches


Neko 03-22-2012 06:29 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by Colombo (Post 32200677)
Is sqlite free?

Thanks

Code:

import sqlite3
std lib man :)

http://stackoverflow.com/questions/2...e-using-python

Colombo 03-22-2012 06:37 PM

Quote:

Originally Posted by Neko (Post 32201051)
Code:

import sqlite3
std lib man :)

http://stackoverflow.com/questions/2...e-using-python

Wow really good stuff. This module is gonna save me so much time. Should be good to go from here - thanks!

EvilSteve 04-13-2012 12:22 AM

Re: ** Python Support Thread **
 
For making very colorful forum posts.

Code:

import random

#0 <= n < 256
def hexstring(n):
    if n < 0 or n > 255:
        return 'XX'
    hdigits = '0123456789ABCDEF'
    l = n / 16
    r = n % 16
    return hdigits[l] + hdigits[r]

def randColor():
    r = random.randint(0,255)
    b = random.randint(0,255)
    g = random.randint(0,255)
    return hexstring(r) + hexstring(b) + hexstring(g)

def colorize(s):
    t = ''
    for letter in s:
        if letter == ' ':
            t += ' '
        else:
            t += ('[color=#' + randColor() + ']' + letter + '[/color]')
    return t

source = "For making very colorful forum posts."
print colorize(source)


gregGGhehe 04-15-2012 02:12 PM

Re: ** Python Support Thread **
 
I'm trying to learn some python and I wanted for python to interact with excel using the win32 module. I found a basic example online on wiki.(http://www.pha.com.au/kb/index.php/Python_COM_Examples)

It won't work however. This is the error I get.

Traceback (most recent call last): File "C:/Users/Greg/Desktop/python programming/excel2.py", line 8, in sheet.Range("A2").Value = str(Application.SIFilter(None, c.siObjectFilter)) NameError: name 'Application' is not defined

My question is what does that line DO EXACTLY and why am I getting an error?

sheet.Range("A2").Value = str(Application.SIFilter(None, c.siObjectFilter))



import win32com.client
from win32com.client import constants as c

excel = win32com.client.Dispatch("Excel.Application")
book = excel.Workbooks.Add()
sheet = book.Worksheets(1)
sheet.Range("A1").Value = "Hello World!"
sheet.Range("A2").Value = str(Application.SIFilter(None, c.siObjectFilter))
book.SaveAs("c:\simple_example.xls")

sheet = None
book = None
excel.Quit()
excel = None

Thanks I feel like such a noob, but alas I am a huge noob.

TheIrishThug 04-15-2012 03:50 PM

Re: ** Python Support Thread **
 
Application is has not been defined as anything. I suspect that Application is a class within the library and you need to explicitly import it like you did with constants.

windwakeroftime 06-06-2012 02:45 AM

Re: ** Python Support Thread **
 
Hi, I just started taking the MIT open courseware class online today, and the second assignment has already got me stumped.

I know how to tell if a number is odd or not, but how do I generate it as a candidate?

[Hints would be nice - but please not the flat out answer, I want to try and figure a little out for myself]

Thanks in advance!

daveT 06-06-2012 03:50 AM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by windwakeroftime (Post 33142673)
Hi, I just started taking the MIT open courseware class online today, and the second assignment has already got me stumped.

I know how to tell if a number is odd or not, but how do I generate it as a candidate?

[Hints would be nice - but please not the flat out answer, I want to try and figure a little out for myself]

Thanks in advance!

I did this class. You're way over-thinking this part of the problem.

Think about this step: You simply want to print a column of numbers that are odd, right?

1
3
5
7
9
11
...

Think about the unique feature of integers that would make generating that list easy.

You have the following steps:

initialize a variable. What variable would allow you to take advantage of the unique feature alluded to?

generate the list: What would you do to that initial variable that reflects the unique feature alluded to?

See how I wrote down what you wanted to do? That class says using a pencil and paper is an important step in programming and I absolutely agree with that assertion. Writing down the output you want to see will help you see things that you may not have noticed before.

If you really do give up, then use the spoiler tag:

Try to fill in the blanks:

Spoiler:
Code:


x = ?

while x < 100:
    print x
    {{ What are you going to do to X?? }}}


riverfish1 06-06-2012 10:11 AM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by windwakeroftime (Post 33142673)
Hi, I just started taking the MIT open courseware class online today, and the second assignment has already got me stumped.

I know how to tell if a number is odd or not, but how do I generate it as a candidate?

[Hints would be nice - but please not the flat out answer, I want to try and figure a little out for myself]

Thanks in advance!

A little offtopic, but I'd recommend Udacity over MIT Opencourseware for intro to Python any day. It starts off a little slower, but is far more suited to online learning, and I think reinforces concepts much much better. Plus if you get stumped there are forums for discussion, and office hours from the instructors, which isn't the case for MIT.

I took a few classes of the intro to comp sci course at MIT, and at times the homework was mildly frustrating as I didn't fully grasp the concepts presented in class (this may have been because my schedule rarely allows me to sit down for the full 40-60 minutes for lecture - so I watched it in bits and pieces). I've only gone through 3 units of the Intro class on Udacity, but it's been much more enjoyable and I feel I understand the concepts far better than with MIT.

Jbrochu 06-06-2012 10:54 AM

Re: ** Python Support Thread **
 
I'm on the 3rd unit of the Web Engineering class at Udacity (which uses Python for the server side) and that class is also excellent so far.

tyler_cracker 06-06-2012 12:37 PM

Re: ** Python Support Thread **
 
i haven't taken either class but be aware of the difference between learning python/"web engineering" and computer science.

riverfish1 06-06-2012 04:36 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by tyler_cracker (Post 33147237)
i haven't taken either class but be aware of the difference between learning python/"web engineering" and computer science.

Udacity offers an intro to comp sci course that uses Python as its base, which is the class I was referring to. I will say its more focused on writing Python code than MIT (which from what I saw, did have a little more of a broader overall comp sci approach).


All times are GMT -4. The time now is 06:18 PM.

Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.

Copyright © 2008-2020, Two Plus Two Interactive