code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
return "freshmen" if (self._number and self._number == 9) else "{}s".format(self._name) if self._name else ""
def name_plural(self)
Return the grade's plural name (e.g. freshmen)
12.552982
6.805085
1.844647
def in_admin_group(user): return user.is_authenticated and user.has_admin_permission(group) return user_passes_test(in_admin_group)
def admin_required(group)
Decorator that requires the user to be in a certain admin group. For example, @admin_required("polls") would check whether a user is in the "admin_polls" group or in the "admin_all" group.
3.869001
3.702536
1.04496
# change this to not use other_phones num_phones = len(user.phones.all() or []) num_emails = len(user.emails.all() or []) num_websites = len(user.websites.all() or []) personal_info = {} for i in range(num_phones): personal_info["phone_{}".format(i)] = user.phones.all()[i] fo...
def get_personal_info(user)
Get a user's personal info attributes to pass as an initial value to a PersonalInformationForm.
2.17402
2.138873
1.016432
# FIXME: remove this hardcoded junk preferred_pic = {"preferred_photo": "AUTO"} if user.preferred_photo: preferred_pic["preferred_photo"] = user.preferred_photo.grade_number return preferred_pic
def get_preferred_pic(user)
Get a user's preferred picture attributes to pass as an initial value to a PreferredPictureForm.
7.690824
7.008366
1.097378
privacy_options = {} for ptype in user.permissions: for field in user.permissions[ptype]: if ptype == "self": privacy_options["{}-{}".format(field, ptype)] = user.permissions[ptype][field] else: privacy_options[field] = user.permissions[ptyp...
def get_privacy_options(user)
Get a user's privacy options to pass as an initial value to a PrivacyOptionsForm.
2.887494
2.862257
1.008817
notification_options = {} notification_options["receive_news_emails"] = user.receive_news_emails notification_options["receive_eighth_emails"] = user.receive_eighth_emails try: notification_options["primary_email"] = user.primary_email except Email.DoesNotExist: user.primary_em...
def get_notification_options(user)
Get a user's notification options to pass as an initial value to a NotificationOptionsForm.
2.659305
2.683732
0.990898
user = request.user if request.method == "POST": logger.debug(dict(request.POST)) phone_formset, email_formset, website_formset, errors = save_personal_info(request, user) if user.is_student: preferred_pic_form = save_preferred_pic(request, user) bus_route_f...
def preferences_view(request)
View and process updates to the preferences page.
1.951962
1.934681
1.008932
if "user" in request.GET: user = User.objects.user_with_ion_id(request.GET.get("user")) elif "student_id" in request.GET: user = User.objects.user_with_student_id(request.GET.get("student_id")) else: user = request.user if not user: messages.error(request, "Invalid ...
def privacy_options_view(request)
View and edit privacy options for a user.
2.720208
2.670377
1.018661
app = AndroidApplication.instance() r = app.create_future() #: Initiate a scan pkg = BarcodePackage.instance() pkg.setBarcodeResultListener(pkg.getId()) pkg.onBarcodeResult.connect(r.set_result) intent = cls(app) if formats: inte...
def scan(cls, formats=ALL_CODE_TYPES, camera=-1)
Shortcut only one at a time will work...
8.581172
8.436328
1.017169
super(AndroidBarcodeView, self).init_widget() d = self.declaration #: Observe activity state changes app = self.get_context() app.observe('state', self.on_activity_lifecycle_changed) if d.active: self.set_active(d.active) if d.light: ...
def init_widget(self)
Initialize the underlying widget.
4.775822
4.477264
1.066683
d = self.declaration if d.active: if change['value'] == 'paused': self.widget.pause(now=True) elif change['value'] == 'resumed': self.widget.resume()
def on_activity_lifecycle_changed(self, change)
If the app pauses without pausing the barcode scanner the camera can't be reopened. So we must do it here.
5.190529
5.012053
1.035609
if self.widget: self.set_active(False) super(AndroidBarcodeView, self).destroy()
def destroy(self)
Cleanup the activty lifecycle listener
11.203561
9.870759
1.135025
headers = {'Accept': 'application/json'} response = requests.get(urljoin(INAT_NODE_API_BASE_URL, endpoint), params, headers=headers, **kwargs) return response
def make_inaturalist_api_get_call(endpoint: str, params: Dict, **kwargs) -> requests.Response
Make an API call to iNaturalist. endpoint is a string such as 'observations' !! do not put / in front method: 'GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE' kwargs are passed to requests.request Returns a requests.Response object
3.774199
4.501555
0.838421
r = get_observations(params={'id': observation_id}) if r['results']: return r['results'][0] raise ObservationNotFound()
def get_observation(observation_id: int) -> Dict[str, Any]
Get details about an observation. :param observation_id: :returns: a dict with details on the observation :raises: ObservationNotFound
5.853145
5.693261
1.028083
r = make_inaturalist_api_get_call('observations', params=params) return r.json()
def get_observations(params: Dict) -> Dict[str, Any]
Search observations, see: http://api.inaturalist.org/v1/docs/#!/Observations/get_observations. Returns the parsed JSON returned by iNaturalist (observations in r['results'], a list of dicts)
10.023524
6.719895
1.491619
# According to the doc: "The large size of the observations index prevents us from supporting the page parameter # when retrieving records from large result sets. If you need to retrieve large numbers of records, use the # per_page and id_above or id_below parameters instead. results = [] # type...
def get_all_observations(params: Dict) -> List[Dict[str, Any]]
Like get_observations() but handles pagination so you get all the results in one shot. Some params will be overwritten: order_by, order, per_page, id_above (do NOT specify page when using this). Returns a list of dicts (one entry per observation)
4.811954
4.578864
1.050906
payload = { 'q': search_query, 'page': page } # type: Dict[str, Union[int, str]] response = requests.get("{base_url}/observation_fields.json".format(base_url=INAT_BASE_URL), params=payload) return response.json()
def get_observation_fields(search_query: str="", page: int=1) -> List[Dict[str, Any]]
Search the (globally available) observation :param search_query: :param page: :return:
3.316757
3.712963
0.893291
results = [] # type: List[Dict[str, Any]] page = 1 while True: r = get_observation_fields(search_query=search_query, page=page) if not r: return results results = results + r page = page + 1 sleep(THROTTLING_DELAY)
def get_all_observation_fields(search_query: str="") -> List[Dict[str, Any]]
Like get_observation_fields(), but handles pagination for you. :param search_query: a string to search
2.905384
3.027065
0.959802
# TODO: Also implement a put_or_update_observation_field_values() that deletes then recreates the field_value? # TODO: Write example use in docstring. # TODO: Return some meaningful exception if it fails because the field is already set. # TODO: Also show in example to obtain the observation_field_id? ...
def put_observation_field_values(observation_id: int, observation_field_id: int, value: Any, access_token: str) -> Dict[str, Any]
Sets an observation field (value) on an observation. :param observation_id: :param observation_field_id: :param value :param access_token: access_token: the access token, as returned by :func:`get_access_token()` :returns: iNaturalist's response as a dict, for example: {'id': 31, ...
6.928912
7.068284
0.980282
payload = { 'client_id': app_id, 'client_secret': app_secret, 'grant_type': "password", 'username': username, 'password': password } response = requests.post("{base_url}/oauth/token".format(base_url=INAT_BASE_URL), payload) try: return response.json(...
def get_access_token(username: str, password: str, app_id: str, app_secret: str) -> str
Get an access token using the user's iNaturalist username and password. (you still need an iNaturalist app to do this) :param username: :param password: :param app_id: :param app_secret: :return: the access token, example use: headers = {"Authorization": "Bearer %s" % access_token}
2.640991
2.409868
1.095907
data = {'observation_photo[observation_id]': observation_id} file_data = {'file': file_object} response = requests.post(url="{base_url}/observation_photos".format(base_url=INAT_BASE_URL), headers=_build_auth_header(access_token), data=data, ...
def add_photo_to_observation(observation_id: int, file_object: BinaryIO, access_token: str)
Upload a picture and assign it to an existing observation. :param observation_id: the ID of the observation :param file_object: a file-like object for the picture. Example: open('/Users/nicolasnoe/vespa.jpg', 'rb') :param access_token: the access token, as returned by :func:`get_access_token()`
3.276493
3.41202
0.960279
response = requests.post(url="{base_url}/observations.json".format(base_url=INAT_BASE_URL), json=params, headers=_build_auth_header(access_token)) response.raise_for_status() return response.json()
def create_observations(params: Dict[str, Dict[str, Any]], access_token: str) -> List[Dict[str, Any]]
Create a single or several (if passed an array) observations). :param params: :param access_token: the access token, as returned by :func:`get_access_token()` :return: iNaturalist's JSON response, as a Python object :raise: requests.HTTPError, if the call is not successful. iNaturalist returns an erro...
3.322124
2.925768
1.135471
response = requests.put(url="{base_url}/observations/{id}.json".format(base_url=INAT_BASE_URL, id=observation_id), json=params, headers=_build_auth_header(access_token)) response.raise_for_status() return response.json()
def update_observation(observation_id: int, params: Dict[str, Any], access_token: str) -> List[Dict[str, Any]]
Update a single observation. See https://www.inaturalist.org/pages/api+reference#put-observations-id :param observation_id: the ID of the observation to update :param params: to be passed to iNaturalist API :param access_token: the access token, as returned by :func:`get_access_token()` :return: iNatu...
2.896599
2.427588
1.1932
headers = _build_auth_header(access_token) headers['Content-type'] = 'application/json' response = requests.delete(url="{base_url}/observations/{id}.json".format(base_url=INAT_BASE_URL, id=observation_id), ...
def delete_observation(observation_id: int, access_token: str) -> List[Dict[str, Any]]
Delete an observation. :param observation_id: :param access_token: :return:
6.388655
6.482351
0.985546
self.kwargs['instantiate'] = True self.kwargs['parent'] = parent instance = self.cls(*self.args, **self.kwargs) instance._field_seqno = self._field_seqno return instance
def create_instance(self, parent)
Create an instance based off this placeholder with some parent
4.948251
4.685879
1.055992
if issubclass(placeholder.cls, FieldAccessor): return placeholder.cls.access(self._parent, placeholder) return self._parent.lookup_field_by_placeholder(placeholder)
def _ph2f(self, placeholder)
Lookup a field given a field placeholder
7.631084
6.153845
1.240051
recorded_checksum = self.field.getval() # convert negative offset to positive if offset < 0: offset += len(data) # replace checksum region with zero data = b''.join((data[:offset], b"\x00" * self.bytes_required, ...
def validate(self, data, offset)
Raises :class:`SuitcaseChecksumException` if not valid
5.470691
4.426862
1.235794
self.field.setval(self.algo(data[self.start:self.end])) sio = BytesIO() self.field.pack(sio) return sio.getvalue()
def packed_checksum(self, data)
Given the data of the entire packet return the checksum bytes
6.008844
6.008597
1.000041
try: return parent.lookup_field_by_placeholder(placeholder) except KeyError: field_placeholder, name = placeholder.args # Find the field whose attribute is being accessed. field = parent.lookup_field_by_placeholder(field_placeholder) #...
def access(cls, parent, placeholder)
Resolve the deferred field attribute access. :param cls: the FieldAccessor class :param parent: owning structure of the field being accessed :param placeholder: FieldPlaceholder object which holds our info :returns: FieldAccessor instance for that field's attribute
6.287016
5.387796
1.1669
tab = CRC16_KERMIT_TAB # minor optimization (now in locals) for byte in six.iterbytes(data): tbl_idx = (crc ^ byte) & 0xff crc = (tab[tbl_idx] ^ (crc >> 8)) & 0xffff return crc & 0xffff
def crc16_kermit(data, crc=0)
Calculate/Update the Kermit CRC16 checksum for some data
4.600915
4.759445
0.966691
tab = CRC16_CCITT_TAB # minor optimization (now in locals) for byte in six.iterbytes(data): crc = (((crc << 8) & 0xff00) ^ tab[((crc >> 8) & 0xff) ^ byte]) return crc & 0xffff
def crc16_ccitt(data, crc=0)
Calculate the crc16 ccitt checksum of some data A starting crc value may be specified if desired. The input data is expected to be a sequence of bytes (string) and the output is an integer in the range (0, 0xFFFF). No packing is done to the resultant crc value. To check the value a checksum, just pa...
4.225977
4.504866
0.938092
self._available_bytes += new_bytes callbacks = [] try: while True: packet = six.next(self._packet_generator) if packet is None: break else: callbacks.append(partial(self.packet_callback, ...
def feed(self, new_bytes)
Feed a new set of bytes into the protocol handler These bytes will be immediately fed into the parsing state machine and if new packets are found, the ``packet_callback`` will be executed with the fully-formed message. :param new_bytes: The new bytes to be fed into the stream protocol ...
9.244908
8.850583
1.044554
crc_fields = [] greedy_field = None # go through the fields from first to last. If we hit a greedy # field, break out of the loop for i, (name, field) in enumerate(self.ordered_fields): if isinstance(field, CRCField): crc_fields.append((field...
def unpack_stream(self, stream)
Unpack bytes from a stream of data field-by-field In the most basic case, the basic algorithm here is as follows:: for _name, field in self.ordered_fields: length = field.bytes_required data = stream.read(length) field.unpack(data) This logic i...
2.885664
2.668988
1.081183
value = super(URL, self).validate(instance, value) parsed_url = urlparse(value) if not parsed_url.scheme or not parsed_url.netloc: self.error(instance, value, extra='URL needs scheme and netloc.') parse_result = ParseResult( scheme=parsed_url.scheme, ...
def validate(self, instance, value)
Check if input is valid URL
2.255178
2.182803
1.033157
json_dict = super(Singleton, self).serialize( include_class=include_class, save_dynamic=save_dynamic, **kwargs ) json_dict['_singleton_id'] = self._singleton_id return json_dict
def serialize(self, include_class=True, save_dynamic=False, **kwargs)
Serialize Singleton instance to a dictionary. This behaves identically to HasProperties.serialize, except it also saves the identifying name in the dictionary as well.
2.710472
2.497156
1.085423
if not isinstance(value, dict): raise ValueError('HasProperties must deserialize from dictionary') identifier = value.pop('_singleton_id', value.get('name')) if identifier is None: raise ValueError('Singleton classes must contain identifying name') if ide...
def deserialize(cls, value, trusted=False, strict=False, assert_valid=False, **kwargs)
Create a Singleton instance from a serialized dictionary. This behaves identically to HasProperties.deserialize, except if the singleton is already found in the singleton registry the existing value is used. .. note:: If property values differ from the existing singleton a...
3.628622
3.408231
1.064664
for name in cls._mutators: #pylint: disable=protected-access if not hasattr(cls, name): continue setattr(cls, name, properties_mutator(cls, name)) for name in cls._operators: #pylint: ...
def add_properties_callbacks(cls)
Class decorator to add change notifications to builtin containers
2.374013
2.34528
1.012251
def wrapper(self, *args, **kwargs): if ( getattr(self, '_instance', None) is None or getattr(self, '_name', '') == '' or self is not getattr(self._instance, self._name) ): return getattr(super(cls, self), name)(*args, **kwarg...
def properties_mutator(cls, name, ioper=False)
Wraps a mutating container method to add HasProperties notifications If the container is not part of a HasProperties instance, behavior is unchanged. However, if it is part of a HasProperties instance the new method calls set, triggering change notifications.
3.006451
2.932717
1.025142
def wrapper(self, *args, **kwargs): output = getattr(super(cls, self), name)(*args, **kwargs) return cls(output) wrapped = getattr(cls, name) wrapper.__name__ = wrapped.__name__ wrapper.__doc__ = wrapped.__doc__ return wrapper
def properties_operator(cls, name)
Wraps a container operator to ensure container class is maintained
3.240168
2.866895
1.130201
container_class = value.__class__ if container_class in OBSERVABLE_REGISTRY: observable_class = OBSERVABLE_REGISTRY[container_class] elif container_class in OBSERVABLE_REGISTRY.values(): observable_class = container_class else: observable_class = add_properties_callbacks( ...
def observable_copy(value, name, instance)
Return an observable container for HasProperties notifications This method creates a new container class to allow HasProperties instances to :code:`observe_mutations`. It returns a copy of the input value as this new class. The output class behaves identically to the input value's original class, ...
3.284345
3.014899
1.089372
if ( isinstance(value, CLASS_TYPES) and issubclass(value, HasProperties) ): value = Instance('', value) if not isinstance(value, basic.Property): raise TypeError('Contained prop must be a Property instance or ' 'HasProperties class') i...
def validate_prop(value)
Validate Property instance for container items
6.980271
6.253002
1.116307
itext = self.class_info if self.prop.info: itext += ' (each item is {})'.format(self.prop.info) if self.max_length is None and self.min_length is None: return itext if self.max_length is None: lentext = 'length >= {}'.format(self.min_length) ...
def info(self)
Supplemental description of the list, with length and type
2.892304
2.761474
1.047377
if not self.coerce and not isinstance(value, self._class_container): self.error(instance, value) if self.coerce and not isinstance(value, CONTAINERS): value = [value] if not isinstance(value, self._class_container): out_class = self._class_container ...
def validate(self, instance, value)
Check the class of the container and validate each element This returns a copy of the container to prevent unwanted sharing of pointers.
3.509772
3.356172
1.045766
valid = super(Tuple, self).assert_valid(instance, value) if not valid: return False if value is None: value = instance._get(self.name) if value is None: return True if ( (self.min_length is not None and len(valu...
def assert_valid(self, instance, value=None)
Check if tuple and contained properties are valid
2.507269
2.286807
1.096406
kwargs.update({'include_class': kwargs.get('include_class', True)}) if self.serializer is not None: return self.serializer(value, **kwargs) if value is None: return None serial_list = [self.prop.serialize(val, **kwargs) for val in v...
def serialize(self, value, **kwargs)
Return a serialized copy of the tuple
3.551071
3.375387
1.052049
kwargs.update({'trusted': kwargs.get('trusted', False)}) if self.deserializer is not None: return self.deserializer(value, **kwargs) if value is None: return None output_list = [self.prop.deserialize(val, **kwargs) for val in value]...
def deserialize(self, value, **kwargs)
Return a deserialized copy of the tuple
4.152282
4.155841
0.999144
serial_list = [ val.serialize(**kwargs) if isinstance(val, HasProperties) else val for val in value ] return serial_list
def to_json(value, **kwargs)
Return a copy of the tuple as a list If the tuple contains HasProperties instances, they are serialized.
7.401935
4.551094
1.626408
classdoc = self.prop.sphinx_class().replace( ':class:`', '{info} of :class:`' ) return classdoc.format(info=self.class_info)
def sphinx_class(self)
Redefine sphinx class to point to prop class
11.099962
10.181638
1.090194
itext = self.class_info if self.key_prop.info and self.value_prop.info: itext += ' (keys: {}; values: {})'.format( self.key_prop.info, self.value_prop.info ) elif self.key_prop.info: itext += ' (keys: {})'.format(self.key_prop.info) ...
def info(self)
Supplemental description of the list, with length and type
2.373452
2.312552
1.026335
valid = super(Dictionary, self).assert_valid(instance, value) if not valid: return False if value is None: value = instance._get(self.name) if value is None: return True if self.key_prop or self.value_prop: for key, val in ...
def assert_valid(self, instance, value=None)
Check if dict and contained properties are valid
2.256915
2.104882
1.072229
kwargs.update({'include_class': kwargs.get('include_class', True)}) if self.serializer is not None: return self.serializer(value, **kwargs) if value is None: return None serial_tuples = [ ( self.key_prop.serialize(key, **kwargs...
def serialize(self, value, **kwargs)
Return a serialized copy of the dict
3.438232
3.211594
1.070569
kwargs.update({'trusted': kwargs.get('trusted', False)}) if self.deserializer is not None: return self.deserializer(value, **kwargs) if value is None: return None output_tuples = [ ( self.key_prop.deserialize(key, **kwargs), ...
def deserialize(self, value, **kwargs)
Return a deserialized copy of the dict
3.783292
3.650862
1.036274
serial_dict = { key: ( val.serialize(**kwargs) if isinstance(val, HasProperties) else val ) for key, val in iteritems(value) } return serial_dict
def to_json(value, **kwargs)
Return a copy of the dictionary If the values are HasProperties instances, they are serialized
5.095697
3.80192
1.340296
props_dict = { k: v for k, v in iter(input_dict.items()) if ( k in has_props_cls._props and ( include_immutable or any( hasattr(has_props_cls._props[k], att) for att in ('required', 'new_name') ) ...
def filter_props(has_props_cls, input_dict, include_immutable=True)
Split a dictionary based keys that correspond to Properties Returns: **(props_dict, others_dict)** - Tuple of two dictionaries. The first contains key/value pairs from the input dictionary that correspond to the Properties of the input HasProperties class. The second contains the remaining key/value ...
2.813226
2.977041
0.944974
prop_def = getattr(self, '_default', utils.undefined) for prop in self.props: if prop.default is utils.undefined: continue if prop_def is utils.undefined: prop_def = prop.default break return prop_def
def default(self)
Default value of the property
3.915759
3.733598
1.04879
error_messages = [] for prop in self.props: try: return getattr(prop, method_name)(instance, value) except GENERIC_ERRORS as err: if hasattr(err, 'error_tuples'): error_messages += [ err_tup.mess...
def _try_prop_method(self, instance, value, method_name)
Helper method to perform a method on each of the union props This method gathers all errors and returns them at the end if the method on each of the props fails.
3.85114
3.654415
1.053832
valid = super(Union, self).assert_valid(instance, value) if not valid: return False if value is None: value = instance._get(self.name) if value is None: return True return self._try_prop_method(instance, value, 'assert_valid')
def assert_valid(self, instance, value=None)
Check if the Union has a valid value
3.995696
3.525724
1.133298
kwargs.update({'include_class': kwargs.get('include_class', True)}) if self.serializer is not None: return self.serializer(value, **kwargs) if value is None: return None for prop in self.props: try: prop.validate(None, value) ...
def serialize(self, value, **kwargs)
Return a serialized value If no serializer is provided, it uses the serialize method of the prop corresponding to the value
3.470629
3.542449
0.979726
kwargs.update({'trusted': kwargs.get('trusted', False)}) if self.deserializer is not None: return self.deserializer(value, **kwargs) if value is None: return None instance_props = [ prop for prop in self.props if isinstance(prop, Instance) ...
def deserialize(self, value, **kwargs)
Return a deserialized value If no deserializer is provided, it uses the deserialize method of the prop corresponding to the value
3.122062
3.073422
1.015826
if isinstance(value, HasProperties): return value.serialize(**kwargs) return value
def to_json(value, **kwargs)
Return value, serialized if value is a HasProperties instance
8.919294
3.765685
2.368571
def wrapped(val, **kwargs): try: return func(val, **kwargs) except TypeError: return func(val) return wrapped
def accept_kwargs(func)
Wrap a function that may not accept kwargs so they are accepted The output function will always have call signature of :code:`func(val, **kwargs)`, whereas the original function may have call signatures of :code:`func(val)` or :code:`func(val, **kwargs)`. In the case of the former, rather than erroring...
3.688937
4.344003
0.849202
if ( (prop.min is not None and value < prop.min) or (prop.max is not None and value > prop.max) ): prop.error(instance, value, extra='Not within allowed range.')
def _in_bounds(prop, instance, value)
Checks if the value is in the range (min, max)
3.79356
3.288867
1.153455
terms = PropertyTerms( self.name, self.__class__, self._args, self._kwargs, self.meta ) return terms
def terms(self)
Initialization terms and options for Property
8.98317
6.092323
1.474506
if not tag: pass elif len(tag) == 1 and isinstance(tag[0], dict): self._meta.update(tag[0]) else: raise TypeError('Tags must be provided as key-word arguments or ' 'a dictionary') self._meta.update(kwtags) r...
def tag(self, *tag, **kwtags)
Tag a Property instance with metadata dictionary
3.481207
3.111923
1.118667
if value is None: value = instance._get(self.name) if ( value is not None and not self.equal(value, self.validate(instance, value)) ): message = 'Invalid value for property: {}: {}'.format( self.name, value ...
def assert_valid(self, instance, value=None)
Returns True if the Property is valid on a HasProperties instance Raises a ValueError if the value is invalid.
3.736288
3.627931
1.029868
return all(equal) return equal
def equal(self, value_a, value_b): #pylint: disable=no-self-use equal = value_a == value_b if hasattr(equal, '__iter__')
Check if two valid Property values are equal .. note:: This method assumes that :code:`None` and :code:`properties.undefined` are never passed in as values
14.475471
140.182343
0.103262
scope = self def fget(self): return self._get(scope.name) return property(fget=fget, doc=scope.sphinx())
def get_property(self)
Establishes access of GettableProperty values
10.256699
8.740591
1.173456
return self.deserializer(value, **kwargs) if value is None: return None return self.from_json(value, **kwargs)
def deserialize(self, value, **kwargs): #pylint: disable=unused-argument kwargs.update({'trusted': kwargs.get('trusted', False)}) if self.deserializer is not None
Deserialize input value to valid Property value This method uses the Property :code:`deserializer` if available. Otherwise, it uses :code:`from_json`. Any keyword arguments are passed through to these methods.
3.904754
3.300376
1.183124
error_class = error_class or ValidationError prefix = 'The {} property'.format(self.__class__.__name__) if self.name != '': prefix = prefix + " '{}'".format(self.name) if instance is not None: prefix = prefix + ' of a {cls} instance'.format( ...
def error(self, instance, value, error_class=None, extra='')
Generate a :code:`ValueError` for invalid value assignment The instance is the containing HasProperties instance, but it may be None if the error is raised outside a HasProperties class.
3.30059
3.265547
1.010731
try: assert __IPYTHON__ classdoc = '' except (NameError, AssertionError): scls = self.sphinx_class() classdoc = ' ({})'.format(scls) if scls else '' prop_doc = '**{name}**{cls}: {doc}{info}'.format( name=self.name, ...
def sphinx(self)
Generate Sphinx-formatted documentation for the Property
4.261243
4.218251
1.010192
classdoc = ':class:`{cls} <{pref}.{cls}>`' if self.__module__.split('.')[0] == 'properties': pref = 'properties' else: pref = text_type(self.__module__) return classdoc.format(cls=self.__class__.__name__, pref=pref)
def sphinx_class(self)
Property class name formatted for Sphinx doc linking
5.25286
4.452349
1.179795
if not callable(func): raise TypeError('setter must be callable function') if hasattr(func, '__code__') and func.__code__.co_argcount != 2: raise TypeError('setter must be a function with two arguments') if func.__name__ != self.name: raise TypeError(...
def setter(self, func)
Register a set function for the DynamicProperty This function must take two arguments, self and the new value. Input value to the function is validated with prop validation prior to execution.
2.776761
2.954503
0.93984
if not callable(func): raise TypeError('deleter must be callable function') if hasattr(func, '__code__') and func.__code__.co_argcount != 1: raise TypeError('deleter must be a function with two arguments') if func.__name__ != self.name: raise TypeErro...
def deleter(self, func)
Register a delete function for the DynamicProperty This function may only take one argument, self.
3.116201
3.169741
0.983109
scope = self def fget(self): value = scope.func(self) if value is None or value is undefined: return None return scope.validate(self, value) def fset(self, value): if scope.set_func is None: ...
def get_property(self)
Establishes the dynamic behavior of Property values
2.704075
2.529016
1.06922
if value is None: value = instance._get(self.name) if value is None and self.required: message = ( "The '{name}' property of a {cls} instance is required " "and has not been set.".format( name=self.name, ...
def assert_valid(self, instance, value=None)
Returns True if the Property is valid on a HasProperties instance Raises a ValueError if the value required and not set, not valid, not correctly coerced, etc. .. note:: Unlike :code:`validate`, this method requires instance to be a HasProperties instance; it cannot be...
2.830689
2.818432
1.004349
scope = self def fget(self): return self._get(scope.name) def fset(self, value): if value is not undefined: value = scope.validate(self, value) self._set(scope.name, value) def fdel(self): ...
def get_property(self)
Establishes access of Property values
3.269409
3.037071
1.076501
if callable(self.default): default_val = self.default() default_str = 'new instance of {}'.format( default_val.__class__.__name__ ) else: default_val = self.default default_str = '{}'.format(self.default) try: ...
def sphinx(self)
Basic docstring formatted for Sphinx docs
2.831217
2.723477
1.039559
if self.cast: value = bool(value) if not isinstance(value, BOOLEAN_TYPES): self.error(instance, value) return value
def validate(self, instance, value)
Checks if value is a boolean
5.060241
3.552391
1.424461
if isinstance(value, string_types): value = value.upper() if value in ('TRUE', 'Y', 'YES', 'ON'): return True if value in ('FALSE', 'N', 'NO', 'OFF'): return False if isinstance(value, int): return value rai...
def from_json(value, **kwargs)
Coerces JSON string to boolean
2.644061
2.484102
1.064393
try: intval = int(value) if not self.cast and abs(value - intval) > TOL: self.error( instance=instance, value=value, extra='Not within tolerance range of {}.'.format(TOL), ) excep...
def validate(self, instance, value)
Checks that value is an integer and in min/max bounds
5.605909
5.414555
1.035341
try: floatval = float(value) if not self.cast and abs(value - floatval) > TOL: self.error( instance=instance, value=value, extra='Not within tolerance range of {}.'.format(TOL), ) ...
def validate(self, instance, value)
Checks that value is a float and in min/max bounds Non-float numbers are coerced to floats
4.861944
4.853462
1.001748
try: compval = complex(value) if not self.cast and ( abs(value.real - compval.real) > TOL or abs(value.imag - compval.imag) > TOL ): self.error( instance=instance, value=v...
def validate(self, instance, value)
Checks that value is a complex number Floats and Integers are coerced to complex numbers
4.16721
3.937744
1.058273
value_type = type(value) if not isinstance(value, string_types): self.error(instance, value) if self.regex is not None and self.regex.search(value) is None: #pylint: disable=no-member self.error(instance, value, extra='Regex does not match.') value...
def validate(self, instance, value)
Check if value is a string, and strips it and changes case
2.764745
2.55675
1.081351
if self.descriptions is None: choice_list = ['"{}"'.format(choice) for choice in self.choices] else: choice_list = [ '"{}" ({})'.format(choice, self.descriptions[choice]) for choice in self.choices ] if len(self.choices...
def info(self)
Formatted string to display the available choices
2.647371
2.358539
1.122462
self.error(instance, value) for key, val in self.choices.items(): test_value = value if self.case_sensitive else value.upper() test_key = key if self.case_sensitive else key.upper() test_val = val if self.case_sensitive else [_.upper() for _ in val] if...
def validate(self, instance, value): #pylint: disable=inconsistent-return-statements if not isinstance(value, string_types)
Check if input is a valid string based on the choices
3.201343
2.9305
1.092422
if isinstance(value, string_types): value = COLORS_NAMED.get(value, value) if value.upper() == 'RANDOM': value = random.choice(COLORS_20) value = value.upper().lstrip('#') if len(value) == 3: value = ''.join(v*2 for v in va...
def validate(self, instance, value)
Check if input is valid color and converts to RGB
2.539015
2.434873
1.042771
if isinstance(value, datetime.datetime): return value if not isinstance(value, string_types): self.error( instance=instance, value=value, extra='Cannot convert non-strings to datetime.', ) try: ...
def validate(self, instance, value)
Check if value is a valid datetime object or JSON datetime string
3.174809
2.853313
1.112675
if not isinstance(value, uuid.UUID): self.error(instance, value) return value
def validate(self, instance, value)
Check that value is a valid UUID instance
4.522349
2.892602
1.563419
default_mode = (self.mode,) if self.mode is not None else None return getattr(self, '_valid_mode', default_mode)
def valid_modes(self)
Valid modes of an open file
5.697187
5.70002
0.999503
prop = super(File, self).get_property() # scope is the Property instance scope = self def fdel(self): if self._get(scope.name) is not None: self._get(scope.name).close() self._set(scope.name, undefined) new_prop = ...
def get_property(self)
Establishes access of Property values
5.941183
5.73824
1.035367
if isinstance(value, string_types) and self.mode is not None: try: value = open(value, self.mode) except (IOError, TypeError): self.error(instance, value, extra='Cannot open file: {}'.format(value)) if not all([h...
def validate(self, instance, value)
Checks that the value is a valid file open in the correct mode If value is a string, it attempts to open it with the given mode.
2.601071
2.323742
1.119346
if self.warn: warnings.warn( "\nProperty '{}' is deprecated and may be removed in the " "future. Please use '{}'.".format(self.name, self.new_name), FutureWarning, stacklevel=3 )
def display_warning(self)
Display a FutureWarning about using a Renamed Property
4.453189
3.504948
1.270543
scope = self def fget(self): scope.display_warning() return getattr(self, scope.new_name) def fset(self, value): scope.display_warning() setattr(self, scope.new_name, value) def fdel(self): ...
def get_property(self)
Establishes the dynamic behavior of Property values
2.800702
2.616706
1.070316
try: if isinstance(value, self.instance_class): return value if isinstance(value, dict): return self.instance_class(**value) return self.instance_class(value) except GENERIC_ERRORS as err: if hasattr(err, 'error_tup...
def validate(self, instance, value)
Check if value is valid type of instance_class If value is an instance of instance_class, it is returned unmodified. If value is either (1) a keyword dictionary with valid parameters to construct an instance of instance_class or (2) a valid input argument to construct instance_class, th...
3.602149
3.327232
1.082626
valid = super(Instance, self).assert_valid(instance, value) if not valid: return False if value is None: value = instance._get(self.name) if isinstance(value, HasProperties): value.validate() return True
def assert_valid(self, instance, value=None)
Checks if valid, including HasProperty instances pass validation
3.837647
3.063793
1.25258
kwargs.update({'include_class': kwargs.get('include_class', True)}) if self.serializer is not None: return self.serializer(value, **kwargs) if value is None: return None if isinstance(value, HasProperties): return value.serialize(**kwargs) ...
def serialize(self, value, **kwargs)
Serialize instance to JSON If the value is a HasProperties instance, it is serialized with the include_class argument passed along. Otherwise, to_json is called.
3.132258
2.398305
1.30603
if isinstance(value, HasProperties): return value.serialize(**kwargs) try: return json.loads(json.dumps(value)) except TypeError: raise TypeError( "Cannot convert type {} to JSON without calling 'serialize' " "on an ins...
def to_json(value, **kwargs)
Convert instance to JSON
5.847172
5.619497
1.040515
classdoc = ':class:`{cls} <{pref}.{cls}>`'.format( cls=self.instance_class.__name__, pref=self.instance_class.__module__, ) return classdoc
def sphinx_class(self)
Redefine sphinx class so documentation links to instance_class
5.330386
4.574029
1.165359
change_only = kwargs.get('change_only', True) observer(instance, prop, callback, change_only=change_only)
def properties_observer(instance, prop, callback, **kwargs)
Adds properties callback handler
4.130173
4.114298
1.003858
if getattr(self, '_unlinked', False): return if getattr(self, '_updating', False): return self._updating = True try: setattr(self.target[0], self.target[1], self.transform( getattr(self.source[0], self.source[1]) ))...
def _update(self, *_)
Set target value to source value
3.142531
2.772813
1.133337