code: def testOptimalModularity(self):
      try:
          g = Graph.Famous("bull")
  
          cl = g.community_optimal_modularity()
          self.assertTrue(len(cl) == 2)
          self.assertMembershipsEqual(cl, [0, 0, 1, 0, 1])
          self.assertAlmostEqual(cl.q, 0.08, places=7)
  
          ws = [i % 5 for i in range(g.ecount())]
          cl = g.community_optimal_modularity(weights=ws)
          self.assertAlmostEqual(
              cl.q, g.modularity(cl.membership, weights=ws), places=7
          )
  
          g = Graph.Famous("zachary")
          cl = g.community_optimal_modularity()
          self.assertTrue(len(cl) == 4)
          self.assertMembershipsEqual(
              cl,
              [
                  0,
                  0,
                  0,
                  0,
                  1,
                  1,
                  1,
                  0,
                  2,
                  2,
                  1,
                  0,
                  0,
                  0,
                  2,
                  2,
                  1,
                  0,
                  2,
                  0,
                  2,
                  0,
                  2,
                  3,
                  3,
                  3,
                  2,
                  3,
                  3,
                  2,
                  2,
                  3,
                  2,
                  2,
              ],
          )
          self.assertAlmostEqual(cl.q, 0.4197896, places=7)
  
          ws = [2 + (i % 3) for i in range(g.ecount())]
          cl = g.community_optimal_modularity(weights=ws)
          self.assertAlmostEqual(
              cl.q, g.modularity(cl.membership, weights=ws), places=7
          )
  
      except NotImplementedError:
          # Well, meh
          pass
  
imports: import math
  import random
  import unittest
  from igraph import Clustering, CohesiveBlocks, Cover, Graph, Histogram, InternalError, UniqueIdGenerator, VertexClustering, compare_communities, split_join_distance, set_random_number_generator
label_desc: Use image node_id 9 for calling the CommunityTests obj's underlying member method code with example usage: obj.testOptimalModularity() without return types