Re: [Tutor] File like object for Windows registry
> 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
Re: [Tutor] File like object for Windows registry
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
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
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
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
Re: [Tutor] file-like object
> > 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
Danny Yoo wrote: >Using the default parameter 'n' in the readline() method isn't safe: all >class instances will end up using the same 'n'. You may want to put the >current line number as part of an instance's state, since two instances of >a macroString should be able to keep track of their line positions >independently. > >But that being said, there's already a module in the Standard Library that >turns strings into file-like objects. Can you use StringIO.StringIO? > > I used kent's solution at first but I'm just using StringIO now I didn't realize what that module did. Thank you all for your suggestion. __ 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
Terry Carroll wrote: 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. In this case it should be safe. This changes the string at each index in the list, but it doesn't change the length or ordering of the list. That's where the problems come in, because the for loop doesn't know that the list "shape" has changed. But your caution is generally a good idea. :) I'd have probably written the above as: self.list = [line+'\n' for line in self.list] Well, actually, I'd have avoided saving the intermediary state and simply done all the list processing at once: self.list = [line+'\n' for line in self.macro.split('\n')] Either way, though, I'm creating a new list rather than modifying the old list in-place, which avoids the risk of accidentally changing the list's "shape" and throwing off the loop. Jeff Shannon Technician/Programmer Credit International ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] file-like object
On Fri, 14 Jan 2005, Terry Carroll 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. Hi Terry, The 'for' loop itself should be ok, as it isn't a change that causes elements to shift around. The Reference Manual tries to explain the issue on in-place list mutation here: http://docs.python.org/ref/for.html So as long as the structure of the list isn't changing, we should be ok to do mutations on the list elements. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] file-like object
On Fri, 14 Jan 2005, 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) Using the default parameter 'n' in the readline() method isn't safe: all class instances will end up using the same 'n'. You may want to put the current line number as part of an instance's state, since two instances of a macroString should be able to keep track of their line positions independently. But that being said, there's already a module in the Standard Library that turns strings into file-like objects. Can you use StringIO.StringIO? Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] file-like object
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
> 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
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
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