Re: map/filter/reduce/lambda opinions and background unscientificmini-survey

2005-07-07 Thread Chris Rebert (cybercobra)
Agreed, I dislike map and its ilk as well.
However, they are handy in some cases. I particularly like the way Ruby
deals with this problem. Instead of all these functions, internal
iterators and true anonymous blocks are used.
Case in point:

def gt_than_5(obj):
return obj > 5
results = filter(gt_than_5, seq)

becomes

results = seq.find_all do |item|
  item > 5
end

This has the advantage of writing less code for the common cases by
having them builtin to the class. Instead of everyone needlessly
recoding these over and over again, they're implemented in one place.
This could be done by defining an abstract class that enumerable
objects inherit from. It's not practical, but I can dream, can't I? ;-)
Also, you don't have to define a separate testing function. Granted, in
this case, the test func is trivial, but when it's non-trivial, you
really notice the advantages.

Ivan Van Laningham wrote:
> Hi All--
>
> Tom Anderson wrote:
> >
> > Comrades,
> >
> > "I expect tons of disagreement in the feedback, all from ex-Lisp-or-Scheme
> > folks. :-)"
> >
> > I disagree strongly with Guido's proposals, and i am not an ex-Lisp,
> > -Scheme or -any-other-functional-language programmer; my only other real
> > language is Java. I wonder if i'm an outlier.
> >
> > So, if you're a pythonista who loves map and lambda, and disagrees with
> > Guido, what's your background? Functional or not?
> >
>
> I'm a pythonista who doesn't love them.  In fact, even though I've done
> more than my fair share of lambda Tkinter programming using lambdas,
> I've never been happy with lambda.  And I've spent months inside of
> Lisp/Emacs Lisp/Scheme, too (I have the world's second largest .emacs
> file [my friend Andy Glew has the largest], even though I can't use it
> on Windows;-).  I find list comprehensions easier to understand than
> map, and small named functions or, better yet, class methods, *tons*
> easier to read/understand than lambda--there are too many restrictions
> you have to remember.
>
> Personally, I find that Lisp & its derivatives put your head in a very
> weird place.  Even weirder than PostScript/Forth/RPN, when you come
> right down to it.
>
> I won't miss them, but since I don't use them now, that doesn't mean a
> whole lot.
>
> Metta,
> Ivan
> --
> Ivan Van Laningham
> God N Locomotive Works
> http://www.andi-holmes.com/
> http://www.foretec.com/python/workshops/1998-11/proceedings.html
> Army Signal Corps:  Cu Chi, Class of '70
> Author:  Teach Yourself Python in 24 Hours

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


Re: Favorite non-python language trick?

2005-07-01 Thread Chris Rebert (cybercobra)
My personal favorite would be ruby's iterators and blocks.
Instead of writing a bunch of repetitive list comprehensions or
defining a bunch of utility functions, you just use the iterators
supported by container objects.
For instance,

[f(x) for x in y]

could be written in Ruby as

y.collect |x| do
  #body of f
end

You don't have to use a lambda or define f() externally.
Best of all, Ruby's containers come with many iterators for common
cases builtin.

Joseph Garvin wrote:
> As someone who learned C first, when I came to Python everytime I read
> about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(),
> getattr/setattr, the % operator, all of this was very different from C.
>
> I'm curious -- what is everyone's favorite trick from a non-python
> language? And -- why isn't it in Python?
>
> Here's my current candidate:
>
> So the other day I was looking at the language Lua. In Lua, you make a
> line a comment with two dashes:
>
> -- hey, this is a comment.
>
> And you can do block comments with --[[ and ---]].
>
> --[[
> hey
> this
> is
> a
> big
> comment
> --]]
>
> This syntax lets you do a nifty trick, where you can add or subtract a
> third dash to change whether or not code runs:
>
> --This code won't run because it's in a comment block
> --[[
> print(10)
> --]]
>
> --This code will, because the first two dashes make the rest a comment,
> breaking the block
> ---[[
> print(10)
> --]]
>
> So you can change whether or not code is commented out just by adding a
> dash. This is much nicer than in C or Python having to get rid of """ or
> /* and */. Of course, the IDE can compensate. But it's still neat :)

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


Re: Archives and magic bytes

2005-03-23 Thread Chris Rebert (cybercobra)
Have you tried the tarfile or zipfile modules? You might need to ugrade
your python if you don't have them. They look pretty easy and should
make this a snap.
You can grab the output from the *nix "file" command using the new
subprocess module.
Good Luck
- Chris
===
PYTHON POWERs all!
All your code are belong to Python!

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


Re: Simple account program

2005-03-21 Thread Chris Rebert (cybercobra)
You probably want:

 import pickle
 pickle.dump(myAccount, file("account.pickle", "w"))

To reload your account:
 import pickle
 myAccount = pickle.load(file("account.pickle"))

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


Re: missing? dictionary methods

2005-03-21 Thread Chris Rebert (cybercobra)

Antoon Pardon wrote:
> Well at least I find them missing.
>
> For the moment I frequently come across the following cases.
>
> 1) Two files, each with key-value pairs for the same dictionary.
> However it is an error if the second file contains a key that
> was not in the first file.
>
> In treating the second file I miss a 'set' method.
> dct.set(key, value) would be equivallent to dct[key] = value,
> except that it would raise a KeyError if the key wasn't
> already in the dictionary.
>
>
> 2) One file with key-value pairs. However it is an error
> if a key is duplicated in the file.
>
> In treating such files I miss a 'make' method.
> dct.make(key, value) would be equivallent to dct[key] = value.
> except that it would raise a KeyError if the key was
> already in the dictionary.
>
>
> What do other people think about this?
>
> --
> Antoon Pardon

If (1) gets accepted, I propose the name .change(key, val)  It's
simple, logical, and makes sense.

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


Re: (",) Do You Want To Know For Sure You Are Going To Heaven?

2005-03-20 Thread Chris Rebert (cybercobra)
Please do STFU! You are most annoying and this is the wrong place to
post this.
If anyone else if reading this, yes, I do realize it's probably a bot.
But I need to vent some rage. Now if only his nick didn't have a "..."
in it... I would so report it to yahoo and/or counterspam it.

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


Re: getting text from WinXP console

2005-03-20 Thread Chris Rebert (cybercobra)
You want the function raw_input().
Have you read the tutorial? I should have been covered there.

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


Re: Simple account program

2005-03-18 Thread Chris Rebert (cybercobra)
Since everyone's improving the program, I thought I'd just tweak Dennis
Lee Bieber's code a little. Here's the result. Good luck Ignorati!

import time

class Transaction(object):
def __init__(self, payee, amount, date=None):
# amount is the amt withdrawn/deposited
self.payee = payee
self.amount = amount
if date is None:
date = time.localtime()
self.date = date

def formatEntry(self):
if self.amount > 0:
transType = "DEPOSIT"
elif self.amount < 0:
transType = "WITHDRAWAL"
return transType+"\t\t%s\t%s\t%s" % ((self.date[0],
  self.date[1],
  self.date[2]),
 self.amount,
 self.payee)

class Account(object):
def __init__(self, acctName, initialBalance):
self.acctName = accName
self.transacts = [Transaction("Initial Balance",
initialBalance)]
self.balance = initialBalance

def deposit(self, payee, amt, date=None):
assert amt >= 0, "amt must be positive for a deposit, got
"+str(amt)
self.transacts.append(Transaction(payee, amt, date))
self.balance += amt

def withdraw(self, payee, amt, date=None):
assert amt <= 0, "amt must be negative for a withdrawl, got
"+str(amt)
self.transacts.append(Transaction(payee, amt, date))
self.balance -= amt

def printRegister(self):
print "Transaction\tDate\t\tAmount\tPayee"
for t in self.transacts:
print t.formatEntry()

if __name__ == "__main__":
myAccount = Account("Sample Account", 0.0)

while True:
print """
Select transaction type:
D)eposit
W)ithdrawal
P)rint register

Q)uit
"""

rep = raw_input("Enter your choice => ")
rep = rep[0].upper()

if rep == "D" or rep == "W":
who = raw_input("Enter payee name => ")
amt = float(raw_input("Enter transaction amount => "))
if rep == "D":
myAccount.deposit(who, amt)
else:
myAccount.withdraw(who, amt)

elif rep == "P":
print "\n"
myAccount.printRegister()
print "\n"

elif rep == "Q":
break

else:
print "Invalid Input, Please Try Again"
print

print "\tCurrent Balance: %s\n" % myAccount.balance

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