Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Python OOP help


Demons Halo's Avatar
Member
0 0

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.

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?


ghost's Avatar
0 0

dir_content[0]=project() ?


Demons Halo's Avatar
Member
0 0
    """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?


Demons Halo's Avatar
Member
0 0

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


ghost's Avatar
0 0

Convert list to dict and then use that, fromkeys maybe?


Demons Halo's Avatar
Member
0 0

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 =)


ghost's Avatar
0 0

Demons Halo wrote: [quote]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 =) [/quote] 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 :)


Demons Halo's Avatar
Member
0 0

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).


Demons Halo's Avatar
Member
0 0

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 --&gt; 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 --&gt; for every item in folder --&gt; film_icon and filename**

tree = gtk.TreeView(treestore) ** #TreeView for the TreeModel**
workspace_tree = gtk.TreeViewColumn(&quot;Projects in workspace&quot;)  **#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, &quot;pixbuf&quot;, 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