def partition(self, arg, **kwargs): """Get a local partition object from either a Partion ORM object, or a partition name. :param arg: :param kwargs: Arguments: arg -- a orm.Partition or Partition object. """ from ambry.orm import Partition as OrmPartition from ambry.identity import PartitionNumber from identity import PartitionIdentity from sqlalchemy import or_ from partition import new_partition session = self.bundle.database.session orm_partition = None if isinstance(arg, OrmPartition): orm_partition = arg elif isinstance(arg, basestring): orm_query = session.query(OrmPartition).filter( or_(OrmPartition.id_ == arg, OrmPartition.vid == arg)) elif isinstance(arg, PartitionNumber): orm_query = session.query(OrmPartition).filter( OrmPartition.id_ == str(arg)) elif isinstance(arg, PartitionIdentity): orm_query = session.query(OrmPartition).filter( OrmPartition.id_ == str( arg.id_)) else: raise ValueError( "Arg must be a Partition or PartitionNumber. Got {}".format( type(arg))) if not orm_partition: orm_partition = orm_query.one() vid = orm_partition.vid if vid in self._partitions: return self._partitions[vid] else: p = new_partition(self.bundle, orm_partition, **kwargs) self._partitions[vid] = p return p
def partition(self, arg, **kwargs): """Get a local partition object from either a Partion ORM object, or a partition name. :param arg: :param kwargs: Arguments: arg -- a orm.Partition or Partition object. """ from ambry.orm import Partition as OrmPartition from ambry.identity import PartitionNumber from identity import PartitionIdentity from sqlalchemy import or_ from partition import new_partition session = self.bundle.database.session orm_partition = None if isinstance(arg, OrmPartition): orm_partition = arg elif isinstance(arg, basestring): orm_query = session.query(OrmPartition).filter( or_(OrmPartition.id_ == arg, OrmPartition.vid == arg)) elif isinstance(arg, PartitionNumber): orm_query = session.query(OrmPartition).filter( OrmPartition.id_ == str(arg)) elif isinstance(arg, PartitionIdentity): orm_query = session.query(OrmPartition).filter( OrmPartition.id_ == str(arg.id_)) else: raise ValueError( "Arg must be a Partition or PartitionNumber. Got {}".format( type(arg))) if not orm_partition: orm_partition = orm_query.one() vid = orm_partition.vid if vid in self._partitions: return self._partitions[vid] else: p = new_partition(self.bundle, orm_partition, **kwargs) self._partitions[vid] = p return p
def partition(self, arg, **kwargs): '''Get a local partition object from either a Partion ORM object, or a partition name Arguments: arg -- a orm.Partition or Partition object. ''' from databundles.orm import Partition as OrmPartition from databundles.identity import PartitionNumber from partition import PartitionIdentity from sqlalchemy import or_ from sqlalchemy.util._collections import KeyedTuple from partition import new_partition session = self.bundle.database.session if isinstance(arg,OrmPartition): orm_partition = arg elif isinstance(arg, basestring): orm_partition = session.query(OrmPartition).filter(or_(OrmPartition.id_==arg,OrmPartition.vid==arg)).one() elif isinstance(arg, PartitionNumber): orm_partition = session.query(OrmPartition).filter(OrmPartition.id_==str(arg) ).one() elif isinstance(arg, PartitionIdentity): orm_partition = session.query(OrmPartition).filter(OrmPartition.id_==str(arg.id_) ).one() else: raise ValueError("Arg must be a Partition or PartitionNumber. Got {}".format(type(arg))) return new_partition(self.bundle, orm_partition, **kwargs)