PDA

View Full Version : [SOLVED] Python -- Make a special dictionary



robz0rz
December 16th, 2008, 03:18 PM
Hello all.

I'm trying to create something here, but I don't know enough python to accomplish this. Could someone please tell me how to do this, or which python libraries I need to get forward?


Let's say I have a folder icons/.

rob@rob-thinkpad:~ $ ls icons/
foo.16.png bar.icon
foo.22.png bar.svg
foo.32.png baz.16.png
foo.icon baz.22.png
foo.svg baz.32.png
bar.16.png baz.icon
bar.22.png baz.svg
bar.32.png junk.txt

Each *.icon file just holds the name of the icon, for example, the contents of foo.icon would be

rob@rob-thinkpad:~ $ cat icons/foo.icon
The Great Foo

What I would like to create in python is the following dictionary:

{ 'foo' : "The Great Foo",
'bar' : "Awesomest Bar",
'baz' : "Dazzing Baz" }


What I need to do for this is
List the files in the directory
Extract only the files that end with '.icon'
Store the name of those *.icon files without the extention
Read the first line of every *.icon file


I don't have a clue how to archieve these things. Could someone please help me? Thanks

ghostdog74
December 16th, 2008, 04:01 PM
What I need to do for this is
List the files in the directory
Extract only the files that end with '.icon'
Store the name of those *.icon files without the extention
Read the first line of every *.icon file

the above are simple to do, should have done more reading up.


List the files in the directory

os.listdir() , os.walk()




Extract only the files that end with '.icon'

str.endswith()



Store the name of those *.icon files without the extention


os.path.split()


Read the first line of every *.icon file


firstline = open("file").readline()

Martin Witte
December 16th, 2008, 08:09 PM
also you can look into the glob module (http://docs.python.org/library/glob.html) for pathname expansion

fiddler616
December 16th, 2008, 08:27 PM
Look into the file module for reading the file, the os module for directory work (this was mentioned before) string methods for tampering with what you scrape off the file, and dictionary methods for adding/sorting/editing/etc. the dictionary.

Reading tutorials (http://www.ibiblio.org/g2swap/byteofpython/read/) is always recommended...

snova
December 16th, 2008, 11:48 PM
Four line script that will probably work:



import glob
icons = {}
for i in glob.glob('icons/*.icon'):
icons[i[:-5]] = open(i).readline().strip()


Yeah, I gave it away. :)

A more explicit version:



import glob
# Obtain list of .icon files
icon_files = glob.glob('icons/*.icon')
# Dictionary
icons = {}
# Iterate over file list
for icon in icon_files:
# Open file
file = open(icon)
# Read line from file. The strip() call just removes the trailing newline, which readline() leaves on.
first_line = file.readline().strip()
# Removes the last five letters of the filename, the '.icon' part.
icon_name = icon[:-5]
# Sets the entry in the dictionary
icons[icon_name] = first_line
# Closes the file
file.close()

fiddler616
December 16th, 2008, 11:50 PM
Four line script that will probably work:



import glob
icons = {}
for i in glob.glob('icons/*.icon'):
icons[i[:-5]] = open(i).readline().strip()
Yeah, I gave it away. :)

A more explicit version:



import glob
# Obtain list of .icon files
icon_files = glob.glob('icons/*.icon')
# Dictionary
icons = {}
# Iterate over file list
for icon in icon_files:
# Open file
file = open(icon)
# Read line from file. The strip() call just removes the trailing newline, which readline() leaves on.
first_line = file.readline().strip()
# Removes the last five letters of the filename, the '.icon' part.
icon_name = icon[:-5]
# Sets the entry in the dictionary
icons[icon_name] = first_line
# Closes the file
file.close()

Elegant--I haven't seen/heard of glob before. I think I'm a fan. +2
-1 for encouraging through your actions that the OP not bother with a tutorial.
-1 for giving away whole code, not just outline and snippets.

snova
December 17th, 2008, 12:19 AM
Elegant--I haven't seen/heard of glob before. I think I'm a fan. +2

:)


-1 for encouraging through your actions that the OP not bother with a tutorial.
-1 for giving away whole code, not just outline and snippets.

It was with trepidation I wrote that. I fully expected that sort of criticism and wondered whether it was worth it.

On the other hand, is it wrong to teach through example?

And, on its own, this dictionary is useless anyway- he'll still have to write the rest of the program.

scooper
December 17th, 2008, 12:44 AM
Just to be redundant, added exception handling and different file name parsing...

import sys
import os.path
from glob import glob
icons = {}
for icon in glob('icons/*.icon'):
try:
file = open(icon)
name = os.path.splitext(os.path.basename(icon))[0]
try:
icons[name] = file.readline().strip()
except IOError, e:
sys.stderr.write('Failed to read "%s" (%s)\n' % (icon, str(e)))
finally:
file.close()
except IOError, e:
sys.stderr.write('Failed to open "%s" (%s)\n' % (icon, str(e)))

fiddler616
December 17th, 2008, 09:12 AM
It was with trepidation I wrote that. I fully expected that sort of criticism and wondered whether it was worth it.

On the other hand, is it wrong to teach through example? Hmm...where's LaRoza when you need him? That's quite the conundrum...I guess the difference lies not in what you post, but how the OP responds to it. If the OP takes the time to read it, understand it, tweak it, even grok it--then we'll all walk away smiling. However if he just says "Oh, all I need to do is copy snova's snippet right here, and I need to feed it ___ variables, and it'll return ____, then nobody gains.

digitalvectorz
December 18th, 2008, 06:41 PM
hrm...I agree with both of you in a sense. The short version, imo, seems more of a bragging piece of code (though a fine piece of code that looks to be); the commented version however, is a better solution, cause then it gives people the opportunity to read and dissect. That's how I learn best, typically.

However, what irks me, is why in the world you're defining the code as "PHP Code" ? Though I must say, the color is attractive.

Just adding two-cents and some irony to the potluck.

cheers.

fiddler616
December 18th, 2008, 06:51 PM
However, what irks me, is why in the world you're defining the code as "PHP Code" ? Though I must say, the color is attractive.
The four options are quote, code, HTML and PHP. Quote and HTML are both bad. Code may be technically more correct, but the syntax highlight PHP provides makes everything much more readable. If only the forum staff would add a Python button...:)

nvteighen
December 18th, 2008, 07:13 PM
Hmm...where's LaRoza when you need him?

LaRoza is a she :)


That's quite the conundrum...I guess the difference lies not in what you post, but how the OP responds to it. If the OP takes the time to read it, understand it, tweak it, even grok it--then we'll all walk away smiling. However if he just says "Oh, all I need to do is copy snova's snippet right here, and I need to feed it ___ variables, and it'll return ____, then nobody gains.

My policy is to analize whether the poster knows what he/she's doing or not. I just don't reply to a forum regular or a someone that seems an experienced programmer the same way as to a newbie. First of all, the type of question reveals the level of the poster and also if code is posted or not.


The four options are quote, code, HTML and PHP. Quote and HTML are both bad. Code may be technically more correct, but the syntax highlight PHP provides makes everything much more readable. If only the forum staff would add a Python button...:)

If you want color, use code and http://www.challenge-you.com/bbcode but I have stepped back to non-color code... it's neutral.

fiddler616
December 18th, 2008, 07:17 PM
LaRoza is a she :)
Ladies and gentlemen:

We have been decieved.

http://laroza77.blogspot.com/2008/12/end-of-secret-and-observations.html

(If you're too lazy to read it all: )




The truth is, I'm just a laid back male, who was mistaken for a female, and I didn't care to correct it

digitalvectorz
December 18th, 2008, 07:31 PM
Okay this thread has exceeded beyond it's point of discussion. Let's keep it to the topic;

The starter of this topic escapes me atm, but if you feel that your question(s) have been answered, please mark this as SOLVED or provide us with some more feedback so that we can better try to get an answer to you :-)


Thanks,
dvz

fiddler616
December 18th, 2008, 07:34 PM
Okay this thread has exceeded beyond it's point of discussion. Let's keep it to the topic;

The starter of this topic escapes me atm, but if you feel that your question(s) have been answered, please mark this as SOLVED or provide us with some more feedback so that we can better try to get an answer to you :-)


Thanks,
dvz
+1
Although the OP seems to have gone AWOL.

robz0rz
December 18th, 2008, 09:37 PM
Still here ;) I haven't had time to get on the forums

This is how my code looks now
for foo in os.listdir(".icons/branding/"):
if foo.endswith(".svg"):
temp = foo[:-4]
brandCodes.append(temp)
if os.path.exists(".icons/branding/"+temp+".name"):
brandNames[temp] = open(".icons/branding/"+temp+".name").readline()[:-1]
else:
brandNames[temp] = temp

It's not perfect code, right now, my main concern is that this script works. If I want to write nice code, I'll program in C++ like I usually do. Also, this is part of an initialization process near the start of my script, nothing can go wrong due to faulty input really.

Thanks for all your help

fiddler616
December 18th, 2008, 09:39 PM
Still here ;) I haven't had time to get on the forums

This is how my code looks now
for foo in os.listdir(".icons/branding/"):
if foo.endswith(".svg"):
temp = foo[:-4]
brandCodes.append(temp)
if os.path.exists(".icons/branding/"+temp+".name"):
brandNames[temp] = open(".icons/branding/"+temp+".name").readline()[:-1]
else:
brandNames[temp] = tempIt's not perfect code, right now, my main concern is that this script works. If I want to write nice code, I'll program in C++ like I usually do. Also, this is part of an initialization process near the start of my script, nothing can go wrong due to faulty input really.

Thanks for all your help
Great!
(It'd be even a little bit greater if you marked the thread as solved, by the way...)