Re: Q for Emacs users: code-folding (hideshow)

2010-07-16 Thread thebjorn
On Jul 15, 10:34 pm, Peter peter.milli...@gmail.com wrote:
 On Jul 16, 2:45 am, kj no.em...@please.post wrote:

  This is a question _for Emacs users_ (the rest of you, go away :)  ).

  How do you do Python code-folding in Emacs?

  Thanks!

  ~K

[...]
 Anybody else now of any better ideas or whatever? Now that I think
 about it, I wouldn't mind using folding mode if I could make it
 easier to use myself! :-)

 Peter

I gave up too :-(

Komodo from ActiveState has Emacs bindings and folding, and is
generally a good editor and environment for when you want such a
thing. It also has an out-of-process debugger that has helped me solve
some nasty bugs.

Still, I keep going back to Emacs, it's just snappier(*) and
easier(**) to work with...  Instead of folding I either split the
window or create a new Frame (Ctrl 5 2) for the content I wish to
refer to.

-- bjorn


(*) ha!
(**) ...I know...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cannot send email

2010-07-16 Thread thebjorn
On Jul 15, 7:07 pm, D'Arcy J.M. Cain da...@druid.net wrote:
 On Thu, 15 Jul 2010 09:50:57 -0700 (PDT)

 G F gscotflem...@yahoo.com wrote:
  Does anyone have any ideas where the trouble is and what can be done
  about it? The little bit about:
  reply: retcode (557); Msg: This mail server does not accept mail
  addressed to aninterestedper...@yahoo.com
  seems to be telling but I do not know why the server would respond this way.

 This is really not a Python question but I think that you will find
 that the issue is that the server needs to know about the new IP
 address.  The error looks like an anti-relay thing.

 Contact the mail server admin or ask on a list dedicated to
 administrating email.


Jeff Atwood had a column about it recently

  
http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html

well worth a read.

-- bjorn

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


how to import a name from a module-path?

2009-06-16 Thread thebjorn
I'm storing the path to functions in a database and now I'd like to
get a reference so I can execute them.

I looked briefly at the imp module and got very confused...  Currently
I'm doing this:

  def import_object(path):
  module, obj = path.rsplit('.', 1)
  exec from rootpkg.%s import %s as fn % (module, obj)
  return fn

  function = import_object('mypackage.mymodule.myfunction')

this is happening in a trusted environment, so I'm not worried about
malicious code.

Are there more elegant ways of doing this (ie. without exec)?

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


Re: waling a directory with very many files

2009-06-16 Thread thebjorn
On Jun 15, 6:56 am, Steven D'Aprano
ste...@remove.this.cybersource.com.au wrote:
 On Sun, 14 Jun 2009 22:35:50 +0200, Andre Engels wrote:
  On Sun, Jun 14, 2009 at 6:35 PM, tomf...@thefsb.org wrote:
  i can traverse a directory using os.listdir() or os.walk(). but if a
  directory has a very large number of files, these methods produce very
  large objects talking a lot of memory.

  in other languages one can avoid generating such an object by walking a
  directory as a liked list. for example, in c, perl or php one can use
  opendir() and then repeatedly readdir() until getting to the end of the
  file list. it seems this could be more efficient in some applications.

  is there a way to do this in python? i'm relatively new to the
  language. i looked through the documentation and tried googling but
  came up empty.

  What kind of directories are those that just a list of files would
  result in a very large object? I don't think I have ever seen
  directories with more than a few thousand files...

 You haven't looked very hard :)

 $ pwd
 /home/steve/.thumbnails/normal
 $ ls | wc -l
 33956

 And I periodically delete thumbnails, to prevent the number of files
 growing to hundreds of thousands.

 Steven

Not proud of this, but...:

[django] www4:~/datakortet/media$ ls bfpbilder|wc -l
 174197

all .jpg files between 40 and 250KB with the path stored in a database
field... *sigh*

Oddly enough, I'm a relieved that others have had similar folder sizes
(I've been waiting for this burst to the top of my list for a while
now).

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


Re: how to import a name from a module-path?

2009-06-16 Thread thebjorn
On Jun 16, 7:43 pm, Gary Herron gher...@islandtraining.com wrote:
 thebjorn wrote:
  I'm storing the path to functions in a database and now I'd like to
  get a reference so I can execute them.

  I looked briefly at the imp module and got very confused...  Currently
  I'm doing this:

    def import_object(path):
        module, obj = path.rsplit('.', 1)
        exec from rootpkg.%s import %s as fn % (module, obj)
        return fn

    function = import_object('mypackage.mymodule.myfunction')

  this is happening in a trusted environment, so I'm not worried about
  malicious code.

  Are there more elegant ways of doing this (ie. without exec)?

 Yes.  Look at the __import__ builtin function,
[...]

Thanks, that is much better:

   def import_object(path):
   module, obj = path.rsplit('.', 1)
   m = __import__(module, fromlist=['rootpkg'])
   return getattr(m, obj)

   function = import_object('mypackage.mymodule.myfunction')


 Gary Herron

Bjorn

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


Re: Is there a SOAP module that can do this...?

2008-09-11 Thread thebjorn
On Sep 10, 9:44 pm, Waldemar Osuch [EMAIL PROTECTED] wrote:
 On Sep 10, 1:23 pm, thebjorn [EMAIL PROTECTED]
 wrote: I've been trying to use SOAPpy and ZSI (with and without the use of
  wsdl2py) to communicate with a SOAP server (looks like it's a WebLogic
  server(?) in front of some enterprise java bean) and not having much
  luck.  I got them to send me an example of what the bytes on the wire
  are supposed to look like (attached below), and I got it to work by
  going lo-tech:

 If you are willing to go low tech you can 
 tryhttp://effbot.org/downloads/#elementsoap

 But before you do that try:https://fedorahosted.org/suds
 It is actively maintained and holds a lot of promise.
 In my testing it knew how to connect to Sharepoint as well
 as WebLogic exposed services.

 Waldemar

Thanks for the info Waldemar. I'm looking into suds now, but there's
something I'm having trouble wrapping my head around (xml isn't my
usual territory, so this is perhaps obvious to someone...) This is
what suds tells me:

 print client
suds ( version=0.2.9 )

service ( InboundLegacyDataService )
prefixes:
ns0 = http://no/brreg/BReMS/WebService/services;
methods (2):
getInfo()
submitMessage(xs:string cpaid, xs:string securityKey,
xs:string message, )
types (4):
submitMessage
submitMessageResponse
getInfo
getInfoResponse

The method I'm interested in is submitMessage and in particular the
``xs:string message`` parameter.  I've been provided with three xsd
files that I'm almost 100% sure defines the format of the xml in the
message (it defines the JegerproveInn sub-structure), but it looks
like that has to be wrapped in a SOAP:Envelope, including the ?xml..
declaration before being stuffed into the xs:string message parameter,
before that in turn is wrapped in an env:Envelope... Am I on the right
track?

Another question:  I'm assuming the xsd files can be used for more
than documentation :-)  I've found the w3schools Introduction to XML
Schema which I'm starting to read right now, however I haven't been
able to google up any Python-xsd thingy that looked promising
(since I'm not sure what I'm looking for, this might not be a big
surprise ;-)  Is there such a thingy?

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


Is there a SOAP module that can do this...?

2008-09-10 Thread thebjorn
I've been trying to use SOAPpy and ZSI (with and without the use of
wsdl2py) to communicate with a SOAP server (looks like it's a WebLogic
server(?) in front of some enterprise java bean) and not having much
luck.  I got them to send me an example of what the bytes on the wire
are supposed to look like (attached below), and I got it to work by
going lo-tech:

cn = httplib.HTTPSConnection('test.xx.no',443)
cn.putrequest(POST, /service/url)
cn.putheader(Host, test.xx.no)
cn.putheader(Content-type, 'text/xml; charset=UTF-8')
cn.putheader(Content-length, %d % len(soap_message))
cn.putheader(SOAPAction, '')
cn.endheaders()
cn.send(soap_message)

Is there a better way to do this?  Can it be done with any of the
Python SOAP libraries?

Bjorn

soap message follows:

env:Envelope xmlns:env=http://schemas.xmlsoap.org/soap/envelope/;
  Header xmlns=http://schemas.xmlsoap.org/soap/envelope/; /
env:Body
submitMessage xmlns=http://no/brreg/BReMS/WebService/services;
  cpaidy111/cpaid
  securityKeyy222/securityKey
  message![CDATA[?xml version='1.0' encoding='ISO-8859-1'?
SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
SOAP:Header xmlns:eb='http://www.oasis-open.org/committees/ebxml-msg/
schema/msg-header-2_0.xsd'
eb:MessageHeader eb:id='1' eb:version='2.0' SOAP:mustUnderstand='1'
eb:From
  eb:PartyId eb:type='orgnr-submitter'x111/eb:PartyId
  eb:PartyId eb:type='orgnr-caseworker'x222/eb:PartyId
  eb:RoleSubmitter/eb:Role
/eb:From
eb:To
  eb:PartyId eb:type='orgnr-registrator'x111/eb:PartyId
  eb:RoleRegistrator/eb:Role
/eb:To
eb:CPAIdy111/eb:CPAId
eb:ConversationIdccd6dc26-836f-4768-8d28-5d46a872b9e8/
eb:ConversationId
eb:Service eb:type='lockup'x444/eb:Service
eb:ActionappendMessage/eb:Action
eb:MessageData
  eb:MessageIdccd6dc26-836f-4768-8d28-5d46a872b9e8/eb:MessageId
  eb:Timestamp23.09.2006/eb:Timestamp
/eb:MessageData
eb:Description xml:lang='no'/eb:Description
/eb:MessageHeader
/SOAP:Header
SOAP:Body
LegacyData
JegerproveInn xsi:noNamespaceSchemaLocation=JegerproveInn.xsd
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance;
Arrangor
   ArrangorId3/ArrangorId
   ArrangorNavn
  Navn1BBB/Navn1
  Navn5/Navn5
   /ArrangorNavn
   PersonNavn
  FornavnCCC/Fornavn
  EtternavnDDD/Etternavn
   /PersonNavn
   Kommunenr/Kommunenr
   Telefon12345678/Telefon
   Epost[EMAIL PROTECTED]/Epost
/Arrangor
 /JegerproveInn/LegacyData/SOAP:Body/SOAP:Envelope]]
/message
/submitMessage
  /env:Body
/env:Envelope
--
http://mail.python.org/mailman/listinfo/python-list


Re: Class definition attribute order

2008-08-09 Thread thebjorn
On Aug 9, 7:55 am, Michele Simionato [EMAIL PROTECTED]
wrote:
 On Aug 5, 5:05 am, Michele Simionato

  Yep. Seehttp://stacktrace.it/articoli/2008/01/metaclassi-python-3000
  (I am working on an English translation these days,
  but for the moment you can use Google Translator).

   M. Simionato

 FWIW, I have just finished translating the first
 part of the article and I have posted it on my
 blog on Artima:

 http://www.artima.com/weblogs/viewpost.jsp?thread=236234

Great feature and great article!  I haven't used ABCs yet, so my
initial instinct would be to derive odict from dict (it would obviate
the conversions in the metaclass). Are you using ABCs just to play
with all the new toys at the same time? ;-)

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


Re: Hyphenation module PyHyphen-0.3 released

2008-02-25 Thread thebjorn
On Feb 25, 3:56 am, Ivan Illarionov [EMAIL PROTECTED] wrote:
  I don't know if I'm just doing it wrong, or if I can't use extensions
  compiled with mingw32 with the standard distribution Python
  executable?

 I was able to install on Windows XP with mingw:

 setup.py build -c mingw32
 setup.py install --skip-build

 It works fine with English dictionary, but hangs with Russian
 dictionary from Mozilla.

This is unfortunately deceptive :-(  The C-runtime mismatch problem
happens when memory malloc'd in an extension module is free'd by the
interpreter (or vice versa). Things might work for a while with this
mismatch, but will (almost) always crash/hang/etc. in hard to track
down ways later.

Using the link that max provided above, I managed to build and install
without passing any switches and getting no error messages.

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


Re: Hyphenation module PyHyphen-0.3 released

2008-02-23 Thread thebjorn
On Feb 23, 8:35 pm, Dr. leo [EMAIL PROTECTED] wrote:
 I am pleased to share with you the great features of the latest version.
 Large parts of the sources were completely rewritten. Also, they are now
 reasonably documented.

 Just go tohttp://pypi.python.org/pypi/PyHyphen/0.3

 I was tempted to classify it as Beta. Indeed I am not aware of any bugs, but
 I haven't spent very much time for testing, just ran some word lists...

 Any feedback is greatly appreciated. Especially I would be interested in
 experiences under Windows. I can only test it under Linux.

 If there were a good soul to send me a DLL for Windows
 ([EMAIL PROTECTED]) , this would be terrific.

 Bests

 Leo

Looks interesting, and I'd love to provide a .pyd file, however I'm
running into some problems :-(   I've got VS 2005 on this machine and
setup-tools is complaining about my Python being built with VS 2003.
(I know what the problem is with intermingling runtime libraries, yet
I can't downgrade my VS version...)  I got a message that I could try
using mingw32, so a quick download and the build step seemed to finish
without any problems:

  C:\work\PyHyphen-0.3python setup.py build -c mingw32
  running build
  running build_py
  running build_ext
  running build_scripts
  creating build\scripts-2.5
  copying example.py - build\scripts-2.5

(I ran build_ext first, which is why there's so little output.) I
copied the dictionary into build\lib.win32-2.5\dict and was able to
import the library without getting any errors and run the example from
http://pypi.python.org/pypi/PyHyphen/0.3 (from the build directory).
I'm stuck at trying to install the package though...:

  C:\work\PyHyphen-0.3python setup.py install
  running install
  running build
  running build_py
  running build_ext
  error: Python was built with Visual Studio 2003;
  extensions must be built with a compiler than can generate
compatible binaries.
  Visual Studio 2003 was not found on this system. If you have Cygwin
installed,
  you can try compiling with MingW32, by passing -c mingw32 to
setup.py.

  C:\work\PyHyphen-0.3python setup.py install -c mingw32
  usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2
[cmd2_opts] ...]
 or: setup.py --help [cmd1 cmd2 ...]
 or: setup.py --help-commands
 or: setup.py cmd --help

  error: invalid command 'mingw32'

I don't know if I'm just doing it wrong, or if I can't use extensions
compiled with mingw32 with the standard distribution Python
executable?

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


Re: Simple - looking for a way to do an element exists check..

2008-02-23 Thread thebjorn
On Feb 23, 6:18 pm, Paul Hankin [EMAIL PROTECTED] wrote:
 On Feb 22, 7:01 pm, Paul McGuire [EMAIL PROTECTED] wrote:

  On Feb 22, 12:54 pm, Paul Rubin http://[EMAIL PROTECTED] wrote:

   Paul Rubin http://[EMAIL PROTECTED] writes:
if any(x==element[0] for x in a):
  a.append(element)

   Should say:

if any(x[0]==element[0] for x in a):
   a.append(element)

  I think you have this backwards.  Should be:

   if not any(x[0]==element[0] for x in a):
  a.append(element)

 IMO Jason's solution of testing containment in a generator is better
 (more readable).
 if element[0] not in (x[0] for x in a):
 a.append(element)

 --
 Paul Hankin

It may be more readable (although that's debatable), but it always
traverses the entire list.

If the list is short I'd use either the traditional for/else or the
newer if all(...):

if all(x[0] != element[0] for x in a):
a.append(element)

which allows for short-circuit evaluation -- I generally try to stay
away from negating any() and all() because the logic often gets
convoluted.

If the lists are long enough to care, either rewrite use a set-based
solution if the items are hashable, or keep the elements sorted and
rewrite to use the bisect module if the elements can be ordered.

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


Re: Simple - looking for a way to do an element exists check..

2008-02-23 Thread thebjorn
On Feb 24, 2:24 am, Paul Rubin http://[EMAIL PROTECTED] wrote:
 thebjorn [EMAIL PROTECTED] writes:
  If the lists are long enough to care, either rewrite use a set-based
  solution if the items are hashable, or keep the elements sorted and
  rewrite to use the bisect module if the elements can be ordered.

 Well, you want a tree data structure to have fast insertions to
 remember what items have already been seen.  There's been a bunch
 of proposals for an ordered dict type that works like this.

 Some day...

Amen!  .. *sigh*...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is this data structure build-in or I'll have to write my own class?

2008-02-20 Thread thebjorn
On Feb 20, 3:32 pm, Jorge Vargas [EMAIL PROTECTED] wrote:
 On Feb 20, 2008 8:15 AM, Larry Bates [EMAIL PROTECTED] wrote:

  Jorge Vargas wrote:
   I need a data structure that will let me do:

   - attribute access (or index)
   - maintain the order (for iter and print)
   - be mutable.

[...]

  Sounds like a good time to learn ElementTree (included in Python 2.5 but
  available for earlier versions).

 I am using ET, to fetch the data from the XML, after that I want a
 plain python object. for the rest of the program.

Ok, you just lost me... Why do you thin ET is not appropriate (*)?  It
fits all your requirements, is optimized for representing hierarchical
data (especially xml), it is fast, it is well tested, it has a
community of users, it is included in the standard library, etc., etc.

...maybe I didn't grok what you meant by plain python object?

-- bjorn

(*) I just had a flashback to the movie ET -- the scene when he's in
the closet ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-18 Thread thebjorn
On Feb 15, 8:55 pm, Jeff Schwab [EMAIL PROTECTED] wrote:
 W. Watson wrote:
  See Subject. It's a simple txt file, each line is a Python stmt, but I
  need up to four digits added to each line with a space between the
  number field and the text. Perhaps someone has already done this or
  there's a source on the web for it. I'm not yet into files with Python.
  A sudden need has burst upon me. I'm using Win XP.

 i = 0
 for line in sys.stdin:
  i += 1
  print i, line,

 Or if you want consistent alignment:

 i = 0
 for line in sys.stdin:
  i += 1
  print %4s % i, line,

I like your version best (it's very clean and easy to understand), but
here's a few more versions...

  from itertools import count

  for i, line in zip(count(1), open('filename.txt')):
  print i, line,

or with consistent alignment:

  for x in zip(count(1), open('filename.txt')):
  print %4d %s % x,

the latter version gives rise to a one-liner

  open('output.txt','w').writelines('%4d %s' % x for x in
zip(count(1), open('perms.py')))

but as I said, I like the simple for loop the best ;-)

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


Re: Python Memory Manager

2008-02-17 Thread thebjorn
On Feb 17, 10:01 pm, Pie Squared [EMAIL PROTECTED] wrote:
[...]
 It seems to me that another, perhaps better strategy, would be to
 allocate a large heap space, then store a pointer to the base of the
 heap, the current heap size, and the beginning of the free memory.
 When you need to 'allocate' more room, just return a pointer to some
 location in the heap and increment the start-of-free-memory pointer.
 That way, allocation really IS free, more or less. Wouldn't that be
 more efficient? Perhaps I'm missing something.

Deallocation?

 As a side note, I'm new to Usenet, so I'm not exactly sure... are
 'tangents' like this - since this IS a Python newsgroup, after all -
 okay?

It varies depending on the group, c.l.py is pretty tolerant as long as
it's interesting ;-)

To bring it back to Python, I was under the impression that the GC was
a generational collector and not a simple mark-sweep, but it's been a
while since I read about it...

-- bjorn

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


Re: Python equivt of __FILE__ and __LINE__

2008-02-11 Thread thebjorn
On Feb 11, 4:55 pm, Gary Herron [EMAIL PROTECTED] wrote:
 Bill Davy wrote:
  Writing a quick and dirty assembler and want to give the user the location
  of an error.  The assembly language is Python.  If the user wants to
  generat some object code they write something  like:

  Label(LoopLable)
  Load(R4)
  Dec()
  JNZ(LoopLabel)

  I can use Python to do all the expression evalutaion, conversion from Python
  FP to target FP, include files, macros (done as function definitions).  The
  functions like Load() generate the approproyte object code.

  So, for example, when a label is defined or referenced, I save the File,Line
  so if there is not exactly one defintion or no references, I can report the
  file location(s) to be considered.  In the example, I would want to report
  that LoopLable is not referenced, and LoopLabel is not defined.

  TIA,
  Bill

  PSwww.SynectixLtd.comis not relevant

 You *can* get at that kind of information: The traceback module has a
 function called extract_stack which can give you a pointer to the
 whole execution stack.  From that can be generated all the usual stuff
 you see in a traceback -- including file and line information.  *How*
 you extract that stuff, I'll leave as an exercises for the reader.
 (Meaning I haven't a clue.)

 Gary Herron

I think the inspect module might be more useful... the getfile() and
getsourcelines() look promising.

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


Re: translating Python to Assembler

2008-02-11 Thread thebjorn
On Jan 27, 12:23 pm, [EMAIL PROTECTED] wrote:
me:
 go troll somewhere else (you obviously don't know anything about
 assembler and don't want to learn anything about Python).

 -- bjorn

 before you start mouthing off, maybe you should learn assembler.

I suppose I shouldn't feed the trolls... but what the heck ;-P   I
could of course try to be helpful, but I don't think I have the skillz
needed.

I might know a thing or two about assembly though, I started out on
the Commodore 64, then I wrote TSR programs (both .com and .exe ;-)
for my IBM AT, and I wrote a compiler for a scheme-like functional
language (with SML-like syntax) that targeted the Motorola 68040
(which was inside my NeXTstation...).

[snip]

 Don't fucking tell me about assembler, you asshole. I can read
 disassembled code in my sleep.

Watch the language, fucktard. Perhaps you should try _writing_
something in assembly for a change? How about linking up a hello
world executable? You seem too clueless to be for real though, so my
original advice stands.

-- bjorn



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


Re: Is there an easy way to sort a list by two criteria?

2008-02-11 Thread thebjorn
On Feb 11, 10:47 am, [EMAIL PROTECTED] wrote:
[...]
 A little known thing from Python 2.5:
[...]
  sorted(lst, key=itemgetter(2, 1))

Cute, thanks :-)

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


Re: Is there an easy way to sort a list by two criteria?

2008-02-09 Thread thebjorn
On Feb 10, 3:05 am, neocortex [EMAIL PROTECTED] wrote:
 Hello!
 I am a newbie in Python. Recently, I get stuck with the problem of
 sorting by two criteria. In brief, I have a two-dimensional list (for
 a table or a matrix). Now, I need to sort by two columns, but I cannot
 figure out how to do that. I read somewhere that it is possible to do: 
 table.sort().sort()

 but it does not work.
 Can anyone help me with this?
 PS: I am using Python under Ubuntu 6.06.

 Best,
 PM

I'm not sure which Python is default for Ubuntu 6.06, but assuming you
can access a recent one (2.4), the list.sort() function takes a key
argument (that seems to be rather sparsely documented in the tutorial
and the docstring...). E.g.:

 lst = [(1,2,4),(3,2,1),(2,2,2),(2,1,4),(2,4,1)]
 lst.sort(key=lambda (a,b,c):(c,b))
 lst
[(3, 2, 1), (2, 4, 1), (2, 2, 2), (2, 1, 4), (1, 2, 4)]


The fancy lambda simply takes a source-tuple and returns a tuple of
the keys to be sorted on, in this case sort on the last element, then
on the middle element.

You can use the cmp argument to get similar results (with the same
list as above):

 lst.sort(cmp=lambda (a1,a2,a3),(b1,b2,b3):cmp((a3,a2),(b3,b2)))
 lst
[(3, 2, 1), (2, 4, 1), (2, 2, 2), (2, 1, 4), (1, 2, 4)]

The lambda in this case is starting to get complicated though, so
probably better to write a separate function. Using the cmp argument
is slower than using the key argument since key is only called once
for each element in the list while cmp is called for each comparison
of elements (it's not the number of times the function is called
that's the big deal, but rather that the highly optimized sort needs
to continually call back to Python in the cmp case).

The old-school way of doing this is using a Schwartzian transform
(a.k.a. decorate-sort-undecorate) which creates an auxilliary list
with the keys in correct sorting order so that sort() can work
directly:

 lst
[(1, 2, 4), (3, 2, 1), (2, 2, 2), (2, 1, 4), (2, 4, 1)]
 decorate = [(x[2],x[1],x) for x in lst]
 decorate.sort()
 decorate
[(1, 2, (3, 2, 1)), (1, 4, (2, 4, 1)), (2, 2, (2, 2, 2)), (4, 1, (2,
1, 4)), (4, 2, (1, 2, 4))]
 lst = [x[2] for x in decorate]  # undecorate
 lst
[(3, 2, 1), (2, 4, 1), (2, 2, 2), (2, 1, 4), (1, 2, 4)]


hth,
-- bjorn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using a dict as if it were a module namespace

2008-01-27 Thread thebjorn
On Jan 27, 8:45 am, Steven D'Aprano [EMAIL PROTECTED]
cybersource.com.au wrote:
 I have a problem which I think could be solved by using a dict as a
 namespace, in a similar way that exec and eval do.

 When using the timeit module, it is very inconvenient to have to define
 functions as strings. A good alternative is to create the function as
 normal, and import it:

 def myfunc(x, y):
 return x+y

 timeit.Timer(myfunc(59, 60), from __main__ import myfunc).timeit()

 Not only is this an easy idiom to follow, but myfunc can live in another
 module: just replace __main__ with the module name.

 Now, I'm trying to build a suite of tests to use with timeit. I have a
 bunch of tests which I've created as dicts:

 test_suite= [dict(x=59, y=60), dict(x=-1, y=-2)]

 What I *think* I want to do is use the from ... import idiom to grab
 arguments from the dicts as if they were modules, but this doesn't work:

 expr = myfunc(x, y)
 for test in test_suite:
 setup = from __main__ import myfunc; from test import x, y
 t = timeit.Timer(expr, setup).timeit()

 Even if the Timer could see test, it is not a module and you can't import
 from it. Naturally.

 Alternatives that I have found:

 (1) Import the test and grab the values needed from it:

 setup = from __main__ import myfunc, test
 x, y = test['x'], test['y']

 I don't like this one. It doesn't seem very elegant to me, and it gets
 unwieldy as the complexity increases. Every item I need from test has to
 be named twice, violating the principle Don't Repeat Yourself. If the
 tests change, the setup string has to be explicitly changed also.

 (2) Mess with the global namespace:

 globals().update(t)
 setup = from __main__ import myfunc

 I don't like this one. It looks hackish, and I worry about conflicts and
 side-effects. If it works (and I haven't tested it) it relies on an
 implementation detail of timeit.Timer.__init__, namely the line
 exec code in globals(), ns. Worst of all, it pollutes or even mangles
 the global namespace of the calling code, not the code being tested.

 (3) Explicitly pass a namespace dict to the Timer class, possibly even
 getting rid of setup altogether:

 test['myfunc'] = myfunc
 t = timeit.Timer(expr, '', ns=test).timeit()

 This would be the most elegant solution, but at this time it is
 completely hypothetical. Timer does not have that functionality.

 (4) Dump the test data straight into the setup string:

 setup = from __main__ import myfunc; x = %(x)s; y = %(y)s % t

 Again, unwieldy and against DRY. The additional disadvantage is that
 there are many types of test data that can't be converted to and from
 strings like that.

 What do others think? Have I missed something? What other alternatives
 are there?

 --
 Steven

You might have lost me, but wouldn't it be easier to do some variation
on this

test_suite = [
'(x=59, y=60)',  # either have strings here...
'(x=-1, y=-2)',
]

for test in test_suite:
# ... or convert the dicts to appropriate strings here...
expr = 'myfunc' + test
t = timeit.Timer(expr, 'from __main__ import myfunc').timeit()
...

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


Re: file write question

2008-01-27 Thread thebjorn
On Jan 27, 4:02 am, Robb Lane (SL name) [EMAIL PROTECTED] wrote:
 I have written a script which:
 - opens a file
 - does what it needs to do, periodically writing to the file... for a
 few hours
 - then closes the file when it's done
 So my question is:
 Would it be better to 'open' and 'close' my file on each write cycle?
 e.g.
 def writeStuff(content):
  myFile = open('aFile.txt', 'a+')
  myFile.write(content)
  myFile.close()

 ... or just leave it till it's done?
 I don't need to use the file contents until the script is done
 (although it would be nice... just to check it while it's working), so
 just curious what people think is the better method.
 - rd

Sounds like a classic case for a database to me (long running process
producing sporadic output that you might want to tinker with as it's
being produced). Python 2.5 comes with sqlite3 included...

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


Re: translating Python to Assembler

2008-01-27 Thread thebjorn
On Jan 27, 9:58 am, [EMAIL PROTECTED] wrote:
 On Fri, 25 Jan 2008 17:44:07 -0800 (PST), ajaksu [EMAIL PROTECTED]
 wrote:



 On Jan 25, 11:36 pm, ajaksu [EMAIL PROTECTED] wrote:
  On Jan 25, 11:10 pm, [EMAIL PROTECTED] wrote:
 [...]

 Gaah, is this what's going on?

 [EMAIL PROTECTED]:~$ cat error.txt
 This is not assembler...

 [EMAIL PROTECTED]:~$ ndisasm error.txt
   54push sp
 0001  686973push word 0x7369
 0004  206973and [bx+di+0x73],ch
 0007  206E6Fand [bp+0x6f],ch
 000A  7420  jz 0x2c
 000C  61popa
 000D  7373  jnc 0x82
 000F  656D  gs insw
 0011  626C65bound bp,[si+0x65]
 0014  722E  jc 0x44
 0016  2Edb 0x2E
 0017  2Edb 0x2E
 0018  0Adb 0x0A

 :/

 not sure what you're saying. Sure looks like assembler to me. Take the
 '54   push sp'. The 54 is an assembler opcode for push and the sp is
 the stack pointer, on which it is operating.

go troll somewhere else (you obviously don't know anything about
assembler and don't want to learn anything about Python).

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


Re: super, decorators and gettattribute

2008-01-14 Thread thebjorn
On Jan 14, 1:41 pm, Richard Szopa [EMAIL PROTECTED] wrote:
 On Jan 13, 3:31 pm, thebjorn [EMAIL PROTECTED]
 wrote:

  They do, except for when it comes to what super(..) returns. It isn't
  really an object in the sense that they're presented in the tutorial,
  but rather a sort of proxy to the methods in the ancestor classes of
  the concrete object (self), relative to the current method's class. I
  can't imagine that sentence would ease any confusion however, suffice
  it to say that you have to call getattr(super(..), 'name') instead of
  super(..).__getattr__('name') and you have to call super(..).__len__()
  instead of len(super(..)) -- I can't imagine that lessens any
  confusion either :-/

 Surprisingly, I think your first sentence *does* make something more
 clear. Let me check if I understand it right: when we call a method on
 super(Foo, self) it is as if we were calling call-next-method in
 Common Lisp or Dylan

I don't remember if CLOS was changed to use C3 Linearization also, but
the concept came from Dylan (http://www.webcom.com/haahr/dylan/
linearization-oopsla96.html) and that's what is implemented in Python.

[...]
 However, there's one piece that doesn't completely fit to the puzzle:
 why does getattr work? The help says:

 getattr(...)
 getattr(object, name[, default]) - value

 Get a named attribute from an object; getattr(x, 'y') is
 equivalent to x.y. When a default argument is given, it
 is returned when the attribute doesn't exist; without it,
 an exception is raised in that case.

 Does it work on the basis that getattr(x, 'y') is equivalent to x.y?
 What is then a named attribute for an object in Python? It seems not
 to be equivalent to the value of the item whose name is 'y' in the
 object's class __dict__...

Conceptually, x.y is always get the y attribute of x and the same as
getattr(x, 'y'). Depending on the type of x and y, and your
familiarity with Python internals, what actually happens during a
lookup might be surprising. In the vast majority of cases however, x.y
is equivalent to one of

  x.__dict__['y']
  type(x).__dict__['y']

but if you're a language geek like me, you might be excited that in
some cases it is

  type(x).__dict__['y'].__get__(x, type(x))

which says you get the value of x.y by calling y and passing x as an
argument -- if you know CLOS you'll recognize that it's a primitive
multi-method call. (there are some other special cases too, although
not as exciting ;-)

Much more detail can be found in Raymond's paper on descriptors
(http://users.rcn.com/python/download/Descriptor.htm) and Michele's
paper on super (http://www.phyast.pitt.edu/~micheles/python/
super.html).

-- bjorn


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


Re: Simple List division problem

2008-01-13 Thread thebjorn
On Jan 12, 8:33 pm, Fredrik Lundh [EMAIL PROTECTED] wrote:
 marcstuart wrote:
  How do I divide a list into a set group of sublist's- if the list is
  not evenly dividable ?  consider this example:

  x = [1,2,3,4,5,6,7,8,9,10]
  y = 3  # number of lists I want to break x into
  z = y/x

  what I would like to get is 3 sublists

  print z[0] = [1,2,3]
  print z[2] = [4,5,6]
  print z[3] = [7,8,9,10]

  obviously not even, one list will have 4 elements, the other 2 will
  have 3.,

 here's one way to do it:

 # chop it up
 n = len(x) / y
 z = [x[i:i+n] for i in xrange(0, len(x), n)]

 # if the last piece is too short, add it to one before it
 if len(z[-1])  n and len(z)  1:
  z[-2].extend(z.pop(-1))

 /F

Eh...

def chop(lst, length):
n = len(lst) / length
z = [lst[i:i+n] for i in xrange(0, len(lst), n)]
if len(z[-1])  n and len(z)  1:
 z[-2].extend(z.pop(-1))
return z

gives

 chop(range(1,9), 3)
[[1, 2], [3, 4], [5, 6], [7, 8]]
 chop(range(1,8), 3)
[[1, 2], [3, 4], [5, 6, 7]]
 chop(range(1,6), 3)
[[1], [2], [3], [4], [5]]
 chop([1], 3)
Traceback (most recent call last):
  File stdin, line 1, in module
  File beforemeth.py, line 9, in chop
if len(z[-1])  n and len(z)  1:
ValueError: xrange() arg 3 must not be zero

Perhaps something like this?

def chop(lst, length):
from itertools import islice
it = iter(lst)
z = [list(islice(it, length)) for i in xrange(1 + len(lst) //
length)]
if len(z)  1:
z[-2].extend(z.pop()) # the last item will be empty or contain
overflow elements.
return z

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


Re: Simple List division problem

2008-01-13 Thread thebjorn
On Jan 13, 1:05 pm, thebjorn [EMAIL PROTECTED]
wrote:
 On Jan 12, 8:33 pm, Fredrik Lundh [EMAIL PROTECTED] wrote:



  marcstuart wrote:
   How do I divide a list into a set group of sublist's- if the list is
   not evenly dividable ?  consider this example:

   x = [1,2,3,4,5,6,7,8,9,10]
   y = 3  # number of lists I want to break x into
   z = y/x

   what I would like to get is 3 sublists

   print z[0] = [1,2,3]
   print z[2] = [4,5,6]
   print z[3] = [7,8,9,10]

   obviously not even, one list will have 4 elements, the other 2 will
   have 3.,

  here's one way to do it:

  # chop it up
  n = len(x) / y
  z = [x[i:i+n] for i in xrange(0, len(x), n)]

  # if the last piece is too short, add it to one before it
  if len(z[-1])  n and len(z)  1:
   z[-2].extend(z.pop(-1))

  /F

 Eh...

 def chop(lst, length):
 n = len(lst) / length
 z = [lst[i:i+n] for i in xrange(0, len(lst), n)]
 if len(z[-1])  n and len(z)  1:
  z[-2].extend(z.pop(-1))
 return z

 gives

  chop(range(1,9), 3)

 [[1, 2], [3, 4], [5, 6], [7, 8]] chop(range(1,8), 3)

 [[1, 2], [3, 4], [5, 6, 7]] chop(range(1,6), 3)

 [[1], [2], [3], [4], [5]] chop([1], 3)

 Traceback (most recent call last):
   File stdin, line 1, in module
   File beforemeth.py, line 9, in chop
 if len(z[-1])  n and len(z)  1:
 ValueError: xrange() arg 3 must not be zero

 Perhaps something like this?

 def chop(lst, length):
 from itertools import islice
 it = iter(lst)
 z = [list(islice(it, length)) for i in xrange(1 + len(lst) //
 length)]
 if len(z)  1:
 z[-2].extend(z.pop()) # the last item will be empty or contain
 overflow elements.
 return z

 -- bjorn

Bad for to reply to myself, I know, but I just realized that the OP
wanted to control the _number_ of chunks, not the size of the
chunks... Building on the above would give something like

from itertools import islice
from operator import add

def chop(lst, nchunks):
chunksize, extra = divmod(len(lst), nchunks)
if not chunksize:
raise ValueError('More chunks than elements in list.')
it = iter(lst)
z = [list(islice(it, chunksize)) for i in xrange(nchunks + extra)]
z, extra = z[:nchunks], z[nchunks:]
z[-1].extend(reduce(add, extra, []))  # because sum ain't add :-(
return z

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


Re: Simple List division problem

2008-01-13 Thread thebjorn
On Jan 13, 2:02 pm, Fredrik Lundh [EMAIL PROTECTED] wrote:
 thebjorn wrote:

   Eh...

 oh, forgot that it was pulling requirements out of thin air week on
 c.l.python.

Well, the OP requirements were to control the number of chunks, not
the size of them, so I guess we both got it wrong initially.

  def chop(lst, length):
  n = len(lst) / length
  z = [lst[i:i+n] for i in xrange(0, len(lst), n)]
  if len(z[-1])  n and len(z)  1:
   z[-2].extend(z.pop(-1))
  return z

  gives
  chop([1], 3)
  Traceback (most recent call last):
File stdin, line 1, in module
File beforemeth.py, line 9, in chop
  if len(z[-1])  n and len(z)  1:
  ValueError: xrange() arg 3 must not be zero

 well, it doesn't.  there's no xrange on that line.

It's from this line

z = [lst[i:i+n] for i in xrange(0, len(lst), n)]

(I executed the file with python -i beforemeth.py to get to an
interactive prompt, I'm sure you're familiar with the technique. You
could have just debugged your own program to find it though, or just
looked at the code -- not that many xrange calls in there, eh?)

   Perhaps something like this?

   from itertools import islice

 or just use an if-statement, or the max function.  but I guess those
 tools are too old and boring for c.l.python these days...

I didn't realize correct code was too new-school. Perhaps you should
publish a list of modules you don't like, or perhaps I should just use
a sufficiently venerable version of Python? Ok, here you go:

C:\Python22python
'import site' failed; use -v for traceback
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
win32
Type help, copyright, credits or license for more information.
 def chop2(lst, nchunks):
... chunksize = len(lst) // nchunks
... if not chunksize:
... raise ValueError('More chunks than elements in list.')
... res = []
... begin, end = 0, chunksize
... for i in range(nchunks-1):
... res.append(lst[begin:end])
... begin, end = end, end+chunksize
... res.append(lst[begin:])
... return res
...
 chop2(range(1,6), 2)
[[1, 2], [3, 4, 5]]
 chop2(range(1,6), 3)
[[1], [2], [3, 4, 5]]
 chop2(range(1,11), 3)
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]


Sufficiently old-school (or do I need to take out the // division
also?)

 /F

Shall we perhaps drop some of the attitude? (you used to be so much
nicer before you wrote sre ;-)

-- bjorn

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


Re: super, decorators and gettattribute

2008-01-13 Thread thebjorn
On Jan 13, 1:51 pm, Richard Szopa [EMAIL PROTECTED] wrote:
 On Jan 13, 8:59 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:

  On Sat, 12 Jan 2008 14:23:52 -0800, Richard Szopa wrote:
   However, I am very surprised to learn that

   super_object.__getattr__(name)(*args, **kwargs)

   getattr(super_object, name)(*args, **kwargs)

   are not equivalent. This is quite odd, at least when with len()
   and .__len__, str() and .__str__. Do you maybe know what's the
   rationale behind not following that convention by getattr?

  I think you are confusing `__getattr__` and `__getattribute__` here!
  `getattr()` maps to `__getattr__()`, it's `__getattribute__` that's
  different.

 Well, in my code calling super_object.__getattr__(name)(*args,
 **kwargs) and getattr(super_object, name)(*args, **kwargs) gives
 *different* effects (namely, the latter works, while the former
 doesn't). That kinda suggests that they don't map to each other :-).
 And that makes me feel confused.

 Cheers,

 -- Richard

They do, except for when it comes to what super(..) returns. It isn't
really an object in the sense that they're presented in the tutorial,
but rather a sort of proxy to the methods in the ancestor classes of
the concrete object (self), relative to the current method's class. I
can't imagine that sentence would ease any confusion however, suffice
it to say that you have to call getattr(super(..), 'name') instead of
super(..).__getattr__('name') and you have to call super(..).__len__()
instead of len(super(..)) -- I can't imagine that lessens any
confusion either :-/

super(..) is designed to handle situations like this correctly

class Root(object):
n = 1

class Left(Root):
def foo(self):
print 'n =', self.n
print 'super n = ', super(Left, self).n

class Right(Root):
n = 2

class Leaf(Left,Right):
n = 3

x = Leaf()
x.foo()

the correct output is

n = 3
super n =  2


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


Re: How to get user home directory on Windows

2008-01-13 Thread thebjorn
On Jan 12, 6:50 pm, Giampaolo Rodola' [EMAIL PROTECTED] wrote:
 Update.
 I found a way for getting the home directory of the user but it
 requires to validate the user by providing username+password:

 def get_homedir(username, password):
 token = win32security.LogonUser(
 username,
 None,
 password,
 win32security.LOGON32_LOGON_NETWORK,
 win32security.LOGON32_PROVIDER_DEFAULT
 )
 return win32profile.GetUserProfileDirectory(token)

 What I'd like to do is avoiding the requirement of the password, the
 same way as if I would on UNIX where it would be enough just using the
 pwd module:

   import pwd
   pwd.getpwnam('user').pw_dir
  '/home/user'

Check out http://msdn2.microsoft.com/en-us/library/bb762181(VS.85).aspx
for some of the complexities of special directories on Windows.

If you give more details on what you need to get done, someone might
come up with a better solution (my instinct tells me this might be a
database problem, but then I'm a database person so that might not be
terribly relevant ;-)

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


Re: Bizarre behavior with mutable default arguments

2007-12-30 Thread thebjorn
On Dec 29, 7:17 pm, Istvan Albert [EMAIL PROTECTED] wrote:
 On Dec 29, 12:50 pm, bukzor [EMAIL PROTECTED] wrote:

  Is this functionality intended? It seems very unintuitive. This has
  caused a bug in my programs twice so far, and both times I was
  completely mystified until I realized that the default value was
  changing.

 it is only unintuitive when you do not know about it

 once you realize how it works and what it does it can actually be very
 useful

 i.

I agree it is a potentially useful feature, yet it can still bite you
even after a decade of Python... Scenario: long running server
process, Bug report: people aren't getting older, Code:

   def age(dob, today=datetime.date.today()):
   ...

None of my unit tests caught that one :-)

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


Re: Bizarre behavior with mutable default arguments

2007-12-30 Thread thebjorn
On Dec 30, 2:45 pm, Istvan Albert [EMAIL PROTECTED] wrote:
 On Dec 30, 5:23 am, thebjorn [EMAIL PROTECTED]
 wrote:

 def age(dob, today=datetime.date.today()):
 ...

  None of my unit tests caught that one :-)

 interesting example I can see how it caused some trouble. A quick fix
 would be to write it:

 def age(dob, today=datetime.date.today ):

 and inside the definition invoke it as today() rather than just today.
 That way it still keeps the original spirit of the definition.

 i.

The purpose of the argument was to be able to calculate the age at a
given point in time -- i.e. was the person 18 y/o at the time of the
incident?

Our coding standard now dictates:

   def foo(arg=None):
   if arg is None:
   arg = default mutable value

(unless there's a very good reason to do it otherwise :-)

a close runner-up, that we specifically opted not to allow was

   def foo(arg={}):
   arg = arg or {}

even though it looks sexy and it's perhaps a bit more self-documenting
in some IDEs, it was rejected because it prevents false overrides of
the default argument.

For purely practical reasons we couldn't consider

   def foo(arg=None):
   arg = default mutable value if arg is None else arg

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


Re: Instrospection question

2007-12-21 Thread thebjorn
On Dec 21, 11:28 am, Matias Surdi [EMAIL PROTECTED] wrote:
 I have the following code:
[...]
 As you can see, what I'm trying to do is replace a method with another
 one wich is the same method but with a function applied to it (in this
 case, a string concatenation ( + modified))

 Can anybody help me with this?

Why not use inheritance?

class A(object):
def a(self):
print Original

class B(A):
def a(self):
super(B, self).a()
print 'modified'

a = B()
a.a()

I'm not saying the code below is a good idea, but it solves the
problem of post-hoc modification

a = A()
a.__class__ = B
a.a()

we could probably come up with a better solution if we knew more about
the problem you were trying to solve...

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


Re: Question about email-handling modules

2007-12-21 Thread thebjorn
On Dec 20, 4:15 pm, Robert Latest [EMAIL PROTECTED] wrote:
 Steven D'Aprano wrote:
[...]
  All methods are attributes (although the opposite is not the case), so if
  a method doesn't exist, you will get an AttributeError.

 I see. I've already gathered that Python likes to use different words for
 common things (attribute instead of member or method).

..we were hoping it would make you feel comfy when coming from
Perl ;-)

On a more serious note, Python is actually quite semantically regular:
a dot (.) always means the same thing, as do parens. It might not be
immediately obvious exactly _what_ it means if you're coming from
languages that confuse issues with syntactic sweetness.

When you see code that says

foo.bar(baz)

there are two distinct operations happening, namely

tmp = foo.bar  # attribute lookup (the dot-operator)
tmp(baz)   # function call (the paren-operator)

this will give you the insight to one of the first optimization
methods you can use if a loop is a bottleneck

for i in range(10):
foo.bar(i)   # profiling says this is a bottleneck

attribute lookup hoisting optimization

tmp = foo.bar# move attribute lookup outside the loop
for i in range(10):
tmp(i)

in the interest of full disclosure, I should probably mention that I'm
of course lying to you ;-)  You can override both attribute lookup and
function call in your own classes, but (a) that shouldn't be important
to you at this point *wink*, and (b) at that level Python is quite
semantically regular.

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


Re: Sorting Objects by property using locale

2007-12-21 Thread thebjorn

Donn Ingle wrote:
 Hi,
  Well, I'm beat. I can't wrap my head around this stuff.

 I need to create a list that will contain objects sorted by a name
 property that is in the alphabetical order of the user's locale.

 I used to have a simple list that I got from os.listdir and I could do this:
 l = os.listdir(.)
 l.sort(locale.strcoll)
 Which would work fine.

 But now I need to make objects (of different stripes) for each of the
 filenames returned and then I want to collect them all into a main list and
 have *that* main list be sorted.

 I started some test code like this: I hope the odd characters show.

 # -*- coding: utf8 -*-

 class Test(object):
 def __init__(self,nam):
 self.name = nam
 def __cmp__(self, other):
 return cmp(self.name, other.name)

 l = [ Test(ABILENE.ttf), Test(Årgate.ttf), Test(årse.ttf),
 Test(Ärt.ttf), Test(MomentGothic.ttf), Test(öggi.ttf),
 Test(Öhmygawd.ttf)]

 import locale

 l.sort() # won't work - locale.strcoll)

 for i in l: print i.name


 Can anyone give me a leg-up?

 \d

You need to use the key argument to the sort function as well as the
cmp argument:

import os, locale

locale.setlocale(locale.LC_ALL, 'NO')

class Test(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name

fnames =  [Test(fname) for fname in os.listdir('.')]
fnames.sort(cmp=locale.strcoll, key=lambda t:t.name)

for fname in fnames:
print fname

and the output was

abel.txt
aegis.txt
onsdag.txt
pysortdir.py
pysortdir.py~
ærlig.txt
ønske.txt
åbel.txt
aaron.txt
åte.txt

Which is even more correct than I hoped for -- in Norwegian, aa is
pronounced the same as å (which is the 29th letter in the Norwegian
alphabet) and is sorted according to pronunciation.

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


How to memoize/cache property access?

2007-12-20 Thread thebjorn
I seem to be writing the following boilerplate/pattern quite
frequently to avoid hitting the database until absolutely necessary,
and to only do it at most once:

class Foo(object):
@property
def expensive(self):
if not hasattr(self, '_expensiv'):
self._expensive = insert expensive db call here
return self._expensive

it's a bit verbose, and it violates the DRY (Don't Repeat Yourself)
principle -- and it has on at least one occasion resulted in really
slow code that produces the correct results (did you spot the typo
above?).

It would have been nice to be able to write

class Foo(object):
@property
def expensive(self):
self.expensive = insert expensive db call here
return self.expensive

but apparently I can't set [that] attribute :-(

I'm contemplating using a decorator to hide the first pattern:

def memprop(fn):
def _fn(self):  # properties only take self
memname = '_' + fn.__name__
if not hasattr(self, memname):
setattr(self, memname, fn(self))
return getattr(self, memname)
return property(fget=_fn, doc=fn.__doc__)

which means I can very simply write

class Foo(object):
@memprop
def expensive(self):
return insert expensive db call here

I'm a bit hesitant to start planting home-grown memprop-s all over the
code-base though, so I'm wondering... does this seem like a reasonable
thing to do? Am I re-inventing the wheel? Is there a better way to
approach this problem?

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


Re: How to memoize/cache property access?

2007-12-20 Thread thebjorn
On Dec 20, 5:43 pm, Michele Simionato [EMAIL PROTECTED]
wrote:
 On Dec 20, 5:02 pm, thebjorn [EMAIL PROTECTED]
 wrote:

  I seem to be writing the following boilerplate/pattern quite
  frequently to avoid hitting the database until absolutely necessary ...

 I use the following module:
[...]

I love it!  much better name too ;-)

I changed your testcase to include a second cached property (the
naming is in honor of my late professor in Optimization of Functional
Languages class: ...any implementation that calls bomb_moscow is per
definition wrong, even if the program produces the correct result...
-- it was a while ago ;-)

if __name__ == '__main__': # a simple test
import itertools
counter = itertools.count()
class Test(object):
@cached
def foo(self):
return counter.next()

@cached
def bomb_moscow(self):
print 'fire missiles'
return counter.next()

reset = cached.reset

it didn't start WWIII, but I had to protect attribute deletion to get
it to run:

def fdel(s):
if private in s.__dict__:
del s.__dict__[private]

I'm a bit ambivalent about the reset functionality. While it's a
wonderful demonstration of a staticmethod, the very few times I've
felt the need to freshen-up the object, I've always felt it was best
to create it again from scratch. Do you have many uses of it in your
code?

-- bjorn

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


Re: Connecting to SQL database

2007-12-20 Thread thebjorn
On Dec 20, 10:01 pm, bill ramsay [EMAIL PROTECTED] wrote:
 On Fri, 14 Dec 2007 23:35:00 -0300, Gabriel Genellina

 [EMAIL PROTECTED] wrote:
 En Fri, 14 Dec 2007 23:24:24 -0300, Unknown [EMAIL PROTECTED]
 escribió:

  I have successfully connected to SQL2000 and MSDEE databases in the
  past,  however I have to move to SQL2005 and SQLEXPRESS databases now.

  Conn.ConnectionString = Provider=SQLNCLI;Server=10.1.1.2;
  Database=csrctest;Uid=bill;Pwd=bill

 Look for the right spelling athttp://www.connectionstrings.com/

 looks like my spelling is ok,  any other ideas?

 thanks for the suggestion above.

 kind regards

 bill

This is working for me to the internal network (I'm using adodbapi,
but the connection string should be the same)

  'Provider=sqloledb;Data Source=dbserv;Initial Catalog=ndb;User
Id=foo;Password=bar;'

I just tested, and this works well too

  'Provider=SQLNCLI;Data Source=dbserv;Initial Catalog=ndb;User
Id=foo;Password=bar;'

and to my local instance (Hermes is my machine)

  'Provider=SQLNCLI;Data Source=HERMES\\SQLEXPRESS;Initial
Catalog=;User Id=sa;Password=pw666;'

Can you connect to the server using SqlServer Management Studio? Can
you create an odbc connection to the server? Unlike 2000, SqlServer
2005 has tcp/ip connections turned off by default, so you might want
to check that with the SqlServer Surface Area Configuration tool.

Externally we're still running 2000, so I don't know how connecting
over the interweb differs.

When you've figured out how to connect, and if you're open to a
suggestion, you might want to try out the adodbapi package (http://
adodbapi.sourceforge.net/). It is a thin wrapper over ADO, and works
quite well. Then you'll get to use the Python db-api v.2.0 syntax,
which is oodles less typing than straight ADO.

-- bjorn

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


Re: Calculate an age

2007-12-06 Thread thebjorn
On Dec 6, 11:19 pm, John Machin [EMAIL PROTECTED] wrote:
 On Dec 7, 8:34 am, Pierre Quentel [EMAIL PROTECTED] wrote:

  Hi all,

  I have searched in the standard distribution if there was a function
  to return the difference between 2 dates expressed like an age :
  number of years, of months and days. The difference between datetime
  instances returns a timedelta object that gives a number of days, but
  not an age

  So is there such a function somewhere ? If not, for what reason, since
  it's a rather usual task

 and a rather usually imprecisely specified task [what do you mean by
 number of months?] with multiple interpretations/implementations/
 doctrines the publication of any one of which attracts a truckload of
 rotten tomatoes and derision from adherents of other sects :-)

It may be imprecisely specified, yet it's quite useful anyway. I've
got an implementation at http://blog.tkbe.org/archive/python-how-old-are-you/
if anyone's interested..

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


Re: python - an eggs...

2007-11-12 Thread thebjorn
On Nov 12, 1:12 am, bruce [EMAIL PROTECTED] wrote:
 Hi Diez...

 I've never used setuptools, (it's not on the box) and i don't have
 easy_install tools installed...

In the link you were given there is a section titled Installing
setuptools.  I'm reading it aloud for you now...

-- bjorn


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


Re: Questions about remembering and caching function arguments

2007-11-11 Thread thebjorn
On Nov 12, 1:05 am, Anand Patil [EMAIL PROTECTED]
wrote:
 Hi all,

 I have two questions about a class, which we'll call MyWrapperClass,
 in a package to which I'm contributing.

 1) MyWrapperClass wraps functions. Each instance has an attribute
 called 'value' and a method called 'eval', which calls the wrapped
 function. An instance D that depends on instances A, B and C can be
 created as follows:

 @mywrapperclass
 def D(A, B, C):
 return foo(A.value, B.value, C.value)

 Now that D exists, the call D.eval() will work without any arguments
 (D remembers that the arguments are A, B and C and passes their values
 to foo). What is the standard terminology for such classes, and does
 anyone know of a package that implements them in a nice way? (It's
 easy enough to roll our own, but reading about other implementations
 might give us good ideas).

 2) D.eval() will frequently be called multiple times in succession
 before A.value, B.value or C.value has had the chance to change. It
 would be _extremely_ helpful to have D detect this situation and skip
 recomputation. I'm looking for the fastest safe way to do this.
 There's no restriction on what kind of object A.value, etc. are, so
 unfortunately they might be mutable.

 Our current solution is to have D compare A.value, B.value and C.value
 to an internal cache using the 'is' operator (and put a big warning in
 the docs about not changing 'value' attributes in-place). Not exactly
 safe, but the speed savings over comparison with '==' will be
 significant. Is this OK, bad or an abomination?

 Again it would be helpful to know the terminology associated with the
 behavior I'm looking for and any packages that implement it nicely.

 Many thanks in advance and apologies for the long post,
 Anand

Cells (A dataflow extension to CLOS) seems like what you want:

  http://common-lisp.net/project/cells/

I think there was talk of a Python version a while back...

-- bjorn

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


Re: How can i find the form name without nr=0

2007-11-06 Thread thebjorn
On Nov 5, 6:05 pm, scripteaze [EMAIL PROTECTED] wrote:
[...]
 Well, i wasnt sure if you could have a form without a form name, i was
 just thinking that it had one but maybe hidden and that i could
 retrieve it

I see you've got the answer you wanted already, but just for
completeness: the following is a sufficiently(*) valid form in html

  form
input type=text name=q
input type=submit
  /form

which will have a textbox, and a submit button with localized text
saying Submit Query. If you type something into the textbox and hit
the button, a GET request is sent to the same page with ?q=something
appended to the url.

You can do the same with POSTed forms:

  form method=post
input type=hidden name=cmd value=rm -rf /
input type=submit value=Erase?
  /form

in this case only a button with the text Erase? is visible.

I'm not expressing an opinion on whether this is good form wink or
not...

-- bjorn

(*) the HTML spec says that the action attribute is required, so
theoretically you must include it.

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


Re: Iterable Flattener with Depth.

2007-11-02 Thread thebjorn
On Nov 2, 6:32 am, praddy [EMAIL PROTECTED] wrote:
 On Nov 1, 5:03 pm, [EMAIL PROTECTED] wrote:

  Pradeep Jindal:

   Any comments?

  Something with similar functionality (plus another 20 utility
  functions/classes or so) has probably to go into the std lib... :-)

  Bye,
  bearophile

 Same Here!

 - Pradeep

Yeah, everyone has to write a flatten sooner or later :-)  My version
is at:

  http://blog.tkbe.org/archive/python-flatten/

-- bjorn

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


Re: .join(string_generator()) fails to be magic

2007-10-11 Thread thebjorn
On Oct 11, 8:53 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 On Thu, 11 Oct 2007 01:26:04 -0500, Matt Mackal wrote:
  I have an application that occassionally is called upon to process
  strings that are a substantial portion of the size of memory. For
  various reasons, the resultant strings must fit completely in RAM.
  Occassionally, I need to join some large strings to build some even
  larger strings.

  Unfortunately, there's no good way of doing this without using 2x the
  amount of memory as the result. You can get most of the way there with
  things like cStringIO or mmap objects, but when you want to actually
  get the result as a Python string, you run into the copy again.

  Thus, it would be nice if there was a way to join the output of a
  string generator so that I didn't need to keep the partial strings in
  memory. subject would be the obvious way to do this, but it of
  course converts the generator output to a list first.

 Even if `str.join()` would not convert the generator into a list first,
 you would have overallocation.  You don't know the final string size
 beforehand so intermediate strings must get moved around in memory while
 concatenating.  Worst case: all but the last string are already
 concatenated and the last one does not fit into the allocated memory
 anymore, so there is new memory allocates that can hold both strings -
 double amount of memory needed.

 Ciao,
 Marc 'BlackJack' Rintsch

Perhaps realloc() could be used to avoid this?  I'm guessing that's
what cStringIO does, although I'm too lazy to check (I don't have
source on this box). Perhaps a cStringIO.getvalue() implementation
that doesn't copy memory would solve the problem?

-- bjorn

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


Re: if then elif

2007-10-10 Thread thebjorn
On Oct 10, 11:03 pm, Larry Bates [EMAIL PROTECTED] wrote:
[...]

 Boolean problem:

 if cal or fat = 0

 That may be the way you say it or think it but it won't work.

 'cal or fat' is evaluated first.  Since they both have values this ALWAYS
 evaluates to 1 which is NEVER less than or equal to 0.

That's not correct. The comparison operator has higher presedence than
the or operator, so the above is interpreted as:

if (cal) or (fat = 0):

This idiom is occasionally useful in a couple of scenarios like e.g.
default and mutable function arguments:

def foo(n=None):
n = n or [42]

which is very common in Perl code, but causes problems if you pass it
an empty list. The better way of doing that in Python is probably:

def foo(n=None):
n = n is None and [42]

but if you're going to write that much, it's clearer to just go with
the canonical:

def foo(n=None):
if n is None:
n = [42]

but I digress :-)

 You are looking for

 if (cal = 0) or (fat =0):

 (Note: Parenthesis not required, but it may help you understand precedence of
 evaluation.

That is a correct coding of the OP's intentions, although I would
think that the number of fat grams could indeed be zero for some
foods(?) so perhaps it would be more correct to say:

   if cal = 0 or fat  0:

  Also read here:

 http://www.ibiblio.org/g2swap/byteofpython/read/operator-precedence.html

 -Larry

Good table, except for higher presedence being further down in the
table which might be confusing pedagogically.

-- bjorn

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


Re: sorteddict [was a PEP proposal, but isn't anymore!]

2007-09-29 Thread thebjorn
On Sep 29, 4:23 pm, Duncan Booth [EMAIL PROTECTED] wrote:
[...]
 Another example would be if you had a library which serialised a dictionary
 to xml. There is nothing wrong with the library if it doesn't care about
 order, but if you have some other reason why you want the xml to be stable
 (e.g. because you store it in a version control system and want to compare
 revisions) then a sorteddict would allow you to impose that behaviour on
 the library from outside.

 Contrary to my earlier insistence that sorteddict is only really useful if
 you can have a key parameter, both of these examples simply want an
 arbitrary but defined order of iteration for dictionary keys. A much
 simpler sorteddict that has been discussed earlier would be sufficient.

In fact, a dictionary that maintains insertion order would work in
this case too.

-- bjorn

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


Re: Program inefficiency?

2007-09-29 Thread thebjorn
On Sep 29, 5:22 pm, [EMAIL PROTECTED] wrote:
 I wrote the following simple program to loop through our help files
 and fix some errors (in case you can't see the subtle RE search that's
 happening, we're replacing spaces in bookmarks with _'s)

 the program works great except for one thing. It's significantly
 slower through the later files in the search then through the early
 ones... Before anyone criticizes, I recognize that that middle section
 could be simplified with a for loop... I just haven't cleaned it
 up...

 The problem is that the first 300 files take about 10-15 seconds and
 the last 300 take about 2 minutes... If we do more than about 1500
 files in one run, it just hangs up and never finishes...

 Is there a solution here that I'm missing? What am I doing that is so
 inefficient?

Ugh, that was entirely too many regexps for my taste :-)

How about something like:

def attr_ndx_iter(txt, attribute):
Return all the start and end indices for the values of
attribute.
txt = txt.lower()
attribute = attribute.lower() + '='
alen = len(attribute)
chunks = txt.split(attribute)
if len(chunks) == 1:
return

start = len(chunks[0]) + alen
end = -1

for chunk in chunks[1:]:
qchar = chunk[0]
end = start + chunk.index(qchar, 1)
yield start + 1, end
start += len(chunk) + alen

def substr_map(txt, indices, fn):
Apply fn to text within indices.
res = []
cur = 0

for i,j in indices:
res.append(txt[cur:i])
res.append(fn(txt[i:j]))
cur = j

res.append(txt[cur:])
return ''.join(res)

def transform(s):
The transformation to do on the attribute values.
return s.replace(' ', '_')

def zap_spaces(txt, *attributes):
for attr in attributes:
txt = substr_map(txt, attr_ndx_iter(txt, attr), transform)
return txt

def mass_replace():
import sys
w = sys.stdout.write

for f in open(r'pathname\editfile.txt'):
try:
open(f, 'w').write(zap_spaces(open(f).read(), 'href',
'name'))
w('.') # progress-meter :-)
except:
print 'Error processing file:', f

minimally-tested'ly y'rs
-- bjorn

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


Re: sorteddict [was a PEP proposal, but isn't anymore!]

2007-09-29 Thread thebjorn
On Sep 29, 7:13 pm, Duncan Booth [EMAIL PROTECTED] wrote:
[...]
 Right now I think there are probably three dict variants needed: sorteddict
 (still waiting for a convincing use case), ordereddict (lots of use cases),
 and this one: stabledict.

What's stabledict? I'm assuming that ordereddict is a mapping that
maintains insertion order(?)

The only other mapping type I use very frequently is a dict where the
keys are limited to valid identifiers, and where attribute lookup
(d.foo) is defined as key lookup (d['foo']). It makes lots of code
easier to read (and write).

In the Smalltalk collection hierarchy SortedCollection is a subclass
of OrderedCollection, which implies to me that it'd be better to add
an ordereddict first.

-- bjorn

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


Re: Program inefficiency?

2007-09-29 Thread thebjorn
On Sep 29, 7:55 pm, Pablo Ziliani [EMAIL PROTECTED] wrote:
 thebjorn wrote:
  On Sep 29, 5:22 pm, [EMAIL PROTECTED] wrote:

  I wrote the following simple program to loop through our help files
  and fix some errors (in case you can't see the subtle RE search that's
  happening, we're replacing spaces in bookmarks with _'s)
  (...)

  Ugh, that was entirely too many regexps for my taste :-)

  How about something like:

  def attr_ndx_iter(txt, attribute):
  (...)
  def substr_map(txt, indices, fn):
  (...)
  def transform(s):
  (...)
  def zap_spaces(txt, *attributes):
  (...)
  def mass_replace():
  (...)

 Oh yeah, now it's clear as mud.

I'm anxiously awaiting your beacon of clarity ;-)

 I do think that the whole program shouldn't take more than 10 lines of
 code

Well, my mass_replace above is 10 lines, and the actual replacement
code is a one liner. Perhaps you'd care to illustrate how you'd
shorten that while still keeping it clear?

 using one sensible regex

I have no doubt that it would be possible to do with a single regex.
Whether it would be sensible or not is another matter entirely...

 (impossible to define without knowing the real input and output formats).

Of course, but I don't think you can guess too terribly wrong. My
version handles upper and lower case attributes, quoting with single
(') and double () quotes, and any number of spaces in attribute
values. It maintains all other text as-is, and converts spaces to
underscores in href and name attributes. Did I get anything majorly
wrong?

 And (sorry to tell) I'm convinced this is a problem for regexes, in
 spite of anybody's personal taste.

Well, let's see it then :-)

smack-smack'ly y'rs
-- bjorn

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


Re: strange unbound local error?

2007-09-29 Thread thebjorn
On Sep 29, 8:04 pm, [EMAIL PROTECTED] wrote:
 hi folks,

 suppose this snipplet:

 spam = 42

 def eggs():
 print spam
 spam = spam + 1

 if __name__==__main__:
 eggs()

 This thows an UnboundLocalError at line 4 (print statement). But if I
 comment out line 5 (variable assignment), no error occurs.

 Can you explain me this, please?

 Regards,
 Enrico

If you're going to assign to a global variable in a function, you need
to declare it as such:

spam = 42

def eggs():
global spam
print spam
spam = spam + 1

When Python sees an assignment to an identifier in a function it
creates a variable that is local to the function (otherwise you'd be
unable to create local variables with the same name as any of the
global variables).

-- bjorn



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


Re: Program inefficiency?

2007-09-29 Thread thebjorn
On Sep 29, 8:32 pm, [EMAIL PROTECTED] wrote:
 It think he's saying it should look like this:

 # File: masseditor.py

 import re
 import os
 import time

 p1= re.compile('(href=|HREF=)+(.*)(#)+(.*)(\w\'\?-:)+(.*)()+')
 p2= re.compile('(name=)+(.*)(\w\'\?-:)+(.*)()+')
 p100= re.compile('(a name=)+(.*)(-)+(.*)(/a)+')
 q1= r\1\2\3\4_\6\7
 q2= r\1\2_\4\5

 def massreplace():
 editfile = open(C:\Program Files\Credit Risk Management\Masseditor
 \editfile.txt)
 filestring = editfile.read()
 filelist = filestring.splitlines()

 for i in range(len(filelist)):
 source = open(filelist[i])
 starttext = source.read()

 for i in range (13):
 interimtext = p1.sub(q1, starttext)
 interimtext= p2.sub(q2, interimtext)
 interimtext= p100.sub(q2, interimtext)
 source.close()
 source = open(filelist[i],w)
 source.write(finaltext)
 source.close()

 massreplace()

 I'll try that and see how it works...

Ok, if you want a single RE... How about:


  test = '''
  a href=Web_Sites.htm#A Web Sites
  a name=A Web Sites/a
  a
  href=Web_Sites.htm#A Web Sites
  a
  name=A Web Sites/a
  a HREF=Web_Sites.htm#A Web Sites
  a name=Quoteless/a
  a name = oo ps/a
  '''

  import re

  r = re.compile(r'''
  (?:href=['][^#]+[#]([^']+)['])
| (?:name=[']?([^']+))
  ''', re.IGNORECASE | re.MULTILINE | re.DOTALL | re.VERBOSE)

  def zap_space(m):
  return m.group(0).replace(' ', '_')

  print r.sub(zap_space, test)

It prints out

  a href=Web_Sites.htm#A_Web_Sites
  a name=A_Web_Sites/a
  a
  href=Web_Sites.htm#A_Web_Sites
  a
  name=A_Web_Sites/a
  a HREF=Web_Sites.htm#A_Web_Sites
  a name=Quoteless/a
  a name = oo ps/a

-- bjorn

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


Re: Program inefficiency?

2007-09-29 Thread thebjorn
On Sep 29, 9:32 pm, stdazi [EMAIL PROTECTED] wrote:
 On Sep 29, 6:07 pm, [EMAIL PROTECTED] wrote:

  You did not mention the OS, but because you are using
  pathname\editfile.txt, it sounds like you are using an MS OS.  From
  past experience with various MS OSes, I found that as the number of
  files in a directory increases the slower your process runs for each
  file.

 how so?

Not entirely sure why, but some of the ms docs allude to the fact that
there is a linked list involved (at least for fat-style disks).

-- bjorn

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


Re: sorting a list numbers stored as strings

2007-09-25 Thread thebjorn
On Sep 25, 2:45 pm, Steven D'Aprano [EMAIL PROTECTED]
cybersource.com.au wrote:
 On Tue, 25 Sep 2007 12:46:54 +0800, Delaney, Timothy (Tim) wrote:
  Carsten Haese wrote:

  On Mon, 2007-09-24 at 19:58 +0800, Delaney, Timothy (Tim) wrote:
  I'm sure that in some version of Python it would have given a
  ValueError (due to the default radix being 0) but it appears to have
  changed to a default radix of 10 somewhere along the way.

  Not even Python 1.5.2 seems to have a problem with leading zeroes:

  Python 1.5.2 (#1, Nov  6 1999, 14:53:40) [C] on sco_sv3 Copyright
  1991-1995 Stichting Mathematisch Centrum, Amsterdam
  int(08)
  8

  Yep - appears I must have been misremembering from another language
  (dunno which) or I misinterpreted the docs.

 I also remember something in Python about leading zeroes leading to
 surprising effects... ah, I got it!

  int(020)
 20
  020
 16

You can get the latter behavior using eval:

 eval(020)
16
 eval(09)
Traceback (most recent call last):
  File stdin, line 1, in module
  File string, line 1
09
^
SyntaxError: invalid token


This usually bites you in the @$$ when you're trying to store config
data as a Python datastructure in an external file -- so that you can
do config_data = eval(open('config.data').read()).

-- bjorn

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


Re: sorteddict PEP proposal [started off as orderedict]

2007-09-25 Thread thebjorn
On Sep 25, 10:53 am, Mark Summerfield [EMAIL PROTECTED]
wrote:
 Hi,

 Below is a PEP proposal for a sorteddict. It arises out of a
 discussion on this list that began a few weeks ago with the subject of
 An ordered dictionary for the Python library?, and a similar one on
 the P3K mailing list with the subject ordered dict for p3k
 collections?.

 If there is positive feedback I will submit the PEP to the reviewers,
 so if you think it is a good idea please say so. (I'm sure that if you
 _don't_ like it you'll tell me anyway:-)

I can't see much advantage over:

  for key in sorted(mydict):
  ...

A much simpler data-structure, that is also very useful, is a
dictionary that keeps insertion order -- it's also something that's a
tad bit more difficult to implement yourself than the above. My
personal implementation is documented at 
http://blog.tkbe.org/archive/python-property-set/
It's very tempting to add value access by numerical index (I did as
well), however I now think it's a mistake.

-1 from me.

-- bjorn




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


Re: %s shortcut?

2007-09-25 Thread thebjorn
On Sep 26, 4:55 am, james_027 [EMAIL PROTECTED] wrote:
 hi i have something like this

 cursor.execute(
 select c.name,
 (select from ap_invoice i where Month(i.date) = 1 and
 Year(i.date) = %s and i.customer_id = c.id and i.status != 'CAN') jan,
 (select from ap_invoice i where Month(i.date) = 2 and
 Year(i.date) = %s and i.customer_id = c.id and i.status != 'CAN') feb,
 (select from ap_invoice i where Month(i.date) = 3 and
 Year(i.date) = %s and i.customer_id = c.id and i.status != 'CAN') mar
 from ap_customer c
 order by %s,
 [year, order_by])

 what I could like to happen is ... since the first three %s points to
 the same year variable, how do I let python know it without doing
 [year ,year, year, order_by] ... This should be 12 I just cut it down

 Thanks
 james

You could do [year]*12 + [order_by] like so:

 [1970] * 12 + ['foo']
[1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970,
1970, 'foo']


I don't know which database you're using, but the above doesn't look
like SQL'92...

-- bjorn

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


Re: ANN: PyPE 2.8.7

2007-09-25 Thread thebjorn
On Sep 25, 12:37 pm, stef [EMAIL PROTECTED] wrote:
 Josiah Carlson wrote:
  === What is PyPE? ===
  PyPE (Python Programmers' Editor) was written in order to offer a
  lightweight but powerful editor for those who think emacs is too much
  and idle is too little. Syntax highlighting is included out of the box,
  as is multiple open documents via tabs.

 sounds very good,
 so now I want to try it ...
 ... the windows ansi zip file is corrupted :-(
 ... the unicode version crashes :-(

 This is what the log file tells:
 Traceback (most recent call last):
   File pype.py, line 110, in ?
   File zipextimporter.pyc, line 78, in load_module
   File configuration.pyc, line 149, in ?
 Exception: Unable to create config directory: 'G:\\.pype'

 I don't have a G-drive ;-)

 cheers,
 Stef Mientki

You must be doing it wrong. Works flawlessly for me (no g: drive here
either) -- well, except that it doesn't have Emacs keybindings wink.

-- bjorn

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


Re: ANN: PyPE 2.8.7

2007-09-25 Thread thebjorn
On Sep 25, 12:46 pm, stef [EMAIL PROTECTED] wrote:
 Another problem,
 I tried to run PyPE in my old IDE,

instead of double-clicking on the PyPE.exe file? Why?

-- bjorn

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


Re: Mixin classes and single/multiple inheritance

2007-09-20 Thread thebjorn
On Sep 20, 6:52 am, Ben Finney [EMAIL PROTECTED]
wrote:
 Michele Simionato [EMAIL PROTECTED] writes:
  Since the language we have does have multiple inheritance, let's
  use it to implement mixins.
  ...
  So, multiple inheritance is giving us very little for the point of
  view of mixins; OTOH, multiple inheritance is giving us a lot of
  headaches for what concerns cooperative methods.

 I may be obtuse, but the above statements seem to be professing
 contradictory viewpoints.

How so? A language doesn't need MI to have mixins (e.g. Ruby does
this). This doesn't seem to contradict that MI is, in general, a
horrible solution to most problems -- one example being the general
difficulty in groking cooperative (super) methods. A single-
inheritance language, otoh, is also a chore to work with (for a
specific subset of designs) -- unless there is _some_ way to do
mixins.  But for the general case I agree with Micele that delegation
is the better solution (Explicit is better than implicit).

 Do you think multiple inheritance should or should not be used in
 Python to do what mixins do?

Michele already answered this, Python has MI so it would just be
confusing to implement mixins any other way.

  My point was that even if Python had been implemented without
  multiple inheritance, it would have been simple to implement mixins,
  or by copying the methods, or by dispatching with __getattr__.

 Can you give a code example of how you think mixins should be
 implemented in Python, assuming the absence of multiple inheritance?

Googling for Ruby mixins tutorial gave me the following link:
http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/

If I tell you that self.something is written @something in Ruby, and
inheritance is written class Base  Parent (as opposed to class
Base(Parent):) everything else should be understandable..?

How to implement include and extend in Python is left as an excercise
for the reader.

-- bjorn


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


Re: Sets in Python

2007-09-20 Thread thebjorn
On Sep 20, 4:17 am, Steven D'Aprano [EMAIL PROTECTED]
cybersource.com.au wrote:
[...]
 Data structures don't have problems. Programmers do.

That's QOTW material :-)

 ... And language
 designers with sense build languages that minimize the programmers
 problems, not maximize them.

...

 The choices for the language designer are:

 (1) Invent some sort of magical non-deterministic hash function which
 always does the Right Thing.

 (2) Allow the hash of mutable objects to change, which means you can use
 mutable objects as keys in dicts but if you change them, you can no
 longer find them in the dict. They'll still be there, using up memory,
 but you can't get to them.

 (3) Simply disallow mutable objects as keys.

(4) Allow mutable objects as keys, but have undefined (implementation
defined) behavior if keys are mutated.

This would seem a natural extension of the we're all adults paradigm
(if I name a variable _foo you should treat it as private).
Unfortunately there is no visual cue in this case, and tracking down
where you relied on undefined behavior is notoriously time-consuming
(just ask your nearest C++ multiplatform programmer).

 Alternative 3 is easy to deal with: simply don't use mutable objects as
 keys. That's what Python does. Sure, the programmer sometimes needs to
 work around the lack (convert the list into a tuple, or a string, or
 pickle it, whatever...) which on rare occasions is hard to do, but at
 least there are no mysterious, hard to track down bugs.

Amen.

-- bjorn

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


Re: Sets in Python

2007-09-20 Thread thebjorn
On Sep 20, 6:02 am, Karthik Gurusamy [EMAIL PROTECTED] wrote:
 On Sep 19, 7:17 pm, Steven D'Aprano [EMAIL PROTECTED]

[...]
  (2) Allow the hash of mutable objects to change, which means you can use
  mutable objects as keys in dicts but if you change them, you can no
  longer find them in the dict. They'll still be there, using up memory,
  but you can't get to them.

 In the new model, at the time of addition, you need to remember the
 key at that time. If it's a list, you make a copy of the items.

Eek! Barf! Gag me with a spoon! etc. etc. :-)

And you mean a deep-copy, not just a copy, right?

Or perhaps you were thinking of something like this (mdict ::= mutable
dict):

class mdict(dict):
def __setitem__(self, k, val):
super(mdict,self).__setitem__(`k`, val)
def __getitem__(self, k):
return super(mdict,self).__getitem__(`k`)
def __contains__(self, k):
return super(mdict,self).__contains__(`k`)
def keys(self):
return list(eval(k) for k in super(mdict,self).keys())
def __iter__(self):
for k in super(mdict,self).__iter__():
yield eval(k)
def items(self):
return list((eval(k),v) for k,v in super(mdict,self).items())
def __repr__(self):
items = ', '.join('%s: %s' % (k,repr(v)) for k,v in
self.items())
return '{' + items + '}'

I think it does what you want..?:

 m = mdict()
 a, b = [], [1,2]
 print m
{}
 m[a] = a
 m[b] = b
 m
{[1, 2]: [1, 2], []: []}
 m.keys()
[[1, 2], []]
 for k in m:
... m[k].append('foo')
...
 m
{[1, 2]: [1, 2, 'foo'], []: ['foo']}
 m.items()
[([1, 2], [1, 2, 'foo']), ([], ['foo'])]
 m.values()
[[1, 2, 'foo'], ['foo']]
 a in m
False
 a
['foo']
 b
[1, 2, 'foo']
 [] in m
True
 [1,2] in m
True
 m[{'key':['val']}] = 'this works too'

It'll work for all keys, k, where eval(`k`) == k, and repr(a) ==
repr(b) when a == b (I'm pretty sure the latter isn't always true for
dicts, although I haven't looked at the implementation.)

-- bjorn

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


Re: Sets in Python

2007-09-20 Thread thebjorn
On Sep 20, 9:50 am, thebjorn [EMAIL PROTECTED]
wrote:

it's bad form to reply to myself, I know, but

 def __iter__(self):
 for k in super(mdict,self).__iter__():
 yield eval(k)

should probably be

def __iter__(self):
return (eval(k) for k in super(mdict,self).__iter__())

I'm still getting used to the power of generator expressions :-)

-- bjorn

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


Re: super() doesn't get superclass

2007-09-19 Thread thebjorn
On Sep 19, 3:41 pm, Michele Simionato [EMAIL PROTECTED]
wrote:
 On Sep 19, 3:22 pm, Sion Arrowsmith [EMAIL PROTECTED]
 wrote:

  Ben Finney  [EMAIL PROTECTED] wrote:

   If a function is named 'super' and operates on
  classes, it's a pretty strong implication that it's about
  superclasses.

  But it doesn't (under normal circumstances) operate on classes.
  It operates on an *instance*. And what you get back is a (proxy
  to) a superclass/ancestor of the *instance*.

  (And in the super(A, B) case, you get a superclass/ancestor of
  *B*. As has just been said somewhere very near here, what is
  misleading is the prominence of A, which isn't really the most
  important class involved.)

 Happily A (and B too) will become invisible in Python 3000.

 Michele Simionato

This is great news! Since it is for Py3K it seems clear to me that
super should be a keyword as well (but historically I'm not the best
at channeling Guido ;-)

-- bjorn



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


Re: adding a static class to another class

2007-09-16 Thread thebjorn
On Sep 17, 1:14 am, Nathan Harmston [EMAIL PROTECTED]
wrote:
 HI,

 I m trying to start an api in a similar way to the djangic way of
 Class.objects.all(). Ie objects is a Manager class.

 So:

 class Foo(object):
def __init__(self):
 self.test = NEE

 class Manager(object):
 def __init__(self):
 pass
def all(self):
return COCONUTS

 Because of how some of the code is set up I cant use
 metaclassesso I try to use a decorator:

 def addto(instance):
 def decorator(f):
 import new
 f = new.instancemethod(f, instance, instance.__class__)
 setattr(instance, objects, f)
 return f
 return decorator

 class Manager(object):
 @addto(Foo)
 def __init__(self):
.

 however this only binds the init method to the Foo.objects, so not
 what I want. If I try using classmethod...then it just says the
 Foo.objects doesnt exist.

 Does anyone have any ideas how I can accomplish this using decorators?
 And also preventing more than one Manager instance instantiated at one
 time.

 Many Thanks in advance,

 Nathan

You want to use descriptors (classes that implement __get__, __set__,
__del__). Google for the particulars.

-- bjorn

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


Re: An ordered dictionary for the Python library?

2007-09-12 Thread thebjorn
On Sep 12, 9:33 am, Mark Summerfield [EMAIL PROTECTED]
wrote:
 I feel that Python lacks one useful data structure: an ordered
 dictionary.

 I find such data structures v. useful in C++. I know that in Python
 the sort function is v. fast, but often I prefer never to sort but
 simply to use an ordered data structure in the first place.
 (I'm aware that for ordered lists I can use the bisect module, but I
 want an ordered key-value data structure.)
[...]
 Do other Python programmers feel this lack? Is this worth a PEP?

I usually make a distinction between a sorted dict, where iteration
(and potentially positional indexing) happens in sorted key order; and
an ordered dict where items maintain insertion order. I use the latter
all the time, and e.g. Django's model metaclass does some minor magic
to overcome the fact that field-order is lost by the time your
metaclass gets control, since the attributes are passed as a regular
dict.

-- bjorn

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


Re: Latest models of Gibson guitars

2007-08-19 Thread thebjorn
On Aug 19, 8:41 pm, [EMAIL PROTECTED] wrote:
 On 19 kol, 19:34, [EMAIL PROTECTED] wrote:

[spam]

 Hello,

 This is  a newsgroup of programming language Python, stop with this!

 Regards,
 Vedran

As someone else pointed out, this is more widely disseminated than
just c.l.py. If you feel the need to do something about the post, and
you're using Google groups, there's a More options link in the top
right corner of every post where you can report it as spam. That might
be marginally more effective than trying to chastise a spammer for
being off-topic (it's what they do) -- at least it won't degrade the
signal/noise ratio further.

-- bjorn

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


Re: Sorting a list of Unicode strings?

2007-08-19 Thread thebjorn
On Aug 19, 8:09 pm, [EMAIL PROTECTED] (Alex Martelli) wrote:
[...]
 In both Swedish and Danish, I believe, A-with-ring sorts AFTER the
 letter Z in the alphabet; so, having Åaland (where I'm using Aa for
 A-with-ring, since this newsreader has some problem in letting me enter
 non-ascii characters;-) sort right at the bottom, while it doesn't
 look right to YOU (maybe an English-speaker?) may look right to the
 inhabitants of that locality (be they Danes or Swedes -- but I believe
 Norwegian may also work similarly in terms of sorting).

You're absolutely correct, the Norwegian and Danish alphabets end
with ..xyzæøå, while the Swedish alphabet ends with ..xyzåäö and sort
order follows placement. Indeed, my first reaction to the op was:
where else would Åland be but at the end? One, perhaps interesting,
tidbit, is that Åland belongs to Finland (it's an autonomous,
demilitarized, monolingually Swedish-speaking administrative province
of Finland). The Finnish alphabet is identical to the Swedish
alphabet, including sort order (at least in this case)

For the ascii-speakers out there, the key point to remember is that
the letter Å (pronounced like the au in brittish autumn) is not an
ascii A with a ring on top. The ring-on-top is an intrinsic part of
the letter, in the same way the tail on the letter Q isn't a
decoration of the letter O.

-- bjorn

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


Re: Compiling python2.5.1 results in 3.5MB python lib?

2007-07-29 Thread thebjorn
On Jul 29, 12:46 pm, simonbun [EMAIL PROTECTED] wrote:
...
 How would I go about doing this? I'm not sure what to strip nor how to
 do it.

  man strip

-- bjorn

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


Re: Catching a key press

2007-07-23 Thread thebjorn
On Jul 23, 9:05 am, westymatt [EMAIL PROTECTED] wrote:
 This is without a gui toolkit.  This is going to be implemented on
 console i/o

If you're on windows, you'll need the kbhit() and getch() functions in
the msvcrt module. The up arrow returns two separate keyboard-
hits (with ordinal value 224 and 72).

-- bjorn

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


Re: determining the source code and module based on the class

2007-07-16 Thread thebjorn
On Jul 16, 6:55 am, alf [EMAIL PROTECTED] wrote:
...
 now I need a piece of code which based on the class name can point to
 the module as well as the line number where the given class is defined.

 Is it doable in Python?

look in the inspect module.

-- bjorn

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


Re: Looping issues

2007-04-06 Thread thebjorn
On Apr 5, 8:01 pm, [EMAIL PROTECTED] wrote:
 What I am trying to do is compare two files to each other.

 If the 2nd file contains the same line the first file contains, I want
 to print it. I wrote up the following code:

 correct_settings = open(C:\Python25\Scripts\Output
 \correct_settings.txt,r)
 current_settings = open(C:\Python25\Scripts\Output\output.txt,r)

 for line in correct_settings:
 for val in current_settings:
 if val == line:
 print line +  found.

 correct_settings.close()
 current_settings.close()

 For some reason this only looks at the first line of the
 correct_settings.txt file. Any ideas as to how i can loop through each
 line of the correct_settings file instead of just looking at the first?

I'm not entirely sure I understand what you're trying to do, but in
case you're trying to walk through the two files in lockstep printing
the lines that correspond, here's a way to do that:

# note the r'..' syntax
correct = open(r'c:\python25\scripts\output\correct_settings.txt')
current = open(r'c:\python25\scripts\output\output.txt')

for correct_line, current_line in zip(correct, current):
if correct_line == current_line:
print correct_line, 'found.'

correct.close()
current.close()

hth,
-- bjorn

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


Re: how to write code into a blog post?

2006-11-04 Thread thebjorn
Jorge Vargas wrote:
 Hi I know many people here blog so sorry for the OT.

 Currently I have a wordpress install and went I wanted to post some
 code I notice how painfull it is.

Indeed :-)  I'm using the iG:Syntax Hiliter over on
http://blog.tkbe.org after I got some comments about the lack of
readability of my code samples ;-)  It can be even more colorful than
I've set it up, but it handles a considerable number of languages and
is pretty simple to both use and manage. You can get it at:
http://blog.igeek.info/wp-plugins/igsyntax-hiliter/  I added the
following to the end of the stylesheet to make the code frame bigger
and dynamic:

div.igBar { width: 95%; }
div.syntax_hilite { width:95%; }

hth,
-- bjorn

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


Re: Is there a way to get utf-8 out of a Unicode string?

2006-10-30 Thread thebjorn
Fredrik Lundh wrote:
 thebjorn wrote:

  I've got a database (ms sqlserver) that's (way) out of my control,
  where someone has stored utf-8 encoded Unicode data in regular varchar
  fields, so that e.g. the string 'Blåbærsyltetøy' is in the database
  as 'Bl\xc3\xa5b\xc3\xa6rsyltet\xc3\xb8y' :-/
..
 first, check if you can get your database adapter to understand that the
 database contains UTF-8 and not ISO-8859-1.

It would be the way to go, however it looks like they've managed to get
Latin-1 data in exactly two columns in the entire database (this is a
commercial product of course, so there's no way for us to fix things).
And just to make things more interesting, I think I'm running into an
ADO bug where capital letters (outside the U+ to U+007F range) are
returning strange values:

 c.execute('create table utf8 (f1 varchar(15))')
 u'ÆØÅÉ'.encode('utf-8')
'\xc3\x86\xc3\x98\xc3\x85\xc3\x89'
 x = _
 c.execute('insert into utf8 (f1) values (?)', (x,))
 c.execute('select * from utf8')
 c.fetchall()
((u'\xc3\u2020\xc3\u02dc\xc3\u2026\xc3\u2030',),)


I haven't tested this through C[#/++] to verify that it's an ADO issue,
but it seems unlikely that MS would view this as anything but incorrect
usage no matter where the issue is...

Anyway, sorry for venting :-)

 if that's not possible, you can roundtrip via ISO-8859-1 yourself:

   u = u'Bl\xc3\xa5b\xc3\xa6rsyltet\xc3\xb8y'
...
   print u.encode(iso-8859-1).decode(utf-8)
 Blåbærsyltetøy

That's very nice!

-- bjorn

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


Re: Is there a way to get utf-8 out of a Unicode string?

2006-10-30 Thread thebjorn
Gerrit Holl wrote:
 Hei,

 On 2006-10-30 08:25:41 +0100, thebjorn wrote:
def unfk(s):
return eval(repr(s)[1:]).decode('utf-8')
 
...
  Is there a less hack'ish way to do this?

 Slightly lack hackish:

 return ''.join(chr(ord(c)) for c in s)

Much less hackish :-)

-- bjorn

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


Is there a way to get utf-8 out of a Unicode string?

2006-10-29 Thread thebjorn
I've got a database (ms sqlserver) that's (way) out of my control,
where someone has stored utf-8 encoded Unicode data in regular varchar
fields, so that e.g. the string 'Blåbærsyltetøy' is in the database
as 'Bl\xc3\xa5b\xc3\xa6rsyltet\xc3\xb8y' :-/

Then I read the data out using adodbapi (which returns all strings as
Unicode) and I get u'Bl\xc3\xa5b\xc3\xa6rsyltet\xc3\xb8y'. I couldn't
find any way to get back to the original short of:

  def unfk(s):
  return eval(repr(s)[1:]).decode('utf-8')

i.e. chopping off the u in the repr of a unicode string, and relying on
eval to interpret the \xHH sequences.

Is there a less hack'ish way to do this?

-- bjorn

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


Re: Lookuperror : unknown encoding : utf-8

2006-10-29 Thread thebjorn
Sachin Punjabi wrote:
 I wanted to read a file encoded in utf-8 and and using the following
 syntax in my source which throws me an error specifying Lookuperror :
 unknown encoding : utf-8. Also I am working on Python version 2.4.1.

You shouldn't have to do anything to have the utf-8 encoding available.
Check in your lib/encodings directory for a file name utf_8.py and the
code in __init__.py in the same directory should take care of the
mapping. This has been this way since at least Python 2.2 (which is the
oldest version I have on this machine).

If that doesn't give you a clue as to what is going on in your setup,
try

  u'foo'.encode('utf-8')

at the prompt and post the complete traceback.

 import codecs
 fileObj = codecs.open( data.txt, r, utf-8 )

That should work fine, although I prefer to explicitly set the mode to
rb (it will be set to binary mode behind your back regardless ;-)

hth,
-- bjorn

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


Unicode/utf-8 data in SQL Server

2006-08-08 Thread thebjorn
I'm working with a MS SQL Server database created by a program from a
fine US company who seems to have gotten run over by the Unicode truck.
In their infinite wisdom they've decided to store Unicode data directly
in regular varchar fields, utf-8 encoded! (on the bright side, it is
properly utf-8 encoded). One of our customers then wants to use a csv
file created from a report to import in Excel and is getting an
attitude when the text shows up all garbled (which I can
understand...)

One method that works is to use Python to pull down the result set from
the database, accumulate the entire result text as a big unicode string
(while decode('utf-8') all text fields in the process) separating each
field with a tab, before encode('utf-16') the result string and writing
it to a file opened in binary mode.  This ensures that the file gets a
bom, that it's in a format (utf-16) that Excel can import, and
hopefully tabs are less common than commas in the source data :-(  The
csv module doesn't support Unicode.

The customer is of the firm belief that our national characters
(æøå) are part of ascii, presumably because they're
single-byte-encoded in iso-8859-1. He has no understanding for the
issues (either by choice or experience) so there is no purpose to
trying to explain the differences... Be that as it may, he might be
satisfied with a csv file in that (iso-8859-1) encoding since the local
version of Excel can import it transparently (with significant
behind-the-scenes magic I believe...?)

The Python script mentioned above has to be run on the server, since it
doesn't accept remote connections, I'm of course the only one with
access, and I'd like to remove myself from the loop. I've looked at
creating a view on the database that would cast or convert the data,
but all I've run into are vague references to StrConv, which seems to
be a VB function. Giving the customer a macro that he could run in
Excel after importing the data would probably be ok as well, so I also
tried creating an Excel VB macro using the StrConv function, but (a) it
isn't entirely clear to me that this function can do this, and (b) the
third argument to the function is an LCID, a Locale ID, which is
numeric and not defined anywhere I can find it...

Anyone have any advice?

tia,
-- bjorn

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


Re: newb question: file searching

2006-08-08 Thread thebjorn
[EMAIL PROTECTED] wrote:
[...]
 that?  And also, I'm still not sure I know exactly how os.walk() works.
  And, finally, the python docs all note that symbols like . and ..
 don't work with these commands.  How can I grab the directory that my
 script is residing in?

os.getcwd() will get you the directory your script is in (at least as
long as you're running the script from the current directory :-)

Here's my version of how to it (comments below):

def collect_ext(*extensions):
Return a list of files from current directory and downwards
   that have given extensions. Call it like collect_ext('.py',
'.pyw')

res = []
for dirname, _, files in os.walk(os.getcwd()):
for fname in files:
name, ext = os.path.splitext(fname)
if ext in extensions:
res.append(os.path.join(dirname, fname))
return res

Each time around the outer for-loop, os.walk() gives us all the
filenames (as a list into the files variable) in a sub-directory. We
need to run through this list (inner for-loop), and save all files with
one of the right extensions. (os.walk() also gives us a list of all
subdirectories, but since we don't need it, I map it to the _ (single
underscore) variable which is convention for I'm not using this
part).

note1: notice the * in the def line, it makes the function accept a
variable number of arguments, so you can call it as collect_ext('.py')
but also as collect_ext('.py', '.pyw', '.pyo'). Inside the function,
what you've called it with is a list (or a tuple, I forget), which
means I can use the _in_ operator in the if test.

note2: I use os.path.join() to attach the directory name to the
filename before appending it to the result. It seems that might be
useful ;-)

hth,
-- bjorn

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


Re: How to find difference in years between two dates?

2006-07-29 Thread thebjorn
John Machin wrote:
 I don't understand. The examples that I showed went from the last day
 of a month to the last day of another month. [...]

Q1: is ((date-4days)+4days) == date?
Q2: is (((date-4days)+1month)+4days) == date+1month?

Ok, let's use Python'ish syntax (including numbering the days from 0
upwards, and backwards from -1, the last day of the month), you want
the last day of a month plus a month be the last day of the next
month. Simplistically, something like:

   month[-1] + 1 month == (month+1)[-1]   {last-to-last}

but that's obviously not the entire rule you want, unless 4-30 + 1
month == 5-31? So you would also like to have:

   month[i] + 1 month = (month+1)[i] {lock-step}

we'd like yesterday to be a day ago? So for suitable i:

   month[i] - 1 day == month[i-1]  {yesterday-1}
   month[0] - 1 day == (month-1)[-1]   {yesterday-2}

which leads to a natural definition for when tomorrow is:

   month[i] + 1 day == month[i+1]   {tomorrow-1}
   month[-1] + 1 day == (month+1)[0]{tomorrow-2}

So far so good. Now let's count backwards:

   month[-1] - 1 day == month[-2]  by: yesterday-1
   month[-2] - 1 day == month[-3]  by: yesterday-1
   month[-3] - 1 day == month[-4]  by: yesterday-1
   etc.

In other words, if you insist that the last day of the month is a well
defined concept and you want a day ago to be yesterday then month[-4],
the forth-to-last day of the month, is necessarily also well
defined. Having a well defined month[i], I'll apply your rules for
adding a month:

   month[-4] + 1 month == (month+1)[-4]   by: last-to-last

but you don't like this, because that means that e.g.:

   april[-4] + 1 month == may[-4]
   april[27] + 1 month == may[28]

which in addition to {lock-step}:

   april[27] + 1 month == may[27]

either gives an inconsistent, ill-formed, or FUZZY system (although
I would call it regular ;-)

My approach is simpler since it doesn't define addition, only
subtraction on valid dates, so if I switch to representing dates as
(month, day):

   (a, b) - (c, d) := a - c   iff b  d   {subtract}
 else a - c - 1

{subtract} is irregular but well defined for all valid dates (it will
always give you an answer, and it's always the same answer ;-) :

   (2,29) - (1,31) == 0
   (3,1) - (1,31) == 2

I can add day addition and still be ok:

   (m,d) + 1 day := (m,d+1){tomorrow-1}
   (m,-1) + 1 day := (m+1,0)   {tomorrow-2}
   (m,d) - 1 day := (m,d-1)   {yesterday-1}
   (m,0) - 1 day := (m-1,-1)  {yesterday-2}

Now my system is well-formed and consitent, even though it is
irregular, and it will answer yes to Q1 above. I can't see a way of
adding month addition to this and stay consistent without enumerating
special cases for every month, so Q2 can't even be asked in my system.

  You're entitled to your opinion.

 And you to yours :-)

Ok, I've explained why I hold mine... You care to do the same?

-- bjorn

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


Re: How to find difference in years between two dates?

2006-07-28 Thread thebjorn
John Machin wrote:
 Jan 31 to Feb 27: 27d (ex) 28d (in)
 Jan 31 to Feb 28: 28d (ex) 1m 1d (in)
 Jan 31 to Mar 01: 1m 1d (ex) 1m 2d (in)
 So 1 day short of 1m 1d is not 1m 0 d???

Exactly. Just as a person born on 1999-3-1 isn't a year old on
2000-2-29. Perfectly regular, consistent and reasonable.

 I'd call this unreasonable, inconsistent, anomalous -- especially
 when on the same website you do 1993-01-31 plus 1 month, it
 gives you 1993-02-28 (as I'd expect).

You're entitled to your opinion.

-- bjorn

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


Re: How to find difference in years between two dates?

2006-07-27 Thread thebjorn
John Machin wrote:
 thebjorn wrote:
  John Machin wrote:
   thebjorn wrote:
[...]
   Holy code bloat, Batman! Try this:
  
   return now.year - born.year - (birthday  now)
 
  yuck :-)

 But this:
 return now.year - born.year - (birthday  now and 1 or 0) is not yuck???

Correct.

  [...]
   It's the irregular-size months that cause the problems. If you can work
   out the months difference, then just floor_div by 12 to get the years
   difference.
 
  I don't agree that the irregular sized months cause a problem in this
  case. They do cause a problem if you're asking when is today + one
  month?, i.e. there isn't an unambiguous answer to that question in
  general (e.g. if today was January 31). We're asking a different kind
  of question though: has it been at least one month since January 31?,
  the answer would be no on Feb 29 and yes on Mar 1.

 If a bank were paying you interest on a monthly basis, and you
 deposited money on Jan 31 and pulled it out on the last day of
 February, that would count as one month. This is what I call the today
 - yesterday == 1 rule. For computing things like duration of employee
 service, you need the today - yesterday == 2 rule -- on the
 assumption that service counts from start of business yesterday to
 close of business today. So hire date of 1 Feb to fire date of (last
 day of Feb) would count as one month.

You give a good argument that the concept of a month is fuzzy, I still
don't believe that it makes the concept of a year fuzzy (or that the
fuzziness of month needs to be accounted for when computing years).

 Sorry, I don't understand. Why are you speechless? What would I want to
 use the calendar module for? Apart from the leap() function and the
 table of days in a month, the calendar module doesn't have any of the
 functionality that one would expect in a general-purpose date class.

Well, I thought replacing a 4 line function with 31 lines, 13 of which
duplicated functionality in the standard library was overkill... I came
up with this yesterday which seems sufficient?

def yeardiff(a, b):
  y = a.year - b.year
  if (a.month, a.day)  (b.month, b.day): # tuple comparison
  y -= 1
  return y 

-- bjorn

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


Re: How to find difference in years between two dates?

2006-07-27 Thread thebjorn
John Machin wrote:
 thebjorn wrote:
[...]
  You give a good argument that the concept of a month is fuzzy

 Sorry,  I can't imagine where you got fuzzy from. Perhaps you mean
 some other word. The concept is capable of being expressed precisely.

and the second to last date in January plus a month is..?

   Sorry, I don't understand. Why are you speechless? What would I want to
   use the calendar module for? Apart from the leap() function and the
   table of days in a month, the calendar module doesn't have any of the
   functionality that one would expect in a general-purpose date class.
 
  Well, I thought replacing a 4 line function with 31 lines, 13 of which
  duplicated functionality in the standard library was overkill.

 I think you missed the point that the lines I quoted were straight out
 of a self-contained library that existed (in C as well as Python) way
 before the datetime module was a gleam in Fred  the timbot's eyes.

I'm guessing you missed that I was looking for a method in the stdlib
to do this, and failing that an idiomatic solution...

 Even if I had noticed a leap year function in the calendar module, I
 would probably not have used it. The Python version of the module was
 just a stopgap while I fiddled with getting a C extension going. The C
 leap year function doesn't have any of that modulo stuff in it.

I'm sure it doesn't. You might want to look at the assembly your C
compiler produces for a modulo-power-of-2 operation...

  I came up with this yesterday which seems sufficient?
 
  def yeardiff(a, b):
y = a.year - b.year
if (a.month, a.day)  (b.month, b.day): # tuple comparison
y -= 1
return y

 At least it doesn't blow up when b is leapyear-02-29. It just gives the
 wrong answer when a is nonleapyear-02-28. E.g. it gives 0 years
 difference  from 1992-02-29 to 1993-02-28 instead of 1.

I believe you're mistaken (but feel free to correct me), my grandmother
is born on Feb 29 and a quick call to my dad verified that they
celebrated it the day after the 28th (unless Mar 1 was a Monday ;-).
http://timeanddate.com/date/duration.html also seem to agree with my
current understanding, just as a datapoint if nothing else.

-- bjorn

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


Re: How to find difference in years between two dates?

2006-07-27 Thread thebjorn
Bruno Desthuilliers wrote:
 Which conversion ? How do you get the data ? as a datetime object ? as a
 (y,m,d) tuple ? as a y-m-d string ? Else ?

All input routines, whether they're from a web-form, database, command
line, or anywhere else, only produce objects from the datetime module
for calendar data. That way the program logic doesn't have to guess
which format it's getting... I suppose I could do something like:

   def age(born):
mx_born = mx.DateTime.Date(born.year, born.month, born.day)
...

-- bjorn

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


How to find difference in years between two dates?

2006-07-26 Thread thebjorn
For the purpose of finding someone's age I was looking for a way to
find how the difference in years between two dates, so I could do
something like:

  age = (date.today() - born).year

but that didn't work (the timedelta class doesn't have a year
accessor).

I looked in the docs and the cookbook, but I couldn't find anything, so
I came up with:

  def age(born):
  now = date.today()
  birthday = date(now.year, born.month, born.day)
  return now.year - born.year - (birthday  now and 1 or 0)

i.e. calculate the raw years first and subtract one if he hasn't had
his birthday yet this year... It works, but I'd rather use a standard
and generic approach if it exists...?

-- bjorn

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


Re: How to find difference in years between two dates?

2006-07-26 Thread thebjorn
Roy Smith wrote:
 thebjorn [EMAIL PROTECTED] wrote:

def age(born):
now = date.today()
birthday = date(now.year, born.month, born.day)
return now.year - born.year - (birthday  now and 1 or 0)

 I don't get that last line.  There's two things in particular that are
 puzzling me.

 1) What does birthday  now mean?  It sounds like you haven't been born
 yet.

I'm making a (perhaps tenous) semantic distinction between birthdate,
the date you were born on, and birthday, an annual event that may or
may not have happened yet this year :-)

 2) I find the and 1 or 0 part very confusing.  I can't remember all the
 minor rules about operator precedence, but I'm sure this works out to some
 clever hack involving boolean short-circuit evaluation to get around the
 lack of a ternary operator in python.

You're correct :-)  the line was originally:

return now.year - born.year - (1 if birthday  now else 0)

which gave a nice traceback on the production server that didn't have
2.5 on it :-(  The and/or short-circuit is a fairly well established
(yet divisive) pydiom, and I was going to say something about people
really ought to learn a few simple precedence rules, but then I
realized the parenthesis aren't needed in the above ehm..  The
parenthesis are needed in a version someone else mentioned:

return now.year - born.year - (birthday  now)

but I wouldn't write that, just like I wouldn't write 1 + True..

 If I need to pull out the reference manual to decipher what an expression 
 means,
 it's too complicated.

Nah, that's a little too restrictive I think. I will agree that the
and/or is more cute than functional, at least in this case. Since it
could also throw, how about:

  def yeardiff(a, b):
  y = a.year - b.year
  if (a.month, a.day)  (b.month, b.day): # tuple comparison
  y -= 1
  return y

  def age(born): return yeardiff(date.today(), born)

 if birthday  now:
return now.year - born.year - 1
 else:
return now.year - born.year

I prefer to hoist the common expression out of the branches so they
don't have an opportunity to get out of sync, but I get your point.

-- bjorn

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


Re: How to find difference in years between two dates?

2006-07-26 Thread thebjorn
John Machin wrote:
 thebjorn wrote:
[...]
 
def age(born):
now = date.today()
birthday = date(now.year, born.month, born.day)

 Bad luck if the punter was born on 29 Feb and the current year is not a
 leap year.

Good catch! Thanks!

[..]
 Holy code bloat, Batman! Try this:

 return now.year - born.year - (birthday  now)

yuck :-)

[...]
 It's the irregular-size months that cause the problems. If you can work
 out the months difference, then just floor_div by 12 to get the years
 difference.

I don't agree that the irregular sized months cause a problem in this
case. They do cause a problem if you're asking when is today + one
month?, i.e. there isn't an unambiguous answer to that question in
general (e.g. if today was January 31). We're asking a different kind
of question though: has it been at least one month since January 31?,
the answer would be no on Feb 29 and yes on Mar 1.

 Below is some code from the ancient times when everybody and his dog
 each had their own date class :-)
[...]

Wow. I'm speechless. (any reason you didn't want to use the calendar
module?)

-- bjorn

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


Re: How to find difference in years between two dates?

2006-07-26 Thread thebjorn
Bruno Desthuilliers wrote:
[...]
 Possible solution:

 import mx.DateTime as dt
 def age(date):
 return dt.Age(dt.today(), date).years
 born = dt.Date(1967, 5, 1)
 assert age(born) == 39

dealbreaker:

 age(datetime.date(1970,5,2))
Traceback (most recent call last):
  File stdin, line 1, in ?
  File c:\python24\lib\site-packages\mx\DateTime\DateTime.py, line
842, in RelativeDateTimeDiff
diff = date1 - date2
TypeError: unsupported operand type(s) for -: 'DateTime' and
'datetime.date'

I'm getting data from a database, and conversions are out of the
question for something like this. Otherwise it's a fine library :-)

-- bjorn

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