problem moving from char to integer

2006-09-26 Thread Melih Onvural
Group,

I'm trying to get a very basic cast from a string to an integer
working. Here is what I'm doing:

for i in url:
 result[counter] = int(i)
 counter += 1;

return result;

anything that I'm doing drastically wrong? Mac OS 10.4, MacPython
upgraded to 2.4. Thanks for any help,

Melih

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem moving from char to integer

2006-09-26 Thread Melih Onvural
This is the error message that I'm having a tough time interpreting:
Traceback (most recent call last):
  File "pagerank.py", line 101, in ?
main()
  File "pagerank.py", line 96, in main
ch = strord(url)
  File "pagerank.py", line 81, in strord
result[counter] = int(i);
ValueError: invalid literal for int(): i

and here is the full code:

def strord(url):
counter=0;
for i in url:
   result[counter] = int(i);
   counter += 1;

return result;

Thanks

Pontus Ekberg wrote:
> Melih Onvural wrote:
>
> > Group,
> >
> > I'm trying to get a very basic cast from a string to an integer
> > working. Here is what I'm doing:
> >
> > for i in url:
> >  result[counter] = int(i)
> >  counter += 1;
> >
> > return result;
> >
> > anything that I'm doing drastically wrong? Mac OS 10.4, MacPython
> > upgraded to 2.4. Thanks for any help,
> >
> > Melih
>
> In what way does it not work? Please post the entire code or at least the
> result of your code.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem moving from char to integer

2006-09-26 Thread Melih Onvural
I'm trying to calculate the checksum of a website so that I can get its
PageRank. I'm modifying the code that I found here:
http://blog.outer-court.com/archive/2004_06_27_index.html#108834386239051706

I apologize for being secretive, but I didn't mean to be. I'm trying to
take characters and push them to their integer value. Thanks for the
help and the advice on writing in Python. Definitely learning to do
things right is the goal here.

Also, the ord function worked. Thanks.

Melih

John Machin wrote:
> Melih Onvural wrote:
> > This is the error message that I'm having a tough time interpreting:
> > Traceback (most recent call last):
> >   File "pagerank.py", line 101, in ?
> > main()
> >   File "pagerank.py", line 96, in main
> > ch = strord(url)
> >   File "pagerank.py", line 81, in strord
> > result[counter] = int(i);
> > ValueError: invalid literal for int(): i
> >
> > and here is the full code:
> >
> > def strord(url):
> > counter=0;
> > for i in url:
> >result[counter] = int(i);
> >counter += 1;
> >
> > return result;
>
> You are getting this error because you are trying to convert string to
> integer, but this of course complains if it finds unexpected
> non-digits.
> int('9') -> 9
> int('x') -> this error
> You may be looking for the ord() function:
> ord('9') -> 57
> ord('x') -> 120
>
> In any case, once you've sorted that out, your function will die in the
> same statement, because "result" isn't defined. Python isn't awk :-)
>
> Given the name of your function (strord), perhaps what you really want
> is this:
>
> def strord(any_string):
> return [ord(x) for x in any_string]
>
> but why you'd want that (unless you were trying to pretend that Python
> is C), I'm having a little troubling imagining ...
>
> Perhaps if you told us what you are really trying to do, we could help
> you a little better.
> 
> HTH,
> John

-- 
http://mail.python.org/mailman/listinfo/python-list


Executing Javascript, then reading value

2007-01-29 Thread Melih Onvural
I need to execute some javascript and then read the value as part of a 
program that I am writing. I am currently doing something like this:

import htmllib, urllib, formatter

class myparser(htmllib.HTMLParser):
insave = 0
def start_div(self, attrs):
for i in attrs:
if i[0] == "id" and i[1] == "pr":
self.save_bgn()
self.insave = 1

def end_div(self):
if self.insave == 1:
print self.save_end()
self.insave = 0

parser = myparser(formatter.NullFormatter())

#def getPageRank(self, url):
try:
learn_url = "http://127.0.0.1/research/getPageRank.html?q=http://
www.yahoo.com&"
pr_url = urllib.urlopen(learn_url)
parser.feed(pr_url.read())
except IOError, e:
print e

but the result is the javascript function and not the calculated 
value. Is there anyway to get the javascript to execute first, and 
then return to me the value? thanks in advance,

Melih Onvural

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Executing Javascript, then reading value

2007-01-29 Thread Melih Onvural
Thanks, let me check out this route, and then I'll post the results.

Melih Onvural

On Jan 29, 4:04 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
> On 29 Jan 2007 12:44:07 -0800, Melih Onvural <[EMAIL PROTECTED]> wrote:
>
> >I need to execute some javascript and then read the value as part of a
> >program that I am writing. I am currently doing something like this:Python 
> >doesn't include a JavaScript runtime.  You might look into the
> stand-alone Spidermonkey runtime.  However, it lacks the DOM APIs, so
> it may not be able to run the JavaScript you are interested in running.
> There are a couple other JavaScript runtimes available, at least.  If
> Spidermonkey is not suitable, you might look into one of them.
>
> Jean-Paul

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Executing Javascript, then reading value

2007-01-30 Thread Melih Onvural
In fact what you're describing is exactly what I needed. I ended up
finding a way to execute the javascript using Rhino and then capturing
the
result. Not exactly what I wanted to do, but once I found it out, it
works.

Melih Onvural

On Jan 30, 2:57 pm, John Nagle <[EMAIL PROTECTED]> wrote:
> Melih Onvural wrote:
> > Thanks, let me check out this route, and then I'll post the results.
>
> > Melih Onvural
>
> > On Jan 29, 4:04 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
>
> >> On 29 Jan 2007 12:44:07 -0800, Melih Onvural <[EMAIL PROTECTED]>
> >> wrote:
>
> >>> I need to execute some javascript and then read the value as part of a
> >>> program that I am writing. I am currently doing something like
> >>> this:Python doesn't include a JavaScript runtime.  You might look into
> >>> the
>
> >> stand-alone Spidermonkey runtime.  However, it lacks the DOM APIs, so it
> >> may not be able to run the JavaScript you are interested in running. There
> >> are a couple other JavaScript runtimes available, at least.  If
> >> Spidermonkey is not suitable, you might look into one of them.
>
> This is getting to be a common problem.  One used to be able to
> look at web pages from a program by reading the HTML.  Now you need to
> load the page into a browser-like environment, run at least the
> OnLoad JavaScript, and then start looking at the document object module.
> This requires a browser emulator, a browser without a renderer.
> Useful for spam filters and such.
>
>  It's not clear if the original poster needs that much capability,
> though.
>
> John Nagle


-- 
http://mail.python.org/mailman/listinfo/python-list


SyntaxError: 'return' outside function

2007-01-31 Thread Melih Onvural
Has anyone seen this error before and been able to solve it? I can't
seem to find anything that leads to a solution. I found this post
http://zope.org/Collectors/Zope/1809, but can't really understand it.
I've attached my code below to see if anything looks funny. It happens
at the very last return at the end. Thanks in advance,

--melih

===Code

def legiturl(self, url):
# this breaks down the url into 6 components to make sure it's
"legit"
t = urlparse.urlparse(url)

if t[0] != 'http':
return ""

# remove URL fragments, but not URL
if len(t[5]) > 0:
url = urlparse.urlunparse((t[0],t[1],t[2],"","",""))
t = urlparse.urlparse(url)

# stupid parser sometimes leaves frag in path
x = find(t[2], '#')
if x >= 0:
return ""

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SyntaxError: 'return' outside function

2007-01-31 Thread Melih Onvural
Thanks all, I did a massive make sure everything is indents and not
spaces across all of my files and now things are running much more
smoothly. I appreciate the responses.

--melih

On Jan 31, 4:49 pm, [EMAIL PROTECTED] wrote:
> Melih> Has anyone seen this error before and been able to solve it? I
> Melih> can't seem to find anything that leads to a solution.
>
> Your code is incorrectly indented.  Try:
>
> def legiturl(self, url):
> # this breaks down the url into 6 components to make sure it's "legit"
> t = urlparse.urlparse(url)
>
> if t[0] != 'http':
> return ""
>
> # remove URL fragments, but not URL
> if len(t[5]) > 0:
> url = urlparse.urlunparse((t[0],t[1],t[2],"","",""))
> t = urlparse.urlparse(url)
>
> # stupid parser sometimes leaves frag in path
> x = find(t[2], '#')
> if x >= 0:
> return ""
>
> instead.  Note also the lack of a return at the end of the function.
>
> Skip


-- 
http://mail.python.org/mailman/listinfo/python-list


Broken Library Code, suggested course of action

2007-02-05 Thread Melih Onvural
I've been trying to use the URLParse library, and I thought I had all
of the kinks worked out when I ran into this:

Traceback (most recent call last):
  File "./run.py", line 130, in ?
main()
  File "./run.py", line 126, in main
r = crawl.crawl(p, x)
  File "/home/monvural/src/crawl.py", line 74, in crawl
root = parser.legiturl(url)
  File "/home/monvural/src/crawl.py", line 22, in legiturl
t = urlparse.urlparse(url)
  File "/usr/lib/python2.4/urlparse.py", line 50, in urlparse
tuple = urlsplit(url, scheme, allow_fragments)
  File "/usr/lib/python2.4/urlparse.py", line 89, in urlsplit
i = url.find(':')
AttributeError: 'tuple' object has no attribute 'find'

I have just recently upgraded my system to FC6, and I wasn't sure if
this was an issue of version change. Why does the urlsplit() function
work on my old station, but here I'm getting a tuple cannot find
error. I understand from looking at the docs that tuples cannot have
operations done onto them as they are immutable and sans index, but
there has to be a way to dive into the http address and parse what's
there. Thanks for any suggestions,

--Melih Onvural

-- 
http://mail.python.org/mailman/listinfo/python-list