@@ -253,21 +253,55 @@ <h1>Source code for pygenalgo.genome.chromosome</h1><div class="highlight"><pre>
253253 < span class ="c1 "> # _end_if_</ span >
254254
255255 < span class ="c1 "> # Compute the dissimilarities in their genomes.</ span >
256- < span class ="k "> return</ span > < span class ="nb "> sum</ span > < span class ="p "> ([</ span > < span class ="n "> k</ span > < span class ="o "> !=</ span > < span class ="n "> l</ span > < span class ="k "> for</ span > < span class ="n "> k</ span > < span class ="p "> ,</ span > < span class ="n "> l</ span > < span class ="ow "> in</ span > < span class ="nb "> zip</ span > < span class ="p "> (</ span > < span class ="bp "> self</ span > < span class ="o "> .</ span > < span class ="n "> _genome</ span > < span class ="p "> ,</ span > < span class ="n "> other</ span > < span class ="o "> .</ span > < span class ="n "> _genome</ span > < span class ="p "> ,</ span > < span class ="n "> strict</ span > < span class ="o "> =</ span > < span class ="kc "> True</ span > < span class ="p "> )])</ span >
256+ < span class ="k "> return</ span > < span class ="p "> [</ span > < span class ="n "> k</ span > < span class ="o "> !=</ span > < span class ="n "> l</ span > < span class ="k "> for</ span > < span class ="n "> k</ span > < span class ="p "> ,</ span > < span class ="n "> l</ span > < span class ="ow "> in</ span > < span class ="nb "> zip</ span > < span class ="p "> (</ span > < span class ="bp "> self</ span > < span class ="o "> .</ span > < span class ="n "> _genome</ span > < span class ="p "> ,</ span > < span class ="n "> other</ span > < span class ="o "> .</ span > < span class ="n "> _genome</ span > < span class ="p "> ,</ span >
257+ < span class ="n "> strict</ span > < span class ="o "> =</ span > < span class ="kc "> True</ span > < span class ="p "> )]</ span > < span class ="o "> .</ span > < span class ="n "> count</ span > < span class ="p "> (</ span > < span class ="kc "> True</ span > < span class ="p "> )</ span >
257258 < span class ="k "> else</ span > < span class ="p "> :</ span >
258259 < span class ="k "> raise</ span > < span class ="bp "> NotImplemented</ span > </ div >
259260
260261 < span class ="c1 "> # _end_if_</ span >
261262
262263 < span class ="c1 "> # _end_def_</ span >
263264
265+ < span class ="k "> def</ span > < span class ="nf "> __deepcopy__</ span > < span class ="p "> (</ span > < span class ="bp "> self</ span > < span class ="p "> ,</ span > < span class ="n "> memo</ span > < span class ="p "> ):</ span >
266+ < span class ="w "> </ span > < span class ="sd "> """</ span >
267+ < span class ="sd "> This custom method overrides the default deepcopy method</ span >
268+ < span class ="sd "> and is used when we call the "clone" method of the class.</ span >
269+
270+ < span class ="sd "> :param memo: dictionary of objects already copied during</ span >
271+ < span class ="sd "> the current copying pass.</ span >
272+
273+ < span class ="sd "> :return: a new identical "clone" of the self object.</ span >
274+ < span class ="sd "> """</ span >
275+ < span class ="c1 "> # Get the class of the object.</ span >
276+ < span class ="bp "> cls</ span > < span class ="o "> =</ span > < span class ="bp "> self</ span > < span class ="o "> .</ span > < span class ="vm "> __class__</ span >
277+
278+ < span class ="c1 "> # Create a new instance.</ span >
279+ < span class ="n "> new_object</ span > < span class ="o "> =</ span > < span class ="bp "> cls</ span > < span class ="o "> .</ span > < span class ="fm "> __new__</ span > < span class ="p "> (</ span > < span class ="bp "> cls</ span > < span class ="p "> )</ span >
280+
281+ < span class ="c1 "> # Don't copy self reference.</ span >
282+ < span class ="n "> memo</ span > < span class ="p "> [</ span > < span class ="nb "> id</ span > < span class ="p "> (</ span > < span class ="bp "> self</ span > < span class ="p "> )]</ span > < span class ="o "> =</ span > < span class ="n "> new_object</ span >
283+
284+ < span class ="c1 "> # Don't copy the cache.</ span >
285+ < span class ="k "> if</ span > < span class ="nb "> hasattr</ span > < span class ="p "> (</ span > < span class ="bp "> self</ span > < span class ="p "> ,</ span > < span class ="s2 "> "_cache"</ span > < span class ="p "> ):</ span >
286+ < span class ="n "> memo</ span > < span class ="p "> [</ span > < span class ="nb "> id</ span > < span class ="p "> (</ span > < span class ="bp "> self</ span > < span class ="o "> .</ span > < span class ="n "> _cache</ span > < span class ="p "> )]</ span > < span class ="o "> =</ span > < span class ="bp "> self</ span > < span class ="o "> .</ span > < span class ="n "> _cache</ span > < span class ="o "> .</ span > < span class ="fm "> __new__</ span > < span class ="p "> (</ span > < span class ="nb "> dict</ span > < span class ="p "> )</ span >
287+ < span class ="c1 "> # _end_if_</ span >
288+
289+ < span class ="c1 "> # Deep copy all other attributes.</ span >
290+ < span class ="k "> for</ span > < span class ="n "> key</ span > < span class ="p "> ,</ span > < span class ="n "> value</ span > < span class ="ow "> in</ span > < span class ="bp "> self</ span > < span class ="o "> .</ span > < span class ="vm "> __dict__</ span > < span class ="o "> .</ span > < span class ="n "> items</ span > < span class ="p "> ():</ span >
291+ < span class ="nb "> setattr</ span > < span class ="p "> (</ span > < span class ="n "> new_object</ span > < span class ="p "> ,</ span > < span class ="n "> key</ span > < span class ="p "> ,</ span > < span class ="n "> deepcopy</ span > < span class ="p "> (</ span > < span class ="n "> value</ span > < span class ="p "> ,</ span > < span class ="n "> memo</ span > < span class ="p "> ))</ span >
292+ < span class ="c1 "> # _end_for_</ span >
293+
294+ < span class ="c1 "> # Return identical instance.</ span >
295+ < span class ="k "> return</ span > < span class ="n "> new_object</ span >
296+ < span class ="c1 "> # _end_def_</ span >
297+
264298< div class ="viewcode-block " id ="Chromosome.clone ">
265299< a class ="viewcode-back " href ="../../../pygenalgo.genome.html#pygenalgo.genome.chromosome.Chromosome.clone "> [docs]</ a >
266300 < span class ="k "> def</ span > < span class ="nf "> clone</ span > < span class ="p "> (</ span > < span class ="bp "> self</ span > < span class ="p "> ):</ span >
267301< span class ="w "> </ span > < span class ="sd "> """</ span >
268302< span class ="sd "> Makes a duplicate of the self object.</ span >
269303
270- < span class ="sd "> :return: a ' ;deep-copy' ; of the object.</ span >
304+ < span class ="sd "> :return: a " ;deep-copy" ; of the object.</ span >
271305< span class ="sd "> """</ span >
272306 < span class ="k "> return</ span > < span class ="n "> deepcopy</ span > < span class ="p "> (</ span > < span class ="bp "> self</ span > < span class ="p "> )</ span > </ div >
273307</ div >
0 commit comments