Best Python code snippet using fMBT_python
mtransaction.py
Source:mtransaction.py  
1from __future__ import absolute_import2############################################################################3#   Copyright (C) 2005 by Reithinger GmbH4#   mreithinger@web.de5#6#   This file is part of metapie.7#                                                                         8#   metapie is free software; you can redistribute it and/or modify9#   it under the terms of the GNU General Public License as published by10#   the Free Software Foundation; either version 2 of the License, or11#   (at your option) any later version.12#13#   pyplan is distributed in the hope that it will be useful,14#   but WITHOUT ANY WARRANTY; without even the implied warranty of15#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the16#   GNU General Public License for more details.17#18#   You should have received a copy of the GNU General Public License19#   along with this program; if not, write to the20#   Free Software Foundation, Inc.,21#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.22############################################################################23from builtins import filter24from builtins import str25from builtins import map26from builtins import object27from . import events28import weakref29import metapie30class ConstraintError(ValueError):31    def __init__(self, **kwargs):32        ValueError.__init__(self, "Model constraints are not satisfied")33        self.message = kwargs34class _SequenceProxy(object):35    def __init__(self, proxy, sequence):36        self.proxy = proxy37        self.sequence = sequence38        for im in proxy._removed_imodels.values():39            try:40                del sequence[im]41            except KeyError:42                pass43            44    def __len__(self):45        return len(self.sequence) + len(self.proxy._new_imodels)46    def __delitem__(self, index):47        imodel = self[index]48        del self.proxy[imodel]49        try:50            del self.sequence[imodel]51        except KeyError:52            pass53    def is_dead(self):54        return self.proxy._dead55    def insert(self, imodel):56        self.proxy.insert(imodel)57    def __getitem__(self, index):58        if index >= len(self.sequence):59            return self.proxy._new_imodels[index - len(self.sequence)]60        imodel = self.sequence[index]61        self.proxy.mark(imodel)62        return imodel63class Transaction(events.Subject):64    active_transactions = { }65    class ContainerProxy(object):66        def __init__(self, src):67            self._src = src68            self._dead = False69            self._removed_imodels = {}70            self._new_imodels = []71            self._transaction = Transaction.get_transaction(src._imodel)72            try:73                keys = src._keys74            except AttributeError:75                return76            for k in list(keys.keys()):77                setattr(self, k, getattr(src, k))78        def mark(self, imodel):79            Transaction.mark_imodel(imodel, self._transaction)80        def commit(self):81            self._dead = True82            if not self._removed_imodels and not self._new_imodels: return83            for v in self._removed_imodels.values():84                self._src.delete(v, False)85            for v in self._new_imodels:86                self._src.insert(v, False)87        def rollback(self):88            self._dead = True89        def insert(self, imodel):90            self._src.check_peer(imodel)91            92            if imodel.id() in self._removed_imodels:93                del self._removed_imodels[imodel.id()]94                self._src._add_to_peer(imodel, self._src._imodel)95                self._src.fire()96            elif (imodel not in self._src \97                  and id(imodel) not in list(map(id, self._new_imodels))):98                99                self.mark(imodel)100                self._new_imodels.append(imodel)101                self._src._add_to_peer(imodel, self._src._imodel)102                self._src.fire()103        def delete(self, imodel):104            def exclude_model():105                try:106                    proxy = self._transaction.active_imodels[imodel]107                except KeyError:108                    pass109                else:110                    proxy.rollback(imodel)111                    del self._transaction.active_imodels[imodel]112            if imodel.id() in self._removed_imodels:113                raise KeyError("model does not exist in container", imodel)114            if imodel in self._src:115                exclude_model()116                self._removed_imodels[imodel.id()] = imodel117                self._src._remove_from_peer(imodel, self._src._imodel)118                self._src.fire()119            else:120                try:121                    index = list(map(id, self._new_imodels)).index(id(imodel))122                except ValueError:123                    raise KeyError("model does not exist in container", imodel)124                else:125                    exclude_model()126                    del self._new_imodels[index]127                    self._src.fire()128        def recatalog(self, obj):129            return self._src.recatalog(obj)130        def __delitem__(self, imodel):131            self.delete(imodel)132            133        def __len__(self):134            return len(self._src) \135                   - len(self._removed_imodels) \136                   + len(self._new_imodels)137        138        def __iter__(self):139            return iter(_SequenceProxy(self, self._src.sequence()))140        def subset(self, idset):141            return _SequenceProxy(self, self._src.subset(idset))142        def sequence(self):143            return _SequenceProxy(self, self._src.sequence())144        145        def __contains__(self, imodel):146            id_ = imodel.id()147            if id_ in self._removed_imodels: return False148            return imodel in self._src or imodel in self._new_imodels149        def __getitem__(self, key):150            if key in self._removed_imodels:151                raise KeyError("key does not exist", key)152            try:153                return self._src[key]154            except KeyError:155                for m in self._new_imodels:156                    if m.id() == key: return m157            raise KeyError158    class ModelProxy(object):159        def __init__(self, transaction):160            self._transaction = weakref.ref(transaction)161            self.error = None162        def remove_error(self, attrib):163            try:164                del self.error.message[attrib]165                if not self.error.message: self.error = None166            except AttributeError:167                pass168            except KeyError:169                pass170                171        def rollback(self, imodel):172            for t in imodel.__attributes_map__.values():173                if hasattr(self, t.private_name):174                    t._rollback_value(imodel, self)175                    delattr(self, t.private_name)176                    imodel.fire(t.name, t.name)177            self.error = None178        def commit(self, imodel):179            self.error = None180            181            for t in imodel.__attributes_map__.values():182                try:183                    pname = t.private_name184                except AttributeError:185                    #readonly attributes don't have a private_name186                    return187                188                if hasattr(self, pname):189                    t._commit_value(imodel, self)190                    delattr(self, pname)191            imodel.reindex()192                193    def remove_proxy(cls, imodel):194        try:195            transaction = cls.get_transaction(imodel)196        except RuntimeError:197            return198        del transaction.active_transactions[imodel]199        try:200            del transaction.active_imodels[imodel]201        except KeyError: pass202    remove_proxy = classmethod(remove_proxy)203    def make_proxy(cls, imodel):204        #proxy or transaction205        206        p_or_t = cls.active_transactions.get(imodel)207        try:208            if not p_or_t: return imodel209        except ReferenceError:210            del cls.active_transactions[imodel]211            return imodel212        213        if isinstance(p_or_t, cls.ModelProxy):214            #it is a proxy215            return p_or_t216        #it is a transaction (weakproxy)217        return p_or_t.include(imodel)218        219        220    make_proxy = classmethod(make_proxy)221    def get_proxy(cls, imodel):222        try:223            p_or_t = cls.active_transactions.get(imodel)224            p_or_t.__class__225            226            if isinstance(p_or_t, cls.ModelProxy):227                return p_or_t228        except ReferenceError:229            del cls.active_transactions[imodel]230        return imodel231        232    get_proxy = classmethod(get_proxy)233    def get_transaction(cls, imodel):234        try:235            p_or_t = cls.active_transactions.get(imodel)236            return weakref.proxy(p_or_t._transaction())237        except ReferenceError:238            raise RuntimeError("no active transaction")239        except AttributeError:240            raise RuntimeError("no active transaction")241    242    243    get_transaction = classmethod(get_transaction)244    def mark_imodel(cls, imodel, transaction):245        assert(type(transaction) is weakref.ProxyType)246        try:247            proxy = cls.active_transactions.setdefault(imodel, transaction)248            proxy.__class__ #causes a ReferenceError if it is a broken reference249        except ReferenceError:250            cls.active_transactions[imodel] = transaction251    mark_imodel = classmethod(mark_imodel)252    253    def __init__(self):254        self.active_imodels = { }255    def __del__(self):256        self.destroy()257        258    def include(self, imodel):259        proxy = self.active_transactions.get(imodel, self)260        if proxy._transaction() is not self:261            raise RuntimeError("the object '%s' belongs "\262                               "already to a transaction" % str(imodel))263        if isinstance(proxy, self.ModelProxy):264            #allready included265            return266            267        proxy = self.ModelProxy(self)268        self.active_transactions[imodel] = proxy269        self.active_imodels[imodel] = proxy270        return proxy271        272    def _transaction(self):273        return self274    275    def commit(self):276        #phase one try commit277        commit_ok = True278        for imodel, proxy in self.active_imodels.items():279            try:280                imodel.check_constraints()281            except ConstraintError as e:282                proxy.error = e283                commit_ok = False284            else:285                proxy.error = None286                287        if commit_ok:288            for imodel, proxy in self.active_imodels.items():289                proxy.commit(imodel)290            self.fire("commit", True)291            metapie._dbcommit()292        else:293            self.fire("commit", False)294        return commit_ok295    def get_errors(self):296        result = {}297        for imodel, proxy in self.active_imodels.items():298            try:299                result[imodel] = proxy.error300            except AttributeError:301                pass302        return result303        304    def rollback(self):305        for imodel, proxy in self.active_imodels.items():306            proxy.rollback(imodel)307        self.fire("rollback")308    def destroy(self):309        def is_my_item(item):310            imodel, p_or_t = item311            try:312                t = p_or_t._transaction()313                return t is self or not t314            except ReferenceError:315                return True316        my_models = list(filter(is_my_item, iter(self.active_transactions.items())))317        for k, v in my_models:318            del self.active_transactions[k]319        ...dbtransient.py
Source:dbtransient.py  
1############################################################################2#   Copyright (C) 2005 by Reithinger GmbH3#   mreithinger@web.de4#5#   This file is part of metapie.6#                                                                         7#   metapie is free software; you can redistribute it and/or modify8#   it under the terms of the GNU General Public License as published by9#   the Free Software Foundation; either version 2 of the License, or10#   (at your option) any later version.11#12#   pyplan is distributed in the hope that it will be useful,13#   but WITHOUT ANY WARRANTY; without even the implied warranty of14#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the15#   GNU General Public License for more details.16#17#   You should have received a copy of the GNU General Public License18#   along with this program; if not, write to the19#   Free Software Foundation, Inc.,20#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.21############################################################################22"""23Objects that will not be persistent24"""25from __future__ import absolute_import26from builtins import str27from builtins import object28import metapie29from . import peer30import bisect31class _ResultSet(object):32    """Lazily accessed set of objects."""33    def __init__(self, uids, uidutil):34        self.uids = uids35        self.uidutil = uidutil36    def __len__(self):37        return len(self.uids)38    def __delitem__(self, imodel):39        try:40            index = self.uids.index(imodel.id())41        except ValueError:42            raise KeyError("model does not exist in container", imodel)43        else:44            del self.uids[index]45        46    def __getitem__(self, index):47        return self.uidutil[self.uids[index]]48    49class FieldIndex(object):50    def __init__(self):51        self.index = []52    def index_id(self, docid, value):53        bisect.insort_right(self.index, (value, docid))54    def unindex_id(self, docid):55        pos = 056        while pos < len(self.index):57            if self.index[pos][1] == docid:58                del self.index[pos]59            else:60                pos += 161    def apply(self, min, max=None, excludemin=False, excludemax=False):62        pos = bisect.bisect_right(self.index, (min, 0))63        if excludemin:64            while pos < len(self.index):65                value, docid = self.index[pos]66                if value != min: break67                pos += 168        def all_keys():69            while pos < len(self.index):70                value, docid = self.index[pos]71                if max:72                    if excludemax and value == max: break73                    if value > max: break74                yield docid75                pos += 176        return list(all_keys())77class Container(peer.Peer):78    def __init__(self, imodel, peer_class, name_to_me, name_to_peer, index):79        self._container = {}80        self._imodel = imodel81        self._peer_class = peer_class82        self._name_to_me = name_to_me83        self._name_to_peer = name_to_peer84        85    def check_peer(self, imodel):86        if not isinstance(imodel, self._peer_class):87            raise ValueError("'%s' is not of type '%s'"%88                             (str(imodel), self.contained_class.__name__))89    def fire(self):90        self._imodel.fire(self._name_to_peer, self._name_to_peer)91        self._imodel.fire("default", self._name_to_peer)92        93    def insert(self, imodel, fire=True):94        self.check_peer(imodel)95        if self._insert_item(imodel):96            self._add_to_peer(imodel, self._imodel)97            if fire: self.fire()98    def delete(self, imodel, fire=True):99        if self._del_item(imodel):100            self._remove_from_peer(imodel, self._imodel)101            if fire: self.fire()102        else:103            raise KeyError("model does not exist in container", imodel)104    def recatalog(self, obj):105        return False106    def sequence(self):107        keys = list(self._container.keys())108        keys.sort()109        return _ResultSet(keys, self._container)110    def __bool__(self): return len(self._container) > 0111    def __len__(self): return len(self._container)112    def __iter__(self): return iter(self._container.values())113    def __contains__(self, imodel): return imodel.id() in self._container114    def __getitem__(self, key): return self._container[key]115    def __delitem__(self, imodel): self.delete(imodel)116    def _insert_item(self, imodel):117        if imodel.id() not in self._container:118            self._container[imodel.id()] = imodel119            return True120        121        return False122    def _del_item(self, imodel):123        try:124            del self._container[imodel.id()]125        except KeyError:126            return False127        return True128class IdGenerator(object):129    instance = None130    def get_id(cls, type="default"):131        if not cls.instance: cls.instance = cls()132        counter = getattr(cls.instance, type, 0)133        setattr(cls.instance, type, counter + 1)134        return counter135    get_id = classmethod(get_id)136    137class _PersistentBase(object):138    __id_type__ = None139    140    def __init__(self):141        #generate a unique long lasting id142        self.__id = IdGenerator.get_id(self.__id_type__ \143                                       or self.__class__.__name__)144        #self.__id = int(time.time()) % 100000 + (id(self) % 10000) * 100000145    146    def id(self): return self.__id147    148def _commitdumy(): pass149metapie._init_db_module("transient", _PersistentBase, Container, _commitdumy)...train.py
Source:train.py  
1import tensorflow as tf2import keras3from keras.models import Sequential4from keras.layers import Dense, MaxPool2D, AveragePooling2D5from keras.layers import Conv2D, Activation, Dropout, Flatten6from keras.preprocessing.image import ImageDataGenerator7import numpy as np8import matplotlib.pyplot as plt9# cpu configuration10config = tf.ConfigProto( device_count = {'GPU': 1 , 'CPU': 56} ) #max: 1 gpu, 56 cpu11sess = tf.Session(config=config)12keras.backend.set_session(sess)13#14num_classes = 7 # sad, happy, surprise, angry, fear, nutral, disgust15batch_size = 25616epoch = 4417with open("fer2013.csv") as f:18    content = f.readlines()19lines = np.array(content)20num_instance =lines.size21# train test22x_train, y_train, x_test, y_test = [], [], [], []23for i in range(1,num_instance):24    try:25        emotion, img, usage = lines[i].split(",")26        val = img.split(" ")27        pixels = np.array(val, 'float32')28        emotion = keras.utils.to_categorical(emotion, num_classes)29        if 'Training' in usage:30            y_train.append(emotion)31            x_train.append(pixels)32        elif 'PublicTest' in usage:33            y_test.append(emotion)34            x_test.append(pixels)35    except:36        print("", end="")37# data transformation for train and test sets38x_train = np.array(x_train, 'float32')39y_train = np.array(y_train, 'float32')40x_test = np.array(x_test, 'float32')41y_test = np.array(y_test, 'float32')42x_train /= 255 #normalize inputs between [0, 1]43x_test /= 25544x_train = x_train.reshape(x_train.shape[0], 48, 48, 1)45x_train = x_train.astype('float32')46x_test = x_test.reshape(x_test.shape[0], 48, 48, 1)47x_test = x_test.astype('float32')48print(x_train.shape[0], 'train samples')49print(x_test.shape[0], 'test samples')50# creating the sequential model51imodel_fe = Sequential()52#1st convolution layer53imodel_fe.add(Conv2D(64, (5, 5), activation='relu', input_shape=(48,48,1)))54imodel_fe.add(MaxPool2D(pool_size=(5, 5), strides=(2, 2)))55#2nd c layer56imodel_fe.add(Conv2D(64, (3, 3), activation='relu'))#12857imodel_fe.add(Conv2D(64, (3,3), activation='relu'))58imodel_fe.add(AveragePooling2D(pool_size=(3, 3),strides=(2, 2)))59#3rd c layer60imodel_fe.add(Conv2D(64, (3, 3), activation='relu'))#12861imodel_fe.add(Conv2D(64, (3,3), activation='relu'))62imodel_fe.add(AveragePooling2D(pool_size=(3, 3),strides=(2, 2)))63imodel_fe.add(Flatten())64# fully connected neural network65imodel_fe.add(Dense(1024, activation='relu'))66imodel_fe.add(Dropout(0.2))67imodel_fe.add(Dense(1024, activation='relu'))68imodel_fe.add(Dropout(0.2))69imodel_fe.add(Dense(num_classes, activation='softmax'))70#--------------------------------------71#batch process72gen = ImageDataGenerator()# this is used to normalise images in format keras can understand73train_generator = gen.flow(x_train, y_train, batch_size=batch_size)74imodel_fe.compile(loss='categorical_crossentropy'75    , optimizer=keras.optimizers.Adam()76    , metrics=['accuracy']77)78#---------------------------------------------79fit = True80if fit == True:81	#model.fit_generator(x_train, y_train, epochs=epochs) #train for all trainset82	imodel_fe.fit_generator(train_generator, steps_per_epoch=batch_size, epochs=epoch) #train for randomly selected one83else:84	imodel_fe.load_weights('model33.h5') #load weights85# ------------------------------86# function for drawing bar chart for emotion preditions87def emotion_analysis(emotions):88    objects = ('angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral')89    y_pos = np.arange(len(objects))90    plt.bar(y_pos, emotions, align='center', alpha=0.5)91    plt.xticks(y_pos, objects)92    plt.ylabel('percentage')93    plt.title('emotion')94    plt.show()95# ------------------------------96imodel_fe.save('model2.h5')97#--------------------------------98monitor_testset_results = True99if monitor_testset_results == True:100    # make predictions for test set101    predictions = imodel_fe.predict(x_test)102    index = 0103    for i in predictions:104        if index < 30 and index >= 20:105            # print(i) #predicted scores106            # print(y_test[index]) #actual scores107            testing_img = np.array(x_test[index], 'float32')108            testing_img = testing_img.reshape([48, 48]);109            plt.gray()110            plt.imshow(testing_img)111            plt.show()112            print(i)113            emotion_analysis(i)114            print("----------------------------------------------")115        index = index + 1116#------------------------------------------------------------------------------117from keras.models import load_model...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
