Re: eval to dict problems NEWB going crazy !

2006-07-06 Thread Eric Deveaud
manstey wrote:
  That doesn't work. I just get an error:
 
  x = eval(line.strip('\n'))
File string, line 1
   [('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]
 
   SyntaxError: unexpected EOF while parsing
 

is the last line of your file empty ??

what with

for line in filAnsMorph:
 # remove any trailing and leading whitespace includes removing \n
 line = line.strip()
 # Get rid of comment lines
 if line.startswith('#'):
continue
# Get rid of blank line
if line == '':
continue
#do the job
 x = eval(line)


NB by default strip() removes leading and trailing characters from the target
string. with whitspace defined as whitespace = '\t\n\x0b\x0c\r '

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


Re: getting current UNIX uid

2006-07-06 Thread Eric Deveaud
Johhny wrote:
  Hello,
 
  I am trying to get the user that is running the scripts uid, I have had
  a look at the pwd module and it does not appear to offer that
  functionality. Is there any way within python to get that information ?

eg:
username = pwd.getpwuid(os.getuid())[4]

Eric

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


Re: Way for see if dict has a key

2006-06-30 Thread Eric Deveaud
Bruno Desthuilliers wrote:
  Fredrik Lundh wrote:
  Bruno Desthuilliers wrote:
  
  on my machine, key in dict is about twice as fast as the full
  
  try/getitem construct when the key is present in the dict,
  
 
  Doesn't it depends on the number of keys in the dict ?
  
  
  why would it depend on the number of keys in the dict ?
  
  /F
 
  Seems that if key in dict do a simple linear search, it depends on the
  number of keys in dict (and the position of the searched key etc...).
 
  And if I'm missing the point and you it and you know why, it would be
  simple to explain than to answer my question with another question.


maybee we can imagine that key in dict
just perform 
try:
dict[key]
return True
except KeyError:
return False

If I understand correctly, it will be time constant no ??
just the needed time to compute the hash for the key

Eric

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


Re: combined files together

2006-05-09 Thread Eric Deveaud
Gary Wessle wrote:
 
  I need to traverse those files in the order they were created
  chronologically. listdir() does not do it, is there a way besides
  build a list then list.sort(), then for element in list_of_files open
  element?

are the name of the files describing the cration date, or have to rely on the
creation date ?

if the name allows to discriminate the chronology, check glob module.

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


Re: Sorting a list of dictionaries by dictionary key

2006-05-03 Thread Eric Deveaud
Nico Grubert wrote:
  Hi there,
 
  I am looking for a way to sort a list containing dictionaries.
 
  This is my example list:
  [{'Title': 'ABC', 'from_datetime': DateTime('2006/04/25 12:45:00 
  GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18 
  12:45:00 GMT+2')}, {'Title': 'GHI', 'from_datetime': 
  DateTime('2006/03/10 12:45:00 GMT+2')}]
 
  I want to sort the list by dictionary's key 'from_datetime' so the 
  sorted list should be:
 
  [{'Title': 'GHI', 'from_datetime': DateTime('2006/03/10 12:45:00 
  GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18 
  12:45:00 GMT+2')}, {'Title': 'ABC', 'from_datetime': 
  DateTime('2006/04/25 12:45:00 GMT+2')}]
 
  Any idea how I can sort this list?

sort can take a comparaison function. which specification are 
takes 2 elements 
returns a negative value if elem1 is lesser than elem2
returns  a positive value if elem1 is grater than elem2
returns zero if elem1 == elem2

you may write such fuction

def dictionary_datetime_sorter(d1, d2):
date1 = d1['from_datetime']
date2 = d2['from_datetime']
if date1  date2: return -1
elif date1  date2: return 1
else: return 0

you'll have to find out how to compare 2 DateTime

and then, in order to sort your list
your_dictionary_list.sort(dictionary_datetime_sorter)


Eric
-- 
 « Voyons-voir : est-ce que ma question est vraiment conne ? Oh oui,
 vraiment trop conne, je vais poster dans premiers-pas, je posterai
 dans configuration quand je serai moins con. »
 -+-JLC - Guide du Neuneu d'Usenet - Bien configurer sa question -+-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a list of dictionaries by dictionary key

2006-05-03 Thread Eric Deveaud
bruno at modulix wrote:
  Eric Deveaud wrote:
  (snip)
  
  sort can take a comparaison function. 
 
  The problem with it is that it may slow down things a lot...

point taken.

I have to purge my mind from the other programing languages I practice. ;-)

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


Re: create pixmap from data

2006-04-14 Thread Eric Deveaud
Thomas Girod wrote:
  Hi there.
 
  I have a list containing integer values from 0 to 255. With this list I
  have informations width and height, as width * height = len(my_list)
 
  What I want to do is to convert this array into a pixmap of dimension
  width * height in order to draw it inside a pygtk GUI.
 
  Any suggestions about a way to display those informations ?


check the PIL (Python Imaging Library), Image module

Eric


-- 
 S Je cherche aussi des adresses de lieux contenant des fossiles dans
 S la région parisienne 
 http://www.senat.fr/
 -+- DP in http://www.le-gnu.net : La dianurette et les fossiles -+-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Initializing defaults to module variables

2006-04-13 Thread Eric Deveaud
Burton Samograd wrote:
  Hi, 
 
  I'm writing an app that stores some user configuration variables in a
  file ~/.program/config, which it then imports like so:
 
  import sys
  from posix import environ
  sys.path.append(environ[HOME]+/.program)
  import config
 
  I can then access the configuration through code like:
 
  login(config.username)

better use some plain text configuration file
check ConfigParser documentation.

  So I guess the real question is:
 
  Is there a way to create a module namespace and populate it
  before sourcing the file?

if you take a look at how naming space is affected by the variants 
from module import * and his conterpart import module, you will have your
answer.

shortly

if you use import foo all the variables/class/functions comming from the
module foo are located in a globlal space dictionary named foo

if you use form foo import * all variables/class/functions comming from the
foo module are located in gloabal space naming dictionary.


try this

-=-=-=-=-=-=- consider module foo.py -=-=-=-=-=-=-

my_var = my var from foo

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


hebus:~/tmp  python
Python 2.4.2 (#1, Mar 22 2006, 12:59:23) 
[GCC 3.3.5 (Debian 1:3.3.5-5)] on linux2
Type help, copyright, credits or license for more information.
 import foo
 print my_var
Traceback (most recent call last):
  File stdin, line 1, in ?
NameError: name 'my_var' is not defined
  
and now

hebus:~/tmp  python
Python 2.4.2 (#1, Mar 22 2006, 12:59:23) 
[GCC 3.3.5 (Debian 1:3.3.5-5)] on linux2
Type help, copyright, credits or license for more information.
 from foo import *
 print my_var
my var from foo


this will lead to a solution regarding your problem

hebus:~/tmp  python
Python 2.4.2 (#1, Mar 22 2006, 12:59:23) 
[GCC 3.3.5 (Debian 1:3.3.5-5)] on linux2
Type help, copyright, credits or license for more information.
 my_var = 'some default value'
 from foo import *
 print my_var
my var from foo


once again, I bet your best choice will be to have some plain text config file

Eric

-- 
 Je voudrais savoir s'il existe un compteur de vitesse (?) pour savoir
 à quelle vitesse on est réellement connecté au FAI et surtout si une
 telle bête existe... où la trouver. 
 -+- RJ in: Guide du Neuneu d'Usenet - Baisse la tête et pédale -+-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Iterating command switches from a data file - have a working solution but it seems inefficient

2006-04-13 Thread Eric Deveaud
News wrote:
  Hi everyone,
 
  My goal is to pull command switches/options from a file and then assign
  the values to select variables which would eventually be included in a
  class object.
 
  The data file looks something like this but the switches could be in any
  order and not all may be used.
 
  -m quemanager -s server -p port -k key -o object -c 20 -t [EMAIL PROTECTED]
 
  Also, please keep in mind that the source code will have more than one
  line in it and each has to be treaded separately.

I suggest you use getopt or optpase to perform this task
those modules are designed to perform what you want, with error checking

import getopt
inp = open(const.txt,r)
for line in inp:
try:
   opt, args = getopt.getopt(iline.split(), 'c:k:m:o:p:s:t:i')
   print opt
   except getopt.GetoptError, msg:
   # handle the error as you need

Eric
-- 
 Je voudrais savoir s'il existe un compteur de vitesse (?) pour savoir
 à quelle vitesse on est réellement connecté au FAI et surtout si une
 telle bête existe... où la trouver. 
 -+- RJ in: Guide du Neuneu d'Usenet - Baisse la tête et pédale -+-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Iterating command switches from a data file - have a working solution but it seems inefficient

2006-04-13 Thread Eric Deveaud
News wrote:
  bruno at modulix wrote:
  
  Have you looked at optparse ?
  
  I have.
 
  The module optparse seemed to be aimed at reading from commandline
  versus pulling attributes from a read line.


hu 

lets see 
optparse seems to be reading from commandline. that's not false but not
really true ;-)

point 1:
remember that commandline reading is done thru sys.argv wich provide a a
list of strings

point 2
now consider pulling datas from readline, wich return a string.
if you split this string you'll have a list of string.

point 3
optparse is designed for reading from a list of strings

I let the conclusion to your imagination ;-))


Eric
 -- 
 Salut,Je m'appele sed.je suis etudiant en communication, j'ai lu votre
 message.je viens vous dire un petiit bonjour,et vous laisser mon
 mél: vous pouvez me repondre maintenant si vous étez conecter.
 -+-Guide du Neuneu d'Usenet - La com', elle ne passera pas par moi -+-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: converting lists to strings to lists

2006-04-12 Thread Eric Deveaud
robin wrote:
  hi,
 
  i'm doing some udp stuff and receive strings of the form '0.87
  0.25 0.79;\n'
  what i'd need though is a list of the form [0.87 0.25 0.79]
  i got to the [0:-3] part to obtain a string '0.87 0.25
  0.79' but i can't find a way to convert this into a list. i tried
  eval() but this gives me the following error:

check pydoc string about split

my_string = '0.87 0.25 0.79;\n'
my_list = my_string.split()
print my_list


  and i have the same problem the other way round. e.g. i have a list
  which i need to convert to a string in order to send it via udp.


my_new_string = ' '.join(my_list)
print my_new_string

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


Re: converting lists to strings to lists

2006-04-12 Thread Eric Deveaud
robin wrote:
  yo!
 
  thank you everone! here's how i finally did it:
  converting the string into a list:
 
   input = net.receiveUDPData()
   input = list(eval(input[0:-2]))

no pun intented but as you did not know how to use split and join, 
please please DON'T USE eval

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


Re: how to comment lot of lines in python

2006-03-31 Thread Eric Deveaud
[EMAIL PROTECTED] wrote:
  Like in C we comment like
  /*
  Bunch of lines of code
  */
 
  Should we use docstring  

I would say NO.
docstring are displayed by pydoc, thus a pydoc on your code will display some
inconsistent information ;-)

  Or there is something else too ??

some moderns editors allow you to comment/uncomment  a selected Bunch 
of lines of code

Eric
-- 
 afin de parfaire mon apprentissage de linux,je cherche sur lille et sa
 périphérie une nana tout linux
  JPH in Guide du linuxien pervers : Connaître le système
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to comment lot of lines in python

2006-03-31 Thread Eric Deveaud
[EMAIL PROTECTED] wrote:
 
  Eric Deveaud wrote:
  [EMAIL PROTECTED] wrote:
Like in C we comment like
/*
Bunch of lines of code
*/
  
Should we use docstring  
 
  I would say NO.  docstring are displayed by pydoc, thus a pydoc on your
  code will display some inconsistent information ;-)
 
 
  docstrings are a bit of a magical construct.  Not all strings in a
  function are docstrings.


yep fogotten that triple quotted strings are considered as docstring
only if they are the first lines of the module/fonction/class/method 
excluding the comments lines.

my bad

Eric

-- 
SYBEX ORIGINAL SOFTWARE
NOUVEAU KIT LINUX REDHAT 5.2 POUR WIN 95/98
-+- Sybex in Guide du linuxien pervers - L'incompétance en action -+-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [OT] .DS_Store files (was Re: Removing .DS_Store files from mac folders)

2006-03-09 Thread Eric Deveaud
Dan Sommers wrote:
  If these are both true, then *new* .DS_Store files could
  be created after I've made the list and before I've made
  the tarball.


not python related, but in order to creater the tarball without the .DS_Store
files why don't you use the --exclude=PATTERN option from tar ??

Eric

-- 
  CM: Faut-il nommer un modérateur suppléant à Christophe Wolfhugel ?
  MG: Faudrait savoir ce qu'il en pense ? Va-t-il répondre : « Rien » ?
  CW: Non.
   -+- in: Guide du Cabaliste Usenet - Euh... Non, rien. -+-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how do you move to a new line in your text editor?

2006-03-03 Thread Eric Deveaud
John Salerno wrote:
  This is a real small point, but I'd like to hear what others do in this 
  case. It's more an 'administrative' type question than Python code 
  question, but it still involves a bit of syntax.
 
  One thing I like to do is use tabs for my indentation, because this 
  makes it easy to outdent when I need to start a new line in column 1. I 
  can press backspace once and move 4 spaces to the left.
 
  But I read in the PEP that spaces are recommended over tabs. If this is 
  the case, it would involve pressing backspace 4 times (or 8, etc.) to 
  get back to column 1.

you can still use tabs for editing, and if you want to provide space indented
code you can write a small tab2space converter in python 

more seriously, most of the editors (at least Emacs and vim) may use tab for
automatic indentation but convert the tabs to a specified space number

Eric


-- 
 Le lecteur c'est Outlook express (ceci explique sans doute celà)
Outlook Express, c'est fait pour demander comment configurer Emacs
pour lire le courrier et les news.
-+- GK in Guide du linuxien pervers - De l'utilité de Outlook -+-
-- 
http://mail.python.org/mailman/listinfo/python-list