code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
line = PRECISION_TABLE[precision] dec = 1/float(10 ** precision) return meters / line[3] * dec
def estimate_meters_to_deg(meters, precision=PRECISION_PERSON)
Meters to degrees estimation See https://en.wikipedia.org/wiki/Decimal_degrees Args: meters (float) precision (float) Returns: float: meters in degrees approximation
14.388977
16.558479
0.868979
if len(dt_str) <= 20: return datetime.datetime.strptime(dt_str, "%Y-%m-%dT%H:%M:%SZ") else: dt_str = dt_str.split(".") return isostr_to_datetime("%sZ" % dt_str[0])
def isostr_to_datetime(dt_str)
Converts iso formated text string into a datetime object Args: dt_str (str): ISO formated text string Returns: :obj:`datetime.datetime`
2.391923
2.710223
0.882556
"s -> (s0,s1), (s1,s2), (s2, s3), ..." now, nxt = tee(iterable) next(nxt, None) return izip(now, nxt)
def pairwise(iterable)
s -> (s0,s1), (s1,s2), (s2, s3), ...
2.935219
3.085716
0.951228
if self.feature_length > 0: result = list(self.labels.classes_) else: result = [] for label in labels: result.append(label) self.labels.fit(result)
def __learn_labels(self, labels)
Learns new labels, this method is intended for internal use Args: labels (:obj:`list` of :obj:`str`): Labels to learn
4.961628
5.493104
0.903247
labels = np.ravel(labels) self.__learn_labels(labels) if len(labels) == 0: return labels = self.labels.transform(labels) if self.feature_length > 0 and hasattr(self.clf, 'partial_fit'): # FIXME? check docs, may need to pass class=[...] ...
def learn(self, features, labels)
Fits the classifier If it's state is empty, the classifier is fitted, if not the classifier is partially fitted. See sklearn's SGDClassifier fit and partial_fit methods. Args: features (:obj:`list` of :obj:`list` of :obj:`float`) labels (:obj:`list` of :obj:`str...
4.221132
4.74638
0.889337
probs = self.clf.predict_proba(features) if verbose: labels = self.labels.classes_ res = [] for prob in probs: vals = {} for i, val in enumerate(prob): label = labels[i] vals[label] = val...
def predict(self, features, verbose=False)
Probability estimates of each feature See sklearn's SGDClassifier predict and predict_proba methods. Args: features (:obj:`list` of :obj:`list` of :obj:`float`) verbose: Boolean, optional. If true returns an array where each element is a dictionary, where keys a...
2.74425
2.668737
1.028295
return all( [callable(getattr(obj, method_name, None)) for method_name in ('__enter__', '__exit__')] + [any([callable(getattr(obj, method_name, None)) for method_name in ('next', '__iter__')])] )
def is_file(obj)
Retrun True is object has 'next', '__enter__' and '__exit__' methods. Suitable to check both builtin ``file`` and ``django.core.file.File`` instances.
3.494177
2.751827
1.269766
if is_file(file_or_path): file_obj = file_or_path else: file_obj = open(file_or_path, 'rb') yield file_obj file_obj.close()
def file_adapter(file_or_path)
Context manager that works similar to ``open(file_path)``but also accepts already openned file-like objects.
2.229391
1.86653
1.194404
if not isinstance(source_paths, Iterable) or len(source_paths) < 3: raise TypeError('FromCSVTablesGenerator.source_loader accepts list of 3 paths as argument. Got `%s` instead' % source_paths) self.native_language = '' self.languages = [] self.templates = [] ...
def source_loader(self, source_paths, create_missing_tables=True)
Load source from 3 csv files. First file should contain global settings: * ``native_lagnauge,languages`` header on first row * appropriate values on following rows Example:: native_lagnauge,languages ru,ru ,en Second file should contain ...
4.609742
3.896008
1.183196
url = request.args.get('url') # read image URL as a request URL param response = requests.get(url) # make request to static image file return response.content
def loader()
Load image from URL, and preprocess for Resnet.
9.781054
9.024106
1.083881
prediction = prediction.data.numpy()[0] top_predictions = prediction.argsort()[-3:][::-1] return [labels[prediction] for prediction in top_predictions]
def postprocessor(prediction)
Map prediction tensor to labels.
3.528961
3.387003
1.041913
logger = logging.getLogger(name) logger.setLevel(getenv('LOGLEVEL', 'INFO')) return logger
def get_logger(name)
Get a logger with the specified name.
3.704875
3.492337
1.060858
if is_serializable(data): return data # if numpy array convert to list try: return data.tolist() except AttributeError: pass except Exception as e: logger.debug('{} exception ({}): {}'.format(type(e).__name__, e, data)) # try serializing each child element ...
def make_serializable(data)
Ensure data is serializable.
2.980819
2.888973
1.031792
data = request.get_json() logger.debug('Received JSON data of length {:,}'.format(len(data))) return data
def json_numpy_loader()
Load data from JSON request and convert to numpy array.
6.068301
5.138002
1.181062
from keras.preprocessing import image import numpy as np from PIL import Image from io import BytesIO def preprocess_image_bytes(data_bytes): try: img = Image.open(BytesIO(data_bytes)) # open image except OSError as e: raise ValueError('Please ...
def get_bytes_to_image_callback(image_dims=(224, 224))
Return a callback to process image bytes for ImageNet.
3.275921
3.140115
1.043249
logger.error(message, exc_info=True) return make_response( message, status_code, dict(exception_type=type(exception).__name__, exception_message=str(exception)), )
def exception_log_and_respond(exception, logger, message, status_code)
Log an error and send jsonified respond.
2.718419
2.754186
0.987014
response_body = dict(message=message) if details: response_body['details'] = details response = jsonify(response_body) response.status_code = status_code return response
def make_response(message, status_code, details=None)
Make a jsonified response with specified message and status code.
1.978549
1.968883
1.00491
# copy instance variables to local scope for resource class predict = self.predict logger = self.app.logger # create restful resource class Predictions(Resource): @staticmethod def post(): # read data from API request ...
def _create_prediction_endpoint( self, to_numpy=True, data_loader=json_numpy_loader, preprocessor=lambda x: x, input_validation=lambda data: (True, None), postprocessor=lambda x: x, make_serializable_post=True)
Create an endpoint to serve predictions. Arguments: - input_validation (fn): takes a numpy array as input; returns True if validation passes and False otherwise - data_loader (fn): reads flask request and returns data preprocessed to be used in the `predi...
2.776935
2.798585
0.992264
# make sure data is serializable data = make_serializable(data) # create generic restful resource to serve static JSON data class InfoBase(Resource): @staticmethod def get(): return data def info_factory(name): ...
def create_info_endpoint(self, name, data)
Create an endpoint to serve info GET requests.
5.209114
5.281765
0.986245
model = self.model # parse model details model_details = {} for key, value in model.__dict__.items(): model_details[key] = make_serializable(value) # create generic restful resource to serve model information as JSON class ModelInfo(Resource): ...
def _create_model_info_endpoint(self, path='/info/model')
Create an endpoint to serve info GET requests.
4.936531
4.868094
1.014058
from meinheld import server, middleware # self.app.run(host=host, port=port) server.listen((host, port)) server.run(middleware.WebSocketMiddleware(self.app))
def serve(self, host='127.0.0.1', port=5000)
Serve predictions as an API endpoint.
5.997481
6.628015
0.904868
model = Sequential() model.add(Dense(100, input_dim=input_dim, activation='sigmoid')) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='SGD') return model
def get_model(input_dim)
Create and compile simple model.
1.858098
1.748258
1.062828
global data # check num dims if input_data.ndim != 2: return False, 'Data should have two dimensions.' # check number of columns if input_data.shape[1] != data.data.shape[1]: reason = '{} features required, {} features provided'.format( data.data.shape[1], input_data...
def validator(input_data)
Simple model input validator. Validator ensures the input data array is - two dimensional - has the correct number of features.
3.999215
3.763785
1.062551
with open(chd_file, "rb") as f: header = { "cinefileheader": cine.CINEFILEHEADER(), "bitmapinfoheader": cine.BITMAPINFOHEADER(), "setup": cine.SETUP(), } f.readinto(header["cinefileheader"]) f.readinto(header["bitmapinfoheader"]) f.re...
def read_chd_header(chd_file)
read the .chd header file created when Vision Research software saves the images in a file format other than .cine
3.11705
2.924409
1.065873
if version is None: versions = short_versions else: versions = [get_canonical_version(version)] for version in versions: url = "http://docs.python.org/{}/objects.inv".format(version) modules = sorted( list( fetch_inventory(DummyApp(), "", ...
def fetch_list(version=None)
For the given version of Python (or all versions if no version is set), this function: - Uses the `fetch_inventory` function of :py:mod`sphinx.ext.intersphinx` to grab and parse the Sphinx object inventory (ie ``http://docs.python.org/<version>/objects.inv``) for the given version. - Grabs the names o...
4.123953
3.126953
1.318841
version = get_canonical_version(version) if version is not None else '.'.join( str(x) for x in sys.version_info[:2]) module_list_file = os.path.join(list_dir, "{}.txt".format(version)) with open(module_list_file) as f: result = [y for y in [x.strip() for x in f.readlines()] if y] ...
def stdlib_list(version=None)
Given a ``version``, return a ``list`` of names of the Python Standard Libraries for that version. These names are obtained from the Sphinx inventory file (used in :py:mod:`sphinx.ext.intersphinx`). :param str|None version: The version (as a string) whose list of libraries you want (one of ``"2.6"``, `...
3.176702
3.792331
0.837665
return autohash_decorate(cls, include=include, exclude=exclude, only_constructor_args=only_constructor_args, only_public_fields=only_public_fields)
def autohash(include=None, # type: Union[str, Tuple[str]] exclude=None, # type: Union[str, Tuple[str]] only_constructor_args=False, # type: bool only_public_fields=False, # type: bool cls=DECORATED )
A decorator to makes objects of the class implement __hash__, so that they can be used correctly for example in sets. Parameters allow to customize the list of attributes that are taken into account in the hash. :param include: a tuple of explicit attribute names to include (None means all) :param...
2.674799
2.948284
0.907239
# type: (...) -> Type[T] # first check that we do not conflict with other known decorators _check_known_decorators(cls, '@autohash') # perform the class mod _execute_autohash_on_class(cls, include=include, exclude=exclude, only_constructor_args=only_constructor_args, ...
def autohash_decorate(cls, # type: Type[T] include=None, # type: Union[str, Tuple[str]] exclude=None, # type: Union[str, Tuple[str]] only_constructor_args=False, # type: bool ...
To automatically generate the appropriate methods so that objects of this class are hashable, manually, without using @autohash decorator. :param cls: the class on which to execute. Note that it won't be wrapped. :param include: a tuple of explicit attribute names to include (None means all) :param exc...
4.387878
5.530842
0.793347
# First check parameters validate_include_exclude(include, exclude) # Override hash method if not already implemented if not method_already_there(object_type, '__hash__'): if only_constructor_args: # a. Find the __init__ constructor signature constructor = get_const...
def _execute_autohash_on_class(object_type, # type: Type[T] include=None, # type: Union[str, Tuple[str]] exclude=None, # type: Union[str, Tuple[str]] only_constructor_args=False,...
A decorator to make objects of the class implement __hash__, so that they can be used correctly for example in sets. Parameters allow to customize the list of attributes that are taken into account in the hash. :param object_type: the class on which to execute. :param include: a tuple of explicit attr...
3.681509
3.542531
1.039232
return autoclass_decorate(cls, include=include, exclude=exclude, autoargs=autoargs, autoprops=autoprops, autodict=autodict, autohash=autohash)
def autoclass(include=None, # type: Union[str, Tuple[str]] exclude=None, # type: Union[str, Tuple[str]] autoargs=True, # type: bool autoprops=True, # type: bool autodict=True, # type: bool autohash=True, # type: bool cls=DE...
A decorator to perform @autoargs, @autoprops and @autodict all at once with the same include/exclude list. :param include: a tuple of explicit attribute names to include (None means all) :param exclude: a tuple of explicit attribute names to exclude. In such case, include should be None. :param autoargs: a...
2.401993
2.999464
0.800808
check_cmd(cmd) all_pkgs_str = " ".join(all_pkgs) print("INSTALLING: " + cmd + " install " + all_pkgs_str) subprocess.check_call([cmd, 'install'] + packages)
def install(cmd, packages)
Installs all packages provided at once :param packages: :return:
4.630583
5.409034
0.856083
return autodict_decorate(cls, include=include, exclude=exclude, only_constructor_args=only_constructor_args, only_public_fields=only_public_fields)
def autodict(include=None, # type: Union[str, Tuple[str]] exclude=None, # type: Union[str, Tuple[str]] only_constructor_args=True, # type: bool only_public_fields=True, # type: bool cls=DECORATED )
A decorator to makes objects of the class behave like a read-only `dict`. It does several things: * it adds collections.Mapping to the list of parent classes (i.e. to the class' `__bases__`) * it generates `__len__`, `__iter__` and `__getitem__` in order for the appropriate fields to be exposed in the dict ...
2.194981
2.702475
0.812211
# type: (...) -> Type[T] # first check that we do not conflict with other known decorators _check_known_decorators(cls, '@autodict') # perform the class mod _execute_autodict_on_class(cls, include=include, exclude=exclude, only_constructor_args=only_constructor_args, ...
def autodict_decorate(cls, # type: Type[T] include=None, # type: Union[str, Tuple[str]] exclude=None, # type: Union[str, Tuple[str]] only_constructor_args=True, # type: bool onl...
To automatically generate the appropriate methods so that objects of this class behave like a `dict`, manually, without using @autodict decorator. :param cls: the class on which to execute. Note that it won't be wrapped. :param include: a tuple of explicit attribute names to include (None means all) :p...
4.440939
5.482401
0.810035
# type: (...) -> Callable if func.__name__ not in {Mapping.__iter__.__name__, Mapping.__getitem__.__name__, Mapping.__len__.__name__}: raise ValueError('@autodict_override can only be used on one of the three Mapping methods __iter__,' '__getitem__ and __len__. Found: ' + ...
def autodict_override_decorate(func # type: Callable )
Used to decorate a function as an overridden dictionary method (such as __iter__), without using the @autodict_override annotation. :param func: the function on which to execute. Note that it won't be wrapped but simply annotated. :return:
4.607988
4.774953
0.965033
'''Map each value in the dictionary D to f(value).''' return { key:f(val) for key,val in D.iteritems() }
def map_values(f, D)
Map each value in the dictionary D to f(value).
4.417874
3.943075
1.120413
'''Produce a representation using the default repr() regardless of whether the object provides an implementation of its own.''' if isproxy(obj): return '<%s with prime_id=%d>' % (obj.__class__.__name__, obj.prime_id) else: return repr(obj)
def raw_repr(obj)
Produce a representation using the default repr() regardless of whether the object provides an implementation of its own.
7.628779
3.902781
1.954703
'''A helper function invoked on the server to tell it to import a module.''' # TODO: handle the case that the module is already loaded try: # First try to find a non-builtin, non-frozen, non-special # module using the client's search path fd, filename, info = imp.find_module(module_n...
def _load_module(module_name, path)
A helper function invoked on the server to tell it to import a module.
4.722891
3.839308
1.230141
'''Return a copy of the underlying object for which the argument is a proxy.''' assert isinstance(proxy, Proxy) return proxy.client.execute(ByValueDelegate(proxy))
def byvalue(proxy)
Return a copy of the underlying object for which the argument is a proxy.
13.065154
6.575062
1.987077
'''Import an module into an isolated context as if with "__import__('module_name')"''' sys.modules[module_name] = load_module(module_name, path=path) return __import__(module_name, fromlist=fromlist, level=level)
def import_isolated(module_name, fromlist=[], level=-1, path=None)
Import an module into an isolated context as if with "__import__('module_name')"
4.667885
2.58046
1.808935
'''Change the state of the client. This is one of the values defined in ClientStates.''' logger.debug('client changing to state=%s', ClientState.Names[state]) self._state = state
def state(self, state)
Change the state of the client. This is one of the values defined in ClientStates.
10.36564
5.460045
1.898453
'''Read an object from a channel, possibly retrying if the attempt is interrupted by a signal from the operating system.''' for i in range(num_retries): self._assert_alive() try: return self._result_channel.get() except IOError as ex: ...
def _read_result(self, num_retries)
Read an object from a channel, possibly retrying if the attempt is interrupted by a signal from the operating system.
6.207318
4.707125
1.318707
'''Stop the server process and change our state to TERMINATING. Only valid if state=READY.''' logger.debug('client.terminate() called (state=%s)', self.strstate) if self.state == ClientState.WAITING_FOR_RESULT: raise ClientStateError('terimate() called while state='+self.strstate) ...
def terminate(self)
Stop the server process and change our state to TERMINATING. Only valid if state=READY.
7.579033
6.621552
1.144601
'''Terminate this client if it has not already terminated.''' if self.state == ClientState.WAITING_FOR_RESULT: # There is an ongoing call to execute() # Not sure what to do here logger.warn('cleanup() called while state is WAITING_FOR_RESULT: ignoring') elif s...
def cleanup(self)
Terminate this client if it has not already terminated.
4.921413
4.561202
1.078973
'''Create a process in which the isolated code will be run.''' assert self._client is None logger.debug('IsolationContext[%d] starting', id(self)) # Create the queues request_queue = multiprocessing.Queue() response_queue = multiprocessing.Queue() # Launch the ...
def start(self)
Create a process in which the isolated code will be run.
4.974845
4.120302
1.207398
'''Import a module into this isolation context and return a proxy for it.''' self.ensure_started() if path is None: path = sys.path mod = self.client.call(_load_module, module_name, path) mod.__isolation_context__ = self return mod
def load_module(self, module_name, path=None)
Import a module into this isolation context and return a proxy for it.
6.143056
4.338307
1.416003
if not path.exists(dest_folder): makedirs(dest_folder) # , exist_ok=True) not python 2 compliant if test_stats.success_percentage < 50: color = 'red' elif test_stats.success_percentage < 75: color = 'orange' elif test_stats.success_percentage < 90: color = 'green' ...
def download_badge(test_stats, # type: TestStats dest_folder='reports/junit' # type: str )
Downloads the badge corresponding to the provided success percentage, from https://img.shields.io. :param test_stats: :param dest_folder: :return:
2.422428
2.456398
0.986171
return autoprops_decorate(cls, include=include, exclude=exclude)
def autoprops(include=None, # type: Union[str, Tuple[str]] exclude=None, # type: Union[str, Tuple[str]] cls=DECORATED)
A decorator to automatically generate all properties getters and setters from the class constructor. * if a @contract annotation exist on the __init__ method, mentioning a contract for a given parameter, the parameter contract will be added on the generated setter method * The user may override the generate...
7.28973
13.038162
0.559107
# type: (...) -> Type[T] # first check that we do not conflict with other known decorators _check_known_decorators(cls, '@autoprops') # perform the class mod _execute_autoprops_on_class(cls, include=include, exclude=exclude) # TODO better create a wrapper than modify the class? Probably ...
def autoprops_decorate(cls, # type: Type[T] include=None, # type: Union[str, Tuple[str]] exclude=None # type: Union[str, Tuple[str]] )
To automatically generate all properties getters and setters from the class constructor manually, without using @autoprops decorator. * if a @contract annotation exist on the __init__ method, mentioning a contract for a given parameter, the parameter contract will be added on the generated setter method ...
5.823493
6.505975
0.895099
# 0. first check parameters validate_include_exclude(include, exclude) # 1. Find the __init__ constructor signature and possible pycontracts @contract constructor = get_constructor(object_type, allow_inheritance=True) s = signature(constructor) # option a) pycontracts contracts_dict =...
def _execute_autoprops_on_class(object_type, # type: Type[T] include=None, # type: Union[str, Tuple[str]] exclude=None # type: Union[str, Tuple[str]] )
This method will automatically add one getter and one setter for each constructor argument, except for those overridden using autoprops_override_decorate(), @getter_override or @setter_override. It will add a @contract on top of all setters (generated or overridden, if they don't already have one) :param o...
4.346601
4.16152
1.044474
property_name = parameter.name # 1. create the private field name , e.g. '_foobar' private_property_name = '_' + property_name # 2. property getter (@property) - create or use overridden getter_fun = _get_getter_fun(object_type, parameter, private_property_name) # 3. property setter (@pr...
def _add_property(object_type, # type: Type[T] parameter, # type: Parameter pycontract=None, # type: Any validators=None # type: Any )
A method to dynamically add a property to a class with the optional given pycontract or validators. If the property getter and/or setter have been overridden, it is taken into account too. :param object_type: the class on which to execute. :param parameter: :param pycontract: :param validators: ...
4.170465
4.185939
0.996303
def matches_property_name(fun): return callable(fun) and hasattr(fun, annotation) \ and getattr(fun, annotation) is value return matches_property_name
def _has_annotation(annotation, value)
Returns a function that can be used as a predicate in get_members, that
9.358498
8.163032
1.146449
property_name = parameter.name # -- check overridden getter for this property name overridden_getters = getmembers(object_type, predicate=_has_annotation(__GETTER_OVERRIDE_ANNOTATION, property_name)) if len(overridden_getters) > 0: if len(overridden_getters) > 1: raise Duplic...
def _get_getter_fun(object_type, # type: Type parameter, # type: Parameter private_property_name # type: str )
Utility method to find the overridden getter function for a given property, or generate a new one :param object_type: :param property_name: :param private_property_name: :return:
4.327716
4.337162
0.997822
# the property will have the same name than the constructor argument property_name = parameter.name overridden_setters = getmembers(object_type, _has_annotation(__SETTER_OVERRIDE_ANNOTATION, property_name)) if len(overridden_setters) > 0: # --check that we only have one if len(ove...
def _get_setter_fun(object_type, # type: Type parameter, # type: Parameter private_property_name # type: str )
Utility method to find the overridden setter function for a given property, or generate a new one :param object_type: :param property_name: :param property_type: :param private_property_name: :return:
4.407262
4.378376
1.006597
return autoprops_override_decorate(f, attribute=attribute, is_getter=True)
def getter_override(attribute=None, # type: str f=DECORATED )
A decorator to indicate an overridden getter for a given attribute. If the attribute name is None, the function name will be used as the attribute name. :param attribute: the attribute name for which the decorated function is an overridden getter :return:
16.32107
34.88512
0.467852
return autoprops_override_decorate(f, attribute=attribute, is_getter=False)
def setter_override(attribute=None, # type: str f=DECORATED )
A decorator to indicate an overridden setter for a given attribute. If the attribute name is None, the function name will be used as the attribute name. The @contract will still be dynamically added. :param attribute: the attribute name for which the decorated function is an overridden setter :return:
17.658104
34.472855
0.512232
# type: (...) -> Callable # Simply annotate the fact that this is a function attr_name = attribute or func.__name__ if is_getter: if hasattr(func, __GETTER_OVERRIDE_ANNOTATION): raise DuplicateOverrideError('Getter is overridden twice for attribute name : ' + attr_name) ...
def autoprops_override_decorate(func, # type: Callable attribute=None, # type: str is_getter=True # type: bool )
Used to decorate a function as an overridden getter or setter, without using the @getter_override or @setter_override annotations. If the overridden setter has no @contract, the contract will still be dynamically added. Note: this should be executed BEFORE @autoprops or autoprops_decorate(). :param func: ...
2.618662
2.749561
0.952393
# 1- AUTHENTICATION if user is not None and secret is None: # using username and password # validate('user', user, instance_of=str) assert isinstance(user, str) # validate('pwd', pwd, instance_of=str) assert isinstance(pwd, str) g = Github(user, pwd) elif...
def create_or_update_release(user, pwd, secret, repo_slug, changelog_file, doc_url, data_file, tag)
Creates or updates (TODO) a github release corresponding to git tag <TAG>.
2.81623
2.807973
1.00294
if include is not None and exclude is not None: raise ValueError("Only one of 'include' or 'exclude' argument should be provided.") validate('include', include, instance_of=(str, Sequence), enforce_not_none=False) validate('exclude', exclude, instance_of=(str, Sequence), enforce_not_none=False)
def validate_include_exclude(include, exclude)
Common validator for include and exclude arguments :param include: :param exclude: :return:
2.998163
3.209198
0.93424
if include is not None and exclude is not None: raise ValueError('Only one of \'include\' or \'exclude\' argument should be provided.') # win time by not doing this # check_var(include, var_name='include', var_types=[str, tuple], enforce_not_none=False) # check_var(exclude, var_name='excl...
def is_attr_selected(attr_name, # type: str include=None, # type: Union[str, Tuple[str]] exclude=None # type: Union[str, Tuple[str]] )
decide whether an action has to be performed on the attribute or not, based on its name
3.608013
3.46001
1.042775
if allow_inheritance: return typ.__init__ else: # check that the constructor is really defined here if '__init__' in typ.__dict__: return typ.__init__ else: raise Exception('No explicit constructor was found for class ' + str(typ))
def get_constructor(typ, allow_inheritance=False # type: bool )
Utility method to return the unique constructor (__init__) of a type :param typ: a type :param allow_inheritance: if True, the constructor will be returned even if it is not defined in this class (inherited). By default this is set to False: an exception is raised when no constructor is explicitly defined ...
4.292213
3.918195
1.095457
# type: (...) -> bool for member in typ.__dict__.values(): if hasattr(member, '__enforcer__'): raise AutoclassDecorationException('It seems that @runtime_validation decorator was applied to type <' + str(typ) + '> BEFORE ' + calling_decorat...
def _check_known_decorators(typ, calling_decorator # type: str )
Checks that a given type is not already decorated by known decorators that may cause trouble. If so, it raises an Exception :return:
7.377251
8.327147
0.885928
if this_class_only: return method_name in vars(object_type) # or object_type.__dict__ else: try: method = getattr(object_type, method_name) except AttributeError: return False else: return method is not None and method is not getattr(obje...
def method_already_there(object_type, method_name, this_class_only=False)
Returns True if method `method_name` is already implemented by object_type, that is, its implementation differs from the one in `object`. :param object_type: :param method_name: :param this_class_only: :return:
2.701776
2.733407
0.988428
return autoargs_decorate(f, include=include, exclude=exclude)
def autoargs(include=None, # type: Union[str, Tuple[str]] exclude=None, # type: Union[str, Tuple[str]] f=DECORATED )
Defines a decorator with parameters, to automatically assign the inputs of a function to self PRIOR to executing the function. In other words: ``` @autoargs def myfunc(a): print('hello') ``` will create the equivalent of ``` def myfunc(a): self.a = a ...
6.422693
9.286619
0.691607
# type: (...) -> Callable # (0) first check parameters validate_include_exclude(include, exclude) # (1) then retrieve function signature # attrs, varargs, varkw, defaults = getargspec(func) func_sig = signature(func) # check that include/exclude dont contain names that are incorrect ...
def autoargs_decorate(func, # type: Callable include=None, # type: Union[str, Tuple[str]] exclude=None # type: Union[str, Tuple[str]] )
Defines a decorator with parameters, to automatically assign the inputs of a function to self PRIOR to executing the function. This is the inline way to apply the decorator ``` myfunc2 = autoargs_decorate(myfunc) ``` See autoargs for details. :param func: the function to wrap ...
5.089844
5.140944
0.99006
self.__items.append((key, value)) try: dict_getitem(self, key).append(value) except KeyError: dict_setitem(self, key, [value])
def append(self, key, value)
Adds a (name, value) pair, doesn't overwrite the value if it already exists.
3.958762
3.98728
0.992848
if len(args) > 1: raise TypeError('expected at most 1 arguments, got %d' % len(args)) iterable = args[0] if args else None if iterable: if isinstance(iterable, Mapping) or hasattr(iterable, 'items'): for key, value in iterable.items(): ...
def extend(self, *args, **kwargs)
Add key value pairs for an iterable.
1.762839
1.641959
1.073619
def check_func(self, key, new_item, instance=0): if key not in self.keys(): raise KeyError("%s not a key in label" % (key)) if not isinstance(new_item, (list, OrderedMultiDict)): raise TypeError("The new item must be a list or PVLModule") ...
def __insert_wrapper(func)
Make sure the arguments given to the insert methods are correct
4.211362
4.011349
1.049862
if instance == 0: # Index method will return the first occurence of the key index = self.keys().index(key) else: occurrence = -1 for index, k in enumerate(self.keys()): if k == key: occurrence += 1 ...
def _get_index_for_insert(self, key, instance)
Get the index of the key to insert before or after
4.453219
4.228964
1.053028
index = self._get_index_for_insert(key, instance) index = index + 1 if is_after else index self.__items = self.__items[:index] + new_item + self.__items[index:] # Make sure indexing works with new items for new_key, new_value in new_item: if new_key in self: ...
def _insert_item(self, key, new_item, instance, is_after)
Insert a new item before or after another item
2.963186
2.917529
1.015649
self._insert_item(key, new_item, instance, True)
def insert_after(self, key, new_item, instance=0)
Insert an item after a key
4.843205
4.458274
1.086341
self._insert_item(key, new_item, instance, False)
def insert_before(self, key, new_item, instance=0)
Insert an item before a key
4.949448
4.738151
1.044595
buf = self._read_buf pos = self._read_pos end = pos + n if end <= len(buf): # Fast path: the data to read is fully buffered. self._read_pos += n return self._update_pos(buf[pos:end]) # Slow path: read from the stream until enough byt...
def read(self, n)
Read n bytes. Returns exactly n bytes of data unless the underlying raw IO stream reaches EOF.
3.533336
3.466053
1.019412
pos = self._read_pos self._read_pos = min(len(self.raw), pos + n) return self.raw[pos:self._read_pos]
def read(self, n)
Read n bytes. Returns exactly n bytes of data unless the underlying raw IO stream reaches EOF.
3.227504
2.922743
1.104272
pos = self._read_pos end = pos + n return self.raw[pos:end]
def peek(self, n)
Returns buffered bytes without advancing the position. The argument indicates a desired minimal number of bytes; we do at most one raw read to satisfy it. We never return more than self.buffer_size.
6.457321
5.767959
1.119516
statements = [] while 1: self.skip_whitespace_or_comment(stream) if has_end(stream): return statements statement = self.parse_statement(stream) if isinstance(statement, EmptyValueAtLine): if len(statements) == 0: ...
def parse_block(self, stream, has_end)
PVLModuleContents ::= (Statement | WSC)* EndStatement? AggrObject ::= BeginObjectStmt AggrContents EndObjectStmt AggrGroup ::= BeginGroupStmt AggrContents EndGroupStmt AggrContents := WSC Statement (WSC | Statement)*
3.37974
3.453728
0.978577
self.skip_whitespace_or_comment(stream) self.optional(stream, self.statement_delimiter)
def skip_statement_delimiter(self, stream)
Ensure that a Statement Delimiter consists of one semicolon, optionally preceded by multiple White Spaces and/or Comments, OR one or more Comments and/or White Space sequences. StatementDelim ::= WSC (SemiColon | WhiteSpace | Comment) | EndProvidedOctetSeq
6.323692
4.837947
1.307102
if self.has_group(stream): return self.parse_group(stream) if self.has_object(stream): return self.parse_object(stream) if self.has_assignment(stream): return self.parse_assignment(stream) if self.has_assignment_symbol(stream): ...
def parse_statement(self, stream)
Statement ::= AggrGroup | AggrObject | AssignmentStmt
3.294953
3.042736
1.082892
for token in self.end_tokens: if not self.has_next(token, stream): continue offset = len(token) if self.has_eof(stream, offset): return True if self.has_whitespace(stream, offset): return True ...
def has_end(self, stream)
EndStatement ::= EndKeyword (SemiColon | WhiteSpace | Comment | EndProvidedOctetSeq)
3.213764
2.959201
1.086024
self.expect_in(stream, self.begin_group_tokens) self.ensure_assignment(stream) name = self.next_token(stream) self.skip_statement_delimiter(stream) statements = self.parse_block(stream, self.has_end_group) self.expect_in(stream, self.end_group_tokens) ...
def parse_group(self, stream)
Block Name must match Block Name in paired End Group Statement if Block Name is present in End Group Statement. BeginGroupStmt ::= BeginGroupKeywd WSC AssignmentSymbol WSC BlockName StatementDelim
4.761158
4.640353
1.026034
self.expect_in(stream, self.begin_object_tokens) self.ensure_assignment(stream) name = self.next_token(stream) self.skip_statement_delimiter(stream) statements = self.parse_block(stream, self.has_end_object) self.expect_in(stream, self.end_object_tokens) ...
def parse_object(self, stream)
Block Name must match Block Name in paired End Object Statement if Block Name is present in End Object Statement StatementDelim. BeginObjectStmt ::= BeginObjectKeywd WSC AssignmentSymbol WSC BlockName StatementDelim
4.881321
4.752299
1.027149
lineno = stream.lineno name = self.next_token(stream) self.ensure_assignment(stream) at_an_end = any(( self.has_end_group(stream), self.has_end_object(stream), self.has_end(stream), self.has_next(self.statement_delimiter, stream, 0...
def parse_assignment(self, stream)
AssignmentStmt ::= Name WSC AssignmentSymbol WSC Value StatementDelim
5.142797
4.927813
1.043627
if self.has_sequence(stream): value = self.parse_sequence(stream) elif self.has_set(stream): value = self.parse_set(stream) else: value = self.parse_simple_value(stream) self.skip_whitespace_or_comment(stream) if self.has_units(strea...
def parse_value(self, stream)
Value ::= (SimpleValue | Set | Sequence) WSC UnitsExpression?
2.707927
2.13478
1.268481
values = [] self.expect(stream, start) self.skip_whitespace_or_comment(stream) if self.has_next(end, stream): self.expect(stream, end) return values while 1: self.skip_whitespace_or_comment(stream) values.append(self.par...
def parse_iterable(self, stream, start, end)
Sequence ::= SequenceStart WSC SequenceValue? WSC SequenceEnd Set := SetStart WSC SequenceValue? WSC SetEnd SequenceValue ::= Value (WSC SeparatorSymbol WSC Value)*
2.46241
2.428566
1.013936
value = b'' self.expect(stream, self.begin_units) while not self.has_next(self.end_units, stream): if self.has_eof(stream): self.raise_unexpected_eof(stream) value += stream.read(1) self.expect(stream, self.end_units) return valu...
def parse_units(self, stream)
UnitsExpression ::= UnitsStart WhiteSpace* UnitsValue WhiteSpace* UnitsEnd
3.863584
3.770118
1.024791
if self.has_quoted_string(stream): return self.parse_quoted_string(stream) if self.has_binary_number(stream): return self.parse_binary_number(stream) if self.has_octal_number(stream): return self.parse_octal_number(stream) if self.has_decim...
def parse_simple_value(self, stream)
SimpleValue ::= Integer | FloatingPoint | Exponential | BinaryNum | OctalNum | HexadecimalNum | DateTimeValue | QuotedString | UnquotedString
2.202741
2.079443
1.059294
value = b'' sign = self.parse_sign(stream) self.expect(stream, b(str(radix)) + self.radix_symbole) sign *= self.parse_sign(stream) while not self.has_next(self.radix_symbole, stream): next = stream.read(1) if not next: self.raise_...
def parse_radix(self, radix, chars, stream)
BinaryNum ::= [+-]? '2' RadixSymbol [0-1]+ RadixSymbol OctalChar ::= [+-]? '8' RadixSymbol [0-7]+ RadixSymbol HexadecimalNum ::= [+-]? '16' RadixSymbol [0-9a-zA-Z]+ RadixSymbol
3.155536
3.163239
0.997565
assert field is not None, "The field parameter must be passed to the 'varchar' method." max_length = field.max_length def source(): length = random.choice(range(1, max_length + 1)) return "".join(random.choice(general_chars) for i in xrange(length)) retu...
def varchar(self, field=None)
Returns a chunk of text, of maximum length 'max_length'
5.177127
4.731478
1.094188
return self.get_allowed_value(lambda: self.faker.bothify(pattern), field)
def simple_pattern(self, pattern, field=None)
Use a simple pattern to make the field - # is replaced with a random number, ? with a random letter.
16.451748
10.547418
1.559789
if val is None: def source(): tzinfo = get_default_timezone() if settings.USE_TZ else None return datetime.fromtimestamp(randrange(1, 2100000000), tzinfo) else: def source(): tz...
def datetime(self, field=None, val=None)
Returns a random datetime. If 'val' is passed, a datetime within two years of that date will be returned.
2.947306
2.842916
1.036719
return self.datetime(field=field, val=val).date()
def date(self, field=None, val=None)
Like datetime, but truncated to be a date only
5.644554
4.073215
1.385773
if val == '': return '' if val is not None: def generate(length): # Get lorem ipsum of a specific length. collect = "" while len(collect) < length: collect += ' %s' % self.faker.sentence() ...
def lorem(self, field=None, val=None)
Returns lorem ipsum text. If val is provided, the lorem ipsum text will be the same length as the original text, and with the same pattern of line breaks.
5.72912
5.289328
1.083147
lorem_text = self.lorem(field, val) max_length = getattr(field, 'max_length', None) suffix_str = str(self.unique_suffixes[field]) unique_text = lorem_text + suffix_str if max_length is not None: # take the last max_length chars unique_text = uniq...
def unique_lorem(self, field=None, val=None)
Returns lorem ipsum text guaranteed to be unique. First uses lorem function then adds a unique integer suffix.
3.374242
2.982387
1.13139
for attname, field, replacer in self.replacers: currentval = getattr(obj, attname) replacement = replacer(self, obj, field, currentval) setattr(obj, attname, replacement)
def alter_object(self, obj)
Alters all the attributes in an individual object. If it returns False, the object will not be saved
4.417346
5.657707
0.780766
return anon.faker.uuid(field=field)
def uuid(anon, obj, field, val)
Returns a random uuid string
17.296112
13.706557
1.261886
return anon.faker.varchar(field=field)
def varchar(anon, obj, field, val)
Returns random data for a varchar field.
22.895212
15.764924
1.452288
return anon.faker.bool(field=field)
def bool(anon, obj, field, val)
Returns a random boolean value (True/False)
20.608763
10.82688
1.903481
return anon.faker.integer(field=field)
def integer(anon, obj, field, val)
Returns a random integer (for a Django IntegerField)
19.942253
11.217731
1.777744
return anon.faker.positive_integer(field=field)
def positive_integer(anon, obj, field, val)
Returns a random positive integer (for a Django PositiveIntegerField)
16.560415
10.419806
1.589321
return anon.faker.small_integer(field=field)
def small_integer(anon, obj, field, val)
Returns a random small integer (for a Django SmallIntegerField)
13.121313
9.030035
1.453074
return anon.faker.positive_small_integer(field=field)
def positive_small_integer(anon, obj, field, val)
Returns a positive small random integer (for a Django PositiveSmallIntegerField)
11.221386
7.55276
1.485733