Join us at IRC!
It is never to LATE to become what you never WERE.
Friday, May 25, 2012
Navigation
Members Online
Total Online: 35
Web Spiders: 14
Guests Online: 34
Members Online: 1

Registered Members: 70209
Newest Member: KalareShou
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python OOP help

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 03-03-10 19:46
Hello again ^^

I've encountered a problem with python OOP that I cannot find any solution to :/ Although I think that I might be the one thinking wrong, yet still I need some help so... :P

I'm trying to create a scblockedript that reads the name of every folder (a project) in a workspace folder and then display some info about a chosen project.


import os

class project:
"""Projects in Workspace"""
def __init(self):
pass
def name(self):
self.name = name
def stats(self):
pass

directory = os.getcwd() + "\Workspace"
dir_content = os.listdir(directory) # --> dir_content = ['Website1", "Website2"]


The question is, how do I get dir_content[0] (website1) to become an instance of the project class?



Edited by Demons Halo on 03-03-10 19:54
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python OOP help

COM
Banned



Posts: 800
Location:
Joined: 31.08.07
Rank:
God
Posted on 03-03-10 20:45
dir_content[0]=project() ?


K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth!
Author

RE: Python OOP help

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 03-03-10 20:58
class project:
"""Projects in Workspace"""
def __init__(self, path):
self.path = path
def name(self, name):
self.name = name

directory = os.getcwd() + "\Workspace"
workspace_content = os.listdir(directory)

for i in range(len(workspace_content)):
project_path = os.path.join(directory, workspace_content[i])
workspace_content[i] = project(project_path)
workspace_content[i].name(workspace_content[i])
print workspace_content[i].name


results:
<__main__.project instance at 0x01B03DF0>

-_- wtf am I doing wrong?



Edited by Demons Halo on 03-03-10 21:03
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python OOP help

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 04-03-10 19:30
ah I see...

doing as you suggested COM will give me a list with instances as elements. what I want to do is create x amount of instances automatically. This should be done with some kind of a loop cause the number of instances will be defined by the number of folders --> 10 folder = 10 instances by the names:

folder1
folder2
etc.
folder10


suggestions? :p
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python OOP help

COM
Banned



Posts: 800
Location:
Joined: 31.08.07
Rank:
God
Posted on 04-03-10 19:43
Convert list to dict and then use that, fromkeys maybe?


K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth!
Author

RE: Python OOP help

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 04-03-10 19:46
COM wrote:
Convert list to dict and then use that, from keys maybe?


I struggled for a day and right when I came up with the solution you posted it :p mohah thnx for the answer anyway =)


Edited by Demons Halo on 04-03-10 19:47
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python OOP help

COM
Banned



Posts: 800
Location:
Joined: 31.08.07
Rank:
God
Posted on 04-03-10 19:50
Demons Halo wrote:
COM wrote:
Convert list to dict and then use that, from keys maybe?


I struggled for a day and right when I came up with the solution you posted it :P mohah thnx for the answer anyway =)

If you're going to quote people, don't try to fix typos, there's a reason for there not being a space between from and keys.
Also, you do know that I really don't know anything about python, right? So come on, man, get your shit together :P

Edit: oh and if you really are this stuck and need help, etc. I'd recommend you talk to ynori about it, unlike me, he's actually good with python and loves to help :)


K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth!

Edited by COM on 04-03-10 19:53
Author

RE: Python OOP help

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 04-03-10 19:54
blame it on firefox spell check =P
who said that I do? XD It's just a hobby and this is my first attempt @ OOP.

mohahah :ninja:

Edit: I know XD he's awesome! but I think that he has had about enough of newPz asking him questions (including me).

Edited by Demons Halo on 04-03-10 19:56
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python OOP help

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Hacker Level 1
Posted on 06-03-10 14:40
ok here's a more complicated problem I encountered using pygtk...
I'm trying to create a treeview for the folders in my workspace, now I've created the treeview and it works like magic. The only problem is that the current tree look like this:

- project 1
|-- file 1
|-- file 2

What I want to do is having an icon ex. file1.png instead of the | symbol.

here's my code so far

treestore = gtk.TreeStore(gtk.gdk.Pixbuf, str) #TreeModel with pixbud and string
for i in range(len(keys)):
tree_menu = treestore.append(None, [self.arrowsout_icon, [keys[i]]]) #Parent row --> arrowout_icon and first project name
for x in range(len(workspace_projects[keys[i]].file_list)):
treestore.append(tree_menu, [self.film_icon,[workspace_projects[keys[i]].file_list[x]]]) #child row --> for every item in folder --> film_icon and filename

tree = gtk.TreeView(treestore) #TreeView for the TreeModel
workspace_tree = gtk.TreeViewColumn("Projects in workspace") #Show the column
tree.append_column(workspace_tree) #Add the column to the tree

cell = gtk.CellRendererPixbuf() #Reder the data in each cell
workspace_tree.pack_start(cell, True)
workspace_tree.add_attribute(cell, "pixbuf", 0)
workspace_tree.set_sort_column_id(0) #Make the column sortable!


I can't figure out why this isn't working. If i remove the icons and use CellRendererText(), everything works. but when using icons and Pixbuf, things seem to get a bit weird.

No error message is shown, only empty cells. :/

My guess is that there is something wrong with the attributes I'm including, but the PyGTK documentation sucks, so... :P


Edited by Demons Halo on 06-03-10 14:41
base_dropper@hotmail.com www.demonshalo.com
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.