Nallo's Profile User Rating: -----

Reputation: 161 Stalwart
Group:
Contributors
Active Posts:
247 (0.14 per day)
Joined:
19-July 09
Profile Views:
8,087
Last Active:
User is offline Aug 02 2013 04:28 PM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
50
Icon   Nallo has not set their status

Posts I've Made

  1. In Topic: Trouble understanding Python classes

    Posted 11 Jul 2013

    Think of a class as a blueprint to create some object (an so called instance of the class). Just like built ins like list. __init__ is called when an instance is created and self refers to the instance.

    Two toy examples that I hope help (not sure its late here and I am bad at explaining things late at night):
    one = [1,1,1]
    two = [2,2,2]
    #one and two are instances of the list "class"
    #(1,1,1) and (2,2,2) are passed to the __init__ of the list class
    
    #"self" is explicitly used
    list.append(one, 1)
    print one
    #the above is the same as
    #self is implicitly used
    two.append(2)
    print two
    


    #class definition
    class MySilly(object):
        def __init__(self, initial_values_of_instances):
            self.im_who = initial_values_of_instances
        def who_am_I(self):
            return self.im_who
    
    #up to here python knows that there is a class
    #but has not done anything with it (except creating a namespace for it)
    
    #create some instances:
    one = MySilly("1")
    two = MySilly("2")
    
    #do something with them
    print "calling one"
    print one.who_am_I()
    print "calling two"
    #for a twist use the instance explicitly
    #call it via the class
    #but this time we need to pass in the instance
    #in the above case it was implicitly given, here we need to do it explicitly:
    print MySilly.who_am_I(two)
    
    
  2. In Topic: Reading file into 2D List

    Posted 11 Jul 2013

    ZeroTorrent, you were almost there. The only thing left is to do the right thing in the else block of you programm:
    Increase the counter and add a new element for the next batch of data:
    i = 0
    #using global variables is ugly
    
    object_data = []
    object_data.append([])
    
    for rows in data.splitlines():
        elems = rows.split(',')
        if elems[0] != "@":
            object_data[i].append((float(elems[0]),float(elems[1]),
                                       float(elems[2]),float(elems[3]),
                                       float(elems[4]),float(elems[5]),
                                       int(elems[6])))
        else:
            i += 1 #increase the counter
            object_data.append([]) #add a new (empty element) for the next data
    
    


    Though you may want to make the code a little more robust, readable and maintainable (some hints in the comments):
    def convert_line(line):
        #making this an own function is good practice
        #you may change the data format later
        #and this way there is one place to change in that event
        elems = line.split(',')
        return (float(elems[0]),float(elems[1]),float(elems[2]),float(elems[3]),
            float(elems[4]),float(elems[5]),int(elems[6]))
    
    def process_data_file(df):
        DATA_SEPARATOR = "@" #magical symbols deserve an own name!!!
        processed = [[]]
        for line in df.splitlines():
            if DATA_SEPARATOR in line:
                #a little more robust, think about a ",@" typo in the data file
                #compared to a line[0] == "@"
                processed.append([])
            else:
                processed[-1].append(convert_line(line))
                #using [-1] to access the last element, so no need to keep a counter variable :-)
        return processed
    
    
  3. In Topic: Extracting subsequences from a list or string

    Posted 3 Jul 2013

    I don't have a beautiful way either. Though if you only need sequential access to the subsequences you could use a generator to avoid memory overhead:
    def subsequences(l,n):
      for i in range (len(l)-n+1):
        yield [l[i:i+n]]
    
    #sequential access
    for subs_5 in subsequences(l, 5):
        pass #do something
    
    


    or more consciese:
    for subs_5 in (l[i:i+5] for i in range (len(l)-5+1)):
        pass
    
    
  4. In Topic: Run Script on Wake

    Posted 3 Jul 2013

    More importantly python doesn't have a switch statement. Don't throw bash code at python. It wont work.

    Sorry for the rant.
  5. In Topic: MetaClass for a class extending from sqlalchemy declarative base

    Posted 3 Jul 2013

    Well diclaimer first: I have never used sqlalchemy and metaclasses feel like dark magic to me.

    Looking at the error decription I checked for the __metaclass__ atribute of Base. None there, so Python will use type(Base) insted.

    this turns out to be:
    >>> type(Base)
    <class 'sqlalchemy.ext.declarative.DeclarativeMeta'>
    
    


    So maybe in line 9
    class SampleMeta(sqlalchemy.ext.declarative.DeclarativeMeta):
    
    


    instead of
    class SampleMeta(type):
    
    


    But I am fishing in the dark here.

My Information

Member Title:
D.I.C Head
Age:
101 years old
Birthday:
January 1, 1913
Gender:
Programming Languages:
who cares

Contact Information

E-mail:
Private
Website URL:
Website URL  http://