Re: Compress a string

2008-05-20 Thread Marc 'BlackJack' Rintsch
On Tue, 20 May 2008 00:38:57 -0400, John Salerno wrote:

> def compress(s):
> new = []
> 
> for c in s:
> if c not in new:
> new.append(c)
> return ''.join(new)
> 
> 
> No, wait! I can do better!
> 
> def compress(s):
> new = []
> [new.append(c) for c in s if c not in new] return ''.join(new)
> 
> Wow, list comprehensions are cool.

And it's a misuse of list comprehension here IMHO because they are meant to
build lists, not to cram a ``for``/``if`` loop into a one liner.  You are
building a list full of `None` objects, just to throw it away.

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: python script to windows exe

2008-05-20 Thread sandeep
hi all

thanks for ur replies. i have a bit closer look at my code and i am
able to fix the problem.now my exe is working fine.the code is bit
more cleaner now as i removed lot of unused function from it and try
to document it also.


import win32com,win32com.client
import os,os.path
import codecs
import zipfile

[EMAIL PROTECTED]:::Sandeep Kumar Sharma

#outlook application refrence
outlook_app=0
#outlook ids to access different folders look into msdn for more info.
not a preffered way as i am hardcoding data here
OlDefaultFolders={'olFolderCalendar':9,'olFolderConflicts':
19,'olFolderContacts':10,'olFolderDeletedItems':3,'olFolderDrafts':
16,'olFolderInbox':6,'olFolderJournal':11,'olFolderJunk':
23,'olFolderLocalFailures':21,'olFolderNotes':12,'olFolderOutbox':
4,'olFolderSentMail':5,'olFolderServerFailures':
22,'olFolderSyncIssues':20,'olFolderTasks':
13,'olPublicFoldersAllPublicFolders':18}
#outlook types to save mailItem look into msdn for more info
#although doesnot work for me :-(
OlSaveAsType={'olTXT': 0,'olRTF':1,'olTemplate': 2,'olMSG': 3,'olDoc':
4,'olHTML':5,'olVCard': 6,'olVCal':7,'olICal': 8};

#refrence to content in inbox
inbox_obj=0

#function which will initialise outlook and return its reference
def getAppRef():
temp=win32com.client.Dispatch("OutLook.Application")
return temp.GetNamespace("MAPI")

#function to return the folders in the outlook
def getOutLookFolders(a,b=OlDefaultFolders['olFolderInbox']):
return a.GetDefaultFolder(b)

#function to get email content
def getMailContent(obj):
txt_file=codecs.open('data.html',encoding='utf-8',mode='w')
emailData=""
for kk in range(len(obj.Items)-1,0,-1):
print 'writting file='+str(kk)
mailItem=obj.Items[kk]
emailData=emailData+getEmailData(mailItem)
saveAttachments(mailItem.Attachments)
txt_file.write(getHTMLString(emailData))
txt_file.close()

#function which will return the emailItem data as form of String
def getEmailData(mailItem):
data=""
sender='SenderName'+checkStringType(mailItem.SenderName)
time='Time'+checkStringType(str(mailItem.ReceivedTime))
attachment='Attachments Count'+str(len(mailItem.Attachments))
edata='Email Content'+checkStringType(mailItem.Body)+""
dataToWrite=data+sender+time+attachment+edata
return dataToWrite

#function for saving the attachment.we are calling attachments
SaveAsFile to save the attachment.
#SaveAsFile is com method.for more info dig into msdn :-)
def saveAttachments(atmts):
for kk in range(1,len(atmts)):
atmt=atmts[kk]
abc=os.path.isdir(os.getcwd()+'\email')
if(abc==True):
print 'directory exists'
else:
os.mkdir(os.getcwd()+'\email')
path=os.path.abspath(os.getcwd()+'\email')
atmt.SaveAsFile(path+"\\"+atmt.DisplayName)

# function to check whether the character encoding is ascii or smthing
else
def checkStringType(a):
if isinstance(a,str):
   b='not a unicode string'
else:
a.encode('utf-8')
#print 'unicode type'
return a

#bit of html stuff so that i can see my output on browsers.
def getHTMLString(emailData):
a='Your Email Data log is here'+emailData+''
return a

#main entrance to the program
def main():
global outlook_app,inbox_obj
outlook_app=getAppRef()
inbox_obj=getOutLookFolders(outlook_app)
getMailContent(inbox_obj)

main()

once again thanks for your help.

thanks and regards
sandeep kumar sharma
--
http://mail.python.org/mailman/listinfo/python-list


Re: scaling problems

2008-05-20 Thread Marc 'BlackJack' Rintsch
On Tue, 20 May 2008 13:57:26 +1000, James A. Donald wrote:

> The larger the program, the greater the likelihood of inadvertent name
> collisions creating rare and irreproducible interactions between
> different and supposedly independent parts of the program that each
> work fine on their own, and supposedly cannot possibly interact.

How should such collisions happen?  You don't throw all your names into
the same namespace!?

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: scaling problems

2008-05-20 Thread Marc 'BlackJack' Rintsch
On Tue, 20 May 2008 10:47:50 +1000, James A. Donald wrote:

> 2.  It is not clear to me how a python web application scales.

Ask YouTube.  :-)

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: explain this function to me, lambda confusion

2008-05-20 Thread Bruno Desthuilliers

Paul McGuire a écrit :

On May 19, 11:04 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:

Paul McGuire <[EMAIL PROTECTED]> writes:

[...]

Could you use it as a decoratore instead?

integer = Word("0123456789")

@integer.setParseAction
def parse_integer(tokens):
return int(tokens[0])

I could make your grammar clearer, because you don't mix it with
processing code... and no need for lambdas!


What a sexy little idiom!  You could really apply this to any API
method that accepts a callable as a single argument, and pyparsing
actually has several of these:

setParseAction
addParseAction
setFailAction
setDebugActions

(Unfortunately, setDebugActions requires 3 callables for its arguments
- one to be run when an expression is about to be parsed, one to be
run after parsing is complete, and one to be run if the expression
fails to be parsed.  So setDebugActions can't be used in this
decorator manner.)


You just have to provide three decorators instead, one for each callback.



Using these methods as decorators deviates from the typical decorator
usage model as I understand it - instead of wrapping the provided
function within some enclosing setup/teardown code (like lock/unlock,
or open-file/close-file, or begin-transaction/commit-transaction), and
then returning the created wrapper function, the decorator usage you
propose is one in which the decorator uses the provided function with
some side-effect (such as setting a property), but then just returns
the original function.


This is already a well-known decorator pattern. It's used in CherryPy to 
mark methods that are exposed as request handlers.


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


Re: test mult vars to same value, how to shorten expr?

2008-05-20 Thread Peter Otten
[EMAIL PROTECTED] wrote:

>  if i want o test:
> if a == 5 and b ==5 and c==5 ... z==5
> 
> is there some synctactic suagr for this?
> 
> rather than maiking one of my own i mean, something built-in like:
> if a,b,c... z == 5:

if all(x == 5 for x in a,b,c,...):
   print "yep"


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


Re: conventions/requirements for 'is' vs '==', 'not vs '!=', etc

2008-05-20 Thread Bruno Desthuilliers

[EMAIL PROTECTED] a écrit :

i am confused.

x=5
y=5

x==y -> True
x is y -> True

shouldnt x is y return False since they shouldnt(dont?) point to the
same place in memory, they just store an equal value?


Python's "variable" do not "store values", they are name to object 
bindings. x = 5 is a shortand for globals()['x'] = int(5), which means 
"create an int instance with value 5 and bind it to the name 'x' in the 
global namespace'. wrt/ the identity test yielding true, it's the result 
of a CPython specific optimisation for small integer objects, that are 
cached and reused. Since Python integers are immutable, they can safely 
be shared. Now since it's an implementation specific thing, you should 
by no mean rely on it.


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


Re: Using Python for programming algorithms

2008-05-20 Thread Bruno Desthuilliers

Henrique Dante de Almeida a écrit :

On May 19, 5:35 pm, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:

 The situation would be simpler if there were good well-known toolkits
for optimization in python (like numpy for matrix operations), but
that's not the case.

There's at least Psyco (if you're willing and able to restrict
yourself from using some of the most dynamic parts of Python - which
might not be a problem here).  One could also mention stuff like Pyrex
and Cython.


 I meant toolkits for "optimization problems", not "code
optimization".


oops, sorry.
--
http://mail.python.org/mailman/listinfo/python-list


Professional Grant Proposal Writing Workshop (August 2008: Manchester, New Hampshire)

2008-05-20 Thread Anthony Jones


The Grant Institute's Grants 101: Professional Grant Proposal Writing Workshop
 will be held in Manchester, New Hampshire on August 6 - 8, 2008.  Interested development professionals, researchers, faculty, and graduate students should register as soon as possible, as demand means that seats will fill up quickly. Please forward, post, and distribute this e-mail to your colleagues and listservs. 
 
All participants will receive certification in professional grant writing from the Institute. For more information call (888) 824 - 4424 (213-817-5308 outside US) or visit The Grant Institute at www.thegrantinstitute.com.
 
Please find the program description below:
 
The Grant Institute
Grants 101: Professional Grant Proposal Writing Workshop
will be held in 
Manchester, New Hampshire
August 6 - 8, 2008
8:00 AM - 5:00 PM
 

The Grant Institute's Grants 101 course is an intensive and detailed introduction to the process, structure, and skill of professional proposal writing. This course is characterized by its ability to act as a thorough overview, introduction, and refresher at the same time. In this course, participants will learn the entire proposal writing process and complete the course with a solid understanding of not only the ideal proposal structure, but a holistic understanding of the essential factors, 
which determine whether or not a program gets funded. Through the completion of interactive exercises and activities, participants will complement expert lectures by putting proven techniques into practice. This course is designed for both the beginner looking for a thorough introduction and the intermediate looking for a refresher course that will strengthen their grant acquisition skills. This class, simply put, is designed to get results by creating professional grant proposal writers. 

 
Participants will become competent program planning and proposal writing professionals after successful completion of the Grants 101 course. In three active and informative days, students will be exposed to the art of successful grant writing practices, and led on a journey that ends with a masterful grant proposal. 
 
Grants 101 consists of three (3) courses that will be completed during the three-day workshop. 
 
(1) Fundamentals of Program Planning
 
This course is centered on the belief that "it's all about the program." This intensive course will teach professional program development essentials and program evaluation. While most grant writing "workshops" treat program development and evaluation as separate from the writing of a proposal, this class will teach students the relationship between overall program planning and grant writing. 
 
(2) Professional Grant Writing
 

Designed for both the novice and experienced grant writer, this course will make each student an overall proposal writing specialist. In addition to teaching the basic components of a grant proposal, successful approaches, and the do's and don'ts of grant writing, this course is infused with expert principles that will lead to a mastery of the process. Strategy resides at the forefront of this course's intent to illustrate grant writing as an integrated, multidimensional, and dynamic endeavor. 
Each student will learn to stop writing the grant and to start writing the story. Ultimately, this class will illustrate how each component of the grant proposal represents an opportunity to use proven techniques for generating support.
 
(3) Grant Research
 

At its foundation, this course will address the basics of foundation, corporation, and government grant research. However, this course will teach a strategic funding research approach that encourages students to see research not as something they do before they write a proposal, but as an integrated part of the grant seeking process. Students will be exposed to online and database research tools, as well as publications and directories that contain information about foundation, corporation, and 
government grant opportunities. Focusing on funding sources and basic social science research, this course teaches students how to use research as part of a strategic grant acquisition effort.
 
Registration
$597.00 tuition includes all materials and certificates.
 
Each student will receive:
*The Grant Institute Certificate in Professional Grant Writing
*The Grant Institute's Guide to Successful Grant Writing
*The Grant Institute Grant Writer's Workbook with sample proposals, forms, and outlines
 
Registration Methods
 
1) On-Line - Complete the online registration form at www.thegrantinstitute.com under Register Now. We'll send your confirmation by e-mail. 
 
2) By Phone - Call (888) 824 - 4424 (213-817-5308 outside US) to register by phone. Our friendly Program Coordinators will be happy to assist you and answer your questions. 
 
3) By E-mail - Send an e-mail with your name, organization, and basic contact information to [EMAIL PROTECTED] and we will reserve your slot and send your Confirmation Pack

Re: explain this function to me, lambda confusion

2008-05-20 Thread Arnaud Delobelle
Bruno Desthuilliers <[EMAIL PROTECTED]> writes:

> Paul McGuire a écrit :
>> On May 19, 11:04 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:
>>> Paul McGuire <[EMAIL PROTECTED]> writes:
>>>
>>> [...]
>>>
>>> Could you use it as a decoratore instead?
>>>
>>> integer = Word("0123456789")
>>>
>>> @integer.setParseAction
>>> def parse_integer(tokens):
>>> return int(tokens[0])
>>>
>>> I could make your grammar clearer, because you don't mix it with
>>> processing code... and no need for lambdas!
>>
>> What a sexy little idiom!  You could really apply this to any API
>> method that accepts a callable as a single argument, and pyparsing
>> actually has several of these:
>>
>> setParseAction
>> addParseAction
>> setFailAction
>> setDebugActions
>>
>> (Unfortunately, setDebugActions requires 3 callables for its arguments
>> - one to be run when an expression is about to be parsed, one to be
>> run after parsing is complete, and one to be run if the expression
>> fails to be parsed.  So setDebugActions can't be used in this
>> decorator manner.)
>
> You just have to provide three decorators instead, one for each callback.
>
>>
>> Using these methods as decorators deviates from the typical decorator
>> usage model as I understand it - instead of wrapping the provided
>> function within some enclosing setup/teardown code (like lock/unlock,
>> or open-file/close-file, or begin-transaction/commit-transaction), and
>> then returning the created wrapper function, the decorator usage you
>> propose is one in which the decorator uses the provided function with
>> some side-effect (such as setting a property), but then just returns
>> the original function.
>
> This is already a well-known decorator pattern. It's used in CherryPy
> to mark methods that are exposed as request handlers.

Actually, IIRC the decorator provided by CherryPy is something like
(names are probably wrong, as I haven't looked at CherryPy for a
while):

def exposed(f):
f.is_exposed = True
return f

So it doesn't mutate anything else than the function that it
decorates, but changes slightly how the world sees that function.
This is in line with classic decorators such as staticmethod,
classmethod, etc.

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


fafdasf

2008-05-20 Thread marco furchi

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

Re: scaling problems

2008-05-20 Thread Graham Dumpleton
On May 20, 2:00 pm, James A. Donald <[EMAIL PROTECTED]> wrote:
> > > 2.  It is not clear to me how a python web application scales.  Python
> > > is inherently single threaded, so one will need lots of python
> > > processes on lots of computers, with the database software handling
> > > parallel accesses to the same or related data.  One could organize it
> > > as one python program for each url, and one python process for each
> > > http request, but that involves a lot of overhead starting up and
> > > shutting down python processes.  Or one could organize it as one
> > > python program for each url, but if one gets a lot of http requests
> > > for one url, a small number of python processes will each sequentially
> > > handle a large number of those requests.  What I am really asking is:
> > > Are there python web frameworks that scale with hardware and how do
> > > they handle scaling?
>
> Reid Priedhorsky
>
> > This sounds like a good match for Apache withmod_python.
>
> I would hope that it is, but the question that I would like to know is
> how does mod_python handle the problem - how do python programs and
> processes relate to web pages and http requests when one is using mod_python, 
> and what happens when one has quite a lot of web pages and
> a very large number of http requests?

Read:

  http://blog.dscpl.com.au/2007/09/parallel-python-discussion-and-modwsgi.html
  http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading

They talk about multi process nature of Apache and how GIL is not as
big a deal when using it.

The latter document explains the various process/threading modes when
using Apache/mod_wsgi. The embedded modes described in that
documentation also apply to mod_python.

The server is generally never the bottleneck, but if you are paranoid
about performance, then also look at relative comparison of mod_wsgi
and mod_python in:

  http://code.google.com/p/modwsgi/wiki/PerformanceEstimates

Graham

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


Re: addendum Re: working with images (PIL ?)

2008-05-20 Thread Ivan Illarionov
On Mon, 19 May 2008 10:18:00 -0400, Poppy wrote:

> Thanks, since posting I  figured out how to interpret the histogram
> results, which seems to be the consensus in responses. I wrote a check
> image program and have been periodically calling it against a folder
> where I make a copy of our images used for production. My method right
> now is to check what we send for errors, but is not preventive.
> 
> Also I determined whitespace is not the only issue, any color that
> dominates. I'm considering rewriting this code below to setup bins, so
> if combined neighboring colors exceeds the threshold then reject the
> image. I have examples where half the image appears black, but actually
> varies throughout.
> 
> Since my image is RGB I'm looping through a 768 element list.

I suggest:
1. convert to greyscale
2. posterize
3. check the max(im.histogram())

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


compressing short strings?

2008-05-20 Thread Paul Rubin
I have a lot of short English strings I'd like to compress in order to
reduce the size of a database.  That is, I'd like a compression
function that takes a string like (for example) "George Washington"
and returns a shorter string, with luck maybe 6 bytes or so.  One
obvious idea is take the gzip function, compress some large text
corpus with it in streaming mode and throw away the output (but
setting up the internal state to model the statistics of English
text), then put in "George Washington" and treat the additional output
as the compressed string.  Obviously to get reasonable speed there
would have to be a way to save the internal state after initializing
from the corpus.

Anyone know if this has been done and if there's code around for it?
Maybe I'm better off with freezing a dynamic Markov model?  I think
there's DMM code around but am not sure where to look.

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


Re: Running commands on cisco routers using python

2008-05-20 Thread Hartmut Goebel

Mike Driscoll schrieb:

On May 19, 10:18 am, SPJ <[EMAIL PROTECTED]> wrote:

Is it possible to run specific commands on cisco router using Python?
I have to run command "show access-list" on few hundred cisco routers and get 
the dump into a file. Please let me know if it is feasible and the best way to achieve 
this.

Thanks,
SPJ


I think it depends on how you're connecting to the routers.
Theoretically, you should be able to use Python's socket or ssh
modules to connect to them, send commands and capture the output.


I've implemented a toolset for login into cisco components, issueing 
commands and fetchign the results. Works like a charm.


Keypoint here is using pexpect for interacting with SSH.

--
Schönen Gruß - Regards
Hartmut Goebel

Goebel Consult
Spezialist für IT-Sicherheit in komplexen Umgebungen
http://www.goebel-consult.de
--
http://mail.python.org/mailman/listinfo/python-list


Re: Compress a string

2008-05-20 Thread Bruno Desthuilliers

Salvatore DI DI0 a écrit :
(top-post corrected - Salvatore, please, don't top-post)
"Matt Porter" <[EMAIL PROTECTED]> a écrit dans le message de news: 
[EMAIL PROTECTED]

Hi guys,

I'm trying to compress a string.
E.g:
 "BBBC" -> "ABC"


> Try this
>
> t = set("bbc")
> list(t)

Won't keep the ordering.


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


Re: Compress a string

2008-05-20 Thread Bruno Desthuilliers

Matt Porter a écrit :

Hi guys,

I'm trying to compress a string.
E.g:
 "BBBC" -> "ABC"

The code I have so far feels like it could be made clearer and more 
succinct, but a solution is currently escaping me.



def compress_str(str):


using 'str' as an indentifier will shadow the builtin str type.


new_str = ""
for i, c in enumerate(str):
try:
if c != str[i+1]:
new_str += c
except IndexError:
new_str += c
return new_str




Now everyone gave smart solutions, may I suggest the stupidier possible one:

def compress_string(astring):
   compressed = []
   for c in astring:
   if c not in compressed:
   compressed.append(c)
   return ''.join(compressed)

Not elegant, but at least very clear.
--
http://mail.python.org/mailman/listinfo/python-list


Re: TPCServer and xdrlib

2008-05-20 Thread Nick Craig-Wood
Henrique Dante de Almeida <[EMAIL PROTECTED]> wrote:
>  On May 19, 10:28?am, Laszlo Nagy <[EMAIL PROTECTED]> wrote:
> > I cannot predict "acceptable speed" requirements, but I can tell that
> > there will be some clients downloading 100MB report files from the
> > server, so I presume that I will need a progress bar. I think that I
> > need to develop my own protocol for this, and probably the underlying
> 
>   Okay, so you need to wrap large binary files in some kind of message,
>  without pre processing them. I think developing your own protocol
>  using XDR is a safe bet.

You might want to consider using netstrings rather than XDR

  http://cr.yp.to/proto/netstrings.txt

They are very simple and would be minimal overhead if all you are
passing is a file and a bit of metadata.

You'll find several modules for python with a bit of searching.  Also
I believe twisted supports them directly or you could easily roll your
own.

-- 
Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: scaling problems

2008-05-20 Thread Nick Craig-Wood
Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>  On Tue, 20 May 2008 13:57:26 +1000, James A. Donald wrote:
> 
> > The larger the program, the greater the likelihood of inadvertent name
> > collisions creating rare and irreproducible interactions between
> > different and supposedly independent parts of the program that each
> > work fine on their own, and supposedly cannot possibly interact.
> 
>  How should such collisions happen?  You don't throw all your names into
>  the same namespace!?

If you ever did a lot of programming in C with large projects you have
exactly that problem a lot - there is only one namespace for all the
external functions and variables, and macro definitions from one
include are forever messing up those from another.  I suspect the OP
is coming from that background.

However python doesn't have that problem at all due to its use of
module namespaces - each name is confined to within a module (file)
unless you take specific action otherwise, and each class attribute is
confined to the class etc.

>From the Zen of Python "Namespaces are one honking great idea -- let's
do more of those!" - as a battle scarred C programmer I'd agree ;-)

-- 
Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using Python for programming algorithms

2008-05-20 Thread Ivan Illarionov
On Mon, 19 May 2008 08:53:11 -0700, Henrique Dante de Almeida wrote:

> On May 19, 6:52 am, Bruno Desthuilliers  [EMAIL PROTECTED]> wrote:
>> Henrique Dante de Almeida a écrit :
>>
>> > On May 17, 7:32 pm, Vicent Giner <[EMAIL PROTECTED]> wrote:
>> >> Hello.
>>
>> (snip)
>> >> However, it is usually said that Python is not a compiled but
>> >> interpreted programming language —I mean, it is not like C, in that
>> >> sense.
>>
>> (snip)
>> >  I guess that python is not a good language for that.
>> (snip)
>> >  My opinion: choose compiled or byte compiled languages.
>>
>> Slightly OT (ie : not talking about computation-heavy alorgithm being
>> better implemented in C then wrapped in Python - this seems quite
>> obvious) but just a couple facts:
>>
>> 1/ being interpreted or compiled (for whatever definition of these
>> terms) is not a property of a language, but a property of an
>> implementation of a language.
>>
>> 2/ actually, all known Python implementations compile to byte-code.
> 
>  Yes, I was actually referring to statically typed JIT-compiled
> languages. Sorry about that, blame the beers that entered my digestive
> system that night. :-P

[beer.blame() for beer in beers]

if len(beers) > 2:
news_reader.stop_working()

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

Re: Using Python for programming algorithms

2008-05-20 Thread Ivan Illarionov
On Mon, 19 May 2008 11:07:06 -0700, Vicent Giner wrote:
[...]
> 
> By the way, is it possible (and easy) to call a C function from a Python
> program??

Yes.

http://groups.google.com/group/comp.lang.python/msg/9d47913a265c348a

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


Re: conventions/requirements for 'is' vs '==', 'not vs '!=', etc

2008-05-20 Thread Ulrich Eckhardt
Asun Friere wrote:
> Well you have to be careful in case some smartarse comes back with a
> class with something like this in it:
> 
> def __eq__ (self, other) :
> if self is other : return False

That's pretty paranoid. :)
Who said that? (:

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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

Re: compressing short strings?

2008-05-20 Thread Arnaud Delobelle
Paul Rubin  writes:

> I have a lot of short English strings I'd like to compress in order to
> reduce the size of a database.  That is, I'd like a compression
> function that takes a string like (for example) "George Washington"
> and returns a shorter string, with luck maybe 6 bytes or so.  One
> obvious idea is take the gzip function, compress some large text
> corpus with it in streaming mode and throw away the output (but
> setting up the internal state to model the statistics of English
> text), then put in "George Washington" and treat the additional output
> as the compressed string.  Obviously to get reasonable speed there
> would have to be a way to save the internal state after initializing
> from the corpus.
>
> Anyone know if this has been done and if there's code around for it?
> Maybe I'm better off with freezing a dynamic Markov model?  I think
> there's DMM code around but am not sure where to look.
>
> Thanks.

Out of the blue idea which is probably rubbish:

Create a tree of your words, look at it as a Deterministic Finite
Automaton (DFA), minimise it, you get what some call a DAWG. That
works very well with lots of small words.

e.g.

Words aabc, babc, bbbc.

Minimal DFA:

1 -a-> 2 -a-> 4 -b-> 5 -c-> 6
\ ^
 b-> 3 -a-+
  \  /
   b+


To compress a word, simply find its position in alphabetical order,
call that its 'path'.

The 'decompression algorithm' is then (in pseudo-python)

def decompress(DFA, path):
letters = []
state = DFA.initial_state
while not state.is_final():
transitions = state.transitions
path, choice = divmod(path, len(transitions))
state = transitions[choice].new_state
letters.append(transitions[choice].letter)

I'm not too sure about this :)

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


Re: Classmethods are evil

2008-05-20 Thread Ivan Illarionov
On Mon, 19 May 2008 13:53:31 -0700, [EMAIL PROTECTED] wrote:

> On 17 mai, 11:50, Ivan Illarionov <[EMAIL PROTECTED]> wrote:
>> On Sat, 17 May 2008 02:33:13 -0300, Gabriel Genellina wrote:
>> > En Sat, 17 May 2008 01:01:50 -0300, Ivan Illarionov
>> > <[EMAIL PROTECTED]> escribió:
>>
>> >> After re-reading "Python is not Java" I finally came to conclusion
>> >> that classmethods in Python are a very Bad Thing.
>>
>> >> I can't see any use-case of them that couldn't be re-written more
>> >> clearly with methods of metaclass or plain functions.
>>
>> > A good use case for class methods are alternate constructors, like
>> > dict.from_keys. I don't think an alternate constructor would be more
>> > clear being a method of the metaclass - actually it belongs to the
>> > class itself, not to its metaclass.
>> > Metaclass methods are harder to find; they don't show in
>> > dir(instance) nor dir(class).
>> > Also the resolution order is harder to grasp for metaclasses - but
>> > this may be just lack of usage from my part...
>>
>> >> They have the following issues:
>> >> 1. You mix instance-level and class-level functionality in one place
>> >> making your code a mess.
>>
>> > Not necesarily; some classmethods are naturally tied to the class
>> > itself, not to the metaclass (like the constructor example above).
>> > But yes, *some* classmethods could be written as methods of their
>> > metaclass instead - but that doesn't always make sense.
>>
>> >> 2. They are slower than metaclass methods or plain functions.
>>
>> > Hu? How did you come to that?
>> > I've done a small test and a class method wins by a very minuscule
>> > but consistent advantage over a metaclass method:
>>
>> > class A(object):
>> >  color = "red"
>>
>> >  @classmethod
>> >  def foo(cls, x):
>> >  return getattr(cls, x)
>>
>> > class MetaB(type):
>> >  def foo(self, x):
>> >  return getattr(self, x)
>>
>> > class B(object):
>> >  __metaclass__ = MetaB
>> >  color = "red"
>>
>> > C:\TEMP>python -m timeit -s "from meta3 import A,B;a,b=A(),B()"
>> > "A.foo('color')"
>> > 100 loops, best of 3: 1.19 usec per loop
>>
>> > C:\TEMP>python -m timeit -s "from meta3 import A,B;a,b=A(),B()"
>> > "B.foo('color')"
>> > 100 loops, best of 3: 1.2 usec per loop
>>
>> How did I come to this:http://code.djangoproject.com/changeset/7098
>>
>> I measured this and there was a marginal speed increase when
>> classmethods wher moved to metaclass.
> 
> IIRC (please correct me if I'm wrong), this part of code is only called
> when the class is created - in which case it makes sense to move it
> where it belongs, ie to the metaclass. This is by no mean a use case for
> classmethods.

Yes, this is not the use case for class methods, but they where used 
there. The whole point of my post was to say that classmethods are used 
in a wrong way too often and most of Python programmers don't know that 
the same thing can be implemented with metaclass methods.

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

Re: compressing short strings?

2008-05-20 Thread Thomas Troeger

Paul Rubin wrote:

I have a lot of short English strings I'd like to compress in order to
reduce the size of a database.  That is, I'd like a compression
function that takes a string like (for example) "George Washington"


[...]



Thanks.


I think your idea is good, maybe you'd want to build an LZ78 encoder in 
Python (LZ78 is pretty easy), feed it with a long English text and then 
pickle the resulting object. You could then unpickle it on program start 
and encode your short strings with it. I bet there's a working 
implementation around that already that does it ... but if you can't 
find any, LZ78 is implemented in 1 or 2 hours. There was a rather good 
explanation of the algorithm in German, unfortunately it's vanished from 
the net recently (I have a backup if you're interested).


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


Embedding Python question.

2008-05-20 Thread Thomas Troeger

Dear all,

I've successfully embedded the Python interpreter into a set of C/C++ 
application programs that use a larger library project with information 
from http://docs.python.org/api/api.html and 
http://docs.python.org/ext/ext.html. Now I want to wrap classes and 
functions from the associated libraries so that I can write new 
applications completely in Python, but I'm not entirely sure how to 
start because I have some problems understanding which is the best way. 
It would be nice if someone could answer the following questions and 
clarify this matter:


- until now I've used the approach as documented in 
http://docs.python.org/ext/extending-with-embedding.html to extend the 
embedded interpreter and that works pretty well. I'd like to use a 
similar approach for other C applications. Can I write a C library that 
implements this technique and link it into all C applications that need 
Python support, or is there a better, more elegant way?


- in the documentation, there's an example that illustrates the creation 
of a python module for pure extending of a python script (the Noddy 
stuff). Do I have to make a separate module for each library I want to 
wrap? If yes, how can I manage the case where two libraries can access 
each other?


- if I write an extension module, how can I handle the case where the C 
wrapper methods want to call back into the same Python interpreter instance?


- I think my questions break down into a reference to documentation 
where a similar problem is explained in detail. Does anyone have such 
information?


Thanks!
Thomas.
--
http://mail.python.org/mailman/listinfo/python-list


Re: conventions/requirements for 'is' vs '==', 'not vs '!=', etc

2008-05-20 Thread Bruno Desthuilliers

Gabriel Genellina a écrit :
En Mon, 19 May 2008 17:58:48 -0300, [EMAIL PROTECTED] 
<[EMAIL PROTECTED]> escribió:



On 19 mai, 22:29, Luis Zarrabeitia <[EMAIL PROTECTED]> wrote:
(snip)

The main
concept here: identity [usually] implies equality,


I really enjoyed the "usually" disclaimer !-)


In some *rare* cases, identity does not imply equality:


Yeps. That's exactly why I enjoyed this disclaimer...

(snip nan example).
--
http://mail.python.org/mailman/listinfo/python-list


Re: conventions/requirements for 'is' vs '==', 'not vs '!=', etc

2008-05-20 Thread Duncan Booth
John Salerno <[EMAIL PROTECTED]> wrote:

 a = 'this is longer'
 b = 'this is longer'
 a == b
> True
 a is b
> False
 
> 
> In the above example, Python has created only one string called
> 'hello' and both x and y reference it. However, 'this is longer' is
> two completely different objects. 

That is true when run interactively, but the behaviour changes again if you 
run it as a script:

C:\Temp>type t.py
a = 'this is longer'
b = 'this is longer'
print a is b

C:\Temp>t
True

In short, two equal strings may or may not be identical and any code which 
makes assumptions based on observed behaviour is broken. If you want to be 
able to test identity on strings safely then use the 'intern()' builtin to 
get repeatable behaviour.

-- 
Duncan Booth http://kupuguy.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Newbie In Python

2008-05-20 Thread andrew . smith . cpp
I have Heard About "Python" its a OOD Language. i have to Learn it
where from i should start it.
i have python compiler at linux Platform.
anyone can suggest me about it.

Thanks In advance.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Classmethods are evil

2008-05-20 Thread Bruno Desthuilliers

Ivan Illarionov a écrit :

On Mon, 19 May 2008 13:53:31 -0700, [EMAIL PROTECTED] wrote:


On 17 mai, 11:50, Ivan Illarionov <[EMAIL PROTECTED]> wrote:

(snip)

How did I come to this:http://code.djangoproject.com/changeset/7098

I measured this and there was a marginal speed increase when
classmethods wher moved to metaclass.

IIRC (please correct me if I'm wrong), this part of code is only called
when the class is created - in which case it makes sense to move it
where it belongs, ie to the metaclass. This is by no mean a use case for
classmethods.


Yes, this is not the use case for class methods, but they where used 
there. 


Don't blame the tool for being misused.

The whole point of my post was to say that classmethods are used 
in a wrong way too often


I think it's the first time I see such a misuse of classmethods, and I 
have read quite a lot of (sometimes pretty hairy) Python code.


 and most of Python programmers don't know that 
the same thing can be implemented with metaclass methods.


I'd say that most Python programmers using metaclasses know when to use 
a metaclass method and when to use a classmethod. One case of (slight) 
misuse is certainly not enough to infer a general rule, and that doesn't 
make classmethods bad in anyway. And while we're at it, I'd consider 
using a custom metaclass only to implement the equivalent of a 
classmethod a misuse too - and in this case, kind of a WTF.


As a last point, there's at least one very big difference between a 
metaclass method and a classmethod, which is that you cannot call a 
metaclass method on an instance:


class MyMeta(type):
def bar(cls):
print "%s.bar()" % cls

class Foo(object):
__metaclass__ = MyMeta

@classmethod
def baaz(cls):
print "%s.baaz()" % cls

Foo.baaz()
Foo.bar()

f = Foo()
f.baaz()
f.bar()

=>

.baaz()
.bar()
.baaz()
Traceback (most recent call last):
  File "", line 1, in 
  File "/tmp/python-18506gCT.py", line 17, in 
f.bar()
AttributeError: 'Foo' object has no attribute 'bar'

I've had use case where I needed to call classmethods on instances, 
using a metaclass method would have required an explicit call, ie 
type(obj).some_method() instead of obj.some_method(), which would have 
uselessly exposed this knowledge to client code.




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

subprocess - please enhance the documentation

2008-05-20 Thread Helmut Jarausch

Hi,

I'd like to suggest to add a few lines to chapter 17.1.3.5 "Replacing os.popen" 
(Python Doc 2.5.2)


I have used the following code in the past

ARC='MyDumpFile'
tar_inp= os.popen('/bin/tar cjf '+ARC+' -T -','w')

tar_exit_code= tar_inp.close()
if  tar_exit_code != None and tar_exit_code % 256 :
  print "some error messages"

When replacing this - as suggested - by

TAR= Popen(('/bin/tar','cjf',ARC,'-T','-'),stdin=PIPE)
tar_inp= TAR.stdin

tar_inp.close() always returns None.

So I have replaced this by

tar_inp.close()
tar_exit_code= TAR.wait()

if  tar_exit_code != 0 :
  print "some error messages"


Perhaps a few lines telling about this would be helpful.

Thanks,
Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie In Python

2008-05-20 Thread Thomas Troeger

[EMAIL PROTECTED] wrote:

I have Heard About "Python" its a OOD Language. i have to Learn it
where from i should start it.
i have python compiler at linux Platform.
anyone can suggest me about it.

Thanks In advance.


How about http://docs.python.org/tut/tut.html?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie In Python

2008-05-20 Thread Bruno Desthuilliers

[EMAIL PROTECTED] a écrit :
I have Heard About "Python" its a OOD Language. 


'OOD' => 'object oriented ???' ?


i have to Learn it
where from i should start it.


Err... What about reading the docs on python.org - possibly starting 
with the tutorial:

http://docs.python.org/tut/tut.html

You'll find other tutorial etc here:
http://www.python.org/doc/



i have python compiler


Just for the record, "python" is also the name of a compiler for CMU 
Common Lisp (totally unrelated to the Python language), so make sure 
that what you have is the CPython language install.


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


Report Lab Image Flowable Issue ..

2008-05-20 Thread dbee
I'm try to generate a report that will span multiple pages and have
dynamic content using python reportlab. I have no issues with regards
to generating images and then using p.drawInlineImage(signup_img,
100,150)  to draw them onto the canvas.

The problem that i have comes when i try to create flowables so that
my layout can span multiple pages. The images themselves never
actually show up on the page ...

# Import pdf
generator
from cStringIO import StringIO
from reportlab.pdfgen import canvas
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import Paragraph, Frame
from PIL import Image

# Create the HttpResponse object with the appropriate PDF
headers.
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment;
filename=somefilename.pdf'

buffer = StringIO()

styles = getSampleStyleSheet()
styleN = styles['Normal']
styleH = styles['Heading1']
story = []

#add some
flowables
story.append(Paragraph("This is a Heading",styleH))
story.append(Paragraph("""This is a paragraph in  style"\
"",styleN))

c = Canvas(buffer)
f = Frame(inch, inch, 6*inch, 9*inch, showBoundary=1)
f.addFromList(story,c)
c.save()

# Get the value of the StringIO buffer and write it to the
response.
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)

This should generate a pdf with the google image in it no ? The pdf
gets generated but no image comes out.

When I try the other option of using image flowables, I get only an
error. The reportlab docs example shows an Image flowable being called
like ... Image("lj8100.jpg")    but when i try to call the object
directly i get an error which suggests that i should use Image.new or
Image.open to call the object ...

# Create image from /tmp
im = Image("/tmp/car_dealership.jpg", width=2*inch, height=2*inch)
im.hAlign = 'CENTER'

#add some
flowables
story.append(Paragraph("This is a Heading",styleH))
story.append(Image(im))

TypeError: 'module' object is not callable for the im = Image( )

When i try to feed the image manually ...

interest_image = interests_barchart(request)
im = Image.open(StringIO(interest_image.content))

#add some
flowables
story.append(Paragraph("This is a Heading",styleH))
story.append(im)

I get ... AttributeError: getSpaceBefore... which seems to suggest
that im isn't a flowable ...

Can anyone help pls ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do *you* use Python in non-GUI work?

2008-05-20 Thread David C. Ullrich
On Sun, 18 May 2008 18:20:22 -0400, John Salerno
<[EMAIL PROTECTED]> wrote:

>Hey all. Just thought I'd ask a general question for my own interest. Every 
>time I think of something I might do in Python, it usually involves creating a 
>GUI interface, so I was wondering what kind of work you all do with Python 
>that does *not* involve any GUI work. This could be any little scripts you 
>write for your own benefit, or what you do at work, if you feel like talking 
>about that! :)

You might get a keyboard with an Enter key, btw. Anyway:

I'm a math professor, not a programmer.
I use Python probably every day to do
all sorts of things, for example:

(i) Calculating grades. (Ok, this could be done in
Excel. But if you know Python anyway you don't need
to figure out how to do various things in Excel.
Quick: In Excel how do you take all the quiz
scores, drop the lowest _two_ of them and average
the rest? Stumps people sometimes - if you're
doing it by hand in Python it's no problem, you
just do it.)

(ii) Every semester I get a lot of emails asking
about grades. Used to be tedious typing the same
replies over and over, looking up the relevant
numbers. A littls Python script takes the student's
name, looks up the numbers and generates a reply
automatically, including a summary of the scores
and an explanation of how the grade was calculated.)

(iii) Taking various data from various places and making
it into HTML to post on the department web site.
(Please don't look - a lot of that stuff is currently
broken due to improvements on the server introduced
by other faculty. These things happen when nobody's
in charge so things get done by whoever's willing
to do them...)

(iv) Say I want to display the following system
of equations on a quiz:

  3x + 2y + z = 3
   x  - z = 1.

Writing TeX to get the variables to line
up properly can be tedious. A little Python
thingie takes lists of variable names and
coefficients and automatically produces
TeX that displays the equations exactly right.

I could go on and on - I use computers for a lot
of things, and any time I want to do something
but it's not obvious how to do it in the relevant
big program Python gets pulled out to do the job. 

A meta-example: I'm about to publish a book on 
[never mind, the topic is still secret.] Python 
has been incredibly useful in writing that book, 
in many different ways. For example:

(v) Making modifications to the text itself. For
example, the other day I finally figured out how
to make a certain aspect of the thing look right.
So I wanted to replace every "$$[w]\qed" in the
text (where [w] denotes any amount of white space)
with "\QED$$". Took about a minute to make a Python
script to go through the entire book and make the
change.

(vi) There are a lot of figures. Some fairly
complicated, illustrating fairly complicated
mathematical things. The figures are eps files
that were generated by Python scripts. The simple
ones could just have easily been done in Adobe
Illustrator or Corel Whatever, but there's no
way you're going to use a mouse-based program
like that to draw the complicated figures and
have everything in exactly the right place.
I have Python do the calculations and then
write the corresponding eps file, done.

(vii) Many magical things were done with a
combination of TeX macros and Python scripts.
For example, index entries: If I say
\index{Some Theorem} in the text and it turns
out that that's on page 37 then
"Some Theorem p.37" appears in the index;
now if something gets revised so the
\index{Some Theorem} is now on page 38 then
the index entry is automatically revised to
page 38.

Or: The first page of Chapter n+1 is supposed
to be the smallest odd number larger than the
last page of Chapter n. A Python script typesets
("texs") each chapter; after typesetting Chapter
n it looks and sees what the last page is,
figures out what the first page of Chapter n+1
should be, and modifies the code for Chapter n+1
to start on the page before typesetting it.

If I wrote this tomorrow the list of examples
would be different. Just now over on 
comp.text.tex I showed someone a Python solution
to a problem he had. I don't know if he's going 
to use it - he _would_ need to learn a _little_
Python first. But it's what I'd use if _I_ wanted
to solve the problem! Other people suggested various
programs available that would solve his problem
for him - writing a little Python to give the
solution took less time than downloading one
of those programs would have.

>Thanks.

David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list


Re: compressing short strings?

2008-05-20 Thread Helmut Jarausch

Paul Rubin wrote:

I have a lot of short English strings I'd like to compress in order to
reduce the size of a database.  That is, I'd like a compression
function that takes a string like (for example) "George Washington"
and returns a shorter string, with luck maybe 6 bytes or so.  One
obvious idea is take the gzip function, compress some large text
corpus with it in streaming mode and throw away the output (but
setting up the internal state to model the statistics of English
text), then put in "George Washington" and treat the additional output
as the compressed string.  Obviously to get reasonable speed there
would have to be a way to save the internal state after initializing
from the corpus.

Anyone know if this has been done and if there's code around for it?
Maybe I'm better off with freezing a dynamic Markov model?  I think
there's DMM code around but am not sure where to look.



I'd ask in comp.compression where the specialists are listening and who are
very helpful.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Re: svg-chart 1.1 SVG Charting Library

2008-05-20 Thread Helmut Jarausch

Jason R. Coombs wrote:

I'm pleased to announce svg-chart 1.1, the first public release of a
library for generating Scalable Vector Graphic (SVG) charts.

  http://sourceforge.net/projects/py-svg


This repository seems to be still empty?

Helmut.



--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Re: "indexed properties"...

2008-05-20 Thread David C. Ullrich
On Mon, 19 May 2008 14:48:03 +0200, pataphor <[EMAIL PROTECTED]>
wrote:

>On Mon, 19 May 2008 06:29:18 -0500
>David C. Ullrich <[EMAIL PROTECTED]> wrote:
>
>> Maybe you could be more specific? Various "positions" I've
>> taken in all this may well be untenable, but I can't think
>> of any that have anything to do with whether the data should
>> be a single list instead of a list of lists.
>
>What's 'untenable' (hey, I tried to get away with a smiley, remember)

Perhaps I should say that I was in no mood for smileys yesterday
morning: I hadn't eaten in 24 hours, hadn't had any water in
12 hours, and was anticipating an unpleasane instance of
what my doctor likes to call a "procedure" coming up in
a few hours. (Yes, everything went fine, thanks for asking -
today all is sweetness and light again.)

>is that a matrix is a list of rows. Suppose you do the transpose trick
>with the zip(*M) routine, now it's a list of columns. Both views are
>equal, there is no getting around the fact that you're holding an
>unnatural predisposition towards seeing the matrix as a list of rows,
>which it is most definitely not ...

Well, ok. Like I said, I never _took_ the position that it _should_
be a list of lists, I just said I didn't see the advantage to using
a single list.

Yes, the asummetry in my setup might be regarded as an 
aesthetic flaw. But that doesn't mean it doesn't work right,
and in any case _I_ regard it as a _feature_: rows and
columns look the same to the user even though they're
very different under the hood.

Although they're going to look the same, in the applications
I have in mind I expect that row operations will be more
frequent than column operations, so if we _were_ going
to worry about optimizing things optimizing row access 
might be reasonable.

>I was holding the brakes for this argument because I realize it's
>intuitive and also because Gabriel seems to want a list stay a list if
>he assigns something a list. But that's untenable too. Suppose you
>assign a column to a list? The list is torn to shreds and placed over
>multiple rows.

His desire here makes a lot more sense to me than it
seemed to at first, when he pointed out the problems
with "arow = m.row[0] = []". But this is a good point;
if we have rows _and_ columns then it seems like
he really can't have it his way.

Today's little joke: Long ago I would have solved
this by storing the data as a list of rows and _also_
a list of columns, updating each one any time the
other changed. Just goes to show you things
could always be worse...

>> (The only way I can parse this to make it relevant is to
>> assume that the position you're referring to is that a
>> list of lists is better than a single list. If so: First, I
>> haven't said that it was. Second, saying "B is untenable"
>> is not much of an answer when someone asks why you
>> say A is better than B.)
>
>Yes, it was not much of an answer but I was afraid of ending up in
>this quagmire. I now see that it is unavoidable anyway if I want to
>explain myself. Why couldn't you just see it the same way as me and
>leave it at that without waking up all the creatures of hell :-)

Sorry.

>> >And to address an 
>> >item in a matrix costs two lookups, row and column, while an array
>> >needs only one. 
>> 
>> The phrase "premature optimization" springs to mind.
>
>Well, I really liked your  slicing idea ...
>
>> This is _Python_ we're talking about. Supposing you're right that
>> doing two lookups _in Python_ is faster than doing one lookup
>> plus the calculuation col + row*width _in Python_, it can't
>> make enough difference to matter. In the sort of application I
>> have in mind things already happen "instantaneously".
>
>The computation is almost certainly faster. Lookups are expensive.

I know one thing about Python: I don't know exactly how it works,
and hence it's very difficult to be certain about such things without
actually testing them. Which is not to say you're not right.

>However I concede the point because we're not supposed to worry about
>such stuff. But it *is* a simpler format.

No wait, I know _two_ things about Python: (i) [repeat 
above] (ii) we're supposed to worry about such things 
_after_ determining that this particular such thing is 
actually the bottleneck. It seems incredibly unlikely 
that this detail is going to have any significance at
all in the final product.

>> The point is not to improve on NumPy. Trying to improve on
>> NumPy in pure Python code would be silly - if I wanted
>> optimized large matrices I'd _use_ NumPy. The point is just
>> to give a simple "intuitive" way to manipulate rows and
>> columns in small matrices. 
>
>Yes, me too. This is all about intuition.
>
>> So I'm not looking ahead to the future, things are not
>> scalable? The thing is not _supposed_ to scale up to
>> large matricies. If a person were dealing with large
>> matricies then almost all of it would need to be
>> rewritten (and if a person were dealing with really
>> large matri

Re: Write bits in file

2008-05-20 Thread pataphor
On Sun, 18 May 2008 06:36:28 -0700 (PDT)
Monica Leko <[EMAIL PROTECTED]> wrote:

> Yes.  I need arbitrary, 8bits, than 10 bits for something else, than
> sequence of bytes, than 10 bits again, etc.

Here's something to get you started. No guarantees, but I managed to
write four 10 bit numbers to a file producing 5 bytes, saying hello. I
was just looking for an excuse to use send.

P.

def gen(f):
L = []
 
def flush(L):
L.reverse()
s = ''.join(map(str,L))
j = int(s,2)
f.write(chr(j))

while 1:
x = yield 
if x in [0,1]:
L.append(x)
else:
break
if len(L) == 8:
flush(L)
L = []
if L:
while len(L) < 8:
L.append(0)
flush(L)
yield

def tenbits(i):
for j in range(9,-1,-1):
yield i >> j & 1

def charbits(s):
for c in s:
i = ord(c)
for j in range(8):
yield i >> j &1

def byten(L):
while L:
yield L[:10]
L = L[10:]

def test():
f = file('out.dat','w')
g = gen(f)
g.send(None)
for x in [90,611,397,758]:
for bit in tenbits(x):
g.send(bit)
g.send('stop')
f.close()

def test1():
bits = list(charbits('hello'))
L = []
for x in byten(bits):
L.append(int(''.join(map(str,x)),2))
print L

if __name__=='__main__':
test()
--
http://mail.python.org/mailman/listinfo/python-list


Re: do you fail at FizzBuzz? simple prog test

2008-05-20 Thread Wolfgang Grafen

globalrev schrieb:

http://reddit.com/r/programming/info/18td4/comments

claims people take a lot of time to write a simple program like this:


"Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz".

for i in range(1,101):
if i%3 == 0 and i%5 != 0:
print "Fizz"
elif i%5 == 0 and i%3 != 0:
print "Buzz"
elif i%5 == 0 and i%3 == 0:
print "FizzBuzz"
else:
print i


is there a better way than my solution? is mine ok?


Your example is comprehensive compared to many other suggestions which 
is an advantage IMO. I minimised it further


for i in range(start, stop, step):
if not i%15:
print "FizzBuzz"
elif not i%3:
print "Fizz"
elif not i%5:
print "Buzz"
else:
print i

Best regards

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


Re: Running commands on cisco routers using python

2008-05-20 Thread Floris Bruynooghe
On May 19, 4:18 pm, SPJ <[EMAIL PROTECTED]> wrote:
> Is it possible to run specific commands on cisco router using Python?
> I have to run command "show access-list" on few hundred cisco routers
> and get the dump into a file. Please let me know if it is feasible and
> the best way to achieve this.

There's no way I'd think about doing this in python.  The best tool
for the task is just shell IMHO:

[EMAIL PROTECTED]:~$ ssh mercury show access-lists
Welcome to mercury
[EMAIL PROTECTED]'s password:

Standard IP access list 1
10 permit any (265350 matches)
Standard IP access list 23
10 permit 192.168.2.0, wildcard bits 0.0.0.255 (2 matches)
Extended IP access list 100
10 deny ip any 192.168.0.0 0.0.255.255 log-input (8576 matches)
20 permit ip any any (743438 matches)Connection to mercury closed
by remote host.
[EMAIL PROTECTED]:~$

You could plug in expect to solve the password thing.  Search for "ssh
expect" for that (and ignore suggestions about public keys, I haven't
found yet how to use those on cisco).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Distributing applications that use 3rd party modules

2008-05-20 Thread Mike Driscoll
On May 17, 4:42 am, eliben <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'm getting into Python now after years of Perl, and as part of my
> research I must understand how to do some common tasks I need.
>
> I have a bunch of Windows PCs at work to which I want to distribute an
> application I've developed on my PC. All these PCs have Python 2.5
> installed.
>
> If my application contains only code I've developed, I simply zip its
> directory with .py files and send it to everyone, who can then use it
> by running the entry-point .py file. However, what if I've installed
> some 3rd party modules on my PC, and my application uses them (for
> example pyparsing, PiYAML and some others) ? I don't want to manually
> install all these packages (there may be dozens of them) on all those
> PCs (there may be dozens of those too). What is the best method I can
> use ? Naturally, I want all the non-standard packages my app uses to
> be detected automatically and collected into some kind of convenient
> distributable that is easy to pass around and run.
>
> I'm aware of py2exe - tried it and it works fine. But it creates huge
> executables, and I don't want to distribute those all the time. I much
> prefer a zipped directory of .py scripts that takes some 10s of KBs.
>
> Thanks in advance,
> Eli

One way I forgot to mention is to put Python on the network (i.e. the
intranet). We do that here at work and I can develop my applications
on my machine and then put them on there for anyone to use. That way
they never have to install Python, let alone the bother of installing
dependencies.

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


Re: compressing short strings?

2008-05-20 Thread bearophileHUGS
Helmut Jarausch:
> I'd ask in comp.compression where the specialists are listening and who are
> very helpful.

Asking in comp.compression is a good starting point.

My suggestions (sorry if they look a bit unsorted): it depends on what
language you want to use, how much you want to compress the strings,
if they are ASCII or unicode, how much quickly you want to compress/
decompress them, and how much time you have to write the encoder/
decoder.

You can use Python or a C/D extension, if you use C you will need more
time to develop it, but you will be quite more free to use any
algorithm you want (well, you are free with Python too, but the code
may be slower if it performs low level things), so D may be an
acceptable compromise.

Generally it's better to start with the simplest solution, and go on
from there looking for more complex ones if the first one isn't
enough. In Python the simplest thing to try is to compress the strings
with zip:
"George Washington".encode("zip")
Probably the result is longer than the original (25 bytes output for
this string). With "bz2" the results are probably worse (56 bytes for
this string).

A possible alternative it to compress many (10-30) strings at a time,
using an external table of their starting points (you can put the
table at the beginning), or you can separate those strings with a
separator char that's never present in the strings; and then you can
zip that group. If you have 1 million strings you can divide them into
such little groups, but the indexing in the DB becomes complex, and I
don't like this solution much.

A bit less simple python solution is to keep a fixed corpus of text
and compress it with and without the little string you want to add
(like you say), but compressing/decompressing everything each time (if
the corpus is small enough this isn't that slow), for example:

>>> s2 = a 2488 bytes long text
>>> len(s2)
2488
>>> z1 = (s2).encode("zip")
>>> len(z1)
1321
>>> s = 'George Washington'
>>> len((s).encode("zip"))
25
>>> len(s.encode("bz2"))
56
>>> z2 = (s2+s).encode("zip")
>>> len(z2)
1332
>>> len(z2) - len(z1)
11

So you need to store only this 11 byte long string to be able to
decompress it. A bigger corpus (s2) allows you a bit more compression,
but it requires more time for compression/decompression:

>>> len(s3) # s3 is a longer English text
10811
>>> z3 = s3.encode("zip")
>>> len(z3)
4971
>>> z4 = (s3+s).encode("zip")
>>> len(z4)
4980
>>> len(z4) - len(z3)
9

One of the simpler ways to compress short ASCII English texts is to
see that the most frequent chars are very few, so you can encode the
15 most common English chars with 1 nibble, and the other ones with 3
nibbles (first nibble is an escape, and the two following are the
whole byte), if you use a low level language you can encode this with
few lines of code, it's very fast and it may shave off 25-33% of your
bytes. It means that your "George Washington" (17 chars long) may
become about 12-13 chars long. I have implemented this with Python
too, it's easy.

Another possible solution is to use a Huffman compression with fixed
symbol frequencies. In Python there are ways to write such compressor/
decompressor in just 10-15 lines code. You can use the heapq to speed
up the processing. This may lead to more compression, but I presume
not much than less than 4-5 bpc. But if you want to compress/
decompress a LOT of strings you may need to write (or look for the
code) in C/D.

This is one implementation that doesn't use heapq yet:

from operator import itemgetter



def clean_tree(obj):

if isinstance(obj, tuple):

return obj[0]

else:

return [clean_tree(obj[0][0]), clean_tree(obj[0][1])]



def huffman_generate(freqsl):

while len(freqsl) > 2:

freqsl.sort(key=itemgetter(1))

freqsl = [ [freqsl[0:2], freqsl[0][1] + freqsl[1][1]] ] +
freqsl[2:]

return [freqsl, -1]



def tree2dict(tree):

def encode(tree, out=""):

if isinstance(tree, list):

encode(tree[0], out+"0")

encode(tree[1], out+"1")

else:

dic[tree[0]] = out

return dic

dic = {}

return encode(tree)



freqs = [('a', 45), ('b', 13), ('c', 12), ('d', 16), ('e', 9), ('f',
5)]

print tree2dict(clean_tree(huffman_generate(freqs)))

If you want to use a Huffman you can use the usual tricks to increase
compression: use 2-char symbols, or even use whole words as symbols
(but you can use whole words only if you are sure your language is
English with few strange words).

If you want more compression, like a 6 bytes (2.8 bpc) output you will
need more complex algorithms. On long English texts the best
compressors need less than 0.9 bpc, but they are surely useless for
you. I don't think you will be able to go down to 6 bytes for that
string, unless you are ready to pay lot of computational time :-)

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Misuse of list comprehensions?

2008-05-20 Thread John Salerno
I posted this code last night in response to another thread, and after I 
posted it I got to wondering if I had misused the list comprehension. Here's 
the two examples:

Example 1:

def compress(s):
new = []

for c in s:
if c not in new:
new.append(c)
return ''.join(new)
--

Example 2:

def compress(s):
new = []
[new.append(c) for c in s if c not in new]
return ''.join(new)
--

In example 1, the intention to make an in-place change is explicit, and it's 
being used as everyone expects it to be used. In example 2, however, I began 
to think this might be an abuse of list comprehensions, because I'm not 
assigning the result to anything (nor am I even using the result in any 
way).

What does everyone think about this? Should list comprehensions be used this 
way, or should they only be used to actually create a new list that will 
then be assigned to a variable/returned/etc.? 


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


What is wrong with my Python threading?

2008-05-20 Thread Chuckk Hubbard
#!/usr/bin/python

#why doesn't this run both threads simultaneously?
#Thanks for any help.
#Chuckk

import threading
import time

def printesc(thrd):
for i in range(10):
time.sleep(1)
print thrd, i

def master():
thd1 = threading.Thread(target=printesc, args=(1,))
thd2 = threading.Thread(target=printesc, args=(2,))
thd1.run()
thd2.run()

master()
--
http://mail.python.org/mailman/listinfo/python-list


Re: compressing short strings?

2008-05-20 Thread bearophileHUGS
bearophile:
> So you need to store only this 11 byte long string to be able to
> decompress it.

Note that maybe there is a header, that may contain changing things,
like the length of the compressed text, etc.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: do you fail at FizzBuzz? simple prog test

2008-05-20 Thread castironpi
On May 20, 6:57 am, Wolfgang Grafen <[EMAIL PROTECTED]>
wrote:
> globalrev schrieb:
>
>
>
>
>
> >http://reddit.com/r/programming/info/18td4/comments
>
> > claims people take a lot of time to write a simple program like this:
>
> > "Write a program that prints the numbers from 1 to 100. But for
> > multiples of three print "Fizz" instead of the number and for the
> > multiples of five print "Buzz". For numbers which are multiples of
> > both three and five print "FizzBuzz".
>
> > for i in range(1,101):
> >     if i%3 == 0 and i%5 != 0:
> >         print "Fizz"
> >     elif i%5 == 0 and i%3 != 0:
> >         print "Buzz"
> >     elif i%5 == 0 and i%3 == 0:
> >         print "FizzBuzz"
> >     else:
> >         print i
>
> > is there a better way than my solution? is mine ok?
>
> Your example is comprehensive compared to many other suggestions which
> is an advantage IMO. I minimised it further
>
> for i in range(start, stop, step):
>      if not i%15:
>          print "FizzBuzz"
>      elif not i%3:
>          print "Fizz"
>      elif not i%5:
>          print "Buzz"
>      else:
>          print i
>
> Best regards
>
> Wolfgang- Hide quoted text -
>
> - Show quoted text -

This one eliminates the modulo operation.

dyn= list( xrange( 1, 101 ) )
for i in xrange( 1, len( dyn ), 3 ):
dyn[ i ]= ''
for i in xrange( 1, len( dyn ), 5 ):
dyn[ i ]= ''
for i in xrange( 1, len( dyn ), 3 ):
dyn[ i ]+= 'Fizz'
for i in xrange( 1, len( dyn ), 5 ):
dyn[ i ]+= 'Buzz'
for d in dyn:
print d

Can we compare?
--
http://mail.python.org/mailman/listinfo/python-list


Re: What is wrong with my Python threading?

2008-05-20 Thread castironpi
On May 20, 8:19 am, "Chuckk Hubbard" <[EMAIL PROTECTED]>
wrote:
> #!/usr/bin/python
>
> #why doesn't this run both threads simultaneously?
> #Thanks for any help.
> #Chuckk
>
> import threading
> import time
>
> def printesc(thrd):
>     for i in range(10):
>         time.sleep(1)
>         print thrd, i
>
> def master():
>     thd1 = threading.Thread(target=printesc, args=(1,))
>     thd2 = threading.Thread(target=printesc, args=(2,))
>     thd1.run()
>     thd2.run()
>
> master()

You meant 'thd1.start( )' and 'thd2.start( )'.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
John Salerno wrote:

> I posted this code last night in response to another thread, and after I
> posted it I got to wondering if I had misused the list comprehension.
> Here's the two examples:
> 
> Example 1:
> 
> def compress(s):
> new = []
> 
> for c in s:
> if c not in new:
> new.append(c)
> return ''.join(new)
> --
> 
> Example 2:
> 
> def compress(s):
> new = []
> [new.append(c) for c in s if c not in new]
> return ''.join(new)
> --
> 
> In example 1, the intention to make an in-place change is explicit, and
> it's being used as everyone expects it to be used. In example 2, however,
> I began to think this might be an abuse of list comprehensions, because
> I'm not assigning the result to anything (nor am I even using the result
> in any way).
> What does everyone think about this? Should list comprehensions be used
> this way, or should they only be used to actually create a new list that
> will then be assigned to a variable/returned/etc.?

the above code is pretty much of a no-no because it has quadratic runtime
behavior.

That being said, I use that idiom myself.  But I don't see anything wrong
with using a list-comp as loop-abbreviation. because that is it's actual
purpose. And also it is common in non-functional languages that l-values
aren't always assigned, if the aren't needed. It's the consequence of
having side-effects in a language - and python has them.

Diez


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


Re: test mult vars to same value, how to shorten expr?

2008-05-20 Thread castironpi
On May 20, 2:08 am, Peter Otten <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> >  if i want o test:
> > if a == 5 and b ==5 and c==5 ... z==5
>
> > is there some synctactic suagr for this?
>
> > rather than maiking one of my own i mean, something built-in like:
> > if a,b,c... z == 5:
>
> if all(x == 5 for x in a,b,c,...):
>    print "yep"
>
> Peter

>>> a,b,c,d=5,5,5,5
>>> import functools
>>> import operator
>>> myequals= functools.partial( operator.eq, 5 )
>>> map( myequals, ( a, b, c, d ) )
[True, True, True, True]
>>> all( _ )
True
--
http://mail.python.org/mailman/listinfo/python-list


Re: What is wrong with my Python threading?

2008-05-20 Thread Diez B. Roggisch
Chuckk Hubbard wrote:

> #!/usr/bin/python
> 
> #why doesn't this run both threads simultaneously?
> #Thanks for any help.
> #Chuckk

Because you should call thread.start().


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


Re: Using Python for programming algorithms

2008-05-20 Thread brad

Vicent Giner wrote:



The usual answer is that development time is more important than running time.


This depends. Run time is not important until you are asked to scale to 
millions or billions of users or computations or large data sets. I've 
seen this first hand. Getting results back the same day or sooner may be 
important. In cases such as this, I use C or C++... nothing else will 
do. Nothing else is as fast. Although I always keep a py version around 
for testing and for smaller tasks. Don't get me wrong, I love Python, 
but there are times when nothing, but pure, hard speed will do.

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


Re: Distributing applications that use 3rd party modules

2008-05-20 Thread castironpi
On May 20, 7:56 am, Mike Driscoll <[EMAIL PROTECTED]> wrote:
> On May 17, 4:42 am, eliben <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Hello,
>
> > I'm getting into Python now after years of Perl, and as part of my
> > research I must understand how to do some common tasks I need.
>
> > I have a bunch of Windows PCs at work to which I want to distribute an
> > application I've developed on my PC. All these PCs have Python 2.5
> > installed.
>
> > If my application contains only code I've developed, I simply zip its
> > directory with .py files and send it to everyone, who can then use it
> > by running the entry-point .py file. However, what if I've installed
> > some 3rd party modules on my PC, and my application uses them (for
> > example pyparsing, PiYAML and some others) ? I don't want to manually
> > install all these packages (there may be dozens of them) on all those
> > PCs (there may be dozens of those too). What is the best method I can
> > use ? Naturally, I want all the non-standard packages my app uses to
> > be detected automatically and collected into some kind of convenient
> > distributable that is easy to pass around and run.
>
> > I'm aware of py2exe - tried it and it works fine. But it creates huge
> > executables, and I don't want to distribute those all the time. I much
> > prefer a zipped directory of .py scripts that takes some 10s of KBs.
>
> > Thanks in advance,
> > Eli
>
> One way I forgot to mention is to put Python on the network (i.e. the
> intranet). We do that here at work and I can develop my applications
> on my machine and then put them on there for anyone to use. That way
> they never have to install Python, let alone the bother of installing
> dependencies.
>
> Mike- Hide quoted text -
>
> - Show quoted text -

Impossible and Useless here:

I assert there's no way to generate the list.

a= raw_input( )
if a== 'something unknown':
  b= 'imp'
  b+= 'ort '
  b+= 'os'
  exec( b )
--
http://mail.python.org/mailman/listinfo/python-list


Re: Misuse of list comprehensions?

2008-05-20 Thread bearophileHUGS
John Salerno:
> What does everyone think about this?

The Example 2 builds a list, that is then thrown away. It's just a
waste of memory (and time).

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: "indexed properties"...

2008-05-20 Thread pataphor
On Tue, 20 May 2008 06:12:01 -0500
David C. Ullrich <[EMAIL PROTECTED]> wrote:

> Well, ok. Like I said, I never _took_ the position that it _should_
> be a list of lists, I just said I didn't see the advantage to using
> a single list.

I'm now thinking about a list of lists containing single element lists.

def test():
n = 3
R = range(n)
M = [[[i+j*n] for i in R] for j in R]
for L in M:
print L
row2 = M[1]
print
print row2
col3 = [L[2] for L in M]
print col3
col3[1][0]=9

print col3
print row2
print
for L in M:
print L

if __name__=='__main__':
test()

The idea is to use a single element list as kind of storage facility.
That enables me to copy a column, then change something in that column
and make the change turn up in the matrix and even in a previously made
copy of a row that shared the specific element.
 
> Today's little joke: Long ago I would have solved
> this by storing the data as a list of rows and _also_
> a list of columns, updating each one any time the
> other changed. Just goes to show you things
> could always be worse...

Well have I ever ... I only thought about this last week and I
actually thought it was a *good* idea.  I only gave up on it because
now I would have to keep  track of how far the two views are out of
sync, because some operation could use data that was modified by an
other view. Zipstar transposition is very handy for syncing, but it is
also very expensive. But probably not for you.

> Expressing interest is a big mistake...

How so? ;-)
 
> Sometime soon a thread will appear titled something
> like "indexed attributes" with a stripped-down but
> functional example of what I have in mind for Matrix

I can't wait! Keep up the good work.

P.

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


RE: Newbie In Python

2008-05-20 Thread Sells, Fred
get a python-aware editor.  I vary between emacs and Eclipse, depending on my 
mood and the size of the project.

> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]
> Behalf Of Bruno Desthuilliers
> Sent: Tuesday, May 20, 2008 6:25 AM
> To: python-list@python.org
> Subject: Re: Newbie In Python
> 
> 
> [EMAIL PROTECTED] a écrit :
> > I have Heard About "Python" its a OOD Language. 
> 
> 'OOD' => 'object oriented ???' ?
> 
> > i have to Learn it
> > where from i should start it.
> 
> Err... What about reading the docs on python.org - possibly starting 
> with the tutorial:
> http://docs.python.org/tut/tut.html
> 
> You'll find other tutorial etc here:
> http://www.python.org/doc/
> 
> 
> > i have python compiler
> 
> Just for the record, "python" is also the name of a compiler for CMU 
> Common Lisp (totally unrelated to the Python language), so make sure 
> that what you have is the CPython language install.
> 
> HTH
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote:

> John Salerno:
>> What does everyone think about this?
> 
> The Example 2 builds a list, that is then thrown away. It's just a
> waste of memory (and time).

No, it doesn't. It uses append because it refers to itself in the
if-expression. So the append(c) is needed - and thus the assignment
possible but essentially useless.

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


Re: compressing short strings?

2008-05-20 Thread castironpi
On May 20, 8:24 am, [EMAIL PROTECTED] wrote:
> bearophile:
>
> > So you need to store only this 11 byte long string to be able to
> > decompress it.
>
> Note that maybe there is a header, that may contain changing things,
> like the length of the compressed text, etc.
>
> Bye,
> bearophile

I've read that military texts contain different letter frequencies
than standard English.  If you use a non-QWERTY keyset, it may change
your frequency distribution also.

I worry that this is an impolite question; as such, I lean to peers
for backup:

Will you be additionally adding further entries to the zipped list?

Will you be rewriting the entire file upon update, or just appending
bytes?

If your sequence is 'ab, ab, ab, cd, ab', you might be at:

00010.

Add 'cd' again and you're at:

000101.

You didn't have to re-output the contents.

But, if you add 'bc', you have:

0001012, which isn't in binary.  So you're left at:

000 000 000 001 000 001 010

But five more and the byte overflows.

I'd say pickle the corpus, with new additions, and re-zip the entire
contents each time.  Would you like to break across
(coughdisksectorscough) multiple files, say, a different corpus-
compression file pair for every thousand entries?
--
http://mail.python.org/mailman/listinfo/python-list


Re: conventions/requirements for 'is' vs '==', 'not vs '!=', etc

2008-05-20 Thread castironpi
On May 20, 5:04 am, Duncan Booth <[EMAIL PROTECTED]> wrote:
> John Salerno <[EMAIL PROTECTED]> wrote:
>  a = 'this is longer'
>  b = 'this is longer'
>  a == b
> > True
>  a is b
> > False
>
> > In the above example, Python has created only one string called
> > 'hello' and both x and y reference it. However, 'this is longer' is
> > two completely different objects.
>
> That is true when run interactively, but the behaviour changes again if you
> run it as a script:
>
> C:\Temp>type t.py
> a = 'this is longer'
> b = 'this is longer'
> print a is b
>
> C:\Temp>t
> True
>
> In short, two equal strings may or may not be identical and any code which
> makes assumptions based on observed behaviour is broken. If you want to be
> able to test identity on strings safely then use the 'intern()' builtin to
> get repeatable behaviour.
>
> --
> Duncan Boothhttp://kupuguy.blogspot.com

Strictly speaking, identity is a real notion whereas equality is an
ideal (symbolic) notion.  There is no such thing as real true equals.
Furthermore, there are no true real symbols.  But, as puzzling as that
is:

'abc' == 'abc'
abc is abc

I argue that 'is' should never be used for primitives/literals.

a is 4  #bad
a is b  #good
a is 'abc' #bad
a is { None: None } #bad
a== { None: None } #good

Just good thing I don't have check-in priveleges, right? :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: scaling problems

2008-05-20 Thread Duncan Booth
Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:

> On Tue, 20 May 2008 10:47:50 +1000, James A. Donald wrote:
> 
>> 2.  It is not clear to me how a python web application scales.
> 
> Ask YouTube.  :-)

Or look at Google appengine where unlike normal Python you really are 
prevented from making good use of threading.

Google App Engine takes an interesting approach by forcing the programmer 
to consider scalability right from the start: state is stored in a 
distributed database which cannot do all the hard to scale things that SQL 
databases do. This means that you have to work as though your application 
were spread on servers all round the world from day 1 instead of waiting 
until the structure that was 'good enough' is threatening to kill your 
business before you address them.

It also puts strict limits on how much a single web request can do, so 
again you have to work from day 1 to make sure that page requests are as 
efficient as possible.

In return you get an application which should scale well. There is nothing 
Python specific about the techniques, it is just that Python is the first 
(and so far only) language supported on the platform.

-- 
Duncan Booth http://kupuguy.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


persistent deque

2008-05-20 Thread castironpi
I'd like a persistent deque, such that the instance operations all
commit atomicly to file system.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Misuse of list comprehensions?

2008-05-20 Thread Bruno Desthuilliers

John Salerno a écrit :
I posted this code last night in response to another thread, and after I 
posted it I got to wondering if I had misused the list comprehension. 


(snip)

def compress(s):
new = []
[new.append(c) for c in s if c not in new]
return ''.join(new)


As far as I'm concerned (and I'm a big fan of list-comps, generator 
expressions etc), this is definitively an abuse. And a waste of 
resources since it builds  a useless list of 'None'.

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


Re: Using Python for programming algorithms

2008-05-20 Thread Duncan Booth
Roel Schroeven <[EMAIL PROTECTED]> wrote:

> C OTOH was designed to be compiled to assembly code (or directly to 
> machine code) and as a result there are no (or virtually) no 
> implementations that interpret C or compile it to bytecode.

Have you considered Microsoft's C/C++ compiler targetted at .Net. That 
compiles to a bytecode known as MSIL which is then interpreted and/or JIT 
compiled to machine code.

> I love Python, but IMHO it's a bit silly to maintain that the fact that 
> Python compiles to byte code instead of assembly code/machine code is 
> purely a matter of implementation; on the contrary, I believe it's a 
> result of its design. I also think that there's a large difference 
> between byte code and machine code (in Python's case; I haven't looked 
> at other languages), and that it's a bit silly to try to trivialize that 
> difference.

And then there's IronPython which is targetted at .Net. That compiles to a 
bytecode known as MSIL which is then interpreted and/or JIT compiled to 
machine code.

-- 
Duncan Booth http://kupuguy.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: TPCServer and xdrlib

2008-05-20 Thread Laszlo Nagy




- use simple file copying from a mounted network drive
Untrustable clients should not mount out anything from my server. (Also, 
it is not a protocol. I need to communicate with a real program, not 
just copying files.)

- use http (web server)
I mentioned this before - don't know how to keep-alive with 
simplehttpserver. Similar solutions e.g. Apache + mod_python are too 
heavy weight. Too many dependencies etc.

- use ftp
- use scp
- use rsync

Why wouldn't one of these work for you? Did I miss something in your 
original requirements?
Yes. I also need business logic on the server. Not just copying file. It 
happens that some of the messages will contain images.



Thank you for all your efforts. I think I'll go with TCPServer + xdrlib.


Laszlo



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


Re: Misuse of list comprehensions?

2008-05-20 Thread Hrvoje Niksic
"Diez B. Roggisch" <[EMAIL PROTECTED]> writes:

> [EMAIL PROTECTED] wrote:
>
>> John Salerno:
>>> What does everyone think about this?
>> 
>> The Example 2 builds a list, that is then thrown away. It's just a
>> waste of memory (and time).
>
> No, it doesn't.

This line:

[new.append(c) for c in s if c not in new]

...throws away the list built by the comprehension itself, composed of
None values (returned by append).

> It uses append because it refers to itself in the if-expression. So
> the append(c) is needed

The append is not in question here.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
Hrvoje Niksic wrote:

> "Diez B. Roggisch" <[EMAIL PROTECTED]> writes:
> 
>> [EMAIL PROTECTED] wrote:
>>
>>> John Salerno:
 What does everyone think about this?
>>> 
>>> The Example 2 builds a list, that is then thrown away. It's just a
>>> waste of memory (and time).
>>
>> No, it doesn't.
> 
> This line:
> 
> [new.append(c) for c in s if c not in new]
> 
> ...throws away the list built by the comprehension itself, composed of
> None values (returned by append).

Ah. You are right of course.

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


Re: Misuse of list comprehensions?

2008-05-20 Thread Thomas Bellman
"Diez B. Roggisch" <[EMAIL PROTECTED]> writes:

> [EMAIL PROTECTED] wrote:

>> The Example 2 builds a list, that is then thrown away. It's just a
>> waste of memory (and time).

> No, it doesn't. It uses append because it refers to itself in the
> if-expression. So the append(c) is needed - and thus the assignment
> possible but essentially useless.

Yes it does.  A list comprehension *always* creates a list.  In
this case it will be a list of None, since that is what list.append()
returns.  See this:

>>> new=[]
>>> s="This is a foolish use of list comprehensions"
>>> [ new.append(c) for c in s if c not in new ]
[None, None, None, None, None, None, None, None, None, None, None,
 None, None, None, None, None, None]

Yes, you do get a correct result in 'new', but you *also* create
a 17 long list with all elements set to None, that is immediately
thrown away.


-- 
Thomas Bellman,   Lysator Computer Club,   Linköping University,  Sweden
"I refuse to have a battle of wits with an   !  bellman @ lysator.liu.se
 unarmed person."!  Make Love -- Nicht Wahr!
--
http://mail.python.org/mailman/listinfo/python-list

Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
Thomas Bellman wrote:

> "Diez B. Roggisch" <[EMAIL PROTECTED]> writes:
> 
>> [EMAIL PROTECTED] wrote:
> 
>>> The Example 2 builds a list, that is then thrown away. It's just a
>>> waste of memory (and time).
> 
>> No, it doesn't. It uses append because it refers to itself in the
>> if-expression. So the append(c) is needed - and thus the assignment
>> possible but essentially useless.
> 
> Yes it does.  A list comprehension *always* creates a list.  In
> this case it will be a list of None, since that is what list.append()
> returns.  See this:

Yep - no idea how that slipped me. I still don't mind the occasional waste
of a list-creation over a more concise looping-construct, but I totally
admit that one has to be aware of this. more than I was

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


Re: Using Python for programming algorithms

2008-05-20 Thread Arnaud Delobelle
brad <[EMAIL PROTECTED]> writes:

>> Vicent Giner wrote:
>
>> The usual answer is that development time is more important than
>> running time.
>
> This depends. Run time is not important until you are asked to scale
> to millions or billions of users or computations or large data
> sets. I've seen this first hand. Getting results back the same day or
> sooner may be important. In cases such as this, I use C or
> C++... nothing else will do. Nothing else is as fast. Although I
> always keep a py version around for testing and for smaller
> tasks. Don't get me wrong, I love Python, but there are times when
> nothing, but pure, hard speed will do.

Sure.  Be careful not to overdose on it though.

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


Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
> That being said, I use that idiom myself.  But I don't see anything wrong
> with using a list-comp as loop-abbreviation. because that is it's actual
> purpose. And also it is common in non-functional languages that l-values
> aren't always assigned, if the aren't needed. It's the consequence of
> having side-effects in a language - and python has them.

After being corrected about missing the construction of a None-containing
list, one needs of course to think about the waste of resources, as a
possible result-list is created in any case.

I personally still don't mind that (if we talk about a few hundred objects a
top) - but it makes a strong point against a general usage.

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


Re: Misuse of list comprehensions?

2008-05-20 Thread Paul McGuire
On May 20, 8:13 am, "John Salerno" <[EMAIL PROTECTED]> wrote:
> I posted this code last night in response to another thread, and after I
> posted it I got to wondering if I had misused the list comprehension. Here's
> the two examples:
>
> Example 1:
> 
> def compress(s):
>     new = []
>
>     for c in s:
>         if c not in new:
>             new.append(c)
>     return ''.join(new)
> --
>
> Example 2:
> 
> def compress(s):
>     new = []
>     [new.append(c) for c in s if c not in new]
>     return ''.join(new)
> --
>
> In example 1, the intention to make an in-place change is explicit, and it's
> being used as everyone expects it to be used. In example 2, however, I began
> to think this might be an abuse of list comprehensions, because I'm not
> assigning the result to anything (nor am I even using the result in any
> way).
>
> What does everyone think about this? Should list comprehensions be used this
> way, or should they only be used to actually create a new list that will
> then be assigned to a variable/returned/etc.?

Why not make the list comp the actual list you are trying to build?

def compress(s):
seen = set()
new = [c for c in s if c not in seen and (seen.add(c) or True)]
return ''.join(new)

or just:

def compress(s):
seen = set()
return ''.join(c for c in s if c not in seen and (seen.add(c) or
True))

Using the set also gets rid of that nasty quadratic performance
thingy.

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


Help with character encodings

2008-05-20 Thread A_H
Help!

I've scraped a PDF file for text and all the minus signs come back as
u'\xad'.

Is there any easy way I can change them all to plain old ASCII '-' ???

str.replace complained about a missing codec.



Hints?




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


Re: Misuse of list comprehensions?

2008-05-20 Thread Arnaud Delobelle
Paul McGuire <[EMAIL PROTECTED]> writes:

> On May 20, 8:13 am, "John Salerno" <[EMAIL PROTECTED]> wrote:
>> I posted this code last night in response to another thread, and after I
>> posted it I got to wondering if I had misused the list comprehension. Here's
>> the two examples:
>>
>> Example 1:
>> 
>> def compress(s):
>>     new = []
>>
>>     for c in s:
>>         if c not in new:
>>             new.append(c)
>>     return ''.join(new)
>> --
>>
>> Example 2:
>> 
>> def compress(s):
>>     new = []
>>     [new.append(c) for c in s if c not in new]
>>     return ''.join(new)
>> --
>>
>> In example 1, the intention to make an in-place change is explicit, and it's
>> being used as everyone expects it to be used. In example 2, however, I began
>> to think this might be an abuse of list comprehensions, because I'm not
>> assigning the result to anything (nor am I even using the result in any
>> way).
>>
>> What does everyone think about this? Should list comprehensions be used this
>> way, or should they only be used to actually create a new list that will
>> then be assigned to a variable/returned/etc.?
>
> Why not make the list comp the actual list you are trying to build?
>
> def compress(s):
> seen = set()
> new = [c for c in s if c not in seen and (seen.add(c) or True)]


Isn't 

c not in seen and (seen.add(c) or True)

the same as

seen.add(c) or c not in seen

?

> return ''.join(new)
>

(notice I haven't closed the tag!)

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


Re: Help with character encodings

2008-05-20 Thread Gary Herron

A_H wrote:

Help!

I've scraped a PDF file for text and all the minus signs come back as
u'\xad'.

Is there any easy way I can change them all to plain old ASCII '-' ???

str.replace complained about a missing codec.



Hints?
  


Encoding it into a 'latin1' encoded string seems to work:

 >>> print u'\xad'.encode('latin1')
 -








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


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


Python in non-standard location erring with "No module named _sha256"

2008-05-20 Thread emallove
I'm running into the below "No modules named _sha256" issue, with a
python installed in a non-standard location.

$ python
Python 2.5.2 (r252:60911, May 20 2008, 09:46:50)
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import md5
Traceback (most recent call last):
  File "", line 1, in 
  File "/ws/ompi-tools/lib/python2.5/md5.py", line 6, in 
from hashlib import md5
  File "/ws/ompi-tools/lib/python2.5/hashlib.py", line 135, in

sha224 = __get_builtin_constructor('sha224')
  File "/ws/ompi-tools/lib/python2.5/hashlib.py", line 63, in
__get_builtin_constructor
import _sha256
ImportError: No module named _sha256

Googling around, this seems to be related to OpenSSL being in a non-
standard location? I've edited the Setup file to set $(SSL) to the non-
standard location. Now Python compiles fine, but I still get the above
error.

Any help would be much appreciated.

Thanks,
Ethan
--
http://mail.python.org/mailman/listinfo/python-list


Re: Write bits in file

2008-05-20 Thread Nick Craig-Wood
Monica Leko <[EMAIL PROTECTED]> wrote:
>  On May 18, 2:20?pm, Ken Starks <[EMAIL PROTECTED]> wrote:
> > You want your file considered as a sequence of bits rather
> > than a sequence of 8-bit bytes, do you?
> 
>  Yes.
> 
> > is the 10-bit bit-pattern to be stored at an arbitrary
> > bit-position in the file
> 
>  Yes.  I need arbitrary, 8bits, than 10 bits for something else, than
>  sequence of bytes, than 10 bits again, etc.

You could try

  http://construct.wikispaces.com/

which could well do exactly what you want.

-- 
Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: "indexed properties"...

2008-05-20 Thread David C. Ullrich
In article <[EMAIL PROTECTED]>,
 pataphor <[EMAIL PROTECTED]> wrote:

> On Tue, 20 May 2008 06:12:01 -0500
> David C. Ullrich <[EMAIL PROTECTED]> wrote:
> 
> > Well, ok. Like I said, I never _took_ the position that it _should_
> > be a list of lists, I just said I didn't see the advantage to using
> > a single list.
> 
> I'm now thinking about a list of lists containing single element lists.
> 
> def test():
> n = 3
> R = range(n)
> M = [[[i+j*n] for i in R] for j in R]
> for L in M:
> print L
> row2 = M[1]
> print
> print row2
> col3 = [L[2] for L in M]
> print col3
> col3[1][0]=9
> 
> print col3
> print row2
> print
> for L in M:
> print L
> 
> if __name__=='__main__':
> test()
> 
> The idea is to use a single element list as kind of storage facility.
> That enables me to copy a column, then change something in that column
> and make the change turn up in the matrix and even in a previously made
> copy of a row that shared the specific element.
>  
> > Today's little joke: Long ago I would have solved
> > this by storing the data as a list of rows and _also_
> > a list of columns, updating each one any time the
> > other changed. Just goes to show you things
> > could always be worse...
> 
> Well have I ever ... I only thought about this last week and I
> actually thought it was a *good* idea. 

Sorry. If you really want to do this you could keep things
in sync automatically by writing the appropriate __setitem__
and resolving never to modify the data except through that...

def whatever.__setitem__(self, (row,col), value):
  self.rows[row][col] = value
  self.cols[col][row] = value

Seems to me like an, um, "untenable" waste of space. And you'd
need to be certain to never set anything except through
self[row,col]; as far as I can see anything like setrow()
would be a loop "for col in range(width): self[row,col] =".

Hmm. You could keep the data in some object that encapsulates
the two double lists and use some fancy feature (__getattribute__?)
to make certain that the only possible access to the data
was through __getitem__...

> I only gave up on it because
> now I would have to keep  track of how far the two views are out of
> sync, because some operation could use data that was modified by an
> other view. Zipstar transposition is very handy for syncing, but it is
> also very expensive. But probably not for you.
> 
> > Expressing interest is a big mistake...
> 
> How so? ;-)
>  
> > Sometime soon a thread will appear titled something
> > like "indexed attributes" with a stripped-down but
> > functional example of what I have in mind for Matrix
> 
> I can't wait! Keep up the good work.
> 
> P.

-- 
David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help with character encodings

2008-05-20 Thread J. Cliff Dyer
On Tue, 2008-05-20 at 08:28 -0700, Gary Herron wrote:
> A_H wrote:
> > Help!
> >
> > I've scraped a PDF file for text and all the minus signs come back as
> > u'\xad'.
> >
> > Is there any easy way I can change them all to plain old ASCII '-' ???
> >
> > str.replace complained about a missing codec.
> >
> >
> >
> > Hints?
> >   
> 
> Encoding it into a 'latin1' encoded string seems to work:
> 
>   >>> print u'\xad'.encode('latin1')
>   -
> 
> 
Here's what I've found:

>>> x = u'\xad'
>>> x.replace('\xad','-')
Traceback (most recent call last):
  File "", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xad in position 0:
ordinal not in range(128)
>>> x.replace(u'\xad','-')
u'-'

If you replace the *string* '\xad' in the first argument to replace with
the *unicode object* u'\xad', python won't complain anymore.  (Mind you,
you weren't using str.replace.  You were using unicode.replace.  Slight
difference, but important.)  If you do the replace on a plain string, it
doesn't have to convert anything, so you don't get a UnicodeDecodeError.

>>> x = x.encode('latin1')
>>> x
'\xad'
>>> # Note the lack of a u before the ' above.
>>> x.replace('\xad','-')
'-'
>>> 

Cheers,
Cliff


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


Re: Misuse of list comprehensions?

2008-05-20 Thread s0suk3
On May 20, 9:58 am, Paul McGuire <[EMAIL PROTECTED]> wrote:
> On May 20, 8:13 am, "John Salerno" <[EMAIL PROTECTED]> wrote:
>
>
>
> > I posted this code last night in response to another thread, and after I
> > posted it I got to wondering if I had misused the list comprehension. Here's
> > the two examples:
>
> > Example 1:
> > 
> > def compress(s):
> > new = []
>
> > for c in s:
> > if c not in new:
> > new.append(c)
> > return ''.join(new)
> > --
>
> > Example 2:
> > 
> > def compress(s):
> > new = []
> > [new.append(c) for c in s if c not in new]
> > return ''.join(new)
> > --
>
> > In example 1, the intention to make an in-place change is explicit, and it's
> > being used as everyone expects it to be used. In example 2, however, I began
> > to think this might be an abuse of list comprehensions, because I'm not
> > assigning the result to anything (nor am I even using the result in any
> > way).
>
> > What does everyone think about this? Should list comprehensions be used this
> > way, or should they only be used to actually create a new list that will
> > then be assigned to a variable/returned/etc.?
>
> Why not make the list comp the actual list you are trying to build?
>
> def compress(s):
> seen = set()
> new = [c for c in s if c not in seen and (seen.add(c) or True)]
> return ''.join(new)
>
> or just:
>
> def compress(s):
> seen = set()
> return ''.join(c for c in s if c not in seen and (seen.add(c) or
> True))
>
> Using the set also gets rid of that nasty quadratic performance
> thingy.

You don't need all those conditionals. A set differs from a list
precisely in the fact that each element is unique. And since the
function is expecting "s" to be an iterable object, it can be
constructed even without a for loop:

def compress(s):
return list(set(s))

That does the trick.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Misuse of list comprehensions?

2008-05-20 Thread Paul McGuire
On May 20, 10:17 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:
> Paul McGuire <[EMAIL PROTECTED]> writes:
> > On May 20, 8:13 am, "John Salerno" <[EMAIL PROTECTED]> wrote:
> >> I posted this code last night in response to another thread, and after I
> >> posted it I got to wondering if I had misused the list comprehension. 
> >> Here's
> >> the two examples:
>
> >> Example 1:
> >> 
> >> def compress(s):
> >>     new = []
>
> >>     for c in s:
> >>         if c not in new:
> >>             new.append(c)
> >>     return ''.join(new)
> >> --
>
> >> Example 2:
> >> 
> >> def compress(s):
> >>     new = []
> >>     [new.append(c) for c in s if c not in new]
> >>     return ''.join(new)
> >> --
>
> >> In example 1, the intention to make an in-place change is explicit, and 
> >> it's
> >> being used as everyone expects it to be used. In example 2, however, I 
> >> began
> >> to think this might be an abuse of list comprehensions, because I'm not
> >> assigning the result to anything (nor am I even using the result in any
> >> way).
>
> >> What does everyone think about this? Should list comprehensions be used 
> >> this
> >> way, or should they only be used to actually create a new list that will
> >> then be assigned to a variable/returned/etc.?
>
> > Why not make the list comp the actual list you are trying to build?
>
> > def compress(s):
> >     seen = set()
> >     new = [c for c in s if c not in seen and (seen.add(c) or True)]
>
> 
> Isn't
>
>     c not in seen and (seen.add(c) or True)
>
> the same as
>
>     seen.add(c) or c not in seen
>
> ?
>
> >     return ''.join(new)
>
> (notice I haven't closed the tag!)
>
> --
> Arnaud- Hide quoted text -
>
> - Show quoted text -

Unfortunately, no.  "seen.add(c) or c not in seen" will never return
true, since c gets added to seen before testing if c in seen.

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


Re: Misuse of list comprehensions?

2008-05-20 Thread Paul McGuire
On May 20, 10:50 am, [EMAIL PROTECTED] wrote:
>
> You don't need all those conditionals. A set differs from a list
> precisely in the fact that each element is unique. And since the
> function is expecting "s" to be an iterable object, it can be
> constructed even without a for loop:
>
> def compress(s):
>     return list(set(s))
>
> That does the trick.
>
>

Only if order does not need to be maintained.  list(set(s)) will not
necessarily keep the unique characters in the order they are seen.
We'll have to check with the OP to see if this is important (I just
assumed that it was because of the use of list comps).

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


Re: Help with character encodings

2008-05-20 Thread Gary Herron

Gary Herron wrote:

A_H wrote:

Help!

I've scraped a PDF file for text and all the minus signs come back as
u'\xad'.

Is there any easy way I can change them all to plain old ASCII '-' ???

str.replace complained about a missing codec.



Hints?
  


Encoding it into a 'latin1' encoded string seems to work:

 >>> print u'\xad'.encode('latin1')
 -


That might be what you want, but really, it was not a very well thought 
answer.  Here's a better answer:




Using the unicodedata module, i see that the character you have  u'\xad' is

   SOFT HYPHEN (codepoint 173=0xad)


If you want to replace that with the more familiar HYPHEN-MINUS 
(codepoint 45) you can use the string replace, but stick will all 
unicode values so you don't provoke a conversion to an ascii encoded string


   >>> print u'ABC\xadDEF'.replace(u'\xad','-')
   ABC-DEF

But does this really solve your problem?   If there is the possibility 
for other unicode characters in your data, this is heading down the 
wrong track, and the question (which I can't answer) becomes: What are 
you going to do with the string?


If you are going to display it via a GUI that understands UTF-8, then 
encode the string as utf8 and display it -- no need to convert the 
hyphens.   

If you are trying to display it somewhere that is not unicode (or UTF-8) 
aware, then you'll have to convert it.  In that case, encoding it as 
latin1 is probably a good choice, but beware:  That does not convert the 
u'\xad' to an chr(45) (the usual HYPHEN-MINUS),  but instead to chr(173) 
which (on latin1 aware applications) will display as the usual hyphen.  
In any case, it won't be ascii (in the strict sense that ascii is chr(0) 
through chr(127)).  If you *really* *really* wanted straight strict 
ascii, replace chr(173) with chr(45).


Gary Herron




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


Removing Space and "-" from a string

2008-05-20 Thread Ahmed, Shakir
I have thousands of records in MS Access database table, which records I
am fetching using python script. One of the columns having string like 
'8 58-2155-58'

Desired output: '858215558'

I want to remove any spaces between string and any dashes between
strings. I could do it in access manually but want to do from python
script 

Any help is highly appreciated.

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


Re: addendum Re: working with images (PIL ?)

2008-05-20 Thread Poppy
Thank you and the other responders have given me something to consider, I 
understand the concept of the posterize idea and will be experimenting with 
that.

I wanted to respond to this statement below which is true, however I believe 
the histogram sums the values so both colors would be in bin 229.  I say 
that because all white is in histogram element 767, while black is in 
element 0. Anyone on this list know how to interpret the histogram list? 
Your point is still valid regardless of my interpretation.

> So, for example, colours (2, 27, 200) and (200, 27, 2) are both in
> the bin for G=27. But they are very different colours.

I will be checking out the SIG for PIL thanks for that pointer.



"Ken Starks" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>I would still be concerned that you are checking against the percentage
> of the 768 bins returned by the histogram method. Two pixels of
> widely different colour end up in the same bin, so long as just ONE
> of the Red, Green, or Blue components is equal.
>
> So, for example, colours (2, 27, 200) and (200, 27, 2) are both in
> the bin for G=27. But they are very different colours.
>
> There are actualy 256 * 256 * 256 colours, but I don't suppose
> you want that many bins!
>
> What you want is a much smaller number of bins, with pixels
> of 'close' colours (whatever that means) put into the same bin.
>
> What 'close' means for colours, is quite a difficult thing, and
> the consensus is that using the three RGB coordinates is not
> as good as certain other colour spaces.
>
> You could use the ImageOps.posterize method to reduce the number of 
> colours in the image, but whether 'close' colours end up together,
> I don't know.
>
> You might try the PIL special interest group (SIG) 'image-sig'
>
> http://mail.python.org/mailman/listinfo/image-sig
>
> (If you want to know exactly how many unique colours an image actually
> has, load the image into the 'GIMP' assuming you have it,
> and go to :
>
> Menubar --> Filters --> Colours --> Colourcube analysis...
>
> )
>
>
>
>
>
>
>
>
>
>
> Poppy wrote:
>> Thanks, since posting I  figured out how to interpret the histogram 
>> results, which seems to be the consensus in responses. I wrote a check 
>> image program and have been periodically calling it against a folder 
>> where I make a copy of our images used for production. My method right 
>> now is to check what we send for errors, but is not preventive.
>>
>> Also I determined whitespace is not the only issue, any color that 
>> dominates. I'm considering rewriting this code below to setup bins, so if 
>> combined neighboring colors exceeds the threshold then reject the image. 
>> I have examples where half the image appears black, but actually varies 
>> throughout.
>>
>> Since my image is RGB I'm looping through a 768 element list.
>>
>> Zach-
>>
>> import Image, os
>>
>>
>> def check_image(file):
>>
>> try:
>> im = Image.open(file)
>> except:
>> return "Can't open file %s " % file
>>
>> imData = im.histogram()
>> i = 0
>> for ea in imData:
>> if ea > ((im.size[0] * im.size[1]) / 4): ## 25% of image size
>> return "bad image %s - %s element num is %s " % (file, ea, 
>> str(i))
>> i = i + 1
>>
>> return "good image %s, image size is %s" % (file, im.size)
>>
>>
>> def main(dir):
>> data = ""
>> try:
>> files = os.listdir(dir)
>> for ea in files:
>> data = data + str(check_image(os.path.join(dir,ea))) + "\n"
>> except:
>> return "Can't get files in %s" % dir
>> return data
>>
>> print main("host\\path\\to\\image_folder\\")
>> 

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


Re: Showing the method's class in expection's traceback

2008-05-20 Thread Gabriel Genellina
En Mon, 19 May 2008 10:54:05 -0300, Agustin Villena  
<[EMAIL PROTECTED]> escribió:

On May 18, 4:31 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:

Agustin Villena schrieb:

> is there anyway to show the class of amethodin an exception's
> traceback?
> I want to improve the line
> File "G:\dev\exceptions\sample.py", line 3, in foo
> to
> File "G:\dev\exceptions\sample.py", line 3, in Some.foo

It should be. You can get a dictionary of the locals of an exception
stack frame, of which you could extract the self-parameter's class.


I digged on sys.exc_info() object and the traceback module and I
could't figure how I can get the locals() of the exception stackframe


Put this function in traceback.py, and replace the lines
name = co.co_name
with
name = guess_full_method_name(f)
everywhere.
Please read the docstring!!!

def guess_full_method_name(frame):
"""Guess classname.methodname for a given frame.

Only a guess!
Assumes the frame belongs to an instancemethod
whose first argument is "self", or a classmethod
whose first argument is "cls".
Doesn't handle correctly any other situation.
Returns the class name of the object on which
the method was called, not the class where
the method was actually defined.
"""
cls_name = None
fun_name = frame.f_code.co_name
self = frame.f_locals.get('self', None)
if self is None:
cls = frame.f_locals.get('cls', None)
else:
cls = getattr(self, '__class__', None)
if cls is not None:
cls_name = getattr(cls, '__name__', None)
if cls_name is not None:
return '%s.%s' % (cls_name, fun_name)
return fun_name

--
Gabriel Genellina

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


Re: Removing Space and "-" from a string

2008-05-20 Thread s0suk3
On May 20, 11:02 am, "Ahmed, Shakir" <[EMAIL PROTECTED]> wrote:
> I have thousands of records in MS Access database table, which records I
> am fetching using python script. One of the columns having string like
> '8 58-2155-58'
>
> Desired output: '858215558'
>
> I want to remove any spaces between string and any dashes between
> strings. I could do it in access manually but want to do from python
> script
>
> Any help is highly appreciated.

string.replace('-', '').replace(' ', '')
--
http://mail.python.org/mailman/listinfo/python-list


Re: Misuse of list comprehensions?

2008-05-20 Thread Paul Hankin
On May 20, 3:58 pm, Paul McGuire <[EMAIL PROTECTED]> wrote:

> def compress(s):
> seen = set()
> return ''.join(c for c in s if c not in seen and (seen.add(c) or
> True))

Slightly nicer is to move the set add out of the conditional...

def compress(s):
seen = set()
return ''.join(seen.add(c) or c for c in s if c not in seen)

I wouldn't write it this way though :)

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


Accumulating values in dictionary

2008-05-20 Thread Zack
Given a bunch of objects that all have a certain property, I'd like to
accumulate the totals of how many of the objects property is a certain
value. Here's a more intelligible example:

Users all have one favorite food. Let's create a dictionary of
favorite foods as the keys and how many people have that food as their
favorite as the value.

d = {}
for person in People:
fav_food = person.fav_food
if d.has_key(fav_food):
d[fav_food] = d[fav_food] + 1
else:
d[fav_food] = 1

d ends up being something like: {'pie': 400, 'pizza': 200, 'burgers':
100} if 400 people like pie, 200 like pizza and 100 like burgers.

There's nothing wrong (that I know of) by doing it as I have up there,
but is there a simpler, easier way? Looking forward to hearing about
how much of a n00b I am. Thanks in advance!
--
http://mail.python.org/mailman/listinfo/python-list


Access to sysctl on FreeBSD?

2008-05-20 Thread Skye
How do I access the sysctl(3) call from Python on BSD?

Specifically I want to retrieve:

$ sysctl -d net.inet.ip.stats
net.inet.ip.stats: IP statistics (struct ipstat, netinet/ip_var.h)

So I'll need some way of getting to struct ipstat from Python as well

Thanks,
Skye

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


Re: default gettext localedir on windows

2008-05-20 Thread ZeeGeek
On May 17, 8:39 pm, Thorsten Kampe <[EMAIL PROTECTED]> wrote:
> * ZeeGeek (Sun, 4 May 2008 10:56:52 -0700 (PDT))
>
> > On May 5, 1:16 am, Thorsten Kampe <[EMAIL PROTECTED]> wrote:
> > > * ZeeGeek (Sun, 4 May 2008 08:59:05 -0700 (PDT))
>
> > > > Hi, what's the default localedir for gettext module on windows? In
> > > > Linux, it's /usr/share/locale. Where should I put the *.mo file in
> > > > order to make the translation work?
>
> > > %PYTHONHOME%\share\locale
>
> > I tried moving the *.mo file into %PYTHONHOME%\share\locale\zh_CN
> > \LC_MESSAGES, but still no luck. The following is the code snippet I
> > use:
>
> > import gettext
> > gettext.install('testprogram', unicode = True)
>
> The syntax is correct. Is the test program localised under Linux or
> Cygwin? If not then the error is somewhere in your application.

Yes, it's localized under linux.

> How did you try to set the language on Windows? It works for me on Vista
> with "set LANG=de" or "set LANGUAGE=de" while I think under XP it had to
> be "set LANG=de_DE" (LANGUAGE or de alone did not work if I remember
> correctly).

I'm using Simplified Chinese Windows, so I didn't set anything
specifically before firing up the program. I found that if I use
gettext.GNUTranslations to read in the .mo file, then it's localized.
But gettext.install doesn't work.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Misuse of list comprehensions?

2008-05-20 Thread John Salerno
"Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> the above code is pretty much of a no-no because it has quadratic runtime
> behavior.


What's that mean, exactly? Are you referring to both examples, or just the 
second one? 


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


RE: Removing Space and "-" from a string

2008-05-20 Thread Ahmed, Shakir
Thanks, works exactly what I needed.

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
[EMAIL PROTECTED]
Sent: Tuesday, May 20, 2008 12:22 PM
To: python-list@python.org
Subject: Re: Removing Space and "-" from a string

On May 20, 11:02 am, "Ahmed, Shakir" <[EMAIL PROTECTED]> wrote:
> I have thousands of records in MS Access database table, which records
I
> am fetching using python script. One of the columns having string like
> '8 58-2155-58'
>
> Desired output: '858215558'
>
> I want to remove any spaces between string and any dashes between
> strings. I could do it in access manually but want to do from python
> script
>
> Any help is highly appreciated.

string.replace('-', '').replace(' ', '')
--
http://mail.python.org/mailman/listinfo/python-list

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


Re: Access to sysctl on FreeBSD?

2008-05-20 Thread Benjamin Kaplan
On Tue, May 20, 2008 at 12:26 PM, Skye <[EMAIL PROTECTED]> wrote:

> How do I access the sysctl(3) call from Python on BSD?
>
> Specifically I want to retrieve:
>
> $ sysctl -d net.inet.ip.stats
> net.inet.ip.stats: IP statistics (struct ipstat, netinet/ip_var.h)
>
> So I'll need some way of getting to struct ipstat from Python as well
>
> Thanks,
> Skye
>

You could use subprocess.Popen to run the command yourself and just parse
the output.

http://docs.python.org/lib/node528.html

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

Re: conventions/requirements for 'is' vs '==', 'not vs '!=', etc

2008-05-20 Thread s0suk3
On May 20, 9:04 am, castironpi <[EMAIL PROTECTED]> wrote:
> On May 20, 5:04 am, Duncan Booth <[EMAIL PROTECTED]> wrote:
>
>
>
> > John Salerno <[EMAIL PROTECTED]> wrote:
> >  a = 'this is longer'
> >  b = 'this is longer'
> >  a == b
> > > True
> >  a is b
> > > False
>
> > > In the above example, Python has created only one string called
> > > 'hello' and both x and y reference it. However, 'this is longer' is
> > > two completely different objects.
>
> > That is true when run interactively, but the behaviour changes again if you
> > run it as a script:
>
> > C:\Temp>type t.py
> > a = 'this is longer'
> > b = 'this is longer'
> > print a is b
>
> > C:\Temp>t
> > True
>
> > In short, two equal strings may or may not be identical and any code which
> > makes assumptions based on observed behaviour is broken. If you want to be
> > able to test identity on strings safely then use the 'intern()' builtin to
> > get repeatable behaviour.
>
> > --
> > Duncan Boothhttp://kupuguy.blogspot.com
>
> There is no such thing as real true equals.

That's quite some dark deep philosophy. Fortunately, computers are not
as complicated as real life. I would argue that, programatically,
there *is* such thing as "real true equals". I would also argue that
that view is most certainly not shared by everyone.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Misuse of list comprehensions?

2008-05-20 Thread Arnaud Delobelle
Paul McGuire <[EMAIL PROTECTED]> writes:

> On May 20, 10:17 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:
[...]
>> 
>> Isn't
>>
>>     c not in seen and (seen.add(c) or True)
>>
>> the same as
>>
>>     seen.add(c) or c not in seen
>>
>> ?
>>
>> >     return ''.join(new)
>>
>> (notice I haven't closed the tag!)
>>
>> --
>> Arnaud- Hide quoted text -
>>
>> - Show quoted text -
>
> Unfortunately, no.  "seen.add(c) or c not in seen" will never return
> true, since c gets added to seen before testing if c in seen.
>
> -- Paul

Ha you're right of course.  But I haven't closed the tag yet, so:

c not in seen and (seen.add(c) or True)

is definitely the same as

c not in seen and not seen.add(c)

which is

not (c in seen or seen.add(c))

:)



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


Re: Removing Space and "-" from a string

2008-05-20 Thread python
Shakir,

> I have thousands of records in MS Access database table, which records I
> am fetching using python script. One of the columns having string like
> '8 58-2155-58'
>
> Desired output: '858215558'
>
> I want to remove any spaces between string and any dashes between
> strings. I could do it in access manually but want to do from python
> script

Try this:

>>> input = '8 58-2155-58'
>>> output = ''.join( [ c for c in input if c not in ' -' ] )
>>> output
'858215558'

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


Re: Using Python for programming algorithms

2008-05-20 Thread sturlamolden
On May 19, 8:07 pm, Vicent Giner <[EMAIL PROTECTED]> wrote:

> By the way, is it possible (and easy) to call a C function from a
> Python program??

Yes it is. You can e.g. use ctypes for that.


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


RE: do you fail at FizzBuzz? simple prog test

2008-05-20 Thread Sells, Fred
or
for i in range(1,100):
  print ('fizz','','')[i%3] + ('buzz','','','','')[i%5] or i

> > 
> > "Write a program that prints the numbers from 1 to 100. But for
> > multiples of three print "Fizz" instead of the number and for the
> > multiples of five print "Buzz". For numbers which are multiples of
> > both three and five print "FizzBuzz".
> > 
> > for i in range(1,101):
> > if i%3 == 0 and i%5 != 0:
> > print "Fizz"
> > elif i%5 == 0 and i%3 != 0:
> > print "Buzz"
> > elif i%5 == 0 and i%3 == 0:
> > print "FizzBuzz"
> > else:
> > print i
> > 
> > 
> > is there a better way than my solution? is mine ok?
> 
> 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Accumulating values in dictionary

2008-05-20 Thread Zack
On May 20, 12:26 pm, Zack <[EMAIL PROTECTED]> wrote:
> Given a bunch of objects that all have a certain property, I'd like to
> accumulate the totals of how many of the objects property is a certain
> value. Here's a more intelligible example:
>
> Users all have one favorite food. Let's create a dictionary of
> favorite foods as the keys and how many people have that food as their
> favorite as the value.
>
> d = {}
> for person in People:
> fav_food = person.fav_food
> if d.has_key(fav_food):
> d[fav_food] = d[fav_food] + 1
> else:
> d[fav_food] = 1
>
> d ends up being something like: {'pie': 400, 'pizza': 200, 'burgers':
> 100} if 400 people like pie, 200 like pizza and 100 like burgers.
>
> There's nothing wrong (that I know of) by doing it as I have up there,
> but is there a simpler, easier way? Looking forward to hearing about
> how much of a n00b I am. Thanks in advance!

Er. OK so I realize now that I could have just done this:

d = {}
for person in people:
fav_food = person.fav_food
d[fav_food] = d.get(fav_food, 0) + 1

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


Re: Using Python for programming algorithms

2008-05-20 Thread sturlamolden
On May 19, 10:42 pm, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:

> Well... They do - they are called 'C compilers' !-) As Roel Schroven
> mentioned - and he is at least partially right on this point - C has
> been designed to make optimizing C compiler not to hairy to write.

C has proven very difficult to optimize, particularly because pointer
aliasing prevents efficient register allocation.

Fortran was designed with ease of optimization in mind. Not many years
ago, it was not uncommon for Fortran code to run twice as fast as
equivalent C. C compilers have recently closed on on the gap by
becoming extremely good at what they do. But that is not because C is
easy to optimize. On the contrary.

For serious number crunshing, there is nothing that compares to
Fortran, even today. f2py makes it easy to call Fortran subroutines
from Python.





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


  1   2   3   >