[Tutor] Open a WebPage with Forms Authentication

2010-03-31 Thread GoodPotatoes
I would like to sign onto a web page, but am having a difficult time determine 
what parameters to pass in my sign in.  For a simple test, I am trying to parse 
text on a "friends" status page on twitter.  This is not via the API, but the 
web.

import urllib2, urllib, cookielib
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
d={'authenticity_token':'8578471445acd12dec39b3353b71c88969f1505e', 
'username_or_email':'user','password':'pswd123'}
params = urllib.urlencode(d)
opener.open('http://twitter.com/login', params)

if not 'id' in [cookie.name for cookie in cj]:
raise ValueError, "Login failed"

#   result
Traceback (most recent call last):
  File "", line 2, in 
raise ValueError, "Login failed"
ValueError: Login failed

How can I tell what the page is asking for, and if I am passing the correct 
parameters?



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] From SQL Blobs to Python Buffers to Actual Files

2010-06-03 Thread GoodPotatoes
Environment:
Sybase/ODBC/Windows/Python2.5

I have been given a legacy database, and need to read the binaryfiles out to a 
disk.  The table has columns "filename" and "binaryFile", where the binaryFile 
is a BLOB

My python script so far is:

import pyodbc
cnxn=pyodbc.Connection("DSN=sybasedatabase")
cursor=cnxn.cursor()

p=cursor.execute("select top 1 * from FOO..Table").fetchone()

#p contains ('UsersOldFile.rtf', )

#I tried to write this out to the disk as:
myfile=open(p[0],'wb')
myfile.write(p[1])
myfile.close()

#but all I get is gibberish.  Is there another way to handle the buffer, or 
something else I'm missing?


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] From SQL Blobs to Python Buffers to Actual Files

2010-06-04 Thread GoodPotatoes
When I write the blob/binary data to a file and give it its original file name 
"customer1.rtf", I am expecting the application to read the binary data into 
text,excel,word documents, or whatever application created the file originally.

Example:
when I write blob1 to customer1.rtf, I get 
"xÚµVKsÛ6î93ù{ÈÁîÈ2%Û±cŸlgœdÒ<&vLJ²\J¨" as my first line.

To get the blob in Python, I use the following:

row=cursor.execute("select top 1 * from FOO..DocUploads").fetchone()
#row[2] is the original file name, row[3] is the binary data

The blob, when selected in python is a buffer:

>>> row[3]


When I print row[3] I get data like "xڵVKsÛ6î93ù{ÈÁîÈ2%۱"
When I print str(row[3]) I get the same data.





From: Steven D'Aprano 
To: tutor@python.org
Sent: Thu, June 3, 2010 9:18:44 PM
Subject: Re: [Tutor] From SQL Blobs to Python Buffers to Actual Files

On Fri, 4 Jun 2010 10:57:07 am GoodPotatoes wrote:

> I have been given a legacy database, and need to read the binaryfiles
> out to a disk.  The table has columns "filename" and "binaryFile",
> where the binaryFile is a BLOB
>
> My python script so far is:
>
> import pyodbc
> cnxn=pyodbc.Connection("DSN=sybasedatabase")
> cursor=cnxn.cursor()
> p=cursor.execute("select top 1 * from FOO..Table").fetchone()
> #p contains ('UsersOldFile.rtf',  size 1496, offset 0 at 0x010C04E0>)
>
> #I tried to write this out to the disk as:
> myfile=open(p[0],'wb')
> myfile.write(p[1])
> myfile.close()
>
> #but all I get is gibberish.  Is there another way to handle the
> buffer, or something else I'm missing?

What do you mean "gibberish"? I would expect a binary blob to look 
exactly like random binary characters, in other words, gibberish, so 
what makes you think this is not working perfectly?

What do you get if you print the binary blob, and what do you expect to 
get?



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Iterating through objects

2009-08-16 Thread GoodPotatoes
I'm not sure if I've been searching using the correct terms.  After I've 
iterated through an object, such as a cursor or webpage, how do I get back to 
the "top"?

e.g.

tutorial from http://www.daniweb.com/code/snippet563.html#

print page1.readlines()

>From this excercise, the first time I read page1 I get all of the lines.  The 
>next time, I only get []. What happens to all of the info after I've read it?

Do I need to again get the info from the web?
page1 = opener1.open(picture_page)


  ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Best Practice: Subroutines and Loop logic

2009-09-03 Thread GoodPotatoes
Hello,

I am trying to find the best way to do this.  My goal is to only call this 
subroutine only ONCE, and the logic is contingent upon two outputs.

lWords=[...] # List of many, many words
lData=[...] #list of some words that need to be validated against lWords

#subroutine to search for words, ignore case
def sub1(foo):  
pFoo=re.compile(foo,re.I)
for word in lWords:
if re.search(pFoo,word):
return[1,word]

#logic loop
for word in lData:
if word in lWords:
continue
elif sub1(word)[0]=1:
word=sub1(word)[1]  # <--- Here is my question.
else:
print word " not found.\n"

The subroutine is being run once at the elif statement.  I don't want to run it 
again just to get the [1] value.  
*Is there any way to capture all of the values returned when it is run 
during the elif statement?
*Is this actually running twice? 


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Simple regex replacement match (converting from Perl)

2009-09-04 Thread GoodPotatoes
I simply want to remark out all non-word characters read from a line.

Line:
Q*bert says "#...@!$%  "

in Perl
#match each non-word character, add "\" before it, globally.

$_=s/(\W)/\\$1/g;

output:
Q\*bert\ says\ \"\...@\!\$\%\ \ \"  #perfect!  

Is there something simple like this in python?

I would imagine:
foo='Q*bert says "#...@!$%  "'
pNw=re.compile('(\W)')
re.sub(pNw,'\\'+(match of each non-word character),foo)

How do I get the match into this function?  Is there a different way to do this?


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Putting a variable into a statement

2009-10-15 Thread GoodPotatoes
I am using the module active_directory and have an ad object called myuser.

I would like to run a series of statements like:

myuser.attribute

where attribute is a string variable, but python says that this is not the 
correct syntax.

error:
for x in myuser.properties:# "myuser.properties" returns a tuple of 
properties associated with the myuser AD object
print myuser.x

Traceback (most recent call last):
  File "", line 2, in 
print myuser.x
  File "build\bdist.win32\egg\active_directory.py", line 421, in __getattr__
raise AttributeError
AttributeError


example:
>>> myuser.properties[0] #first value in user tuple
u'cn'
>>> myuser.cn
u'Joe Sixpack'

How can I iterate through all of the values of the properties tuple and get 
each value?



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Faster list searching?

2009-11-18 Thread GoodPotatoes
I'm dealing with bigger lists than I have been, and noticed this is getting 
really slow.  Is there a faster way to do this?

for x in list1:
if x not in list2:
list3.append(x)

My search is taking up to 5 minutes to complete.


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Calling a python script using subprocess

2011-04-28 Thread GoodPotatoes
I am trying to execute a python script using the subprocess module.  I am 
feeding parameters into the script, which reads them using argparse and returns 
a list.

The script simply returns a list when called.  When I execute my script from my 
prompt, I get the following:
H:\pythonscripts>installModule.pyc --mods mod1 mod2 mod3
['mod1', 'mod2', 'mod3']
# This is the result I would like
 
 
When I call the script from within another script using subprocess I get an 
error:
 
p = subprocess.Popen(r'installModule.py --mods mod1 mod2 mod3', 
executable = sys.executable, stdout = subprocess.PIPE, stderr = 
subprocess.STDOUT)
 
'Unknown option: --\r\n'
'usage: installModule.py [option] ... [-c cmd | -m mod | file | -] [arg] 
...\r\n'
 
Is the subprocess module the best thing for me to use?  Is there a better way 
to call this script?___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor