how to save a modified .xml back

2009-12-25 Thread Hyunchul Kim
Hi all,

I want to load a small .xml file, modifiy some nodes and save the .xml with
modifications to another .xml file.

I managed to load and modify a small .xml file using xml.dom.minidom and
xml.dom.pulldom.

However, I don't know how to save it back using any of two modules.

Any suggestion?

Thank you in advance,

**
from xml.dom.pulldom import START_ELEMENT, parse
doc = parse('input.xml')
for event, node in doc:
if event == START_ELEMENT and node.localName == "something_interesting":
doc.expandNode(node)
for an_element in node.childNodes:
do_something()
*

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


Re: faster than list.extend()

2009-11-17 Thread Hyunchul Kim
Thank Tim, Raymond, and all.

In my test, Tim's t1 was fastest and Raymond's triple3 is very near to Tim's
t1.
Anyway both of them took only 6~7% time of my original .extend() version.

I think that following n_ple() is a good generalized function that was 1~3%
slower than t1() and triple3() for "triple".

*
from itertools import *
def n_ple(inputlist, n):
chain_from_iterable = chain.from_iterable; izip = izip
return chain_from_iterable(izip(*[inputlist]*n))
*

 def t2(i):
   r = range(3)
   return (x for x in i for _ in r)
 def t2_1(i):
   r = range(3)
   return [x for x in i for _ in r]

I didn't know that list(t2(inputlist)) is much faster than t2_1(inputlist),
it's gonna be a helpful tip for me.

Thank you all.

Hyunchul


On Tue, Nov 17, 2009 at 7:55 AM, Tim Chase wrote:

> Hyunchul Kim wrote:
>
>> Hi, all.
>>
>> I want to improve speed of following simple function.
>> Any suggestion?
>>
>> **
>> def triple(inputlist):
>>  results = []
>>  for x in inputlist:
>>results.extend([x,x,x])
>>  return results
>> **
>>
>
> Several ways occur to me:
>
>  def t1(i):
>for x in i:
>  yield x
>  yield x
>  yield x
>
>  def t2(i):
>r = range(3)
>return (x for x in i for _ in r)
>
>
> I prefer to return generators in case the input is large, but in both
> cases, you can just wrap it in list() like
>
>  list(t1(inputlist))
>
> or in t2(), you can just change it to use
>
>return [x for x in i for _ in r]
>
> -tkc
>
>
>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


faster than list.extend()

2009-11-16 Thread Hyunchul Kim
Hi, all.

I want to improve speed of following simple function.
Any suggestion?

**
def triple(inputlist):
  results = []
  for x in inputlist:
results.extend([x,x,x])
  return results
**

Thank you in advance,

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


sequential multiple processes

2009-10-24 Thread Hyunchul Kim
Hi, all

How to run multiple processes with sequential input of a thousand of data in
a script run?

I have a python script and 1,000 independent input data for it.
Previously, I divided input data into* n* groups and ran a  same python
script *n* times to use *n* processors.
It's inconvenient.

How can I do same thing in a signle script running?


Thank you in advance,

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


speed of string chunks file parsing

2009-04-06 Thread Hyunchul Kim

Hi, all

I have a simple script.
Can you improve algorithm of following 10 line script, with a view point 
of speed ?

Following script do exactly what I want but I want to improve the speed.

This parse a file and accumulate lines till a line match a given regular 
expression.
Then, when a line match a given regular expression, this function yield 
lines before the matched lines.



import re
resultlist = []
cp_regularexpression = re.compile('^a complex regular expression here$)
for line in file(inputfile):
   if cp_regularexpression.match(line):
   if resultlist != []:
   yield resultlist
   resultlist = []
   resultlist.append(line)
yield resultlist


Thank you in advance,

Hyunchul


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


Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread Hyunchul Kim

In that case, I usually use

# when rounding is proper,
s = '1.23456789'
print round(float(s))

or

# when cut out is proper,
from math import floor
print floor(float(s)*1000)/1000

Hyunchul

valpa wrote:

I only need the 3 digits after '.'

Is there any way other than converting from/to string?
--
http://mail.python.org/mailman/listinfo/python-list


  


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


Re: unbiased benchmark

2009-03-12 Thread Hyunchul Kim

In the system that I tested, results were different.
*
System was
CPU: AMD Phenom(tm) 9950 Quad-Core Processor, Frequency 2600MHz, L2 
cache 512KB

Memory 3164MB
OS: Ubuntu 8.10


When I tried this benchmark for the first time,
i...@pc:~$ time python bench.py

real0m0.010s
user0m0.008s
sys0m0.000s
i...@pc:~$ time ruby bench.rb

real0m0.108s
user0m0.000s
sys0m0.004s

Python was faster than Ruby 10 times!!
As I repeated, results showed shorter times and became stable.
After a few repeat, stable results were

i...@pc:~$ time python bench.py

real0m0.010s
user0m0.008s
sys0m0.000s
i...@pc:~$ time ruby bench.rb

real0m0.004s
user0m0.004s
sys0m0.000s

Now, Ruby is faster two times.
Have you repeated your benchmark?
Your results looks to be over-exaggeration even if this benchmark is fair.

Hyunchul


Sam Ettessoc wrote:

Dear sir,

I would like to share a benchmark I did. The computer used was a
2160MHz Intel Core Duo w/ 2000MB of 667MHz DDR2 SDRAM running MAC OS
10.5.6 and a lots of software running (a typical developer
workstation).

Python benchmark:
HAMBURGUESA:benchmark sam$ echo 1+1 > bench.py
HAMBURGUESA:benchmark sam$ time python bench.py
real0m0.064s
user0m0.049s
sys 0m0.013s

Ruby benchmark:
HAMBURGUESA:benchmark sam$ echo 1+1 > bench.rb
HAMBURGUESA:benchmark sam$ time ruby bench.rb
real0m0.006s
user0m0.003s
sys 0m0.003s

Can you believe it? Ruby is 10 times faster than Python.

On a side note, when I subscribed to the Python mailing list, it took
less than 200ms before I got the confirmation email. For the Ruby
mailing list, it took over 9000ms! Which is 4500% less performant!

Sam Ettessoc
p.s. I have no affiliation with ruby or python devloppement team.
--
http://mail.python.org/mailman/listinfo/python-list


  


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


Re: how to find all completely connected sub-graphs?

2009-03-02 Thread Hyunchul Kim

Dear Odeits,

Yes, I meant directly connected to each other.

Thanks.

Hyunchul

odeits wrote:

On Mar 2, 10:35 pm, Hyunchul Kim  wrote:
  

Hi, all,

How can I find all "completely connected subgraphs" in a graph when node
and edge data are available?

"completely connected subgraph" is a group, all members of which are
connected to each other.

Thanks,

Hyunchul



Do you mean all of the member are directly connected to each other?
--
http://mail.python.org/mailman/listinfo/python-list


  


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


how to find all completely connected sub-graphs?

2009-03-02 Thread Hyunchul Kim

Hi, all,

How can I find all "completely connected subgraphs" in a graph when node 
and edge data are available?


"completely connected subgraph" is a group, all members of which are 
connected to each other.


Thanks,

Hyunchul


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


How to decompress .Z file?

2007-08-20 Thread Hyunchul Kim
Hi, all

How can .Z file be decompressed?

According to python library reference, .Z file might not be supported by
python, yet.

Best,
Hyunchul Kim
-- 
http://mail.python.org/mailman/listinfo/python-list


[python-list] pdf read & write

2007-07-27 Thread Hyunchul Kim
Dear all,

How can I read a pdf file and add invisible comment?
I want to make a script which read a pdf file and add tags inside the
file invisibly. Then, I will make a script for managing tags of given
pdf files.

I know "referencer" can manage tags for pdf file but it seems store tag
information to additional file outside pdf file.

Any suggestion are welcome.

Best,
Hyunchul Kim

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


[python-list] pdf read & write

2007-07-27 Thread Hyunchul Kim
Dear all,

How can I read a pdf file and add invisible comment?
I want to make a script which read a pdf file and add tags inside the
file invisibly. Then, I will make a script for managing tags of given
pdf files.

I know "referencer" can manage tags for pdf file but it seems store tag
information to additional file outside pdf file.

Any suggestion are welcome.

Best,
Hyunchul Kim
-- 
http://mail.python.org/mailman/listinfo/python-list