Follow us on Twitter!
The measure of a mans life is not how well he dies, but how well he lives.
Saturday, April 19, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 34
Guests Online: 31
Members Online: 3

Registered Members: 82839
Newest Member: fezphantom
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:
Apprentice
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 script that reads the name of every folder (a project) in a workspace folder and then display some info about a chosen project.

Code

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


Member

Your avatar

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


Author

RE: Python OOP help

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
Posted on 03-03-10 20:58
Code
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:
Apprentice
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? Pfft
base_dropper@hotmail.com www.demonshalo.com
Author

RE: Python OOP help


Member

Your avatar

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


Author

RE: Python OOP help

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
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 Pfft 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


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
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 :)




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

RE: Python OOP help

Demons Halo
Member



Posts: 261
Location: Sweden
Joined: 26.03.09
Rank:
Apprentice
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:
Apprentice
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
Code

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