What is the most efficient way to test for False in a list?

2007-07-08 Thread lex
Of course there is the always the iteration method:

list = [1, True, True, False, False, True]
status = True
for each in list:
status = status and each

but what is your best way to test for for False in a list?

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


replace c-style comments with newlines (regexp)

2007-12-20 Thread lex __

I'm tryin to use regexp to replace multi-line c-style comments  (like /*  this 
/n */ ) with /n (newlines).
I tried someting like   re.sub('/\*(.*)/\*'  , '/n'   , file)
but it doesn't work for multiple lines. 

besides that I want to keep all newlines as they were in the original file, so 
I can still use the original linenumbers (I want to use linenumbers as a 
reference for later use.)
I know that that will complicate things a bit more, so this is a bit less 
important.

background: I'm trying to create a 'intelligent' source-code security analysis 
tool for c/c++ , python and php files, but filtering the comments seems to be  
the biggest problem.  :( 

So, if you have an answer to this , please let me know how to do this!

thanks in advance,
- Alex



_
Download de nieuwe Windows Live Messenger!
http://get.live.com/messenger/overview
-- 
http://mail.python.org/mailman/listinfo/python-list


Request for tips on my first python script.

2006-09-07 Thread Lex Hider
Hi,
Apologies if this is against etiquette. I've just got my first python app up 
and running. It is a podcast aggregator depending on feedparser. I've really 
only learnt enough to get this up and running.

Any tips on the code quality and use of python would be appreciated. I've got 
a feeling the overall structure is up the creek.
approx 220 LOC.
file: GodCast.py

Cheers,
Lex.

#!/usr/bin/python
# GodCast: podcast aggregator!
# depends on wget & lynx
# * one of the main features of GodCast is it's use of bandwidth.
#Many podcatchers

# http://www.faqts.com/knowledge_base/view.phtml/aid/422/fid/17
# TODO: not found log
# TODO:
# config file
# opml feed list?
# pygtk/pyqt/qtkde gui?

# possible flags: test, print but don't actual do anything

import re, feedparser, os, sys, shutil, time, getopt
import urllib2
import urllib
import md5

boz = ""
HOME = os.path.expanduser("~")
# user configurable
#maxChecksPerDay = 8
#maxChecksPerDay = 12
maxChecksPerDay = 24
myTemp = '/tmp'
#podDir = os.path.join(HOME, 'Audio/Podcasts')
podDir = os.path.join(HOME, 'Podcasts')
# end user configurable
downDir = os.path.join(myTemp, 'Podcasts')
dotDir = os.path.join(HOME, '.aGodCast')
logFile = os.path.join(dotDir, 'log') #list of downloaded urls
cacheDir = os.path.join(dotDir, 'cache')
ignoreNotFound = False # if true, add files not found to log
# list of feeds, ignore lines not beginning ^http
feedList = os.path.join(dotDir, 'feeds.txt')


def exitFunc():
#f.close()
#log.close()
if boz:
print boz


def makeDirs(*dirs):
for dir in dirs:
if not os.path.exists(dir):
os.makedirs(dir)


# render is used because feeds use a lot of html, not just plain text.
def render(html):
if html:
html = re.sub('"', '\\"', html.encode('utf8'))
#command = 'echo "' + html + '" | w3m -dump -T text/html'
#command = 'echo "' + html + '" | html2text'
command = 'echo "' + html + '" | lynx -dump -stdin -force_html'
os.system(command)


def localMD5(url):
hash = md5.new(url).hexdigest() + '.xml' #unique name from url
return os.path.join(cacheDir, hash)


def cache(url):
max  = 60 * 60 * 24 / maxChecksPerDay #seconds
myfile = localMD5(url)
if os.path.isfile(myfile):
elapsed = int(time.time()) - os.path.getmtime(myfile)
if elapsed <= max:
return
print "FETCHING:", url + ' ...'
urllib.urlretrieve(url, myfile)
# handle half finish?


def updateCache(feeds):
l = []
print "updating local xml cache..."
for feed in file(feeds, "r").read().split('\n'):
if not re.match('^http://', feed): # feedList ignores anything but 
urls
continue
# TODO: handle whitespace, strip trailing
cache(feed)
l.append([localMD5(feed), feed])
print "cache up to date"
return l


def geturl(url):
try:
redir =  urllib2.urlopen(url).geturl()
except urllib2.HTTPError, e:
if e.code != 404:
print url
print "geturl HTTPError:", e.code
return e.code
except urllib2.URLError, e:
# (110, 'Connection timed out')
print e.reason
#print "geturl URLError:", e.code
else:
return redir
return 0


def htmlTitle(mainTitle, subTitle):
s = ''
s += '' + mainTitle + ''
s += '' + subTitle + ''
return s


def downloadPod(url, dest):
kb = 2
success = 0
command = 'wget --continue -O "' + dest + '" "' + url + '"'
status  =  os.system(command)
if status == success:
return True
else:
print "\nWGET:", status
if status == kb:
pass
#raise KeyboardInterrupt
return False


def downloadQueue(q, latest):
for x in range(latest):
for [feedTitle, castList] in q:
if not len(castList) > x:
continue
cast = castList[x]
if cast is None:
continue
url = cast.enclosures[0]['href']
redirect = geturl(url) # TRAFFIC
if type(redirect) != int: #success
render(htmlTitle(feedTitle + ": #" + str(x+1), cast.title))
render(cast.description)

podFile = os.path.basename(redirect).split('?')[0]
permDir = os.path.join(podDir, feedTitle)
permFile = os.path.join(permDir, podFile)
tempDir = os.path.join(downDir, feedTitle

Re: Is there a best linux distro for a python hobbyist?

2009-01-05 Thread Lex Hider
Probably not a big difference in most cases between debian, ubuntu,
fedora. The latter two may be more likely to have more recent
versions.

I'm pretty sure ubuntu is the only one which currently has python 3.0
in it's archives [no, it's not the default version].

On 06/01/2009, member thudfoo  wrote:
> One with all the major python GUIs: pyQT, pyGtk, pyKde, wxPython, 
> And so on.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Kohonen neural network

2010-05-26 Thread Lex Lebedeff
Hello!

Has anyone tried to build an implementation of subject in Python?
Any help is appreciated!

--- news://freenews.netfront.net/ - complaints: n...@netfront.net ---
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Kohonen neural network

2010-05-26 Thread Lex Lebedeff
On Wed, 26 May 2010 05:26:56 -0700, alex23 wrote:

> On May 26, 7:21 pm, Lex Lebedeff  wrote:
>> Has anyone tried to build an implementation of subject in Python? Any
>> help is appreciated!
> 
> http://www.dia.fi.upm.es/~jamartin/download.htm
> 
> Seriously, though, any reason why you couldn't just type "kohonen neural
> network python" into Google? You would've found it a lot easier to wade
> through the results before your question & it's many mirrors. It
> would've been the 2nd entry, immediately after the wikipedia article on
> Kohonen SOMs.

Thanx, I've already googled this stuff. Rather unreadable and freaky code.

--- news://freenews.netfront.net/ - complaints: n...@netfront.net ---
-- 
http://mail.python.org/mailman/listinfo/python-list