Re: application and web app technologies

2006-01-02 Thread Van
Try matching technologies when building a decision matrix...
For example, if you need the PHP open source pile then MySQL works
well.
If you like SQL Server then ASP.NET or Mono with C# is attractive.
Eliminate contenders that have performance obstacles...
PERL does not multithread, PHP.com holds back the cache goods from
PHP.org.
Find a theme and follow it a logical conclusion, here is my
experienceI was looking for a good JavaScript Library to Ajax my
offering.I found Mochikit to be well documented, cross browser with an
active forum.The author described Mochikit as Python like which caused
me to investigate Python.Terse, compact, easy to embed C/C++ with
multithreading are impressive.ZOPE a python written web server with
ZODB (in-memory db/cache mechanism) sealed the deal for me.  Most of
the Zope/Python/SQL code uses PostgreSQL examples.
I am concerned about bandwidth associated with the myriad dev tasks.
A good JavaScript library eliminates major hassles, you may have to
combine some.
SQL is SQL, it comes down to documentation, interface, licensing or
preference.
Server code that does not render standards compliant web pages is a
pain to test.
ZPT from ZOPE solves that problem nicely, with a fast XML page parser.
So prioritize you needs:  Labor, Standards, Functionality, Training,
etc.
Then evaluate you stacks based on your priorities.
The last time I was involved with university operations we had unit
record gear.
Good Luck,
Van

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


Re: Trying to find zip codes/rest example

2007-08-04 Thread Van Lindberg
Jay Loden wrote:
> I don't remember the demo, but a little creative googling turned up
>
> http://bitworking.org/news/132/REST-Tips-URI-space-is-infinite
>
> Which matches the description above perfectly, so I assume it's what you were 
> after :-)
That is exactly what I was trying to find!  I bow down before your 
superior google-fu.

Thanks,

VanL

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


Re: sending a handmade SOAP request

2008-01-31 Thread Van Gale
On Thu, 31 Jan 2008 11:03:26 -0800, Bernard wrote:

> Hey y'all,
> 
> Is there a way to POST a handmade SOAP request *without* using any
> libraries like SOAPpy?

Yes, it's quite easy to SOAP by hand.

I use Oren Tirosh's ElementBuilder class (on top of lxml instead of 
ElementTree) to build the SOAP request and the xpath capabilities in lxml 
to pull out the data I need from the response.

http://www.tothink.com/python/ElementBuilder/
http://codespeak.net/lxml/

An incomplete example for contructing a request looks something like this:

body = Element('soap:Envelope',
 { 'xmlns:soap': nss['soap']},
 Element('soap:Header'), Element('soap:Body',
   { 'xmlns:msgs': nss['msgs'] },
   Element('msgs:login',
 Element('msgs:passport',
   { 'xmlns:core': nss['core'] },
 Element('core:password', password),
 Element('core:account', account)

I use httplib2 for sending the HTTP requests:

http://code.google.com/p/httplib2/

Incomplete example:

headers['SOAPAction'] = action
headers['Content-length'] = str(len(etree.tostring(body)))
response, content = self._client.request(
   self.ns_uri, "POST",
   body=etree.tostring(body), headers=self._headers)
if response.status == 500 and not \
(response["content-type"].startswith("text/xml") and \
len(content) > 0):
raise HTTPError(response.status, content)
if response.status not in (200, 500):
raise HTTPError(response.status, content)
doc = etree.parse(StringIO(content))
if response.status == 500:
faultstring = doc.findtext(".//faultstring")
raise HTTPError(response.status, faultstring)

Now it's just a matter of using xpath expressions to dig into the "doc" 
structure for the bits you need.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with os.chdir()

2009-03-11 Thread van Asselt
Hello,

In order to prevent this type of problems, I alway do the following:

import path

path = something
path = os.path.normpath(path)
os.chdir(path)

This prevents a lot of problems for me.

Regards,
Henk

"Tim Golden"  wrote in message 
news:[email protected]...
> [email protected] wrote:
>> Hello all,
>>I am writing a python script which has to access deep paths
>> then supported normally by the Windows OS (>255). So I am appending "\
>> \?\" to do so. But when I use the path in the above fashion with
>> os.chdir() it is unable to recognize my folder and throwing an error:
>>
>> Traceback (most recent call last):
>>   File "C:\JPDump\test.py", line 31, in 
>> renameStubs(file)
>>   File "C:\JPDump\test.py", line 15, in renameStubs
>> os.chdir (path)
>> WindowsError: [Error 123] The filename, directory name, or volume
>> label syntax is incorrect: '\\?\\C:\\TestDataSet\
>> \Many_Files_1_1KB_FIles\\001_0009_1000 FILES\\'
>>
>> The value of my path variable is
>> \?\C:\TestDataSet\Many_Files_1_1KB_FIles\001_0009_1000 FILES\
>
>
> There need to be two backslashes at the beginning:
>
> \\?\C:\TEST.FILES\
>
> Note the double backslash before the question mark.
>
> TJG 


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


Re: Command parsing... best module to use?

2009-11-07 Thread van Asselt
Hello Colin,

I have been using 'cmdloop.py' from Crutcher Dunnavant in a few programs
See http://py-cmdloop.googlecode.com/svn/trunk/cmdloop.py

Regards,
Henk

-

"Collin D"  wrote in message 
news:[email protected]...
> Hey everyone.
>
> I am writing a game in python, and it includes a text console somewhat
> like the one in WoW and Runescape. I want to be able to include "/"
> commands, like IRC, and was wondering what the best module would be to
> parse these.
>
> Thanks a lot,
> Collin D 


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


Re: Conditional decoration

2012-06-18 Thread Emile van Sebille

On 6/18/2012 3:16 PM Roy Smith said...

Is there any way to conditionally apply a decorator to a function?
For example, in django, I want to be able to control, via a run-time
config flag, if a view gets decorated with @login_required().

@login_required()
def my_view(request):
 pass



class myDecorator(object):
def __init__(self, f):
self.f = f
def __call__(self):
print "Entering", self.f.__name__
self.f()
print "Exited", self.f.__name__


def null(a): return a


#if condition:
myDecorator = null


@myDecorator
def aFunction():
print "aFunction running"

aFunction()


Cobbled together from http://www.chrisevans3d.com/pub_blog/?p=530 and 
http://stackoverflow.com/questions/3687046/python-sphinx-autodoc-and-decorated-members


HTH

Emile

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


Re: emded revision control in Python application?

2012-06-22 Thread Emile van Sebille

On 6/22/2012 8:58 AM duncan smith said...

Hello,
I have an application that would benefit from collaborative working.
Over time users construct a "data environment" which is a number of
files in JSON format contained in a few directories


You don't say what your target platform is, but on linux I've done some 
testing with python-fuse that allows interception on file access to take 
whatever actions you like, in your case archive prior upon write.


Might be worth a look.

Emile

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


Re: emded revision control in Python application?

2012-06-22 Thread Emile van Sebille

On 6/22/2012 11:19 AM duncan smith said...

On 22/06/12 17:42, Emile van Sebille wrote:

On 6/22/2012 8:58 AM duncan smith said...

Hello,
I have an application that would benefit from collaborative working.
Over time users construct a "data environment" which is a number of
files in JSON format contained in a few directories


So, will the users modify their local environment and you'd push the 
revisions to a known location for redistribution? How might peer-to-peer 
work? How would you know which peers get the change, or would all peers 
get the change?


I've been working with rpyc (in as much spare time as I can manage) on 
some similar sounding issues and am now settling on a central system 
which provides convenient administration and potential relaying or 
pulling.  See http://rpyc.sourceforge.net/


I just tested my in-process development status and find 64 remote 
machines up and 5 non-responsive which in my case are likely machines 
that are not yet configured properly.  As this has been on the back 
burner the past two months I'm pleased with how it's fared in the face 
of neglect.


At least with rpyc (which does have a low learning curve) you'll be 
fully in python.


Emile




You don't say what your target platform is, but on linux I've done some
testing with python-fuse that allows interception on file access to take
whatever actions you like, in your case archive prior upon write.

Might be worth a look.

Emile



I develop on Linux, but most users would be running some flavour of
Windows. Initially I'd like to get something up and running that would
allow me to collaborate from an Ubuntu box at home with someone using a
Windows machine (not sure which version) in an office at the University
of Manchester. The most likely end users would (probably) be running
Windows machines on a local network with no internet access.

I expect it would generally be possible to have an always-on server, but
I'm also thinking about peer to peer style communication (information
might not always be completely available, but it's better than being
totally unaware of changes being made by others).

I don't have much experience getting applications to communicate across
a network, particularly in a reasonably secure fashion. Someone I know
also suggested RabbitMQ. Any pointers that help me to reduce the options
to a manageable number of candidates will be appreciated. A shallow
learning curve would also be good (given that ATM this is an idea I want
to try out rather than paid work). I am looking at fuse at the moment.
Thanks.

Duncan



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


moving methods from class to instance of other class

2012-06-28 Thread lars van gemerden
Hi all,

I have some trouble with the following question: Let say i have the
following classes:

class A(object):
def __init__(self):
self.name = 'a'
def do(self):
print 'A.do: self.name =', self.name

class B(object):
def __init__(self):
self.name = 'b'



The question is: How do i move the 'do' method from A to b (resulting
in  printing "A.do: self.name = b")?

I have tried (with a = A() and b  B()):

B.do = types.MethodType(A.do, b) #Error

and stuff like:

b.do = a.do
b.do()

But either i get an error or b.do() prints  "A.do: self.name = a", so
the self parameter of a.do is stored somehow in the method.

In other words, how do i unbind 'do' from a/A and bind it to b (the
instance)?

Cheers, Lars




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


Re: moving methods from class to instance of other class

2012-06-28 Thread lars van gemerden
On Jun 28, 9:22 am, Benjamin Kaplan  wrote:
> On Wed, Jun 27, 2012 at 11:59 PM, lars van gemerden
>
>
>
>
>
>
>
>
>
>  wrote:
> > Hi all,
>
> > I have some trouble with the following question: Let say i have the
> > following classes:
>
> > class A(object):
> >    def __init__(self):
> >        self.name = 'a'
> >    def do(self):
> >        print 'A.do: self.name =', self.name
>
> > class B(object):
> >    def __init__(self):
> >        self.name = 'b'
>
> > The question is: How do i move the 'do' method from A to b (resulting
> > in  printing "A.do: self.name = b")?
>
> > I have tried (with a = A() and b  B()):
>
> > B.do = types.MethodType(A.do, b) #Error
>
> > and stuff like:
>
> > b.do = a.do
> > b.do()
>
> > But either i get an error or b.do() prints  "A.do: self.name = a", so
> > the self parameter of a.do is stored somehow in the method.
>
> > In other words, how do i unbind 'do' from a/A and bind it to b (the
> > instance)?
>
> > Cheers, Lars
>
> Is there any particular reason you can't just have B be a subclass of
> A? You could do
>
> b.do = types.MethodType(A.do.im_func, b, B)
>
> but there's no point in re-inventing the wheel.

Perfect, Thank you,

As to the why, to make a long story short, actually instantiation
would fit better conceptually than inheritance in this case, but that
would mean the 'A' instances would be types, which introduces
metaclasses, which i tried but i ran into problems with e.g. pickle
and with complexity.

Cheers, Lars
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2012-07-05 Thread Emile van Sebille

On 7/5/2012 12:22 AM Maurizio Spadaccino said...

Hi all

I'm new to Python but soon after a few days of studying its features I
find it my favourite mean of programming scripts to allow for data
storing and mining. My idea would be to inplement python scripts from
inside an excel sheet that would store and fetch data from a Mysql
database.


Look again at the com interface -- I've created excel commands 
implemented entirely in python this way and it will do everything you're 
looking for.  Just start with some of the examples and extend from there.


See http://oreilly.com/catalog/pythonwin32/chapter/ch12.html

I did this ten years ago so things have likely changed, but the bits of 
the python script that set up com then looked like this:



class fenxUtilities:
_public_methods_ = [ 'FirstPartInsp' ]
_reg_progid_ = "fenxDCom.Util"
_reg_clsid_ = "{3EAD7AB4-2978-4360-8F7D-33FB36E9E146}"
def FirstPartInsp(self,nomDiam, numFlutes, nomOAL, nomLOC):
return EMSpecs(nomDiam, numFlutes, nomOAL, nomLOC).retvals


if __name__=='__main__':
print "Registering COM server..."
import win32com.server.register
win32com.server.register.UseCommandLine(fenxUtilities)


HTH,

Emile





So i need the script to be launched, say, by pressing a button
in excel and, for instance, retrieve immediately data from the mysql
table. For what I found in the net, it seems there are three ways to
control python from excel:
1) run my python script via shell using the Run command in VBA, which
seems to me the easiest (but maybe slower?) way;
2) creating a COM server, for which I need more training since it doesnt
appear that easy;
3) via Microsoft Script Control, for which I saw some example around
where It looks like I can 'simulate' a python shell via the
'executeStatement' command.

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

Thanks for you help
Maurizio


--
Maurizio


www.mauriziospadaccino.com 
---
If you can avoid printing this e-mail, you are only doing good for our
planet.





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


Re: Re: code review

2012-07-06 Thread lars van gemerden
On Sunday, July 1, 2012 5:48:40 PM UTC+2, Evan Driscoll wrote:
> On 7/1/2012 4:54, Alister wrote:
> > On Sat, 30 Jun 2012 23:45:25 -0500, Evan Driscoll wrote:
> >> If I had seen that in a program, I'd have assumed it was a bug.
> > 
> > You would?
> > I have only been using python for 6 - 12 months but in my past I 
> > programmed microcontrollers in assembly.
> > 
> > as soon as i saw it i understood it & thought great, like a light bulb 
> > going on.
> 
> It's not a matter of "I wouldn't have understood what the author was
> trying to do" (with a small caveat), it's a matter of "I'd have assumed
> that the author didn't understand the language semantics."
> 
> I'm pretty sure it goes contrary to *every other programming language
> I've ever used* (and a couple that I haven't).
> 
> I understand why Python does it, and it certainly is nice in that it
> matches typical mathematical notation, but the surprise quotient is
> *very* high in the PL world IMO.
> 
> Evan

Avoiding suprises would mean we cannot improve languages, just reshuffle 
features?

Cheers, Lars
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2012-07-06 Thread Emile van Sebille

On 7/6/2012 1:31 AM Maurizio Spadaccino said...


Could you provide me a more detailed 'how-to' tutorial on implementing a VBA 
macro that calls a script or a function from python, or tell me where on the 
web I can find it? The OReilly chapter seems a bit hard for me at this stage?


I'm not going to write a how-to, but the relevant bits from the VBA code 
look like this:


Set fpiUtils = CreateObject("fenxDCom.Util")
response = fpiUtils.FirstPartInsp(nomDiam, numFlutes, nomOAL, nomLOC)


I dont know, for example, where i should tell the macro where to locate the 
script...


Registering the com server does that for you (the __name__ == __main__ 
part of the python script)


Again, get one of the examples working and move out from there.

Emile



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


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

2012-07-07 Thread Emile van Sebille

On 7/7/2012 2:05 AM Maurizio Spadaccino said...

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



Here's how to generate an ID, but it's likely not to occur on other 
machines.


>>> import pythoncom; print pythoncom.CreateGuid()
{CE571F2A-6BD8-4A8D-9482-4EC02FAC171E}

There's an interesting perspective on uniqueness of guid's at

http://betterexplained.com/articles/the-quick-guide-to-guids/

So, create it once and hardcode it in.  If you provide new features in a 
subsequent version you may want to issue a new guid, particularly to 
develop a new version while still running the old on the same machine.


Each user will then need to have python and pythonwin installed, then 
run your com server to execute the com server registration piece in the 
"if __name__ == '__main__'" part of things.


Emile



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


Re: select module missing/corrupt

2012-07-07 Thread Emile van Sebille

On 7/7/2012 5:03 AM John Pote said...

We are using a virtual web server running some version of Unix. It has
Python versions 2.4,2.6 and 3.1 pre-installed.

(BTW the intention is to use Python for a CGI script.)

When my script imports subprocess I get the traceback
File "/usr/lib/python2.6/subprocess.py", line 427, in 
import select
ImportError: No module named select

On searching the Python installation I found what I presume is the
select module library file

/usr/lib/python2.6/lib-dynload/select_failed.so

whereas in the 2.4 installation the file is
/usr/lib/python2.4/lib-dynload/select.so
and subprocess imports OK.

Anyone know why the version 2.6 select .so file should be renamed
select_failed.so and so not able to be imported?



When python builds, modules that don't build cleanly get renamed 
[module]_failed.  See 
http://mail.python.org/pipermail/python-dev/2002-March/020568.html for 
more info.


Emile





Of interest the 3.1 installation also has the select module file
re-named select_failed.so.

Any help appreciated,

Regards,
John Pote


--- Posted via news://freenews.netfront.net/ - Complaints to
[email protected] ---



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


Re: Python Interview Questions

2012-07-09 Thread Emile van Sebille

On 7/9/2012 2:22 PM Peter said...

One of my favourite questions when interviewing - and it was 100% reliable :-) - 
"what are your hobbies?"

If the answer included programming then they were hired, if not, then they went to the 
"B" list.

In my experience, anybody who is really interested in programming will have it as a hobby (and is 
keen to learn even if they don't currently have the knowledge you require) - otherwise it is 
"just a job". Every job has a learning curve - whether it is just the particular domain 
or even a new language, the individual who sees programming as more "than a job" will 
come up to speed much faster and be more productive in both the short and long term.

Every programmer I have ever hired using this criteria worked out well.



Hence the age bias.

Personally, I'm quite happy now that I've divorced my hobby from my 
career.  And my family likes me better too.


Emile





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


Re: What's wrong with this code?

2012-07-23 Thread Emile van Sebille

On 7/23/2012 7:50 AM Stone Li said...

I'm totally confused by this code:

Code:

a = None
b = None
c = None
d = None
x = [[a,b],
  [c,d]]
e,f = x[1]
print e,f


This prints the first None,None


c = 1
d = 2
print e,f


And nothing has happened to e or f, so this is the second None,None

Why do you expect 1,2?

Emile




e = 1
f = 2
print c,d

Output:

None None
None None
1 2


I'm expecting the code as:

None None
1 2
1 2


What's wrong?
And this question made my GUI program totally out of control.
Thanks






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


Re: Python and Outlook, sendinf an image in the body of email

2012-07-23 Thread Emile van Sebille

On 7/23/2012 11:33 AM [email protected] said...

I tried something similar to the example at 
http://stackoverflow.com/questions/4312687/how-to-embed-images-in-email .

Problem is, this line is not understood:
mail.BodyFormat = OlBodyFormat.olFormatHTML


If I read the example properly, this is bvisual basic and should result 
in some form of visual basic error (possibly library related) if 
anything and not a python traceback.


Emile



 Traceback (most recent call last):
   ...
   appt.BodyFormat = OlBodyFormat.olFormatHTML
 NameError: name 'OlBodyFormat' is not defined

Bruce





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


Re: Generating valid identifiers

2012-07-26 Thread Emile van Sebille

On 7/26/2012 5:26 AM Laszlo Nagy said...

I have a program that creates various database objects in PostgreSQL.
There is a DOM, and for each element in the DOM, a database object is
created (schema, table, field, index and tablespace).

I do not want this program to generate very long identifiers. It would
increase SQL parsing time, and don't look good. Let's just say that the
limit should be 32 characters. But I also want to recognize the
identifiers when I look at their modified/truncated names.


I had a similar problem with one customer where their legacy green 
screen app allowed only 40 characters max for the product description, 
but the marketing description lengths routinely ran out to 75 or so 
characters.  In that situation I reviewed the source marketing 
descriptions and prepped a utility to translate common longer words or 
phrases to one of a selection of shortened abbreviations. I then cycled 
through best fitting substitutions to get as close as reasonable to the 
40 character limit which generally resulted in recognizable descriptions 
in the legacy environment that matched by being directly derived from 
the longer marketing names.


YMMV,

Emile






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


Re: Python Error

2012-07-29 Thread Emile van Sebille

On 7/29/2012 5:30 AM [email protected] said...

On Sunday, July 29, 2012 2:57:18 PM UTC+5:30, (unknown) wrote:

Dear Group,
I was trying to convert the list to a set, with the following code:
set1=set(list1)

Thanks for the answer. But my list does not contain another list that is the 
issue. Intriguing. Thinking what to do.


Now you need to identify the type of the object that is causing python 
to misreport the unhashable type causing the error as the error you're 
getting says list and you say there isn't one.  So, now we have a python 
bug.


>>> set ([1,2,3])
set([1, 2, 3])
>>> set ([1,2,[]])
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'list'
>>> set ([1,2,{}])
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'dict'



> the code was running fine, but all on a sudden started to give the 
following error,

>
>
>
> set1=set(list1)
>
> TypeError: unhashable type: 'list'


Try adding the following:

for ii in list1:
try:
set([ii])
except:
print "this causes an error type (val): %s (%s)"  (type(ii),ii)


Either it's a python bug or there really is a list in there.

Emile


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


Re: Is Python a commercial proposition ?

2012-07-30 Thread Emile van Sebille

On 7/29/2012 5:12 PM Rodrick Brown said...

Until the
GIL is fixed I doubt anyone will seriously look at Python as an option
for large enterprise standalone application development.


See openERP -- http://www.openerp.com/ -- they've been actively 
converting SAP accounts and have recently absorbed a couple SAP resellers.


Emile


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


Re: Linux shell to python

2012-07-30 Thread Emile van Sebille

On 7/30/2012 3:56 PM Dan Stromberg said...


On Mon, Jul 30, 2012 at 9:26 PM, Barry Scott 


And of course you can write list comprehensions on as many lines as
it take to make the code maintainable.

Sigh, and I'm also not keen on multi-line list comprehensions,
specifically because I think they tend to make less readable code.  It
also becomes a mess when you need to insert print statements to get some
visibility into what's going on.


I tend to write long one-liners then convert them to loops the first 
time I need to see what's going on.


Premature optimization otherwise.  :)

Emile



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


Re: looking for a neat solution to a nested loop problem

2012-08-06 Thread Emile van Sebille

On 8/6/2012 10:14 AM Tom P said...

On 08/06/2012 06:18 PM, Nobody wrote:

On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote:


consider a nested loop algorithm -

for i in range(100):
  for j in range(100):
  do_something(i,j)

Now, suppose I don't want to use i = 0 and j = 0 as initial values, but
some other values i = N and j = M, and I want to iterate through all
10,000 values in sequence - is there a neat python-like way to this?


for i in range(N,N+100):
for j in range(M,M+100):
do_something(i,j)

Or did you mean something else?


no, I meant something else ..

   j runs through range(M, 100) and then range(0,M), and i runs through
range(N,100) and then range(0,N)

.. apologies if I didn't make that clear enough.



 for i in range(N,N+100):
 for j in range(M,M+100):
 do_something(i % 100 ,j % 100)

Emile



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


Re: looking for a neat solution to a nested loop problem

2012-08-06 Thread Emile van Sebille

On 8/6/2012 12:22 PM Grant Edwards said...

On 2012-08-06, Tom P  wrote:



   ah, that looks good - I guess it works in 2.x as well?


I don't know.  Let me test that for you...





Yes, it works in 2.x as well.



:)

And from the docs, all the way back to 2.3!

9.7. itertools  Functions creating iterators for efficient looping
New in version 2.3.

Emile

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


Re: [newbie] String to binary conversion

2012-08-06 Thread Emile van Sebille

On 8/6/2012 1:46 PM Mok-Kong Shen said...


If I have a string "abcd" then, with 8-bit encoding of each character,
there is a corresponding 32-bit binary integer. How could I best
obtain that integer and from that integer backwards again obtain the
original string? Thanks in advance.


It's easy to write one:

def str2val(str,_val=0):
if len(str)>1: return str2val(str[1:],256*_val+ord(str[0]))
return 256*_val+ord(str[0])


def val2str(val,_str=""):
if val>256: return val2str(int(val/256),_str)+chr(val%256)
return _str+chr(val)


print str2val("abcd")
print val2str(str2val("abcd"))
print val2str(str2val("good"))
print val2str(str2val("longer"))
print val2str(str2val("verymuchlonger"))

Flavor to taste.

Emile

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


Installing Python 3.2.3 on Win 7

2012-08-16 Thread Johan van Zyl
Hi

I installed Python 3.2.3 successfully on my work laptop (XP) but
cannot seem to do it on my home PC (Win7)
I click the button to install and the window just disappears o the screen.
So how do I in fact install Python 3.2.3 on Win 7?

-- 
Johan van Zyl
PMB - Box 21673, Mayors Walk, 3208
Pretoria - Box 2667, Brooklyn Square, 0075
FAX: 086 622 9554
The answer my friend is blowin in the wind...
If you forward messages, Please use the BCC area, and Please REMOVE
all email addresses and all the useless info at the bottom of the
message, before you send it on. ONLY send out CLEAN and TIDY emails.
Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installing Python 3.2.3 on Win 7

2012-08-16 Thread Johan van Zyl
Python 32 bit on Win 7 32 bit.
Thx!

On 16 August 2012 12:32, Tommi Helander  wrote:
> Hi Johan,
>
> -Are you trying to install 32 or 64-bit Python?
> -Is your Win7 32 or 64-bits?
> -Have you tried running the Python installer from the command line to see if
> it generates any helpful output?
>
> -Tommi Helander
>
>> Date: Thu, 16 Aug 2012 09:17:43 +0200
>> Subject: Installing Python 3.2.3 on Win 7
>> From: [email protected]
>> To: [email protected]
>
>>
>> Hi
>>
>> I installed Python 3.2.3 successfully on my work laptop (XP) but
>> cannot seem to do it on my home PC (Win7)
>> I click the button to install and the window just disappears o the screen.
>> So how do I in fact install Python 3.2.3 on Win 7?
>>
>> --
>> Johan van Zyl
>> PMB - Box 21673, Mayors Walk, 3208
>> Pretoria - Box 2667, Brooklyn Square, 0075
>> FAX: 086 622 9554
>> The answer my friend is blowin in the wind...
>> If you forward messages, Please use the BCC area, and Please REMOVE
>> all email addresses and all the useless info at the bottom of the
>> message, before you send it on. ONLY send out CLEAN and TIDY emails.
>> Thank you.
>> --
>> http://mail.python.org/mailman/listinfo/python-list



-- 
Johan van Zyl
PMB - Box 21673, Mayors Walk, 3208
Pretoria - Box 2667, Brooklyn Square, 0075
FAX: 086 622 9554
The answer my friend is blowin in the wind...
If you forward messages, Please use the BCC area, and Please REMOVE
all email addresses and all the useless info at the bottom of the
message, before you send it on. ONLY send out CLEAN and TIDY emails.
Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python web apps on shared ASO servers?

2012-08-16 Thread Emile van Sebille

On 8/16/2012 7:01 AM Gilles said...

On Sun, 12 Aug 2012 02:03:33 +0200, Gilles  wrote:

Does it mean that ASO only supports writing Python web apps as
long-running processes (CGI, FCGI, WSGI, SCGI) instead of embedded
Python à la PHP?


I need to get the big picture about the different solutions to run a
Python web application.


From what I read, it seems like this is the way things involved over

the years:

CGI : original method. Slow because the server has to spawn a new
process to run the interpreter + script every time a script is run.

mod_python : Apache module alternative to CGI. The interpreter is
loaded once, and running a script means just handling the script

mod_wsgi : mod_python is no longer developped, and mod_wsgi is its new
reincarnation

FastCGI and SCGI: Faster alternativees to CGI; Run as independent
programs, and communicate with the web server through either a Unix
socket (located on the same host) or a TCP socket (remote  host)

Is this correct?

Thank you.




I'm sure there's no single correct answer to this.

Consider (python 2.6]:

emile@paj39:~$ mkdir web
emile@paj39:~$ cd web
emile@paj39:~/web$ cat > test.html
hello from test.html
emile@paj39:~/web$ python -m SimpleHTTPServer

Then browse to localhost:8000/test.html

Emile



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


Re: Python 2.7 import socket urllib fails to load, module not found

2012-08-17 Thread Emile van Sebille

On 8/17/2012 12:20 PM [email protected] said...

Just installed python 2.7 and using with web2py.

When running python from command line to bring up web2py server, get errors 
that python socket and urllib modules cannot be found, can't be loaded. This is 
not a web2py issue.



So, on my system I get:


ActivePython 2.7.0.2 (ActiveState Software Inc.) based on
Python 2.7 (r27:82500, Aug 23 2010, 17:18:21) [MSC v.1500 32 bit 
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> import socket
>>>

What does your system show?

Emile



No other python versions are on the my machine.  Pythonpath has the requisite 
folders identified.

Would appreciate any insights as to what may be happening.

thanks in advance




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


Re: Python 2.7 import socket urllib fails to load, module not found

2012-08-17 Thread Emile van Sebille

On 8/17/2012 1:41 PM [email protected] said...

From cmd prompt - I get this:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.

import urllib

Traceback (most recent call last):
   File "", line 1, in 
   File "C:\Python27\lib\urllib.py", line 26, in 
 import socket
   File "C:\Python27\lib\socket.py", line 47, in 
 import _socket
ImportError: DLL load failed: The specified module could not be found

I also get that if I attempt to import socket.

NOTE this does not happen when I'm in the pythonwin IDE.





So, try the following in both environments:

import sys
for ii in sys.path: print ii

You'll likely find diffferences between the two.


In the pythonwin environment, try:

import socket
print socket.__file__


Chances are the __file__'s directory isn't in the command line's sys.path.

Emile




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


Re: Python 2.7 import socket urllib fails to load, module not found

2012-08-17 Thread Emile van Sebille

On 8/17/2012 2:22 PM [email protected] said...

Done - tail end of the python path had a missing bit...gr... thanks so much


Well it's bizarre - now it doesn't.  did an import sys from within interpreter, 
then did import socket.  Worked the first time.  Restarted and it happened 
again.  The sys.path outputs are identical.  The print socket.__file__ reveals 
a file that is in the sys.path...g.




Next, I'd check for rogue versions of _socket.pyd in directories 
occuring in sys.path.


Emile


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


Re: python 6 compilation failure on RHEL

2012-08-20 Thread Emile van Sebille

On 8/20/2012 6:31 AM Ganesh Reddy K said...

But, python compilation is not successfully done and showing a failure
log.  Below is the capture  of the same. Please see failure log shown
in the bottom of this mail.
How to solve the failure modules  mentioned in the log ( bsddb185,
dl ,  imageop, sunaudiodev )

Please guide me to proceed further.



You're done, unless you specifically need support for any of those 
specific modules.


Emile


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


Re: python 6 compilation failure on RHEL

2012-08-20 Thread Emile van Sebille

On 8/20/2012 10:20 AM Walter Hurry said...

On Mon, 20 Aug 2012 19:12:05 +0200, Kwpolska wrote:





>Do you really need to compile python2.6?  RHEL has packages for python,
>and it's better


s/better/sometimes easier


> to use pre-compiled packages rather than compile them yourself.




I concur, but FYI the version of Python with RHEL5 is 2.4. Still, OP
should stick with that unless there is a pressing reason.


Hence, the 2.6 install.

Learn-to-trim-ly y'rs,

Emile





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


Re: python 6 compilation failure on RHEL

2012-08-20 Thread Emile van Sebille

On 8/20/2012 11:37 AM Walter Hurry said...

On Mon, 20 Aug 2012 11:02:25 -0700, Emile van Sebille wrote:


On 8/20/2012 10:20 AM Walter Hurry said...

I concur, but FYI the version of Python with RHEL5 is 2.4. Still, OP
should stick with that unless there is a pressing reason.


Hence, the 2.6 install.


First, sorry for my omission to trim.

Second, the reason for recommending that OP stick to the Red Hat provided
version (unless there is a pressing reason) is the question of the
already-paid-for Red Hat support.


Generally, when you compile from source the binaries will install to 
/usr/local/bin and not be in conflict with RH's install version.




And for that matter, if OP is forced to a later Python 2 version than
2.4, why not 2.7.3?



Package dependencies.  If the OP intends to install a package that 
doesn't support other than 2.6, you install 2.6.


Emile



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


Re: Why doesn't Python remember the initial directory?

2012-08-20 Thread Piet van Oostrum
kj  writes:

> This means that no library code can ever count on, for example,
> being able to reliably find the path to the file that contains the
> definition of __main__.  That's a weakness, IMO.  

On Unix based systems there is no reliable way to find out this
information. So how could Python reliably supply this?
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I display unicode value stored in a string variable using ord()

2012-08-20 Thread Piet van Oostrum
"Blind Anagram"  writes:

> This is an average slowdown by a factor of close to 2.3 on 3.3 when
> compared with 3.2.
>
> I am not posting this to perpetuate this thread but simply to ask
> whether, as you suggest, I should report this as a possible problem with
> the beta?

Being a beta release, is it certain that this release has been compiled
with the same optimization level as 3.2?
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python 6 compilation failure on RHEL

2012-08-20 Thread Emile van Sebille

On 8/20/2012 1:55 PM Walter Hurry said...

On Mon, 20 Aug 2012 12:19:23 -0700, Emile van Sebille wrote:


Package dependencies.  If the OP intends to install a package that
doesn't support other than 2.6, you install 2.6.


It would be a pretty poor third party package which specified Python 2.6
exactly, rather than (say) "Python 2.6 or later, but not Python 3"


It doesn't need to be a poorly supported third party package to require 
a non-current version of python -- just something as simple as an up and 
running application.  Suppose you're migrating an application to new 
hardware.  To make it interesting, assume it's a 10 year old zope 
application.  It's likely that to minimize effort you'll gather 
(assuming you didn't save your sources - you do install from source, 
right?) and install the versions prescribed.


Of course, if you're comfortable upgrading to the latest release then 
then, by all means, do so.  For me, python is used for for dozens of 
rather significant one-offs that I prefer not to upgrade.  Why mess with 
a working app.


Emile



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


Re: python 6 compilation failure on RHEL

2012-08-20 Thread Emile van Sebille

On 8/20/2012 9:34 PM John Nagle said...


 After a thread of clueless replies,


s/clueless/unread

Emile


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


Re: Filter versus comprehension (was Re: something about split()???)

2012-08-24 Thread Emile van Sebille

On 8/24/2012 3:03 PM Terry Reedy said...

On 8/24/2012 5:56 PM, Dennis Lee Bieber wrote:

On Fri, 24 Aug 2012 19:03:51 + (UTC), Walter Hurry
 declaimed the following in
gmane.comp.python.general:



Google Groups sucks. These are computer literate people here. Why don't
they just use a proper newsreader?


Probably because their ISP doesn't offer a free server 


Python lists are available on the free gmane mail-to-news server.


I'm getting high load related denials with the gmane connections a lot 
recently so I'm open to alternatives.


Suggestions or recommendations?


Emile



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


Re: "convert" string to bytes without changing data (encoding)

2012-08-29 Thread Piet van Oostrum
Ross Ridge  writes:

>
> But it is in fact only stored in one particular way, as a series of bytes.
>
No, it can be stored in different ways. Certainly in Python 3.3 and
beyond. And in 3.2 also, depending on wide/narrow build.
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "convert" string to bytes without changing data (encoding)

2012-08-29 Thread Piet van Oostrum
Heiko Wundram  writes:

> Reading from stdin/a file gets you bytes, and
> not a string, because Python cannot automagically guess what format the
> input is in.
>
Huh?

Python 3.3.0rc1 (v3.3.0rc1:8bb5c7bc46ba, Aug 25 2012, 10:09:29) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = input()
abcd123
>>> x
'abcd123'
>>> type(x)


>>> y = sys.stdin.readline()
abcd123
>>> y
'abcd123\n'
>>> type(y)


-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Twisted-Python] Twisted 12.2.0 released

2012-09-02 Thread Laurens Van Houtven
Congratulations to the entire team, particularly Ashwini, and thank you for
an awesome release manager job well done, Ashwini :)


On Sat, Sep 1, 2012 at 3:47 PM, Ashwini Oruganti  wrote:

> On behalf of Twisted Matrix Laboratories, I am honored to announce the
> release of Twisted 12.2.
>
> Highlights for this release include:
>
>   * To be able to work on Python3 support, Python 2.5 is no longer
> supported.
>
>   * twisted.mail.imap4 now serves BODYSTRUCTURE responses which provide
> more information and conform to the IMAP4 RFC more closely.
>
>   * twisted.conch now supports commercial SSH implementations which don't
> comply with the IETF standard.
>
>   * twisted.internet.endpoints now provides several new endpoints,
> including a
> TCP client endpoint that resolves hostnames to IPv6 host addresses.
>
>   * IReactorSocket.adoptStreamConnection, implemented by some reactors,
> allows adding an existing established connection to the reactor.
>
> Starting with the release after 12.2, Twisted will begin requiring
> zope.interface 3.6 (as part of Python 3 support).
>
> This is the last Twisted release supporting Python 2.6 on Windows.
>
> For more information, see the NEWS file here:
>
>  http://twistedmatrix.com/Releases/Twisted/12.2/NEWS.txt
>
>
> Download it now from:
>
>  http://pypi.python.org/packages/source/T/Twisted/Twisted-12.2.0.tar.bz2
>  or
>
>
> http://pypi.python.org/packages/2.6/T/Twisted/Twisted-12.2.0.win32-py2.6.exe
>  or
>
>
> http://pypi.python.org/packages/2.6/T/Twisted/Twisted-12.2.0.win32-py2.6.msi
>  or
>
>
> http://pypi.python.org/packages/2.7/T/Twisted/Twisted-12.2.0.win32-py2.7.exe
>  or
>
>
> http://pypi.python.org/packages/2.7/T/Twisted/Twisted-12.2.0.win32-py2.7.msi
>
>
> Thanks to the supporters of Twisted and to the many contributors for this
> release.
>
>
> --
> Ashwini Oruganti
>
> ___
> Twisted-Python mailing list
> [email protected]
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>


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


Re: Time Bound Input in python

2012-09-03 Thread Emile van Sebille

On 9/3/2012 3:01 AM Vikas Kumar Choudhary said...

Hi

I though of taking time bound input from user in python using "input"
command.
it waits fro infinite time , but I want to limit the time after that
user input should expire with none.
Please help.



Googling yields 
http://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python 
among others.


Emile

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


Re: Error 32 - Broken Pipe . Please Help!!

2012-09-04 Thread Emile van Sebille

On 9/4/2012 10:08 AM Sreenath k said...

Error:


Exception in thread Thread-1:
Traceback (most recent call last):
   File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 self.run()
   File 
"/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/monitor.py", 
line 575, in run
 already_pickled=True)
   File "/usr/lib/python2.7/dist-packages/spyderlib/utils/bsdsocket.py", line 
24, in write_packet
 sock.send(struct.pack("l", len(sent_data)) + sent_data)
error: [Errno 32] Broken pipe

Code :




So, what code is invoking spyderlib stuff that's causing the error?

Emile


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


Re: How to print something only if it exists?

2012-09-06 Thread Emile van Sebille

On 9/6/2012 10:59 AM [email protected] said...

I want to print a series of list elements some of which may not exist,
e.g. I have a line:-

  print day, fld[1], balance, fld[2]

fld[2] doesn't always exist (fld is the result of a split) so the
print fails when it isn't set.

I know I could simply use an if but ultimately there may be more
elements of fld in the print and the print may well become more
complex (most like will be formatted for example).  Thus it would be
good if there was some way to say "print this if it exists".


You may be better off ensuring that fld is an appropriate length.  One
way may be tweaking the split to return the right numbers of elements:

>>> T = "1,2,3"
>>> flds = (T+","*5).split(",")[:5]
>>> flds
['1', '2', '3', '', '']

Emile


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


Re: What’s the differences between these two pieces of code ?

2012-09-09 Thread Piet van Oostrum
iMath  writes:

> What’s the differences between these two  pieces of code ?
> (1)
> for i in range(1, 7):
> print(2 * i, end='   ')
>
>
> (2)
> for i in range(1, 7):
> print(2 * i, end='   ')
> print()
>
>
> when executed both  respectively in Python shell ,I  get  the same effect . 
> Who can tell me why  ?

The first one gives a syntax error (IndentationError: expected an indented 
block)
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SAP MM Cupertino, CA

2012-09-10 Thread Emile van Sebille

On 9/10/2012 7:58 AM Ramchandra Apte said...

On Monday, 10 September 2012 18:51:10 UTC+5:30, Suresh Kumar  wrote:



delete the original message.


Marking this as abusive in Google Groups - this seems like spam.
Please explain what does this have to do with Python.


Please learn to trim -- your reposting of the entire 'abusive' post is 
in and of itself abusive.


Emile



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


Re: Guides for communicating with business accounting systems

2012-09-13 Thread Emile van Sebille

On 9/13/2012 8:02 AM Ben Finney said...

Howdy all,

What material should a team of programmers read before designing a
database model and export format for sending commerce transactions to a
business accounting system?


The only standard I'm aware of is the EDI specification which I first 
encountered in the mid 70's and which is updated routinely.  The full 
spec is the size of a telephone book (do they even still make those?) 
and both trading partners select from it the documents they intend to 
exchange. The back end integration is then left to both parties.  If 
your data structure is sufficient to supply the content expected in the 
EDI specs for the documents you'd expect to exchange you should be OK on 
your database model.


Unfortunately, the spec resembles the RS232 spec in that it leaves the 
details as an implementation issue to be settled between the two trading 
partners.  Another problem is that the spec is privately (through an 
association) controlled and I've often had issues getting my hands on 
the proper documentation when I wasn't working with a trading partner. 
(I didn't want to pay the association fees to join and thereby gain 
access to the documentation directly.)


There's a good overview at http://www.linktionary.com/e/edi.html

HTH,

Emile







I'm especially not wanting ad hoc advice in this thread; this is surely
an old, complex problem with a lot of ground already covered. Primers on
pitfalls to avoid and non-obvious best practices are what I'd like to be
directed toward.

Constraints:

* The shop is already written, and is maintained internally. Ideally we
   would use a widely-tested and third-party-maintained solution, but
   that's a struggle still ahead of us. For now, we must work with our
   private code base.

* None of the developer team are have much experience with the field of
   business accounting, so if possible we need to learn from the past
   design mistakes of others without making them ourselves.

* Our application is operating in Australia, with the sales tax tracking
   requirements that come with that. Australia-specific information is
   particularly desirable.

* The business has switched to a different accounting service recently;
   it may well change again soon. We want to at least consider robustness
   of our shop's transaction tracking design in the face of a possible
   future switch to a different accounting system.

I'm happy to asnwer questions, but I'm not about to hash out the design
in this thread; that's our development team's job.

What I want is pointers to a putative “What every programmer needs to
know about storing commercial transactions for business accounting”
general guide.

Does that information already exist where I can point our team to it?




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


Re: Passing arguments to & executing, a python script on a remote machine from a python script on local machine (using ssh ?)

2012-09-20 Thread Emile van Sebille

On 9/19/2012 12:50 PM ashish said...

Hi c.l.p folks

Here is my situation

1. I have two machines. Lets call them 'local' & 'remote'.
Both run ubuntu & both have python installed

2. I have a python script, local.py, running on 'local' which needs to pass 
arguments ( 3/4 string arguments, containing whitespaces like spaces, etc ) to 
a python script, remote.py running on 'remote' (the remote machine).

I have the following questions:

1. What's the best way to accomplish my task ?


Check out http://rpyc.sourceforge.net/ -- It's reasonably lightweight 
and has been working well for our similar situation.


Emile


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


Re: Passing arguments to & executing, a python script on a remote machine from a python script on local machine (using ssh ?)

2012-09-20 Thread Piet van Oostrum
Ismael Farfán  writes:

> How about something like this:
> os.system ( 'ssh remoteuser@remote python remote.py "arg 1" "arg 2" "arg 3"' )

That won't work. You need an additional level of quoting because ssh is
also a shell so it adds another level of interpretation.

The following works:

os.system('''ssh remoteuser@remote "python remote.py 'arg 1' 'arg 2' 'arg 
3'"''')
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Passing arguments to & executing, a python script on a remote machine from a python script on local machine (using ssh ?)

2012-09-20 Thread Piet van Oostrum
Chris Rebert  writes:

> Use the `subprocess` module instead (with shell=False). You then won't
> need to worry about escaping.
> http://docs.python.org/library/subprocess.html

You will still need to worry about escaping because on the remote end
you invoke ssh which is a shell. The obvious call:

subprocess.call(["ssh", "remoteuser@remote", "python", "remote.py", "arg
1", "arg 2", "arg 3"])

won't work because ssh will break up the "arg n" strings. You need to
use  "'arg n'" or put the whole python command in a string like:

"python TEST/testargs.py 'arg 1' 'arg 2' 'arg 3'"
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: technologies synergistic with Python

2012-09-22 Thread Emile van Sebille

On 9/21/2012 2:59 PM Ethan Furman said...

...if my dream job is one that
consists mostly of Python, and might allow telecommuting?


Hi Ethan,

I have an open position in my two man office I've tried to fill a couple 
times without success that is predominately python and would allow for 
telecommuting.  I'm looking for a third member of the team that will 
focus on back end development integrating various systems through to an 
open source python platform.


Where are you located?  I'm on the SF Peninsula.

Emile




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


Re: One of my joomla webpages has been hacked. Please help.

2012-09-26 Thread Emile van Sebille

On 9/26/2012 6:06 PM Wayne Werner said...

On Sun, 23 Sep 2012, Dwight Hutto wrote:


We're the borg.


Oh, so you *are* a robot. That does explain your posts ;)


Damn.  Now I'll forever more hear Stephen Hawkin's voice as I read the 
repeated contexts.  Maybe that'll help.


EMile



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


Re: test

2012-09-27 Thread Emile van Sebille

On 9/27/2012 2:58 PM Rikishi42 said...


Inboxes?

What is this, usenet or email ?



Yes.  Both.

Emile



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


Re: Should one always add super().__init__() to the __init__?

2012-09-29 Thread Piet van Oostrum
Chris Angelico  writes:

> On Sun, Sep 30, 2012 at 3:17 AM, Steven D'Aprano
>  wrote:
>> No. Only add code that works and that you need. Arbitrarily adding calls
>> to the superclasses "just in case" may not work:
>>
>> py> class Spam(object):
>> ... def __init__(self, x):
>> ... self.x = x
>> ... super(Spam, self).__init__(x)
>> ...
>> py> x = Spam(1)
>> Traceback (most recent call last):
>>   File "", line 1, in 
>>   File "", line 4, in __init__
>> TypeError: object.__init__() takes no parameters
>
> That's because you're subclassing something that doesn't take
> parameters and giving it parameters. Of course that won't work. The
> normal and logical thing to do is to pass on only the parameters that
> you know the parent class expects... but that implies knowing the
> parent, so it's kinda moot.

It is not necesarily calling the parent class. It calls the initializer
of the next class in the MRO order and what class that is depends on the
actual multiple inheritance structure it is used in, which can depend
on subclasses that you don't know yet. This makes it even worse.
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to insert random error in a programming

2012-10-15 Thread Emile van Sebille

Debashish Saha wrote:

how to insert random error in a programming?


Make the changes late in the day then leave for the weekend?

Emile

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


Re: numpy - 2D matrix/array - initialization like in Matlab...

2012-10-15 Thread Emile van Sebille

someone wrote:

How to initialize my array directly using variables ?

It could also be that I wanted:

test11 = 1
test12 = 1.5
test13 = 2
test21 = 0
test22 = 5

Dx = numpy.matrix('test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5')

Etc... for many variables...

Appreciate ANY help, thank you very much!


You could use string interpolation:

Dx = numpy.matrix('%s %s %s; %s %s -0.5; 0 -0.5 1.5'
  % (test11 test12 test13 test21 test22))


Emile

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


use of exec()

2012-10-18 Thread lars van gemerden
I am trying to implement a way to let users give a limited possibility to 
define functions in text, that wille be stored and executed at a later time. I 
use exec() to transform the text to a function. The code is as follows:

class code(str):
def __call__(self, *args):
try:
return self._func_(*args)
except AttributeError:
self._func_ = self._creat_func_()
return self._func_(*args)
def __getstate__(self):
return {}

class _lambdacode(code):
def _creat_func_(self):
return eval("lambda %s: %s" % (", ".join(type(self).args), self),{},{})

class _functioncode(code):
def _creat_func_(self):
exec("def function(%s):\n\t%s" % (", ".join(type(self).args), 
  "\n\t".join(self.split('\n'
return function

def lambdatype(*argnames):
return type('lambdacode', (_lambdacode,),{'args': argnames}) 

def functiontype(*argnames):
return type('functioncode', (_functioncode,),{'args': argnames})

if __name__ == '__main__':
f = lambdatype('a', 'b')('a ** b')
print f
print f(3, 4)
print f(4, 3)

g = functiontype('a', 'b')('a, b = a/b, a*b\nreturn a ** b')
print g
print g(3.0, 4.0) 
print g(4.0, 3.0)

This code works. But if I replace _functioncode with:

class _functioncode(code):
def _creat_func_(self):
exec("def function(%s):\n\t%s" % (", ".join(type(self).args), 
  "\n\t".join(self.split('\n'))),{})
return function

or

class _functioncode(code):
def _creat_func_(self):
exec("def function(%s):\n\t%s" % (", ".join(type(self).args), 
  "\n\t".join(self.split('\n'))),{},{})
return function

to restrict access to global variables, similar to the lambda version, i get 
the error:

Traceback (most recent call last):
  File 
"D:\Documents\Code\Eclipse\workspace\FlowWare_1\toolshed\tests\mini_test.py", 
line 41, in 
print g(3.0, 4.0)
  File 
"D:\Documents\Code\Eclipse\workspace\FlowWare_1\toolshed\tests\mini_test.py", 
line 13, in __call__
self._func_ = self._creat_func_()
  File 
"D:\Documents\Code\Eclipse\workspace\FlowWare_1\toolshed\tests\mini_test.py", 
line 25, in _creat_func_
return function
NameError: name 'function' is not defined

which seems an odd error, but i think some global variable is necessary for 
this to work (if i put in globals() instead of {}, it works).

My question is which variable or if that is not the problem, what is and how 
can i restrict access the user code has.

Cheers, Lars
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: use of exec()

2012-10-18 Thread lars van gemerden
On Thursday, October 18, 2012 1:49:35 PM UTC+2, Chris Angelico wrote:
> On Thu, Oct 18, 2012 at 10:41 PM, lars van gemerden
> 
>  wrote:
> 
> > NameError: name 'function' is not defined
> 
> >
> 
> > which seems an odd error, but i think some global variable is necessary for 
> > this to work (if i put in globals() instead of {}, it works).
> 
> 
> 
> The def statement simply adds a name to the current namespace. This
> 
> should work (untested):
> 
> 
> 
> class _functioncode(code):
> 
> def _creat_func_(self):
> 
> ns={}
> 
> exec("def function(%s):\n\t%s" % (", ".join(type(self).args),
> 
>   
> "\n\t".join(self.split('\n'))),ns,ns)
> 
> return ns.function
> 
> 
> 
> But it's going to be eternally plagued by security issues. You may
> 
> want, instead, to look at literal_eval from the ast module; but that
> 
> won't work if you need anything other than, as the name suggests,
> 
> literals.
> 
> 
> 
> ChrisA

Thanks, Chris,

That works like a charm (after replacig "return ns.function" with "return 
ns['function']" ;-) ).

About the security, i noticed you can still import and use modules within the 
exec'ed code. Is there a way to prevent this or otherwise make this approach 
more secure.

I should say that the users that will be able to make custom functions, are not 
end-users, but authenticated designers, however i would like to close a 
backdoor to the whole framework.

Cheers, Lars 



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


Re: use of exec()

2012-10-18 Thread lars van gemerden
On Thursday, October 18, 2012 4:29:45 PM UTC+2, Chris Angelico wrote:
> On Fri, Oct 19, 2012 at 1:07 AM, lars van gemerden  
> wrote:
> 
> > Thanks, Chris,
> 
> >
> 
> > That works like a charm (after replacig "return ns.function" with "return 
> > ns['function']" ;-) ).
> 
> 
> 
> Err, yes, I forget sometimes that Python doesn't do that. JavaScript
> 
> and Pike both let you (though Pike uses -> instead of . for that
> 
> operator). Yes, Python has real methods on dictionary objects :)
> 
> 
> 
> > About the security, i noticed you can still import and use modules within 
> > the exec'ed code. Is there a way to prevent this or otherwise make this 
> > approach more secure.
> 
> 
> 
> Basically no, there's no real way to make it secure. Without
> 
> eliminating exec/eval, destroying insecurity is the hopeless work of a
> 
> wasted life, as the oracle said to Alice.
> 
> 
> 
> > I should say that the users that will be able to make custom functions, are 
> > not end-users, but authenticated designers, however i would like to close a 
> > backdoor to the whole framework.
> 
> 
> 
> You have to decide one thing: Will you permit them to execute
> 
> untrusted code on your system? If so, go ahead (and just warn them
> 
> that things like import shouldn't be done, as they can cause other
> 
> messes). I run a server that I build with the help of another guy (I
> 
> do the code, he does the bulk of the content - descriptions and
> 
> stuff), and I'm happy to trust him to not be malicious, so the purpose
> 
> of "embedded code in loci" is to make it easier to write tiny bits of
> 
> code, without any security requirement. But if you need security,
> 
> don't use eval. AT ALL.
> 
> 
> 
> There may be a brand new service coming along, though. The ast module
> 
> I think is getting a new evaluator that allows a little more
> 
> functionality than literal_eval, while still not permitting most
> 
> things. But you then have the question of performance, since you
> 
> effectively interpret the code at a high level.
> 
> 
> 
> ChrisA

I get your point, since in this case having the custom code option makes the 
system a whole lot less complex and flexible, i will leave the option in. The 
future customer will be informed that they should handle the security around 
the designers as if they were programmers. Aditionally i will probably add some 
screening for unwanted keywords (like 'import') and securely log any 
new/changed custom code including the designer account (must do that for other 
actions anyway).

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


Re: Python does not take up available physical memory

2012-10-19 Thread Emile van Sebille

On 10/19/2012 10:08 AM, Pradipto Banerjee wrote:

Hi,

I am trying to read a file into memory. The size of the file is around 1
GB. I have a 3GB memory PC and the Windows Task Manager shows  2.3 GB
available physical memory when I was trying to read the file. I tried to
read the file as follows:


fdata = open(filename, ‘r’).read()


I got a “MemoryError”. I was watching the Windows Task Manager while I
run the python command, and it appears that python **perhaps** never
even attempted to use more memory but gave me this error.

Is there any reason why python can’t read a 1GB file in memory even when
a 2.3 GB physical memory is available?


The real issue is likely that there is more than one copy of the file in 
memory somewhere.  I had a similar issue years back that I resolved by 
using numeric (now numpy?) as it had a more efficient method of 
importing content from disk.


Also realize that windows may not allow the full memory to user space. 
I'm not sure what exactly the restrictions are, but a 4Gb windows box 
doesn't always get you 4Gb of memory.


Emile


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


Re: use of exec()

2012-10-19 Thread lars van gemerden
On Thursday, October 18, 2012 5:16:50 PM UTC+2, Chris Angelico wrote:
> On Fri, Oct 19, 2012 at 2:00 AM, lars van gemerden  
> wrote:
> 
> > I get your point, since in this case having the custom code option makes 
> > the system a whole lot less complex and flexible, i will leave the option 
> > in. The future customer will be informed that they should handle the 
> > security around the designers as if they were programmers. Aditionally i 
> > will probably add some screening for unwanted keywords (like 'import') and 
> > securely log any new/changed custom code including the designer account 
> > (must do that for other actions anyway).
> 
> 
> 
> That sounds like a reasonable implementation of Layer Eight security.
> 
> As long as everyone understands that this code can do ANYTHING, you'll
> 
> be fine.
> 
> 
> 
> You may want to add some other programmatic checks, though; for
> 
> instance, a watchdog timer in case the code gets stuck in an infinite
> 
> loop, or a memory usage limit, or somesuch. Since you're no longer
> 
> worrying about security, this sort of thing will be fairly easy, and
> 
> will be just to help catch common errors.
> 
> 
> 
> ChrisA

Do you have any ideas about to what extend the "lambda" version of the code 
(custom code is only the 'body' of the lambda function) has the same issues?

Cheers, Lars 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: use of exec()

2012-10-20 Thread lars van gemerden
On Saturday, October 20, 2012 4:00:55 AM UTC+2, Chris Angelico wrote:
> On Sat, Oct 20, 2012 at 10:43 AM, lars van gemerden
> 
>  wrote:
> 
> > Do you have any ideas about to what extend the "lambda" version of the code 
> > (custom code is only the 'body' of the lambda function) has the same issues?
> 
> 
> 
> The lambda version definitely has the same issues. You can do pretty
> 
> much anything with a single expression.
> 
> 
> 
> ChrisA

Thanks a lot Chris, I will return later to the watchdog/memory limit ideas (no 
idea how to do that yet).

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


Re: get each pair from a string.

2012-10-21 Thread Emile van Sebille

On 10/21/2012 11:33 AM, Vincent Davis wrote:

I am looking for a good way to get every pair from a string. For example,
input:
x = 'apple'
output
'ap'
'pp'
'pl'
'le'

I am not seeing a obvious way to do this without multiple for loops, but
maybe there is not :-)
In the end I am going to what to get triples, quads... also.



How far have you gotten?  Show us the loops you're trying now and any 
errors you're getting.


Emile



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


Re: get each pair from a string.

2012-10-21 Thread Emile van Sebille

On 10/21/2012 11:51 AM, Ian Kelly wrote:

On Sun, Oct 21, 2012 at 12:33 PM, Vincent Davis
 wrote:

I am looking for a good way to get every pair from a string. For example,
input:
x = 'apple'
output
'ap'
'pp'
'pl'
'le'

I am not seeing a obvious way to do this without multiple for loops, but
maybe there is not :-)


Use the "pairwaise" recipe from the itertools docs:

def pairwise(iterable):
 "s -> (s0,s1), (s1,s2), (s2, s3), ..."
 a, b = tee(iterable)
 next(b, None)
 return izip(a, b)


In the end I am going to what to get triples, quads... also.


Generalizing:

def nwise(iterable, n=2):
 iters = tee(iterable, n)
 for i, it in enumerate(iters):
 for _ in range(i):
 next(it, None)
 return izip(*iters)





Hmmm.  And it seemed so straightforward to me as:

>>> groupsize=3
>>> a = "applesauce"
>>> for i in range(len(a)-groupsize+1): a[i:i+groupsize]
...
'app'
'ppl'
'ple'
'les'
'esa'
'sau'
'auc'
'uce'

Other than adding depth to my knowledge of the ever growing standard 
library, is there a reason to prefer pairwise over my simple loop?


Emile

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


Re: get each pair from a string.

2012-10-21 Thread Emile van Sebille

On 10/21/2012 12:06 PM, Ian Kelly wrote:

On Sun, Oct 21, 2012 at 12:58 PM, Vincent Davis
 wrote:

x = 'apple'
for f in range(len(x)-1):
 print(x[f:f+2])

@Ian,
Thanks for that I was just looking in to that. I wonder which is faster I
have a large set of strings to process. I'll try some timings if I get a
chance later today.


The solution you came up with is probably faster, but less general --
it will only work on sliceable sequences like strings, not arbitrary
iterables.



So the simple loop is the right answer for sliceable sequences like 
strings, but not if your code needs to deal with arbitrary iterables 
such as those that the standard library authors are expected to handle.


So, as OP's a self confessed newbie asking about slicing, why provide an 
example requiring knowledge of tee, enumerate, next and izip?


def nwise(iterable, n=2):
iters = tee(iterable, n)
for i, it in enumerate(iters):
for _ in range(i):
next(it, None)
return izip(*iters)

It's good that the standard library provides these tools as a 
convenience, but when all you need is a derringer, why reach for a howitzer?


Emile


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


Re: get each pair from a string.

2012-10-22 Thread Emile van Sebille

On 10/21/2012 9:19 PM, Ian Foote wrote:

On 22/10/12 09:03, Emile van Sebille wrote:

So, as OP's a self confessed newbie asking about slicing, why provide an
example requiring knowledge of tee, enumerate, next and izip?



Because not only the newbie will read the thread? I for one was
interested to see all the different possible approaches, and their
upsides and downsides.


Fair -- I get concerned that newcomers are faced with a much higher cost 
of entry when answers to their apparently simple problems require 
knowledge of specific functions in specific library modules to solve.


This presents a very high bar as my quick local test (help(); modules) 
shows 398 modules!


In this case the only requirement should have been a quick pass through 
the tutorial which should be enough to solve most problems.


Emile




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


Re: Fast forward-backward (write-read)

2012-10-24 Thread Emile van Sebille

On 10/23/2012 4:35 PM, emile wrote:


So, let's see, at that point in time (building backward) you've got
probably somewhere close to 400-500Gb in memory.

My guess -- probably not so fast.  Thrashing is sure to be a factor on
all but machines I'll never have a chance to work on.



I went looking for a machine capable of this and got about halfway there 
with http://www.tech-news.com/publib/pl2818.html which allows up to 
248Gb memory -- near as I can tell the price for the maxed out system is 
$2,546,200. Plus $3k/mo maintenance. Memory's still not quite enough, 
but I'll bet it's fast.  And a lot more reasonable at $1000 per Gb of 
memory particularly when contrasted to the $1000 I paid for a single Mb 
of memory back in 1984 or thereabouts.


Emile


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


Re: simple string format question

2012-10-24 Thread Piet van Oostrum
Adrien  writes:

> print "{:.3g}".format(2.356)  # this rounds up

But:

>>> print "{:.3g}".format(12.356) 
12.4
>>> print "{:.3g}".format(123.356) 
123

-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a simpler way to modify all arguments in a function before using the arguments?

2012-11-15 Thread Emile van Sebille

[email protected] wrote:


Using a decorator works when named arguments are not used. When named arguments 
are used, unexpected keyword error is reported. Is there a simple fix?


Extend def wrapper(*args) to handle *kwargs as well

Emile


Code:
-

from functools import wraps

def fix_args(fn):
@wraps(fn)
def wrapper(*args):
args = (arg.replace('_', '') for arg in args)
return fn(*args)
return wrapper

@fix_args

def foo(a1="", a2="", b1="", b2=""):
 print(a1)
 print(a2) 
 print(b1)
 print(b2) 
 
foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_x')

foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_x')

Results:

a1a1x
a2a2x
b1b1x
b2b2x
Traceback (most recent call last):
  File "C:\WORK\masterDB_Update\argtest.py", line 19, in 
foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_x')
TypeError: wrapper() got an unexpected keyword argument 'a1'


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


Re: Compare list entry from csv files

2012-11-26 Thread Emile van Sebille

Anatoli Hristov wrote:

I understand, but in my case I have for sure the field "Name" in the
second file that contains at least the first or the last name on it...
So probably it should be possible:)
The Name "Billgatesmicrosoft" contains the word "Gates" so logically I
might find a solution for it.


Take a look at difflib -- I've used it before for this type of 'matching'

Emile

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


deepcopy questions

2012-11-27 Thread lars van gemerden
Hi,

I get a very strange result when using deepcopy. The following code:

def __deepcopy__(self, memo):
independent = self.independent()
if independent is self:
out = type(self)()
out.__dict__ = copy.deepcopy(self.__dict__, memo)
print self.__dict__
print out.__dict__ #strange result
return out
else:
return copy.deepcopy(independent, memo).find(self.id).take()

prints different results for self.__dict__ and out.__dict__:

{'_active_': False, 'init': {}, '_id_': 0, '_items_': [], '_name_': 'main'} 
{'_active_': False, 'init': {}, '_id_': 0}

Two items are missing in the copy. Maybe i am missing something obvious, but i 
cannot figure out how this could happen.

Can anyone tell me how this is possible?

Cheers, Lars 

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


Re: deepcopy questions

2012-11-28 Thread lars van gemerden
On Wednesday, November 28, 2012 12:59:38 AM UTC+1, lars van gemerden wrote:
> Hi,
> 
> 
> 
> I get a very strange result when using deepcopy. The following code:
> 
> 
> 
> def __deepcopy__(self, memo):
> 
> independent = self.independent()
> 
> if independent is self:
> 
> out = type(self)()
> 
> out.__dict__ = copy.deepcopy(self.__dict__, memo)
> 
> print self.__dict__
> 
> print out.__dict__ #strange result
> 
> return out
> 
> else:
> 
> return copy.deepcopy(independent, memo).find(self.id).take()
> 
> 
> 
> prints different results for self.__dict__ and out.__dict__:
> 
> 
> 
> {'_active_': False, 'init': {}, '_id_': 0, '_items_': 
> [], '_name_': 'main'} 
> 
> {'_active_': False, 'init': {}, '_id_': 0}
> 
> 
> 
> Two items are missing in the copy. Maybe i am missing something obvious, but 
> i cannot figure out how this could happen.
> 
> 
> 
> Can anyone tell me how this is possible?
> 
> 
> 
> Cheers, Lars

I have just tried to make a simple runnable testcase but no luck. in my code 
it's part of a rather complex data structure. 

As I understood the documentation, the memo parameter is to hold a dictionary 
of data that have already been copied (e.g. to deal with circular references), 
and is normally only explicitly used when implementing __deepcopy__, just 
passing memo to calls to deepcopy within the body of __deepcopy__.

If memo contains items, this should, to my understanding, not remove them from 
the output of deepcopy, they will just not be copied again, but instead be 
taken from memo and put in the output (otherwise 2 references to the same 
object would after deepcopying result in 2 distinct copies of that object).

Anyway, since i cannot reproduce the error in a simple testcase and i have no 
adea what is going on, I'll implement what i need differently.

Any ideas are still more then welcome,

Thanks for the feedback,

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


weird isinstance/issubclass behavior?

2012-11-29 Thread lars van gemerden
Hi,

I have encountered some strange behavior of isinstance(/issubclass): depending 
on the import path used for classes i get different output, while the classes i 
compare are in the same file. 

Basically if i import a class as:

from mod1.mod2 import A

or:

from mod0.mod1.mod2 import A

which both result in importing the same class, a call to isinstance(inst, A) in 
another module can have a different output. In this module

print type(inst), A, isinstance(inst, A), issubclass(type(inst), A)

gives:

  False False

resp.

  True True

which seems somewhat logical, but in my case a strange gotcha. My question is:
Is this intended, inevitable or a bug?

Cheers, Lars

PS: this is somewhat simpler than the actual case i've encountered, and i 
haven't tested this exact case, but for now i hope this is enough to get some 
of your insight.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: weird isinstance/issubclass behavior?

2012-11-29 Thread lars van gemerden
On Thursday, November 29, 2012 3:59:37 PM UTC+1, lars van gemerden wrote:
> Hi,
> 
> 
> 
> I have encountered some strange behavior of isinstance(/issubclass): 
> depending on the import path used for classes i get different output, while 
> the classes i compare are in the same file. 
> 
> 
> 
> Basically if i import a class as:
> 
> 
> 
> from mod1.mod2 import A
> 
> 
> 
> or:
> 
> 
> 
> from mod0.mod1.mod2 import A
> 
> 
> 
> which both result in importing the same class, a call to isinstance(inst, A) 
> in another module can have a different output. In this module
> 
> 
> 
> print type(inst), A, isinstance(inst, A), issubclass(type(inst), A)
> 
> 
> 
> gives:
> 
> 
> 
>   False False
> 
> 
> 
> resp.
> 
> 
> 
>   True True
> 
> 
> 
> which seems somewhat logical, but in my case a strange gotcha. My question is:
> 
> Is this intended, inevitable or a bug?
> 
> 
> 
> Cheers, Lars
> 
> 
> 
> PS: this is somewhat simpler than the actual case i've encountered, and i 
> haven't tested this exact case, but for now i hope this is enough to get some 
> of your insight.

I know for sure that the imports both import the same file, though if i 
understand you correctly, it creates 2 different module objects? Are module 
object only created on an import statement?  

The 2 parameters of isinstance() follow a very different import path. 

Anyway, to not spend too much time us this, i'll just file it under 'dangerous' 
and use the second import statement. It does solve my problem. 

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


Re: ANN: eGenix mx Base Distribution 3.2.5 (mxDateTime, mxTextTools, etc.)

2012-12-01 Thread Piet van Oostrum
"M.-A. Lemburg"  writes:

> 
>
> ANNOUNCING
>
>eGenix.com mx Base Distribution
>
>   Version 3.2.5 for Python 2.4 - 2.7
>
>Open Source Python extensions providing
>  important and useful services
> for Python programmers.
>
> This announcement is also available on our web-site for online reading:
> http://www.egenix.com/company/news/eGenix-mx-Base-Distribution-3.2.5-GA.html

It says 3.2.4.
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


modify image and save with exif data

2012-12-16 Thread jwe . van . dijk
I want to resize an image but retain the exif data
I now have:
import Image

img = Image.open('photo.jpg')
img.thumbnail((800, 800), Image.ANTIALIAS)
img.save('photo800.jpg', 'JPEG')

The saved image photo800.jpg has no exif info anymore.
I would so much like to have it retained in particular the exposure and gps 
data.

I use Python 2.7 with PIL 1.17 on Ubuntu 12.04
For exif reading/writing I use pyexiv2 0.3.2 but apparently you cannot add tags 
to an image that has none; only modifying existing ones seems to work.

Thanks for any helpful suggestions,
Janwillem
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: modify image and save with exif data

2012-12-17 Thread jwe . van . dijk
On Sunday, 16 December 2012 20:43:12 UTC+1, [email protected]  wrote:
> I want to resize an image but retain the exif data
> 
> I now have:
> 
> import Image
> 
> 
> 
> img = Image.open('photo.jpg')
> 
> img.thumbnail((800, 800), Image.ANTIALIAS)
> 
> img.save('photo800.jpg', 'JPEG')
> 
> 
> 
> The saved image photo800.jpg has no exif info anymore.
> 
> I would so much like to have it retained in particular the exposure and gps 
> data.
> 
> 
> 
> I use Python 2.7 with PIL 1.17 on Ubuntu 12.04
> 
> For exif reading/writing I use pyexiv2 0.3.2 but apparently you cannot add 
> tags to an image that has none; only modifying existing ones seems to work.
> 
> 
> 
> Thanks for any helpful suggestions,
> 
> Janwillem

Thanks Vincent,
I tried that but without try-except. I should have done and with also a try 
except in your except it works. It appears that the failure is in a few Nikon 
tags that apparently can be read but not set.

Setting Exif.Nikon3.0x002d failed, 
Setting Exif.Nikon3.0x009d failed, 
Setting Exif.Nikon3.ExposureTuning failed, 
Setting Exif.Nikon3.Preview failed, 

Not important so I am happy with your tip.
Janwillem
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Twisted-Python] Twisted 12.3.0 released

2012-12-26 Thread Laurens Van Houtven
Yay! Thanks for an awesome Christmas present Thomas :)


On Wed, Dec 26, 2012 at 2:21 PM, Thomas Hervé  wrote:

> On behalf of Twisted Matrix Laboratories, I am pleased to announce, in
> extremis, the release of Twisted 12.3.
>
> 161 tickets are closed by this release, with the following highlights:
>
>  * Support for local parallel testing in trial
>
>  * A new "react" function to easily run a single asynchronous function
> in a script with the reactor.
>
>  * Partial support for Python 3.3.
>
> Note that only Python 2.7 is supported on Windows now. We also require
> zope.interface 3.6.0 or newer.
>
>
> For more information, see the NEWS file here:
>
>  http://twistedmatrix.com/Releases/Twisted/12.3/NEWS.txt
>
>
> Download it now from:
>
>  http://pypi.python.org/packages/source/T/Twisted/Twisted-12.3.0.tar.bz2or
>
>
> http://pypi.python.org/packages/2.7/T/Twisted/Twisted-12.3.0.win32-py2.7.msi
>
>
> Thanks to the supporters of Twisted via the Software Freedom Conservancy
> and to the many contributors for this release.
>
> --
> Thomas
>
> ___
> Twisted-Python mailing list
> [email protected]
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>



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


Re: ANN: PyDTLS

2013-01-08 Thread Guido van Rossum
This sounds exciting. Are you considering a Python 3 port? It might make a
nice demo of PEP 3156.

On Monday, January 7, 2013, rbit wrote:

> I would like to announce Datagram Transport Layer Security for
> Python. From the top of the project README:
>
> PyDTLS brings Datagram Transport Layer Security (DTLS - RFC 6347:
> http://tools.ietf.org/html/rfc6347) to the Python environment. In a
> nutshell, DTLS brings security (encryption, server authentication,
> user authentication, and message authentication) to UDP datagram
> payloads in a manner equivalent to what SSL/TLS does for TCP stream
> content.
>
> DTLS is now very easy to use in Python. If you're familiar with the
> ssl module in Python's standard library, you already know how. All it
> takes is passing a datagram/UDP socket to the *wrap_socket* function
> instead of a stream/TCP socket. Here's how one sets up the client side
> of a connection:
>
> import ssl
> from socket import socket, AF_INET, SOCK_DGRAM
> from dtls import do_patch
> do_patch()
> sock = ssl.wrap_socket(socket(AF_INET, SOCK_DGRAM))
> sock.connect(('foo.bar.com', 1234))
> sock.send('Hi there')
>
> The project is hosted at https://github.com/rbit/pydtls, and licensed
> under
> the Apache license 2.0. PyPI has packages. I can be reached
> at code AT liquibits DOT com for questions, feedback, etc.
>
> http://pypi.python.org/pypi/Dtls/0.1.0";>Dtls 0.1.0 -
>   Datagram Transport Layer Security for Python.  (07-Jan-13)
> --
> http://mail.python.org/mailman/listinfo/python-announce-list
>
> Support the Python Software Foundation:
> http://www.python.org/psf/donations/
>


-- 
--Guido van Rossum (python.org/~guido)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: PyDTLS

2013-01-08 Thread Guido van Rossum
On Tue, Jan 8, 2013 at 8:39 PM, rbit  wrote:
> Thank you. I will gladly port to Python 3 if there is interest from
> the community.

Python 3 is where it's at! :-)

> Regarding PEP 3156: asynchronous use of unreliable network protocols
> makes for an interesting use case. In particular, it forces
> applications to deal with packet loss under some circumstances.

But don't you have to deal with that when doing synchronous I/O as
well? It's a datagram protocol after all.

> One
> such situation occurs during DTLS's handshaking phase: if no response
> is received from the peer after some period of time, we must assume
> that our most recent datagram has been lost, and so we need to
> retransmit.

Is this something the transport can handle, or does the protocol (and
hence the application) need to be involved here?

> The event loop interface as outlined in the PEP makes this
> a bit difficult (as did the asyncore module). One possible way to make
> things easier would be by adding two parameters to add_reader: a
> callable to retrieve the current timeout, and a callable that is
> invoked if that timeout expires before the descriptor becomes
> readable. Each loop iteration would then collect all given timeouts,
> and pass the minimum of that set to whatever polling facility it
> invokes. If that timeout expires, the corresponding timeout handler
> would be invoked prior to the next loop iteration.

Hm, this would add a fair amount of complexity to the event loop. It's
true that I don't have the complete story for timeouts yet, but I am
hopeful that things like this can be implemented by using call_later()
with some callback that does the retransmit (and resets some internal
state), and cancelling that callback whenever a packet is received
(i.e. in the protocol's datagram_received() method).

> The PEP also considers only stream transports when referring to
> "transport." Datagram transports do not, for example, have the
> property that calling t.write(b'abc'); t.write(b'def') is equivalent
> to calling t.write(b'abcdef').

Yeah, obviously this invariant only applies to stream protocols. The
PEP currently doesn't really specify datagram support (it's just in
the Open Issues section).

> I'm not sure what sort of impact this
> omission of datagram transports has for an implementation. Though I
> would certainly like to see datagram transports be treated as
> first-class citizens, despite not being nearly used as often as stream
> transports. I would hope that an implementer of, say, RTP over UDP,
> can tie into the same event loop as someone implementing a
> single-process, single-threaded Web server.

Yeah, at the level of the eventloop proper (the APIs that deal with
callbacks, not futures, transports and protocols) datagrams won't be a
problem. There will have to be separate specifications for the
transport and protocol  interfaces used with datagrams.

> Implementing DTLS as a tulip transport sounds interesting. Is the
> tulip package available somewhere so that I can try it out?

Absolutely -- it is very much in flux, but you can check out the
latest source from http://code.google.com/p/tulip/source/checkout
using Mercurial.

--Guido

> Ray
>
> On Tue, Jan 8, 2013 at 6:53 AM, Guido van Rossum  wrote:
>> This sounds exciting. Are you considering a Python 3 port? It might make a
>> nice demo of PEP 3156.
>>
>>
>> On Monday, January 7, 2013, rbit wrote:
>>>
>>> I would like to announce Datagram Transport Layer Security for
>>> Python. From the top of the project README:
>>>
>>> PyDTLS brings Datagram Transport Layer Security (DTLS - RFC 6347:
>>> http://tools.ietf.org/html/rfc6347) to the Python environment. In a
>>> nutshell, DTLS brings security (encryption, server authentication,
>>> user authentication, and message authentication) to UDP datagram
>>> payloads in a manner equivalent to what SSL/TLS does for TCP stream
>>> content.
>>>
>>> DTLS is now very easy to use in Python. If you're familiar with the
>>> ssl module in Python's standard library, you already know how. All it
>>> takes is passing a datagram/UDP socket to the *wrap_socket* function
>>> instead of a stream/TCP socket. Here's how one sets up the client side
>>> of a connection:
>>>
>>> import ssl
>>> from socket import socket, AF_INET, SOCK_DGRAM
>>> from dtls import do_patch
>>> do_patch()
>>> sock = ssl.wrap_socket(socket(AF_INET, SOCK_DGRAM))
>>> sock.connect(('foo.bar.com', 1234))
>>> sock.send('Hi there')
>>>
>>> The 

Re: ANN: PyDTLS

2013-01-09 Thread Guido van Rossum
On Tue, Jan 8, 2013 at 11:38 PM, rbit  wrote:
> On Tue, Jan 8, 2013 at 9:09 PM, Guido van Rossum  wrote:
>> But don't you have to deal with that when doing synchronous I/O as
>> well? It's a datagram protocol after all.
>
> No: when dealing with blocking sockets, the OpenSSL library activates its
> own retransmission timers, and the application never becomes aware of
> whether timeouts occurred. Since OpenSSL can't do this in the the case of
> non-blocking sockets, it becomes the application's problem to call back into
> OpenSSL at some point in the future (the very same time for which OpenSSL
> would have set its own timeout had the socket been blocking). OpenSSL
> exports functions DTLSv1_get_timeout and DTLSv1_handle_timeout to
> applications to address this case. The timeouts start at one second, and
> double for each encountered timeout, until the timeout ceiling of one
> minutes is reached. I'm using the term "application" here for any code that
> uses the OpenSSL library, but is not part of it.

Got it.

>>> One
>>> such situation occurs during DTLS's handshaking phase: if no response
>>> is received from the peer after some period of time, we must assume
>>> that our most recent datagram has been lost, and so we need to
>>> retransmit.
>>
>> Is this something the transport can handle, or does the protocol (and
>> hence the application) need to be involved here?
>
> Given my current understanding of the PEP, I think this can be handled in
> the transport. Maybe there's some pitfall here that I can't quite see yet -
> even more reason for me to try to implement it.

Yes -- I won't considered the PEP ready for acceptance until several
people have successfully implemented new protocols using it and agree
that they can do everything they need. (I want to get started with a
decent HTTP client and server myself.)

>>> The event loop interface as outlined in the PEP makes this
>>> a bit difficult (as did the asyncore module). One possible way to make
>>> things easier would be by adding two parameters to add_reader: a
>>> callable to retrieve the current timeout, and a callable that is
>>> invoked if that timeout expires before the descriptor becomes
>>> readable. Each loop iteration would then collect all given timeouts,
>>> and pass the minimum of that set to whatever polling facility it
>>> invokes. If that timeout expires, the corresponding timeout handler
>>> would be invoked prior to the next loop iteration.
>>
>> Hm, this would add a fair amount of complexity to the event loop. It's
>> true that I don't have the complete story for timeouts yet, but I am
>> hopeful that things like this can be implemented by using call_later()
>> with some callback that does the retransmit (and resets some internal
>> state), and cancelling that callback whenever a packet is received
>> (i.e. in the protocol's datagram_received() method).
>
> Yes, ok, I can see how that could work, too. I thought that it might make
> sense to centralize handling timeouts in the event loop in order to prevent
> proliferation in the transports (since there are multiple event loop
> implementations, perhaps a mix-in would be good?).

A mix-in for what? Each event loop presumably already has its own
timer implementation; call_later() and call_repeatedly() are supported
in one way or another by all other event loops I'm aware of.

We'll have to have more experience with writing transports and
protocols before we'll know what is really missing.

> I think one will want to
> contain handshake (vs. application data) timeout handling at least to the
> transport, though, and not let it spill over into various protocols. I'm not
> sure yet where the right place is for cancelling a timeout callback.

This seems pretty unique to your TLS-over-UDP use case. I am quite
sure that you can write a transport that suits your purpose with just
the socket, callback and timer primitives in the PEP.

>>> Implementing DTLS as a tulip transport sounds interesting. Is the
>>> tulip package available somewhere so that I can try it out?
>>
>> Absolutely -- it is very much in flux, but you can check out the
>> latest source from http://code.google.com/p/tulip/source/checkout
>> using Mercurial.
>
> All right, thanks, I'll check it out.

Looking forward to your feedback!

-- 
--Guido van Rossum (python.org/~guido)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to call ltm function using rpy package in python

2013-01-11 Thread Piet van Oostrum
Mohit Khanna  writes:

> I am trying the following code--
>
> from rpy import *
> r.library("ltm")
>
> dat= #some data frame or matrix
> r.ltm(r('dat~z1'))
>
> error coming is---
> RPy_RException: Error in eval(expr, envir, enclos) : object 'dat' not found
>
> Please tell me the right way to call ltm function using rpy library

Your problem is that the 'dat' object is in the Python world but not in
the R world. However, the expression r('dat~z1') is looking for a 'dat'
object in the R world. So you must copy the Python object to the R
world.

I don't know if this will work in rpy, but it does work in rpy2. Rpy2
has much better possibilities than rpy. But just try it:

r['=']('dat', dat)
r.ltm(r('dat~z1'))
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


strace of python shows nonsense

2013-01-14 Thread Joep van Delft
Hi there, 


I am puzzled at how I borked my installation. Python loads slow on my
machine, and I decided to use strace and /usr/bin/time to see what is
actually happening. 

# sync && echo 3 > /proc/sys/vm/drop_caches
$ /usr/bin/time python2 -c ""
0.19user 0.04system 0:01.22elapsed 19%CPU (0avgtext+0avgdata
4200maxresident)k
7312inputs+0outputs (4major+1145minor)pagefaults 0swaps

And when all is in memory: 
$ /usr/bin/time python2 -c ""
0.19user 0.01system 0:00.21elapsed 98%CPU
 (0avgtext+0avgdata 4216maxresident)k 0inputs+0outputs
 (0major+1153minor)pagefaults 0swaps

$ /usr/bin/time python2 -c "import argparse"
0.36user 0.02system 0:00.39elapsed 98%CPU
 (0avgtext+0avgdata 5752maxresident)k 0inputs+0outputs
 (0major+1699minor)pagefaults 0swaps

.2 and .4 seconds to not even get started when all disk I/O is
cached. So what is happening here? 

$ strace -c python2 -c ""
% time seconds  usecs/call callserrors syscall
-- --- --- - - 
 93.260.001910   8   230   168 open
  3.660.75   9 8   mprotect
  3.080.63   197   fstat64
  0.000.00   0   172   read
  0.000.00   063   close
<...>

$ strace -c python2 -c "import argparse"
% time seconds  usecs/call callserrors syscall
-- --- --- - - 
 51.890.003732  13   290   read
 47.290.003401  18   192   155 stat64
  0.820.59   0   129   mmap2
  0.000.00   0   549   443 open
  0.000.00   0   107   close
<...>

What puzzles me, is the amount of errors for open and stat64. There
are references to stuff I don't know (~/GNUStep directory? Stuff
under site-packages that does not exist? Subdirs of site-packages
that are not included in sys.path?) What is python doing there, and
why? And, more importantly, how can this be corrected? 

Probably irrelevant, but Python2 version 2.7.3, Archlinux (current as
of previous weekend). 


Thanks, 

Joep



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


Re: Loading a PKCS#1 public key using M2Crypto

2013-01-16 Thread Piet van Oostrum
Marc Aymerich  writes:

> Hi, 
> I've been trying very, very hard to load an RSA key using M2Crypto but 
> without any success.
>
> basically this is what I'm trying to do:
>>>> from M2Crypto import BIO, RSA
>>>> 
>>>> pubkey = """-BEGIN RSA PUBLIC KEY-
> ... MIIBCgKCAQEApwotnfHT9RAmxnuaGEMdI3lYPYE4aaqSD9v4KbTh1E7Le3GNJQb7
> ... wCpmDe8+n8S5Kp/gBEpWiYuvsVA/T4KseoX7NMcacP+DJMwjmNd9U58USn2vLz0Z
> ... TMtXpc/FUhW5PZdgCiuNzw6IFgGn9ZCCv85jjUIW3KD8fUNdrUfVSv4olDoL9NkR
> ... dTRg3Os/znC6l0gv/mqnLaqj2bJ/tx47kUmj6Oq13JuEq34T+DVmsUCFVundQnRp
> ... c/vVEqQot7Rvj9UmSvTi4WKt/qxiAnyZf3gXOdrXvxfVTGzD5I/Xg+By+a4C2JwB
> ... A5RGvZP3fyfhkCnnhFDpfws5lc20FA6ryQIDAQAB
> ... -END RSA PUBLIC KEY-"""
>>>> 
>>>> bio = BIO.MemoryBuffer(pubkey.encode('ascii'))
>>>> RSA.load_pub_key_bio(bio)
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/lib/python2.7/dist-packages/M2Crypto/RSA.py", line 422, in 
> load_pub_key_bio
> rsa_error()
>   File "/usr/lib/python2.7/dist-packages/M2Crypto/RSA.py", line 302, in 
> rsa_error
> raise RSAError, m2.err_reason_error_string(m2.err_get_error())
> M2Crypto.RSA.RSAError: no start line
>
>
> Reading all whats in Internet about this problem it seems that my key is in 
> PKCS#1 format but M2Crypto only reads X.501 RSA keys. 
>
> I know that python-crypto doesn't have any problem loading this key, but I'll 
> prefer to stick with M2Crypto because I already have lots code using M2Crypto.
>
> So my question is, is there any way to load this key using M2Crypto? Can I 
> convert somehow the key to X.501?
>
Converting to X.501 isn't difficult (assuming this is a 2048 bit key):
Get rid of the 'RSA' in header and trailer
Prepend X.501 header 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' to the data
Reformat the lines to 64 characters.

from M2Crypto import BIO, RSA

pubkey="""-BEGIN RSA PUBLIC KEY-
MIIBCgKCAQEApwotnfHT9RAmxnuaGEMdI3lYPYE4aaqSD9v4KbTh1E7Le3GNJQb7
wCpmDe8+n8S5Kp/gBEpWiYuvsVA/T4KseoX7NMcacP+DJMwjmNd9U58USn2vLz0Z
TMtXpc/FUhW5PZdgCiuNzw6IFgGn9ZCCv85jjUIW3KD8fUNdrUfVSv4olDoL9NkR
dTRg3Os/znC6l0gv/mqnLaqj2bJ/tx47kUmj6Oq13JuEq34T+DVmsUCFVundQnRp
c/vVEqQot7Rvj9UmSvTi4WKt/qxiAnyZf3gXOdrXvxfVTGzD5I/Xg+By+a4C2JwB
A5RGvZP3fyfhkCnnhFDpfws5lc20FA6ryQIDAQAB
-END RSA PUBLIC KEY-
"""

pk = pubkey.split('\n')
pk = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' + ''.join(pk[1:-2])
pk = [pk[i:i+64] for i in range(0, len(pk), 64)]
pk = '-BEGIN PUBLIC KEY-\n' + '\n'.join(pk) + '\n-END PUBLIC 
KEY-'

bio = BIO.MemoryBuffer(pk) # pk is ASCII, don't encode
key = RSA.load_pub_key_bio(bio)

-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Loading a PKCS#1 public key using M2Crypto

2013-01-17 Thread Piet van Oostrum
Piet van Oostrum  wrote:

> Converting to X.501 isn't difficult (assuming this is a 2048 bit key):
> Get rid of the 'RSA' in header and trailer
> Prepend X.501 header 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' to the data
> Reformat the lines to 64 characters.

This solution is a bit restricted as it only works if the key is 2048
bits and uses an exponent of 65537 (which is the default). Otherwise it
fails.

Here is a robust solution that works for all PKCS#1 keys. Instead of
using a fixed X.501 header it calculates the header. We could do a
complete ASN.1 encoding, but most of the parts are fixed. The only
variable parts are two length fields. So I just plug these into the
fixed stuff. This saves using one of the ASN.1 libraries. We do have to
work in binary (DER format) instead of base64, however.

from M2Crypto import BIO, RSA
import base64

def der_length(length):
"""DER encoding of a length"""
if length < 128:
return chr(length)
prefix = 0x80
result = ''
while length > 0:
result = chr(length & 0xff) + result
length >>= 8
prefix += 1
return chr(prefix) + result

pubkey="""-BEGIN RSA PUBLIC KEY-
MIIBCgKCAQEApwotnfHT9RAmxnuaGEMdI3lYPYE4aaqSD9v4KbTh1E7Le3GNJQb7
wCpmDe8+n8S5Kp/gBEpWiYuvsVA/T4KseoX7NMcacP+DJMwjmNd9U58USn2vLz0Z
TMtXpc/FUhW5PZdgCiuNzw6IFgGn9ZCCv85jjUIW3KD8fUNdrUfVSv4olDoL9NkR
dTRg3Os/znC6l0gv/mqnLaqj2bJ/tx47kUmj6Oq13JuEq34T+DVmsUCFVundQnRp
c/vVEqQot7Rvj9UmSvTi4WKt/qxiAnyZf3gXOdrXvxfVTGzD5I/Xg+By+a4C2JwB
A5RGvZP3fyfhkCnnhFDpfws5lc20FA6ryQIDAQAB
-END RSA PUBLIC KEY-
"""

pk = pubkey.split('\n')
pk = '\0' + base64.decodestring("".join(pk[1:-2]))
pk = '\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00\x03' + \
 der_length(len(pk)) + pk
pk = '\x30' + der_length(len(pk)) + pk
pk = '-BEGIN PUBLIC KEY-\n' + base64.encodestring(pk) + '-END 
PUBLIC KEY-'

bio = BIO.MemoryBuffer(pk)
key = RSA.load_pub_key_bio(bio)

-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Loading a PKCS#1 public key using M2Crypto

2013-01-17 Thread Piet van Oostrum
Marc Aymerich  writes:

> Thank you very much Piet, 
> I'm just starting to grasp these cryptography related concepts and your code 
> is helping me a lot to understand how to handle these keys in a low level.
>
> I'm updating my code incorporating your new contribution!
>
> Just to let you know, during my previous research I had found a python-Crypto 
> related solution that also uses DER and ASN.1 [1], but it uses a different 
> approach (I guess). I suspect that this approach is also possible with 
> M2Crypto because it has a method for constructing RSA keys [2]. 
>
> [1] http://stackoverflow.com/a/10574723
> [2] 
> http://www.heikkitoivonen.net/m2crypto/api/M2Crypto.RSA-module.html#new_pub_key
>
new_pub_key could be used but then you would have to do an ASN.1 parse
of the DER format of your key to get the n and e values. AFAICT M2Crypto
doesn't have methods to do this, so you would need to use one of the
python ASN.1 libraries (or write that part yourself).
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Forcing Python to detect DocumentRoot

2013-01-19 Thread Piet van Oostrum
Ferrous Cranus  writes:

> This is addon domain's counter.py snippet tried to load an image mail.png and 
> failed because it cant see past its document root
>
> 
> # render html template and print it
> data = f.read()
> counter = '''
>  mailto:[email protected]";>  src="/data/images/mail.png"> 
>  
>  
>   Αριθμός Επισκεπτών 
>   %d ''' % hits[0]
> 
>

> While from within the same counter.py file
>
> # open html template file
> f = open( '/home/nikos/public_html/test.txt' )
>
> opens OK the page file which is also past addons domain's document root
>
> Can you help counter.py to load the image? Why does it fail to load it? 
> Python can have access to ANY filesystempath , no matter from what folder 
> counter.py script runs from. Correct?

That piece of code is not opening the image file. It just issues the URL
for the image file. The file will then be loaded by the browser in a new
request. The image should be at
/home/nikos/public_html/data/images/mail.png

P.S. I don't understand what you mean by "addon domain".

-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Forcing Python to detect DocumentRoot

2013-01-21 Thread Piet van Oostrum
Ferrous Cranus  writes:

> Ok i see its just a convention.
> Can you help on this:
>
> so we need to remove   since the apache cant 
> see to open it and let Python open it which we know it can because it has 
> access to any system file the user has access too. 
>
> httpd cannot open this file because the location of the image is past the 
> addon domain's Document Root. 
>
> /home/nikos/public_html/cafebar-idea.gr = Addon's Domain Document Root 
>
> /home/nikos/public_html/data/images/mail.png = where the image file is 
> located 
>
> and the python scipt is on: 
>
> /home/nikos/public_html/cafebar-idea.gr/cgi-bin/counter.py 
>
> So if a python script can open any file the user has access too then we need 
> a "python way" of opening this file. 

So why don't you put the image at  
/home/nikos/public_html/cafebar-idea.gr/data/images/mail.png?
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Uniquely identifying each & every html template

2013-01-21 Thread Piet van Oostrum
Ferrous Cranus  writes:

> This python script acts upon websites other people use and every html
> templates has been written by different methods(notepad++,
> dreamweaver, joomla).
>
> Renames and moves are performed, either by shell access or either by
> cPanel access by website owners.
>
> That being said i have no control on HOW and WHEN users alter their html 
> pages.

Under these circumstances the only way to solve it is to put an
identification *inside* the file and make sure it will not be changed.
It could for example be some invisible piece of HTML, or an attribute to
some tag. If that can't be done the problem cannot be solved and it
makes no sense keeping asking the same question over and over again.
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


finding abc's

2013-01-25 Thread lars van gemerden
Hi all,

i was writing a function to determine the common base class of a number classes:

def common_base(classes):
if not len(classes):
return None
common = set(classes.pop().mro())
for cls in classes:
common.intersection_update(cls.mro())
while len(common) > 1:
cls1 = common.pop()
cls2 = common.pop()
if issubclass(cls1, cls2):
common.add(cls1)
elif issubclass(cls2, cls1):
common.add(cls2)
return common.pop()

and ran common_base(int, float), hoping to get numbers.Number.

this did not work because abstract base classes are not always in the mro() of 
classes.

My question is: is there a way to obtain the abc's of a class or otherwise a 
way to make the function above take abc's into account (maybe via a predefined 
function)?

Cheers, Lars 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding abc's

2013-01-25 Thread lars van gemerden
On Friday, January 25, 2013 8:04:32 PM UTC+1, Ian wrote:
> On Fri, Jan 25, 2013 at 10:40 AM, lars van gemerden
> 
>  wrote:
> 
> > Hi all,
> 
> >
> 
> > i was writing a function to determine the common base class of a number 
> > classes:
> 
> >
> 
> [...]
> 
> >
> 
> > and ran common_base(int, float), hoping to get numbers.Number.
> 
> >
> 
> > this did not work because abstract base classes are not always in the mro() 
> > of classes.
> 
> >
> 
> > My question is: is there a way to obtain the abc's of a class or otherwise 
> > a way to make the function above take abc's into account (maybe via a 
> > predefined function)?
> 
> 
> 
> 
> 
> If the abstract base class's module has not been imported, it may not
> 
> even be loaded into memory, even though it is technically considered a
> 
> superclass.  Consider this:
> 
> 
> 
> 
> 
> Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
> 
> (Intel)] on win32
> 
> Type "help", "copyright", "credits" or "license" for more information.
> 
> >>> def common_base(classes):
> 
> ... common = set()
> 
> ... for cls in object.__subclasses__():
> 
> ... if all(issubclass(c, cls) for c in classes):
> 
> ... common.add(cls)
> 
> ... return common
> 
> ...
> 
> >>> common_base([int, float])
> 
> set([])
> 
> >>> import numbers
> 
> >>> common_base([int, float])
> 
> set([, ])
> 
> 
> 
> 
> 
> If you're okay with that, then the approach above might work.
> 
> 
> 
> 
> 
> > while len(common) > 1:
> 
> > cls1 = common.pop()
> 
> > cls2 = common.pop()
> 
> > if issubclass(cls1, cls2):
> 
> > common.add(cls1)
> 
> > elif issubclass(cls2, cls1):
> 
> > common.add(cls2)
> 
> 
> 
> There is a flaw with your set reduction code here.  If neither class
> 
> is a subclass of the other, then both will be removed.  There may not
> 
> actually be a single closest common base class, however.  What would
> 
> you expect the function to return in the following situation?
> 
> 
> 
> class A(object): pass
> 
> class B(object): pass
> 
> class C(A, B): pass
> 
> class D(A, B): pass
> 
> 
> 
> print common_base([C, D])

thanks, good catch, and very concise answer. I'll give up on trying to get 
abc's   and improve my algorithm.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding abc's

2013-01-25 Thread lars van gemerden
On Friday, January 25, 2013 8:08:18 PM UTC+1, Peter Otten wrote:
> lars van gemerden wrote:
> 
> 
> 
> > Hi all,
> 
> > 
> 
> > i was writing a function to determine the common base class of a number
> 
> > classes:
> 
> > 
> 
> > def common_base(classes):
> 
> > if not len(classes):
> 
> > return None
> 
> > common = set(classes.pop().mro())
> 
> > for cls in classes:
> 
> > common.intersection_update(cls.mro())
> 
> > while len(common) > 1:
> 
> > cls1 = common.pop()
> 
> > cls2 = common.pop()
> 
> > if issubclass(cls1, cls2):
> 
> > common.add(cls1)
> 
> > elif issubclass(cls2, cls1):
> 
> > common.add(cls2)
> 
> > return common.pop()
> 
> > 
> 
> > and ran common_base(int, float), hoping to get numbers.Number.
> 
> > 
> 
> > this did not work because abstract base classes are not always in the
> 
> > mro() of classes.
> 
> > 
> 
> > My question is: is there a way to obtain the abc's of a class or otherwise
> 
> > a way to make the function above take abc's into account (maybe via a
> 
> > predefined function)?
> 
> 
> 
> The abstract base classes may run arbitrary code to determine the subclass 
> 
> relationship:
> 
>  
> 
> >>> from abc import ABCMeta
> 
> >>> import random
> 
> >>> class Maybe(metaclass=ABCMeta):
> 
> ... @classmethod
> 
> ... def __subclasshook__(cls, C):
> 
> ... print("processing", C)
> 
> ... return random.choice((False, True))
> 
> ... 
> 
> >>> isinstance(1.1, Maybe)
> 
> processing 
> 
> True
> 
> >>> isinstance(1.1, Maybe)
> 
> True
> 
> >>> isinstance(1, Maybe)
> 
> processing 
> 
> False
> 
> >>> issubclass(float, Maybe)
> 
> True
> 
> 
> 
> You'd have to check every pair of classes explicitly and might still miss 
> 
> (for example) numbers.Number as the module may not have been imported. 
> 
> 
> 
> I think you are out of luck.

Thank you, interesting example. Added confirmation that trying to get the abc's 
is a bad idea. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding abc's

2013-01-25 Thread lars van gemerden
for future reference, i decided to go with 2 functions:

def common_bases(classes):
if not len(classes):
return None
common = set(classes.pop().mro())
for cls in classes:
common.intersection_update(cls.mro()) #all subclasses in common   
return [cls for cls in common if not any(sub in common for sub in 
cls.__subclasses__())] #the classes of which no subclasses are present

def unique_common_base(classes):
while len(classes) > 1:
classes = common_bases(classes)
return classes.pop()

if i tested and understood correctly, they only take classes in the mro() into 
account (which might include abc's), the first gives all common base classes, 
the second recursively reduces further to one single class (the latter might 
not make to much sense, but in my program it is a safe bet for rare cases).

Thanks again for the help,

Cheers, Lars
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The devolution of English language and slothful c.l.p behaviors exposed!

2012-01-27 Thread Emile van Sebille

On 1/25/2012 9:14 PM Steven D'Aprano said...

In the
same way that a native English speaker would never make the mistake of
using "organ" to refer to an unnamed mechanical device, so she would
never use "gadget" to refer to an unnamed body part.


My wife introduced me to the term "picnic gadget" as the means by which 
males avoid restroom lines...


Emile


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


Re: runtime error

2012-01-28 Thread Emile van Sebille

On 1/27/2012 10:38 AM nikos spanakis said...

Hi

I just minstalled python 3.1 on my windons XP SP3

but on the start up I get the following error message:


You don't say what you specifically installed, but for windows you may 
find activestates distribution a good fit.  See 
http://www.activestate.com/activepython/downloads


HTH,

Emile

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


Re: PyCrypto builds neither with MSVC nor MinGW

2012-02-08 Thread Case Van Horsen
On Tue, Feb 7, 2012 at 9:37 PM, Alec Taylor  wrote:
> Thanks all for your replies.
>
> I have now installed MSVC8 and YASM.
I assume you installed Visual Studio. I've omitted the commands to use
the SDK compiler below.
>
> I was able to successfully run configure.bat and make.bat (including
> make.bat check).
>
> However, I'm unsure what to do about install, since there is no
> install arg. Do I copy it across to my VC\bin folder, or does it need
> it's own place in PATH + system variables?

The following is just a guess.

I copy the files to a convenient location and then specify that
location to setup.py. Below is an excerpt from my build process.

mkdir c:\src\lib
mkdir c:\src\include
xcopy /Y mpir.h c:\src\include\*.*
xcopy /Y win\mpir.lib c:\src\lib\*.*

python setup.py build_ext -Ic:\src\include -Lc:\src\lib install

>
> I am asking because I don't know where it is looking for the MPIR library.
>
> Thanks for all suggestions,
>
> Alec Taylor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyCrypto builds neither with MSVC nor MinGW

2012-02-08 Thread Case Van Horsen
On Wed, Feb 8, 2012 at 4:24 AM, Alec Taylor  wrote:
> Thanks, but to get it to work with pip, wouldn't I need to add it to
> PATH? - Or can I just add those library args to pip?
I don't think so. pyCrypto probably builds a single DLL so the MPIR library is
statically linked into that DLL. Only the innvocation of setup.py should need
to refer to the MPIR library locations.  I don't use pip so I'm not sure how to
get pip to install the resulting DLL, etc.
>
> On Wed, Feb 8, 2012 at 9:48 PM, Case Van Horsen  wrote:
>> On Tue, Feb 7, 2012 at 9:37 PM, Alec Taylor  wrote:
>>> Thanks all for your replies.
>>>
>>> I have now installed MSVC8 and YASM.
>> I assume you installed Visual Studio. I've omitted the commands to use
>> the SDK compiler below.
>>>
>>> I was able to successfully run configure.bat and make.bat (including
>>> make.bat check).
>>>
>>> However, I'm unsure what to do about install, since there is no
>>> install arg. Do I copy it across to my VC\bin folder, or does it need
>>> it's own place in PATH + system variables?
>>
>> The following is just a guess.
>>
>> I copy the files to a convenient location and then specify that
>> location to setup.py. Below is an excerpt from my build process.
>>
>> mkdir c:\src\lib
>> mkdir c:\src\include
>> xcopy /Y mpir.h c:\src\include\*.*
>> xcopy /Y win\mpir.lib c:\src\lib\*.*
>>
>> python setup.py build_ext -Ic:\src\include -Lc:\src\lib install
>>
>>>
>>> I am asking because I don't know where it is looking for the MPIR library.
>>>
>>> Thanks for all suggestions,
>>>
>>> Alec Taylor
-- 
http://mail.python.org/mailman/listinfo/python-list


Postpone evaluation of argument

2012-02-10 Thread Righard van Roy
Hello,

I want to add an item to a list, except if the evaluation of that item
results in an exception.
I could do that like this:

def r(x):
if x > 3:
raise(ValueError)

try:
list.append(r(1))
except:
pass
try:
list.append(r(5))
except:
pass

This looks rather clumbsy though, and it does not work with i.e. list
comprehensions.

I was thinking of writing a decorator like this:

def tryAppendDecorator(fn):
def new(*args):
try:
fn(*args)
except:
pass
return new

@tryAppendDecorator
def tryAppend(list, item):
list.append(item)

tryAppend(list, r(1))
tryAppend(list, r(5))

This does not work however because the 'item' argument gets evaluated
before the decorator does it's magic.

Is there a way to postpone the evaluation of 'item' till it gets used
inside the decorator. Like it is possible to quote a form in Lisp.

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


Re: list of properties

2012-02-16 Thread Emile van Sebille

On 2/16/2012 9:19 AM Emmanuel Mayssat said...

Hello,

Is there a way to list 'properties' ?


dir(thingy)




from PyQt4.QtCore import *

class LObject(QObject):
 def __init__(self, parent=None):
 super(LObject, self).__init__(parent)
 self.arg1 = 'toto'

 def getArg2(self):
 return self.arg1 + " <--"
 arg2 = property(getArg2)

l = LObject()
print l.__dict__

returns only arg1


arg2 is defined at the class level and not the instance level. 
l.__dict__ returned the instance attributes. Look in LObject.__dict__ 
for arg2.


Emile





How can I find that arg2 is a 'virtual' attribute (i.e. property)?

Regards,
--
E





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


  1   2   3   4   5   6   7   8   9   10   >