win32com early binding problem

2006-01-25 Thread Roland
Hello,

I'm trying to use Sparx Systems Enterprise Architect OLE automation
interface. There is no problem to get early binding interface using
Microsoft Visual Basic. But using win32com makepy utility there problem
is.

Does anybody have an idea, what may disqualify python win32com from
creating early binding interface?

There is possibility to download trial version on considered program on
www.sparxsystems.com.

Thank you

Roland Divin
[EMAIL PROTECTED]

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


Re: Ideal way to separate GUI and logic?

2013-07-13 Thread Roland Koebler
Hi,

> But how then do I separate out the logic and the GUI?
I usually write a library (C library, Python module, ...) which contains
the logic.

Then, I write a GUI (in a separate file), which imports and uses the library.
If I need another UI (e.g. GUI with an other toolkit, or a text-based or
HTML5-based interface), I simply write another UI (in a separate file), and
import+use the library again.

That's the cleanest way to separate user-interface and logic in my
opinion. (But keep in mind that it's not always obvious which parts
belong to the library and which belong to the GUI, and you sometimes
have to carefully think about it.)

Oh, and yes, you can do nice things then, e.g. remote-GUIs by transparently
tunneling all calls from the GUI to the library through RPC over a network
(like I have done with a GTK+-GUI for Raspberry Pi; the GUI runs on the PC,
uses JSON-RPC over TCP-sockets and calls functions on the RPi).


regards,
Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OAuth 2.0 implementation

2012-03-27 Thread Roland Hedberg
And then to complicate the picture you have OpenID Connect which is an attempt 
at
bringing OpenID and OAuth2.0 together.

By the way I have an implementation of OpenID Connect here:

https://github.com/rohe/pyoidc

-- Roland

27 mar 2012 kl. 11:59 skrev Stuart Bishop:

> On Tue, Mar 27, 2012 at 10:11 AM, Ben Finney  
> wrote:
>> Demian Brecht  writes:
>> 
>>> I'm getting close to an alpha release of an OAuth 2.0 implementation
>>> (https://github.com/demianbrecht/py-sanction).
>> 
>> Thank you for doing this work.
>> 
>> As someone who uses OpenID, what can I read about why OAuth is better?
> 
> They are different, and often you need to use both.
> 
> OpenID allows web sites to authenticate someone. It is not really
> useful for anything not an interactive web site. The consuming site
> never gets your keys, it just gets confirmation from the provider that
> the user is who they claim they are and maybe some details that the
> provider chooses to provide such as an email address.
> 
> OAuth is for generating authentication keys that allow a program to
> authenticate as someone and perform operations on their behalf. You
> use OAuth to generate a key so that Foursquare can send messages via
> Twitter on your behalf, or so the Facebook client on your phone can
> access your account without storing your password. You also get
> authentication here, as you can't generate a key without being
> authenticated, but the real reason it is used instead of OpenID is so
> you can keep the key and keep using it to act as the user; you can
> keep using that key until it expires or it is revoked.
> 
> Authentication providers that don't provide a webapi just implement
> OpenID. Big sites like Google and Facebook implement both OpenID (for
> 'log in with your GMail account') and OAuth ('post this message to
> your Facebook wall').
> 
> -- 
> Stuart Bishop 
> http://www.stuartbishop.net/
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Roland

---
With anchovies there is no common ground 
-- Nero Wolfe

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


Slow termination of process

2012-03-27 Thread Roland Hedberg
Hi!

I have an application/a script that is run from another application.
The script is mostly working as a HTTP client but in some cases it also has to 
act as a HTTP server.
Basically just for serving a few files. The files are dynamically created by 
the script when needed.

To accomplish this I'm trying to use subprocess Popen
and an extremely simple web server script which basically contains:

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer((hostname, port), Handler)
httpd.serve_forever()

In the main script I do:

op = Popen(popen_args, stdout=PIPE, stderr=PIPE)

When the main script is done it closes down the HTTP server by doing:

op.terminate()

The problem I have is that if the main script is run again almost immediate 
then the old HTTP server 
process doesn't seem to have released the port yet. So setting up a new server 
fails.

Is there anything I can do to get the port released immediately when the 
tcpserver is terminated.
Or is there another way of doing this that will work better ?

Roland

---
With anchovies there is no common ground 
-- Nero Wolfe

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


Re: Slow termination of process

2012-03-27 Thread Roland Hedberg
So, I went for the low-hanging fruit and defined my own TCPServer class

class MyTCPServer(SocketServer.TCPServer):
def __init__(self, server_address, RequestHandlerClass):
self.allow_reuse_address = True
SocketServer.TCPServer.__init__(self, server_address,
RequestHandlerClass)

and then

httpd = MyTCPServer((hostname, port), Handler)
httpd.serve_forever()

and this solved my problem!

So, thanks again Chris!

27 mar 2012 kl. 15:55 skrev Chris Angelico:

> On Wed, Mar 28, 2012 at 12:03 AM, Roland Hedberg  wrote:
>> When the main script is done it closes down the HTTP server by doing:
>> 
>>op.terminate()
>> 
>> The problem I have is that if the main script is run again almost immediate 
>> then the old HTTP server
>> process doesn't seem to have released the port yet. So setting up a new 
>> server fails.
> 
> You may wish to consider a more orderly shutdown (send the server a
> signal upon which it shuts itself down), but the simplest and most
> direct solution is to set the SO_REUSEADDR flag.
> 
> http://docs.python.org/library/socket.html?highlight=so_reuseaddr
> 
> I've not actually used the TCPServer class myself, but a cursory
> glance at the docs suggests that it's possible if you subclass it:
> 
> http://docs.python.org/library/socketserver.html#SocketServer.BaseServer.allow_reuse_address
> 
> Chris Angelico
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Roland

---
With anchovies there is no common ground 
-- Nero Wolfe

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


Python Tools for Visual Studio from Microsoft - Free & Open Source

2011-03-09 Thread roland garros

FYI...

http://pytools.codeplex.com

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


Re: English Idiom in Unix: Directory Recursively

2011-05-17 Thread Roland Hutchinson
, it doesn't seems to matter.
> 
> So the interesting question is, why this idiom works? Or, how it
> developed?

It's also not an idiom.  It's meaning is completely determined by the 
meaning of "delete" and the meaning of "recurse", as in "recurse down a 
tree structure"--which is precisely what the various *nix commands do 
when their recursive option is invoked.
 
"Recurse _down_ a tree" is an interesting phrase, though, as it implies 
that, in computing, trees are thought of as growing with their root 
topmost and their branches underneath -- i.e., upside-down!

> I think, among programers (which all unix users are in the 1970s), every
> one knows the concept of recursion, and many unix tools on dir probably
> are implemented with a recursive algorithm. When you say “…
> recursively”, the point gets across, because we all understand it, even
> when we are not actually talking about implementation. The phrase “…
> directory recursively” is short and memorable, while “… directory and
> all its contents” or “… directory and all its branches” or “… directory
> and all its sub-directories and files” are wordy and unwieldy.
> ✍
> 
> Idiocy Of Unix Copy Command
> Emacs Lisp Suggestion: Function to Copy/Delete a Directory
> Recursively
> How to rsync, unison, wget, curl
> Hunspell Tutorial
> Mac OS X Resource Fork and Command Line Tips ImageMagick Tutorial
> Making System Calls in Perl and Python Unix And Literary Correlation
> The Unix Pestilence
> To An Or Not To An
> On “I” versus “i” (capitalization of first person pronoun) On the
> Postposition of Conjunction in Penultimate Position of a
> Sequence
> What's Passive Voice? What's Aggressive Voice? Why You Should Avoid
> The Jargon “Tail Recursion” Why You should Not Use The Jargon Lisp1
> and Lisp2 Jargons of Info Tech Industry
> 
>  Xah

I'm writing from alt.usage.english.  The non-natural language mavens may 
have more to add.

-- 
Roland Hutchinson   

He calls himself "the Garden State's leading violist da gamba,"
... comparable to being ruler of an exceptionally small duchy.
--Newark (NJ) Star Ledger  ( http://tinyurl.com/RolandIsNJ ) 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: English Idiom in Unix: Directory Recursively

2011-05-20 Thread Roland Hutchinson
On Wed, 18 May 2011 07:19:08 +0200, Pascal J. Bourguignon wrote:

> Roland Hutchinson  writes:
> 
>> Sorry to have to contradict you,
> 
> Don't be sorry.
> 
> 
>> but it really is a textbook example of recursion.  Try this psuedo-code
>> on for size:
>>
>> FUNCTION DIR-DELETE (directory)
>>   FOR EACH entry IN directory
>>   IF entry IS-A-DIRECTORY THEN DIR-DELETE (entry).
>>
>> Well, now that's not just recursion; it's tail recursion.
> 
> It's not tail recursion.  If you had indented your code properly, you'd
> see why it's not:
> 
> (defun dir-delete (directory)
>   (loop for entry in directory
> do (if (is-a-directory entry)
>(dir-delete entry
> 

You are right, of course.  Thanks for the correction.

> (I put parentheses, so my editor knows what I mean and can do the
> indentation for me).

My editor would have done that, too--if I had bothered to be thinking 
clearly.

> That's why walking a directory is done with a recursive procedure,
> instead of an iterative one: it's much simplier.  To implement an
> iterative procedure, you would have to manage a stack yourself, instead
> of using the implicit stack of the recursive procedure.

Got it!  Thanks again.


-- 
Roland Hutchinson   

He calls himself "the Garden State's leading violist da gamba,"
... comparable to being ruler of an exceptionally small duchy.
--Newark (NJ) Star Ledger  ( http://tinyurl.com/RolandIsNJ ) 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Bulk] Re: Alternatives to XML?

2016-08-26 Thread Roland Koebler
Hi,

> It is *my* XML, and I know that I only use the offending characters inside
> attributes, and attributes are the only place where double-quote marks are
> allowed.
> 
> So this is my conversion routine -
> 
> lines = string.split('"')  # split on attributes
> for pos, line in enumerate(lines):
>if pos%2:  # every 2nd line is an attribute
>lines[pos] = line.replace('<', '<').replace('>', '>')
> return '"'.join(lines)
OMG!
So, you have a fileformat, which looks like XML, but actually isn't XML,
and will break if used with some "real" XML.

Although I don't like XML, if you want XML, you should follow Chris advice:
On Thu, Aug 25, 2016 at 09:40:03PM +1000, Chris Angelico wrote:
> just make sure it's always valid XML, rather
> than some "XML-like" file structure.

So, please:
- Don't try to write your own (not-quite-)XML-parser.
- Read how XML-files work.
- Read https://docs.python.org/3/library/xml.html
  and https://pypi.python.org/pypi/defusedxml/
- Think what you have done.
- Use a sensible XML-parser/dumper. This should escape most special-
  characters for you (at least: < > & " ').


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


Re: Alternatives to XML?

2016-08-26 Thread Roland Koebler
Hi,

after reading the mails of this thread, I would recommend one of the
following ways:

1. Use a computer-readable format and some small editor for humans.
   
   The file-format could then be very simple -- I would recommend JSON.
   Or some kind of database (e.g. SQLite).

   For humans, you would have to write a (small/nice) graphical editor,
   where they can build the logic e.g. by clicking on buttons.
   This can also work for non-programmers, since the graphical editor
   can be adapted to the indended users, give help, run wizards etc.

or:

2. Use a human-readable format and a parser for the computer.

   Then, the fileformat should be optimized for human readability.
   I would recommend a restricted subset of Python. This is much more
   readable/writeable for humans than any XML/JSON/YAML.
   And you could even add a graphical editor to further support
   non-programming-users.

   The computer would then need a special parser. But by using
   Python-expressions (only eval, no exec) and a parser for flow
   control (if/else/for/...) and assignments, this is not too much
   work and is good for many applications.

   I've written such a parser incl. some kind of (pseudo-)sandbox [2]
   for my template-engine "pyratemp" [1], and I've also used it for
   small user-created-procedures.

   [1] http://www.simple-is-better.org/template/pyratemp.html
   [2] It's not a real sandbox -- it's secured only by restricting
   the available commands. If you add unsafe commands to the
   pseudo-sandbox (e.g. Pythons "open"), the user can do bad
   things.
   But without manually adding unsafe commands, I don't know
   any way to get out of this pseudo-sandbox.
   And if you really need a sandbox which is more powerful
   than my pseudo-sandbox, you may want to have a look at
   the sandbox of PyPy.


Trying to use a format which is both directly computer-readable
(without a special parser) and well human readable never really
works well in my experience. Then, you usually have to manually
read/write/edit some kind of parse-tree, which is usually much
harder to read/write than code. But if you want to do this, I
recommend LISP ;).

(By the way: If I did understand your mails correctly, your
program would probably break if someone edits the XML-files
manually, since you're using some kind of XML-like-fileformat
with many non-intuitive assumptions.)


Roland


PS: 

On Wed, Aug 24, 2016 at 04:58:54PM +0200, Frank Millman wrote:
> Here is a JSON version -
> 
> {
>  "case": {
>"compare": {
>  "-src": "_param.auto_party_id",
>  "-op": "is_not",
>  "-tgt": "$None",
>  "case": {
>"on_insert": {
>  "auto_gen": { "-args": "_param.auto_party_id" }
>},
>"not_exists": {
>  "literal": { "-value": "" }
>}
>  }
>}
>  }
> }
I think this is not really good. In JSON, you also have lists, and in this
case, it would probably be better to use some lists instead of dicts, e.g.:

[
["if", ["_param.auto_party_id", "is not", "None"],
["if",   ["on_insert"],  ["set", "value", ["call", "auto_gen", 
"_param.auto_party_id"]]],
["elif", ["not_exists"], ["set", "value", "''"]]
]
]

I think this is much more readable than your XML-code and the
auto-converted JSON.

And it's even less ambigious. (How do you distinguish between the
variable _param.auto_party_id and the string "_param.auto_party_id"
in your XML-example?)

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


Any available SAML 2.0 tools in Python ?

2005-11-09 Thread Roland Hedberg
Hi!

The subject says it all.

If there isn't anything already available, does anyone know about  
anyone working on something ?

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


Problem build python bindings to lasso with swig on mac os x

2005-12-04 Thread Roland Hedberg
Hi!

This involves quite a lot of different system, so I'm not really sure  
who which would be the right list to query.

So, I'm trying a couple, this list being one of them.

I'm trying to build the lasso (http://lasso.entrouvert.org/)  
libraries and what I really want to get at is the Python bindings.
Those binding are done with the help of swig and it fails with the  
following output:

gcc  -o .libs/_lasso.so -bundle  .libs/_lasso_la-lasso_wrap.o  -L/sw/ 
lib -L/Users
/rolandhedberg/src/libxml2-2.6.22 -L/usr/local/lib -L/lib ../ 
lasso/.libs/liblasso
.dylib /usr/lib/libiconv.dylib /usr/lib/libpthread.dylib /usr/lib/ 
libz.dylib /usr
/lib/libm.dylib /usr/lib/libssl.dylib /usr/lib/libcrypto.dylib /usr/ 
local/lib/lib
dl.dylib /usr/lib/libresolv.dylib /usr/local/lib/ 
libgobject-2.0.dylib /usr/lib/li
bc.dylib /usr/local/lib/libglib-2.0.dylib /usr/local/lib/ 
libintl.dylib /usr/local
/lib/libxmlsec1-openssl.dylib /usr/local/lib/libxmlsec1.dylib -ldl / 
usr/local/lib
/libxslt.dylib /usr/local/lib/libxml2.dylib -lpthread -lz /sw/lib/ 
libiconv.dylib
-lm -lssl -lcrypto /usr/local/lib/libsasl2.dylib -Wl,-F. -Wl,-F.
/usr/bin/ld: Undefined symbols:
_PyArg_ParseTuple
_PyCObject_AsVoidPtr
_PyCObject_FromVoidPtrAndDesc
_PyCObject_GetDesc
_PyCObject_Type
_PyDict_SetItemString
_PyErr_NewException
_PyErr_Occurred
_PyErr_SetObject
_PyErr_SetString
_PyExc_IOError
_PyExc_IndexError
_PyExc_MemoryError
_PyExc_NameError
_PyExc_OverflowError
_PyExc_RuntimeError
_PyExc_SyntaxError
_PyExc_SystemError
_PyExc_TypeError
_PyExc_ValueError
_PyExc_ZeroDivisionError
_PyFloat_FromDouble
_PyInt_AsLong
_PyInt_FromLong
_PyModule_AddObject
_PyModule_GetDict
_PyObject_CallObject
_PyObject_GetAttr
_PyObject_SetAttrString
_PyString_FromString
_PyType_Type
_Py_BuildValue
_Py_InitModule4
__Py_NoneStruct
collect2: ld returned 1 exit status
make[1]: *** [_lasso.la] Error 1
make: *** [all-recursive] Error 1

To me it looks like there is a missing library somewhere, but then I  
might be wrong.

I've tried with different versions of swig 1.3.21 - 1.3.27 with the  
same result.
It's the 0.6.3 version of lasso I'm trying to build.

Comments ?

-- Roland

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


Re: here document

2005-01-11 Thread Roland Heiber
harold fellermann wrote:
f = open("/bin/exe.x","w")
print >>f , """CategoryY = GRIB
etc.
"""
This would overwrite the existing /bin/exe.x ...
HtH, Roland
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating text on images

2005-01-13 Thread Roland Heiber
morphex wrote:
Hi all,
I'm trying to create a script that will superimpose text on an image.
I didn't find any great examples out there on how this can be done (I
presume using PIL is necessary), do you know of any examples?
Thanks,
Morten
Hi,
something like this?
###
from PIL import Image, ImageFont, ImageDraw
import sys
im = Image.open(YOUR_IMAGE_HERE)
idraw = ImageDraw.Draw(im)
idraw.text((1,1),"Hello", fill=128)
im.save(YOUR_NEW_IMAGE_HERE, IMAGE_TYPE)
###
HtH, Roland
--
http://mail.python.org/mailman/listinfo/python-list


Best python postgres module?

2005-01-28 Thread Roland Heiber
Hi,
i recently migrated from mysql to postgresql and did use severel python 
postgres-modules. All do what they are designed for, so which one would 
you use? psycopg, pygresql, pypgsql? psycopg seems to be the best 
solution for heavy traffic/multiple connections  i have no real 
testing environment, so any advice which one to use for different 
usecases would be nice.

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


Re: Generating .pyc/.pyo from a make file

2005-02-02 Thread Roland Heiber
Tim Daneliuk wrote:
I use a makefile to create distribution tarballs of freestanding Python
programs and their documentation.  I cannot seem to find the right
command line option to just generate a pyc/pyo file from the program
and then exit.  If I use 'python - -c"import myprog"' it creates
the pyo file, but myprog starts up and keeps running.
IOW, I need a batch method for generating compiled python.  I know it
exists, but I can't find it for some reason ...
TIA,
Hi,
take a look at http://docs.python.org/lib/module-compileall.html
HtH, Roland
--
http://mail.python.org/mailman/listinfo/python-list


Re: Generating .pyc/.pyo from a make file

2005-02-02 Thread Roland Heiber
Tim Daneliuk wrote:
It does - thanks.  One more question:  Are pyc and pyo file portable
across operating systems?  I suspect not since I generated a pyo
on a FreeBSD machine that will not run on a Win32 machine.  I was
under the impression that "compiled" meant optimized byte code that
was portable across implementations, but it looks to not be the case...
Hi,
.pyc's should be, cause it's standard python-bytecode, if you use 
massive optimizations it depends not on the os but on the underlying 
cpu/architecture ...

So long, Roland
--
http://mail.python.org/mailman/listinfo/python-list


Re: Generating .pyc/.pyo from a make file

2005-02-03 Thread Roland Heiber
Roland Heiber wrote:
Tim Daneliuk wrote:
under the impression that "compiled" meant optimized byte code that
You where right, i was totally mislead by "optimized" ... ;)
Greetings, Roland
--
http://mail.python.org/mailman/listinfo/python-list


Re: python nested class

2005-07-08 Thread Roland Heiber
Vedanta Barooah wrote:
> o = mother()
> o.show()
> y=mother.child()
> y.increase(20)
> # this should print 20
> o.show()
> 
> .. is it possible somehow ???

Hi,

this should do what you want:

--- test.py
class mother:
x=0
def __init__(self):
mother.x=1
def show(self):
print mother.x
class child:
def increase(self,num):
mother.x=num

o = mother()
o.show()
y=mother.child()
y.increase(20)
# this should print 20
o.show()

---
 >pythonw -u "test.py"
1
20
 >Exit code: 0

HtH, Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Frankenstring

2005-07-13 Thread Roland Heiber
Thomas Lotze wrote:
> It's definitely no help that file-like objects are iterable; I do want
> to get a character, not a complete line, at a time.

Hi,

if i did understand what you mean, what about using mmap? Iterating over 
characters in a file like this:

# -*- coding: iso-8859-1 -*-
import os
import mmap

f = open("file.txt", "r+")
size = os.path.getsize("file.txt")
m = mmap.mmap(f.fileno(), size)

for x in m:
 print x

m.close()
f.close()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Frankenstring

2005-07-13 Thread Roland Heiber
Thomas Lotze wrote:
> AIUI (and as a little experimenting seems to confirm), you can't
> reposition an iterator over an mmap'ed file by seeking. True, you have
> both iterating by characters and seeking/telling, but the two
> functionalities don't play together.

A quick and dirty hack!? Maybe i'm missing what it is that you need ...

class MmapWithSeekAndTell(object):
 def __init__(self, m, size):
 self._m = m
 self._pos = 0
 self._size = size-1

 def __iter__(self):
 return self

 def next(self):
 if self._pos < self._size-1:
 self._pos += 1
 return self._m[self._pos]
 raise StopIteration

 def seek(self, offset, whence=0):
 if whence == 0 and 0 <= offset < self._size:
 self._pos = offset
 elif whence == 1 and 0 <= (self._pos + offset) < self._size:
 self._pos += offset
 elif whence == 2 and 0<= (self._size - offset) < self._size:
 self._pos = self._size - offset

 def tell(self):
 return self._pos


HtH, Roland

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


Re: Frankenstring

2005-07-13 Thread Roland Heiber
Roland Heiber wrote:
> class MmapWithSeekAndTell(object):
> def __init__(self, m, size):

.. where m is a mmap-object and size the filesize ... sorry.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Building a function call?

2005-07-13 Thread Roland Heiber
Francois De Serres wrote:
> Hiho,
> 
> Having a string: "dothat"
> and a tuple: (x, y)
> 1. What's the best way to build a function call like: dothat(x,y)?

Not the best (not at all) but one way:

def dothat(x,y):
   print "Called with:", x, y

c = (1,2)

locals().get("dothat")(*c)


Called with: 1 2

HtH, Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: **kwargs?

2005-07-13 Thread Roland Heiber
Francois De Serres wrote:
> All your **kwargs are belong to us.
> 
> *args is documented in the Tutorial. I reckon **kwargs represents a 
> dictionary of arguments. But I don't quite get the semantics of **x. 
> Undefined length tuple of undefined length tuples? Are there other 
> practical use cases for ** (common enough please, I wish I was, but I'm 
> not a scientist).
> 
> TIA,
> Francois
Assume d = { 'arg1':'value1','arg2':'value2' }. Then

func(**d)

is the same as:

func(arg1='value1', arg2='value2')

HtH, Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Building a function call?

2005-07-13 Thread Roland Heiber
Peter Hansen wrote:
>> locals().get("dothat")(*c)

This was just meant as a quick example, not as production-level code ;)

Even with globals(), I think its a bit odd ..., but safer than using 
eval() ...

HtH, Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Network performance

2005-08-23 Thread Roland Hedberg

Hi!

I need a fast protocol to use between a client and a server, both  
sides written i Python.


What the protocol has to accomplish is extremely simple; the client  
sends a number of lines (actually a RDF) and the server accepts or  
rejects the packet. That's all !


Now, presently I'm using Twisted and XMLRPC ( why so is a long  
history which I will not go into here) and that is a bit to slow for  
my needs. On my present setup it takes close to 200 ms to perform one  
transfer. But since that includes setting up and tearing  down the  
connection I thought I'd try doing something simpler so I wrote a  
server and a client using socket.


The client sends a number of lines (each ending with \n) and ends one  
set of lines with a empty line.
When the client sends a line with only a "." it means "I'm done close  
the connection".


Letting the client open the connection and sending a number of sets I  
was surprised to find that the performance was equal to what Twisted/ 
XMLRPC did. Around 200 ms per set, not significantly less.


So what can be done to speed up things or is Python incapable of  
supporting fast networking (just a teaser).


-- Roland 

#!/usr/bin/env python

from SocketServer import ThreadingMixIn, TCPServer, StreamRequestHandler

class UdsRequestHandler( StreamRequestHandler ):
	def handle(self):
		rdf = []
		while 1:
			tri = self.rfile.readline().strip()
			if tri == ".":
if rdf:
	print rdf
	self.wfile.write("ACCEPT")
break
			if tri:
rdf.append(tri)
			else:
print rdf
self.wfile.write("ACCEPT")
rdf = []

class UdsServer(ThreadingMixIn, TCPServer):
	allow_reuse_address = 1

if __name__ == "__main__":
	serveraddr = ('', 8765)
	srv = UdsServer(serveraddr, UdsRequestHandler)
	srv.serve_forever()

#!/usr/bin/env python

import socket

# Ending \n vital !!
RDF = """  "primula" .
  "4182" .
  "NIN:19720412-7802" .
  _:a .
_:a  "anst1" .
_:a  "100" .
_:a  "7528" .
_:a  "staff" .
"""

class UdsClient:
	def __init__(self, ip, port):
		self.ip = ip
		self.port = port
		self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
		try:
			self.s.connect((self.ip, self.port))
		except:
			print "Connection error"
			return None

	def send( self, rdf ):
		self.s.send( rdf )
		self.s.send( "\n" )

	def close( self ):
		self.s.send( ".\n" )

	def read( self ):
		r = ""
		try:
			r = self.s.recv(256)
		except:
			print "No response"
		return r

if __name__ == "__main__":
	uc = UdsClient("localhost", 8765)
	n = 10

	while n:
		uc.send( RDF )
		print "Response: %s" % uc.read()
		n -= 1

	uc.close()
	print "DONE"


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

Re: Network performance

2005-08-23 Thread Roland Hedberg

23 aug 2005 kl. 10.14 skrev Michael Sparks:

> Roland Hedberg wrote:
>
>> I was surprised to find that the performance was equal to what
>> Twisted/XMLRPC did. Around 200 ms per set, not significantly less.
>>
>
> That should tell you two things:
>* Twisted/XMLRPC is as efficient as you can hand craft. (which is a
>  good use reason for using it).

I already gathered that much :-)

>* That what you're measuring is overhead - and most likely of  
> setup.

Not necessarily! If the number of client - server queries/responses  
are large enough the effect of the setup time should be negligible.  
Or making testes with different numbers of queries you should be able  
to deduce the setup time.

> I'd measure the ping time between your two hosts.
>
> If your ping time is significantly lower -  eg you're running on
> localhost - I'd suggest you translate your code to C (and/or post
> your code),

I did the tests on localhost!

And I did post the code!

So, I made another test. I used  a server I have already written in C  
and which I know quite well how fast it is.

Using a python client I've written that talks to this server, it  
takes 0.8 s for the python client to start, connect and send
1000 queries. A C client is a bit faster but not a lot.

This is more in the order of what I'd like to have.

Hmm, not surprising this makes me suspect my python server  
implementation :-/

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


Re: Network performance

2005-08-23 Thread Roland Hedberg

23 aug 2005 kl. 15.14 skrev Sion Arrowsmith:

> Roland Hedberg  <[EMAIL PROTECTED]> wrote:
>
>
> The easy solutions are to either change:
>
>
>> def send( self, rdf ):
>> self.s.send( rdf )
>> self.s.send( "\n" )
>>
>
> to
>
> def send( self, rdf ):
> self.s.send( rdf+"\n" )
>
> or to self.s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 0) after
> connecting self.s .

I had just caught this but didn't really know way yet!

> What's going on is something like this:

Excellent description !

Thanks ever so much !!

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


Re: how to control a USB DISK?

2005-03-02 Thread Roland Heiber
Hi,
when you're under linux and a 2.6er kernel, take a look at hotplug, you 
can configure it the way that your script gets executed whenever a 
specific device is plugged in/out ...

HtH, Roland
[EMAIL PROTECTED] wrote:
when a new usb disk is plugged into the usb socket, the program will
copy a file to the usb disk, then disabled the usb disk to be pulled
out safely.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linux Multimedia System

2005-03-13 Thread Roland Heiber
Marek Franke wrote:
too. The whole project is just for fun. Afaik Freevo and/or MythTV are
written in C/C++ and don't have any support for joysticks (afaik!). And the
Freevo is pure python already ;)
Greetings, Roland
--
http://mail.python.org/mailman/listinfo/python-list


DB problem while trying to use Shelve

2004-11-30 Thread Roland Hedberg
Hi!
Want to use shelve in a program I'm writing, but I encounter a problem.
This is the tail of the traceback I get back:
  File "/sw/lib/python2.3/shelve.py", line 231, in open
return DbfilenameShelf(filename, flag, protocol, writeback, binary)
  File "/sw/lib/python2.3/shelve.py", line 212, in __init__
Shelf.__init__(self, anydbm.open(filename, flag), protocol, 
writeback, binary)
  File "/sw/lib/python2.3/anydbm.py", line 83, in open
return mod.open(file, flag, mode)
  File "/sw/lib/python2.3/dbhash.py", line 16, in open
return bsddb.hashopen(file, flag, mode)
  File "/sw/lib/python2.3/bsddb/__init__.py", line 186, in hashopen
d.set_flags(hflags)
bsddb._db.DBInvalidArgError: (22, 'Invalid argument')
Illegal instruction

This is on a MacOSX machine with python 2.3.3 and db 4.2
What's going on ?
And what can I do about it ?
-- Roland


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

Re: KeyError

2004-12-16 Thread Roland Heiber
[EMAIL PROTECTED] wrote:
Hi "R",
The only explanation I can give is that the environment varialbe REMOTE_ADDR
does not exist!  Wrap your high-level code with try and except. Example:
try:
 tablesDirectory = tablesDirectoryPrefix + os.environ['REMOTE_ADDR']
except KeyError:
  # Code to  handle the fact tht REMOT_ADDR does not exist. 
... or just replace os.environ['REMOTE_ADDR'] with 
os.environ.get('REMOTE_ADDR', 'enter_default_here') to use a default in 
case of missing REMOTE_ADDR ...

HtH, Roland
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python IDE

2004-12-16 Thread Roland Heiber
limodou wrote:
http://wiki.wookpecker.org.cn/moin.cgi/NewEdit
Try this instead:
http://wiki.woodpecker.org.cn/moin.cgi/NewEdit
   ^
SCNR, Roland ;)
--
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Snakelets 1.38 (simple-to-use web app server with dynamic pages)

2005-03-23 Thread Roland Heiber
Irmen de Jong wrote:
I'm happy to say that Snakelets 1.38 is available.
Fine thing again! Maybe someone is interested in this:
I just tried tlslite and did a dirty hack so you could use snakelets via 
SSL. See the patch below. Snip and save it, use it against 
snakeserver/server.py.

You've to place a key-pair in your server-dir 
(localhost.crt/localhost.private.key in the path). You can generate a 
self-signed certificate easily with openssl.

HtH, Roland
- SNIP AND SAVE BELOW -
961d
927a
def handshake(self, tlsConnection):
try:
tlsConnection.handshakeServer(certChain=certChain,
privateKey=privateKey,
sessionCache=sessionCache)
tlsConnection.ignoreAbruptClose = True
return True
except (SyntaxError, TLSError), error:
print "Handshake failure:", str(error)
return False
.
908c
if IS_SSL:
tlsConnection = TLSConnection(request)
if self.handshake(tlsConnection) == True:
self.RequestHandlerClass(tlsConnection, 
client_address, self)
tlsConnection.close()
else:
self.RequestHandlerClass(request, client_address, self)
.
889c
if IS_SSL:
tlsConnection = TLSConnection(request)
if self.handshake(tlsConnection) == True:
self.RequestHandlerClass(tlsConnection, 
client_address, self)
tlsConnection.close()
else:
self.RequestHandlerClass(request, client_address, self)
.
21a
IS_SSL=True
try:
from tlslite.api import *
except ImportError:
IS_SSL=False

if IS_SSL:
s = open("./localhost.crt").read()
x509 = X509()
x509.parse(s)
certChain = X509CertChain([x509])
s = open("./localhost.private.key").read()
privateKey = parsePEMKey(s, private=True)
sessionCache = SessionCache()
.
--
http://mail.python.org/mailman/listinfo/python-list


Snakelets via SSL

2005-03-23 Thread Roland Heiber
Hi,
after Irmen de Jong did another fine release with Snakelets-1.38 i just 
did a dirty hack for using snakelets with SSL-support. SSL-support is 
added through the use of tlslite from Trevor Perrin. You'll have to 
download and install it from http://trevp.net/tlslite/. Furthermore you 
need a key-pair in your serverdir, easily generetad with openssl.

Just snip the patch below and use it against snakeserver/server.py (with 
standard *nix patch).

HtH, Roland
 SNIP BELOW, SAVE AS ssl.patch 
961d
927a
def handshake(self, tlsConnection):
try:
tlsConnection.handshakeServer(certChain=certChain,
privateKey=privateKey,
sessionCache=sessionCache)
tlsConnection.ignoreAbruptClose = True
return True
except (SyntaxError, TLSError), error:
print "Handshake failure:", str(error)
return False
.
908c
if IS_SSL:
tlsConnection = TLSConnection(request)
if self.handshake(tlsConnection) == True:
self.RequestHandlerClass(tlsConnection, 
client_address, self)
tlsConnection.close()
else:
self.RequestHandlerClass(request, client_address, self)
.
889c
if IS_SSL:
tlsConnection = TLSConnection(request)
if self.handshake(tlsConnection) == True:
self.RequestHandlerClass(tlsConnection, 
client_address, self)
tlsConnection.close()
else:
self.RequestHandlerClass(request, client_address, self)
.
21a
IS_SSL=True
try:
from tlslite.api import *
except ImportError:
IS_SSL=False

if IS_SSL:
s = open("./localhost.crt").read()
x509 = X509()
x509.parse(s)
certChain = X509CertChain([x509])
s = open("./localhost.private.key").read()
privateKey = parsePEMKey(s, private=True)
sessionCache = SessionCache()
.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Snakelets via SSL

2005-03-23 Thread Roland Heiber
Irmen de Jong wrote:
However, may I ask you to re-submit the patch but this time
in the patch tracker on SF; http://sourceforge.net/tracker/?group_id=41175
because news/mail clients often mangle source code.
Hi,
I re-submited it as requested. It's just a quick hack, but maybe it's 
worth trying.

HtH, Roland
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python instances

2005-04-20 Thread Roland Heiber
Hi,
class MyClass:
list = []
you have "list" defined as a classmember, not an instancemember. So 
"list" ist defined ONCE for all instances.

Try this instead:
class MyClass:
def __init__(self):
self.list = []
[...]
and use self.list ...
HtH, Roland
--
http://mail.python.org/mailman/listinfo/python-list


Weird connection problem

2014-10-25 Thread Roland Hedberg
When I try to access a URL using requests I always get: 
socket.error: [Errno 104] Connection reset by peer

If I try to access the same URL using curl I get no error message instead I get 
the page.
The same result if I use a web browser like Safari.

But, if I use python httplib I also get Errno 104.

What gives ?

Oh, by the way it’s a HTTPS url.

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

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


Re: Weird connection problem

2014-10-25 Thread Roland Hedberg
Oh, by the way!
To make this more interesting :-/

I saw this behavior on a Linux machine (Ubuntu 14.04 LTS) using Python 2.7.6 if 
I do the same exercise 
on a Mac OS X machine also with Python 2.7.6 - no problem what so ever.

> 25 okt 2014 kl. 08:40 skrev Roland Hedberg :
> 
> When I try to access a URL using requests I always get: 
> socket.error: [Errno 104] Connection reset by peer
> 
> If I try to access the same URL using curl I get no error message instead I 
> get the page.
> The same result if I use a web browser like Safari.
> 
> But, if I use python httplib I also get Errno 104.
> 
> What gives ?
> 
> Oh, by the way it’s a HTTPS url.
> 
> — Roland
> 
> ”Being able to think like a child is an important attribute of being an 
> adult” - Eddie Izzard
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

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


Re: Weird connection problem

2014-10-25 Thread Roland Hedberg
It’s a special HTTPS url and searching further it seems to be a SNI problem 
talked about here:

http://stackoverflow.com/questions/18578439/using-requests-with-tls-doesnt-give-sni-support

> 25 okt 2014 kl. 08:48 skrev Joel Goldstick :
> 
> On Sat, Oct 25, 2014 at 9:40 AM, Roland Hedberg  wrote:
>> When I try to access a URL using requests I always get:
>> socket.error: [Errno 104] Connection reset by peer
>> 
>> If I try to access the same URL using curl I get no error message instead I 
>> get the page.
>> The same result if I use a web browser like Safari.
>> 
>> But, if I use python httplib I also get Errno 104.
>> 
>> What gives ?
>> 
>> Oh, by the way it’s a HTTPS url.
>> 
>> — Roland
> 
> Is this for any url or a specific one?
>> --
>> https://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> -- 
> Joel Goldstick
> http://joelgoldstick.com
> -- 
> https://mail.python.org/mailman/listinfo/python-list

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

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


Loading modules from files through C++

2014-05-16 Thread Roland Plüss
I'm using Python in an embedded situation. In particular I have to load
python scripts through a memory interface so regular python module
loading can not be used. I got working so far a module loader object
I've added using C++ to sys.meta_path . Now I'm totally stuck at the
finally loading step.

I've got this a C++ loader method "load_module(fullname)" which does
load the requested module script files into a null-terminated string. I
know that "load_module" has to return the module PyObject*. But I can't
get the python source in the c-string into a module PyObject*.

I've tried so far this:

#CODE#
return Py_CompileStringFlags( content.GetString(),
lookupPath.GetPathNative(), Py_file_input, NULL );
#CODE#

This did not result in a module that worked. In particular the module
script in question contains a method "create". Using "dir(module)" on
the returned value I see no "create" method and the type is a compiled
string. So I tried this:

#CODE#
loadedModule = Py_InitModule3( fullname, NULL, "Loaded module" );
if( ! PyRun_StringFlags( content.GetString(), Py_file_input,
loadedModule, loadedModule, NULL ) ){
if( PyErr_Occurred() ) PyErr_Print();
Py_INCREF( Py_None );
return Py_None;
}
Py_INCREF( loadedModule );
return loadedModule;
#CODE#

I concluded that I have to create the module myself and somehow
eval/compile the c-string into the module. For this I figured out
PyRun_StringFlags would be a suitable candidate. But doing the above I
get an error:

#CODE#
Traceback (most recent call last):
  File "", line 22, in 
ImportError: __import__ not found
#CODE#

Looks like builtins are not existing. But from the internet I read that
if code is parsed from a module both globals and locals are the module.
The above code should do this but yet it fails. I tried also with
PyEval_GetGlobals() and PyEval_GetBuiltins() I had no luck either except
the function complaining about receiving NULL values.

Can anybody help how in gods name one is supposed to create a module
from an in-memory c-string when called from within load_module (or
anywhere)?

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Loading modules from files through C++

2014-05-17 Thread Roland Plüss
That doesn't work in 2.x, doesn't it?

On 05/17/2014 01:58 PM, Stefan Behnel wrote:
> Roland Plüss, 17.05.2014 02:27:
>> I'm using Python in an embedded situation. In particular I have to load
>> python scripts through a memory interface so regular python module
>> loading can not be used. I got working so far a module loader object
>> I've added using C++ to sys.meta_path . Now I'm totally stuck at the
>> finally loading step.
>>
>> I've got this a C++ loader method "load_module(fullname)" which does
>> load the requested module script files into a null-terminated string. I
>> know that "load_module" has to return the module PyObject*. But I can't
>> get the python source in the c-string into a module PyObject*.
>> [...]
>> Can anybody help how in gods name one is supposed to create a module
>> from an in-memory c-string when called from within load_module (or
>> anywhere)?
> Looks like you want to implement a SourceLoader:
>
> https://docs.python.org/3.4/library/importlib.html#importlib.abc.SourceLoader
>
> I recommend implementing this in Python code instead of C code, though.
> Much easier. Cython can help with the integration between both.
>
> Stefan
>
>


-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Loading modules from files through C++

2014-05-17 Thread Roland Plüss
I'm willing to go to Py3 but only if the solution to the problem is
simpler than getting it fixed in Py2. So some questions first:

- does this importlib stuff you showed there apply to C++ land (I need
to fully drive it from C++ not Python code)?
- is the C++ land of Py3 similar to Py2 or totally different?

On 05/17/2014 03:26 PM, Stefan Behnel wrote:
> Roland Plüss, 17.05.2014 15:00:
>> On 05/17/2014 01:58 PM, Stefan Behnel wrote:
>>> Roland Plüss, 17.05.2014 02:27:
>>>> I'm using Python in an embedded situation. In particular I have to load
>>>> python scripts through a memory interface so regular python module
>>>> loading can not be used. I got working so far a module loader object
>>>> I've added using C++ to sys.meta_path . Now I'm totally stuck at the
>>>> finally loading step.
>>>>
>>>> I've got this a C++ loader method "load_module(fullname)" which does
>>>> load the requested module script files into a null-terminated string. I
>>>> know that "load_module" has to return the module PyObject*. But I can't
>>>> get the python source in the c-string into a module PyObject*.
>>>> [...]
>>>> Can anybody help how in gods name one is supposed to create a module
>>>> from an in-memory c-string when called from within load_module (or
>>>> anywhere)?
>>> Looks like you want to implement a SourceLoader:
>>>
>>> https://docs.python.org/3.4/library/importlib.html#importlib.abc.SourceLoader
>>>
>>> I recommend implementing this in Python code instead of C code, though.
>>> Much easier. Cython can help with the integration between both.
>> That doesn't work in 2.x, doesn't it?
> Is there a reason you have to use Py2?
>
> Anyway, PEP 302 predates Py3 by a couple of years:
>
> http://legacy.python.org/dev/peps/pep-0302/
>
> Stefan
>
>


-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Loading modules from files through C++

2014-05-17 Thread Roland Plüss

On 05/17/2014 04:01 PM, Stefan Behnel wrote:
> Hi,
>
> please avoid top-posting.
>
>
> Roland Plüss, 17.05.2014 15:49:
>> On 05/17/2014 03:26 PM, Stefan Behnel wrote:
>>> Roland Plüss, 17.05.2014 15:00:
>>>> On 05/17/2014 01:58 PM, Stefan Behnel wrote:
>>>>> Roland Plüss, 17.05.2014 02:27:
>>>>>> I'm using Python in an embedded situation. In particular I have to load
>>>>>> python scripts through a memory interface so regular python module
>>>>>> loading can not be used. I got working so far a module loader object
>>>>>> I've added using C++ to sys.meta_path . Now I'm totally stuck at the
>>>>>> finally loading step.
>>>>>>
>>>>>> I've got this a C++ loader method "load_module(fullname)" which does
>>>>>> load the requested module script files into a null-terminated string. I
>>>>>> know that "load_module" has to return the module PyObject*. But I can't
>>>>>> get the python source in the c-string into a module PyObject*.
>>>>>> [...]
>>>>>> Can anybody help how in gods name one is supposed to create a module
>>>>>> from an in-memory c-string when called from within load_module (or
>>>>>> anywhere)?
>>>>> Looks like you want to implement a SourceLoader:
>>>>>
>>>>> https://docs.python.org/3.4/library/importlib.html#importlib.abc.SourceLoader
>>>>>
>>>>> I recommend implementing this in Python code instead of C code, though.
>>>>> Much easier. Cython can help with the integration between both.
>>>> That doesn't work in 2.x, doesn't it?
>>> Is there a reason you have to use Py2?
>>>
>>> Anyway, PEP 302 predates Py3 by a couple of years:
>>>
>>> http://legacy.python.org/dev/peps/pep-0302/
>> I'm willing to go to Py3 but only if the solution to the problem is
>> simpler than getting it fixed in Py2. So some questions first:
>>
>> - does this importlib stuff you showed there apply to C++ land (I need
>> to fully drive it from C++ not Python code)?
> As I said, implementing this in Python code is much simpler than doing it
> in C/C++ code. Basically, stop where you got the C string and do the rest
> in Python. All your C code has to do is to take a module lookup request
> from your custom Python module Finder and return a byte string with the
> code. Then let your Python code wrap that in a Loader and return it to the
> import machinery.
I don't get how this is supposed to work. I'm running it as fully
embedded Python. There is no main script. The builtin modules are added
as C++ bound classes and a user made main script is loaded but not run
directly (I'm hooking into a create object). For this purpose I load the
script module using C++ code using PyImport_ImportModule(moduleName). At
this time the module loading code has to kick in already (I've added
this one by C++ too before). The problem is now that in this call I end
up in my C++ loader version where there is no Python script involved. I
came to the conclusion that I can solve this only by having the C++ end
properly load the module. I could add Python code with
PyRun_SimpleString but then I'm down to the same problem as before: how
to evaluate code so it is attached to a module or type-class? As I
understand it the problem is the same as before just pushed around a bit.
>> - is the C++ land of Py3 similar to Py2 or totally different?
> Much the same, just slight differences. As I said, Cython can help with the
> integration and would mostly cover the differences for you.
Sounds good. I'll give it a try
>
> Stefan
>
>


-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Loading modules from files through C++

2014-05-17 Thread Roland Plüss

On 05/17/2014 05:49 PM, Stefan Behnel wrote:
> Roland Plüss, 17.05.2014 17:28:
>> On 05/17/2014 04:01 PM, Stefan Behnel wrote:
>>> Roland Plüss, 17.05.2014 15:49:
>>>> On 05/17/2014 03:26 PM, Stefan Behnel wrote:
>>>>> Roland Plüss, 17.05.2014 15:00:
>>>>>> On 05/17/2014 01:58 PM, Stefan Behnel wrote:
>>>>>>> Roland Plüss, 17.05.2014 02:27:
>>>>>>>> I'm using Python in an embedded situation. In particular I have to load
>>>>>>>> python scripts through a memory interface so regular python module
>>>>>>>> loading can not be used. I got working so far a module loader object
>>>>>>>> I've added using C++ to sys.meta_path . Now I'm totally stuck at the
>>>>>>>> finally loading step.
>>>>>>>>
>>>>>>>> I've got this a C++ loader method "load_module(fullname)" which does
>>>>>>>> load the requested module script files into a null-terminated string. I
>>>>>>>> know that "load_module" has to return the module PyObject*. But I can't
>>>>>>>> get the python source in the c-string into a module PyObject*.
>>>>>>>> [...]
>>>>>>>> Can anybody help how in gods name one is supposed to create a module
>>>>>>>> from an in-memory c-string when called from within load_module (or
>>>>>>>> anywhere)?
>>>>>>> Looks like you want to implement a SourceLoader:
>>>>>>>
>>>>>>> https://docs.python.org/3.4/library/importlib.html#importlib.abc.SourceLoader
>>>>>>>
>>>>>>> I recommend implementing this in Python code instead of C code, though.
>>>>>>> Much easier. Cython can help with the integration between both.
>>>>>> That doesn't work in 2.x, doesn't it?
>>>>> Is there a reason you have to use Py2?
>>>>>
>>>>> Anyway, PEP 302 predates Py3 by a couple of years:
>>>>>
>>>>> http://legacy.python.org/dev/peps/pep-0302/
>>>> I'm willing to go to Py3 but only if the solution to the problem is
>>>> simpler than getting it fixed in Py2. So some questions first:
>>>>
>>>> - does this importlib stuff you showed there apply to C++ land (I need
>>>> to fully drive it from C++ not Python code)?
>>> As I said, implementing this in Python code is much simpler than doing it
>>> in C/C++ code. Basically, stop where you got the C string and do the rest
>>> in Python. All your C code has to do is to take a module lookup request
>>> from your custom Python module Finder and return a byte string with the
>>> code. Then let your Python code wrap that in a Loader and return it to the
>>> import machinery.
>> I don't get how this is supposed to work. I'm running it as fully
>> embedded Python. There is no main script. The builtin modules are added
>> as C++ bound classes and a user made main script is loaded but not run
>> directly (I'm hooking into a create object). For this purpose I load the
>> script module using C++ code using PyImport_ImportModule(moduleName). At
>> this time the module loading code has to kick in already (I've added
>> this one by C++ too before). The problem is now that in this call I end
>> up in my C++ loader version where there is no Python script involved. I
>> came to the conclusion that I can solve this only by having the C++ end
>> properly load the module. I could add Python code with
>> PyRun_SimpleString but then I'm down to the same problem as before: how
>> to evaluate code so it is attached to a module or type-class? As I
>> understand it the problem is the same as before just pushed around a bit.
> No, just run some Python code (using PyRun_SimpleString() if you have to)
> and let it do whatever you like. Such as, defining a Finder class and
> injecting it into the import hook. Just provide it with the entry point of
> your C++ loader as a (CFunction) object when you execute it, and then let
> it call that function at need whenever the Finder gets executed.
>
> Alternatively, compile your Python integration code with Cython and link it
> into your main program as yet another binary extension module.
>
> Stefan
>
>
I'm not using Cython so that's out of question. Concerning the injection
how would this work? From the PEP I assume it would have to look like this:

# CODE #
import sys
class VFSModuleLoader:
   def find_module(fullname, path=None):
  return self if VFS.exists( vfsPathFromFullname(fullname) ) else None
   def load_module(fullname):
  sourceCode = VFS.read( vfsPathFromFullname(fullname))
  ???
# CODE #

How does ??? work? If I use "eval" I end up with the code inside
VFSModuleLoader.load_modules as context but it should go into the global
context as an own module.

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Loading modules from files through C++

2014-05-18 Thread Roland Plüss

On 05/17/2014 07:05 PM, Stefan Behnel wrote:
> Roland Plüss, 17.05.2014 18:28:
>> On 05/17/2014 05:49 PM, Stefan Behnel wrote:
>>> Roland Plüss, 17.05.2014 17:28:
>>>> On 05/17/2014 04:01 PM, Stefan Behnel wrote:
>>>>> Roland Plüss, 17.05.2014 15:49:
>>>>>> On 05/17/2014 03:26 PM, Stefan Behnel wrote:
>>>>>>> Roland Plüss, 17.05.2014 15:00:
>>>>>>>> On 05/17/2014 01:58 PM, Stefan Behnel wrote:
>>>>>>>>> Roland Plüss, 17.05.2014 02:27:
>>>>>>>>>> I'm using Python in an embedded situation. In particular I have to 
>>>>>>>>>> load
>>>>>>>>>> python scripts through a memory interface so regular python module
>>>>>>>>>> loading can not be used. I got working so far a module loader object
>>>>>>>>>> I've added using C++ to sys.meta_path . Now I'm totally stuck at the
>>>>>>>>>> finally loading step.
>>>>>>>>>>
>>>>>>>>>> I've got this a C++ loader method "load_module(fullname)" which does
>>>>>>>>>> load the requested module script files into a null-terminated 
>>>>>>>>>> string. I
>>>>>>>>>> know that "load_module" has to return the module PyObject*. But I 
>>>>>>>>>> can't
>>>>>>>>>> get the python source in the c-string into a module PyObject*.
>>>>>>>>>> [...]
>>>>>>>>>> Can anybody help how in gods name one is supposed to create a module
>>>>>>>>>> from an in-memory c-string when called from within load_module (or
>>>>>>>>>> anywhere)?
>>>>>>>>> Looks like you want to implement a SourceLoader:
>>>>>>>>>
>>>>>>>>> https://docs.python.org/3.4/library/importlib.html#importlib.abc.SourceLoader
>>>>>>>>>
>>>>>>>>> I recommend implementing this in Python code instead of C code, 
>>>>>>>>> though.
>>>>>>>>> Much easier. Cython can help with the integration between both.
>>>>>>>> That doesn't work in 2.x, doesn't it?
>>>>>>> Is there a reason you have to use Py2?
>>>>>>>
>>>>>>> Anyway, PEP 302 predates Py3 by a couple of years:
>>>>>>>
>>>>>>> http://legacy.python.org/dev/peps/pep-0302/
>>>>>> I'm willing to go to Py3 but only if the solution to the problem is
>>>>>> simpler than getting it fixed in Py2. So some questions first:
>>>>>>
>>>>>> - does this importlib stuff you showed there apply to C++ land (I need
>>>>>> to fully drive it from C++ not Python code)?
>>>>> As I said, implementing this in Python code is much simpler than doing it
>>>>> in C/C++ code. Basically, stop where you got the C string and do the rest
>>>>> in Python. All your C code has to do is to take a module lookup request
>>>>> from your custom Python module Finder and return a byte string with the
>>>>> code. Then let your Python code wrap that in a Loader and return it to the
>>>>> import machinery.
>>>> I don't get how this is supposed to work. I'm running it as fully
>>>> embedded Python. There is no main script. The builtin modules are added
>>>> as C++ bound classes and a user made main script is loaded but not run
>>>> directly (I'm hooking into a create object). For this purpose I load the
>>>> script module using C++ code using PyImport_ImportModule(moduleName). At
>>>> this time the module loading code has to kick in already (I've added
>>>> this one by C++ too before). The problem is now that in this call I end
>>>> up in my C++ loader version where there is no Python script involved. I
>>>> came to the conclusion that I can solve this only by having the C++ end
>>>> properly load the module. I could add Python code with
>>>> PyRun_SimpleString but then I'm down to the same problem as before: how
>>>> to evaluate code so it is attached to a module or type-class? As I
>>>> understand it the problem is the same as before just pushed around a bit.
>>> No, jus

Re: Loading modules from files through C++

2014-05-19 Thread Roland Plüss

On 05/19/2014 03:40 AM, Chris Angelico wrote:
> On Mon, May 19, 2014 at 5:41 AM, Roland Plüss  wrote:
>> This exec source_code in module.__dict__ , should this not also be doable
>> with PyEval_EvalCode?
> General principle: The more code you write in Python and the less in
> C/C++, the happier and more productive you will be.
>
> Drop into Python as soon as you can, and do all the work from there.
> You won't have to worry about RAM (de)allocation, Unicode (especially
> if you use Python 3 rather than 2), integer overflow, etc, etc, etc.
> Only write lower-level code for the bits that actually demand it; and
> as Stefan has pointed out, Cython is a great help there.
>
> (Which reminds me. I still need some "excuse project" to justify my
> learning Cython. It's good-looking tech but everything I can imagine
> writing seems to already exist.)
>
> ChrisA
Cython is out of question since link time dependencies has to be kept at
an utmost minimum. An concerning doing things from python code it's
simply not possible (or I don't know how this should work). Python is
fully embedded including any file actions (virtual file system). The
launch comes from C++ land. No interpreter is involved. The main work is
done outside Python and only dropped in python to do higher logic. This
also means the very first python code run requires a C++ script file
loading before this first code can ever run. I can't run code that
doesn't yet exist in the reach of the python VM. That's the main
problem. This is why I need to figure out how to push that first code
into Python in a way I can construct an object (from a predefined
class). From there on all calls into Python land are solely done trough
function calls on this one object obtained in the beginning. I see no
other way to handle the problem.

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Loading modules from files through C++

2014-05-20 Thread Roland Plüss

On 05/19/2014 03:40 AM, Chris Angelico wrote:
> On Mon, May 19, 2014 at 5:41 AM, Roland Plüss  wrote:
>> This exec source_code in module.__dict__ , should this not also be doable
>> with PyEval_EvalCode?
> General principle: The more code you write in Python and the less in
> C/C++, the happier and more productive you will be.
>
> Drop into Python as soon as you can, and do all the work from there.
> You won't have to worry about RAM (de)allocation, Unicode (especially
> if you use Python 3 rather than 2), integer overflow, etc, etc, etc.
> Only write lower-level code for the bits that actually demand it; and
> as Stefan has pointed out, Cython is a great help there.
>
> (Which reminds me. I still need some "excuse project" to justify my
> learning Cython. It's good-looking tech but everything I can imagine
> writing seems to already exist.)
>
> ChrisA
Figured out the solution to the problem. Inspected some python imported
files to check out the module is actually constructed. Turns out
something has been missing. In general I needed this to get it working:

PyObject * const loadedModule = Py_InitModule3( fullname, NULL, "Loaded
module" );
PyObject * const moduleDict = PyModule_GetDict( loadedModule ); //
borrowed reference
PyDict_SetItemString( moduleDict, "__builtins__", PyEval_GetBuiltins() );
PyRun_StringFlags( fileContent, Py_file_input, moduleDict, moduleDict,
NULL );

The important part are the last two lines. An important module is
lacking the __builtins__ dictionary member so I had to add it.
Furthermore I had to call the string runner with moduleDict both as
global and local dictionary. With that change the virtual script is
properly loaded and working as it should.

Hopefully this works also in Py3 should I switch some time later. But I
guess it should seeing how simple the import now became.

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Loading modules from files through C++

2014-05-21 Thread Roland Plüss

On 05/20/2014 07:55 PM, Chris Angelico wrote:
> On Wed, May 21, 2014 at 3:17 AM, Roland Plüss  wrote:
>> The important part are the last two lines. An important module is
>> lacking the __builtins__ dictionary member so I had to add it.
>>
>> Hopefully this works also in Py3 should I switch some time later. But I
>> guess it should seeing how simple the import now became.
> An interesting omission, I'm a little surprised at that. But if your
> switch to Py3 is a serious (or even half-serious) possibility, I
> recommend tossing a quick comment against that line of code. Check to
> see if you actually need it, and if you still do, see if there's a
> change there. The module has been renamed (from __builtin__ to
> builtins, although the global reference to it is still __builtins__),
> so you may need to adjust something there, too. But mainly, see if you
> can drop that line of code in Py3.
That has been a huge mess. Py3 totally messes things up there. Even the
most simply code doesn't work. Take this example:

# CODE #
PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, NULL, NULL, 0, NULL,
NULL, NULL, NULL, NULL };

moduledef.m_name = "MyModule";
moduledef.m_doc = "MyModule";
pModule = PyModule_Create( &moduledef );
PyState_AddModule( pModule, &moduledef );

PyRun_SimpleString( "print( globals() )\n" );
PyRun_SimpleString( "import MyModule\nprint( globals() )\n" );
# CODE #

yields this:

{'__name__': '__main__', '__builtins__': ,
'__doc__': None, '__loader__': , '__package__': None}
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named 'MyModule'

This worked in Py2 and in Py3 created modules are invisible breaking
anything. Why is there no sane documentation providing a working example
for something that elementary. Any ideas what Py3 requires that it does
not write into the docs as one should do?

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Loading modules from files through C++

2014-05-24 Thread Roland Plüss
> # CODE #
> PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, NULL, NULL, 0, NULL,
> NULL, NULL, NULL, NULL };
>
> moduledef.m_name = "MyModule";
> moduledef.m_doc = "MyModule";
> pModule = PyModule_Create( &moduledef );
> PyState_AddModule( pModule, &moduledef );
>
> PyRun_SimpleString( "print( globals() )\n" );
> PyRun_SimpleString( "import MyModule\nprint( globals() )\n" );
> # CODE #
>
> yields this:
>
> {'__name__': '__main__', '__builtins__': ,
> '__doc__': None, '__loader__':  '_frozen_importlib.BuiltinImporter'>, '__package__': None}
> Traceback (most recent call last):
>   File "", line 1, in 
> ImportError: No module named 'MyModule'
>
Anybody got an idea? Somebody has a working example where he create a
valid module from C++? It's just a namespace not even yet script sources
applied.

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Loading modules from files through C++

2014-06-03 Thread Roland Plüss
I came now a bit further with Python 3 but I'm hitting a total
road-block right now with the importer in C++ which worked in Py2 but is
now totally broken in Py3. In general I've got a C++ class based module
which has two methods:

{ "find_module", ( PyCFunction )spModuleModuleLoader::cfFindModule,
METH_VARARGS, "Retrieve finder for a path." },
{ "load_module", ( PyCFunction )spModuleModuleLoader::cfLoadModule,
METH_VARARGS, "Load module for a path." },

An instance of this object is added to sys.meta_path

This is the same as with Py2. But in Py3 I get now this strange error
and everything breaks:

Traceback (most recent call last):
  File "", line 1565, in _find_and_load
  File "", line 1523, in
_find_and_load_unlocked
  File "", line 1477, in _find_module
SystemError: Bad call flags in PyCFunction_Call. METH_OLDARGS is no
longer supported!

This happenes whenever I try to import something. I never used
METH_OLDARGS anywhere so I assume something is broken inside python.
Maybe wrong error code for not finding some method or wrong arguments? I
can't find any useful documentation on what could cause this problem.

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: pyflakes best practices?

2014-06-04 Thread Roland Koebler
Hi,

I would recommend to use Pylint (http://www.pylint.org/) in addition
to pyflakes. Pylint is much more powerful than pyflakes, and largely
configurable.

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


Re: What's the best way to extract 2 values from a CSV file from each row systematically?

2013-09-27 Thread Roland Mueller
Hello,

2013/9/24 Alex Lee 

> Thanks for the help guys! I'll definitely read up on the csv module
> documentation.
>
> Tim, that's incredibly helpful, thanks a lot! :) My CSV file doesn't have
> headers, but I'm sure I can just as easily add it in manually.
>

a better way to provide column headers is to use the fieldname parameter
when creating the CSV reader. fieldnames is a string array that should
match the number of columns in the csv file:

*class *csv.DictReader(*csvfile*, *fieldnames=None*, *restkey=None*, *
restval=None*, *dialect='excel'*, **args*, ***kwds*)

BR,
Roland

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


module_traverse segfault

2014-06-16 Thread Roland Plüss
I'm still trying to get Python3 embedded working. The main problem I'm
hitting now is an unexplainable segfault:

Program received signal SIGSEGV, Segmentation fault.
0x7fffc958 in ?? ()
(gdb) bt
#0  0x7fffc958 in ?? ()
#1  0x7fffdd2f9ed0 in module_traverse () from
/usr/lib64/libpython3.3.so.1.0
#2  0x7fffdd396cc7 in collect_with_callback () from
/usr/lib64/libpython3.3.so.1.0
#3  0x7fffdd397738 in PyGC_Collect () from
/usr/lib64/libpython3.3.so.1.0
#4  0x7fffdd3808ca in Py_Finalize () from /usr/lib64/libpython3.3.so.1.0

It seems to be linked with GC somehow. I don't know how to trace this
problem into the python library. Anybody has seen a similar problem and
knows where I could start looking for the problem? Is there a way to
debug this into python library? I'm using GDB but I'm getting no useful
information out of it.

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: module_traverse segfault

2014-06-16 Thread Roland Plüss

>> Program received signal SIGSEGV, Segmentation fault.
>> 0x7fffc958 in ?? ()
>> (gdb) bt
>> #0  0x7fffc958 in ?? ()
>> #1  0x7fffdd2f9ed0 in module_traverse () from
>> /usr/lib64/libpython3.3.so.1.0
>> #2  0x7fffdd396cc7 in collect_with_callback () from
>> /usr/lib64/libpython3.3.so.1.0
>> #3  0x7fffdd397738 in PyGC_Collect () from
>> /usr/lib64/libpython3.3.so.1.0
>> #4  0x7fffdd3808ca in Py_Finalize () from
>> /usr/lib64/libpython3.3.so.1.0
>
> Can you run (some of) the same code in a normal interpreter?  If so,
> does it raise an uncaught exception?
Can't, it's fully embedded inside the application and fully loads
C-classes from C-land memory without any files or libraries.
> Was the program supposed to be shutting down when this occurred? Or
> did this happen during 'normal' execution?
Shutting down. It happens though also if I trigger an error for example
using
if( ! PyArg_ParseTuple( args, "", &x1, &y1, &x2, &y2 ) ){
return NULL;
}
and proving for example floats instead of integer.

So it happens as soon as the GC is involved. Could it be a tp_* function
that is marked optional is actually not optional at all? According to
the documentation I need only this:
@@
typeObject = new sObjectType;

memset( typeObject, 0, sizeof( sObjectType ) );
typeObject->tp_name = "Dragengine.Gui.Graphics.Graphics";
typeObject->tp_basicsize = sizeof( sObjectData );
typeObject->tp_flags = Py_TPFLAGS_DEFAULT;
typeObject->tp_doc = "Graphics Object";
typeObject->tp_methods = vMethodsInstance;
typeObject->tp_new = ( newfunc )spModuleGraphics::cfNew;
typeObject->tp_init = ( initproc )spModuleGraphics::cfInit;
typeObject->tp_dealloc = ( destructor )spModuleGraphics::cfDealoc;
typeObject->ownerClass = this;

PyType_Ready( typeObject );

Py_INCREF( typeObject );
PyModule_AddObject( pPyModule, "Graphics", ( PyObject* )typeObject );
@@

where sObject is:
@@
struct sObjectType : public PyTypeObject{
spModuleGraphics *ownerClass;
};
@@

but maybe the documentation is missing something there?

>
> In any case, switch to 3.4.1 or later for improved gc and finalization.
>
Not in GenToo yet as far as I know.

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Loading modules from files through C++

2014-07-08 Thread Roland Plüss

> On Wed, May 21, 2014 at 3:17 AM, Roland Plüss  wrote:
>> The important part are the last two lines. An important module is
>> lacking the __builtins__ dictionary member so I had to add it.
>>
>> Hopefully this works also in Py3 should I switch some time later. But I
>> guess it should seeing how simple the import now became.
> An interesting omission, I'm a little surprised at that. But if your
> switch to Py3 is a serious (or even half-serious) possibility, I
> recommend tossing a quick comment against that line of code. Check to
> see if you actually need it, and if you still do, see if there's a
> change there. The module has been renamed (from __builtin__ to
> builtins, although the global reference to it is still __builtins__),
> so you may need to adjust something there, too. But mainly, see if you
> can drop that line of code in Py3.
>
>> Furthermore I had to call the string runner with moduleDict both as
>> global and local dictionary. With that change the virtual script is
>> properly loaded and working as it should.
> This part does make sense, though. Normally, module-level code runs
> with the same locals and globals:
>
>>>> locals() is globals()
> True
>
> And that doesn't change in Py3, so I would expect that your C++ code
> also won't change.
>
> ChrisA
I changed now a couple of things and it looks like the segfault happens
somewhere inside PyErr_Print(). So I've programatically loaded a script
into a new module using load_module and a module created by hand. If now
in this script I've loaded an error is located the crash happens. The
situation thus looks like this. Let's say Script.py is my script. The
call chain would be something like this for example:

someNativeClass.cpp : someFunction()
Script.py : def run()
main.cpp

The error is due to a
if( ! PyArg_ParseTuple( args, "", &x1, &y1, &x2, &y2 ) ){
return NULL;
}

Thus the PyErr_Print() would have to print in the stack trace two C++
class, one in the someNativeClass and one generated for the loaded
Script.py . It looks like Python 3 expects something from the C++
classes not written in the documentation.

What could a C++ class require to be working being included in a stack
trace like this?

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools



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


Re: Which OAuth library?

2014-09-02 Thread Roland Hedberg

14 aug 2014 kl. 19:54 skrev Chris “Kwpolska” Warrick :

> On 14 August 2014 18:51 "Richard Prosser"  wrote:
> >
> > I "need" one for use with Flask, as I don't really have time to implement 
> > my own.
> 
> You should not implement things on your own if there are existing and same 
> implementations.
> 
> > Initially this will be for the "Two-Legged" case but I may well have to 
> > support the "Three-Legged" version later on. "Open ID Connect" may also be 
> > an option eventually.
> >
> > The basic idea is to provide an authorization/authentication service in a 
> > fairly conventional manner. My boss has told me to use OAuth, probably 
> > because he has experience with it and also to allow for third-party 
> > transactions.
> >
> > However it is not clear to me how I should decide between the various 
> > packages on offer. Any advice from experienced/informed users would be very 
> > welcome.

I’m responsible for one implementation (pyoidc) that is primary a OpenID 
connect implementation.
But since OpenID Connect is a profile of OAuth2 it will work in an OAuth2 
context too.

This implementation is special in that it’s the de facto reference 
implementation for OpenID Connect.
It’s that, due to the fact that I’ve built the OpenID Connect test suit which 
most today available
OpenID Provider implementations have verified themselves against.

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

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


Weird SSL problem

2014-09-29 Thread Roland Hedberg
Hi!

I’m trying to access 
https://stsadweb.one.microsoft.com/adfs/.well-known/openid-configuration

Doing it the simplest way I get the following:

>>> import urllib
>>> f = 
>>> urllib.urlopen("https://stsadweb.one.microsoft.com/adfs/.well-known/openid-configuration";)
Traceback (most recent call last):
  File "", line 1, in 
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", 
line 87, in urlopen
return opener.open(url)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", 
line 208, in open
return getattr(self, name)(url)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", 
line 437, in open_https
h.endheaders(data)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", 
line 969, in endheaders
self._send_output(message_body)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", 
line 829, in _send_output
self.send(msg)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", 
line 791, in send
self.connect()
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", 
line 1176, in connect
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 
387, in wrap_socket
ciphers=ciphers)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 
143, in __init__
self.do_handshake()
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 
305, in do_handshake
self._sslobj.do_handshake()
IOError: [Errno socket error] [Errno 54] Connection reset by peer
>>> import ssl
>>> ssl.OPENSSL_VERSION
’OpenSSL 0.9.8za 5 Jun 2014'

Now, using Safari, or curl for that matter, from the same machine works without 
a hitch.

The URL above is also the only URL I’ve encountered this problem with.

Anyone got an idea ?

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

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


Re: Weird SSL problem

2014-10-01 Thread Roland Hedberg

30 sep 2014 kl. 00:55 skrev Ned Deily :

> In article ,
> Roland Hedberg  wrote:
> 
>> Hi!
>> 
>> I¹m trying to access 
>> https://stsadweb.one.microsoft.com/adfs/.well-known/openid-configuration
>> 
>> Doing it the simplest way I get the following:
>> 
>>>>> import urllib
>>>>> f = 
>>>>> urllib.urlopen("https://stsadweb.one.microsoft.com/adfs/.well-known/openid
>>>>> -configuration")
>> Traceback (most recent call last):
>>  File "", line 1, in 
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py",
>>   line 87, in urlopen
>>return opener.open(url)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py",
>>   line 208, in open
>>return getattr(self, name)(url)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py",
>>   line 437, in open_https
>>h.endheaders(data)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py"
>>  , line 969, in endheaders
>>self._send_output(message_body)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py"
>>  , line 829, in _send_output
>>self.send(msg)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py"
>>  , line 791, in send
>>self.connect()
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py"
>>  , line 1176, in connect
>>self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", 
>>  line 387, in wrap_socket
>>ciphers=ciphers)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", 
>>  line 143, in __init__
>>self.do_handshake()
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", 
>>  line 305, in do_handshake
>>self._sslobj.do_handshake()
>> IOError: [Errno socket error] [Errno 54] Connection reset by peer
>>>>> import ssl
>>>>> ssl.OPENSSL_VERSION
>> ¹OpenSSL 0.9.8za 5 Jun 2014'
>> 
>> Now, using Safari, or curl for that matter, from the same machine works 
>> without a hitch.
>> 
>> The URL above is also the only URL I¹ve encountered this problem with.
>> 
>> Anyone got an idea ?
> 
> I believe the problem is that the connection is protected by a 
> multi-hostname server certificate and Python 2's urllib (and underlying 
> httplib and ssl modules) do not support SNI extensions to TLS.  The 
> request above works fine with Python 3 (which has supported client-side 
> SNI since Python 3.2).  See http://bugs.python.org/issue5639 for more 
> discussion of the matter.  If Python 3 is not an option for you, the 
> requests package available via PyPI should help.

You’re absolutely correct in that it’s a SNI problem.
Python 3 is not an option and I was already using requests obviously missing 
something.
Ah, had to install some extra libraries.

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

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


Support for Galois/Counter Mode (GCM) ?

2011-11-01 Thread Roland Hedberg
Hi !

Is there a crypto library for Python that has support for GCM ??

-- Roland



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


Re: Is there a graphical GUI builder?

2013-02-19 Thread Roland Koebler
Hi,

> I'm new to Python and only a hobbyist programmer.  A long time ago I used 
> Microsoft's Visual Basic which had a nice (graphical) facility for creating 
> GUIs which was part of the development environment.  I'm wondering if there's 
> a utility for Python to build GUIs.
yes, there are several, depending on the GUI-toolkit (GTK+, Qt, ...)
you want to use.

But I would recommend Glade and the GTK+-Toolkit. Simply search
for Glade, GTK and Python in your favourite search engine, and you
will find several tutorials.

regards,
Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a graphical GUI builder?

2013-02-20 Thread Roland Koebler
Hi,

> That way of building a window tends to produce programs that port
> badly to other systems.
hmm, I don't think so. I've build several applications in C + GTK/Glade and
Python + GTK/Glade, which easily run on Linux and Windows without any GUI
changes.

> playing with Java applets introduced
> the novel and somewhat strange idea that your window should be built
> using rules and layouts, to avoid problems with button sizes, fonts,
> etc, etc.
Do you know the container-concept of GTK+ and Glade?

In many GUI-builders, you set your widgets to fixed positions (e.g. a text
field at x16/y16 with 100*30 pixels, a button at x16/y50 with 100*50 pixels
etc.). This is *bad*, and causes all kinds of problems with e.g. different
window- or font-sizes, like widgets outside of the visible window, text
running over the border of a widget or being cut at the edge of the widget
etc.

But: GTK+ has a wonderful concept of "containers" [*]. You normally don't
set widgets to fixed positions -- instead, you add layout tables (or
vertical boxes or horizontal boxes or ...), and essentially define
that some widgets should be above each other, side by side or in a grid
layout, so you more or less define the layout logically. The real size
and position of the widgets is dynamically calculated by GTK+, so they
always have the right size, and different font sizes, different window
sizes, etc. are not a problem anymore [q]. And Glade (the GTK+ GUI builder)
works exactly that way.


[*] Besides, the container-concept also allows such nice things like
putting anything inside a button (e.g. 2 images and a label), or inside
a notebook tab etc. pp.

[q] In Qt, it's also possible to generate such flexible layouts. But
it's unfortunately not the default way in Qt, and the Qt designer only
supports it rudimentarily, and in a much less obvious way. And Qt does
not have such a "container"-concept, where many widgets (e.g. buttons,
notebook registers etc.) contain other widgets.

> You have to think about your window differently - think about what
> you're putting where, rather than going visually "that looks about
> right" - but the reward is that it'll look right no matter where you
> run your app.
Yes, that's also true for GTK+/Glade.
But you have the choice to either build you GUI graphically with your
mouse, or textually in your editor -- or mix both.


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


Re: Is there a graphical GUI builder?

2013-02-20 Thread Roland Koebler
Hi,

> I agree that on Linux GTK is pretty darn slick.  I use it for all my
> little GUIs.  But on Windows, GTK, particularly under python, isn't
> quite as easy to get running.
installing GTK+ 2.x should be easy, since there are all-in-one-installers
for windows on http://www.gtk.org (for GTK+) and http://www.pygtk.org
(for Python+GTK+).

Installing GTK+ 3.x on windows is currently a bit more complicated, but this
situation should improve soon.

> I think if the OP is on windows (which it seems like he is) then Qt with
> PySide (using either QML or QtDesigner to manipulate ui files) is an
> excellent choice.
I never was happy with QtDesigner -- I always struggled *a lot*
(in contrast to Glade, where most things worked like a charm).

I not even achieved to rename the tab of a notebook (?!) or to create
a button with an icon above the text, or a grid layout without a fixed
layout. It even seems that the QtDesigner doesn't even provide standard-
icons (e.g. for open, close, exit etc.) or a file dialog. Am I doing
something fundamentally wrong in QtDesigner, or is QtDesigner really
that bad?

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


Re: working with csv module in python

2013-02-20 Thread Roland Koebler
Hi,

On Wed, Feb 20, 2013 at 10:50:54AM +0100, inshu chauhan wrote:
> I have 10 simple text files with 3 columns x,y,z delimited by "space". I am
> trying to combine these 10 files to get a single text file.
> 
> Eg. of data in 10 files is
> 299 446 2
Do you only want to concat the files, or do you want to parse/mangle
them?

If you only want to concat the files, I would use some shell-tools,
like "cat" on Linux or "copy" on Windows, so

copy C:\Users\inshu.chauhan\Desktop\ForModel_600\*.arff 
C:\Users\inshu.chauhan\Desktop\test2.arff

should do it.

> Can in some some way I set delimiter to NULL as the prog gives me error if
> I do so.
Of course -- a CSV without a delimiter doesn't make any sense.

> I dont why there is space between the attribute of first column in
> reading and there is space between every row too..
Because there's a "split()" missing in your code. You currently tell the
CSV-writer to write the columns 2,9,9, , , ,4,4,6, , , ,2 as
space-separated CSV. So, try something like
rows = [r.split() for r in open(f, "r").readlines()]

> Or can I merge these text files without using csv module , directly in
> python ?
If you don't need to parse/mangle the contents, you don't need the csv
module. Simple open the resulting file for writing, and then read out
the source files and write their contents into the resulting file.


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


Re: Is there a graphical GUI builder?

2013-02-20 Thread Roland Koebler
Hi,

> > [q] In Qt, it's also possible to generate such flexible layouts. But
> > it's unfortunately not the default way in Qt, and the Qt designer only
> > supports it rudimentarily, and in a much less obvious way. And Qt does
> > not have such a "container"-concept, where many widgets (e.g. buttons,
> > notebook registers etc.) contain other widgets.
> 
> ...
> 
> I'm sorry but all of that is completely wrong. Using layouts that
> automatically adapt to fonts, the size of widgets being laid out etc. is
> the default way. You could use explicit sizes and positions if you wanted
> to, but that would be bad for the reasons you gave.
hmm, interesting, but then Qt Designer is a total mess.

In Qt Designer (at least in 4.x), the default is a fixed layout, where
I have to position the widgets at precise pixel-positions and have to
define the size in pixels. And I cannot remove the default fixed layout
without modifying the .ui-file in a text editor!

> Qt does have a
> container concept - that's what a QWidget is (the base class of all
> widgets).
A container concept like in GTK+ is *much* more than having a base
widget where all widgets are derived from, or having layout boxes.
It means that most widgets are containers, like buttons, notebook
labels, checkboxes, radio buttons, scrollbar-windows etc.
And I haven't seen anything like this in Qt (or: in Qt Designer).


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


Re: Is there a graphical GUI builder?

2013-02-20 Thread Roland Koebler
Hi Phil,

> > In Qt Designer (at least in 4.x), the default is a fixed layout, where
> > I have to position the widgets at precise pixel-positions and have to
> > define the size in pixels. And I cannot remove the default fixed layout
> > without modifying the .ui-file in a text editor!
> 
> I'm sorry but that is just wrong.
I now found out, that I can indeed change the default fixed layout by
clicking onto the empty main window or by selecting the main window in
the Object Inspector and then clicking on a layout-icon on the toolbar.
Thanks.

I hope that most developers who use Qt use this feature, since the fixed
layout is the default in Qt designer and it's not really obvious how to
change this for beginners...


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


Re: Is there a graphical GUI builder?

2013-02-21 Thread Roland Koebler
Hi,

> How so? It's LGPL. You can't get much freer than that.
you can -- MIT/BSD/public domain etc. provide much more freedom to the
developer. (And I prefer freedom for the developer over the guarantee
(freedom or restriction -- call it as you wish) that nobody may lock
down a copy of the sourcecode.)

In addition, using the LGPL-version of Qt for proprietary/commercial
software may be risky, because if you violate the LGPL by accident
(e.g. because of some formal issue), the Qt-owner may demand
compensation, e.g. the license-fee for the commercial Qt version
for the last couple of years...

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


Re: Is there a graphical GUI builder?

2013-02-21 Thread Roland Koebler
Hi,

> The situation has not substantively changed, but your description of
> it is not really accurate. There was and still is a "commercial
> license" which allows for completely proprietary development without
> needing to allow end users to relink the application against
> user-supplied versions of Qt. The free license is the LGPL,
that's not quite correct; things have changed 2009 and Qt now has three
different licenses:
- commercial licence
- GPL (+GPL exceptions)
- LGPL + Qt LGPL Exception (because of inline-functions/templates)
  since Qt 4.5

best regards
Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can't catch CTRL-C when SimpleXMLRPCServer running ?

2013-02-22 Thread Roland Koebler
Hi,

> I would like to stop the script running in response to a CTRL-C.
how about "KeyboardInterrupt"?

try:
...
except KeyboardInterrupt:
print "You pressed Ctrl+C"


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


Re: Embedding a for inside an html template for substitution

2013-03-05 Thread Roland Koebler
Hi,

On Mon, Mar 04, 2013 at 09:22:38AM -0800, Ferrous Cranus wrote:
> can i just put the liens you provided me inside "files.html" and thwy
> will work?
> 
> Thats pure pythjon code!
There are several template-engines where you more or less include
python-code into the template, e.g.: empy, mako, pyratemp

On Mon, Mar 04, 2013 at 08:56:14PM -0700, Michael Torrie wrote:
> Check out that link in my previous e-mail to a list of python templating
> engines.  Choose one and try it.  No I cannot provide any example code.
Here's an example for pyratemp (where I'm the author ;)):

files.html:


@!title!@

@!e!@



files.py:

import pyratemp
t = pyratemp.Template(filename="files.html")
result = t(title="title ...", mylist=["entry 1", "entry 2", "entry 3"])
print result.encode("ascii", 'xmlcharrefreplace')


On Mon, Mar 04, 2013 at 08:56:14PM -0700, Michael Torrie wrote:
> I do recommend you at least take a look at Django.  It may be overkill
> for your needs.  It does contain a templating engine,
Last time I tried, the template-engine of Django did not work on its
own. If you need Django-like templates without Django, you can use Jinja.
And if you need a real sandbox (so that you can use untrusted templates),
I would recommend Jinja.


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


Re: Inserting-embedding some html data at the end of a .py file

2013-03-06 Thread Roland Koebler
Hi,

On Tue, Mar 05, 2013 at 09:39:19AM -0800, Νίκος Γκρ33κ wrote:
> But i did, I just tried this:
> 
>   # open html template
>   if htmlpage.endswith('.html'):
>   f = open( "/home/nikos/public_html/" + htmlpage )
> 
>   htmldata = f.read()
>   counter =   '''  href="mailto:supp...@superhost.gr";>  
>cellpadding=2 bgcolor=black>
>color=lime>Αριθμός Επισκεπτών
>href="http://superhost.gr/?show=stats";> %d 
>   ''' % data[0]
>   else:
>   f = open( "/home/nikos/public_html/cgi-bin/" + htmlpage )
>   
>   htmldata = f.read()
>   counter =   '''
>   print ''' href="mailto:supp...@superhost.gr";>  
>cellpadding=2 bgcolor=black>
>color=lime>Αριθμός Επισκεπτών
>href="http://superhost.gr/?show=stats";> %d 
>   '''
>   ''' % data[0]   
>   
>   template = htmldata + counter
>   print ( template )
> =
> 
> But still doens't embed correctly the additional html data at the end of the 
> .py files.
> 
> Do you have an idea?
as someone said: You're doing it the wrong way.

I would recommend to use a template-engine; then you can put the
complete html-design (and some design-control-structures) into
the template (and *not* into the cgi) and fill data into the
template with a python-script.


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


Re: Inserting-embedding some html data at the end of a .py file

2013-03-07 Thread Roland Koebler
Hi,

> but now iam receiving this error concering except:
> 
> ni...@superhost.gr [~/www/cgi-bin]# /usr/bin/python3 metrites.py 
>   File "metrites.py", line 88
> except MySQLdb.Error, e:
> ^
> SyntaxError: invalid syntax
> ni...@superhost.gr [~/www/cgi-bin]# 
> 
> which used to work ok in v2.6.6
> 
> can you help?
Python 3 introduced some Python 2-incompatible changes.
So, please read: http://docs.python.org/release/3.0.1/whatsnew/3.0.html

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


Re: round off to two decimal & return float

2013-03-30 Thread Roland Mueller
Hello,

2013/3/30 ஆமாச்சு 

> Consider the scenario,
>
> >> a = 10
> >> "{0:.2f}".format(a)
> '10.00'
>
> This returns a string 10.00. But what is the preferred method to retain
> 10.0 (float) as 10.00 (float)?
>
> I assume you have a numeric value a and want to have a float with 2
decimals. This can be achieved with the function round():

>>> a = 10


>>> type(a)


>>> a = round(10,2)



>>> type (a)





>>> a


10.0

BR,
Roland


> I am trying to assign the value to a cell of a spreadsheet, using
> python-xlwt. I would like to have 10.00 as the value that is right
> aligned. With text it is left aligned.
>
> --
>
> Sri Ramadoss M
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: round off to two decimal & return float

2013-03-30 Thread Roland Mueller
2013/3/30 Roland Mueller 

> Hello,
>
> 2013/3/30 ஆமாச்சு 
>
>> Consider the scenario,
>>
>> >> a = 10
>> >> "{0:.2f}".format(a)
>> '10.00'
>>
>> This returns a string 10.00. But what is the preferred method to retain
>> 10.0 (float) as 10.00 (float)?
>>
>> I assume you have a numeric value a and want to have a float with 2
> decimals. This can be achieved with the function round():
>
> Of course the representation of the float having a given number of
 decimals printed out is always a string. The float format does not imply
any formatting and Python's default formatting when a float is printed out
is to print as decimals as needed. Thus, using round() fives you a float
with maximal two decimals.

>>> a = 9.202
>>> a = round(a,2)
>>> a
9.2

>>> a = 9.222
>>> a = round(a,2)
>>> a
9.22


>>> a = 10
>
>
> >>> type(a)
> 
>
> >>> a = round(10,2)
>
>
>
> >>> type (a)
>
>
> 
>
>
> >>> a
>
>
> 10.0
>
> BR,
> Roland
>
>
>> I am trying to assign the value to a cell of a spreadsheet, using
>> python-xlwt. I would like to have 10.00 as the value that is right
>> aligned. With text it is left aligned.
>>
>> --
>>
>> Sri Ramadoss M
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I hate you all

2013-04-07 Thread Roland Koebler
Hi,

> Well all previous (python 2) code is meant to work for a tab size of
> 8.
yes, but even in Python 2, mixing spaces and tabs is considered bad
style and should be avoided. And code-checkers like pylint (which I
can recommend to everyone) create a warning.

> You may call this "categorically wrong", but it has been there a
> long while, is is still in use, and it sticks to the default.
As I said, mixing tabs and spaces for indentation was *always* a bad
idea, and is discouraged also in Python 2.

> Spaces-only can achieve compatibility between different people
> settings for formatted text like source code. But so does a common
> default for the tab size,
But there's no such thing as "default tab size". Configuring the
tab-size is quite common among programmers.


But why do you insist on using tabs at all? The best way -- in my
opinion -- is to use the tab- and backspace-key for indentation, and
let the editor convert it to spaces. (And use some tool to convert
all tabs in the old code.)

I don't see *any* advantage of mixed spaces and tabs, but quite some
disadvantages/problems.

> What I would expect is some option in python to make tabs work the
> way they used to. I want a choice for me to preserve my settings,
> the same way you want to preserve yours.
>
> What I want should not be much to ask, since this is how python 2
> used to do things.
> 
> I admit such a '--fixed-tabs' option, that will make tab stops be 8
> columns apart, and allow any number of spaces like in python 2,
> makes the code I write dependent on that option.
There's no need to add this to Python 3, since you already have what
you want. Simply use:

expand yourscript.py | python3


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


Re: Encoding NaN in JSON

2013-04-17 Thread Roland Koebler
Hi,

> > Easiest way is probably to transform your object before you try to write
> Yeah, that's what I ended up doing. Wondered if there's a better way ...
yes, there is: subclass+extend the JSON-encoder, see pydoc json.

e.g.:
class JsonNanEncoder(json.JSONEncoder):
def default(self, obj):
if some-check-if-obj-is-NaN:
return 'NaN'
return json.JSONEncoder.default(self, obj)

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


Re: Encoding NaN in JSON

2013-04-17 Thread Roland Koebler
Hi,

> > yes, there is: subclass+extend the JSON-encoder, see pydoc json.
> Please read the original post before answering. What you suggested does not 
> work since NaN is of float type.
ok, right, default does not work this way.
But I would still suggest to extend the JSON-encoder, since that is
quite simple (see sourcecode of JSON module); as a quickhack, you
could even monkey patch json.encoder.floatstr with a wrapper which
returns "N/A" for NaN. (I've tested it: It works.)

But: If you only need NaN and inf, and are ok with 'NaN' instead of 'N/A',
you can simply use the json module. See pydoc json:

If allow_nan is True, then NaN, Infinity, and -Infinity will be
encoded as such.  This behavior is not JSON specification compliant,
but is consistent with most JavaScript based encoders and decoders.
Otherwise, it will be a ValueError to encode such floats.

>>> import json
>>> json.dumps(float('NaN'))
'NaN'
>>> json.dumps(float('inf'))
'Infinity'


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


Re: Encoding NaN in JSON

2013-04-18 Thread Roland Koebler
On Thu, Apr 18, 2013 at 11:46:37AM +1000, Chris Angelico wrote:
> Wait... you can do that? It's internal to iterencode, at least in
> Python 3.3 and 2.7 that I'm looking at here.
In Python 2.6 it wasn't internal to iterencode; in Python 2.7 and 3.x
you probably would have to monkey-patch iterencode. (In addition, patching
floatstr alone wouldn't be enough in 3.x and probably 2.7, since you also
have to make sure that the C-extension is not used here.)

BUT: Keep in mind that monkey-patches are problematic, and should be
avoided (or used very carefully) in production code. So, better
replace the complete encoder.py or use your own patched version
of the complete json-module.

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


Re: How to set my gui?

2013-04-19 Thread Roland Koebler
Hi,

> These days, GUI programming is to me just
> programming and calling on certain libraries/modules.
+1

> One thing you may want to consider is using your main thread for the
> UI, and spinning off another thread to do your search. But do that
> ONLY if you know you understand threads, and threading in Python.
> Otherwise you'll make your life unnecessarily hard. :)
For simple tasks, you don't need threads, but can use the glib-functions
timeout_add(), idle_add() etc.

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


ANN: template-engine pyratemp 0.3.0/0.2.3

2013-04-23 Thread Roland Koebler
Hi,

since there were some questions about template-engines some time ago,
I would like to announce:

- I updated my comparison and benchmarks of several template-engines
  on http://www.simple-is-better.org/template/
- I have released a new version of my small and simple but powerful and
  pythonic template-engine "pyratemp":


=
pyratemp 0.3.0 / 0.2.3 released -- 2013-04-03
=

A new version of pyratemp is released, which officially adds Python 3
support; and a backport of this version to Python <=2.5:

- 0.3.0 for Python >=2.6 / 3.x
- 0.2.3 for Python <=2.5

No changes in your templates and your Python-code should be necessary,
except if you use cmp() / xrange() in your templates, which are gone
in Python 3 and pyratemp 0.3.0/0.2.3.

About pyratemp
--
pyratemp is a small, simple and powerful template-engine for Python.

Changes
---
see http://www.simple-is-better.org/template/pyratemp-latest/NEWS

The main changes are:

- Python 3 support
- added setup.py for installation via distutils
- renamed yaml2pyratemp.py to pyratemp_tool.py
- added LaTeX- and mail-header-escaping
- removed cmp(), xrange() from the template-functions

Resources
-
Homepage, documentation, download and mailinglists:
   http://www.simple-is-better.org/template/pyratemp.html

Download:
- http://www.simple-is-better.org/template/pyratemp-0.3.0.tgz
- http://www.simple-is-better.org/template/pyratemp-0.2.3.tgz

on PyPI:
- https://pypi.python.org/pypi/pyratemp/0.3.0
- https://pypi.python.org/pypi/pyratemp/0.2.3

---

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


Porting python to a TI Processor (C64xx)

2006-06-21 Thread Roland Geibel
Dear all.

We want to make python run on DSP processors (C64xx family of TI).

I've already tried to ask [EMAIL PROTECTED] (about his "Python for 
arm-Linux"),
but didn't get an answer so far.
Neither could I  find it in the Python tree at sourceforge.

Any hints welcome

Roland Geibel

[EMAIL PROTECTED]



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


Feed wxComboBox with dictionary/hash

2006-06-22 Thread Roland Rickborn
Hi folks,

I am relatively new to Python. Although I read a lot of howtos,
introductions and wikis, I am still having trouble ;-)

My querstion:
As the subject says, I'd like to feed a wx.ComboBox with a
dictionary/hash. According to the posting of Stano Paska ("wxComboBox
<> combobox", 20 Jul. 2004), there seems to be a way to do this:

> You must use something like
> combo.Append('aaa', 'a')
> combo.Append('bbb', 'b')
> ...

My problem is:
my data has thousands of entries. Therefore, I'd like to feed the
combobox with a dictionary (which itself is fed by a database query).

My first question:
how can a wx.ComboBox be fed by a dictionary?

For further help, Stano says:

> read manual for more details...

Ok, I'd like to. But which one?
I was reading http://www.wxpython.org/docs/api/wx.ComboBox-class.html
and didn't even find the above mentioned append method :-(

TIA,
Roland R.

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


wxPython: Icon aus base64 decoded Image

2006-12-11 Thread Roland Rickborn
Hallo zusammen,

in meine Anwendung ist ein Bild eingebettet und oben in der Leiste soll
ein Icon erscheinen.
Ausserdem will ich nur _eine_ Datei ausgeben, also ohne zusärtliche
Bild-Dateien etc.

Dazu habe ich das Bild in base64 codiert und als String im Skript
gespeichert, siehe unten. Beim Ausführen des Skripts wird dieser
String decodiert, in ein Image umgewandelt und als Bitmap dargestellt.
Funzt prima.

# Base64 codiertes Bild, einmalig ausserhalb des Skripts
import base64
file = open('smallPic.png',"rb")
pic = file.read()
pic_b64 = pic.encode("base64")
# ergibt einen String wie """'iVBORw0KGgoTkSuQmCC\n'"""

# Danach im Skript:
self.staticImage =
wx.ImageFromStream(StringIO(pic_b64.decode("base64")))
self.staticBitmap =
wx.StaticBitmap(bitmap=wx.BitmapFromImage(self.staticImage,
  wx.BITMAP_TYPE_PNG),
  name='staticBitmap3', parent=self.panel1, pos=wx.Point(8,
96),
  size=wx.Size(168, 72), style=0)

Wie gesagt, funkzt  prima!


Und jetzt das Icon:
# Base64 codiertes Bild, einmalig ausserhalb des Skripts
import base64
file = open('smallIcon.ico',"rb")
ico = file.read()
ico_b64 = ico.encode("base64")
# ergibt einen String wie
"""'==123445342gsgadfgdghsfhsdhxfghxfghTRG>dfg\n'"""

# Danach im Skript:
icon = base64.b64decode(ico_b64)
self.SetIcon(wx.Icon(icon, wx.BITMAP_TYPE_ICO))

--> Fehler beim Start der Anwendung: "Failed to load icom from the
file"

Wo ist der Fehler und was muss ich machen, damit das Icon angezeigt
wird?

Besten Dank und schöne Grüsse,
Roland

--

E-Mail-Adresse ist reply-fähig, wird aber nicht gelesen.
Besser: r_2 bei Ge Em Ix oder hier in der NG

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


Re: array, a better shell

2006-12-20 Thread Roland Puntaier
>I have also used the shell of Mathematica. It's quite powerful and it
>can show graphics too inlined, but globally I don't like it fully
>because it makes editing small programs a pain (for me)...

I use Vim to edit python code and can execute any selection (F3) or single 
lines (F2) 
whenever I want to (Vim must be built to include the python interpreter).

Here is the according Vimrc code

"pyout if present must be right below
"r replaces, p prints, symb sets the start
py << EOL 
def PrintCurrentLine(*args): 
  cur_str = vim.current.line 
  action, symb = None, None 
  for i in args: 
if i in ["r","p"]: action = i 
else:  symb   = i 
  try:start = cur_str.rindex(symb)+len(symb) 
  except: start = 0 
  eval(compile("pyoutres="+cur_str[start:],'','exec'),globals()) 
  result = eval("pyoutres",globals()) 
  if action == "r": 
vim.current.line = cur_str[:start]+str(result) 
  else: 
iPy=PyOutput()
if iPy != -1:
  resS=cur_str+"=="+'\n'+str(result)+'\n'
  resL=resS.split('\n')
  vim.buffers[iPy].range(0,0).append(resL)
  vim.command("wincmd b")
  vim.command("normal gg")
  vim.command("wincmd p")
else:
  print result
EOL 
command -narg=* Pyl python PrintCurrentLine() 
map  :py PrintCurrentLine()

python << EOL 
def EvaluateCurrentRange(): 
  c_r=vim.current.range
  i=0
  while c_r[0].startswith(' '*(i+1)):
i+=1
  rngstr='\n'.join([ln[i:] for ln in c_r])
  eval(compile(rngstr,'','exec'),globals()) 
EOL 
map  :py EvaluateCurrentRange()
-- 
http://mail.python.org/mailman/listinfo/python-list


import order or cross import

2007-01-03 Thread Roland Hedberg
Hi!

I'm having a bit of a problem with import.

I'm writing a marshalling system that based on a specification will
create one or more files containing mostly class definitions.

If there are more than one file created (and there are reasons for
creating more than one file in some instances) then they will import
each other since there may be or is interdependencies in between them.

And this is where the going gets tough.

Let's assume I have the following files:

- ONE.py 

import TWO

class Car:
def __init__(self):
self.color = None

def set_color(self,v):
if isinstance(v,TWO.Black) or isinstance(v,TWO.White):
self.color = v

class BaseColor:
def __init__(self):
pass
def __str__(self):
return self.color

if __name__ == "__main__":
car = Car()
color = TWO.Black()
car.set_color(color)
print car.color

-- TWO.py ---

import ONE

class Black(ONE.BaseColor):
color = "Black"
def __init__(self):
ONE.BaseColor.__init__(self)

class White(ONE.BaseColor):
color = "White"
def __init__(self):
ONE.BaseColor.__init__(self)

-

Now, running ONE.py causes no problem it will print "Black", but running
TWO.py I get:
AttributeError: 'module' object has no attribute 'BaseColor'

So, how can this be solved if it can be ?

To join ONE.py and TWO.py into one file could be a solution if there
where no other conditions (non-language based) that prevented it.

-- Roland

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


Re: import order or cross import

2007-01-03 Thread Roland Hedberg
Duncan Booth wrote:

> 
> Remember that all statements in Python are executed at the time they are 
> encountered: there are no declarations (apart from 'global') so no looking 
> ahead to see what classes or functions are coming up.

Yes, I've seen this time and time again.

> One other complication in your particular instance: when you run a ONE.py 
> as a script the script is loaded in a module called '__main__', so 'import 
> ONE' will actually load a separate module which is NOT the same as the 
> __main__ module. Likewise when you run TWO.py as a script you have three 
> modules __main__ (loaded from TWO.py), ONE, and TWO.

Hmm, that's interesting.

> 
> The fix in this sort of case is usually to extract the base class out to a 
> third module. If you put BaseColor in base.py then you can safely import 
> that anywhere you want it.

OK, so I'll go for this option. Takes some more intelligence in the
marshalling code but it should be doable.

> An alternative in this case would be to edit ONE.py and move the line 
> 'import TWO' down below the definition of BaseColor: nothing before that 
> actually requires TWO to have been imported yet.

Since no person is involved, in creating files like ONE.py and TWO.py,
this also has to be done by the marshalling code.
I've no clear idea which alternative would be best/easiest but your
first one seems clearer.

> However, you really should try to separate scripts from modules otherwise 
> the double use as both __main__ and a named module is going to come back 
> and bite you.

Oh, that was only there as an example, so you could see the intent usage.

Thanks Duncan!

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


Re: import order or cross import

2007-01-03 Thread Roland Hedberg
Jussi Salmela wrote:
> Roland Hedberg kirjoitti:

>> I'm having a bit of a problem with import.
>>
>> I'm writing a marshalling system that based on a specification will
>> create one or more files containing mostly class definitions.

> Maybe I'm missing something, but why is the class BaseColor in file 
> ONE.py and not in TWO.py?

This has to do with the how the specifications that the marshallings
system works with are structured. And it is therefor not something I can
control.

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


Re: Testing for the presence of input from stdin.

2006-01-24 Thread Roland Heiber
Will McDonald wrote:
> Hi all.
> 
> I'm writing a little script that operates on either stdin or a file
> specified on the command line when run. I'm trying to handle the
> situation where the script's run without any input gracefully but
> can't think how to test for stdin.
> 

Hi,

maybe http://docs.python.org/lib/module-fileinput.html is useful for you:

"This iterates over the lines of all files listed in sys.argv[1:], 
defaulting to sys.stdin if the list is empty. If a filename is '-', it 
is also replaced by sys.stdin. To specify an alternative list of 
filenames, pass it as the first argument to input(). A single file name 
is also allowed."

HtH, Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A __getattr__ for class methods?

2006-02-08 Thread Roland Heiber
Dylan Moreland wrote:
> I have a metaclass generating basic properties such as .name and .city,
> but I don't want to generate a class method for every permutation of
> the attributes. I'd like to have something much like __getattr__ for
> instance attributes, so that if a method like
> Person.find_by_city_and_email cannot be found, I can construct a call
> to the basic find method that hides the SQL. Is there any way of doing
> this, or am I trying to mirror a functionality that Python simply does
> not have?
> 

I don't get it? __getattr__ is exactly what you are looking for:

http://docs.python.org/ref/attribute-access.html#l2h-206

__getattr__( self, name)

Called when an attribute lookup has not found the attribute in the usual 
places (i.e. it is not an instance attribute nor is it found in the 
class tree for self). name is the attribute name. This method should 
return the (computed) attribute value or raise an AttributeError exception.

HtH, Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Serving binary data from a cgi-script

2005-05-11 Thread Roland Heiber
Thomas W wrote:
>   print d

Hi,

use sys.stdout.write instead, print is adding linebreaks ...

HtH, Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: c[:]()

2007-06-04 Thread Roland Puntaier
Hi,

I have started to look into the last postings. It is funny and sad how 
people talking the same language cannot understand each other.
I have become curious on what exactly you mean.

Warren, can you please restate your point. 
When doing so, please define every identifier or non-plain english word 
before you use it and be concise.

e.g.
You cannot use b if it is not clear what it means. 
When writing "c[:]()" there is no "b". So where does "b" come from?
You told us that "b" is a container (*), but where should the python 
interpreter or the people take it from.

People start to guess, as I did:
  (1) - Might you mean: "b() for b in c". (I use python syntax here)
This means c is a container, an undertanding that is supported 
also by the used slicing ("[:]").
  (2) - Let's suppose that by (*) you mean that, in addition to "c", "b" 
is a container, too.
  (3) - Now for a standard container "b()" does not make sense. Maybe you 
mean "b[:]()" according to the guessed interpretation (1).
This would then be a recursive evaluation of all functions with no 
parameters in a tree ("tree" in the informatics sense).
  (end of my guess)
 

I think somehow it should be possible to convey an idea so that others can 
grasp it.
If it does not work the first time, it is good to clarify the points that 
one found others to misunderstand.
Otherwise one risks to be regarded as esoteric.

Thanks, Roland

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


Re: Python IDE

2007-06-20 Thread Roland Puntaier
Hi,

With a python-enabled VIM it's possible to execute and thus test the 
python code. I have the following lines in my vimrc.
F2 prints the result of a line evaluation on the bottom or in a window 
named pyout if there. I open pyout with ":vert rightb new pyout" mapped to 
F1.
F4 does the same on the selected part of a line
F3 evaluates without printing


py import vim

py << EOL
def PyOutput(): 
  iPy=-1
  for i in range(len(vim.buffers)):
if vim.buffers[i].name and vim.buffers[i].name.find("pyout")!=-1:
  iPy=i
  return iPy
EOL

"pyout if present must be right below
"r replaces, p prints, symb sets the start
py << EOL 
def PrintCurrentLine(*args): 
  cur_str = vim.current.line 
  action, symb = None, None 
  for i in args: 
if i in ["r","p"]: action = i 
else:  symb   = i 
  try:start = cur_str.rindex(symb)+len(symb) 
  except: start = 0 
  eval(compile("pyoutres="+cur_str[start:],'','exec'),globals()) 
  result = eval("pyoutres",globals()) 
  if action == "r": 
vim.current.line = cur_str[:start]+str(result) 
  else: 
iPy=PyOutput()
if iPy != -1:
  resS=">>> "+cur_str+":"+'\n'+str(result)+'\n'
  resL=resS.split('\n')
  vim.buffers[iPy].range(0,0).append(resL)
  vim.command("wincmd b")
  vim.command("normal gg")
  vim.command("wincmd p")
  vim.command("wincmd j")
else:
  print result
EOL 
command -narg=* Pyl python PrintCurrentLine() 

map  :py PrintCurrentLine()

"this evaluates selected region with 
map  "tyo^["tpu
map  :vert rightb new pyout


python << EOL 
def EvaluateCurrentRange(): 
  c_r=vim.current.range
  i=0
  while c_r[0].startswith(' '*(i+1)):
i+=1
  rngstr='\n'.join([ln[i:] for ln in c_r])
  eval(compile(rngstr,'','exec'),globals()) 
EOL 
map  :py EvaluateCurrentRange()




[EMAIL PROTECTED] schrieb 
am 19.06.2007 17:20:08:

> Bjoern Schliessmann wrote:
> > BartlebyScrivener wrote:
> >> VIM
> > 
> > *clap-clap*
> > 
> > BTW, are there tutorials on the more arcane vim functions that come
> > in handy with Python?
> > 
> > Regards,
> > 
> > 
> > Bj?rn
> > 
> Not a tutorial, but I found this little snippet:
> 
> http://www.ibiblio.org/obp/pyBiblio/tips/elkner/vim4python.php
> 
> I thought the last line was especially neat.
> 
> Will
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Antwort: How to calculate definite integral with python

2007-05-15 Thread Roland Puntaier
Searching the web I have found:

from numpy import *
def simple_integral(func,a,b,dx = 0.001):
  return sum(map(lambda x:dx*x, func(arange(a,b,dx
simple_integral(sin,0,2*pi)


Or in
http://pylab.sourceforge.net/
quadrature.py

Cheers.

[EMAIL PROTECTED] schrieb 
am 15.05.2007 04:23:00:

> I'm trying to do some integral calculation. I have searched the web, but 

> haven't found any useful information. Will somebody point me to the 
> right resources on the web for this job ?
> 
> Thanks a lot.
> 
> ps. Can numpy be used for this job?*
> *
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


strange list comprehension on generator

2007-08-29 Thread Roland Puntaier
def changeOne(aa,idx):
  aa[idx]=not aa[idx]
  yield aa
  for i in range(idx):
for x in changeOne(aa,i):
  yield x

def changeOneOrder(aa):
  yield aa
  for i in range(len(aa)): 
for x in changeOne(aa,i):
  yield x

a=[False]*3
og=changeOneOrder(a)
#this does not return the way I would expect. why?
list(og)
#returns
#[[False, False, True], [False, False, True], [False, False, True], 
[False, False, True], [False, False, True], [False, False, True], [False, 
False, True], [False, False, True]]


#this works as expected
a=[False]*3
og=changeOneOrder(a)
og.next()
og.next()
...

#this works as expected
def ty():
  yield 1
  yield 2
  yield 3

tg=ty()
list(tg)

cheers, Roland

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


Other classes in a module

2007-03-25 Thread Roland Hedberg
Hi!

Can an instance of a class in a module, in any simple way find out which
other classes that exists in said module ?

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


call graph using python and cscope

2007-01-10 Thread Roland Puntaier
""" 
Sometimes it is nice to have the data used by cscope accessible in a 
programatic way.
The following python script extract the "functions called" information 
from cscope (function: callGraph)
and produced an html file from them.

  from csCallGraph import *
  acg=callGraph(entryFun,workingDir)

entryFun is the function to start with (e.g. main)
workingDir is the directory where cscope.out is located

As a script it can be called like:
  csCallGraph main > myprogram.html
"""

import subprocess , os, sys

def functionsCalled(entryFun,workingDir):
  cmd = "cscope -d -l -L -2%s"%entryFun
  process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, 
cwd=workingDir) 
  csoutput= process.stdout.read() 
  del process
  cslines=[arr.strip().split(' ') for arr in csoutput.split('\n') if 
len(arr.split(' '))>1]
  funsCalled={}
  for fl in cslines:
if funsCalled.has_key(fl[0]):
  funsCalled[fl[0]]|=set([fl[1]])
else:
  funsCalled[fl[0]]=set([fl[1]])
  allFuns=set(map(lambda x:x[1],cslines))
  return (allFuns,funsCalled)

def callGraph(entryFun,workingDir,cg={}):
  if not cg.has_key(entryFun): 
allFuns,funsCalled=functionsCalled(entryFun,workingDir)
cg[entryFun]=funsCalled
for af in allFuns:
  cg=callGraph(af,workingDir,cg)
  return cg

def textCallGraph(acg):
  innerFuns=[(f,d,len(reduce(lambda x,y:x|y,d.values( for f,d in 
acg.items() if len(d)>0 ]
  leafFuns=[(f,d,0) for f,d in acg.items() if not len(d)>0 ]
  innerFuns.sort(lambda x,y: y[2]-x[2])
  innerLen=len(innerFuns)
  leafLen=len(leafFuns)
  title=lambda aFun: '\n' + aFun + '\n' + '-'*len(aFun)
  def ff(aFun,funsCalled):
fileFuns=zip(funsCalled.keys(),[''+',\n '.join(funsCalledInFile) 
for funsCalledInFile in funsCalled.values()])
funIn=lambda f: '\n%s in '%f
return title(aFun) + funIn(aFun) + funIn(aFun).join(map(lambda 
x:'%s:\n%s'%(x[0],x[1]),fileFuns))
  strInner='\n'.join([ff(f[0],f[1]) for f in innerFuns])
  strLeaves='\n'.join(map(lambda x:title(x[0]),leafFuns))
  return strInner+'\n'+strLeaves

def funWeights(acg):
  funWeights=dict([(f,reduce(lambda x,y:x|y,d.values())) for f,d in 
acg.items() if len(d)>0 ]+
  [(f,[]) for f,d in acg.items() if not len(d)>0 ])
  weights={}
  def calcWeights(af):
if not weights.has_key(af):
  subFuns=funWeights[af]
  weights[af]=1
  for f in subFuns:
calcWeights(f)
weights[af]+=weights[f]
  for af in funWeights.keys(): calcWeights(af)
  return weights

def htmlCallGraph(acg):
  funW=funWeights(acg)
  innerFuns=[(f,d,funW[f]) for f,d in acg.items() if len(d)>0 ]
  leafFuns=[(f,d,0) for f,d in acg.items() if not len(d)>0 ]
  #innerFuns.sort(lambda x,y: y[2]-x[2]))
  def cfun(a,b):
if b > a:
  return 1
elif b < a:
  return -1
return 0
  innerFuns.sort(lambda x,y: cfun(x[2],y[2]))
  innerLen=len(innerFuns)
  leafLen=len(leafFuns)
  funDict=dict(zip(map(lambda x:x[0],innerFuns)+map(lambda 
x:x[0],leafFuns),range(innerLen+leafLen)))
  title=lambda aFun: '' + aFun + ' 
(%i)'%funW[aFun] + '\n'
  def ff(aFun,funsCalled):
fun=lambda y:''+y+''
fileFuns=zip(funsCalled.keys(),[',\n'.join(map(fun,funsCalledInFile)) 
for funsCalledInFile in funsCalled.values()])
funIn=lambda f: '%s in '%f
return title(aFun) + funIn(aFun) + funIn(aFun).join(map(lambda 
x:'%s:\n%s'%(x[0],x[1]),fileFuns))
  strInner='\n'.join([ff(f[0],f[1]) for f in innerFuns])
  strLeaves='\n'.join(map(lambda x:title(x[0]),leafFuns))
  return '\n\n'+strInner+'\n'+strLeaves+"\n\n"

if __name__ == '__main__':
  if len(sys.argv) < 2:
  print 'Usage: csGragh.py entryFunction'
  sys.exit()
  entryFun=sys.argv[1]
  workingDir=os.getcwd()
  acg=callGraph(entryFun,workingDir)
  print htmlCallGraph(acg)
-- 
http://mail.python.org/mailman/listinfo/python-list


SubProcess _make_inheritable

2007-01-10 Thread Roland Puntaier
SubProcess.py needs to be patched - at least in 2.4 -  to work from 
windows GUIs:

def _make_inheritable(self, handle):
"""Return a duplicate of handle, which is inheritable"""
if handle==None: handle=-1
return DuplicateHandle(GetCurrentProcess(), handle,
   GetCurrentProcess(), 0, 1,
   DUPLICATE_SAME_ACCESS)
-- 
http://mail.python.org/mailman/listinfo/python-list


Antwort: Re: SubProcess _make_inheritable

2007-01-11 Thread Roland Puntaier
Thanks for pointing me to the tracker.
I've seen there is already an entry for this:
[ 1603907 ] subprocess: error redirecting i/o from non-console process 

Roland

Gabriel Genellina <[EMAIL PROTECTED]> schrieb am 11.01.2007 05:24:03:

> At Wednesday 10/1/2007 13:10, Roland Puntaier wrote:
> 
> >SubProcess.py needs to be patched - at least in 2.4 -  to work from
> >windows GUIs:
> >
> > def _make_inheritable(self, handle):
> > """Return a duplicate of handle, which is inheritable"""
> > if handle==None: handle=-1
> > return DuplicateHandle(GetCurrentProcess(), handle,
> >GetCurrentProcess(), 0, 1,
> >DUPLICATE_SAME_ACCESS)
> 
> You should submit it to the tracker, not post here and hope someone 
> would notice...
> http://sourceforge.net/tracker/?group_id=5470
> 
> Anyway, I don't see in which case would handle be None.
> 
> 
> -- 
> Gabriel Genellina
> Softlab SRL 
> 
> 
> 
> 
> 
> 
> __ 
> Preguntá. Respondé. Descubrí. 
> Todo lo que querías saber, y lo que ni imaginabas, 
> está en Yahoo! Respuestas (Beta). 
> ¡Probalo ya! 
> http://www.yahoo.com.ar/respuestas 
> 
> 
> 

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


Re: how to mimik a main() function to start a program with entry point?

2007-01-19 Thread Roland Puntaier
Hi,

>From a python module you could use subprocess  and start the exe. Normally 
one must leave it to the OS to load a binary module, because there are 
certain things done during loading.
Nevertheless the location of the entry point is coded in the binary file 
format (e.g. PE for windows or ELF for Linux)
Whether it is possible from python to make an import of a python module in 
a python package gone through py2exe, I cannot answer.

But I think you mean how to tell py2exe, where the entry point is. Read: 
http://www.py2exe.org/index.cgi/Tutorial

Roland

[EMAIL PROTECTED] schrieb 
am 19.01.2007 14:30:33:

> hello all.
> I have one simple query and may be that's to stupid to answer but I am
> not finding the answer any ways.
> I have a set of modules in my package and out if which one is my
> actual starting point to my entire program.  say for example I have an
> entire database application ready and I want a main (as in java or c)
> to initiate the program and may be bring up a login screen and then
> pass the control on to the main window.
> if I am giving raw source code that is easy because I will tell user
> to run the command ./xyz.py which has that function.
> but if I freze it into an executable with py2exe for example , how do
> I solve this problem?
> thanks.
> Krishnakant.
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

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


Re: Introspection Class/Instance Name

2006-04-25 Thread Roland Heiber
*binarystar* wrote:
> Hello there,
> 
> what method would you use to return the name of the class and/or 
> instance introspectively eg.
> 
> class Bollocks:
> 
> def __init__( self ):
>
> print self.__method_that_returns_class_name__()
> print self.__method_that_returns_instance_name__()
> 
> 
> instance_of_bollocks= Bollocks()
> 
> # Which outputs
> 
> 'Bollocks'
> 'instance_of_bollocks'
> 
> 
> 
> I have been scouring the 2.4 docs ... I am sure it is frustratingly simple
> 
> thx in advance
> 
> **

Hi,

take a look at self.__class__.__name__ for the Class-name.

HtH, Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >