Python doesn't understand %userprofile%

2008-06-10 Thread bsagert
In xp when I try os.path.getmtime("%userprofile/dir/file%") Python
bites back with "cannot find the path specified" Since my script has
to run on machines where the username is unspecified I need a fix.
Thanks in advance.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python doesn't understand %userprofile%

2008-06-10 Thread bsagert
On Jun 10, 8:56 am, [EMAIL PROTECTED] wrote:
> In xp when I try os.path.getmtime("%userprofile/dir/file%") Python
> bites back with "cannot find the path specified" Since my script has
> to run on machines where the username is unspecified I need a fix.
> Thanks in advance.

oops that should be os.path.getmtime("%userprofile%/dir/file")
--
http://mail.python.org/mailman/listinfo/python-list


Thanks for help re: %userprofile%

2008-06-10 Thread bsagert
The python community is very helpful to newbies like me. I did however
manage to solve my problem in the meantime. I needed  the modification
time of certain files on various computers, but I didn't know the
usernames ahead of time, so I used windows %userprofile% method.
Python likes forward slashes in file names, whereas windows likes back
slashes.  Here is my script.

import os, re
u = os.getenv("USERPROFILE")
# python returns "c:\\documents and Settings\\user"
# note the escaped backslashes which windows hates.
# let's repair that with re.sub
u = re.sub( r"\\", "/", u)
f = u+"/dir1/file1"
mod = os.path.getmtime(f)
# success, now do something

c = "copy '%userprofile%\dir1\file1' c:\dir2\file2"
# note back slashes here which windows tolerates.
# In the os.system context, python delivers unescaped slashes.
os.system(c)
# success

I'm a retired old fart trying to learn python so I welcome criticism
and advice. My original post was at
http://groups.google.ca/group/comp.lang.python/browse_thread/thread/59cc71e92bef1ee2?hl=en#
--
http://mail.python.org/mailman/listinfo/python-list


Please explain Python "__whatever__" construct.

2008-06-16 Thread bsagert
After a couple of weeks studying Python, I already have a few useful
scripts, including one that downloads 1500 Yahoo stock quotes in 6
seconds. However, many things are puzzling to me. I keep on seeing
things like "__main__" in scripts.  A more obscure example would be
"__add__" used in string concatenation. For example, I can use "Hello
"+"world (or just "Hello" "world") to join those two words. But I can
also use "Hello ".__add__("world"). When and why would I ever use
"__main__" or the many other "__whatever__" constructs?
--
http://mail.python.org/mailman/listinfo/python-list


Importing module PIL vs beautifulSoup.

2008-06-18 Thread bsagert
I downloaded BeautifulSoup.py from http://www.crummy.com/software/BeautifulSoup/
and being a n00bie, I just placed it in my Windows c:\python25\lib\
file. When I type "import beautifulsoup" from the interactive prompt
it works like a charm. This seemed too easy in retrospect.

Then I downloaded the PIL (Python Imaging Library) module from
http://www.pythonware.com/products/pil/. Instead of a simple file that
BeautifulSoup sent me, PIL is an .exe that installed itself in c:
\python25\lib\site-packages\PIL\. However it won't load by typing
"import pil".

I know I am supposed to RTFM, but a Google search has not led to the
holy grail that Monty Python found. I realize that PIL is a package as
opposed to a simple script (and it does not include a readme file).
Thanks in advance for any help.
--
http://mail.python.org/mailman/listinfo/python-list


Never mind folks, n00bie here forgot Python is case sensitive!

2008-06-18 Thread bsagert
On Jun 18, 10:18 am, Duncan Booth <[EMAIL PROTECTED]>
wrote:
> [EMAIL PROTECTED] wrote:
> > I downloaded BeautifulSoup.py from
> >http://www.crummy.com/software/BeautifulSoup/and being a n00bie, I
> > just placed it in my Windows c:\python25\lib\ file. When I type
> > "import beautifulsoup" from the interactive prompt it works like a
> > charm. This seemed too easy in retrospect.
>
> It might be better if you put the file in \python25\lib\site-packages\
> The same import will still work, but you probably want to avoid putting
> non-core files directly in \python25\lib.
>
> Also, it sounds like you renamed the file: "import beautifulsoup" should
> fail (the file is supposed to be called BeautifulSoup.py). If you want to
> be able to install other software which has been written to use
> BeautifulSoup you'll need to make sure the case of the filename is correct.
>
>
>
> > Then I downloaded the PIL (Python Imaging Library) module from
> >http://www.pythonware.com/products/pil/. Instead of a simple file that
> > BeautifulSoup sent me, PIL is an .exe that installed itself in c:
> > \python25\lib\site-packages\PIL\. However it won't load by typing
> > "import pil".
>
> > I know I am supposed to RTFM, but a Google search has not led to the
> > holy grail that Monty Python found. I realize that PIL is a package as
> > opposed to a simple script (and it does not include a readme file).
> > Thanks in advance for any help.
>
> Did you try "import PIL"? All module and package names in Python are case
> sensitive.

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


Re: Importing module PIL vs beautifulSoup.

2008-06-18 Thread bsagert
On Jun 18, 10:18 am, Duncan Booth <[EMAIL PROTECTED]>
wrote:
> [EMAIL PROTECTED] wrote:
> > I downloaded BeautifulSoup.py from
> >http://www.crummy.com/software/BeautifulSoup/and being a n00bie, I
> > just placed it in my Windows c:\python25\lib\ file. When I type
> > "import beautifulsoup" from the interactive prompt it works like a
> > charm. This seemed too easy in retrospect.
>
> It might be better if you put the file in \python25\lib\site-packages\
> The same import will still work, but you probably want to avoid putting
> non-core files directly in \python25\lib.
>
> Also, it sounds like you renamed the file: "import beautifulsoup" should
> fail (the file is supposed to be called BeautifulSoup.py). If you want to
> be able to install other software which has been written to use
> BeautifulSoup you'll need to make sure the case of the filename is correct.
>
>
>
> > Then I downloaded the PIL (Python Imaging Library) module from
> >http://www.pythonware.com/products/pil/. Instead of a simple file that
> > BeautifulSoup sent me, PIL is an .exe that installed itself in c:
> > \python25\lib\site-packages\PIL\. However it won't load by typing
> > "import pil".
>
> > I know I am supposed to RTFM, but a Google search has not led to the
> > holy grail that Monty Python found. I realize that PIL is a package as
> > opposed to a simple script (and it does not include a readme file).
> > Thanks in advance for any help.
>
> Did you try "import PIL"? All module and package names in Python are case
> sensitive.

YIKES, Python is case sensitive! I knew that, says he blushing. Now it
works. Thanks Duncan. Ciao, Bill
--
http://mail.python.org/mailman/listinfo/python-list


My n00bie brain hurts after "Python setup.py install".

2008-06-21 Thread bsagert
I downloaded Mark Pilgrims's feedparser.py in a zipfile to my Windows
machine, unzipped it and tried to install it to no avail.

Here is the result =>

C:\>python c:\scripts\feedparser-4.1\setup.py install
running install
running build
running build_py
file feedparser.py (for module feedparser) not found
running install_lib
warning: install_lib: 'build\lib' does not exist -- no Python modules
to install
running install_egg_info
Writing C:\Python25\Lib\site-packages\feedparser-4.1-py2.5.egg-info

WTF? The file feedparser.py did exist in the same place as setup.py.
Even if it works, what exactly does this setup.py do for me? If I just
manually place feedparser.py in my Python site-packages file it works
fine. As for that egg-info file, I googled "python eggs" and now I am
really confused.



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


Help me optimize my feed script.

2008-06-26 Thread bsagert
I wrote my own feed reader using feedparser.py but it takes about 14
seconds to process 7 feeds (on a windows box), which seems slow on my
DSL line. Does anyone see how I can optimize the script below? Thanks
in advance, Bill

# UTF-8
import feedparser

rss = [
'http://feeds.feedburner.com/typepad/alleyinsider/
silicon_alley_insider',
'http://www.techmeme.com/index.xml',
'http://feeds.feedburner.com/slate-97504',
'http://rss.cnn.com/rss/money_mostpopular.rss',
'http://rss.news.yahoo.com/rss/tech',
'http://www.aldaily.com/rss/rss.xml',
'http://ezralevant.com/atom.xml'
]
s = '\n\nC:/x/test.htm\n'

s += '\n'\
 'h3{margin:10px 0 0 0;padding:0}\n'\
 'a.x{color:black}'\
 'p{margin:5px 0 0 0;padding:0}'\
 '\n'

s += '\n\n\n'

for url in rss:
d = feedparser.parse(url)
title = d.feed.title
link = d.feed.link
s += '\n'+ title +'\n'
# aldaily.com has weird feed
if link.find('aldaily.com') != -1:
description = d.entries[0].description
s += description + '\n'
for x in range(0,3):
if link.find('aldaily.com') != -1:
continue
title = d.entries[x].title
link = d.entries[x].link
s += ''+ title +'\n'

s += '\n\n'

f = open('c:/scripts/myFeeds.htm', 'w')
f.write(s)
f.close

print
print 'myFeeds.htm written'
--
http://mail.python.org/mailman/listinfo/python-list


n00bie wants advice.

2008-07-01 Thread bsagert
This simple script writes html color codes that can be viewed in a
browser.  I used short form hex codes (fff or 000, etc) and my list
has only six hex numbers otherwise the results get rather large. I
invite criticism as to whether my code is "pythonic". Are there other
ways to generate the hex combos besides the nested "for" loops? Thanks
in advance, Bill

list = ['3','6','9','b','d','f']

s = 'h1{margin:0}\n'

for a in list:
for b in list:
for c in list:
s += ''+ a + b 
+ c +'
\n'

s += ''

f = open('c:/x/test.htm', 'w')
f.write(s)
f.close()
--
http://mail.python.org/mailman/listinfo/python-list


Python and decimal character entities over 128.

2008-07-09 Thread bsagert
Some web feeds use decimal character entities that seem to confuse
Python (or me). For example, the string "doesn't" may be coded as
"doesn’t" which should produce a right leaning apostrophe.
Python hates decimal entities beyond 128 so it chokes unless you do
something like string.encode('utf-8'). Even then, what should have
been a right-leaning apostrophe ends up as "’". The following script
does just that. Look for the string "The Canuck iPhone: Apple doesnâ
€™t care" after running it.

# coding: UTF-8
import feedparser

s = ''
d = feedparser.parse('http://feeds.feedburner.com/Mathewingramcom/
work')
title = d.feed.title
link = d.feed.link
for i in range(0,4):
title = d.entries[i].title
link = d.entries[i].link
s += title +'\n' + link + '\n'

f = open('c:/x/test.txt', 'w')
f.write(s.encode('utf-8'))
f.close()

This useless script is adapted from a "useful" script. Its only
purpose is to ask the Python community how I can deal with decimal
entities > 128. Thanks in advance, Bill


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


For_loops hurt my brain.

2008-07-16 Thread bsagert
This script uses a simple for loop to zip some files. However I am
repeating code that cries out for a nested loop. My two lists of
files_to_be_zipped (spare and seekfacts) are of uneven length so I
can't seem to decipher the "for_logic". I would appreciate any help.
Thanks, Bill

import zipfile
import os

zips = [
'c:/spare.zip',
'c:/seekfacts.zip'
]
spare = [
'c:/spare/huge.fm3',
'c:/spare/huge.wk3'
]
seekfacts = [
'c:/seekfacts/bookmark.html',
'c:/seekfacts/index.htm',
'c:/seekfacts/seek.css',
'c:/seekfacts/seek.js'
]

zFile = zipfile.ZipFile(zips[0], 'w')
for files in spare:
zFile.write(files, os.path.basename(files), zipfile.ZIP_DEFLATED)
zFile.close()

zFile = zipfile.ZipFile(zips[1], 'w')
for files in seekfacts:
zFile.write(files, os.path.basename(files), zipfile.ZIP_DEFLATED)
zFile.close()

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