0

I am writing an add-on for some softwate, using its API. What i need to do is to extract necessary data. I use 'FOR' to go thought API classes. Each object has properties: index (from 0), type (Lin, Ptp, and some others), and value. Going through list of objects, I am interested in two types of objects - those that have type 'Lin' or 'Ptp'; so a few conditions should be met:

As to Lin type:

  • if there is some Ptp before Lin (there may be other objects of other types between them, though), Lin gets Ptp's value [Ptp....Lin].
  • if there is some other Lin before Lin (there may be other objects of other types between them, though), Lin gets that previous closest Lin's value [Lin....Lin].
  • if there is neither Lin nor Ptp before Lin (there may be other objects of other types between them, though), Lin gets value "0" [...Lin].

As to Ptp type, it always gets its own value

As i am a beginner in Python, my thoughs are mixed now and i cannot come up with appropriate algorithm.

I was thinking it should be somthing like this:

for object in obects:
   If object.type == Ptp:
     ...object gets its own value
   elif object.type == Lin:
     ...

Here, there should be other 3 conditions according to [...Lin] or [Lin...Lin] or [Ptp...Lin]

4
  • Your question is difficult to understand. Posting some code will help. Commented Dec 15, 2010 at 8:50
  • What does "object gets its own value" mean? Commented Dec 15, 2010 at 9:20
  • Value that is stored inside the object. I need to extract it and save it as a string in a file Commented Dec 15, 2010 at 9:22
  • Your pseudocode makes no mention of files and is therefore incomplete. Commented Dec 15, 2010 at 22:39

3 Answers 3

2

As i am a beginner in Python, my thoughs are mixed now and i cannot come up with appropriate algorithm.

If you are trying to come up with an appropriate algorithm, take a step back. Forget Python (C++, Fortran, Logo, Awk, etc...) and think about the problem you are trying to solve. Try writing some pseudocode on paper.

From your pseudocode, the Python should become more apparent and any technical difficulties could be posed as more specific questions on StackOverflow (for instance) or asked of your colleagues.

Sign up to request clarification or add additional context in comments.

2 Comments

Did that and you can see it on the page
That isn't even close to enough detail. Keep thinking.
1

I'd suggest you iterate over your objects, and remember the last occurrence of a Lin or Ptp type (whatever those may be ... :)):

lastOccurrence = None
for obj in objects:
    if obj.type not in ('PtP', 'Lin'):
        continue

    if obj.type == 'Lin':
        if lastOccurrence is not None:
            obj.value = lastOccurrence.value
        else:
            obj.value = "0"

    lastOccurrence = obj                

or something like that ...

Comments

0

I'd use something called a finite-state machine or FSM to go though the API objects. As you encounter the different types and associated properties you can store information about what was seen in the FSM's "state" which also determines what happens when the next "event" (items you are iterating over) is occurs or is encountered. Information gathered can be output as required (i.e. when a certain state is reached.

FSM's are a fairly simple concept to lean and program (in almost any language) and very useful for this sort of problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.