Re: Classes as namespaces?

2010-03-27 Thread J. Clifford Dyer
On Fri, Mar 26, 2010 at 02:49:02PM +, kj wrote regarding Classes as 
namespaces?:
> 
> What's the word on using "classes as namespaces"?  E.g.
> 
> class _cfg(object):
> spam = 1
> jambon = 3 
> huevos = 2
> 
> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
> 
> 
> Granted, this is not the "intended use" for classes, and therefore
> could be viewed as a misuse ("that's what dictionaries are for",
> etc.).  But other than this somewhat academic objection[*], I really
> can see no problem with using classes in this way.
> 
> And yet, I've come across online murky warnings against using
> classes as "pseudo-namespaces".  Is there some problem that I'm
> not seeing with this technique?
> 
> ~K

I don't see anything wrong with this, except that I would clean it up in a 
couple ways.  Like other posters, I would give the class a proper class name 
(Cfg).

I also would not assign integers to spam, jambon, or huevos.  Instead I would 
assign each a bare object().  That way you won't get unexpected interactions 
with other constants outside the class.  An object() is equal only to itself.

I would also not rule out letting your "pseudo-namespace" grow into a 
full-fledged class.  If you've got a method that makes sense with your class, 
use it.

class Cfg(object):
spam = object()
jambon = object()
huevos = object()

def get_animal(self, meat):
if meat == self.jambon:
return 'pig'
elif meat == self.huevos:
return 'chicken'
elif meat = self.spam:
return 'spamalope'

Later, perhaps, you might refactor so that each meat type (OK so huevos aren't 
a meat) gets its own subclass, with a simple, one-line get_animal method.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Classes as namespaces?

2010-03-27 Thread J. Clifford Dyer
On Sat, Mar 27, 2010 at 04:28:56AM -0700, Jonathan Hartley wrote regarding Re: 
Classes as namespaces?:
> 
> Hey everyone. By coincidence, only yesterday I was wondering about
> using classes as a way of labeling a block of code, ie. an lightweight
> alternative to defining a function that would only be called from one
> location.
> 
> eg. instead of:
> 
> 
> x = 1
> ((some complex logic))
> y = 2
> 
> 
> one might like to name the complex block of logic, just to make it
> readable:
> 
> 
> x = 1
> def account_for_non_square_pixels(x):
>((some complex logic))
> account_for_non_square_pixels()
> y = 2
> 
> 
> But defining and then calling the function like that is a tad
> cumbersome. So I was wondering about:
> 
> 
> 
> x = 1
> class account_for_non_square_pixels:
>   ((some complex logic))
> y = 2
> 
> 
> I don't exactly like this, but I think you can see what I'm getting
> at. Does this fall down in some way I haven't grasped? Is it as awful
> an abuse of 'class' as my intuition suggests it is? Is there a way to
> do it better?
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Hmm.  I don't like that because it leaves a class polluting your namespace that 
doesn't behave like a class.  Using a function for that purpose doesn't seem as 
bad, because even if you don't call it again, at least you *could*, and it 
would behave in an expected fashion.  

If you're dead-set against calling the chunk of code you just created, and 
you're using python 2.5 or higher, you might consider creating a no-op context 
manager:

x = 1
with code_block("Account for non square pixels"):
((complex_logic))
y = 2

Though in general, I think refactoring your code to reasonably scoped functions 
or methods is a better idea.  If it's too complex to read in one block, it's 
probably too complex for one function.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another Screwy Problem

2010-01-08 Thread J. Clifford Dyer
On Fri, Jan 08, 2010 at 03:32:34PM -0400, Victor Subervi wrote regarding 
Another Screwy Problem:
> Date: Fri, 8 Jan 2010 15:32:34 -0400
> From: Victor Subervi 
> To: python-list 
> Subject: Another Screwy Problem
> 
>Hi;
>I have this line of code:
> sql = 'select Name, Price from %sPackages where ID=%s;' % (store, pid)
>which prints to this:
> select Name, Price from productsPackages where ID=1;
>which when I enter it into the MySQL interpreter gives me this:
>mysql> select Name, Price from productsPackages where ID=1;
>+--++
>| Name | Price  |
>+--++
>| pkg  | 123.45 |
>+--++
>1 row in set (0.00 sec)
>exactly what I expect. However, in my script for some reason it returns
>this:
>((1,),)
>Why would it do that? I guess there's some foolish thing I did in my
>code somewhere, but I'll be darned if I know where. Here's the whole
>script:
>#! /usr/bin/python
>import cgitb; cgitb.enable()
>import cgi
>import MySQLdb
>import sys,os
>sys.path.append(os.getcwd())
>from login import login
>from template import top, bottom
>from sets import Set
>import fpformat
>form = cgi.FieldStorage()
>store = form.getfirst('store')
>cat = form.getfirst('cat', '')
>user, passwd, db, host = login()
>db = MySQLdb.connect(host, user, passwd, db)
>cursor = db.cursor()
>def displayProducts(patientID=''):
>  try: # These are stores with categories where ordering by price is
>important
>sql = 'select ID from %s where Category="%s" order by Price desc;'
>% (store, cat)
>cursor.execute(sql)
>  except: # Stores, like prescriptions, where ordering by price is not
>important
>sql = 'select ID from %s;' % (store)
>cursor.execute(sql)
>  ids = [itm[0] for itm in cursor]
>  cursor.execute('describe %s;' % store)
>  colFields, colFieldValues = [itm[0] for itm in cursor], [itm[1] for
>itm in cursor]
>  i = 0
>  if len(ids) > 0:
>for id in ids:
>#  print '\n'
>  print ''
>  print "" % store
>  print "" % id
>  j = 0
>  for col in colFields:
>sql = 'select %s from %s where ID="%s";' % (col, store,
>str(id))
>cursor.execute(sql)
>colValue = cursor.fetchone()
>if col == 'SKU':
>  print "" %
>colValue[0]
>  print '%s: %s\n' % (col, colValue[0])
>elif col == 'Category':
>  print "" %
>colValue[0]
>  print '%s: %s\n' % (col, colValue[0])
>elif col == 'OutOfStock':
>  if colValue[0] == '1': # This product is out of stock
>outOfStockFlag = 'yes'
>  else:
>outOfStockFlag = 'no'
>elif col[:3] != 'pic':
>  notSet = 1
>  if isinstance(colValue[0], (str, int, float, long, complex,
>unicode, list, buffer, xrange, tuple)):
>pass
>  else:
>try:
>  html = "%s: " % (col, col)
>  notSet = 0
>  for itm in colValue[0]:
>try:
>  color, number = itm.split('$')
>  html += "%s" % (itm,
>color)
>except:
>  html += "%s" % (itm, itm)
>  html += ""
>  print html
>except:
>  pass
>  if notSet == 1:
>if len(col) > 49:
>  colX = col[:50] + '...'
>else:
>   colX = col
>print '%s: %s\n' % (colX, colValue[0])
>elif col == 'pic1':
>  try:
>if (colValue[0] != None):
>#if (colValue[0] != None) and (len(colValue[0] > 0)):
>  print 'class="highslide" href="getpic.py?store=%s&pic=%s&id=%s"
>onclick="return hs.expand(this)">src="getpic.py?store=%s&pic=%s&id=%s" width="100" height="80" alt=""
>align="left" border="0" title="Click to enlarge" style="border:
>0px">\n' % (store, col[3:], id, store, col[3:],
>id, store, col[3:], id)
>  except TypeError:
>raise
>  except:
>raise
>#  i += 1
>#  try:
>#content = colValues[0][x].tostring()
>#pic = "tmp" + str(i) + ".jpg"
>#try:
>#  os.remove(pic)
>#except:
>#  pass
>#img = open(pic, "w")
>#img.write(content)
>#print '' % pic
>#img.close()
>#  except:
>#pass
>j += 1
>  if store != 'prescriptions':
>if outOfStockFlag == 'yes':
>  print 'This item is currently out of
>st

Re: Another Screwy Problem

2010-01-08 Thread J. Clifford Dyer
Victor Subervi wrote:
> Hi;
> I have this line of code:
>  sql = 'select Name, Price from %sPackages where ID=%s;' % (store, pid)
> which prints to this:
>  select Name, Price from productsPackages where ID=1;
> which when I enter it into the MySQL interpreter gives me this:
> mysql> select Name, Price from productsPackages where ID=1;
> +--++
> | Name | Price  |
> +--++
> | pkg  | 123.45 |
> +--++
> 1 row in set (0.00 sec)
> 
> exactly what I expect. However, in my script for some reason it returns
> this:
> ((1,),)


First, never use string formatting to pass parameters to your database.  Read 
the MySQLdb documentation (or sqlite, or psycopg2) documentation for reasons 
why, and how to do it right.

Second, in the same documentation, look up anything having the word "fetch" in 
it.  That should show you how to get the data you want

Third, please be more specific in your questions.  You say "In my script for 
some reason *it* returns this: ((1,),)," but you don't show us what "it" is.  
How did you get that from your script?  So far we've got a variable called sql 
with your query in it.  How do I get the same wrong result you got?  I don't 
know.  I could pass it to a function that looks like this:

def botch_sql_query(sql):
return ((1,),)

I could furthermore "fix" it, so that it looks right, by doing this:

def fixed_sql_query(sql):
return(('pkg', 123.45),)

But that probably doesn't help.  Give us enough code to be clear, but not so 
much as to be overwhelming.  There's an essay called "How to ask smart 
question" by Eric Raymond that should help.

Cheers,
Cliff

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


Re: count

2009-07-08 Thread J. Clifford Dyer
On Wed, 2009-07-08 at 14:45 -0700, Paul Rubin wrote:
> a...@pythoncraft.com (Aahz) writes:
> > >Avoid that len(tuple(g)), use something like the following, it's lazy
> > >and saves some memory.
> > The question is whether it saves time, have you tested it?
> 
> len(tuple(xrange(1))) ... hmm.

timer.py

from datetime import datetime

def tupler(n):
return len(tuple(xrange(n)))

def summer(n):
return sum(1 for x in xrange(n))

def test_func(f, n):
print f.__name__,
start = datetime.now()
print f(n)
end = datetime.now()
print "Start: %s" % start
print "End: %s" % end
print "Duration: %s" % (end - start,)

if __name__ == '__main__':
test_func(summer, 1000)
test_func(tupler, 1000)
test_func(summer, 1)
test_func(tupler, 1)

$ python timer.py
summer 1000
Start: 2009-07-08 22:02:13.216689
End: 2009-07-08 22:02:15.855931
Duration: 0:00:02.639242
tupler 1000
Start: 2009-07-08 22:02:15.856122
End: 2009-07-08 22:02:16.743153
Duration: 0:00:00.887031
summer 1
Start: 2009-07-08 22:02:16.743863
End: 2009-07-08 22:02:49.372756
Duration: 0:00:32.628893
Killed
$ 

Note that "Killed" did not come from anything I did.  The tupler just
bombed out when the tuple got too big for it to handle.  Tupler was
faster for as large an input as it could handle, as well as for small
inputs (test not shown).

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


Re: Paramiko help - processing multiple commands

2009-06-25 Thread J. Clifford Dyer
On Wed, 2009-06-24 at 15:22 -0700, Frank Ruiz wrote:
> Greetings,
> 
> I am trying to process multiple commands using paramiko. I have
> searched other threads, and I think my use case is a little different.
> I am trying to login to a storage node that has a special shell, and
> as such I cant execute a script on the storage node side.
> 
> I am also trying to avoid using pexpect because I hate making system
> calls.. hence my leaning towards paramiko.
> 
> Was hoping someone could help me identify a way to process multiple
> commands using paramiko.
> 
> I have two commands listed below, however only one is getting processed.
> 
> Any help is much appreciated.
> 
> Thanks!
> 
> Here is my script:
> 
> #!/usr/bin/env python
> 
> 
> #-Modules-
> import optparse
> import sys
> import paramiko
> 
> 
> #-Variables---
> plog = 'storagessh.log'
> suser = 'root'
> 
> 
> #-Config--
> 
> #-Subs-Defined
> def options():
> global hostname
> global goldenimage
> global lunclone
> global sshport
> 
> usage = "usage: %prog [options] -n  -g  -l "
> 
> parser = optparse.OptionParser(usage)
> 
> parser.add_option("-n", "--node",
>   dest="hostname",
>   help="Name of storage node you are connecting to.")
> parser.add_option("-g", "--gold",
>   dest="goldenimage",
>   help="Name of goldenimage to clone.")
> parser.add_option("-l", "--lun",
>   dest="lunclone",
>   help="Name of lun to create.")
> parser.add_option("-p", "--port",
>   dest="sshport",
>   default=22,
>   help="SSH port number.")
> options, args = parser.parse_args()
> 
> if not options.hostname:
> parser.error("Missing hostname argument.")
> exit
> elif not options.goldenimage:
> parser.error("Missing goldenimage argument.")
> exit
> elif not options.lunclone:
> parser.error("Missing lun argument.")
> exit
> 
> hostname = options.hostname
> goldenimage = options.goldenimage
> lunclone = options.lunclone
> sshport = options.sshport
> 

It looks like you are trying to create a configuration class, but going
through extra gyrations to use global variables instead.  Perl's class
system is rather convoluted, but it is straightforward in python, and
will make your code easier to maintain.  I recommend you take a look at
the section on classes in the python tutorial, and play around with it.
Not everything needs to be a class in python, but in certain cases (and
this is one of them) it will make things much cleaner.

> def storagessh():
> paramiko.util.log_to_file(plog)
> client = paramiko.SSHClient()
> client.load_system_host_keys()
> client.connect(hostname, sshport, suser)
> stdin, stdout, stderr = client.exec_command('show')
> stdin, stdout, stderr = client.exec_command('help')
> print stdout.read()
> client.close()
> 

You just did it.  You processed two commands.  The only problem is that
you wrote values to stdin, stdout, and stderr with the show command, and
then immediately overwrote them with the help command.  Use different
variable names for each command (like stdout_show and stdout_help), and
you should be fine.

> 
> 
> 
> #--Initialization-
> if __name__ == "__main__":
> options()
> storagessh()

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


Re: It's ...

2009-06-24 Thread J. Clifford Dyer
On Wed, 2009-06-24 at 14:54 -0700, Aahz wrote:
> In article ,
> J. Cliff Dyer  wrote:
> >
> >Glad you're enjoying Beazley.  I would look for something more
> >up-to-date.  Python's come a long way since 2.1.  I'd hate for you to
> >miss out on all the iterators, booleans, codecs, subprocess, yield,
> >unified int/longs, decorators, decimals, sets, context managers and
> >new-style classes that have come since then.
> 
> While those are all nice, they certainly aren't essential to learning
> Python.

Mostly, no, you are correct.  With some exceptions:

1) You have to know iterators at a basic level (not enough to understand
how the iterator protocol works, but enough to know what 'for line in
f:' does.

2) Sets are as essential as any other data structure.  If you are
learning both lists and tuples, you should be learning sets as well.

3) If you're learning object-oriented programmin, new-style classes
should be the only classes you use.

4) You should know how a decorator works, in case you run across one in
the wild.  

5) Booleans are a basic type.  You should know them.

Codecs, the subprocess module, yield, decimals and context managers can
certainly come later.  (All this of course, is assuming the Python 2.x
world, which I think is still the right way to learn, for now)


Cheers,
Cliff

> -- 
> Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/
> 
> "as long as we like the same operating system, things are cool." --piranha

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


Re: reseting an iterator

2009-05-22 Thread J. Clifford Dyer
On Fri, 2009-05-22 at 10:54 -0700, Jan wrote:
> This produces an error because by definition of for-loops
> it is executed the same way as:
> 
> temp_iterator = iter(y) # temp_iterator is y
> while True:
> try:
> print(next(temp_iterator)) # temp_iterator does not support
> __next__()
> except StopIteration:
> break
> 

I think this is where you missed my point.

iter(y) actually returns an instance of class X, which does support
iteration.  And it returns a new X each time, thus resetting the
iterator.

That exact setup might or might not support your use case.  I don't
know, because you haven't described it.  However, whatever you need done
to X to get it back in shape to reiterate over can be done in
Y.__iter__().

Honestly, do you care if it's an iterator or an iterable, so long as
python can handle the job?




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


Re: unicode bit me

2009-05-09 Thread J. Clifford Dyer
You're still not asking questions in a way that we can answer them.

Define "Doesn't work."  Define "a".


On Sat, 2009-05-09 at 00:04 -0700, anuraguni...@yahoo.com wrote:
> also not sure why (python 2.5)
> print a # works
> print unicode(a) # works
> print [a] # works
> print unicode([a]) # doesn't works
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

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


RE: Wing vs Netbeans IDE?

2009-05-07 Thread J. Clifford Dyer
On Thu, 2009-05-07 at 10:49 -0700, Benjamin J. Racine wrote:
> I'd love to see an updated shootout between these three, as I cannot for the 
> life of me seem to be able to settle down with one of them.
> 
> Things I don't like: 
> Wing's lack of integrated mercurial/svn support.

Wing *does* have integrated SVN support already, but nothing for the
distributed systems yet.  I just purchased a Wing license this past
week.  So far I'm very happy with it, though I can't speak to a
comparison with other IDEs. 




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


Re: Web validation

2009-04-07 Thread J. Clifford Dyer
On Tue, 2009-04-07 at 08:44 +1000, r-w wrote:
> If no internet connection:
>  if have files:
>  run anyway, with warning
>  else:
>  ERROR
> else:
>  if error getting hash/files:
>  if have files:
>  run anyway, with warning
>  else:
>  ERROR
>  else:
>  run
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

import logging

def run_with_files(files):
if files:
logging.warn("warning")
run()
else:
logging.error("error")
raise IOError

if internet_connection() and got_hash():
run()
else:
run_with_files(files)


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


Re: extract Infobox contents

2009-04-07 Thread J. Clifford Dyer
On Mon, 2009-04-06 at 23:41 +0100, Rhodri James wrote:
> On Mon, 06 Apr 2009 23:12:14 +0100, Anish Chapagain  
>  wrote:
> 
> > Hi,
> > I was trying to extract wikipedia Infobox contents which is in format
> > like given below, from the opened URL page in Python.
> >
> > {{ Infobox Software
> > | name   = Bash
> > | logo   = [[Image:bash-org.png|165px]]
> > | screenshot = [[Image:Bash demo.png|250px]]
> > | caption= Screenshot of bash and [[Bourne shell|sh]]
> > sessions demonstrating some features
> > | developer  = [[Chet Ramey]]
> > | latest release version = 4.0
> > | latest release date= {{release date|mf=yes|2009|02|20}}
> > | programming language   = [[C (programming language)|C]]
> > | operating system   = [[Cross-platform]]
> > | platform   = [[GNU]]
> > | language   = English, multilingual ([[gettext]])
> > | status = Active
> > | genre  = [[Unix shell]]
> > | source model   = [[Free software]]
> > | license= [[GNU General Public License]]
> > | website= [http://tiswww.case.edu/php/chet/bash/
> > bashtop.html Home page]
> > }} //upto this line
> >
> > I need to extract all data between {{ Infobox ...to }}
> >
> > Thank's if anyone can help,
> > am trying with
> >
> > s1='{{ Infobox'
> > s2=len(s1)
> > pos1=data.find("{{ Infobox")
> > pos2=data.find("\n",pos2)
> >
> > pat1=data.find("}}")
> >
> > but am ending up getting one line at top only.
> 
> How are you getting your data?  Assuming that you can arrange to get
> it one line at a time, here's a quick and dirty way to extract the
> infoboxes on a page.
> 
> infoboxes = []
> infobox = []
> reading_infobox = False
> 
> for line in feed_me_lines_somehow():
>  if line.startswith("{{ Infobox"):
>  reading_infobox = True
>  if reading_infobox:
>  infobox.append(line)
>  if line.startswith("}}"):
>  reading_infobox = False
>  infoboxes.append(infobox)
>   infobox = []
> 
> You end up with 'infoboxes' containing a list of all the infoboxes
> on the page, each held as a list of the lines of their content.
> For safety's sake you really should be using regular expressions
> rather than 'startswith', but I leave that as an exercise for the
> reader :-)
> 

I agree that startswith isn't the right option, but for matching two
constant characters, I don't think re is necessary.  I'd just do:

if '}}' in line:
pass

Then, as the saying goes, you only have one problem.

Cheers,
Cliff


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


Re: Eval Problem

2009-04-06 Thread J. Clifford Dyer
On Mon, 2009-04-06 at 15:11 -0400, Victor Subervi wrote:
> Hi:
> I have this code:
> 
> x = 1
> while x <= bitties:
>   file = open(p + str(x) + ".txt")
>   for line in file:
> print line
>   print eval(bits[x - 1])
>   x += 1
> 
> which throws this error:
> 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: Traceback (most recent call
> last): 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: File
> "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line 299,
> in HandlerDispatch\n result = object(req) 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: File
> "/usr/lib64/python2.4/site-packages/mod_python/cgihandler.py", line
> 96, in handler\n imp.load_module(module_name, fd, path, desc) 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: File
> "/var/www/vhosts/articles.13gems.com/httpdocs/index_frame.py", line
> 89, in ?\n print eval(bits[1]) 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: File "", line 1 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: tableBottom(348,180) 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: ^ 
> [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> PythonHandler mod_python.cgihandler: SyntaxError: invalid syntax
> 

Hmm.  That's not the problem I get.  For me, your code raises:
"NameError: name 'bitties' is not defined"

It's easier for us to help you if you present a self-contained piece of
code that exhibits the problem you've encountered.  Creating that code
will often reveal the problem with the original code, and you will have
solved your own problem.

The other problem with your code is that you are using eval.  Eval leads
to difficult-to-debug errors, and is usually unnecessary, given the
dynamic nature of python.  If you need to access a class method using a
string, you can use getattr.  If you need to dynamically select a
function based on some condition known in another variable, you can use
a dictionary to hash into the function.  

It's hard to say what might be appropriate for your situation, because
your code fragment is so... fragmentary.

Cheers,
Cliff


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


Re: Characters aren't displayed correctly

2009-03-27 Thread J. Clifford Dyer
On Mon, 2009-03-02 at 06:16 -0800, Hussein B wrote:
> On Mar 2, 4:03 pm, "J. Clifford Dyer"  wrote:
> > On Mon, 2009-03-02 at 00:33 -0800, Hussein B wrote:
> > > On Mar 1, 11:27 pm, "J. Clifford Dyer"  wrote:
> > > > On Sun, 2009-03-01 at 09:51 -0500, Philip Semanchuk wrote:
> > > > > On Mar 1, 2009, at 8:31 AM, Hussein B wrote:
> >
> > > > > > Hey,
> > > > > > I'm retrieving records from MySQL database that contains non english
> > > > > > characters.
> > > > > > Then I create a String that contains HTML markup and column values
> > > > > > from the previous result set.
> > > > > > +
> > > > > > markup = u'''.'''
> > > > > > for row in rows:
> > > > > > markup = markup + '' + row['id']
> > > > > > markup = markup + '
> > > > > > +
> > > > > > Then I'm sending the email according to this tip:
> > > > > >http://code.activestate.com/recipes/473810/
> > > > > > Well, the email contains ? characters for each non english ones.
> > > > > > Any ideas?
> >
> > > > > There's so many places where this could go wrong and you haven't  
> > > > > narrowed down the problem.
> >
> > > > > Are the characters stored in the database correctly?
> >
> > > > > Are they stored consistently (i.e. all using the same encoding, not  
> > > > > some using utf-8 and others using iso-8859-1)?
> >
> > > > > What are you getting out of the database? Is it being converted to  
> > > > > Unicode correctly, or at all?
> >
> > > > > Are you sure that the program you're using to view the email  
> > > > > understands the encoding?
> >
> > > > > Isolate those questions one at a time. Add some debugging 
> > > > > breakpoints.  
> > > > > Ensure that you have what you think you have. You might not fix your  
> > > > > problem, but you will make it much smaller and more specific.
> >
> > > > > Good luck
> > > > > Philip
> >
> > > > Let me add to that checklist:
> >
> > > > Are you sure the email you are creating has the encoding declared
> > > > properly in the headers?
> >
> > > > Cheers,
> > > > Cliff
> >
> > > My HTML markup contains only table tags (you know, table, tr and td)
> >
> > Ah.  The issue is not with the HTML markup, but the email headers.  For
> > example, the email you sent me has a header that says:
> >
> > Content-type: text/plain; charset="iso-8859-1"
> >
> > Guessing from the recipe you linked to, you probably need something
> > like:
> >
> > msgRoot['Content-type'] = 'text/plain; charset="utf-16"'
> >
> > replacing utf-16 with whatever encoding you have encoded your email
> > with.
> >
> > Or it may be that the header has to be attached to the individual mime
> > parts.  I'm not as familiar with MIME.
> >
> > Cheers,
> > Cliff
> 
> Hey Cliff,
> I tried your tip and I still get the same thing (?)
> I added print statement to print each value of the result set into the
> console, which also prints  characters instead of the real
> characters values.
> Maybe a conversion is happened upon getting the data from the
> database?
> (the values are stored correctly in the database)

Sorry for responding so late.

Getting ? characters when you print to the console does not
*necessarily* mean python is doing anything wrong.  It may be outputting
the correct characters, but your console lacks the proper font for
displaying those characters.  Try doing for c in email_body: print
hex(ord(c)), and see if the results correspond to what you're trying to
get.

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

Cheers,
Cliff


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


Re: Characters aren't displayed correctly

2009-03-02 Thread J. Clifford Dyer
On Mon, 2009-03-02 at 00:33 -0800, Hussein B wrote:
> On Mar 1, 11:27 pm, "J. Clifford Dyer"  wrote:
> > On Sun, 2009-03-01 at 09:51 -0500, Philip Semanchuk wrote:
> > > On Mar 1, 2009, at 8:31 AM, Hussein B wrote:
> >
> > > > Hey,
> > > > I'm retrieving records from MySQL database that contains non english
> > > > characters.
> > > > Then I create a String that contains HTML markup and column values
> > > > from the previous result set.
> > > > +
> > > > markup = u'''.'''
> > > > for row in rows:
> > > > markup = markup + '' + row['id']
> > > > markup = markup + '
> > > > +
> > > > Then I'm sending the email according to this tip:
> > > >http://code.activestate.com/recipes/473810/
> > > > Well, the email contains ? characters for each non english ones.
> > > > Any ideas?
> >
> > > There's so many places where this could go wrong and you haven't  
> > > narrowed down the problem.
> >
> > > Are the characters stored in the database correctly?
> >
> > > Are they stored consistently (i.e. all using the same encoding, not  
> > > some using utf-8 and others using iso-8859-1)?
> >
> > > What are you getting out of the database? Is it being converted to  
> > > Unicode correctly, or at all?
> >
> > > Are you sure that the program you're using to view the email  
> > > understands the encoding?
> >
> > > Isolate those questions one at a time. Add some debugging breakpoints.  
> > > Ensure that you have what you think you have. You might not fix your  
> > > problem, but you will make it much smaller and more specific.
> >
> > > Good luck
> > > Philip
> >
> > Let me add to that checklist:
> >
> > Are you sure the email you are creating has the encoding declared
> > properly in the headers?
> >
> >
> >
> >
> > Cheers,
> > Cliff
> 
> My HTML markup contains only table tags (you know, table, tr and td)

Ah.  The issue is not with the HTML markup, but the email headers.  For
example, the email you sent me has a header that says: 

Content-type: text/plain; charset="iso-8859-1"

Guessing from the recipe you linked to, you probably need something
like:

msgRoot['Content-type'] = 'text/plain; charset="utf-16"'

replacing utf-16 with whatever encoding you have encoded your email
with.

Or it may be that the header has to be attached to the individual mime
parts.  I'm not as familiar with MIME.


Cheers,
Cliff



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


Re: Characters aren't displayed correctly

2009-03-01 Thread J. Clifford Dyer
On Sun, 2009-03-01 at 09:51 -0500, Philip Semanchuk wrote:
> On Mar 1, 2009, at 8:31 AM, Hussein B wrote:
> 
> > Hey,
> > I'm retrieving records from MySQL database that contains non english
> > characters.
> > Then I create a String that contains HTML markup and column values
> > from the previous result set.
> > +
> > markup = u'''.'''
> > for row in rows:
> > markup = markup + '' + row['id']
> > markup = markup + '
> > +
> > Then I'm sending the email according to this tip:
> > http://code.activestate.com/recipes/473810/
> > Well, the email contains ? characters for each non english ones.
> > Any ideas?
> 
> There's so many places where this could go wrong and you haven't  
> narrowed down the problem.
> 
> Are the characters stored in the database correctly?
> 
> Are they stored consistently (i.e. all using the same encoding, not  
> some using utf-8 and others using iso-8859-1)?
> 
> What are you getting out of the database? Is it being converted to  
> Unicode correctly, or at all?
> 
> Are you sure that the program you're using to view the email  
> understands the encoding?
> 
> Isolate those questions one at a time. Add some debugging breakpoints.  
> Ensure that you have what you think you have. You might not fix your  
> problem, but you will make it much smaller and more specific.
> 
> 
> Good luck
> Philip
> 
> 

Let me add to that checklist:

Are you sure the email you are creating has the encoding declared
properly in the headers?

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

Cheers,
Cliff


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


Re: XML Parsing

2009-02-25 Thread J. Clifford Dyer
Probably because you responded an hour after the question was posted,
and in the dead of night.  Newsgroups often move slower than that.  But
now we have posted a solution like that, so all's well in the world.  :)

Cheers,
Cliff


On Wed, 2009-02-25 at 08:20 +, hrishy wrote:
> Hi Lie
> 
> I am not a python guy but very interested in the langauge and i consider the 
> people on this list to be intelligent and was wundering why you people did 
> not suggest xpath for this kind of a problem just curious and willing to 
> learn.
> 
> I am searching for a answer but the question is 
> why not use xpath to extract xml text from a xml doc ?
> 
> regards
> Hrishy
> 
> 
> --- On Wed, 25/2/09, Lie Ryan  wrote:
> 
> > From: Lie Ryan 
> > Subject: Re: XML Parsing
> > To: python-list@python.org
> > Date: Wednesday, 25 February, 2009, 7:33 AM
> > Are you searching for answer or searching for another people
> > that have 
> > the same answer as you? :)
> > 
> > "Many roads lead to Rome" is a very famous
> > quotation...
> > 
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> 
> 
>   
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

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


Re: PHP like documentation?

2009-02-15 Thread J. Clifford Dyer
On Sun, 2009-02-15 at 09:54 -0800, Pavan Mishra wrote:
> I was wondering if I can use python documentation source
> (reStructuredText) and restructure it along the lines of PHP
> documentation. Allowing users to add comments, improving search etc.
> 
> I was thinking if it would be useful.
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

I think it would be useful to have somewhere, but I'm -1 on replacing
the official documentation with it.  I like having the official
documentation more... official.  I like that what you see there has been
fully vetted, edited, and tested.  If there's something wrong with it,
fix it.  If you've got a helpful tip, add it, but it shouldn't be as
easy as commenting to add to the official documentation.  You could
argue that the documentation itself is official, and the comments are
not, but I like not having to worry about what part of the page I'm
looking at.  I also like that google only indexes the official
documentation, and not the comments.  

As I said, it could be very helpful, and I'm not against people having a
place to share their advice, but I'd rather see it happen somewhere
besides python.org. 

Just my thought. 

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


Re: v = json.loads("{'test':'test'}")

2009-01-25 Thread J. Clifford Dyer
Please include all relevant information in the *body* of your message,
not just in the subject.  It's a pain having to piece a question back
together between the subject.

On Sun, 2009-01-25 at 13:12 -0800, gert wrote:
> raise ValueError(errmsg("Expecting property name", s, end))
> http://docs.python.org/library/json.html
> What am I doing wrong ?

JSON requires strings to be enclosed in double quotes.  It is not as
flexible as python when it comes to quotation.  If you change your
example to

v = json.loads('{"test":"test"}')

it will work.  (Note JSON also doesn't allow trailing commas, so
'{"test":"test",}' will also fail)

Cheers,
Cliff


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

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


Re: Skull Socks (was Re: Convention vs. fascism)

2009-01-16 Thread J. Clifford Dyer
On Fri, 2009-01-16 at 16:41 -0500, Steve Holden wrote:
> Steven D'Aprano wrote:
> > On Fri, 16 Jan 2009 11:01:18 -0500, J. Cliff Dyer wrote:
> > 
> >> On Fri, 2009-01-16 at 08:57 +, Steven D'Aprano wrote:
> >>> On Fri, 16 Jan 2009 10:03:28 +0200, Hendrik van Rooyen wrote:
> >>>
>  Oh come on you lot - you are carrying on as if Diez were wearing his
>  skull socks again - do me a favour and give him a break!
> >>> And... skull socks? Cool. Where can I get some?
> >>>
> >>>
> >>>
> >> http://www.letmegooglethatforyou.com/search?q=skull+socks
> > 
> > Not Found
> > The requested URL /search was not found on this server.
> > Additionally, a 404 Not Found error was encountered while trying to use 
> > an ErrorDocument to handle the request.
> > 
> > 
> > 
> > You know, it was just a throw-away comment to lighten the mood. I don't 
> > really need to see 18,000+ links to places that sell socks with skulls 
> > printed on them.
> > 
> > On the other hand, if they were socks made from actual skulls, that would 
> > be something...
> > 
> Of course Cliff actually meant
> 
>   http://letmegooglethatforyou.com/?q=skull+socks
> 
> Jokes are never as funny when they go wrong :)

Maybe I could run a "teach me letmegooglethatforyou.com" at pycon this
year.

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


Re: Creating new instances of subclasses.

2009-01-12 Thread J. Clifford Dyer
On Fri, 2009-01-09 at 10:46 -0800, Dennis Lee Bieber wrote:
> On Wed, 07 Jan 2009 11:38:29 -0500, "J. Cliff Dyer" 
> declaimed the following in comp.lang.python:
> 
> > I want to be able to create an object of a certain subclass, depending
> > on the argument given to the class constructor.
> > 
> > I have three fields, and one might need to be a StringField, one an
> > IntegerField, and the last a ListField.  But I'd like my class to
> > delegate to the proper subclass automatically, so I can just do:
> > 
> > >>> f1 = Field('abc')
> > >>> f2 = Field('123')
> > >>> f3 = Field('D,E,F')
> 
>   And how do you differentiate a string that contains a comma from a
> purported list?
> 
>   What is expected for:
> 
>   ambiguous = Field('He said "Blast, it won''t work"')

My strings don't look like that.  Nor is that relevant to my question.
I created a simple, uncluttered example to illustrate my issue with
instantiating the proper subclass for a given argument.  I did not try
to make sure my example handled all edge cases properly.

Cheers,
Cliff


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


Re: Exec inside a class method to call other class methods?

2008-12-25 Thread J. Clifford Dyer
On Thu, 25 Dec 2008 13:22:15 -0500
Matthew Dubins  wrote:

> Hello all,
> 
> I have made a python script to upload contact information from an
> excel worksheet to an online database.  One part of the program that
> really tripped me up was when I wanted to call specific class methods
> that I had made to deal with specific types of contact information
> (Parent's name, Child's name, Phone #, etc).  My first thought was to
> make it easy using the exec statement. 
> 
> The code (a method within a class) looked like this:
> --
> def parse(self, data, data_type)
> exec "self.__parse_%s(data)" % data_type
> --
> The data_type variable contains strings that exactly match the
> spellings of the 2nd word in the titles of the class methods that I
> wanted to call for each data_type inputted into the parse function.
> For some reason, *it didn't work*.  Alternately, I found the ugly
> code shown below to be functional.  As you can see, for each
> data_type, I call the corresponding class method that I've
> specified.  Please help me to transform my ugly functional code into
> concise functional code. :)
> 
> Thanks,
> Matthew
> --
> def parse(self, data, data_type):
> if data_type == 'nocall':
> self.__parse_nocall(data)
> elif data_type == 'DOB':
> self.__parse_DOB(data)
> elif data_type == 'gender':
> self.__parse_gender(data)
> elif data_type == 'Prematurity':
> self.__parse_Prematurity(data)
> elif data_type == 'email':
> self.__parse_email(data)
> elif data_type == 'languages':
> self.__parse_languages(data)
> elif data_type == 'phone':
> self.__parse_phone(data)
> elif data_type == 'cname':
> self.__parse_cname(data)
> elif data_type == 'pname':
> self.__parse_pname(data)
> elif data_type == 'address':
> self.__parse_address(data)
> elif data_type == 'duedate':
> self.__parse_dudedate(data)
> 

This is precisely what subclasses were designed for:

class DataField(object):
def __init__(self, data):
self.data = data
def parse(self):
pass

class AddressDataField(DataField):
def parse(self):
self.data = do_something(self.data)

class DueDateDataField(DataField):
def parse(self):
self.data = parse_date(self.data)

and so forth.  Then your if/else chain can be pulled out to the place where you 
instantiate each field: 

if data_type=='address':
field=AddressDataField(data)
elif data_type=='due_date':
field=DueDateDataField(data)
else:
field = DataField(data)

Then a simple field.parse() will do the right thing to field.data().  You might 
even include the parsing in __init__().  Define __init__() once on the base 
class (DataField), and it will pull the proper parse method from the subclass.

Happy Hacking, and Merry Christmas.
Cliff
--
http://mail.python.org/mailman/listinfo/python-list


Re: the official way of printing unicode strings

2008-12-14 Thread J. Clifford Dyer
On Sun, 2008-12-14 at 11:16 +0100, Piotr Sobolewski wrote:
> Marc 'BlackJack' Rintsch wrote:
> 
> > I'd make that first line:
> > sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
> > 
> > Why is it even more cumbersome to execute that line *once* instead
> > encoding at every ``print`` statement?
> 
> Oh, maybe it's not cumbersome, but a little bit strange - but sure, I can
> get used to it. 
> 
> My main problem is that when I use some language I want to use it the way it
> is supposed to be used. Usually doing like that saves many problems.
> Especially in Python, where there is one official way to do any elementary
> task. And I just want to know what is the normal, official way of printing
> unicode strings. I mean, the question is not "how can I print the unicode
> string" but "how the creators of the language suppose me to print the
> unicode string". I couldn't find an answer to this question in docs, so I
> hope somebody here knows it.
> 
> So, is it _the_ python way of printing unicode?
> 

The "right way" to print a unicode string is to encode it in the
encoding that is appropriate for your needs (which may or may not be
UTF-8), and then to print it.  What this means in terms of your three
examples is that the first and third are correct, and the second is
incorrect.  The second one breaks when writing to a file, so don't use
it.  Both the first and third behave in the way that I suggest.  The
first (print u'foo'.encode('utf-8')) is less cumbersome if you do it
once,  but the third method (rebinding sys.stdout using codecs.open) is
less cumbersome if you'll be doing a lot of printing on stdout.  

In the end, they are the same method, but one of them introduces another
layer of abstraction.  If you'll be using more than two print statements
that need to be bound to a non-ascii encoding, I'd recommend the third,
as it rapidly becomes less cumbersome, the more you print.  

That said, you should also consider whether you want to rebind
sys.stdout or not.  It makes your print statements less verbose, but it
also loses your reference to the basic stdout.  What if you want to
print using UTF-8 for a while, but then you need to switch to another
encoding later?  If you've used a new name, you can still refer back to
the original sys.stdout.

Right:

my_out = codecs.getwriter('utf-8')(sys.stdout)
print >> my_out u"Stuff"
my_out = codecs.getwriter('ebcdic')(sys.stdout)
print >> my_out u"Stuff"

Wrong

sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
print u"Stuff"
sys.stdout = codecs.getwriter('ebcdic')(sys.stdout)
# Now sys.stdout is geting encoded twice, and you'll probably 
# get garbage out. :(
print u"Stuff"

Though I guess this is why the OP is doing:

sys.stdout = codecs.getwriter('utf-8')(sys.__stdout__)

That avoids the problem by not rebinding the original file object.
sys.__stdout__ is still in its original state. 

Carry on, then.

Cheers,
Cliff

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


Re: var or inout parm?

2008-12-10 Thread J. Clifford Dyer
On Mon, 2008-12-08 at 00:57 -0800, Chris Rebert wrote:
> On Mon, Dec 8, 2008 at 12:26 AM, Bruno Desthuilliers
> <[EMAIL PROTECTED]> wrote:
> > Colin J. Williams a écrit :
> >>
> >> [EMAIL PROTECTED] wrote:
> >>>
> >>> How can I make a "var" parm, where the called function can modify
> >>> the value of the parameter in the caller?
> >>>
> >>> def f(x):
> >>>x = x + 1
> >>>
> >>> n = 1
> >>> f(n)
> >>> # n should now be 2
> >>>
> >>> Many TIA!!
> >>> Mark
> >>>
> >>>
> >>
> >> Why not run it and see?
> >>
> >> Your function returns None.
> >>
> >> The function in effect takes a copy of n.
> >
> > Nope, it takes the object bound to global name 'n'.
> 
> See Also: the earlier heated debate thread over what evaluation
> strategy Python uses (Survey says!: call-by-object).
> 

Oh dear God.  PLEASE don't see also that thread.  That way lies madness.
Just google for call-by-object, and ignore the hell out of that thread.



> Cheers,
> Chris
> 

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


Re: Improving interpreter startup speed

2008-10-29 Thread J. Clifford Dyer
Maybe Ruby is the right language for your need.

Just sayin'.



On Sun, 2008-10-26 at 13:19 +, Pedro Borges wrote:
> The scripts i need to run but be executed with no apparent delay  
> specially when the text transforms are simple.
> 
> 
> On Oct 26, 2008, at 11:13 AM, James Mills wrote:
> 
> > On Sun, Oct 26, 2008 at 11:23 AM, BJörn Lindqvist  
> > <[EMAIL PROTECTED]> wrote:
> >> How are you getting those numbers? 330 μs is still pretty fast,  
> >> isn't
> >> it? :) Most disks have a seek time of 10-20 ms so it seem implausible
> >> to me that Ruby would be able to cold start in 47 ms.
> >
> > $ time python -c "pass"
> >
> > real0m0.051s
> > user0m0.036s
> > sys 0m0.008s
> >
> > $ time python3.0 -c "pass"
> >
> > real0m0.063s
> > user0m0.048s
> > sys 0m0.004s
> >
> > And yes I agree. the CPython interpreter startup times is
> > a stupid thing to be worrying about, especially since that
> > is never the bottleneck.
> >
> > Python loads plenty fast enough!
> >
> > --JamesMills
> >
> > -- 
> > --
> > -- "Problems are solved by method"
> 
> --
> http://mail.python.org/mailman/listinfo/python-list

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


Re: indentation

2008-10-20 Thread J. Clifford Dyer
On Mon, 2008-10-20 at 13:29 +, Steven D'Aprano wrote:
> On Mon, 20 Oct 2008 11:01:19 +0200, Bruno Desthuilliers wrote:
> 
> > Steven D'Aprano a écrit :
> >> On Sun, 19 Oct 2008 19:03:29 +0200, Bruno Desthuilliers wrote:
> >> 
> >>> Steven D'Aprano a écrit :
> >>>
> >>> (snip)
> >>>
>  You can use tabs, or spaces. If you use spaces, you can choose 4
>  spaces, or 8, or any number,
> >>> By all means, make it 4 spaces - that's the standard.
> >> 
> >> It's *a* standard. I believe it is the standard for the Python standard
> >> library, but there are other standards.
> > 
> > I can't remember having seen any other "standard" so far.
> 
> 
> How about PEP 8? It's not even hidden deep in the bowels of the PEP -- 
> it's almost at the top.
> http://www.python.org/dev/peps/pep-0008/
> 
> "For really old code that you don't want to mess up, you can continue to 
> use 8-space tabs."
> 

Fair, but limited to old code, so doesn't apply to instructions for new
code.

> 
> Then there's string.expandtabs():
> 
> expandtabs(...)
> S.expandtabs([tabsize]) -> string
> 
> Return a copy of S where all tab characters are expanded using spaces.
> If tabsize is not given, a tab size of 8 characters is assumed.
> 

The default for a tab does not imply anything about how python code
should be indented.

> 
> Here's Jamie Zawinski:
> http://www.jwz.org/doc/tabs-vs-spaces.html
> 
> "On defaultly-configured Unix systems, and on ancient dumb terminals and 
> teletypes, the tradition has been for the TAB character to mean ``move to 
> the right until the current column is a multiple of 8.'' (As it happens, 
> this is how Netscape interprets TAB inside  as well.) This is also 
> the default in the two most popular Unix editors, Emacs and vi."
> 

Again, refers to the interpretation of a tab, rather than indentation
conventions.

> 
> This page is a little old (2002), but it states that the standards for 
> OpenBSD and Linux (presumably the kernels) are 8 space indents:
> 
> http://xarg.net/writing/tabs
> 

Not python.  I think when Bruno says it's *the* standard, we can assume
he means "for python."

> Here's a style guide that recommends 2, 3 or 4 space indents:
> 
> http://www.cs.bris.ac.uk/Teaching/Resources/COMS12100/style/
> 

Again, it's for java and C, not python.

> 
> And of course, whenever there's a difference of opinion, we can turn to 
> the ultimate source of all knowledge: Googlefight!  *wink*
> 
> http://www.googlefight.com/index.php?lang=en_GB&word1=tab+8
> +spaces&word2=tab+4+spaces

> Nearly 50 million hits for "tab 8 spaces" versus a piddly 762 thousand 
> hits for "tab 4 spaces".
> 
> 

And I still don't care how many spaces are in a tab. ;D

Cheers,
Cliff



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


Re: Case-insensitive string compare?

2008-09-05 Thread J. Clifford Dyer
On Thu, 2008-09-04 at 18:48 -0500, Robert Dailey wrote:
> Thanks everyone for your help. I'm not opposed to using [key.lower()
> for key in stage_map] at all, I was just curious to see if there were
> any cleaner alternatives. It looks like that is what I'll be using.
> I'm not familiar with how python works internally, but coming from C++
> it seems like "remaking" the map would be slow. However, speed is not
> my main concern in my particular situation, I'm just curious to learn
> more about python.

You should be opposed to that particular solution.  You have just taken
a dictionary lookup (very fast) and turned it into a list traversal
(slow).  Even if speed isn't your main concern, this is an unnecessary
de-optimization.  You are deliberately slowing down your program to
avoid a slightly more verbose lookup later.  Your data structure might
as well be a list of tuples to begin with, to avoid creating a new list.
You have effectively made your keys useless as keys.  

If your lookups need to be case insensitive, make the key lower case,
and store the cased version in the value, whether as a tuple or a dict
(depending on whether you want named access).

d = {
   'foo': {'key': 'Foo', 'value': 'val1'}
   'spam': {'key': 'sPAm', 'value': 'val2'}
}

search = 'FOO'.lower()
if search in d:
result = d[search]
key = result['key']
value = result['value']

The only reason I wouldn't use this solution is if you expect to have
keys that will be identical when made lowercase, but if you're doing
case-insensitive lookup, you obviously don't expect this to be an issue.

Cheers,
Cliff


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


Re: Hexadecimal: how to convert 'ED6F3C01' to "\xED\x6F\x3C\x01" in python coding?

2008-05-24 Thread J. Clifford Dyer
On Sat, 2008-05-24 at 15:59 -0700, zxo102 wrote:
> But this is not "\xED\x6F\x3C\x01".  I need it for
> struct.unpack('f',"\xED\x6F\x3C\x01") to calculate the decimal value
> (IEEE 754).
> Any other suggestions?
> 
> ouyang
> 

In fact it is exactly the same string.  The repr of a string always
substitutes ascii values for their numeric equivalent, but those bytes
are still the underlying representation of the string.

>>> [hex(ord(c)) for c in "\xefo<\x01"]
['0xef', '0x6f', '0x3c', '0x1']

Cheers,
Cliff

> On 5月25日, 上午6时46分, Sebastian 'lunar' Wiesner <[EMAIL PROTECTED]>
> wrote:
> > -BEGIN PGP SIGNED MESSAGE-
> > Hash: SHA1
> >
> > [ zxo102 <[EMAIL PROTECTED]> ]
> >
> > >how  to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
> > > "\xED\x6F\x3C\x01" in python coding?
> > > When I take 'ED6F3C01' as a string and insert '\x' into it, I just got
> > > the error information : invalid \x escape.
> >
> > [1]--> 'ED6F3C01'.decode('hex')
> > Out[1]: '\xedo<\x01'
> >
> > - --
> > Freedom is always the freedom of dissenters.
> >   (Rosa Luxemburg)
> > -BEGIN PGP SIGNATURE-
> > Version: GnuPG v2.0.9 (GNU/Linux)
> >
> > iEYEARECAAYFAkg4mtEACgkQn3IEGILecb7W6ACeNwr/vavkaXluvc0zeSa4cy1N
> > YFIAoJjMsrRcLhqAPRxKktUqt7miMTrs
> > =jxll
> > -END PGP SIGNATURE-
> 
> --
> http://mail.python.org/mailman/listinfo/python-list

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

Re: Hexadecimal: how to convert 'ED6F3C01' to "\xED\x6F\x3C\x01" in python coding?

2008-05-24 Thread J. Clifford Dyer
On Sat, 2008-05-24 at 15:36 -0700, zxo102 wrote:
> Hi,
>how  to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
> "\xED\x6F\x3C\x01" in python coding?
> When I take 'ED6F3C01' as a string and insert '\x' into it, I just got
> the error information : invalid \x escape.
>Thanks.
> 
> ouyang

A string is made up of a list of bytes, and when you use hexadecimal
character references, the \ and the x do not represent bytes on their
own, hence they cannot be manipulated as though they were individual
characters of the string.  The sequence \xNN is one byte, as a unit.  It
cannot be divided.  So if you try:

'\x%s" % 'ef'

you will get an error, because you are substituting a two-byte string
'ef', where in fact you only wanted to complete the one byte in
question.  \x can never be separated from the two hexadecimal digits
that follow it.  They must be treated as a unit.  To get the two-byte
string 'ef' converted to the one byte character '\xef', you have to find
a way to get the numerical value of ef, and create a character of that
value.  The first part is done as follows:

int('ef', 16)

which means "get the integer value of 'ef', where 'ef' is represented in
base 16.  In order to get the byte of hexadecimal numeric value 0xef,
you use the chr() function:

'\xef' == chr(int('ef', 16))

Or if you are working with unicode, use unichr() instead of chr().

If you always treat \xNN escape sequences as indivisible, you won't go
wrong.

Cheers,
Cliff

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


[EMAIL PROTECTED]: Re: Compress a string]

2008-05-18 Thread J. Clifford Dyer
On Sun, May 18, 2008 at 07:06:10PM +0100, Matt Porter wrote regarding Compress 
a string:
> 
> Hi guys,
> 
> I'm trying to compress a string.
> E.g:
>  "BBBC" -> "ABC"
> 
> The code I have so far feels like it could be made clearer and more  
> succinct, but a solution is currently escaping me.
> 
> 
> def compress_str(str):
> new_str = ""
> for i, c in enumerate(str):
> try:
> if c != str[i+1]:
> new_str += c
> except IndexError:
> new_str += c
> return new_str
> 
> 
> Cheers
> Matt

def compress_str(s): 
# str is a builtin keyword. Don't overload it.
out = []
for c in s:
if out and c == out[-1]:
out.append(c) # This is faster than new_str += c
return ''.join(out) 


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


Re: Python and Flaming Thunder

2008-05-14 Thread J. Clifford Dyer
On Tue, 2008-05-13 at 10:33 -0700, Dave Parker wrote:
> > You sound like a commercial.
> 
> Get Flaming Thunder for only $19.95!  It slices, it dices!
> 
> > And while programs and libraries written in assembly may be twice as fast
> > as programs and libraries written in C, ...
> 
> It's a myth that they're only twice as fast.  An experienced assembly
> language programmer can usually get out at least a factor of 5 by
> using tricks such as cache-coherence, carry flag tricks, stack
> manipulations, etc.
> 
> > ... they're real hell to maintain.
> 
> That's also a myth.  For example, if C is easy to maintain, why is
> Flaming Thunder the only single-asset 8-by-8 shotgun cross compiler in
> the world?  There should be lots of single-asset 8-by-8 shotgun cross
> compilers written in C, if C is easier to maintain.

Not only is it the world's only "single-asset 8-by-8 shotgun cross
compiler," but according to google, it's also the world's only "shotgun
cross compiler" period.  But I guess if you make up your own terminology
you're bound to be unique.  :)  Do you mind if I ask: what exactly is a
single-asset 8x8 shotgun cross compiler, and what makes that of any
value to me?

Cheers,
Cliff


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


Re: Best way to delimit a list?

2008-05-13 Thread J. Clifford Dyer
On Tue, 2008-05-13 at 03:28 -0700, [EMAIL PROTECTED] wrote:
> Hi - I have a list returned from popen/readlines, and am wondering how
> to go about iterating over each item which was returned (rather than
> currently having the whole lot returned).
> 
> so far:
> 
> >>> f=os.open("./get_hostnames").readlines
> 
> returns ['host1 host2 host3 ... hostN\n]'
> 
> i'd like to be in a position to iterate through these, grabbing each
> host.  I have played with transmuting to a str, and using split, and
> this works, but I get the subscript brackets from the list output as
> expected, as the list output is now a string literal, and this is not
> what I want - and I think it's a bit long-winded to do a search 'n
> replace on it - hence why I ask in the subject what's the best way.
> 
> >>> f=str(f)
> >>> f.split()
> ["['host1","host2", ... ,"hostN\n']"]
> 

Instead of casting to a string, each element of your list is already a
string, so use that instead:

f = open("get_hostnames")
hosts =[]

# gets each string one at a time.
for line in f:
# get rid of the pesky \n at the end
line = line.strip()
# separate the hostnames into a list
hosts += line.split(' ')

> Any help is highly appreciated
> 
> ta
> 
> dan.
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

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

Re: Backslash frowned upon?

2008-05-13 Thread J. Clifford Dyer
On Tue, 2008-05-13 at 03:25 -0700, [EMAIL PROTECTED] wrote:
> Why is the  \  backslash character frowned upon? Can I still use it in
> Python 3.0 to achieve the same thing it was designed to do?
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

Many people think it looks ugly, but it still works, and there are times
when nothing else will do, so it should continue to work for the
foreseeable future.

Cheers,
Cliff

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


Re: Am I missing something with Python not having interfaces?

2008-05-08 Thread J. Clifford Dyer
On Thu, 2008-05-08 at 09:12 +0200, Daniel Marcel Eichler wrote:
> Am Mittwoch 07 Mai 2008 21:48:56 schrieben Sie:
> 
> > > That's the point. Interfaces garantee that a duck is a duck, an not
> > > only a chicken that quack.
> > Which, in spite of your rhetorical flourish, is the exact opposite of
> > duck typing.  Duck typing means that if you're looking for quacking
> > behavior, and you find a quacking chicken, it's close enough.
> 
> I didn't said that interfaces are a kind of duck-typing. In fact it was 
> the exact opposite.
> 
> > Sometimes you need that kind of rigor, and you can get it as easily
> > as
> 
> And sometimes you need more. So what?
> 

More rigor than Zope's interfaces offer?  That's new information to me.
Perhaps you should stop being argumentative for a moment, and explain
exactly what it is you're looking for and why Zope interfaces don't fit
the bill.



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


Re: Custom Classes?

2008-05-02 Thread J. Clifford Dyer
On Thu, 2008-05-01 at 17:31 -0500, Victor Subervi wrote:
> On Wed, Apr 30, 2008 at 12:35 PM, J. Cliff Dyer <[EMAIL PROTECTED]> wrote:
> Post working code, and I'll answer your actual question.
> 
> Good grief!  The code is *not* double spaced! Take a look. Click to
> the end of the first line and hit the right arrow key, and see for
> yourself.  As for not initializing w, well, I did in my code and just
> forgot to cut and paste that. Same with the except. Sorry on those.
> And yes, I pull out try clauses when I need to look at stacks. Here:

Taking a closer look--it's still double spaced, but I'm sure that's just
something funky in how it's getting rendered along the way.  

That said, your code still doesn't work.  After removing your try
statements, and the corresponding except-pass blocks, I get the
following problems:

Traceback (most recent call last):
  File "test.py", line 5, in 
os.remove(getpic)
NameError: name 'os' is not defined

and when I fix that:

Traceback (most recent call last):
  File "test.py", line 34, in 
print ''
% pic

Please test your code snippet before you post it.  Testing the program
you extracted it from is not sufficient.

On line 34 (or thereabouts--I had to add a line for import os, and
stripped out all blank lines, since my email client is still rendering
it double spaced), I think you wanted "x" instead of str(x).  At least,
that's what your generated script wants.  str(x) takes the value of the
variable x (not yet defined), and converts it to a str.  It does not
give you a str with a value of "x".  

As for the generated script, why are you creating that on the fly every
time?  Why not just create it as an independent python file?  When you
want an image displayed in another HTML file, static or generated, just
point your src to that python file with the appropriate arguments.



Also, remove the line that says "print 'Content-Type: text/html'."  You
only need one content type, and it should be image/jpeg, or whatever
type your images are.  If you are going to have a few pngs in there as
well, for example, you'll need some mechanism for determining what type
of image the file actually is, and outputting the appropriate header
from:

Content-Type: image/jpeg
Content-Type: image/png
Content-Type: image/gif

This could be by checking the file extension, probing the file itself,
or by including the mime-type in your pics dictionary like so:

pics = {1: ('pic1', 'image/jpeg'), 2: ('pict_num_two', 'image/png')} 


Then, of course, you'll have to revise how you retrieve the information.

That should be enough to get you moving in the right direction.

Cheers,
Cliff
> 
> 
> w = 0
> 
> 
> try:
> 
>   w += 1
> 
>   getpic = "getpic" + str(w) + ".py"
> 
>   try:
> 
> os.remove(getpic)
> 
>   except:
> 
> pass
> 
>   code = """
> 
> #!/usr/local/bin/python
> 
> import cgitb; cgitb.enable()
> 
> import MySQLdb
> 
> import cgi
> 
> import sys,os
> 
> sys.path.append(os.getcwd())
> 
> from login import login
> 
> user, passwd, db, host = login()
> 
> form = cgi.FieldStorage()
> 
> picid = int(form["id"].value)
> 
> x = int(form["x"].value)
> 
> pics =
> {1:'pic1',2:'pic1_thumb',3:'pic2',4:'pic2_thumb',5:'pic3',6:'pic3_thumb',7:'pic4',8:'pic4_thumb',\
> 
> 9:'pic5',10:'pic5_thumb',11:'pic6',12:'pic6_thumb'}
> 
> pic = pics[x]
> 
> print 'Content-Type: text/html'
> 
> db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
> 
> cursor= db.cursor()
> 
> sql = "select " + pic + " from products where id='" + str(picid) +
> "';"
> 
> cursor.execute(sql)
> 
> content = cursor.fetchall()[0][0].tostring()
> 
> cursor.close()
> 
> print 'Content-Type: image/jpeg'
> 
> print
> 
> print content
> 
> """
> 
>   script = open(getpic, "w")
> 
>   script.write(code)
> 
>   print ''
> % pic
> 
>   print '\n' % (getpic, d,
> y)
> 
> except:
> 
>   pass
> 
> 
> TIA,
> Victor
> 
-- 
Oook!
J. Cliff Dyer
Carolina Digital Library and Archives
UNC Chapel Hill

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

Re: Unicode chr(150) en dash

2008-04-18 Thread J. Clifford Dyer

On Fri, 2008-04-18 at 07:27 -0400, J. Clifford Dyer wrote:
> On Fri, 2008-04-18 at 10:28 +0100, [EMAIL PROTECTED] wrote:
> > On Thu, 17 Apr 2008 20:57:21 -0700 (PDT)
> > hdante <[EMAIL PROTECTED]> wrote:
> > 
> > >  Don't use old 8-bit encodings. Use UTF-8.
> > 
> > Yes, I'll try. But is a problem when I only want to read, not that I'm 
> > trying to write or create the content.
> > To blame I suppose is Microsoft's commercial success. They won't adhere to 
> > standars if that doesn't make sense for their business.
> > 
> > I'll change the approach trying to filter the contents with htmllib and 
> > mapping on my own those troubling characters.
> > Anyway this has been a very instructive dive into unicode for me, I've got 
> > things cleared up now.
> > 
> > Thanks to everyone for the great help.
> > 
> 
> There are a number of code points (150 being one of them) that are used
> in cp1252, which are reserved for control characters in ISO-8859-1.
> Those characters will pretty much never be used in ISO-8859-1 documents.
> If you're expecting documents of both types coming in, test for the
> presence of those characters, and assume cp1252 for those documents.  
> 
> Something like:
> 
> for c in control_chars:
> if c in encoded_text:
>   unicode_text = encoded_text.decode('cp1252')
> break
> else:
> unicode_text = encoded_text.decode('latin-1')
> 
> Note that the else matches the for, not the if.
> 
> You can figure out the characters to match on by looking at the
> wikipedia pages for the encodings.

One warning: This works if you know all your documents are in one of
those two encodings, but you could break other encodings, like UTF-8
this way.  Fortunately UTF-8 is a pretty fragile encoding, so it's easy
to break.  You can usually test if a document is decent UTF-8 just by
wrapping it in a try except block:

try:
unicode_text = encoded.text.decode('utf-8')
except UnicodeEncodeError: # I think that's the proper exception
# do the stuff above

None of these are perfect methods, but then again, if text encoding
detection were a perfect science, python could just handle it on its
own.

If in doubt, prompt the user for confirmation.

Maybe others can share better "best practices."

Cheers,
Cliff

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


Re: Unicode chr(150) en dash

2008-04-18 Thread J. Clifford Dyer

On Fri, 2008-04-18 at 10:28 +0100, [EMAIL PROTECTED] wrote:
> On Thu, 17 Apr 2008 20:57:21 -0700 (PDT)
> hdante <[EMAIL PROTECTED]> wrote:
> 
> >  Don't use old 8-bit encodings. Use UTF-8.
> 
> Yes, I'll try. But is a problem when I only want to read, not that I'm trying 
> to write or create the content.
> To blame I suppose is Microsoft's commercial success. They won't adhere to 
> standars if that doesn't make sense for their business.
> 
> I'll change the approach trying to filter the contents with htmllib and 
> mapping on my own those troubling characters.
> Anyway this has been a very instructive dive into unicode for me, I've got 
> things cleared up now.
> 
> Thanks to everyone for the great help.
> 

There are a number of code points (150 being one of them) that are used
in cp1252, which are reserved for control characters in ISO-8859-1.
Those characters will pretty much never be used in ISO-8859-1 documents.
If you're expecting documents of both types coming in, test for the
presence of those characters, and assume cp1252 for those documents.  

Something like:

for c in control_chars:
if c in encoded_text:
unicode_text = encoded_text.decode('cp1252')
break
else:
unicode_text = encoded_text.decode('latin-1')

Note that the else matches the for, not the if.

You can figure out the characters to match on by looking at the
wikipedia pages for the encodings.

Cheers,
Cliff


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


Re: Pycon disappointment

2008-03-17 Thread J. Clifford Dyer
I just want to step in and offer my 2¢.  This is my first PyCon, and I
agree that a lot of the Lightning talks seemed pretty useless.  Overall
though, I had a great experience at this conference.  I learned a lot; I
met a lot of cool people; and I got really excited about new ideas to
bring back home.  

Django code lab was fantastic.  

Teach me Twisted was a fantastic, innovative, and effective way to teach
a new technology.  There was a little bit of difficulty hearing over the
cross-talk, but I just moved up front and had no further troubles (and
better access to the Balvenie single-malt!

Most of the sessions I attended were moderately to highly useful.  FWIW,
none of my presenters had laptop troubles, except at teach me twisted,
but we weren't on as much of a time crunch, and we took care of that one
pretty easily and kept going.  

The only useless one I attended was actually the most highly technical,
not because it didn't have good information, but because functions were
used without reference to what module they had been imported from and
slides containing 15-20 line functions were left up for about thirty
seconds, and then were gone.  I couldn't even finish reading them.  

Note to speakers:  do not say

   x, y = tee(foo)

say

   from itertools import tee
   x, y = tee(foo)

or better (for pedagogical purposes)

   import itertools
   x, y = itertools.tee(foo)

I don't disagree with the criticisms leveled throughout this thread, but
I do want to say that I think it has been a great conference, and for
me, the problems did not ruin the experience.  Heed these criticisms and
it will be even better next year.  Ignore them, and it will probably
degrade over time.

Thanks,
Cliff


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

Re: How about adding rational fraction to Python?

2008-02-26 Thread J. Clifford Dyer
On Tue, 2008-02-26 at 14:08 -0800, Jeff Schwab wrote:
> J. Cliff Dyer wrote:
> > On Tue, 2008-02-26 at 13:51 -0500, D'Arcy J.M. Cain wrote:
> >> On Tue, 26 Feb 2008 13:39:38 -0500
> >> "J. Cliff Dyer" <[EMAIL PROTECTED]> wrote:
> >> a = 2 * 2
> >> b = 20 * 20
> >> type(a)
> >>> 
> >> type(b)
> >>> 
> >> A long int is still integral which is the crux of the issue.
> >>
> > 
> > So do you believe that you should not be able to do natural division
> > without explicitly casting ints as floats, or is your concern just that
> > you want to still be able to to integer division simply?
> 
> I think the issue is whether this qualifies as "natural" division in the 
> first place.  (For the record, I'm only semi-old-school.)

OK.  My use of the word "natural" was ill advised.

Sorry.

But I don't think that *was* the issue in the first place.  The issue is
whether python's division should only yield integers when given integer
inputs.  "Natural" is a polemical term no matter who is using it in this
argument, so lets drop it altogether.  If the issue is that it should
remain an integer because that mimics how the computer works, than I
think it is worth pointing out that allowing a conversion to a long also
goes against how the computer works; the computer would have a register
overflow.  If the issue is that in python the division operator has
always performed integer division, and should not change, then I think
we're talking about a philosophical opposition to Python 3 that goes far
deeper than just how integer division behaves.

At any rate, the whole argument is irrelevant--python already supports
floating point results for integer division, and that isn't going away,
but neither is integer division that always results in integer results,
so if you don't like getting floating point results, you never have to
use the / operator.  

Practicality beats purity.  

Cheers,
Cliff

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


Re: Question from a python newbie

2007-12-13 Thread J. Clifford Dyer
On Thu, Dec 13, 2007 at 04:57:04PM +0100, Remco Gerlich wrote regarding Re: 
Question from a python newbie:
> 
>On Dec 13, 2007 4:39 PM, Russell <[EMAIL PROTECTED]> wrote:
> 
>  I've been learning Python slowly for a few months, coming from a
>  C/C+
>  +, C#, Java, PHP background.  I ran across a code fragment I'm
>  having
>  trouble wrapping my brain around.  I've searched the Language
>  Reference and was not able to find any info regarding the structure
>  of
>  this code fragment:
>  int(text) if text.isdigit() else text
> 
>Hi,
>It's a pretty new construct; basically it's Python's version of the ? :
>"ternary operator" in other languages.
>The line above is equivalent to
>text.isdigit() ? int(text) : text
>in, for instance, C.
>Remco Gerlich
> 

Or, if you don't know C.  It is equivalent to

if text.isdigit():
int(text)
else:
text
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread J. Clifford Dyer
On Wed, Dec 12, 2007 at 11:02:04AM -0600, [EMAIL PROTECTED] wrote regarding Re: 
Is anyone happy with csv module?:
> 
> J. Clifford Dyer <[EMAIL PROTECTED]> wrote:
> > But the software you are dealing with probably doesn't actually 
> > need spreadsheets.  It just needs digital ledgers.
> 
> I saw someone else in this thread note that they considered CSV to
> be a serialization method and not a file format, which I thought was a
> brilliant way of looking at things.
> 
> OTOH, from a practical perspective, most of the people I end up interacting
> with say "spreadsheet" to mean exactly what you're describing as a
> "ledger paper" ... sure they might want to do operations on those cells
> in the data, but it isn't key what actual format the data is presented
> to them in (XLS, CSV, tab delimited, etc).  Oddly enough though, they
> almost always say "excel spreadsheet" even though that's not what they
> need (rather, its just a side effect of them not realizing there actually
> *is* other software that handles these things)

I know, I know.  The last place I worked, we had timesheets that were excel 
spreadsheets, but you actually had to total up your own hours.  I added to my 
job description the role of teaching people how to make the spreadsheet do the 
work for you.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread J. Clifford Dyer
On Wed, Dec 12, 2007 at 10:08:38AM -0600, [EMAIL PROTECTED] wrote regarding Re: 
Is anyone happy with csv module?:
> 
> FWIW, CSV is a much more generic format for spreadsheets than XLS.
> For example, I deal almost exclusively in CSV files for simialr situations
> as the OP because I also work with software that can't (or in some
> cases "can't easily") deal with XLS files.  CSV files can be read in
> by basically anything.

Compatibility-wise, yes, CSV is much more generic.  But spreadsheet != ledger 
paper.  Spreadsheets incorporate many functions (as simple as summation!) that 
CSV cannot handle.  Thus functionality-wise CSV is infact a very specific 
subset of spreadsheets, and is not generic in the slightest.

But the software you are dealing with probably doesn't actually need 
spreadsheets.  It just needs digital ledgers.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread J. Clifford Dyer
On Wed, Dec 12, 2007 at 06:36:49AM -0800, sturlamolden wrote regarding Re: Is a 
"real" C-Python possible?:
> 
> On 12 Des, 12:56, George Sakkis <[EMAIL PROTECTED]> wrote:
> 
> > Ah, the 'make' statement.. I liked (and still do) that PEP, I think it
> > would have an impact comparable to the decorator syntax sugar, if not
> > more.
> 
> I think it is one step closer to Lisp. I believe that it would be
> worth considering adding defmacro statement. Any syntax, including if,
> else, for, while, class, lambda, try, except, etc.  would be
> implemented with defmacros. We would only need a minimalistic syntax,
> that would bootstrap a full Python syntax on startup. And as for
> speed, we all know how Lisp compares to Python.
> 

Programmable syntax is a very powerful concept.  However, python is designed 
not only to be powerful, but simple, and this change would drastically reduce 
the simplicity of Python.  It would cease to be a good beginner's language.  If 
you want a language with different syntax than python, python has wonderful 
parsing libraries.  Use those instead.

My 2?.

Cheers,
Cliff

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

Re: psycopg

2007-12-12 Thread J. Clifford Dyer
On Wed, Dec 12, 2007 at 09:08:44AM -0500, Calvin Spealman wrote regarding Re: 
psycopg:
> 
>Don't do that, for a number of reasons. String concatenation is really
>never a good idea and formatting your own query strings is exactly what
>leads to things like sql injection. Let the db library handle it for
>you:
> 

If you don't know what a SQL injection is, and you don't feel like googling for 
it, this should give you a good idea of why this matters:

http://xkcd.com/327/

>cur.execute('insert into seq(id,sequence) values(3, %s)', (content,))
> 
>Notice that, although we're using the %s placeholder, we are _not_
>using the % operator to format the string. This is because the db
>module will do any proper preparation of the value for you before
>inserting into the string.
> 
>On Dec 12, 2007, at 8:31 AM, sujitha mary wrote:
> 
>  hi all,
>  while executing this cur.execute('insert into seq(id,sequence)
>  values(3,'+content+')')
>  i'm getting an error  psycopg2.ProgrammingError : syntax error at or
>  near "prophage"
>  LINE 1: insert into seq(id,sequence) values(3,Tum2 prophage
>  complete...
> 
>--
> 
>[1]http://mail.python.org/mailman/listinfo/python-list
> 
> References
> 
>1. http://mail.python.org/mailman/listinfo/python-list

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


eval raising SyntaxError (was No Subject)

2007-12-11 Thread J. Clifford Dyer

On Tue, 2007-12-11 at 16:55 -0800, katie smith wrote:
> I tried your suggestions and all that came up was the error
> Traceback (most recent call last):
>   File "C:\Python25\empire\Empire Strategy.pyw", line 1788, in
> 
> NewMap1= eval (NewMap1, {}, {})
>   File "", line 1
> Tropical Islands
>^
> SyntaxError: invalid syntax
>  
> And what is this about trusting your source? The string is taken
> exactly from another part in the program so I know for sure it is
> correct if that is what that means.
> 

Katie,

First, please provide a useful subject heading when posting to the list.
It makes everyone's life easier when searching the archives.

Second, the example you provided in your last post was converting a
string of the form "[1,2,3,4,5]" to a list of integers.  What is
happening here, is you are trying to convert a string of the form
"Tropical Islands" to... what?  

I assume you are not getting the string you thought you would get.  Eval
is acting as advertised.  It is taking the line "Tropical Islands" and
trying to parse it as python code.  If that's not what you wanted to do,
the question you should be asking is "why did I get the string "Tropical
Islands" instead of "[1,2,3,4,5]".  You should not be asking "what is
eval doing wrong."

Before you were advised not to use eval unless you were sure of the
source of your inputs.  

If the source of your input is "me," or "trusted operators" then you are
not sure of your inputs, because you might type something in thinking
that the focus was on a different window, or you might mistype
something.  

If the source of your inputs is a line you are extracting from another
python file, you are not sure of your inputs, because there could be an
error in your python file, or someone could add a line, so while
pulling, e.g., line 15 used to give you "[1,2,3,4,5]", that's now line
16, and line 15 gives you "Tropical Islands" instead, because somebody
thought there should be an extra blank line before the start of the code
or something.  

If the source of your inputs is the output of another function, why is
it passing you a repr of a list instead of the list itself.  

Almost NEVER use eval.  If you find yourself using eval, make your next
task figuring out how not to use eval.

Cheers,
Cliff


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


Re: Dumb newbie back in shell

2007-12-11 Thread J. Clifford Dyer
On Tue, Dec 11, 2007 at 08:36:54AM -0600, Chris Mellon wrote regarding Re: Dumb 
newbie back in shell:
> Delivered-To: [EMAIL PROTECTED]
> Date: Tue, 11 Dec 2007 08:36:54 -0600
> From: "Chris Mellon" <[EMAIL PROTECTED]>
> To: "python-list@python.org" 
> Subject: Re: Dumb newbie back in shell
> In-Reply-To: <[EMAIL PROTECTED]>
> Precedence: list
> List-Id: General discussion list for the Python programming language
>   
> List-Unsubscribe: <http://mail.python.org/mailman/listinfo/python-list>,
>   <mailto:[EMAIL PROTECTED]>
> List-Archive: <http://mail.python.org/pipermail/python-list>
> List-Post: <mailto:python-list@python.org>
> List-Help: <mailto:[EMAIL PROTECTED]>
> List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
>   <mailto:[EMAIL PROTECTED]>
> Errors-To: [EMAIL PROTECTED]
> 
> On Dec 11, 2007 8:23 AM, J. Clifford Dyer <[EMAIL PROTECTED]> wrote:
> > The code you just posted doesn't compile successfully.
> >
> 
> It *compiles* fine, but it'll raise an error when run.
> 
> > However, in your code, you probably have char_ptr defined at the module 
> > level, and you're confused because you didn't declare it as global.  Am I 
> > right?  My crystal ball has a smudge on it, but I think I can still see 
> > okay.
> >
> 
> I assume that's what he think he's seeing also.
> 
> > You can still reference module level variables that aren't declared as 
> > global, but you can't assign to them.  Or rather, when you try to, you 
> > create a new local variable that shadows the global one.
> >
> 
> No, the determination of what names are local and which are global
> happens at compile time. The code as posted will not run correctly. It
> could run if it weren't in a function and were executed in global
> scope.
> 
> What's probably happening is that line_ptr < last_line is not true and
> the body of the function isn't executed at all. The unbound local
> exception is a runtime error that occurs when the local is accessed,
> not when the function is compiled.

Drat!  You're right.  I tried it in the interactive interpeter, and the 
function compiled just fine.  I tried calling the function and got an 
"UnboundLocalError."  I think I need to go sit in the corner and review the 
documentation.

Sorry all.  Disregard my previous post.

Chris, many thanks for your corrections.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dumb newbie back in shell

2007-12-11 Thread J. Clifford Dyer
The code you just posted doesn't compile successfully.

However, in your code, you probably have char_ptr defined at the module level, 
and you're confused because you didn't declare it as global.  Am I right?  My 
crystal ball has a smudge on it, but I think I can still see okay.

You can still reference module level variables that aren't declared as global, 
but you can't assign to them.  Or rather, when you try to, you create a new 
local variable that shadows the global one.

So the first time through, your char_ptr in the while expression is a module 
level variable, but the second time through, it references a local variable, 
because it has now been defined.

Cheers,
Cliff

On Tue, Dec 11, 2007 at 05:18:00AM -0800, [EMAIL PROTECTED] wrote regarding Re: 
Dumb newbie back in shell:
> 
> I'm less confused. If someone can explain the wisdom of this design,
> I'd be grateful.
> 
> If someone can explain why the following compiles successfully, I'd be
> even more grateful:
> 
> def get_toks( text ):
> global line_ptr, last_line
> while line_ptr < last_line:
> while char_ptr < len(text[line_ptr]):
> if matches_EOI():
> tokens.append( Token(EOI) )
> elif matches_EOL():
> tokens.append( Token(EOL) )
> line_ptr += 1
> char_ptr = 0
> 
> Shouldn't "char_ptr" be flagged as an error, appearing in line 4
> before being a lhs in the last line?
> 
> Martin
> 
> [EMAIL PROTECTED] wrote:
> > Peter,
> >
> > question is, why did the first one work? In my real code I've got
> > module-level vars and an error msg trying to use them in a function.
> > In my test example I've got them accessed from within a function w/o
> > error message.
> >
> > I am confused.
> >
> > Martin
> >
> > Peter Otten wrote:
> > > MartinRinehart wrote:
> > >
> > > > However, here's the little tester I wrote:
> > > >
> > > > # t.py - testing
> > > >
> > > > global g
> > > > g = 'global var, here'
> > > >
> > > > def f():
> > > > print g
> > > >
> > > > f()
> > > >
> > > > It prints 'global var, here,' not an error message. Wassup?
> > >
> > > Try it again with a modified f():
> > >
> > > def f():
> > > print g
> > > g = 42
> > >
> > > In Python variables that are assigned to in a function are
> > > function-local by default.
> > >
> > > Peter
> -- 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File to dict

2007-12-07 Thread J. Clifford Dyer

On Fri, 2007-12-07 at 03:31 -0800, [EMAIL PROTECTED] wrote: 
> Hello everyone,
> 
> I have written this small utility function for transforming legacy
> file to Python dict:
> 
> 
> def lookupdmo(domain):
> lines = open('/etc/virtual/domainowners','r').readlines()
> lines = [ [y.lstrip().rstrip() for y in x.split(':')] for x in
> lines]
> lines = [ x for x in lines if len(x) == 2 ]
> d = dict()
> for line in lines:
> d[line[0]]=line[1]
> return d[domain]
> 
> The /etc/virtual/domainowners file contains double-colon separated
> entries:
> domain1.tld: owner1
> domain2.tld: own2
> domain3.another: somebody
> ...
> 
> Now, the above lookupdmo function works. However, it's rather tedious
> to transform files into dicts this way and I have quite a lot of such
> files to transform (like custom 'passwd' files for virtual email
> accounts etc).

Don't do more lookup than you have to to get the answer you need.

(untested code)

class DictFromFile(object):
def __init__(self, filename):
self.f = open(filename)
self.d = dict()

def __getitem__(self, key):
while key not in self.d:
try:
k, v = self._retrieve_next()
self.d[k] = v
if k == key:
break
except StopIteration:
break
return self.d[key]

def _retrieve_next(self):
line = self.f.next()
k, v = line.split(':',1)
return k.strip(), v.strip()


This will act like a dictionary, but only look as far into the file as
it needs to.  If you've got a 10,000 line file, and all your results
come out of the first 10 lines, this will save you some processing.
There's more operator overloading you could do, of course.

Cheers,
Cliff

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


Re: Why did no one tell me about import antigravity?

2007-12-05 Thread J. Clifford Dyer

On Wed, 2007-12-05 at 10:08 +, Adrian Cherry wrote:
> Ant <[EMAIL PROTECTED]> wrote in news:52f0eca3-e807-4890-b21d-
> [EMAIL PROTECTED]:
> 
> > Python on xkcd:
> > 
> > http://xkcd.com/353/
> > 
> 
> Another good comic from xkcd, I'm surprised by the muted response 
> on here. Don't forget to check out the alt. text on the comic
> 
> Alt text: "I wrote 20 short programs in Python yesterday. It was 
> wonderful. Perl, I'm leaving you."
> 
> 
> Regards
> 
> Adrian

I did check it out.  Loved it.  I'm about to forward it to my coworkers.
I had previously wondered at his Perl zealousy.  Now we see--he just
didn't know any better.

:)

Cheers,
Cliff

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


Re: "Python" is not a good name, should rename to "Athon"

2007-12-01 Thread J. Clifford Dyer

On Sat, 2007-12-01 at 12:10 -0800, Russ P. wrote:
> On Dec 1, 2:10 am, Bjoern Schliessmann  [EMAIL PROTECTED]> wrote:
> > Russ P. wrote:
> > > I agree that Python is not a good name for a programming language,
> >
> > Why not?
> 
> Think about proposing its use to someone who has never heard of it
> (which I did not too long ago). As the OP pointed out, a Python is a
> snake. Why should a programming language be named after a snake?

That's not a persuasive argument.

First of all, Python is named for a comedy troupe from England.  For
comparison, Perl is named for a knitting technique, Lisp is named for a
speech impediment, Ruby is named for a rock, Smalltalk is named for a
not-so-useful form of communication, and Java is named after a beverage
or an island.  

Which of those is a good name for a programming language by your
criterion?  I like the name Python.  It has a nice ring to it, and has
connotations that are simultaneously badass and humorous.  

For that matter, why name sneakers after a goddess of justice?  Why name
a car after a planet/resort town/rodent/mountain?

Because brand names are supposed to be memorable, not meaningful.

Cheers,
Cliff



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


Re: C/C++ on UNIX Developer required in Woodmead, Johannesburg

2007-12-01 Thread J. Clifford Dyer
Please don't.  This job is not even vaguely python related.


On Sat, 2007-12-01 at 18:42 +0100, James Matthews wrote:
> Please post in the Python Wiki under the jobs
> 
> On Dec 1, 2007 9:38 AM, arnold <[EMAIL PROTECTED]> wrote:
> We seek the following sort of experience / skill for developer
> resources (if u do not qualify - we offer a handsome referral
> fee if
> you refer someone that is succesfully placed:-) .
> 
> C Programming  5 years
> C++ Programming  2 years
> SQL Programming  2 years
> Software Design  2 years
> Software Engineering Process  3 years
> GUI Development 1 year
> Unix/Linux environment 2 years
> Scientific/Mathematical Programming  2 years 
> 
> 
> These skills all need to be current.
> 
> 
> The candidate should demonstrate an ability to work
> resourcefully and
> independently, whilst delivering in the framework of a team.
>  The
> candidate must also demonstrate a willingness and desire to
> program 
> hands-on in C and C++ code.  He/she must have personally
> developed
> significant amounts of program code, demonstrating and
> requiring
> modular design and the use of a source code repository for
> version
> control.
> 
> 
> The candidate must communicate well, and be comfortable
> discussing
> design issues with both the team and the customer.  His / her
> present
> role should require this of him/her.
> 
> 
> The candidate must be able to describe the software
> development 
> lifecycle and identify his/her personal points of interaction
> in that
> cycle. These points must be Functional Requirements
> Specification,
> Software Design, Programming and Unit Testing.
> 
> 
> The following skills would be an advantage: 
>   Object oriented design
>   UML and the use of design tools like Rational Rose
>   Development tools (GDB, DBX, WxWidets/Qt/GTK,
> Makefiles
> etc.)
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> -- 
> http://search.goldwatches.com/?Search=Movado+Watches 
> http://www.jewelerslounge.com
> http://www.goldwatches.com 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Re: Oh no, my code is being published ... help!

2007-11-30 Thread J. Clifford Dyer
On Fri, Nov 30, 2007 at 12:25:25PM -0200, Eduardo O. Padoan wrote regarding Re: 
Oh no, my code is being published ... help!:
> 
> On Nov 30, 2007 11:36 AM, Hrvoje Niksic <[EMAIL PROTECTED]> wrote:
> > "Eduardo O. Padoan" <[EMAIL PROTECTED]> writes:
> >
> > > No, writing this way will confound the 2to3 tool.
> >
> > Why?  print("foo") is a perfectly valid Python 2 statement.  Maybe
> > it's simply a matter of fixing the tool.
> >
> 
> print("foo") -> print(("foo"))
> 

And more to the point

(2.5) print(foo, bar) != (3.0) print(foo, bar)

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


Re: Bit Operations

2007-11-28 Thread J. Clifford Dyer
On Wed, Nov 28, 2007 at 10:05:40PM +0100, Gianmaria Iaculo - NVENTA wrote 
regarding Re: Bit Operations:
> 
> Txs all,
> i wont to respond to who asked why i needed it:
> 
> I'm using python on GSM modules and the informations i have to move goes 
> along GPRS/UMTS connections so it's beatiful for me to transfer more 
> informations with less space...
> imagine i have to send this simple data
> 
> 41.232323,12.345678
> 
> i can send it as it's or use the nibble trick and on the receiving station 
> 'unlift" the data and rebuild the original information...
> 
> isn'it???
> 

Um, no.  It isn't.  How exactly are you going to pack floating point numbers 
into a half a byte?

Or are you sending it as strings?  Also a waste of space, and unnecessarily 
complex.


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


Re: Bit Operations

2007-11-28 Thread J. Clifford Dyer
On Wed, Nov 28, 2007 at 09:07:56PM +0100, Gianmaria Iaculo - NVENTA wrote 
regarding Bit Operations:
> 
> Hi there,
> I'm so new to python (coming from .net so excuse me for the stupid question) 
> and i'm tring to do a very simple thing,with bytes.
> 
> My problem is this:
> 
> i've a byte that naturally is composed from 2 nibbles hi&low, and two 
> chars.. like A nd B. What i wonna do is to write A to the High nibble and B 
> to the the lower nibble.
> Or an other example can be i've 2 numbers.. like 7 and 8 and whant to do the 
> same as for chars.
> 
> I'm really confused on how t do it, maybe cause python is type-less (dynamic 
> typed)
> 
Do you possibly mean that your letters are hexadecimal digits?  If so, you can 
follow the advice given to you by others for numbers, treating your letters as 
numbers:

A=10
B=11
...
F=15

py>>> hex(15)
'0xf'
>>> int('f', 16)
15
>>> int('0xf', 16)
15

Cheers,
Cliff

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


Re: How to Teach Python "Variables"

2007-11-28 Thread J. Clifford Dyer

On Wed, Nov 28, 2007 at 11:23:42AM -0600, Chris Mellon wrote regarding Re: How 
to Teach Python "Variables":
> 
> On Nov 28, 2007 10:57 AM, hdante <[EMAIL PROTECTED]> wrote:
> > On Nov 28, 2:12 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> > >
> > > Right. Python variables are pointers, except for all the ways that
> > > they are different. By the same criteria, they are also puppies. Give
> > > it a rest.
> >
> >  I'm sorry if your notion of pointer is incorrect. A pointer (or, more
> > formally, a reference) is an object that points to some value. A
> > python variable isn't different than that in any way.
> >
> 
> A python name isn't an object. The difference between names and
> objects is at the core of this entire conversation. In fact, you are
> exactly the sort of person this discussion was targeted at, because
> you don't understand the difference.
> 
> You can play semantic games if you want and just declare that
> everything is the same at some fundamental level. It's a little like
> taking your ball and going home, because insisting on names that don't
> indicate the differences between things is useless at best and harmful
> at worst.
> 
> Whether you like it or not, the term "pointer" has a specific common
> meanings. They do not mean "any means by which you may reference a
> value". "variable" is certainly less clear and is used much more
> loosely, but in the specific context of comparison to C, python
> name/object bindings are not the same as C variables.
> 
> Pointers are values. They also point at other values, and those values
> may be other pointers, ad infinitum. Understanding this is core to
> understanding and using pointers.
> 
> Python names are *not* values, and all they do is tag a specific
> object in a specific namespace. You can't have a name that refers to
> another name, names can only refer to objects. Understanding this is
> core to understanding and using the Python object model, *especially*
> if some nitwit has been telling you that they're the same as pointers.
> -- 
> http://mail.python.org/mailman/listinfo/python-list

OK, now we're down to mock sympathy, insulting analogies and name-calling.  Can 
we just let this thread die.  It was an interesting discussion for a while, but 
it's moved into that ugly putrefaction stage now.  Nothing new is being said, 
and the old stuff is being said more rudely than before.

This thread is bleedin' demised.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best ways of managing text encodings in source/regexes?

2007-11-27 Thread J. Clifford Dyer
On Tue, Nov 27, 2007 at 01:30:15AM +0200, tinker barbet wrote regarding Re: 
Best ways of managing text encodings in source/regexes?:
> 
> Hi
> 
> Thanks for your responses, as I said on the reply I posted I thought
> later it was a bit long so I'm grateful you held out!
> 
> I should have said (but see comment about length) that I'd tried
> joining a unicode and a byte string in the interpreter and got that
> working, but wondered whether it was safe (I'd seen the odd warning
> about mixing byte strings and unicode).  Anyway what I was concerned
> about was what python does with source files rather than what happens
> from the interpreter, since I don't know if it's possible to change
> the encoding of the terminal without messing with site.py (and what
> else).
> 
> Aren't both ASCII and ISO-8859-1 subsets of UTF-8?  Can't you then use
> chars from either of those charsets in a file saved as UTF-8 by one's
> editor, with a # -*- coding: utf-8 -*- pseudo-declaration for python,
> without problems?  You seem to disagree.
> 
I do disagree.  Unicode is a superset of ISO-8859-1, but UTF-8 is a specific 
encoding, which changes many of the binary values.  UTF-8 was designed 
specifically not to change the values of ascii characters.  0x61 (lower case a) 
in ascii is encoded with the bits 0110 0001.  In UTF-8 it is also encoded 0110 
0001.  However, ?, "latin small letter n with tilde", is unicode/iso-8859-1 
character 0xf1.  In ISO-8859-1, this is represented by the bits  0001.  

UTF-8 gets a little tricky here.  In order to be extensible beyond 8 bits, it 
has to insert control bits at the beginning, so this character actually 
requires 2 bytes to represent instead of just one.  In order to show that UTF-8 
will be using two bytes to represent the character, the first byte begins with 
110 (1110 is used when three bytes are needed).  Each successive byte begins 
with 10 to show that it is not the beginning of a character.  Then the 
code-point value is packed into the remaining free bits, as far to the right as 
possible.  So in this case, the control bits are 

110x  10xx .  

The character value, 0xf1, or:
 0001

gets inserted as follows:

110x xx{11}  10{11 0001}

and the remaining free x-es get replaced by zeroes.

1100 0011  1011 0001.

Note that the python interpreter agrees:

py>>> x = u'\u00f1'
py>>> x.encode('utf-8')
'\xc3\xb1'

(Conversion from binary to hex is left as an exercise for the reader)

So while ASCII is a subset of UTF-8, ISO-8859-1 is definitely not.  As others 
have said many times when this issue periodically comes up: UTF-8 is not 
unicode.  Hopefully this will help explain exactly why.

Note that with other encodings, like UTF-16, even ascii is not a subset.

See the wikipedia article on UTF-8 for a more complete explanation and external 
references to official documentation (http://en.wikipedia.org/wiki/UTF-8).



> The reason all this arose was that I was using ISO-8859-1/Latin-1 with
> all the right declarations, but then I needed to match a few chars
> outside of that range.  So I didn't need to use u"" before, but now I
> do in some regexes, and I was wondering if this meant that /all/ my
> regexes had to be constructed from u"" strings or whether I could just
> do the selected ones, either using literals (and saving the file as
> UTF-8) or unicode escape sequences (and leaving the file as ASCII -- I
> already use hex escape sequences without problems but that doesn't
> work past the end of ISO-8859-1).
> 

Do you know about unicode escape sequences?

py>>> u'\xf1' == u'\u00f1'
True

> Thanks again for your feedback.
> 
> Best wishes
> Tim
> 

No problem.  It took me a while to wrap my head around it, too.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to write Regular Expression for recursive matching?

2007-11-26 Thread J. Clifford Dyer
On Mon, Nov 26, 2007 at 06:04:54PM +0100, Diez B. Roggisch wrote regarding Re: 
How to write Regular Expression for recursive matching?:
> 
> lisong wrote:
> 
> > Hi All,
> > 
> > I have problem to split a string like this:
> > 
> > 'abc.defg.hij.klmnop'
> > 
> > and I want to get all substrings with only one '.' in mid. so the
> > output I expect is :
> > 
> > 'abc.defg', 'defg.hij', 'hij.klmnop'
> > 
> > a simple regular expression '\w+.\w' will only return:
> > 'abc.defg', 'hij.klmnop'
> > 
> > is there a way to get 'defg.hij' using regular expression?
> 
> Nope. Regular expressions can't get back in their input-stream, at least not
> for such stuff.
> 
> The problem at hand is easily solved using
> 
> s = 'abc.defg.hij.klmnop'
> 
> pairs = [".".join(v) for v in zip(s.split(".")[:-1], s.split(".")[1:])]
> 

which is veritably perlesque in its elegance and simplicity!

A slightly more verbose version.

l = s.split('.')
pairs = []
for x in xrange(len(l)-1):
pairs.append('.'.join(l[x:x+2]))

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need to call functions/class_methods etc using string ref :How

2007-11-26 Thread J. Clifford Dyer
On Mon, Nov 26, 2007 at 04:07:03PM +0530, Ravi Kumar wrote regarding Need to 
call functions/class_methods etc using string ref :How:
> 
>Hi,
>First of all, since this is my first mail to Python-List, I want to say
>"Hello world!"
>After that;
>I am stuck in a project. Actually I am writing a module (for testing
>now), which takes URL, parses it, finds which modules and then which
>method to call or which class to initiate and which string to load.
>So far, I have done some basic URL manipulation and validation and
>extracted the name of modules in  a dict
>{
>  'ModuleName-1': None,
>  'ModuleName-2': None
>  --ETC--
>}
>Now I want your help about how to call the  function i.e _render() in
>the module. I have to iterate it calling all modules/also Class.methods
>and assinging the values in dict for each key as modulename.
>Means,
>just take that moduleName is a string which contains the name of module
>to load.
>FuncName is the string which contains the name of def  to
>call
>clName is the string which contains the name of the Class, which is to
>init and get returned object.
>means everything in string.
>ALso, if there are multiple methods to do it, if you provide a little
>pros/cons and comments, it would really be very nice of you.
>Before I came here, I have searched Google, and found a way
>hasattr()/getattr(), but that confused me a little and didnt worked for
>me. I am missing something I know, so please ENLIGHTEN Me :)
>Thanks in advance EVen you read this mail :P
>--
>-=Ravi=-

I see someone already showed you eval.  Eval is evil.  Don't use it.  
Especially if the functions are coming to you from a public URL!  

You are on the right track putting the module names in a dictionary.  Now tie 
those dictionary keys to functions.

func = { 'function_a': function_a,
 'function_b': function_b }

now you can call them as follows, with the desired function name extracted from 
the URL into the variable f_request:

if f_request in func:
result = func[f_request]()

This way, users can't call functions you haven't explicitly added to the func 
dictionary.

Cheers,
Cliff


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


Re: basic if stuff- testing ranges

2007-11-25 Thread J. Clifford Dyer

On Sun, 2007-11-25 at 20:49 +0200, Donn Ingle wrote:
> Sheesh, I've been going spare trying to find how to do this short-hand:
> if 0 > x < 20: print "within"
> 


  if 0 > x: print "within"


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


Re: How to import xplt, pylab?

2007-11-22 Thread J. Clifford Dyer
On Thu, Nov 22, 2007 at 02:00:00PM -0800, Caren Balea wrote regarding How to 
import xplt, pylab?:
> 
> Hello,
> 
> I'm newbie to python.
> 
> So far, I'm a bit disappointed. It's awful to set Python up to work.
> It's not working!!!
> 
> Ok, calm down. Here are my settings:
> I'm using Windows XP machine and have installed
> Python 2.5.1.
> Also, I have also installed something called "Cygwin" to type
> in my commands.
> Finally, I have installed scipy-0.6.0.
> 
> After starting Cygin I type
> 
> python
> import math
> import scipy
> *
> Then I try
> 
> import xplt
> 
> and get the message
> Traceback (most recent call last):
> File "", line 1, in 
> ImportError: No module named xplt
> *
> 

xplt is a module within scipy, so it will not be available at the top level.  
However, all you need to do is

py>>> from scipy.sandbox import xplt

or

py>>> import scipy.sandbox.xplt

The advantage of the former is that you can just use the names xplt.*, while 
with the latter you have to type scipy.sandbox.xplt.*.

> Also I try
> 
> import pylab
> 
> and get the message
> Traceback (most recent call last):
> File "", line 1, in 
> ImportError: No module named pylab
> 
> *
> 

I haven't worked with pylab, but it looks like you may need another install 
(matplotlib?)  I'm not sure.  I just did a quick google search.

> So, how can I use xplt and pylab???
> 
> Any help would be much appreciated!!!
> 
> Thank you,
> Caren

Happy Thankgiving hacking!

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: eof

2007-11-22 Thread J. Clifford Dyer
On Thu, Nov 22, 2007 at 07:17:41AM -0800, braver wrote regarding Re: eof:
> 
> On Nov 22, 6:08 pm, "J. Clifford Dyer" <[EMAIL PROTECTED]> wrote:
> 
> > > So why Python's IO cannot yield f.eof() as easily as Ruby's can?  :)
> 
> > Because that's not how you compare languages.  You compare languages by 
> > stating what you are actually trying to do, and figuring out the most 
> > natural solution in each language.  Not "I can do this in x--how come I 
> > can't do it in y?"
> 
> Python doesn't have f.eof() because it doesn't compare to Ruby?  Or
> because I'm trying to compare them?  :)  That's giving up to Ruby too
> early!
> 

No and no, to your two questions.  I'm not giving up on Ruby at all.  In fact, 
I've never tried Ruby.  My point isn't that the languages don't compare.  My 
point is that your question shouldn't be "why doesn't python have an eof method 
on its file objects?"  Your question should be "I want to do something 
different with the last line of a file that I iterate over.  How do I do that 
best in python?"  You've been given a couple solutions, and a very valid reason 
(performance) why buffered file objects are not the default.  You may also 
consider trying subclassing file with a buffered file object that provides 
self.eof.  (I recommend making it an attribute rather than a method.  Set it 
when you hit eof.)  That way you have the fast version, and the robust version.

You may find something of interest in the for/else construction as well

for line in file:
pass
else:
# This gets processed at the end unless you break out of the for loop.
pass


> Ruby has iterators and generators too, but it also has my good ol'
> f.eof().  I challenge the assumption here of some majectically Python-
> wayist spirit forbidding Python to have f.eof(), while Ruby, which has
> all the same features, has it.  Saying "it's not the Python way" is
> not a valid argument.
> 

No, but showing a different python way is more valid, and if you were more 
forthcoming about your use case from the get-go, you would have gotten fewer 
vague answers.  

> The suspicion lurking in the thread above was that that has to do with
> Python IO buffering, that it somehow can't tell the f.eof() with
> automatic look-ahead/push-back/simulate read, as transparently an
> effectively as (in practice) Ruby does without much fuss.  The reason
> why such a useful feature -- useful not in Ruby or Perl or Pascal, but
> algorithmically -- is not present in Python is a recurrent mystery,
> evidenced in this group recurrently.
> 

A mystery which has been answered a couple times in this thread--it causes a 
performance hit, and python is designed so that you don't suffer that 
performance hit, unless you want it, so you have to program for it yourself.

You yourself said that performance is a complaint of yours regarding Ruby, so 
why claim that Ruby's way is clearly better in a case where it causes a known 
performance hit?

> Cheers,
> Alexy

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: eof

2007-11-22 Thread J. Clifford Dyer
On Thu, Nov 22, 2007 at 06:53:59AM -0800, braver wrote regarding Re: eof:
> 
> > Language comparisons are sometimes good. They are best when
> > they are free of FUD.
> 
> So why Python's IO cannot yield f.eof() as easily as Ruby's can?  :)

Because that's not how you compare languages.  You compare languages by stating 
what you are actually trying to do, and figuring out the most natural solution 
in each language.  Not "I can do this in x--how come I can't do it in y?" 

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why it is invalid syntax?

2007-11-22 Thread J. Clifford Dyer
On Thu, Nov 22, 2007 at 06:47:33AM +, Marc 'BlackJack' Rintsch wrote 
regarding Re: why it is invalid syntax?:
> 
> It's quite unreadable and if this would be allowed you would have to
> introduce a special rule to forbid ``else``, ``except`` and ``finally``
> because it can lead to ambiguities. To which ``if`` does the ``else``
> belong to here? ::
> 
>   if 1: print 1 if: 1 print 1 else: print 1
> 
> Ciao,
>   Marc 'BlackJack' Rintsch

I don't reckon in matters much.  Your output will be:

1
1

;)

No, actually on second inspection your output will be:

File "", line 1
if 1: print 1 if: 1 print 1 else: print 1
^
SyntaxError: invalid syntax

But it's a good point.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the annoying, verbose self

2007-11-22 Thread J. Clifford Dyer
On Thu, Nov 22, 2007 at 10:13:46AM +0100, A.T.Hofkamp wrote regarding Re: the 
annoying, verbose self:
> 
> On 2007-11-22, Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> > On Wed, 21 Nov 2007 15:51:56 -0800, braver wrote:
> >
> >> Is there any trick to get rid of having to type the annoying,
> >> character-eating "self." prefix everywhere in a class?
> 

You're tilting against windmills.  This has been gone over many times.  There 
are very valid reasons for the explicit self, which I understood well enough 
when I read about them to stop complaining about self, but not well enough to 
be able to justify to someone else.  Sorry for that.  But since Alexy likes 
finding old threads that address his concerns, maybe he'll hunt them down, 
himself.  

> You got this highly flexible language, very good for rapid programming, no 
> more
> clutter from block brackets and variable declarations, and now this 'self' 
> pops
> up.
> 
> So much overhead
> 
> Can the computer not decide by itself what I want stored in the object? It can
> read my code too!
> 

But not every variable declared inside a class needs to be assigned to self.  
Make judicious use of non-self variables, if you don't want to see self 
everywhere.

> 
> > Oh I know! It' uch a pain. Sinc writing a hug cla lat wk, I'v had a 
> > trribl hortag o lowrca S E L and F charactr. It mak writing vry annoying.
> 
> Yes annoying isn't it?
> 
> Last week I was programming in C++ again, and now I have this terrible 
> sur-plus
> of lowercase T H I and S letters. I don't understand how they do it (I used 
> to,
> but not any more).
> 
> Maybe we should setup an exchange program so everybody can trade letters with
> each other.
> 
> 
> >> Sometimes I avoid OO just not to deal with its verbosity.
> >
> > There are other values than brevity. In fact, brevity is one of the less 
> > important values.
> 
> NO! You got it all wrong here! It is not about brevity, it is about SPEED.
> 

if you're looking for speed of typing, just type a one character self 
equivalent, like:

class q(object):
def method(;, arg):
;.var = 3

Then when you're done coding: :%s/;/self/g

> With less letters, there is less to read, so it can be read faster, so it must
> be better!
> 

Ah.  Speed of reading.  In fact, speed of reading is connected to clarity, not 
brevity.  If you have to keep looking back to some declaration section to 
remember if a given variable is tied to self, it's going to slow down your 
comprehension speed.  the written chinese language is far more concise than 
japansese, but it's faster to skim japanese, because of the three alphabet 
system.  Most of the meaning is contained in kanji, while the hiragana supplies 
disambiguation and grammatical specifiers, so you know you can skip past the 
hiragana and still retain the basic meaning.  Having self used by convention 
means that once you get used to seeing it, you don't need to read it any more.  
It would really mess with python coders, though, if each person made up their 
own self-equivalent.  You write "s", I write "self", Larry Wall starts coding 
python and uses "my".  Then we'd all have to actually pay attention, and 
reading really would slow down.  That's probably why self seems to !
 be the most consistently held convention in python.  Occasionally, someone 
will use cls, but that's for a different context, where you really do want 
people to slow down and realise that something different is going on.

> Just like "if x:" is MUCH BETTER than "if x != 0:"
> 
Only when "if x:" is what you mean.  The semantics are different:

py>>> x = ''
py>>> if x: print "x"
py>>> if x != 0: print "x != 0"
x != 0

> 
> The thing that should be fixed imho, is that the brain of some of us tries to
> deduce higher levels of abstractions from what is essentially a very long line
> of simple instructions.
> 

Sorry, no entiendo.  What needs to be fixed here?

> 
> >> But things grow -- is there any metaprogramming tricks or whatnot we can
> >> throw on the self?
> >
> > Oh yeah, that's just what I like to see! Complicated, brittle, hard to 
> > debug, difficult to understand metaprogramming tricks in preference to a 
> > simple, easy-to-read naming convention.
> 
> Maybe we should state first which variables we want to store in self, and then
> have a meta-programming hook that automatically puts assignments to those
> variables into the object.
> And while we are at it, maybe we should also state the type of data we want to
> put in it. That should help too.
> 
> Now that would be progress.
> 

I hope you are taking the piss.

> 
> Ah well, I guess we will have to wait a while before that happens.
> Albert

Oh God.  You are taking the piss.  IHBT.


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


Re: Python strings question (vertical stack)

2007-11-20 Thread J. Clifford Dyer
On Tue, 2007-11-20 at 13:59 -0800, John Machin wrote:
> On Nov 21, 7:15 am, Farshid Lashkari <[EMAIL PROTECTED]> wrote:
> > J. Clifford Dyer wrote:
> > > I think you mean '\n'.join([string1,string2,string3])
> >
> > > You actually do want the \ to do its thing in this case.
> >
> > Yeah, my brain must still be asleep. Thanks for waking it up :)
> 
> You're not alone :-)
> 
> >>> a = """
> ... x
> ... y
> ... z
> ... """
> >>> a
> '\nx\ny\nz\n'
> >>> '\n'.join(['x', 'y', 'z'])
> 'x\ny\nz'
> >>>

It's true: my solution did not match the exact specification given by
the OP's code, but 1) I was responding to Farshid Lashkari's slip and 2)
It does what the OP was ultimately asking for--it stacks the results
vertically.  My brain was not asleep, I was just not interested in the
other, largely irrelevant part of the OP's question.  If he wants a new
line at the beginning and end, I'm sure he can figure out how to do it.
I don't need to jump through those hoops for him.

Cheers,
Cliff



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


Re: [regex] Basic rewriting

2007-11-20 Thread J. Clifford Dyer
On Wed, 2007-11-21 at 03:24 +0100, Gilles Ganault wrote:
> Hello
> 
>   I've been reading tutorials on regexes in Python, but I still
> don't get it:
> 
> 
> #!/usr/bin/python
> 
> #myscript.py 0123456789
> 
> import sys,re
> 
> #Turn 0123456789 into 01.23.45.67.89
> p = re.compile('(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)')
> phone = p.sub('\1.\2.\3.\4.\5',sys.argv[1])
> print phone
> 
> 
> => Python displays "" instead. Any idea what I'm doing wrong?
> 
> Thank you.

Use raw strings for re expressions.

r'this is a raw string'
'this is not'

p = re.compile(r'(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)')
phone = p.sub(r'\1.\2.\3.\4.\5',sys.argv[1])

Should clear up your problem.

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


Re: Writing Error in program

2007-11-20 Thread J. Clifford Dyer
On Tue, Nov 20, 2007 at 01:02:38PM -0800, [EMAIL PROTECTED] wrote regarding 
Writing Error in program:
> 
> I have a code that writes to 2 seperate files.  I keep getting a "list
> index out of range" error.  The strange part is that when checking the
> files that I'm writing too, the script has already iterated through
> and finished writing, yet the error stated implies that it hasn't?  So
> how can it be, that my script has written to the files, yet the error
> is stating that it hasn't made it through the script?  I'll have 15
> files that I have written to and the script will bog out at number
> 10?  Is python doing something I'm not seeing?  I printed everything
> that was written on the shell and it shows that it went through the
> script, so how can it still say there are a few files left to iterate
> through?

As others have mentioned, posting code would be very helpful.  Also, what you 
say doesn't sound right.  "List index out of range" does not mean there are a 
few files left to iterate through.  It means that you have a list somewhere and 
you are trying to access an index beyond the last list item.

So say you have the following list:

l=['a','b','c']

and you try to access each item in it with the following loop:

for x in range(4):
print l[x]

You will get the following output.

a
b
c
Traceback (most recent call last):
  File "", line 2, in ?
IndexError: list index out of range

in other words it will print l[0], l[1], and l[2], but then when it tries to 
print l[3], it will raise an IndexError, because there is no l[3].  This does 
not mean it still has files to process.  More likely, it means it has overshot 
the files it does have to process, but more likely still it has nothing to do 
with file access.  We can't help you diagnose that without a code sample, 
though.

Cheers,
Cliff



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


Re: Python strings question (vertical stack)

2007-11-20 Thread J. Clifford Dyer
On Tue, Nov 20, 2007 at 11:40:59AM -0800, Farshid Lashkari wrote regarding Re: 
Python strings question (vertical stack):
> 
> dmitrey wrote:
> > Hi all,
> > I have some strings, let it be string1, string2, string3.
> > 
> > So how could String=
> > """
> > string1
> > string2
> > string3
> > """
> > 
> > be obtained?
> > 
> > Thank you in advance, D.
> 
> If you just want the to add newlines between the strings then you can do 
> the following:
> 
> String = '\\n'.join([string1,string2,string3])
> 
> -Farshid
> -- 
> http://mail.python.org/mailman/listinfo/python-list

I think you mean '\n'.join([string1,string2,string3])

You actually do want the \ to do its thing in this case.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


[EMAIL PROTECTED]: Re: overriding methods - two questions]

2007-11-19 Thread J. Clifford Dyer
On Mon, Nov 19, 2007 at 04:33:39PM +0100, Bruno Desthuilliers wrote regarding 
Re: overriding methods - two questions:
> 
> J. Clifford Dyer a ?crit :
> > On Mon, Nov 19, 2007 at 01:41:46PM +0100, Bruno Desthuilliers wrote 
> > regarding Re: overriding methods - two questions:
> >> Steven D'Aprano a ?crit :
> >>> On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote:
> >>>
> >>>>> Question 1:
> >>>>>
> >>>>> Given that the user of the API can choose to override foo() or not, how
> >>>>> can I control the signature that they use?
> >>>> While technically possible (using inspect.getargspec), trying to make
> >>>> your code idiot-proof is a lost fight and a pure waste of time.
> >>>
> >>> Worse: it's actually counter-productive!
> >>>
> >>> The whole idea of being able to subclass a class means that the user 
> >>> should be able to override foo() *including* the signature. 
> >> If you see subclassing as subtyping, the signatures should always stay 
> >> fully compatibles.
> >>
> > 
> > Isn't that more what Zope-type interfaces are for than inheritance?
> 
> With dynamically typed languages like Python, you don't need any formal 
> mechanism (zope-like interface or whatever) for subtyping to work - just 
> make sure your two classes have the same public interface and it's ok. 
> So indeed inheritance is first a code-reuse feature.
> 

Makes sense.  As nifty as interfaces seem, there's also something that struck 
me as too club-like about them.  Your suggestion seems much more pythonically 
dynamic and ducky.

> > I'm uncertain here, but I'm not persuaded that changing signature is
> > bad.
> 
> Depends if the subclass is supposed to be a proper subtype (according to 
> LSP) too.

Also makes sense, somewhat, but I'll need to reread the previously linked 
article, because I'm not clear in my mind on what makes a subclass a subtype 
*other than* having a matching signature.

Thanks,
Cliff

- End forwarded message -
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: overriding methods - two questions

2007-11-19 Thread J. Clifford Dyer
On Mon, Nov 19, 2007 at 01:41:46PM +0100, Bruno Desthuilliers wrote regarding 
Re: overriding methods - two questions:
> 
> Steven D'Aprano a ?crit :
> > On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote:
> > 
> >>> Question 1:
> >>>
> >>> Given that the user of the API can choose to override foo() or not, how
> >>> can I control the signature that they use?
> >> While technically possible (using inspect.getargspec), trying to make
> >> your code idiot-proof is a lost fight and a pure waste of time.
> > 
> > 
> > Worse: it's actually counter-productive!
> > 
> > The whole idea of being able to subclass a class means that the user 
> > should be able to override foo() *including* the signature. 
> 
> If you see subclassing as subtyping, the signatures should always stay 
> fully compatibles.
> 

Isn't that more what Zope-type interfaces are for than inheritance?  I'm 
uncertain here, but I'm not persuaded that changing signature is bad.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Sorting Countries by Region

2007-11-17 Thread J. Clifford Dyer

On Sat, 2007-11-17 at 03:34 -0800, [EMAIL PROTECTED] wrote:
> #This seems to not work today and I don't know why
> #for country in countries_list:
> #if country not in REGIONS_COUNTRIES['European Union'] or not in
> REGIONS_COUNTRIES['North America']:
> #print "%s is not in the expected list", country
> 

This snippet fails because you want 'and' rather than 'or'.  In this
one, you test to see that country is not in EU, and if you have, say
'France' as your country, the first half evaluates false, so the or
tells it to try again with North America.  Lo and behold, france is not
in North America, so the second half returns true.  False or True
returns True, so your code prints "France is not in the expected list."

Cheers,
Cliff


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


Re: implement random selection in Python

2007-11-17 Thread J. Clifford Dyer
On Fri, 2007-11-16 at 16:47 -0800, Bruza wrote:
I think I need to explain on the probability part: the "prob" is a
> relative likelihood that the object will be included in the output
> list. So, in my example input of
> 
>   items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)]
> 
> So, for any size of N, 'Tom' (with prob of 45) will be more likely to
> be included in the output list of N distinct member than 'Mary' (prob
> of 30) and much more likely than that of 'John' (with prob of 10).
> 
> I know "prob" is not exactly the "probability" in the context of
> returning a multiple member list. But what I want is a way to "favor"
> some member in a selection process.
> 
My understanding of what you are looking for is that on each individual 
selection, the probability of picking a given name is that name's prob value, 
divided by the sum of all prob values.  That is, in the following case:

items = [('Mary',96),('Shimon',1),('Aisha',1),('Toshiro',2)]
N = 3

On the first pass Mary has a 96% chance of getting selected, Shimon a 1% 
Chance, Aisha a 1% chance and Toshiro a 2% chance.

If Mary gets selected, then on the second round Shimon and Aisha should each 
have a 25% chance of getting selected, and Toshiro a 50% chance.

If Shimon gets selected, then on round three, Aisha will have a ~33% chance, 
and Toshiro a little less than 67%.

If that is correct, then the following might meet your needs:

from random import choice

def select(weighted_list, n=1):
selected = set()
for iteration in xrange(n):
print iteration
selection_list = []
for item in weighted_list:
if item[0] not in selected:
selection_list.extend([item[0]] * item[1])
#print selection_list
this_choice = choice(selection_list)
#print this_choice
selected.add(this_choice)
return selected

if __name__ == '__main__':
items = [('Mary',96),('Shimon',1),('Aisha',1),('Toshiro',2)]
N = 3
print select(items, 3)


It creates a new selection list at every pass, but it should be a marked 
improvement over solutions that have to flail against already chosen values, 
especially in unbalanced cases like the one I've shown above.  I have not run 
tests to establish this for certain.  Exercise left to the reader.  

If you care who was chosen first or last (like, say, if this is for selecting 
members of a kick ball team), you may want to replace the selected set with a 
list.  Or you may want to keep both the set and a list.  Depends on your needs.

Cheers,
Cliff

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


Re: sorting contacts alphabetically, sorted by surname

2007-11-16 Thread J. Clifford Dyer
On Fri, Nov 16, 2007 at 12:31:19PM +, Marie Hughes wrote regarding sorting 
contacts alphabetically, sorted by surname:
> 
>HI
> 
>I have to write a program that contains a text file in the following
>format:
> 
>AcademicPositionRoom Ext.   Email
>Prof Marie Maguire  Head of
>School  M9002    [EMAIL PROTECTED]
> 
>And produce a output file where conatcts are sorted alphabetically-
>sorted by surname.
> 
>Has anyone any ideas.

Well, the first thing you need to do is develop a workable understanding of how 
your data file is formatted.  What separates one field from another?  Is it 
tab-delimited?  Does the field always start at a given column number?  Then you 
need to read each line of the file, and split the lines up according to the 
format.  Save to a data structure, sort by last name (you'll have to further 
parse the name to isolate this information), and reprint the results.

Depending on the details you uncover about the precise format of your data 
file, you will find it helpful to read the documentation on string methods, 
particularly s.split(), and sequence slicing: s[4:14].

Best of luck with it.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing user input?

2007-11-15 Thread J. Clifford Dyer
On Thu, Nov 15, 2007 at 09:03:26AM -0800, Mohammed_M wrote regarding Printing 
user input?:
> 
> Hi,
> I'm v.new to Python, so please don't be too harsh :)
> I get a NameError with the code below - All I want to do is store some
> input taken from the user in a variable called name, & then print name
> 
> # START CODE ==
> # Print name demo
> 
> 
> def PersonsDetails():
> name = input("What's your name?")
> PersonsDetails()
> 
> print(name)
> 
> 
> # END CODE ==
> 
> Thanks for reading & any help on this
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Also,  you'll want to use raw_input() instead of input.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating Installer or Executable in Python

2007-11-14 Thread J. Clifford Dyer
On Wed, Nov 14, 2007 at 03:34:14PM +0100, Laszlo Nagy wrote regarding Re: 
Creating Installer or Executable in Python:
> 
> DanielJohnson wrote:
> > I have a small project which has around 10 .py files and I run this
> > project using command line arguments. I have to distribute this
> > project to somebody.
> >
> > I was wondering how can I make an executable or some kind of
> > installer, so that end user doesn't need to compile and worry if he/
> > she has Python installed or not ?
> >   
> http://www.fuckinggoogleit.com/?q=py2exe
> 

While I agree with the sentiment, I'm left wondering how you think he would 
have known to google for py2exe.  Maybe you meant:

http://www.fuckinggoogleit.com/?q=python+executable

(Which also yields py2exe as the first result.)

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why no automatic conversion in string concatenation?

2007-11-13 Thread J. Clifford Dyer
On Tue, Nov 13, 2007 at 07:15:06AM -0800, Michael Pelz Sherman wrote regarding 
why no automatic conversion in string concatenation?:
> 
>As a Java & PHP developer, I find it kind of annoying that I have to
>explicitly convert non-string variables to strings when concatenating
>them, especially when python is quite capable of doing the conversion
>automatically.
>i.e.:
>>>> myBool = True
>>>> print myBool
>True
>>>> print "myBool is " + myBool
>Traceback (most recent call last):
>  File "", line 1, in 
>TypeError: cannot concatenate 'str' and 'bool' objects
>>>> print "myBool is " + str(myBool)
>myBool is True
>Can anyone explain why this is so? 

Because python doesn't know if '1' + 1 should equal 2 or '11' and would rather 
you mad that decision.  Should it be different than 1 + '1'?  

or to put it more succinctly, because "explicit is better than implicit."
In fact, I think it's more often the case that I have string data that I need 
to treat as integers than the other way around (input from stdin and textfiles 
for example).

> Are there any plans to change this
>in python 3000?

I hope not, and I don't think so.

>Thanks,
>- Michael

No problem. 

Cheers,
Cliff

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


Re: regular expressions

2007-11-06 Thread J. Clifford Dyer
On Tue, Nov 06, 2007 at 08:49:33AM -0800, [EMAIL PROTECTED] wrote regarding 
regular expressions:
> 
> hi i am looking for pattern in regular expreesion that replaces
> anything starting with and betweeen http:// until /
> like http://www.start.com/startservice/yellow/ fdhttp://helo/abcd will
> be replaced as
> p/startservice/yellow/ fdp/abcd
> 

You don't need regular expressions to do that.  Look into the methods that 
strings have.  Look at slicing. Look at len.  Keep your code readable for 
future generations.

Py>>> help(str)
Py>>> dir(str)
Py>>> help(str.startswith)

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python newbie

2007-11-02 Thread J. Clifford Dyer
On Fri, Nov 02, 2007 at 11:13:00AM -0400, Jim Hendricks wrote regarding Re: 
python newbie:
> 
> BartlebyScrivener wrote:
> > On Nov 2, 8:51 am, Jim Hendricks <[EMAIL PROTECTED]> wrote:
> >> New to python, programming in 15 or so langs for 24 years.
> >>
> >> Couple of questions the tuts I've looked at don't explain:
> > 
> > Did you look at THE tut?  You would seem to be the perfect reader for
> > it, because you are already a programmer.
> > 
> > http://docs.python.org/tut/node6.html#SECTION00660
> > 
> > rd
> > 
> 
> I initially looked at THE tut, and it came across so techy that it's 
> such a slow read.  Therefore, I looked to other tuts that were very easy 
> reads and could give me the basic lowdown of the language.  Problem of 
> course is the easy read tuts don't get into the details.
> 
> That said, I looked at the section of the tutorial you provided (thanks) 
> and I am still confused.  It specifies a global var cannot be assigned 
> unless it's specified in the global statement.
> 
> Here's an example of what I am asking:
> 
> def my_function():
>global x
> 
>x = open( 
> 
> before calling my_function, x does not exist in my program.  So, my 
> question is in my_function, the combination of using the global 
> statement, then implicitly creating x via assignment to the result of 
> the open function, is x created in the global namespace?

Yes.

>>> y

Traceback (most recent call last):
  File "", line 1, in 
y
NameError: name 'y' is not defined
>>> def f():
global y
y = 123
>>> f()
>>> y
123
>>> 

Remember, the interactive interpreter is your friend.  Just type 'python' at 
the command line and hammer away.  (It's even better through a decent IDE).

Oh yeah, you have to actually call f before y will get defined.  

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: shouldn't 'string'.find('ugh') return 0, not -1 ?

2007-10-31 Thread J. Clifford Dyer
On Wed, Oct 31, 2007 at 03:55:49PM +0100, jelle feringa wrote regarding Re: 
shouldn't 'string'.find('ugh') return 0, not -1 ?:
> 
>There is a subtle point though.
> 
>If the substring is not found '_'.find(' '), will return -1
> 
>Semanticly, I was expecting the that if the substring was not found,
>the conditional statement would not be found.
> 
>However, python evaluates -1 to True, so that is what I do find
>confusing.
> 
>So, I was arguing that '_'.find(' ') might return 0, however that is
>obviously ambigious, since 0 might be an index as well.
> 
> 
> 
>So, perhaps I should rephrase and ask, why if -1 evaluates to True?
> 
>I think that's pretty ugly...
> 
> 

It is ugly, but there's no alternative.  In essence, you can't do a truth-test 
on it, because you're not testing the truth of the find statement.  The 
statement that you want to test the truth of is s.find(q) >= 0.  In other 
words, you want to see that the substring was found at a valid (non-negative) 
location. As someone else pointed out, it would make more sense to use None 
instead of -1.  You still couldn't use `if s.find(q):` because you'd get a 
false result for a substring found at the beginning of the string, but the 
return value would make more intuitive sense.  

Witness also the problem of actually using the result of find:

s[s.find(q)]

will yield a valid part of the string (the last character) when q is not found. 
 If it evaluated to none, you'd get a respectable TypeError instead.

But you still wouldn't--and never will--be able to say `if s.find(q)`, because 
valid array indices can be either true or false.

Cheers,
Cliff



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


Re: Parsing xml file in python

2007-10-30 Thread J. Clifford Dyer
On Tue, Oct 30, 2007 at 11:45:17AM -0700, [EMAIL PROTECTED] wrote regarding Re: 
Parsing xml file in python:

Top-posting corrected

> 
> 
> 
> On Oct 30, 12:32 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> > [EMAIL PROTECTED] schrieb:
> >
> >
> >
> > > I am a newbie in python
> > > I am trying to parse a xml file and write its content in a txt file.
> > > The txt contains null elements. Any reason what  iam doing wrong here
> >
> > > Here is the code that i wrote
> >
> > > import sys,os
> > > import xml.sax
> > > import xml.sax.handler
> > > from xml.sax.handler import ContentHandler
> > > from xml.sax import make_parser
> >
> > > class gmondxmlparse (ContentHandler):
> >
> > > def __init__(self,searchTerm):
> > > self.searchTerm=searchTerm;
> >
> > > def startElement(self,name,attrs):
> >
> > > if name=="HOST":
> > > self.hostname=attrs.get('NAME',"")
> > > self.IP=attrs.get('IP',"")
> > > elif name=="METRIC":
> > > self.metricname=attrs.get('NAME', "")
> > > self.metricvalue=attrs.get('VAL',"")
> > > self.metrictype=attrs.get('TYPE',"")
> > > self.metricunit=attrs.get('UNITS',"")
> > > return
> >
> > > def endElement(self,name):
> > > if name=="HOST" and self.searchTerm==self.hostname:
> > > try:
> > > fh=open('/root/yhpc-2.0/ganglia.txt' ,'w')
> > > except:
> > > print "File /root/yhpc-2.0/ganglia.txt can not be
> > > open"
> > > sys.exit(1)
> > > fh.write("This is a test for xml parsing with python with
> > > chris and amjad \n")
> > > fh.write("the host name is", self.hostname, "\n")
> > > fh.write("the ip address is", self.IP, "\n")
> > > fh.close()
> >
> > > searchTerm="HOST"
> > > parser=make_parser()
> > > curHandler=gmondxmlparse(searchTerm)
> > > parser.setContentHandler(curHandler)
> > > parser.parse(open("/root/yhpc-2.0/gmond.xml"))
> >
> > > Here is the sample of xml file
> >
> > > Here is the  xmk file called gmond.xml
> > >  > > TN="0" TMAX="20" DMAX="0" LOCATION="unspecified"
> > > GMOND_STARTED="1193170061">
> > >  > > TMAX="1200" DMAX="0" SLOPE="zero" SOURCE="gmond"/>
> >
> > Without an actual error given, it's hard to know what your problem is.
> >
> > One thing though is noticable: your XML below isn't valid - XML has only
> > one root-element.
> >
> > And just for the record: it appears that you work under linux using a
> > root-account. Bad idea. Really.
> >
> > http://linuxbraindump.org/2007/08/13/the-10-commandments-for-new-linu...
> >
> > Diez
> 
> 
> That XML is just a snapshot
> I am not getting into the xml parser. The error is not generated but
> also the /root/yhpc-2.0/ganglia.txt does not contain anything.
> 

Well, if ganglia.txt contains nothing, and you received no output from the 
program, then either endElement never got called, or `if name=="HOST" and 
self.searchTerm==self.hostname:` never evaluated to true.  Because if you 
couldn't open for writing, you would have gotten the message you set up on the 
except block, and if you could, then even if your variables didn't contain any 
data, you would have seen the boilerplate text that you wrote.

Cheers,
Cliff

P.S. Please bottom-post when replying to the python list.  It sucks to have to 
look up and down a thread to see what's been said.

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


Re: Iteration for Factorials

2007-10-30 Thread J. Clifford Dyer
On Tue, Oct 30, 2007 at 11:37:57AM -0700, [EMAIL PROTECTED] wrote regarding Re: 
Iteration for Factorials:
> 
> On Oct 30, 10:25 am, "J. Clifford Dyer" <[EMAIL PROTECTED]> wrote:
> > On Tue, Oct 30, 2007 at 01:09:38PM +0100, Boris Borcic wrote regarding Re: 
> > Iteration for Factorials:
> >
> >
> >
> > > Py-Fun wrote:
> > > > I'm stuck trying to write a function that generates a factorial of a
> > > > number using iteration and not recursion.  Any simple ideas would be
> > > > appreciated.
> >
> > > fact = lambda n : len(map([1].__imul__,range(1,n+1))[0])
> >
> > OK.  Now I've been sucked in.  How about this:
> >
> > def fact(x):
> > def f(x):
> > if int(x) != x:
> > raise ValueError
> > elif x > 1:
> > return f(x-1) ** x
> > elif x == 1:
> > return 10
> > else:
> > raise ValueError
> > return len(str(f(x))) -1
> >
> > The great part about this recursive solution is that you don't have to 
> > worry about the stack limit because performance degrades so quickly on the 
> > conversion to string!  fact(8) takes a little less than a second, fact(9) 
> > takes about a minute, and fact(10) takes more time than I had patience to 
> > wait for!
> 
> And the not-so-great part is that it raises an exception
> on fact(0) which makes it utterly useless for calculating
> combinations of m things taken n at a time: m!/n!*(m-n)!
> 
> Why is it that no one seems able to get this right?
> 

I can't speak for everyone, but my excuses are as follows:

* I haven't studied math or done math-heavy work in 8 years.
* I'm not at my home computer, and most of the thread (wherein, I now recall, 
that particular rule was specified) is downloaded to my home computer, so I was 
working from my feeble memory.
* I didn't care enough to google for it.

That said, s/elif x == 1:/elif x in (0,1):/ should solve the problem
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Iteration for Factorials

2007-10-30 Thread J. Clifford Dyer
On Tue, Oct 30, 2007 at 01:09:38PM +0100, Boris Borcic wrote regarding Re: 
Iteration for Factorials:
> 
> Py-Fun wrote:
> > I'm stuck trying to write a function that generates a factorial of a
> > number using iteration and not recursion.  Any simple ideas would be
> > appreciated.
> > 
> 
> fact = lambda n : len(map([1].__imul__,range(1,n+1))[0])
> 

OK.  Now I've been sucked in.  How about this:

def fact(x):
def f(x):
if int(x) != x:
raise ValueError
elif x > 1:
return f(x-1) ** x
elif x == 1:
return 10
else:
raise ValueError
return len(str(f(x))) -1

The great part about this recursive solution is that you don't have to worry 
about the stack limit because performance degrades so quickly on the conversion 
to string!  fact(8) takes a little less than a second, fact(9) takes about a 
minute, and fact(10) takes more time than I had patience to wait for!

Cheers,
Cliff

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


Re: Iteration for Factorials

2007-10-30 Thread J. Clifford Dyer
On Tue, Oct 30, 2007 at 01:09:38PM +0100, Boris Borcic wrote regarding Re: 
Iteration for Factorials:
> 
> Py-Fun wrote:
> > I'm stuck trying to write a function that generates a factorial of a
> > number using iteration and not recursion.  Any simple ideas would be
> > appreciated.
> > 
> 
> fact = lambda n : len(map([1].__imul__,range(1,n+1))[0])
> 
> hth :)
> 

Nice one.  I was trying to grok it, and started out by breaking it down:

>>> [1].__imul__(2)
[1, 1]
>>> map([1].__imul__,range(1,3))
[[1, 1], [1, 1]]

So far so good, but I tried to break it down a little more:

>>> [1].__imul__(1), [1].__imul__(2), [1].__imul__(3)
([1], [1, 1], [1, 1, 1])

Hmm.  Wasn't what I was expecting.

Then it hit me:

>>> L = [1]
>>> L.__imul__(1), L.__imul__(2), L.__imul__(3)
([1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1])

Pretty sneaky, sis.

Cheers,
Cliff

P.S.  Regards to those who lack a grounding in American pop-culture, or who are 
to young to remember the origins of "Pretty sneaky, sis."
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Built-in functions and keyword arguments

2007-10-29 Thread J. Clifford Dyer
On Mon, Oct 29, 2007 at 06:45:22PM +, Duncan Booth wrote regarding Re: 
Built-in functions and keyword arguments:
> 
> "J. Clifford Dyer" <[EMAIL PROTECTED]> wrote:
> 
> >> I think you are being a little bit unfair here: help(len) says:
> >> 
> >> len(...)
> >> len(object) -> integer
> >> 
> >> Return the number of items of a sequence or mapping.
> >> 
> >> which implies that the argument to len has the name 'object'
> >> (although in fact it doesn't have a name). The OP was simply asking
> >> about the difference in calling conventions, not proposing to write
> >> code using 'object' as the argument name.
> > 
> > Hmm  To my mind, that just implies that the *type* of the expected
> > input is an object.  Just like the "-> integer" tells you that the
> > type of the output is an integer.  If the documentation read
> > "len(s=object) -> integer", then I would expect a keyword argument s
> > typed as an object. 
> > 
> 
> How do you interpret:
> 
> >>> help(__import__)
> Help on built-in function __import__ in module __builtin__:
> 
> __import__(...)
> __import__(name, globals={}, locals={}, fromlist=[], level=-1) -> 
> module
> ...
> >>> help(int)
> Help on class int in module __builtin__:
> 
> class int(object)
>  |  int(x[, base]) -> integer
> ...
> 
> Can you find any case (other than a single parameter identified as 
> 'object') where you can interpret the help string as telling you the types 
> of the parameters?

OK, good point.  Perhaps it's not so specific as the type, but certainly the 
use of name and x in the docstrings listed above only imply something about the 
character of the argument, not the name of the argument itself, which is what I 
was trying to get at.  Help documentation for keyword arguments usually shows 
the argument being used as a keyword, like the example from __import__ above.

Thanks for the correction.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Built-in functions and keyword arguments

2007-10-29 Thread J. Clifford Dyer
On Mon, Oct 29, 2007 at 02:27:50PM +, Duncan Booth wrote regarding Re: 
Built-in functions and keyword arguments:
> 
> Bruno Desthuilliers <[EMAIL PROTECTED]> 
> wrote:
> 
> > In the second case, the name of the argument *is* 'object'. Which is not 
> > the case for the builtin len (which, fwiw, has type 
> > 'builtin_function_or_method', not 'function', so inspect.getargspec 
> > couldn't tell me more).
> > 
> >
> > While we're at it, you should avoid using builtin's names for 
> > identifiers - here, using 'object' as the arg name shadows the builtin 
> > 'object' class).
> >
> 
> I think you are being a little bit unfair here: help(len) says:
> 
> len(...)
> len(object) -> integer
> 
> Return the number of items of a sequence or mapping.
> 
> which implies that the argument to len has the name 'object' (although in 
> fact it doesn't have a name). The OP was simply asking about the difference 
> in calling conventions, not proposing to write code using 'object' as the 
> argument name.

Hmm  To my mind, that just implies that the *type* of the expected input is 
an object.  Just like the "-> integer" tells you that the type of the output is 
an integer.  If the documentation read "len(s=object) -> integer", then I would 
expect a keyword argument s typed as an object.

Now that might also be misleading, as not all objects have a length (integers, 
for example raise a TypeError).  But the documentation has to say something, 
which doesn't imply that every argument has to be a keyword.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: two files into an alternate list

2007-10-29 Thread J. Clifford Dyer
That depends:

What do you want when you have these two files:

file 1:
a
b
c

file 2:
1
2
3
4
5

Options: 
*['a',1,'b',2,'c',3,None,4,None,5]
*['a',1,'b',2,'c',3,4,5]
*['a',1,'b',2,'c',3]
*Throw an exception

And what if file 1 has more lines than file 2?

Cheers,
Cliff
1
On Mon, Oct 29, 2007 at 09:50:51PM +0530, Beema shafreen wrote regarding two 
files into an alternate list:
> Delivered-To: [EMAIL PROTECTED]
> Date: Mon, 29 Oct 2007 21:50:51 +0530
> From: "Beema shafreen" <[EMAIL PROTECTED]>
> To: python-list@python.org
> Subject: two files into an alternate list
> Precedence: list
> List-Id: General discussion list for the Python programming language
>   
> List-Unsubscribe: ,
>   
> List-Archive: 
> List-Post: 
> List-Help: 
> List-Subscribe: ,
>   
> Errors-To: [EMAIL PROTECTED]
> 
>hi everybody ,
>i have a file :
> file 1:
>1
>2
>3
>4
>5
>6
>file2:
>a
>b
>c
>d
>e
>f
>how do i make the two files into  list like this =
>[1,a,2,b,3,c,4,d,5,e,6,f]
>regards
>shafreen

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


Re: two file into a single file

2007-10-29 Thread J. Clifford Dyer
You will need to use the open() builtin for each input file, and again for the 
output file.  Documentation is available in the python tutorial here: 

http://docs.python.org/tut/node9.html#SECTION00920

You should read also the whole tutorial, and work with it until you understand 
it, but for now, that section should get you going.

Cheers,
Cliff


On Mon, Oct 29, 2007 at 09:02:30PM +0530, Beema shafreen wrote regarding two 
file into a single file:
> Delivered-To: [EMAIL PROTECTED]
> Date: Mon, 29 Oct 2007 21:02:30 +0530
> From: "Beema shafreen" <[EMAIL PROTECTED]>
> To: python-list@python.org
> Subject: two file into a single file
> Precedence: list
> List-Id: General discussion list for the Python programming language
>   
> List-Unsubscribe: ,
>   
> List-Archive: 
> List-Post: 
> List-Help: 
> List-Subscribe: ,
>   
> Errors-To: [EMAIL PROTECTED]
> 
>hi everybody,
>   I have a two file,
> file 1:
>17097
>17186
>1723
>17895
>17906
>18295
>18311
>1880
>19160
>19629
>file 2:
>17097
>17186
>1723
>17895
>17906
>18295
>18311
>1880
>19160
>19629
>how do i make into a single file..like this
>file 1 file 2
>17097  17097
>17186  17097
>17186   1880
>172317895
>17895  17895
>17906  17895
>18295  8311
>18311  188

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


Re: tuples within tuples

2007-10-26 Thread J. Clifford Dyer
On Fri, Oct 26, 2007 at 06:59:51AM -0700, [EMAIL PROTECTED] wrote regarding Re: 
tuples within tuples:
> 
> > Resolve *what*?  The problem isn't clear yet; at least to me.  Above you
> > say what you get.  What exactly do you want?  Examples please.
> >
> 
> 
> Sorry for my poor english, but I meant: how can I obtain a list of A
> and C starting from something like this?
> 
> (A,B,C,D)
> that could be
> ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)
> but also
> ('tagA', None, description, None)
> when I don't know if C is a tuple or not?
> 
> I guess that, at least,  within the cicle I may test if C is a tuple
> or not.. And then apply the same cicle for C... and so on
> 
> Am i right?
> 
> 
> ciao
> korovev

So, to clarify the piece that you still haven't explicitly said, you want to 
keep the tag name and children of every element in your XML document.  (which 
is different from keeping (A, C) from tuple (A, B, C, D), because you want to 
modify C as well, and it's decendants.)

As a first solution, how about: 

def reduceXML(node):
if type(node) == str:
return node
else: 
return (node[0], reduceXML(node[2])

N.B. Beware of stack limitations.

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


Re: Better writing in python

2007-10-24 Thread J. Clifford Dyer
On Wed, Oct 24, 2007 at 12:09:40PM -, Alexandre Badez wrote regarding 
Better writing in python:
> 
> lMandatory = []
> lOptional = []
> for arg in cls.dArguments:
>   if arg is True:
> lMandatory.append(arg)
>   else:
> lOptional.append(arg)
> return (lMandatory, lOptional)
> 
> I think there is a better way, but I can't see how...
> 

I assume cls.dArguments is a dict, correct?

`for arg in cls.dArguments` takes each *key* for cls.dArguments and assigns it 
to arg, so the line 'if arg is True' will test the truth value of each key.  I 
assume (you haven't shown the details here) that your dict looks like this:

cls.dArguments = { 'a': True, 'b': False, 'c': True, 'd': False, '': True, 0: 
False }

and you want your for loop to do this:

lMandatory == [ 'a', 'c', '' ]
lOptional == [ 'b', 'd', 0 ]

in fact, since it's testing the truth value of the keys, not the values, you 
will get this:

lMandatory == [ 'a', 'b', 'c', 'd' ]
lOptional == [ '', 0 ]

In no particular order, of course.

Also, `if x is True:` should be written as `if x:`

Actually, come to think of it, what I said above is false, because the string 
'a' *is not* the boolean value True, per se: it is *equal to* True.  So if you 
change `if x is True:` to `if x == True:`  then everything I said above holds 
true, including that you should change `if x == True:` to `if x:`

As it stands, the only key in your dict that has a snowball's chance in key 
largo of being marked as mandatory is the boolean value True itself.

Cheers,
Cliff

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


Re: New module for method level access modifiers

2007-10-23 Thread J. Clifford Dyer
On Tue, Oct 23, 2007 at 08:54:52PM +0200, Bruno Desthuilliers wrote regarding 
Re: New module for method level access modifiers:
> 
> TimeHorse a ?crit :
> > I have started work on a new module that would allow the decoration of
> > class methods to restrict access based on calling context.
> > Specifically, I have created 3 decorators named public, private and
> > protected.
> 
> Lord have mercy.
> 

I invented a new decorator too.  It gets rid of many of the limitations of 
python, including duck typing and hideously flat namespaces.  It's used kind of 
like this:

@java
def public_com.sun.lord.have.mercy():
pass

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


Re: How can i protect text format ?

2007-10-23 Thread J. Clifford Dyer
On Tue, Oct 23, 2007 at 06:30:18AM -0700, Abandoned wrote regarding How can i 
protect text format ?:
> 
> Hi..
> I want to do a forum with python but i have a problem..
> 
> I have a  and i write:
> line 1 hi
> line 2 how r u
> 
> And then i save to this database ( colomn data type is text)
> And than it looks "line 1 hi line 2 how r u"..
> How can i protect \n characters ?
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Are you sure they're getting lost on the way into the DB?  Could they be 
getting lost on the way out?  Say... into an HTML page?

You need to pinpoint your problem more clearly to get a helpful response.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Appending a list's elements to another list using a list comprehension

2007-10-18 Thread J. Clifford Dyer
On Thu, Oct 18, 2007 at 11:57:10AM -, Paul Hankin wrote regarding Re: 
Appending a list's elements to another list using a list comprehension:
> 
> Not to me: I can never remember which of a.append and a.extend is
> which. Falling back to a = a + b is exactly what you want. For
> instance:
> 
> a = (1, 2, 3)
> a += (4, 5, 6)
> 
> works, whereas:
> 
> a = (1, 2, 3)
> a.extend((4, 5, 6))
> 
> doesn't. So using += makes your code more general. There's no standard
> sequence type that has extend and not +=, so worrying that += is
> slower isn't a concern unless you're using a user-defined class. Even
> then, it's probably a mistake that should be fixed in the class rather
> than requiring .extend() to be used instead of +=.
> 

I was going to argue that in fact += is not more general, it just covers a 
different set of use cases, but then I tested my hypothesis...

>>> a = [1,2,3]
>>> b = a
>>> c = [4,5,6]
>>> d = c
>>> e = [7,8,9]
>>> a.extend(e)
>>> b
[1, 2, 3, 7, 8, 9]
>>> c += e
>>> d # I expected [4, 5, 6]
[4, 5, 6, 7, 8, 9]
>>> c = c + e # But += doesn't do the same as this
>>> c
[4, 5, 6, 7, 8, 9, 7, 8, 9]
>>> d
[4, 5, 6, 7, 8, 9]

So I learned something new.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with MySQL cursor

2007-10-11 Thread J. Clifford Dyer
On Thu, Oct 11, 2007 at 03:14:30PM +0200, Florian Lindner wrote regarding 
Problem with MySQL cursor:
> 
> Traceback (most recent call last):
>   File "manage.py", line 90, in ?
> addDomain(domainName)
>   File "manage.py", line 27, in addDomain
> executeSQL(sql,  DOMAIN_TABLE, DOMAIN_FIELD, domainname)
>   File "manage.py", line 22, in executeSQL
> cursor.execute(sql, args)
>   File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 163, in
> execute
> self.errorhandler(self, exc, value)
>   File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 35,
> in defaulterrorhandler
> raise errorclass, errorvalue
> _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL
> syntax; check the manual that corresponds to your MySQL server version for
> the right syntax to use near ''domains' ('domain') VALUES ('xgm.de')' at
> line 1")
> 
> I see the error: 2 opening quotes but only 1 closing around domains. But
> where do they come from?
> 
> Note that there are no quotes at print sql % args.
> 

No, there are no double quote issues.  The first quote is provided by the error 
message, and is paired with the quote after the closed parenthesis in 
('xgm.de')'.  It is not part of your SQL.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pytz has so many timezones!

2007-10-09 Thread J. Clifford Dyer
On Tue, Oct 09, 2007 at 11:21:41AM -0700, [EMAIL PROTECTED] wrote regarding Re: 
pytz has so many timezones!:
> 
> The Earth says. It takes 24 hours to revolve.
> 
> > Why aren't they separated by 30minutes, or 20, or 10? Or 2 hours?
> 
> Why isn't an hour defined to be 30 minutes?
> 
> > Or why don't we have a global time?
> 
> Like UTC?
> 
> >
> > Your 25 timezones are an abstraction the same way
> 
> Not the same way at all. The 25 timezones I speak of are
> not merely an abstraction, but related to longitude.
> 
> > as are the 400 apparently in use by people all over the world
> 
> Where the correlation to longitude is much looser.
> Granted, it doesn't need to be for non-navigational
> purposes. And although governments can legislate things
> like DST, they can't legislate longitude.
> 
> > - and last time I checked, there was no
> > fundamental law in physics or such that limited the allowed or sensible
> > number of timezones...
> 
> Isn't there some law somewhere that says the circumference
> of a sphere is 360deg? Doesn't that same law mean that no two
> points on a sphere can be seperated by more than 180deg
> longitude? Doesn't that make GMT+13 non-sensible?
> 

You seem to be talking about time zones as if they are a scientific abstraction 
based on the physical layout of the earth.  They are not.  They are an 
abstraction away from true scientific (solar) time to give us regular 24 hour 
days, and to simplify calculation to make sure that trains don't run into one 
another for having left their respective stations at times based on locally 
defined solar noon.  Solar time is the only kind of time that doesn't have to 
take political considerations into account.  

GMT+13 is not non-sensible at all, if the major trade partners of the island in 
question are at GMT+12.  Imagine the confusion not being able to schedule 
meetings on monday or friday because your next door neighbor, one time zone 
away, is actually off-calendar from you by one day.  The IDL was arbitrarily 
placed in the middle of the pacific to limit this problem to as few people as 
possible, but the people of Kiribati have no reason to accept the disadvantage 
under which this (European) abstraction places them.  What would be 
non-sensible is for them to live 23 hours offset from their closest neighbors 
and family, while living a mere three hours offset from people that they have 
minimal contact with.  

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why did MIT drop scheme for python in intro to computing?

2007-10-09 Thread J. Clifford Dyer
On Tue, Oct 09, 2007 at 06:28:00AM -0500, Harold Ancell wrote regarding Re: why 
did MIT drop scheme for python in intro to computing?:
> 
> On Tue, 09 Oct 2007 03:28:53 -, [EMAIL PROTECTED] wrote:
> 
> >On Oct 8, 1:23 pm, [EMAIL PROTECTED] (Brian Harvey) wrote:
> 
> >> "Kjetil S. Matheussen" <[EMAIL PROTECTED]> writes:
> 
> >> >I don't think your speculations makes very much sence.
> 
> >> Amen.
> 
> >> And, in any case, there's no need to speculate.
> >> MIT has published, on their web site, pages and
> >> pages of rationale for the new curriculum.
> 
> >> The most important point, imho, is that the
> >> programming language was the /least/ important
> >> aspect of the decision.  The most important
> >> aspect was the move to an application-based
> >> (rather than topic-based) organization of the
> >> curriculum.  The details flow out of that big
> >> shift of focus.
> 
> >[ much snipped. ]
> 
> >Does scheme have a gui library?
> 
> >I really dont follow the logic.
> 
> I really REALLY hope that not a single GUI is
> constructed in 6.01-2; adding that to the load
> would be stark raving mad (look and you'll agree).
> 
> As Brian points out, languages are a means to the
> end of teaching stuff, and I wouldn't be surprised
> if not a single GUI is constructed in the entire
> required/restricted elective curriculum.  That's
> just not to the point of an EECS education that
> has to be squeezed into 4/5 years (most students
> take the combined MEng path, where the MS degree
> is terminal and leads straight to industry).
> 
> If any library was a consideration in choosing
> Python, it was the robots one for 6.01.  Note also
> that Hal helped design and teach 6.01, and fully
> supports the new curriculum.
> 
> As a total LISP/Scheme fanatic who finds parts of
> Python's syntax to be too hard for his brain (not
> the indentation, that's weird but useful and cool,
> much like S-expressions in LISP), I looked hard at
> the beginning of 6.01 where they're only teaching
> SICP.
> 
> For that purpose, Python is not "awful" (remember,
> I believe LISP is the One True Way of Computing).
> For that initial bit of SICP material, I do not
> believe the students will be handicapped.
> 
> Beyond that initial bit of material, I have no
> informed opinions.
> 
>   - Harold
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

I, for one, (coming from the Python side) would be thrilled to see a rigorous 
SICP-like book published using Python as its basis.  But maybe with the new 
change of focus to application based, that won't be forthcoming.  We'll see.

Cheers,
Cliff


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


Re: pytz has so many timezones!

2007-10-08 Thread J. Clifford Dyer
On Mon, Oct 08, 2007 at 01:13:24PM -0700, [EMAIL PROTECTED] wrote regarding Re: 
pytz has so many timezones!:
> 
> On Oct 8, 1:03 pm, Carsten Haese <[EMAIL PROTECTED]> wrote:
> > On Mon, 2007-10-08 at 10:41 -0700, [EMAIL PROTECTED] wrote:
> > > For example, Windows has seperate listings for
> >
> > > Central America
> > > Central Time (US & Canada)
> > > Guadalahara, Mexico City, Monterry - New
> > > Guadalahara, Mexico City, Monterry - Old
> > > Saskatchewan
> >
> > > but they are all GMT-6
> >
> > But they could have different rules for Daylight Saving Time.
> 
> Which only matters if you're setting your clock.
> 

Maybe this is where I'm not understanding you:  Do you have another use for 
setting a timezone?  The only thing a time zone does, as far as I can tell, is 
set clocks relative to a shared conception of time.


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


Re: pytz has so many timezones!

2007-10-08 Thread J. Clifford Dyer
On Mon, Oct 08, 2007 at 01:12:32PM -0700, [EMAIL PROTECTED] wrote regarding Re: 
pytz has so many timezones!:
>  [ I wrote ]
> > Reducing them to a single time zone will result in aberrant functionality 
> > in one or more locales.
> 
> I would hardly think that's an issue on the user registration
> form the OP is trying to create.
> 

You don't think that it's an issue that the OPs users will complain about their 
time being inaccurate?  If I register in what we in the US call Eastern Time 
Zone, I don't want to have to switch to Atlantic time zone just because the 
form designer couldn't be bothered to include a time zone that actually matched 
the function of the clocks in my area.  

Pedantry about the definition of a time zone is not going to win you points 
with irate users.  

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


Re: pytz has so many timezones!

2007-10-08 Thread J. Clifford Dyer
On Mon, Oct 08, 2007 at 10:41:03AM -0700, [EMAIL PROTECTED] wrote regarding Re: 
pytz has so many timezones!:
> 
> On Oct 8, 2:32 am, Sanjay <[EMAIL PROTECTED]> wrote:
> > Hi All,
> >
> > I am using pytz.common_timezones to populate the timezone combo box of
> > some user registration form. But as it has so many timezones (around
> > 400),
> 
> There are only 25 timezones: -12, -11, ... -1, 0 (GMT), +1, ... +11,
> +12.
> 
> A handful of countries set their clocks offset by a half hour,
> but those aren't timezones.
> 

I'm sorry.  By what even vaguely useful definition of "Timezone" is it not a 
timezone if it's offset by half an hour?  

> 
> The 400 you're seeing are duplications based on locality. Of the 86
> shown in Windows, all but 33 are dulplicate references to the same
> timezones.
> 
> For example, Windows has seperate listings for
> 
> Central America
> Central Time (US & Canada)
> Guadalahara, Mexico City, Monterry - New
> Guadalahara, Mexico City, Monterry - Old
> Saskatchewan
> 
> but they are all GMT-6
> 

Those are non-duplicate (and perhaps inaccurate, I'm not sure).  US time 
switches from standard to Daylight Savings earlier than Mexico, and switches 
back later, as of this year.  Reducing them to a single time zone will result 
in aberrant functionality in one or more locales.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Magazine: Issue 1 Free!

2007-10-05 Thread J. Clifford Dyer
On Fri, Oct 05, 2007 at 04:11:07PM -, Grant Edwards wrote regarding Re: 
Python Magazine: Issue 1 Free!:
> 
> On 2007-10-05, Steve Holden <[EMAIL PROTECTED]> wrote:
> >>
> >>> I've just been told by the editors at Python Magazine that the
> >>> first issue is out.
> >> 
> >> The first issue is issue number 10?
> >> 
> >> That's a bit silly.
> >
> > It's the October edition. They obviously decided to make sure
> > the month numbering was consistent across the volumes.
> 
> I presumed that was the reasoning. It just seems
> counter-intuitive (and sort of un-Pythonic) to start numbering
> a sequence of objects at 10. ;)
> 

Well, it's also unpythonic to start numbering a sequence at 1, but it's clearly 
the right thing to do in this case.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: remove list elements..

2007-10-05 Thread J. Clifford Dyer
On Fri, Oct 05, 2007 at 07:27:39AM -0700, Abandoned wrote regarding remove list 
elements..:
> 
> Hi..
> I have a problem..
> list1=[11, 223, 334, 4223...] 1 million element
> list2=[22,223,4223,2355...] 500.000 element
> 
> I want to difference list1 to list2 but order very importent..
> 
> My result must be:
> list3=[11,334,...]
> 
> I do this use FOR easly but the speed very imported for me. I want to
> the fastest method please help me.
> 
> I'm sorry my bad english.
> 
> King regards..
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

If you're using a recent python, you can create a set from list2, and preserve 
order with list1 like so:

set2 = set(list2)
list3 = [ x for x in list1 if x not in set2 ]

That will speed up your access to list2 (for 500,000 scans) at the low cost of 
reading through the whole thing once.

You say order is very important, but if your order is known to be strictly 
increasing (as it appears from your sample code), you can speed up further by 
doing:

set1 = set(list1)
set2 = set(list2)
list3 = sorted(set1 - set2)


Though to tell the truth, I'm not sure this actually would be faster, since you 
access each element of list1 just once in the list comprehension version.  And 
I can't be bothered to try it out with timeit.

Cheers,
Cliff

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


  1   2   >