Multiple turtles

2020-03-01 Thread Giuseppe

Hi,

I am new to Python.
I already tried turtle and it works well for my needs.

I would like to draw fractals using turtle module. There are many ways 
to do that but I would like to use multiple turtles drawing in parallel 
but I don't know how.


My first idea is to use clones as I do with Scratch (the problem with 
Scratch is the maximum number of simultaneous clones is set to 300) : 
each clone draw a part of the fractal and each clone (before being 
deleted) create clones who do the same thing and so on and so on...


1/ Is it possible to clone turtles ?
2/ Is it possible to make them work simultaneously ?
3/ Should I use multiple turtles instead ?
4/ Are there special things I should read first ?

Thank you.


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


Re: Multiple turtles

2020-03-02 Thread Giuseppe

Thank you.

Giuseppe

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


IDE

2006-10-13 Thread giuseppe
What is the better IDE software for python programming?

many thanks

joe 


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


Re: IDE

2006-10-13 Thread giuseppe
for example I ask wingware python IDE is good?

other program similar is better?

joe


"Theerasak Photha" <[EMAIL PROTECTED]> ha scritto nel messaggio 
news:[EMAIL PROTECTED]
> On 10/13/06, giuseppe <[EMAIL PROTECTED]> wrote:
>> What is the better IDE software for python programming?
>
> Long story short, I use Emacs 22 from CVS (text editor on steroids),
> but SPE looks like a good bet.
>
> It all depends on criteria! There's a million free variables in your 
> request...
>
> --- Theerasak 


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


no data exclution and unique combination.

2012-07-24 Thread giuseppe . amatulli
Hi,
would like to take eliminate a specific number in an array and its 
correspondent in an other array, and vice-versa. 

given 

a=np.array([1,2,4,4,5,4,1,4,1,1,2,4])
b=np.array([1,2,3,5,4,4,1,3,2,1,3,4])

no_data_a=1
no_data_b=2

a_clean=array([4,4,5,4,4,4])
b_clean=array([3,5,4,4,3,4])

after i need to calculate unique combination in pairs to count the observations 
and obtain
(4,3,2)
(4,5,1)
(5,4,1)
(4,4,2)

For the fist task i did

a_No_data_a = a[a != no_data_a]
b_No_data_a = b[a != no_data_a]

b_clean = b_No_data_a[b_No_data_a != no_data_b]
a_clean  = a_No_data_a[a_No_data_a != no_data_b]

but the results are not really stable. 

For the second task 
The np.unique would solve the problem if it can be apply to a two arrays.

Any idea?
thanks in advance 
Giuseppe




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


Re: no data exclution and unique combination.

2012-08-09 Thread giuseppe . amatulli
Terry and MRAB,
thanks for yours suggestions, 
in the end i found this solution 


mask=( a != 0 ) & ( b != 0  )

a_mask=a[mask]
b_mask=b[mask]

array2D = np.array(zip(a_mask,b_mask))

unique=dict()
for row in  array2D :
row = tuple(row)
if row in unique:
unique[row] += 1
else:
unique[row] = 1

print unique
{(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}

I choose this solution because i could not install "from collections import 
Counter". 
Anyway how i can print to a file the unique results without the brackets and 
obtain something like this?
4 5 1
5 4 1
4 4 2
2 3 1
4 3 2

Thanks in advance
Best regards.
Giuseppe








On Tuesday, 24 July 2012 13:27:05 UTC-5, giuseppe...@gmail.com  wrote:
> Hi,
> 
> would like to take eliminate a specific number in an array and its 
> correspondent in an other array, and vice-versa. 
> 
> 
> 
> given 
> 
> 
> 
> a=np.array([1,2,4,4,5,4,1,4,1,1,2,4])
> 
> b=np.array([1,2,3,5,4,4,1,3,2,1,3,4])
> 
> 
> 
> no_data_a=1
> 
> no_data_b=2
> 
> 
> 
> a_clean=array([4,4,5,4,4,4])
> 
> b_clean=array([3,5,4,4,3,4])
> 
> 
> 
> after i need to calculate unique combination in pairs to count the 
> observations 
> 
> and obtain
> 
> (4,3,2)
> 
> (4,5,1)
> 
> (5,4,1)
> 
> (4,4,2)
> 
> 
> 
> For the fist task i did
> 
> 
> 
> a_No_data_a = a[a != no_data_a]
> 
> b_No_data_a = b[a != no_data_a]
> 
> 
> 
> b_clean = b_No_data_a[b_No_data_a != no_data_b]
> 
> a_clean  = a_No_data_a[a_No_data_a != no_data_b]
> 
> 
> 
> but the results are not really stable. 
> 
> 
> 
> For the second task 
> 
> The np.unique would solve the problem if it can be apply to a two arrays.
> 
> 
> 
> Any idea?
> 
> thanks in advance 
> 
> Giuseppe

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


save dictionary to a file without brackets.

2012-08-09 Thread giuseppe . amatulli
Hi,
I have a dict() unique
like this 
{(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
and i want to print to a file without the brackets comas and semicolon in order 
to obtain something like this?
4 5 1
5 4 1
4 4 2
2 3 1
4 3 2
Any ideas?
Thanks in advance 
Giuseppe
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: save dictionary to a file without brackets.

2012-08-09 Thread Giuseppe Amatulli
thanks for the fast replies
my testing were very closed to yours but i did not know how

On 9 August 2012 15:25, Oscar Benjamin  wrote:
>
> On Aug 9, 2012 9:17 PM,  wrote:
>>
>> Hi,
>> I have a dict() unique
>> like this
>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>> and i want to print to a file without the brackets comas and semicolon in
>> order to obtain something like this?
>> 4 5 1
>> 5 4 1
>> 4 4 2
>> 2 3 1
>> 4 3 2
>> Any ideas?
>> Thanks in advance
>
> How's this?
>
> from __future__ import print_function
>
> output = open("out.txt", "w")
>
> for (a, b), c in d.items():
> print(a, b, c, file=output)
>
> output.close()
>
> Oscar.
>> --
>> http://mail.python.org/mailman/listinfo/python-list



-- 
Giuseppe Amatulli
Web: www.spatial-ecology.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: save dictionary to a file without brackets.

2012-08-09 Thread Giuseppe Amatulli
thanks for the fast replies
my testing were very closed to yours but i did not know how to print
the the number after the semicolon!
thanks!


On 9 August 2012 15:25, Oscar Benjamin  wrote:
>
> On Aug 9, 2012 9:17 PM,  wrote:
>>
>> Hi,
>> I have a dict() unique
>> like this
>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>> and i want to print to a file without the brackets comas and semicolon in
>> order to obtain something like this?
>> 4 5 1
>> 5 4 1
>> 4 4 2
>> 2 3 1
>> 4 3 2
>> Any ideas?
>> Thanks in advance
>
> How's this?
>
> from __future__ import print_function
>
> output = open("out.txt", "w")
>
> for (a, b), c in d.items():
> print(a, b, c, file=output)
>
> output.close()
>
> Oscar.
>> --
>> http://mail.python.org/mailman/listinfo/python-list



-- 
Giuseppe Amatulli
Web: www.spatial-ecology.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: save dictionary to a file without brackets.

2012-08-09 Thread Giuseppe Amatulli
Thanks a lot for the clarification.
Actually my problem is giving to raster dataset in geo-tif format find out
unique pair combination, count the number of observation
unique combination in rast1, count the number of observation
unique combination in rast2, count the number of observation

I try different solution and this seems to me the faster


Rast00=dsRast00.GetRasterBand(1).ReadAsArray()
Rast10=dsRast10.GetRasterBand(1).ReadAsArray()

mask=( Rast00 != 0 ) & ( Rast10 != 0  )  # may be this masking
operation can be included in the for loop

Rast00_mask= Rast00[mask]# may be this masking
operation can be included in the for loop
Rast10_mask= Rast10[mask]# may be this masking
operation can be included in the for loop

array2D = np.array(zip( Rast00_mask,Rast10_mask))

unique_u=dict()
unique_k1=dict()
unique_k2=dict()

for key1,key2 in  array2D :
row = tuple((key1,key2))
if row in unique_u:
unique_u[row] += 1
else:
unique_u[row] = 1
if key1 in unique_k1:
unique_k1[key1] += 1
else:
unique_k1[key1] = 1
if key2 in unique_k2:
unique_k2[key2] += 1
else:
unique_k2[key2] = 1

output = open(dst_file_rast0010, "w")
for (a, b), c in unique_u.items():
print(a, b, c, file=output)
output.close()

output = open(dst_file_rast00, "w")
for (a), b in unique_k1.items():
print(a, b, file=output)
output.close()

output = open(dst_file_rast10, "w")
for (a), b in unique_k2.items():
print(a, b, file=output)
output.close()

What do you think? is there a way to speed up the process?
Thanks
Giuseppe





On 9 August 2012 16:34, Roman Vashkevich  wrote:
> Actually, they are different.
> Put a dict.{iter}items() in an O(k^N) algorithm and make it a hundred 
> thousand entries, and you will feel the difference.
> Dict uses hashing to get a value from the dict and this is why it's O(1).
>
> 10.08.2012, в 1:21, Tim Chase написал(а):
>
>> On 08/09/12 15:41, Roman Vashkevich wrote:
>>> 10.08.2012, в 0:35, Tim Chase написал(а):
>>>> On 08/09/12 15:22, Roman Vashkevich wrote:
>>>>>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>>>>>> and i want to print to a file without the brackets comas and semicolon 
>>>>>> in order to obtain something like this?
>>>>>> 4 5 1
>>>>>> 5 4 1
>>>>>> 4 4 2
>>>>>> 2 3 1
>>>>>> 4 3 2
>>>>>
>>>>> for key in dict:
>>>>>print key[0], key[1], dict[key]
>>>>
>>>> This might read more cleanly with tuple unpacking:
>>>>
>>>> for (edge1, edge2), cost in d.iteritems(): # or .items()
>>>>   print edge1, edge2, cost
>>>>
>>>> (I'm making the assumption that this is a edge/cost graph...use
>>>> appropriate names according to what they actually mean)
>>>
>>> dict.items() is a list - linear access time whereas with 'for
>>> key in dict:' access time is constant:
>>> http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#use-in-where-possible-1
>>
>> That link doesn't actually discuss dict.{iter}items()
>>
>> Both are O(N) because you have to touch each item in the dict--you
>> can't iterate over N entries in less than O(N) time.  For small
>> data-sets, building the list and then iterating over it may be
>> faster faster; for larger data-sets, the cost of building the list
>> overshadows the (minor) overhead of a generator.  Either way, the
>> iterate-and-fetch-the-associated-value of .items() & .iteritems()
>> can (should?) be optimized in Python's internals to the point I
>> wouldn't think twice about using the more readable version.
>>
>> -tkc
>>
>>
>



-- 
Giuseppe Amatulli
Web: www.spatial-ecology.net
-- 
http://mail.python.org/mailman/listinfo/python-list


looping in array vs looping in a dic

2012-09-20 Thread giuseppe . amatulli
Hi,  
I have this script in python that i need to apply for very large arrays (arrays 
coming from satellite images). 
The script works grate but i would like to speed up the process. 
The larger computational time is in the for loop process.
Is there is a way to improve that part?
Should be better to use dic() instead of np.ndarray for saving the results?
and if yes how i can make the sum in dic()(like in the correspondent 
matrix[row_c,1] = matrix[row_c,1] + valuesRaster[row,col] )?
If the dic() is the solution way is faster?

Thanks
Giuseppe

import numpy  as  np
import sys
from time import clock, time

# create the arrays

start = time()
valuesRaster = np.random.random_integers(0, 100, 100).reshape(10, 10)
valuesCategory = np.random.random_integers(1, 10, 100).reshape(10, 10)

elapsed = (time() - start)
print(elapsed , "create the data")

start = time()

categories = np.unique(valuesCategory)
matrix = np.c_[ categories , np.zeros(len(categories))]

elapsed = (time() - start)
print(elapsed , "create the matrix and append a colum zero ")

rows = 10
cols = 10

start = time()

for col in range(0,cols):
for row in range(0,rows):
for row_c in range(0,len(matrix)) :
if valuesCategory[row,col] == matrix[row_c,0] :
matrix[row_c,1] = matrix[row_c,1] + valuesRaster[row,col]
break
elapsed = (time() - start)
print(elapsed , "loop in the  data ")

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


Re: looping in array vs looping in a dic

2012-09-20 Thread giuseppe . amatulli
Hi Ian and MRAB
thanks to you input i have improve the speed  of my code. Definitely reading in 
dic() is faster. I have one more question.
In the dic() I calculate the sum of the values, but i want count also the 
number of observation, in order to calculate the average in the end. 
Should i create a new dic() or is possible to do in the same dic().
Here in the final code. 
Thanks Giuseppe
  


rows = dsCategory.RasterYSize
cols = dsCategory.RasterXSize

print("Generating output file %s" %(dst_file))

start = time()

unique=dict()

for irows in xrange(rows):
valuesRaster=dsRaster.GetRasterBand(1).ReadAsArray(0,irows,cols,1)
valuesCategory=dsCategory.GetRasterBand(1).ReadAsArray(0,irows,cols,1)
for icols in xrange(cols):
if ( valuesRaster[0,icols] != no_data_Raster ) and ( 
valuesCategory[0,icols] != no_data_Category ) :
row = valuesCategory[0, icols],valuesRaster[0, icols]
if row[0] in unique :
unique[row[0]] += row[1]
else:
unique[row[0]] = 0+row[1] # this 0 was add if not the first 
observation was considered = 0

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


write to a file two dict()

2012-09-23 Thread giuseppe . amatulli
Hi 
Have two dict() of the same length and i want print them to a common file.


a={1: 1, 2: 2, 3: 3}
b={1: 11, 2: 22, 3: 33}

in order to obtain 

1 1 1 11
2 2 2 22
3 3 3 33

I tried 

output = open(dst_file, "w")
for (a), b , (c) , d in a.items() , b.items() :
output.write("%i %i %i %i\n" % (a,b,c,d))
output.close()

but i get the error ValueError: need more than 3 values to unpack.

do you have some suggestions?.
Thanks 
Giuseppe


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


python gdal error

2017-09-08 Thread falcone . giuseppe
Hi to all,

I'm new to python program and I have a problem with the following code.
I'm unable to read field_data variable in retrieve_model_params function.
In debug, execution ends immediately without a message.
Anyone have some idea about that?
If the for cycle in retrieve_model_params is moved to read_data_from_shape 
function, everything works perfectly ...

Thanks a lot.
Giuseppe


import os
from osgeo import ogr

def read_data_from_shape():
"This function load data from a shapefile"

shp_driver = ogr.GetDriverByName("ESRI Shapefile")

data_source = shp_driver.Open("../shape.shp", 0) 

layer = data_source.GetLayer()
feature_count = layer.GetFeatureCount()
print("Number of features:: %d" % (feature_count))
return layer

def retrieve_model_params():

# load data
field_data = read_data_from_shape()

for feature in field_data:
   vol = feature.GetField('VolHa')
   print(vol)

def main():
retrieve_model_params()

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


String Template

2013-12-27 Thread t . giuseppe
Hello

I'm rewriting a program previously written in C #, and trying to keep the same 
configuration file, I have a problem with untapped strings.

The previous configuration files provide an input template string of this type:

 


This string is parsed and the values are replaced with the actual values 
written to a log file (apache), then he is given the variable name.

Taking for example a classic line of apache log:

0.0.0.0 - [27/Dec/2013: 00:56:51 +0100] "GET / webdav / HTTP/1.1" 404 524 "-" 
"Mozilla/5.0 (Windows, U, Windows NT 5.1, en-US , rv: 1.9.2.12) Gecko/20101026 
Firefox/3.6.12 "

Is there any way to pull out the values so arranged as follows:

ip = 0.0.0.0
date = 27/Dec/2013: 00:56:51 +0100
url = / webdav /

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


Re: String Template

2013-12-28 Thread Giuseppe Tripoli
The problem is that I have a huge amount of log apache, log Akami, log cotendo, 
log iis ... messily all together.

And the program does is that, according to the file name, use the configuration 
file to read.
Certainly the quickest and easiest method is to use the regex, but I did not 
really intend to change the configuration file.

I think I will change technique.

Thank you very much for your answer
-- 
https://mail.python.org/mailman/listinfo/python-list


[ANN] BPT (Boxed Package Tool)

2009-04-29 Thread Giuseppe Ottaviano

Hi all,
I am pleased to announce BPT 0.2a (despite the number, this is the  
first public version).


http://pypi.python.org/pypi/bpt

Like virtualenv, BPT allows to create isolate environments, but it is  
not limited to Python software, as it can be used to install arbitrary  
Unix software. It can be used for development (several versions of the  
same software can be installed in the same environment, and  
administrator privileges are not needed), and for deployment of  
complete installations (the environments are relocatable).


Feel free to use and test it, comments are welcome.

What is BPT
===

BPT is a Python library (``bpt``) and a command line application
(``box``) to create and manage isolated enviroments, or *boxes*. Boxes
are *relocatable*, which means that they can be moved to a different
directory or even distributed to other machines (provided that the
architecture is compatible). Packages inside the box can be easily
disabled, enabled and removed, so that different versions of the same
software can be installed simultaneously, allowing to switch between
them.

BPT is similar in some ways to `virtualenv
<http://pypi.python.org/pypi/virtualenv>`_, but it is not restricted
to Python packages, allowing to install virtually any Unix
software. It also takes some ideas from `jhbuild
<http://live.gnome.org/Jhbuild>`_, but without the dependency
resolution and automatic downloading machinery, and the ``bpt-rules``
format is inspired by `Gentoo <http://www.gentoo.org/>`_'s ebuilds.

--
Giuseppe Ottaviano

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


[ANN] BPT (Boxed Package Tool) 0.3

2009-05-06 Thread Giuseppe Ottaviano

Hi all,
I am happy to announce BPT 0.3

http://pypi.python.org/pypi/bpt

This release solves some important bugs, and the documentation has  
less TODOs.


Version 0.3
===

- Improved documentation

- Fixed a bug that prevented the ``env`` script to be ``source``\'d
  (Works only with bash >= 3.0)

- Fixed a bug in the creation of symlinks that broken relocatability.

- ``env`` script does no more rely on GNU's ``readlink``, that is not
  present in vanilla OSX

- Other minor fixes

What is BPT
===

BPT is a Python library (``bpt``) and a command line application
(``box``) to create and manage isolated enviroments, or *boxes*.

- Boxes are *relocatable*, which means that they can be moved to a
  different directory or even distributed to other machines (provided
  that the architecture is compatible).

- Packages inside the box can be easily disabled, enabled and removed,
  so that different versions of the same software can be installed
  simultaneously, allowing to switch between them.

- Boxes can be *nested*, in the sense that it is possible to activate
  a box environment while inside another box environment, so that all
  the packages installed in both boxes are available, and so on.

BPT is similar in some ways to `virtualenv
<http://pypi.python.org/pypi/virtualenv>`_, but it is not restricted
to Python packages, allowing to install virtually any Unix
software. It also takes some ideas from `jhbuild
<http://live.gnome.org/Jhbuild>`_, but without the dependency
resolution and automatic downloading machinery, and the ``bpt-rules``
format is inspired by `Gentoo <http://www.gentoo.org/>`_'s ebuilds.

Enjoy,
Giuseppe Ottaviano
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] how GNU stow is complementary rather than alternative to distutils

2009-05-11 Thread Giuseppe Ottaviano
Talking of stow, I take advantage of this thread to do some shameless  
advertising :)
Recently I uploaded to PyPI a software of mine, BPT [1], which does  
the same symlinking trick of stow, but it is written in Python (and  
with a simple api) and, more importantly, it allows with another trick  
the relocation of the installation directory (it creates a semi- 
isolated environment, similar to virtualenv).
I find it very convenient when I have to switch between several  
versions of the same packages (for example during development), or I  
have to deploy on the same machine software that needs different  
versions of the dependencies.


I am planning to write an integration layer with buildout and  
easy_install. It should be very easy, since BPT can handle directly  
tarballs (and directories, in trunk) which contain a setup.py.


HTH,
Giuseppe

[1] http://pypi.python.org/pypi/bpt
P.S. I was not aware of stow, I'll add it to the references and see if  
there are any features that I can steal



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


[ANN] BPT (Boxed Package Tool) 0.4a -- now with PIP support

2009-05-15 Thread Giuseppe Ottaviano

Hi all,
I am happy to announce BPT 0.4a

http://pypi.python.org/pypi/bpt

This release is particularly interesting for python users, since it  
includes a modified version of PIP that installs the packages inside  
the box. So, at least for python packages, installation (including  
dependency resolution) is fully automated.


Version 0.4
===

- Included a forked version of `PIP
<http://pypi.python.org/pypi/pip>`_ 0.3.1 that installs packages in
the current box. Installing python packages is now as easy as
``easy_install``

- Implemented the ``autoremove`` command that removes all the disabled
packages (unless they match given patterns)

- Written some new documentation, added a TODO list

What is BPT
===

BPT is a Python library (``bpt``) and a command line application
(``box``) to create and manage isolated enviroments, or *boxes*.

- Boxes are *relocatable*, which means that they can be moved to a
different directory or even distributed to other machines (provided
that the architecture is compatible).

- Packages inside the box can be easily disabled, enabled and removed,
so that different versions of the same software can be installed
simultaneously, allowing to switch between them.

- Boxes can be *nested*, in the sense that it is possible to activate
a box environment while inside another box environment, so that all
the packages installed in both boxes are available, and so on.

BPT is similar in some ways to `virtualenv
<http://pypi.python.org/pypi/virtualenv>`_, but it is not restricted
to Python packages, allowing to install virtually any Unix
software. It also takes some ideas from `jhbuild
<http://live.gnome.org/Jhbuild>`_, but without the dependency
resolution and automatic downloading machinery, and the ``bpt-rules``
format is inspired by `Gentoo <http://www.gentoo.org/>`_'s ebuilds.

A fork of PIP_ is included to make installation of python packages
easier, and as an example of use of the BPT API.


Enjoy,
Giuseppe Ottaviano
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to delimit a list?

2008-05-13 Thread Giuseppe Ottaviano


On May 13, 2008, at 12:28 PM, [EMAIL PROTECTED] wrote:


Hi - I have a list returned from popen/readlines, and am wondering how
to go about iterating over each item which was returned (rather than
currently having the whole lot returned).

so far:


f=os.open("./get_hostnames").readlines


returns ['host1 host2 host3 ... hostN\n]'

i'd like to be in a position to iterate through these, grabbing each
host.  I have played with transmuting to a str, and using split, and
this works, but I get the subscript brackets from the list output as
expected, as the list output is now a string literal, and this is not
what I want - and I think it's a bit long-winded to do a search 'n
replace on it - hence why I ask in the subject what's the best way.


f=str(f)
f.split()

["['host1","host2", ... ,"hostN\n']"]


If the file is really big, you may want not to construct an actual  
list with all the words, but instead use an iterator. If you define  
the function


def ichain(seq):
for s in seq:
for x in s: yield x

(which is often useful and I don't think it has been included in  
itertools) you can iterate lazily on the file:


hosts = ichain(s.split() for s in f)
for host in hosts:
# ...

HTH,
Giuseppe

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


Re: ANN: equivalence 0.1

2008-06-02 Thread Giuseppe Ottaviano


Interesting.. it took me a while to figure out why the second case is
so much slower and you're right, it is indeed quadratic. I don't know
how likely would such pathological cases be in practice, given that
the preferred way to merge a batch of objects is
eq.merge(*xrange(10001)), which is more than 3 times faster than the
non-pathologic first case (and which I optimized even further to avoid
attribute lookups within the loop so it's more like 5 times faster
now). Also the batch version in this case remains linear even if you
merge backwards, eq.merge(*xrange(1,-1,-1)), or in any order for
that matter.


The example just showed what could happen if the merges are done in  
pathological order, it is not about batch merging. I think that  
pathological cases like this indeed show up in real cases: many  
algorithms of near duplicate elimination and clustering reduce to  
finding connected components of a graph whose edges are given as a  
stream, so you can't control their order.
With this implementation, every time a component sized N is given a  
second (or following) argument to merge, you pay Omega(N).



I am familiar with it and I will certainly consider it for the next
version; for now I was primarily interested in functionality (API) and
correctness.


Good :) 
--

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


Re: ANN: equivalence 0.1

2008-06-02 Thread Giuseppe Ottaviano


On Jun 1, 2008, at 6:16 PM, George Sakkis wrote:


Equivalence is a class that can be used to maintain a partition of
objects into equivalence sets, making sure that the equivalence
properties (reflexivity, symmetry, transitivity) are preserved. Two
objects x and y are considered equivalent either implicitly (through a
key function) or explicitly by calling merge(x,y).


I think this library would be very useful, and I like the interface  
(in particular the idea of the projecting function), but I think the  
algorithm is less than optimal. It can show quadratic behavior in some  
cases:


$ python -m timeit -s 'import equivalence' -s 'eq =  
equivalence.Equivalence()' 'for i in xrange(1): eq.merge(i, i+1)'

10 loops, best of 3: 57.6 msec per loop

$ python -m timeit -s 'import equivalence' -s 'eq =  
equivalence.Equivalence()' 'for i in xrange(1): eq.merge(i+1, i)'

10 loops, best of 3: 2.59 sec per loop

Have you considered using the Union-Find algorithm, which would be  
(almost) linear in all cases?


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


Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread Giuseppe Ottaviano


Hmm, difficult to react to this. On the one hand I have had people
argue that block delimiting in python is explicit too. So in that
case python doesn't differ from those other languages.

On the other hand if we accept that blocks are delimited implicitely
in python then it seems python doesn't follow its own zen:

 Explicit is better than implicit


So also duck typing is against python's philosophy? :)
--
http://mail.python.org/mailman/listinfo/python-list


Named tuples and projection

2008-06-20 Thread Giuseppe Ottaviano
I found the namedtuple very convenient for rapid prototyping code, for  
functions that have to return a number of results that could grow as  
the code evolves. They are more elegant than dicts, and I don't have  
to create a new explicit class. Unfortunately in this situation they  
lose the convenience of tuple unpacking: changing tuple's parameters  
would break other functions unpacking the result.
One solution, with 3.0 syntax, would be unpacking only the used  
parameters, using always a *rest

a, b, *rest = f()
so that if the tuple grows, the code keeps working.
However I find this confusing (and not avaliable in python 2.5).
I don't know if similar solutions have been proposed, I came up with  
this one:


Add a method "project" that given a string of arguments (in a similar  
fashion as namedtuple construction) returns a tuple with only that  
items. I monkeypatched the namedtuple recipe as a proof of concept,  
replace "return result" with these lines:


def _project(self, fields):
return tuple(getattr(self, field) for field in fields.split())
def _getitem(self, item):
if isinstance(item, str):
return self.project(item)
return super(result, self).__getitem__(item)

result.project = _project
result.__getitem__ = _getitem

return result

This is the result:

In [2]: X = namedtuple('X', 'a b c d')

In [3]: x = X(1, 2, 3, 4)

In [4]: a, c = x.project('a c')

In [5]: a, c
Out[5]: (1, 3)

In [6]: a, d = x['a d']

In [7]: a, d
Out[7]: (1, 4)

In [8]: x[2]
Out[8]: 3

I implemented also __getitem__ just to show another possible syntax  
(maybe a bit ugly). project may be not a self-evident name, probably  
something like "select" (for SQL addicts) would be more appropriate.

Other possible solutions?

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


Re: Named tuples and projection

2008-06-20 Thread Giuseppe Ottaviano



[snip]
Provided you don't change the order of the items in the tuple, you can
just use slicing:

a, b = f()[ : 2]


Yes, this is what you would normally do with tuples. But i find this  
syntax very implicit and awkward. Also, you cannot skip elements, so  
you often end up with things like

a, _, b = f()[:3]

The use case I posted is just the most recent that I stumbled upon.  
But often a project function would have saved me a lot of typing.  
Consider for example when named tuples are used for rows in an ORM (or  
attributes in XML):


for person in people:
name, age, address = person['name age address']
# code






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


Re: Bind compiled code to name?

2008-06-22 Thread Giuseppe Ottaviano

class D:pass
d = D()
exec source_code in d.__dict__
print d.some_name

Notice that this will also give you d.__builtins__, which you might
want to del afterwards.



If you want to mimic an import you can also do this:

import types
D = types.ModuleType('D')
exec source_code in D.__dict__
print D.some_name

This way D is a module (don't know if there are real differences with  
the class approach, though)



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


Re: Boost Python - C++ class' private static data blown away before accessing in Python?

2008-07-05 Thread Giuseppe Ottaviano


In Python, I retrive an Entity from the EntityList:

elist = EntityList()
elist.append(Entity())
elist.append(Entity())

entity = elist.get_at(0)

entity.foo()

But it crashes inside foo() as the private static data is empty; or
rather the string array is empty. I know before that point that the
private static data is valid when accessed earlier by the C++ code as
the program works fine. It just won't work from Python, so somehow the
private static data has been blown away but I can't work out where or
why.


Probably it is a problem of lifetime. What is the signature of append?  
Who deletes the appended Entity in C++ code?
If append takes a raw pointer, Boost.Python copies the pointer but  
destroys the Entity object because it is a temporary and its reference  
count went to zero. So the pointer in the list is referring to a  
destroyed object, which results in undefined behaviour.


Did you have a look at the lifetime policies of Boost.Python? The  
simplest way to workaround the problem is using const reference  
arguments, and always use value semantics. If it can result in a  
performance penalty, another simple way is using shared_ptr's, which  
have their own reference count (different from the one in CPython  
lib), but Boost.Python does the magic to make them work together.


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


Re: python + database book

2006-12-04 Thread Giuseppe Di Martino
Il Sat, 02 Dec 2006 21:00:05 -0800, progman ha scritto:

> new to python.
> i work with db heavily.
> 
> any good  book for python + database?

Evaluate http://www.sqlalchemy.org/ and his documentation

Giuseppe

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


Re: PATH or PYTHONPATH under Windows ???

2007-06-05 Thread Giuseppe Di Martino
Il Tue, 05 Jun 2007 23:57:15 +0200, Stef Mientki ha scritto:

> hello,
> 
> after cleaning up a PC, Python can't find any libraries anymore.
> But happily I've still one PC, where Python is running perfect.
> Now I always read about the environment variable "PYTHONPATH".
> But on neither PC there exists a "PYTHONPATH".
> 
> On the PC that works ok,
> there is the standard "PATH" environment variable,
> and indeed there are all the Python Library references.
> 
> Please enlighten me.
> 
> thanks,
> Stef Mientki

Download pyrun.exe (http://developer.berlios.de/projects/pyrun) and copy
it in your Python's folder; to exec python.exe use pyrun python or to exec
script.py use pyrun python script.py

Giuseppe

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


Re: PATH or PYTHONPATH under Windows ???

2007-06-06 Thread Giuseppe Di Martino
Il Wed, 06 Jun 2007 12:01:13 +0200, stef ha scritto:

>>   
> I ran your program but it didn't solve the problem (running Python, 
> embedded in Delphi).
> I'm beginning to get the feeling that Python installation is a very 
> complex case.
> Anyway thanks.
> 

In the original post you don't mention "embedded in Delphi" !
This is a different thing, the problem is that your Delphi's app relies on
a Python installation in the system and, insteed, it shoud have
on its own copy of Python, avoiding any problems.

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


Re: for web application development

2007-06-14 Thread Giuseppe Di Martino
Il Thu, 14 Jun 2007 09:16:24 +, james_027 ha scritto:

> 
> My problem is looking for a web framework for python, that could
> provide a natural way of developing web app. I am avoiding to learn
> template language as much as possible.

Currently, i'm playing with Pylons (http://pylonshq.com) and until now i'm
happy !

Giuseppe


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


Re: Problems running on HP Intel duo core machine

2008-12-11 Thread Giuseppe Di Martino
Il Thu, 11 Dec 2008 14:58:16 -0500, jim-on-linux ha scritto:

> The first module that is imported is win32api. line 8 of that module
> adds to the path the module named 'win32api.pyd'.
> The import is is completed without error.
> 
> The next module that is imported is win32ui. line 8 of that module adds
> to the path a module named 'win32ui.pyd'.
> The search for the win32ui.pyd module seems to be the cause of the
> problem.
> Traceback:
> ImportError: Dll load failed: The specified module could not be found.
> 
> Both modules 'win32api.pyd'  and win32ui.pyd are in the same directory.


Have you checked if there is a file named "win32api.pyd" somewhere in the 
system path (c:\windows\, c:\windows\system32, etc. ) ?

Try to add the path to the folder containing your win32api.pyd and 
win32ui.pyd to the PATH enviroinment variable, before running your 
application.

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


Re: Code for generating validation codes (in images)

2005-09-04 Thread Giuseppe di Sabato (aka deelan)
morphex wrote:
> Hi,
> 
> does anyone of you know of some code I can use to generate validation
> code images?
> 
> Those images you can see on various login forms used to prevent bots
> for performing a brute-force attack..

take a look at the "pycaptcha" package:


later,
deelan.

-- 
deelan, #1 fan of adriana lima!

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