Re: Problem building Python extension

2009-02-14 Thread martijnsteenwijk
On 13 feb, 12:45, Christian Heimes  wrote:
> martijnsteenw...@gmail.com wrote:
> > Thanks a lot for your reply. I downloaded & installed Visual C# 2008
> > express, but unfortunately this doesn't change anything in running the
> > setup file. Unfortunately, still no pyd file is produced...
>
> > Did I something wrong?
>
> Yeah, you installed the C# environment. Python is written in C, so you
> have to get Visual Studio C++ 2008. However a recent version of MinGW32
> is sufficient to build most extensions.
>
> Christian

Hi Christian,

Thanks for your reply. In mean time, I discovered the C++ thing
already ;-).
Now I installed MinGW, but still the same happens. If I run the setup
file, the same output comes on the command line and no pyd file is
created... GRRR

Any suggestions?

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


Re: Pickling classes (not class instances)

2009-02-14 Thread Nicolas M . Thiéry
 Dear python developers,

I got no answer to my previous post on this thread "Pickling classes
(not class instances)".
This issue is a show stopper for our project. Any suggestion for where
to ask?

Thanks in advance!

Best regards,
Nicolas

> Purpose of this e-mail:
> ---
>
> How to customize how a class (not an instance of a class!!!) is
> pickled?
>
> Example:
> ==
> class metaclass(type):
> def __new__(mcs, name, bases, dict):
> print " running metaclass.__new__"
> return type.__new__(mcs, name, bases, dict)
> #
> def __reduce_class__(self):
> print "reducing a class"
> # do the real work
>
> c = metaclass("foo", (object,), dict())
>
> import copy_reg
> copy_reg.pickle(metaclass, metaclass.__reduce_class__)
>
> pickle.dumps(c)
> ---
> PicklingError Traceback (most recent call
> last)
> ...
> PicklingError: Can't pickle : it's not found as
> __main__.foo
> ==
>
> Context:
> 
>
> I am working on the Sage project (www.sagemath.org), and more
> precisely on the category infrastructure. The code is building lots of
> classes on the fly by composing preexisting classes by inheritance
> (for the curious, see the doc of the class Category 
> inhttp://sage.math.washington.edu:2144/file/1567cea09170/categories-nt).
>
> Since those classes are built on the fly, they cannot be pickled with
> the default mechanism of name lookup. A proper pickling would be to
> rebuild the class anew. Nothing problematic, except for the question
> above.
>
> Discussion:
> ---
>
> It sounds like copy_reg would be the natural way to go (as in the
> example above). However, its documentation suggests that it explicitly
> is not intended for pickling classes, e.g. first paragraph of:
>
> http://docs.python.org/library/copy_reg.html#module-copy_reg
>
> is:
>
>   The copy_reg module provides support for the pickle and cPickle
>   modules. The copy module is likely to use this in the future as
>   well.  It provides configuration information about object
>   constructors which are not classes. Such constructors may be factory
>   functions or class instances.
>
> And indeed, looking up at the code of pickle (l. 289-299 of pickle.py)
> (and similarly in cpickle), the copy-reg dispatch is explicit bypassed
> for metaclasses:
>
> # Check for a class with a custom metaclass; treat as regular
> class
> try:
> issc = issubclass(t, TypeType)
> except TypeError: # t is not a class (old Boost; see SF
> #502085)
> issc = 0
> if issc:
> self.save_global(obj)
> return
>
> # Check copy_reg.dispatch_table
> reduce = dispatch_table.get(t)
>
> Which causes the failure above.
>
> Is there a specific reason for this restriction?
>
> Would it be thinkable to move up the copy reg dispatch before the
> metaclass treatment in pickle and cPickle? I did it locally, and it
> fixed my problem.
>
> If not, what would be the right way to achieve this?
>
> Many thanks in advance!
>
> Best regards,
> Nicolas
> --
> Nicolas M. Thiéry "Isil" http://Nicolas.Thiery.name/

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


Re: Problem building Python extension

2009-02-14 Thread martijnsteenwijk
On 14 feb, 09:04, martijnsteenw...@gmail.com wrote:
> On 13 feb, 12:45, Christian Heimes  wrote:
>
> > martijnsteenw...@gmail.com wrote:
> > > Thanks a lot for your reply. I downloaded & installed Visual C# 2008
> > > express, but unfortunately this doesn't change anything in running the
> > > setup file. Unfortunately, still no pyd file is produced...
>
> > > Did I something wrong?
>
> > Yeah, you installed the C# environment. Python is written in C, so you
> > have to get Visual Studio C++ 2008. However a recent version of MinGW32
> > is sufficient to build most extensions.
>
> > Christian
>
> Hi Christian,
>
> Thanks for your reply. In mean time, I discovered the C++ thing
> already ;-).
> Now I installed MinGW, but still the same happens. If I run the setup
> file, the same output comes on the command line and no pyd file is
> created... GRRR
>
> Any suggestions?
>
> Martijn

Hmmm, problem fixed :-)
Downloaded the C++ compiler, runned the Visual Studio 2008 command
prompt and it works!
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] RELEASED Python 3.0.1

2009-02-14 Thread Martin v. Löwis
> Any chance of getting a Mac installer for this one?

Chances are non-zero, yes.

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


Re: Pickling classes (not class instances)

2009-02-14 Thread Aaron Brady
On Feb 14, 2:19 am, Nicolas M. Thiéry 
wrote:
>          Dear python developers,
>
> I got no answer to my previous post on this thread "Pickling classes
> (not class instances)".
> This issue is a show stopper for our project. Any suggestion for where
> to ask?
snip to
http://groups.google.com/group/comp.lang.python/browse_thread/thread/66c282afc04aa39c/4d9304aa0cc791c2#4d9304aa0cc791c2
> > How to customize how a class (not an instance of a class!!!) is
> > pickled?
snip
> > Is there a specific reason for this restriction?
>
> > Would it be thinkable to move up the copy reg dispatch before the
> > metaclass treatment in pickle and cPickle? I did it locally, and it
> > fixed my problem.
>
> > If not, what would be the right way to achieve this?

Hi Nicolas,

I couldn't get yours to work.  I also tried defining '__reduce__' and
'__getstate__' in the metaclass; they didn't work either.

Can you just subclass Pickler, and override the 'save' method?

> > Is there a specific reason for this restriction?

It sounds very odd.  I'm not a core dev, so I can't say.

I also tried defining '__subclasshook__' to convince 'metaclass' it is
not a subclass of 'type'.  It didn't work, but 'register' was not
working the way I wanted.

Perhaps you could define your own 'custom_type' type, which looks and
acts just like type, but just doesn't pass the 'subclass' test.  It
may be as simple as just cutting-and-pasting the TypeObject definition
from the core.


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


Re: Levenshtein word comparison -performance issue

2009-02-14 Thread Peter Otten
Gabriel Genellina wrote:

> En Fri, 13 Feb 2009 08:16:00 -0200, S.Selvam Siva 
> escribió:
> 
>> I need some help.
>> I tried to find top n(eg. 5) similar words for a given word, from a
>> dictionary of 50,000 words.
>> I used python-levenshtein module,and sample code is as follow.
>>
>> def foo(searchword):
>> disdict={}
>> for word in self.dictionary-words:
>>distance=Levenshtein.ratio(searchword,word)
>>disdict[word]=distance
>> """
>>  sort the disdict dictionary by values in descending order
>> """
>> similarwords=sorted(disdict, key=disdict.__getitem__, reverse=True)
>>
>> return similarwords[:5]
> 
> You may replace the last steps (sort + slice top 5) by heapq.nlargest - at
> least you won't waste time sorting 49995 irrelevant words...
> Anyway you should measure the time taken by the first part (Levenshtein),
> it may be the most demanding. I think there is a C extension for this,
> should be much faster than pure Python calculations.
> 

[I didn't see the original post]

You can use the distance instead of the ratio and put the words into bins of
the same length. Then if you find enough words with a distance <= 1 in the
bin with the same length as the search word you can stop looking.

You might be able to generalize this approach to other properties that are
fast to calculate and guarantee a minimum distance, e. g. set(word).

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


Re: codecs.open and buffering modes...

2009-02-14 Thread Sam
Hmmm...no one knows the reason for the discrepancy?  Should I post on
the developers' list to see if anyone knows?

Thanks

On Feb 9, 6:19 pm, Sam  wrote:
> codecs.open defaults to line buffering.  But open defaults to using
> the system buffer size.  Why the discrepancy?  Is it different for a
> reason?
>
> How do these choices affect performance?  (Particularly under Linux).
>
> Thanks

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


Re: best set of modules for web automation without javascript

2009-02-14 Thread coldpizza
You should have a look at twill:
   http://twill.idyll.org

It seems to be no longer maintained, but will probably do most of what
you expect. And it is built on top of mechanize.


On Feb 13, 4:04 pm, News123  wrote:
> Hi,
>
> I'd like to do some web automation with python 2.5
> - https:
> - a cookiejar
> - some forms to be filled in
>
> what is the best set of modules.
>
> As far as I understood, there is httplib, but it seems (if I understood
> well) to be incoompatible with cookielib
>
> I'm a newcomer to webautomation with python and would be thankful for
> good suggestions.
>
> I used so far perl with LWP::UserAgent HTTP::Cookies and some module (I
> forgot the name) to handle parsing and filling in Forms.
>
> thanks in advance for any pointers opinions
>
> N

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


Re: best set of modules for web automation without javascript

2009-02-14 Thread coldpizza
And btw, Selenium scripts can be exported to Python and run under
Selenium Remote Control server.

I'd say this is the most flexible and advanced way to do webtesting,
since you can have a single script that runs with many browsers
(opera, firefox, ie, etc), and on many platforms.

And combined with HTMLTestRunner you can get beautiful test reports.

On Feb 13, 8:01 pm, Matias Surdi  wrote:
> You should give a look to Selenium. It's great.
>
> http://www.openqa.org
>
>
>
> News123 wrote:
> > Hi,
>
> > I'd like to do some web automation with python 2.5
> > - https:
> > - a cookiejar
> > - some forms to be filled in
>
> > what is the best set of modules.
>
> > As far as I understood, there is httplib, but it seems (if I understood
> > well) to be incoompatible with cookielib
>
> > I'm a newcomer to webautomation with python and would be thankful for
> > good suggestions.
>
> > I used so far perl with LWP::UserAgent HTTP::Cookies and some module (I
> > forgot the name) to handle parsing and filling in Forms.
>
> > thanks in advance for any pointers opinions
>
> > N
> > --
> >http://mail.python.org/mailman/listinfo/python-list

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


Re: Thank you, Tkinter. (easy to use)

2009-02-14 Thread DLitgo
On Feb 12, 12:39 am, r  wrote:
> Hello,
>
> Tkinter is a great GUI toolkit, for what it lacks in prettiness it
> more than makes up for in simple and quick GUI building. I think this
> is the main reason Tkinter continues to be Python's built-in GUI
> toolkit. It is a great place to start for those with no GUI
> experience. Sure it will never be as rich as wxPython or the like, but
> that is not what Tkinter is made for.
>
> I use Tkinter for all my tools that need a UI, and others as well. The
> only complaint i have is the poor support for image types i really
> wish there where at least support for one good image like jpg, png,
> and full color bitmaps.The canvas widget could also use a little more
> functionality, but hey maybe one day i will have time to polish it up
> a bit.

There is the Python Image Library (PIL), but when I tried to install
it I of course had to install the JPEG package as well which didn't
install correctly. After trying several times to install it (the JPEG
package that is, PIL installed fine, but I only wanted it for the
extra image support) I just gave up.

Does anyone know of a quick and easy install for PIL + JPEG for Mac OS
X (10.5)?
--
http://mail.python.org/mailman/listinfo/python-list


Re: String concatenation performance with +=

2009-02-14 Thread Sammo
On Feb 14, 4:47 pm, Steven D'Aprano  wrote:
> > Sammo  gmail.com> writes:
> >> String concatenation has been optimized since 2.3, so using += should
> >> be fairly fast.
>
> > This is implementation dependent and shouldn't be relied upon.
>
> It's also a fairly simple optimization and really only applies to direct
> object access, not items or attributes.
>
> >> Why is the second test so much slower?
>
> > Probably several reasons:
>
> > 1. Function call overhead is quite large compared to these simple
> > operations. 2. You are resolving attribute names.
>
> 3. Because the optimization isn't applied in this case.

Thanks Steven -- that's the answer I was looking for. The += string
concatenation optimization only applies to local variables. In my
case, I incorrectly assumed it applied to attributes.

Can you point me to any references regarding the limitations of the
optimization? I guess it's another Python idiom that's documented
somewhere ...
--
http://mail.python.org/mailman/listinfo/python-list


Re: String concatenation performance with +=

2009-02-14 Thread Sammo
On Feb 14, 5:33 pm, Steven D'Aprano  wrote:
> > AFAIK, using list mutation and "".join only improves performance if
> > the "".join is executed outside of the loop.
>
> Naturally. If you needlessly join over and over again, instead of delaying
> until the end, then you might as well do string concatenation without the
> optimization.
>
> join() isn't some magical function that makes your bugs go away. You have to
> use it sensibly. What you've done is a variant of Shlemiel the
> road-painter's algorithm:
>
> http://www.joelonsoftware.com/articles/fog000319.html
>
> Perhaps you have to repeatedly do work on the *temporary* results in between
> loops? Something like this toy example?
>
> s = ""
> block = "abcdefghijklmnopqrstuvwxyz"*1000
> for i in xrange(1000):
>     s += block
>     # do something with the partially concatenated string
>     print "partial length is", len(s)
> # all finished
> do_something(s)
>
> You've written that using join:
>
> L = []
> block = "abcdefghijklmnopqrstuvwxyz"*1000
> for i in xrange(1000):
>     L.append(block)
>     # do something with the partially concatenated string
>     L = [ ''.join(L) ]
>     print "partial length is", len(L[0])
> # all finished
> s = ''.join(L)
> do_something(s)
>
> Okay, but we can re-write that more sensibly:
>
> L = []
> block = "abcdefghijklmnopqrstuvwxyz"*1000
> for i in xrange(1000):
>     L.append(block)
>     # do something with the partially concatenated string
>     print "partial length is", sum(len(item) for item in L)
> # all finished
> s = ''.join(L)
> do_something(s)
>
> There's still a Shlemiel algorithm in there, but it's executed in fast C
> instead of slow Python and it doesn't involve copying large blocks of
> memory, only walking them, so you won't notice as much. Can we be smarter?
>
> L = []
> block = "abcdefghijklmnopqrstuvwxyz"*1000
> partial_length = 0
> for i in xrange(1000):
>     L.append(block)
>     partial_length += len(block)
>     # do something with the partially concatenated string
>     print "partial length is", partial_length
> # all finished
> s = ''.join(L)
> do_something(s)
>
> Naturally this is a toy example, but I think it illustrates one technique
> for avoiding turning an O(N) algorithm into an O(N**2) algorithm.

So even though I can't delay the "".join operation until after the
loop, I can still improve performance by reducing the length of
"".join operations inside the loop. In my case, I need to temporarily
work on the latest two blocks only.

L = []
block = "abcdefghijklmnopqrstuvwxyz"*1000
for i in xrange(1000):
L.append(block)
# do something with the latest two blocks
tmp = "".join(L[-2:])
# all finished
s = ''.join(L)
do_something(s)

Unfortunately, the downside is that the code becomes more difficult to
read compared to using the obvious +=. If only the optimization worked
for attributes ...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Turning a signature-changing decorator into a signature-preserving one

2009-02-14 Thread Gustavo Narea
On Feb 14, 6:38 am, Michele Simionato 
wrote:
> On Feb 14, 12:56 am, Gustavo Narea  wrote:
>
>
>
> > Hello, everybody.
>
> > I have this signature-changing decorator
> > 
> > which I
> > want to turn into a signature-preserving one. Here's my try
> > ,
> > but I get
> > this 
> > error:http://paste.chrisarndt.de/paste/33e06be6e6d74e49b05c45534dfcc9fe?wra...
>
> > What am I doing wrong? I think it looks like this 
> > example:http://pypi.python.org/pypi/decorator#async
>
> > Thanks in advance.
>
> >   - Gustavo.
>
> > PS: functols.wrap is not an option because my code has to work with
> > Python 2.4+.
>
> Difficult to see what is happening, since you have so many
> dependencies. However, from the traceback I would say that
> you are passing to the decorator a function with a __name__
> attribute which is None. I would insert a call to pdb here
>
> def __call__(self, func):
> import pdb; pdb.set_trace()
> return decorator(self.call, func)
>
> and I would inspect func.__name__.
>
> HTH,
>
>  M.Simionato

Thanks for your response, Michele.

This is what I get:
"""
(Pdb) func.__name__
'greetings'
(Pdb) func.__dict__
{}
(Pdb) func.__module__
'pylonsproject.controllers.root'
"""

Which seems correct to me.

By the way, I forgot to mention that what is decorated is an instance
method. I don't know if that would change something.

Thanks in advance.

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


encrypting lines from file with md5 module doesn't work?

2009-02-14 Thread Canned
Hi,
I need some help with my script. I hope someone can show me the right
direction or at least explain to me what did I wrong?

I write a small script that read lines from plain text file and encrypt
each lines using md5 module. I have a small word list that contain 2000+
words, 1 word/line. Using the code below, I can save the output to
another file to use it with john the ripper (http://www.openwall.com).

Here is the part that annoys me, john recognize the output as des and
not as md5. Using the original wordlist, I tried to crack the list but
nothing has come up after waiting for almost 9 hours. Can someone please
tell me what did I wrong? Why john don't recognize the output as md5?
I'm using python 2.5.1 and I'm python noob and also don't have any
knowledge about encryption.

code
import sys, md5

f = open(sys.argv[1])
obj = md5.new()

for line in f:
if line[-1:] == '\n':
text = line[:-1]
obj.update(text),
print text + ':' + obj.hexdigest()

f.close()


---result---
00:670b14728ad9902aecba32e22fa4f6bd
:c47532bbb1e2883c902071591ae1ec9b
11:bf874003f752e86e6d6ba6d6df1f24a2
:65a89de3000110bf37bcafdbd33df55a
121212:38a8eeb4dfb0f86aefea908365817c15
123123:f226a65a908909b83aed92661897d0c9


Thanks in advance
--
http://mail.python.org/mailman/listinfo/python-list


Reading a file

2009-02-14 Thread zaheer . agadi
Hi

How do i read  a file in Python and search a particular pattern
like I have a file char.txt  which has

Mango=sweet
Sky=blue

I want to get the strings sweet and blue,How to do this..?

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


ANN: SuPy 1.2

2009-02-14 Thread Greg Ewing

SuPy 1.2 Available
--

   http://www.cosc.canterbury.ac.nz/greg.ewing/SuPy/

Changes in this version:

- Ruby object and class wrappers now have __class__ and __bases__ attributes
  that return the right things. As a consequence, isinstance() and
  issubclass() also work properly on Ruby objects.

- UI module made available to Python.

- Data in a module can be preserved across a Py.refresh() by giving the
  module a __keep__ attribute holding a sequence of names to preserve.

- Added a utility module to assist with managing menu items.

- Fixed some bugs in the block-passing mechanism.


What is SuPy?
-

SuPy is a plugin for the Sketchup 3D modelling application
that lets you script it in Python.

--
Greg Ewing
greg.ew...@canterbury.ac.nz
--
http://mail.python.org/mailman/listinfo/python-list


Re: sgmllib parser keeps old tag data?

2009-02-14 Thread Berend van Berkum
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On Fri, Feb 13, 2009 at 03:41:52PM +, MRAB wrote:
> Berend van Berkum wrote:
> >Yes.. tested that and SGMLParser won't let me override __init__, 
> >(SGMLParser vars are uninitialized even with sgmllib.SGMLParser(self) 
> >call).
> 
> OK, so SGMLParser needs to be initialised:
> 
>   def __init__(self):
>   sgmllib.SGMLParser.__init__(self)
>   self.content = ''
>   self.markup = []
>   self.span_stack = []

argh.. forgot about old-style supercall syntax
used sgmllib.SGMLParser(self), did not investigate the error
gonna be ashamed for a while now..

- -- 
 web, http://dotmpe.com  ()ASCII Ribbon
 email, berend.van.ber...@gmail.com  /\
 icq, 26727647;  irc, berend/mpe at irc.oftc.net

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFJlsStn70fkTNDJRgRArzdAJ9QhTaIcx0Kgps8rHe0oGnf6qQm+QCeJSh/
+pMOged64wmns1HvoV+u4fA=
=O4QU
-END PGP SIGNATURE-
--
http://mail.python.org/mailman/listinfo/python-list


Re: encrypting lines from file with md5 module doesn't work?

2009-02-14 Thread MRAB

Canned wrote:

Hi,
I need some help with my script. I hope someone can show me the right
direction or at least explain to me what did I wrong?

I write a small script that read lines from plain text file and encrypt
each lines using md5 module. I have a small word list that contain 2000+
words, 1 word/line. Using the code below, I can save the output to
another file to use it with john the ripper (http://www.openwall.com).


[snip]
MD5 is a type of checksum, not a method of encryption.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Turning a signature-changing decorator into a signature-preserving one

2009-02-14 Thread Michele Simionato
On Feb 14, 1:30 pm, Gustavo Narea  wrote:
> This is what I get:
> """
> (Pdb) func.__name__
> 'greetings'
> (Pdb) func.__dict__
> {}
> (Pdb) func.__module__
> 'pylonsproject.controllers.root'
> """
>
> Which seems correct to me.
>
> By the way, I forgot to mention that what is decorated is an instance
> method. I don't know if that would change something.

This is bad, func should be a function and not an instance
method (see line 49 of decorator.py). If the .signature
attribute of the FunctionMaker is missing, than that's the
reason for the AssertionError you see.
I should probably raise a clearer error message.
I lack the context to see how this could be fixed in your case, but
this a not a show stopper, you can just keep the original decorator
and forget about making it signature preserving.
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] RELEASED Python 3.0.1

2009-02-14 Thread Barry Warsaw

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On Feb 13, 2009, at 11:46 PM, Benjamin Kaplan wrote:


Any chance of getting a Mac installer for this one?


I believe Ronald is planning to upload it soon.
Barry

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (Darwin)

iQCVAwUBSZbWDHEjvBPtnXfVAQJNOQQAl89fJc7ezmpaDlClehtFJTwX5xE7JAtD
yZ47O9yUOQfdIem2l1VVApHnsnUmLKILYG3v4MHiWJBjOT10Oxjc4JKlV3nmREda
aUYOHCk1aHrPgdLHS4Cb/NBA6uYoG/+fHBcEMujClxv30lUTj76kDcjIMlflcpu6
r9I/jJdYywg=
=nyT7
-END PGP SIGNATURE-
--
http://mail.python.org/mailman/listinfo/python-list


Function name limit in Python ?

2009-02-14 Thread Linuxguy123
Excuse my ignorance, but is there a limit to the size of function names
in Python ?

I named a function getSynclientVersion() and I got an error when I
called it.  I renamed the same function to getSCVersion() and it called
fine.

Why ?

Thanks

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


Re: Problem building Python extension

2009-02-14 Thread John Machin
On Feb 14, 7:04 pm, martijnsteenw...@gmail.com wrote:
> On 13 feb, 12:45, Christian Heimes  wrote:
>
> > martijnsteenw...@gmail.com wrote:
> > > Thanks a lot for your reply. I downloaded & installed Visual C# 2008
> > > express, but unfortunately this doesn't change anything in running the
> > > setup file. Unfortunately, still no pyd file is produced...
>
> > > Did I something wrong?
>
> > Yeah, you installed the C# environment. Python is written in C, so you
> > have to get Visual Studio C++ 2008. However a recent version of MinGW32
> > is sufficient to build most extensions.
>
> > Christian
>
> Hi Christian,
>
> Thanks for your reply. In mean time, I discovered the C++ thing
> already ;-).
> Now I installed MinGW, but still the same happens. If I run the setup
> file, the same output comes on the command line and no pyd file is
> created... GRRR
>
> Any suggestions?

AFAIK it will attempt to use a compiler other than the one that Python
was compiled with only if specifically directed to e.g. with --
compiler=mingw32 in the command line when running setup.py.

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


Re: how can this iterator be optimized?

2009-02-14 Thread josh logan
On Feb 13, 7:44 pm, Basilisk96  wrote:
> On Feb 12, 1:15 am, Steven D'Aprano
>
>  wrote:
> > > I usually strive
> > > for comprehensions if a for loop can be reduced to such.
>
> > Any particular reason?
>
> Only two.
> 1.) I was impressed by their clarity and conciseness when I first
> discovered them.
> 2.) I also read now and then that simple list comprehensions are
> faster when compared with their for-loop equivalents because of the
> way comprehensions are implemented under the hood. My example is a far
> cry from a "simple" comprehension, however. :)
>
> > If there's only one call to func(), and you ignore the (probably) fixed
> > cost of jumping into a generator each time, then it shouldn't make any
> > difference.
>
> > If you are comparing one call to func() in a for loop versus three calls
> > to func() in a list comp or generator expression, then of course the for
> > loop will be more efficient.
>
> I agree. I would rather call func() only once per iteration in any
> case. I will revise it to a plain for loop with a single call.
>
> Thanks,
> -Basilisk96

Just as long as you do realize that it is possible to do what you were
looking with one call to func() using chained generators, as
demonstrated in my post above. Whichever way is clearest for you would
be the way I'd go.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Function name limit in Python ?

2009-02-14 Thread MRAB

Linuxguy123 wrote:

Excuse my ignorance, but is there a limit to the size of function names
in Python ?

I named a function getSynclientVersion() and I got an error when I
called it.  I renamed the same function to getSCVersion() and it called
fine.

Why ?

Probably a just spelling mistake. Double-check that the two names are 
identical.

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


Re: Function name limit in Python ?

2009-02-14 Thread Paul McGuire
On Feb 14, 8:45 am, Linuxguy123  wrote:
> Excuse my ignorance, but is there a limit to the size of function names
> in Python ?
>
> I named a function getSynclientVersion() and I got an error when I
> called it.  I renamed the same function to getSCVersion() and it called
> fine.
>
> Why ?
>

Hello again, Linuxguy123!  I see you are forging ahead in your quest
for Python knowledge.

Here is a small snippet that refutes your assertion that function name
length is a problem (but surely, you could have written this small a
test and answered your own question...):

def getSynclientVersion():
return "1.2.3.4"

print getSynclientVersion()

prints:
1.2.3.4

Let's try something a bit longer:
def
abcdefghijklmnopqrstuvwxyz_1234567890_abcdefghijklmnopqrstuvwxyz_1234567890
():
return "Howdy!"

print
abcdefghijklmnopqrstuvwxyz_1234567890_abcdefghijklmnopqrstuvwxyz_1234567890
()

prints:
Howdy!

So I don't think the problem you are having is the length of your
method name.

Your quest will be much more productive if, in the future, you post a
little more information with these questions:

- Post the code that you wrote - this is trickier than it sounds.  The
best post is the smallest possible extract of your code that
reproduces the error in question.

But please, DONT try to send in fresh code that you feel represents
your problem without first verifying that it does.  Many people post a
summary of their code because they don't want to post their secret
source code, or their 3000 lines of code - and then in their summary,
they fix the problem they were having.  Then c.l.py readers spend a
lot of time going "wha...?" because the posted code has no error.

- Post the exception that was returned

Probably even more helpful than posting code.  What exactly was the
problem?  Python is not really that bad at giving some informative
messages.  Perhaps you misspelled your method name:

print getSynClientVersion()

gives this error:
NameError: name 'getSynClientVersion' is not defined

It's not defined because I named it "getSynclientVersion" - with a
little 'c', not a big 'C'.

Or maybe you misused the value returned from the method:

print getSynclientVersion().length

gives:

AttributeError: 'str' object has no attribute 'length'

An error, surely, but it has nothing to do with the length of the
method name I called.


Help us help you, Linuxguy123!
-- Paul
--
http://mail.python.org/mailman/listinfo/python-list


Re: how can this iterator be optimized?

2009-02-14 Thread Paul McGuire
On Feb 11, 7:22 pm, Basilisk96  wrote:
>...
> where "func" is a single-argument function that returns either a
> string or None, but is an expensive call.
> I am pretty sure that the sorted() construct cannot be improved much
> further, but...
> ...does anyone have ideas on improving the "rawPairs" iterator so that
> it calls "func(s)" only once per iteration?  Perhaps a lambda
> construct, but I am not sure how to go about it...?
>
> Cheers,
> Basilisk96

If func is expensive, you could try memoizing it.  Then subsequent
"calls" just do arg lookups.  Michele Simianato has posted a good
memoizing decorator on the Python wiki.

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


confusion about variable scope in a class

2009-02-14 Thread gyro
Hi,
I was writing a Python script to perform some data analyses and was
surprised by some behavior I noted. A simple test program illustrating
the behavior is below.
I do not understand why the value of 'data' is being modified. I am
obviously missing something obvious, and would certainly appreciate an
explanation of why this is happening.

Thank you.

-gf

--

#!/bin/env python

class TestPop(object):
def round1(self,data1):
t = data1.pop(-1)

def round2(self,data2):
t = data2.pop(-1)

def tester(self):
data = range(10)
self.round1(data)
print data
self.round2(data)
print data

if __name__ == '__main__':
tp = TestPop()
tp.tester()
--
http://mail.python.org/mailman/listinfo/python-list


Can Python serial support run at 45.45 baud?

2009-02-14 Thread John Nagle

   Can Python's serial port support be made to run at 45.45 baud, the old
"60 speed" Teletype machine speed?  I've restored a Model 15 teletype from
WWII.  Works great after cleaning, oiling, and adjustment.  There's
Perl support for this, and a Perl program ("http://www.buzbee.net/heavymetal";)
that runs the things.  I'd like to do something similar in Python, both
on Windows and Linux.

   I'm proposing to use Python 2.6, PyWin32-212, and PySerial 2.4.
The PySerial interface allows requesting a specific baud rate, but does that
propagate properly all the way down into Windows?   And what about
Linux, where the "stty" interface is quite different?

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


Re: confusion about variable scope in a class

2009-02-14 Thread andrew cooke

it's not a scope issue.  you are confusing variables and objects.

a variable is a box that can hold an object

so x = 2 puts the object '2' in the box 'x'.

following that with x = '3' changes the box 'x' to hold the object '3'.

but lists are also boxes, different from variables.

so x = [1,2,3]

puts the a list object, that is a box that contains 1, 2 and 3, in 'x'

then y = x

puts THE SAME list object in box 'y'.

then y[1] = 4 changes the second item in the list's box to 4

the print x gives "[1,4,3]" because you have changed contents of the list.

in contrast x = 3 then y = x then y = 4 does not change x because you care
changing variables, not list contents.

to fix your code, you need to copy the list

x = [1,2,3]
y = list(x) # copy
y[1] = 4
print x
[1,2,3]

surely this is ina faq?  it comes up once a day...

andrew



gyro wrote:
> Hi,
> I was writing a Python script to perform some data analyses and was
> surprised by some behavior I noted. A simple test program illustrating
> the behavior is below.
> I do not understand why the value of 'data' is being modified. I am
> obviously missing something obvious, and would certainly appreciate an
> explanation of why this is happening.
>
> Thank you.
>
> -gf
>
> --
>
> #!/bin/env python
>
> class TestPop(object):
> def round1(self,data1):
> t = data1.pop(-1)
>
> def round2(self,data2):
> t = data2.pop(-1)
>
> def tester(self):
> data = range(10)
> self.round1(data)
> print data
> self.round2(data)
> print data
>
> if __name__ == '__main__':
> tp = TestPop()
> tp.tester()
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


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


Re: String concatenation performance with +=

2009-02-14 Thread Nick Craig-Wood
Sammo  wrote:
>  String concatenation has been optimized since 2.3, so using += should
>  be fairly fast.
> 
>  In my first test, I tried concatentating a 4096 byte string 1000 times
>  in the following code, and the result was indeed very fast (12.352 ms
>  on my machine).
> 
>  import time
>  t = time.time()
>  mydata = ""
>  moredata = "A"*4096
>  for i in range(1000):
>  mydata += moredata # 12.352 ms
>  print "%0.3f ms"%(1000*(time.time() - t))
> 
>  However, I got a different result in my second test, which is
>  implemented in a class with a feed() method. This test took 4653.522
>  ms on my machine, which is 350x slower than the previous test!
> 
>  class StringConcatTest:
>  def __init__(self):
>  self.mydata = ""
> 
>  def feed(self, moredata):
>  self.mydata += moredata # 4653.522 ms
> 
>  test = StringConcatTest()
>  t = time.time()
>  for i in range(1000):
>  test.feed(moredata)
>  print "%0.3f ms"%(1000*(time.time() - t))
> 
>  Note that I need to do something to mydata INSIDE the loop, so please
>  don't tell me to append moredata to a list and then use "".join after
>  the loop.
> 
>  Why is the second test so much slower?

The optimized += depends on their being no other references to the
string.  Strings are immutable in python.  So append must return a new
string.  However the += operation was optimised to do an in-place
append if and only if there are no other references to the string.

You can see this demonstrated here

$ python -m timeit -s 'a="x"' 'a+="x"'
100 loops, best of 3: 0.231 usec per loop

$ python -m timeit -s 'a="x"; b=a' 's = a; a+="x"'
10 loops, best of 3: 30.1 usec per loop

You are keeping the extra reference in a class instance like this

$ python -m timeit -s 'class A(object): pass' -s 'a=A(); a.a="x"' 'a.a+="x"'
10 loops, best of 3: 30.7 usec per loop

Knowing that, this optimization suggests itself

$ python -m timeit -s 'class A(object): pass' -s 'a=A(); a.a="x"' 's = a.a; a.a 
= None; s += "x"; a.a = s'
100 loops, best of 3: 0.954 usec per loop

Or in your example

class StringConcatTest:
def __init__(self):
self.mydata = ""
def feed(self, moredata):
#self.mydata += moredata
s = self.mydata
del self.mydata
s += moredata
self.mydata = s

moredata = "A"*4096
test = StringConcatTest()
t = time.time()
for i in range(1000):
test.feed(moredata)

print "%0.3f ms"%(1000*(time.time() - t))

Before it was 3748.012 ms on my PC, afterwards it was 52.737 ms

However that isn't a perfect solution - what if something had another
reference on self.mydata?

You really want a non-immutable string for this use.  array.array
is a possibility

$ python -m timeit -s 'import array' -s 'a = array.array("c")' 'a.extend("x")'
10 loops, best of 3: 2.01 usec per loop

There are many other possibilities though like the mmap module.
-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: encrypting lines from file with md5 module doesn't work?

2009-02-14 Thread Nick Craig-Wood
Canned  wrote:
>  I write a small script that read lines from plain text file and encrypt
>  each lines using md5 module. I have a small word list that contain 2000+
>  words, 1 word/line. Using the code below, I can save the output to
>  another file to use it with john the ripper (http://www.openwall.com).
> 
>  Here is the part that annoys me, john recognize the output as des and
>  not as md5. Using the original wordlist, I tried to crack the list but
>  nothing has come up after waiting for almost 9 hours. Can someone please
>  tell me what did I wrong? Why john don't recognize the output as md5?
>  I'm using python 2.5.1 and I'm python noob and also don't have any
>  knowledge about encryption.
> 
>  import sys, md5
> 
>  f = open(sys.argv[1])
>  obj = md5.new()
> 
>  for line in f:
>  if line[-1:] == '\n':
>  text = line[:-1]
>  obj.update(text),
>  print text + ':' + obj.hexdigest()
> 
>  f.close()
> 
> 
>  00:670b14728ad9902aecba32e22fa4f6bd
>  :c47532bbb1e2883c902071591ae1ec9b
>  11:bf874003f752e86e6d6ba6d6df1f24a2
>  :65a89de3000110bf37bcafdbd33df55a
>  121212:38a8eeb4dfb0f86aefea908365817c15
>  123123:f226a65a908909b83aed92661897d0c9

john cracks password files if I remember rightly.

md5 encoded password files don't look like that, they look like this

guest:$1$3nvOlOaw$vRWaitT8Ne4sMjf9NOrVZ.:13071:0:9:7:::

(not a real password line!)

You need to work out how to write that format.

>From memory: the "$1" bit means it is an md5 hash, the next
"$3nvOlOaw$" is the salt and the final "$vRWaitT8Ne4sMjf9NOrVZ." is
the md5 hash in some encoded format or other!  Some googling should
reveal the correct algorithm!

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread Grant Edwards
On 2009-02-14, John Nagle  wrote:

> Can Python's serial port support be made to run at 45.45 baud,
> the old "60 speed" Teletype machine speed? 

If your hardware and OS supports it, Python can be made to
support it.

> I've restored a Model 15 teletype from WWII.

Fun.  I worked with one of those in school back in the day.

> Works great after cleaning, oiling, and adjustment.  There's
> Perl support for this, and a Perl program
> ("http://www.buzbee.net/heavymetal";) that runs the things. I'd
> like to do something similar in Python, both on Windows and
> Linux.

OK, go ahead.

> I'm proposing to use Python 2.6, PyWin32-212, and PySerial
> 2.4. The PySerial interface allows requesting a specific baud
> rate, but does that propagate properly all the way down into
> Windows?

Good question.  Since you're the one with the hardware, you'll
have to tell us.  If it doesn't work, it should be trivial to
fix.  Just tell us what mechanism is used by Perl, and we'll
either fix pyserial or tell you how to do so.

> And what about Linux, where the "stty" interface is quite
> different?

Same answer.

-- 
Grant

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


Re: encrypting lines from file with md5 module doesn't work?

2009-02-14 Thread Canned
MRAB schreef:
> Canned wrote:
>> Hi,
>> I need some help with my script. I hope someone can show me the right
>> direction or at least explain to me what did I wrong?
>>
>> I write a small script that read lines from plain text file and encrypt
>> each lines using md5 module. I have a small word list that contain 2000+
>> words, 1 word/line. Using the code below, I can save the output to
>> another file to use it with john the ripper (http://www.openwall.com).
>>
> [snip]
> MD5 is a type of checksum, not a method of encryption.

I found this from google: http://tinyurl.com/4ow2q4
Using that, I changed my script and it seems that it works well. John
recognize it as md5.

It also raise another question. John can crack my password from
/etc/shadow in less than a second, but if I write it down in a file and
using the code below to encrypt the password, john would took really
long time to crack the password.

I've tried several different password using passwd command and my code
to generate md5 hash, but it seems that john really having trouble with
hashes generated with my code as the hashes generated with passwd being
cracked in a matter of second.
Is there something wrong with the code? Am I missing something?

-code-
#!/usr/bin/env python

import sys
import md5encrypt  # from http://tinyurl.com/4ow2q4

f = open(sys.argv[1])

for line in f:
if line[-1:] == '\n':
text = line[:-1]
print text + ':' + md5encrypt.md5crypt(text,
md5encrypt.generate_salt(5))

f.close()

---password.txt
00

11

121212
123123


-result.txt-
00:$1$http://mail.python.org/mailman/listinfo/python-list


Re: confusion about variable scope in a class

2009-02-14 Thread Diez B. Roggisch

gyro schrieb:

Hi,
I was writing a Python script to perform some data analyses and was
surprised by some behavior I noted. A simple test program illustrating
the behavior is below.
I do not understand why the value of 'data' is being modified. I am
obviously missing something obvious, and would certainly appreciate an
explanation of why this is happening.



Lists are mutables, and python passes around references to objects, not 
copies.


So if you want a copy, you need to create one explicit, either by using e.g.


new_list = list(old_list)

which will create a shollow copy of the list, or by using the 
library-module "copy" that also offers a "deepcopy" function.



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


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread MRAB

Grant Edwards wrote:

On 2009-02-14, John Nagle  wrote:


Can Python's serial port support be made to run at 45.45 baud,
the old "60 speed" Teletype machine speed? 


If your hardware and OS supports it, Python can be made to
support it.


[snip]
I had a quick look at the Windows API. The struct member for the baud
rate is of type DWORD, ie an integer.

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


Re: Function name limit in Python ?

2009-02-14 Thread Albert Hopkins
On Sat, 2009-02-14 at 07:45 -0700, Linuxguy123 wrote:
> Excuse my ignorance, but is there a limit to the size of function names
> in Python ?
> 
> I named a function getSynclientVersion() and I got an error when I
> called it.  

You forgot to paste the error.

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


Extract an image from a RTF file

2009-02-14 Thread bryan.fodn...@gmail.com
I have a large amount of RTF files where the only thing in them is an
image.  I would like to extract them an save them as a png.
Eventually, I would like to also grab some text that is on the image.
I think PIL has something for this.

Does anyone have any suggestion on how to start this?
--
http://mail.python.org/mailman/listinfo/python-list


Re: encrypting lines from file with md5 module doesn't work?

2009-02-14 Thread Canned
Nick Craig-Wood schreef:
> Canned  wrote:
>>  I write a small script that read lines from plain text file and encrypt
>>  each lines using md5 module. I have a small word list that contain 2000+
>>  words, 1 word/line. Using the code below, I can save the output to
>>  another file to use it with john the ripper (http://www.openwall.com).
>>
>>  Here is the part that annoys me, john recognize the output as des and
>>  not as md5. Using the original wordlist, I tried to crack the list but
>>  nothing has come up after waiting for almost 9 hours. Can someone please
>>  tell me what did I wrong? Why john don't recognize the output as md5?
>>  I'm using python 2.5.1 and I'm python noob and also don't have any
>>  knowledge about encryption.
>>
>>  import sys, md5
>>
>>  f = open(sys.argv[1])
>>  obj = md5.new()
>>
>>  for line in f:
>>  if line[-1:] == '\n':
>>  text = line[:-1]
>>  obj.update(text),
>>  print text + ':' + obj.hexdigest()
>>
>>  f.close()
>>
>>
>>  00:670b14728ad9902aecba32e22fa4f6bd
>>  :c47532bbb1e2883c902071591ae1ec9b
>>  11:bf874003f752e86e6d6ba6d6df1f24a2
>>  :65a89de3000110bf37bcafdbd33df55a
>>  121212:38a8eeb4dfb0f86aefea908365817c15
>>  123123:f226a65a908909b83aed92661897d0c9
> 
> john cracks password files if I remember rightly.
> 
> md5 encoded password files don't look like that, they look like this
> 
> guest:$1$3nvOlOaw$vRWaitT8Ne4sMjf9NOrVZ.:13071:0:9:7:::
> 
> (not a real password line!)
> 
> You need to work out how to write that format.
> 
Yes, I'm aware of that format and john only need the first 2 fields to
work with, and the rest, AFAIK is ignored.

So the format from my example should be:
00:$1$670b14728ad9902aecba32e22fa4f6bd

> From memory: the "$1" bit means it is an md5 hash, the next
> "$3nvOlOaw$" is the salt and the final "$vRWaitT8Ne4sMjf9NOrVZ." is
> the md5 hash in some encoded format or other!  Some googling should
> reveal the correct algorithm!
> 

Googling around, I found a piece of code that could do that.
http://tinyurl.com/4ow2q4. Please read my another post about that. I'm
not python expert and I have a little knowledge about encryption.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Easier to wrap C or C++ libraries?

2009-02-14 Thread Diez B. Roggisch

argo...@gmail.com schrieb:

When creating a Python binding to a C or C++ library, which is easier
to wrap, the C lib or the C++ one? Given a choice, if you had to
choose between using one of two libs, one written in C, the other in C+
+ -- both having approximately the same functionality -- which would
you rather deal with from your Python code?

It would seem to me that there's fewer design considerations when
wrapping a library written in C; you just wrap the functions. However,
since Python supports OOP nicely, it might also be that wrapping C++
code *could* also be staightforward... Are there many pitfalls when
having to map C++'s notion of OO to Python?


The answer is easy: if you use C, you can use ctypes to create a wrapper 
- with pure python, no compilation, no platform issues.


Which IMHO makes a strong point for C - if you need OO, it's bolted on 
easily using Python itself, by creating some nice wrapper classes.


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


Re: Thank you, Tkinter. (easy to use)

2009-02-14 Thread Terry Reedy

DLitgo wrote:


Does anyone know of a quick and easy install for

> PIL + JPEG for Mac OS X (10.5)?

If you don't get an answer, try a thread with the above as the title.
There may be a python-mac list somewhere too.

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


Financial Modeling in Python

2009-02-14 Thread Ajith Prasad
http://as.wiley.com/WileyCDA/WileyTitle/productCd-0470987847.html now
lists the contents of a new book:

Financial Modeling in Python
Shayne Fletcher, Christopher Gardner
ISBN: 978-0-470-98784-1
Hardcover
280 pages
August 2009
Wiley List Price:  US $130.00

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


Re: Reading a file

2009-02-14 Thread Terry Reedy

zaheer.ag...@gmail.com wrote:

Hi

How do i read  a file in Python and search a particular pattern
like I have a file char.txt  which has

Mango=sweet
Sky=blue

I want to get the strings sweet and blue,How to do this..?


for line in open('char.txt'):
  if line.find('sweet') != -1 or line.find('blue') != -1:
print(line)

Read the section of the Lib Manual on string methods.

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


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread John Nagle

Grant Edwards wrote:

On 2009-02-14, John Nagle  wrote:


Can Python's serial port support be made to run at 45.45 baud,
the old "60 speed" Teletype machine speed? 


If your hardware and OS supports it, Python can be made to
support it.


   OK, tried to open the port, using Python 2.6, latest PySerial
and PyWin32:

ser = serial.Serial(port, baudrate=baud,
bytesize=serial.FIVEBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO)

Traceback (most recent call last):
  File "serialtest.py", line 27, in 
main()
  File "serialtest.py", line 24, in main
ser = openportbaudot(0,45.45)
  File "serialtest.py", line 17, in openportbaudot
stopbits=serial.STOPBITS_TWO)
  File "D:\python26\lib\site-packages\serial\serialutil.py", line 171, in 
__init__
self.open()
  File "D:\python26\lib\site-packages\serial\serialwin32.py", line 63, in open
self._reconfigurePort()
  File "D:\python26\lib\site-packages\serial\serialwin32.py", line 171, in 
_reconfigurePort
raise ValueError("Cannot configure port, some setting was wrong. Original 
message: %s" % e)
ValueError: Cannot configure port, some setting was wrong. Original message: 
(87, 'SetCommState', 'The parameter is incorrect.')


   Something doesn't like "serial.FIVEBITS".  That's a valid value, according
to "http://pyserial.wiki.sourceforge.net/pySerial";.  If changed to 
"serial.EIGHTBITS",  the code will execute, but of course does the wrong

thing.   That looks like a bug.

   I tried various values for "baud".  PySerial will accept "45", and even 
"45.45", although I doubt that it's really calculating the serial port divisor

values from a floating point value.   (Standard serial port hardware can do
45.45 baud, and most PCs with non-USB serial ports will do it quite well.)
For my application, 45 baud should work, with two stop bits; the
tolerances aren't that tight.

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


Re: Extract an image from a RTF file

2009-02-14 Thread Terry Reedy

bryan.fodn...@gmail.com wrote:

I have a large amount of RTF files where the only thing in them is an
image.  I would like to extract them an save them as a png.
Eventually, I would like to also grab some text that is on the image.
I think PIL has something for this.

Does anyone have any suggestion on how to start this?


Wikepedia Rich Text Format has several links, which lead to
http://pyrtf.sourceforge.net/
http://code.google.com/p/pyrtf-ng/
The former says rtf generation, including images.
The latter says rtf generation and parsing, but only claims to be a 
rewrite of the former.


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


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread MRAB

John Nagle wrote:

Grant Edwards wrote:

On 2009-02-14, John Nagle  wrote:


Can Python's serial port support be made to run at 45.45 baud,
the old "60 speed" Teletype machine speed? 


If your hardware and OS supports it, Python can be made to
support it.


   OK, tried to open the port, using Python 2.6, latest PySerial
and PyWin32:

ser = serial.Serial(port, baudrate=baud,
bytesize=serial.FIVEBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO)

Traceback (most recent call last):
  File "serialtest.py", line 27, in 
main()
  File "serialtest.py", line 24, in main
ser = openportbaudot(0,45.45)
  File "serialtest.py", line 17, in openportbaudot
stopbits=serial.STOPBITS_TWO)
  File "D:\python26\lib\site-packages\serial\serialutil.py", line 171, 
in __init__

self.open()
  File "D:\python26\lib\site-packages\serial\serialwin32.py", line 63, 
in open

self._reconfigurePort()
  File "D:\python26\lib\site-packages\serial\serialwin32.py", line 171, 
in _reconfigurePort
raise ValueError("Cannot configure port, some setting was wrong. 
Original message: %s" % e)
ValueError: Cannot configure port, some setting was wrong. Original 
message: (87, 'SetCommState', 'The parameter is incorrect.')


   Something doesn't like "serial.FIVEBITS".  That's a valid value, 
according
to "http://pyserial.wiki.sourceforge.net/pySerial";.  If changed to 
"serial.EIGHTBITS",  the code will execute, but of course does the wrong

thing.   That looks like a bug.

   I tried various values for "baud".  PySerial will accept "45", and 
even "45.45", although I doubt that it's really calculating the serial 
port divisor

values from a floating point value.   (Standard serial port hardware can do
45.45 baud, and most PCs with non-USB serial ports will do it quite well.)
For my application, 45 baud should work, with two stop bits; the
tolerances aren't that tight.


I don't think it's a Python bug. The MSDN website says this:

(http://msdn.microsoft.com/en-us/library/aa363214(VS.85).aspx.)

When a DCB structure is used to configure the 8250, the following 
restrictions apply to the values specified for the ByteSize and StopBits 
members:


* The number of data bits must be 5 to 8 bits.
* The use of 5 data bits with 2 stop bits is an invalid 
combination, as is 6, 7, or 8 data bits with 1.5 stop bits.


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


Re: How to debug deadlock?

2009-02-14 Thread Aahz
In article ,
Victor Lin   wrote:
>
>I am developing a multi-threading application, I encounter a deadlock.
>I use Visual C++ Express 2008 to trace the program. Once the deadlock
>occurs, I just pause the program and trace. I found that when deadlock
>occurs, there will be two threads called python from my C++ extension.
>All of them use Queue in python code, so I guess the deadlock might
>caused by Queue. But however, once the extension goes into python
>code, I can't see nothing but asm code and binary from the VC++
>debugger. I would like to know are there any way to dump the call
>stack of python code after I paused the program? And how can I know
>what lock are there in threads caused the deadlock?

You are already hosed if you have two threads calling into Python,
because you didn't properly acquire the GIL.  I can't help beyond that;
try using the mailing list
http://mail.python.org/mailman/listinfo/capi-sig
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] RELEASED Python 3.0.1

2009-02-14 Thread Ronald Oussoren


On 14 Feb, 2009, at 9:55, Martin v. Löwis wrote:


Any chance of getting a Mac installer for this one?


Chances are non-zero, yes.



I had hoped to build one last night, but got home way later than I had  
planned. The installer is building as I type this.


Ronald



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


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread John Nagle

John Nagle wrote:


   OK, tried to open the port, using Python 2.6, latest PySerial
and PyWin32:

ser = serial.Serial(port, baudrate=baud,
bytesize=serial.FIVEBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO)

ValueError: Cannot configure port, some setting was wrong. Original 
message: (87, 'SetCommState', 'The parameter is incorrect.')


   Something doesn't like "serial.FIVEBITS".  That's a valid value, 
according
to "http://pyserial.wiki.sourceforge.net/pySerial";.  If changed to 
"serial.EIGHTBITS",  the code will execute, but of course does the wrong

thing.   That looks like a bug.


   OK, here's what's wrong.  The allowed numbers for stop bits in Windows are

ONESTOPBIT  0   1 stop bit.
ONE5STOPBITS1   1.5 stop bits.
TWOSTOPBITS 2   2 stop bits.

The Python interface, however, only exports STOPBITS_ONE and STOPBITS_TWO.
See "serialutil.py", at line 9, and "serialwin32.py" at lines 141-146.

Microsoft documentation 
("http://msdn.microsoft.com/en-us/library/aa363214(VS.85).aspx") says:


* The use of 5 data bits with 2 stop bits is an invalid combination,
as is 6, 7, or 8 data bits with 1.5 stop bits.

So the correct combination, 5 bits with 1.5 stop bits, isn't supported in
Python.  1 stop bit will not physically work on Baudot teletypes; the
main camshaft doesn't come around fast enough.  (Yes, there's an actual
mechanical reason for 1.5 stop bits.)  Requesting 2 stop bits at the
Python level gets a reject at the Win32 level.  (Not sure why Win32
doesn't allow that; extra stop bits just add delay, but don't hurt
anything.  But it's not supported.)

Linux has a different set of restrictions; Linux offers only 1 or 2 stop
bits, and won't do arbitrary baud rates via the "termios" data structure,
although there are other ways to request that.  At the hardware level,
there's a clock rate, a counter, and a divisor, so arbitrary baud
rates can be set.

John Nagle





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


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread MRAB

John Nagle wrote:

John Nagle wrote:


   OK, tried to open the port, using Python 2.6, latest PySerial
and PyWin32:

ser = serial.Serial(port, baudrate=baud,
bytesize=serial.FIVEBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO)

ValueError: Cannot configure port, some setting was wrong. Original 
message: (87, 'SetCommState', 'The parameter is incorrect.')


   Something doesn't like "serial.FIVEBITS".  That's a valid value, 
according
to "http://pyserial.wiki.sourceforge.net/pySerial";.  If changed to 
"serial.EIGHTBITS",  the code will execute, but of course does the wrong

thing.   That looks like a bug.


   OK, here's what's wrong.  The allowed numbers for stop bits in 
Windows are


ONESTOPBIT01 stop bit.
ONE5STOPBITS11.5 stop bits.
TWOSTOPBITS22 stop bits.

The Python interface, however, only exports STOPBITS_ONE and STOPBITS_TWO.
See "serialutil.py", at line 9, and "serialwin32.py" at lines 141-146.

Microsoft documentation 
("http://msdn.microsoft.com/en-us/library/aa363214(VS.85).aspx") says:


* The use of 5 data bits with 2 stop bits is an invalid combination,
as is 6, 7, or 8 data bits with 1.5 stop bits.

So the correct combination, 5 bits with 1.5 stop bits, isn't supported in
Python.  1 stop bit will not physically work on Baudot teletypes; the
main camshaft doesn't come around fast enough.  (Yes, there's an actual
mechanical reason for 1.5 stop bits.)  Requesting 2 stop bits at the
Python level gets a reject at the Win32 level.  (Not sure why Win32
doesn't allow that; extra stop bits just add delay, but don't hurt
anything.  But it's not supported.)

Linux has a different set of restrictions; Linux offers only 1 or 2 stop
bits, and won't do arbitrary baud rates via the "termios" data structure,
although there are other ways to request that.  At the hardware level,
there's a clock rate, a counter, and a divisor, so arbitrary baud
rates can be set.


If 5+2 is invalid, could you work around it with 6+1 instead (one of the
data bits masquerading as a stop bit)? Presumably the device on the
other end won't care as long as it gets the bits it expects...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread MRAB

John Nagle wrote:
[snip]

So the correct combination, 5 bits with 1.5 stop bits, isn't supported in
Python.  1 stop bit will not physically work on Baudot teletypes; the
main camshaft doesn't come around fast enough.  (Yes, there's an actual
mechanical reason for 1.5 stop bits.)  Requesting 2 stop bits at the
Python level gets a reject at the Win32 level.  (Not sure why Win32
doesn't allow that; extra stop bits just add delay, but don't hurt
anything.  But it's not supported.)


I forgot to add that what Windows allows depends on what the hardware
(the serial chip) allows: the MSDN webpage mentions that the DCB struct
configures the 8250.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread Roy Smith
In article <49970ce7$0$1665$742ec...@news.sonic.net>,
 John Nagle  wrote:

> At the hardware level, there's a clock rate, a counter, and a divisor, 
> so arbitrary baud rates can be set.

Is that really true of modern hardware?  The last time I looked at serial 
port hardware, UARTs took a base clock rate and divided it sequentially 
with flip-flops to get all the possible rates (they usually had some ugly 
logic to generate 110).  You were limited to the specific rates the 
hardware gave you.  Is that no longer the case?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Extract an image from a RTF file

2009-02-14 Thread Curt Hash
On Sat, Feb 14, 2009 at 11:01 AM, Terry Reedy  wrote:
>
> bryan.fodn...@gmail.com wrote:
>>
>> I have a large amount of RTF files where the only thing in them is an
>> image.  I would like to extract them an save them as a png.
>> Eventually, I would like to also grab some text that is on the image.
>> I think PIL has something for this.
>>
>> Does anyone have any suggestion on how to start this?
>
> Wikepedia Rich Text Format has several links, which lead to
> http://pyrtf.sourceforge.net/
> http://code.google.com/p/pyrtf-ng/
> The former says rtf generation, including images.
> The latter says rtf generation and parsing, but only claims to be a rewrite 
> of the former.
>
> --
> http://mail.python.org/mailman/listinfo/python-list

I've written an RTF parser in Python before, but for the purpose of
filtering and discarding content rather than extracting it.

Take a look at the specification here:
http://www.microsoft.com/downloads/details.aspx?familyid=dd422b8d-ff06-4207-b476-6b5396a18a2b&displaylang=en

You will find that images are specified by one or more RTF control
words followed by a long string of hex data. For this special purpose,
you will not need to write a parser for the entire specification. Just
search the file for the correct sequence of control words, extract the
hex data that follows, and save it to a file.

It helps if you open the RTF document in a text editor and locate the
specific control group that contains the image, as the format and
order of control words varies depending on the application that
created it. If all of your documents are created with the same
application, it will be much easier.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Easier to wrap C or C++ libraries?

2009-02-14 Thread argo785
On Feb 14, 12:14 pm, "Diez B. Roggisch"  wrote:
>
> The answer is easy: if you use C, you can use ctypes to create a wrapper
> - with pure python, no compilation, no platform issues.
>
> Which IMHO makes a strong point for C - if you need OO, it's bolted on
> easily using Python itself, by creating some nice wrapper classes.
>

Thanks for the replies, everyone.

After posting I went back and reviewed some of my old C books as well
as the C++ ones. It's interesting and instructive to look back at C++
after you've used languages like Python for a while. I'd forgotten how
complicated C++ code can get.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Easier to wrap C or C++ libraries?

2009-02-14 Thread Hrvoje Niksic
"Diez B. Roggisch"  writes:

> The answer is easy: if you use C, you can use ctypes to create a
> wrapper - with pure python, no compilation, no platform issues.

The last part is not true.  ctypes doesn't work on 64-bit
architectures, nor does it work when Python is built with non-gcc Unix
compilers.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Easier to wrap C or C++ libraries?

2009-02-14 Thread Christian Heimes
Hrvoje Niksic schrieb:
> "Diez B. Roggisch"  writes:
> 
>> The answer is easy: if you use C, you can use ctypes to create a
>> wrapper - with pure python, no compilation, no platform issues.
> 
> The last part is not true.  ctypes doesn't work on 64-bit
> architectures, nor does it work when Python is built with non-gcc Unix
> compilers

ctypes works on 64bit systems, too. However it's a pain in the ... back
to write code with ctypes that works across all platforms. I used to use
ctypes for wrapper but eventually I switched to Cython.

Christian

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


Re: Fortran array in python (f2py?)...

2009-02-14 Thread tripp
OK.  It sounds like it would be easiest for me, then, to dump the
arrays to a binary file (much faster than dumping it to a text) from
the fortran program.  Then use f2py to load a fortran module to read
it.?.

How well does python handle binary files?  Maybe I could skit the f2py
all together if I can get python to read the fortran binary file...

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


Re: Fortran array in python (f2py?)...

2009-02-14 Thread Kurt Smith
On Sat, Feb 14, 2009 at 2:06 PM, tripp  wrote:

> OK.  It sounds like it would be easiest for me, then, to dump the
> arrays to a binary file (much faster than dumping it to a text) from
> the fortran program.  Then use f2py to load a fortran module to read
> it.?.


I've done something similar and have written some wrapper functions (in pure
python) that read in fortran binary arrays and puts them into a numpy
array.  You have to deal with fortran records, which (for the fortran
compiler I'm using) puts a 4-byte record length indicator at the beginning
and end of each record, with the raw binary data between.  The issue is
mildly complicated if you have to deal with endianness incompatibilites
between computers.  IIRC, the format of the records is compiler dependent,
although I've found that gfortran, g77 and xlfortran are consistent with
each other.

If you like I'd be happy to send you the code.

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


Re: Fortran array in python (f2py?)...

2009-02-14 Thread Scott David Daniels

tripp wrote:
... dump the arrays to a binary file (much faster than dumping it to a text) 

> from the fortran program


How well does python handle binary files?  Maybe I could skit the f2py
all together if I can get python to read the fortran binary file...


Likely your best plan.  Look into the numpy external package, because
those arrays are _far_ superior for actual calculation.  With numpy you
can do array_1 + array_2 without requiring a Python loop.  I'm sure
it has a way to read in binary arrays of the various data types.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread John Nagle

MRAB wrote:

John Nagle wrote:
[snip]

So the correct combination, 5 bits with 1.5 stop bits, isn't supported in
Python.  1 stop bit will not physically work on Baudot teletypes; the
main camshaft doesn't come around fast enough.  (Yes, there's an actual
mechanical reason for 1.5 stop bits.)  Requesting 2 stop bits at the
Python level gets a reject at the Win32 level.  (Not sure why Win32
doesn't allow that; extra stop bits just add delay, but don't hurt
anything.  But it's not supported.)


   I patched PySerial to support "STOPBITS_ONE5", for 1.5 stop bits,
and the big Teletype Model 15 is now banging out text from Python.

   I actually put in "45.45" as the baud rate; it gets rounded down
to 45 for Windows, which works.  That won't work on Linux, though;
there's a canned list of speeds for POSIX serial ports.  (It's the
same list PDP-11 serial ports had in the 1970s, plus some later
extensions at the high end.)

   1.5 or 2 stop bits is acceptable for the old heavy iron, but 1.5
is preferred, because there's less vibration and wear.  With 2 stop bits,
the Teletype Model 15 clutch drops out on every character and the drive
train momentarily comes to a halt.   With 1.5, everything rotates steadily.

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


Rapidshare to Megaupload script

2009-02-14 Thread aiwarrior

I've made this script and would like to have some input and share it
with the community.
I also have a page with some code i produce on my spare time. http://pneves.net
Thanks

# -*- coding: utf-8 -*-
## I Paulo Neves am the owner of this script and i do not allow the
copy or distribution
## of this script without my permission to commercial purposes
## If you have any suggestions please mail me and i will reply and
post the on the site
from __future__ import with_statement
from urlparse import urljoin
import urllib2, urllib
import os, sys
import re
import ConfigParser


urls = []
acc_details = 0
firs_run = 0


def from_rapidshare(url):
'''Check if this is a rapidshare link'''
return (url.startswith("rapidshare.com") or
url.startswith("www.rapidshare.com") or
url.startswith("http://rapidshare.com";) or
url.startswith("http://www.rapidshare.com";))


def account_details():
if os.environ.has_key("APPDATA") and os.path.exists(os.environ
["APPDATA"]):
path = os.environ["APPDATA"] + "\\murs.cfg"
else:
path = os.path.expanduser("~") + "\.murs.cfg"

if not os.path.exists(path):
print path
m_user = raw_input("Enter your user name for megaupload: ")
m_password = raw_input("Enter your password for megaupload: ")
r_user = raw_input("Enter your user name for rapidshare: ")
r_password = raw_input("Enter your password for rapidshare: ")
cfg = ConfigParser.SafeConfigParser()
cfg.add_section('Acc_Details')
cfg.set('Acc_Details', 'm_user ', m_user)
cfg.set('Acc_Details', 'm_password', m_password)
cfg.set('Acc_Details', 'r_user', r_user)
cfg.set('Acc_Details', 'r_password', r_password)
with open(path, 'wb') as configfile:
cfg.write(configfile)

cfg = ConfigParser.SafeConfigParser()
cfg.read(path)
try:
m_user = cfg.get("Acc_Details", "m_user")
m_password = cfg.get("Acc_Details", "m_password")
r_user = cfg.get("Acc_Details", "r_user")
r_password = cfg.get("Acc_Details", "r_password")
except ConfigParser.NoSectionError or ConfigParser.NoOptionError:
print "no section or No Option"
print
os.remove(path)
return (m_user, m_password, r_user, r_password)

def cookie_processor(cookie):
cookie = cookie.split("; ")
##cookie = dict( [cookie[x].split("=") for x in xrange ( len
(cookie) ) ] )
##if cookie['user'] != None:
return cookie
##else:
##print "Scheme has changed or authentication failes. Last
option most likely"
##sys.exit()
##

def rs_auth(login):
r_user = login[0]
r_password = login[1]
opener = urllib2.build_opener(urllib2.HTTPSHandler())
urllib2.install_opener( opener )
cred = urllib.urlencode({"login": r_user, "password": r_password})
try:
req = urllib2.urlopen("https://ssl.rapidshare.com/cgi-bin/
premiumzone.cgi", cred)
cookie_rs = cookie_processor( req.headers.get("set-cookie",
"") )
except  urllib2.URLError:
print"Some error with the connection ocurred. Try again now or
later"
sys.exit()
#Returns the page if flag is set
return req.read(), cookie_rs


def mu_auth(login):
r_user = login[0]
r_password = login[1]
cred = urllib.urlencode({"login": r_user, "password": r_password})
try:
req = urllib2.urlopen("http://www.megaupload.com";, cred)
cookie_mu = cookie_processor( req.headers.get("set-cookie",
"") )

except:
print "Connection failed"
sys.exit()


#returns the authenticated header, in case future changes
specificaly ask the cookie it would be easy
#to change code or even extract it from the header itself as a
dictionary key
return cookie_mu

def set_rscookie_in_musite(cookie_mu, cookie_rs) :
#it doesnt need to check for the cookie because it automaticaly
resets for the new value
cookie_rs = cookie_rs.split("=",1)
header = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain", "cookie":cookie_mu}
params = urllib.urlencode({'domain': "http://rapidshare.com";,
'cookiename1': cookie_rs[0], 'cookievalue1': cookie[1] })
req = urllib2.Request("http://www.megaupload.com/multifetch/?
c=cookies", params, header )
print header
print params
#r = urllib2.urlopen(req)
return True

acc_details = account_details()
if len(sys.argv)==1:
print "Try -help for more information"
sys.exit("No arguments")


if sys.argv[1] == "-h" or sys.argv[1] == "–help":
print " > murs http://megaupload.com/?d=FILE1 … 
http://megaupload.com/?d=FILEN";
print " Download one or several rapidshare links passed as
argument separated by whitespace\n"
print " >murs links.txt"
print " Download a list of links from a file\n"
print " >murs account"
print " Automaticaly downloads the entire contents of your
account"
sys.exit()
## Process the arguments
## AFTER THIS ITS ALL

PySerial "write" should accept "bytearray"

2009-02-14 Thread John Nagle

  PySerial, which is basically a binary input/output system, is
still requiring "str" instead of "bytearray", even under Python 2.6.
For "file-like objects", "write" functions are supposed to accept
"bytearray" now, and "read" functions should return a "bytearray".

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


Re: PySerial "write" should accept "bytearray"

2009-02-14 Thread Tino Wildenhain

John Nagle wrote:

  PySerial, which is basically a binary input/output system, is
still requiring "str" instead of "bytearray", even under Python 2.6.
For "file-like objects", "write" functions are supposed to accept
"bytearray" now, and "read" functions should return a "bytearray".


I'm sure patches are welcome. And did you copy the pyserial author?

Regards
Tino


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


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread John Nagle

Roy Smith wrote:

In article <49970ce7$0$1665$742ec...@news.sonic.net>,
 John Nagle  wrote:

At the hardware level, there's a clock rate, a counter, and a divisor, 
so arbitrary baud rates can be set.


Is that really true of modern hardware?  The last time I looked at serial 
port hardware, UARTs took a base clock rate and divided it sequentially 
with flip-flops to get all the possible rates (they usually had some ugly 
logic to generate 110).  You were limited to the specific rates the 
hardware gave you.  Is that no longer the case?


   It is, but the traditional serial clock rate is 115200 Hz, so you can use
any subdivision of that as a baud rate.  The divisor for 45.45 baud is
something like 2535.

   Some exotic serial port devices use a 16MHz clock; I've used those for
supporting SICK LMS laser rangerfinders at 500,000 baud.

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


Re: ANN: SuPy - Script Sketchup with Python

2009-02-14 Thread r
Great work Greg, you are a Python zen master!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Untangling pythonWin and IDLE Processes on XP Pro

2009-02-14 Thread Thorsten Kampe
* Rhodri James (Fri, 13 Feb 2009 22:57:42 -)
> 
> On Fri, 13 Feb 2009 11:13:38 -, W. eWatson   
> wrote:
> 
> > OK, enough tinkering with the code and others matters on my end trying  
> > to find a work around. Somehow after much successful use of IDLE's  
> > execution facility, I've stepped on an invisible banana peel. I think  
> > it's evident that I'm not going around this problem easily with the IDLE  
> > execution attempts, and that another solution is required.
> 
> Congratulations, we've only been telling you this for the last few days.
> I wonder, is there any chance that you've noticed the solution given?

The Lord gives and the Lord takes. Sometimes it just takes a bit 
longer...

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


Re: Untangling pythonWin and IDLE Processes on XP Pro

2009-02-14 Thread Thorsten Kampe
* W. eWatson (Fri, 13 Feb 2009 20:58:33 -0800)
> Scott David Daniels wrote:
> > OK, you are using the oldest and least useful revision control
> > system, "rename and remember." I'd suggest you get and use bazaar,
> > but you'll just ask for shortcuts on how to use it without
> > understanding what it does.
> It works for me, and, frankly, I'm not interested in going to Linux,
> SunOS or other "revision systmes". These are way in my distant past,
> and the only reason I'm currently, and begrudgingly, once again
> writing programs is that the Python software program I am using is
> limited in its ability.

You, sir, certainly made my day. Thank you!

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


Re: Rapidshare to Megaupload script

2009-02-14 Thread James Matthews
This can be used as a great guide on writing pythonic code. Don't look at
the specific code that is being corrected but look at how the improvements
are being presented.

I would recommend someone who is learning python read this guide.

On Sun, Feb 15, 2009 at 12:17 AM, MRAB  wrote:

> aiwarrior wrote:
>
>> I've made this script and would like to have some input and share it
>> with the community.
>> I also have a page with some code i produce on my spare time.
>> http://pneves.net
>> Thanks
>>
>>  [snip]
>
>>
>> def from_rapidshare(url):
>>'''Check if this is a rapidshare link'''
>>return (url.startswith("rapidshare.com") or
>>url.startswith("www.rapidshare.com") or
>>url.startswith("http://rapidshare.com";) or
>>url.startswith("http://www.rapidshare.com";))
>>
>>  You can shorten:
>
>s.startswith(x) or s.startswith(y) or ...
>
> to:
>
>s.startswith((x, y, ...))
>
> [snip]
>
>>cfg = ConfigParser.SafeConfigParser()
>>cfg.read(path)
>>try:
>>m_user = cfg.get("Acc_Details", "m_user")
>>m_password = cfg.get("Acc_Details", "m_password")
>>r_user = cfg.get("Acc_Details", "r_user")
>>r_password = cfg.get("Acc_Details", "r_password")
>>except ConfigParser.NoSectionError or ConfigParser.NoOptionError:
>>
>
> In order to catch multiple exceptions it should be:
>
>except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
>
> print "no section or No Option"
>>print
>>os.remove(path)
>>return (m_user, m_password, r_user, r_password)
>>
>> def cookie_processor(cookie):
>>cookie = cookie.split("; ")
>> ##cookie = dict( [cookie[x].split("=") for x in xrange ( len
>> (cookie) ) ] )
>>
> Or:
> ##cookie = dict(x.split("=") for x in cookie)
>
>  ##if cookie['user'] != None:
>>
> When checking for None use "is" and "is not":
> ##if cookie['user'] is not None:
>
> return cookie
>> ##else:
>> ##print "Scheme has changed or authentication failes. Last
>> option most likely"
>> ##sys.exit()
>>
> Raising an exception is better than exiting.
>
>  ##
>>
>>  [snip]
>
>> def mu_auth(login):
>>r_user = login[0]
>>r_password = login[1]
>>cred = urllib.urlencode({"login": r_user, "password": r_password})
>>try:
>>req = urllib2.urlopen("http://www.megaupload.com";, cred)
>>cookie_mu = cookie_processor( req.headers.get("set-cookie",
>> "") )
>>
>>except:
>>
> Don't use an empty "except"; be explicit in which exception you want to
> catch .
>
> [snip]
>
>>
>> if sys.argv[1] == "-h" or sys.argv[1] == "–help":
>>
> Or:
>
>if sys.argv[1] in ("-h", "–help"):
>
> [snip]
>
>  elif from_rapidshare(sys.argv[1]): #if argument is one or more urls
>>for i in range(1, len(sys.argv)):
>>
> Or:
>
>for arg in sys.argv[1 : ]:
>
> and so on.
>
> if from_rapidshare(sys.argv[i]):
>>urls.append(sys.argv[i])
>> else:
>>print "This is not valid argument" , sys.argv[1]
>>sys.exit()
>> urls = []
>>
>>  [snip]
> HTH
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.goldwatches.com/

http://www.jewelerslounge.com/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading a file

2009-02-14 Thread Philipp Pagel
zaheer.ag...@gmail.com wrote:
> Hi

> How do i read  a file in Python and search a particular pattern
> like I have a file char.txt  which has

> Mango=sweet
> Sky=blue

> I want to get the strings sweet and blue,How to do this..?

If your entire file consists of such key=value pairs you may want to properly 
parse them:

for lne in infile:
line = line.rstrip()
key, value = line.split('=')
if key in ('Mango', 'Sky'):
print value

Or something like that - details depend on what exactly your criteria for
picking the values are, of course.

cu
Philipp

-- 
Dr. Philipp Pagel
Lehrstuhl f. Genomorientierte Bioinformatik
Technische Universität München
http://mips.gsf.de/staff/pagel
--
http://mail.python.org/mailman/listinfo/python-list


Re: Rapidshare to Megaupload script

2009-02-14 Thread MRAB

aiwarrior wrote:

I've made this script and would like to have some input and share it
with the community.
I also have a page with some code i produce on my spare time. http://pneves.net
Thanks


[snip]


def from_rapidshare(url):
'''Check if this is a rapidshare link'''
return (url.startswith("rapidshare.com") or
url.startswith("www.rapidshare.com") or
url.startswith("http://rapidshare.com";) or
url.startswith("http://www.rapidshare.com";))


You can shorten:

s.startswith(x) or s.startswith(y) or ...

to:

s.startswith((x, y, ...))

[snip]

cfg = ConfigParser.SafeConfigParser()
cfg.read(path)
try:
m_user = cfg.get("Acc_Details", "m_user")
m_password = cfg.get("Acc_Details", "m_password")
r_user = cfg.get("Acc_Details", "r_user")
r_password = cfg.get("Acc_Details", "r_password")
except ConfigParser.NoSectionError or ConfigParser.NoOptionError:


In order to catch multiple exceptions it should be:

except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):


print "no section or No Option"
print
os.remove(path)
return (m_user, m_password, r_user, r_password)

def cookie_processor(cookie):
cookie = cookie.split("; ")
##cookie = dict( [cookie[x].split("=") for x in xrange ( len
(cookie) ) ] )

Or:
##cookie = dict(x.split("=") for x in cookie)


##if cookie['user'] != None:

When checking for None use "is" and "is not":
##if cookie['user'] is not None:


return cookie
##else:
##print "Scheme has changed or authentication failes. Last
option most likely"
##sys.exit()

Raising an exception is better than exiting.


##


[snip]

def mu_auth(login):
r_user = login[0]
r_password = login[1]
cred = urllib.urlencode({"login": r_user, "password": r_password})
try:
req = urllib2.urlopen("http://www.megaupload.com";, cred)
cookie_mu = cookie_processor( req.headers.get("set-cookie",
"") )

except:
Don't use an empty "except"; be explicit in which exception you want to 
catch .


[snip]


if sys.argv[1] == "-h" or sys.argv[1] == "–help":

Or:

if sys.argv[1] in ("-h", "–help"):

[snip]


elif from_rapidshare(sys.argv[1]): #if argument is one or more urls
for i in range(1, len(sys.argv)):

Or:

for arg in sys.argv[1 : ]:

and so on.


if from_rapidshare(sys.argv[i]):
urls.append(sys.argv[i])
else:
print "This is not valid argument" , sys.argv[1]
sys.exit()
urls = []


[snip]
HTH
--
http://mail.python.org/mailman/listinfo/python-list


Re: Untangling pythonWin and IDLE Processes on XP Pro

2009-02-14 Thread Rhodri James
On Sat, 14 Feb 2009 05:03:13 -, W. eWatson   
wrote:



See my response to Scott. Thanks for your reply.


I did.  It was fundamentally mistaken in so many respects that
I formally give up on you.

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


GetKeyboardLayoutName Win32API

2009-02-14 Thread Tsolakos Stavros
Hi all.

I was trying to find a way to read the currently selected input layout
from an app written in python. I am aware that if the app were written
in C, I would have to call the GetKeyboardLayoutName() function. How can
this be done in Python? I want to avoid writing an extension just for
this. The function is not available through pywin32.

Thank you,
Stavros
--
http://mail.python.org/mailman/listinfo/python-list


ImportError: dynamic module does not define init function (inittypes)

2009-02-14 Thread konteya joshi
Hi,

Iam trying to run a programs which resides insides ..\pythonroot.

I see the following error on running the program:

'import site' failed; use -v for traceback
Traceback (most recent call last):
  File "skyline\alpine_kickoff.py", line 6, in ?
import re
  File "C:\Perforce\qa\testware\build\vmqa\python\python-2.4.1-as\lib\re.py",
line 5, in ?
from sre import *
  File "C:\Per\qa\test\build\vmqa\python\python-2.4.1-as\lib\sre.py",
line 265, in ?
import copy_reg
  File "C:\Per\qa\test\build\vmqa\python\python-2.4.1-as\lib\copy_reg.py",
line 7, in ?
from types import ClassType as _ClassType
ImportError: dynamic module does not define init function (inittypes)

Any body who would have an idea on what might be going wrong?

I have python-2.4.1-as directory in my PYTHONPATH which contains the
Lib,libs...etc in the directory. The Lib contains the re,sre,copy_reg
inside the directory.

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


Re: Function name limit in Python ?

2009-02-14 Thread Steven D'Aprano
Linuxguy123 wrote:

> Excuse my ignorance, but is there a limit to the size of function names
> in Python ?
> 
> I named a function getSynclientVersion() and I got an error when I
> called it.  

No no, don't tell us what error you got! I love guessing games. I'm guessing
you got either a SyntaxError or a NameError.


> I renamed the same function to getSCVersion() and it called 
> fine.

>>> def python_allows_really_long_function_names_with_no_problem():
... print "hello world"
... 
>>> python_allows_really_long_function_names_with_no_problem()
hello world
>>>
>>> ptyhon_allows_really_long_function_names_with_no_problem()
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'ptyhon_allows_really_long_function_names_with_no_problem'
is not defined

Can you spot the difference? More importantly, can you see how the error
message actually tells you what the error is?


-- 
Steven

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


Re: String concatenation performance with +=

2009-02-14 Thread Steven D'Aprano
Nick Craig-Wood wrote:

> The optimized += depends on their being no other references to the
> string.  Strings are immutable in python.  So append must return a new
> string.  However the += operation was optimised to do an in-place
> append if and only if there are no other references to the string.
> 
> You can see this demonstrated here
> 
> $ python -m timeit -s 'a="x"' 'a+="x"'
> 100 loops, best of 3: 0.231 usec per loop
> 
> $ python -m timeit -s 'a="x"; b=a' 's = a; a+="x"'
> 10 loops, best of 3: 30.1 usec per loop

That is a fantastic explanation of why you shouldn't rely on the concat
optimization, although the assignment b=a in the setup isn't necessary and
is a little misleading. Thank you.


-- 
Steven

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


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread Grant Edwards
On 2009-02-14, John Nagle  wrote:
> John Nagle wrote:
>
>>OK, tried to open the port, using Python 2.6, latest PySerial
>> and PyWin32:
>> 
>> ser = serial.Serial(port, baudrate=baud,
>> bytesize=serial.FIVEBITS,
>> parity=serial.PARITY_NONE,
>> stopbits=serial.STOPBITS_TWO)
>> 
>> ValueError: Cannot configure port, some setting was wrong. Original 
>> message: (87, 'SetCommState', 'The parameter is incorrect.')
>> 
>>Something doesn't like "serial.FIVEBITS".  That's a valid value, 
>> according
>> to "http://pyserial.wiki.sourceforge.net/pySerial";.  If changed to 
>> "serial.EIGHTBITS",  the code will execute, but of course does the wrong
>> thing.   That looks like a bug.
>
> OK, here's what's wrong.  The allowed numbers for stop bits in Windows are
>
>   ONESTOPBIT  0   1 stop bit.
>   ONE5STOPBITS1   1.5 stop bits.
>   TWOSTOPBITS 2   2 stop bits.
>
> The Python interface, however, only exports STOPBITS_ONE and STOPBITS_TWO.
> See "serialutil.py", at line 9, and "serialwin32.py" at lines 141-146.

That should be simple enough to fix.

> Linux has a different set of restrictions; Linux offers only 1 or 2 stop
> bits, and won't do arbitrary baud rates via the "termios" data structure,
> although there are other ways to request that.

It can be done via the ioctl that setserial uses, but that's
specific to a very few low-level drivers like the 16x50 one --
which is probably the one that matters to the OP, though he
hasn't said so.

-- 
Grant

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


Re: Can Python serial support run at 45.45 baud?

2009-02-14 Thread Grant Edwards
On 2009-02-14, John Nagle  wrote:
> Roy Smith wrote:
>> In article <49970ce7$0$1665$742ec...@news.sonic.net>,
>>  John Nagle  wrote:
>> 
>>> At the hardware level, there's a clock rate, a counter, and a
>>> divisor, so arbitrary baud rates can be set.
>> 
>> Is that really true of modern hardware?  The last time I
>> looked at serial port hardware, UARTs took a base clock rate
>> and divided it sequentially with flip-flops to get all the
>> possible rates (they usually had some ugly logic to generate
>> 110).  You were limited to the specific rates the hardware
>> gave you.  Is that no longer the case?
>
> It is, but the traditional serial clock rate is 115200 Hz, so
> you can use any subdivision of that as a baud rate.  The
> divisor for 45.45 baud is something like 2535.

On Linux IFF you're using a 16x50 UART, you can set arbitrary
divisors using the setserial utility.  From Python, you can
just invoke setserial (via os.system() or subprocess). It
should be pretty trivial: execute setserial with the parameters
"/dev/whatever spd_cust divisor 2535".  Then use the normal
calls to set the baud rate to 38400, and you should get 45.444
baud.  [Disclaimer: I haven't tested the above, and I may be
remembering it incorrectly.]

If that's too easy and you want to get your hands dirty, you
can look up the ioctl and parameter structure used by setserial
and invoke the ioctl directly from Python.

Neither method is portable to any other OS (or any other serial
driver, AFAIK).

> Some exotic serial port devices use a 16MHz clock; I've used
> those for supporting SICK LMS laser rangerfinders at 500,000
> baud.

:) 

Been there, done that -- along with that goofy checksum they
call a CRC.  They actually only do the CRC on one bit out of
every byte, and then they just xor in the rest of the byte.
It's very strange, rather slow, and not very good at detecting
errors.  A real, table-driven CRC would be both a lot faster
and a lot more robust.  My guess is that it was _supposed_ to
be a CRC routine, but somebody botched it.  They used the same
botched routine on the host end when they did testing, so
nobody noticed it was broken.  By the time anybody realized
that the CRC routine was broken, masked ROMs were in the field
and it was too late to fix it -- so they just documented the
"custom" algorithm in the manual and called it good.

-- 
Grant

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


Re: PySerial "write" should accept "bytearray"

2009-02-14 Thread Grant Edwards
On 2009-02-14, John Nagle  wrote:

> PySerial, which is basically a binary input/output system, is
> still requiring "str" instead of "bytearray", even under Python 2.6.
> For "file-like objects", "write" functions are supposed to accept
> "bytearray" now, and "read" functions should return a "bytearray".

I'm sure a patch would be happily accepted.  I don't use 2.6
yet, and I don't know if the other people who've worked on
pyserial do either.

-- 
Grant

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


Re: Reading a file

2009-02-14 Thread Steven D'Aprano
Philipp Pagel wrote:

> zaheer.ag...@gmail.com wrote:
>> Hi
> 
>> How do i read  a file in Python and search a particular pattern
>> like I have a file char.txt  which has
> 
>> Mango=sweet
>> Sky=blue
> 
>> I want to get the strings sweet and blue,How to do this..?
> 
> If your entire file consists of such key=value pairs you may want to
> properly parse them:

The proper way to parse them would be with the ConfigParser module.

The only negative with ConfigParser is that it requires a section label. In
my experience it is quite common for INI files with only a single section
to leave out the label.


-- 
Steven

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


Re: doctest fails to see tests in decorated functions

2009-02-14 Thread Steven D'Aprano
Steven D'Aprano wrote:

> I've just spotted a bug in doctest that it fails to see tests inside
> decorated functions. It's been reported before:
> 
> http://bugs.python.org/issue1108
> 
> but the patch submitted doesn't work for me.

Never mind, it was a PEBCAK error. I failed to notice that applying a
decorator to a function shadows the functions docstring.

Normally I would use functools.wraps, but I am currently limping along on a
Python 2.4 installation here, which doesn't have it.


-- 
Steven

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


Re: doctest fails to see tests in decorated functions

2009-02-14 Thread Michele Simionato
On Feb 15, 6:31 am, Steven D'Aprano  wrote:
> Never mind, it was a PEBCAK error. I failed to notice that applying a
> decorator to a function shadows the functions docstring.
>
> Normally I would use functools.wraps, but I am currently limping along on a
> Python 2.4 installation here, which doesn't have it.

You may consider this: http://pypi.python.org/pypi/decorator

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


"Byte" type?

2009-02-14 Thread John Nagle

With "bytearray", the element type is considered to be "unsigned byte",
or so says PEP 3137: "The element data type is always 'B' (i.e. unsigned byte)."

Let's try:

Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on
win32
>>> xx = b'x'
>>> repr(xx)
"'x'"
>>> repr(xx[0])
"'x'"
>>> repr(xx[0][0])
"'x'"
>>>

But that's not what "repr" indicates.  The bytearray element is apparently
being promoted to "bytes" as soon as it comes out of the array.

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


Re: "Byte" type?

2009-02-14 Thread Lie Ryan
On Sat, 14 Feb 2009 22:10:41 -0800, John Nagle wrote:

>  >>> xx = b'x'

Isn't this creating a regular byte?

Shouldn't creation of bytearray be:

>>> xx = bytearray(b'x')


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


Re: "Byte" type?

2009-02-14 Thread Erik Max Francis

John Nagle wrote:

With "bytearray", the element type is considered to be "unsigned byte",
or so says PEP 3137: "The element data type is always 'B' (i.e. unsigned 
byte)."


Let's try:

Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit 
(Intel)] on

win32
 >>> xx = b'x'
 >>> repr(xx)
"'x'"
 >>> repr(xx[0])
"'x'"
 >>> repr(xx[0][0])
"'x'"
 >>>

But that's not what "repr" indicates.  The bytearray element is apparently
being promoted to "bytes" as soon as it comes out of the array.


There's no distinction byte type.  A single character of a bytes type is 
also a bytes.


--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
 San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
  Did you ever love somebody / Did you ever really care
   -- Cassandra Wilson
--
http://mail.python.org/mailman/listinfo/python-list


Re: "Byte" type?

2009-02-14 Thread Chris Rebert
On Sat, Feb 14, 2009 at 9:56 PM, Lie Ryan  wrote:
> On Sat, 14 Feb 2009 22:10:41 -0800, John Nagle wrote:
>
>>  >>> xx = b'x'
>
> Isn't this creating a regular byte?
>
> Shouldn't creation of bytearray be:
>
 xx = bytearray(b'x')

Indeed, and slicing that does give back a single byte (which Python
represents as an integer):

>>> b = bytearray(b'abc')
>>> b[0]
97

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Setuptools & Python 3.0

2009-02-14 Thread Sylvain Rabot

Hi,

I would like to know if the official maintainers of setuptools are working 
on a release for Python 3.0.


Regards. 


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


How to peek inside a decorated function

2009-02-14 Thread Steven D'Aprano
Suppose I have a function f() which I know has been decorated, but I don't
have access to the original undecorated function any longer:

def reverse(func):
def f(*args):
args = list(args)
args.reverse()
return func(*args)
return f

def say(*args):
print args

rsay = reverse(say)
del say


Is there any way to peek inside the decorated function rsay() to get access
to the undecorated function say()?

If I look at the code object I can see a reference to the original:

>>> rsay.func_code.co_names
('list', 'args', 'reverse', 'func')

and if I disassemble the code object I can see it being dereferenced:

>>> dis.dis(rsay.func_code)
[snip for brevity]
  5  22 LOAD_DEREF   0 (func)
 25 LOAD_FAST0 (args)
 28 CALL_FUNCTION_VAR0
 31 RETURN_VALUE

but if I look at the closure object, nothing seems useful:

>>> dir(rsay.func_closure[0])
['__class__', '__cmp__', '__delattr__', '__doc__', 
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__']


and I can't find any other attributes which refers back to the undecorated
original function.


-- 
Steven

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


multiprocessing python2.6.1

2009-02-14 Thread km
Dear all,


I have a problem with multiprocessing module usage with python2.6.1
Here Pool object is to be instantiated with 50 processes  and the method
'run' has to be called with pool object.
My actual requirement is to reuse the 50 processes for further processing
until 100 items.

#
import multiprocessing as mp

def add(a,b):
return a+b


if __name__ == '__main__':
p = mp.Pool(processes=50)
y = 40
for x in  range(100):
p.apply_async(run, (x, y) )
##
The problem now is that it creates only 32 processes (and not more) and
exits with processing first 32 numbers  from range(100) .
Is there a cap on the number of subprocesses one can fork ?
Even then,  once these 32 processes r free, I would like to reuse them with
next 32 inputs (x) from  range(100 ) in that order.
how could i accomplish this ?

thanks in advance.
KM
--
http://mail.python.org/mailman/listinfo/python-list