Dialog boxes in curses

2011-08-13 Thread fab
Hello.

I've googled for hints but I didn't find anything, I hope it's not an
RTFM question :^)

I want to have dialog boxes (a message with Yes/No/Cancel options,
possibly with keyboard accels) in python + curses.

Does anyone have a pointer to docs about this?

Thanks!

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


Re: Dialog boxes in curses

2011-08-13 Thread fab

Thanks all for your suggestions, I'll look into them.

See you.

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


error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread fab
Hello.

I read a string from an utf-8 file:

fichierLaTeX = codecs.open(sys.argv[1], "r", "utf-8")
s = fichierLaTeX.read()
fichierLaTeX.close()

I can then print the string without error with 'print s'.

Next I parse this string:

def parser(s):
  i = 0
  while i < len(s):
if s[i:i+1] == '\\':
   i += 1
   if s[i:i+1] == '\\':
 print "backslash"
   elif s[i:i+1] == '%':
  print "pourcentage"
   else:
  if estUnCaractere(s[i:i+1]):
motcle = ""
while estUnCaractere(s[i:i+1]):
  motcle += s[i:i+1]
  i += 1
   print "mot-clé '"+motcle+"'"

but when I run this code, I get this error:

Traceback (most recent call last):
  File "./versOO.py", line 115, in 
  parser(s)
File "./versOO.py", line 105, in parser
print "mot-clé '"+motcle+"'"
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in
position 6: ordinal not in range(128)

What must I do to solve this?

Thanks!

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


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread fab
> Change your string literals to unicode by adding the u-prefix and you should 
> be OK.

Thanks, it solved the problem... for a while!

I need now to know if s[i] gives the next byte or the next character,
when I scan the string s. I've googled pages about python and unicode,
but didn't find a solution to that. I scan the string read from the
file char by char to construct s, but now get the same error when just
trying 'print s'.

Is there a way to tell python that all strings and characters are to
be treated as UTF-8? I have LC_ALL=en_GB.utf-8 in my shell, but python
does'nt seem to use this variable?

Thanks!

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


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread fab
Thanks for your insights.

I have taken the easy way out, I read on a page that python 3 worked
by default in UTF-8, so I downloaded and installed it.

Apart from a few surprises (print is not a funtion, and rules about
mixing spaces and tabs in indentation are much more strict, and I
guess more is to come :^) everything now works transparently.

Thanks again.

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


[Python3] Reading a binary file and wrtiting the bytes verbatim in an utf-8 file

2010-04-23 Thread fab
Hello.

I have to read the contents of a binary file (a PNG file exactly), and
dump it into an RTF file.

The RTF-file has been opened with codecs.open in utf-8 mode.

As I expected, the utf-8 decoder chokes on some combinations of bits;
how can I tell python to dump the bytes as they are, without
interpreting them?

Thanks.

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


Re: [Python3] Reading a binary file and wrtiting the bytes verbatim ?in an utf-8 file

2010-04-23 Thread fab
Thanks, I'll try this.

> I have no idea how you'd go about reading the contents of such a file
> in a sensible way.

The purpose is to embed PNG pictures in an RTF file that will be read
by OpenOffice. It seems that OpenOffice reads RTF in 8-bit, so it
should be ok.

The RTF is produced from a TeX source file encoded in UTF-8, that's
why I mix unicode and 8-bit.

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


Re: [Python3] Reading a binary file and wrtiting the bytes verbatim?in an utf-8 file

2010-04-25 Thread fab
> Another possibility is to open the file in binary mode and do the 
> encoding yourself when writing text. This might actually be a better 
> solution, since I'm not sure RTF uses utf-8 by default.

Yes, thanks for this suggestion, it seems the best to me. Actually RTF
is not UTF-8 encoded, it's 8-bit and maybe even ASCII only. Every
unicode char has to be encoded as an escape sequence (\u2022 for
example).

Thanks again.

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


Classes in a class: how to access variables from one in another

2010-10-18 Thread fab
Hello.

I have a class A that contains two classes B and C:

class A:
  class B:
self.x = 2
  
  class C:

Is there a way to access the x  defined in B in class C?

Thanks.

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


Re: Classes in a class: how to access variables from one in another

2010-10-18 Thread fab
Neil Cerutti  wrote:
>> I have a class A that contains two classes B and C:
>>
>> class A:
>>   class B:
>> self.x = 2
>>   
>>   class C:

I only wanted to show the structure of the code, not the actual
instructions.

> That's not valid Python code. Do you mean:
> 
> Class A:
>  Class B:
>x = 2

> Class A:
>  Class B:
>def __init__(self):
>  self.x = 2
> 

Any of these, aslong as I can access x  in C.

By the way, is the first proposition (that is, x = 2, without the
self.) valid? Is x a global variable then?

Thanks.

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


Re: Classes in a class: how to access variables from one in another

2010-10-18 Thread fab
Gary Herron  wrote:
> Well, your code still doesn't make sense, but the generic answers are:

I'll clarify what I need then:

I'm drawing Bézier curves. I draw them on a zone that is defined as a
subclass of GtkDrawingArea.

In a zone, I define a system of coordinates by 4 values: xmin, xmax,
ymin, and ymax that define the viewing port.

A curve is defined in this system of coordinates by a collection of
points.

So my way of coding it is the following:

class zone(GtkDrawingArea):

  class systemOfCoordinates:
self.xmin = -5
self.xmax = 5
self.ymin = -5
self.ymax = 5

  class Curve:
self.listOfPoints = ()

def draw(self):
  pass
  # for each point in self.listOfPoints: draw this point
  # then draw the Bézier curve passing through these points 

  class Point:
   def __init__(self, x, y):
 (self.x, self.y) = (x, y)

   def draw(self):
 # ... code for drawing a dot in the system of coordinates...

  def __init__(self): # for the zone object
 self.coord = self.systemOfCoordinates()
 self.listOfCurves = ( self.Curve() )

  def expose(self, widget, event):
 pass
 # for each curve of self.listOfCurves: draw it

Now to actually draw the dot on the screen, I need to access
coord.xmin, coord.xmax, coord.ymin and coord.ymax to Point.draw(). I
could do this by passing a reference to Point.draw(), but I'd like to
do this implicitely.

I guess that in that case, xmin, xmax, ymin and ymax should be class variables 
of zone, right?

> If *any* object, class or instance of a class (or module or whatever) 
> contains another, access is by chaining the dots.
>   OuterOb.InnerOb.attribute
> 
> Hope that answers your question.

This should help, I'll make some tests.

Thanks.

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


Re: Classes in a class: how to access variables from one in another

2010-10-18 Thread fab
Jean-Michel Pichavant  wrote:
> Always post working code, or at least something we can paste in the 
> python interpreter (even if it's buggy)

Ok, noted.

> class A:
>class B:
>x=2
>class C:
>def __init__(self):
>print A.B.x
> 
> c = A.C()
> 
> >2

Good, thanks.

I've made further tests and here is what I have come to:

class zoneDeDessin(gtk.DrawingArea):
  xmin = -5.5
  xmax = 5.5
  ymin = -5.5
  ymax = 5.5

  class Handle:
def __init__(self, xEcran, yEcran):
  (self.xEcran, self.yEcran) = (xEcran, yEcran)
  (self.xRepere, self.yRepere) = (zoneDeDessin.XdeLEcranAuRepere(xEcran), 
zoneDeDessin.YdeLEcranAuRepere(yEcran))

  def __init__(self):  
gtk.DrawingArea.__init__(self)
(self.largeur, self.hauteur) = (0,0)

  def expose(self, widget, event):
rect = self.get_allocation()
(self.largeur, self.hauteur) = (rect.width, rect.height)
  
  def XdeLEcranAuRepere(self, x):
return 
zoneDeDessin.xmin+x*(zoneDeDessin.xmax-zoneDeDessin.xmin)/float(self.largeur)   
  def YdeLEcranAuRepere(self, y):
return 
zoneDeDessin.ymax+y*(zoneDeDessin.ymin-zoneDeDessin.ymax)/float(self.hauteur)

I have further code that should add a Handle when I double-click on
the screen.

However I get this error when executing:

Traceback (most recent call last):
  File "./zoneDeDessin.py", line 161, in boutonRelache
zoneDeDessin.Courbe.ajouterUnHandle(self.courbeCourante, event.x, event.y)
  File "./zoneDeDessin.py", line 36, in ajouterUnHandle
self.listeDesPoints.append(zoneDeDessin.Handle(xEcran, yEcran))
  File "./zoneDeDessin.py", line 22, in __init__
(self.xRepere, self.yRepere) = (zoneDeDessin.XdeLEcranAuRepere(xEcran), 
zoneDeDessin.YdeLEcranAuRepere(yEcran))
TypeError: unbound method XdeLEcranAuRepere() must be called with zoneDeDessin 
instance as first argument (got float instance instead)

I understand the error (XdeLEcranAuRepere is an instance method, not a
class method), but I don't see how to fix it, except by making largeur
and hauteur class variables...

Any hint?

Thanks!

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


Re: Classes in a class: how to access variables from one in another

2010-10-18 Thread fab
Christian Heimes  wrote:
> Don't nest classes. Just don't. This might be a valid and good approach
> in some programming languages but it's not Pythonic. Your code can
> easily be implemented without nested classes.

I think you're right. It would have been more aesthetically pleasant
to me (a Handle has only sense on a drawing zone), but if it's more
complicated I'll avoid nested classes.

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