What could be reasons for this code not to add to the dictionary(assume that it is going into the else block):
if dup_agent_log.modify_stamp > merge_agent_log.modify_stamp:
merge_agent_log.set_status_log(dup_agent_log.status, None, dup_agent_log.agents_id, dup_agent_log.source, dup_agent_log.status_type)
else:
print merge_agent_log.data # {}
now = str(datetime.datetime.now())
merge_agent_log.data[now] = [{"status": dup_agent_log.status, "change_agent": dup_agent_log.change_agent, "source": dup_agent_log.source}]
print "after", merge_agent_log.data # after {}
transaction.savepoint()
The AgentStatusLog() class(model):
class AgentStatusLog(Base):
__tablename__ = "agent_status_log"
id = Column(
Integer,
Sequence("agent_status_log_id", optional=True),
primary_key=True
)
status_id = Column(Integer, ForeignKey(StatusLookup.id))
_status = relationship(StatusLookup, uselist=False)
status = Column(Unicode(100), index=True)
previous_status = Column(Unicode(50), index=True)
effective_stamp = Column(DateTime, index=True)
modify_stamp = Column(DateTime, index=True)
agents_id = Column(Integer, ForeignKey(Agents.id))
agent = relationship(Agents, primaryjoin=Agents.id == agents_id, uselist=False)
_data = Column("data", Unicode(2048))
status_type = Column(Unicode(40), index=True)
change_agents_id = Column(Integer, ForeignKey(Agents.id))
change_agent = relationship(Agents, primaryjoin=Agents.id == change_agents_id, uselist=False)
source = Column(Unicode(100), index=True)
#property
def data(self):
if not self._data:
return {}
return json.loads(self._data)
#data.setter
def data(self, value):
self._data = json.dumps(value)
def set_data(self, field, value):
data = self.data
data[field] = value
self.data = data
def get_data(self):
if not self._data:
return {}
return json.loads(self._data)
def unset_data(self, field):
data = self.get_data()
if field in data:
del data[field]
self.data = data
#classmethod
def by_id(cls, id):
return Session.query(cls).filter(cls.id == id).first()
#classmethod
def by_agents_id(cls, aid):
return Session.query(cls).filter(cls.agents_id == aid).first()
#classmethod
def set_status_log(cls, status, change_agent=None, agent_id=None, source=None, status_type=None):
if agent_id:
sl = Session.query(cls).filter(cls.agents_id == agent_id).first()
sl.modify_stamp = func.now()
sl.source = source
if sl.status:
sl.previous_status = sl.status
sl.data[str(datetime.datetime.now())] = [{"status": sl.status, "change_agent": change_agent, "source": source}]
sl.status = status
sl.change_agents_id = change_agent
sl.status_type = status_type
transaction.commit()
Maybe I'm just over looking something simple, but this boggles my mind... When I do the same thing in my python shell, it works...
merge_agent_log is a SQLAlchemy object.
What possible could possibly cause this cause this?
Am I missing something? I have be having at this for two days...
Is there in any case an entry to a dict would fail using data["foo"] = "bar"?
No dict has failed here, it just that you created more than one dict and did not realize it.
Let's analyze merge_agent_log.data[now] = something closely: first the property getter is called. It reads _data and creates a new dict with json.loads. Then you modify this new dict, adding something for key now.
But you do not modify _data!
So the next time you call the property getter with merge_agent_log.data, you read the same string from _data and create another dict with the same contents you did before.
Try this:
data = merge_agent_log.data
data[now] = something
merge_agent_log.data = data # calls property setter, which modifies _data
Related
In my class BaseFoo, I want to determine which arguments of its subclass Foo were passed explicitly in the constructor:
class BaseFoo:
def __init__(self, created_at, updated_at, deleted_at):
self.created_at = created_at
self.updated_at = updated_at
self.deleted_at = deleted_at
self.is_set = {}
class Foo(BaseFoo):
def __init__(self, friend_id: int = None, start_time: datetime = None, id: int = None, end_time: datetime = None, created_at=None,
updated_at=None,
deleted_at=None):
super().__init__(created_at, updated_at, deleted_at)
self.id = id
self.friend_id = friend_id
self.start_time = start_time
self.end_time = end_time
My goal is to properly populate the is_set field of a BaseFoo/Foo instance according to what has been set explicitly by the constructor/setters. Here are a few examples of my end goal:
# input
foo = Foo(friend_id=1)
print(foo.is_set)
#output
# {"friend_id": True}
foo = Foo()
print(foo.is_set)
#output
# {}
foo = Foo(start_time=None)
foo.friend_id = 3
print(foo.is_set)
#output
# {"start_time": True, "friend_id": True}
foo = Foo(start_time=datetime.datetime.now())
foo.friend_id = 3
print(foo.is_set)
#output
# {"start_time": True, "friend_id": True}
I'm almost where I want to be with this method in BaseFoo:
def __setattr__(self, attrname, val):
import inspect
f = inspect.currentframe()
if 'self' in f.f_back.f_locals and issubclass(type(f.f_back.f_locals['self']), BaseFoo):
# call came from __init__
if attrname != "is_set":
self.is_set = self.is_set if hasattr(self, "is_set") else {}
self.is_set[attrname] = True
super().__setattr__(attrname, val)
else:
if attrname != "is_set":
self.is_set = self.is_set if hasattr(self, "is_set") else {}
self.is_set[attrname] = True
super().__setattr__(attrname, val)
The only missing piece here is that I need to know, in case __setattr__ has been triggered by the __init__ constructor, whether the val of attrname has been explicitly set by the user (if it has been explicitly set to its default value).
Is this possible?
I have what I would term a composite class
Whereby the class LegoSet is constructed from an instantiation of the class LegoSetVariant and inturn the class LegoSetVariant is constructed from a number of different class instantiations.
This is fine and I can successfully initialize the class. However, I then want to serialize the class object into JSON. This is proving a bit tricky as the composite classes are erroring as non-serializable.
class LegoSet:
def __init__(
self,
typename,
id,
productCode,
name,
slug,
primaryImage,
baseImgUrl,
overrideUrl,
variant,
):
self.__typename = typename
self.__id = id
self.__productCode = productCode
self.__name = name
self.__slug = slug
self.__primaryImage = primaryImage
self.__baseImgUrl = baseImgUrl
self.__overrideUrl = overrideUrl
self.__variant = variant
class LegoSetVariant:
def __init__(self, id, sku, salePercentage, attributes, price, listPrice):
self.__id = id
self.__sku = sku
self.__salePercentage = salePercentage
self.__atributes = attributes
self.__price = price
self.__listPrice = listPrice
class LegoSetVariantAttributes:
def __init__(
self,
rating,
maxOrderQuantity,
availabilityStatus,
availabilityText,
vipAvailabilityStatus,
vipAvailabilityText,
canAddToBag,
canAddToWishlist,
vipCanAddToBag,
onSale,
isNew,
featuredFlags,
):
self.__rating = rating
self.__maxOrderQuantity = maxOrderQuantity
self.__availabilityStatus = availabilityStatus
self.__availabilityText = availabilityText
self.__vipAvailabilityStatus = vipAvailabilityStatus
self.__vipAvailabilityText = vipAvailabilityText
self.__canAddToBag = canAddToBag
self.__canAddToWishlist = canAddToWishlist
self.__vipCanAddToBag = vipCanAddToBag
self.__onSale = onSale
self.__isNew = isNew
self.__featuredFlags = featuredFlags
class LegoSetVariantAttributesFeaturedFlags:
def __init__(self, key, label):
self.__key = key
self.__label = label
class LegoSetVariantPrice:
def __init__(self, formattedAmount, centAmount, currencyCode, formattedValue):
self.__formattedAmount = formattedAmount
self.__centAmount = centAmount
self.__currencyCode = currencyCode
self.__formattedValue = formattedValue
class LegoSetVariantListPrice:
def __init__(self, formattedAmount, centAmount):
self.__formattedAmount = formattedAmount
self.__centAmount = centAmount
Try this:
def unpack_into_dict(object):
res = {}
for key, val in object.__dict__.items():
if hasattr(val, '__dict__'):
res[key] = unpack_into_dict(val)
else:
res[key] = val
return res
Is there a sure-fire way to check that the class of an object is a sub-class of the desired super?
For Example, in a migration script that I'm writing, I have to convert objects of a given type to dictionaries in a given manner to ensure two-way compatability of the data.
This is best summed up like so:
Serializable
User
Status
Issue
Test
Set
Step
Cycle
However, when I'm recursively checking objects after depickling, I receive a Test object that yields the following results:
Testing data object type:
type(data)
{type}< class'__main.Test' >
Testing Class type:
type(Test())
{type}< class'__main.Test' >
Testing object type against class type:
type(Test()) == type(data)
{bool}False
Testing if object isinstance() of Class:
isinstance(data, Test)
{bool}False
Testing if Class isinstance() of Super Class:
isinstance(Test(), Serializable)
{bool}True
Testing isinstance() of Super Class::
isinstance(data, Serializable)
{bool}False
Interestingly, it doesn't appear to have any such problem prior to pickling as it handles the creation of dictionary and integrity hash just fine.
This only crops up with depickled objects in both Pickle and Dill.
For Context, here's the code in it's native environment - the DataCache object that is pickled:
class DataCache(object):
_hash=""
_data = None
#staticmethod
def genHash(data):
dataDict = DataCache.dictify(data)
datahash = json.dumps(dataDict, sort_keys=True)
return hashlib.sha256(datahash).digest()
#staticmethod
def dictify(data):
if isinstance(data,list):
datahash = []
for item in data:
datahash.append(DataCache.dictify(item))
elif isinstance(data,(dict, collections.OrderedDict)):
datahash = collections.OrderedDict()
for key,value in datahash.iteritems():
datahash[key]= DataCache.dictify(value)
elif isinstance(data, Serializable):
datahash = data.toDict()
else:
datahash = data
return datahash
def __init__(self, restoreDict = {}):
if restoreDict:
self.__dict__.update(restoreDict)
def __getinitargs__(self):
return (self.__dict__)
def set(self, data):
self._hash = DataCache.genHash(data)
self._data = data
def verify(self):
dataHash = DataCache.genHash(self._data)
return (self._hash == dataHash)
def get(self):
return self._data
Finally, I know there's arguments for using JSON for readability in storage, I needed Pickle's ability to convert straight to and from Objects without specifying the object type myself. (thanks to the nesting, it's not really feasible)
Am I going mad here or does pickling do something to the class definitions?
EDIT:
Minimal Implementation:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
from aenum import Enum
import json # _tricks
import base64
import argparse
import os
import sys
import datetime
import dill
import hashlib
import collections
class Serializable(object):
def __init__(self, initDict={}):
if initDict:
self.__dict__.update(initDict)
def __str__(self):
return str(self.sortSelf())
def sortSelf(self):
return collections.OrderedDict(sorted(self.__dict__.items()))
def toDict(self):
return self.__dict__
def fromDict(self, dict):
# Not using __dict__.update(...) to avoid polluting objects with the excess data
varMap = self.__dict__
if dict and varMap:
for key in varMap:
if (key in dict):
varMap[key] = dict[key]
self.__dict__.update(varMap)
return self
return None
class Issue(Serializable):
def __init__(self, initDict={}):
self.id = 0
self.key = ""
self.fields = {}
if initDict:
self.__dict__.update(initDict)
Serializable.__init__(self)
def fieldToDict(self, obj, key, type):
if key in obj:
result = obj[key]
else:
return None
if result is None:
return None
if isinstance(result, type):
return result.toDict()
return result
def fromDict(self, jsonDict):
super(Issue, self).fromDict(jsonDict)
self.fields["issuetype"] = IssueType().fromDict(self.fields["issuetype"])
self.fields["assignee"] = User().fromDict(self.fields["assignee"])
self.fields["creator"] = User().fromDict(self.fields["creator"])
self.fields["reporter"] = User().fromDict(self.fields["reporter"])
return self
def toDict(self):
result = super(Issue, self).toDict()
blankKeys = []
for fieldName, fieldValue in self.fields.iteritems():
if fieldValue is None:
blankKeys.append(fieldName)
if blankKeys:
for key in blankKeys:
self.fields.pop(key, None)
result["fields"]["issuetype"] = self.fieldToDict(result["fields"], "issuetype", IssueType)
result["fields"]["creator"] = self.fieldToDict(result["fields"], "creator", User)
result["fields"]["reporter"] = self.fieldToDict(result["fields"], "reporter", User)
result["fields"]["assignee"] = self.fieldToDict(result["fields"], "assignee", User)
return result
class IssueType(Serializable):
def __init__(self):
self.id = 0
self.name = ""
def toDict(self):
return {"id": str(self.id)}
class Project(Serializable):
def __init__(self):
Serializable.__init__(self)
self.id = 0
self.name = ""
self.key = ""
class Cycle(Serializable):
def __init__(self):
self.id = 0
self.name = ""
self.totalExecutions = 0
self.endDate = ""
self.description = ""
self.totalExecuted = 0
self.started = ""
self.versionName = ""
self.projectKey = ""
self.versionId = 0
self.environment = ""
self.totalCycleExecutions = 0
self.build = ""
self.ended = ""
self.name = ""
self.modifiedBy = ""
self.projectId = 0
self.startDate = ""
self.executionSummaries = {'executionSummary': []}
class Step(Serializable):
def __init__(self):
self.id = ""
self.orderId = 0
self.step = ""
self.data = ""
self.result = ""
self.attachmentsMap = {}
def toDict(self):
dict = {}
dict["step"] = self.step
dict["data"] = self.data
dict["result"] = self.result
dict["attachments"] = []
return dict
class Status(Serializable):
def __init__(self):
self.id = 0
self.name = ""
self.description = ""
self.isFinal = True
self.color = ""
self.isNative = True
self.statusCount = 0
self.statusPercent = 0.0
class User(Serializable):
def __init__(self):
self.displayName = ""
self.name = ""
self.emailAddress = ""
self.key = ""
self.active = False
self.timeZone = ""
class Execution(Serializable):
def __init__(self):
self.id = 0
self.orderId = 0
self.cycleId = -1
self.cycleName = ""
self.issueId = 0
self.issueKey = 0
self.projectKey = ""
self.comment = ""
self.versionId = 0,
self.versionName = "",
self.executedOn = ""
self.creationDate = ""
self.executedByUserName = ""
self.assigneeUserName = ""
self.status = {}
self.executionStatus = ""
def fromDict(self, jsonDict):
super(Execution, self).fromDict(jsonDict)
self.status = Status().fromDict(self.status)
# This is already listed as Execution Status, need to associate and convert!
return self
def toDict(self):
result = super(Execution, self).toDict()
result['status'] = result['status'].toDict()
return result
class ExecutionContainer(Serializable):
def __init__(self):
self.executions = []
def fromDict(self, jsonDict):
super(ExecutionContainer, self).fromDict(jsonDict)
self.executions = []
for executionDict in jsonDict["executions"]:
self.executions.append(Execution().fromDict(executionDict))
return self
class Test(Issue):
def __init__(self, initDict={}):
if initDict:
self.__dict__.update(initDict)
Issue.__init__(self)
def toDict(self):
result = super(Test, self).toDict()
stepField = "CustomField_0001"
if result["fields"][stepField]:
steps = []
for step in result["fields"][stepField]["steps"]:
steps.append(step.toDict())
result["fields"][stepField] = steps
return result
def fromDict(self, jsonDict):
super(Test, self).fromDict(jsonDict)
stepField = "CustomField_0001"
steps = []
if stepField in self.fields:
for step in self.fields[stepField]["steps"]:
steps.append(Step().fromDict(step))
self.fields[stepField] = {"steps": steps}
return self
class Set(Issue):
def __init__(self, initDict={}):
self.__dict__.update(initDict)
Issue.__init__(self)
class DataCache(object):
_hash = ""
_data = None
#staticmethod
def genHash(data):
dataDict = DataCache.dictify(data)
datahash = json.dumps(dataDict, sort_keys=True)
return hashlib.sha256(datahash).digest()
#staticmethod
def dictify(data):
if isinstance(data, list):
datahash = []
for item in data:
datahash.append(DataCache.dictify(item))
elif isinstance(data, (dict, collections.OrderedDict)):
datahash = collections.OrderedDict()
for key, value in datahash.iteritems():
datahash[key] = DataCache.dictify(value)
elif isinstance(data, Serializable):
datahash = data.toDict()
else:
datahash = data
return datahash
def __init__(self, restoreDict={}):
if restoreDict:
self.__dict__.update(restoreDict)
def __getinitargs__(self):
return (self.__dict__)
def set(self, data):
self._hash = DataCache.genHash(data)
self._data = data
def verify(self):
dataHash = DataCache.genHash(self._data)
return (self._hash == dataHash)
def get(self):
return self._data
def saveCache(name, projectKey, object):
filePath = "migration_caches/{projectKey}".format(projectKey=projectKey)
if not os.path.exists(path=filePath):
os.makedirs(filePath)
cache = DataCache()
cache.set(object)
targetFile = open("{path}/{name}".format(name=name, path=filePath), 'wb')
dill.dump(obj=cache, file=targetFile)
targetFile.close()
def loadCache(name, projectKey):
filePath = "migration_caches/{projectKey}/{name}".format(name=name, projectKey=projectKey)
result = False
try:
targetFile = open(filePath, 'rb')
try:
cache = dill.load(targetFile)
if isinstance(cache, DataCache):
if cache.verify():
result = cache.get()
except EOFError:
# except BaseException:
print ("Failed to load cache from file: {filePath}\n".format(filePath=filePath))
except IOError:
("Failed to load cache file at: {filePath}\n".format(filePath=filePath))
targetFile.close()
return result
testIssue = Test().fromDict({"id": 1000,
"key": "TEST",
"fields": {
"issuetype": {
"id": 1,
"name": "TestIssue"
},
"assignee": "Minothor",
"reporter": "Minothor",
"creator": "Minothor",
}
})
saveCache("Test", "TestProj", testIssue)
result = loadCache("Test", "TestProj")
EDIT 2
The script in it's current form, now seems to work correctly with vanilla Pickle, (initially switched to Dill due to a similar issue, which was solved by the switch).
However, if you are here with this issue and require Dill's features, then as Mike noted in the comments - it's possible to change the settings in dill.settings to have Dill behave pickle referenced items only with joblib mode, effectively mirroring pickle's standard pickling behaviour.
I'm trying to filter my flask-sqlalchemy query via strings. My procedure for creating the string results in the following array:
["user.name == 'Sid'", "user.role == 'admin'"]
It gets formed with something like this:
for i in myarray:
filter_string = "%s.%s == '%s'" % (self.model.__tablename__, i[0], i[2])
or_filters.append(filter_string)
Here is how I'm using it:
db = SQLAlchemy(app)
...
class myclass:
def __init(self, model):
self.model = model
self.q = db.session.query(self.model)
def get(self, cfg):
...
# the following line works
myfilter = [User.name == 'Sid', User.role == 'admin']
# the following line does not work, but I need it to. How to modify into the line above?
myfilter = ["user.name == 'Sid'", "user.role == 'admin'"]
if myfilter is not None:
self.q = self.apply_filter(myfilter)
items = self.q.all()
return items
def apply_filter(self, ftr):
self.q.filter(or_(*ftr))
Ilja had a good solution
myfilter = getattr(self.model, i[0]).is_(i[2])
if myfilter is not None:
self.q = self.apply_filter(myfilter)
I want to compress my movies automatically. So I've written a mediainfo wrapper class in python, to generate a xml output, which I then parse to a movieinfo class, with a list of audio and subtitle tracks.
__author__ = 'dominik'
class Error(Exception):
""" Error class
"""
class ValidationError(Error):
""" Invalid or missing xml items
"""
class MovieInfo(object):
""" Description of movie file
"""
def __init__(self, media_info):
self._video_track = None
self._audio_tracks = []
self._subtitle_tracks = []
self.valid_movie = True
for track in media_info.tracks:
if track.track_type == "Audio":
self._audio_tracks.append(AudioTrack(track))
elif track.track_type == "Text":
self._subtitle_tracks.append(SubtitleTrack(track))
elif track.track_type == "Video":
self._video_track = VideoTrack(track)
#property
def audio_tracks(self):
if not hasattr(self, "_audio_tracks"):
self._audio_tracks = []
if len(self._audio_tracks) != 0:
return self._audio_tracks
#property
def subtitle_tracks(self):
if not hasattr(self, "_subtitle_tracks"):
self._subtitle_tracks = []
if len(self._subtitle_tracks) != 0:
return self._subtitle_tracks
class Track(object):
""" Abstract track class for audio and subtitle tracks
"""
__KNOWN_LANGUAGE_CODES = {"en": "ENG", "de": "DE"}
def __init__(self, track, valid_codecs):
self._valid = True
track_id = int(track.id)
codec_id = self._determine_codec(track.codec_id, valid_codecs)
language = self._determine_language(track.language)
self._id = track_id
self._codec_id = codec_id
self._language = language
def _determine_codec(self, track_codec, types):
result = types.get(track_codec, None)
if result is None:
self._valid = False
return result
def _determine_language(self, track_language, types=__KNOWN_LANGUAGE_CODES):
result = types.get(track_language, None)
if result is None:
self._valid = False
return result
class AudioTrack(Track):
""" Audio track class
"""
__KNOWN_AUDIO_CODECS = {"A_DTS": "DTS", "A_AC3": "AC3"}
def __init__(self, track):
self._type = 1
Track.__init__(self, track, self.__KNOWN_AUDIO_CODECS)
class SubtitleTrack(Track):
""" Subtitle track class
"""
__KNOWN_SUBTITLE_CODECS = {"S_VOBSUB": "VOBSUB"}
def __init__(self, track):
self._type = 2
if track.forced == "Yes":
self._forced = True
else:
self._forced = False
Track.__init__(self, track, self.__KNOWN_SUBTITLE_CODECS)
class VideoTrack(object):
""" Video track class (only one video track in movie info!)
"""
def __init__(self, track):
self._type = 0
self._framerate = float(track.frame_rate)
self._width = track.width
self._height = track.height
Here is the mediainfo class (it's the pymediainfo class):
from subprocess import Popen
import os
from tempfile import mkstemp
from bs4 import BeautifulSoup, NavigableString
from setuptools.compat import unicode
class Track(object):
""" Hold the track information
"""
def __getattr__(self, item):
try:
return object.__getattribute__(self, item)
except:
pass
return None
def __init__(self, xml_track):
self.xml_track = xml_track
self.track_type = xml_track.attrs["type"]
for child in self.xml_track.children:
if not isinstance(child, NavigableString):
node_name = child.name.lower().strip()
node_value = unicode(child.string)
node_other_name = "other_%s" % node_name
if getattr(self, node_name) is None:
setattr(self, node_name, node_value)
else:
if getattr(self, node_other_name) is None:
setattr(self, node_other_name, [node_value, ])
else:
getattr(self, node_other_name).append(node_value)
for key in [c for c in self.__dict__.keys() if c.startswith("other_")]:
try:
primary = key.replace("other_", "")
setattr(self, primary, int(getattr(self, primary)))
except:
for value in getattr(self, key):
try:
actual = getattr(self, primary)
setattr(self, primary, int(value))
getattr(self, key).append(actual)
break
except:
pass
def __repr__(self):
return("<Track id='{0}', type='{1}'>".format(self.id, self.track_type))
def to_data(self):
data = {}
for k, v in self.__dict__.items():
if k != 'xml_track':
data[k] = v
return data
class Mediainfo(object):
""" MediaInfo wrapper
"""
def __init__(self, xml):
self.xml_dom = xml
if isinstance(xml, str):
self.xml_dom = BeautifulSoup(xml, "xml")
def _populate_tracks(self):
if self.xml_dom is None:
return
for xml_track in self.xml_dom.Mediainfo.File.find_all("track"):
self._tracks.append(Track(xml_track))
#property
def tracks(self):
if not hasattr(self, "_tracks"):
self._tracks = []
if len(self._tracks) == 0:
self._populate_tracks()
return self._tracks
#staticmethod
def parse(filename):
filehandler_out, filename_out = mkstemp(".xml", "mediainfo-")
filehandler_err, filename_err = mkstemp(".error", "mediainfo-")
filepointer_out = os.fdopen(filehandler_out, "r+b")
filepointer_err = os.fdopen(filehandler_err, "r+b")
mediainfo_command = ["mediainfo", "-f", "--Output=XML", filename]
p = Popen(mediainfo_command, stdout=filepointer_out, stderr=filepointer_err)
p.wait()
filepointer_out.seek(0)
xml_dom = BeautifulSoup(filepointer_out.read(), "xml")
filepointer_out.close()
filepointer_err.close()
print(xml_dom)
return Mediainfo(xml_dom)
def to_data(self):
data = {'tracks': []}
for track in self.tracks:
data['tracks'].append(track.to_data())
return data
This class gives me every track in the xml and then I parse the relevant info in movieinfo.
Ok now I have a list of audiotracks e.g. 3 tracks one in german language and DTS, one in german and AC3 and one in english and AC3. Now I want to get the ids from the tracks in the format "1,2,3" to give it to handbrake cli.
My problem is the order of the tracks. If there is a german DTS track this schould be the first track, the second track should be also the first, but compressed to aac and the third track should be one english track in AAC. If there is only a german AC3 track then the first track should be this track but compressed to AAC, and the second track should englisch and AAC.
I don't know exactly how I can achive that, can you help me? I'm new to python, and come from C, C++ and C#. In C# this is very easy to get with lambda.
Assuming you know to define a compare-tor that given two items can define which is bigger then Python functions as well as C or C++.
Start here -
1. https://wiki.python.org/moin/HowTo/Sorting/
https://developers.google.com/edu/python/sorting
http://docs.python.org/2/library/functions.html#sorted
Using sorted method and define the key you want.