[Tutor] File like object for Windows registry

2006-08-03 Thread K . Weinert
Hello!

My app should run on debian and windows platforms. For storing the 
configuration data, I use the ConfigParser module.

What I find difficult is to determine a place for my configuration file. On 
debian, it is simply

os.path.join(os.path.expanduser(~)),myconfig)

but what am I supposed to do on Windows? I think a clean solution would be to 
create a file-like object that reads and writes to the registry, is it?

Kind regards,
Karsten.
-- 


Feel free – 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File like object for Windows registry

2006-08-03 Thread Henry Finucane
On 8/3/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 Hello!

 My app should run on debian and windows platforms. For storing the 
 configuration data, I use the ConfigParser module.

 What I find difficult is to determine a place for my configuration file. On 
 debian, it is simply

 os.path.join(os.path.expanduser(~)),myconfig)

 but what am I supposed to do on Windows? I think a clean solution would be to 
 create a file-like object that reads and writes to the registry, is it?

You might be able to do that, I don't know much about win32
programming, but I believe a better solution is to use the built-in
windows variables. %APPDATA% is where you should store user-specific
application data (and even Microsoft is starting to store XML
configuration files there), and it's an easy variable to get.

 import os
 os.environ[APPDATA]
'C:\\Documents and Settings\\UserName\\Application Data'

That should function just fine as a home directory replacement.

 Kind regards,
 Karsten.
 --


 Feel free – 10 GB Mailbox, 100 FreeSMS/Monat ...
 Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor



-- 
--H.F.
My penguin is bigger than yours, mister...
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File like object for Windows registry

2006-08-03 Thread Andre Roberge
On 8/3/06, Henry Finucane [EMAIL PROTECTED] wrote:
 On 8/3/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
  Hello!
 
  My app should run on debian and windows platforms. For storing the 
  configuration data, I use the ConfigParser module.
 
  What I find difficult is to determine a place for my configuration file. On 
  debian, it is simply
 
  os.path.join(os.path.expanduser(~)),myconfig)

This works on Windows as well.  I just tried it :-)

 
  but what am I supposed to do on Windows? I think a clean solution would be 
  to create a file-like object that reads and writes to the registry, is it?

Messing with the registry is (imo)  a bad idea.


 You might be able to do that, I don't know much about win32
 programming, but I believe a better solution is to use the built-in
 windows variables. %APPDATA% is where you should store user-specific
 application data (and even Microsoft is starting to store XML
 configuration files there), and it's an easy variable to get.

  import os
  os.environ[APPDATA]
 'C:\\Documents and Settings\\UserName\\Application Data'

 That should function just fine as a home directory replacement.

...

André
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File like object for Windows registry

2006-08-03 Thread Henry Finucane
On 8/3/06, Andre Roberge [EMAIL PROTECTED] wrote:
 On 8/3/06, Henry Finucane [EMAIL PROTECTED] wrote:
  On 8/3/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
   Hello!
  
   My app should run on debian and windows platforms. For storing the 
   configuration data, I use the ConfigParser module.
  
   What I find difficult is to determine a place for my configuration file. 
   On debian, it is simply
  
   os.path.join(os.path.expanduser(~)),myconfig)

 This works on Windows as well.  I just tried it :-)

Doh. Always try the simple stuff first :P.

  
   but what am I supposed to do on Windows? I think a clean solution would 
   be to create a file-like object that reads and writes to the registry, is 
   it?

 Messing with the registry is (imo)  a bad idea.

 
  You might be able to do that, I don't know much about win32
  programming, but I believe a better solution is to use the built-in
  windows variables. %APPDATA% is where you should store user-specific
  application data (and even Microsoft is starting to store XML
  configuration files there), and it's an easy variable to get.
 
   import os
   os.environ[APPDATA]
  'C:\\Documents and Settings\\UserName\\Application Data'
 
  That should function just fine as a home directory replacement.
 
 ...

 André



-- 
--H.F.
My penguin is bigger than yours, mister...
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File like object for Windows registry

2006-08-03 Thread Alan Gauld
 My app should run on debian and windows platforms.

Hard lines :-(

 For storing the configuration data, I use the ConfigParser module.
 What I find difficult is to determine a place for my configuration 
 file.

Config parser basically produces an .in file.
The rules that Windows uses to locate .ini files vary
according to Windows version. If you can assume you
only have to deal with Win2K and XP then things are
a bit easier since there is a recommended location
and indeed users have the concept of a home directory
- even the $HOME environment variable works.

However traditionally ini files were stored in one of
1) the Windows directory (%WINDIR%) or
2) the application home directory.
3) The C:\ root directory - but this is now strongly
discouraged

If you want to have application level ini files as well as
per user configurations those are still the preferred
locations for the global files.

Between Windows 95 and Windows 2000 the Registry
was being pushed as the best place for config data
but as Registry performance and corruption problems
increase .ini files are coming back into favour.

To summarise. If you want to just have a per user
config file just store it in the users data folder. If you
also have a global ini file then I recommend putting
it in the app install folder. If the app is cross platform
I'd advise keeping well clear of the registry, but if
you must use it I'd favour using the WSH objects
rather than the Win32 API calls to access the
Registry - although it does add another dependency
to the app.

HTH,

Alan G. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] file-like object

2005-01-14 Thread Chad Crabtree
I have created a file-like object out of a triple quoted string.  I
was 
wondering if there is a better way to implement readline than what I 
have below? It just doesn't seem like a very good way to do this.

class _macroString(object):
def __init__(self,s):
self.macro=s
self.list=self.macro.split(\n)
for n,v in enumerate(self.list):
self.list[n]=v+'\n'
def readline(self,n=[-1]):
n[0]+=1
return self.list[n[0]]
def __str__(self):
return str(self.list)
def __len__(self):
return len(self.list)

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] file-like object

2005-01-14 Thread Kent Johnson
Best: use the StringIO or cStringIO module instead, this is exactly what it is for. If you really 
need len() you could maybe subclass StringIO to do what you want.

Next best: Use an iterator. Something like this (Warning! not tested!):
class _macroString(object):
def __init__(self,s):
self.macro=s
self.list=[ line+'\n' for line in self.macro.split(\n) ]
self._iter = iter(self.list)
def readline(self):
try:
return self._iter.next()
except StopIteration:
return ''
def __str__(self):
return str(self.list)
def __len__(self):
return len(self.list)
Note that your implementation of readline will raise IndexError when there are no more lines which 
is not correct behaviour.

Kent
Chad Crabtree wrote:
I have created a file-like object out of a triple quoted string.  I
was 
wondering if there is a better way to implement readline than what I 
have below? It just doesn't seem like a very good way to do this.

class _macroString(object):
def __init__(self,s):
self.macro=s
self.list=self.macro.split(\n)
for n,v in enumerate(self.list):
self.list[n]=v+'\n'
def readline(self,n=[-1]):
n[0]+=1
return self.list[n[0]]
def __str__(self):
return str(self.list)
def __len__(self):
return len(self.list)
__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] file-like object

2005-01-14 Thread Chad Crabtree
Thank you KentBot.  That was what I wanted.

Kent Johnson wrote:

 Best: use the StringIO or cStringIO module instead, this is exactly

 what it is for. If you really need len() you could maybe subclass 
 StringIO to do what you want.

 Next best: Use an iterator. Something like this (Warning! not
tested!):
 class _macroString(object):
 def __init__(self,s):
 self.macro=s
 self.list=[ line+'\n' for line in self.macro.split(\n) ]
 self._iter = iter(self.list)
 def readline(self):
 try:
 return self._iter.next()
 except StopIteration:
 return ''
 def __str__(self):
 return str(self.list)
 def __len__(self):
 return len(self.list)

 Note that your implementation of readline will raise IndexError
when 
 there are no more lines which is not correct behaviour.

 Kent






__ 
Do you Yahoo!? 
Take Yahoo! Mail with you! Get it on your mobile phone. 
http://mobile.yahoo.com/maildemo 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] file-like object

2005-01-14 Thread Alan Gauld
 class _macroString(object):
 def __init__(self,s):
 self.macro=s
 self.list=self.macro.split(\n)
 for n,v in enumerate(self.list):
 self.list[n]=v+'\n'
 def readline(self,n=[-1]):
 n[0]+=1
 return self.list[n[0]]

Why not just create a current pointer as a clas attribute?
Increment or reset as required. after all maintaining 
object state is what classes and objects are for!

HTH,

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] file-like object

2005-01-14 Thread Terry Carroll
On Fri, 14 Jan 2005, Chad Crabtree wrote:

 class _macroString(object):
 def __init__(self,s):
 self.macro=s
 self.list=self.macro.split(\n)
 for n,v in enumerate(self.list):
 self.list[n]=v+'\n'


Is this for loop a safe technique, where the list you're enumerating over
in the for statement is the same as the one being updated in the loop
body?  I always avoid things like that.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] file-like object

2005-01-14 Thread Alan Gauld
  for n,v in enumerate(self.list):
  self.list[n]=v+'\n'


 Is this for loop a safe technique, where the list you're enumerating
over
 in the for statement is the same as the one being updated in the
loop
 body?  I always avoid things like that.

Its not changing the list, its changing the list contents.
If it were adding or re,moving items from the list that
would be dodgy, but modifying a existing element doesn't
really change the list itself in any significant way.

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] file-like object

2005-01-14 Thread Terry Carroll
On Fri, 14 Jan 2005, Terry Carroll wrote:

 Is this for loop a safe technique, where the list you're enumerating over
 in the for statement is the same as the one being updated in the loop
 body?  

Rather than cluttering the list by making three replies, I'd just like to
thank Danny, Alan and Jeff each for their answers to this.  Clears it
right up for me.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor