Re: KeyError: '13'

2012-06-06 Thread Phanindra Ramesh Challa
I got metatype compiled. Thanks for your help

On Wed, Jun 6, 2012 at 6:44 PM, Peter Otten <__pete...@web.de> wrote:

> Phanindra Ramesh Challa wrote:
>
> [Please hit "reply-all" in you email client when you answer a post. That
> way
> it will appear on the mailing list and give more people a chance to answer]
>
> > output of the is just the line
> > "sizes".
>
> >> and then decide if you to need to change the lookup key or to add
> records
> >> to the database.
> >>
> > How can I add recors to the database?
>
> The Python part of the answer is
>
> db[some_key] = some_value
>
> but I fear that won't help. Google suggests you are struggling with the
> metatype project If so, there seems to be a corresponding mk_db.py that you
> can use. I don't know anything about metatype, but the comment in mk_db.py
> at
>
> http://metatype.cvs.sourceforge.net/viewvc/metatype/metatype/mk_db.py?revision=1.3&view=markup
>
> tells to run it like so:
>
> find . -name '*.ugs' | python mk_db.py -a glyphlist -o dbmfile
>
> Perhaps you ran it, but there weren't any *.ugs files?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem python

2012-06-06 Thread Aldrich DeMata
Python can't find site.py. It's either missing when you installed python,
it was deleted, or it's located in another directory. Run  >>> find /
-iname 'site.py' , sudo if needed, and add the location of site.py in your
python search path.

The python online docs has a good documentation on python search path:

http://docs.python.org/install/index.html#search-path


On Wed, Jun 6, 2012 at 6:25 AM, Tom King  wrote:

> hi im new in python and i have a problem about
> when i type python in my command line console i get an error message
>
>
> 'import site' failed; use -v for traceback
> Python 2.4.3 (#1, May  5 2011, 18:44:23)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>>
>
> what is going on and how i can fix it
> thanks in andvance
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adopting ‘lockfile’

2012-06-06 Thread Ben Finney
Ben Finney  writes:

> Thanks for the suggestion. I've imported it from Subversion into Bazaar
> (my preferred DVCS), and it went smoothly.

Development of ‘python-lockfile’ now proceeds on Alioth
https://alioth.debian.org/projects/python-lockfile/>.

> I will proceed with a handover from Skip for maintenance of ‘lockfile’.
>
> This will definitely need more people than me to maintain, though!

Thanks to Aaron Maxwell for joining the development team, with initial
progress to a Python 3 port of the library.

> I am interested and motivated to work on the Linux platform, but other
> platforms will suffer unless I get co-maintainers with experience in
> the different file locking semantics there.

We still need people capable of working with this library on non-Linux
platforms. Join the development discussion mailing list
https://lists.alioth.debian.org/mailman/listinfo/python-lockfile-devel>
if you can offer us assistance with this.

-- 
 \ “I was trying to daydream, but my mind kept wandering.” —Steven |
  `\Wright |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Compare 2 times

2012-06-06 Thread t_texas
On Jun 6, 7:50 am, loial  wrote:
> I have a requirement to test the creation time of a file with the
> current time and raise a message if the file is  more than 15 minutes
> old.
>
> Platform is Unix.
>
> I have looked at using os.path.getctime for the file creation time and
> time.time() for the current time, but is this the best approach?

Unless you are using ext4 you are going to have to store the creation
time yourself.  If the files are coming from your application, use the
sqlite3 or shelve module to store the creation time for each file then
check that data to determine which files are more than 15 minutes old.

If the files are being generated from another application or from
users, have your application check the directory every minute and
record any new files.  Worst case scenario, a file will be present for
16 minutes vs 15.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-ideas] changing sys.stdout encoding

2012-06-06 Thread Rurpy
On 06/06/2012 10:09 AM, MRAB wrote:
> On 06/06/2012 08:09, Rurpy wrote:
>> On 06/05/2012 05:56 PM, MRAB wrote:
>>>  On 06/06/2012 00:34, Victor Stinner wrote:
  2012/6/5 Rurpy:
>   In my first foray into Python3 I've encountered this problem:
>   I work in a multi-language environment.  I've written a number
>   of tools, mostly command-line, that generate output on stdout.
>   Because these tools and their output are used by various people
>   in varying environments, the tools all have an --encoding option
>   to provide output that meets the needs and preferences of the
>   output's ultimate consumers.
[snip]
>   In converting them to Python3, I found the best (if not very
>   pleasant) way to do this in Python3 was to put something like
>   this near the top of each tool[*1]:
>
> import codecs
> sys.stdout = codecs.getwriter(opts.encoding)(sys.stdout.buffer)

>>>  In Python 3, you should use io.TextIOWrapper instead of
>>>  codecs.StreamWriter. It's more efficient and has less bugs.
>>>
>    What I want to be able to put there instead is:
> 
>  sys.stdout.set_encoding (opts.encoding)
[snip]
>>>  And if you _do_ want multiple encodings in a file, it's clearer to open
>>>  the file as binary and then explicitly encode to bytes and write _that_
>>>  to the file.
>>
>> But is it really?
>>
>> The following is very simple and the level of python
>> expertise required is minimal.  It (would) works fine
>> with redirection.  One could substitute any other ordinary
>> open (for write) text file for sys.stdout.
>>
>>[off the top of my head]
>>text = 'This is %s text: 世界へ、こんにちは!'
>>sys.stdout.set_encoding ('sjis')
>>print (text % 'sjis')
>>sys.stdout.set_encoding ('euc-jp')
>>print (text % 'euc-jp')
>>sys.stdout.set_encoding ('iso2022-jp')
>>print (text % 'iso2022-jp')
>>
>> As for your suggestion, how do I reopen sys.stdout in
>> binary mode?  I don't need to do that often and don't
>> know off the top of my head.  (And it's too late for
>> me to look it up.)  And what happens to redirected output
>> when I close and reopen the stream?  I can open a regular
>> filename instead.  But remember to make the last two
>> opens with "a" rather than "w".  And don't forget the
>> "\n" at the end of the text line.
>>
>> Could you show me an code example of your suggestion
>> for comparison?
>>
>> Disclaimer: As I said before, I am not particularly
>> advocating for a for a set_encoding() method -- my
>> primary suggestion is a programatic way to change the
>> sys.std* encodings prior to first use.  Here I am just
>> questioning the claim that a set_encoding() method
>> would not be clearer than existing alternatives.
>>
> This example accesses the underlying binary output stream:
> 
> 
> # -*- coding: utf-8 -*-
> 
> import sys
> 
> class Writer:
>  def __init__(self, output):
>  self.output = output
>  self.encoding = output.encoding
>  def write(self, string):
>  self.output.buffer.write(string.encode(self.encoding))
>  def set_encoding(self, encoding):
>  self.output.buffer.flush()
>  self.encoding = encoding
> 
> sys.stdout = Writer(sys.stdout)
> 
> initial_encoding = sys.stdout.encoding
> 
> text = 'This is %s text: 世界へ、こんにちは!'
> sys.stdout.set_encoding('utf-8')
> print (text % 'utf-8')
> sys.stdout.set_encoding('sjis')
> print (text % 'sjis')
> sys.stdout.set_encoding('euc-jp')
> print (text % 'euc-jp')
> sys.stdout.set_encoding('iso2022-jp')
> print (text % 'iso2022-jp')
> 
> sys.stdout.set_encoding(initial_encoding)

OK, let's see if I've got this right...

You take a duplicate of my code, add a class with three
methods and some other statements and you claim the result 
is clearer and simpler than my code?

That is, union (A, B) is simpler than A?

Interesting definition of simpler you've got there :-)

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


Re: UDPSock.recvfrom(buf) ??????? (buf) ???

2012-06-06 Thread MRAB

On 06/06/2012 22:12, Diego Uribe Gamez wrote:
> Estoy mirando una conexión por Soket y no logro entender que hace el
> buf? que es lo que cambia? el numero que se le asigna es que?
>
> |host=  "localhost"
>
> port=  21567
>
> buf=  1024
>
> data=  ''
>
> addr=  (host,  port)
>
> UDPSock  =  socket(AF_INET,  SOCK_DGRAM)|
>
> |while  (1):
>
>data, addr = UDPSock.recvfrom(buf)|
>
'buf' is the size of the buffer. 'recvfrom' reads at most 'buf' bytes
('bufsize' would be a better name) and returns them as the bytestring
'data'.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Compare 2 times

2012-06-06 Thread Jon Clements

On 06/06/12 14:39, Christian Heimes wrote:

Am 06.06.2012 14:50, schrieb loial:

I have a requirement to test the creation time of a file with the
current time and raise a message if the file is  more than 15 minutes
old.

Platform is Unix.

I have looked at using os.path.getctime for the file creation time and
time.time() for the current time, but is this the best approach?


Lots of people are confused by ctime because they think 'c' stands for
change. That's wrong. st_ctime is status change time. The ctime is
updated when you change (for example) owner or group of a file, create a
hard link etc. POSIX has no concept of creation time stamp.

Christian


I haven't thought this through too much, but perhaps an ugly 
"work-around" would be to use inotify (in some kind of daemon) to watch 
for the IN_CREATE events and store the crtime in a personal DB. Then 
possibly look at some sort of scheduling to fulfil what happens after 15 
minutes.


I'm sure there's subtleties I'm missing, but just thought it could be 
useful.


Jon.

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


ANN: Tracerlib 0.1 Released

2012-06-06 Thread Calvin Spealman
Tracerlib is a set of utilities to make tracing Python code easier.

It provides TracerManager, which can allow multiple trace functions to
coexist. It can easily be enabled and disabled, either manually or as
a context manager in a with statement.

Tracer classes make handling the different trace events much easier.

class TraceExceptions(Tracer):
def trace_exception(self, func_name, exctype, value, tb):
print "Saw an exception: %r" % (value,)

Tracer is also easily capable of filtering which events it listens to.
It accepts both an events parameter, a list of trace events it will
respond to, and a watch parameter, a list of paths it will respond to
in the form of package.module.class.function.

This can easily wrap a trace function, or you can subclass Tracer and
implement one of its helpful trace_*() methods.

And, a helper class FrameInspector which wraps a frame and makes it
trivial to inspect the function name and arguments the function had
been called with.

inspector = FrameInspector(sys._getframe())

print "Called", inspector.func_name
print "args:", inspector.args
print "kwargs:", inspector.kwargs

You can read the full documentation at the read the docs site and see
the code at github.

http://tracerlib.readthedocs.org
https://github.com/ironfroggy/tracerlib
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: usenet reading

2012-06-06 Thread Colin Higwell
On Sun, 03 Jun 2012 16:25:53 +0200, Matej Cepl wrote:

> Yes, Pan is better, but it used to have some rough edges
> (e.g., it's offline qualities were a bit elusive)

I wouldn't know about that. My connection is always-on.

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


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread Terry Reedy

On 6/6/2012 4:56 PM, Jerry Hill wrote:

On Wed, Jun 6, 2012 at 4:10 PM, Alec Ross  wrote:

FWIW, English idiomatic usage includes "see overleaf", and "see over", for
the obverse side of a page/sheet, i.e, the following page; and "see facing
page", w/ the obvious meaning.


For what it's worth, I've never seen either of those constructs ("see
overleaf" and "see over").  Are they perhaps more common in a
particular academic context, or possibly more common in places that
use "British English" spellings rather than "American English"?
Typically I've just seen "see other side", or (very occasionally) "see
reverse" and "see obverse".


While this nice document is intended to be printed on two sides of one 
card or sheet, it may also get printed on two sheets.

'see page 1' and 'see page 2' will work either way

--
Terry Jan Reedy

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


Re: what gui designer is everyone using

2012-06-06 Thread Colin Higwell
On Tue, 05 Jun 2012 10:10:17 -0400, Mark R Rivet wrote:

> I want a gui designer that writes the gui code for me. I don't want to
> write gui code. what is the gui designer that is most popular?
> I tried boa-constructor, and it works, but I am concerned about how
> dated it seems to be with no updates in over six years.

I'm using wxGlade,and am very happy with it.

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


RE: Searching 2 Strings in A File

2012-06-06 Thread Prasad, Ramit
> > Dave, actually it's a 2 arguments. Sorry, i did not make it clear in my
> question. I used Ramit's hint and it worked. The code should be as follows:
> >
> > pattern = "({0}|{1})".format(x,y)
> > cmd_line = Popen(["egrep", pattern, aLogFile], stdout=PIPE, stdin=PIPE,
> stderr=STDOUT)
> >
> > Regards,
> >
> > Do Nguyen
> >
> 
> If it's two arguments, then  you need pattern1 and pattern2.
> 
> But if it works, perhaps egrep is happy with a single argument.

egrep is happy with a single arugment because '(1|2)' is a regular 
expression meaning match either 1 or 2.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Searching 2 Strings in A File

2012-06-06 Thread Dave Angel
On 06/06/2012 05:10 PM, dohoang4...@comcast.net wrote:
> hi all, 
(You forgot to include the list in your reply)

> 
> thanks a lot for your quick response. 
> 
> Dave, actually it's a 2 arguments. Sorry, i did not make it clear in my 
> question. I used Ramit's hint and it worked. The code should be as follows: 
> 
> pattern = "({0}|{1})".format(x,y) 
> cmd_line = Popen(["egrep", pattern, aLogFile], stdout=PIPE, stdin=PIPE, 
> stderr=STDOUT) 
> 
> Regards, 
> 
> Do Nguyen 
> 

If it's two arguments, then  you need pattern1 and pattern2.

But if it works, perhaps egrep is happy with a single argument.

-- 

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


Re: How to exec() a string like interactive python does?

2012-06-06 Thread News123

On 06/06/2012 01:31 AM, Steven D'Aprano wrote:

On Wed, 06 Jun 2012 00:12:00 +0200, News123 wrote:


If I start Python in interactive mode, and I yype the commands,
'a=3', 'a', 'print a'


Then the  output would look like:
  >>>  a = 3
  >>>  a
3
  >>>   print a
3


Now within an application I'd like to achieve exactly this behaviour


Before you reinvent the wheel, see the cmd and code modules.

http://docs.python.org/library/cmd.html
http://docs.python.org/library/code.html



Thanks a lot.

The cmd library looks great at a first glance
However somehow I fail to get it working (meaning, that it executes 
following strings identical to the python interactive shell

'a = 3'
'a'
'print a'

It reports errors for each line,

The code snippet I tried:


import cmd

class MyCmd(cmd.Cmd):
pass

my_cmd = MyCmd()
my_cmd.cmdloop()




I assume the part, that I don't understand is
Cmd.identchars and the concept of a command prefix.

Perhaps it's the fact, that my native language is not English, but 
folowing sentence doesn't make a lot of sense to me:


" A command is parsed out of each line by collecting the prefix composed
  of characters in the identchars member."

It almost seems as if the part of executing valid 'python strings'  is 
missing and had to be added manually to the default() method


> help(cmd)

   Interpreters constructed with this class obey the following conventions:

1. End of file on input is processed as the command 'EOF'.
2. A command is parsed out of each line by collecting the prefix composed
   of characters in the identchars member.
3. A command `foo' is dispatched to a method 'do_foo()'; the do_ method
   is passed a single argument consisting of the remainder of the line.
4. Typing an empty line repeats the last command.  (Actually, it calls the
   method `emptyline', which may be overridden in a subclass.)
5. There is a predefined `help' method.  Given an argument `topic', it
   calls the command `help_topic'.  With no arguments, it lists all topics
   with defined help_ functions, broken into up to three topics; documented
   commands, miscellaneous help topics, and undocumented commands.
6. The command '?' is a synonym for `help'.  The command '!' is a synonym
   for `shell', if a do_shell method exists.
7. If completion is enabled, completing commands will be done automatically,
   and completing of commands args is done by calling complete_foo() with
   arguments text, line, begidx, endidx.  text is string we are matching
   against, all returned matches must begin with it.  line is the current
   input line (lstripped), begidx and endidx are the beginning and end
   indexes of the text being matched, which could be used to provide
   different completion depending upon which position the argument is in.

The `default' method may be overridden to intercept commands for which there
is no do_ method.

The `completedefault' method may be overridden to intercept completions for
commands that have no complete_ method.

The data member `self.ruler' sets the character used to draw separator lines



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


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread Jerry Hill
On Wed, Jun 6, 2012 at 4:10 PM, Alec Ross  wrote:
> FWIW, English idiomatic usage includes "see overleaf", and "see over", for
> the obverse side of a page/sheet, i.e, the following page; and "see facing
> page", w/ the obvious meaning.

For what it's worth, I've never seen either of those constructs ("see
overleaf" and "see over").  Are they perhaps more common in a
particular academic context, or possibly more common in places that
use "British English" spellings rather than "American English"?
Typically I've just seen "see other side", or (very occasionally) "see
reverse" and "see obverse".

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


Need to build Python 2.6 with Microsoft Visual Studio 2010

2012-06-06 Thread Tom Kacvinsky
For reasons beyond my control, I have a need to build Python 2.6 with
MSVC 2010 (for x64).  Does anyone have any hints as to how I can
accomplish this?  I see there are instructions for building Python 2.7
with MSVC 2010, but those are using the Python 2.7 source as the base
for patching, so they do me little to no good.  My main tripping point
right now is the deprecation of vcbuild (replaced by msbuild).  I can
run msbuild on the 2.6 solution files:

msbuild PCbuild\pcbuild.sln /p:Configuration="Release" /
p:platform="x64"

but I get all sorts of errors, related to vcbuild not being
available.  Will this be as simple as getting the project/solution
files up to date for MSVC 2010, or am I going to have to patch a lot
of code?

Any and all help is much appreciated

Thanks in advance,

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


Re: English version for M�mento Python 3 (draft, readers needed)

2012-06-06 Thread Alec Ross
In message <4fcf9108$0$16471$426a7...@news.free.fr>, Laurent Pointal 
 writes

Ulrich Eckhardt wrote:


Am 05.06.2012 19:32, schrieb Laurent Pointal:


...




"see verso for string formatting..." - what is "verso"?


Modified with Paul indications (its the "other side" in french - from
latin).



FWIW, English idiomatic usage includes "see overleaf", and "see over", 
for the obverse side of a page/sheet, i.e, the following page; and "see 
facing page", w/ the obvious meaning.


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


Re: ttk.Spinbox missing?

2012-06-06 Thread Terry Reedy

On 6/6/2012 9:06 AM, Mark Summerfield wrote:


I have Python 3.2 with Tcl/Tk 8.5, but there doesn't seem to be a
ttk.Spinbox widget even though that widget is part of Tcl/Tk 8.5:
http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_spinbox.htm

Why is that?


My guess is that Spinbox was not present when tkinter.ttk was written 
(perhaps back in 8.4+tile days). I presume the intention has been to 
expand the set of themed widgets. Or perhaps it was simply overlooked. I 
suggest you open an issue on the tracker: "Update tkinter.ttk" and put 
'gpolo' and 'terry.reedy' on the nosy list. If you can, check if 
anything else is missing and put a list in the message.


If you look at the formulaic code for other ttk widgets in 
tkinter.ttk.py and you understand the Spinbox doc you referenced,

http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_spinbox.htm
and that of one of the other ttk widgets, you should be able to 
contribute a patch.


--
Terry Jan Reedy

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


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread Jugurtha Hadjar

On 06/06/2012 07:45 PM, MRAB wrote:

On 06/06/2012 18:23, Jugurtha Hadjar wrote:
[snip]

"range returns a « generator », convert it to list to see.." -->
"converts" instead of "convert".


No, "convert" is correct here; it's the imperative, i.e. "convert it to
a list if you want to see...".


My bad. I didn't understand it that way because of the comma before it.


--
~Jugurtha Hadjar,
--
http://mail.python.org/mailman/listinfo/python-list


Re: file pointer array

2012-06-06 Thread Jon Clements

On 06/06/12 19:51, MRAB wrote:

On 06/06/2012 19:28, Jon Clements wrote:

On 06/06/12 18:54, Prasad, Ramit wrote:

data= []
for index in range(N, 1): # see Chris Rebert's comment
with open('data%d.txt' % index,'r') as f:
data.append( f.readlines() )



I think "data.extend(f)" would be a better choice.


.extend does something different, and "range(N, 1)" is an empty range
if N > 0.


Mea culpa -  I had it in my head the OP wanted to treat the files as one 
contiguous one. So yeah:


# something equiv to... (unless it is definitely a fixed range in which
# case (x)range can be used)
data = [ list(open(fname)) for fname in iglob('/home/jon/data*.txt') ]

# then if they ever need to treat it as a contiguous sequence...
all_data = list(chain.from_iterable(data))

Jon.




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


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread Andrew Berg
On 6/6/2012 1:45 PM, MRAB wrote:
> On 06/06/2012 18:23, Jugurtha Hadjar wrote:
> [snip]
>> "range returns a « generator », convert it to list to see.." -->
>> "converts" instead of "convert".
>>
> No, "convert" is correct here; it's the imperative, i.e. "convert it to
> a list if you want to see...".
> 
>> "frequently used in for iterative loops" -->  .. I think it should be
>> "frequently used in "for" iterative loops" .. It is confusing if there
>> are no quotes, because "for" is a meaningful english word.. So
>> specifying you are talking about the reserved word would be nice.
>>
>> "storage of data on disk, and read back" -->  maybe something like
>> "storing data on disk, and reading it back".
>>
>> "memorisation" -->  "memorization" (most used spelling).
>>
>> "initialisation" -->  "initialization"
>>
> Some use "s", others use "z". As long as it's consistent, I wouldn't 
> worry too
> much about it.

https://en.wikipedia.org/wiki/American_and_British_English_spelling_differences
American English is more common, but either is acceptable as long as
it's consistent.

-- 
CPython 3.3.0a3 | Windows NT 6.1.7601.17790
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: file pointer array

2012-06-06 Thread MRAB

On 06/06/2012 19:28, Jon Clements wrote:

On 06/06/12 18:54, Prasad, Ramit wrote:

 data= []
 for index in range(N, 1): # see Chris Rebert's comment
  with open('data%d.txt' % index,'r') as f:
  data.append( f.readlines() )



I think "data.extend(f)" would be a better choice.


.extend does something different, and "range(N, 1)" is an empty range
if N > 0.
--
http://mail.python.org/mailman/listinfo/python-list


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread MRAB

On 06/06/2012 18:23, Jugurtha Hadjar wrote:
[snip]

"range returns a « generator », convert it to list to see.." -->
"converts" instead of "convert".


No, "convert" is correct here; it's the imperative, i.e. "convert it to
a list if you want to see...".


"frequently used in for iterative loops" -->  .. I think it should be
"frequently used in "for" iterative loops" .. It is confusing if there
are no quotes, because "for" is a meaningful english word.. So
specifying you are talking about the reserved word would be nice.

"storage of data on disk, and read back" -->  maybe something like
"storing data on disk, and reading it back".

"memorisation" -->  "memorization" (most used spelling).

"initialisation" -->  "initialization"

Some use "s", others use "z". As long as it's consistent, I wouldn't 
worry too

much about it.
--
http://mail.python.org/mailman/listinfo/python-list


RE: file pointer array

2012-06-06 Thread Prasad, Ramit
> > data= []
> > for index in range(N, 1): # see Chris Rebert's comment
> >  with open('data%d.txt' % index,'r') as f:
> >  data.append( f.readlines() )
> >
> 
> I think "data.extend(f)" would be a better choice.

Wouldn't that concatenate the data from each file
rather than keeping each file's data separate? 

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: file pointer array

2012-06-06 Thread Jon Clements

On 06/06/12 18:54, Prasad, Ramit wrote:

data= []
for index in range(N, 1): # see Chris Rebert's comment
 with open('data%d.txt' % index,'r') as f:
 data.append( f.readlines() )



I think "data.extend(f)" would be a better choice.

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


Re: Compare 2 times

2012-06-06 Thread Nobody
On Wed, 06 Jun 2012 05:50:02 -0700, loial wrote:

> I have a requirement to test the creation time of a file with the current
> time and raise a message if the file is  more than 15 minutes old.
> 
> Platform is Unix.
> 
> I have looked at using os.path.getctime for the file creation time and
> time.time() for the current time, but is this the best approach?

Most Unix filesystems don't store a "creation" time.

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


RE: file pointer array

2012-06-06 Thread Prasad, Ramit
> >
> > I have a simple question. I wish to generate an array of file pointers.
> > For example, I have files:
> >
> > data1.txt
> > data2.txt
> > data3.txt
> > 
> >
> > I wish to generate fine pointer array so that I can read the files at
> > the same time.
> >
> > for index in range(N):
> >  fid[index] = open('data%d.txt' % index,'r')

> You can let the files be closed by the garbage collector, but there is no
> guarantee that this will happen in a timely manner. Best practice is to
> close them manually once you are done.

I personally prefer to use context managers when possible. Not sure it
will work for you, but I would probably just read all data into memory 
first by doing. I imagine file access would be faster read all at once
rather than switching between files unless you have very large files.

data= []
for index in range(N, 1): # see Chris Rebert's comment
with open('data%d.txt' % index,'r') as f:
data.append( f.readlines() )


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread Laurent Pointal
Ulrich Eckhardt wrote:

> Am 05.06.2012 19:32, schrieb Laurent Pointal:
>> I started a first translation of my document originally in french. Could
>> some fluent english people read it and indicate errors or bad english
>> expressions.
> 
> Just one note up front: Languages or nationalities are written with
> uppercase letters, like English and French. Other common faults of that
> category are days of the week (Monday..) and month names (January..),
> although that's irrelevant for your doc.

I modified slightly the page, to have first links in the page for document 
downloading (and other links deeper in the page).

> Another thing I noticed that was missing was that the "in" keyword can
> not only be used to iterate over a sequence (for i in seq:...) but also
> to test if something is contained in a sequence (if i in seq:...).

I reworked the second page to have less informations about string formating 
(nice, but less important than operations on containers), and add sections 
on containers, lists, dictionaries and set.

> "don't miss to close file after use": Use a "with" statement.

Added as a note.

> "see verso for string formatting..." - what is "verso"?

Modified with Paul indications (its the "other side" in french - from 
latin).

> "dont" -> "don't"

Done.

> "char strings" -> "strings" (in the context of indexing, byte strings
> have the same syntax)

Modified (even if I dont teach byte strings with my students).

> "with several else if, else if..." - there is no "else if" but "elif".

Modified (it was originally a translation from french, but the 
correcpondance between english version and keywords can be confusing).

> "block else for other cases" - this sounds as if it was blocking the
> else. Maybe "else-block for other cases", but English hyphenation is
> complicated and I'm not sure.

Modified to "else block..."

Thanks for your reading and comments.

A+
Laurent.

-- 
Laurent POINTAL - laurent.poin...@laposte.net

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


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread Jugurtha Hadjar

On 06/05/2012 06:32 PM, Laurent Pointal wrote:

Hello,

I started a first translation of my document originally in french. Could
some fluent english people read it and indicate errors or bad english
expressions.

http://perso.limsi.fr/pointal/python:memento

Thanks.
A+
Laurent.



Very nice !

Some additions..

"formating" --> "formatting"

In the french version: "Parcours des index de la séquence" .. In the 
english one "Go over sequence's index" .. I think it would be"Go over 
sequence's indexes".


In the upper left corner, the "sigma" sign for the sum of the squares. 
i=1, i=100... It should be "100" alone, not "i=100". The "i" is written 
only on the bottom of sigma.


"strings formating" --> "string formatting"


"range returns a « generator », convert it to list to see.." --> 
"converts" instead of "convert".


"frequently used in for iterative loops" --> .. I think it should be 
"frequently used in "for" iterative loops" .. It is confusing if there 
are no quotes, because "for" is a meaningful english word.. So 
specifying you are talking about the reserved word would be nice.


"storage of data on disk, and read back" --> maybe something like 
"storing data on disk, and reading it back".


"memorisation" --> "memorization" (most used spelling).

"initialisation" --> "initialization"


Thanks you,

--
~Jugurtha Hadjar,
--
http://mail.python.org/mailman/listinfo/python-list


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread Laurent Pointal
Paul Rubin wrote:

> Laurent Pointal  writes:
>>> There are a few other things like that, and I'll try to look more
>>> carefully tonight, I can't spend more time on it right now.
>> I updated the document into 1.0.5a (and fix some other errors).
> 
> A few more things:
> 


> In "Files"
>   don't miss to close file => don't forget to close the file
> [but you might mention the "with" statement]

I put it as a note (I want students to have file closing in mind, they may 
practice other languages without such constructions).

> In "Function definition"
>   bloc => block
>   (« black box »)  => ("black box")
> [English speakers may not recognize « » symbols]

I switched them to "". 
Its here to remember students that they can't access to functions internal 
variables from outside (and should try to limit access to outside world from 
inside functions) - a common error during practice. In lecture class, I try 
to show them only two access points to functions: parameters and return 
value, and a "black box" blocking all other informations.

> In "Generator of int sequences"
>   You might mention that range makes a generator only in Python 3.
>   In Python 2 it makes an actual list and xrange makes a generator.

We teach with Python3 as several modifications in the language are better 
for leaning programming basics (like 1/2 => 0.5).

Thanks for all your corrections, I'll continue with other posters.

A+
L.Pointal.

-- 
Laurent POINTAL - laurent.poin...@laposte.net

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


Re: [Python-ideas] changing sys.stdout encoding

2012-06-06 Thread MRAB

On 06/06/2012 08:09, Rurpy wrote:

On 06/05/2012 05:56 PM, MRAB wrote:

 On 06/06/2012 00:34, Victor Stinner wrote:

 2012/6/5 Rurpy:

  In my first foray into Python3 I've encountered this problem:
  I work in a multi-language environment.  I've written a number
  of tools, mostly command-line, that generate output on stdout.
  Because these tools and their output are used by various people
  in varying environments, the tools all have an --encoding option
  to provide output that meets the needs and preferences of the
  output's ultimate consumers.


 What happens if the specified encoding is different than the encoding
 of the console? Mojibake?

 If the output is used as in the input of another program, does the
 other program use the same encoding?

 In my experience, using an encoding different than the locale encoding
 for input/output (stdout, environment variables, command line
 arguments, etc.) causes various issues. So I'm curious of your use
 cases.


  In converting them to Python3, I found the best (if not very
  pleasant) way to do this in Python3 was to put something like
  this near the top of each tool[*1]:

import codecs
sys.stdout = codecs.getwriter(opts.encoding)(sys.stdout.buffer)


 In Python 3, you should use io.TextIOWrapper instead of
 codecs.StreamWriter. It's more efficient and has less bugs.


  What I want to be able to put there instead is:

sys.stdout.set_encoding (opts.encoding)


 I don't think that your use case merit a new method on
 io.TextIOWrapper: replacing sys.stdout does work and should be used
 instead. TextIOWrapper is generic and your use case if specific to
 sys.std* streams.

 It would be surprising to change the encoding of an arbitrary file
 after it is opened. At least, I don't see the use case.


 [snip]

 And if you _do_ want multiple encodings in a file, it's clearer to open
 the file as binary and then explicitly encode to bytes and write _that_
 to the file.


But is it really?

The following is very simple and the level of python
expertise required is minimal.  It (would) works fine
with redirection.  One could substitute any other ordinary
open (for write) text file for sys.stdout.

   [off the top of my head]
   text = 'This is %s text: 世界へ、こんにちは!'
   sys.stdout.set_encoding ('sjis')
   print (text % 'sjis')
   sys.stdout.set_encoding ('euc-jp')
   print (text % 'euc-jp')
   sys.stdout.set_encoding ('iso2022-jp')
   print (text % 'iso2022-jp')

As for your suggestion, how do I reopen sys.stdout in
binary mode?  I don't need to do that often and don't
know off the top of my head.  (And it's too late for
me to look it up.)  And what happens to redirected output
when I close and reopen the stream?  I can open a regular
filename instead.  But remember to make the last two
opens with "a" rather than "w".  And don't forget the
"\n" at the end of the text line.

Could you show me an code example of your suggestion
for comparison?

Disclaimer: As I said before, I am not particularly
advocating for a for a set_encoding() method -- my
primary suggestion is a programatic way to change the
sys.std* encodings prior to first use.  Here I am just
questioning the claim that a set_encoding() method
would not be clearer than existing alternatives.


This example accesses the underlying binary output stream:


# -*- coding: utf-8 -*-

import sys

class Writer:
def __init__(self, output):
self.output = output
self.encoding = output.encoding
def write(self, string):
self.output.buffer.write(string.encode(self.encoding))
def set_encoding(self, encoding):
self.output.buffer.flush()
self.encoding = encoding

sys.stdout = Writer(sys.stdout)

initial_encoding = sys.stdout.encoding

text = 'This is %s text: 世界へ、こんにちは!'
sys.stdout.set_encoding('utf-8')
print (text % 'utf-8')
sys.stdout.set_encoding('sjis')
print (text % 'sjis')
sys.stdout.set_encoding('euc-jp')
print (text % 'euc-jp')
sys.stdout.set_encoding('iso2022-jp')
print (text % 'iso2022-jp')

sys.stdout.set_encoding(initial_encoding)
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] TZ-aware local time

2012-06-06 Thread MRAB

On 06/06/2012 07:10, Ben Finney wrote:

MRAB  writes:


 datetime objects would consist of the UTC time, time zone and DST.


“time zone” information always entails DST information doesn't it? It
isn't proper time zone information if it doesn't tell you about DST.

That is, when you know the full time zone information, that includes
when (if ever) DST kicks on or off.

Or have I been spoiled by the Olsen database?


I was thinking timezone as the base offset from UTC; DST would be an
additional offset which changes (or may change) twice a year.
--
http://mail.python.org/mailman/listinfo/python-list


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread MRAB

On 06/06/2012 12:08, Tim Wintle wrote:

On Wed, 2012-06-06 at 12:03 +0200, Ulrich Eckhardt wrote:

 "block else for other cases" - this sounds as if it was blocking the
 else. Maybe "else-block for other cases",


I would say "else block". "else-block" is grammatically correct too, but
I don't think I've seen it used regularly.


If the "else" could be in a different colour, that may be clearer.


RE: the order - "else" is being used as an adjective to clarify the noun
"block" - in English the adjective comes before the noun (unlike a lot
of European languages)

e.g. we say "the red book", not "the book red", where the French would
say "livre rouge" (I believe).

If you want to put an adjective after the noun (for poetical reasons
etc) then there needs to be another clause. e.g. "the book, which was
red"


 but English hyphenation is complicated and I'm not sure.


You're German and you say English hyphenation is complicated! ;-)

Tim



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


Re: Compare 2 times

2012-06-06 Thread William R. Wing (Bill Wing)
On Jun 6, 2012, at 9:45 AM, William R. Wing (Bill Wing) wrote:

> On Jun 6, 2012, at 9:29 AM, Alain Ketterlin wrote:
> 
>> loial  writes:
>> 
>>> I have a requirement to test the creation time of a file with the
>>> current time and raise a message if the file is  more than 15 minutes
>>> old.
>>> Platform is Unix.
>>> I have looked at using os.path.getctime for the file creation time and
>>> time.time() for the current time, but is this the best approach?
>> 
>> No. getctime() returns the last "change" time. The creation time is not
>> kept anywhere. This may still match your requirement though. And os.path
>> is the right package to look at for such tasks.
>> 
>> -- Alain.
>> -- 
>> http://mail.python.org/mailman/listinfo/python-list
> 
> If you REALLY want the creation time rather than the last time the file was 
> touched, you will probably have to invoke the subprocess module and call ls 
> -U, something like the following:
> 
> creation_time = subprocess.check_output(["ls", "-U", 
> string_filename_variable])
> 
> The -U option isn't universal, but it does exist in most of the UNIces I'm 
> familiar with.
> 
> -Bill

Addendum, with apologies - I was in too much of a hurry.  The -U option has to 
be used with -l (at least on my system).  Thus, it would be ls -lU.  
Alternatively, you could "stat" the file in a subprocess and then parse the 
return data to extract the creation date.

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


Re: Compare 2 times

2012-06-06 Thread Alain Ketterlin
Alain Ketterlin  writes:

> loial  writes:
>
>> I have a requirement to test the creation time of a file with the
>> current time and raise a message if the file is  more than 15 minutes
>> old.
>> Platform is Unix.
>> I have looked at using os.path.getctime for the file creation time and
>> time.time() for the current time, but is this the best approach?
>
> No. getctime() returns the last "change" time. The creation time is not
> kept anywhere. This may still match your requirement though. And os.path
> is the right package to look at for such tasks.

Sorry, it may happen that the filesystem you're working with provides
that time, in which case it's called birthtime. This _may_ be available
via os.stat().

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


Re: Compare 2 times

2012-06-06 Thread William R. Wing (Bill Wing)
On Jun 6, 2012, at 9:29 AM, Alain Ketterlin wrote:

> loial  writes:
> 
>> I have a requirement to test the creation time of a file with the
>> current time and raise a message if the file is  more than 15 minutes
>> old.
>> Platform is Unix.
>> I have looked at using os.path.getctime for the file creation time and
>> time.time() for the current time, but is this the best approach?
> 
> No. getctime() returns the last "change" time. The creation time is not
> kept anywhere. This may still match your requirement though. And os.path
> is the right package to look at for such tasks.
> 
> -- Alain.
> -- 
> http://mail.python.org/mailman/listinfo/python-list

If you REALLY want the creation time rather than the last time the file was 
touched, you will probably have to invoke the subprocess module and call ls -U, 
something like the following:

creation_time = subprocess.check_output(["ls", "-U", string_filename_variable])

The -U option isn't universal, but it does exist in most of the UNIces I'm 
familiar with.

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


Re: Compare 2 times

2012-06-06 Thread Christian Heimes
Am 06.06.2012 14:50, schrieb loial:
> I have a requirement to test the creation time of a file with the
> current time and raise a message if the file is  more than 15 minutes
> old.
> 
> Platform is Unix.
> 
> I have looked at using os.path.getctime for the file creation time and
> time.time() for the current time, but is this the best approach?

Lots of people are confused by ctime because they think 'c' stands for
change. That's wrong. st_ctime is status change time. The ctime is
updated when you change (for example) owner or group of a file, create a
hard link etc. POSIX has no concept of creation time stamp.

Christian

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


Re: Compare 2 times

2012-06-06 Thread Peter Otten
loial wrote:

> I have a requirement to test the creation time of a file with the
> current time and raise a message if the file is  more than 15 minutes
> old.
> 
> Platform is Unix.
> 
> I have looked at using os.path.getctime for the file creation time and

ctime is not actually the creation time:
http://www.brandonhutchinson.com/ctime_atime_mtime.html

> time.time() for the current time, but is this the best approach?

What kind of improvement do you have in mind?

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


Re: problem python

2012-06-06 Thread Peter Otten
Tom King wrote:

[Please hit "reply-all" in your email client when you answer a post. That 
way it will appear on the mailing list and give more people a chance to 
answer]

>>> when i type python in my command line console i get an error message
>>> 
>>> 'import site' failed; use -v for traceback
>>> Python 2.4.3 (#1, May  5 2011, 18:44:23)
>>> [GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
>>> Type "help", "copyright", "credits" or "license" for more information.

>>> what is going on and how i can fix it
>>> thanks in andvance
>> 
>> Did you write a module site.py yourself? If so rename it and don't forget
>> to remove the corresponding site.pyc. If that doesn't help invoke python
>> with
>> 
>> $ python -S
>> 
>> and then import site explicitly in the interactive interpreter:
>> 
>> >>> import site
>> 
>> You should get a traceback. If you cut and paste it and show it to us we
>> might tell you more. 

> thank you Peter for your response i followed your instructions and 
> get this time 
> 
>  python -S
> Python 2.4.3 (#1, May  5 2011, 18:44:23) 
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
> >>> import site
> Traceback (most recent call last):
>   File "", line 1, in ?
> ImportError: No module named site
> >>> 

So the file seems to be missing indeed. Frankly, that's not what I expected, 
and I don't know how to proceed from here. Can you import other modules, e. 
g. 

>>> import dis

? What does

>>> import sys
>>> sys.path

show?

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


Re: KeyError: '13'

2012-06-06 Thread Peter Otten
Phanindra Ramesh Challa wrote:

[Please hit "reply-all" in you email client when you answer a post. That way 
it will appear on the mailing list and give more people a chance to answer]

> output of the is just the line
> "sizes".

>> and then decide if you to need to change the lookup key or to add records
>> to the database.
>>
> How can I add recors to the database?

The Python part of the answer is 

db[some_key] = some_value

but I fear that won't help. Google suggests you are struggling with the 
metatype project If so, there seems to be a corresponding mk_db.py that you 
can use. I don't know anything about metatype, but the comment in mk_db.py 
at 
http://metatype.cvs.sourceforge.net/viewvc/metatype/metatype/mk_db.py?revision=1.3&view=markup

tells to run it like so:

find . -name '*.ugs' | python mk_db.py -a glyphlist -o dbmfile

Perhaps you ran it, but there weren't any *.ugs files?

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


ttk.Spinbox missing?

2012-06-06 Thread Mark Summerfield
Hi,

I have Python 3.2 with Tcl/Tk 8.5, but there doesn't seem to be a
ttk.Spinbox widget even though that widget is part of Tcl/Tk 8.5:
http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_spinbox.htm

Why is that?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem python

2012-06-06 Thread Peter Otten
Tom King wrote:

> hi im new in python and i have a problem about
> 
> when i type python in my command line console i get an error message
> 
> 'import site' failed; use -v for traceback
> Python 2.4.3 (#1, May  5 2011, 18:44:23)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 
> 
> 
> what is going on and how i can fix it
> thanks in andvance

Did you write a module site.py yourself? If so rename it and don't forget to 
remove the corresponding site.pyc. If that doesn't help invoke python with

$ python -S

and then import site explicitly in the interactive interpreter:

>>> import site

You should get a traceback. If you cut and paste it and show it to us we 
might tell you more.

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


problem python

2012-06-06 Thread Tom King
hi im new in python and i have a problem about 

when i type python in my command line console i get an error message 

'import site' failed; use -v for traceback
Python 2.4.3 (#1, May  5 2011, 18:44:23) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 


what is going on and how i can fix it
thanks in andvance-- 
http://mail.python.org/mailman/listinfo/python-list


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread Tim Wintle
On Wed, 2012-06-06 at 12:03 +0200, Ulrich Eckhardt wrote:
> "block else for other cases" - this sounds as if it was blocking the
> else. Maybe "else-block for other cases",

I would say "else block". "else-block" is grammatically correct too, but
I don't think I've seen it used regularly.

RE: the order - "else" is being used as an adjective to clarify the noun
"block" - in English the adjective comes before the noun (unlike a lot
of European languages)

e.g. we say "the red book", not "the book red", where the French would
say "livre rouge" (I believe).

If you want to put an adjective after the noun (for poetical reasons
etc) then there needs to be another clause. e.g. "the book, which was
red"

> but English hyphenation is complicated and I'm not sure.

You're German and you say English hyphenation is complicated! ;-)

Tim

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


Re: English version for Mémento Python 3 (draft, readers needed)

2012-06-06 Thread Ulrich Eckhardt
Am 05.06.2012 19:32, schrieb Laurent Pointal:
> I started a first translation of my document originally in french. Could 
> some fluent english people read it and indicate errors or bad english 
> expressions.

Just one note up front: Languages or nationalities are written with
uppercase letters, like English and French. Other common faults of that
category are days of the week (Monday..) and month names (January..),
although that's irrelevant for your doc.

Another thing I noticed that was missing was that the "in" keyword can
not only be used to iterate over a sequence (for i in seq:...) but also
to test if something is contained in a sequence (if i in seq:...).

"don't miss to close file after use": Use a "with" statement.

"see verso for string formatting..." - what is "verso"?

"dont" -> "don't"

"char strings" -> "strings" (in the context of indexing, byte strings
have the same syntax)

"with several else if, else if..." - there is no "else if" but "elif".

"block else for other cases" - this sounds as if it was blocking the
else. Maybe "else-block for other cases", but English hyphenation is
complicated and I'm not sure.


Thanks for your work!

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


Re: How to exec() a string like interactive python does?

2012-06-06 Thread News123

On 06/06/2012 12:12 AM, News123 wrote:

If I start Python in interactive mode,
and I yype the commands,
'a=3', 'a', 'print a'


Then the output would look like:
 >>> a = 3
 >>> a
3
 >>> print a
3


Now within an application I'd like to achieve exactly this behaviour
Meaning, that
- for assignments nothing is displayed
- for expressions the result of the exprission displayed
- and statements like print statements would be executed


The only thing, that I came up with is following code and that would
even print out results for 'a=3', where the normal interactive python
would not echo any result.

for cmd in [ 'a=3', 'a', 'print a' ] :
try:
print('>>> ' + cmd)
exec('__rslt = ' + cmd)
if __rslt is not None:
print repr(__rslt)
except SyntaxError:
exec(cmd)

The result would look like:
 >>> a=3
3
 >>> a
3
 >>> print a
3
 >>>


Is There anything better?



Thanks a lot Devin,
Following line does the trick:

eval(compile(s, '', 'single'))


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


Re: KeyError: '13'

2012-06-06 Thread Peter Otten
Phanindra Ramesh Challa wrote:

> I am trying to run a python program. It is giving the KeyError: '13'. I
> have no clue regarding this error.
>  this is the python code relevant to the error:
> 
> dpi = 100
> bold = 0
> italic = 0
> mono = 0
> comment = ""
> dbname = "font.db"
> 
> for o, a in opts:
> if o == '-b': bold = 1
> if o == '-i': italic = 1
> if o == '-m': mono = 1
> if o == '-d': dpi = int (a)
> if o == '-c': comment = string.join (string.split (a), " ")
> if o == '-g': dbname = a
> 
> fontname = args[0]
> pixel_size = int (args[1])

pixel_size is now 13
 
> point_size = int (pixel_size * 72.27 / dpi + 0.5)
> 
> # Approximate average glyph width.
> avg_width = pixel_size * 17 / 24
> 
> db = anydbm.open (dbname, "r")

The above line opens a database (a Berkeley DB as the traceback reveals).

> codes = loads (db [str (pixel_size)])

str(pixel_size) converts 13 back to the string "13"

db[some_key]

looks up the record with some_key, "13" in this case. There is no record 
with the key "13" in the font.db database, and therefore the script fails 
with the aptly named KeyError. (The database interface is modeled after 
Python's dictionary, so the handling is similar)
 
> And the error is :
> python2 ../../../mk_bdf.py -c "Converted from fonts of Computer Modern
> family (C) 1979-1985 Donald E. Knuth and others." -b  'TeX Sans' 13 >
> tex09sb.bdf
> Traceback (most recent call last):
>   File "../../../mk_bdf.py", line 108, in 
> codes = loads (db [str (pixel_size)])
>   File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in __getitem__
> return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
>   File "/usr/lib/python2.7/bsddb/dbutils.py", line 68, in DeadlockWrap
> return function(*_args, **_kwargs)
>   File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in 
> return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
> KeyError: '13'

>  Anybody please help me in running the program.

For now you can ignore the code in the traceback which may be a bit 
intimidating to a newbie. It contains lines from the implementation of the 
database interface. Instead have a look at the keys that are in the database 
with

db = anydb.open("font.db")
for key in db: 
print key

and then decide if you to need to change the lookup key or to add records to 
the database.

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