wxpython problems using GLCanvas

2006-10-08 Thread nelson
Hi,
  i'm coding a 3d interactive geometry progam and i want to use opengl
for displaying ogbjects. I can open a opengl canvas (i follow the
wxpython demo) and i can draw a cube. First time i execute the
application all is ok. second time it freezes X and i can't interact
with my machine. I hava only to press power button.. what will
cause it? the code i use is exactly the code of the demo

If using openGL is so difficult to do together with wxpython, what
toolkit can i use do do 3d graphics? Simple graphics, i have to plot
point, lines, sphere, surfaces and their intersections...

thanks,
  nelson

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


3D Vector Type Line-Drawing Program

2006-10-11 Thread nelson -
hi,


> > Take a look at VPython -- easy to start, 3-D display (wall-eye /
> > cross-eye) easy to run on.
>
> I really like VPython because of how easy it is to use once you are familiar
> with it.
>
> Is there a way to have the display show a wire frame image instead of shaded
> shapes?  And with or without hidden line removal?
>

i'm goind the same. I looked to vpython, but i can't make it go with
wxpython. So i make a slightly different question. There is a library
that fits well with wxpython that have features similar to vpython?

thanks,
  nelson
-- 
http://mail.python.org/mailman/listinfo/python-list


3d programming without opengl

2006-10-30 Thread nelson -
hi!
   i want to build up a simple 3d interactive geometry application in
python. Since i want to run it without 3D acceleration (a scene will
be quite simple) I was wondering if there was a library in python that
allow me to build 3D graphic without the need to use OpenGL I
google but i can't find nothing interesting... (the best would be a
pure python solution)

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


Re: 3d programming without opengl

2006-10-31 Thread nelson -
Hi paul,
 i look at slut and it seem very good... Can i embed it into a
wxpython application?

thanks,
 nelson
-- 
http://mail.python.org/mailman/listinfo/python-list


ANN: pyfuzzylib 0.1.3 Released

2006-09-16 Thread nelson -
PyFuzzyLib is a library for fuzzy inference engine building. Using
pyfuzzylib you can add fuzzy logic to your programs. The program is in
it early stage of development, but it is still usable. Every sort of
feedback is appreciated!

the project homepage is

http://sourceforge.net/projects/pyfuzzylib


good afternoon,
   nelson
-- 
http://mail.python.org/mailman/listinfo/python-list


Window, Windows, Linux, client and server...

2006-12-07 Thread nelson -
hi all!
  i'm trying to implement an appllication with this two requirements.
I have a server and some clients. I want to be able to launch an
application (openoffice impress, for example) and display what i'm
doing on the server on the client. Conversely, I want to be able to
have a picture of the desktop client. Is it a thing that i can do in
python? any advice? I googled but i can't find something too useful...
 II know i can use vnc, but i want a pure python solution... if it's
possibile... Doing it using VNC it seems not so "clear"... :)

thanks,
  nelson
-- 
http://mail.python.org/mailman/listinfo/python-list


FloatCanvas in a wxpython application.... layout problems

2007-02-21 Thread nelson -
Hi all,
 i'm developing an application that uses Floatcanvas to diplay a
cartesian plane. how can i embed it into a "complex" layout?

I have this controls


MENUS
TOOLBAR
NOTEBOOK
FLOATCANVAS
PYSHELL

The problem is that i can show all the components but i can't insert
the floatcanvas in the right position... it is'n displaied... can
annyone give me an example of use of floatcanvas in an application
with more than one panel...

thanks a lot!

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


Integrating python with smalltalk

2006-03-23 Thread nelson
Hi all,
  sorry if it isn't the right group (and sorry for the cross post, but
it's a cross- question :P ), but i'm not sure where to post. I have a
python library and i want to be able to call it from smalltalk (the
squeak implementation in particular..). I was just wondering if there
is a standard mechanism to do it. On windows i know i can create in
python a COM server and call it from smalltalk, but i wonder if there
is a more general solution (platform independent).

thanks for any advice,
  nelson

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


COM Server & wxPython

2006-04-01 Thread nelson
Hi all,
 i'm doing a COM server that have to expose some graphics (panels and
configuration controls), that would be embedded in an application through
OLE. I was wondering if I can do this using wxPython. Another question is
if my COM server would inherits from one of the wxPython class. If it's
true, from which? wx.App, wx.Frame, wx.Panel.

The important thing is that i would to have the same effect that you have
when you the calendar OLE control on a Delphi or VB form (you see the
graphics ;) )

thanks,
  nelson 

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


strange TypeError exception in function compiled from a string

2010-12-01 Thread nelson
Hi all,
  I have this function, defined in a string and ecetuted through ad
exec call

def cell1(d):

x=d.get('x')
print x

import y
return y.add(y.add(self.adf0(x),self.adf0(x)),self.adf0(x))


d is a dict of this kind {'x':2}

I receive the following exception, that i find very strange...

  File "", line 7, in cell1
TypeError: 'dict' object is not callable

I have tested all the function al line 7, and none of them raise any
exception.
Have anyone some suggestion on how to solve this?

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


Remote audio recording in a multiuser environment

2009-07-14 Thread nelson -
Hi all!
   I'm developing an application that will be used in a school. It
will allow client connected over ssh to liste to a multimedia file on
the server. The server componente will record the audio taken from a
specified client microphone, or it would pair two client, allowing
them to communicate each other registering the conversation.

I think to use pygst to record the microphone, but i can' figure out
how to register remote sound and how to create the channel between two
client. Do you think i have to use twisted (i don't know this
framework at all), or there is a simple solution?

I will surely use wxpython for the UI on the client and on the server.

Thank for any advice,
  nelson

-- 
Consulenze Linux e Windows

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


Re: Python presentations

2012-09-18 Thread Trent Nelson
On Thu, Sep 13, 2012 at 09:00:19AM -0700, andrea crotti wrote:
> I have to give a couple of Python presentations in the next weeks, and
> I'm still thinking what is the best approach.
> 
> In one presentation for example I will present decorators and context
> managers, and my biggest doubt is how much I should show and explain in
> slides and how much in an interactive way (with ipython for example).

FWIW, I gave a presentation on decorators to the New York Python
User Group back in 2008.  Relevant blog post:

http://blogs.onresolve.com/?p=48

There's a link to the PowerPoint presentation I used in the first
paragraph.  It's in .pptx format; let me know if you'd like it in
some other form.

Regards,

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


Re: Are ABCs an anti-pattern?

2012-10-05 Thread Trent Nelson
On Tue, Oct 02, 2012 at 07:23:05AM -0700, Demian Brecht wrote:
> I don't use them anymore, but I'm curious about others opinions on this 
> list...

I like them.  In particular, I like that I can enumerate all the
subclasses that happen to implement the ABC via the metaclass's
__subclasses__() method.  I also like that I can trust Python
not to instantiate a subclass of an ABC unless it meets all the
interface criteria I've stipulated.

Both of these aspects make ABCs a great fit when your base classes
will be subclassed by other users/libraries (i.e. that you have no
control over).

For example, I use them in a project of mine called Enversion in
order to automatically generate command-line scaffolding with as
little effort (programming/typing-wise as possible).  The ABC
stuff lives in 'evn.cli' and provides a set of generic classes
for command-line tools.

One of the command line tools that ships with Enversion is the
'evnadmin' command.  Thanks to all the ABC scaffolding in the
'evn.cli/command' module, all I need to write is the following
to get an 'evnadmin' command-line interface:

from evn.cli import (
CLI,
CommandLine,
)

class AdminCommandLine(CommandLine):
@property
def commands_module(self):
return evn.admin.commands

class AdminCLI(CLI):

@property
def program_name(self):
return 'evnadmin'

@property
def commandline_subclasses(self):
return AdminCommandLine.__subclasses__()

class DoctestCommandLine(AdminCommandLine):
_quiet_ = True

class DumpDefaultConfigCommandLine(AdminCommandLine):
pass

class DumpConfigCommandLine(AdminCommandLine):
_conf_ = True

class ShowConfigFileLoadOrderCommandLine(AdminCommandLine):
_conf_ = True

class DumpHookCodeCommandLine(AdminCommandLine):
_conf_ = True
_repo_ = True

[snip]

Running 'evnadmin' with no arguments yields this:

% evnadmin
Type 'evnadmin  help' for help on a specific subcommand.

Available subcommands:
analyze
create
disable-remote-debug (drd)
doctest
dump-config (dc)
dump-default-config (ddc)
dump-hook-code (dhc)
enable
enable-remote-debug (erd)
find-merges (fm)
fix-hooks (fh)
root-info (ri)
run-hook (rh)
show-config-file-load-order (scflo)
show-repo-hook-status (srhs)
show-repo-remote-debug-sessions (srrds)
show-roots (sr)
toggle-remote-debug (trd)
version

The scaffolding can generate all of that automatically thanks to the
ability to enumerate __subclasses__() of a metaclass.  The fact that
Python will bomb out if a derived class doesn't implement all the
required methods/properties is icing on the cake.  When you are
expecting other developers to leverage your ABCs, getting instant
feedback when you forget to implement a required method/property
is incredibly useful.  It's definitely not a case of battling the
design decisions made by the ABC author...

Relevant Python files:
https://github.com/tpn/enversion/blob/master/evn/cli.py
https://github.com/tpn/enversion/blob/master/evn/admin/cli.py

Regards,

Trent.

> Demian Brecht
> @demianbrecht
> http://demianbrecht.github.com
> -- 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


AI Example Help

2012-10-11 Thread Trevor Nelson
Hello all,

For the first time in well... a very long time I am coming to a loss on where 
to really get started here on the project I wish to undertake to help me really 
get into Python programming.

A quick explanation of how I learn. I learn best with example working small 
code bits. With the code I then disect it to understand how each part works. 
(within "beginner" limits of course).


I have been looking around the web and google searching a whole lot the past 
week about and really have not had much luck on what I am needing / that is 
really helpful. So, I am here to beg/plead for some help on this.

My end game goal is to create a "hybrid" chat bot. Such as that you can ask it 
questions and talk to it etc. Such as asking "What's the server's current 
load?". I found out I could do that with the HOWIE bot I have been playing 
with. But, I am looking to build up a more "AI" system where instead of me 
asking all the questions, the bot can yell at me and say " HEY THE SERVER IS 
OUT OF RESOURCES".


Further developement will lead to a FAQ style bot as well. Such as that where I 
can ask them things like (for a very basic example, what is a 404 error?), or 
even (what is causing a 500 ISE on the site? [by having it read the logs and 
tell me what the logs say for the site])


These are just the basics to help me get started. As I get more advanced and 
better at this, I would like to be able to have it crawl sites and learn 
different informain. Something as far fetched and I consider a "dream" would be 
it crawling the cpanel forums and gathering up the responses to questions so 
that I could query it as well.


So, as I really, really, hate to have to ask. Does anyone have any example 
coding in what my basic needs are? As I feel that working off the HOWIE bot 
won't turn out to what I am wanting and then I will end up wasting a whole lot 
of time going in the wrong direction. Which is also why I am here


TL;DR
I really would truely appreciate and example coding of how to put together an 
initial basic "AI" bot where it can monitor the system and tell me alerts as 
with being able to query it for questions. As with I am looking for some sort 
of time efficient way for it to gather it's own data. As otherwise all the 
information I would have to manually put in would be extreme.


I greatly appreciate any help you guys can prodive me with to help me get 
started. Before you assume I want everyone to do all the work for me, it's not 
the case. It's just that I learn best/faster from example code.
Also, I am thinking of going with python 2.7 for this since it seems all the 
documentation I run into isn't for the 3.x Python.


Thank you so much anyone who can help me. Also, if you are someone who actually 
knows how to do everything I have mentioned, I would even ben willing to hire a 
"tutor" to teach me these things.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: AI Example Help

2012-10-11 Thread Trevor Nelson

> 
> An interesting concept. AI monitoring systems have never really
> 
> appealed to me; I personally prefer something with simpler and clearer
> 
> rules (eg "if server load exceeds 3.0, raise an alert"), coupled with
> 
> information retrieval commands that read like commands, not natural
> 
> English. But I can see the value of the more "human-friendly style",
> 
> if that's the term for it.
The other goal on this project is to make the application work it's best to 
automatically narrow down the cause and correct it by itself with a report.



> 
> > I greatly appreciate any help you guys can prodive me with to help me get 
> > started. Before you assume I want everyone to do all the work for me, it's 
> > not the case. It's just that I learn best/faster from example code.
> 
> > Also, I am thinking of going with python 2.7 for this since it seems all 
> > the documentation I run into isn't for the 3.x Python.
> 
> >
> 
> 
> 
> Understandable.
> 
> 
> 
> I would recommend treating this as two completely separate projects:
> 
> 1) Natural language request/response - the "AI" bit, tinkering with
> 
> the HOWIE bot
> 
> 2) Server status querying, log analysis, etc, etc.
> 
> 
> 
> You may already be familiar with one or the other, I don't know. But
> 
> in any case, they're quite separate; it'll be easier to develop one
> 
> and then the other. Divide your problem into pieces, then divide the
> 
> pieces further, and keep on dividing until you have a series of
> 
> one-banana problems[1].

This is what I have actually been thinking of doing is splitting up the HOWIE 
bot.  But, that is one thing I am working on still to really figure out how to 
split it apart between automagicness of telling you things without user input.  
And then the input and answer part.  Although, from what I can think of at this 
time is to write a main program that is the "Automagic part" and then it can 
call in Howie as needed?
> 
> 
> 
> I would also recommend using Python 3.3, for several reasons:
> 
> * There's plenty of docs for 3.3; perhaps web searches are
> 
> highlighting 2.x, but if you go to the main docs page[2] you'll find
> 
> 3.3 there.
> 
> * Py3 has much better Unicode support than Py2, a significant factor
> 
> when you're working with natural language parsing
> 
> * The 2.x line is no longer being developed. Only bugfixes and
> 
> security patches will be applied. 3.x, on the other hand, has cool
> 
> toys added periodically with new versions.

I will have to see how HOWIE runs with python 3, and give it a shot.

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


Re: Questions about GIL and web services from a n00b

2011-04-19 Thread Lamont Nelson
 > 1. Are you sure you want to use python because threading is not
good due
> to the Global Lock (GIL)?  Is this really an issue for multi-threaded
> web services as seems to be indicated by the articles from a Google
> search?  If not, how do you avoid this issue in a multi-threaded process
> to take advantage of all the CPU cores available?

To take advantage of the cores on your server you'll want to consider
a multi-process design instead of multi-threading. You can achieve
this with something like http://projects.unbit.it/uwsgi/, which will
allow you to manage the processes. I've used this behind apache
successfully.

>
> 2. Are there good web services frameworks available for building a REST
> based service?  I admit I have looked at web2py, Django, pyramid/pylons,
> and a few others.  SOAP seems to be pretty well supported but I'm not
> finding the same for quick development of REST based services for
> exchanging JSON or XML formatted data.  This is probably just my n00b
> status, but what tools are best for building a simple REST data exchange
> API?

I've personally used Pyramid to implement REST web services with
multiple data transport formats (json, xml) for the same endpoints
with minimal fuss. It's basically as simple as returning an object
from your view and defining a renderer that knows how to translate
this object to the desired format. Look at
http://docs.pylonsproject.org/projects/pyramid/1.0/narr/renderers.html#views-which-use-a-renderer
and 
http://docs.pylonsproject.org/projects/pyramid/1.0/narr/viewconfig.html#view-config-chapter
for more information.

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


RE: Coolest Python recipe of all time

2011-05-09 Thread Trent Nelson
> What are your favorites?

I think I've posted this before, but I love my 
3-lines-if-you-ignore-the-scaffolding language translator.  Not because it's 
clever code -- quite the opposite, the code is dead simple -- but because it 
encompasses one of the things I love about Python the most: it gets shit done.

In [1]: from translate import *

In [2]: translate('French', 'The quick brown fox jumped over the lazy dog.')
Le renard brun rapide a sauté par-dessus le chien paresseux.

In [3]: translate('German', 'The quick brown fox jumped over the lazy dog.')
Der schnelle braune Fuchs sprang über den faulen Hund.

In [4]: translate('Spanish', 'The quick brown fox jumped over the lazy 
dog.')
El zorro marrón rápido saltó sobre el perro perezoso.

translate.py:

import sys
from urllib import urlopen, urlencode
from BeautifulSoup import BeautifulSoup

url = 'http://babelfish.altavista.com/tr'
languages = {
'French': 'en_fr',
'German': 'en_de',
'Italian'   : 'en_it',
'Spanish'   : 'en_es',
'Russian'   : 'en_ru',
'Portuguese': 'en_pt',
'Dutch' : 'en_nl',
'Japanese'  : 'en_ja',
}

def translate(lang, text):
kwds = { 'trtext' : text, 'lp' : languages[lang]}
soup = BeautifulSoup(urlopen(url, urlencode(kwds)))
print soup.find('div', style='padding:10px;').string

if __name__ == '__main__':
translate(sys.argv[1], sys.argv[2])


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


Re: Ann: New Python curses book

2021-04-11 Thread Daniel Nelson
On Tue, Mar 30, 2021 at 12:12:19PM +0100, Alan Gauld via Python-list wrote:
> I've just published, in Kindle and paperback formats,
> my book on "Programming curses with Python".
> 
> https://www.amazon.co.uk/dp/B091B85B77/
> 
> (It should be available in most other Amazon stores too)

This looks handy, I'd love to buy a copy but I don't do business with
Amazon if I can avoid it.  Any chance this will be available from other
locations?


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


stat_result.st_ino from os.stat isn't constant on a network drive.

2020-08-26 Thread Andrew Nelson
OS: windows 7
Python: 3.8

I am trying to track which files have been modified in a directory on a
network mount. When I call the os.stat function for a given file in
that directory the stat_result.st_ino changes every time I call it:

```
>>> os.stat('U:\data\current\myfile')
os.stat_result(st_mode=33206, st_ino=18446735965107825360, st_dev=33674366,
st_nlink=1, st_uid=0, st_gid=0, st_size=433874, st_atime=1598333128,
st_mtime=1598330233, st_ctime=1598329773)

>>> os.stat('U:\data\current\myfile')
os.stat_result(st_mode=33206, st_ino=18446735965183676432, st_dev=33674366,
st_nlink=1, st_uid=0, st_gid=0, st_size=433874, st_atime=1598333128,
st_mtime=1598330233, st_ctime=1598329773)
```
It's the same file, why on earth does the st_ino field change everytime?
st_ino is supposed to "uniquely identify the file for a given value of
st_dev."
If I do the same thing on a local file instead of on a network mount st_ino
is constant and doesn't change.

Is this a Python bug, or is it an issue with the way Windows deals with
network mounts?

Andrew.
-- 
https://mail.python.org/mailman/listinfo/python-list


Unicode charmap decoders slow

2005-10-02 Thread Tony Nelson
Is there a faster way to decode from charmaps to utf-8 than unicode()?

I'm writing a small card-file program.  As a test, I use a 53 MB MBox 
file, in mac-roman encoding.  My program reads and parses the file into 
messages in about 3..5 seconds, but takes about 13.5 seconds to iterate 
over the cards and convert them to utf-8:

for i in xrange(len(cards)):
u = unicode(cards[i], encoding)
cards[i] = u.encode('utf-_8')

The time is nearly all in the unicode() call.  It's not so much how much 
time it takes, but that it takes 4 times as long as the real work, just 
to do table lookups.

Looking at the source (which, if I have it right, is 
PyUnicode_DecodeCharmap() in unicodeobject.c), I think it is doing a 
dictionary lookup for each character.  I would have thought that it 
would make and cache a LUT the size of the charmap (and hook the 
relevent dictionary stuff to delete the cached LUT if the dictionary is 
changed).

I thought of using U"".translate(), but the unicode version is defined 
to be slow.  Is there some similar approach?  I'm almost (but not quite) 
ready to try it in Pyrex.

I'm new to Python.  I didn't google anything relevent on python.org or 
in groups.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


gtk.TextView.move_mark_onscreen() broken?

2005-10-02 Thread Tony Nelson
Is gtk.TextView.move_mark_onscreen() broken?  Perhaps only in Python's 
gtk module, in Python 2.3, gtk 2.4.14?  I'm asking here because I'm 
using gtk from Python and don't want to write a C program to verify my 
issue.  I've also tried gtk.TextView.scroll_to_mark() and 
gtk.TextView.place_cursor_onscreen(), and none of them want to do 
anything.  The rest of my program works, so I'm not a complete gtk bazo.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode charmap decoders slow

2005-10-03 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:

> Tony Nelson wrote:
> > Is there a faster way to decode from charmaps to utf-8 than unicode()?
> 
> You could try the iconv codec, if your system supports iconv:
> 
> http://cvs.sourceforge.net/viewcvs.py/python-codecs/practicecodecs/iconv/

I had seen iconv.  Even if my system supports it and it is faster than 
Python's charmap decoder, it might not be available on other systems.  
Requiring something unusual in order to do a trivial LUT task isn't an 
acceptable solution.  If I write a charmap decoder as an extension 
module in Pyrex I can include it with the program.  I would prefer a 
solution that doesn't even need that, preferably in pure Python.  Since 
Python does all the hard wark so fast it certainly could do it, and it 
can almost do it with "".translate().

TonyN.:'[EMAIL PROTECTED]
  '  <http://www.georgeanelson.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Unicode charmap decoders slow

2005-10-03 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:

> Tony Nelson wrote:
> > I had seen iconv.  Even if my system supports it and it is faster than 
> > Python's charmap decoder, it might not be available on other systems.  
> > Requiring something unusual in order to do a trivial LUT task isn't an 
> > acceptable solution.  If I write a charmap decoder as an extension 
> > module in Pyrex I can include it with the program.  I would prefer a 
> > solution that doesn't even need that, preferably in pure Python.  Since 
> > Python does all the hard wark so fast it certainly could do it, and it 
> > can almost do it with "".translate().
> 
> Well, did you try a pure-Python version yourself?
> 
> table = [chr(i).decode("mac-roman","replace") for i in range(256)]
> 
> def decode_mac_roman(s):
>  result = [table[ord(c)] for c in s]
>  return u"".join(result)
> 
> How much faster than the standard codec is that?

It's .18x faster.

TonyN.:'[EMAIL PROTECTED]
  '  <http://www.georgeanelson.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Exception raising, and performance implications.

2005-10-03 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "leo" <[EMAIL PROTECTED]> wrote:

> Hello all -
> 
> I was wondering about the performance implications of explicitly
> raising exceptions to get information about the current frame.
> Something like what the inspect module does, with:

Python uses exceptions internally, using StopIteration to terminate the 
iterator in a for: loop.

> ---
> def currentframe():
> """Return the frame object for the caller's stack frame."""
> try:
> raise 'catch me'
> except:
> return sys.exc_traceback.tb_frame.f_back
> ---

This also does a traceback; you might want to measure the cost of that.

> I come from a java background, where Exceptions are a big Avoid Me, but
> are the performance implications the same in Python? We're expecting a
> big load on our app (100,000 users/hour) , so we'd like to be as tuned
> as possible.

Switching to Python, eh?  Remember to measure, measure, measure!

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Controlling who can run an executable

2005-10-04 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Cigar" <[EMAIL PROTECTED]> wrote:

> I am developing a program for a client.  She runs a shop where her
> clients bring in items for sale or short term buyback.  Development of
> the program has been going great but she's mentioned that there is a
> 'feature' coming up in the next couple of weeks that she'd like me to
> implement that has me a bit worried.
> 
> My client has told me a story of how she hired someone from a competing
> store and that person had brought a copy of the program her competition
> was using to track clients and transactions.  He couldn't demonstrate
> the program for one reason or another because it was protected in a way
> that neither could circumvent. (She didn't remember how it was
> protected, she had hired this person a long time ago.)
> 
> Now that I'm three months into the development of this program, my
> client tells me she would like to protect her investment by preventing
> her employees from doing the same to her.  (Going to the competition
> and using her program.)
 ...

Call the competition and ask them what they used.  Point out that it 
worked.  If they won't tell you, just look at their software until you 
find out.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C Wrapper Function, crashing Python?

2005-10-14 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Java and Swing" <[EMAIL PROTECTED]> wrote:

> one more update...
> 
> if I remove PyMem_Free and free(...) ...so no memory clean up...I can
> still only call doStuff 4 times, the 5th attemp crashes Python.
> 
> Java and Swing wrote:
> > update:
> > if I use C's free(result), free(a) free(b) instead of PyMem_Free...I
> > only get one successfuly use/call of doStuff.
> >
> > i.e.
> > // this works
> > doStuff(...)
> >
> > // python crashes here
> > doStuff(...)
> >
> > Java and Swing wrote:
> > > static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
> > >   // this will store the result in a Python object
> > >   PyObject *finalResult;
> > >
> > >   // get arguments from Python
> > >   char *result = 0;
> > >   char *in= 0;
> > >   char *aString = 0;
> > >   char *bString = 0;
> > >   MY_NUM *a;
> > >   MY_NUM *b;
> > >   int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
> > >   if (!ok) return 0;
> > >
> > >   // do work to get a and b
> > >   // count - returns an int;  GetVal - returns a char *
> > >   a = GetVal(aString, count(aString, ","));
> > >   b = GetVal(bString, count(bString, ","));
> > >
> > >   // make function call, which returns a char *
> > >   result = doStuff(in, a, b);
> > >
> > >   // save result in Python string
> > >   finalResult = PyString_FromString(result);
> > >
> > >   // free memory
> > >   PyMem_Free(result);
> > >   PyMem_Free(a);
> > >   PyMem_Free(b);
> > >
> > >   // return the result as a Python string
> > >   return finalResult;
> > > }
> > >
> > > ...from python I can call this function 4 times...works fine.  WHen I
> > > call it for the fifth time python.exe crashes.  im thinking some memory
> > > problem in the wrapper function perhaps...but I am not sure.  The
> > > actually C function, doStuff can be called 5, 6,7...N times without a
> > > problem
> > > so i know its gotta be my wrapper.
> > > 
> > > Any ideas?  Thanks!
> 

I think your wrapper should look something like:

static PyObject *wrap_doStuff(PyObject *self, PyObject *args)
{
// this will store the result in a Python object
PyObject *finalResult;

// get arguments from Python
char *result = 0;
char *in= 0;
char *aString = 0;
char *bString = 0;
MY_NUM *a;
MY_NUM *b;

int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
if (!ok) return 0;

// do work to get a and b
// count - returns an int;  GetVal - returns a char *
a = GetVal(aString, count(aString, ","));
b = GetVal(bString, count(bString, ","));

// make function call, which returns a char *
result = doStuff(in, a, b);

// save result in Python string
finalResult = PyString_FromString(result);

// free memory
free(result);
free(a);
free(b);

// return the result as a Python string
return finalResult;
}

You must match malloc() with free(), and PyMem_Malloc() with 
PyMem_Free().  Malloc() and free() usually crash /after/ the call that 
did the damage.  You may wish to avail yourself of your platform's 
malloc debugging facilities.

Note that I don't do this stuff in C, I do it in pyrex, and I'm new to 
it anyway, so there may still be something wrong.  Unless you are 
determined to learn how to do this in C, I think you should switch to 
pyrex.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to make this code faster

2005-10-14 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

> def f(x,y):
> return math.sin(x*y) + 8 * x
> I have code like this:
> 
> def main():
> n = 2000
> a = zeros((n,n), Float)
> xcoor = arange(0,1,1/float(n))
> ycoor = arange(0,1,1/float(n))
> 
> 
> for i in range(n):
> for j in range(n):
> a[i,j] = f(xcoor[i], ycoor[j])  # f(x,y) = sin(x*y) + 8*x
> 
> print a[1000,1000]
> pass
> 
> if __name__ == '__main__':
> main()

I would guess that you are spending most of your time calculating 
sin(x*y).  To find out, just replace f(x,y) with 1, which will produce 
wrong results really fast, and see what that does to your execution time.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can module access global from __main__?

2005-10-14 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Neal  Norwitz" <[EMAIL PROTECTED]> wrote:

> Steve Holden wrote:
> > Neal Becker wrote:
> > >
> > > Still curious about the answer.  If I know that I am imported from 
> > > __main__,
> > > then I can do access X as sys.modules[__main__].X.  In general, I don't
> > > know how to determine who is importing me.
> > >
> > I don't think you can without huge amounts of introspection - it's even
> > worse than the "what's the name of this object" question that seems to
> > come up regularly.
> 
> import sys
> 
> frame = sys._getframe()
> caller = frame.f_back
> print 'Called from', caller.f_code.co_filename, caller.f_lineno
> # for more info, look into the traceback module
> 
> > A module can be imported from multiple modules, and
> > you only get to execute code on the first import.
> > Even then (on the first import) I am not sure how you could introspect
> > to find the answer you want.
> 
> You can install your own __import__() hook to catch all imports.
> 
> Just because you can do something, it doesn't follow that you should do
> it, especially in this case.  Unless you really, really need these
> tricks, they shouldn't be used.

Neal, I have a similar question (in that maybe I shouldn't do 
something), and you seem to know your way around modules, so I'll just 
butt in.  If this isn't OK I'll post as a new thread.

I have written a python module that uses some C functions.  I wrote the 
module in two parts, one python, one pyrex (C).  They need to share some 
globals.  (I use pyrex to handle ref counting.  I think I'm glad I did.)

At first they just sort of mutually imported each other, and it worked 
until I put tests in the python one and set it up to run them when it 
was __main__.  This reminded me that there are also other ways modules 
can be imported under different names, so I tried a different approach.

Now I'm just shoving references to the python module's globals into the 
pyrex module (first defining them in the pyrex module).  The pyrex 
module doesn't import the python module anymore.  This also works, even 
when the python module has a different name ("__main__").  I just feel 
dirty about it.

How does one normally make a module that uses some functions in C?  Will 
that approach work with pyrex?  With globals?

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] Speed up Charmap codecs with fastcharmap module

2005-10-16 Thread Tony Nelson
Fastcharmap is a python extension module that speeds up Charmap codecs 
by about 5 times.

 

Usage:

import fastcharmap
fastcharmap.hook('codec_name')

Fastcharmap will then speed up calls that use that codec, such as 
unicode(str, 'codec_name') and str.encode('codec_name'), and won't 
interfere with Charmap codecs that haven't been hooked.

Documentation is in PyDoc form:

import fastcharmap
help(fastcharmap)

Fastcharmap is available as a standard Python source tarball, binary 
tarball, and RPMs.  It isn't packaged for MSWindows yet (maybe soon), or 
for MOSX.  It is written in Python and Pyrex 0.9.3, but builds from .c 
source when Pyrex is not available.  A C compiler is required for source 
installs.  I have only used it with Python 2.3.4 on FC3 Linux, but it 
should work on Python 2.4 and on other platforms.

As fastcharmap is an extension module, it might not be available on a 
particular computer.  I handle that this way in a program of mine:

try:
import fastcharmap
except ImportError:
print "fastcharmap not available"
else:
fastcharmap.hook('mac_roman')

This is done on document open, which on Gnome / GTK is chatty anyway.

I wrote fastcharmap when I found that decoding a large amount of text 
was taking 3 times as long as loading the document from a file.  Python 
should be fast!  The application is a simple card-file program that can 
also open mbox files as cards.  I am using a 50 MB test file from a Mac 
that loads in about 4 seconds on my computer (wow!), and was decoding in 
about 13 seconds.  Now it takes 2 seconds to decode or encode.

Python developers are working on faster Charmap codecs for a future 
version of Python.  Fastcharmap may be useful until then, and shouldn't 
cause any problems when the new codecs are available.

As this is my first Python module, I'd like some experienced module 
authors and packagers to comment on it, before I make it into cheese.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do you draw this layout with wxpython?

2005-10-17 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Young H. Rhiu" <[EMAIL PROTECTED]> wrote:

> See: http://hilug.org/img/app_layout.GIF
> 
> I'm implementing an album-like application with wxpython but I'm new to
> wxPython though I know how to program with python. The problem is that
> it's not easy for me to deal with drawing layout stuff with wxpython.
> What layout I'm thinking of looks like the gif image above.
> 
> The application is about saving some comments for each pictures on the
> filesystem. There are two windows. The above one is a window for
> loading some images and the below one is a TextCtrl for writing
> comments and if img1, for instance, is clicked, the first notebook(?)
> folder should be active.
> 
> Below is the program I tried but I don't know how to attach a notebook
> window under the image window.
> Any help is greatly appreciated.
> 
> Thanks in Advance, Rhiu
> 
> My Code:
> from wxPython.wx import *
> 
> class smallAppFrame(wxFrame):
> def __init__(self, parent, id, title):
> wxFrame.__init__(self, parent = None, id = -1,
>  title = "MySmallApp", pos = wxPoint(200, 200),
>  size = wxSize(460, 200), style =
> wxDEFAULT_FRAME_STYLE)
> 
> self.bitmap01 = wxStaticBitmap(self, -1,
> wxEmptyBitmap(100,100))
> self.bitmap02 = wxStaticBitmap(self, -1,
> wxEmptyBitmap(100,100))
> self.bitmap03 = wxStaticBitmap(self, -1,
> wxEmptyBitmap(100,100))
> self.bitmap04 = wxStaticBitmap(self, -1,
> wxEmptyBitmap(100,100))
> 
> box = wxBoxSizer(wxHORIZONTAL)
> 
> box.Add((10,10))
> box.Add(self.bitmap01, 0, wxALIGN_CENTER)
> box.Add((10,10))
> box.Add(self.bitmap02, 0, wxALIGN_CENTER)
> box.Add((10,10))
> box.Add(self.bitmap03, 0, wxALIGN_CENTER)
> box.Add((10,10))
> box.Add(self.bitmap04, 0, wxALIGN_CENTER)
> box.Add((10,10))
> 
> self.SetAutoLayout(True)
> self.SetSizer(box)
> 
> class MySmallApp(wxApp):
> def OnInit(self):
> frame = smallAppFrame(None, -1, "MySmallApp")
> frame.Show(true)
> self.SetTopWindow(frame)
> return true
> 
> app = MySmallApp(0)
> app.MainLoop()

I've never used (or learned) wxWidgets, and I'm even new to GTK, but I 
think you just need a deeper structure.  SmallAppFrame() would contain 
two subwindows, probably using something like wxBoxSizer(wxVERTICAL).  
The top subwindow would contain what you've done now, and the bottom one 
would have the rest of your stuff.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Stripping ASCII codes when parsing

2005-10-17 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 David Pratt <[EMAIL PROTECTED]> wrote:

> I am working with a text format that advises to strip any ascii control 
> characters (0 - 30) as part of parsing data and also the ascii pipe 
> character (124) from the data. I think many of these characters are 
> from a different time. Since I have never seen most of these characters 
> in text I am not sure how these first 30 control characters are all 
> represented (other than say tab (\t), newline(\n), line return(\r) ) so 
> what should I do to remove these characters if they are ever 
> encountered. Many thanks.

Most of those characters are hard to see.

Represent arbitrary characters in a string in hex: "\x00\x01\x02" or 
with chr(n).

If you just want to remove some characters, look into "".translate().  

nullxlate = "".join([chr(n) for n in xrange(256)])
delchars = nullxlate[:31] + chr(124)
outputstr = inputstr.translate(nullxlate, delchars)

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Mutual module imports

2005-10-17 Thread Tony Nelson
How does one normally make a Python extension module that has some parts 
in Python and some functions in C share globals between the Python and C 
functions?  Will that approach work with Pyrex?

I have written a Python module that uses some C functions.  I wrote the 
module in two parts, one Python, one Pyrex (C).  They need to share some 
globals.  (I use pyrex to handle ref counting.  I think I'm glad I did.)

At first they just sort of mutually imported each other, and it worked 
until I put tests in the Python one and set it up to run them when it is 
named "__main__".  What happened reminded me that there are also other 
ways modules can be imported under different names, so I tried a 
different approach.

Now the Python module imports the Pyrex module and just shoves 
references to its globals into the Pyrex module (the Pyrex module 
defines them as None).  The Pyrex module doesn't import the Python 
module anymore.  This also works, even when the Python module has a 
different name (e.g. "__main__").  I just feel dirty about it.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Stripping ASCII codes when parsing

2005-10-17 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 David Pratt <[EMAIL PROTECTED]> wrote:

> This is very nice :-)  Thank you Tony.  I think this will be the way to  
> go.  My concern ATM is where it will be best to unicode. The data after  
> this will go into dict and a few processes and into database. Because  
> input source if not explicit encoding, I will have to assume ISO-8859-1  
> I believe but could well be cp1252 for most part ( because it says no  
> ASCII (0-30) but alright ASCII chars 128-254) and because most are  
> Windows users.  Am thinking to unicode after stripping these characters  
> and validating text, then unicoding (utf-8) so it is unicode in dict.  
> Then when I perform these other processes it should be uniform and then  
> it will go into database as unicode.  I think this should be ok.

Definitely "".translate() then unicode().  See the docs for 
"".translate().  As far as charset, well, if you can't know in advance 
you'll want to have some way to configure it for when it's wrong.  Also, 
maybe 255 is not allowed and should be checked for?

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How best to reference parameters.

2005-10-25 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "David Poundall" <[EMAIL PROTECTED]> wrote:

> I am writing a scada package that has a significant amount of user
> defined parameters stored in text files that I wish to cleanly access
> in code.  By way of an example, a few lines from the configuration file
> would typically be ...
> 
> [Plant Outputs]
> Y0P1  Pump 1 Pressure
> Y1P2  Pump 2 Fluid Transfer Pump
> Y2P3  Pump 3 Vac Pump
> Y3P4  Pump 4 Vac Pump
> Y4P5  Pump 5 / Pump 1B
> Y5P6  Pump 6 / Pump 2B
> Y6M
> Y7D
> Y10   E
> Y11   F
> 
> I can read these values in as dictionary items and refernce them in
> code like this...
> 
> Y['P4'] = 1   # Which will ultimately switch my pump on
> Y['P3'] = 0   # Which will ultimately switch my pump off
> 
> but I would much rather reference the plant outputs like this ...
> 
> Y.P4 = 1
> Y.P3 = 0
 ...

d = {'a':1, 'b':2, 'c':3}

class foo:
def __init__(self, d):
self.__dict__.update(d)

f = foo(d)

print f.a, f.b, f.c

(retyped from memory)

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Any Pythonic GTK Undo library?

2005-10-29 Thread Tony Nelson
I'm looking for a "pythonic" GTK Undo library/class.  It would have a 
framework for Undo/Redo, and would provide Undo/Redo for TextView, 
Entry, and containers and other classes.  In a "batteries included" 
fashion, just instantiating a "UndoableTextView" or "UndoableEntry" or 
"UndoableContainer" would provide Undo and Redo in the right-click menu; 
simply connecting such an object to an "UndoableUIManager" would take 
care of the stock items in the menus and toolbar; and there would be a 
simple connection to some sort of "UndoableDocument" interface or mix-in 
for more global sequencing of Undo/Redo.  Does something like this exist 
for Python or GTK?  Googling didn't turn up anything useful.

I'm disappointed that GTK doesn't do this already.  Making my own seems 
doable, but a fair amount of work.  If there isn't some such thing 
already, is there interest in using one that I make?

I know about GUndo, which doesn't implement any actual undo operations; 
the actual operations would need to be coded for TextView and Entry and 
anything else.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any Pythonic GTK Undo library?

2005-10-29 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 Dave Cook <[EMAIL PROTECTED]> wrote:

> On 2005-10-29, Tony Nelson <[EMAIL PROTECTED]> wrote:
> 
> > I'm looking for a "pythonic" GTK Undo library/class.  It would have a 
> 
> You might ask the authors of Kiwi if they plan to add undo/redo.  Or help
> them add it if you can.
> 
> http://www.async.com.br/projects/kiwi/
> 

Well, after I implement it myself, if I do, I could give them the code 
to port to kiwi.


> It would be great to have this feature in the Gtk C API, though.  I do see
> some relevant bugzilla entries:
> 
> http://bugzilla.gnome.org/show_bug.cgi?id=316551
> 
> You might want to make a new request for a general undo/redo interface.

Well, after I implement it myself, if I do, I could give them the code 
to translate to C.

TonyN.:'[EMAIL PROTECTED]
  '  <http://www.georgeanelson.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-30 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] wrote:

> Steve Holden wrote:
> > Indeed, but reading one byte at a time is about the slowest way to
> > process a file, in Python or any other language, because it fails to
> > amortize the overhead cost of function calls over many characters.
> >
> > Buffering wasn't invented because early programmers had nothing better
> > to occupy their minds, remember :-)
> 
> Buffer, and then read one byte at a time from the buffer.

Have you mesured it?

#!/usr/bin/python
'''Time some file scanning.
'''

import sys, time

f = open(sys.argv[1])
t = time.time()
while True:
b = f.read(256*1024)
if not b:
break
print 'initial read', time.time() - t
f.close()

f = open(sys.argv[1])
t = time.time()
while True:
b = f.read(256*1024)
if not b:
break
print 'second read', time.time() - t
f.close()

if 1:
f = open(sys.argv[1])
t = time.time()
while True:
b = f.read(256*1024)
if not b:
break
for c in b:
pass
print 'third chars', time.time() - t
f.close()

f = open(sys.argv[1])
t = time.time()
n = 0
srch = '\x00\x00\x01\x00'
laplen = len(srch)-1
lap = ''
while True:
b = f.read(256*1024)
if not b:
break
n += (lap+b[:laplen]).count(srch)
n += b.count(srch)
lap = b[-laplen:]
print 'fourth scan', time.time() - t, n
f.close()


On my (old) system, with a 512 MB file so it won't all buffer, the 
second time I get:

initial read 14.513395071
second read 14.8771388531
third chars 178.250257969
fourth scan 26.1602909565 1

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NTFS reparse points

2005-11-03 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 Stanislaw Findeisen <[EMAIL PROTECTED]> wrote:
 ...
> However I can't see FILE_ATTRIBUTE_REPARSE_POINT turned on in any file / 
> directory shortcuts I create. In fact the only attribute set in 
> shortcuts created using Windows Explorer is FILE_ATTRIBUTE_ARCHIVE. (I 
> am using GetFileAttributes() to examine this.)
 ...

Shortcuts are files with a .lnk extention.  Reparse points are NTFS 
directory data.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-04 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Tieche Bruce A MSgt USMTM/AFD" <[EMAIL PROTECTED]> 
 wrote:

> I am new to python,

> Could someone explain (in English) how and when to use self?

> I have been reading, and haven't found a good example/explanation

 is a good explanation of just about all of 
Python.  You should read it.  It explains when to use "self".

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ? MDI depreciated

2005-11-06 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "LenS" <[EMAIL PROTECTED]> wrote:

> Hate to ask this dum question (since I've been hiding under a rock).
> But if the MDI UI model is/was depreciated.  What is the new UI model.
> 
> Would love some links that explain in gerneral and specific terms.

In article <[EMAIL PROTECTED]>,
 "Brendan" <[EMAIL PROTECTED]> wrote:

> This is probably a question better suited for a wxPython or MSDN
> newsgroup.  What OS are you referring to?  What GUI toolkit are you
> using?
> 
> Microsoft's office on Windows has moved to a model where every document
> has its own toolbar, menubar, and taskbar entry.  Windows developers
> tend to mimic MS Office, so many are also moving to this model. Mac
> apps have never had MDI.

MS also uses a "Tabbed" version of MDI where only one document at a time 
is visible.  Sometimes this is /implemented/ using the MDI APIs (and 
they also have some MDI apps that don't use the APIs; go figure).  Gnome 
uses Tabbed windows as well; see Gedit, which opens documents in tabs, 
though they can be dragged out into their own windows.  In GTK, at 
least, the Tabbed interface is easily done as a Notebook.

MacOS apps used MDI from the beginning of Multifinder; it just worked 
better than on MSWindows because instead of a grey background you got to 
see the rest of the desktop and the other apps.  On MacOS, MDI was 
referred to as "Layers".  If on MSWindows MDI windows were always 
maximized, had no grey background, and hid the MDI Frame when not in 
front, they would be almost exactly what MacOS did.

MOSX, being a version of NextOS and NextStep, has the more "advanced" 
no-layer, no-MDI UI, and Apple recommends that each app should have only 
one window.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Validate string as UTF-8?

2005-11-06 Thread Tony Nelson
I'd like to have a fast way to validate large amounts of string data as 
being UTF-8.

I don't see a fast way to do it in Python, though:

unicode(s,'utf-8').encode('utf-8)

seems to notice at least some of the time (the unicode() part works but 
the encode() part bombs).  I don't consider a RE based solution to be 
fast.  GLib provides a routine to do this, and I am using GTK so it's 
included in there somewhere, but I don't see a way to call GLib 
routines.  I don't want to write another extension module.

Is there a (fast) Python function to validate UTF-8 data?

Is there some other fast way to validate UTF-8 data?

Is there a general way to call GLib functions?

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Validate string as UTF-8?

2005-11-06 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 david mugnai <[EMAIL PROTECTED]> wrote:

> On Sun, 06 Nov 2005 18:58:50 +0000, Tony Nelson wrote:
> 
> [snip]
> 
> > Is there a general way to call GLib functions?
> 
> ctypes?
> http://starship.python.net/crew/theller/ctypes/

Umm.  Might be easier to write an extension module.

TonyN.:'[EMAIL PROTECTED]
  '  <http://www.georgeanelson.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Validate string as UTF-8?

2005-11-06 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Fredrik Lundh" <[EMAIL PROTECTED]> wrote:

> Tony Nelson wrote:
> 
> > I'd like to have a fast way to validate large amounts of string data as
> > being UTF-8.
> 
> define "validate".

All data conforms to the UTF-8 encoding format.  I can stand if someone 
has made data that impersonates UTF-8 that isn't really Unicode.


> > I don't see a fast way to do it in Python, though:
> >
> > unicode(s,'utf-8').encode('utf-8)
> 
> if "validate" means "make sure the byte stream doesn't use invalid
> sequences", a plain
> 
> unicode(s, "utf-8")
> 
> should be sufficient.

You are correct.  I misunderstood what was happening in my code.  I 
apologise for wasting bandwidth and your time (and I wasted my own time 
as well).

Indeed, unicode(s, 'utf-8') will catch the problem and is fast enough 
for my purpose, adding about 25% to the time to load a file.

TonyN.:'[EMAIL PROTECTED]
  '  <http://www.georgeanelson.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: modifying small chunks from long string

2005-11-14 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "MackS" <[EMAIL PROTECTED]> wrote:

> Hello everyone
> 
> I am faced with the following problem. For the first time I've asked
> myself "might this actually be easier to code in C rather than in
> python?", and I am not looking at device drivers. : )
> 
> This program is meant to process relatively long strings (10-20 MB) by
> selectively modifying small chunks one at a time. Eg, it locates
> approx. 1000-2000 characters and modifies them. Currently I was doing
> this using a string object but it is getting very slow: although I only
> modify a tiny bit of the string at a time, a new entire string gets
> created whenever I "merge" it with the rest. Eg,
> 
> shortstr = longstr[beg:end]
> 
> # edit shortstr...
> 
> longstr = longstr[:beg] + shortstr + longstr[end:] # new huge string is
> created!!
> 
> Can I get over this performance problem without reimplementing the
> whole thing using a barebones list object? I though I was being "smart"
> by avoiding editing the long list, but then it struck me that I am
> creating a second object of the same size when I put the modified
> shorter string in place...

A couple of minutes experimenting with array.array at the python command 
line indicates that it will work fine for you.  Quite snappy on a 16 MB 
array, including a slice assignment of 1 KB near the beginning.  
Array.array is probably better than lists for speed, and uses less 
memory.  It is the way to go if you are going to be randomly editing all 
over the place but don't need to convert to string often.

MutableString warns that it is very slow.  It seems to work by having a 
string data item that it keeps replacing.  I didn't try it.


> shortstr = longstr[beg:end]
> 
> # edit shortstr...
> 
> longstr = longstr[:beg] + shortstr + longstr[end:] # new huge string is
> created!!

Replace this with slice assignment:

longarray = array.array('c',longstr) # once only at beginning!

shortstring = longarray[beg:end].tostring() # or just edit an array

# edit shortstring (or shortarray)

longarray[beg:end] = array.array('c',shortstr)

longstring = longarray.tostring() # if needed

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Library Reference - question

2005-11-17 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] wrote:

> The "Python LIbrary Reference" at
> http://docs.python.org/lib/contents.html seems to be an important
> document. I have two questions
> 
> Q1. How do you search inside "Python LibraryReference" ? Does it exist
> in pdf or chm form?
 ...

I use Google:

site:docs.python.org/lib foo bar

Note that Google is pretty flexible about what is a "site".  
"docs.python.org/lib", "docs.python.org", and "python.org" are all 
"sites", so you can zoom in and out as needed.

The other suggestions look good, too.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


os.path.expanduser('~/foo') and MSWindows "My Documents"

2005-11-17 Thread Tony Nelson
On *nix, ~/foo refers to a file in a user's home directory.  On 
MSWindows, users normally look at "My Documents" in their home 
directory.  ISTM that a file that my program would put in ~/. on Linux 
should be put in "~/My Documents/" (modulo os.path.normpath()) on 
MSWindows, where a user would expect it.  How do you guys cope with this 
issue?

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Hot to split string literals that will across two or more lines ?

2005-11-17 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Sam Pointon" <[EMAIL PROTECTED]> wrote:

> > print "a string which is very loo" \
> > + "ong."
> 
> Minor pedantry, but the plus sign is redundant. Python automatically
> concatenates string literals on the same logical line separated by only
> whitespace.
> 

While we're at it, I use bracketing instead of line continuation:

print ( "a long string, longer than this "
"and some more of the string" )

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.path.expanduser('~/foo') and MSWindows "My Documents"

2005-11-17 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] wrote:

> Try this:
> 
> 
> from win32com.shell import shell, shellcon
> HOMEDIR = shell.SHGetSpecialFolderPath(0, shellcon.CSIDL_PERSONAL)
> myfile_location = os.path.join(HOMEDIR, myfile_name)
> 
> Define a HOMEDIR for your various platforms (use sys.platform to figure
> out what choice to make) and the rest of your code should not need to
> care where "home" is...

Sounds good, thanks.  I'll read up on it all tomorrow.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.path.expanduser('~/foo') and MSWindows "My Documents"

2005-11-18 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] wrote:

> python
> 
> >>> import os
> >>> help(os.path.expanduser)
> >>> import platform
> >>> platform.system()
> 'Windows'
> >>> platform.release()
> 'XP'
> >>>

platform looks good.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using gettext to provide different language-version of a script

2005-11-22 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Thomas W" <[EMAIL PROTECTED]> wrote:

> I'm trying to wrap my head around the docs at python.org related to the
> gettext-module, but I'm having some problem getting it to work. Is
> there any really simple, step-by-step on how to use this module
> available?
> 
> This is my script so far :
> 
> import gettext
> gettext.install('test2', '.', unicode=1)
> lang1 = gettext.translation('test2', languages=['no'])
> print  _('writing a log to file')

I do:

# assume the script is named "myscript.py":
us = os.path.splitext(os.path.basename(sys.argv[0]))[0]
usdir = os.path.dirname(sys.argv[0])

import gettext
gettext.install(us, usdir)


> in the folder where the test2.py-script lives I've created a
> folder-structure like
> 
> ./locales/NO/LC_MESSAGES/messages.mo

Mine looks like:

./en_PL/LC_MESSAGES/myscript.mo


> the messages.mo-file I've created using the scripts in the
> \Tools\i18l\-folder by running :
> 
> python pygettext.py test2.py
> 
> and renaming the generated messages.pot-file to messages.po, and
> editing it to look like :
> 
> # SOME DESCRIPTIVE TITLE.
> # Copyright (C) YEAR ORGANIZATION
> # FIRST AUTHOR <[EMAIL PROTECTED]>, YEAR.
> #
> msgid ""
> msgstr ""
> "Project-Id-Version: PACKAGE VERSION\n"
> "POT-Creation-Date: 2005-11-22 13:02+W. Europe Standard Time\n"
> "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
> "Last-Translator: FULL NAME <[EMAIL PROTECTED]>\n"
> "Language-Team: LANGUAGE <[EMAIL PROTECTED]>\n"
> "MIME-Version: 1.0\n"
> "Content-Type: text/plain; charset=CHARSET\n"
> "Content-Transfer-Encoding: ENCODING\n"
> "Generated-By: pygettext.py 1.5\n"
 ...

You need to set the "Content-Type: charset" and 
"Content-Transfer-Encoding:".  I use:

"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mixed types and variants

2005-11-23 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] wrote:

 ...
> - Maybe someone here can suggest some other variant type, or some other
> solution.

Pyrex?  Pyrex is mostly like Python with the possibility of C types.  It 
handles mixed types just like Python, and the C code it produces is sort 
of readable.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ncurses' Dark Devilry

2005-11-29 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 Jeremy Moles <[EMAIL PROTECTED]> wrote:

> I'm working on a project using ncurses w/ Python. As an aside, I
> implemented addchstr in the cursesmodule.c file in Python SVN, if anyone
> wants me to try and get that made permanent.
> 
> AT ANY RATE...
> 
> I was wondering--and this is more a general curses question rather than
> a Python one, but I know there are some old-timers here who have made
> curses obey before--is there a way to "repaint" a portion of screen
> without stealing the "cursor?" That is:
> 
> I have a focus "wheel" of sorts that allows the user to do input on
> various wigets and windows and whatnot. However, if I want to quickly
> call addstr somewhere else in the application I have to:
> 
>   1. Store the YX coords of the cursor currently
>   2. Use the cursor in the "current" action
>   3. Restore the old cursor location
> 
> I know there are ways around this as I have seen curses apps that, for
> example, have a clock that updates every second without stealing
> "focus."
> 
> I tried implementing/using addchstr (mentioned above) to no success.
> 
> Any ideas? Is this just the plain wrong place to ask this? :)

I've only tried to read the Python Library Curses docs, but I thought 
that the Window object method addstr() would do what you want.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode speed

2005-11-29 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 David Siroky <[EMAIL PROTECTED]> wrote:

> Hi!
> 
> I need to enlighten myself in Python unicode speed and implementation.
> 
> My platform is AMD [EMAIL PROTECTED] (x86-32), Debian, Python 2.4.
> 
> First a simple example (and time results):
> 
> x = "a"*5000
> real0m0.195s
> user0m0.144s
> sys 0m0.046s
> 
> x = u"a"*5000
> real0m2.477s
> user0m2.119s
> sys 0m0.225s
> 
> So my first question is why creation of a unicode string lasts more then 10x
> longer than non-unicode string?

Your first example uses about 50 MB.  Your second uses about 200 MB, (or 
100 MB if your Python is compiled oddly).  Check the size of Unicode 
chars by:

>>> import sys
>>> hex(sys.maxunicode)

If it says '0x10' each unichar uses 4 bytes; if it says '0x', 
each unichar uses 2 bytes.


> Another situation: speed problem with long strings
> 
> I have a simple function for removing diacritics from a string:
> 
> #!/usr/bin/python2.4
> # -*- coding: UTF-8 -*-
> 
> import unicodedata
> 
> def no_diacritics(line):
> if type(line) != unicode:
> line = unicode(line, 'utf-8')
> 
> line = unicodedata.normalize('NFKD', line)
> 
> output = ''
> for c in line:
> if not unicodedata.combining(c):
> output += c
> return output
> 
> Now the calling sequence (and time results):
> 
> for i in xrange(1):
> x = u"a"*5
> y = no_diacritics(x)
> 
> real0m17.021s
> user0m11.139s
> sys 0m5.116s
> 
> for i in xrange(5):
> x = u"a"*1
> y = no_diacritics(x)
> 
> real0m0.548s
> user0m0.502s
> sys 0m0.004s
> 
> In both cases the total amount of data is equal but when I use shorter strings
> it is much faster. Maybe it has nothing to do with Python unicode but I would
> like to know the reason.

It has to do with how strings (either kind) are implemented.  Strings 
are "immutable", so string concatination is done by making a new string 
that has the concatenated value, ans assigning it to the left-hand-side.  
Often, it is faster (but more memory intensive) to append to a list and 
then at the end do a u''.join(mylist).  See GvR's essay on optimization 
at .

Alternatively, you could use array.array from the Python Library (it's 
easy) to get something "just as good as" mutable strings.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: wxPython : getting started

2005-11-29 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 David Sulc <[EMAIL PROTECTED]> wrote:

> Hi !
> 
> I've looked all over (internet, books, etc.) and I haven't found a very 
> good ressource to get started with wxPython (yes, I've been through 
> their tutorial).
> 
> What I would basically like to do for starters is to be able to define 
> the main panel being displayed. For example :
> 1. wxFrame contains a wxPanel  (call it mainPanel).
> 2. mainPanel contains another panel (childPanelA)
> 3. another panel has been defined (childPanelB) but is not displayed 
> (the user can only see childPanelA inside mainPanel)
> 4. by clicking on a menu entry (for example), the displayed panel is now 
> childPanelA (which is inside mainPanel)
> 
> So how do I do this ? I realize it's a very basic question, but it's 
> been driving me mad...
 ...

I don't know or use wxWidgets, and I've just learned GTK, but I think 
one good answer is the same as with GTK:  use a wxNotebook.  You may be 
able to hide the tabs, or you may just decide they're a good thing to 
have.

To do what you asked, see the wxWindow method Show(), which you would 
call for each or A and B etc. in response to the command.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Pydoc: restrict base class doc?

2005-11-30 Thread Tony Nelson
I'd like to prevent Pydoc from adding base class documentation for some 
of my classes.  Specifically, I have a couple of classes that derive 
from GTK widgets, and dumping all that documentation in doesn't have 
much benefit.  Is there some thing I can do in my source, or to Pydoc, 
to tell it to skip some base classes?

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


How to keep Pydoc from listing too much?

2005-12-02 Thread Tony Nelson
How can I tell Pydoc not to list information for some of the base 
classes?  For example, when a class inherits from gtk.Widget, lots of 
GTK stuff gets added that doesn't really need to be there.  Is there 
some option to Pydoc to tell it to skip some classes?  Is there 
something I can put in my source that is a hint to Pydoc?

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Dao Language v.0.9.6-beta is release!

2005-12-02 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 Dave Hansen <[EMAIL PROTECTED]> wrote:

> On 2 Dec 2005 10:08:21 -0800 in comp.lang.python, [EMAIL PROTECTED]
> wrote:
> 
> >Here it is again...  Python bypassed/discounted because, of all things,
> >scoping by indentation!?!?
> >
> >This used to surprise me.  Until I hear more and more otherwise
> >reasonable programmers list this as their number one reason for
> >shunning Python.
> >
> >I gauge design defects by how much after market
> >discussion/documentation a feature generates/requires.   Scoping by
> >indentation is a whopper of a defect.
> 
> FWIW, indentation scoping one one of the features that _attracted_ me
> to Python.

Me too.  Or rather, Python code is much more readable because every 
nincompoop has to use indentation correctly whether they want to or not, 
and I like readable code.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python! Is! Truly! Amazing!

2005-01-02 Thread Nelson Minar
"Erik  Bethke" <[EMAIL PROTECTED]> writes:
> I have NEVER experienced this kind of programming joy.

Yep, I feel the same way since learning Python. It's really a
productive and pleasant language.

Congratulations on all your game successes!
-- 
http://mail.python.org/mailman/listinfo/python-list


Help Optimizing Word Search

2005-01-11 Thread Case Nelson
Hi there I've just been playing around with some python code and I've
got a fun little optimization problem I could use some help with.

Basically, the program needs to take in a random list of no more than
10 letters,  and find all possible mutations that match a word in my
dictionary (80k words). However a wildcard letter '?' is also an
acceptable character which increases the worst case time significantly.
So if the letters are ['a','b','c'] check a, b, c, ab, ac, ba, bc, ca,
cb, abc, acb, bac, bca, cab, cba where only a, ba and cab would be
added to the dict of words. If the letters are ['?','?'] check a-z, aa,
ab, ac, ad, ..., az, ba, bb, bc, bd, ..., zz

I'm using a trie structure to load and query my dictionary, which
returns a 1 if the word is found, 4 if a partial word is found, and 3
if there is no possible word.

I guess I'm wondering if anyone could think of a faster way to deal
with the wildcards, perhaps removing the inner for-loop or else
something entirely different (iterative, don't use the trie?).

Any other suggestions would be welcome

findword recursion runs in 9.16300010681 secs
words.log is simply my list of words:
['hats', 'easts', 'baizes',...,'sash']

--- Code Begin ---
PY> import trie
PY> import time
PY>
PY> print 'loading trie ...',
PY> mytrie = trie.Trie('dicts.txt')
PY> print 'done.'
PY>
PY> alpha = list('abcdefghijgklmnopqrstuvwxyz')
PY>
PY> words = {}
PY> def findword(word, letters):
PY> # run out of letters, recursion stop point
PY> if letters == []:
PY> return
PY>
PY> if word != "":
PY> # Check if word is valid
PY> result = mytrie.query(word)
PY>
PY> # Found a word, add it to the database
PY> if result == 1:
PY> words[word] = 1
PY>
PY> # No other word starts with my current word, recursion stop
point
PY> if result == 3:
PY> return
PY>
PY> # Run through every letter in our list
PY> for i,letter in enumerate(letters):
PY> # Remove current letter and recurse
PY> newletters = letters[:]
PY> newletters.pop(i)
PY>
PY> # if current letter is ? must recurse through all 26
letters
PY> if letter == '?':
PY> for c in alpha:
PY> # recurse word+wildcard letter
PY> findword(word+c,newletters)
PY> else:
PY> # recurse word+letter
PY> findword(word+letter,newletters)
PY>
PY> letters = list('abae?s?')
PY> s = time.time()
PY> findword("",letters)
PY> print time.time() - s
PY>
PY> output = open('words.log','w')
PY> print >> output, words.keys()
PY> output.close()
PY>
--- Code End ---
Thanks 
(Hopefully google doesn't eat my whitespace)

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


XPath and XQuery in Python?

2005-01-11 Thread Nelson Minar
Could someone help me get started using XPath or XQuery in Python? I'm
overwhelmed by all the various options and am lacking guidance on what
the simplest way to go is. What library do I need to enable three line
Python programs to extract data with XPath expressions?

I have this problem a lot with Python and XML. Even with Uche's
excellent yearly roundups I have a hard time finding how to do fancy
things with XML in Python. I think it's a bit like web server
frameworks in Python - too many choices.
  http://www.xml.com/pub/a/2004/10/13/py-xml.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XPath and XQuery in Python?

2005-01-14 Thread Nelson Minar
Nelson Minar <[EMAIL PROTECTED]> writes:
> Could someone help me get started using XPath or XQuery in Python?

I figured this out. Thanks for the help, John! Examples below.

I used this exercise as an opportunity to get something off my chest
about XML and Python - it's kind of a mess! More here:
  http://www.nelson.monkey.org/~nelson/weblog/tech/python/xpath.html

Here are my samples, in three libraries:

# PyXML

from xml.dom.ext.reader import Sax2
from xml import xpath
doc = Sax2.FromXmlFile('foo.opml').documentElement
for url in xpath.Evaluate('//@xmlUrl', doc):
  print url.value

# libxml2

import libxml2
doc = libxml2.parseFile('foo.opml')
for url in doc.xpathEval('//@xmlUrl'):
  print url.content

# ElementTree

from elementtree import ElementTree
tree = ElementTree.parse("foo.opml")
for outline in tree.findall("//outline"):
  print outline.get('xmlUrl')

Please see my blog entry for more commentary
  http://www.nelson.monkey.org/~nelson/weblog/tech/python/xpath.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python and SOAP

2005-01-20 Thread Nelson Minar
Peter Schaefer <[EMAIL PROTECTED]> writes:
> Is SOAPy still the way to go, or are there better methods?

SOAPy hasn't been maintained in awhile. The two contemporary options
are ZSI or SOAPpy, both at
  http://pywebsvcs.sourceforge.net/
ZSI seems to have more serious development now, but neither is perfect.

You should consider doing a document/literal service instead of
rpc/encoded, the old way. Unfortunately in my experience doc/lit
doesn't work as well with Python, but it's a better way of doing
things. The ZSI guys are working at it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is print? A function?

2005-01-23 Thread Nelson Minar
Frans Englich <[EMAIL PROTECTED]> writes:
> The reason I thinks about this is I need to implement a debug print for my 
> program; very simple, a function/print statement that conditionally prints 
> its message whether a bool is true. Not overly complex.

As several folks have said, print is a statement, and Python doesn't
really let you add statements to the language.

You said you wanted to build a simple debugging function, so maybe
this doesn't fit the bill, but for bigger pieces of code Python has a
nice logging library. It's something like log4j or java.util.logging.
  http://www.python.org/doc/2.4/lib/module-logging.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where are list methods documented?

2005-02-03 Thread Nelson Minar
You're not the only one with a hard time finding the list
documentation. It's even crazier for string docs.

If you want to see how to strip strings in Python you have to go to
the library docs, then click "sequence types" (betcha don't think of
strings as sequences), then scroll to the bottom, then click "String
Methods", then scroll to find "strip()". This makes perfect sense if
you understand that strings are "mutable sequences". But if you're
coming from (say) Perl and are looking for the quick way to work with
strings, it's pretty deeply buried.

A simple internal link or two would help a lot. Say, something in
"built-in functions" which says "see also..."

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


Re: time.clock() or time.time()

2005-08-03 Thread Nelson Minar
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> I'm trying to benchmark some function calls for Zope project

Other folks have explained time() vs. clock(), so I'll leave that.

But rather than roll your own timer functions, consider using timeit.

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


Re: Getting the word to conventional programmers

2005-03-23 Thread Christopher Nelson
Hey, Jeff Hobbs got the last word. ;-)

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


new to mac OS10

2005-04-14 Thread Thomas Nelson
I'm on a mac OS X (10.3.8), and I seem to have accidentally destroyed 
the default python installation.  How should I put it on?  Do I need to 
use the unix version?  any help would be greatly appreciated.

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


Re: new to mac OS10

2005-04-14 Thread Thomas Nelson
Maurice LING wrote:
I'm using OSX 10.3.8 as well. Just wondering, how did you "destroy" it? 
What I am thinking is, it may not be as destroyed as you think it might 
have...

cheers
maurice
I was actually trying to update to the newest python version, and I had 
read something saying it would conflict with the old version, so I went 
through and deleted all the folders that had "python" in the name =]. 
clever of me, huh?  now I can't make either the new or the old work. 
Once again, any help would be terrific.

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


Re: new to mac OS10

2005-04-15 Thread Thomas Nelson
The main thing I would like is to be able to use tkinter with python on 
my mac.  will the command-line-style source allow this?  Does it come 
with IDLE?  How is the fink version different from the source i can 
download at python.org?  Here's the result of the requested commands on 
my Terminal.  It would appear that these links are broken, as 
Python.framework no longer exists.  Sorry to be so much trouble,

THN

Welcome to Darwin!
~ $ ls -all /usr/bin/python*
lrwxr-xr-x  1 root  wheel9 20 Jun  2004 /usr/bin/python -> python2.3
lrwxr-xr-x  1 root  wheel   72 20 Jun  2004 /usr/bin/python2.3 -> 
../../System/Library/Frameworks/Python.framework/Versions/2.3/bin/python
lrwxr-xr-x  1 root  wheel   10 20 Jun  2004 /usr/bin/pythonw -> pythonw2.3
-rwxr-xr-x  1 root  wheel  122  8 Jun  2003 /usr/bin/pythonw2.3
~ $ ls /System/Library/Framework/Python.framework
ls: /System/Library/Framework/Python.framework: No such file or directory
~ $ cd /System/Library/Framework/Python.framwork
-bash: cd: /System/Library/Framework/Python.framwork: No such file or 
directory
~ $

Jorl Shefner wrote:
If you simply want the latest python to run from the command line, then
compiling the source code works fine on the mac.  I just installed
2.4.1 on Os 10.3.8 last week without any problems.
http://www.python.org/ftp/python/2.4.1/Python-2.4.1.tgz
"./configure"
"make"
"make install"
J.S.
--
http://mail.python.org/mailman/listinfo/python-list


Extension of while syntax

2014-12-11 Thread Nelson Crosby
I was thinking a bit about the following pattern:

value = get_some_value()
while value in undesired_values:
value = get_some_value()

I've always hated code that looks like this. Partly due to the repetition, but 
partly also due to the fact that without being able to immediately recognise 
this pattern, it isn't very readable.

Python already has one-line syntaxes (e.g. list comprehensions), I was 
wondering what people thought about a similar thing with while. It might look 
something like:

value = get_some_value() while value in undesired_values()

Perhaps not this exact syntax though, as the parser might try to do `value = 
(get_some_value() while...)` instead of `(value = get_some_value) while...`.

Other languages have features which allow something to look slightly less like 
this pattern, e.g. Java:

SomeType value;
while ((/* The assignment */ value = getSomeValue()) /* Compare */ == 
undesired_value) {}

Granted, this isn't exactly tidy, but it's a little more DRY and, IMO, 
preferable.

What are other's thoughts on this?
-- 
https://mail.python.org/mailman/listinfo/python-list


Free vs proprietary (was Re: NumPy, SciPy, & Python 3X Installation/compatibility issues)

2014-05-10 Thread Nelson Crosby
I also believe in this more 'BSD-like' view, but from a business point of view. 
No one is going to invest in a business that can't guarantee against piracy, 
and such a business is much less likely to receive profit (see Ardour).

Don't get me wrong - I love free software. It's seriously awesome to she what a 
community can do. But at the same time, some people want to earn a living from 
writing code. That is simply not possible without proprietary software. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OO in Python? ^^

2005-12-10 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 Matthias Kaeppler <[EMAIL PROTECTED]> wrote:
 ...
> obj = Base() # I want a base class reference which is polymorphic

obj now refers to an instance of Base.

> if ():
> obj =  D1()

obj now refers to an instance of D1().  The Base instance is 
unreferenced.

> else:
> obj = D2()

obj now refers to an instance of D2().  The Base instance is 
unreferenced.

Note that there is no code path that results in obj still referring to 
an instance of Base.  Unless making a Base had side effects, there is no 
use in the first line.


> I could as well leave the whole inheritance stuff out and the program 
> would still work (?).

That program might.


> Please give me hope that Python is still worth learning :-/

Python has inheritance and polymorphism, implemented via dictionaries.  
Python's various types of namespace are implemented with dictionaries.

Type this in to the Python interpreter:

class Base:
def foo(self):
print 'in Base.foo'

class D1(Base):
def foo(self):
print 'in D1.foo'
Base.foo(self)

class D2(Base):
def foo(self):
print 'in D2.foo'
Base.foo(self)

def makeObj():
return needD1 and D1() or D2()

needD1 = True
makeObj().foo()

needD1 = False
makeObj().foo()

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Wingide is a beautiful application

2005-12-19 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
 ...
> I get the feeling that a ot of people working with heavy IDEs don't
> realize how capable vim/emacs are, so I'll give a brief rundown of what
> my Vim environment does for me.  (I do Python web development)--if you
> don't like the Vi keybindings, the Cream package is Vim that behaves
> like a regular modeless editor but with all of vim's power (and a nice
> embedded Python interpreter for writing extensions):
> 
> 1. Python syntax checking: as I'm typing along, if I input a syntax
> error then the line is immediately highlighted in red.  Useful for
> catching brainos like:
> if a=1:
> (which will highlight in red when I hit enter, point out that I need ==
> instead of =).

What do you use to do this?  Cream doesn't seem to do this oob.


> 2. Normal tag-jump stuff: Ctrl-click on a function/method call (or
> class or whatever) will jump to the function/method/class definition
> (Ctrl-T works as well if you don't like clicking).  It keeps a stack of
> visited files so you can drill down through your call stack and then
> pop back up to where you came from.

Do you set up ctags for this?  I get error messages "E433: No tags file" 
and "E426: tag not found: xxx" when I Ctrl-click on a method call.


> 3. Python class browsing stuff: A Class menu shows the parent and child
> classes of the one you're currently in, and all the methods of the
> current class; selecting any of the above jumps to the appropriate file
> and line.

Is this the Tag List?


> 4. Interactive documentation stuff: When I type an open-paren, it looks
> to see what the prior keyword is and displays help for it in the status
> line (preferring Python documentation, then docstrings, then comments
> before the function/method/class definition).  Even if there's no
> help/comments, it'll show the arguments that the function takes.  So
> if, say, I type:
> 
> cmp(
> 
> then the status line displays:
> 
> cmp(x, y) Compare the two objects X and Y and return an integer
> according to ...
> 
> If I hit F1 it'll show the full help text.  Often the arguments are
> enough, and I find the status-line display a lot less intrusive than
> many on-the-fly help systems I've seen.

This stuff doesn't happen either.  How is it set up?


> 5. A client menu selects which client I want to work in (so, say, I get
> a bug report for Client A, I select them from the menu).  The Class
> menu and other functions respect this (if I'm in the generic Company
> class, the Class menu will list Client A's Company subclass before the
> subclasses of other companies; if I jump to the Company definition,
> it'll go to Company A's client-specific version).  It also restarts
> development httpd servers on the current machine running with conf
> files appropriate to that client.
 ...

Where is this "client menu"?  How is it set up?

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Wingide is a beautiful application

2005-12-21 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

> Tony Nelson wrote:
> > > 1. Python syntax checking: as I'm typing along, if I input a syntax
> > > error then the line is immediately highlighted in red.
> >
> > What do you use to do this?  Cream doesn't seem to do this oob.
> 
> Nope.  I'll try to package up my vimrc and get it uploaded somewhere
> next week (busy with holiday stuff).

OK, thanks.

> The meat of it is:
> 
> import vim
> def cur_x():
> return vim.current.window.cursor[1]
> def cur_y():
> return vim.current.window.cursor[0]
> def find_current_block():
> block = [vim.current.line]
> current = cur_y()-1
> while len(block[0])==0 or block[0][0] in " \t#":
> current = current - 1
> if current < 0:
> break
> block = [vim.current.buffer[current] ]+ block
> return block
> def check_current_block():
> import code
> vim.command("syn clear Error")
> block = find_current_block()
> length = len(block)
> try:
> code.compile_command("\n".join(block))
> print ""
> return 0
> except:
> (type, value, tb) = sys.exc_info()
> line_no = cur_y()-1+value.lineno-length
> badline = vim.current.buffer[line_no]
> badline = badline.replace('"', '\\"')
> print "Error at line %d: " %(line_no+1), badline
> return 1

OK, I can tell that this is Python code, not VI script stuff.  I'll need 
to see how your vimrc sets this up.  It looks like learning to use VI 
(even with Cream) would take a few weeks of hard work.


> > > 2. Normal tag-jump stuff: Ctrl-click on a function/method call (or
> > > class or whatever) will jump to the function/method/class definition
> > > (Ctrl-T works as well if you don't like clicking).  It keeps a stack of
> > > visited files so you can drill down through your call stack and then
> > > pop back up to where you came from.
> >
> > Do you set up ctags for this?  I get error messages "E433: No tags file"
> > and "E426: tag not found: xxx" when I Ctrl-click on a method call.
> 
> Umm, either click the "build tags in current directory" button on the
> toolbar, or "find . -name '*.py' | ctags -L -" in the top directory of
> your python file.

OK.


> > > 3. Python class browsing stuff: A Class menu shows the parent and child
> > > classes of the one you're currently in, and all the methods of the
> > > current class; selecting any of the above jumps to the appropriate file
> > > and line.
> >
> > Is this the Tag List?
> 
> No, this is more complex, I'll post it w/ the rest of my stuff.
 ...

Thanks.

TonyN.:'[EMAIL PROTECTED]
  '  <http://www.georgeanelson.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Wingide is a beautiful application

2005-12-21 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

> Tony Nelson wrote:
> > OK, I can tell that this is Python code, not VI script stuff.  I'll need
> > to see how your vimrc sets this up.
> 
> vim has a Python interpreter embedded in it (assuming it's a reasonably
> complete build--it's possible to leave the interpreter, or even parts
> of the vim scripting stuff, out).

OK, mine has Python in it.


> I just put .py files in my .vim
> directory, import them, and then use ":py myfile.doStuff()" or map keys
> to such commands.

So, you bind check_current_block() to the Enter key?


> The "vim" Python module (included in vim, just "import vim" from your
> Python scripts) lets you run vim commands, access
> windows/buffers/variables/etc.
 ...

OK.  I see Python in the help.

TonyN.:'[EMAIL PROTECTED]
  '  <http://www.georgeanelson.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Wingide is a beautiful application

2005-12-22 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

> Tony Nelson wrote:
> > So, you bind check_current_block() to the Enter key?
> 
> Yeah.  The binding's not quite just "check_current_block()"
> because you need a bit of magic to keep autoindent working.  I'll post
> it with my conf files next week, essentially use = with an empty
> vim function wrapper around the python function (instead of ) and
> input a character before leaving insert mode that's deleted when you
> return (to preserve indent).
> 
> My goal is to make my conf files into a decent drop-in so you just put
> them in your .vim directory and go, and post them next week.

OK, thank you.

TonyN.:'[EMAIL PROTECTED]
  '  <http://www.georgeanelson.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Wingide is a beautiful application

2006-01-05 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

> Tony Nelson wrote:
> >  "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> >
> > > My goal is to make my conf files into a decent drop-in so you just put
> > > them in your .vim directory and go, and post them next week.
> >
> > OK, thank you.
> >
> 
> FYI, I am still working on this but some changes in vim 7 are requiring
> more work than I expected.

I suspected that it might be taking longer than you thought.  No hurry 
from my end.


> On the bright side, there are some nice
> features that will allow for cool stuff like debugger info (after
> running in a debugger, you'll be able to mouse over variables and get
> tooltip popups with their values, etc) when it's done.  I'm talking
> with Bram Moolenaar (vim author) on the vim-dev mailing list to try to
> get things squared away.

Thank you for your efforts.

TonyN.:'[EMAIL PROTECTED]
  '  <http://www.georgeanelson.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list


statistical analysis tools in python?

2006-07-12 Thread Thomas Nelson
Sorry if this is a FAQ, but I couldn't find a good summary through
google.  What kinds of statistical analysis tools exist in python?  I
really just need t-tests, chi-squared test, and other such tests of
statistical significance.  A few things point to numpy and scipy, but I
was surprised to find the documentation for numpy is not freely
available, and I thought it would be wise to ask here before I download
it and start hunting through the source code for what I want.  Is numpy
the best option for my simple needs?  Also I was a little surprised to
find nothing in the builtin python modules that can find standard
deviation, quartiles, etc.  Is this for any particular reason, or
perhaps no one has shown any interest?  I'd be willing to work on a
project to make simple single-variable analysis part of the builtin
python distribution.

Thanks all,

THN

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


Re: execute a shell script from a python script

2006-07-17 Thread Thomas Nelson
If your script is foo.sh and takes args:
import subprocess
subprocess.call(["foo.sh","args"],shell=True)
Should work fine.  check out
http://www.python.org/dev/doc/maint24/lib/module-subprocess.html

Enjoy,
THN

spec wrote:
> Hi all, I know nothing about Python. What I need to do is to get a
> Python script to execute a local shell script. I do not need any
> output. What would be th eeasiest way to accomplish this?
> 
> Thanks!

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


Re: execute a shell script from a python script

2006-07-18 Thread Thomas Nelson
As described in the docs I pointed to before:
subprocess.call("foo.sh",shell=True)
Is the way to do it without args.  I think it is simplest to learn the
subprocess module because (quoting from the docs) this module intends
to replace several other, older modules and functions, such as:
os.system
os.spawn*
os.popen*
popen2.*
commands.*
This way you only need to learn one thing.  Actually I would like to
see some of these older functions deprecated.

THN

Cameron Laird wrote:
> In article <[EMAIL PROTECTED]>,
> Simon Forman <[EMAIL PROTECTED]> wrote:
> >spec wrote:
> >> Thanks, actually there are no args, is there something even simpler?
> >>
> >> Thanks
> >> Frank
> >
> >you could try os.system()
> >
> >>From the docs:
> >
> >system(command)
>   .
>   [more detail]
>   .
>   .
> I'm concerned the follow-ups in this thread have been too subtle.
> Here is what you need to know:  use system().  A model such as
>
>   import os
>   os.system("my_script")
> 
> fulfills exactly the requirements the original poster described.

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


Re: statistical analysis tools in python?

2006-07-18 Thread Thomas Nelson
Actually, after a little looking, the simple stats.py module at
http://www.nmr.mgh.harvard.edu/Neural_Systems_Group/gary/python.html
is exactly what I needed.  It may not be as fast or as comprehensive as
scipy or R, but installation simply involves downloading the module and
importing into the code, and it has the ttests I was looking for.
Thanks,
THN

[EMAIL PROTECTED] wrote:
> And there is a python interface to R, so that you can call R routines from
> Python.  R is a free stat language that has all the tests you've mentioned,
> 
> Gerry

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


Re: looking for a regular expression

2006-08-01 Thread Thomas Nelson
How about
my_string = "We the people of the United States, in order to form a
more perfect union, establish justice, insure domestic
tranquility,.."
print (x for x in my_string.split(",") if "justice" in x).next()

This isn't a regular expression, but it gives what you're looking for.

THN

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


Re: Programming newbie coming from Ruby: a few Python questions

2006-08-03 Thread Thomas Nelson
I strongly recommend trying to come up with your own projects.  Just
pick small things that reflect something you actually want to do: maybe
make a simple board game, or a few scripts to help you keep all your
files organized, etc.  Generally speaking I think it's easier to teach
yourself a language if you have a project you care about finishing.  I
was pleasantly surprised by how easy python makes it to travel from "It
would be cool if I could..." to finished product.  In my own case, I'm
a college student, and I wanted a program that could help me schedule
my next semester's classes without conflicts and with the maximum sleep
possible.  That first "project" gave me a lot of good python
experience, and I know use python all the time for "real life"
applications.

THN

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


Re: What is the best way to print the usage string ?

2006-08-05 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "Simon Forman" <[EMAIL PROTECTED]> wrote:
 ...
> Python also concatenates adjacent strings, but the "real" newlines
> between your strings will need to be escaped (otherwise, because the
> newlines are statement separators, you will have one print statement
> followed by string literals with the wrong indentation.)
> 
> print "Usage: blah blah blah\n" \
>   "Some more lines in the usage text\n" \
>   "Some more lines here too."
> 
> (Note that the final string literal newline is not needed since print
> will add one of it's own.)

One can also use parentheses:

print ( "Usage: blah blah blah\n"
"Some more lines in the usage text\n"
"Some more lines here too." )

The newlines in the string are still needed, but the ones escaping the 
EOLs are not.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: E' possibile integrare ironpython con visual studio 2005?

2006-08-05 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Alex Martelli) wrote:

> LaGuna <[EMAIL PROTECTED]> wrote:
> 
> > Se si come?
> > 
> > Ciao by Enzo
> 
> Questo newsgroup preferisce l'inglese -- per favore, chiedi su
> it.comp.lang.python invece che qui.
> 
> This newsgroup prefers English -- please ask on it.comp.lang.python
> rather than here.

Still, the question is obvious enough.

Microsoft's "Visual Studio 2005 SDK" now contains IronPython 
integration.  I haven't used it, as I have only the free Visual C# 2005 
Express Edition.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Two Classes In Two Files

2006-08-10 Thread Thomas Nelson
Perhaps __init__.py has what you're looking for?

THN


[EMAIL PROTECTED] wrote:
> I just started working with Python and ran into an annoyance. Is there
> a way to avoid having to use the "from xxx import yyy" syntax from
> files in the same directory? I'm sure it's been asked a million times,
> but I can't seem to find the answer.
>
> For example, I have two classes stored in separate files as such.
>
> File: one.py
> 
> class One:
>   def methodA(self):
> print "class One"
>   def methodB(self):
> print "class One"
>
>
> File two.py
> 
> from one import One
>
> class Two(One):
>   def methodA(self):
> print "class Two"
>
> if __name__ == "__main__":
>   x = Two()
>   x.methodA()
>   x.methodB()
>
> When I run the Two.py file, I get the expected output but I'd like to
> eliminate the from line in two.py.

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


Re: Memory problem

2006-08-14 Thread Thomas Nelson

Yi Xing wrote:
> On a related question: how do I initialize a list or an array with a
> pre-specified number of elements, something like
> int p[100] in C? I can do append() for 100 times but this looks silly...
> 
> Thanks.
> 
> Yi Xing

Use [0]*100 for a list.

THN

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


Re: can I import the module twice (under differnet names)

2006-11-01 Thread Thomas Nelson
alf wrote:
> Hi,
>
> wonder if in the python I could treat modules imorts like classes
> instances. It means I could import it twice or more times under
> different names.
>
> --
> alfz1

You can always give any object as many names as you want:
>>> import sys
>>> s1 = sys
>>> s2 = sys
>>> s1.path
['', '/usr/local/bin', '/bin', '/sbin', '/usr/bin', '/usr/sbin',...
>>> s2.argv
['']

Or maybe you're looking for the builtin function reload?
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
...
>>> import this  #note nothing happens
>>> reload(this)  #runs again
The Zen of Python, by Tim Peters

Beautiful is better than ugly.



Neither one of these methods are too common in practice I think.  Could
you tell us why you want to import a module more than once?
-Tom

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


Re: Python in sci/tech applications

2006-11-03 Thread Thomas Nelson
How hard would it be to have numpy/ scipy part of the python standard
library?
Tom


mattf wrote:
> I've discovered Python and have been trying it out lately as a possible
> replacement for computations that would ordinarily be done with a
> commercial package like Matlab or IDL. I'd like to mention a few things
> I've run across that have either surprised me or kept me from doing
> things the way I'd like to.
>
> 1) -There's a large and active sci/tech Python community out there.-
> This was something of a surprise. If you look at the python.org site
> and click down a couple of levels past the front page, there's a rather
> brief mention of scientific and numeric applications-- but I don't
> think this does justice to the current levels of activity and
> accomplishment.
>
> 2) -There's a very impressive set of libraries out there-
> NumPy, SciPy, Enthought. It's really kind of stunning how mature these
> libraries are and how much I had to poke around to figure that out.
>
> 3) -There's a problem with development under Windows.
> A typical task will entail writing a 'pure python' prototype to get the
> 'data in, data out' part of a problem straightened out, then writing a
> module in C to get adequate performance in production runs. But the C
> compiler that my employer provides (the current version of MSVS)
> doesn't produce libraries that work with the current version of Python.
> Ooops. This, in the real world, is a big problem. I -love- Python. And
> I think I could convince other people to use it. But I've got to have a
> way to produce compiled modules painlessly, i.e., without installing a
> new operating system.

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


Re: Py3K idea: why not drop the colon?

2006-11-10 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 Bjoern Schliessmann <[EMAIL PROTECTED]> 
 wrote:

> Michael Hobbs wrote:
> 
> > That is, assume that the expression ends at the colon, not at the
> > newline. That would make this type of statement possible:
> > if color == red or
> >   color == blue or
> >   color == green:
> > return 'primary'
> > Right now, such a statement would have to be spelled thus:
> > if color == red or \
> >   color == blue or \
> >   color == green:
> > return 'primary'
> > or
> > if (color == red or
> >   color == blue or
> >   color == green):
> > return 'primary'
> 
> What about
> 
> if color == red or blue or green:
> return 'primary'

What about

if color in [red, blue, green]:
return 'primary'

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: calling functions style question

2006-06-06 Thread Thomas Nelson
The difference becomes clear when you import your program into another
program (or the command line python editor).  __name__!='__main__' when
you import, so the functions will not be called if they're inside the
block.  This is why you see this block so often at the end of scripts;
so that the script runs its main functions when called as a standalone
program, but you can also import the code and do something with it
without setting off those functions.

THN

Brian wrote:
> I just have a basic style question here.  Suppose you have the program:
>
> def foo1():
> do something
>
> def foo2()
> do something else
>
> Assume that you want to call these functions at execution.  Is it more
> proper to call them directly like:
>
> foo1()
> foo2()
>
> or in an if __name__ == "__main__": ?
>
> Both will execute when the script is called directly, I was just
> wondering if there is a preference, and what the pros and cons to each
> method were.
> 
> Thanks,
> Brian

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


Trace KeyboardInterrupt exception?

2006-06-13 Thread Tony Nelson
I'm trying to find out what is eating some KeyboardInterrupt exceptions 
in a fairly large program (yum).  My KeyboardInterrupt handler is called 
for some Ctl-C presses, but for others nothing seems to happen.  
Grepping the source (what of it I've found, looking at import 
statements) doesn't turn up anything likely.

My thinking is that either some "except:" clause is eating them, or some 
place I haven't looked is eating them, or possibly C code is eating 
them.  For the first two, at least, I'd like to use a debugger to trace 
KeyboardInterrupt exceptions, make sure that they're happening, and see 
what is handling them.  I don't see a way to trace or break on a 
specific exception type in Idle.  Can someone give me a hint on how to 
do this?  I'm willing to consider other debuggers, including gdb (DDD).

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trace KeyboardInterrupt exception?

2006-06-14 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] wrote:

> Tony Nelson wrote:
> > I'm trying to find out what is eating some KeyboardInterrupt exceptions
> > in a fairly large program (yum).  My KeyboardInterrupt handler is called
> > for some Ctl-C presses, but for others nothing seems to happen.
> 
> >  ... I'd like to use a debugger to trace
> > KeyboardInterrupt exceptions, make sure that they're happening, and see
> > what is handling them.
> 
> I don't know how to do that in Idle.  You can replace the default
> Ctrl-C interrupt handler with your own and use that to inspect the
> current stack.

Thanky you, that helps.  Interestingly, some Ctl-Cs don't get caught.  
Presumably they're happening in a subprocess.

Now to see if I can get into that situation again where Ctl-C is 
ignored.  I need to know what's eating the exceptions.  I don't think 
it's a subprocess in the case I'm concerned with.  I don't think yum is 
threaded, but apparantly importing the tread module anywhere should keep 
KeyboardInterrupt on the main thread anyway (?).

It would be nice if I could inspect the stack and find the exception 
handlers.  I'm using trace handlers, but their output seems somewhat 
spotty and inconsistent, or maybe just confusing.

TonyN.:'[EMAIL PROTECTED]
  '  <http://www.georgeanelson.com/>
-- 
http://mail.python.org/mailman/listinfo/python-list


socket and Ctl-C workaround?

2006-06-15 Thread Tony Nelson
I've been trying to figure out why Ctl-C sometimes doesn't interrupt 
yum.  It appears to be unresolved Python bug 926423, unresolved proposed 
patch 1102879, don't know if anything ever came of it.  Note that I 
cannot ask all yum users to apply the patch.  I'm not sure I should be 
getting rid of the timeout, either.

When a Ctl-C is lost, sometimes it is silently eaten, and other times it 
becomes socket.error(11,...) (EWOULDBLOCK).  As (AFAICT) the socket in 
question is a blocking socket, I could use that nonsense exception to 
detect at least some of the lost Ctl-Cs (eww).  The socket does have a 
timeout.

Note that I am using FC5 Linux, Python 2.4.2.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trace KeyboardInterrupt exception?

2006-06-15 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

> if you want to interrupt the code to find out where it is,
> you can instead connect to it in gdb and get the python traceback of
> each thread.
> if you're interested I'll post the necesary gdb-macro for that (didn't
> put it on the net yet)

I think I've found the problem, using Python's Bugzilla.  This appears 
to be unresolved Python bug 926423, unresolved proposed patch 1102879, 
don't know if anything ever came of it.  See other thread.

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


how can I avoid abusing lists?

2006-07-07 Thread Thomas Nelson
I have this code:
type1 = [0]
type2 = [0]
type3 = [0]
map = {0:type1, 1:type1, 2:type3, 3:type1, 4:type2}  # the real map is
longer than this

def increment(value):
map[value][0] += 1

increment(1)
increment(1)
increment(0)
increment(4)
#increment will actually be called many times through other functions
print type1[0], type2[0], type3[0]
#should print "3 1 0"

This is exactly what I want to do: every time I encounter this kind of
value in my code, increment the appropriate type by one.  Then I'd like
to go back and find out how many of each type there were.  This way
I've written seems simple enough and effective, but it's very ugly and
I don't think it's the intended use of lists.  Does anyone know a
cleaner way to have the same funtionality?

Thanks,
THN

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


Re: how can I avoid abusing lists?

2006-07-07 Thread Thomas Nelson
Thanks to everyone who posted.  First, I don't think my question was
clear enough: Rob Cowie, Ant, Simon Forman, [EMAIL PROTECTED], and Jon
Ribbens offered solutions that don't quite work as-is, because I need
multiple values to map to a single type.  Tim Chase and Bruno
Destuilliers both offer very nice OOP solutions, and I think this is
the route I will probably go.  However, for the simplest and easiest
solution, I really like this from Peter Otten:
inflated = [0]*5
groups = [[0,1,3],[4],[2]]
for value in [1,1,0,4]:
inflated[value] += 1
print [sum(inflated[i] for i in group) for group in groups]

4 lines (one more to assign the lists to name values, but that's
minor), and intuitive.  If I had just thought of this to begin with, I
wouldn't have bothered posting.

Thanks to all for the advice.

THN

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


Re: Best command for running shell command

2006-07-11 Thread Thomas Nelson
Yes, I highly recommend the subprocess module.  subprocess.call() can
do almost anything you want to do, and the options are all pretty
intuitive  Whenever I need to write quick scripts for myself, it's what
I use.

THN


Roy Smith wrote:
> In article <[EMAIL PROTECTED]>,
>  Donald Duck <[EMAIL PROTECTED]> wrote:
>
> > I'm a little bit confused about what is the best way to run a shell command,
> > if I want to run a command like
> >
> > xx -a -b > yy
> >
> > where I'm not interested in the output, I only want to make sure that the
> > command was executed OK. How should I invoke this (in a Unix/linux
> > environment)?
>
> The most straight-forward way would be:
>
> import os
> status = os.system ("xx -a -b > yy")
> if status == 0:
>print "it worked"
> else:
>print "it failed"
> 
> You might also want to look at the new (in 2.4) subprocess module.

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


Re: Non greedy regex

2006-12-14 Thread Thomas Nelson
There's an optional count argument that will give what you want.  Try
re.sub('a.*b','','ababc',count=1)


Carsten Haese wrote:
> On Thu, 2006-12-14 at 06:45 -0800, [EMAIL PROTECTED] wrote:
> > Can someone please explain why these expressions both produce the same
> > result? Surely this means that non-greedy regex does not work?
> >
> > print re.sub( 'a.*b', '', 'ababc' )
> >
> > gives: 'c'
> >
> > Understandable. But
> >
> > print re.sub( 'a.*?b', '', 'ababc' )
> >
> > gives: 'c'
> >
> > NOT, understandable. Surely the answer should be: 'abc'
>
> You didn't tell re.sub to only substitute the first occurrence of the
> pattern. It non-greedily matches two occurrences of 'ab' and replaces
> each of them with ''. Try replacing with some non-empty string instead
> to observe the difference between the two behaviors.
> 
> -Carsten

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


Re: newbieee

2007-01-08 Thread Thomas Nelson

lee wrote:
> I getting familiarised with python...can any one suggest me a good
> editor available for python which runs on windows xpone more
> request guys...can nyone tell me a good reference manual for python..

I think vim is a very good editor for python, and it's certainly
available for windows xp.

O'reilly has a book called Programming Python that covers much of the
standard library and how to use it for complex tasks.  It may be out of
date by now, though.  Truthfully the only python references I use
regularly are the online docs and this list.

hth,
THN

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


  1   2   >