Best Python code snippet using fMBT_python
namespace_range.py
Source:namespace_range.py  
...54    _LEX_DISTANCE.append(55        _LEX_DISTANCE[i-1] * len(NAMESPACE_CHARACTERS) + 1)56  del i57_setup_constants()58def _ord_to_namespace(n, _max_length=None):59  """Convert a namespace ordinal to a namespace string.60  Converts an int, representing the sequence number of a namespace ordered61  lexographically, into a namespace string.62  >>> _ord_to_namespace(0)63  ''64  >>> _ord_to_namespace(1)65  '-'66  >>> _ord_to_namespace(2)67  '--'68  >>> _ord_to_namespace(3)69  '---'70  Args:71    n: A number representing the lexographical ordering of a namespace.72    _max_length: The maximum namespace length.73  Returns:74    A string representing the nth namespace in lexographical order.75  """76  if _max_length is None:77    _max_length = MAX_NAMESPACE_LENGTH78  length = _LEX_DISTANCE[_max_length - 1]79  if n == 0:80    return ''81  n -= 182  return (NAMESPACE_CHARACTERS[n / length] +83          _ord_to_namespace(n % length, _max_length - 1))84def _namespace_to_ord(namespace):85  """Converts a namespace string into an int representing its lexographic order.86  >>> _namespace_to_ord('')87  ''88  >>> _namespace_to_ord('_')89  190  >>> _namespace_to_ord('__')91  292  Args:93    namespace: A namespace string.94  Returns:95    An int representing the lexographical order of the given namespace string.96  """97  n = 098  for i, c in enumerate(namespace):99    n += (_LEX_DISTANCE[MAX_NAMESPACE_LENGTH - i- 1] *100          NAMESPACE_CHARACTERS.index(c)101          + 1)102  return n103def _key_for_namespace(namespace, app):104  """Return the __namespace__ key for a namespace.105  Args:106    namespace: The namespace whose key is requested.107    app: The id of the application that the key belongs to.108  Returns:109    A db.Key representing the namespace.110  """111  if namespace:112    return db.Key.from_path(metadata.Namespace.KIND_NAME,113                            namespace,114                            _app=app)115  else:116    return db.Key.from_path(metadata.Namespace.KIND_NAME,117                            metadata.Namespace.EMPTY_NAMESPACE_ID,118                            _app=app)119class NamespaceRange(object):120  """An inclusive lexographical range of namespaces.121  This class is immutable.122  """123  def __init__(self,124               namespace_start=None,125               namespace_end=None,126               _app=None):127    """Initializes a NamespaceRange instance.128    Args:129      namespace_start: A string representing the start of the namespace range.130          namespace_start is included in the range. If namespace_start is None131          then the lexographically first namespace is used.132      namespace_end: A string representing the end of the namespace range.133          namespace_end is included in the range and must be >= namespace_start.134          If namespace_end is None then the lexographically last namespace is135          used.136    Raises:137      ValueError: if namespace_start > namespace_end.138    """139    if namespace_start is None:140      namespace_start = MIN_NAMESPACE141    if namespace_end is None:142      namespace_end = MAX_NAMESPACE143    if namespace_start > namespace_end:144      raise ValueError('namespace_start (%r) > namespace_end (%r)' % (145          namespace_start, namespace_end))146    self.__namespace_start = namespace_start147    self.__namespace_end = namespace_end148    self.__app = _app149  @property150  def app(self):151    return self.__app152  @property153  def namespace_start(self):154    return self.__namespace_start155  @property156  def namespace_end(self):157    return self.__namespace_end158  @property159  def is_single_namespace(self):160    """True if the namespace range only includes a single namespace."""161    return self.namespace_start == self.namespace_end162  def split_range(self):163    """Splits the NamespaceRange into two nearly equal-sized ranges.164    Returns:165      If this NamespaceRange contains a single namespace then a list containing166      this NamespaceRange is returned. Otherwise a two-element list containing167      two NamespaceRanges whose total range is identical to this168      NamespaceRange's is returned.169    """170    if self.is_single_namespace:171      return [self]172    mid_point = (_namespace_to_ord(self.namespace_start) +173                 _namespace_to_ord(self.namespace_end)) // 2174    return [NamespaceRange(self.namespace_start,175                           _ord_to_namespace(mid_point),176                           _app=self.app),177            NamespaceRange(_ord_to_namespace(mid_point+1),178                           self.namespace_end,179                           _app=self.app)]180  def __copy__(self):181    return self.__class__(self.__namespace_start,182                          self.__namespace_end,183                          self.__app)184  def __eq__(self, o):185    return (self.namespace_start == o.namespace_start and186            self.namespace_end == o.namespace_end)187  def __hash__(self):188    return hash((self.namespace_start, self.namespace_end, self.app))189  def __repr__(self):190    if self.app is None:191      return 'NamespaceRange(namespace_start=%r, namespace_end=%r)' % (192          self.namespace_start, self.namespace_end)193    else:194      return 'NamespaceRange(namespace_start=%r, namespace_end=%r, _app=%r)' % (195          self.namespace_start, self.namespace_end, self.app)196  def with_start_after(self, after_namespace):197    """Returns a copy of this NamespaceName with a new namespace_start.198    Args:199      after_namespace: A namespace string.200    Returns:201      A NamespaceRange object whose namespace_start is the lexographically next202      namespace after the given namespace string.203    Raises:204      ValueError: if the NamespaceRange includes only a single namespace.205    """206    namespace_start = _ord_to_namespace(_namespace_to_ord(after_namespace) + 1)207    return NamespaceRange(namespace_start, self.namespace_end, _app=self.app)208  def make_datastore_query(self, cursor=None):209    """Returns a datastore.Query that generates all namespaces in the range.210    Args:211      cursor: start cursor for the query.212    Returns:213      A datastore.Query instance that generates db.Keys for each namespace in214      the NamespaceRange.215    """216    filters = {}217    filters['__key__ >= '] = _key_for_namespace(218        self.namespace_start, self.app)219    filters['__key__ <= '] = _key_for_namespace(220        self.namespace_end, self.app)221    return datastore.Query('__namespace__',222                           filters=filters,223                           keys_only=True,224                           cursor=cursor,225                           _app=self.app)226  def normalized_start(self):227    """Returns a NamespaceRange with leading non-existant namespaces removed.228    Returns:229      A copy of this NamespaceRange whose namespace_start is adjusted to exclude230      the portion of the range that contains no actual namespaces in the231      datastore. None is returned if the NamespaceRange contains no actual232      namespaces in the datastore.233    """234    namespaces_after_key = list(self.make_datastore_query().Run(limit=1))235    if not namespaces_after_key:236      return None237    namespace_after_key = namespaces_after_key[0].name() or ''238    return NamespaceRange(namespace_after_key,239                          self.namespace_end,240                          _app=self.app)241  def to_json_object(self):242    """Returns a dict representation that can be serialized to JSON."""243    obj_dict = dict(namespace_start=self.namespace_start,244                    namespace_end=self.namespace_end)245    if self.app is not None:246      obj_dict['app'] = self.app247    return obj_dict248  @classmethod249  def from_json_object(cls, json):250    """Returns a NamespaceRange from an object deserialized from JSON."""251    return cls(json['namespace_start'],252               json['namespace_end'],253               _app=json.get('app'))254  @classmethod255  def split(cls,256            n,257            contiguous,258            can_query=itertools.chain(itertools.repeat(True, 50),259                                      itertools.repeat(False)).next,260            _app=None):261    """Splits the complete NamespaceRange into n equally-sized NamespaceRanges.262    Args:263      n: The maximum number of NamespaceRanges to return. Fewer than n264          namespaces may be returned.265      contiguous: If True then the returned NamespaceRanges will cover the266          entire space of possible namespaces (i.e. from MIN_NAMESPACE to267          MAX_NAMESPACE) without gaps. If False then the returned268          NamespaceRanges may exclude namespaces that don't appear in the269          datastore.270      can_query: A function that returns True if split() can query the datastore271          to generate more fair namespace range splits, and False otherwise.272          If not set then split() is allowed to make 50 datastore queries.273    Returns:274      A list of at most n NamespaceRanges representing a near-equal distribution275      of actual existant datastore namespaces. The returned list will be sorted276      lexographically.277    Raises:278      ValueError: if n is < 1.279    """280    if n < 1:281      raise ValueError('n must be >= 1')282    ranges = None283    if can_query():284      if not contiguous:285        ns_keys = get_namespace_keys(_app, n + 1)286        if not ns_keys:287          return []288        else:289          if len(ns_keys) <= n:290            ns_range = []291            for ns_key in ns_keys:292              ns_range.append(NamespaceRange(ns_key.name() or '',293                                             ns_key.name() or '',294                                             _app=_app))295            return sorted(ns_range,296                          key=lambda ns_range: ns_range.namespace_start)297          ranges = [NamespaceRange(ns_keys[0].name() or '', _app=_app)]298      else:299        ns_range = NamespaceRange(_app=_app).normalized_start()300        if ns_range is None:301          return [NamespaceRange(_app=_app)]302        ranges = [ns_range]303    else:304      ranges = [NamespaceRange(_app=_app)]305    singles = []306    while ranges and (len(ranges) + len(singles)) < n:307      namespace_range = ranges.pop(0)308      if namespace_range.is_single_namespace:309        singles.append(namespace_range)310      else:311        left, right = namespace_range.split_range()312        if can_query():313          right = right.normalized_start()314        if right is not None:315          ranges.append(right)316        ranges.append(left)317    ns_ranges = sorted(singles + ranges,318                       key=lambda ns_range: ns_range.namespace_start)319    if contiguous:320      if not ns_ranges:321        return [NamespaceRange(_app=_app)]322      continuous_ns_ranges = []323      for i in range(len(ns_ranges)):324        if i == 0:325          namespace_start = MIN_NAMESPACE326        else:327          namespace_start = ns_ranges[i].namespace_start328        if i == len(ns_ranges) - 1:329          namespace_end = MAX_NAMESPACE330        else:331          namespace_end = _ord_to_namespace(332              _namespace_to_ord(ns_ranges[i+1].namespace_start) - 1)333        continuous_ns_ranges.append(NamespaceRange(namespace_start,334                                                   namespace_end,335                                                   _app=_app))336      return continuous_ns_ranges337    else:338      return ns_ranges339  def __iter__(self):340    """Iterate over all the namespaces within this range."""341    cursor = None342    while True:343      query = self.make_datastore_query(cursor=cursor)344      count = 0345      for ns_key in query.Run(limit=NAMESPACE_BATCH_SIZE):...cpp_type_generator.py
Source:cpp_type_generator.py  
1# Copyright (c) 2012 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4from code import Code5from model import PropertyType6import any_helper7import cpp_util8class CppTypeGenerator(object):9  """Manages the types of properties and provides utilities for getting the10  C++ type out of a model.Property11  """12  def __init__(self, root_namespace, namespace=None, cpp_namespace=None):13    """Creates a cpp_type_generator. The given root_namespace should be of the14    format extensions::api::sub. The generator will generate code suitable for15    use in the given namespace.16    """17    self._type_namespaces = {}18    self._root_namespace = root_namespace.split('::')19    self._cpp_namespaces = {}20    if namespace and cpp_namespace:21      self._namespace = namespace22      self.AddNamespace(namespace, cpp_namespace)23  def AddNamespace(self, namespace, cpp_namespace):24    """Maps a model.Namespace to its C++ namespace name. All mappings are25    beneath the root namespace.26    """27    for type_ in namespace.types:28      qualified_name = self._QualifyName(namespace, type_)29      if qualified_name in self._type_namespaces:30        raise ValueError('Type %s is declared in both %s and %s' %31            (qualified_name, namespace.name,32             self._type_namespaces[qualified_name].name))33      self._type_namespaces[qualified_name] = namespace34    self._cpp_namespaces[namespace] = cpp_namespace35  def GetExpandedChoicesInParams(self, params):36    """Returns the given parameters with PropertyType.CHOICES parameters37    expanded so that each choice is a separate parameter and sets a unix_name38    for each choice.39    """40    expanded = []41    for param in params:42      if param.type_ == PropertyType.CHOICES:43        for choice in param.choices.values():44          choice.unix_name = (45              param.unix_name + '_' + choice.type_.name.lower())46          expanded.append(choice)47      else:48        expanded.append(param)49    return expanded50  def GetCppNamespaceName(self, namespace):51    """Gets the mapped C++ namespace name for the given namespace relative to52    the root namespace.53    """54    return self._cpp_namespaces[namespace]55  def GetRootNamespaceStart(self):56    """Get opening root namespace declarations.57    """58    c = Code()59    for namespace in self._root_namespace:60      c.Append('namespace %s {' % namespace)61    return c62  def GetRootNamespaceEnd(self):63    """Get closing root namespace declarations.64    """65    c = Code()66    for namespace in reversed(self._root_namespace):67      c.Append('}  // %s' % namespace)68    return c69  def GetNamespaceStart(self):70    """Get opening self._namespace namespace declaration.71    """72    return Code().Append('namespace %s {' %73        self.GetCppNamespaceName(self._namespace))74  def GetNamespaceEnd(self):75    """Get closing self._namespace namespace declaration.76    """77    return Code().Append('}  // %s' %78        self.GetCppNamespaceName(self._namespace))79  def GetEnumNoneValue(self, prop):80    """Gets the enum value in the given model.Property indicating no value has81    been set.82    """83    return '%s_NONE' % prop.unix_name.upper()84  def GetEnumValue(self, prop, enum_value):85    """Gets the enum value of the given model.Property of the given type.86    e.g VAR_STRING87    """88    return '%s_%s' % (89        prop.unix_name.upper(), cpp_util.Classname(enum_value.upper()))90  def GetChoicesEnumType(self, prop):91    """Gets the type of the enum for the given model.Property.92    e.g VarType93    """94    return cpp_util.Classname(prop.name) + 'Type'95  def GetType(self, prop, pad_for_generics=False, wrap_optional=False):96    """Translates a model.Property into its C++ type.97    If REF types from different namespaces are referenced, will resolve98    using self._type_namespaces.99    Use pad_for_generics when using as a generic to avoid operator ambiguity.100    Use wrap_optional to wrap the type in a scoped_ptr<T> if the Property is101    optional.102    """103    cpp_type = None104    if prop.type_ == PropertyType.REF:105      dependency_namespace = self._ResolveTypeNamespace(prop.ref_type)106      if not dependency_namespace:107        raise KeyError('Cannot find referenced type: %s' % prop.ref_type)108      if self._namespace != dependency_namespace:109        cpp_type = '%s::%s' % (self._cpp_namespaces[dependency_namespace],110                               prop.ref_type)111      else:112        cpp_type = prop.ref_type113    elif prop.type_ == PropertyType.BOOLEAN:114      cpp_type = 'bool'115    elif prop.type_ == PropertyType.INTEGER:116      cpp_type = 'int'117    elif prop.type_ == PropertyType.DOUBLE:118      cpp_type = 'double'119    elif prop.type_ == PropertyType.STRING:120      cpp_type = 'std::string'121    elif prop.type_ == PropertyType.ENUM:122      cpp_type = cpp_util.Classname(prop.name)123    elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES:124      cpp_type = 'DictionaryValue'125    elif prop.type_ == PropertyType.ANY:126      cpp_type = any_helper.ANY_CLASS127    elif prop.type_ == PropertyType.OBJECT:128      cpp_type = cpp_util.Classname(prop.name)129    elif prop.type_ == PropertyType.ARRAY:130      if prop.item_type.type_ in (131          PropertyType.REF, PropertyType.ANY, PropertyType.OBJECT):132        cpp_type = 'std::vector<linked_ptr<%s> > '133      else:134        cpp_type = 'std::vector<%s> '135      cpp_type = cpp_type % self.GetType(136          prop.item_type, pad_for_generics=True)137    else:138      raise NotImplementedError(prop.type_)139    # Enums aren't wrapped because C++ won't allow it. Optional enums have a140    # NONE value generated instead.141    if wrap_optional and prop.optional and prop.type_ != PropertyType.ENUM:142      cpp_type = 'scoped_ptr<%s> ' % cpp_type143    if pad_for_generics:144      return cpp_type145    return cpp_type.strip()146  def GenerateForwardDeclarations(self):147    """Returns the forward declarations for self._namespace.148    Use after GetRootNamespaceStart. Assumes all namespaces are relative to149    self._root_namespace.150    """151    c = Code()152    for namespace, types in sorted(self._NamespaceTypeDependencies().items()):153      c.Append('namespace %s {' % namespace.name)154      for type_ in types:155        c.Append('struct %s;' % type_)156      c.Append('}')157    c.Concat(self.GetNamespaceStart())158    for (name, type_) in self._namespace.types.items():159      if not type_.functions:160        c.Append('struct %s;' % name)161    c.Concat(self.GetNamespaceEnd())162    return c163  def GenerateIncludes(self):164    """Returns the #include lines for self._namespace.165    """166    c = Code()167    for dependency in sorted(self._NamespaceTypeDependencies().keys()):168      c.Append('#include "%s/%s.h"' % (169            dependency.source_file_dir,170            self._cpp_namespaces[dependency]))171    return c172  def _QualifyName(self, namespace, name):173    return '.'.join([namespace.name, name])174  def _ResolveTypeNamespace(self, ref_type):175    """Resolves a type name to its enclosing namespace.176    Searches for the ref_type first as an explicitly qualified name, then within177    the enclosing namespace, then within other namespaces that the current178    namespace depends upon.179    """180    if ref_type in self._type_namespaces:181      return self._type_namespaces[ref_type]182    qualified_name = self._QualifyName(self._namespace, ref_type)183    if qualified_name in self._type_namespaces:184      return self._type_namespaces[qualified_name]185    for (type_name, namespace) in self._type_namespaces.items():186      if type_name == self._QualifyName(namespace, ref_type):187        return namespace188    return None189  def _NamespaceTypeDependencies(self):190    """Returns a dict containing a mapping of model.Namespace to the C++ type191    of type dependencies for self._namespace.192    """193    dependencies = set()194    for function in self._namespace.functions.values():195      for param in function.params:196        dependencies |= self._PropertyTypeDependencies(param)197      if function.callback:198        for param in function.callback.params:199          dependencies |= self._PropertyTypeDependencies(param)200    for type_ in self._namespace.types.values():201      for prop in type_.properties.values():202        dependencies |= self._PropertyTypeDependencies(prop)203    dependency_namespaces = dict()204    for dependency in dependencies:205      namespace = self._ResolveTypeNamespace(dependency)206      if namespace != self._namespace:207        dependency_namespaces.setdefault(namespace, [])208        dependency_namespaces[namespace].append(dependency)209    return dependency_namespaces210  def _PropertyTypeDependencies(self, prop):211    """Recursively gets all the type dependencies of a property.212    """213    deps = set()214    if prop:215      if prop.type_ == PropertyType.REF:216        deps.add(prop.ref_type)217      elif prop.type_ == PropertyType.ARRAY:218        deps = self._PropertyTypeDependencies(prop.item_type)219      elif prop.type_ == PropertyType.OBJECT:220        for p in prop.properties.values():221          deps |= self._PropertyTypeDependencies(p)...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!!
