LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  How is input data represented in Python

    Employee
    Posted 07-03-2010 01:59

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Brad.Shea

    When you have an input pin into a Python node, how do you refer to the data from the input?
    i.e. is it a nested list, where the list contains each row, and the value for each record in the list is another list containing each column for that row?
    Are the column names and types referred to separately, or just as the first row?

    Thanks, Brad


  • 2.  RE: How is input data represented in Python

    Employee
    Posted 07-06-2010 06:37

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rboccuzzi

    you need to read the data from the input:
    rec = self.inputs[0].read()
    and then you can refer to the data in the record using subscripts, like this:
    rec["Some Field Name"]
    or even
    rec[1]
    if you want to refer by position.

    And if you want to understand the metadata (field names and types), you want to look at:
    self.inputs[0].metadata, which is a class, and you can do things like:
    self.inputs[0].metadata.find("FieldName") to find out if a field exists, and if so, what the postion is. You can also do:
    self.inputs[0].metadata["FieldName"] to get at the type of the field.

    One place you can look to find example nodes written in Python is the core library. Feel free to look at (but don't change!) some of the python nodes in that library, and you will see examples of code doing some of the things mentioned above.

    Cheers
    Rich


  • 3.  RE: How is input data represented in Python

    Employee
    Posted 07-08-2010 17:01

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Brad.Shea

    Thanks Rich for your help, I have got my node running now.
    I assume that self.inputs[0] refers to the first input, and if there was a second input I would need to use self.inputs[1] - can you please confirm this.
    This raises other questions though, i.e. if "while quant.permitsRunning(self):" is the method for looping though each record in the input dataset, how would you loop through 2 inputs?
    eg. if input 1 has 100 records and input 2 has 5 records, is it possible to read input 1 rec 1 and then apply proessing steps for input 2 rec 1, input 2 rec 2, ... , input 2 rec 5, then move onto input 1 rec 2 and do the same processing?
    From looking at a core node it appears that pump loops through self.inputs[0] first then when self.inputs[1] when self.inputs[0].read() is None


  • 4.  RE: How is input data represented in Python

    Employee
    Posted 07-12-2010 06:35

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rboccuzzi

    Yes, if there were a second input, it would be self.inputs[1].

    As for processing records...the input does not store records for you, so as you read them, they are removed from the input. So if you wish to pass over all the data in the second input multiple times, once for each record in the first input, you would have to cache the values in the second input in some way (either memory or disk, paying attention to the possible size of the input, so not to exhaust memory).

    The artifact of pumping through the first input, and then moving to the second in the example you were looking at is not a requirement of the python interface, but rather an implementation detail of that node...that particular node probably treats all inputs together, so processes the first input, then moves on to the second, processing similarly. There is no requirement in the API to do this.

    Cheers
    Rich


  • 5.  RE: How is input data represented in Python

    Employee
    Posted 02-11-2011 07:08

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ljarvio

    Hello,

    I'm a novice with Python nodes, and it is unclear to me how to manipulate input-data and then output the records like in "normal" nodes.

    my problem is to extract top-3 amounts for each company from data set like below:


    Company,amount:int
    A,1
    A,2
    A,3
    A,4
    B,11
    B,22
    B,33
    B,44
    B,55
    C,111
    C,222
    C,333
    C,444
    C,555
    C,666


    How should I implement a Python(?) Node, which takes a list of companies (A,B,C) as input and outputs three rows for each company? see attached project, where composite "manual split" contains the desired result.

    I would be very eager to learn the basics of Python nodes, so I appreciate all help!

    br, Lippo J�rvi�
    Attachments:
    auto_split.brg


  • 6.  RE: How is input data represented in Python

    Employee
    Posted 02-11-2011 08:37

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: jodycrutchfield

    Lippo,

    You can get this data with just your sort node and the agg node below.

    Does this way work for you? Or are you trying to learn about the python node?

    node:Agg
    bretype:core::Agg
    editor:sortkey=4d5554b512ef61dd
    input:@40fd2c7427456e5b/=sort_by_company_amount_2.40fd2c746a2a3b47
    output:@40fd2c744c862db0/=
    prop:GroupBy=<<EOX
    Company
    EOX
    prop:Script=<<EOX
    if firstInGroup then {
    c = 0
    }
    c = c + 1

    emit *
    where c <= 3

    EOX
    editor:XY=360,450
    end:Agg


  • 7.  RE: How is input data represented in Python

    Employee
    Posted 02-14-2011 00:57

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ljarvio

    many thanks, I should have realised that myself however, say I want to do the same with a Python node, how would it go then? I'm interested in handling the inputs and outputs in general..

    br,

    Lippo


  • 8.  RE: How is input data represented in Python

    Employee
    Posted 02-18-2011 12:22

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: jodycrutchfield

    Lippo,

    Attached is a graph that contains the Agg Ex and a Python Node example.

    Here is my discaimer: Python as with any programming language provides many ways to accomplish the same thing. This is just an example that worked for me at the time. It does not make it the right way or the wrong way, just one way.

    On the python node you will see a parameter I created called OutputRecords. This allows you to customize how many records you want to output. Set it to 3 and it will display the first 3 records per company.

    Have fun exploring the many things you can do in Python and BRE.

    Jody
    Attachments:
    TopNExample.brg


  • 9.  RE: How is input data represented in Python

    Employee
    Posted 02-21-2011 03:04

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ljarvio

    Thanks a lot, this kind of example was exactly what I was looking for!

    Is there documentation available on BrainNode methods such as "pump", "handleRecord" etc. which would clarify them a bit?


  • 10.  RE: How is input data represented in Python

    Employee
    Posted 03-04-2011 18:41

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rboccuzzi

    There is not currently extra documentation on those functions, aside from the comments in the code describing how they are to be used. You can look at the core library however, to see many examples of how python nodes have been written, as many of them have been written in python.

    Cheers
    Rich