Re: MySQL set and enum, calling values

2009-12-12 Thread Victor Subervi
On Fri, Dec 11, 2009 at 8:13 PM, Gabriel Genellina
wrote:

> En Fri, 11 Dec 2009 16:28:23 -0300, Victor Subervi <
> [email protected]> escribió:
>
>  On Fri, Dec 11, 2009 at 3:12 PM, Carsten Haese > >wrote:
>>
>>  Victor Subervi wrote:
>>> > [...] if I go to print, say,
>>> > colFieldValues[20], which is a set, it prints out the whole set:
>>> >
>>> set('Extra-small','Small','Medium','Large','XLarge','XXLarge','XXXLarge')
>>> > But if I print out colFieldValues[20][0], it prints out "s".
>>>
>>> > Also, how can I test
>>> > for it? It's an instance of string. How do I know if it's a set?
>>>
>>> That's a fantastic question. Python thinks it's a string. What makes you
>>> think it's a set?
>>>
>>
>> Right. I'm doing it the ugly way with the truncating tuple and string
>> replace.
>>
>
> Are you sure the column is declared as SET and not, say, VARCHAR?
>

yes


> Anyway, I don't think MySQLdb is able to handle the SET data type
> correctly.
>

Oh, lovely! What do?
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Python for simulating BRDF

2009-12-12 Thread Sharma, R.C.
Dear python users,
Greetings.
I would like to write a simulation program using Python language for
simulating bidirectional reflectance distribution function (BRDF) obtained
from multi-angular remote sensing. I have a program written in C language.
Can I convert it into Python language? In Python, being a higher level
programming language, do we encounter more problems in debugging while
developing such simulating program?
Thank you very much for your kind help in the advance.
Sincerely,
Sharma, R.C
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Open source projects

2009-12-12 Thread Daniel Fetchinson
>> I'm a pretty okay python programmer and I really want to start
>> developing for an open source project. I'm looking for one that
>> preferably deals with networking and isn't as huge as twisted (that's
>> just a preference, not extremely important). Could anyone suggest any
>> projects? I also know C, Perl, Ruby and Java (java least preferred).

Turbogears is a web application server framework which could
definitely use help. It comes in two flavors, tg1 and tg2, they have
their own trees and you would be more than welcome to help in either
of them. See http://www.turbogears.org

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with debugging Lists

2009-12-12 Thread Sancar Saran
On Saturday 12 December 2009 04:52:26 am Gabriel Genellina wrote:
> En Fri, 11 Dec 2009 19:11:38 -0300, Sancar Saran 
> 
> escribió:
> > In php we had print_r function to see entire array structure. After some
> > search I found some equal module named pprint.
> >
> > And some how this module wont work with mod_wsgi it was something about
> > mod_wsgi portability standards.
> >
> > After some research there where some thing about putting some variables
> > in
> > apache config to disable this.
> >
> > And now I can see some dictionary structure in my apache log and I got
> > some
> > errors like
> > r += pprint.pprint(self.data)
> > TypeError: cannot concatenate 'str' and 'NoneType' objects
> 
> The pprint function in the pprint module (that is, pprint.pprint) *prints*
> its argument, and returns nothing -- or, better said, it returns None
> (same as print_r in PHP, without the return parameter set to true)
> 
> > So is there any way to get dictionary structure in string format ?
> 
> You don't need anything special for that. There are two built-in functions
> that convert any object to string: str and repr. str(x) provides a simple
> representation of x (whatever it is), and repr(x) provides a more
> technical view; when possible, eval(repr(x)) should return x.
> For debugging purposes, repr() is your friend.
> pprint.pformat is like the built-in repr(), but provides a better
> formatted representation, with indenting, a maximum width, etc.
> 
> > Another question is. When I import a module from top is it available for
> > later
> > imported modules
> 
> Each module contains its own, separate namespace. If you `import foo` in
> some module, the name `foo` becomes available to be used in that module --
> if you want to use `foo` in another module, you have to `import foo` in
> that other module too.
> 
> Don't worry; after the very first import (which involves locating the
> module, loading and compiling it if necesary, and writing the .pyc file)
> any subsequent imports of the same module just return a new reference to
> the existing, in-memory module object.
> 
Hello Gabriel,

Thanks for support.

repr works as you say and I had some complaints, it wont format the output. 
Finding some string in 100 key dictionary in formatted in single line bit 
problematic. Is it any way to format it ?

Also, very interesting things are happen.

def debug(self):

r = ''
r += repr(self.data)
r += ''

return r

following code works and does not work ever reload ?

One time work another reload wont work.  What I missing ?

And is possible to get this ? (I store the environ here)

r += repr(self.data['environ']['mod_wsgi.listener_port'])

or similar multi sub level elements ?

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


Re: read text file byte by byte

2009-12-12 Thread census
daved170 wrote:

> Hello everybody,
> I need to read a text file byte after byte.
> Eache byte is sent to a function that scramble it
> and I need to write the result to binary file.
>
> I've got some questions -
> 1) How do I read the file byte by byte
> 2) Should I use streams? If so and I get my entire scrambled text in
> stream can I just write it to the binary file?
>
> Thanks
> Dave

f = open ("binaryfile", "r")
bytearray = map (ord, f.read () )

Stores the content of binaryfile in the list bytearray.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: read text file byte by byte

2009-12-12 Thread census
daved170 wrote:

> Hello everybody,
> I need to read a text file byte after byte.
> Eache byte is sent to a function that scramble it
> and I need to write the result to binary file.
>
> I've got some questions -
> 1) How do I read the file byte by byte
> 2) Should I use streams? If so and I get my entire scrambled text in
> stream can I just write it to the binary file?
>
> Thanks
> Dave

OK, now here a complete code to read a file byte by byte, scramble each
byte (with a really complex algorithm in my example) and write the
output to another file.

def scramble (a): return (a + 13) % 256

infile = open ("binaryfile1", "r")
outfile = open ("binaryfile2", "w")

bytearray = map (ord, infile.read () )
scrambled = map (scramble, bytearray)
map (lambda x : outfile.write (chr (x) ), scrambled)

infile.close ()
outfile.flush ()
outfile.close ()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: read text file byte by byte

2009-12-12 Thread Steven D'Aprano
On Sat, 12 Dec 2009 10:35:55 +0100, census wrote:

>> I've got some questions -
>> 1) How do I read the file byte by byte 2) Should I use streams? If so
>> and I get my entire scrambled text in stream can I just write it to the
>> binary file?
>>
>> Thanks
>> Dave
> 
> f = open ("binaryfile", "r")
> bytearray = map (ord, f.read () )
> 
> Stores the content of binaryfile in the list bytearray.

If it's a binary file, you should open it in binary mode:

f = open ("binaryfile", "rb")



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


Re: read text file byte by byte

2009-12-12 Thread Steven D'Aprano
On Fri, 11 Dec 2009 23:16:42 -0800, daved170 wrote:

> Hello everybody,
> I need to read a text file byte after byte. Eache byte is sent to a
> function that scramble it and I need to write the result to binary file.
> 
> I've got some questions -
> 1) How do I read the file byte by byte 

f = open(filename, 'rb')
f.read(1)

will read a single byte.




2) Should I use streams? 

What do you mean by "streams"?



-- 
Steven


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


Re: Spawning an interactive interpreter in a running python process?

2009-12-12 Thread Horace Blegg
Better and better!

You know, I think I knew about those two, I just never connected the dots.
With a little fiddling, I think I can cobble together what I want. My
sincere thanks, sir/ma'am.

On Fri, Dec 11, 2009 at 9:46 PM, Gabriel Genellina
wrote:

> En Sat, 12 Dec 2009 02:11:27 -0300, Horace Blegg 
> escribió:
>
>
>  I wonder if I could cook something up with PyRun_SimpleString("import pdb;
>> pdb.set_trace()"). This bears investigation!
>>
>
> pdb is a debugger, and provides a lot more than you're looking for, I
> presume.
> You may want to look at the code [1] and cmd [2] modules too.
>
> [1] http://docs.python.org/library/code.html
> [2] http://docs.python.org/library/cmd.html
>
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: read text file byte by byte

2009-12-12 Thread census
Steven D'Aprano wrote:

> On Sat, 12 Dec 2009 10:35:55 +0100, census wrote:
>
>>> I've got some questions -
>>> 1) How do I read the file byte by byte 2) Should I use streams? If so
>>> and I get my entire scrambled text in stream can I just write it to the
>>> binary file?
>>>
>>> Thanks
>>> Dave
>> 
>> f = open ("binaryfile", "r")
>> bytearray = map (ord, f.read () )
>> 
>> Stores the content of binaryfile in the list bytearray.
>
> If it's a binary file, you should open it in binary mode:
>
> f = open ("binaryfile", "rb")
>
>
>

Add the "b" flag to both in and out file if you prefer it:

def scramble (a): return (a + 13) % 256

infile = open ("binin", "rb")
outfile = open ("binout", "wb")

bytearray = map (ord, infile.read () )
scrambled = map (scramble, bytearray)
map (lambda x : outfile.write (chr (x) ), scrambled)

infile.close ()
outfile.flush ()
outfile.close ()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When to use mechanize and Windmill library during WebScraping ?

2009-12-12 Thread Javier Collado
Hello,

If a script that uses mechanize fails to find an html node that has
been identified with Firebug, this is probably because that node has
been autogenerated (provided that the expression to get the node is
correct).

As an alternative to verify this, you can try to download the html
page and open it in your favourite editor. If some of the nodes that
you can see in your browser are missing or empty, then one of the
JavaScript scripts in the page should have created/populated it.

If you're in doubt, you can try to use mechanize and, if you have
problems such as the described above, then you can move to windmill or
some other tool that executes JavaScript code before trying to get the
desired data.

Best regards,
Javier

2009/12/11 Raji Seetharaman :
> Hi
>
> For 'Webscraping with Python' mechanize or urllib2 and windmill or selenium
> libraries are used  to download the webpages.
>
> http://www.packtpub.com/article/web-scraping-with-python
>
> The above link makes use of mechanize library to download the web pages.
>
> The below link uses windmill library to download the web pages.
>
> http://www.packtpub.com/article/web-scraping-with-python-part-2
>
> I dont know when to use mechanize or windmill library
>
> It has been said that Windmill library is used when the HTML file is auto
> generated by the JavaScript code.
>
> Also i dont know how to identify whether the HTML file is auto generated by
> the JavaScript code or not ?
>
> Suggest me
>
> Thanks
>
> Raji. S
> http://sraji.wordpress.com/
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Which graph library is best suited for large graphs?

2009-12-12 Thread Tiago de Paula Peixoto
Hi there,

On 12/11/2009 08:12 AM, Wolodja Wentland wrote:
> I really like the API of networkx but have no problem in switching to
> another one (right now)  I have the impression that graph-tool might
> be faster and have a smaller memory footprint than networkx, but am
> unsure about that.

I'm the author of graph-tool, so my opinion may be biased. :-)

Nevertheless, I do think that graph-tool will be faster and have a
smaller memory footprint than networkx, since the graph data structures
and most algorithms are written in C++, using the Boost Graph Library,
whereas networkx is mostly pure python. I have made this library due to
my own requirements of being able to work with large graphs.

The only other library I can think of which may be comparable in
performance is igraph, which is implemented in C. But I think
graph-tool's interface is a bit more polished (my opinion
only). Moreover, since graph-tool uses template metaprograming to obtain
specialized versions of algorithms, it may be that it is even faster
than igraph, but I have at the moment no benchmarks to back this up.

Cheers,
Tiago



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Best way to conduct a google search

2009-12-12 Thread Daniel Dalton
Hi,

I need to do the following in my program:
1. Conduct a google search, supplying the variable "text" to the
search. Is there a google api or something similar I should use?
2. I then need to be able to get the url, of the page, or the html
content, so I can dump it to text.

Thanks,
Dan


signature.asc
Description: Digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Caps Lock State on Windows

2009-12-12 Thread census
N3wCr4Zy wrote:

> how to i get Caps Lock state (on/off) on windows with win32api?

public static extern short GetKeyState(int keyCode)
in user32.dll
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a list/re problem

2009-12-12 Thread Neil Cerutti
On 2009-12-11, Grant Edwards  wrote:
> [s[1:-1] for s in l if (s[0] == s[-1] == '*')]

That last bit doesn't work right, does it, since an == expression
evaluates to True or False, no the true or false value itself?

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


Re: a list/re problem

2009-12-12 Thread Grant Edwards
On 2009-12-11, Neil Cerutti  wrote:
> On 2009-12-11, Grant Edwards  wrote:
>> [s[1:-1] for s in l if (s[0] == s[-1] == '*')]
>
> That last bit doesn't work right, does it, since an == expression
> evaluates to True or False, no the true or false value itself?

It works for me.  Doesn't it work for you?

>From the fine manual (section 5.9. Comparisons):

  Comparisons can be chained arbitrarily, e.g., x < y <= z is
  equivalent to x < y and y <= z, except that y is evaluated
  only once (but in both cases z is not evaluated at all when x
  < y is found to be false).

  


-- 
Grant Edwards   grante Yow! Hand me a pair of
  at   leather pants and a CASIO
   visi.comkeyboard -- I'm living
   for today!
-- 
http://mail.python.org/mailman/listinfo/python-list


pyserial - escape codes transportation

2009-12-12 Thread ObservantP
need help. newbie. pyserial and  dot matrix printer.
issue- escape codes arrive at printer ( verified from hex dump) but do
not get applied.
printer make/model STAR POS printer SP500.
oddly, printer commands not initiated with the escape code .i.e. a
symbol, work fine.


import serial
ser=serial.Serial(0)

ser.bytesize=7
ser.timeout=1
ser.dsrdtr=1
ser.write('PySerial'+'\n')
ser.write('Star SP500'+'\n')



#Double Width On   14   SO = Or ESC W 1 / 0
# Off  20   DC4= 

ser.write('' +'Double Width-Symbol\n')  #Good
ser.write('' +'Reset\n') #Good
ser.write('\n')  #Good

ser.write('Xw1\n'+'Double Width-ESC\n')#Nope ( Nor CHR(027)
'\x1b'
ser.write('Xw0\n'+'Reset\n')   #X is symbol not
showing up here.
ser.write('\n')
ser.write('\n')
ser.write('\n')


ser.close()

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


Re: Graph library for Python

2009-12-12 Thread Tiago de Paula Peixoto
On 12/10/2009 01:57 PM, Bearophile wrote:
> Geremy Condra:
>> Well, we all seem to have reinvented the wheel differently ;)
> 
> Maybe also because they are designed for different purposes.

This is true. For instance, the data structures and most algorithms in
graph-tool are implemented in C++ to achieve good performance, which is
necessary if you are working with very large graphs. I think this
differs from most python graph libraries, which don't have this as a
high priority.

>> Bearophile, Tiago- any interest in trying to combine the
>> best parts of our libraries, with an eye towards eventual
>> integration into the standard library?
> 
> The first thing to do is to ask Guido and Hettinger if they are
> willing to put a "good" graph module into the std lib. If their answer
> is positive for some definition of "good", then we can think about
> doing something.

A crucial element in this hypothetical module would be the main graph
data structure. The simplest approach would be to implement it in pure
python, with lists, dicts and such, as many libraries do. However, this
would rule out its use by high-performance code, which would need a
simpler C-based data structure for direct interaction. On the other
hand, I'm not sure if there is a need for a high performance graph
module in python's standard library...

Cheers,
Tiago



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


parallelpython 1.5.7 crash

2009-12-12 Thread makobu
Hi all,

Hi all,

with the above version of parallelpython, what causes this?

Traceback (most recent call last):
  File "C:\Users\tim\code_base\svn\octopus\parallel_python\pp.py",
line 762, in
__run
sresult = worker.t.receive()
  File "C:\Users\tim\code_base\svn\octopus\parallel_python
\pptransport.py", line
 133, in receive
msg = self.r.read(msg_len)
OverflowError: long int too large to convert to int
Unhandled exception in thread started by >
Traceback (most recent call last):
  File "C:\Users\tim\code_base\svn\octopus\parallel_python\pp.py",
line 771, in
__run
job.finalize(sresult)
UnboundLocalError: local variable 'sresult' referenced before
assignment
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to implement Varient/Tagged Unions/Pattern Matching in Python?

2009-12-12 Thread Kay Schluehr
> BTW, Please don't ask "Why do you want to do like this"

No, I don't ask although it would be the interesting aspect for me ;)

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


Re: power of explicit self?

2009-12-12 Thread John Roth
On Dec 11, 9:04 pm, Fire Crow  wrote:
> I'm looking for an explanation of how explicit self is implimented and
> what features are only possible because of, or are greatly improved,
> because of it. I've always liked explicit self and am looking for the
> computer science behind it, so that I can explain the benefits that I
> see.
>
> I'm also interested in the files/lines of the python source that shows
> how explicit self is implemented if anyone can point out where that
> takes place.
>
> all help welcome

It's not implemented in the compiler. There's a place in the runtime
for invoking a method where the object is inserted at the beginning
of the parameter list. IIRC, that's done by wrapping the function
object.

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


Re: MySQL set and enum, calling values

2009-12-12 Thread Carsten Haese
Victor Subervi wrote:
> On Fri, Dec 11, 2009 at 8:13 PM, Gabriel Genellina
> mailto:[email protected]>> wrote:
> Are you sure the column is declared as SET and not, say, VARCHAR?
> 
> 
> yes

Indeed it is, and knowing that, I can actually decode your original
post. I apologize that I didn't see the forest for all the trees you
gave us.

Your problem is that you are inspecting the metadata of a table to find
out which possible values can be stored in a particular SET column of a
particular table, and when you get the description of the table, you're
getting a Python string object that describes the column, rather than a
list (or set) of strings.

The only solution is the one you already resigned to using, which is to
dissect the string into its parts, as confirmed by this article:
http://dev.mysql.com/tech-resources/articles/mysql-set-datatype.html .

> Anyway, I don't think MySQLdb is able to handle the SET data type
> correctly.
> 
> 
> Oh, lovely! What do?

MySQLdb does too handle the SET data type correctly, or at least as
correctly as the underlying database itself will allow. It seems that
SET-typed values can only be retrieved as strings or integers, and the
latter only by "casting" the value to an integer by adding 0 to it. Oh,
the humanity!

The best advice I can give you is to rethink your data schema and avoid
using the SET data type altogether. (Note that I'm only referring to the
MySQL data type here. There's nothing wrong with Python's Set type.)
The above-mentioned article has a section called "Why you shouldn't use
SET." You should read it.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net

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


Python for simulating BRDF

2009-12-12 Thread Franky Frank
Dear python users,
Greetings.
I would like to write a simulation program using Python language for
simulating bidirectional reflectance distribution function (BRDF) obtained
from multi-angular remote sensing. I have a program written in C language.
Can I convert it into Python language? In Python, being a higher level
programming language, do we encounter more problems in debugging while
developing such simulating program?
Thank you very much for your kind help in the advance.
Sincerely,
Franky Frank
-- 
http://mail.python.org/mailman/listinfo/python-list


Getting Default Values Out of MySQL

2009-12-12 Thread Victor Subervi
Hi;
I'm using MySQLdb. If I do a
cursor.execute('describe myTable;')
it gives me all sorts of data but not my default values. How do I retrieve
them?
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with debugging Lists

2009-12-12 Thread Lie Ryan

On 12/12/2009 8:28 PM, Sancar Saran wrote:

repr works as you say and I had some complaints, it wont format the output.
Finding some string in 100 key dictionary in formatted in single line bit
problematic. Is it any way to format it ?


As has been mentioned, use pprint.pformat() to get what pprint.pprint() 
would print as a str instead of being printed.



Also, very interesting things are happen.

def debug(self):

r = ''
r += repr(self.data)
r += ''

return r

following code works and does not work ever reload ?

One time work another reload wont work.  What I missing ?


What do you mean by it doesn't work? Does it produce the wrong output? 
What's wrong with the output? Does it raises an error? Are you sure it 
isn't because self.data is empty?



And is possible to get this ? (I store the environ here)

r += repr(self.data['environ']['mod_wsgi.listener_port'])

or similar multi sub level elements ?


python had no problem with that.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Spawning an interactive interpreter in a running python process?

2009-12-12 Thread Lie Ryan

On 12/12/2009 3:49 PM, Tim Chase wrote:

 I'm curious, in an academic sense, if it's possible to spawn the
interactive interpreter (>>>) in a running python application. Ideally, I
would like to be able to access the modules, functions and variables the
application can.

Is something like this possible?


you can also use the -i switch to start the interactive interpreter once 
the script reaches execution:

$ python -i myprogram.py
program output
>>> # names that are not been deleted at the end of program execution
>>> # can be accessed here
>>>

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


Re: a list/re problem

2009-12-12 Thread Lie Ryan

import re
r = re.compile('\*(.+)\*')

def f(s):
 m = r.match(s)
 if m:
 return m.group(1)

l = ['asc', '*nbh*', 'jlsdjfdk', 'ikjh', '*jkjsdfjasd*', 'rewr']

n = [y for y in (f(x) for x in l) if y]

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


Re: Getting Default Values Out of MySQL

2009-12-12 Thread Carsten Haese
Victor Subervi wrote:
> Hi;
> I'm using MySQLdb. If I do a
> cursor.execute('describe myTable;')
> it gives me all sorts of data but not my default values.

That function call doesn't "give" any data at all, except for the
rowcount (which would be the number of columns in the table). You must
use some other command to retrieve the result set. What command are you
using, and what are the results?

> How do I
> retrieve them?

In my version of MySQL, he default value is in the fifth column of the
result set.

--
Carsten Haese
http://informixdb.sourceforge.net

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


Re: Getting Default Values Out of MySQL

2009-12-12 Thread Victor Subervi
On Sat, Dec 12, 2009 at 10:54 AM, Carsten Haese wrote:

> Victor Subervi wrote:
> > Hi;
> > I'm using MySQLdb. If I do a
> > cursor.execute('describe myTable;')
> > it gives me all sorts of data but not my default values.
>
> That function call doesn't "give" any data at all, except for the
> rowcount (which would be the number of columns in the table).


Really?

cursor.execute('describe %s;' % store)
storeDescription = cursor.fetchall()
print storeDescription

Prints out this:

(('ID', 'tinyint(5) unsigned', 'NO', 'PRI', None, 'auto_increment'), ('SKU',
'varchar(40)', 'NO', 'UNI', None, ''), ('Category', 'varchar(40)', 'YES',
'', None, ''), ('Name', 'varchar(50)', 'NO', '', None, ''), ('Title',
'varchar(100)', 'NO', '', None, ''), ('Description', 'mediumtext', 'NO', '',
None, ''), ('Price', 'float(8,2)', 'YES', '', None, ''), ('SortFactor',
'int(4)', 'YES', '', '500', ''), ('Availability', 'tinyint(1)', 'NO', '',
None, ''), ('ShipFlatFee', 'float(5,2)', 'NO', '', '10.00', ''),
('ShipPercentPrice', 'tinyint(2) unsigned', 'NO', '', '5', ''),
('ShipPercentWeight', 'tinyint(2) unsigned', 'NO', '', '2', ''),
('Associations', 'varchar(40)', 'NO', '', None, ''), ('TempPrice',
'float(7,2)', 'NO', '', None, ''), ('LastDatePrice', 'date', 'NO', '', None,
''), ('Weight', 'float(7,2)', 'NO', '', None, ''), ('Metal', "enum('14k
gold','18k gold','white gold','silver','tungsten','titanium')", 'NO', '',
None, ''), ('PercentMetal', 'tinyint(2) unsigned', 'NO', '', None, ''),
('pic1', 'blob', 'YES', '', None, ''), ('pic2', 'blob', 'YES', '', None,
''), ('sizes',
"set('Extra-small','Small','Medium','Large','XLarge','XXLarge','XXXLarge')",
'YES', '', None, ''), ('colorsShadesNumbersShort',
"set('blue:99','gray:465945','purple:50404D','navy-blue:CC7722','fuchsia:FF77FF','aqua:7FFFD4','maroon:B03060','black:FF','yellow:9ACD32','teal:E2725B','olive:6B8E23','green:00A550','white:0F4D92','silver:708090','red:FE2712','lime:32CD32')",
'YES', '', None, ''))

In my version of MySQL, he default value is in the fifth column of the
> result set.
>

Nice. As you can see, I only get 4 columns by default. How do I get the
fifth?
TIA,
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a list/re problem

2009-12-12 Thread Lie Ryan

On 12/12/2009 8:24 AM, Peter Otten wrote:


But it is inefficient, because it is matching the regex twice for each
item, and it is a bit ugly.

I could use:


n = []
for x in keys:
 m = r.match(x)
 if m:
 n.append(m.group(1))


It is more efficient, but much uglier.


It's efficient and easy to understand; maybe you have to readjust your
taste.


I agree, it's easy to understand, but it's also ugly because of the 
level of indentation (which is too deep for such a simple problem).



Does anyone have a better solution?


(sorry to ramble around)

A few months ago, I suggested an improvement in the python-ideas list to 
add a post-filter to list-comprehension, somewhere in this line:


a = [f(x) as F for x in l if c(F)]

where the evaluation of f(x) will be the value of F so F can be used in 
the if-expression as a post-filter (complementing list-comps' pre-filter).


Many doubted its usefulness since they say it's easy to wrap in another 
list-comp:

a = [y for y in (f(x) for x in l) if c(y)]
or with a map and filter
a = filter(None, map(f, l))

Up till now, I don't really like the alternatives.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Getting Default Values Out of MySQL

2009-12-12 Thread MRAB

Victor Subervi wrote:



On Sat, Dec 12, 2009 at 10:54 AM, Carsten Haese > wrote:


Victor Subervi wrote:
 > Hi;
 > I'm using MySQLdb. If I do a
 > cursor.execute('describe myTable;')
 > it gives me all sorts of data but not my default values.

That function call doesn't "give" any data at all, except for the
rowcount (which would be the number of columns in the table). 



Really?

cursor.execute('describe %s;' % store)
storeDescription = cursor.fetchall()
print storeDescription

Prints out this:

(('ID', 'tinyint(5) unsigned', 'NO', 'PRI', None, 'auto_increment'), 
('SKU', 'varchar(40)', 'NO', 'UNI', None, ''), ('Category', 
'varchar(40)', 'YES', '', None, ''), ('Name', 'varchar(50)', 'NO', '', 
None, ''), ('Title', 'varchar(100)', 'NO', '', None, ''), 
('Description', 'mediumtext', 'NO', '', None, ''), ('Price', 
'float(8,2)', 'YES', '', None, ''), ('SortFactor', 'int(4)', 'YES', '', 
'500', ''), ('Availability', 'tinyint(1)', 'NO', '', None, ''), 
('ShipFlatFee', 'float(5,2)', 'NO', '', '10.00', ''), 
('ShipPercentPrice', 'tinyint(2) unsigned', 'NO', '', '5', ''), 
('ShipPercentWeight', 'tinyint(2) unsigned', 'NO', '', '2', ''), 
('Associations', 'varchar(40)', 'NO', '', None, ''), ('TempPrice', 
'float(7,2)', 'NO', '', None, ''), ('LastDatePrice', 'date', 'NO', '', 
None, ''), ('Weight', 'float(7,2)', 'NO', '', None, ''), ('Metal', 
"enum('14k gold','18k gold','white 
gold','silver','tungsten','titanium')", 'NO', '', None, ''), 
('PercentMetal', 'tinyint(2) unsigned', 'NO', '', None, ''), ('pic1', 
'blob', 'YES', '', None, ''), ('pic2', 'blob', 'YES', '', None, ''), 
('sizes', 
"set('Extra-small','Small','Medium','Large','XLarge','XXLarge','XXXLarge')", 
'YES', '', None, ''), ('colorsShadesNumbersShort', 
"set('blue:99','gray:465945','purple:50404D','navy-blue:CC7722','fuchsia:FF77FF','aqua:7FFFD4','maroon:B03060','black:FF','yellow:9ACD32','teal:E2725B','olive:6B8E23','green:00A550','white:0F4D92','silver:708090','red:FE2712','lime:32CD32')", 
'YES', '', None, '')) 


In my version of MySQL, he default value is in the fifth column of the
result set.


Nice. As you can see, I only get 4 columns by default. How do I get the 
fifth?



That's strange, I count 6!

For example, the first field has the following columns:

1. 'ID'

2. 'tinyint(5) unsigned'

3. 'NO'

4. 'PRI'

5. None

6. 'auto_increment'

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


Re: Getting Default Values Out of MySQL

2009-12-12 Thread Victor Subervi
On Sat, Dec 12, 2009 at 11:38 AM, MRAB  wrote:

> That's strange, I count 6!
>
> For example, the first field has the following columns:
>
>1. 'ID'
>
>2. 'tinyint(5) unsigned'
>
>3. 'NO'
>
>4. 'PRI'
>
>5. None
>
>6. 'auto_increment'


Dunno why I counted 4 last time. Maybe changed something. Thanks,
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Perl to Python conversion

2009-12-12 Thread Colin W.

On 09-Dec-09 15:33 PM, Martin Schöön wrote:

First off: I am new here and this is my first post after
lurking for quite some time.

Second off: I don't know much Python---yet.

Problem: I have come across a small open source application
that I find quite useful. It does have one major flaw though.
Its output is in imperial units. Converting isn't a big deal
for occasional use but if I start to use this stuff on a
regular basis...

So I down-loaded the source code and found this thing is written
in Perl.

Should I learn enough Perl to add the conversion? Probably
but this may be a nice excuse to get my Python education
going and if I do I might as well re-do the user interface.

If I do re-write this thing in Python I might need to learn both
Perl and Python...

Hence, are there any Perl to Python converters? So far I
have only found bridgekeeper which really is (was?) consultancy.
Apart from that I only find people recommending a manual re-write.

Any thoughts/recommendations?

TIA,

/Martin


Martin,

If you convert the Perl, you continue the other fellow's errors.  If you 
do it yourself, you'll be able to make your own - there should be fewer 
of them.


Google: unit conversion python

you'll have lots of offers.

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


Re: Getting Default Values Out of MySQL

2009-12-12 Thread Carsten Haese
Victor Subervi wrote:
> 
> 
> On Sat, Dec 12, 2009 at 10:54 AM, Carsten Haese  > wrote:
> 
> Victor Subervi wrote:
> > Hi;
> > I'm using MySQLdb. If I do a
> > cursor.execute('describe myTable;')
> > it gives me all sorts of data but not my default values.
> 
> That function call doesn't "give" any data at all, except for the
> rowcount (which would be the number of columns in the table). 
> 
> 
> Really?

Yes, really.

> cursor.execute('describe %s;' % store)
> storeDescription = cursor.fetchall()
> print storeDescription
> 
> Prints out this:
> [snip...]

So it does. And if you look really, really carefully, maybe you'll be
able to tell the difference between this, your original code snippet:

cursor.execute('describe %s;' % store)

And this, which is what *actually* produces your output:

cursor.execute('describe %s;' % store)
storeDescription = cursor.fetchall()
print storeDescription

The "all sorts of data" you referred to in your original post is
obtained in the fetchall() call and then printed in the print statement.
As I said, your output did not come from the execute() call.

As usual, in your original post you didn't include the code that
produces the output that's causing you grief, and you didn't include the
output, either. This information is essential for helping you, and you
have been asked many times before to provide this information.
Persistently asking for help and not providing enough information is, as
has been noted before, a form of rudeness.

--
Carsten Haese
http://informixdb.sourceforge.net

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


Re: Best way to conduct a google search

2009-12-12 Thread Terry Reedy

Daniel Dalton wrote:

Hi,

I need to do the following in my program:
1. Conduct a google search, supplying the variable "text" to the
search. Is there a google api or something similar I should use?
2. I then need to be able to get the url, of the page, or the html
content, so I can dump it to text.


google 'python google api'

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


Re: Python for simulating BRDF

2009-12-12 Thread Terry Reedy

Franky Frank wrote:


I would like to write a simulation program using Python language for
simulating bidirectional reflectance distribution function (BRDF) obtained
from multi-angular remote sensing. I have a program written in C language.
Can I convert it into Python language? 


Yes, but it would be slower unless you use numpy to the heavy calculation.

Or you can wrap your current C program or routines within it so that 
they can be called from Python. If you compile to a .dll or .so, you can 
access function with the ctypes module.


Google gives 20K hits with 'python brdf'

>In Python, being a higher level

programming language, do we encounter more problems in debugging while
developing such simulating program?


If you know Python well, you should have fewer problems. That is why 
people sometimes develop prototypes in Python and then translate to C if 
they need more speed.


Terry Jan Reedy

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


Re: When will Python 3 be fully deployed

2009-12-12 Thread Martin v. Loewis
> In addition to Ned Deily's previous comments, I'd like to note that 2to3
> assumes the source is valid 2.6 code - you have to ensure the code runs
> fine with Python 2.6 before using 2to3 to convert to 3.x

That's wrong - 2to3 works just fine on, say, 2.3 code that has never
been run on 2.6.

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


Re: power of explicit self?

2009-12-12 Thread Fire Crow
> It's not implemented in the compiler. There's a place in the runtime
> for invoking a method where the object is inserted at the beginning
> of the parameter list. IIRC, that's done by wrapping the function
> object.

This is the source of Objects/methodobject.c it look like this is
where
self is added to the argument list, but I'll have to do some more
digging.
thanks for the tip.


 50 PyObject *
 51 PyCFunction_GetSelf(PyObject *op)
 52 {
 53 if (!PyCFunction_Check(op)) {
 54 PyErr_BadInternalCall();
 55 return NULL;
 56 }
 57 return ((PyCFunctionObject *)op) -> m_self;
 58 }

 69
 70 PyObject *
 71 PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
 72 {
...
 75 PyObject *self = PyCFunction_GET_SELF(func);
...
 78 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS |
METH_STATIC |
METH_COEXIST)) {
 79 case METH_VARARGS:
 80 if (kw == NULL || PyDict_Size(kw) == 0)
 81 return (*meth)(self, arg);
 82 break;
 83 case METH_VARARGS | METH_KEYWORDS:
...
126 }

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


Re: a list/re problem

2009-12-12 Thread Nobody
On Fri, 11 Dec 2009 12:49:42 -0800, Ed Keith wrote:

> the following works:
> 
> r = re.compile('\*(.+)\*')
> 
> def f(s):
> m = r.match(s)
> if m:
> return m.group(1)
> else:
> return ''
>   
> n =  [f(x) for x in l if r.match(x)]
> 
> 
> 
> But it is inefficient, because it is matching the regex twice for each
> item, and it is a bit ugly. 

> Does anyone have a better solution?

Use a language with *real* list comprehensions?

Flamebait aside, you can use another level of comprehension, i.e.:

n = [m.group(1) for m in (r.match(x) for x in l) if m]


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


Re: Moving from PHP to Python. Is it Possible

2009-12-12 Thread Tino Wildenhain
MRAB schrieb:
> zeph wrote:
> [snip]
>> 4) It's better to collect all your eventual output into a string that
>> you print - there are examples at [3]. You can import from other
>> modules as needed (even conditionally), grow your string for output,
>> then finally print it like (this example was adapted from one found on
>> [3]):
>>
>> output =  ''
>> output += 'My Page'
>> output += ''
>> output += 'Powers of two\n'
>> for n in range(1,11):
>>   output += ''+str(2**n)+''
>>
>> output += ''
>> print output
>>
>>
>> You can copy-paste this right into your Python interactive shell to
>> see the output. Note: += is inline string concatenation.
>>
> It's better to put the strings into a list and then concatenate them in
> one go:
> 
> output = ['']
> output.append('My Page')
> output.append('')
> output.append('Powers of two\n')
> for n in range(1, 11):
> output.append('%s' % (2 ** n))
> 
> output.append('')
> print ''.join(output)

Actually I'd use a proper template engine in any case. The above
construction of mixing code and representation (or rather code with
code and data for another interpreter - the users browser) is not only
unlucky, it is almost everytime very dangerous.

Keep in mind if you are using a user supplied string, like coming from
a form entry and just include it as above literally into your HTML, you
have created a way of cross site scripting, a very common attack.

To prevent that, you should always propery quote strings for the context
where they are used. Template engines such as Zope Page Templates (also
usable stand allone) are doing this for you.

Regards
Tino


smime.p7s
Description: S/MIME Cryptographic Signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Manipulating MySQL Sets

2009-12-12 Thread Victor Subervi
Hi;
What type does python consider a MySQL Set??

  if isinstance(colValue[0], (str, int, long, float, long,
complex, unicode, list, buffer, xrange, tuple)):
pass
  else:
print 'XXX'

And the following fields pass through this net: Set and datetime. What type
is a datetime?

How do I parse a Set? Can't slice it. What do I do with the &*$( thing?
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Manipulating MySQL Sets

2009-12-12 Thread Carsten Haese
Victor Subervi wrote:
> Hi;
> What type does python consider a MySQL Set??

Let's take a look:

>>> import MySQLdb
>>> conn = MySQLdb.connect(db="carsten", user="blah", passwd="blah")
>>> cur = conn.cursor()
>>> cur.execute("""
... create table pizza (
... id integer,
... toppings set('cheese','sauce','peperoni','mushrooms')
... )
... """)
0L
>>> cur.execute("""
... insert into pizza values(1, 'cheese,sauce,peperoni')
... """)
1L
>>> cur.execute("select * from pizza")
1L
>>> rows = cur.fetchall()
>>> toppings = rows[0][1]
>>> print toppings
cheese,sauce,peperoni
>>> print type(toppings)



Looks like a string to me.

--
Carsten Haese
http://informixdb.sourceforge.net

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


Re: Graph library for Python

2009-12-12 Thread geremy condra
On Thu, Dec 10, 2009 at 10:57 AM, Bearophile  wrote:
> Geremy Condra:
>
>> is there a particular way you want your attribution line to read?
>
> You can just use my nickname (in all lowercase), with the list of
> parts you have used. Don't worry.
>
>
>> Well, we all seem to have reinvented the wheel differently ;)
>
> Maybe also because they are designed for different purposes.
>
>
>> Bearophile, Tiago- any interest in trying to combine the
>> best parts of our libraries, with an eye towards eventual
>> integration into the standard library?
>
> The first thing to do is to ask Guido and Hettinger if they are
> willing to put a "good" graph module into the std lib. If their answer
> is positive for some definition of "good", then we can think about
> doing something.
>
> Several years ago I have suggested to put a graph module in the std
> lib, and the answer was something like: "Put the lib online, and if
> people use it a lot, we'll see to put it into the std lib." In the
> meantime my lib was used by no one and ten other graph libs are used
> (networkx seems among the most used), but I think no one of them has
> shown a strong usage. (In the meantime Hettinger has written and added
> two or three or four GOOD data structures to the std lib using a "fast
> lane", avoiding the step of popular usage test).

Well, I've just concluded a short conversation with Raymond Hettinger,
and I think its fair to characterize him as being opposed to the idea
at present. In addition to the popularity test, he's also noted that
ideally a core CPython dev should be involved in the project. Putting
the two together is, AFAICS, a death knell for any extant graph lib.

Having said that, I'd still like to see how much common ground we
could find among the existing libraries. IMHO, there's a lot more
in common than there is different.

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


Re: read text file byte by byte

2009-12-12 Thread Rhodri James

On Sat, 12 Dec 2009 11:14:13 -, census  wrote:


Steven D'Aprano wrote:


On Sat, 12 Dec 2009 10:35:55 +0100, census wrote:


I've got some questions -
1) How do I read the file byte by byte 2) Should I use streams? If so
and I get my entire scrambled text in stream can I just write it to  
the

binary file?

Thanks
Dave


f = open ("binaryfile", "r")
bytearray = map (ord, f.read () )

Stores the content of binaryfile in the list bytearray.


If it's a binary file, you should open it in binary mode:

f = open ("binaryfile", "rb")





Add the "b" flag to both in and out file if you prefer it:


It's not a matter of preferring.  Depending on your operating system  
and/or version of Python, reading a binary file without the "b" flag will  
give you wrong answers.


--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: umath import error for Numpy builds on OSX 10.6

2009-12-12 Thread hardcoreUFO
On Dec 12, 5:44 pm, Robert Kern  wrote:
> On 2009-12-11 20:55 PM, hardcoreUFO wrote:
>
>
>
>
>
> > On Dec 11, 5:47 pm, Robert Kern  wrote:
>
> >> Right, when the -lnpymath stuff got checked in. Looking at the build log 
> >> you
> >> posted to numpy-discusson, it does appear that the $LDFLAGS is 
> >> obliterating the
> >> intended flags. Please post a build log without setting those flags to
> >> numpy-discussion. A correct link line should look something like this:
>
> >> gcc -arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -g 
> >> -bundle
> >> -undefined dynamic_lookup
> >> build/temp.macosx-10.3-i386-2.5/numpy/core/src/umath/umathmodule_onefile.o
> >> -Lbuild/temp.macosx-10.3-i386-2.5 -lnpymath -o
> >> build/lib.macosx-10.3-i386-2.5/numpy/core/umath.so
>
> > Thanks Robert,
>
> > Here is the log from a build without the LDFLAGS set.  Having a quick
> > look, all I can see are a few warnings and a CAPI version mismatch
> > message. Any insight most appreciated. The build still gives the same
> > umath error.
>
> On numpy-discussion, please. But first, start with a clean checkout. I think 
> you
> have stale files.

Log from build with clean checkout posted to the numpy list.

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


Re: Manipulating MySQL Sets

2009-12-12 Thread Victor Subervi
On Sat, Dec 12, 2009 at 5:11 PM, Carsten Haese wrote:

> Victor Subervi wrote:
> > Hi;
> > What type does python consider a MySQL Set??
>
> Let's take a look:
>
> >>> import MySQLdb
> >>> conn = MySQLdb.connect(db="carsten", user="blah", passwd="blah")
> >>> cur = conn.cursor()
> >>> cur.execute("""
> ... create table pizza (
> ... id integer,
> ... toppings set('cheese','sauce','peperoni','mushrooms')
> ... )
> ... """)
> 0L
> >>> cur.execute("""
> ... insert into pizza values(1, 'cheese,sauce,peperoni')
> ... """)
> 1L
> >>> cur.execute("select * from pizza")
> 1L
> >>> rows = cur.fetchall()
> >>> toppings = rows[0][1]
> >>> print toppings
> cheese,sauce,peperoni
> >>> print type(toppings)
> 
>
>
> Looks like a string to me.
>

Yep, sure does, but it isn't. Again:

  if isinstance(colValue[0], (str, int, long, float, long,
complex, unicode, list, buffer, xrange, tuple)):
pass
  else:
print 'XXX'

and those "strings" printed triple-X. It ain't no string. Besides, if it
were a string, I'd be able to slice it, wouldn't I? Can't slice it either.
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Manipulating MySQL Sets

2009-12-12 Thread Victor Subervi
On Sat, Dec 12, 2009 at 6:07 PM, Victor Subervi wrote:

> On Sat, Dec 12, 2009 at 5:11 PM, Carsten Haese wrote:
>
>> Victor Subervi wrote:
>> > Hi;
>> > What type does python consider a MySQL Set??
>>
>> Let's take a look:
>>
>> >>> import MySQLdb
>> >>> conn = MySQLdb.connect(db="carsten", user="blah", passwd="blah")
>> >>> cur = conn.cursor()
>> >>> cur.execute("""
>> ... create table pizza (
>> ... id integer,
>> ... toppings set('cheese','sauce','peperoni','mushrooms')
>> ... )
>> ... """)
>> 0L
>> >>> cur.execute("""
>> ... insert into pizza values(1, 'cheese,sauce,peperoni')
>> ... """)
>> 1L
>> >>> cur.execute("select * from pizza")
>> 1L
>> >>> rows = cur.fetchall()
>> >>> toppings = rows[0][1]
>> >>> print toppings
>> cheese,sauce,peperoni
>> >>> print type(toppings)
>> 
>>
>>
>> Looks like a string to me.
>>
>
> Yep, sure does, but it isn't. Again:
>
>   if isinstance(colValue[0], (str, int, long, float, long,
> complex, unicode, list, buffer, xrange, tuple)):
> pass
>   else:
> print 'XXX'
>
> and those "strings" printed triple-X. It ain't no string. Besides, if it
> were a string, I'd be able to slice it, wouldn't I? Can't slice it either.
> V
>

PS:

Changed the code to this:

elif col[:3] != 'pic':
  if isinstance(colValue[0], (str, int, long, float, long,
complex, unicode, list, buffer, xrange, tuple)):
pass
  else:
print 'XXX'
if col == 'sizes': # One of those lovely sets
  print colValue[0][0]


throws this lovely error:

 /var/www/html/angrynates.com/cart/display.py
   96 raise
   97   cursor.close()
   98   bottom()
   99
  100 display()
display = 
 /var/www/html/angrynates.com/cart/display.py in display()
   50 print 'XXX'
   51 if col == 'sizes':
   52   print colValue[0][0]
   53 #ourValue = string.split(colValue[0][6:-3], "','")
   54 #   print ourValue
colValue = (Set(['Small', 'Extra-small', 'Medium']),)

TypeError: unindexable object
  args = ('unindexable object',)

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


Re: Manipulating MySQL Sets

2009-12-12 Thread Carsten Haese
Victor Subervi wrote:
> Yep, sure does, but it isn't. Again:
> 
>   if isinstance(colValue[0], (str, int, long, float, long,
> complex, unicode, list, buffer, xrange, tuple)):
> pass
>   else:
> print 'XXX'
> 
> and those "strings" printed triple-X. It ain't no string. Besides, if it
> were a string, I'd be able to slice it, wouldn't I? Can't slice it either.

Well, then colValue[0] is not a string. This in turn probably means that
colValue[0] does not represent the contents of a SET-type column.
However, I can't tell you what it actually is, because you're once again
not providing enough information. We'd need to see where colValue is
coming from to find out what colValue[0] is.

--
Carsten Haese
http://informixdb.sourceforge.net

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


Re: Manipulating MySQL Sets

2009-12-12 Thread MRAB

Victor Subervi wrote:
On Sat, Dec 12, 2009 at 5:11 PM, Carsten Haese > wrote:


Victor Subervi wrote:
 > Hi;
 > What type does python consider a MySQL Set??

Let's take a look:


[snip]


Looks like a string to me.


Yep, sure does, but it isn't. Again:

  if isinstance(colValue[0], (str, int, long, float, long, 
complex, unicode, list, buffer, xrange, tuple)):

pass
  else:
print 'XXX'

and those "strings" printed triple-X. It ain't no string. Besides, if it 
were a string, I'd be able to slice it, wouldn't I? Can't slice it either.



If you want to know what type colValue[0] is, print type(colValue[0]).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Manipulating MySQL Sets

2009-12-12 Thread Carsten Haese
Victor Subervi wrote:
> PS:
> 
> Changed the code to this:
> 
> elif col[:3] != 'pic':
>   if isinstance(colValue[0], (str, int, long, float, long,
> complex, unicode, list, buffer, xrange, tuple)):
> pass
>   else:
> print 'XXX'
> if col == 'sizes': # One of those lovely sets
>   print colValue[0][0]
> 
> 
> throws this lovely error:
> 
>  /var/www/html/angrynates.com/cart/display.py
> 
>96 raise
>97   cursor.close()
>98   bottom()
>99
>   100 display()
> display = 
>  /var/www/html/angrynates.com/cart/display.py
>  in display()
>50 print 'XXX'
>51 if col == 'sizes':
>52   print colValue[0][0]
>53 #ourValue = string.split(colValue[0][6:-3], "','")
>54 #   print ourValue
> colValue = (Set(['Small', 'Extra-small', 'Medium']),)
> 
> TypeError: unindexable object
>   args = ('unindexable object',)

The traceback helpfully shows us that colValue is a 1-tuple whose zeroth
entry, colValue[0], is an actual bona-fide Python Set object. Such
objects aren't indexable, because sets are unordered. That still doesn't
tell us where colValue is coming from, though, so why it is a Python Set
object when you expected it to be a string remains an unsolved mystery.

--
Carsten Haese
http://informixdb.sourceforge.net

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


Dangerous behavior of list(generator)

2009-12-12 Thread Tom Machinski
In most cases, `list(generator)` works as expected. Thus,
`list()` is generally equivalent to `[]`.

Here's a minimal case where this equivalence breaks, causing a serious
and hard-to-detect bug in a program:

  >>> def sit(): raise StopIteration()
  ...
  >>> [f() for f in (lambda:1, sit, lambda:2)]
  Traceback (most recent call last):
File "", line 1, in 
File "", line 1, in sit
  StopIteration
  >>> list(f() for f in (lambda:1, sit, lambda:2))
  [1]

I was bitten hard by this inconsistency when sit() was returning the
idiom `(foo for foo in bar if foo.is_baz()).next()`. The nonexistence
of a foo with is_baz() True in that query raises an exception as
designed, which expresses itself when I use the list comprehension
version of the code above; the generator version muffles the error and
silently introduces a subtle, confusing bug: `lambda:2` is never
reached, and a truncated list of 1 element (instead of 3) is
"successfully" generated..

Just wondered what you guys think,

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


parse a string of parameters and values

2009-12-12 Thread bsneddon
I have a problem that I can come up with a brute force solution to
solve but it occurred to me that there may be an
 "one-- and preferably only one --obvious way to do it".

I am going to read a text file that is an export from a control
system.
It has lines with information like

base=1 name="first one" color=blue

I would like to put this info into a dictionary for processing.
I have looked at optparse and getopt maybe they are the answer but
there could
be and very straight forward way to do this task.

Thanks for your help
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: read text file byte by byte

2009-12-12 Thread Tim Chase

Steven D'Aprano wrote:
2) Should I use streams? 


What do you mean by "streams"?


they're what come out of proton packs...just don't cross them. 
It would be bad.


-tkc

(I suspect the OP is a Java/C++ programmer where "streams" are 
somewhat akin to generators, but less powerful; so the answer is 
"you can if you want, but it may not get you what you think you 
want")







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


Re: Dangerous behavior of list(generator)

2009-12-12 Thread Benjamin Kaplan
On Sat, Dec 12, 2009 at 7:15 PM, Tom Machinski  wrote:
> In most cases, `list(generator)` works as expected. Thus,
> `list()` is generally equivalent to `[ expression>]`.
>

Actually, it's list(generator) vs. a list comprehension. I agree that
it can be confusing, but Python considers them to be two different
constructs.

>>> list(xrange(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [xrange(10)]
[xrange(10)]



> Here's a minimal case where this equivalence breaks, causing a serious
> and hard-to-detect bug in a program:
>
>  >>> def sit(): raise StopIteration()
>  ...
>  >>> [f() for f in (lambda:1, sit, lambda:2)]
>  Traceback (most recent call last):
>    File "", line 1, in 
>    File "", line 1, in sit
>  StopIteration
>  >>> list(f() for f in (lambda:1, sit, lambda:2))
>  [1]
>
> I was bitten hard by this inconsistency when sit() was returning the
> idiom `(foo for foo in bar if foo.is_baz()).next()`. The nonexistence
> of a foo with is_baz() True in that query raises an exception as
> designed, which expresses itself when I use the list comprehension
> version of the code above; the generator version muffles the error and
> silently introduces a subtle, confusing bug: `lambda:2` is never
> reached, and a truncated list of 1 element (instead of 3) is
> "successfully" generated..
>
> Just wondered what you guys think,
>
>  -- Tom
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parallelpython 1.5.7 crash

2009-12-12 Thread zeph
I looked in the browsable svn repo for parallel python at
http://parallelpython.googlecode.com/svn/trunk/

1) OverflowError: long int too large to convert to int

This is a bug in their code (comments are mine) in pytransport.py:

size_packed = self.r.read(struct.calcsize("!Q"))  # reads in 8-
character string
msg_len = struct.unpack("!Q", size_packed)[0] # unpacks to a long
value, e.g. "12345678" -> 3544952156018063160L
msg = self.r.read(msg_len)#
3544952156018063160L is too long for file.read, which I presume must
be able to cast as an int

As an example:
>>> sys.stdin.read(3544952156018063160L)
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: long int too large to convert to int



2) UnboundLocalError: local variable 'sresult' referenced before
assignment

This is another bug in their code. This is an error in pp.py (again,
comments are mine, some code omitted for brevity):

try:
  # ...snip...
  sresult = worker.t.receive()   # sets sresult
except:
  if self.__exiting:
return
  else:
# at this point, __run doesn't return, but sresult was not defined
since the
# execution was unwound because, presumable, the first fatal you
got, the OverflowError
sys.excepthook(*sys.exc_info())
# ...snip...
job.finalize(sresult)  # UnboundLocalError here from missing sresult


You can submit these as a bug report to the author if you want - feel
free to copy-paste or link to this post :-)

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


Re: Dangerous behavior of list(generator)

2009-12-12 Thread Ned Deily
In article 
,
 Benjamin Kaplan  wrote:
> On Sat, Dec 12, 2009 at 7:15 PM, Tom Machinski  
> wrote:
> > In most cases, `list(generator)` works as expected. Thus,
> > `list()` is generally equivalent to `[ > expression>]`.
> Actually, it's list(generator) vs. a list comprehension. I agree that
> it can be confusing, but Python considers them to be two different
> constructs.
> 
> >>> list(xrange(10))
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> >>> [xrange(10)]
> [xrange(10)]

That's not a list comprehension, that's a list with one element.

>>> [x for x in xrange(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 Now *that's* a list comprehension. 

-- 
 Ned Deily,
 [email protected]

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


Re: Dangerous behavior of list(generator)

2009-12-12 Thread Benjamin Kaplan
On Sat, Dec 12, 2009 at 9:01 PM, Ned Deily  wrote:
> In article
> ,
>  Benjamin Kaplan  wrote:
>> On Sat, Dec 12, 2009 at 7:15 PM, Tom Machinski 
>> wrote:
>> > In most cases, `list(generator)` works as expected. Thus,
>> > `list()` is generally equivalent to `[> > expression>]`.
>> Actually, it's list(generator) vs. a list comprehension. I agree that
>> it can be confusing, but Python considers them to be two different
>> constructs.
>>
>> >>> list(xrange(10))
>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> >>> [xrange(10)]
>> [xrange(10)]
>
> That's not a list comprehension, that's a list with one element.
>
 [x for x in xrange(10)]
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>
>  Now *that's* a list comprehension. 
>

I know. But the OP was wondering why list() was
behaving differently than [] and I was pointing
out that list comprehensions are considered their own syntax- the list
comprehension [x for x in xrange(10)] is different than [(x for x in
xrange(10)].
> --
>  Ned Deily,
>  [email protected]
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dangerous behavior of list(generator)

2009-12-12 Thread Ned Deily
In article ,
 Ned Deily  wrote:

> In article 
> ,
>  Benjamin Kaplan  wrote:
> > On Sat, Dec 12, 2009 at 7:15 PM, Tom Machinski  
> > wrote:
> > > In most cases, `list(generator)` works as expected. Thus,
> > > `list()` is generally equivalent to `[ > > expression>]`.
> > Actually, it's list(generator) vs. a list comprehension. I agree that
> > it can be confusing, but Python considers them to be two different
> > constructs.
> > 
> > >>> list(xrange(10))
> > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> > >>> [xrange(10)]
> > [xrange(10)]
> 
> That's not a list comprehension, that's a list with one element.
> 
> >>> [x for x in xrange(10)]
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> 
>  Now *that's* a list comprehension. 

Which is not quite the point Benjamin was trying to make - sorry!

Consulting the adjacent sections on "List displays" and "Generator 
expressions" in the Language Reference:

http://docs.python.org/reference/expressions.html#list-displays

for generator expressions "the parentheses can be omitted on calls with 
only one argument " but the expressions in a list_comprehension are not 
in a call context.  So there is no ambiguity: [] 
requires parens around the generator expression and that list display 
produces a list with one element as Benjamin points out.

-- 
 Ned Deily,
 [email protected]

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


Re: parse a string of parameters and values

2009-12-12 Thread Steven D'Aprano
On Sat, 12 Dec 2009 16:16:32 -0800, bsneddon wrote:

> I have a problem that I can come up with a brute force solution to solve
> but it occurred to me that there may be an
>  "one-- and preferably only one --obvious way to do it".

I'm not sure that "brute force" is the right description here. Generally, 
"brute force" is used for situations where you check every single 
possible value rather than calculate the answer directly. One classical 
example is guessing the password that goes with an account. The brute 
force attack is to guess every imaginable password -- eventually you'll 
find the matching one. A non-brute force attack is to say "I know the 
password is a recent date", which reduces the space of possible passwords 
from many trillions to mere millions.

So I'm not sure that brute force is an appropriate description for this 
problem. One way or another you have to read every line in the file. 
Whether you read them or you farm the job out to some pre-existing 
library function, they still have to be read.


> I am going to read a text file that is an export from a control system.
> It has lines with information like
> 
> base=1 name="first one" color=blue
> 
> I would like to put this info into a dictionary for processing. 

Have you looked at the ConfigParser module?

Assuming that ConfigParser isn't suitable, you can do this if each 
key=value pair is on its own line:


d = {}
for line in open(filename, 'r'):
if not line.strip():
# skip blank lines
continue
key, value = line.split('=', 1)
d[key.strip()] = value.strip()


If you have multiple keys per line, you need a more sophisticated way of 
splitting them. Something like this should work:

d = {}
for line in open(filename, 'r'):
if not line.strip():
continue
terms = line.split('=')
keys = terms[0::2]  # every second item starting from the first
values = terms[1::2]  # every second item starting from the second
for key, value in zip(keys, values):
d[key.strip()] = value.strip()




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


Re: parse a string of parameters and values

2009-12-12 Thread John Machin
Steven D'Aprano  REMOVE-THIS-cybersource.com.au> writes:

> 
> On Sat, 12 Dec 2009 16:16:32 -0800, bsneddon wrote:
> 

> 
> > I am going to read a text file that is an export from a control system.
> > It has lines with information like
> > 
> > base=1 name="first one" color=blue
> > 
> > I would like to put this info into a dictionary for processing. 
> 
> Have you looked at the ConfigParser module?
> 
> Assuming that ConfigParser isn't suitable, you can do this if each 
> key=value pair is on its own line:
> [snip]
> If you have multiple keys per line, you need a more sophisticated way of 
> splitting them. Something like this should work:
> 
> d = {}
> for line in open(filename, 'r'):
> if not line.strip():
> continue
> terms = line.split('=')
> keys = terms[0::2]  # every second item starting from the first
> values = terms[1::2]  # every second item starting from the second
> for key, value in zip(keys, values):
> d[key.strip()] = value.strip()
> 

There appears to be a problem with the above snippet, or you have a strange
interpretation of "put this info into a dictionary":

| >>> line = 'a=1 b=2 c=3 d=4'
| >>> d = {}
| >>> terms = line.split('=')
| >>> print terms
| ['a', '1 b', '2 c', '3 d', '4']
| >>> keys = terms[0::2]  # every second item starting from the first
| >>> values = terms[1::2]  # every second item starting from the second
| >>> for key, value in zip(keys, values):
| ... d[key.strip()] = value.strip()
| ...
| >>> print d
| {'a': '1 b', '2 c': '3 d'}
| >>>

Perhaps you meant

terms = re.split(r'[= ]', line)

which is an improvement, but this fails on cosmetic spaces e.g. a = 1  b = 2 ...

Try terms = filter(None, re.split(r'[= ]', line))

Now we get to the really hard part: handling the name="first one" in the OP's
example. The splitting approach has run out of steam.

The OP will need to divulge what is the protocol for escaping the " character if
it is present in the input. If nobody knows of a packaged solution to his
particular scheme, then he'll need to use something like pyparsing.




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


Re: read text file byte by byte

2009-12-12 Thread daved170
On Dec 13, 2:34 am, Dennis Lee Bieber  wrote:
> On Sat, 12 Dec 2009 10:46:01 +0100, census 
> declaimed the following in gmane.comp.python.general:
>
>
>
> > def scramble (a): return (a + 13) % 256
>
>         I'll see your modulo rot 13 and raise with a exclusive or...
>
> -=-=-=-=-
>
> import sys
>
> def scramble(block, key="don't look"):
>     copies = int(len(block) / len(key)) + 1
>     keystring = key * copies
>     return "".join([ chr( ord(block[i])
>                           ^ ord(keystring[i]))
>                      for i in range(len(block))])
>
> def process(fin, fout, key=None):
>     din = open(fin, "rb")
>     dout = open(fout, "wb")
>     while True:
>         block = din.read(1024)
>         if not block: break
>         if key is None:
>             block = scramble(block)
>         else:
>             block = scramble(block, key)
>         dout.write(block)
>     dout.close()
>     din.close()
>
> if __name__ == "__main__":
>     fin = sys.argv[1]
>     fout = sys.argv[2]
>     if len(sys.argv) > 3:
>         key = sys.argv[3]
>     else:
>         key = None
>     process(fin, fout, key)
> --
>         Wulfraed         Dennis Lee Bieber               KD6MOG
>         [email protected]      HTTP://wlfraed.home.netcom.com/


Thank you all.
Dennis I really liked you solution for the issue but I have two
question about it:
1) My origin file is Text file and not binary
2) I need to read each time 1 byte. I didn't see that on your example
code.
Thanks again All of you
Dave
-- 
http://mail.python.org/mailman/listinfo/python-list


Nanoengineer-1 Simulator

2009-12-12 Thread technologiclee

This is from a thread started at the Open Manufacturing Group.

It is about the Nanoengineer-1 molecular modeling program.
It is released under GPL license. I would like to know if this project
can be forked and continued - as development seems to have ceased, but
it is still the best software of its kind that I can find. All the
details are in the thread.

http://groups.google.com/group/openmanufacturing/browse_thread/thread/aff728b7182ebd44

Specifically, I would like to ask this community about an error trying
to use the simulator:

This is the error from Netbeans when I try to run the simulator. Any
suggestions?

error trying to import dylib sim: : No
module
named sim
  [runSim.py:787]
sim parameters used by NE1 read from:
[/home/lee/nesuite1.1.12./cad/plugins/NanoDynamics-1/sim-params.txt]
bug in simulator-calling code: :
SimRunner
instance has no attribute 'system_parameters_file'
  [runSim.py:372] [runSim.py:970] [runSim.py:1089]
exception opening trace file
'/home/lee/Nanorex/Untitled.2009-12-13-00-18-10-trace.txt': : [Errno 2] No such file or directory:
'/home/lee/Nanorex/Untitled.2009-12-13-00-18-10-trace.txt'
  [runSim.py:1936]

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


Re: read text file byte by byte

2009-12-12 Thread Steven D'Aprano
On Sat, 12 Dec 2009 22:15:50 -0800, daved170 wrote:

> Thank you all.
> Dennis I really liked you solution for the issue but I have two question
> about it:
> 1) My origin file is Text file and not binary 

That's a statement, not a question.


> 2) I need to read each time 1 byte.

f = open(filename, 'r')  # open in text mode
f.read(1)  # read one byte


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


Re: parse a string of parameters and values

2009-12-12 Thread Steven D'Aprano
On Sun, 13 Dec 2009 05:52:04 +, John Machin wrote:

> Steven D'Aprano  REMOVE-THIS-cybersource.com.au> writes:
[snip]
>> If you have multiple keys per line, you need a more sophisticated way
>> of splitting them. Something like this should work:
[...]
> There appears to be a problem with the above snippet, or you have a
> strange interpretation of "put this info into a dictionary":


D'oh!

In my defence, I said it "should" work, not that it did work!


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


Re: read text file byte by byte

2009-12-12 Thread Lie Ryan

On 12/13/2009 5:15 PM, daved170 wrote:

Thank you all.
Dennis I really liked you solution for the issue but I have two
question about it:
1) My origin file is Text file and not binary
2) I need to read each time 1 byte. I didn't see that on your example
code.


That's where you're confusing things. The counting unit in text is 
characters, not bytes. Text is binary as well, it's just binary encoded 
in specific way (like ASCII or UTF-8), and computers decoded that binary 
stream into characters.


What you actually need? Reading the text character-per-character OR 
treating an encoded text as binary and reading it byte-per-byte.


Rather, why don't you explain the problem you're trying to solve so we 
can see which you actually need.

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


Re: read text file byte by byte

2009-12-12 Thread Dave Angel

daved170 wrote:

On Dec 13, 2:34 am, Dennis Lee Bieber  wrote:
  

On Sat, 12 Dec 2009 10:46:01 +0100, census 
declaimed the following in gmane.comp.python.general:





def scramble (a): return (a + 13) % 256
  

I'll see your modulo rot 13 and raise with a exclusive or...

-=-=-=-

import sys

def scramble(block, key=on't look"):
copies =nt(len(block) / len(key)) + 1
keystring =ey * copies
return "".join([ chr( ord(block[i])
  ^ ord(keystring[i]))
 for i in range(len(block))])

def process(fin, fout, key=ne):
din =pen(fin, "rb")
dout =pen(fout, "wb")
while True:
block =in.read(1024)
if not block: break
if key is None:
block =cramble(block)
else:
block =cramble(block, key)
dout.write(block)
dout.close()
din.close()

if __name__ ="__main__":
fin =ys.argv[1]
fout =ys.argv[2]
if len(sys.argv) > 3:
key =ys.argv[3]
else:
key =one
process(fin, fout, key)
--
Wulfraed Dennis Lee Bieber   KD6MOG
[email protected]  HTTP://wlfraed.home.netcom.com/




Thank you all.
Dennis I really liked you solution for the issue but I have two
question about it:
1) My origin file is Text file and not binary
2) I need to read each time 1 byte. I didn't see that on your example
code.
Thanks again All of you
Dave

  
If you really need to see each byte of the file, you need to open it as 
binary.  You can then decide that the bytes represent text, in some 
encoding.  If you don't use the "b" flag, the library may change the 
newline characters out from under you.  You have to decide if that matters.


I may have missed it, but I don't think you ever explained why you 
insist on the data being read one byte at a time.  Usually, it's more 
efficient to read it into a buffer, and process that one byte at a 
time.  But in any case, you can supply your own count to read().  
Instead of using 1024, use 1.


DaveA

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