Author Topic: Python Question  (Read 11617 times)

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Python Question
« on: August 09, 2005, 03:35:37 pm »
I need to search a line of text out in a file and replace it, so far the code examples on the net have produced empty files.

Basically the file is a *.conf file, now the structure of the file looks alot like a win *.ini file and in visual basic there is a function to access the sections (the headers in brackets) and each entry (the name with '='). Now the question,  does python support or rather have a built in function for handleing that? If yes what is it? If no then how do I do a search and replace? I only have a partial string and want to replace the whole line until the eof(its the last line in the file)

and second question how is the Launcher.conf 'reloaded' after a change, does it get checked every so often?

or even better, what command line will change my wallpaper? Obviously you have the direction I am looking in so far.
« Last Edit: August 09, 2005, 03:37:41 pm by bam »
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Python Question
« Reply #1 on: August 10, 2005, 03:06:38 pm »
ok got the generator to work properly, I can randonly generate any pic from a specific directory and change the launcher.conf file.

Problem: How to tell the launcher to "refresh" the wallpaper. Or is there a commandline to set the wallpaper already, so I dont have to edit the launcher.config file?

BTW: in that file containes a list of fastload apps too, along with some other interesting stuff.
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

nilch

  • Hero Member
  • *****
  • Posts: 668
    • View Profile
    • http://
Python Question
« Reply #2 on: August 10, 2005, 03:15:02 pm »
So you got the string replace command in Python ?
If you didnt ....

You can search each line till eof after reading it into a list (or tuple, I keep getting confused which is which)

readFile = open(dataPath + "launcher.conf")
readLines = readFile.readlines()
for line in readLines :
    header = line.split("=")[0]
    entry = line.split("=")[1]

Once you find a match (on header or enrty), use replace function to replace a string with new string

For reference - string functions of Python
http://docs.python.org/lib/string-methods.html
New no more-C1000 / 5000D (sold my 6000 and 750) | Cacko ROM 1.23 on C1000 | 256 MB CF | 2GB PNY SD card | Socket Networker WiFi CF Card | USB Host cable from StreamlineCPUS | Mini Microphone (for voice recording) |

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Python Question
« Reply #3 on: August 10, 2005, 03:59:28 pm »
yea, go that, actually I only know the entry name "wallFName =" the rest is unknown, so I used the "for line in sFile" then did a if/then/else construct when it finds the line re-write the entire line(this is what *.replace does not do"

hek I will post the code, I just have to find a way to tell the launcher o refresh the wallpaper, google provided nothing
Code: [Select]
#!/usr/bin/env python

import os

import sys

import fileinput

import glob

import random

import re



piclist=glob.glob('/hdd3/Documents/Image_Files/wallpapers/*.*')
wallpic=random.choice(piclist)

wppath="/hdd3/Documents/Image_Files/wallpapers/" + wallpic

#/home/root/settings/launcher.conf


textToSearchFor= "wallFName"

newWallP= "wallFName =" + wppath

fileToSearch= "/hdd3/Documents/temp/launcher.conf"

fileToOutput= "/hdd3/Documents/temp/tempfile.conf"
        
fileToOutput = open( fileToOutput, 'w' )

#print "outfile = ", fileToOutput


for line in fileinput.input( fileToSearch ):

    #fileToOutput.write( line.replace( textToSearchFor, textToReplaceWith ) )

    slinee=""

    sline=line[0:9]

    
    if sline == "wallFName":

  fileToOutput.write(newWallP)

  
    else:

  fileToOutput.write(line)  

  
  
fileinput.close()

fileToOutput.close()
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

kopsis

  • Sr. Member
  • ****
  • Posts: 329
    • View Profile
    • http://kopsisengineering.com
Python Question
« Reply #4 on: August 10, 2005, 10:56:12 pm »
Regular expressions are your friend
Code: [Select]
for line in fileinput.input( fileToSearch ):
    fileToOutput.write(re.sub(r'wallFName =.*', "wallFName = " + wallpic, line))
And yes, a compiled re would be faster but I'm too lazy to type the extra line of code

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Python Question
« Reply #5 on: August 11, 2005, 04:08:00 am »
cool, gotta try that one, you recommend any books? I am a vb programmer as well, with some fortran.
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

zmiq2

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
    • http://
Python Question
« Reply #6 on: August 11, 2005, 04:27:29 am »
For having the launcher reloaded, serach the forums for qcop; sending a qcop message will tell qtopia to reload its settings.

HTH
sl-c750, archos av580, socket cf [bt, wifi, modem], noname cf lan, audiovox rtm800 gsm-gprs cf, rom: sharp -> oz3.5.3 -> cacko -> oz3.5.4.1

kopsis

  • Sr. Member
  • ****
  • Posts: 329
    • View Profile
    • http://kopsisengineering.com
Python Question
« Reply #7 on: August 11, 2005, 07:44:41 am »
Quote
cool, gotta try that one, you recommend any books? I am a vb programmer as well, with some fortran.
[div align=\"right\"][{POST_SNAPBACK}][/a][/div]
Truth be told, I learned Python just by reading the [a href=\"http://docs.python.org/tut/tut.html]Tutorial[/url] and the Library Reference but I came in with a lot of experience in other languages.

If you're an experienced programmer, you may find Dive Into Python helpful. It's probably a good place to start since you can read it online and then if it looks too advanced save your $$$ for something a little more beginner oriented. If you're looking for something a little more introductory I'm told that the O'Reilly Learning Python book is pretty good.

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Python Question
« Reply #8 on: August 12, 2005, 12:08:26 am »
Quote
For having the launcher reloaded, serach the forums for qcop; sending a qcop message will tell qtopia to reload its settings.

HTH
[div align=\"right\"][a href=\"index.php?act=findpost&pid=91455\"][{POST_SNAPBACK}][/a][/div]

havent found anything to tell it to reload its settings, maybe you saw something I did not?
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

zmiq2

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
    • http://
Python Question
« Reply #9 on: August 16, 2005, 06:33:41 am »
You can take a look at

https://www.oesf.org/index.php?title=Sharp_...a_QCop_Messages

Maybe the QPE/System restart() would do the trick for you.
sl-c750, archos av580, socket cf [bt, wifi, modem], noname cf lan, audiovox rtm800 gsm-gprs cf, rom: sharp -> oz3.5.3 -> cacko -> oz3.5.4.1

slapout

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Python Question
« Reply #10 on: August 16, 2005, 01:58:23 pm »
Quote
Quote
cool, gotta try that one, you recommend any books? I am a vb programmer as well, with some fortran.
[div align=\"right\"][{POST_SNAPBACK}][/a][/div]
Truth be told, I learned Python just by reading the [a href=\"http://docs.python.org/tut/tut.html]Tutorial[/url] and the Library Reference but I came in with a lot of experience in other languages.

If you're an experienced programmer, you may find Dive Into Python helpful. It's probably a good place to start since you can read it online and then if it looks too advanced save your $$$ for something a little more beginner oriented. If you're looking for something a little more introductory I'm told that the O'Reilly Learning Python book is pretty good.
[div align=\"right\"][a href=\"index.php?act=findpost&pid=91470\"][{POST_SNAPBACK}][/a][/div]

Other resouces include: A Byte of Python (http://www.byteofpython.info) and the The Python Grimoire (http://the.taoofmac.com/space/Python/Grimoire)
SL-5600
Watapon 1.2 Rom