Re: Equal sets with unequal print and str() representations

2011-10-16 Thread Terry Reedy

On 10/16/2011 9:17 PM, Ian Kelly wrote:

On Sun, Oct 16, 2011 at 5:52 PM, Ganesh Gopalakrishnan
  wrote:

This probably is known, but a potential pitfall (was, for me) nevertheless.
I suspect it is due to hash collisions between 's3' and 's13' in this case?
It happens only rarely, depending on the contents of the set.


I'm not sure exactly which keys are colliding here, but in general the
iteration order of a set (or dict) depends not just on the contents,
but also on the order of insertion.  And of course the repr depends on
the iteration order -- anything consistent would require sorting,
which would be O(n log n) and is not even necessarily well-defined.


To put it another way, any dependence on the arbitrary ordering of 
unordered collections in iteration or displays is a programming error.


--
Terry Jan Reedy

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


Re: Equal sets with unequal print and str() representations

2011-10-16 Thread Ian Kelly
On Sun, Oct 16, 2011 at 5:52 PM, Ganesh Gopalakrishnan
 wrote:
> This probably is known, but a potential pitfall (was, for me) nevertheless.
> I suspect it is due to hash collisions between 's3' and 's13' in this case?
> It happens only rarely, depending on the contents of the set.

I'm not sure exactly which keys are colliding here, but in general the
iteration order of a set (or dict) depends not just on the contents,
but also on the order of insertion.  And of course the repr depends on
the iteration order -- anything consistent would require sorting,
which would be O(n log n) and is not even necessarily well-defined.
-- 
http://mail.python.org/mailman/listinfo/python-list


Equal sets with unequal print and str() representations

2011-10-16 Thread Ganesh Gopalakrishnan
This probably is known, but a potential pitfall (was, for me) 
nevertheless. I suspect it is due to hash collisions between 's3' and 
's13' in this case? It happens only rarely, depending on the contents of 
the set.


>>> S1 = {'s8', 's3', 's2', 's0', 's7', 's6', 's4', 's13', 's14'}
S1 = {'s8', 's3', 's2', 's0', 's7', 's6', 's4', 's13', 's14'}
>>> S2 = {'s8', 's13', 's2', 's0', 's7', 's6', 's4', 's3', 's14'}
S2 = {'s8', 's13', 's2', 's0', 's7', 's6', 's4', 's3', 's14'}
>>> S1
S1
{'s8', 's13', 's2', 's0', 's7', 's6', 's4', 's3', 's14'}
>>> S2
S2
{'s8', 's3', 's2', 's0', 's7', 's6', 's4', 's13', 's14'}
>>> S1==S2
S1==S2
True
>>> str(S1)
str(S1)
"{'s8', 's13', 's2', 's0', 's7', 's6', 's4', 's3', 's14'}"
>>> str(S2)
str(S2)
"{'s8', 's3', 's2', 's0', 's7', 's6', 's4', 's13', 's14'}"
>>> str(S1) == str(S2)
False

--
Ganesh

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


Re: python.org appears to be down

2011-10-16 Thread Chris Angelico
On Mon, Oct 17, 2011 at 8:35 AM, Ian  wrote:
> Hopefully someone who can do something about it will read this.

Working for me. What are you seeing as down?

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


python.org appears to be down

2011-10-16 Thread Ian

Hopefully someone who can do something about it will read this.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help with beginner form using lpthw.web

2011-10-16 Thread Emile van Sebille

On 10/15/2011 8:03 AM pngrv said...




  
   Please Use This Form To Request Resources
  
  
  
 Name:
 Project:
 # of Locations:
 # of Engines:
 
  1
  2
  3
  4
  5
  6
  7
  8
  9+
 
 


Where do you close this tag?



   
 $def with (request_string)
 $if request_string:
 I just wanted to say$request_string.
 $else:
 Hello, world!
  


And seeing this error:
  at /rs_request
invalid syntax Template traceback: File 'templates/rs_request.html', line 
30  (rs_request.html, line 30)



Emile

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


Re: Reading a file into a data structure....

2011-10-16 Thread Ian Kelly
On Sat, Oct 15, 2011 at 8:18 PM, MrPink  wrote:
> I did not understand what a tuple was.
> So it was very hard for me to understand what a namedtuple was and
> follow the code.
> Then I looked up the word at dictionary.com and got this:
> http://dictionary.reference.com/browse/tuple
>
> tuple:  computing  a row of values in a relational database
>
> Now I understand a little better what a tuple is and can follow the
> code better.

Python tuples do not have anything to do with relational databases.
You would get a better introduction from Wikipedia:

http://en.wikipedia.org/wiki/Tuple

In Python, tuples are nothing more than immutable sequences, as
opposed to lists, which are mutable sequences.  Another way of
characterizing the difference between lists and tuples is in how they
are typically used.  A list is typically used for a homogeneous
(meaning all elements are treated in the same way) sequence containing
an arbitrary number of unrelated objects.  A tuple is typically used
for a heterogeneous sequence of a certain length.  For example, a
tuple might be expected to contain exactly two strings and an int that
are related in some fashion.

> A namedtuple seems like a dictionary type.  I'll need to read up on
> the difference between the two.

A namedtuple is a tuple subclass where each of the elements the tuple
is expected to contain has been given a specific name for ease of
reference.  The names are essentially aliases for numerical indices.
It differs from a dictionary in that it is ordered and only contains
elements with specific names (and always contains those elements),
whereas a dictionary contains arbitrary key-value pairs.
-- 
http://mail.python.org/mailman/listinfo/python-list


callling python function in c

2011-10-16 Thread masood shaik
Hi

  I am trying to call python function from c code.The following
program i got from the web source while i am trying to run this
program it throws an segmentation fault.I am working on Ubuntu 10.10
version.Can anyone guide me please

The following program

call_function.c
// call_function.c - A sample of calling python functions from C code
//
#include 
#include

int main(int argc, char *argv[])
{
int i;
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;

if (argc < 3)
{
printf("Usage: exe_name python_source function_name\n");
return 1;
}

// Initialize the Python Interpreter
Py_Initialize();

// Build the name object
pName = PyString_FromString(argv[1]);

// Load the module object
pModule = PyImport_Import(pName);

// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);

// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, argv[2]);

if (PyCallable_Check(pFunc))
{
// Prepare the argument list for the call
if( argc > 3 )
{
pArgs = PyTuple_New(argc - 3);
for (i = 0; i < argc - 3; i++)
{
pValue = PyInt_FromLong(atoi(argv[i + 
3]));
if (!pValue)
{
PyErr_Print();
return 1;
}
PyTuple_SetItem(pArgs, i, pValue);
}

pValue = PyObject_CallObject(pFunc, pArgs);

if (pArgs != NULL)
{
Py_DECREF(pArgs);
}
} else
{
pValue = PyObject_CallObject(pFunc, NULL);
}

if (pValue != NULL)
{
printf("Return of call : %ld\n", PyInt_AsLong(pValue));
Py_DECREF(pValue);
}
else
{
PyErr_Print();
}
} else
{
PyErr_Print();
}

// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);

// Finish the Python Interpreter
Py_Finalize();

return 0;
}

py_function.py

'''py_function.py - Python source designed to demonstrate the use of
python embedding'''

def multiply():
c = 12345*6789
print 'The result of 12345 x 6789 :', c
return c

def multiply1(a,b):
c = a*b
print 'The result of', a, 'x', b, ':', c
return c

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


Re: Loop through a dict changing keys

2011-10-16 Thread Jon Clements
On Oct 16, 12:53 am, PoD  wrote:
> On Sat, 15 Oct 2011 11:00:17 -0700, Gnarlodious wrote:
> > What is the best way (Python 3) to loop through dict keys, examine the
> > string, change them if needed, and save the changes to the same dict?
>
> > So for input like this:
> > {'Mobile': 'string', 'context': '', 'order': '7',
> > 'time': 'True'}
>
> > I want to booleanize 'True', turn '7' into an integer, escape
> > '', and ignore 'string'.
>
> > Any elegant Python way to do this?
>
> > -- Gnarlie
>
> How about
>
> data = {
>     'Mobile': 'string',
>     'context': '',
>     'order': '7',
>     'time': 'True'}
> types={'Mobile':str,'context':str,'order':int,'time':bool}
>
> for k,v in data.items():
>     data[k] = types[k](v)

Bit of nit-picking, but:

>>> bool('True')
True
>>> bool('False')
True
>>> bool('')
False
-- 
http://mail.python.org/mailman/listinfo/python-list