Re: WebScraping

2006-11-19 Thread Graham Feeley
Well I would like to publicly thank Bernard Chhun for actually writing this 
script and "pretting " it up for me.
He is truly a talented guy.
He used Beautifull Soup and Regex which i am still coming to terms trying to 
understand them any way Thanks again Bernard.
Regards
graham

"Graham Feeley" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Can someone steer me to scripts / modules etc on webscraping please???
> Ultimately I would like someone to write a script for me.
> However i am still searching for documentation on this subject
> Thanks Graham
>
>
> 


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


Re: basic python questions

2006-11-19 Thread Paul McGuire
"Hendrik van Rooyen" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> <[EMAIL PROTECTED]> wrote:
>
>
>> I am currently going to school at Utah Valley State College, the course
>> that I am taking is analysis of programming languages. It's an upper
>> division course but our teacher wanted to teach us python as part of
>
> what does "upper division" mean in this context ?  I am unfamiliar with 
> the
> term.
>
> - Hendrik
>
In a 4-year college program in the US, an upper division course is an 
advanced course, usually reserved for those in the 3rd or 4th years.

-- Paul 


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


Re: Finding skilled pythonistas for micro-jobs?

2006-11-19 Thread Nick Vatamaniuc
  If you are agile it means that the developer is in constant
communication with the user/client often changing and updating the
requirements. The typical work that you specify though is not of that
nature because it a list of specifications and then the coder
implements it, and when done gets back to you.

 In other words, you will pay for not having a relationship with the
programmer. Theoretically just having the requirements and a set of
unit tests should do it, but in practice it might not work, stuff
_always_ comes up when you least expect it. Sometimes the clear goals
end up not being that clear to both you and a strange new programmer
you just met over email.

 There are other things to consider what if the person doesn't come
through? They end up doing something else or forget -- you wait for 3
days, your company expects a result and the programmer you just asked
to work for you does not deliver. Or what if they are not satisfied
with the pay because it took longer than expected. Also, what would you
do about taxes, technically  you are employing someone, a day job is
still a job. Theoretically one could run a company and never pay the
proper taxes because they pay their programmers per day through
Paypal...

 However, you do mention CS students.  I would suggest to find a local
university and befriend a professor. Then whenever you need a small job
ask him. He will know the students (who knows what, how well and so
on). Just based on a personal experience from my CS department you will
not have a shortage of candidates ( starving graduate students _will_
code for food ), and you get to meet the students in person if you so
desire, and then keep them on  your contact list.  An even better
advice -- get two students,  you will get a true xp team, they'll have
to split the pay but in case one is busy,  the other one will be there
for you. You can hire them as part time with no benefits and they can
put the work on their resume, which will be of great help for them.

Note: if your company is well known, you might even find students who
will help for free, just so they can put some work experience on their
resume...

Hope that helps,
Nick V.

darran wrote:
> I'm a partner in a design and technology studio that creates
> large-scale interactive exhibits for museums.  We are agile - by
> choice.   For big 6-12 month projects, we try and secure exceptional
> python talent on contract.  The python job board addresses this need.
>
> Every few weeks though I run up against a bite-sized programming
> problem begging to be farmed out by our small company.  The tasks
> themselves span the gamut from data wrangling (format conversions) to
> SWIG wrappers to Twisted to pyOpenGL.  Often the task is 1 or 2 days of
> work.  Not really big enough to warrant a job search, a contract, or
> even someone's full-time attention.  The type of problem that is
> perfectly suited to a CS student or daytime programmer looking to make
> some extra money.  Presently, when one of these jobs pops up, I just
> add 8-16 hours to my work week - much to the dismay of my 3-year old
> daughter who'd rather I pay someone and go to the park.  The nice thing
> though about our bite-sized jobs is that the goals are perfectly clear
> because we are religious in our use of unit testing and test-driven
> development.
>
> Any suggestions then for locating skilled Python/C++ programmers for
> these small (micro) jobs?  
> 
> Cheers,
> Darran.

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


Why isn't SPLIT splitting my strings

2006-11-19 Thread ronrsr
I'm trying to break up the result tuple into keyword phrases.  The
keyword phrases are separated by a ;  -- the split function is not
working the way I believe it should be. Can anyone see what I"m doing
wrong?

bests,

-rsr-



 print "newstring =", str(result[i][0])
print "just split", str(result[i][0]).split('; ()[]"')
zd.append(str(result[i][0]).split('; ()[]"'))


result= (('Agricultural subsidies; Foreign aid',), ('Agriculture;
Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
Childhood Development, Birth Defects; Toxic Chemicals',),
('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',),
('Agricultural Subsidies',), ('Biodiversity',), ('Citizen Activism',),
('Community Gardens',), ('Cooperatives',), ('Dieting',), ('Agriculture,
Cotton',), ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
Feed, Corn, Pesticides',), ('Aquaculture',), ('Chemical Warfare',),
('Compost',), ('Debt',), ('Consumerism',), ('Fear',), ('Pesticides, US,
Childhood Development, Birth Defects',), ('Corporate Reform,
Personhood (Dem. Book)',), ('Corporate Reform,  Personhood, Farming
(Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
('Democracy, Corporate



partial results:

string a =  Agricultural subsidies; Foreign aid
newstring = Agricultural subsidies; Foreign aid
just split ['Agricultural subsidies; Foreign aid']
newstring = Agriculture; Sustainable Agriculture - Support; Organic
Agriculture; Pesticides, US, Childhood Development, Birth Defects;
Toxic Chemicals
just split ['Agriculture; Sustainable Agriculture - Support; Organic
Agriculture; Pesticides, US, Childhood Development, Birth Defects;
Toxic Chemicals']
newstring = Antibiotics, Animals
just split ['Antibiotics, Animals']

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


Re: Why isn't SPLIT splitting my strings

2006-11-19 Thread Nick Vatamaniuc
The regular string split does not take a _class_ of characters as the
separator, it only takes one separator. Your example suggests that you
expect that _any_ of the characters "; ()[]" could be a separator.
If you want to use a regular expression as a list of separators, then
you need to use the split function from the regular expression module
(re):
http://docs.python.org/lib/node46.html

Hope this helps,
Nick V.


ronrsr wrote:
> I'm trying to break up the result tuple into keyword phrases.  The
> keyword phrases are separated by a ;  -- the split function is not
> working the way I believe it should be. Can anyone see what I"m doing
> wrong?
>
> bests,
>
> -rsr-
>
>
>
>  print "newstring =", str(result[i][0])
> print "just split", str(result[i][0]).split('; ()[]"')
> zd.append(str(result[i][0]).split('; ()[]"'))
>
>
> result= (('Agricultural subsidies; Foreign aid',), ('Agriculture;
> Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
> Childhood Development, Birth Defects; Toxic Chemicals',),
> ('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',),
> ('Agricultural Subsidies',), ('Biodiversity',), ('Citizen Activism',),
> ('Community Gardens',), ('Cooperatives',), ('Dieting',), ('Agriculture,
> Cotton',), ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
> ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
> Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
> Feed, Corn, Pesticides',), ('Aquaculture',), ('Chemical Warfare',),
> ('Compost',), ('Debt',), ('Consumerism',), ('Fear',), ('Pesticides, US,
> Childhood Development, Birth Defects',), ('Corporate Reform,
> Personhood (Dem. Book)',), ('Corporate Reform,  Personhood, Farming
> (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
> Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
> ('Democracy, Corporate
>
>
>
> partial results:
>
> string a =  Agricultural subsidies; Foreign aid
> newstring = Agricultural subsidies; Foreign aid
> just split ['Agricultural subsidies; Foreign aid']
> newstring = Agriculture; Sustainable Agriculture - Support; Organic
> Agriculture; Pesticides, US, Childhood Development, Birth Defects;
> Toxic Chemicals
> just split ['Agriculture; Sustainable Agriculture - Support; Organic
> Agriculture; Pesticides, US, Childhood Development, Birth Defects;
> Toxic Chemicals']
> newstring = Antibiotics, Animals
> just split ['Antibiotics, Animals']

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


Re: Why isn't SPLIT splitting my strings

2006-11-19 Thread Fredrik Lundh
ronrsr wrote:

> I'm trying to break up the result tuple into keyword phrases.  The
> keyword phrases are separated by a ;  -- the split function is not
> working the way I believe it should be. 

 >>> help(str.split)

split(...)
 S.split([sep [,maxsplit]]) -> list of strings

 Return a list of the words in the string S, using sep as the
 delimiter string.

note that it says *delimiter string*, not *list of characters that I 
want it to split on*.



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


Re: Why isn't SPLIT splitting my strings

2006-11-19 Thread Duncan Booth
"ronrsr" <[EMAIL PROTECTED]> wrote:

> I'm trying to break up the result tuple into keyword phrases.  The
> keyword phrases are separated by a ;  -- the split function is not
> working the way I believe it should be. Can anyone see what I"m doing
> wrong?

I think your example boils down to:

>>> help(str.split)
Help on method_descriptor:

split(...)
S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator.

>>> s = 'Antibiotics, Animals'
>>> s.split('; ()[]"')
['Antibiotics, Animals']

This is what I expect. The separator string '; ()[]"' does not occur 
anywhere in the input string, so there is nowhere to split it.

If you want to split on multiple single characters, then either use 
re.split and filter out the separators, or use string.translate or 
str.replace to replace all of the separators with the same character and 
split on that. e.g.

>>> def mysplit(s):
s = s.replace(' ', ';')
s = s.replace('(', ';')
s = s.replace(')', ';')
s = s.replace('[', ';')
s = s.replace(']', ';')
s = s.replace('"', ';')
return s.split(';')

>>> mysplit(s)
['Antibiotics,', 'Animals']

which still looks a bit weird as output, but you never actually said what 
output you wanted.
-- 
http://mail.python.org/mailman/listinfo/python-list


need help?

2006-11-19 Thread JFletcher
Get Python help at TheScripts [dot] com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why isn't SPLIT splitting my strings

2006-11-19 Thread Paul McGuire
"ronrsr" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> I'm trying to break up the result tuple into keyword phrases.  The
> keyword phrases are separated by a ;  -- the split function is not
> working the way I believe it should be. Can anyone see what I"m doing
> wrong?
>
> bests,
>
> -rsr-
>
>
>
> print "newstring =", str(result[i][0])
>print "just split", str(result[i][0]).split('; ()[]"')
>zd.append(str(result[i][0]).split('; ()[]"'))
>
>
> result= (('Agricultural subsidies; Foreign aid',), ('Agriculture;
> Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
> Childhood Development, Birth Defects; Toxic Chemicals',),
> ('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',),
> ('Agricultural Subsidies',), ('Biodiversity',), ('Citizen Activism',),
> ('Community Gardens',), ('Cooperatives',), ('Dieting',), ('Agriculture,
> Cotton',), ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
> ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
> Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
> Feed, Corn, Pesticides',), ('Aquaculture',), ('Chemical Warfare',),
> ('Compost',), ('Debt',), ('Consumerism',), ('Fear',), ('Pesticides, US,
> Childhood Development, Birth Defects',), ('Corporate Reform,
> Personhood (Dem. Book)',), ('Corporate Reform,  Personhood, Farming
> (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
> Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
> ('Democracy, Corporate
>
>
There are no ()'s or []'s in any of your tuple values, these are just 
punctuation to show you the nested tuple structure of your db query results. 
You say ';' is your keyword delimiter, so just split on ';'.

Also split('; ()[]') does not split on any one of the listed characters, but 
only on the complete string '; ()[]', which occurs nowhere in your data.

-- Paul


# for each result in the tuple of tuples, split on ';', and then print the 
results separated by " : " to show that we really did separate the results

for t in result:
print " : ".join(t[0].split(';'))

Gives:
Agricultural subsidies :  Foreign aid
Agriculture : Sustainable Agriculture - Support :  Organic Agriculture : 
Pesticides, US,Childhood Development, Birth Defects :  Toxic Chemicals
Antibiotics, Animals
Agricultural Subsidies, Global Trade
Agricultural Subsidies
Biodiversity
Citizen Activism
Community Gardens
Cooperatives
Dieting
Agriculture,Cotton
Agriculture, Global Trade
Pesticides, Monsanto
Agriculture, Seed
Coffee, Hunger
Pollution, Water,Feedlots
Food Prices
Agriculture, Workers
AnimalFeed, Corn, Pesticides
Aquaculture
Chemical Warfare
Compost
Debt
Consumerism
Fear
Pesticides, US,Childhood Development, Birth Defects
Corporate Reform,Personhood (Dem. Book)
Corporate Reform,  Personhood, Farming(Dem. Book)
Crime Rates, Legislation, Education
Debt,Credit Cards
Democracy
Population, World
Income 


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


Re: basic python questions

2006-11-19 Thread John Machin

Paddy wrote:
> John Machin wrote:
>
>
> > [Aside] How are you going to explain all this to your instructor, who
> > may be reading all this right now?
> >
>
> The instructor should be proud!
> He has managed to do his very first post to a this newsgroup, about a
> homework question, and do it in the right way. that is no mean feat.
>
> - Paddy.

In fact, he may well by now know more than his instructor, and be
explaining the finer points of Python :-)

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


Re: Programmatically finding "significant" data points

2006-11-19 Thread robert
erikcw wrote:
> Hi all,
> 
> I have a collection of ordered numerical data in a list.  The numbers
> when plotted on a line chart make a low-high-low-high-high-low (random)
> pattern.  I need an algorithm to extract the "significant" high and low
> points from this data.
> 
> Here is some sample data:
> data = [0.10, 0.50, 0.60, 0.40, 0.39, 0.50, 1.00, 0.80, 0.60, 1.20,
> 1.10, 1.30, 1.40, 1.50, 1.05, 1.20, 0.90, 0.70, 0.80, 0.40, 0.45, 0.35,
> 0.10]
> 
> In this data, some of the significant points include:
> data[0]
> data[2]
> data[4]
> data[6]
> data[8]
> data[9]
> data[13]
> data[14]
> 
> 
> How do I sort through this data and pull out these points of
> significance?

Its obviously a kind of time series and you are search for a 
"moving_max(data,t,window)>data(t)" / "moving_min(data,t,window)http://mail.python.org/mailman/listinfo/python-list


Re: A python IDE for teaching that supports cyrillic i/o

2006-11-19 Thread Leo Kislov
Kirill Simonov wrote:
> Hi,
>
> Could anyone suggest me a simple IDE suitable for teaching Python as a
> first programming language to high school students?  It is necessary
> that it has a good support for input/output in Cyrillic.
>
> Unfortunately, most IDEs I tried failed miserably in this respect.  My
> test was simple: I've run the code
> name = raw_input("What's your name? ")  # written in Russian
> print "Hello, %s!" % name   # in Russian as well
> both from the shell and as a standalone script. This either caused a
> UnicodeError or just printed invalid characters.
>
> For the record, I've checked IDLE, PythonWin, Eric, DrPython, SPE, and
> WingIDE.  The only ones that worked are WingIDE and IDLE (under Linux,
> but not under Windows).

IDLE on Windows works fine for your example in interactive console:

>>> name = raw_input("What's your name? ")
What's your name? Леонид
>>> print name
Леонид
>>> name
u'\u041b\u0435\u043e\u043d\u0438\u0434'

and as a script:

What's your name? Леонид
Hello, Леонид!

>>>

That is IDLE + python 2.4 on Windows. So I'm not sure what is the
problem. In other messages you seems to be talking about system
console. Why? It's not part of IDE.

And another question: are you aware of the fact that recommended way to
handle non-ascii characters is to use unicode type? Most of IDEs should
work fine with unicode. 

  -- Leo

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

Re: A python IDE for teaching that supports cyrillic i/o

2006-11-19 Thread Alan Franzoni
Kirill Simonov  si è divertito a scrivere:

> Unfortunately, most IDEs I tried failed miserably in this respect.  My
> test was simple: I've run the code
> name = raw_input("What's your name? ")  # written in Russian
> print "Hello, %s!" % name   # in Russian as well
> both from the shell and as a standalone script. This either caused a
> UnicodeError or just printed invalid characters.

I highly dislike asking stupid questions, and this might be stupid
indeed... but did you write

# -*- coding: iso-8859-5 -*-

or

# -*- coding: koi8_r -*-

(they both seem suited to the Russian language, but I don't know the
difference)

as the first line in your .py file?

Personally, I use Eclipse+Pydev (a bit steep to learn at the beginning, and
quite memory and cpu hogging since it's a java-based ide; don't use it on
old/slow computers with less than 512MB RAM, and don't use version < 3.2
either) and it uses that very line to recognize the actual character set
employed. You may check with other encodings as well.

http://docs.python.org/lib/standard-encodings.html

It does work on Windows indeed.

UPDATE:
I tried with Eclipse+Pydev, and using koi8_r I seems to be able to simply
copy&paste a piece of the ixbt.com homepage in the editor I can save and
use it correctly.


-- 
Alan Franzoni <[EMAIL PROTECTED]>
-
Togli .xyz dalla mia email per contattarmi.
Remove .xyz from my address in order to contact me.
-
GPG Key Fingerprint (Key ID = FE068F3E):
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding skilled pythonistas for micro-jobs?

2006-11-19 Thread John Machin
darran wrote:
> I'm a partner in a design and technology studio that creates
> large-scale interactive exhibits for museums.  We are agile - by
> choice.   For big 6-12 month projects, we try and secure exceptional
> python talent on contract.  The python job board addresses this need.
>
> Every few weeks though I run up against a bite-sized programming
> problem begging to be farmed out by our small company.  The tasks
> themselves span the gamut from data wrangling (format conversions) to
> SWIG wrappers to Twisted to pyOpenGL.  Often the task is 1 or 2 days of
> work.  Not really big enough to warrant a job search, a contract, or
> even someone's full-time attention.  The type of problem that is
> perfectly suited to a CS student or daytime programmer looking to make
> some extra money.  Presently, when one of these jobs pops up, I just
> add 8-16 hours to my work week - much to the dismay of my 3-year old
> daughter who'd rather I pay someone and go to the park.  The nice thing
> though about our bite-sized jobs is that the goals are perfectly clear
> because we are religious in our use of unit testing and test-driven
> development.
>
> Any suggestions then for locating skilled Python/C++ programmers for
> these small (micro) jobs?
>

A few entries from my own cynic's dictionary:

Agile: the accounts dept displays fancy footwork when the bill is
submitted

Religious: Religions in IT come and go; the only tenets universally
held by customer management have always been the Divine Right of Kings
and Papal Infallibility, both applied of course only to themselves

Clear goals, unit testing as applied to "data wrangling" -- a nonsense.
Specs are prepared that are only tangentially relevant to the task and
have obviously not been derived from any inspection of the data.

Example (had allegedly been peer reviewed and mgt reviewed):
"Placenames [in Australia] shall be validated to contain only letters
and spaces" [or words to that effect] -- never mind no mention of case.
Never mind that a moment's armchair reflection would have indicated
that apostrophe and hyphen might be reasonable candidates. Australia is
not heavily populated. Then (and now) the full list of placenames and
postcodes needed for the application was printed at the back of
larger-city phone directories, as found on the desks of  "business
analysts". It was available on floppy disk and in paper booklets from
the Post Office. I believe that it was even available in Braille, so
that the proverbial Blind Freddie would have been able to tell them
that the *first* entry was [then] "A1 MINE SETTLEMENT".

Cheers,
John

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


Re: [Zopyrus] A python IDE for teaching that supports cyrillic i/o

2006-11-19 Thread Alexey Wasilyev
Здравствуйте, Kirill.

Вы писали 18 ноября 2006 г., 22:22:48:
>> > Could anyone suggest me a simple IDE suitable for teaching Python as a
>> > first programming language to high school students?
>> 
>>Does it have to be an IDE? Wouldn't it be better to use a simple text
>> editor + command line?
> Preferably.  I believe that using a editor + command line will only make
> things worse because console and GUI have different encodings under
> Windows.  So a student that have written a script in a GUI editor and saved
> it in UTF-8 would see garbage in console.

Ничего, что я по русски? Вроде все свои :)
Я тоже озаботился выбором нормального IDE. Кроме редактора и
запускателя скриптов в ide же еще должен быть нормальный отладчик,
интеграция с системой контроля версий, автодополнение кода... Под виндами самое 
приличное из
всего увиденного показалось komodo. Да и то, подглюкивает
периодически, подвисает, из vcs знает только css да svn.
SourceOffSite'а не умеет :( Человеку, привыкшему к нормальному ide от
вижуал студии тяжко :)


-- 
С уважением,
 Alexey  mailto:[EMAIL PROTECTED]

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

tempfile.NamedTemporaryFile wont work

2006-11-19 Thread Imbaud Pierre
On suse 9.3, tempfile.NamedTemporaryFile() doesnt work as expected.
(I found a permanent workaround, so I dont ask for help)
I expected to write to a file, and access it thru a shell command.
This code, in a loop:
 tf = tempfile.NamedTemporaryFile()
 tfName = tf.name
 #tf.seek(0)  # rewind the file
 tf.write(chunk); tf.flush()
 print >> sys.stderr, '%s: %s' % (tfName, ['no', 
'yes'][os.path.exists(tfName)])
 subprocess.Popen(['strings', tfName])

Symptom: the file does not always exist, after the call to
NamedTemporaryFile(). Or at least its not seen by the strings command,
or by os.path.exists.

I guess the bug is pretty much os dependent, or even filesystem
dependent (Im on reiserfs). Maybe the os is buggy, maybe, somehow, the
python interface. Or did I miss something?
Shame, I didnt even try to check for a python bug tracker.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tempfile.NamedTemporaryFile wont work

2006-11-19 Thread Bjoern Schliessmann
Imbaud Pierre wrote:

>  tf = tempfile.NamedTemporaryFile()
>  tfName = tf.name
> [...]
>  print >> sys.stderr, '%s: %s' % (tfName, ['no',
> 'yes'][os.path.exists(tfName)])
>  subprocess.Popen(['strings', tfName])

Just out of curiosity: Why did you assign tf.name to tfname?

Hypothetically, if tf.name changed, tfname wouldn't follow since
strings are immutable.

Regards,


Björn

-- 
BOFH excuse #149:

Dew on the telephone lines.

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


Re: syntax error in sum(). Please explicate.

2006-11-19 Thread Bjoern Schliessmann
Paul Rubin wrote:

> Generator comprehensions

Are generator comprehensions and generator expressions the same?

Regards,


Björn

-- 
BOFH excuse #35:

working as designed

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


Re: decompiler

2006-11-19 Thread Bjoern Schliessmann
Fuzzyman wrote:

> That's terrible. You read comp.lang.perl.misc *as well* ?
> 
> ;-)

You have to know your enemy ;)

Regards,


Björn

-- 
BOFH excuse #343:

The ATM board has run out of 10 pound notes.  We are having a whip
round to refill it, care to contribute ?

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


ElementTree: namespace declaration in each element?

2006-11-19 Thread cyberco
The (excellent) ElementLibrary ads namespaces to each element when
writing the tree back to a file.
Say I do:

tree = ET.parse('playlist.xml')

tree.write(outFile)

with the following XML:

=
http://xspf.org/ns/0/";>


Kick off!



=

Writing the tree out after adding/removing elements results in:

=
http://xspf.org/ns/0/";>

Kick off!




=

Unfortunately the client that needs to consume this XML can't handle
the 'ns0:' part of each tag. Is there a way to output XML similar to
the input?

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


Re: syntax error in sum(). Please explicate.

2006-11-19 Thread Fredrik Lundh
Bjoern Schliessmann wrote:

> Are generator comprehensions and generator expressions the same?

the documentation uses the word "expression", not comprehension.



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


Re: ElementTree: namespace declaration in each element?

2006-11-19 Thread Fredrik Lundh
cyberco wrote:

> Unfortunately the client that needs to consume this XML can't handle
> the 'ns0:' part of each tag.

 > Is there a way to output XML similar to the input?

written by a "it's the bytes, not the data" guy, obviously ;-)

the standard serializer in 1.2 doesn't support default namespaces, so 
the quickest way to do this is to loop over all tags (use getiterator), 
strip off the "{http://xspf.org/ns/0/}"; prefix, add an xmlns attribute 
to the root (if necessary), and write it out.



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


Re: lxml/ElementTree and .tail

2006-11-19 Thread Uche Ogbuji

Fredrik Lundh wrote:
> Uche Ogbuji wrote:
>
> > I certainly have never liked the aspects of the ElementTree API under
> > present discussion.  But that's not as important as the fact that I
> > think the above statement is misleading.  There has always been a
> > battle in XML between the people who think the serialization is
> > preeminent, and those who believe some data model is preeminent, but
> > the reality is that XML 1.0 (an 1.1) is a spec *defined* by its
> > serialization.
>
> sure, the computing world is and has always been full of people who want
> the simplest thing to look a lot harder than it actually is.  after all,
> *they* spent lots of time reading all the specifications, they've bought
> all the books, and went to all the seminars, so it's simply not fair
> when others are cheating.

You sound bitter about something.  Don't worry, it's really not all
that serious.

> in reality, *all* interchange formats are easier to understand and use
> if you focus on a (complete or intentionally simplified) data model of
> the things being interchanged, and treat various artifacts of the
> byte-stream used by the wire format as artifacts, historical accidents
> based on what specification happened to be written before the other, or
> what some guy did or did not do in the seventies, as accidents, and
> esoteric arcana disseminated on limited-distribution mailing lists as
> about as relevant for your customer as last week's episode of American Idol.

The fact that the XML Infoset is hardly used outside W3C XML Schema,
and that the XPath data model is far more common, and that focus on the
serialization is even more common than that is a matter of everyday
practicality.

And oh by the way, this thread is all about *your* customer's
complaining.  And your response is to give them your philosophical take
on XML.  Doesn't that contradict what you're saying above?

Oh never mind.  You posted something misleading, and I posted another
point of view.  I know you're incapable of any disagreement that
doesn't devolve into a full-scale flame-war.  Sometimes I have time for
that sort of thing.  This is not one fo those times, so this is
probably where I get off.

--
Uche Ogbuji   Fourthought, Inc.
http://uche.ogbuji.nethttp://fourthought.com
http://copia.ogbuji.net   http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

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


Re: tempfile.NamedTemporaryFile wont work

2006-11-19 Thread Steven D'Aprano
On Sun, 19 Nov 2006 13:11:13 +0100, Imbaud Pierre wrote:

> On suse 9.3, tempfile.NamedTemporaryFile() doesnt work as expected.
[snip]

> Symptom: the file does not always exist, after the call to
> NamedTemporaryFile(). Or at least its not seen by the strings command,
> or by os.path.exists.
> 
> I guess the bug is pretty much os dependent, or even filesystem
> dependent (Im on reiserfs). Maybe the os is buggy, maybe, somehow, the
> python interface. Or did I miss something?
> Shame, I didnt even try to check for a python bug tracker.

I can verify this problem occurs on Fedora Core 5 too:

import os
import sys
import tempfile
import subprocess
def test(n):
chunk = ': +++ abcd +++'
for i in xrange(n):
tf = tempfile.NamedTemporaryFile()
tfName = tf.name
tf.seek(0)
tf.write(str(i) + chunk)
tf.flush()
if not os.path.exists(tfName):
print 'pre-check: %s not there' % tfName
subprocess.Popen(['strings', tfName])
if not os.path.exists(tfName):
print 'post-check: %s not there' % tfName


And here is a typical run, with the boring bits removed for ease of
reading:

>>> test(30)
0: +++ abcd +++
1: +++ abcd +++
[ more of the same ]
14: +++ abcd +++
strings: '/tmp/tmpOALbx9': No such file
16: +++ abcd +++
17: +++ abcd +++
18: +++ abcd +++
[ more of the same ]
27: +++ abcd +++
strings: /tmp/tmpdc52Nz: No such file or directory
29: +++ abcd +++


Curiouser and curiouser... not only does os.path.exist always report the
temp file as existing (at least in my tests), even when strings can't find
it, but strings returns different error messages.

Is it possible this is a bug in strings?


-- 
Steven.

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


Re: [Zopyrus] A python IDE for teaching that supports cyrillic i/o

2006-11-19 Thread tool69
Sorry, but did someone knows if Pida works under Windows ?
Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A python IDE for teaching that supports cyrillic i/o

2006-11-19 Thread Kirill Simonov
On Sun, Nov 19, 2006 at 12:33:39PM +0100, Alan Franzoni wrote:
> Kirill Simonov  si è divertito a scrivere:
> 
> > Unfortunately, most IDEs I tried failed miserably in this respect.  My
> > test was simple: I've run the code
> > name = raw_input("What's your name? ")  # written in Russian
> > print "Hello, %s!" % name   # in Russian as well
> > both from the shell and as a standalone script. This either caused a
> > UnicodeError or just printed invalid characters.
> 
> I highly dislike asking stupid questions, and this might be stupid
> indeed... but did you write
> 
> # -*- coding: iso-8859-5 -*-
> 
> or
> 
> # -*- coding: koi8_r -*-
> 
> (they both seem suited to the Russian language, but I don't know the
> difference)

They are different encodings for the same character set. The latter is
mostly used under Unices, and the former is not used anywhere as far as
I know. There are two more cyrillic encodings: cp866 and cp1251 - for
DOS and Windows correspondingly.
 
> as the first line in your .py file?

No, I would prefer the editor to save the .py files with non-ASCII
characters in UTF-8 encoding adding the BOM at the beginning of the
file. This will allow the interpreted to detect the file encoding
correctly and would save a teacher from explaining what an encoding is
and why it is needed.

> Personally, I use Eclipse+Pydev (a bit steep to learn at the beginning, and
> quite memory and cpu hogging since it's a java-based ide; don't use it on
> old/slow computers with less than 512MB RAM, and don't use version < 3.2
> either) and it uses that very line to recognize the actual character set
> employed. You may check with other encodings as well.

Unfortunately, the Eclipse CPU/memory requirements are extremely high
for a high school, so I haven't even tried it.


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


Re: tempfile.NamedTemporaryFile wont work

2006-11-19 Thread Steven D'Aprano
On Sun, 19 Nov 2006 13:18:39 +0100, Bjoern Schliessmann wrote:

> Imbaud Pierre wrote:
> 
>>  tf = tempfile.NamedTemporaryFile()
>>  tfName = tf.name
>> [...]
>>  print >> sys.stderr, '%s: %s' % (tfName, ['no',
>> 'yes'][os.path.exists(tfName)])
>>  subprocess.Popen(['strings', tfName])
> 
> Just out of curiosity: Why did you assign tf.name to tfname?
> 
> Hypothetically, if tf.name changed, tfname wouldn't follow since
> strings are immutable.

Well, yes, but if tf.name changed, that won't change the file name on disk
either:

>>> tf = tempfile.NamedTemporaryFile()
>>> tf.name
'/tmp/tmpYVV1Ij'
>>> os.path.exists(tf.name)
True
>>> oldname = tf.name
>>> tf.name = "/tmp/something"
>>> os.path.exists(tf.name)
False
>>> os.path.exists(oldname)
True


I'm guessing that binding tf.name to tfName is a micro-optimization. In a
very tight loop, name lookups can take considerable time, and one
optimization can be to reduce the number of lookups:

method = something.method
while 1:
something.method # needs at least two lookups
method # needs a single lookup


-- 
Steve.

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


Re: lxml/ElementTree and .tail

2006-11-19 Thread Uche Ogbuji
Paul McGuire wrote:
> Thankfully, I'm largely on the periphery of that universe (except for being
> a sometimes victim).  But it is certainly frustrating to see many of the OMG
> concepts of the 90's reimplemented in Java services, and then again in
> XML/SOAP, with no detectable awareness that these messaging and
> serialization problems have been considered before, and much more
> thoroughly.

You'll be surprised at how many XMLers agree that Web services are a
pretty inept reinvention of CORBA.  I was pretty much slain by this
take:

http://wanderingbarque.com/nonintersecting/2006/11/15/the-s-stands-for-simple

I think Duncan Grisby of OmniORB put it most succintly when he pointed
out that SOAP and friends are more complex, more bloated, and less
interoprable than CORBA ever was.  But they use XML so they get the
teacher's pet treatment.


> I liked XML when I could read it and hack it out in Notepad.

You still can, and don't let anyone tell you otherwise.  I've always
argued that XML doesn't work unless it's Notepad-hackable.  I do
usually allow an exception for SVG.

> I like
> attributes, which puts me on the outs with most XML zealots who forswear the
> use of attributes on purely academic grounds (they defeat the future
> possible expansion of an attribute's value into more complex substructure).

Really?  Do you have any references for this?  I haven't seen much
criticism of attributes since the very early days, and almost all XML
technologies make heavy use of attributes.  Here's my take:

http://www.ibm.com/developerworks/xml/library/x-eleatt.html

As you can see, elements and attributes get equal billing.

> I dislike namespaces, especially the default xmlns kind, as they make me
> take extra steps when retrieving nodes via Xpaths; and everyone seems to
> think their application needs namespaces, when there is no threat that these
> tags will ever get mixed up with anyone else's.

Namespaces are possibly the worst thing to have ever happened to XML.
Again, my take:

http://www.ibm.com/developerworks/xml/library/x-namcar.html

And yes, default namespaces are about 50% of the problem with
namespace.  QNames in content (which are of course an abuse of
namespaces) are almost all of the other 50%.  I call them "hidden
namespaces":

http://copia.ogbuji.net/blog/2006-08-14/Some_thoug

--
Uche Ogbuji   Fourthought, Inc.
http://uche.ogbuji.nethttp://fourthought.com
http://copia.ogbuji.net   http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

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


Re: A python IDE for teaching that supports cyrillic i/o

2006-11-19 Thread Alan Franzoni
Kirill Simonov  si è divertito a scrivere:

> On Sun, Nov 19, 2006 at 12:33:39PM +0100, Alan Franzoni wrote:
 
> No, I would prefer the editor to save the .py files with non-ASCII
> characters in UTF-8 encoding adding the BOM at the beginning of the
> file. This will allow the interpreted to detect the file encoding
> correctly and would save a teacher from explaining what an encoding is
> and why it is needed.

You'll run into encoding problems anyway in your programmer's life. I don't
think it's a workaround, try this article:

http://www.joelonsoftware.com/articles/Unicode.html

I think it's highly useful, you could tell that to your students.

BTW, not every editor supports the BOM. Have you tried with the explicit
encoding line:

# -*- coding: utf-8 -*-

Eclipse+Pydev seems to work with that. I'm not able to check with other
editors right now, but it seems you're experiencing a simple encoding
problem; your editor doesn't know which encoding you'd like to use, so it
defaults to ascii or iso-8859-1 leading to such problems.



-- 
Alan Franzoni <[EMAIL PROTECTED]>
-
Togli .xyz dalla mia email per contattarmi.
Remove .xyz from my address in order to contact me.
-
GPG Key Fingerprint (Key ID = FE068F3E):
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A python IDE for teaching that supports cyrillic i/o

2006-11-19 Thread Kirill Simonov
On Sun, Nov 19, 2006 at 03:27:32AM -0800, Leo Kislov wrote:
> IDLE on Windows works fine for your example in interactive console:
> 
> >>> name = raw_input("What's your name? ")

Have you tried to use cyrillic characters in a Python string in
interactive console? When I do it, I get the "Unsupported characters in
input" error. For instance,

>>> print "Привет"  # That's "Hi" in Russian.
Unsupported characters in input

>>>

> And another question: are you aware of the fact that recommended way to
> handle non-ascii characters is to use unicode type? Most of IDEs should
> work fine with unicode. 

Usually using unicode type gives you much more headache than benefits
unless you are careful enough to never mix unicode and str objects.

Anyway, I just want the interactive console of an IDE to behave like a
real Python console under a UTF-8 terminal (with sys.stdout.encoding ==
'utf-8').


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

Re: How do I stop Python IDLE (GUI) from immediately exiting when I enter it?

2006-11-19 Thread BartlebyScrivener
John (Z R) L wrote:

> But after clicking "run module"

Being new is never problem, but do learn to provide concise, complete
descriptions of exactly what happened and what you were doing at the
time.

Are you in IDLE? Or are you in PythonWin?  OR did you make a script
file and try to run it by double clicking on it?

Sounds like you were creating a script file, then tried to run it?
Search this list at Google Groups comp.lang.python for "keep DOS box
open."

http://tinyurl.com/wh7fy

> Another problem I have is firewall. On my old computer (Windows 98)
> when using Python GUI, it can't run modules because of some firewall.

Please enter the exact error message you get. It's probably some
officious Windows security glitch, but nobody can help you until you
provide the messages you get.

Windows 98? Linux would run twice as fast on any machine old enough to
run Windows 98, and your Python would work better, too. 

Good luck,

rd

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


Re: tempfile.NamedTemporaryFile wont work

2006-11-19 Thread Peter Otten
Steven D'Aprano wrote:

> On Sun, 19 Nov 2006 13:11:13 +0100, Imbaud Pierre wrote:
> 
>> On suse 9.3, tempfile.NamedTemporaryFile() doesnt work as expected.
> [snip]
> 
>> Symptom: the file does not always exist, after the call to
>> NamedTemporaryFile(). Or at least its not seen by the strings command,
>> or by os.path.exists.
>> 
>> I guess the bug is pretty much os dependent, or even filesystem
>> dependent (Im on reiserfs). Maybe the os is buggy, maybe, somehow, the
>> python interface. Or did I miss something?
>> Shame, I didnt even try to check for a python bug tracker.
> 
> I can verify this problem occurs on Fedora Core 5 too:
> 
> import os
> import sys
> import tempfile
> import subprocess
> def test(n):
> chunk = ': +++ abcd +++'
> for i in xrange(n):
> tf = tempfile.NamedTemporaryFile()
> tfName = tf.name
> tf.seek(0)
> tf.write(str(i) + chunk)
> tf.flush()
> if not os.path.exists(tfName):
> print 'pre-check: %s not there' % tfName
> subprocess.Popen(['strings', tfName])
> if not os.path.exists(tfName):
> print 'post-check: %s not there' % tfName
> 
> 
> And here is a typical run, with the boring bits removed for ease of
> reading:
> 
 test(30)
> 0: +++ abcd +++
> 1: +++ abcd +++
> [ more of the same ]
> 14: +++ abcd +++
> strings: '/tmp/tmpOALbx9': No such file
> 16: +++ abcd +++
> 17: +++ abcd +++
> 18: +++ abcd +++
> [ more of the same ]
> 27: +++ abcd +++
> strings: /tmp/tmpdc52Nz: No such file or directory
> 29: +++ abcd +++
> 
> 
> Curiouser and curiouser... not only does os.path.exist always report the
> temp file as existing (at least in my tests), even when strings can't find
> it, but strings returns different error messages.
> 
> Is it possible this is a bug in strings?

What /you/ are seeing is not a bug, I think. Popen() is asynchronous,
therefore you may enter the second iteration -- which implicitly closes the
temporary file -- before strings actually tries to access it. Use call()
and everything should be fine.

Peter

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


Re: lxml/ElementTree and .tail

2006-11-19 Thread Diez B. Roggisch
> You'll be surprised at how many XMLers agree that Web services are a
> pretty inept reinvention of CORBA.  I was pretty much slain by this
> take:
> 
> http://wanderingbarque.com/nonintersecting/2006/11/15/the-s-stands-for-simple

Thanks for that! Sums up nicely my experiences, and gave me a good chuckle!

While I liked the idea of AXIS reflecting my java code in the first 
place (as long as interoperability only meant "I can test my own code"), 
it sucked s hard when trying to make it work with anything else 
(including python of course).

And I don't know why I've complained about this style of inverse 
interface generation on so many other occasions (e.g. COM interfaces in 
VStudio, JBuilder GUI design and so on), but could never quite put the 
finger on what disturbed me on SOAP.

Probably because looking at a WSDL it immediately made me shrink away 
from that mess and hope that there must be _some_ merciful deity that 
will produce that crap for me, so that I never asked myself  the right 
questions

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


Re: A python IDE for teaching that supports cyrillic i/o

2006-11-19 Thread Kirill Simonov
On Sun, Nov 19, 2006 at 03:13:30PM +0100, Alan Franzoni wrote:
> Kirill Simonov  si è divertito a scrivere:
> 
> > On Sun, Nov 19, 2006 at 12:33:39PM +0100, Alan Franzoni wrote:
>  
> > No, I would prefer the editor to save the .py files with non-ASCII
> > characters in UTF-8 encoding adding the BOM at the beginning of the
> > file. This will allow the interpreted to detect the file encoding
> > correctly and would save a teacher from explaining what an encoding is
> > and why it is needed.
> 
> You'll run into encoding problems anyway in your programmer's life. I don't
> think it's a workaround, try this article:
> 
> http://www.joelonsoftware.com/articles/Unicode.html
> 
> I think it's highly useful, you could tell that to your students.

Please remember that most of the students will not become professional
programmers.  Personally I think that encoding problems are nightmare in
general, and in Python in particular, and would like to avoid explaining
it as longer as possible.  Besides, it won't be me, it will be a teacher
who will explain it, and the teacher themself might have a vague notion
about this topic.

> Eclipse+Pydev seems to work with that. I'm not able to check with other
> editors right now, but it seems you're experiencing a simple encoding
> problem; your editor doesn't know which encoding you'd like to use, so it
> defaults to ascii or iso-8859-1 leading to such problems.

No, my problem is that the emulation of sys.stdin/sys.stdout under
an IDE's interactive console doesn't work like the real
sys.stdin/sys.stdout under a real terminal.


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


failure building python 2.5 on mac os x 10.3.9

2006-11-19 Thread Thomas Ploch
Hello,

I followed the instructions in the Mac/README file.

I ran ./configure --enable-framework

But when I try to build from source with gcc 4.0.2, following happens:


[snip]
libtool: can't locate file for: -lSystemStubs
libtool: file: -lSystemStubs is not an object file (not allowed in a
library)
make: *** [Python.framework/Versions/2.5/Python] Error 1

What does that mean?

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


Re: lxml/ElementTree and .tail

2006-11-19 Thread Fredrik Lundh
Uche Ogbuji wrote:

 > The fact that the XML Infoset is hardly used outside W3C XML Schema,
 > and that the XPath data model is far more common, and that focus on
 > the serialization is even more common than that is a matter of
 > everyday practicality.

everyday interoperability problems, that is.  yesterday, someone 
reported a bug in Python's xml.dom because he couldn't get it to 
serialize the string " " as " ".  earlier today, someone
asked how to work around an XML parser that didn't understand
namespace prefixes.

> And oh by the way, this thread is all about *your* customer's
> complaining.

from what I can tell, it was *your* customer posting FUD about a 
different library, not my customer asking for help with a specific 
problem.  this is free software; people who use a piece of software 
count a *lot* more than people who don't want to use it.

 > This is not one fo those times, so this is probably where I get off.

I'll be looking forward to your next O'Reilly article.



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


Re: lxml/ElementTree and .tail

2006-11-19 Thread Chas Emerick
On Nov 19, 2006, at 9:55 AM, Fredrik Lundh wrote:

>> And oh by the way, this thread is all about *your* customer's
>> complaining.
>
> from what I can tell, it was *your* customer posting FUD about a
> different library, not my customer asking for help with a specific
> problem.  this is free software; people who use a piece of software
> count a *lot* more than people who don't want to use it.

Holy hell Fredrik -- I hadn't even *downloaded* 4suite before I  
posted my original question.  I've tried to be nice, tried to be  
complimentary, and tried to be diplomatic, so it would be nice if  
*everyone* would stop casting aspersions or otherwise speculating  
about my intentions.  Flame amongst yourselves, but leave me out of it.

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


Re: Re:A python IDE for teaching that supports cyrillic i/o

2006-11-19 Thread Kirill Simonov
On Sat, Nov 18, 2006 at 06:11:48PM -0800, PyScripter wrote:
> Kirill Simonov wrote:
> > PyScripter does, indeed, look nice, but unfortunately it appeared to
> > have similar issues with cyrillic support. Thank you anyway for the
> > suggestion.
> 
> 
> What are the issues?  PyScripter offers full support for utf-8 encoded
> files and PEP-263.  The editor internally is unicode based.Please
> read the PyScripter help topic "Encoded Python Source Files", paying
> special attention to the last paragraph, and if you follow the
> guidelines you should have no problem with cyrillic or other encodings.
>  If you have problems email [EMAIL PROTECTED] for support.

The issues are related to the emulation of sys.stdin and sys.stdout in
the interactive console (which is called Python Interpreter in
PyScripter).

In PyScripter,
>>> print "...some cyrillic characters..."
produces output as if an UTF-8 string is displayed in latin1.
On the other hand,
>>> print u"...some cyrillic characters..."
works correctly in PyScripter.  Both above examples works correctly in a
real console when sys.stdout.encoding is set to 'utf-8'.

raw_input() doesn't work at all with non-ASCII characters in PyScripter.
>>> raw_input("...some cyrillic characters...")
displays the label in latin1 while
>>> raw_input(u"...some cyrillic characters...")
produces UnicodeDecodeError.

Moreover, if I enter some cyrillic characters to the input box of
raw_input(), it returns a line of '?'. This might be related to the fact
that I use WinXP ENG (not RUS), but still I believe it's possible to
make it work even in this environment.

I'd like the emulated sys.stdin and sys.stdout to behave like the real
sys.stdin and sys.stdout do under a UTF-8 terminal when
sys.stdout.encoding is set to 'utf-8'.

Python 2.5, PyScripter 1.7.2.0.


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


Re: lxml/ElementTree and .tail

2006-11-19 Thread Fredrik Lundh
Uche Ogbuji wrote:

> The fact that the XML Infoset is hardly used outside W3C XML Schema,
> and that the XPath data model is far more common,

and for the bystanders, it should be noted that the Infoset is pretty 
much the same thing as the XPath data model; it's mostly just that the 
specifications use different names for the same concept.  if you cut 
through the vocabulary, it's all about a tree of elements, plus text and 
attributes and a few more (but usually less interesting) things.  it's a 
bit like arguing that

 class Person(object):
 __slots__ = ["name"]
 def __init__(self, name):
 self.name = name

and

 class Employee:
 def __init__(self, first_name, last_name):
 self.full_name = first_name + " " + last_name

and

 employee_name = "..."

are entirely different things, and not just three more or less con- 
venient ways to store exactly the same information.



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


A little confuse

2006-11-19 Thread [EMAIL PROTECTED]
When I run this code in the pdb it works.
 accountNbr = 1
 for testLine in ftest.readlines():
acct = testLine[1:2] #there account number
if accountNbr == int(acct):
accountNbr = accountNbr + 1

When I run without the debugger I get this error.

 File "./casco.py", line 62, in process
if accountNbr == int(acct):
ValueError: invalid literal for int(): "

Can someone explain?

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


Re: how to print pdf with python on a inkjet printer.

2006-11-19 Thread Thomas Heller
Gabriel Genellina schrieb:
> At Friday 17/11/2006 17:40, Tim Roberts wrote:
> 
>> > double wow!  as it is my customer wants me to print to the default
>> > printer.
>> > can you please help me with the command for rendering the pdf to the
>> > printer with acrobat using python?
>>
>>You'll have to use the registry to find "acrord32", but once you find
>>it, you just do:
>> os.system( "\\Program Files\\Adobe\\Acrobat 7.0\\Reader\acrord32.exe
>>/p xxx.pdf" )
>>The /p switch requests printing.
> 
> Or just let Windows do the dirty work of locating Adobe Reader, 
> figuring out the parameters and such:
> 
> import win32api
> win32api.ShellExecute(0, "print", path/to/document.pdf, None, None, 0)
> 
Note that in Pyhton2.5, os.startfile was extended to accept an optional
second parameter; so
  os.startfile(path/to/document.pdf, "print")
should also work.

Thomas

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


Re: Why isn't SPLIT splitting my strings

2006-11-19 Thread John Henry
Meaning:

import re

newString= re.split('[; ()\[\]", result)



Nick Vatamaniuc wrote:
> The regular string split does not take a _class_ of characters as the
> separator, it only takes one separator. Your example suggests that you
> expect that _any_ of the characters "; ()[]" could be a separator.
> If you want to use a regular expression as a list of separators, then
> you need to use the split function from the regular expression module
> (re):
> http://docs.python.org/lib/node46.html
>
> Hope this helps,
> Nick V.
>
>
> ronrsr wrote:
> > I'm trying to break up the result tuple into keyword phrases.  The
> > keyword phrases are separated by a ;  -- the split function is not
> > working the way I believe it should be. Can anyone see what I"m doing
> > wrong?
> >
> > bests,
> >
> > -rsr-
> >
> >
> >
> >  print "newstring =", str(result[i][0])
> > print "just split", str(result[i][0]).split('; ()[]"')
> > zd.append(str(result[i][0]).split('; ()[]"'))
> >
> >
> > result= (('Agricultural subsidies; Foreign aid',), ('Agriculture;
> > Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
> > Childhood Development, Birth Defects; Toxic Chemicals',),
> > ('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',),
> > ('Agricultural Subsidies',), ('Biodiversity',), ('Citizen Activism',),
> > ('Community Gardens',), ('Cooperatives',), ('Dieting',), ('Agriculture,
> > Cotton',), ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
> > ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
> > Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
> > Feed, Corn, Pesticides',), ('Aquaculture',), ('Chemical Warfare',),
> > ('Compost',), ('Debt',), ('Consumerism',), ('Fear',), ('Pesticides, US,
> > Childhood Development, Birth Defects',), ('Corporate Reform,
> > Personhood (Dem. Book)',), ('Corporate Reform,  Personhood, Farming
> > (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
> > Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
> > ('Democracy, Corporate
> >
> >
> >
> > partial results:
> >
> > string a =  Agricultural subsidies; Foreign aid
> > newstring = Agricultural subsidies; Foreign aid
> > just split ['Agricultural subsidies; Foreign aid']
> > newstring = Agriculture; Sustainable Agriculture - Support; Organic
> > Agriculture; Pesticides, US, Childhood Development, Birth Defects;
> > Toxic Chemicals
> > just split ['Agriculture; Sustainable Agriculture - Support; Organic
> > Agriculture; Pesticides, US, Childhood Development, Birth Defects;
> > Toxic Chemicals']
> > newstring = Antibiotics, Animals
> > just split ['Antibiotics, Animals']

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


Re: Why isn't SPLIT splitting my strings

2006-11-19 Thread John Henry
Oops!

newString= re.split("[; ()\[\]", result)

John Henry wrote:
> Meaning:
>
> import re
>
> newString= re.split('[; ()\[\]", result)
>
>
>
> Nick Vatamaniuc wrote:
> > The regular string split does not take a _class_ of characters as the
> > separator, it only takes one separator. Your example suggests that you
> > expect that _any_ of the characters "; ()[]" could be a separator.
> > If you want to use a regular expression as a list of separators, then
> > you need to use the split function from the regular expression module
> > (re):
> > http://docs.python.org/lib/node46.html
> >
> > Hope this helps,
> > Nick V.
> >
> >
> > ronrsr wrote:
> > > I'm trying to break up the result tuple into keyword phrases.  The
> > > keyword phrases are separated by a ;  -- the split function is not
> > > working the way I believe it should be. Can anyone see what I"m doing
> > > wrong?
> > >
> > > bests,
> > >
> > > -rsr-
> > >
> > >
> > >
> > >  print "newstring =", str(result[i][0])
> > > print "just split", str(result[i][0]).split('; ()[]"')
> > > zd.append(str(result[i][0]).split('; ()[]"'))
> > >
> > >
> > > result= (('Agricultural subsidies; Foreign aid',), ('Agriculture;
> > > Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
> > > Childhood Development, Birth Defects; Toxic Chemicals',),
> > > ('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',),
> > > ('Agricultural Subsidies',), ('Biodiversity',), ('Citizen Activism',),
> > > ('Community Gardens',), ('Cooperatives',), ('Dieting',), ('Agriculture,
> > > Cotton',), ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
> > > ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
> > > Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
> > > Feed, Corn, Pesticides',), ('Aquaculture',), ('Chemical Warfare',),
> > > ('Compost',), ('Debt',), ('Consumerism',), ('Fear',), ('Pesticides, US,
> > > Childhood Development, Birth Defects',), ('Corporate Reform,
> > > Personhood (Dem. Book)',), ('Corporate Reform,  Personhood, Farming
> > > (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
> > > Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
> > > ('Democracy, Corporate
> > >
> > >
> > >
> > > partial results:
> > >
> > > string a =  Agricultural subsidies; Foreign aid
> > > newstring = Agricultural subsidies; Foreign aid
> > > just split ['Agricultural subsidies; Foreign aid']
> > > newstring = Agriculture; Sustainable Agriculture - Support; Organic
> > > Agriculture; Pesticides, US, Childhood Development, Birth Defects;
> > > Toxic Chemicals
> > > just split ['Agriculture; Sustainable Agriculture - Support; Organic
> > > Agriculture; Pesticides, US, Childhood Development, Birth Defects;
> > > Toxic Chemicals']
> > > newstring = Antibiotics, Animals
> > > just split ['Antibiotics, Animals']

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


Re: A little confuse

2006-11-19 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> When I run this code in the pdb it works.
>  accountNbr = 1
>  for testLine in ftest.readlines():
> acct = testLine[1:2] #there account number
> if accountNbr == int(acct):
> accountNbr = accountNbr + 1
> 
> When I run without the debugger I get this error.
> 
>  File "./casco.py", line 62, in process
> if accountNbr == int(acct):
> ValueError: invalid literal for int(): "
> 
> Can someone explain?

adding a

  print repr(acct), repr(testLine)

debug statement after the "acct = testLine" line might give you the 
clues you need to solve this.



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


Re: how to print pdf with python on a inkjet printer.

2006-11-19 Thread km

i dont see os.startfile in python2.5 installation on my system :-(
KM

On 11/19/06, Thomas Heller <[EMAIL PROTECTED]> wrote:


Gabriel Genellina schrieb:
> At Friday 17/11/2006 17:40, Tim Roberts wrote:
>
>> > double wow!  as it is my customer wants me to print to the default
>> > printer.
>> > can you please help me with the command for rendering the pdf to the
>> > printer with acrobat using python?
>>
>>You'll have to use the registry to find "acrord32", but once you find
>>it, you just do:
>> os.system( "\\Program Files\\Adobe\\Acrobat
7.0\\Reader\acrord32.exe
>>/p xxx.pdf" )
>>The /p switch requests printing.
>
> Or just let Windows do the dirty work of locating Adobe Reader,
> figuring out the parameters and such:
>
> import win32api
> win32api.ShellExecute(0, "print", path/to/document.pdf, None, None, 0)
>
Note that in Pyhton2.5, os.startfile was extended to accept an optional
second parameter; so
  os.startfile(path/to/document.pdf, "print")
should also work.

Thomas

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

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

Re: Why isn't SPLIT splitting my strings

2006-11-19 Thread Fredrik Lundh
John Henry wrote:

> Oops!

for cases like this, writing

"[" + re.escape(charset) + "]"

is usually a good way to avoid repeated oops:ing.

 > newString= re.split("[; ()\[\]", result)

 >>> newString= re.split("[; ()\[\]", result)
Traceback (most recent call last):
...
sre_constants.error: unexpected end of regular expression



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


Re: Reloading after from adder import * ????

2006-11-19 Thread Seymour
John,
Thanks for the reply.  Just as a little further background, I am using
Windows XP and Komodo and just trying to learn about classes - how they
work, what they are used for, etc.  I have been studying "Learning
Python (2nd Ed)" by Mark Lutz and Davis Ascher and have been typing in
some of the exercises from Chapt 23, Advanced Class Topics (I tend to
learn better by re-typing them in).  What I posted was only a partial
snippet of the full solution for the Inheritance code solution given on
page 558 of the book, but really was not relevant to my question -
sorry.  I just made some typos and was wondering if there was an easier
way to clear the Python namespace at the interactive prompt rather than
shutting Komodo down and restarting (really brute force).  I finally
figured out the four step solution that I posted but was still
wondering if there was yet an easier way.

FYI, below is the full solution to the Inheritance class exercise in
the book.  Python is a hobby for me and I am not sure  how of if I will
ever use this stuff, but would like to understand it better.  That
said, if you have any follow up study source recommendations, I would
be interested.
Seymour

class Adder:
def add(self, x, y):
print 'not implemented!'
def __init__(self, start=[]):
self.data = start
def __add__(self, other):# Or in subclasses?
return self.add(self.data, other)# Or return type?

class ListAdder(Adder):
def add(self, x, y):
return x + y

class DictAdder(Adder):
def add(self, x, y):
new = {}
for k in x.keys(): new[k] = x[k]
for k in y.keys(): new[k] = y[k]
return new






John Machin wrote:
> Seymour wrote:
> > I created a module with the DictAdder subclass as follows:
> >
> > class DictAdder(Adder):
> > def add(self, x, y):
> > new={}
> > for k in x.keys(): new[k]=x[k]
> > for k in y.keys(): new[k]=y[k]
> > return new
> >
> > At the interactive prompt I then said: from Adder import *
> >
> > After defining an instance of DictAdder as x=DictAdder() and trying to
> > add two dictionaries, I notice a typo in DictAdder.  If I then go back
> > and make a change to the DictAdder subclass I have to go through many
> > steps to add two dictionaries (get the revision into active memory):
> > 1. import Adder
> > 2. Reload(Adder)
> > 3. from Adder import *
> > 4. x=DictAdder()
> >
> > Question: Is there an easier and quicker way to get DictAdder into
> > active memory?
>
> This is one of the occasions where semicolons can come in handy in
> Python:
>
> import adder; reload(adder); from adder import *
>
> Then depending on your OS and what add-on line editing kit (if any) you
> are using, it should only a very small number of keystrokes to retrieve
> that and execute it.
>
> If you meant "is there a single command?", the answer is no, not AFAIK.
>
> Having said that, you seem to have a strange idea about what classes
> are for -- "self" is not used; having such a class "method" seems
> pointless. All you need to do what you are doing  is a simple function,
> whose body can be simply expressed in terms of dict methods:
>
> def funnyfunc(x, y):
> """This is called a docstring; try to say what the func is doing
> for you"""
> new = x.copy()
> new.update(y)
> return new
>
> Of course that is probably not what you *want* to be doing; but you
> might need to divulge what the Adder class is doing to get more help.
>
> Hints: unlike some other languages,
>
> (1) You don't need to have 1 module file per class; for several small
> related classes (e.g. a main class and a handful of subclasses) it
> makes a lot of sense to put it all in one module.
>
> (2) To do most simple things, you don't even need to write a class at
> all. For example, you can write a few hundred lines of clear
> intelligible Python with very little must-recite-this-spell overhead to
> do quite complicated data manipulations using only (a) built-in
> functions (b) the methods of built-in types like strings, lists and
> dictionaries (c) functions and classes in built-in modules (d) ditto
> 3rd-party modules. In Python classes usually are things that map onto
> something in the real world or at least your model of that; you write
> them when you need to; classes are not artificial hoops you must jump
> through to do the equivalent of:
>print 2 + 2
> You may end up using classes to emulate some other programming
> constructs, but not yet.
> Eventually you can write those 3rd party modules which are chock-full
> of classes and other goodies, using them as a guide.
> 
> OK, relax, rant over :-)
> 
> HTH,
> John

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


Re: lxml/ElementTree and .tail

2006-11-19 Thread Damjan
> sure, the computing world is and has always been full of people who want
> the simplest thing to look a lot harder than it actually is.  after all,
> *they* spent lots of time reading all the specifications, they've bought
> all the books, and went to all the seminars, 

and have been sold all the expensive proprietary tools

> so it's simply not fair when others are cheating.

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


Re: Reloading after from adder import * ????

2006-11-19 Thread Fredrik Lundh
Seymour wrote:

 > I just made some typos and was wondering if there was an easier
> way to clear the Python namespace at the interactive prompt rather than
> shutting Komodo down and restarting (really brute force).

most IDE's have a "reset interactive mode" command (it's ctrl-F6 in 
IDLE, for example).

another approach is to put the code in a script file, and run that file. 
  scripts always run in a clean namespace.

yet another approach is to stop using wildcard import ("from import *"). 
  if you refer to the things in Adder via the Adder namespace, you can 
just do

 reload(Adder)

to fetch the new version.

("from import *" is generally considered a "only use if you know what 
you're doing" construct in Python, so if it causes trouble for you, you 
shouldn't have used it in the first place, per definition ;-)



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


Re: How do I stop Python IDLE (GUI) from immediately exiting when I enter it?

2006-11-19 Thread Hertha Steck
Am Sat, 18 Nov 2006 23:24:08 -0800 schrieb John (Z R) L:

> Hi all, I am very new to programming, and I chose to study the Python
> language before C++. I am currently using the Wikibooks
> "Non-Programmer's Tutorial for Python", and am up to the section "Who
> goes there"?
> 
> http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python/Who_Goes_There%3F
> 
> But after clicking "run module" for
> "
> a = 1
> print a
> a = a + 1
> print a
> a = a * 2
> print a "
> 
> The results "1, 2, 4" didn't appear on the GUI screen, in fact nothing
> did. And I clicked twice again, this time, it exited the Python GUI
> program. I tried to enter back in, but it would quickly display
> 
> "1
> 2
> 4"
> 
> before exiting immediately within half a second. How do I stop the this
> exiting from occurring.
> 

All of this using IDLE as your GUI (not PythonWin and not the python
interpreter by itself), right?

You did all this as explained at the start of the tutorial: open a "New
window", type the code into this empty window, save it to a reasonable
place and then click "Run / Run module". Right? (Silly question: there is
no run menu in the interactive window. On the other hand, you never know.)

What about the examples before this one, did you type, save and run them
just like this one? And did they work as you expected?

What version of Python and of IDLE do you use?

If you have PythonWin (it's part of ActiveState Python, for example): what
happens, if you run your script using that? What happens if you open a
command window, go to the right directory and run your script from there?
(Don't try to start it by double clicking in the explorer: it will run,
but the window will close immediately afterwards, and you won't see
anything.)

> Another problem I have is firewall. On my old computer (Windows 98)
> when using Python GUI, it can't run modules because of some firewall.
> But I thought firewalls were for internet sites only?! How do I fix
> this??
> 

Do you mean this?


Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface.  This connection is not visible on any external
interface and no data is sent to or received from the Internet.


This shouldn't keep any script from running. What exactly happens if you
try? 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: About alternatives to Matlab

2006-11-19 Thread sturlamolden

sturlamolden wrote:

> def D4_Transform(x, s1=None, d1=None, d2=None):
>"""
>D4 Wavelet transform in NumPy
>(C) Sturla Molden
>"""
>C1 = 1.7320508075688772
>C2 = 0.4330127018922193
>C3 = -0.066987298107780702
>C4 = 0.51763809020504137
>C5 = 1.9318516525781364
>if d1 == None:
>   d1 = numpy.zeros(x.size/2)
>   s1 = numpy.zeros(x.size/2)
>   d2 = numpy.zeros(x.size/2)
>odd = x[1::2]
>even = x[:-1:2]
>d1[:] = odd[:] - C1*even[:]
>s1[0] = even[0] + C2*d1[0] + C3*d1[-1]   #typ0
>s1[1:] = even[1:] + C2*d1[1:] + C3*d1[:-1]   #typo
>d2[0] = d1[0] + s1[-1]
>d2[1:] = d1[1:] + s1[:-1]
>even[:] = C4 * s1[:]
>odd[:] = C5 * d2[:]
>if x.size > 2:
>D4_Transform(even,s1[0:even.size/2],
>d1[0:even.size/2],d2[0:even.size/2])


Actually, there was a typo in the original code. I used d1[l-1] where I
should have used d1[l+1]. Arrgh. Here is the corrected version, the
Matlab code must be changed similarly. It has no relevance for the
performance timings though.




def D4_Transform(x, s1=None, d1=None, d2=None):
"""
D4 Wavelet transform in NumPy
(C) Sturla Molden
"""
C1 = 1.7320508075688772
C2 = 0.4330127018922193
C3 = -0.066987298107780702
C4 = 0.51763809020504137
C5 = 1.9318516525781364
if d1 == None:
   d1 = numpy.zeros(x.size/2)
   s1 = numpy.zeros(x.size/2)
   d2 = numpy.zeros(x.size/2)
odd = x[1::2]
even = x[:-1:2]
d1[:] = odd[:] - C1*even[:]
s1[:-1] = even[:-1] + C2*d1[:-1] + C3*d1[1:]
s1[-1] = even[-1] + C2*d1[-1] + C3*d1[0]
d2[0] = d1[0] + s1[-1]
d2[1:] = d1[1:] + s1[:-1]
even[:] = C4 * s1[:]
odd[:] = C5 * d2[:]
if x.size > 2:
D4_Transform(even,s1[0:even.size/2],
d1[0:even.size/2],d2[0:even.size/2])

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


Re: basic python questions

2006-11-19 Thread [EMAIL PROTECTED]
I normaly try to be as resourceful as I can. I find that newgroups give
a wide range of answers and solutions to problems and you get a lot
responses to what is the right way to do things and different point of
views about the language that you can't find in help manuals. I also
want to thank everyone for being so helpful in this group, it has been
one of the better groups that I have used.


John Machin wrote:
> Paddy wrote:
> > John Machin wrote:
> >
> >
> > > [Aside] How are you going to explain all this to your instructor, who
> > > may be reading all this right now?
> > >
> >
> > The instructor should be proud!
> > He has managed to do his very first post to a this newsgroup, about a
> > homework question, and do it in the right way. that is no mean feat.
> >
> > - Paddy.
>
> In fact, he may well by now know more than his instructor, and be
> explaining the finer points of Python :-)

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


Re: decompiler

2006-11-19 Thread John Bokma
Bjoern Schliessmann <[EMAIL PROTECTED]> wrote:

> Fuzzyman wrote:
> 
>> That's terrible. You read comp.lang.perl.misc *as well* ?
>> 
>> ;-)
> 
> You have to know your enemy ;)

LOL :-)

I am a Perl programmer who wants to keep up his Perl skills, and to study 
Python.

-- 
John   MexIT: http://johnbokma.com/mexit/
   personal page:   http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why isn't SPLIT splitting my strings

2006-11-19 Thread John Henry

Fredrik Lundh wrote:
> John Henry wrote:
>
> > Oops!
>
> for cases like this, writing
>
>   "[" + re.escape(charset) + "]"
>

So, like this?

newString= re.split("[" + re.escape("; ()[]") + "]", result)

> is usually a good way to avoid repeated oops:ing.
>
>  > newString= re.split("[; ()\[\]", result)
>
>  >>> newString= re.split("[; ()\[\]", result)
> Traceback (most recent call last):
> ...
> sre_constants.error: unexpected end of regular expression
>

Strange.  It works for me.  Oh, I know, it's missing a "]".

newString= re.split("[; ()\[\]]", result)

> 

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


[ANN] clnum-1.4 Class Library For Numbers Python Binding

2006-11-19 Thread Raymond L. Buvel
The clnum package adds rational numbers and arbitrary precision floating
point numbers in real and complex form to Python. Also provides
arbitrary precision floating point replacements for the functions in the
math and cmath standard library modules.

Home page: http://calcrpnpy.sourceforge.net/clnum.html

Changes in 1.4

* Fixed anomaly where an exact zero was converted using default
precision in multiply and divide routines.

* Added function to approximate a number as a rational with the size of
the numerator and denominator constrained.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] ratfun-2.4 Polynomials and Rational Functions

2006-11-19 Thread Raymond L. Buvel
The ratfun module provides classes for defining polynomial and rational
function (ratio of two polynomials) objects. These objects can be used
in arithmetic expressions and evaluated at a particular point.

Home page:  http://calcrpnpy.sourceforge.net/ratfun.html

Note: If you are using rpncalc-1.2 or later, this module is already
included.  This release is for folks who don't want rpncalc.

Changes in 2.4

* Updated the included clnum package.

* Added uniqueRoots to handle polynomials with multiple roots.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] rpncalc-2.5 RPN Calculator for Python

2006-11-19 Thread Raymond L. Buvel
The rpncalc package adds an interactive Reverse Polish Notation (RPN)
interpreter to Python.  This interpreter allows the use of Python as
an RPN calculator.  You can easily switch between the RPN interpreter
and the standard Python interpreter.

Home page:  http://calcrpnpy.sourceforge.net/

Changes in 2.5

* Update the included clnum package.

* Added ratapx and unique_roots commands.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: decompiler

2006-11-19 Thread Skip Montanaro
> You have to know your enemy ;)

We have met the enemy and they are us.
http://www.igopogo.com/we_have_met.htm  ;-)

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


Re: tempfile.NamedTemporaryFile wont work

2006-11-19 Thread Imbaud Pierre
Steven D'Aprano a écrit :
> On Sun, 19 Nov 2006 13:18:39 +0100, Bjoern Schliessmann wrote:
> 
> 
>>Imbaud Pierre wrote:
>>
>>
>>> tf = tempfile.NamedTemporaryFile()
>>> tfName = tf.name
>>>[...]
>>> print >> sys.stderr, '%s: %s' % (tfName, ['no',
>>>'yes'][os.path.exists(tfName)])
>>> subprocess.Popen(['strings', tfName])
>>
>>Just out of curiosity: Why did you assign tf.name to tfname?
>>
>>Hypothetically, if tf.name changed, tfname wouldn't follow since
>>strings are immutable.
> 
> 
> Well, yes, but if tf.name changed, that won't change the file name on disk
> either:
> 
> 
tf = tempfile.NamedTemporaryFile()
tf.name
> 
> '/tmp/tmpYVV1Ij'
> 
os.path.exists(tf.name)
> 
> True
> 
oldname = tf.name
tf.name = "/tmp/something"
os.path.exists(tf.name)
> 
> False
> 
os.path.exists(oldname)
> 
> True
> 
> 
> I'm guessing that binding tf.name to tfName is a micro-optimization.
indeed. And I dont see why tf.name would change.
  In a
> very tight loop, name lookups can take considerable time, and one
> optimization can be to reduce the number of lookups:
> 
> method = something.method
> while 1:
> something.method # needs at least two lookups
> method # needs a single lookup
> 
> 

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


Re: tempfile.NamedTemporaryFile wont work

2006-11-19 Thread Imbaud Pierre
Peter Otten a écrit :
> Steven D'Aprano wrote:
> 
> 
>>On Sun, 19 Nov 2006 13:11:13 +0100, Imbaud Pierre wrote:
>>
>>
>>>On suse 9.3, tempfile.NamedTemporaryFile() doesnt work as expected.
>>
>>[snip]
>>
>>
>>>Symptom: the file does not always exist, after the call to
>>>NamedTemporaryFile(). Or at least its not seen by the strings command,
>>>or by os.path.exists.
>>>
>>>I guess the bug is pretty much os dependent, or even filesystem
>>>dependent (Im on reiserfs). Maybe the os is buggy, maybe, somehow, the
>>>python interface. Or did I miss something?
>>>Shame, I didnt even try to check for a python bug tracker.
>>
>>I can verify this problem occurs on Fedora Core 5 too:
>>
>>import os
>>import sys
>>import tempfile
>>import subprocess
>>def test(n):
>>chunk = ': +++ abcd +++'
>>for i in xrange(n):
>>tf = tempfile.NamedTemporaryFile()
>>tfName = tf.name
>>tf.seek(0)
>>tf.write(str(i) + chunk)
>>tf.flush()
>>if not os.path.exists(tfName):
>>print 'pre-check: %s not there' % tfName
>>subprocess.Popen(['strings', tfName])
>>if not os.path.exists(tfName):
>>print 'post-check: %s not there' % tfName
>>
>>
>>And here is a typical run, with the boring bits removed for ease of
>>reading:
>>
>>
>test(30)
>>
>>0: +++ abcd +++
>>1: +++ abcd +++
>>[ more of the same ]
>>14: +++ abcd +++
>>strings: '/tmp/tmpOALbx9': No such file
>>16: +++ abcd +++
>>17: +++ abcd +++
>>18: +++ abcd +++
>>[ more of the same ]
>>27: +++ abcd +++
>>strings: /tmp/tmpdc52Nz: No such file or directory
>>29: +++ abcd +++
>>
>>
>>Curiouser and curiouser... not only does os.path.exist always report the
>>temp file as existing (at least in my tests), even when strings can't find
>>it, but strings returns different error messages.
>>
>>Is it possible this is a bug in strings?
> 
> 
> What /you/ are seeing is not a bug, I think. Popen() is asynchronous,
> therefore you may enter the second iteration -- which implicitly closes the
> temporary file -- before strings actually tries to access it. Use call()
> and everything should be fine.
Thanks A LOT, works fine, I feel kind of silly; your diagnostic is
pretty obvious, afterward... I felt uneasy not closing the Popen, but
it worked, so why bother? Its so easy to make ugly code!

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


Re: ElementTree: namespace declaration in each element?

2006-11-19 Thread cyberco
> written by a "it's the bytes, not the data" guy, obviously ;-)

hehehe...I figured it was. Wonder 'who' it was... ;)

> the standard serializer in 1.2 doesn't support default namespaces, so
> the quickest way to do this is to loop over all tags (use getiterator),
> strip off the "{http://xspf.org/ns/0/}"; prefix, add an xmlns attribute
> to the root (if necessary), and write it out.

OK, that's what I was already doing. Thanks.
Will the standard serializer in 1.3 support default namespaces?

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


Re: os.lisdir, gets unicode, returns unicode... USUALLY?!?!?

2006-11-19 Thread gabor
Martin v. Löwis wrote:
> gabor schrieb:
>> depends on the application. in the one where it happened i would just
>> display an error message, and tell the admins to check the
>> filesystem-encoding.
>>
>> (in other ones, where it's not critical to get the correct name, i would
>> probably just convert the text to unicode using the "replace" behavior)
>>
>> what about using flags similar to how unicode() works? strict, ignore,
>> replace and maybe keep-as-bytestring.
>>
>> like:
>> os.listdir(dirname,'strict')
>>
>> i know it's not the most elegant, but it would solve most of the
>> use-cases imho (at least my use-cases).
> 
> Of course, it's possible to implement this on top of the existing
> listdir operation.
> 
> def failing_listdir(dirname, mode):
>   result = os.listdir(dirname)
>   if mode != 'strict': return result
>   for r in result:
> if isinstance(r, str):
>   raise UnicodeDecodeError
>   return result
> 

yes, sure... but then.. it's possible to implement it also on top of an 
raise-when-error version :)

so, what do you think, how should this issue be solved?

currently i see 2 ways:

1. simply fix the documentation, and state that if the file-name cannot 
be decoded into unicode, then it's returned as byte-string. but that 
also means, that the typical usage of:

[os.path.join(path,n) for n in os.listdir(path)]

will not work.

2. add support for some unicode-decoding flags, like i wrote before

3. some solution.

?

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

Re: ElementTree: namespace declaration in each element?

2006-11-19 Thread Fredrik Lundh
cyberco wrote:

> OK, that's what I was already doing. Thanks.
> Will the standard serializer in 1.3 support default namespaces?

that's the plan.  I've been working on a more modular XML writer, which 
will make it easier to tweak the output in various ways, without having 
to write everything from scratch.



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


Re: os.lisdir, gets unicode, returns unicode... USUALLY?!?!?

2006-11-19 Thread Fredrik Lundh
gabor wrote:

> yes, sure... but then.. it's possible to implement it also on top of an 
> raise-when-error version :)

not necessarily if raise-when-error means raise-error-in-os-listdir.



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


Re: os.lisdir, gets unicode, returns unicode... USUALLY?!?!?

2006-11-19 Thread gabor
Fredrik Lundh wrote:
> gabor wrote:
> 
>> yes, sure... but then.. it's possible to implement it also on top of 
>> an raise-when-error version :)
> 
> not necessarily if raise-when-error means raise-error-in-os-listdir.
> 

could you please clarify?

currently i see 2 approaches how to do it on the raise-when-error version:

1.
dirname = u'something'
try:
files = os.listdir(dirname)
except UnicodeError:
byte_files = os.listdir(dirname.encode('encoding))
#do something with it

2.

dirname = u'something'
byte_files = os.listdir(dirname.encode('encoding'))
for byte_file in byte_files:
try:
file = byte_file.decode(sys.getfsenc())
except UnicodeError:
#do something else
#do something


the byte-string version of os.listdir remains. so all the other versions 
can be implemented on the top of it. imho the question is:
which should be the 'default' behavior, offered by the python standard 
library.

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


Re: About alternatives to Matlab

2006-11-19 Thread Filip Wasilewski
sturlamolden wrote:
[...]
> Here is the correct explanation:
>
> The factorization of the polyphase matrix is not unique. There are
> several valid factorizations. Our implementations corresponds to
> different factorizations of the analysis and synthesis poyphase
> matrices, and both are in a sence correct, although numerically
> different.

Please correct me if I'm missing something but I'm pretty sure that
choice of polyphase matrix factorization for given wavelet filter bank
has influence only on speed and numerical stability of computation and
not on the result itself. Particularly it should be possible to
reconstruct analysis and synthesis filters from polyphase matrices
regardless of the chosen factorization and both the discrete wavelet
transform and the wavelet lifting scheme should give corresponding
results for chosen wavelet (one can be rewritten in the form of other).

cheers,
fw

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


Problem with icon on menubar

2006-11-19 Thread Vyz

I have given icon like this to main frame of my program
ib=wx.IconBundle()
ib.AddIconFromFile("SriPadma.ico",wx.BITMAP_TYPE_ANY)
self.SetIcons(ib

and then I have made binary with py2exe and in setup file mentioned it
as icon resource.

windows = [
{
"script": "SriPadmagui.pyw",
"icon_resources": [(1, "SriPadma.ico")]
}

and made an setup.exe and installed the binary i have made.
the first time titlebar icon is recognised but from the next time it
would ask for sripadma.ico

The icon file contains all three sizes required for various things.
16x16 32x32 and 48x48. desktop icon and programs menu icons are working
fine.

Any suggestions are appreciated
Thanks
vyz

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


Re: About alternatives to Matlab

2006-11-19 Thread Filip Wasilewski
sturlamolden wrote:
>
> Actually, there was a typo in the original code. I used d1[l-1] where I
> should have used d1[l+1]. Arrgh. Here is the corrected version, the
> Matlab code must be changed similarly. It has no relevance for the
> performance timings though.
>
>
> def D4_Transform(x, s1=None, d1=None, d2=None):
> """
> D4 Wavelet transform in NumPy
> (C) Sturla Molden
> """
> C1 = 1.7320508075688772
> C2 = 0.4330127018922193
> C3 = -0.066987298107780702
> C4 = 0.51763809020504137
> C5 = 1.9318516525781364
> if d1 == None:
>d1 = numpy.zeros(x.size/2)
>s1 = numpy.zeros(x.size/2)
>d2 = numpy.zeros(x.size/2)
> odd = x[1::2]
> even = x[:-1:2]
> d1[:] = odd[:] - C1*even[:]
> s1[:-1] = even[:-1] + C2*d1[:-1] + C3*d1[1:]
> s1[-1] = even[-1] + C2*d1[-1] + C3*d1[0]
> d2[0] = d1[0] + s1[-1]
> d2[1:] = d1[1:] + s1[:-1]
> even[:] = C4 * s1[:]
> odd[:] = C5 * d2[:]

If you swap C4 with C5 then our solutions give identical results and
both match the classic dwt approach:

even[:] = C5 * s1[:]
odd[:] = C4 * d2[:]

> if x.size > 2:
> D4_Transform(even,s1[0:even.size/2],
> d1[0:even.size/2],d2[0:even.size/2])

cheers,
fw

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


a few extensions for the itertools

2006-11-19 Thread Mathias Panzenboeck
I wrote a few functions which IMHO are missing in python(s itertools).

You can download them here:
http://sourceforge.net/project/showfiles.php?group_id=165721&package_id=212104

A short description to all the functions:

icmp(iterable1, iterable2) -> integer
Return negative if iterable1 < iterable2,
zero if iterable1 == iterable1,
positive if iterable1 > iterable1.

isum(iterable, start=0) -> value
Returns the sum of the elements of a iterable
plus the value of parameter 'start'.  When the
iterable is empty, returns start.


iproduct(iterable, start=0) -> value
Returns the product of the elements of a iterable
times the value of parameter 'start'.  When the
iterable is empty, returns start.


forall(predicate, iterable, default=True) -> bool
Returns True, when for all elements x in iterable
predicate(x) is True. When the iterable is empty,
returns default.


forany(predicate, iterable, default=False) -> bool
Returns True, when for any element x in iterable
predicate(x) is True. When the iterable is empty,
returns default.


take(n,iterable) -> iterator
returns a iterator over the first n
elements of the iterator


drop(n,iterable) -> iterable
drops the first n elemetns of iterable and
return a iterator over the rest


heads(iterable) -> iterator over all heads
example:
for head in heads([1,2,3,4,5,6,7,8,9]):
print head

output:
[]
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]


tails(iterable) -> iterator over all tails
example:
for tail in tails([1,2,3,4,5,6,7,8,9]):
print tail

output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7, 8, 9]
[4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
[6, 7, 8, 9]
[7, 8, 9]
[8, 9]
[9]
[]


fcain(funct,*functs) -> function(...,***)
fcain(f1,f2,...,fn)(*args,*kwargs) equals f1(f2(...fn(*args,*kwargs)))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Programmatically finding "significant" data points

2006-11-19 Thread Paul McGuire
"robert" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> erikcw wrote:
>> Hi all,
>>
>> I have a collection of ordered numerical data in a list.  The numbers
>> when plotted on a line chart make a low-high-low-high-high-low (random)
>> pattern.  I need an algorithm to extract the "significant" high and low
>> points from this data.
>>
>> Here is some sample data:
>> data = [0.10, 0.50, 0.60, 0.40, 0.39, 0.50, 1.00, 0.80, 0.60, 1.20,
>> 1.10, 1.30, 1.40, 1.50, 1.05, 1.20, 0.90, 0.70, 0.80, 0.40, 0.45, 0.35,
>> 0.10]
>>
>> In this data, some of the significant points include:
>> data[0]
>> data[2]
>> data[4]
>> data[6]
>> data[8]
>> data[9]
>> data[13]
>> data[14]
>> 
>>
>> How do I sort through this data and pull out these points of
>> significance?

Using zip and map, it's easy to compute first and second derivatives of a 
time series of values.  The first lambda computes 


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


Re: Programmatically finding "significant" data points

2006-11-19 Thread Paul McGuire
... dang touchy keyboard!

> Here is some sample data:
> data = [0.10, 0.50, 0.60, 0.40, 0.39, 0.50, 1.00, 0.80, 0.60, 1.20,
> 1.10, 1.30, 1.40, 1.50, 1.05, 1.20, 0.90, 0.70, 0.80, 0.40, 0.45, 0.35,
> 0.10]
>
> In this data, some of the significant points include:
> data[0]
> data[2]
> data[4]
> data[6]
> data[8]
> data[9]
> data[13]
> data[14]

Using the first derivative, and looking for sign changes, finds many of the 
values you marked as "significant".

-- Paul


data = [0.10, 0.50, 0.60, 0.40, 0.39, 0.50, 1.00, 0.80, 0.60, 1.20,
1.10, 1.30, 1.40, 1.50, 1.05, 1.20, 0.90, 0.70, 0.80, 0.40, 0.45, 0.35,
0.10]

delta = lambda (x1,x2) : x2-x1
dy_dx =[0]+map(delta,zip(data,data[1:]))
d2y_dx2 = [0]+map(delta,zip(dy_dx,dy_dx[1:]))

sgnChange = lambda (x1,x2) : x1*x2<0
sigs = map(sgnChange,zip(dy_dx,dy_dx[1:]))
print [i for i,v in enumerate(sigs) if v]
[2, 4, 6, 8, 9, 10, 13, 14, 15, 17, 18, 19, 20] 


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


Re: a few extensions for the itertools

2006-11-19 Thread Paul McGuire
"Mathias Panzenboeck" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>I wrote a few functions which IMHO are missing in python(s itertools).
>
> You can download them here:
> http://sourceforge.net/project/showfiles.php?group_id=165721&package_id=212104
>
> A short description to all the functions:
>
Just a couple of questions:

> iproduct(iterable, start=0) -> value
> Returns the product of the elements of a iterable
> times the value of parameter 'start'.  When the
> iterable is empty, returns start.
>
Wouldn't 1 be a better default value for start?

> forall(predicate, iterable, default=True) -> bool
> Returns True, when for all elements x in iterable
> predicate(x) is True. When the iterable is empty,
> returns default.
>
>
> forany(predicate, iterable, default=False) -> bool
> Returns True, when for any element x in iterable
> predicate(x) is True. When the iterable is empty,
> returns default.
>
How are these different from all and any in Python 2.5?

-- Paul


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


Re: a few extensions for the itertools

2006-11-19 Thread Steven D'Aprano
On Sun, 19 Nov 2006 21:35:24 +0100, Mathias Panzenboeck wrote:

> I wrote a few functions which IMHO are missing in python(s itertools).
> 
> You can download them here:
> http://sourceforge.net/project/showfiles.php?group_id=165721&package_id=212104
> 
> A short description to all the functions:
> 
> icmp(iterable1, iterable2) -> integer
>   Return negative if iterable1 < iterable2,
>   zero if iterable1 == iterable1,
>   positive if iterable1 > iterable1.


What does it mean for an iterable to be less than another iterable? That
it has fewer items? How do these two iterables compare?

iter([1, 2, None, "foo", 3+2j])

def ones():
while 1:
yield 1

Which is smaller?



> isum(iterable, start=0) -> value
>   Returns the sum of the elements of a iterable
>   plus the value of parameter 'start'.  When the
>   iterable is empty, returns start.


You mean just like the built-in sum()?

>>> sum(xrange(12), 1000)
1066


> iproduct(iterable, start=0) -> value
>   Returns the product of the elements of a iterable
>   times the value of parameter 'start'.  When the
>   iterable is empty, returns start.

If I recall, product() was requested about the same time that sum() was
introduced, and Guido rejected it as a built-in because it was really only
useful for calculating geometric means, and it is easy to do if you need
it:

def product(it, start=1):
# default value of 1 is more sensible than 0
# 1 is the multiplicative identity
p = start
for x in it:
p *= x
return p


> forall(predicate, iterable, default=True) -> bool
>   Returns True, when for all elements x in iterable
>   predicate(x) is True. When the iterable is empty,
>   returns default.
> 
> 
> forany(predicate, iterable, default=False) -> bool
>   Returns True, when for any element x in iterable
>   predicate(x) is True. When the iterable is empty,
>   returns default.


I vaguely recall plans for all() and any() builtins -- perhaps for Python
2.5?


> take(n,iterable) -> iterator
>   returns a iterator over the first n
>   elements of the iterator

Just like itertools.islice(iterable, n).

>>> list(itertools.islice(xrange(10), 5))
[0, 1, 2, 3, 4]


> drop(n,iterable) -> iterable
>   drops the first n elemetns of iterable and
>   return a iterator over the rest

Just like itertools.islice(iterable, n, None)

>>> list(itertools.islice(xrange(20), 15, None))
[15, 16, 17, 18, 19]

(Aside: I think islice would be so much cleaner if it took keyword
arguments.)



> heads(iterable) -> iterator over all heads
> tails(iterable) -> iterator over all tails

What would you use these for?


> fcain(funct,*functs) -> function(...,***)
>   fcain(f1,f2,...,fn)(*args,*kwargs) equals f1(f2(...fn(*args,*kwargs)))


The usual term for this is function composition.


-- 
Steven.

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


Re: Overwrite only one function with property()

2006-11-19 Thread Bruno Desthuilliers
Kai Kuehne a écrit :
> Hi list!
> It is possible to overwrite only one function with the property-function?

property is not function, it's a class. And it doesn't "overwrite" anything.

> x = property(getx, setx, delx, 'doc')
> 
> I just want to overwrite setx, but when I set the others to None,
> I can't read and del the member. 

You don't "overwrite" setx, you pass it as an argument to the property 
constructor call.

> Any ideas or is this not possible?

Read this, and you'll have a detailed answer:
http://users.rcn.com/python/download/Descriptor.htm


> Thank you!
> Kai
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I stop Python IDLE (GUI) from immediately exiting when I enter it?

2006-11-19 Thread John (Z R) L
Thanks for the replies so far. I do exactly what that website says, and
on the old computer (Windows 98), I click run module and nothing
happens. No text gets displayed on my IDLE. It contained that firewall
message at the top.

Back on the new computer, I deleted all of my .py files and I could go
back to the Python IDLE (GUI) again.

But my problem is with that when I run this module:
"a = 1
print a
a = a + 1
print a
a = a * 2
print a"  ,

the "1
2
4"
appears on the screen, but the program doesn't end. You don't get a new
">>>" like when you finish running all the other modules. So when I
want to run other modules, it says:
 "The Python Shell Window is already executing a command; please wait
until it is finished."

Note that all of my modules don't need any "end" at the last line, nor
do I need "raw_input" to end the program.

How do I make it so this module ends without using the word "end"
because it would exit the entire IDLE Shell?

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


Re: basic python questions

2006-11-19 Thread Bruno Desthuilliers
Diez B. Roggisch a écrit :
> [EMAIL PROTECTED] schrieb:
> 
>> I have taken the coments and think I have implemented most. My only
> 
> 
> Unfortunately, no.
> 
>> question is how to use the enumerator. Here is what I did, I have tried
>> a couple of things but was unable to figure out how to get the line
>> number.
>>
>> def Xref(filename):
>> try:
>> fp = open(filename, "r")
>> except:
>> raise "Couldn't read input file \"%s\"" % filename
> 
> 
> You still got that I-catch-all-except in there.
> This will produce subtle bugs when you e.g. misspell a variable name:
> 
> filename = '/tmp/foo'
> try:
>f = open(fliename, 'r')
> except:
>raise "can't open filename"
> 
> 
> Please notice the wrong-spelled 'fliename'.
> 
> This OTOH will give you more clues on what really goes wrong:
> 
> 
> 
> filename = '/tmp/foo'
> try:
>f = open(fliename, 'r')
> except IOError:
>raise "can't open filename"
> 
> 

And this would be still more informative (and not deprecated...):

filename = '/tmp/foo'
f = open(fliename)

Catching an exception just to raise a less informative one is somewhat 
useless IMHO.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Replace only if whole word?

2006-11-19 Thread Leonhard Vogt
Michael Yanowitz schrieb:
>   Yeah, I am operating this on a Python script. However, I am working off
> a requirement that the script be pre-processed and the strings replaced
> before executing the script and that if there are any remaining (not
> replaced)
> names that I don't execute the script and report that some 'mnemonics' have
> not been replaced.

If you control the source(template), you could use string formatting.
http://docs.python.org/lib/typesseq-strings.html

You would have to rewrite your MNEMONIC as %(MNEMONIC)s .

An exception will be raised if you miss a replacement and you are sure
you do not replace anything by accident.

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


Re: Python v PHP for web, and restarting Apache?

2006-11-19 Thread Bruno Desthuilliers
walterbyrd a écrit :
> Fredrik Lundh wrote:
> 
> 
>>modularity, modularity, and modularity.
>>
> 
> 
> Can't PHP be made to be just as modular?

PHP has no notion of modules.

> As a matter of popular practise, I suppose that is not done. I would
> think that it could be.

Certainly not the way Python is modular...

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


Re: A python IDE for teaching that supports cyrillic i/o

2006-11-19 Thread Leo Kislov

Kirill Simonov wrote:
> On Sun, Nov 19, 2006 at 03:27:32AM -0800, Leo Kislov wrote:
> > IDLE on Windows works fine for your example in interactive console:
> >
> > >>> name = raw_input("What's your name? ")
>
> Have you tried to use cyrillic characters in a Python string in
> interactive console? When I do it, I get the "Unsupported characters in
> input" error. For instance,
>
> >>> print "Привет"  # That's "Hi" in Russian.
> Unsupported characters in input

That works for me in Win XP English, with Russian locale and Russian
language for non-unicode programs. Didn't you say you want to avoid
unicode? If so, you need to set proper locale and language for
non-unicode programs.

>
> > And another question: are you aware of the fact that recommended way to
> > handle non-ascii characters is to use unicode type? Most of IDEs should
> > work fine with unicode.
>
> Usually using unicode type gives you much more headache than benefits
> unless you are careful enough to never mix unicode and str objects.

For a professional programmer life is full of headaches like this :)
For high school students it could be troublesome and annoying, I agree.


> Anyway, I just want the interactive console of an IDE to behave like a
> real Python console under a UTF-8 terminal (with sys.stdout.encoding ==
> 'utf-8').

Do you realize that utf-8 locale makes len() function and slicing of
byte strings look strange for high school students?

hi = u"Привет".encode("utf-8")
r = u"р".encode("utf-8")
print len(hi)# prints 12
print hi[1] == r   # prints False
for char in hi:
print char  # prints garbage

As I see you have several options:
1. Set Russian locale and Russian language for non-unicode programs on
Windows.
2. Introduce students to unicode.
3. Wait for python 3.0
4. Hack some IDE to make unicode friendly environment like unicode
literals by default, type("Привет") == unicode, unicode
stdin/stdout, open() uses utf-8 encoding by default for text files,
etc...

   -- Leo

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

edit text in a file and save it again

2006-11-19 Thread cyberco
I must be overlooking something here... I'm trying to edit a line in a
text file. I thought this was easy with fileinput, but all examples do
not write the line back to the file but simply 'print' it. I want to
open the file, edit the line and save it again. Is fileinput the right
module for that?

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


Re: os.lisdir, gets unicode, returns unicode... USUALLY?!?!?

2006-11-19 Thread Martin v. Löwis
gabor schrieb:
> 1. simply fix the documentation, and state that if the file-name cannot
> be decoded into unicode, then it's returned as byte-string. 

For 2.5, this should be done. Contributions are welcome.

[...then]
> [os.path.join(path,n) for n in os.listdir(path)]
> 
> will not work.
> 
> 2. add support for some unicode-decoding flags, like i wrote before

I may have missed something, but did you present a solution that would
make the case above work?

> 3. some solution.

One approach I had been considering is to always make the decoding
succeed, by using the private-use-area of Unicode to represent bytes
that don't decode correctly.

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.lisdir, gets unicode, returns unicode... USUALLY?!?!?

2006-11-19 Thread gabor
Martin v. Löwis wrote:
> gabor schrieb:
>> 1. simply fix the documentation, and state that if the file-name cannot
>> be decoded into unicode, then it's returned as byte-string. 
> 
> For 2.5, this should be done. Contributions are welcome.
> 
> [...then]
>> [os.path.join(path,n) for n in os.listdir(path)]
>>
>> will not work.
>>
>> 2. add support for some unicode-decoding flags, like i wrote before
> 
> I may have missed something, but did you present a solution that would
> make the case above work?

if we use the same decoding flags as binary-string.decode(),
then we could do:

[os.path.join(path,n) for n in os.listdir(path,'ignore')]

or

[os.path.join(path,n) for n in os.listdir(path,'replace')]

it's not an elegant solution, but it would solve i think most of the 
problems.


> 
>> 3. some solution.
> 
> One approach I had been considering is to always make the decoding
> succeed, by using the private-use-area of Unicode to represent bytes
> that don't decode correctly.
> 

hmm..an interesting idea..

and what happens with such texts, when they are encoded into let's say 
utf-8? are the in-private-use-area characters ignored?

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

re.match -- not greedy?

2006-11-19 Thread EXI-Andrews, Jack
the '*' and '+' don't seem to be greedy.. they will consume less in
order to match a string:

>>> import re;re.match('(a+)(ab)','aaab').groups()
('aa', 'ab')

this is the sort of behaviour i'd expect from 
   '(a+?)(ab)'

a+ should greedily consume a's at the expense of the string not matching

in real life, i am trying to match #defines:

>>> re.match( '#define ([A-Za-z0-9_]+)([^(])', '#define
abc(d)').groups()
('ab', 'c')

i want this example to fail because the first character after a string
of letters is a '('
i want to match only #defines without parameters.

so what's the definition of greedy?

(pls copy me on responses)

ta,


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


Re: os.lisdir, gets unicode, returns unicode... USUALLY?!?!?

2006-11-19 Thread Martin v. Löwis
gabor schrieb:
>> I may have missed something, but did you present a solution that would
>> make the case above work?
> 
> if we use the same decoding flags as binary-string.decode(),
> then we could do:
> 
> [os.path.join(path,n) for n in os.listdir(path,'ignore')]

That wouldn't work. The characters in the file name that didn't
decode would be dropped, so the resulting file names would be
invalid. Trying to do os.stat() on such a file name would raise
an exception that the file doesn't exist.

> [os.path.join(path,n) for n in os.listdir(path,'replace')]

Likewise. The characters would get replaced with REPLACEMENT
CHARACTER; passing that to os.stat would give an encoding
error.

> it's not an elegant solution, but it would solve i think most of the
> problems.

No, it wouldn't. This idea is as bad or worse than just dropping
these file names from the directory listing.

>> One approach I had been considering is to always make the decoding
>> succeed, by using the private-use-area of Unicode to represent bytes
>> that don't decode correctly.
>>
> 
> hmm..an interesting idea..
> 
> and what happens with such texts, when they are encoded into let's say
> utf-8? are the in-private-use-area characters ignored?

UTF-8 supports encoding of all Unicode characters, including the PUA
blocks.

py> u"\ue020".encode("utf-8")
'\xee\x80\xa0'

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.lisdir, gets unicode, returns unicode... USUALLY?!?!?

2006-11-19 Thread gabor
Martin v. Löwis wrote:
> gabor schrieb:
>>> I may have missed something, but did you present a solution that would
>>> make the case above work?
>> if we use the same decoding flags as binary-string.decode(),
>> then we could do:
>>
>> [os.path.join(path,n) for n in os.listdir(path,'ignore')]
> 
> That wouldn't work. The characters in the file name that didn't
> decode would be dropped, so the resulting file names would be
> invalid. Trying to do os.stat() on such a file name would raise
> an exception that the file doesn't exist.
> 
>> [os.path.join(path,n) for n in os.listdir(path,'replace')]
> 
> Likewise. The characters would get replaced with REPLACEMENT
> CHARACTER; passing that to os.stat would give an encoding
> error.
> 
>> it's not an elegant solution, but it would solve i think most of the
>> problems.
> 
> No, it wouldn't. This idea is as bad or worse than just dropping
> these file names from the directory listing.

i think that depends on the point of view.
if you need to do something later with the content of files, then you're 
right.

but if all you need is to display them for example...

> 
>>> One approach I had been considering is to always make the decoding
>>> succeed, by using the private-use-area of Unicode to represent bytes
>>> that don't decode correctly.
>>>
>> hmm..an interesting idea..
>>
>> and what happens with such texts, when they are encoded into let's say
>> utf-8? are the in-private-use-area characters ignored?
> 
> UTF-8 supports encoding of all Unicode characters, including the PUA
> blocks.
> 
> py> u"\ue020".encode("utf-8")
> '\xee\x80\xa0'

so basically you'd like to be able to "round-trip"?

so that:

listdir returns an array of filenames, the un-representable bytes will 
be represented in the PUA.

all the other file-handling functions (stat, open, etc..) recognize such 
strings, and handle them correctly.

?

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

Re: re.match -- not greedy?

2006-11-19 Thread Carl Banks
EXI-Andrews, Jack wrote:
> the '*' and '+' don't seem to be greedy.. they will consume less in
> order to match a string:
>
> >>> import re;re.match('(a+)(ab)','aaab').groups()
> ('aa', 'ab')
>
> this is the sort of behaviour i'd expect from
>'(a+?)(ab)'
>
> a+ should greedily consume a's at the expense of the string not matching

It's called backtracking.  If a match fails, the regexp engine
recursively unwinds some of the greedy matching and tries again.

> in real life, i am trying to match #defines:
>
> >>> re.match( '#define ([A-Za-z0-9_]+)([^(])', '#define
> abc(d)').groups()
> ('ab', 'c')
>
> i want this example to fail because the first character after a string
> of letters is a '('
> i want to match only #defines without parameters.

Probably the easiest thing to do is to attempt to match zero or one
opening parentheses, and bail out if it did end up matching the
parenthesis.

m = re.match(r"#define ([A-Za-z0-9_]+)(\(?)","#define abc(d)")
if m and m.groups(2) != "(":
...

(BTW, it's good practice to use a raw strings for the regular
expressions as I did.)

> so what's the definition of greedy?

It means match as much you can *while getting a match*.

Just remember regexp parsing is not a one way street: if it hits a dead
end, it'll go back and try another path.  In fact, greediness usually
doesn't affect *whether* a regexp matches; it only affects the
groupings.  I'm suddenly curious if there are any cases at all where
greediness changes whether it finds a match.

> (pls copy me on responses)

Ah, too bad you won't see it then


Carl Banks

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


Re: a few extensions for the itertools

2006-11-19 Thread Carl Banks

Paul McGuire wrote:
> "Mathias Panzenboeck" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> >I wrote a few functions which IMHO are missing in python(s itertools).
> >
> > You can download them here:
> > http://sourceforge.net/project/showfiles.php?group_id=165721&package_id=212104
> >
> > A short description to all the functions:
> >
> Just a couple of questions:
>
> > iproduct(iterable, start=0) -> value
> > Returns the product of the elements of a iterable
> > times the value of parameter 'start'.  When the
> > iterable is empty, returns start.
> >
> Wouldn't 1 be a better default value for start?

I concur; start should default to 1.


> > forall(predicate, iterable, default=True) -> bool
> > Returns True, when for all elements x in iterable
> > predicate(x) is True. When the iterable is empty,
> > returns default.
> >
> > forany(predicate, iterable, default=False) -> bool
> > Returns True, when for any element x in iterable
> > predicate(x) is True. When the iterable is empty,
> > returns default.
> >
> How are these different from all and any in Python 2.5?

1. These functions apply a predicate to the items.  It's simple enough
to do with any/all and a genexp, but by the same argument, it's simple
enough to do imap and ifilter with a plain genexp.
2. They have default values.  Default values for any and all don't make
sense, and I don't think they make sense here, either.  All of nothing
is always True; any of nothing is always False.


Carl Banks

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


Re: re.match -- not greedy?

2006-11-19 Thread André Malo
* Carl Banks wrote:

> I'm suddenly curious if there are any cases at all where
> greediness changes whether it finds a match.

nope, per definitionem ;-)

greedy := longest leftmost match

nd
-- 
die (eval q-qq[Just Another Perl Hacker
]
;-)
# André Malo,  #
-- 
http://mail.python.org/mailman/listinfo/python-list

RE: syntax error in sum(). Please explicate.

2006-11-19 Thread Delaney, Timothy (Tim)
John Machin wrote:

> Michael Press wrote:
>> I have not written python codes nor run any. I saw this
>> code posted and decided to try it. It fails. I read the
>> tutorial and the entry for the built in function sum,
>> but still do not see the problem. The code was cut and
>> paste.
> 
> I doubt it -- "none" should be "None"

Not necessarily. He said he cut-and-pasted - I interpreted that to be
from *his* editor. But since he was getting a SyntaxError, he would not
have hit the NameError yet ...

All-in-all, this actually felt like a post from someone who *had* read:
http://www.catb.org/~esr/faqs/smart-questions.html

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


Re: re.match -- not greedy?

2006-11-19 Thread Noah Rawlins
Carl Banks wrote:
> EXI-Andrews, Jack wrote:
>> the '*' and '+' don't seem to be greedy.. they will consume less in
>> order to match a string:
>>
> import re;re.match('(a+)(ab)','aaab').groups()
>> ('aa', 'ab')
>>
>> this is the sort of behaviour i'd expect from
>>'(a+?)(ab)'
>>
>> a+ should greedily consume a's at the expense of the string not matching
> 
> It's called backtracking.  If a match fails, the regexp engine
> recursively unwinds some of the greedy matching and tries again.
> 
>> in real life, i am trying to match #defines:
>>
> re.match( '#define ([A-Za-z0-9_]+)([^(])', '#define
>> abc(d)').groups()
>> ('ab', 'c')
>>
>> i want this example to fail because the first character after a string
>> of letters is a '('
>> i want to match only #defines without parameters.
> 
> Probably the easiest thing to do is to attempt to match zero or one
> opening parentheses, and bail out if it did end up matching the
> parenthesis.
> 
> m = re.match(r"#define ([A-Za-z0-9_]+)(\(?)","#define abc(d)")
> if m and m.groups(2) != "(":
> ...
> 
> (BTW, it's good practice to use a raw strings for the regular
> expressions as I did.)
> 

Another way that seems clearer to me is to use negative lookahead 
assertions.

 >>>defPatt = re.compile(r'#define\s+(\w+)\b(?!\()')
 >>> defPatt.match("#define abc(x)")
 >>> defPatt.match("#define foo_BarBaz")
<_sre.SRE_Match object at 0xb7dd5820>
 >>> defPatt.match("#define foo_BarBaz").groups()
('foo_BarBaz',)

In general '\w' is the same as [A-Za-z0-9_]

There are other considerations too... I don't know if

#define abc (x)

But the main thing here is the use of '\b' to require there to be a word 
boundary at the end your match and a (?!\() to require that the match is 
not followed by a '('

see http://docs.python.org/lib/re-syntax.html

noah


>> so what's the definition of greedy?
> 
> It means match as much you can *while getting a match*.
> 
> Just remember regexp parsing is not a one way street: if it hits a dead
> end, it'll go back and try another path.  In fact, greediness usually
> doesn't affect *whether* a regexp matches; it only affects the
> groupings.  I'm suddenly curious if there are any cases at all where
> greediness changes whether it finds a match.
> 
>> (pls copy me on responses)
> 
> Ah, too bad you won't see it then
> 
> 
> Carl Banks
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Looking for a graph (as in network, nodes) package

2006-11-19 Thread John Henry
I am looking for a ready made simple graph package.  I found an
extensive one in the piana package but when  I try to use just the
graph portion, it fails to load because of the line:

class Graph(object):
 ...

It seems that their Graph is subclassed from "object" but I couldn't
find a "object" class anywhere.  So, I abandoned using that one.

Before I reinvent the wheel and start writing one myself, can somebody
point me to a ready made one?  I am afraid Googling "Python Graph"
wouldn't be much help (millions and trillions of hits).

Thanks,

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