Reading Windows CSV file with LCID entries under Linux.

2008-09-22 Thread Thomas Troeger

Dear all,

I've stumbled over a problem with Windows Locale ID information and 
codepages. I'm writing a Python application that parses a CSV file,
the format of a line in this file is LCID;Text1;Text2. Each line can 
contain a different locale id (LCID) and the text fields contain data 
that is encoded in some codepage which is associated with this LCID. My 
current data file contains the codes 1033 for German and 1031 for 
English US (as listed in 
http://www.microsoft.com/globaldev/reference/lcid-all.mspx). 
Unfortunately, I cannot find out which Codepage (like cp-1252 or 
whatever) belongs to which LCID.


My question is: How can I convert this data into something more 
reasonable like unicode? Basically, what I want is something like 
Text1;Text2, both fields encoded as UTF-8. Can this be done with 
Python? How can I find out which codepage I have to use for 1033 and 1031?


Any help appreciated,
Thomas.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between type and class

2008-08-01 Thread Thomas Troeger

Steven D'Aprano wrote:

class A:
def bar(self):
print A



Alas, you've chosen the worst-possible example to clarify matters, 
because old-style classic classes are *not* unified with types, and will 
disappear in the future:


Of course I wanted to write `class A(object)', but I keep forgetting 
this one because I'm still used to the old ways...


Will this disappear in Python 3.0., i.e. can you again simply write

class A:

and inherit from object automagically? That would be nice since that's 
what most people probably want.

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


Re: Difference between type and class

2008-07-31 Thread Thomas Troeger

Can someone explain to me the difference between a type and a class?


If your confusion is of a more general nature I suggest reading the 
introduction of `Design Patterns' (ISBN-10: 0201633612), under 
`Specifying Object Interfaces'.


In short: A type denotes a certain interface, i.e. a set of signatures, 
whereas a class tells us how an object is implemented (like a 
blueprint). A class can have many types if it implements all their 
interfaces, and different classes can have the same type if they share a 
common interface. The following example should clarify matters:


class A:
def bar(self):
print A

class B:
def bar(self):
print B

class C:
def bla(self):
print C

def foo(x):
x.bar()

you can call foo with instances of both A and B, because both classes 
share a common type, namely the type that has a `bar' method), but not 
with an instance of C because it has no method `bar'. Btw, this example 
shows the use of duck typing (http://en.wikipedia.org/wiki/Duck_typing).


HTH,
Thomas.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between type and class

2008-07-31 Thread Thomas Troeger

That would imply that I cannot create instances of a type, only of
a class that implements the type, wouldn't it?

But Python denotes 'int' as a type *and* I can instantiate it.


Now I start getting confused also ;-)

 a=5
 a.__class__
type 'int'
 a.__class__.__class__
type 'type'
 dir(a)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', 
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__', 
'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', 
'__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', 
'__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', 
'__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', 
'__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', 
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', 
'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', 
'__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']


I think in Python everything is implemented as a class which makes 
sense. AFAIK you can implement a certain interface in a custom class and 
it behaves like, for example, a builtin integer. But I guess one of the 
gurus can clarify this matter with an appropriate URL ;-)

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


Re: Function editing with Vim throws IndentError

2008-07-25 Thread Thomas Troeger

Lawrence D'Oliveiro wrote:

Specified by whom? The most common setting these days is 4 columns.


Where? I've randomly seen code snipplets that indent using spaces or, 
worse, tabstop != 8, but most code I've come across uses tabstop width 
8, which is how it was meant to be from the beginning of time.


Unfortunately, for many people it's hard to understand that a tabstop is 
not the same as indenting with spaces, but I think this is leading too 
far and will end in a dogmatic discussion like the `ViM vs. Emacs' war.


Anyways, if you claim tabstop 4 is most widely used I'd like to see some 
kind of a reference for that since it contradicts not only my experience.


Finally, I'd like to throw in this one from the Linux kernel sources, 
from `Documentation/CodingStyle:


 


Chapter 1: Indentation

Tabs are 8 characters, and thus indentations are also 8 characters.
There are heretic movements that try to make indentations 4 (or even 2!)
characters deep, and that is akin to trying to define the value of PI to
be 3.

[...]

Now, some people will claim that having 8-character indentations makes
the code move too far to the right, and makes it hard to read on a
80-character terminal screen.  The answer to that is that if you need
more than 3 levels of indentation, you're screwed anyway, and should fix
your program.

In short, 8-char indents make things easier to read, and have the added
benefit of warning you when you're nesting your functions too deep.
Heed that warning.
 
--

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


Re: Function editing with Vim throws IndentError

2008-07-23 Thread Thomas Troeger

ptn wrote:

Hi everybody,

I have a weird problem.  Say I have a .py file with some functions in
it, like this:


[...]


Could someone provide some pointers?

Thanks,

Pablo Torres N.


Have you enabled the `list' option to see which characters are acutally 
on the lines of interest? Try out `:set list' and see the contents! 
Otherwise helpful is `ga' to see the code values. Of course this will 
not tell you why it is going wrong, but maybe it helps you to determine 
what is going wrong :-)

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


Re: Python embedding question (2).

2008-07-22 Thread Thomas Troeger

Carl Banks wrote:

On Jul 17, 9:57 am, Thomas Troeger [EMAIL PROTECTED]
wrote:

I'd say that PyGame could be a solution.
Or otherwise you could do your own audio/graphics programming (you don't
tell us which OS you use, but there exist python modules that allow you
to do barebones graphics  sound programming on linux...).

Pyglet runs on top of OpenGL, which might have performance problems on
an embedded device, if OpenGL or Mesa is even supported.  If it's
supported, I suspect performance will be adequate for 2D drawing.  It
almost certainly is the lightest solution you can find.


Carl Banks


I've managed to put together a small pyGame program, it runs smoothly 
and seems to be exactly what I wanted. It's fast! Even with 100 moving 
objects it still runs so fast that I can consider using Python/pyGame 
for the whole project.


There are still some questions left which I haven't found out by myself, 
so maybe someone here can answer them:


- I can't see how to create more sophisticated text output, it seems the 
built in font render facilities are limited to simple strings. Is that 
true? I'd need a way to at least render multiline text with paragraphs 
and bidirectionality, like pango does it. Is there a way to integrate 
pango support into pyGame? I'd prefer marked up text display with text 
properties ...
- Is there some way to reserve screen areas so they are excluded from a 
blit, or do I have to manage stuff like this myself? I am thinking about 
several graphic layers where each layer is painted on top of the next 
layer, for example to draw a gui in front of a background image.
- There seems to be support for video overlay, i.e. is it possible to 
have an external program paint an image from a camera into a portion of 
the screen while pyGame is running?


Maybe this is the wrong list to ask, so please forgive the question but 
direct me to somewhere better.


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


Re: Using Tcl extensions with Python?

2008-07-18 Thread Thomas Troeger

C Martin wrote:

How do you setup a Tcl extension to be accessible through Python? I
understand that I'll have to use native Tcl calls to use it (tk.call()
etc), but I can't figure out where to put the files or how to
initialize them so I can call them.

The package I would like to use is TkPNG: 
http://www.muonics.com/FreeStuff/TkPNG/

Thanks.


ARGH, Tcl!!! ;-) I know that's not the question, but do you know PIL 
(http://www.pythonware.com/products/pil)?

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


Re: Python embedding question.

2008-07-17 Thread Thomas Troeger

Jan Claeys wrote:

I'd say that PyGame could be a solution.

Or otherwise you could do your own audio/graphics programming (you don't 
tell us which OS you use, but there exist python modules that allow you 
to do barebones graphics  sound programming on linux...).


Yes, I'm using a very small Linux system with busybox, running from a 
compact flash drive. I'll investigate PyGame, it sounds as if it is a 
good candidate :P


Thanks so far,
Thomas.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python embedding question (2).

2008-07-17 Thread Thomas Troeger

I'd say that PyGame could be a solution.

Or otherwise you could do your own audio/graphics programming (you don't 
tell us which OS you use, but there exist python modules that allow you 
to do barebones graphics  sound programming on linux...).


After some more reading I've stumbled over pyglet. Any experiences with 
it? It seems it does a lot of cool things, if anyone has used it more 
intensely I'd be happy to hear if the following things can be done:


- Linux framebuffer (16, 24 bpp) display of 2d graphics with overlays 
(i.e. menues, contextual areas that pop up etc.). I don't have X on the 
embedded device, just the regular framebuffer.

- alpha blending of several layers.
- rendering of TTF fonts and unicode, for example display of arabic text 
(which renders from right to left) and mixed text support like in the 
unicode bidirectional algorithm.
- hardware caching of bitmaps for faster graphics operations (needed for 
tool tips or similar tasks).


I'll try to find that out myself (I'm pretty excited about the thing 
already ^^), but I'd be happy to hear of people who have used it already.


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


Python embedding question.

2008-07-15 Thread Thomas Troeger

Hi,

Sorry I've posted a similar question some weeks ago, but I got no 
answers. I want to embed a Python application on a device with limited 
resources, esp. storage limitations. Is there a way to reduce the Python 
interpreter to a set of modules that's urgently needed? Or is there a 
method to have gzipped modules that are unzipped on the fly into memory 
when they're accessed? That would be even better.


Additionally, is there a Python module that contains all the stuff 
needed for an embedded application like graphics, sound etc. or do I 
have to use the various bindings to libraries like cairo, Qt or similar? 
Is there a site that helps with those decisions?


I've really looked at a lot of places but haven't found a suitable 
solutions yet, so I'm asking here in hope that someone has experience 
with that topic.


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


Re: Testing for Internet Connection

2008-07-15 Thread Thomas Troeger

Alex Marandon wrote:

Alexnb wrote:

I am wondering, is there a simple way to test for Internet connection? If
not, what is the hard way :p


Trying to fetch the homepage from a few major websites (Yahoo, Google, 
etc.)? If all of them are failing, it's very likely that the connection 
is down. You can use urllib2 [1] to accomplish that.


[1] http://docs.python.org/lib/module-urllib2.html


This seems to work and is rather fast and wastes no bandwidth:

==
#!/usr/bin/python

import socket, struct

def check_host(host, port, timeout=1):

Check for connectivity to a certain host.

# assume we have no route.
ret=False

# connect to host.
try:
# create socket.
sock=socket.socket()
# create timeval structure.
timeval=struct.pack(2I, timeout, 0)
# set socket timeout options.
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeval)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDTIMEO, timeval)
# connect to host.
sock.connect((host, port))
# abort communications.
sock.shutdown(SHUT_RDWR)
# we have connectivity after all.
ret=True
except:
pass

# try to close socket in any case.
try:
sock.close()
except:
pass

return ret

#  main -

if check_host(www.heise.de, 80):
print Horray!
else:
print We've lost headquarters!
==

I hope the code is ok, but there is always something you can do better. 
Comments? :)


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


Static Class Initialization Question.

2008-07-04 Thread Thomas Troeger

Hello,

I have a class that looks like this:

class A(object):
def __init__(self, a=0, b=1):
self.a, self.b=a, b

def __str__(self):
return %s(%d,%d) % (type(a).__name__, self.a, self.b)

I want to have a list of such classes instantiated automatically on 
startup of my program. My current (most probably clumsy) implementation 
looks like this:


bla=[A(x[0], x[1]) for x in ((1, 2), (3, 4))]

giving the following:

 map(str, bla)
['A(1,2)', 'A(3,4)']

Is there a better way to construct a list of such classes? Basically 
what I want is something similar to the following C example:


struct {
int a;
int b;
} bla[]={ {1, 2}, {3, 4} };

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


Re: Static Class Initialization Question.

2008-07-04 Thread Thomas Troeger

Bruno Desthuilliers wrote:

  return %s(%d,%d) % (type(self).__name__, self.a, self.b)


Er, yes exactly! I noticed it a few seconds after I had sent the message ;-(

I want to have a list of such classes instantiated automatically on 


Of course I meant class instances ... sorry :) It's always good to have 
an example to compensate for English errors *g*.



bla = [A(*args) for args in ((1,2), (3,4))]


...

 Note that it's not a list of classes, but a list of instances of A. But
 given your specs, nope, your approach is the right one.

Ah I knew there was something and I couldn't find it in the docs 
anymore! Now my potential follow-up question is answered as well, namely 
how I can instantiate with variable argument lists, like this:


 bla = [A(*args) for args in ((), (1,), (1, 2))]
 map(str, bla)
['A(0,1)', 'A(1,1)', 'A(1,2)']

Basically (no pun intended[1]), Python is not C. Trying to write C in 
Python will only buy you pain and frustration (and this can be 
generalized for any combination of two languages for any known 
programming language).


Hehe. I am trying to develop a program prototype in python because of 
it's repaid prototyping properties, and once it's working I will port it 
to C, because of speed issues and the fact that it's running on an 
embedded machine without space for a python interpreter. I have like 4 
Megs left, but until now noone has answered my question how I can cut 
down a standard python installation so that it fit's into 4 megs.


Thanks for your quick answer :)
T.
--
http://mail.python.org/mailman/listinfo/python-list


Minimal Python installation?

2008-05-21 Thread Thomas Troeger

Hi,

I'd like to put the python library and interpreter on a small embedded 
Linux x86 compatible device where disk space is an issue. I played 
around with the minimal Python interpreters, but was not entirely happy 
with them, so my question is, is there an (preferably easy) way to put 
the interpreter and a cut-down version of the python library/modules 
that ship with regular Python into a new archive that I can safely copy 
and use? My current method is to copy the python interpreter and the 
python shared library to the device. Then I create a tar archive with 
all startup files from the site package directory to my archive using 
the following one-liner:


strace -f -eopen python -c 'pass' 21 | grep -v ENO | grep '\.py' | awk 
'BEGIN { FS=\ } { print $2 }' | tar cvf x.tar -T -


This effectively packs all python library files that are accessed upon 
startup into a tar archive. But I figure this method is not what I want 
because there surely are files I am missing here (for example which are 
accessed when starting a script), and what I really want is a minimal 
environment that works. Let's say I want a complete Python install, but 
without all the batteries :-)


Any pointers?

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


Re: compressing short strings?

2008-05-20 Thread Thomas Troeger

Paul Rubin wrote:

I have a lot of short English strings I'd like to compress in order to
reduce the size of a database.  That is, I'd like a compression
function that takes a string like (for example) George Washington


[...]



Thanks.


I think your idea is good, maybe you'd want to build an LZ78 encoder in 
Python (LZ78 is pretty easy), feed it with a long English text and then 
pickle the resulting object. You could then unpickle it on program start 
and encode your short strings with it. I bet there's a working 
implementation around that already that does it ... but if you can't 
find any, LZ78 is implemented in 1 or 2 hours. There was a rather good 
explanation of the algorithm in German, unfortunately it's vanished from 
the net recently (I have a backup if you're interested).


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


Embedding Python question.

2008-05-20 Thread Thomas Troeger

Dear all,

I've successfully embedded the Python interpreter into a set of C/C++ 
application programs that use a larger library project with information 
from http://docs.python.org/api/api.html and 
http://docs.python.org/ext/ext.html. Now I want to wrap classes and 
functions from the associated libraries so that I can write new 
applications completely in Python, but I'm not entirely sure how to 
start because I have some problems understanding which is the best way. 
It would be nice if someone could answer the following questions and 
clarify this matter:


- until now I've used the approach as documented in 
http://docs.python.org/ext/extending-with-embedding.html to extend the 
embedded interpreter and that works pretty well. I'd like to use a 
similar approach for other C applications. Can I write a C library that 
implements this technique and link it into all C applications that need 
Python support, or is there a better, more elegant way?


- in the documentation, there's an example that illustrates the creation 
of a python module for pure extending of a python script (the Noddy 
stuff). Do I have to make a separate module for each library I want to 
wrap? If yes, how can I manage the case where two libraries can access 
each other?


- if I write an extension module, how can I handle the case where the C 
wrapper methods want to call back into the same Python interpreter instance?


- I think my questions break down into a reference to documentation 
where a similar problem is explained in detail. Does anyone have such 
information?


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


Re: Newbie In Python

2008-05-20 Thread Thomas Troeger

[EMAIL PROTECTED] wrote:

I have Heard About Python its a OOD Language. i have to Learn it
where from i should start it.
i have python compiler at linux Platform.
anyone can suggest me about it.

Thanks In advance.


How about http://docs.python.org/tut/tut.html?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Embedding python code into text document question.

2008-01-11 Thread Thomas Troeger
Thanks guys, you've helped me very much :) Cheers  happy new year!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: module finalizer - is there such a beast?

2008-01-11 Thread Thomas Troeger
You can execute cleanup code if the interpreter exits:

http://docs.python.org/lib/module-atexit.html

This will only cover the `Python's exit' part of your question, not the 
module reloading stuff. On the other hand, if you load a module you 
could set a global variable and check for it on reload...
-- 
http://mail.python.org/mailman/listinfo/python-list


Embedding python code into text document question.

2008-01-10 Thread Thomas Troeger
Dear all,

I've written a program that parses a string or file for embedded python 
commands, executes them and fills in the returned value. The input might 
look like this:

process id: $$return os.getpid()$$
current date: $$return time.ctime()$$
superuser: $$
if os.geteuid():
return Yes
else:
return No$$

I've tried several solutions using eval, execfile or compile, but none 
of those would solve my problem. Does anyone have a solution that works? 
Any suggestions? Any help will be appreciated :)

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


Re: Embedding python code into text document question.

2008-01-10 Thread Thomas Troeger
Ok I've written a small example program to clarify matters:

 [SNIP] 
#!/usr/bin/python

import os, sys, time

def template(src, dst, sep):
 
 Copy file from src to dst, executing embedded python code.
 
 try:
 # read template file.
 f=open(src, rU)
 data=.join(f.readlines())
 f.close()

 # find embedded python code and execute it.
 while True:
 # find embedded embedded python command.
 offset=data.find(sep)
 if offset  0:
 break
 offset2=data.find(sep, offset+1)
 if offset2  0:
 break
 cmd=data[offset+len(sep):offset2]

 # execute command.
 try:
 ret=
 exec(compile(cmd,
'from string', 'exec'))
 except:
 print error compiling code `%s'. % cmd

 # substitute command with return value.
 data=data[:offset]+str(ret)+\
data[offset2+len(sep):]

 # write translated input.
 f=open(dst, w)
 f.write(data)
 f.close()
 except:
 print error processing template file `%s'. % src

# -- main ---

if len(sys.argv)  2:
 template(sys.argv[1], sys.argv[2], '$$')
 [SNIP] 

This is the example input that works:

 [SNIP] 
process id: $$ret=os.getpid()$$
current date: $$ret=time.ctime()$$
superuser: $$
if os.geteuid():
 ret=No
else:
 ret=Yes$$
 [SNIP] 

Now the `ret= ...' mechanism is not nice, I'd prefer to have a return 
statement instead.
-- 
http://mail.python.org/mailman/listinfo/python-list