Re: seperating directories from files

2006-01-30 Thread John Zenger
raj wrote:
> How can I find ... if it's a file,
> whether it is an image file or not?

Assuming you are running on an operating system that uses file 
extensions to indicate a file is an image (like Windows or Linux), 
something like this will work:

import os.path

def isImage(filename):
 return os.path.splitext(filename)[1] in ['.jpg','.png','.gif']

(Add as many file extensions to that list as you like...)

> Is there any method to read the metadata of files?

It's not what you are looking for here, but os.stat gives you access to 
lots of information about files.  In Linux and Windows, though, the 
'type' of a file is incorporated into its filename through an extension.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple perl program in python gives errors

2006-01-30 Thread John Zenger
[EMAIL PROTECTED] wrote:
> Also I noticed if I initialize a variable
> as 0 , then I can only do integer math not floating math. this just
> seems kind of backward as I am used to php and perl which dont require
> such strict rules.
> 

Not quite:

 >>> foo = 0
 >>> foo += 122
 >>> print foo
122
 >>> print foo / 7
17
 >>> print foo / 7.0
17.4285714286
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Needing a WinXP Python variant of a line of code

2006-01-30 Thread John Zenger
import os, os.path

path_to_nethack_logfile = ""
for root, dirs, files in os.walk("c:\\"):
 if 'nethackdir' in root:
 logs = [x for x in files if 'logfile' in x]
 if len(logs) > 0:
 path_to_nethack_logfile = os.path.join(root, logs[0])
 exit

Ron Rogers Jr. wrote:
> I have this line of code that's written with Linux in mind:
> 
> path_to_nethack_logfile = os.popen("locate logfile | grep 
> nethackdir").read()
> 
> and I'm wanting a Windows equivalent, any suestions?
> 
> 
> Thanks.
> 
> CronoCloud (Ron Rogers Jr.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removing characters before writing to file

2006-02-09 Thread John Zenger
As mentioned in the thread, it makes sense to build the desired output 
you want from the tuple, rather than converting the tuple to a string 
and then doing replace operations on the string.

If you do want to go the replace route, you don't need the power of 
regex substitutions for what you are interested in.  Just try the 
replace method:

 >>> foo = "('sometext1', 1421248118, 1, 'P ')"
 >>> foo.replace("\'", "").replace("(", "").replace(")", "")
'sometext1, 1421248118, 1, P '

or, more elegantly:

 >>> "".join([x for x in foo if x not in ['(',')','\''] ])
'sometext1, 1421248118, 1, P '

However, all of these replace-based solutions are bad because they will 
not only replace the apostrophes and parentheses between the strings, 
but also within them; what if "sometext1" is actually "John's Text?" 
You are much better off building your desired output from the actual 
tuple data yourself.

[EMAIL PROTECTED] wrote:
> hi
> i have some output that returns a lines of tuples eg
> 
> ('sometext1', 1421248118, 1, 'P ')
> ('sometext2', 1421248338, 2, 'S ')
> and so on
> 
> 
> I tried this
> re.sub(r" '() ",'',str(output)) but it only get rid of the ' and not
> the braces. I need to write the output to a file such that
> 
> sometext1, 1421248118, 1, P
> sometext2, 1421248338, 2, S
> 
> I also tried escaping , re.sub(r" '\(\) ",'',str(output)) but also did
> not work
> How can i get rid of the braces before writing to file? thanks
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: line wrapping problem

2006-02-09 Thread John Zenger
S Borg wrote:

>  print >> mytextfile, '\n'  #new line

Wouldn't this print two line breaks--the one you specify in the string, 
plus the one always added by print if there is no comma at the end of 
the statement?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check...

2006-02-11 Thread John Zenger
This should be just a matter of determining how your string is encoded 
(ASCII, UTF, Unicode, etc.) and checking the ord of each character to 
see if it is in the contiguous range of English characters for that 
encoding.  For example, if you know that the string is ASCII or UTF-8, 
you could check ord for each character and confirm it is less than 128.

Lad wrote:
> Hello,
> How can I  check that a string does NOT contain NON English characters?
> Thanks
> L.
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strange error I can't figure out...

2006-02-18 Thread John Zenger
It works fine for me.  You must be having an indentation problem.

Also, get rid of the comma at the end of that last print statement.

Brian Blais wrote:
> Hello,
> 
> I have an odd kind of Heisenbug in what looks like a pretty simple 
> program.  The program is a progress bar code I got at the Python Cookbook:
> 
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/168639
> 
> (including the code below)
> 
> 
> If you uncomment the one print statement I added in the progressBar 
> class, you get the error:
> 
>   File "test_progress2.py", line 19
> diffFromMin = float(self.amount - self.min)
> ^
> SyntaxError: invalid syntax
> 
> 
> yet, without the print statement, it works fine.  what am I overlooking 
> here?
> 
> 
> thanks,
> 
> 
> bb
> 
> #
> 
> class progressBar:
> def __init__(self, minValue = 0, maxValue = 10, totalWidth=12):
> self.progBar = "[]"   # This holds the progress bar string
> self.min = minValue
> self.max = maxValue
> self.span = maxValue - minValue
> self.width = totalWidth
> self.amount = 0   # When amount == max, we are 100% done
> self.updateAmount(0)  # Build progress bar string
> 
> def updateAmount(self, newAmount = 0):
> if newAmount < self.min: newAmount = self.min
> if newAmount > self.max: newAmount = self.max
> self.amount = newAmount
> 
> #print "hello"   #<--  uncomment line to break
> 
> # Figure out the new percent done, round to an integer
> diffFromMin = float(self.amount - self.min)
> percentDone = (diffFromMin / float(self.span)) * 100.0
> percentDone = round(percentDone)
> percentDone = int(percentDone)
> 
> # Figure out how many hash bars the percentage should be
> allFull = self.width - 2
> numHashes = (percentDone / 100.0) * allFull
> numHashes = int(round(numHashes))
> 
> # build a progress bar with hashes and spaces
> self.progBar = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]"
> 
> # figure out where to put the percentage, roughly centered
> percentPlace = (len(self.progBar) / 2) - len(str(percentDone))
> percentString = str(percentDone) + "%"
> 
> # slice the percentage into the bar
> self.progBar = self.progBar[0:percentPlace] + percentString + 
> self.progBar[percentPlace+len(percentString):]
> 
> def __str__(self):
> return str(self.progBar)
> 
> 
> if __name__ == "__main__":
> 
> import time
> prog = progressBar(0, 100, 77)
> for i in xrange(101):
> prog.updateAmount(i)
> print prog, "\r",
> time.sleep(.05)
> 
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Should we still be learning this?

2006-02-18 Thread John Zenger
Don't overly concern yourself with your course being 100% up to date. 
When learning programming, the concepts are what is important, not the 
syntax or libraries you happen to be using.  Even if they were to teach 
you the latest and greatest features of 2.4.2, that would be out of date 
in a few months/years when the next version comes along and the Python 
gods decide to deprecate the entire os module or something.

Syntax and libraries change; just roll with it.  When you are a student, 
the important thing is learning the mental skills of how to put it all 
together.  When I was a wee lad, they taught me Pascal, a language that 
is now as dead as Latin, but I now realize that the language did not 
matter; all that mattered was that I learned the basics of how to design 
and put together a program in a (procedural) language.  Once I knew 
that, it was a cinch to teach myself C in an afternoon, and only 
slightly tougher to learn C++ and then Java.

Python is a good teaching language because by learning one language you 
learn three major programming paradigms: procedural, OO, and functional. 
  It doesn't matter if, three years from now, a Dark Age descends upon 
the land and Python becomes as extinct as Pascal.  If your course was 
decent, you spent your time learning programming, not just learning 
today's syntax and libraries, and you'll be able to learn Microsoft 
Visual C#++.Net.Com.Org or whatever other language happens to be 
fashionable in the future.

And BTW, map and filter are such useful concepts that it makes sense to 
teach them to students even if they will one day be deprecated in 
Python.  If you want to teach yourself Haskell or a Lisp dialect (and 
you should!), knowing those concepts will come in very handy.

Max wrote:
> On monday I start a semester course in Python (the alternative was 
> Java). I was looking through the course outline and noticed the following:
> 
> 1) UserDict is used. This is deprecated, right?
> 2) There is no mention of list comprehensions, but map and filter are 
> taught early and then revisited later. I don't think this is good: list 
> comprehensions are, IMO, one of Python's great features, Psyco prefers 
> them, they're more pythonic, and map and filter seem to be going out the 
> window for Python 3000.
> 
> What do you think?
> 
> --Max
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tab Character?

2006-02-18 Thread John Zenger
Tab is \t .  As in:

print "coke\tpepsi"
tsvline.split("\t")

[EMAIL PROTECTED] wrote:
> How do I make a tab character in code to split a line read with tabs in
> it?
> 
> 
> Thanks.
> 
> Tom
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get parameters from URL using CGI

2006-02-19 Thread John Zenger
import cgi
import cgitb; cgitb.enable()  # Optional; for debugging only

print "Content-Type: text/html"
print

f = cgi.FieldStorage()
for i in f.keys():
print "",i,":",f[i].value


abcd wrote:
> i want to create a CGI script which simply prints out values given via
> the URL (such as when a GET is performed).
> 
> So if I have a script named, foo.cgi and I access it by going to:
> 
> http://www.somesite.com/cgi-bin/foo.cgi?name=john&age=90
> 
> I want foo.cgi to print out:
> name: john
> age: 90
> 
> 
> how do i get the values from the URL like that?
> 
> thanks
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Format file size for printing

2006-02-19 Thread John Zenger
I know of no built-in way, but you could probably code this in a few 
lines using print "%.1f" and so on.

(Some of us, by the way, are in the camp that believes a kilobyte is 
1024 bytes, not 1000, so 103803 bytes for us is about 101.4 kilobytes).

abcd wrote:
> is there a built-in way of printing the size of a file nicely?
> 
> So if the file size is 103803 bytes it prints out like: 103.8K
> or
> 0.1MB 
> 
> something liek that?
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: number ranges (was Re: Matlab page on scipy wiki)

2006-02-19 Thread John Zenger
Colin J. Williams wrote:
> Bryan Cole wrote:
> 
>>>   
>>> First, I think the range() function in python is ugly to begin with.
>>> Why can't python just support range notation directly like 'for a in
>>> 0:10'.  Or with 0..10 or 0...10 syntax.  That seems to make a lot more
>>> sense to me than having to call a named function.   Anyway, that's a
>>> python pet peeve, and python's probably not going to change something
>>> so fundamental...   

I strongly agree that Python should promote range or xrange to syntax. 
I favor [0..10] rather than [0:10] because 0..10 is inherently easier to 
understand.  Every maths text I have read uses the ".." notation to show 
ranges; for that reason, perhaps, Haskell uses "..".  The colon is 
already overused; it both signals the beginning of compound statements, 
and has all sorts of slice/indexing meanings when it is inside square 
brackets, depending on how many colons there are and whether there are 
arguments between them.

Haskell also has a good step notation.  In Haskell:

[1..10] means [1,2,3,4,5,6,7,8,9,10]
[1,3..10] means [1,3,5,7,9]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: number ranges (was Re: Matlab page on scipy wiki)

2006-02-20 Thread John Zenger
Steven D'Aprano wrote:
> John Zenger wrote:
> 
>> I strongly agree that Python should promote range or xrange to syntax. 
>> I favor [0..10] rather than [0:10] because 0..10 is inherently easier 
>> to understand.
 >
> "Inherently"?
> 
> You mean people are born with an instinctive, unlearnt understanding of 
> ..? 

I mean that most people will have seen that notation somewhere else in 
their pre-Python lives, so it won't take much effort for them to 
remember what it is, at least more so than 0:10.

> With the 
> introduction of a single keyword, we could do this:
> 
> for i in 2 to 5:
> print i,
> 
> which would print 2 3 4 5

This proposed syntax is also easy to understand, maybe more than "..", 
because it uses natural language.  The only criticism is that it creates 
a list without using [] notation.  How about [2 to 5]?

> 
> (I'm open to arguments that it should be more Pythonic and less 
> mathematical, and halt at 4.)

I am also open to such arguments but it will be tough to convince me 
that "x to y" should mean something different from what it means in 
Pascal, BASIC, and English.

> 
> A second keyword "downto" would allow easy backwards loops,

"Downto" would only be necessary if we wanted "[10 to 1]" to equal [].

> and a third 
> "step" will absolutely kill any chance of Guido agreeing to this 
> whatsoever.

What about [1 to 3 to 10]?  (Or [1 to 10:2]?)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Processing text using python

2006-02-20 Thread John Zenger
If you have already read the string into memory and want a convenient 
way to loop through it 3 characters at a time, check out the "batch" recipe:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303279

It uses itertools to make an iterator over the string, returning 3 
characters at a time.  Cool stuff.


nuttydevil wrote:
> Hey everyone! I'm hoping someone will be able to help me, cause I
> haven't had success searching on the web so far... I have large chunks
> of text ( all in a long string) that are currently all in separate
> notebook files. I want to use python to read these strings of text,
> THREE CHARACTERS AT A TIME. (I'm studying the genetic code you see, so
> I need to read and analyse each sequence one codon at a time
> effectively.) Does anyone have any idea of how to do this using python?
> 
> 
> I'm going to be optimistic and thank you for your help in advance!
> Samantha.
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Basic coin flipper program - logical error help

2006-02-21 Thread John Zenger
wes weston wrote:
>Looping is easier with:
> for x in range(100):
>if random.randint(0,1) == 0:
>   heads += 1
>else:
>   tails += 1

Also, with the functional programming tools of map, filter, and lambda, 
this code can be reduced to just six lines:

import random

flips = map(lambda x: random.randrange(2), xrange(100))
heads = len(filter(lambda x: x is 0, flips))
tails = len(filter(lambda x: x is not 0, flips))

print "The coin landed on heads", heads, "times."
print "The coin landed on tails", tails, "times."
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regular expresson for Unix and Dos Lineendings wanted

2006-02-23 Thread John Zenger
How about r"\s+[\n\r]+|\s+$"  ?

Franz Steinhaeusler wrote:
> Hello, I need a regularexpression, which trims trailing whitespaces.
> 
> While with unix line endings, it works; 
> but not with Window (Dos) CRLF's:
> 
> 
import re
retrailingwhitespace = re.compile('(?<=\S)[ \t]+$', re.MULTILINE)
> 
> 
> 1) Windows
> 
r="erewr\r\nafjdskl "
newtext, n = retrailingwhitespace.subn('', r)
n
> 
> 1
> 
newtext
> 
> 'erewr\r\nafjdskl'
> 
> 2) Unix
> 
r="erewr\nafjdskl "
newtext, n = retrailingwhitespace.subn('', r)
n
> 
> 2
> 
newtext
> 
> 'erewr\nafjdskl'
> 
> 
> Who can help me (regular expression, which works for both cases).
> 
> Thank you in advance!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regular expresson for Unix and Dos Lineendings wanted

2006-02-24 Thread John Zenger
Franz Steinhaeusler wrote:
> 
> Thank you all for the replies.
> But I still don't have a solution.
> 
> Of course with more lines it is possible, 
> but it would be fine to have a "oneliner".

re.sub(r"\s+[\n\r]+", lambda x: x.expand("\g<0>"). \
 lstrip(" \t\f\v"),text).rstrip()

...where "text" is the unsplit block of text with mysterious line-endings.

But I think your code is a lot easier to read.  :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie question

2006-03-01 Thread John Zenger
orangeDinosaur wrote:

> I wrote up a script in my preferred text editor.  It contains maybe ten
> lines of code.  I want to be able to execute those code lines with a
> single command either from the inline mode or from IDLE.  How do I do
> this?  I saved the file (myscript.py) in a folder that I've specified
> in my PYTHONPATH environment variable, and when I type

 From the Python shell, you can use execfile to run a script:

 >>> execfile("joshua.py")

This works regardless of your OS.  The file must be in your Python path. 
  If it isn't, just specify the full path.  Like:

 >>> execfile(r"C:\Code\WOPR\Backdoor\joshua.py")

While viewing your code with IDLE, hit F5 to execute it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple py script to calc folder sizes

2006-03-21 Thread John Zenger
Caleb Hattingh wrote:
> Hi everyone
> 
> [Short version: I put a some code below: what changes can make it run
> faster?]

On my slow notebook, your code takes about 1.5 seconds to do my 
C:\Python24 dir.  With a few changes my code does it in about 1 second.

Here is my code:

import os, os.path, math

def foldersize(fdir):
 """Returns the size of all data in folder fdir in bytes"""
 root, dirs, files = os.walk(fdir).next()
 files = [os.path.join(root, x) for x in files]
 dirs = [os.path.join(root, x) for x in dirs]
 return sum(map(os.path.getsize, files)) + sum(map(foldersize, dirs))

suffixes = ['bytes','kb','mb','gb','tb']
def prettier(bytesize):
 """Convert a number in bytes to a string in MB, GB, etc"""
 # What power of 1024 is less than or equal to bytesize?
 exponent = int(math.log(bytesize, 1024))
 if exponent > 4:
 return "%d bytes" % bytesize
 return "%8.2f %s" % (bytesize / 1024.0 ** exponent, suffixes[exponent])

rootfolders = [i for i in os.listdir('.') if os.path.isdir(i)]
results = [ (foldersize(folder), folder) for folder in rootfolders ]

for size, folder in sorted(results):
 print "%s\t%s" % (folder, prettier(size))

print
print "Total:\t%s" % prettier(sum ( size for size, folder in results ))

# End

The biggest change I made was to use os.walk rather than os.path.walk. 
os.walk is newer, and a bit easier to understand; it takes just a single 
directory path as an argument, and returns a nice generator object that 
you can use in a for loop to walk the entire tree.  I use it in a 
somewhat unconventional way here.  Look at the docs for a more 
conventional application.

The "map(os.path.getsize, files)" code should run a bit faster than a 
for loop, because map only has to look up the getsize function once.

I use log in the "prettier" function rather than your chain of ifs.  The 
chain of ifs might actually be faster.  But I spent so long studying 
math in school that I like to use it whenever I get a chance.

Some other comments on your code:

> def cmpfunc(a,b):
> if a.count > b.count:
> return 1
> elif a.count == b.count:
> return 0
> else:
> return -1

This could be just "return a.count - b.count".  Cmp does not require -1 
or +1, just a positive, negative, or zero.

> foldersizeobjects.sort(cmpfunc)

You could also use the key parameter; it is usually faster than a cmp 
function.  As you can see, I used a tuple; the sort functions by default 
sort on the first element of the tuples.  Of course, sorting is not a 
serious bottleneck in either program.

> tot=0
> for foldersize in foldersizeobjects:
> tot=tot+foldersize.count
> print foldersize

"tot +=" is cooler than tot = tot + .  And perhaps a bit faster.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "For" loop and list comprehension similarity

2006-03-26 Thread John Zenger
Rather than a list comprehension, it would be faster and more 
memory-efficient to use a generator comprehension.  Just change the 
square brackets to parentheses:

 for j in (i*2 for i in c if ):
print j


Grant Edwards wrote:
> On 2006-03-26, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> 
>>Hi All,
>>
>>I apologize if this was brought up before, I couldn't find any "prior
>>art" :-).
>>On more than one occasion, I found myself wanting to use a "conditional
>>loop" like this (with "Invalid syntax" error, of course):
>>
>>  for i in c if :
>>  print i*2
>>
>>...because it's similar to the list comprehension construct:
>>
>>  [i*2 for i in c if ]
>>-
>>
>>Is this the intended difference in constructs? The available equivalent
>>feels a bit awkward:
>>
>>  for i in c:
>>  if :
>>  print i*2
> 
> 
>for j in [i*2 for i in c if ]:
>   print j
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: wildcard exclusion in cartesian products

2006-03-26 Thread John Zenger
A quick fix:  change your last two functions to:

def generateNotMatching(A,n,P):
 for g in gen(A,n,P,[]):
 for x in g:
 yield x

def gen(A,n,P,acc):
 if any(imap((lambda p:  allStar(p) and notNullOrZero(p,n)), P)):
yield []
 else:
if n==0:
   yield map(rev,[acc])
else:
   for a in A:
   for xx in gen(A,n-1,advancePatterns(a,P),[a]+acc):
   yield xx

For the most part, just replace return with yield.

By the way:  you are reinventing the wheel with imap and rev.  imap is 
itertools.imap.  rev(L) is the same as L[:-1].

[EMAIL PROTECTED] wrote:
> The python code below is adapted from a Haskell program written by
> Tomasz
> Wielonka on the comp.lang.functional group. It's more verbose than his
> since I wanted to make sure I got it right.
> 
> http://groups.google.com/group/comp.lang.functional/browse_frm/thread...
> 
> Does anyone know how to turn it into a module (or whatever it's called)
> so that I can put it in a
> loop and not have to generate the whole list? I've already tried
> without success.
> 
> The program solves the following problem: generate the subset X of the
> cartesian product S^n that excludes n-tuples defined by wildcards. For
> example, if I want to exclude from [1,2,3]^3 the wc's [1,"*",2] and
> ["*",3,"*",3,"*"], where "*" stands for a sequence of zero or more
> elements of [1,2,3], then I just type
> 
> for x in generateNotMatching([1,2,3,4],4,[[1,'*',2],[3,'*',4]]): print
> x
> 
> and get the output
> 
>[1, 1, 1]
>[1, 1, 3]
>[1, 2, 1]
>[1, 2, 3]
>[1, 3, 1]
>[2, 1, 1]
>[2, 1, 2]
>[2, 1, 3]
>[2, 2, 1]
>[2, 2, 2]
>[2, 2, 3]
>[2, 3, 1]
>[2, 3, 2]
>[3, 1, 1]
>[3, 1, 2]
>[3, 2, 1]
>[3, 2, 2]
> 
> This is nice! But all elements are generated before they are printed.
> 
> ##
> import sys, os, string
> 
> def imap(function, source):
> for element in source:
> yield function(element)
> 
> def any(iterable):
>  '''True iff at least one element of the iterable is True'''
>  for element in iterable:
> if element:
> return True # or element and change the definition
>  return False
> 
> def all(iterable):
>  '''True iff no element of the iterable is True'''
>  '''SHOULD BE?: True iff all element of the iterable are True'''
>  for element in iterable:
> if not element:
> return False
>  return True
> 
> def rev(L):
> rL=[]
> for x in L: rL=[x]+rL
> return rL
> 
> def advancePattern(x,p):
> if p==[]: return []
> else:
>h=p[0]
>t=p[1:]
>if h != '*':
>   if h == x: return [t]
>   else: return []
>else: return [p] + [t] + advancePattern(x,t)
> 
> def advancePatterns(x,P):
> aP=[]
> for p in P:
> aP += advancePattern(x,p)
> return aP
> 
> #return [advancePattern(x,p) for p in P]
> 
> def allStar(p):
> return all( imap((lambda x: x=='*'),p) )
> 
> def notNullOrZero(p,n):
> return p !=[] or n==0
> 
> def generateNotMatching(A,n,P):
> return gen(A,n,P,[])
> 
> def gen(A,n,P,acc):
> if any(imap((lambda p:  allStar(p) and notNullOrZero(p,n)), P)):
>return []
> else:
>if n==0:
>   return map(rev,[acc])
>else:
>   aG=[]
>   for a in A:
>   aG += gen(A,n-1,advancePatterns(a,P),[a]+acc)
>   return aG
> 
> ##
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: very strange problem in 2.4

2006-04-07 Thread John Zenger
Your list probably contains several references to the same object, 
instead of several different objects.  This happens often when you use a 
technique like:

list = [ object ] * 100

..because although this does make copies when "object" is an integer, it 
just makes references in other cases.

[EMAIL PROTECTED] wrote:
> The Problem (very basic, but strange):
> 
> I have a list holding a population of objects, each object has 5 vars
> and appropriate funtions to get or modify the vars.  When objects in
> the list have identical vars (like all = 5 for var "a" and all = 10 for
> var "b" across all vars and objects) and i change
> 
> self.mylist[i].change_var_a(5)
> 
> to a new value, in this case var "a" in object i to 5, now all vars of
> type "a" in all objects in my list are changed to 5 instead of just var
> "a" in object mylist[i], which is my goal.
> 
> if i print self.mylist[i].return_var_a() right after I change var "a"
> in object i, I get the correct change, however when i print out the
> whole list, the last change to var "a" in the last object modified
> takes over for all objects in the list.
> 
> note: all the vars not being modified must be the same across all
> objects in the list, the var being modified need not be the same as the
> one before it in the list (but will be once just one of the identical
> object are changed).  The value changed in the last object var modified
> takes over for all object vars making them exactly identical.
> 
> If, for example, half the list has objects with random vars init.
> and the other half is identical, as above, and I perform the same
> operation, as above, to one of the identical var objects
> 
> self.mylist[i].change_var_a(5) (to an object that has identicals in the
> list)
> 
> all the identicals are changed in the same way as above, however the
> objects that have different var values are unchanged.
> 
> 
> 
> What is python doing? Am I missing something? Any ideas at all would be
> wonderful?
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how relevant is C today?

2006-04-08 Thread John Zenger
Martin v. Löwis wrote:

> As for *learning* the languages: never learn a language without a
> specific inducement. If you know you are going to write a Python
> extension, an Apache module, or a Linux kernel module in the
> near future, start learning C today. If you don't know what you
> want to use it for, learning it might be a waste of time, as
> you won't know what to look for if you don't have a specific project
> in mind.

Your message makes me sad, as if I heard someone say "never read a book 
without a specific inducement; if you know someone is going to ask you 
about the book, start reading it today, but if you don't know what you 
are going to use the book for, reading it will be a waste of time."

Programming languages are intellectual achievements and you can learn a 
lot every time you study a new one (provided it is different enough from 
the ones you already know.)  Even if you don't have an immediate project 
in mind, learning a new programming language can mean learning a new 
style of programming, or at least a new way of looking at computer 
science.  That is worthwhile even if you get no immediate use from the 
new language.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multiple parameters in if statement

2006-04-15 Thread John Zenger
Try this:

if form.get("delete_id","") != "" and form.get("delete_data","") != "" 
and...

the "get" method lets you have an optional second argument that gets 
returned if the key is not in the dictionary.

Also, am I reading your code right?  If I enter some fields but not all, 
you print a message that says "Nothing entered."  Nothing?

The other thing I'd recommend is stick that long list of fields in a 
list, and then do operations on that list:

fields = ['delete_id', 'delete_date', 'delete_purchasetype', 
'delete_price', 'delete_comment']

then to see if all those fields are empty:

everything = ""
for field in fields:
 everything += form.get(field,"")
if everything == "":
 print "Absolutely nothing entered!"

Kun wrote:
> I am trying to make an if-statement that will not do anything and print 
> 'nothing entered' if there is nothing entered in a form.  I have the 
> following code that does that, however, now even if I enter something 
> into the form, the code still outputs 'nothing entered'.  This violates 
> the if statement and I am wondering what I did wrong.
> 
> if form.has_key("delete_id") and form["delete_id"].value != "" and 
> form.has_key("delete_date") and form["delete_date"].value != "" and 
> form.has_key("delete_purchasetype") and 
> form["delete_purchasetype"].value != "" and form.has_key("delete_price") 
> and form["delete_price"].value != "" and form.has_key("delete_comment") 
> and form["delete_comment"].value != "":
> delete_id=form['delete_id'].value
> delete_date=form['delete_date'].value
> delete_purchasetype=form['delete_purchasetype'].value
> delete_price=form['delete_price'].value
> delete_comment=form['delete_comment'].value
> else:
> print "ERROR: Nothing entered!"
> raise Exception
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multiple parameters in if statement

2006-04-16 Thread John Zenger
Yup, join is better.  The problem with using form.values() is that it 
will break if the HTML changes and adds some sort of new field that this 
function does not care about, or if an attacker introduces bogus fields 
into his query.

John Machin wrote:
> On 16/04/2006 1:43 PM, John Zenger wrote:
> 
>>
>> The other thing I'd recommend is stick that long list of fields in a 
>> list, and then do operations on that list:
>>
>> fields = ['delete_id', 'delete_date', 'delete_purchasetype', 
>> 'delete_price', 'delete_comment']
>>
>> then to see if all those fields are empty:
>>
>> everything = ""
>> for field in fields:
>> everything += form.get(field,"")
> 
> 
> Or everything = "".join(form.get(field, "") for field in fields)
> 
> Somewhat labour-intensive. It appears from the OP's description that no 
> other entries can exist in the dictionary. If this is so, then:
> 
> everything = "".join(form.values())
> 
> but what the user sees on screen isn't necessarily what you get, so:
> 
> everything = "".join(form.values()).strip()
> 
>> if everything == "":
>> print "Absolutely nothing entered!"
>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to initialize a table of months.

2007-04-15 Thread John Zenger
On Apr 15, 9:30 pm, "Steven W. Orr" <[EMAIL PROTECTED]> wrote:
> I'm reading a logfile with a timestamp at the begging of each line, e.g.,
>
> Mar 29 08:29:00
>
> I want to call datetime.datetim() whose arg2 is a number between 1-12 so I
> have to convert the month to an integer.
> I wrote this, but I have a sneaky suspicion there's a better way to do it.
>
> mons = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6,
>  'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12 }
>
> def mon2int( mon ):
>  global mons
>  return mons[mon]
>
> Is there a generator expression or a list comprehension thingy that would
> be *betterer*? (I realize it's probably not that important but I find lots
> of value in learning all the idioms.)
>
> TIA


Well, I think you want time.strptime.

>>> time.strptime("Mar 29 08:29:00", "%b %d %H:%M:%S")
(1900, 3, 29, 8, 29, 0, 3, 88, -1)

See http://docs.python.org/lib/node85.html

However, if strptime did not exist, your dictionary solution is fine
-- a tad bit slow, but easy and maintainable, which is worth a lot.

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


Re: Using the Python interpreter

2007-04-19 Thread John Zenger
On Apr 18, 8:32 pm, [EMAIL PROTECTED] wrote:
> Instead of starting IDLE as I normally do, I started the Python
> interpreter and tried to run a program. I got a Python prompt (>>>),
> and then tried unsuccessfully to run a Python script named Script1.py
> that runs perfectly well in IDLE. Here's what I did:
>
> >>>Script1.py
>
> Traceback (most recent call last):
>   File "", line 1, in ?
> NameError: name Script1 is not defined
>
> >>>python Script1.py
>
>   File "", line 1
>python Script1.py
> SyntaxError: invalid syntax
>
> I can load it (and have it execute) by typing
>
> >>>import Script1
>
> 0
> 1
> 2
> 3
> 4
>
> and if I edit it, I can then execute it by reloading it
>
> >>>import Script1
>
> 0
> 1
> 2
> 3
> 4
> 
>
> But this seems contrived - is there no way to repeatedly run Script1
> from the interpreter without reloading it?
>
> Thomas Philips

You want execfile:

>>> execfile("Script1.py")

See http://pyref.infogami.com/execfile

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


Re: when format strings attack

2007-01-19 Thread John Zenger
Perhaps it is not as severe a security risk, but pure Python programs
can run into similar problems if they don't check user input for %
codes.  Example:

>>> k = raw_input("Try to trick me: ")
Try to trick me: How about %s this?
>>> j = "User %s just entered: " + k
>>> print j % "John"
Traceback (most recent call last):
  File "", line 1, in ?
print j % "John"
TypeError: not enough arguments for format string



On Jan 19, 10:44 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> <[EMAIL PROTECTED]> escribió en el mensajenews:[EMAIL PROTECTED]
>
> >http://www.ddj.com/184405774;jsessionid=BDDEMUGJOPXUMQSNDLQCKHSCJUNN2JVN
>
> > I saw a warning from homeland security about this.  I only comment on
> > the because I am trying to use os.system('command1 arg') and it doesn't
> > work but I do see examples with % that is borrowed from the c language.
> > Seems like if I can write a batch file that does something the same
> > behavior should happen in the os module..Pure Python programs are not 
> > affected, but a review of the C implementation
> should be made to see if any (variant of) printf is used without a proper
> format. Anyway I doubt you could find something, because the vulnerability
> is so well known for ages.
> 
> --
> Gabriel Genellina

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


Re: removing common elemets in a list

2007-05-16 Thread John Zenger
On May 16, 2:17 am, [EMAIL PROTECTED] wrote:
> Hi,
>  Suppose i have a list v which collects some numbers,how do i
> remove the common elements from it ,without using the set() opeartor.
>   Thanks

Submit this as your homework answer -- it will blow your TA's mind!

import base64
def uniq(v):
return
eval(base64.b64decode('c29ydGVkKGxpc3Qoc2V0KHYpKSxrZXk9bGFtYmRhIHg6di5pbmRleCh4KSk='),locals())

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


Re: remove all elements in a list with a particular value

2007-05-16 Thread John Zenger
On May 16, 11:21 am, Lisa <[EMAIL PROTECTED]> wrote:
> I am reading in data from a text file.  I want to enter each value on
> the line into a list and retain the order of the elements.  The number
> of elements and spacing between them varies, but a typical line looks
> like:
>
> '   SRCPARAM   1 6.35e-07 15.00  340.00   1.10  3.0   '
>
> Why does the following not work:
>
> line = '   SRCPARAM   1 6.35e-07 15.00  340.00   1.10  3.0   '
> li = line.split(' ')
> for j,i in enumerate(li):
> if i == '':
> li.remove(i)
>
> After the original split I get:
> ['', '', '', 'SRCPARAM', '', '', '1', '6.35e-07', '15.00', '',
> '340.00', '', '', '1.10', '', '', '', '', '', '3.0', '', '', '']
>
> And after the for loop I get:
> ['SRCPARAM', '1', '6.35e-07', '15.00', '340.00', '1.10', '', '', '',
> '3.0', '', '', '']
>
> It doesn't remove all of the empty elements.  Is there a better way to
> split the original string?  Or a way to catch all of the empty
> elements?
>
> Thanks

As explained elsewhere, this is not the best way to do a split.  But
if in the future you run into the problem of how to remove all
elements in a list with a particular value, here are two popular
approaches:

>>> items =  ['', '', '', 'SRCPARAM', '', '', '1', '6.35e-07', '15.00', '',
'340.00', '', '', '1.10', '', '', '', '', '', '3.0', '', '', '']
>>> print filter(lambda i: i != '', items)
['SRCPARAM', '1', '6.35e-07', '15.00', '340.00', '1.10', '3.0']
>>> print [x for x in items if x != '']
['SRCPARAM', '1', '6.35e-07', '15.00', '340.00', '1.10', '3.0']

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