maximum value in a column of file

2008-07-23 Thread maurizio
which is the best way for the calculation of the maximum value in a 
   column of a file?


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


Re: Re: maximum value in a column of file

2008-07-23 Thread maurizio

i tryed to use the module max of numpy,
the problem is that i don't know how to put the column of the file in an 
array.

(i'm new in phyton).
anyway if you think there is a better way.

Fredrik Lundh wrote:
maurizio 
wrote:


which is the best way for the calculation of the maximum value in 
acolumn of a file?


what approach have you tried, and what happened when you tried it?






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


Re: Re: maximum value in a column of file

2008-07-23 Thread maurizio

thank you for your answer
actually i've to do some statistics (maximum,minimum,mean,standard 
deviation,) of a file of data in which each column is a particular 
type of data. (the file is a tab separated value).
I was trying to do this by using python (usually i work with fortran or 
bash, but i'm learning python), that the reason why i tried to use numpy.


Fredrik Lundh wrote:
maurizio 
wrote:



i tryed to use the module max of numpy,
the problem is that i don't know how to put the column of the file in 
an array.

(i'm new in phyton).
anyway if you think there is a better way.


What kind of file is it?  Did you pick numpy because you want to do 
matrix operations (beyond just finding a maximum value), or was it 
just the first thing you stumbled upon when researching the problem?


A simple pattern for finding the maximum value in a file, using only 
plain Python code, is:


max_value = ... some very small value ...
for line in file:
 value = ... extract value from line ...
 if value > max_value:
 max_value = value

If it's not obvious what "some very small value" is, given the range 
of data you're working with, you can do


max_value = None
for line in file:
 value = ... extract value from line ...
 if max_value is None or value > max_value:
 max_value = value

instead (this leaves max_value set to None if the file is empty)

A more experienced Python hacker might write

def get_all_values(file):
for line in file:
value = ... extract value from line ...
yield value

...

max_value = max(get_all_values(file))

instead.  But this still leaves us with the problem of extracting the 
value.  The best way to do that depends on the kind of files you're 
working with; for fixed-format text files, you could use string 
slicing and int/float for conversion (e.g. "value = 
float(line[10:20])", see the tutorial for details); for other text 
formats, you could use split/partition or regular expressions, or 
maybe an existing module (such as "csv"); for binary formats, there's 
a large

number of existing tools.

And if you really want to use numpy for other reasons than just 
getting a maximum value from a column, there's plenty of stuff in 
NumPy and SciPy (http://www.scipy.org/) that might be useful.


So, in other words, I guess we still need more info.






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


Which way is best to execute a Python script in Excel?

2012-07-05 Thread Maurizio Spadaccino
Hi all

I'm new to Python but soon after a few days of studying its features I find
it my favourite mean of programming scripts to allow for data storing and
mining. My idea would be to inplement python scripts from inside an excel
sheet that would store and fetch data from a Mysql database. So i need the
script to be launched, say, by pressing a button in excel and, for
instance, retrieve immediately data from the mysql table. For what I found
in the net, it seems there are three ways to control python from excel:
1) run my python script via shell using the Run command in VBA, which seems
to me the easiest (but maybe slower?) way;
2) creating a COM server, for which I need more training since it doesnt
appear that easy;
3) via Microsoft Script Control, for which I saw some example around where
It looks like I can 'simulate' a python shell via the 'executeStatement'
command.

What would you suggest would be the more performing way to achieve my goals
as described above?

Thanks for you help
Maurizio


-- 
Maurizio


www.mauriziospadaccino.com
---
If you can avoid printing this e-mail, you are only doing good for our
planet.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Which way is best to execute a Python script in Excel?

2012-07-06 Thread Maurizio Spadaccino
Hi Emile
Thanks for the reply. Could you provide me a more detailed 'how-to' tutorial on 
implementing a VBA macro that calls a script or a function from python, or tell 
me where on the web I can find it? The OReilly chapter seems a bit hard for me 
at this stage? I dont know, for example, where i should tell the macro where to 
locate the script...
Maurizio
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Which way is best to execute a Python script in Excel?

2012-07-07 Thread Maurizio Spadaccino
Thanks again Emile, I'll try out some examples. I found this one: 
http://showmedo.com/videotutorials/video?name=2190050&fromSeriesID=219
quite enlightning. 
One last doubt is: say the python code gets used by more Excel Users (different 
pc), can I include in some way a dinamic generation of the id in order to allow 
each pc to register is own COM server or do I have to hard code the id on each 
machine specifically?

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


non-terminating regex match

2008-04-02 Thread Maurizio Vitale

Has to be something really stupid, but the following never finish
(running Python 2.5.1 (r251:54863, Jan 10 2008, 18:00:49) 
[GCC 4.2.1 (SUSE Linux)] on linux2).

The intention is to match C++ identifiers, with or without namespace
qualification, with or without arguments (e.g. variables, functions and
macros).
The following should be accepted:
main
main(int,char**)
::main
std::cout
::std::cout
NDEBUG

Thanks for any help.
And yes, I'm a total beginner when it comes to Python, but it seems
very strange to me that a regex match on a finite length string
doesn't terminate
Regards,

    Maurizio

#!/usr/bin/env python
# -*- Python -*-

import re

if __name__ == '__main__':
r = re.compile (
r'(?:(?P(?:(?:::)?\w+)*)::)?'
r'(?P\w+)'
r'(?:\((?P[^\)]*)\))?'
)
match = r.search ('WITH_ALOHA_EXCEPTION_HANDLERS')


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


Re: non-terminating regex match

2008-04-02 Thread Maurizio Vitale
Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> writes:

> On Wed, 02 Apr 2008 16:01:59 +, Maurizio Vitale wrote:
>
>> And yes, I'm a total beginner when it comes to Python, but it seems
>> very strange to me that a regex match on a finite length string
>> doesn't terminate
>
> It does terminate, you just don't wait long enough.  Try it with fewer
> characters and then increase the identifier character by character and
> watch the time of the runs grow exponentially.
>

Ok. Now, my assumption was that re.compile would produce a DFA (or NFA)
and the match would then being linear in the size of the string.

Apparently this is not the case, so is there anything that can be done
to the regex itself to make sure that whatever re.compile produces is
more efficient?

Thanks,

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


Re: non-terminating regex match

2008-04-02 Thread Maurizio Vitale
MRAB <[EMAIL PROTECTED]> writes:

>
> I think the problem is with this bit: '(?:(?:::)?\w+)*'. The '::' is
> optional and also in a repeated group, so if it tries to match, say,
> 'abc' it can try and then backtrack all of these possibilities: abc,
> ab c, a bc, a b c. The longer the string, the more possibilities to
> try. Try this instead:
>
> r = re.compile (
> r'(?P(?:::)?(?:\w+::)*)?'
> r'(?P\w+)'
> r'(?:\((?P[^\)]*)\))?'
> )

That was indeed the problem. Your version is ok w/ the minor difference
that the named group  includes the '::' at the end. This can be
easily stripped off. Or maybe the regexp can be massaged further.
Thanks a lot,

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