Re: [Maya-Python] Check and grab the latest value of a certain condition using dictionary

2017-07-10 Thread Marcus Ottosson
Interesting challenge! Here’s one more from me, altering the initial layout
of your data.

assets = [
{
"name": "model",
"version": 5,
"status": "Approved"
},
{
"name": "model",
"version": 3,
"status": "In Use"
},
{
"name": "model",
"version": 2,
"status": "In Use"
},
{
"name": "model",
"version": 6,
"status": "In Progress"
},
{
"name": "model",
"version": 1,
"status": "Approved"
},
{
"name": "model",
"version": 4,
"status": "Pending"
},
]
# We'll assume the list is sorted
assets = sorted(assets, key=lambda asset: asset["version"], reverse=True)

latest = (
next((a for a in assets if a["status"] == "Approved"), None) or
next((a for a in assets if a["status"] == "In Progress"), None) or
next((a for a in assets if a["status"] == "Pending"), None) or
None
)
if not latest:
raise Exception("No suitable version found")

print(latest)

Could wrap the query into a function of its own, for readability.

def has_status(status):
return next((
a for a in assets if a["status"] == status
), None)

latest = (
has_status("Approved") or
has_status("In Progress") or
has_status("Pending") or
None
)
if not latest:
raise Exception("No suitable version found")

print(latest)

​

On 11 July 2017 at 03:35, Justin Israel  wrote:

>
>
> On Tue, Jul 11, 2017 at 1:52 PM Alok Gandhi 
> wrote:
>
>> If you consider creating the model data dict having status as the keys
>> then the whole search becomes increasingly simple:
>>
>
> True. Although imagine that the model data is likely to have more fields
> than just a status. It seems it would become difficult to support this
> model when it contains other types of info. Otherwise you would have to
> maintain this as an additional "index" model, and keep it up to date when
> your primary model changes.
>
>
>>
>> # Force an order for status search
>> STATUS_SEARCH_ORDER = ('Approved', 'In Progress', 'Pending', 'In Use')
>>
>> def get_model(model_data):
>> model = None
>> model_status = None
>> # Get latest
>> for status in STATUS_SEARCH_ORDER:
>> models = model_data.get(status)
>> if models:
>> model = sorted(models, reverse=True)[0]
>> model_status = status
>> break
>>
>> return model, model_status
>>
>> if __name__ == '__main__':
>> # Model data is based on status on rather than model. Then it becomes
>> # increasingly simple to query by status.
>> model_data = {
>> 'In Progress': ['model_v003', 'model_v001', 'model_v004'],
>> 'In Use': ['model_v002'],
>> 'Approved': ['model_v005'],
>> 'Pending': ['model_v006'],
>> }
>>
>> # Test 01 - 'Approved' status
>> model, status = get_model(model_data=model_data)
>> assert model == 'model_v005'
>> assert status == 'Approved'
>>
>> # Test 02 - 'In Progress' status
>> # Let's remove any approved model from model data
>> # Now we should get the latest from 'In Progress'
>> model_data['Approved'] = []
>> model, status = get_model(model_data=model_data)
>> assert model == 'model_v004'
>> assert status == 'In Progress'
>>
>> # Test 03 - 'Pending' status
>> # Removing any 'In Progress' models
>> model_data['In Progress'] = []
>> model, status = get_model(model_data=model_data)
>> assert model == 'model_v006'
>> assert status == 'Pending'
>>
>> # Test 04 - 'In Use' status
>> # Removing any 'Pending' models
>> model_data['Pending'] = []
>> model, status = get_model(model_data=model_data)
>> assert model == 'model_v002'
>> assert status == 'In Use'
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Python Programming for Autodesk Maya" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to python_inside_maya+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/
>> msgid/python_inside_maya/CAPaTLMQFx6zJuyNLvnQoS3J7ioz2K
>> v8S55i5_yFwqzNQ%3D%3DGTFw%40mail.gmail.com
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to python_inside_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/python_inside_maya/CAPGFgA3nBcuus2AT4UAZqfV5zhEru
> oQDCiPWjbaQ9-ZNrqcqYg%40mail.gmail.com
> 

Re: [Maya-Python] Check and grab the latest value of a certain condition using dictionary

2017-07-10 Thread Justin Israel
On Tue, Jul 11, 2017 at 1:52 PM Alok Gandhi 
wrote:

> If you consider creating the model data dict having status as the keys
> then the whole search becomes increasingly simple:
>

True. Although imagine that the model data is likely to have more fields
than just a status. It seems it would become difficult to support this
model when it contains other types of info. Otherwise you would have to
maintain this as an additional "index" model, and keep it up to date when
your primary model changes.


>
> # Force an order for status search
> STATUS_SEARCH_ORDER = ('Approved', 'In Progress', 'Pending', 'In Use')
>
> def get_model(model_data):
> model = None
> model_status = None
> # Get latest
> for status in STATUS_SEARCH_ORDER:
> models = model_data.get(status)
> if models:
> model = sorted(models, reverse=True)[0]
> model_status = status
> break
>
> return model, model_status
>
> if __name__ == '__main__':
> # Model data is based on status on rather than model. Then it becomes
> # increasingly simple to query by status.
> model_data = {
> 'In Progress': ['model_v003', 'model_v001', 'model_v004'],
> 'In Use': ['model_v002'],
> 'Approved': ['model_v005'],
> 'Pending': ['model_v006'],
> }
>
> # Test 01 - 'Approved' status
> model, status = get_model(model_data=model_data)
> assert model == 'model_v005'
> assert status == 'Approved'
>
> # Test 02 - 'In Progress' status
> # Let's remove any approved model from model data
> # Now we should get the latest from 'In Progress'
> model_data['Approved'] = []
> model, status = get_model(model_data=model_data)
> assert model == 'model_v004'
> assert status == 'In Progress'
>
> # Test 03 - 'Pending' status
> # Removing any 'In Progress' models
> model_data['In Progress'] = []
> model, status = get_model(model_data=model_data)
> assert model == 'model_v006'
> assert status == 'Pending'
>
> # Test 04 - 'In Use' status
> # Removing any 'Pending' models
> model_data['Pending'] = []
> model, status = get_model(model_data=model_data)
> assert model == 'model_v002'
> assert status == 'In Use'
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to python_inside_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMQFx6zJuyNLvnQoS3J7ioz2Kv8S55i5_yFwqzNQ%3D%3DGTFw%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3nBcuus2AT4UAZqfV5zhEruoQDCiPWjbaQ9-ZNrqcqYg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] Check and grab the latest value of a certain condition using dictionary

2017-07-10 Thread Alok Gandhi
If you consider creating the model data dict having status as the keys then
the whole search becomes increasingly simple:

# Force an order for status search
STATUS_SEARCH_ORDER = ('Approved', 'In Progress', 'Pending', 'In Use')

def get_model(model_data):
model = None
model_status = None
# Get latest
for status in STATUS_SEARCH_ORDER:
models = model_data.get(status)
if models:
model = sorted(models, reverse=True)[0]
model_status = status
break

return model, model_status

if __name__ == '__main__':
# Model data is based on status on rather than model. Then it becomes
# increasingly simple to query by status.
model_data = {
'In Progress': ['model_v003', 'model_v001', 'model_v004'],
'In Use': ['model_v002'],
'Approved': ['model_v005'],
'Pending': ['model_v006'],
}

# Test 01 - 'Approved' status
model, status = get_model(model_data=model_data)
assert model == 'model_v005'
assert status == 'Approved'

# Test 02 - 'In Progress' status
# Let's remove any approved model from model data
# Now we should get the latest from 'In Progress'
model_data['Approved'] = []
model, status = get_model(model_data=model_data)
assert model == 'model_v004'
assert status == 'In Progress'

# Test 03 - 'Pending' status
# Removing any 'In Progress' models
model_data['In Progress'] = []
model, status = get_model(model_data=model_data)
assert model == 'model_v006'
assert status == 'Pending'

# Test 04 - 'In Use' status
# Removing any 'Pending' models
model_data['Pending'] = []
model, status = get_model(model_data=model_data)
assert model == 'model_v002'
assert status == 'In Use'

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMQFx6zJuyNLvnQoS3J7ioz2Kv8S55i5_yFwqzNQ%3D%3DGTFw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] Check and grab the latest value of a certain condition using dictionary

2017-07-10 Thread Justin Israel
Hey,

Check out these two approaches:
https://repl.it/JViC/1

The overall approach is to have a custom comparison function so that you
can sort you model dict by status and then by name. We would want our
subset of statuses that are "available" to come first, followed by a
secondary sorting of the highest name.

In the first approach, it is more simple and just identifies the subset of
our "available" statuses in a dictionary with an order value. Then we write
a cmp function that will sort them on status first, and then reverse order
by name (note it is just alpha-numeric default sorting and not natural
sorting).

Available = {
  'Approved': 0,
  'In Progress': 1,
  'Pending': 2,
}
def cmpStatus(i1, i2):
  name1, status1 = i1
  name2, status2 = i2

  if status1 != status2:
return cmp(Available.get(status1, 99), Available.get(status2, 99))

  # Reverse naming order
  return cmp(name1, name2) * -1
print [name for name, status in sorted(models.iteritems(),
cmp=cmpStatus) if status in Available]# ['model_v005', 'model_v004',
'model_v003', 'model_v001', 'model_v006']

​

But another approach would be to organize your status value into constants,
and provide mappings between values and display names.

class Status(object):
  APPROVED = 0
  IN_PROGRESS = 1
  PENDING = 2
  IN_USE = 3

  ToValue = {
'Approved': APPROVED,
'In Progress': IN_PROGRESS,
'Pending': PENDING,
'In Use': IN_USE,
  }

  ToName = {v:k for k,v in ToValue.iteritems()}

  Available = frozenset([APPROVED, IN_PROGRESS, PENDING])
def cmpStatus2(i1, i2):
  name1, status1 = i1[0], Status.ToValue[i1[1]]
  name2, status2 = i2[0], Status.ToValue[i2[1]]

  if status1 != status2:
return cmp(status1, status2)

  # Reverse naming order
  return cmp(name1, name2) * -1
print [name for name, status in sorted(models.iteritems(), cmp=cmpStatus2) \
if Status.ToValue[status] in Status.Available]# ['model_v005',
'model_v004', 'model_v003', 'model_v001', 'model_v006']

​

Both of them return the same results.

Justin


On Tue, Jul 11, 2017 at 7:55 AM likage  wrote:

> I have created a dictionary in which it stores the information as follows:
> dict = {'model_v001': 'In Progress',
>  'model_v002': 'In Use',
>  'model_v003': 'In Progress',
>  'model_v004': 'In Progress',
>  'model_v005': 'Approved',
>  'model_v006': 'Pending'}
>
>
>
> I am trying to achieve that, from the contents in the dictionary, in which
> the conditions are as follows:
>
>- Grab latest 'Approved' versions if there are any
>- Grab latest 'In Porgress' versions if there are any and if no
>'Approved' versions are found
>- Grab latest 'Pending' versions if there are any, only if no
>'Approved' and 'In Progress' versions are found
>
>
> Using the above example, I should be grabbing `model_v005` as it is the
> latest Approved.
> So if the `model_v004`and `model_v005` status are reversed, `model_v004`
> will be grabbed instead.
>
> keys = dict.keys()
> valid_status = ['Element Approved', 'Element in Pipe']
> if dict[keys[0]] in valid_status:
> #Use this version
> print 'Using this version'
> else:
> #iterate and check the 'previous' version?
>
> What is the best way to go around this?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to python_inside_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/24accbdb-6f51-4d30-a43f-148701955447%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2VKPG8YdEiWz%2BaQKxyLoMeeVsTeUMRwG_ArUEZ9c1Zng%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[Maya-Python] Check and grab the latest value of a certain condition using dictionary

2017-07-10 Thread likage
I have created a dictionary in which it stores the information as follows:
dict = {'model_v001': 'In Progress',
 'model_v002': 'In Use',
 'model_v003': 'In Progress',
 'model_v004': 'In Progress',
 'model_v005': 'Approved',
 'model_v006': 'Pending'}



I am trying to achieve that, from the contents in the dictionary, in which 
the conditions are as follows:

   - Grab latest 'Approved' versions if there are any
   - Grab latest 'In Porgress' versions if there are any and if no 
   'Approved' versions are found
   - Grab latest 'Pending' versions if there are any, only if no 'Approved' 
   and 'In Progress' versions are found
   

Using the above example, I should be grabbing `model_v005` as it is the 
latest Approved.
So if the `model_v004`and `model_v005` status are reversed, `model_v004` 
will be grabbed instead.

keys = dict.keys()
valid_status = ['Element Approved', 'Element in Pipe']
if dict[keys[0]] in valid_status:
#Use this version
print 'Using this version'
else:
#iterate and check the 'previous' version?

What is the best way to go around this?

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/24accbdb-6f51-4d30-a43f-148701955447%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.