Re: How did you learn Python?

2004-12-03 Thread Miles
Eric Pederson wrote:
I think you might appreciate "Learning Python" as it's written very succinctly, but it goes through the language features pretty comprehensively.
I second the recommendation of "Learning Python" - it's an excellent book.  
I read
it cover-to-cover in about a week, stopping to do each of the exercises along 
the
way, and I'm now comfortable and confident coding programs that I previously 
would
have written in Perl.  I had no Python experience at all when I started, but do
have a programming background (mainly Perl and PL/SQL), and found the book to
be very well-paced.
Miles
--
http://mail.python.org/mailman/listinfo/python-list


Re: a cx_Oracle ORA-01036 problem

2005-05-06 Thread Miles
Damjan wrote:
> I'm using Python 2.4, cx_Oracle-4.1 on Linux with Oracle instant client
> 10.1.0.3. This is the sql string:
> 
> SQL = """insert into D.D_NOTIFY values (:CARDREF, :BANKKEY, :OK1, :OK2 \
> :DEBTEUR, :DEBTDEN, to_date(:INVOICE_DATE,'DD.MM.YY'),
> to_date(:PAYMENT_DEADLINE,'DD.MM.YY'), :POINTS)"""
> 
> And I'm trying to execute it as:
> c = db.cursor()
> c.execute(SQL, CARDREF=id, BANKKEY=dc_kluc, OK1=okd, OK2=okc, 
>   
>   DEBTEUR=iznos_eur, DEBTDEN=iznos_mkd, INVOICE_DATE=datum_g, 
>   
>   PAYMENT_DEADLINE=datum_d, POINTS=bodovi)
> 
> And I get an ORA-01036 exception.

Try using a variable name other than "id" for the CARDREF variable... say
"card_id".  id is a built in function name; I suspect your problem may be
that you are assiging that function to the variable rather than your intended
value...


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


Re: "assert" annoyance

2007-06-21 Thread Miles
On Jun 22, 1:31 am, Paul Rubin  wrote:
> What I really want is for any assertion failure, anywhere in the
> program, to trap to the debugger WITHOUT blowing out of the scope
> where the failure happened, so I can examine the local frame.  That
> just seems natural, but I don't see an obvious way to do it.

You could run the entire program through pdb:

#!/usr/bin/env python -m pdb

print "Hello!"
assert False
print "...world!"


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


Re: "assert" annoyance

2007-06-22 Thread Miles
On Jun 22, 2:45 am, "Evan Klitzke" <[EMAIL PROTECTED]> wrote:
> On 6/21/07, Miles <[EMAIL PROTECTED]> wrote:
>
> > On Jun 22, 1:31 am, Paul Rubin <http://[EMAIL PROTECTED]> wrote:
> > > What I really want is for any assertion failure, anywhere in the
> > > program, to trap to the debugger WITHOUT blowing out of the scope
> > > where the failure happened, so I can examine the local frame.  That
> > > just seems natural, but I don't see an obvious way to do it.
>
> > You could run the entire program through pdb:
> > 
> > #!/usr/bin/env python -m pdb
>
> > print "Hello!"
> > assert False
> > print "...world!"
> > 
>
> You can only pass one argument to a command that you invoke with the
> shebang sequence, so this won't work the way you wrote it.
>
> --
> Evan Klitzke <[EMAIL PROTECTED]>

It actually does work on my system (OS X); I didn't realize it wasn't
portable.

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


Re: "Empty" text

2007-07-09 Thread Miles
On Jul 9, 4:30 am, Nick Craig-Wood <[EMAIL PROTECTED]> wrote:
> Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> >  On Sun, 08 Jul 2007 22:23:20 +0200, Jan Danielsson wrote:
> > >Firefox is very unhappy about the textarea not having separate
> > > opening and a closing tags. i.e. I need this:
> >
> >  Then either Firefox is broken or you don't declare your XHTML properly and
> >  Firefox thinks it's HTML.
>
> I suspect the former - we noticed exactly the same thing (can't
> remember which tags we were having problems with), using the
> declaration :-
>
>   
>  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
>
> I haven't tested this again recently though.

Firefox is not broken.  The XML prolog and DOCTYPE do not affect HTML
vs. XHTML interpretation--only the Content-Type does.  If you are
serving a page as "text/html", which you almost certainly are, then
all browsers will parse it as HTML, regardless of prolog and doctype,
and HTML does not support self-closing tags.  You need to serve the
page as "application/xhtml+xml", or avoid self-closing tags on
elements other than , , , etc.

http://www.w3.org/TR/xhtml1/#C_3

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


Re: bool behavior in Python 3000?

2007-07-11 Thread Miles
On Jul 11, 2:50 am, Alan Isaac <[EMAIL PROTECTED]> wrote:
> >>> bool(False-True)
>
> True

What boolean operation does '-' represent?

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


Re: Where does str class represent its data?

2007-07-11 Thread Miles
On Jul 11, 7:21 pm, [EMAIL PROTECTED] wrote:
> I'd like to implement a subclass of string that works like this:
>
> >>>m = MyString('mail')
> >>>m == 'fail'
> True
> >>>m == 'mail'
> False
> >>>m in ['fail', hail']
>
> True
>
> My best attempt for something like this is:
>
> class MyString(str):
>   def __init__(self, seq):
> if self == self.clean(seq): pass
> else: self = MyString(self.clean(seq))
>
>   def clean(self, seq):
> seq = seq.replace("m", "f")
>
> but this doesn't work.  Nothing gets changed.
>
> I understand that I could just remove the clean function from the
> class and call it every time, but I use this class in several
> locations, and I think it would be much safer to have it do the
> cleaning itself.

Since strings are immutable, you need to override the __new__ method.
See http://www.python.org/download/releases/2.2.3/descrintro/#__new__

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


Re: bool behavior in Python 3000?

2007-07-12 Thread Miles
On Jul 12, 8:37 pm, Alan Isaac <[EMAIL PROTECTED]> wrote:
> I do not like that bool(False-True) is True.

I've never seen the "A-B" used to represent "A and not B", nor have I
seen any other operator used for that purpose in boolean algebra,
though my experience is limited.  Where have you seen it used?

What's wrong with 'and', 'or', and 'not'?  I think that redefining *,
+, and - to return booleans would only encourage programmers to use
them as shortcuts for standard boolean operations--I'd hate to see
code like this:
>>> if user.registered * (user.age > 13) - user.banned: ...

I don't mind that arithmatic operations are _possible_ with bools, but
I would strongly prefer to see the boolean keywords used for
operations on booleans.

-Miles

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


Re: Fetching a clean copy of a changing web page

2007-07-15 Thread Miles
On Jul 16, 1:00 am, John Nagle <[EMAIL PROTECTED]> wrote:
> I'm reading the PhishTank XML file of active phishing sites,
> at "http://data.phishtank.com/data/online-valid/";  This changes
> frequently, and it's big (about 10MB right now) and on a busy server.
> So once in a while I get a bogus copy of the file because the file
> was rewritten while being sent by the server.
>
> Any good way to deal with this, short of reading it twice
> and comparing?
>
> John Nagle

Sounds like that's the host's problem--they should be using atomic
writes, which is usally done be renaming the new file on top of the
old one.  How "bogus" are the bad files?  If it's just incomplete,
then since it's XML, it'll be missing the "" and you should
get a parse error if you're using a suitable strict parser.  If it's
mixed old data and new data, but still manages to be well-formed XML,
then yes, you'll probably have to read it twice.

-Miles

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


Re: In a dynamic language, why % operator asks user for type info?

2007-07-16 Thread Miles
On Jul 16, 8:10 pm, Karthik Gurusamy <[EMAIL PROTECTED]> wrote:
> Since % by definition is string formatting, the operator should be
> able to infer how to convert each of the argument into strings.

In addition to what Dan mentioned, you can use "%s" with any object to
perform an automatic string conversion.

>>> '%s %s %s %s' % ('Hello!', 3.14, 42+1j, object())
'Hello! 3.14 (42+1j) '

-Miles

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


Re: How to check if an item exist in a nested list

2007-07-19 Thread Miles
Arash Arfaee wrote:
> is there any way to check if "IndexError: list index out of
> range" happened or going to happen and stop program from terminating?

Use a try/except block to catch the IndexError
http://docs.python.org/tut/node10.html#SECTION001030

try:
do_something_0(M_list[line][row][d])
except IndexError:
do_something_1()

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


Re: Sorting dict keys

2007-07-20 Thread Miles
On 7/20/07, Alex Popescu <[EMAIL PROTECTED]> wrote:
> If you just want to iterate over your dict in an ordered manner than all
> you have to do is:
>
> for k in my_dict.keys().sort():
>   # rest of the code

I think you meant sorted(my_dict.keys()), since, as you just pointed
out, the sort() method returns None.

> If you just want to keep a list of ordered keys you can probably do
> something like:
>
> key_list = list(my_dict.keys())
> key_list.sort()

Creating a copy with list() is unnecessary, as keys() already returns a copy.

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


Re: Lazy "for line in f" ?

2007-07-22 Thread Miles
On 7/22/07, Alexandre Ferrieux  wrote:
> The Tutorial says about the "for line in f" idiom that it is "space-
> efficient".
> Short of further explanation, I interpret this as "doesn't read the
> whole file before spitting out lines".
> In other words, I would say "lazy". Which would be a Good Thing, a
> much nicer idiom than the usual while loop calling readline()...
>
> But when I use it on the standard input, be it the tty or a pipe, it
> seems to wait for EOF before yielding the first line.

It doesn't read the entire file, but it does use internal buffering
for performance.  On my system, it waits until it gets about 8K of
input before it yields anything.  If you need each line as it's
entered at a terminal, you're back to the while/readline (or
raw_input) loop.

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


Re: URL parsing for the hard cases

2007-07-22 Thread Miles
On 7/22/07, John Nagle wrote:
> Is there something available that will parse the "netloc" field as
> returned by URLparse, including all the hard cases?  The "netloc" field
> can potentially contain a port number and a numeric IP address.  The
> IP address may take many forms, including an IPv6 address.

What do you mean by "parse" the field?  What do you want to get back
from the parser function?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: URL parsing for the hard cases

2007-07-22 Thread Miles
On 7/23/07, John Nagle wrote:
> Here's another hard case.  This one might be a bug in urlparse:
>
> import urlparse
>
> s = 'ftp://administrator:[EMAIL PROTECTED]/originals/6 june
> 07/ebay/login/ebayisapi.html'
>
> urlparse.urlparse(s)
>
> yields:
>
> (u'ftp', u'administrator:[EMAIL PROTECTED]', u'/originals/6 june
> 07/ebay/login/ebayisapi.html', '', '', '')
>
> That second field is supposed to be the "hostport" (per the RFC usage
> of the term; Python uses the term "netloc"), and the username/password
> should have been parsed and moved to the "username" and "password" fields
> of the object. So it looks like urlparse doesn't really understand FTP URLs.

Those values aren't "moved" to the fields; they're extracted on the
fly from the netloc.  Use the .hostname property of the result tuple
to get just the hostname.

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


Re: URL parsing for the hard cases

2007-07-23 Thread Miles
On 7/22/07, John Nagle wrote:
> Is there any library function that correctly tests for an IP address vs. a
> domain name based on syntax, i.e. without looking it up in DNS?

import re, string

NETLOC_RE = re.compile(r'''^ #start of string
(?:([EMAIL PROTECTED])+@)?# 1:
(?:\[([0-9a-fA-F:]+)\]|  # 2: IPv6 addr
([^\[\]:]+)) # 3: IPv4 addr or reg-name
(?::(\d+))?  # 4: optional port
$''', re.VERBOSE)#end of string

def normalize_IPv4(netloc):
try: # Assume it's an IP; if it's not, catch the error and return None
host = NETLOC_RE.match(netloc).group(3)
octets = [string.atoi(o, 0) for o in host.split('.')]
assert len(octets) <= 4
for i in range(len(octets), 4):
octets[i-1:] = divmod(octets[i-1], 256**(4-i))
for o in octets: assert o < 256
host = '.'.join(str(o) for o in octets)
except (AssertionError, ValueError, AttributeError): return None
return host


def is_ip(netloc):
if normalize_IPv4(netloc) is None:
match = NETLOC_RE.match(netloc)
# IPv6 validation could be stricter
if match and match.group(2): return True
else: return False
return True

The first function, I'd imagine, is the more interesting of the two.

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


Re: Maintaining leading zeros with the lstrip string function?

2007-07-23 Thread Miles
On 7/23/07, Randy Kreuziger <[EMAIL PROTECTED]> wrote:
> I need just the file name from a string containing the path to a file.  The
> name of the file starts with zeros.  This is problematic because the  lstrip
> function strips them leaving this as the result:
> 6128.jpg
>
>
> How do I strip the path without losing the leading zeros in the file name?
>
>  -
> import sys, os, win32com.client, string
>
> teststring = 'C:\shoreline\dvd\prep area\800x\\006128.jpg'
> print string.lstrip(teststring, 'C:\shoreline\dvd\prep area\800x\\')

lstrip removes *any* of the set of characters in the argument, not the
exact string[1].  Use of the string module for lstrip and other
functions[2] has been deprecated for using the string methods
directly, i.e. teststring.lstrip(...).

You should probably be using split (or basename) in the os.path
module.  Also, use raw strings ( r'\path\to\file' ) to avoid problems
with backslashes being interpreted in strings.

-Miles

[1] http://docs.python.org/lib/string-methods.html
[2] http://docs.python.org/lib/node42.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: URL parsing for the hard cases

2007-07-23 Thread Miles
On 7/23/07, Miles wrote:
> On 7/22/07, John Nagle wrote:
> > Is there any library function that correctly tests for an IP address vs. a
> > domain name based on syntax, i.e. without looking it up in DNS?
>
> import re, string
>
> NETLOC_RE = re.compile(r'''^ #start of string
> (?:([EMAIL PROTECTED])+@)?# 1:
> (?:\[([0-9a-fA-F:]+)\]|  # 2: IPv6 addr
> ([^\[\]:]+)) # 3: IPv4 addr or reg-name
> (?::(\d+))?  # 4: optional port
> $''', re.VERBOSE)#end of string
>
> def normalize_IPv4(netloc):
> try: # Assume it's an IP; if it's not, catch the error and return None
> host = NETLOC_RE.match(netloc).group(3)
> octets = [string.atoi(o, 0) for o in host.split('.')]
> assert len(octets) <= 4
> for i in range(len(octets), 4):
> octets[i-1:] = divmod(octets[i-1], 256**(4-i))
> for o in octets: assert o < 256
> host = '.'.join(str(o) for o in octets)
> except (AssertionError, ValueError, AttributeError): return None
> return host

Apparently this will generally work as well:

import re, socket

NETLOC_RE = ...

def normalize_IPv4(netloc):
try:
host = NETLOC_RE.match(netloc).group(3)
return socket.inet_ntoa(socket.inet_aton(host))
except (AttributeError, socket.error):
return None

Thanks to http://mail.python.org/pipermail/python-list/2007-July/450317.html

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


Re: datetime.time() class - How to pass it a time string?

2007-07-24 Thread Miles
On 7/24/07, Robert Dailey wrote:
> Hi,
>
> I have a string in the following format:
>
> "00:00:25.886411"
>
> I would like to pass this string into the datetime.time() class and
> have it parse the string and use the values. However, the __init__()
> method only takes integers (which means I'd be forced to parse the
> string myself). Does anyone know of a way I can make it use the
> string? Thanks.

timestr = "00:00:25.886411"
timesep = re.compile('[:.]')
datetime.time(*[int(i) for i in timesep.split(timestr)])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: idiom for RE matching

2007-07-24 Thread Miles
On 7/24/07, Gordon Airporte <[EMAIL PROTECTED]> wrote:
> I did already find that it speeds things up to pre-test a line like
>
> if 'bets' or 'calls' or 'raises' in line:
> run the appropriate re's

Be careful: unless this is just pseudocode, this Python doesn't do
what you think it does; it always runs the regular expressions, so any
speed-up is imaginary.

>>> line = 'eggs'
>>> bool('spam' or 'ham' in line)
True
>>> 'spam' or 'ham' in line  # Equivalent to: 'spam' or ('ham' in line)
'spam'

AFAIK, the (Python 2.5) idiom for what you want is:

>>> any(s in line for s in ('spam', 'ham'))
False
>>> line = 'Spam, spam, spam, spam'
>>> any(s in line for s in ('spam', 'ham'))
True

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


Re: What order does info get returned in by os.listdir()

2007-08-14 Thread Miles
On 8/14/07, Zentrader wrote:
> On Aug 14, 1:52 pm, Jeremy C B Nicoll wrote:
> > When I use os.listdir() to return that list of leaf values, I do seem to get
> > them in alphabetical order, A before B before C etc, but the ~-prefixed ones
> > are returned after the Z-prefixed files rather than before the A-ones.
>
> I think that os.listdir() returns file names in chronological order,
> that is in the order they were created on disk.

http://docs.python.org/lib/os-file-dir.html
The order is arbitrary, and depends on the underlying directory
structure and system calls.  If you rely on the order being sorted,
you should do it yourself.

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


Re: What order does info get returned in by os.listdir()

2007-08-15 Thread Miles
On 8/15/07, Jeremy C B Nicoll wrote:
> Marc 'BlackJack' Rintsch wrote:
> > > How would I sort leaflist in a way that mimics the sort order that XP
> > > shows me things under?
> >
> > This depends on what XP is.  Which program?  Which locale?  How does the
> > locale influence that programs sorting?
>
> Well... XP is Windows XP (Pro as I think I said earlier), and I'm in the UK.
> I explained earlier how XP shows me stuff in order when I tell it to sort by
> name.

Case insensitive sort with ~ coming before all other characters:
some_list.sort(key=lambda k: k.lower().split("~"))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting the result of a process after exec*()

2007-08-17 Thread Miles
On 8/17/07, AndrewTK <[EMAIL PROTECTED]> wrote:
> The problem for me is this: once an external process is called via
> exec*() the script has effectively fulfilled its task. Is there any
> way one can process a file with an external process and continue
> further processing in Python; /once the external processing is
> completed/?

Assuming you're looking at the docs for the os module, instead of the
exec*() functions, check out the spawn*() functions, or, to use a
subshell, system().  Better yet, take a look at the subprocess module:
http://docs.python.org/lib/module-subprocess.html

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


Re: fcntl problems

2007-08-30 Thread Miles
On 8/30/07, mhearne808 wrote:
> I'm having a number of problems with the fcntl module.

Read this first: http://linux.die.net/man/2/flock

> First of all, if I try this:
> file = open("counter.txt","w+")
> fcntl.flock(file.fileno(), fcntl.LOCK_NB)
>
> I get this:
> ---
>Traceback (most recent call
> last)
> /Users/mhearne/src/python/ in ()
> : [Errno 9] Bad file descriptor

That should be:
>>> fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)

> Proceeding forward with the locked file, let's say I do the above in
> Python interactive Process A.  Then in python interactive Process B, I
> repeat the "open" function on the same file with the same
> permissions.  Then, in each process, I write some text to the file
> using the write() method.  After closing the file in both processes,
> the only text I see in the file is from Process B!

This is due to two issues: caching and file position.  When you open
the file in both processes as 'w+', they are both positioned at the
*current* EOF, but from that point on the offset is not externally
influenced.  The correct sequence of events should be:
- open file in mode w+
- obtain exclusive lock
- f.seek(0, 2) # (to end of file)
- write to file
- f.flush() # or f.close()
- release lock

> Is this my lack of understanding, or have I discovered a bug?

If you find yourself asking this question, it's too often the former :)

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


Re: fcntl problems

2007-08-30 Thread Miles
Sorry, that last quote-only reply was accidental. :)

On 8/30/07, mhearne808 wrote:
> I've been doing some experiments, and here are some specific examples
> to try.

[snipped examples]

> From these last two experiments I can only conclude that file locking
> isn't doing a durned thing.
>
> What's going on?

File locking isn't doing a durned thing in those cases because you're
only obtaining the lock from a single process.

> According to my Python Cookbook:
> "Exclusive lock: This denies all _other_ processes both read and write
> access to the file."

This is only for mandatory locking; POSIX flock is advisory locking,
which states: "Only one process may hold an exclusive lock for a given
file at a given time."  Advisory locks don't have any effect on
processes that don't use locks.  Mandatory locks are kernel enforced,
but non-POSIX and not available in Mac OS X.

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


Re: fcntl problems

2007-08-31 Thread Miles
On 8/31/07, mhearne808 wrote:
> I have a script that will be run from a cron job once a minute.  One
> of the things this script will do is open a file to stash some
> temporary results.  I expect that this script will always finish its
> work in less than 15 seconds, but I didn't want to depend on that.
> Thus I started to look into file locking, which I had hoped I could
> use in the following fashion:
>
> Process A opens file foo
> Process A locks file foo
> Process A takes more than a minute to do its work
> Process B wakes up
> Process B determines that file foo is locked
> Process B quits in disgust
> Process A finishes its work

That would look like (untested):

import fcntl, sys
f = open('foo', 'w+')
try:
fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError, e:
if e.args[0] == 35:
sys.exit(1)
else:
raise
f.seek(0, 2) # seek to end
# do your thing with the file
f.flush()
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
f.close()

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


Re: fcntl problems

2007-08-31 Thread Miles
On 8/31/07, mhearne808 wrote:
> Looking at my flock(3) man page, I'm guessing that "35" is the error
> code for EWOULDBLOCK.  Which system header file am I supposed to look
> in to figure that magic number out?

I got the error number by looking at the IOError exception raised when
playing with the interactive interpreter, but I really should have
written:

from errno import EWOULDBLOCK
...
if e.args[0] == EWOULDBLOCK:
...

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


Re: Printing lists in columns

2007-09-04 Thread Miles
On 9/4/07, Hrvoje Niksic wrote:
> Python isn't too happy about adding individual keyword arguments after
> an explicit argument tuple.  Try this instead:
>
> for row in izip_longest(*d, **dict(fillvalue='*')):
>  print ', '.join(row)

Or simply:

for row in izip_longest(fillvalue='*', *d):
print ', '.join(row)

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


Re: sys.argv index out of range error

2007-09-13 Thread Miles
On 9/13/07, Brian McCann wrote:
> bootstrap.sh
> #!/bin/sh
> cd /home/workspaces
> export LANG="en_US.UTF-8"
>
> source init $1
> test.py $2
> #
>
> what's wrong with  the line "source init $1  ?

Assuming your /bin/sh is actually the Bourne-again shell, this excerpt
from the Bash manual explains what's happening ("source" is a synonym
for "."):

"""
. filename [arguments]
Read and execute commands from the filename argument in the current
shell context. [...] If any arguments are supplied, they become the
positional parameters when filename is executed. Otherwise the
positional parameters are unchanged.
"""

So "source [script] [args]" clobbers the parameters of the *current*
shell context.  So any of these should work:
* use "source init.sh" to use the same shell context;
* use "./init.sh $1" to use a new context; or
* save the needed parameters in other variables before clobbering them.

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


Re: Regular Expressions: Can't quite figure this problem out

2007-09-24 Thread Miles
On 9/24/07, Robert Dailey <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm attempting to create a regular expression that removes redundancy in
> empty XML elements. For example:
>
> 
>
> The regular expression would convert the XML above into:
>
>  

If you can guarantee that the XML is well-formed, then this should work:

pattern = r'<([^/>][^>]*(?]+>'
replace = r'<\1/>'

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


Re: Regular Expressions: Can't quite figure this problem out

2007-09-25 Thread Miles
On 9/25/07, Paul McGuire wrote:
> On Sep 24, 11:23 pm, Gabriel Genellina wrote:
> > py> print re.sub(r"<(\w+)([^>]*)>", r"<\1\2 />", source)
>
> And let's hope the OP doesn't have to parse anything truly nasty like:
>
>  esolang:language>

Or something mildly nasty, like 

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


Re: Ternary operator and tuple unpacking -- What am I missing ?

2009-01-12 Thread Miles
On Tue, Jan 13, 2009 at 12:02 AM, imageguy  wrote:
> Using py2.5.4 and entering the following lines in IDLE, I don't really
> understand why I get the result shown in line 8.
>
> Note the difference between lines 7 and 10 is that 'else' clause
> result enclosed in brackets, however, in line 2, both the 'c,d'
> variables are assign correctly without the brackets being required.
>
> 1) >>> n = None
> 2) >>> c,d = n if n is not None else 0,0
> 3) >>> print c,d, type(c), type(d)
> 4) 0 0  

The ternary expression has higher precedence than the comma, so the
actual effect of line 2 (and 8) is:

>>> c, d = (n if n is not None else 0), 0

Or written more explicitly:

>>> c = n if n is not None else 0
>>> d = 0

So the only correct way to write the expression, for the result you
want, is to use your line 10:

> 10)  >>> c,d = n if n is not None else (0,0)

But if you're struggling with the precedence issues, I'd recommend
ditching ternary expressions altogether and using full conditional
blocks.

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


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

2009-01-26 Thread Miles
On Mon, Jan 26, 2009 at 4:06 AM, Diez B. Roggisch wrote:
> There are people who say something along the lines of "be strict when
> writing, and tolerant when reading" (the exact quote is different, but
> neither google:~site:mybrain nor any other have helped me here)

That's Postel's Law:
http://en.wikipedia.org/wiki/Robustness_Principle

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


Re: Rounding to the nearest 5

2009-01-29 Thread Miles
On Thu, Jan 29, 2009 at 7:26 PM, Tim Chase wrote:
>> How can you make python round numbers to the nearest 5:
>>  Example:
>>  3 => 0
>> 8 => 10
>> 23.2 => 20
>> 36 => 35
>> 51.5 => 50
>
> I'm not sure *any* rounding system will give those results.

def bogoround(n):
n1 = n / 5.0
return int(round(n1) if n1 % 2 > 1 else n1) * 5

best-I-could-do-ly y'rs,
-Miles
--
http://mail.python.org/mailman/listinfo/python-list


Re: select.poll.poll() never blocks

2009-02-11 Thread Miles
On Wed, Feb 11, 2009 at 10:47 PM, Rhodri James
 wrote:
> If you want to
> do it in Python, the only thing that springs to mind is
> periodically checking the size of the file and reading more
> when that changes.  You'll need to be very careful to keep
> what size you think the file is in sync with how much you've
> read!

See also this recipe:
http://code.activestate.com/recipes/157035/

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


Re: Problems with OS X 10.5.6 and Python 2.5 and GDAL 1.6

2009-02-17 Thread Miles
On Mon, Feb 16, 2009 at 8:35 PM, Helly John J. wrote:
> However, when I run python and try to import gdal, this is what happens:
> Python 2.5.4 (r254:67917, Dec 23 2008, 14:57:27)
> [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import gdal
> Traceback (most recent call last):
>   File "", line 1, in 
> ImportError: No module named gdal

What about "from osgeo import gdal"?

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


Re: print string as raw string

2009-02-17 Thread Miles
On Tue, Feb 17, 2009 at 2:01 PM, Scott David Daniels wrote:
>>> Mirko Dziadzka schrieb:
>>>>
>>>> I'm trying to find a way to output strings in the raw-string format,
>>>> e.g.
>>>> print_as_raw_string(r"\.") should output r"\." instead of "\\."  ...
>
> ...
> The big issue I see is that lots of values cannot be represented
> as raw strings.  Your code checks for a final '\', but many "control"
> characters and the quote marks are also at issue.
>
> raw_repr('["\']a\x13\xfebc\\de') should contain several nasty cases.

It seems to me that a raw_repr should only be required to be able to
encode strings that are representable as raw strings in the first
place.  Here's mine:

def raw_repr(s):
if s and s[-1] == '\\':
raise ValueError('No raw string representation; '
 'string ends in backslash')
# If you want to test for control and non-ASCII characters to raise
# an exception, that's up to you
quote = None
if '\n' not in s:
if "'" not in s:
quote = "'"
elif '"' not in s:
quote = '"'
if not quote:
if "'''" not in s:
quote = "'''"
elif '"""' not in s:
quote = '"""'
if not quote:
raise ValueError('No raw string representation; '
 'string contains unescapable quote characters')
return 'r' + quote + s + quote

>>> print raw_repr(r"1\2'3\n")
r"1\2'3\n"
>>> print raw_repr(r'''"What's up," he said.''')
r'''"What's up," he said.'''
>>> # valid raw string, left as an exercise for the reader:
... print raw_repr(r'"""'"'''") # ;)
Traceback (most recent call last):
  File "", line 2, in ?
  File "", line 19, in raw_repr
ValueError: No raw string representation; string contains unescapable
quote characters

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


Re: why cannot assign to function call

2008-12-29 Thread Miles
On Mon, Dec 29, 2008 at 1:01 AM, scsoce  wrote:
> I have a function return a reference, and want to assign to the reference,
> simply like this:
>>>def f(a)
> return a
>b = 0
>   * f( b ) = 1*
> but the last line will be refused as "can't assign to function call".
> In my thought , the assignment is very nature,  but  why the interpreter
> refused to do that ?

Here's some links to help you better understand Python objects:

http://effbot.org/zone/python-objects.htm
http://effbot.org/zone/call-by-object.htm

The second one is a bit denser reading, but it's important to learn
that Python's approach to objects and "variables" is fundamentally
different from that of C/C++.  In the example below, there's no way in
the Python language* that bar() can change the value of b, since
strings and numbers are immutable.

def foo():
b = 0
bar(b)
print b # will always be 0

* There are stupid [ctypes/getframe/etc.] tricks, though I think all
are implementation-specific

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


Re: Is this a bug in Python or something I do not understand.

2009-01-01 Thread Miles
On Thu, Jan 1, 2009 at 1:13 PM,   wrote:
> Consider these two lists comprehensions:
>
> L1=[[1 for j in range(3)] for i in range(3)]
> L2=[[1]*3]*3
>
[snip]
>
> It seems a misbehaviour in Python, or there is something I do not
> understand in the syntax 

It's not a Python bug.  Does this help illuminate the difference?

>>> L1 = [object() for j in range(3)]
>>> L2 = [object()] * 3
>>> [id(o) for o in L1]
[164968, 164976, 164984]
>>> L1[0] is L1[1]
False
>>> [id(o) for o in L2]
[164992, 164992, 164992]
>>> L2[0] is L2[1]
True

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


Re: Is there a better/simpler way to filter blank lines?

2008-11-05 Thread Miles
Ben Finney wrote:
> Falcolas writes:
>
>> Using the surrounding parentheses creates a generator object
>
> No. Using the generator expression syntax creates a generator object.
>
> Parentheses are irrelevant to whether the expression is a generator
> expression. The parentheses merely group the expression from
> surrounding syntax.

As others have pointed out, the parentheses are part of the generator
syntax.  If not for the parentheses, a list comprehension would be
indistinguishable from a list literal with a single element, a
generator object.  It's also worth remembering that list
comprehensions are distinct from generator expressions and don't
require the creation of a generator object.

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


Re: end child process when parent dies (on Unix)

2008-11-17 Thread Miles
On Mon, Nov 17, 2008 at 2:51 AM, Дамјан Георгиевски wrote:
>>> I'm starting a Unix tool with subprocess.Popen() from a python script
>>> and I want the child to be killed when the parent (my script) ends
>>> for whatever reason *including* if it gets killed by SIGKILL.
>>
>> A Linux-specific solution is prctl(2).
>
> I've tried this in a test C program... exactly what I need. Now if I
> could slip this between the fork and exec in subprocess.Popen()

preexec_fn, perhaps?

http://docs.python.org/library/subprocess.html#using-the-subprocess-module

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


Re: Raw String Question

2009-03-12 Thread Miles
On Thu, Mar 12, 2009 at 3:28 PM, Jim Garrison wrote:
> OK, I'm curious as to the reasoning behind saying that
>
>   When an 'r' or 'R' prefix is present, a character following a
>   backslash is included in the string without change, and all
>   backslashes are left in the string.
>
> which sounds reasonable, but then saying in effect "Oh wait, let's
> introduce a special case and make it impossible to have a literal
> backslash as the last character of a string without doubling it".

That's not a special case; that's the *opposite* of a special case.

> So you have a construct (r'...') whose sole reason for existence
> is to ignore escapes, but it REQUIRES an escape mechanism for one
> specific case (which comes up frequently in Windows pathnames).

The backslash still IS an escape character, it just behaves
differently than it does for a non-raw string.

> At the very least the "all backslashes are left in the string" quote
> from the Lexical Analysis page (rendered in italics no less) needs to
> be reworded to include the exception instead of burying this in a
> parenthetical side-comment.

There is no exception.  All backslashes are left in the string.  The
impossibility of ending a raw string in an unescaped backslash is also
rendered in italics.

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


Re:

2009-03-13 Thread Miles
On Fri, Mar 13, 2009 at 2:12 PM, John Posner wrote:
>
>>     I have 2 lists
>> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
>> b = [2, 4, 1, 3]
>>
>>     Now, I want to order _a_ (a[1]) based on _b_.
>>     i.e. the second element in tuple should be the same as
>> b.
>>     i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]
>
> Essentially, you're sorting a list. The Pythonic approach is to use the 
> sort() function, hiding the details in a "custom comparison function":
>
> def compare_func(first, second):
>    b = [2, 4, 1, 3]
>    return cmp(b.index(first[1]), b.index(second[1]))
>
> if __name__ == '__main__':
>    a = [(4, 1), (7, 3), (3, 2), (2, 4)]
>    a.sort(cmp=compare_func)
>    print a

A version that runs in O(n ln n) instead of O(n^2 ln n):

a = [(4, 1), (7, 3), (3, 2), (2, 4)]
b = [2, 4, 1, 3]
bdict = dict((v,k) for (k,v) in enumerate(b))
a.sort(key=lambda k: bdict[k[1]])

If you don't want to build the intermediary dict, a less efficient
version that runs in O(n^2):

a.sort(key=lambda k: b.index(k[1]))

Which is mostly similar to John's solution, but still more efficient
because it only does a b.index call once per 'a' item instead of twice
per comparison.

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


Re:

2009-03-13 Thread Miles
On Fri, Mar 13, 2009 at 3:13 PM, Miles wrote:
> [snip]

Sorry, didn't see the original thread on this.

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


Re: don't understand behaviour of recursive structure

2009-03-14 Thread Miles
On Sat, Mar 14, 2009 at 12:31 PM, Dan Davison wrote:
> I'm new to python. Could someone please explain the following behaviour
> of a recursive data structure?
>
> def new_node(id='', daughters=[]):
>    return dict(id=id, daughters=daughters)

This is something of a FAQ:

http://effbot.org/zone/default-values.htm
http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects

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


Re: Is this type of forward referencing possible?

2009-03-15 Thread Miles
On Sun, Mar 15, 2009 at 11:33 AM, Paul McGuire wrote:
> I'm guessing the class mapping is all part of an overall design, so I
> would define all of these after creating A and B as empty classes:
>
> class A: pass
> class B: pass
>
> A.someType = B
> B.anotherType = A

While this would work in the general case, it's more iffy with ORM
tools like the App Engine Datastore API.  Those classes often have
metaclasses that process the properties when the classes are created,
and tacking on additional properties later is unlikely to work (I
suspect the OP neglected to point out that A and B likely both inherit
from db.Model).

The Django solution is to allow forward references in the form of
strings of class names (e.g., ReferenceType('B')).  It doesn't look
like App Engine has a solution for this situation:
http://code.google.com/p/googleappengine/issues/detail?id=811

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


Re: How evil is this use of sys.getrefcount?

2009-03-20 Thread Miles
On Fri, Mar 20, 2009 at 6:27 PM, Brian Cole wrote:
> I'm trying to cache the construction and destruction of an expensive
> object coming out of a generator function. I can do the following to
> force a new object to be used if the object is being used somewhere
> else (based upon it's reference count). Here is the pseudo-code of
> what I am trying to accomplish.
>
> def GetExpensiveObjects():
>    obj = ExpensiveObject()
>    baserefs = sys.getrefcount(obj)
>    while UpdateExpensiveObject(obj):
>        yield obj
>        if sys.getrefcount(obj) > baseline + 1:
>            obj = ExpensiveObject()

Seems pretty evil to me!  My approach would probably either be to have
two generators and allow the user to decide if they want the object to
be valid after a subsequent call to the iterator, or to use a wrapper
object like the following:

class ExpensiveProxy(object):
_cache = []

def __init__(self):
if self.cache:
self._object = self._cache.pop()
else:
self._object = ExpensiveObject()
UpdateExpensiveObject(self._object)

def __del__(self):
self._cache.append(self._object)

# Define your own methods to wrap those of the object,
# or maybe define __getattr__

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


Re: How can I do bit operation on python float value

2009-03-23 Thread Miles
On Mon, Mar 23, 2009 at 2:44 AM, valpa wrote:
> I have a python float 1.2345678. I know that it is stored as a double
> in C type. And I know it actually is 1010101010101 -like format. Then
> I want to do some bit operation on it. How?
>
> Sure, I want a float output when I finish the operation.

Just for fun:

>>> import ctypes
>>> def cast(value, to_type):
... return ctypes.cast(ctypes.pointer(value), ctypes.POINTER(to_type))[0]
...
>>> def float_to_int(x):
... return cast(ctypes.c_double(x), ctypes.c_uint64)
...
>>> def int_to_float(x):
... return cast(ctypes.c_uint64(x), ctypes.c_double)
...
>>> float_to_int(5)
4617315517961601024
>>> bin(float_to_int(5))
'0b1010100'
>>> int_to_float(float_to_int(5) & float_to_int(10))
2.5

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


Re: Syntax error when importing a file which starts with a number

2009-03-23 Thread Miles
On Mon, Mar 23, 2009 at 1:56 PM,   wrote:
> Hello, all.
>
> I don't suppose anyone has any idea why it seems to be impossible to
> import any file which starts with a number?  You get a syntax error,
> whether the file exists or not.

Identifiers can't start with a number.

http://docs.python.org/reference/lexical_analysis.html#identifiers

> It's a bit annoying, as I have an enforced naming scheme.  Any way
> round it?

You could import it like so:

some_valid_identifer = __import__('1foo', globals(), locals())

http://docs.python.org/library/functions.html#__import__

But a far better solution is to fix your naming scheme—it's completely
illogical to have a Python module naming scheme where the names aren't
valid identifiers.

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


Re: dict view to list

2009-03-27 Thread Miles
On Fri, Mar 27, 2009 at 9:58 AM, Aaron Brady wrote:
> The suggestion is entirely a "look and feel" observation.  In an
> interactive session, to examine the contents of a dictionary I've just
> created, I need to type list(_), and lose the previous return value.
> It's a better for my  train of thought too.

Versus _.tolist(), which would also overwrite the previous return value?

> I don't necessarily advocate that every collection and iterator should
> grow one, though I don't really have the case for a special case for
> dict views.  OTOH, I find three 'tolist' methods in the standard
> library: array, memoryview, and parser.ST.  It could offer the same as
> they do.

array has had that method since before Python v1.0. memoryview, for
whatever reason, is not iterable; parser.ST is also not, since it
doesn't convert to a flat sequence but rather a list tree.  I don't
see the special case for dict views, at all.

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


Re: Python print and types selection

2009-03-27 Thread Miles
On Fri, Mar 27, 2009 at 6:56 PM, Gabriel Genellina wrote:
> En Fri, 27 Mar 2009 18:43:16 -0300,  escribió:
>> Python print recognizes the local constant "dog", but it goes and
>> fetches the __int__ type from my object-based class, even though it's
>> value is a long.  Then Python print doesn't expect a long to come back
>> and bombs out.  I don't want to force the main program to cast the
>> long value getting returned.  I know Python print can print a long,
>> but how can I make it select the __long__ instead of the __int__ ?
>
> This bug was corrected in version 2.6 - see
> http://bugs.python.org/issue1742669
> If you have to stay with 2.5 I'm afraid any solution would require to modify
> your code:
>
> -- put long() around those arguments
> -- "%s" % format_hex(val) (where format_hex does the long conversion)
> -- redefine __str__ and use %s

Or make your class a subclass of long.

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


Re: Generators/iterators, Pythonicity, and primes

2009-04-04 Thread Miles
On Sat, Apr 4, 2009 at 2:50 PM, John Posner wrote:
> Inspired by recent threads (and recalling my first message to Python
> edu-sig), I did some Internet searching on producing prime numbers using
> Python generators. Most algorithms I found don't go for the infinite,
> contenting themselves with "list all the primes below a given number".
>
> Here's a very Pythonic (IMHO) implementation that keeps going and going and
> going ...:
>
> from itertools import count
> from math import sqrt
>
> def prime_gen():
> """
> Generate all prime numbers
> """
> primes = []
> for n in count(2):
> if all(n%p for p in primes if p < sqrt(n)):
> primes.append(n)
> yield n
>
> The use of all() is particularly nifty (see
> http://code.activestate.com/recipes/576640/). And so is the way in which the
> list comprehension easily incorporates the sqrt(n) optimization.
>
> Question: Is there a way to implement this algorithm using generator
> expressions only -- no "yield" statements allowed?

def prime_gen():
primes = []
return (primes.append(n) or n for n in count(2) if all(n%p for p
in primes if p<=sqrt(n)))

That version is only marginally faster than your original version.
The biggest performance penalty is that the (... for p in primes ...)
generator isn't aborted once p > sqrt(n). Here's a less nifty but much
more efficient version:

def prime_gen():
prime_list = [2]
for p in prime_list: yield p
for n in itertools.count(prime_list[-1] + 1):
for p in prime_list:
if p * p > n:
prime_list.append(n)
yield n
break
elif n % p == 0:
break
else:
raise Exception("Shouldn't have run out of primes!")

When generating the first 1000 primes, this version's approximately 20
times faster; for the first 10,000 primes, ~80x (but still much slower
than a simple Sieve of Eratosthenes).  To make repeated calls faster,
move prime_list = [2] outside the function.

-Miles

P.S. Gmail shows all your messages with a blank body and a text
attachment containing your message; this is perhaps because your
mailer includes an invalid blank Content-disposition header.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tab completion

2009-04-05 Thread Miles
On Thu, Apr 2, 2009 at 10:59 AM, Steven D'Aprano wrote:
> Does anyone use the tab-completion recipe in the docs?
>
> http://docs.python.org/library/rlcompleter.html#module-rlcompleter
>
> suggests using this to enable tab-completion:
>
> try:
>    import readline
> except ImportError:
>    print "Module readline not available."
> else:
>    import rlcompleter
>    readline.parse_and_bind("tab: complete")
>
> which is all very nice, but it makes it rather difficult to indent code
> blocks:
>
>>>> def func(x):
> ...
> Display all 174 possibilities? (y or n)
>
>
> I like tab-completion, but I'd rather not be reduced to typing spaces for
> indents in the interpreter. What do other people do?

Personally, I just use single spaces for indentation when typing in
the interpreter.  But how would something like this work for you?

import readline
import rlcompleter

class TabCompleter(rlcompleter.Completer):
def complete(self, text, state):
if text == '' or text.endswith('\t'):
if state == 0:
return text + '\t'
else:
    return None
return rlcompleter.Completer.complete(self, text, state)

readline.set_completer(TabCompleter().complete)

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


Re: more fun with iterators (mux, demux)

2009-04-06 Thread Miles
On Mon, Apr 6, 2009 at 8:05 PM, Neal Becker wrote:
> I'm trying to make a multiplexor and demultiplexor, using generators.  The
> multiplexor will multiplex N sequences -> 1 sequence  (assume equal length).
> The demultiplexor will do the inverse.
>
> The demux has me stumped.  The demux should return a tuple of N generators.

from itertools import islice, tee

def demux(iterable, n):
return tuple(islice(it, i, None, n) for (i, it) in
enumerate(tee(iterable, n)))

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


Re: more fun with iterators (mux, demux)

2009-04-06 Thread Miles
On Mon, Apr 6, 2009 at 10:05 PM, Steven D'Aprano wrote:
> On Mon, 06 Apr 2009 20:05:51 -0400, Neal Becker wrote:
>
>> I'm trying to make a multiplexor and demultiplexor, using generators.
>> The multiplexor will multiplex N sequences -> 1 sequence  (assume equal
>> length). The demultiplexor will do the inverse.
>>
>> The mux seems easy enough:
>>
>> ---
>> def mux (*ranges):
>>     iterables = [iter (r) for r in ranges] while (True):
>>         for i in (iterables):
>>             yield i.next()
>
>
> This is like a zip, and can be re-written using itertools.izip.
>
> def mux(*iterables):
>    for i in itertools.izip(*iterables):
>        for item in i:
>            yield item

In Python 2.6, you could also do this:

def mux(*iterables):
return itertools.chain.from_iterable(itertools.izip(*iterables))

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


Re: more fun with iterators (mux, demux)

2009-04-08 Thread Miles
On Wed, Apr 8, 2009 at 1:21 PM, pataphor wrote:
> On Wed, 08 Apr 2009 10:51:19 -0400 Neal Becker wrote:
>
>> What was wrong with this one?
>>
>> def demux(iterable, n):
>>     return tuple(islice(it, i, None, n) for (i, it) in
>> enumerate(tee(iterable, n)))
>
> Nothing much, I only noticed after posting that this one handles
> infinite sequences too. For smallish values of n it is acceptable.

I assume that "smallish values of n" refers to the fact that
itertools.tee places items into every generator's internal deque,
which islice then skips over, whereas your version places items only
into the deque of the generator that needs it.  However, for small n,
the tee-based solution has the advantage of having most of the work
done in C instead of in Python generator functions; in my limited
benchmarking, the point where your version becomes faster is somewhere
around n=65.

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


Re: is there a rwlock implementation in python library?

2009-04-08 Thread Miles
On Wed, Apr 8, 2009 at 11:10 PM, Ken wrote:
> I need a read-write-lock, is there already an implementation in the standard
> library?

No, but there are several recipes on ActiveState:

http://code.activestate.com/recipes/413393/
http://code.activestate.com/recipes/502283/
http://code.activestate.com/recipes/465156/

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


Re: Why does Python show the whole array?

2009-04-09 Thread Miles
On Thu, Apr 9, 2009 at 2:59 AM, Peter Otten wrote:
> Lawrence D'Oliveiro wrote:
>> This is why conditional constructs should not accept any values other than
>> True and False.
>
> So you think
>
> if test.find(item) == True: ...
>
> would have been better?

Clearly, any comparison with a boolean literal should be illegal.  ;)

-Miles

P.S. ... really, though.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unsupported operand types in if/else list comprehension

2009-04-10 Thread Miles
On Fri, Apr 10, 2009 at 5:26 PM, Mike H wrote:
> Thanks to all of you.
>
> FYI, I'm doing this because I'm working on creating some insert
> statements in SQL, where string values need to be quoted, and integer
> values need to be unquoted.

This is what you should have posted in the first place.  Your solution
is entirely the wrong one, because it will break if your input strings
contain the quote character (and suffers from other issues as
well)--this is where SQL injection vulnerabilities come from.  The
safe and correct way is to allow your database driver to insert the
parameters into the SQL query for you; it will look something like
this (though the exact details will vary depending on what module
you're using):

cursor.execute('INSERT INTO my_table VALUES (?, ?, ?)', ['test',1,'two'])

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


Re: Adding a Badge to an Icon in Mac OS X

2009-04-10 Thread Miles
On Fri, Apr 10, 2009 at 5:22 PM, bingo wrote:
> PyObjc seems to offer the option to add badges to icons in the doc. I
> need to add badges to any icon... kinda like SCPlugin and dropbox do.
> I think that SCPlugin is doing it through carbon Icon Services. But I
> am still trying to figure out how it is done!

I believe those programs are able to do so because they are Finder
plugins--it's not something that a separate program could do.  This
isn't really a Python question, though; you'd probably have better
luck finding answers on a OS X-related list.

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


Re: How to check all elements of a list are same or different

2009-04-15 Thread Miles
On Wed, Apr 15, 2009 at 8:48 PM, Paul Rubin wrote:
> I'd use:
>
>   from operator import eq
>   all_the_same = reduce(eq, mylist)

That won't work for a sequence of more than two items, since after the
first reduction, the reduced value that you're comparing to is the
boolean result:

>>> reduce(eq, [0, 0, 0])
False
>>> reduce(eq, [0, 1, False])
True

I'd use:

# my preferred:
def all_same(iterable):
it = iter(iterable)
first = it.next()
return all(x == first for x in it)

# or, for the pathologically anti-generator-expression crowd:
from functools import partial
from operator import eq
from itertools import imap
def all_same(iterable):
it = iter(iterable)
    return all(imap(partial(eq, it.next()), it))

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


Re: struct unpack issue

2008-06-13 Thread Miles
On Fri, Jun 13, 2008 at 9:27 PM, Ping Zhao <[EMAIL PROTECTED]> wrote:
> However, when I tried to rewrite them in short:
>
> header = struct.unpack('2s1i', self.__read(src, 6))
> ...
> It was weired that the required argument length increased to 8.

This is an alignment issue; if you don't specify a structure format
character, the struct module acts like the data is laid out in memory
as a C compiler would do it.

http://en.wikipedia.org/wiki/Data_structure_alignment
http://docs.python.org/lib/module-struct.html

Specifying little-endian byte order will force the data to be packed
(unaligned), and will also make your code more portable:

struct.unpack('<2s1i', ...)

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


Re: socket.connect() hangs in SYN_SENT state.

2008-07-13 Thread Miles
On Sat, Jul 12, 2008 at 11:23 PM, bukzor <[EMAIL PROTECTED]> wrote:
> I'm connecting to an apache2 process on the same machine,
> for testing. When looking at netstat, the socket is in the SYN_SENT
> state, like this:
>
> $netstat -a -tcp
> tcp0  0 *:www   *:* LISTEN  7635/apache2
> tcp0  1 bukzor:38234adsl-75-61-84-249.d:www SYN_SENT  
>   9139/python
>
> Anyone know a general reason this might happen? Even better, a way to
> fix it?

That socket connection is to a remote machine, not the same one.  Your
test code works fine for me.  The "hang then crash" (and I'm assuming
"crash" here means an uncaught exception) just means that your packets
are being silently ignored by whatever machine you're actually
attempting to connect to. It's possible that your machine has odd DNS
settings causing buzkor.hopto.org to resolve to the wrong address.

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


Re: socket.connect() hangs in SYN_SENT state.

2008-07-13 Thread Miles
On Sun, Jul 13, 2008 at 2:32 PM, bukzor <[EMAIL PROTECTED]> wrote:
> On Jul 13, 1:14 am, Miles <[EMAIL PROTECTED]> wrote:
>> On Sat, Jul 12, 2008 at 11:23 PM, bukzor <[EMAIL PROTECTED]> wrote:
>> > I'm connecting to an apache2 process on the same machine,
>> > for testing. When looking at netstat, the socket is in the SYN_SENT
>> > state, like this:
>>
>> > $netstat -a -tcp
>> > tcp0  0 *:www   *:* LISTEN  7635/apache2
>> > tcp0  1 bukzor:38234adsl-75-61-84-249.d:www 
>> > SYN_SENT9139/python
>>
>> > Anyone know a general reason this might happen? Even better, a way to
>> > fix it?
>>
>> That socket connection is to a remote machine, not the same one.  Your
>> test code works fine for me.  The "hang then crash" (and I'm assuming
>> "crash" here means an uncaught exception) just means that your packets
>> are being silently ignored by whatever machine you're actually
>> attempting to connect to. It's possible that your machine has odd DNS
>> settings causing buzkor.hopto.org to resolve to the wrong address.
>
> I'm connecting to my machine through the internet, and the resolved
> URL of my router is what you're seeing above. If you run the code
> above you'll see what I mean.

I did run the code, and as I said, it works fine.  Your description of
the setup is not consistent. The netstat output unambiguously states
that a Python script on "buzkor" is attempting to open a connection to
the HTTP port on the "adsl" machine (and failing because "adsl" is not
responding).  The problem here is not Python; you seem to be confused
about which machine is connecting to which.

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


Re: heapq question

2008-07-13 Thread Miles
On Sun, Jul 13, 2008 at 3:05 PM, Giampaolo Rodola' <[EMAIL PROTECTED]> wrote:
> On 13 Lug, 19:31, "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:
>> > I understand that heapq is not that efficient to implement timeouts as
>> > I thought at first.
>> > It would have been perfect if there were functions to remove arbitrary
>> > elements withouth needing to re-heapify() the heap every time.
>>
>> It is efficient for that - you just need to use it correctly.
>>
>> To remove the nth element from a heap, replace it with the last element,
>> and then _siftup that element:
>>
>> The time complexity for that operation is O(log(len(heap))).

The problem is that in order to remove an arbitrary element from a
heap, you usually have to do an O(n) linear search in order to find it
first, since you can't know ahead of time which index an arbitrary
element is at.  (You can, actually, but only if your heap
implementation somehow notifies the elements of their new index when
it moves them in the heap, which the Python heapq module doesn't do,
so you'd have to write your own heap implementation.)

> And if instead of removing an element I'd want to change its value?
> E.g.:
>
>  >>> heap = [0, 2, 1, 4, 5, 6, 7, 8, 9]
>  >>> heapify(heap)
>  >>> heap
>  [0, 2, 1, 4, 5, 6, 7, 8, 9]
>  >>> heap[4] = 12

Don't do that; you must first remove the element and then reinsert it.

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

Re: heapq question

2008-07-13 Thread Miles
On Sun, Jul 13, 2008 at 4:16 PM, Duncan Booth
<[EMAIL PROTECTED]> wrote:
> "Giampaolo Rodola'" <[EMAIL PROTECTED]> wrote:
>> I understand that heapq is not that efficient to implement timeouts as
>> I thought at first.
>> It would have been perfect if there were functions to remove arbitrary
>> elements withouth needing to re-heapify() the heap every time.
>>
> There could be suitable functions, but there aren't any.
> ...
> Bottom line though is that heaps aren't really suitable for timeouts.

I would argue that heaps in general are ideally suited for timeouts;
it's just that the Python heapq implementation is lacking if you ever
need to cancel a timeout before it expires.

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


Re: Magic?

2008-07-13 Thread Miles
On Sun, Jul 13, 2008 at 12:55 PM, mk <[EMAIL PROTECTED]> wrote:
> So my updated question is where does this lstr() instance keep the original
> value ('abcdef')? It obviously has smth to do with inheriting after str
> class, but I don't get the details of the mechanism.

Through the str.__new__ method: http://docs.python.org/ref/customization.html

Calling a constructor:
foo = Foo(1, 2, 3)

Is roughly equivalent to:
foo = Foo.__new__(Foo, 1, 2, 3)
Foo.__init__(foo, 1, 2, 3)

As a side note, you may already know this, but it's not necessary to
create a property setter that raises an error to make a property
read-only; you can simply not define a setter method.

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


Re: socket.connect() hangs in SYN_SENT state.

2008-07-13 Thread Miles
On Sun, Jul 13, 2008 at 8:35 PM, bukzor <[EMAIL PROTECTED]> wrote:
> The problem only manifests about 1 in 20 runs. Below there's code for
> a client that shows the problem 100% of the time.
>
> The two URL's that I seem to be "confused" about point to the same IP.
> Maybe this will make it clear:
>
> PING bukzor.hopto.org (75.61.84.249) 56(84) bytes of data.
> 64 bytes from adsl-75-61-84-249.dsl.pltn13.sbcglobal.net
> (75.61.84.249): icmp_seq=1 ttl=255 time=1.68 ms

For me, buzkor.hopto.org resolves to 69.65.19.125, which I hope
explains why I thought you were confused, and increases my own
suspicion that DNS settings are to blame.  I let the script run for
about five minutes without it failing.

Does your luck change if you use "localhost" or a numeric IP address
in the ServerProxy URL?

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


Re: socket.connect() hangs in SYN_SENT state.

2008-07-13 Thread Miles
On Sun, Jul 13, 2008 at 9:31 PM, Miles <[EMAIL PROTECTED]> wrote:
> On Sun, Jul 13, 2008 at 8:35 PM, bukzor <[EMAIL PROTECTED]> wrote:
>> The problem only manifests about 1 in 20 runs. Below there's code for
>> a client that shows the problem 100% of the time.
>>
>> The two URL's that I seem to be "confused" about point to the same IP.
>> Maybe this will make it clear:
>>
>> PING bukzor.hopto.org (75.61.84.249) 56(84) bytes of data.
>> 64 bytes from adsl-75-61-84-249.dsl.pltn13.sbcglobal.net
>> (75.61.84.249): icmp_seq=1 ttl=255 time=1.68 ms
>
> For me, buzkor.hopto.org resolves to 69.65.19.125

Ahhh... "bukzor".  Well, that makes sense.  Pardon my temporary dyslexia.

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


Re: socket.connect() hangs in SYN_SENT state.

2008-07-13 Thread Miles
On Sat, Jul 12, 2008 at 11:23 PM, bukzor <[EMAIL PROTECTED]> wrote:
> Anyone know a general reason this might happen? Even better, a way to
> fix it?

Another reason that a socket can hang in the SYN_SENT state (besides
trying to connect to an unreachable host without getting an ICMP
destination-unreachable message in response): if the server's listen
queue is full, it will silently ignore SYN packets until there is room
in the queue.

Sorry again about the "bukzor" vs. "buzkor" thing.  I don't know
what's causing your problem (and it's probably not a DNS issue after
all) but it's more likely to be a server issue than a client one.
Maybe your client has an unusually low socket timeout for some reason,
though; does increasing it (with socket.setdefaulttimeout) help?  Mine
seems to default to about 75 seconds.

If you can't work out the root cause, but it only happens every once
in a while, you could try changing your client code to catch the
socket exception and retry a limited number of times.

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


Re: socket.connect() hangs in SYN_SENT state.

2008-07-13 Thread Miles
On Sun, Jul 13, 2008 at 10:29 PM, Miles <[EMAIL PROTECTED]> wrote:
> On Sat, Jul 12, 2008 at 11:23 PM, bukzor <[EMAIL PROTECTED]> wrote:
>> Anyone know a general reason this might happen? Even better, a way to
>> fix it?
>
> Maybe your client has an unusually low socket timeout for some reason,
> though; does increasing it (with socket.setdefaulttimeout) help?

Never mind on that, as you already said that it hangs for about two
minutes.  Clearly my reading comprehension and retention rate are at
an all-time low today.

low-signal-to-noise-ratio-ly yours,
Miles
--
http://mail.python.org/mailman/listinfo/python-list


Re: __del__ methods

2008-07-18 Thread Miles
On Fri, Jul 18, 2008 at 2:31 PM, Jason Baker <[EMAIL PROTECTED]> wrote:
> I have a class that I need to do some finalization on when it dies.  I
> know I can use the __del__ method, but I seem to recall that it
> impedes garbage collection.  Is this the case?

Yes.

"Objects that have __del__() methods and are part of a reference cycle
cause the entire reference cycle to be uncollectable, including
objects not necessarily in the cycle but reachable only from it.
Python doesn't collect such cycles automatically because, in general,
it isn't possible for Python to guess a safe order in which to run the
__del__() methods."

The uncollectable objects are stored in gc.garbage and will not be
freed until their reference cycles are broken and they are removed
from that list.

http://docs.python.org/lib/module-gc.html

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


Re: Not entirely serious: recursive lambda?

2008-07-19 Thread Miles
On Sat, Jul 19, 2008 at 10:43 PM, Michael Tobis <[EMAIL PROTECTED]> wrote:
> Can a lambda call itself without giving itself a name?

Kind of.  There's a couple ways I know of.

The functional way, which involves the lambda receiving itself as an argument:

(lambda f: f(10, f))(lambda n, f: n and (sys.stdout.write("%d\n" % n)
or f(n-1,f)))

The stack frame examination way:

import sys, inspect, new
(lambda:sys.stdout.write('recurse\n') or
new.function(inspect.currentframe().f_code, globals())())()

The functional way is probably harder to grok unless you've studied
lambda calculus or had experience with "real" functional languages (I
haven't).  For fun, try throwing a Y combinator in there.

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


Re: MethodChain

2008-07-20 Thread Miles
On Sun, Jul 20, 2008 at 1:01 AM, Marc 'BlackJack' Rintsch
<[EMAIL PROTECTED]> wrote:
> The methods are a problem IMHO.  You can't add an own method/function with
> the name `fire()` or `toFunction()`.  `MethodChain` has to know all
> functions/methods in advance.  You can add the methods of whole classes at
> once and there are over 300 pre-added, this begs for name clashes.

Name clashes aren't an issue, since MethodChain doesn't apply any
special meaning to the method names it knows; the limitation is
because JavaScript doesn't allow you to modify property lookup
behavior.  And since we can make the chain object callable, we don't
need "fire" or "toFunction" methods.

###

from functools import partial

class MethodChain(object):
# The implementation of this could be cleaner.  I would prefer
# chain.foo() to return a new object instead of modifying chain.
# But this is easier to write and closer to the JavaScript implementation.
def __init__(self):
self._methodname = None
self._chain = []

def __getattr__(self, methodname):
assert self._methodname is None
self._methodname = methodname
return self

def __call__(self, *args, **kwargs):
if self._methodname is None:
assert len(args) == 1 and not kwargs
result = args[0]
for method in self._chain:
result = getattr(result, method[0])(*method[1], **method[2])
return result
else:
self._chain.append((self._methodname, args, kwargs))
self._methodname = None
return self

def compose(*callables):
def composition(arg):
result = arg
for callable in callables:
# or should that be reversed(callables)? to be mathematically
# accurate, yes, probably; however, it also makes sense to
# specify the callables in the order they'll be applied
result = callable(result)
return result
return composition

chain = MethodChain().lower().replace('ello,', 'ey').title().split()

print chain('HELLO, world')

func = compose(chain, partial(map, lambda s: s+'!'), ' '.join)

print func('HELLO, world')

###

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


Re: how to split text into lines?

2008-07-30 Thread Miles
On Wed, Jul 30, 2008 at 4:45 PM, kj wrote:
>>What's the Python idiom for splitting text into lines, preserving
>>the end-of-line sequence in each line?
>
>
> Sorry, I should have googled this first.  I just found splitlines()...
>
> Still, for my own edification, is there a way to achieve the same
> effect using re.split?

Not directly: re.split doesn't split on zero-length matches.

http://mail.python.org/pipermail/python-dev/2004-August/047272.html
http://bugs.python.org/issue852532
http://bugs.python.org/issue988761
http://bugs.python.org/issue3262

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


Re: overriding file.readline: "an integer is required"

2008-07-30 Thread Miles
On Wed, Jul 30, 2008 at 5:52 PM, kj <[EMAIL PROTECTED]> wrote:
> I know that I could rewrite the method like this:
>
>def readline(self, size=None):
>if size == None:
>line = self.file.readline()
>else:
>line = self.file.readline(size)
># etc., etc.
>
> ...but this seems to me exceptionally awkward.  (Actually, it's worse
> than awkward: it fails to complain when the overriding method is
> called with the argument None.

IMO, the method should have been possible to call with None in the
first place (like str.split and list.sort), and it's not incorrect for
your method to allow it.

> It would be better to test for the
> number of arguments passed to the function, but I can't figure out
> how to do this either.)

You could of course do:

def readline(self, *args):
line = self.file.readline(*args)
# etc., etc.

But this has its drawbacks.

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


Re: Optimizing size of very large dictionaries

2008-07-30 Thread Miles
On Wed, Jul 30, 2008 at 8:29 PM,  <[EMAIL PROTECTED]> wrote:
> Background: I'm trying to identify duplicate records in very large text
> based transaction logs. I'm detecting duplicate records by creating a SHA1
> checksum of each record and using this checksum as a dictionary key. This
> works great except for several files whose size is such that their
> associated checksum dictionaries are too big for my workstation's 2G of RAM.

What are the values of this dictionary?

You can save memory by representing the checksums as long integers, if
you're currently using strings.

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


Re: Difference between type and class

2008-07-31 Thread Miles
On Thu, Jul 31, 2008 at 1:59 PM, Nikolaus Rath wrote:
> If it is just a matter of different rendering, what's the reason for
> doing it like that? Wouldn't it be more consistent and straightforward
> to denote builtin types as classes as well?

Yes, and in Python 3, it will be so:

>>> class myint(int): pass
...
>>> int

>>> myint


The reason the distinction is made currently is that before Python
2.2, "types" were built-in (or C extension) classes, and "classes"
were Python classes; it was not possible to subclass built-in types.
That "classic" style of classes are still supported in Python 2.2 and
above (but not in Python 3), by not inheriting from object or any
other built-in.  However, for new-style classes, the only distinction
is in the repr.

>>> class classic: pass
...
>>> class newstyle(object): pass
...
>>> type(classic)

>>> type(classic())

>>> classic.__class__
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: class classic has no attribute '__class__'
>>> classic().__class__

>>>
>>> type(newstyle)

>>> type(newstyle())


Further reading:

http://www.python.org/download/releases/2.2.3/descrintro/
http://svn.python.org/view?rev=23331&view=rev
http://bugs.python.org/issue2565

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


Re: Class definition attribute order

2008-08-01 Thread Miles
On Fri, Aug 1, 2008 at 7:23 PM, Andrew Lentvorski <[EMAIL PROTECTED]> wrote:
> How do I determine the order of definition of class attributes?
>
> For example, if I have a class
>
> class Test(object):
>y = 11
>x = 22
>
> How do I tell that y was defined before x?

You can't.  The order that the locals are bound is not something
that's preserved.

There are frameworks that simulate this, but they actually just store
the order that the attributes are initialized:

class Test(FrameworkClass):
y = FrameworkObject(11)
x = FrameworkObject(22)

where FrameworkObject is assigned a number from an incrementing
counter on initialization, and FrameworkClass has a metaclass that
sorts those objects when the class is created.

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


Re: string to type object (C)

2008-08-05 Thread Miles
On Tue, Aug 5, 2008 at 2:30 AM, castironpi <[EMAIL PROTECTED]> wrote:
> I'm looking for a one-to-one function from strings to the built-in
> data types in C.  I will be keeping the string in a file.  I need the
> PyTypeObject* back from it.  If nothing else I'll just do a bunch of
> strcmp( "tuple" ) { return &PyTuple_Type; } commands, provided
> &PyTuple_Type; will be linking the right address.

Something along the lines of this?
b = PyImport_Import("__builtin__");
return PyObject_GetAttrString(b, typestring);

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


Re: how to find out if an object is a class?

2008-08-08 Thread Miles
On Fri, Aug 8, 2008 at 2:31 AM, Stefan Behnel wrote:
> I recently had the reverse case that a (stupidly implemented) extension module
> required a callback function and I wanted to pass a function wrapped in a
> wrapper object. That failed, because it specifically checked for the argument
> being a function, not just a callable object. I had to pull quite a number of
> tricks to reimplement the wrapper class as a function (thank god, it's 
> Python!).

You really only needed one trick:

def functionize(callable):
return lambda *args, **kwargs: callable(*args, **kwargs)

:)

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


Re: style question - hasattr

2008-04-08 Thread Miles
On Tue, Apr 8, 2008 at 10:21 PM, ian <[EMAIL PROTECTED]> wrote:
>  Ok, so what about 'hasattr'  ??
> hasattr(myObject,'property')
>  seems equivalent to
> 'property' in dir(myObject)
>
>  I would suggest that using the 'in' is cleaner in this case also. Is
>  there a performance penalty here? Or is there reason why the two are
>  not actually the same?

>>> class HasAll(object):
... def __getattr__(self, name): pass
...
>>> hasattr(HasAll(), 'spam')
True
>>> 'spam' in dir(HasAll())
False

>From the docs:  "Because dir() is supplied primarily as a convenience
for use at an interactive prompt, it tries to supply an interesting
set of names more than it tries to supply a rigorously or consistently
defined set of names, and its detailed behavior may change across
releases.  ... [hasattr] is implemented by calling getattr(object,
name) and seeing whether it raises an exception or not."
http://docs.python.org/lib/built-in-funcs.html

> Which style is preferred??

Don't test for the existence of the attribute if you're going to get
it when it exists; just go ahead and get it.

try:
x = myObject.property
except AttributeError:
x = None

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


Re: class definition

2008-05-07 Thread Miles
On Wed, May 7, 2008 at 7:40 PM, Yves Dorfsman <[EMAIL PROTECTED]> wrote:
> Does it make a difference if you put subclass object or not ?
>
>  What is the difference between c1 and c2 here:
>
>  class c1:
>   pass
>
>  class c2(object):
>   pass

>>> type(c1)

>>> type(c1())

>>> type(c2)

>>> type(c2())


In Python 2.2, classes and types were unified.  If a class inherits
from object (or any other built-in), it is considered a "new-style"
class; otherwise, it is an old-style (or classic) class.  There are
some differences in their behavior; most notably, descriptors
(computer properties) will not work with old-style classes.  Old-style
classes will go away in Python 3 (I think), and all classes will have
object as a base.

An introduction to new-style classes:
http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html
A guide to descriptors:
http://users.rcn.com/python/download/Descriptor.htm
The reference manual on the distinction:
http://docs.python.org/ref/node33.html
The technical explanation:
http://www.python.org/download/releases/2.2.3/descrintro/

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


Re: slicing lists

2008-05-07 Thread Miles
On Wed, May 7, 2008 at 7:46 PM, Ivan Illarionov
 <[EMAIL PROTECTED]> wrote:
 > On Wed, 07 May 2008 23:29:27 +, Yves Dorfsman wrote:
 >
 >  > Is there a way to do:
 >  > x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 >  > x[0,2:6]
 >  >
 >  > That would return:
 >  > [0, 3, 4, 5, 6]
 >
 >  IMHO this notation is confusing.
 >
 >  What's wrong with:
 >  [0]+x[2:6]

 I think Yves meant to return [1, 3, 4, 5, 6], as in Perl's list slicing:

 my @x = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
 return @x[0, 2..6]; // returns (1, 3, 4, 5, 6)

 This isn't incredibly efficient, but it does what you want (I think):

 from itertools import chain

 class multisliceable(list):
  def __getitem__(self, slices):
if isinstance(slices, (slice, int, long)):
  return list.__getitem__(self, slices)
else:
  return list(chain(*[list.__getitem__(self, s) if isinstance(s, slice)
  else [list.__getitem__(self, s)] for s in slices]))

 p = open('/etc/passwd')
 q = [multisliceable(e.strip().split(':'))[0,2:] for e in p]

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


Re: Getting pid of a remote process

2008-08-19 Thread Miles
On Mon, Aug 18, 2008 at 9:34 AM, srinivasan srinivas wrote:
> Could you please suggest me a way to find pid of a process started on a 
> remote machine by the current process??

If ps + grep (or the more robust tool pgrep) won't work because you're
running more than one instance at once, try this: instead of running
the process via SSH directly, write a small wrapper script which runs
the process locally and saves the PID to a file on the machine (or
prints it to stdout if the program itself has no output), and then run
that wrapper remotely.

For example:

import subprocess, sys
f = open(sys.argv[1], 'w')
p = subprocess.Popen(sys.argv[2:])
print >>f, p.pid
f.close()

You may have to be careful with the quoting of the arguments, since
OpenSSH uses a shell to execute the command.  You could then obtain
the pid with something along the lines of:

int(subprocess.Popen(['ssh', , 'cat', ],
stdout=subprocess.PIPE).stdout.read())

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


Re: os.readlink() doco snafu?

2008-08-20 Thread Miles
Fredrik Lundh wrote:
> JBW wrote:
>> I'm not sure readlink() lives up to its documentation. ...
>> The result makes sense, but it belies the documentation. Should I report
>> this?  Or is there another plausible interpretation?
>
> the part about joining with dirname(path) assumes that you pass in an
> absolute path to the readlink function.

That documentation should probably say, "if it is relative, it may be
converted to a pathname valid from the current working directory using
..."

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


Re: property() usage - is this as good as it gets?

2008-08-22 Thread Miles
On Fri, Aug 22, 2008 at 12:18 PM, David Moss <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I want to manage and control access to several important attributes in
> a class and override the behaviour of some of them in various
> subclasses.
>
> Below is a stripped version of how I've implemented this in my current
> bit of work.
>
> It works well enough, but I can't help feeling there a cleaner more
> readable way of doing this (with less duplication, etc).

It could certainly be made more concise, without changing the meaning:

###

from operator import attrgetter
class attrsetter(object):
def __init__(self, attr):
self._attr = attr
def __call__(self, object, value):
setattr(object, self._attr, value)
def defaultproperty(attr):
return property(attrgetter(attr), attrsetter(attr))

class A(object):
def __init__(self):
self._x = None
self._y = None

x = defaultproperty('_x')
y = defaultproperty('_y')

class B(A):
def __init__(self):
super(B, self).__init__()
self._z = None

def _set_x(self, x):
#   An example subclass 'set' override.
if x == 10:
raise Exception('%r is invalid!' % x)
self._x = x

x = property(A.x.fget, _set_x)
z = defaultproperty('_z')
# You really don't need to do this, but you can if you want
del _set_x

###

But, given the example you gave, you could also write things this way
(aside from no longer forbidding deleting the properties, which really
shouldn't be necessary):

###

class A(object):
def __init__(self):
self.x = None
self.y = None

class B(A):
def __init__(self):
super(B, self).__init__()
self._x = self.x
self.z = None

def _set_x(self, x):
#   An example subclass 'set' override.
if x == 10:
raise Exception('%r is invalid!' % x)
self._x = x

x = property(attrgetter('_x'), _set_x)

###

It's difficult to know exactly what would work best for you without a
better idea of how your properties override each other.

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


Re: Python multimap

2008-08-27 Thread Miles
brad wrote:
> There is only one '1' key in your example. I need multiple keys that are all
> '1'. I thought Python would have something built-in to handle this sort of
> thing.
>
> I need a true multimap ... without making K's value a list of stuff
> to append to.

That's what a multimap is.  If you really need the syntactic sugar,
it's simple to implement:

class multidict(dict):
  def __setitem__(self, key, value):
try:
  self[key].append(value)
except KeyError:
  dict.__setitem__(self, key, [value])

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


Re: Checking if the file is a symlink fails

2008-08-28 Thread Miles
saswat wrote:
> On Aug 28, 3:11 pm, Christian Heimes wrote:
>> [EMAIL PROTECTED] wrote:
>> > File symLinkTest is a symbolic link.
>>
>> > Why S_ISLNK(mode) returns False and S_ISREG(mode) returns True ?
>>
>> Because you are using os.stat() instead of os.lstat().
>
> Do you mean the following is deprecated ?
> http://docs.python.org/lib/module-stat.html
>
> >From the documentation -
>
> S_ISLNK( mode)
>Return non-zero if the mode is from a symbolic link.

No, it means that os.stat follows the symbolic link and gives stat
information for the file that the symbolic link points to, which is
*not* a symbolic link.  Like the documentation for lstat says: "Like
stat(), but do not follow symbolic links."  But why not just use
os.path.islink?

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


Re: help needed with dictionary

2008-08-29 Thread Miles
On Fri, Aug 29, 2008 at 5:02 AM, lee wrote:
> i wanted to input values  through command line and store the values in a
> dictionary. i mean for the same key , multiple values.

http://mail.python.org/pipermail/python-list/2008-August/505509.html
http://mail.python.org/pipermail/python-list/2008-August/505584.html

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


Re: Picking up resource forks and extended attributes on Mac OS X ?!

2008-08-30 Thread Miles
Mitrokhin wrote:
> Also (dare I ask for this too) If some nice soul could point me in the
> right direction for picking up files with extended attributes under OS
> X I'd be really gratefull to. Should it be done the same way, ie. via
> os. calls or perhaps by way of a popen or ... ?

Mac OS X 10.5 comes with a Python "xattr" package pre-installed, which
the xattr command-line utility is built on top of.  Furthermore, the
resource fork can be accessed as an extended attribute
(xattr.XATTR_RESOURCEFORK_NAME).  This might also be true of Mac OS X
10.4, but I don't recall for sure and I don't have access to it to
check.

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


Re: Inquiry regarding the name of subprocess.Popen class

2008-09-02 Thread Miles
Derek Martin wrote:
> On Tue, Sep 02, 2008 at 10:55:54PM +, Marc 'BlackJack' Rintsch wrote:
>> but the instances of `Popen` are no actions.  There's no way to
>> "execute" a `Popen` instance.
>
> Yes there is... you execute it when you instantiate the object.  At
> the time of instantiation, you "open" the "P" (pipes).

The subprocess module is also supposed to replace os.system and
os.spawn*, neither of which involve opening pipes. (I use it for that
functionality just as often, if not more so, as for piped subprocess
communication).  All rationalizations aside, I think Popen is a poor
name for the class.  But I would imagine the odds of it ever being
changed are miniscule (Python 4?).

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


Re: Why are "broken iterators" broken?

2008-09-21 Thread Miles
On Sun, Sep 21, 2008 at 11:13 AM, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> According to the Python docs, once an iterator raises StopIteration, it
> should continue to raise StopIteration forever. Iterators that fail to
> behave in this fashion are deemed to be "broken":
>
> http://docs.python.org/lib/typeiter.html
>
> I don't understand the reasoning behind this. As I understand it, an
> iterator is something like a stream. There's no constraint that once a
> stream is empty it must remain empty forever.
>
> Can somebody explain why "broken iterators" are broken?

It's not a terribly onerous restriction.  If you're iterating with a
for-loop, you can make the iterable return a new iterator object when
the old one is exhausted, and if the intent is for the next()-method
to be called directly, you don't have to conform to the iterator
protocol.

Strictly speaking, file objects are broken iterators:

Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:16)
>>> f = open('foo')
>>> it = iter(f)
>>> it.next()
'hi\n'
>>> it.next()
'bye\n'
>>> it.next()
Traceback (most recent call last):
  File "", line 1, in 
StopIteration
>>> f.seek(0)
>>> it.next()
'hi\n'

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


Re: dict generator question

2008-09-21 Thread Miles
On Fri, Sep 19, 2008 at 9:51 PM, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> Extending len() to support iterables sounds like a good idea, except that
> it's not.
>
> Here are two iterables:
>
>
> def yes():  # like the Unix yes command
>while True:
>yield "y"
>
> def rand(total):
>"Return random numbers up to a given total."
>from random import random
>tot = 0.0
>while tot < total:
>x = random()
>yield x
>tot += x
>
>
> What should len(yes()) and len(rand(100)) return?

Clearly, len(yes()) would never return, and len(rand(100)) would
return a random integer not less than 101.

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


Re: Borg vs Singleton vs OddClass

2008-09-27 Thread Miles
Lie wrote:
> This is probably unrelated to Python, as this is more about design
> pattern. I'm asking your comments about this design pattern that is
> similar in functionality to Singleton and Borg: to share states.
>
> I'm thinking about this design pattern (I don't know if anyone has
> ever thought of this pattern before):
>
> class OddClass(object):
>def __init__(self):
>global OddClass
>OddClass = self
>def __call__():

I'll change this to def __call__(self):

>return self
>
> The OddClass is a class that would overwrite its own class definition
> at its first instantiation. OddClass defines __call__ so that
> subsequent "instantiation" (technically it is no more an
> instantiation, but Duck Typing says it does) of the class would return
> the single instance.

This seems like a terrible idea to me, but then I never really
understood the appeal of the Singleton pattern, especially in Python.

> Singleton and OddClass is inheritable.
>
>>>> class OddClass(object): ...
>>>> s = OddClass()
>>>> class A(OddClass): pass

Traceback (most recent call last):
  File "", line 1, in 
TypeError: Error when calling the metaclass bases
__init__() takes exactly 1 argument (4 given)

Oops!  And assuming you carefully ordered your code so that OddClass
will never be instantiated before it is subclassed (which seems
fragile), you get behavior like this:

>>> s = OddClass()
>>> s is OddClass()
True
>>> a = A()
>>> s is OddClass()
False
>>> a is OddClass()
True
>>> a is A()
False
>>> a is OddClass()
False

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


Re: Urgent:Serial Port Read/Write

2013-05-09 Thread Frank Miles
On Thu, 09 May 2013 23:35:53 +0800, chandan kumar wrote:

> Hi all,I'm new to python and facing issue using serial in python.I'm
> facing the below error
>     ser.write(port,command)NameError: global name 'ser' is not defined
> Please find the attached script and let me know whats wrong in my script
> and also how can i read data from serial port for the  same script.
[snip]
> if __name__ == "__main__":
> 
> CurrDir=os.getcwd()
> files = glob.glob('./*pyc')
> for f in files:
> os.remove(f)
> OpenPort(26,9600)
> SetRequest(ER_Address)
> #SysAPI.SetRequest('ER',ER_Address)
> 
> print "Test Scripts Execution complete"

What kind of 'port' is 26?  Is that valid on your machine?  My guess is 
that  is NULL (because the open is failing, likely due to the port 
selection), leading to your subsequent problems.

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


Re: PDF generator decision

2013-05-14 Thread Frank Miles
On Tue, 14 May 2013 08:05:53 -0700, Christian Jurk wrote:

> Hi folks,
> 
> This questions may be asked several times already, but the development
> of relevant software continues day-for-day. For some time now I've been
> using xhtml2pdf [1] to generate PDF documents from HTML templates (which
> are rendered through my Django-based web application. This have been
> working for some time now but I'm constantly adding new templates and
> they are not looking like I want it (sometimes bold text is bold,
> sometimes not, layout issues, etc). I'd like to use something else than
> xhtml2pdf.
> 
> So far I'd like to ask which is the (probably) best way to create PDFs
> in Python (3)? It is important for me that I am able to specify not only
> background graphics, paragaphs, tables and so on but also to specify
> page headers/footers. The reason is that I have a bunch of documents to
> be generated (including Invoice templates, Quotes - stuff like that).
> 
> Any advice is welcome. Thanks.
> 
> [1] https://github.com/chrisglass/xhtml2pdf

Reportlab works well in Python 2.x.  Their _next_ version is supposed to 
work with Python3... {yes, not much help there}
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-08-14 Thread Robert Miles

On 7/10/2012 1:08 PM, Demian Brecht wrote:

I also judge candidates on their beards 
(http://www.wired.com/wiredenterprise/2012/06/beard-gallery/). If the beard's 
awesome enough, no questions needed. They're pro.


You should hire me quickly, then, since I have a beard, already turning 
partly gray.  Never mind that the computer languages I have studied

enough to write even one program don't yet include Python.

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


Re: lambda in list comprehension acting funny

2012-08-15 Thread Robert Miles

On 7/11/2012 11:39 PM, Dennis Lee Bieber wrote:

On 12 Jul 2012 03:53:03 GMT, Steven D'Aprano
 declaimed the following in
gmane.comp.python.general:



"ALU class"?

Googling gives me no clue.


Arithmetic/Logic Unit

http://en.wikipedia.org/wiki/Arithmetic_logic_unit
http://en.wikipedia.org/wiki/74181
{diversion: http://en.wikipedia.org/wiki/VAX-11/780 -- incredible...
that used to be considered a "super-mini" when I worked on them; and now
would be shamed by most laptops except for the ability to support so
many users concurrently (let me know when a Windows laptop supports 32
VT-100 class connections )}


Installing Cygwin (a Linux emulation) under Windows appears to add some 
VT-100 support but without an easy way to find documentation on whether

it can support 32 of them or not.

I used to work on a VAX 11/780 and also a VAX 8600.

Cygwin has a version of Python available, in case you're interested.

Robert Miles

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


Re: Encapsulation, inheritance and polymorphism

2012-08-18 Thread Robert Miles

On 7/23/2012 11:18 AM, Albert van der Horst wrote:

In article <[email protected]>,
Steven D'Aprano   wrote:

Even with a break, why bother continuing through the body of the function
when you already have the result? When your calculation is done, it's
done, just return for goodness sake. You wouldn't write a search that
keeps going after you've found the value that you want, out of some
misplaced sense that you have to look at every value. Why write code with
unnecessary guard values and temporary variables out of a misplaced sense
that functions must only have one exit?


Example from recipee's:

Stirr until the egg white is stiff.

Alternative:
Stirr egg white for half an hour,
but if the egg white is stiff keep your spoon still.

(Cooking is not my field of expertise, so the wording may
not be quite appropriate. )


--
Steven


Groetjes Albert


Note that you forgot applying enough heat to do the cooking.


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


  1   2   >