Open Side Menu Go to the Top

03-12-2012 , 08:15 PM
Quote:
Originally Posted by Xhad
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. :\
** Python Support Thread ** Quote
** Python Support Thread **
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** Python Support Thread **
03-12-2012 , 08:17 PM
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))
** Python Support Thread ** Quote
03-12-2012 , 08:22 PM
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?
** Python Support Thread ** Quote
03-12-2012 , 08:30 PM
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.
** Python Support Thread ** Quote
03-12-2012 , 08:37 PM
Thanks. Both method works.
** Python Support Thread ** Quote
03-21-2012 , 09:26 PM
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.
** Python Support Thread ** Quote
03-21-2012 , 09:51 PM
There is a way to run unix commands in a script.
** Python Support Thread ** Quote
03-21-2012 , 10:05 PM
Is this what you mean? Or something else?

http://docs.python.org/library/os.html#os.system
** Python Support Thread ** Quote
03-21-2012 , 10:08 PM
Probably, I'm on the couch with my iPad so didn't do more than give a Google trail to start on...
** Python Support Thread ** Quote
03-22-2012 , 08:56 AM
os.system or subprocess will definitely be my last resort, but I'm hoping to use something that is purely python

Last edited by Colombo; 03-22-2012 at 09:02 AM.
** Python Support Thread ** Quote
03-22-2012 , 05:28 PM
Quote:
Originally Posted by Colombo
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.
** Python Support Thread ** Quote
03-22-2012 , 05:31 PM
Couldn't you just read them in one at a time and write them to the same output file?
** Python Support Thread ** Quote
03-22-2012 , 06:10 PM
Quote:
Originally Posted by Neko
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

Last edited by Colombo; 03-22-2012 at 06:27 PM.
** Python Support Thread ** Quote
03-22-2012 , 06:26 PM
Quote:
Originally Posted by kerowo
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

Last edited by Colombo; 03-22-2012 at 06:33 PM.
** Python Support Thread ** Quote
03-22-2012 , 06:29 PM
Quote:
Originally Posted by Colombo
Is sqlite free?

Thanks
Code:
import sqlite3
std lib man

http://stackoverflow.com/questions/2...e-using-python
** Python Support Thread ** Quote
03-22-2012 , 06:37 PM
Quote:
Originally Posted by Neko
Wow really good stuff. This module is gonna save me so much time. Should be good to go from here - thanks!
** Python Support Thread ** Quote
04-13-2012 , 12:22 AM
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)
** Python Support Thread ** Quote
04-15-2012 , 02:12 PM
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.
** Python Support Thread ** Quote
04-15-2012 , 03:50 PM
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.
** Python Support Thread ** Quote
06-06-2012 , 02:45 AM
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!
** Python Support Thread ** Quote
06-06-2012 , 03:50 AM
Quote:
Originally Posted by windwakeroftime
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?? }}}

Last edited by daveT; 06-06-2012 at 03:59 AM.
** Python Support Thread ** Quote
06-06-2012 , 10:11 AM
Quote:
Originally Posted by windwakeroftime
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.
** Python Support Thread ** Quote
06-06-2012 , 10:54 AM
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.
** Python Support Thread ** Quote
06-06-2012 , 12:37 PM
i haven't taken either class but be aware of the difference between learning python/"web engineering" and computer science.
** Python Support Thread ** Quote
06-06-2012 , 04:36 PM
Quote:
Originally Posted by tyler_cracker
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).
** Python Support Thread ** Quote
** Python Support Thread **
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** Python Support Thread **

      
m