kwargs = self.formset_kwargs.copy()<EOL>kwargs.update({<EOL>'<STR_LIT>': self.get_initial(),<EOL>'<STR_LIT>': self.get_prefix(),<EOL>})<EOL>if self.request.method in ('<STR_LIT:POST>', '<STR_LIT>'):<EOL><INDENT>kwargs.update({<EOL>'<STR_LIT:data>': self.request.POST.copy(),<EOL>'<STR_LIT>': self.request.FILES,<EOL>})<EOL><DEDENT>return kwargs<EOL>

Returns the keyword arguments for instantiating the formset.


<EOL>for attr in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if hasattr(self, attr):<EOL><INDENT>klass = type(self).__name__<EOL>raise DeprecationWarning(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(klass, attr)<EOL>)<EOL><DEDENT><DEDENT>kwargs = self.factory_kwargs.copy()<EOL>if self.get_formset_class():<EOL><INDENT>kwargs['<STR_LIT>'] = self.get_formset_class()<EOL><DEDENT>return kwargs<EOL>

Returns the keyword arguments for calling the formset factory


if self.success_url:<EOL><INDENT>url = self.success_url<EOL><DEDENT>else:<EOL><INDENT>url = self.request.get_full_path()<EOL><DEDENT>return url<EOL>


return HttpResponseRedirect(self.get_success_url())<EOL>

If the formset is valid redirect to the supplied URL


return self.render_to_response(self.get_context_data(formset=formset))<EOL>

If the formset is invalid, re-render the context data with the
data-filled formset and errors.


kwargs = super(ModelFormSetMixin, self).get_formset_kwargs()<EOL>kwargs['<STR_LIT>'] = self.get_queryset()<EOL>return kwargs<EOL>


kwargs = super(ModelFormSetMixin, self).get_factory_kwargs()<EOL>kwargs.setdefault('<STR_LIT>', self.fields)<EOL>kwargs.setdefault('<STR_LIT>', self.exclude)<EOL>if self.get_form_class():<EOL><INDENT>kwargs['<STR_LIT>'] = self.get_form_class()<EOL><DEDENT>return kwargs<EOL>


return modelformset_factory(self.model, **self.get_factory_kwargs())<EOL>

Returns the formset class from the model formset factory


self.object_list = formset.save()<EOL>return super(ModelFormSetMixin, self).formset_valid(formset)<EOL>

If the formset is valid, save the associated models.


return self.inline_model<EOL>

Returns the inline model to use with the inline formset


<EOL>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>klass = type(self).__name__<EOL>raise DeprecationWarning(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(klass)<EOL>)<EOL><DEDENT>kwargs = super(BaseInlineFormSetFactory, self).get_formset_kwargs()<EOL>kwargs['<STR_LIT>'] = self.object<EOL>return kwargs<EOL>


kwargs = super(BaseInlineFormSetFactory, self).get_factory_kwargs()<EOL>kwargs.setdefault('<STR_LIT>', self.fields)<EOL>kwargs.setdefault('<STR_LIT>', self.exclude)<EOL>if self.get_form_class():<EOL><INDENT>kwargs['<STR_LIT>'] = self.get_form_class()<EOL><DEDENT>return kwargs<EOL>


return inlineformset_factory(self.model, self.get_inline_model(), **self.get_factory_kwargs())<EOL>

Returns the formset class from the inline formset factory


def get(self, request, *args, **kwargs):

formset = self.construct_formset()<EOL>return self.render_to_response(self.get_context_data(formset=formset))<EOL>

Handles GET requests and instantiates a blank version of the formset.


def post(self, request, *args, **kwargs):

formset = self.construct_formset()<EOL>if formset.is_valid():<EOL><INDENT>return self.formset_valid(formset)<EOL><DEDENT>else:<EOL><INDENT>return self.formset_invalid(formset)<EOL><DEDENT>

Handles POST requests, instantiating a formset instance with the passed
POST variables and then checked for validity.


for n in range(int((end_date - start_date).days + <NUM_LIT:1>)):<EOL><INDENT>yield start_date + datetime.timedelta(n)<EOL><DEDENT>

Returns an iterator of dates between two provided ones


return self.end_date_field<EOL>

Returns the model field to use for end dates


obj_date = getattr(obj, self.get_date_field())<EOL>try:<EOL><INDENT>obj_date = obj_date.date()<EOL><DEDENT>except AttributeError:<EOL><INDENT>pass<EOL><DEDENT>return obj_date<EOL>

Returns the start date for a model instance


obj_date = getattr(obj, self.get_end_date_field())<EOL>try:<EOL><INDENT>obj_date = obj_date.date()<EOL><DEDENT>except AttributeError:<EOL><INDENT>pass<EOL><DEDENT>return obj_date<EOL>

Returns the end date for a model instance


if self.first_of_week is None:<EOL><INDENT>raise ImproperlyConfigured("<STR_LIT>" % self.__class__.__name__)<EOL><DEDENT>if self.first_of_week not in range(<NUM_LIT:7>):<EOL><INDENT>raise ImproperlyConfigured("<STR_LIT>" % self.__class__.__name__)<EOL><DEDENT>return self.first_of_week<EOL>

Returns an integer representing the first day of the week.

0 represents Monday, 6 represents Sunday.


qs = super(BaseCalendarMonthView, self).get_queryset()<EOL>year = self.get_year()<EOL>month = self.get_month()<EOL>date_field = self.get_date_field()<EOL>end_date_field = self.get_end_date_field()<EOL>date = _date_from_string(year, self.get_year_format(),<EOL>month, self.get_month_format())<EOL>since = date<EOL>until = self.get_next_month(date)<EOL>if since.weekday() != self.get_first_of_week():<EOL><INDENT>diff = math.fabs(since.weekday() - self.get_first_of_week())<EOL>since = since - datetime.timedelta(days=diff)<EOL><DEDENT>if until.weekday() != ((self.get_first_of_week() + <NUM_LIT:6>) % <NUM_LIT:7>):<EOL><INDENT>diff = math.fabs(((self.get_first_of_week() + <NUM_LIT:6>) % <NUM_LIT:7>) - until.weekday())<EOL>until = until + datetime.timedelta(days=diff)<EOL><DEDENT>if end_date_field:<EOL><INDENT>predicate1 = Q(**{<EOL>'<STR_LIT>' % date_field: since,<EOL>end_date_field: None<EOL>})<EOL>predicate2 = Q(**{<EOL>'<STR_LIT>' % date_field: since,<EOL>'<STR_LIT>' % end_date_field: until<EOL>})<EOL>predicate3 = Q(**{<EOL>'<STR_LIT>' % date_field: since,<EOL>'<STR_LIT>' % end_date_field: since,<EOL>'<STR_LIT>' % end_date_field: until<EOL>})<EOL>predicate4 = Q(**{<EOL>'<STR_LIT>' % date_field: since,<EOL>'<STR_LIT>' % date_field: until,<EOL>'<STR_LIT>' % end_date_field: until<EOL>})<EOL>predicate5 = Q(**{<EOL>'<STR_LIT>' % date_field: since,<EOL>'<STR_LIT>' % end_date_field: until<EOL>})<EOL>return qs.filter(predicate1 | predicate2 | predicate3 | predicate4 | predicate5)<EOL><DEDENT>return qs.filter(**{<EOL>'<STR_LIT>' % date_field: since<EOL>})<EOL>

Returns a queryset of models for the month requested


data = super(BaseCalendarMonthView, self).get_context_data(**kwargs)<EOL>year = self.get_year()<EOL>month = self.get_month()<EOL>date = _date_from_string(year, self.get_year_format(),<EOL>month, self.get_month_format())<EOL>cal = Calendar(self.get_first_of_week())<EOL>month_calendar = []<EOL>now = datetime.datetime.utcnow()<EOL>date_lists = defaultdict(list)<EOL>multidate_objs = []<EOL>for obj in data['<STR_LIT>']:<EOL><INDENT>obj_date = self.get_start_date(obj)<EOL>end_date_field = self.get_end_date_field()<EOL>if end_date_field:<EOL><INDENT>end_date = self.get_end_date(obj)<EOL>if end_date and end_date != obj_date:<EOL><INDENT>multidate_objs.append({<EOL>'<STR_LIT>': obj,<EOL>'<STR_LIT>': [x for x in daterange(obj_date, end_date)]<EOL>})<EOL>continue  <EOL><DEDENT><DEDENT>date_lists[obj_date].append(obj)<EOL><DEDENT>for week in cal.monthdatescalendar(date.year, date.month):<EOL><INDENT>week_range = set(daterange(week[<NUM_LIT:0>], week[<NUM_LIT:6>]))<EOL>week_events = []<EOL>for val in multidate_objs:<EOL><INDENT>intersect_length = len(week_range.intersection(val['<STR_LIT>']))<EOL>if intersect_length:<EOL><INDENT>slot = <NUM_LIT:1><EOL>width = intersect_length  <EOL>nowrap_previous = True  <EOL>nowrap_next = True  <EOL>if val['<STR_LIT>'][<NUM_LIT:0>] >= week[<NUM_LIT:0>]:<EOL><INDENT>slot = <NUM_LIT:1> + (val['<STR_LIT>'][<NUM_LIT:0>] - week[<NUM_LIT:0>]).days<EOL><DEDENT>else:<EOL><INDENT>nowrap_previous = False<EOL><DEDENT>if val['<STR_LIT>'][-<NUM_LIT:1>] > week[<NUM_LIT:6>]:<EOL><INDENT>nowrap_next = False<EOL><DEDENT>week_events.append({<EOL>'<STR_LIT>': val['<STR_LIT>'],<EOL>'<STR_LIT>': slot,<EOL>'<STR_LIT:width>': width,<EOL>'<STR_LIT>': nowrap_previous,<EOL>'<STR_LIT>': nowrap_next,<EOL>})<EOL><DEDENT><DEDENT>week_calendar = {<EOL>'<STR_LIT>': week_events,<EOL>'<STR_LIT>': [],<EOL>}<EOL>for day in week:<EOL><INDENT>week_calendar['<STR_LIT>'].append({<EOL>'<STR_LIT>': day,<EOL>'<STR_LIT>': date_lists[day],<EOL>'<STR_LIT>': day == now.date(),<EOL>'<STR_LIT>': day.month == date.month,<EOL>})<EOL><DEDENT>month_calendar.append(week_calendar)<EOL><DEDENT>data['<STR_LIT>'] = month_calendar<EOL>data['<STR_LIT>'] = [DAYS[x] for x in cal.iterweekdays()]<EOL>data['<STR_LIT>'] = date<EOL>data['<STR_LIT>'] = self.get_next_month(date)<EOL>data['<STR_LIT>'] = self.get_previous_month(date)<EOL>return data<EOL>

Injects variables necessary for rendering the calendar into the context.

Variables added are: `calendar`, `weekdays`, `month`, `next_month` and `previous_month`.


formset = super(InlineFormSetFactory, self).construct_formset()<EOL>formset.model = self.inline_model<EOL>return formset<EOL>

Overrides construct_formset to attach the model class as
an attribute of the returned formset instance.


return self.inlines<EOL>


response = self.form_valid(form)<EOL>for formset in inlines:<EOL><INDENT>formset.save()<EOL><DEDENT>return response<EOL>

If the form and formsets are valid, save the associated models.


return self.render_to_response(self.get_context_data(form=form, inlines=inlines))<EOL>

If the form or formsets are invalid, re-render the context data with the
data-filled form and formsets and errors.


inline_formsets = []<EOL>for inline_class in self.get_inlines():<EOL><INDENT>inline_instance = inline_class(self.model, self.request, self.object, self.kwargs, self)<EOL>inline_formset = inline_instance.construct_formset()<EOL>inline_formsets.append(inline_formset)<EOL><DEDENT>return inline_formsets<EOL>


form_class = self.get_form_class()<EOL>form = self.get_form(form_class)<EOL>inlines = self.construct_inlines()<EOL>return self.render_to_response(self.get_context_data(form=form, inlines=inlines, **kwargs))<EOL>

Handles GET requests and instantiates a blank version of the form and formsets.


form_class = self.get_form_class()<EOL>form = self.get_form(form_class)<EOL>if form.is_valid():<EOL><INDENT>self.object = form.save(commit=False)<EOL>form_validated = True<EOL><DEDENT>else:<EOL><INDENT>form_validated = False<EOL><DEDENT>inlines = self.construct_inlines()<EOL>if all_valid(inlines) and form_validated:<EOL><INDENT>return self.forms_valid(form, inlines)<EOL><DEDENT>return self.forms_invalid(form, inlines)<EOL>

Handles POST requests, instantiating a form and formset instances with the passed
POST variables and then checked for validity.


return self.inlines_names<EOL>

Returns a list of names of context variables for each inline in `inlines`.


context = {}<EOL>inlines_names = self.get_inlines_names()<EOL>if inlines_names:<EOL><INDENT>context.update(zip(inlines_names, kwargs.get('<STR_LIT>', [])))<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>context[inlines_names[<NUM_LIT:0>]] = kwargs['<STR_LIT>']<EOL><DEDENT><DEDENT>context.update(kwargs)<EOL>return super(NamedFormsetsMixin, self).get_context_data(**context)<EOL>

If `inlines_names` has been defined, add each formset to the context under
its corresponding entry in `inlines_names`


return self.response<EOL>


try:<EOL><INDENT>self.response.close()<EOL>self.logger.debug("<STR_LIT>")<EOL><DEDENT>except Exception as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>


def execute(self, command, timeout=None):

try:<EOL><INDENT>self.channel = self.ssh.get_transport().open_session()<EOL><DEDENT>except paramiko.SSHException as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>try:<EOL><INDENT>self.channel.settimeout(self.args.timeout if not timeout else timeout)<EOL><DEDENT>except socket.timeout as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>try:<EOL><INDENT>self.logger.debug("<STR_LIT>".format(command))<EOL>self.channel.exec_command(command)<EOL><DEDENT>except paramiko.SSHException as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>try:<EOL><INDENT>self.stdin = self.channel.makefile('<STR_LIT:wb>', -<NUM_LIT:1>)<EOL>self.stderr = map(string.strip, self.channel.makefile_stderr('<STR_LIT:rb>', -<NUM_LIT:1>).readlines())<EOL>self.stdout = map(string.strip, self.channel.makefile('<STR_LIT:rb>', -<NUM_LIT:1>).readlines())<EOL><DEDENT>except Exception as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>try:<EOL><INDENT>self.status = self.channel.recv_exit_status()<EOL><DEDENT>except paramiko.SSHException as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>else:<EOL><INDENT>if self.status != <NUM_LIT:0>:<EOL><INDENT>self.unknown("<STR_LIT>" % (self.status, self.errors))<EOL><DEDENT>else:<EOL><INDENT>return self.stdout<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>self.logger.debug("<STR_LIT>")<EOL><DEDENT>


try:<EOL><INDENT>self.ssh.close()<EOL>self.logger.debug("<STR_LIT>")<EOL><DEDENT>except paramiko.SSHException as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>


try:<EOL><INDENT>self.__wql = ['<STR_LIT>', '<STR_LIT>',<EOL>self.args.domain + '<STR_LIT:\\>' + self.args.user + '<STR_LIT:%>' + self.args.password,<EOL>'<STR_LIT>' + self.args.host,<EOL>'<STR_LIT>', self.args.namespace,<EOL>'<STR_LIT>', self.args.delimiter,<EOL>wql]<EOL>self.logger.debug("<STR_LIT>".format(self.__wql))<EOL>self.__output = subprocess.check_output(self.__wql)<EOL>self.logger.debug("<STR_LIT>".format(self.__output))<EOL>self.logger.debug("<STR_LIT>")<EOL>self.__wmi_output = self.__output.splitlines()[<NUM_LIT:1>:]<EOL>self.logger.debug("<STR_LIT>".format(self.__wmi_output))<EOL>self.__csv_header = csv.DictReader(self.__wmi_output, delimiter='<STR_LIT:|>')<EOL>self.logger.debug("<STR_LIT>".format(self.__csv_header))<EOL>return list(self.__csv_header)<EOL><DEDENT>except subprocess.CalledProcessError as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>


return self.ftp<EOL>


try:<EOL><INDENT>self.ftp.quit()<EOL>self.logger.debug("<STR_LIT>")<EOL><DEDENT>except ftplib.Error as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>


try:<EOL><INDENT>self.conn.close()<EOL>self.logger.debug("<STR_LIT>")<EOL><DEDENT>except pymysql.Error as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>


try:<EOL><INDENT>self.conn.close()<EOL>self.logger.debug("<STR_LIT>")<EOL><DEDENT>except pymssql.Error as e:<EOL><INDENT>self.unknown("<STR_LIT>" % e)<EOL><DEDENT>


extend = os.path.splitext(readme)[<NUM_LIT:1>]<EOL>if (extend == '<STR_LIT>'):<EOL><INDENT>import codecs<EOL>return codecs.open(readme, '<STR_LIT:r>', '<STR_LIT:utf-8>').read()<EOL><DEDENT>elif (extend == '<STR_LIT>'):<EOL><INDENT>import pypandoc<EOL>return pypandoc.convert(readme, '<STR_LIT>')<EOL><DEDENT>


plugin = Register()<EOL>if plugin.args.option == '<STR_LIT>':<EOL><INDENT>plugin.command_handle()<EOL><DEDENT>else:<EOL><INDENT>plugin.unknown("<STR_LIT>")<EOL><DEDENT>

Register your own mode and handle method here.


self.__results = self.execute(self.args.command)<EOL>self.close()<EOL>self.logger.debug("<STR_LIT>".format(self.__results))<EOL>if not self.__results:<EOL><INDENT>self.unknown("<STR_LIT>".format(self.args.command))<EOL><DEDENT>if len(self.__results) != <NUM_LIT:1>:<EOL><INDENT>self.unknown(<EOL>"<STR_LIT>".format(<EOL>self.args.command))<EOL><DEDENT>self.__result = int(self.__results[<NUM_LIT:0>])<EOL>self.logger.debug("<STR_LIT>".format(self.__result))<EOL>if not isinstance(self.__result, (int, long)):<EOL><INDENT>self.unknown(<EOL>"<STR_LIT>".format(<EOL>self.args.command))<EOL><DEDENT>status = self.ok<EOL>if self.__result > self.args.warning:<EOL><INDENT>status = self.warning<EOL><DEDENT>if self.__result > self.args.critical:<EOL><INDENT>status = self.critical<EOL><DEDENT>self.shortoutput = "<STR_LIT>".format(<EOL>self.args.command, self.__result)<EOL>[self.longoutput.append(line)<EOL>for line in self.__results if self.__results]<EOL>self.perfdata.append("<STR_LIT>".format(<EOL>crit=self.args.critical,<EOL>warn=self.args.warning,<EOL>result=self.__result,<EOL>command=self.args.command))<EOL>status(self.output(long_output_limit=None))<EOL>self.logger.debug("<STR_LIT>")<EOL>


plugin = Register()<EOL>if plugin.args.option == '<STR_LIT>':<EOL><INDENT>plugin.sql_handle()<EOL><DEDENT>else:<EOL><INDENT>plugin.unknown("<STR_LIT>")<EOL><DEDENT>


plugin = Register()<EOL>if plugin.args.option == '<STR_LIT>':<EOL><INDENT>plugin.filenumber_handle()<EOL><DEDENT>elif plugin.args.option == '<STR_LIT>':<EOL><INDENT>plugin.fileage_handle()<EOL><DEDENT>elif plugin.args.option == '<STR_LIT>':<EOL><INDENT>plugin.sqlserverlocks_handle()<EOL><DEDENT>else:<EOL><INDENT>plugin.unknown("<STR_LIT>")<EOL><DEDENT>


self.file_list = []<EOL>self.count = <NUM_LIT:0><EOL>status = self.ok<EOL>if self.args.recursion:<EOL><INDENT>self.__result, self.__file_list = self.__get_folder(self.args.path)<EOL><DEDENT>else:<EOL><INDENT>self.__result, self.__file_list = self.__get_file(self.args.path)<EOL><DEDENT>if self.__result > self.args.critical:<EOL><INDENT>status = self.critical<EOL><DEDENT>elif self.__result > self.args.warning:<EOL><INDENT>status = self.warning<EOL><DEDENT>else:<EOL><INDENT>status = self.ok<EOL><DEDENT>self.shortoutput = "<STR_LIT>".format(self.__result,<EOL>self.args.path)<EOL>self.logger.debug("<STR_LIT>".format(self.__file_list))<EOL>[self.longoutput.append(file_data.get('<STR_LIT:Name>'))<EOL>for file_data in self.__file_list]<EOL>self.perfdata.append("<STR_LIT>".format(<EOL>crit=self.args.critical,<EOL>warn=self.args.warning,<EOL>result=self.__result,<EOL>path=self.args.path))<EOL>status(self.output(long_output_limit=None))<EOL>self.logger.debug("<STR_LIT>")<EOL>


self.wql_time = "<STR_LIT>"<EOL>self.current_time = self.query(self.wql_time)<EOL>self.current_time_string = str(<EOL>self.current_time[<NUM_LIT:0>].get('<STR_LIT>').split('<STR_LIT:.>')[<NUM_LIT:0>])<EOL>self.current_time_format = datetime.datetime.strptime(<EOL>self.current_time_string, '<STR_LIT>')<EOL>return self.current_time_format<EOL>


self.file_list = []<EOL>self.ok_file = []<EOL>self.warn_file = []<EOL>self.crit_file = []<EOL>status = self.ok<EOL>if self.args.recursion:<EOL><INDENT>self.__file_list = self.__get_folder(self.args.path)<EOL><DEDENT>else:<EOL><INDENT>self.__file_list = self.__get_file(self.args.path)<EOL><DEDENT>self.logger.debug("<STR_LIT>".format(self.__file_list))<EOL>for file_dict in self.__file_list:<EOL><INDENT>self.filename = file_dict.get('<STR_LIT:Name>')<EOL>if self.filename and self.filename != '<STR_LIT:Name>':<EOL><INDENT>self.logger.debug(<EOL>"<STR_LIT>".format(<EOL>self.filename))<EOL>self.file_datetime_string = file_dict.get(<EOL>'<STR_LIT>').split('<STR_LIT:.>')[<NUM_LIT:0>]<EOL>self.file_datetime = datetime.datetime.strptime(<EOL>self.file_datetime_string, '<STR_LIT>')<EOL>self.logger.debug(<EOL>"<STR_LIT>".format(<EOL>self.file_datetime))<EOL>self.current_datetime = self.__get_current_datetime()<EOL>self.logger.debug(<EOL>"<STR_LIT>".format(<EOL>self.current_datetime))<EOL>self.__delta_datetime = self.current_datetime - self.file_datetime<EOL>self.logger.debug(<EOL>"<STR_LIT>".format(<EOL>self.__delta_datetime))<EOL>self.logger.debug(<EOL>"<STR_LIT>".format(<EOL>datetime.timedelta(<EOL>minutes=self.args.warning)))<EOL>self.logger.debug(<EOL>"<STR_LIT>".format(<EOL>datetime.timedelta(<EOL>minutes=self.args.critical)))<EOL>if self.__delta_datetime > datetime.timedelta(<EOL>minutes=self.args.critical):<EOL><INDENT>self.crit_file.append(self.filename)<EOL><DEDENT>elif self.__delta_datetime > datetime.timedelta(minutes=self.args.warning):<EOL><INDENT>self.warn_file.append(self.filename)<EOL><DEDENT>else:<EOL><INDENT>self.ok_file.append(self.filename)<EOL><DEDENT><DEDENT><DEDENT>if self.crit_file:<EOL><INDENT>status = self.critical<EOL><DEDENT>elif self.warn_file:<EOL><INDENT>status = self.warning<EOL><DEDENT>else:<EOL><INDENT>status = self.ok<EOL><DEDENT>self.shortoutput = "<STR_LIT>".format(<EOL>len(self.crit_file))<EOL>if self.crit_file:<EOL><INDENT>self.longoutput.append("<STR_LIT>")<EOL><DEDENT>[self.longoutput.append(filename)<EOL>for filename in self.crit_file if self.crit_file]<EOL>if self.warn_file:<EOL><INDENT>self.longoutput.append("<STR_LIT>")<EOL><DEDENT>[self.longoutput.append(filename)<EOL>for filename in self.warn_file if self.warn_file]<EOL>if self.ok_file:<EOL><INDENT>self.longoutput.append("<STR_LIT>")<EOL><DEDENT>[self.longoutput.append(filename)<EOL>for filename in self.ok_file if self.ok_file]<EOL>self.perfdata.append("<STR_LIT>".format(<EOL>crit=self.args.critical,<EOL>warn=self.args.warning,<EOL>result=len(self.crit_file),<EOL>path=self.args.drive + self.args.path))<EOL>status(self.output(long_output_limit=None))<EOL>self.logger.debug("<STR_LIT>")<EOL>


plugin = Register()<EOL>if plugin.args.option == '<STR_LIT>':<EOL><INDENT>plugin.filenumber_handle()<EOL><DEDENT>else:<EOL><INDENT>plugin.unknown("<STR_LIT>")<EOL><DEDENT>


self.__results = []<EOL>self.__dirs = []<EOL>self.__files = []<EOL>self.__ftp = self.connect()<EOL>self.__ftp.dir(self.args.path, self.__results.append)<EOL>self.logger.debug("<STR_LIT>".format(self.__results))<EOL>self.quit()<EOL>status = self.ok<EOL>for data in self.__results:<EOL><INDENT>if "<STR_LIT>" in data:<EOL><INDENT>self.__dirs.append(str(data.split()[<NUM_LIT:3>]))<EOL><DEDENT>else:<EOL><INDENT>self.__files.append(str(data.split()[<NUM_LIT:2>]))<EOL><DEDENT><DEDENT>self.__result = len(self.__files)<EOL>self.logger.debug("<STR_LIT>".format(self.__result))<EOL>if self.__result > self.args.warning:<EOL><INDENT>status = self.warning<EOL><DEDENT>if self.__result > self.args.critical:<EOL><INDENT>status = self.critical<EOL><DEDENT>self.shortoutput = "<STR_LIT>".format(self.__result,<EOL>self.args.path)<EOL>[self.longoutput.append(line)<EOL>for line in self.__results if self.__results]<EOL>self.perfdata.append("<STR_LIT>".format(<EOL>crit=self.args.critical,<EOL>warn=self.args.warning,<EOL>result=self.__result,<EOL>path=self.args.path))<EOL>self.logger.debug("<STR_LIT>")<EOL>status(self.output())<EOL>


plugin = Register()<EOL>if plugin.args.option == '<STR_LIT>':<EOL><INDENT>plugin.sqlserverlocks_handle()<EOL><DEDENT>else:<EOL><INDENT>plugin.unknown("<STR_LIT>")<EOL><DEDENT>


plugin = Register()<EOL>if plugin.args.option == '<STR_LIT>':<EOL><INDENT>plugin.sql_handle()<EOL><DEDENT>elif plugin.args.option == '<STR_LIT>':<EOL><INDENT>plugin.database_used_handle()<EOL><DEDENT>elif plugin.args.option == '<STR_LIT>':<EOL><INDENT>plugin.database_log_used_handle()<EOL><DEDENT>else:<EOL><INDENT>plugin.unknown("<STR_LIT>")<EOL><DEDENT>


<EOL>path = None<EOL>names = []<EOL>extension = None<EOL>timestamps = []<EOL>shas = []<EOL>filenames.sort()<EOL>concat_names = "<STR_LIT:_>".join(filenames)<EOL>if concat_names in COMBINED_FILENAMES_GENERATED:<EOL><INDENT>return COMBINED_FILENAMES_GENERATED[concat_names]<EOL><DEDENT>for filename in filenames:<EOL><INDENT>name = os.path.basename(filename)<EOL>if not extension:<EOL><INDENT>extension = os.path.splitext(name)[<NUM_LIT:1>]<EOL><DEDENT>elif os.path.splitext(name)[<NUM_LIT:1>] != extension:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>for base in MEDIA_ROOTS:<EOL><INDENT>try:<EOL><INDENT>shas.append(md5(os.path.join(base, filename)))<EOL>break<EOL><DEDENT>except IOError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if path is None:<EOL><INDENT>path = os.path.dirname(filename)<EOL><DEDENT>else:<EOL><INDENT>if len(os.path.dirname(filename)) < len(path):<EOL><INDENT>path = os.path.dirname(filename)<EOL><DEDENT><DEDENT><DEDENT>m = hashlib.md5()<EOL>m.update("<STR_LIT:U+002C>".join(shas))<EOL>new_filename = "<STR_LIT>" % m.hexdigest()<EOL>new_filename = new_filename[:max_length]<EOL>new_filename += extension<EOL>COMBINED_FILENAMES_GENERATED[concat_names] = new_filename<EOL>return os.path.join(path, new_filename)<EOL>

Return a new filename to use as the combined file name for a
    bunch of files, based on the SHA of their contents.
    A precondition is that they all have the same file extension

    Given that the list of files can have different paths, we aim to use the
    most common path.

    Example:
      /somewhere/else/foo.js
      /somewhere/bar.js
      /somewhere/different/too/foobar.js
    The result will be
      /somewhere/148713695b4a4b9083e506086f061f9c.js

    Another thing to note, if the filenames have timestamps in them, combine
    them all and use the highest timestamp.


try:<EOL><INDENT>kOrientationEXIFTag = <NUM_LIT><EOL>if hasattr(im, '<STR_LIT>'): <EOL><INDENT>e = im._getexif()       <EOL>if e is not None:<EOL><INDENT>orientation = e[kOrientationEXIFTag]<EOL>f = orientation_funcs[orientation]<EOL>return f(im)<EOL><DEDENT><DEDENT><DEDENT>except:<EOL><INDENT>pass <EOL><DEDENT>return im<EOL>

Extract the oritentation EXIF tag from the image, which should be a PIL Image instance,
and if there is an orientation tag that would rotate the image, apply that rotation to
the Image instance given to do an in-place rotation.

:param Image im: Image instance to inspect
:return: A possibly transposed image instance


pass<EOL>


click.echo("<STR_LIT>")<EOL>title = click.prompt("<STR_LIT>")<EOL>url = slugify(title)<EOL>url = click.prompt("<STR_LIT>", default=url)<EOL>click.echo("<STR_LIT>" % url)<EOL>scaffold_piece(title, url)<EOL>


click.echo("<STR_LIT>")<EOL>title = click.prompt("<STR_LIT>")<EOL>url = click.prompt("<STR_LIT>")<EOL>click.echo("<STR_LIT>" % url)<EOL>


try:<EOL><INDENT>build_site(dev_mode=False, clean=True)<EOL>click.echo('<STR_LIT>')<EOL>call("<STR_LIT>" % (BUILD_DIR, CONFIG["<STR_LIT>"],), shell=True)<EOL>if "<STR_LIT>" in CONFIG and "<STR_LIT>" in CONFIG["<STR_LIT>"] and CONFIG["<STR_LIT>"]["<STR_LIT>"]:<EOL><INDENT>do_purge()<EOL><DEDENT><DEDENT>except (KeyboardInterrupt, SystemExit):<EOL><INDENT>raise<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>


if "<STR_LIT>" not in os.environ:<EOL><INDENT>warn("<STR_LIT>")<EOL>echo("<STR_LIT>")<EOL>echo("<STR_LIT>")<EOL>echo("<STR_LIT>")<EOL>echo("<STR_LIT>")<EOL><DEDENT>echo("<STR_LIT>")<EOL>profiles = buffer_get("<STR_LIT>")<EOL>for p in profiles:<EOL><INDENT>supported_profile = False<EOL>if p["<STR_LIT>"].lower() == "<STR_LIT>" or p["<STR_LIT>"].lower() == "<STR_LIT>":<EOL><INDENT>facebook_profiles.append(p)<EOL>supported_profile = True<EOL><DEDENT>elif p["<STR_LIT>"].lower() == "<STR_LIT>":<EOL><INDENT>twitter_profiles.append(p)<EOL>supported_profile = True<EOL><DEDENT>if supported_profile:<EOL><INDENT>click.secho("<STR_LIT>" % (p["<STR_LIT>"], p["<STR_LIT>"]), fg="<STR_LIT>")<EOL><DEDENT><DEDENT>echo("<STR_LIT>")<EOL>site_json_filename = os.path.join(ROOT_DIR, BUILD_DIR, "<STR_LIT>", "<STR_LIT>")<EOL>with open(site_json_filename, "<STR_LIT:r>") as site_json:<EOL><INDENT>site = load(site_json)<EOL><DEDENT>echo('<STR_LIT>')<EOL>posts = {}<EOL>unpublished_posts = []<EOL>for dirpath, dirnames, filenames in os.walk(os.path.join(ROOT_DIR, "<STR_LIT>"), topdown=False):<EOL><INDENT>for filename in filenames:<EOL><INDENT>if "<STR_LIT>" in filename:<EOL><INDENT>if exists(dirpath, "<STR_LIT>") and exists(dirpath, "<STR_LIT>"):<EOL><INDENT>with open(os.path.join(dirpath, "<STR_LIT>")) as f:<EOL><INDENT>social = load(f)<EOL><DEDENT>with open(os.path.join(dirpath, "<STR_LIT>")) as f:<EOL><INDENT>meta = load(f)<EOL><DEDENT>if "<STR_LIT:url>" in meta:<EOL><INDENT>site_json_entry = None<EOL>for sp in site["<STR_LIT>"]:<EOL><INDENT>if meta["<STR_LIT:url>"] == sp["<STR_LIT:url>"]:<EOL><INDENT>site_json_entry = sp<EOL>break<EOL><DEDENT><DEDENT>posts[meta["<STR_LIT:url>"]] = {<EOL>"<STR_LIT>": meta,<EOL>"<STR_LIT>": social,<EOL>"<STR_LIT>": dirpath,<EOL>"<STR_LIT>": site_json_entry,<EOL>}<EOL>if "<STR_LIT>" not in social or social["<STR_LIT>"] is not True:<EOL><INDENT>unpublished_posts.append(meta["<STR_LIT:url>"])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>warn("<STR_LIT>" % dirpath.replace(ROOT_DIR))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>automark_set = False<EOL>automark = None<EOL>for u in unpublished_posts:<EOL><INDENT>post = posts[u]<EOL>if "<STR_LIT>" in post["<STR_LIT>"] and post["<STR_LIT>"]["<STR_LIT>"] and len(post["<STR_LIT>"]["<STR_LIT>"]) > <NUM_LIT:0>:<EOL><INDENT>facebook_posts = []<EOL>twitter_posts = []<EOL>mark_as_published = False<EOL>has_valid_post = False<EOL>for p in post["<STR_LIT>"]["<STR_LIT>"]:<EOL><INDENT>try:<EOL><INDENT>if len(list(p.keys())) != <NUM_LIT:1>:<EOL><INDENT>error("<STR_LIT>" % u)<EOL>break<EOL><DEDENT>if list(p.keys())[<NUM_LIT:0>] == "<STR_LIT>":<EOL><INDENT>facebook_posts.append(p["<STR_LIT>"])<EOL>if post_in_future(p["<STR_LIT>"], post):<EOL><INDENT>has_valid_post = True<EOL><DEDENT><DEDENT>elif list(p.keys())[<NUM_LIT:0>] == "<STR_LIT>":<EOL><INDENT>if post_in_future(p["<STR_LIT>"], post):<EOL><INDENT>has_valid_post = True<EOL><DEDENT>twitter_posts.append(p["<STR_LIT>"])<EOL><DEDENT>else:<EOL><INDENT>warn("<STR_LIT>" % list(p.keys())[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>except:<EOL><INDENT>error("<STR_LIT>" % post["<STR_LIT>"]["<STR_LIT:title>"])<EOL>import traceback<EOL>traceback.print_exc()<EOL><DEDENT><DEDENT>if not has_valid_post:<EOL><INDENT>if automark:<EOL><INDENT>mark_as_published = True<EOL><DEDENT>else:<EOL><INDENT>warn('<STR_LIT>' % post["<STR_LIT>"]["<STR_LIT:title>"])<EOL>if click.confirm("<STR_LIT>"):<EOL><INDENT>mark_as_published = True<EOL>if not automark_set:<EOL><INDENT>if click.confirm("<STR_LIT>"):<EOL><INDENT>automark = True<EOL><DEDENT>automark_set = True<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>echo('<STR_LIT>' % post["<STR_LIT>"]["<STR_LIT:title>"])<EOL>if len(facebook_posts) > <NUM_LIT:0>:<EOL><INDENT>echo("<STR_LIT>")<EOL>for p in facebook_posts:<EOL><INDENT>if (len(p["<STR_LIT:content>"]) > <NUM_LIT>):<EOL><INDENT>truncated_content = "<STR_LIT>" % p["<STR_LIT:content>"][:<NUM_LIT>]<EOL><DEDENT>else:<EOL><INDENT>truncated_content = p["<STR_LIT:content>"]<EOL><DEDENT>if post_in_future(p, post):<EOL><INDENT>echo("<STR_LIT>" % (<EOL>publish_datetime(p, post).strftime("<STR_LIT>"),<EOL>truncated_content,<EOL>))<EOL><DEDENT>else:<EOL><INDENT>warn("<STR_LIT>" % (<EOL>publish_datetime(p, post).strftime("<STR_LIT>"),<EOL>truncated_content,<EOL>))<EOL><DEDENT><DEDENT><DEDENT>echo("<STR_LIT>")<EOL>if len(twitter_posts) > <NUM_LIT:0>:<EOL><INDENT>for p in twitter_posts:<EOL><INDENT>if (len(p["<STR_LIT:content>"]) > <NUM_LIT>):<EOL><INDENT>truncated_content = "<STR_LIT>" % p["<STR_LIT:content>"][:<NUM_LIT>]<EOL><DEDENT>else:<EOL><INDENT>truncated_content = p["<STR_LIT:content>"]<EOL><DEDENT>if post_in_future(p, post):<EOL><INDENT>echo("<STR_LIT>" % (<EOL>publish_datetime(p, post).strftime("<STR_LIT>"),<EOL>truncated_content,<EOL>))<EOL><DEDENT>else:<EOL><INDENT>warn("<STR_LIT>" % (<EOL>publish_datetime(p, post).strftime("<STR_LIT>"),<EOL>truncated_content,<EOL>))<EOL><DEDENT><DEDENT><DEDENT>if click.confirm(click.style("<STR_LIT>", fg="<STR_LIT>")):<EOL><INDENT>mark_as_published = True<EOL>echo("<STR_LIT>")<EOL>for p in facebook_posts:<EOL><INDENT>if post_in_future(p, post):<EOL><INDENT>publish_facebook(p, post)<EOL>if (len(p["<STR_LIT:content>"]) > <NUM_LIT>):<EOL><INDENT>truncated_content = "<STR_LIT>" % p["<STR_LIT:content>"][:<NUM_LIT>]<EOL><DEDENT>else:<EOL><INDENT>truncated_content = p["<STR_LIT:content>"]<EOL><DEDENT>click.secho("<STR_LIT>" % (<EOL>publish_datetime(p, post).strftime("<STR_LIT>"),<EOL>truncated_content,<EOL>), fg="<STR_LIT>")<EOL><DEDENT><DEDENT>for p in twitter_posts:<EOL><INDENT>if post_in_future(p, post):<EOL><INDENT>publish_twitter(p, post)<EOL>if (len(p["<STR_LIT:content>"]) > <NUM_LIT>):<EOL><INDENT>truncated_content = "<STR_LIT>" % p["<STR_LIT:content>"][:<NUM_LIT>]<EOL><DEDENT>else:<EOL><INDENT>truncated_content = p["<STR_LIT:content>"]<EOL><DEDENT>click.secho("<STR_LIT>" % (<EOL>publish_datetime(p, post).strftime("<STR_LIT>"),<EOL>truncated_content,<EOL>), fg="<STR_LIT>")<EOL><DEDENT><DEDENT>echo("<STR_LIT>")<EOL><DEDENT><DEDENT>if mark_as_published or automark:<EOL><INDENT>post["<STR_LIT>"]["<STR_LIT>"] = True<EOL>with open(os.path.join(post["<STR_LIT>"], "<STR_LIT>"), "<STR_LIT:w>") as f:<EOL><INDENT>dump(post["<STR_LIT>"], f, default_flow_style=False, width=<NUM_LIT:1000>)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if click.confirm("<STR_LIT>"):<EOL><INDENT>print ("<STR_LIT>")<EOL><DEDENT>


build_site(dev_mode=True)<EOL>serve_site()<EOL>

Serve the site for local testing and editing.


if dirname[-<NUM_LIT:1>] == '<STR_LIT:/>':<EOL><INDENT>dirname = dirname[:-<NUM_LIT:1>]<EOL><DEDENT>full_path = os.path.abspath(dirname)<EOL>if not os.path.isdir(full_path):<EOL><INDENT>raise AttributeError('<STR_LIT>')<EOL><DEDENT>base, name = os.path.split(full_path)<EOL>tempdir = tempfile.mkdtemp()<EOL>shutil.move(full_path, tempdir)<EOL>os.mkdir(full_path)<EOL>try:<EOL><INDENT>yield tempdir<EOL><DEDENT>finally:<EOL><INDENT>shutil.rmtree(full_path)<EOL>moved = os.path.join(tempdir, name)<EOL>shutil.move(moved, base)<EOL>shutil.rmtree(tempdir)<EOL><DEDENT>

This ``Context Manager`` is used to move the contents of a directory
    elsewhere temporarily and put them back upon exit.  This allows testing
    code to use the same file directories as normal code without fear of
    damage.

    The name of the temporary directory which contains your files is yielded.

    :param dirname:
        Path name of the directory to be replaced.

    Example:

    .. code-block:: python

        with replaced_directory('/foo/bar/') as rd:
            # "/foo/bar/" has been moved & renamed
            with open('/foo/bar/thing.txt', 'w') as f:
                f.write('stuff')
                f.close()

        # got here? => "/foo/bar/ is now restored and temp has been wiped,
        # "thing.txt" is gone


stdout = sys.stdout<EOL>try:<EOL><INDENT>capture_out = StringIO()<EOL>sys.stdout = capture_out<EOL>yield capture_out<EOL><DEDENT>finally:<EOL><INDENT>sys.stdout = stdout<EOL><DEDENT>

This ``Context Manager`` redirects STDOUT to a ``StringIO`` objects
    which is returned from the ``Context``.  On exit STDOUT is restored.

    Example:

    .. code-block:: python

        with capture_stdout() as capture:
            print('foo')

        # got here? => capture.getvalue() will now have "foo\\n"


stderr = sys.stderr<EOL>try:<EOL><INDENT>capture_out = StringIO()<EOL>sys.stderr = capture_out<EOL>yield capture_out<EOL><DEDENT>finally:<EOL><INDENT>sys.stderr = stderr<EOL><DEDENT>

This ``Context Manager`` redirects STDERR to a ``StringIO`` objects
    which is returned from the ``Context``.  On exit STDERR is restored.

    Example:

    .. code-block:: python

        with capture_stderr() as capture:
            print('foo')

        # got here? => capture.getvalue() will now have "foo\\n"


print(json.dumps(data, sort_keys=True, indent=<NUM_LIT:4>, separators=('<STR_LIT:U+002C>', '<STR_LIT>')))<EOL>

Alternative to `pprint.PrettyPrinter()` that uses `json.dumps()` for
    sorting and displaying data.

    :param data: item to print to STDOUT.  The item must be json serializable!


timezone = country_dict.get(country_code)<EOL>if not timezone:<EOL><INDENT>return None<EOL><DEDENT>if isinstance(timezone, str):<EOL><INDENT>return timezone<EOL><DEDENT>return timezone.get(region_code)<EOL>

Returns time zone from country and region code.

:arg country_code: Country code
:arg region_code: Region code


try:<EOL><INDENT>return int(binascii.hexlify(socket.inet_aton(ip)), <NUM_LIT:16>)<EOL><DEDENT>except socket.error:<EOL><INDENT>return int(binascii.hexlify(socket.inet_pton(socket.AF_INET6, ip)), <NUM_LIT:16>)<EOL><DEDENT>

Wrapper function for IPv4 and IPv6 converters.

:arg ip: IPv4 or IPv6 address


return BytesIO(bytearray(data, const.ENCODING)) if const.PY3 else StringIO(data)<EOL>

Convert bytes data to file handle object (StringIO or BytesIO).

:arg data: String data to transform


if len(args) > <NUM_LIT:0>:<EOL><INDENT>filename = args[<NUM_LIT:0>]<EOL><DEDENT>elif '<STR_LIT:filename>' in kwargs:<EOL><INDENT>filename = kwargs['<STR_LIT:filename>']<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>if not kwargs.get('<STR_LIT>', True):<EOL><INDENT>return super(_GeoIPMetaclass, cls).__call__(*args, **kwargs)<EOL><DEDENT>try:<EOL><INDENT>cls._instance_lock.acquire()<EOL>if filename not in cls._instances:<EOL><INDENT>cls._instances[filename] = super(_GeoIPMetaclass, cls).__call__(*args, **kwargs)<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>cls._instance_lock.release()<EOL><DEDENT>return cls._instances[filename]<EOL>

Singleton method to gets an instance without reparsing
the database, the filename is being used as cache key.


def __init__(self, filename, flags=STANDARD, cache=True):

self._lock = Lock()<EOL>self._flags = flags<EOL>self._netmask = None<EOL>if self._flags & const.MMAP_CACHE and mmap is None:  <EOL><INDENT>import warnings<EOL>warnings.warn("<STR_LIT>")<EOL>self._flags &= ~const.MMAP_CACHE<EOL><DEDENT>if self._flags & const.MMAP_CACHE:<EOL><INDENT>f = codecs.open(filename, '<STR_LIT:rb>', ENCODING)<EOL>access = mmap.ACCESS_READ<EOL>self._fp = mmap.mmap(f.fileno(), <NUM_LIT:0>, access=access)<EOL>self._type = '<STR_LIT>'<EOL>f.close()<EOL><DEDENT>elif self._flags & const.MEMORY_CACHE:<EOL><INDENT>f = codecs.open(filename, '<STR_LIT:rb>', ENCODING)<EOL>self._memory = f.read()<EOL>self._fp = util.str2fp(self._memory)<EOL>self._type = '<STR_LIT>'<EOL>f.close()<EOL><DEDENT>else:<EOL><INDENT>self._fp = codecs.open(filename, '<STR_LIT:rb>', ENCODING)<EOL>self._type = '<STR_LIT>'<EOL><DEDENT>try:<EOL><INDENT>self._lock.acquire()<EOL>self._setup_segments()<EOL><DEDENT>finally:<EOL><INDENT>self._lock.release()<EOL><DEDENT>

Create and return an GeoIP instance.

:arg filename: File path to a GeoIP database
:arg flags: Flags that affect how the database is processed.
    Currently supported flags are STANDARD (default),
    MEMORY_CACHE (preload the whole file into memory) and
    MMAP_CACHE (access the file via mmap)
:arg cache: Used in tests to skip instance caching


self._databaseType = const.COUNTRY_EDITION<EOL>self._recordLength = const.STANDARD_RECORD_LENGTH<EOL>self._databaseSegments = const.COUNTRY_BEGIN<EOL>filepos = self._fp.tell()<EOL>self._fp.seek(-<NUM_LIT:3>, os.SEEK_END)<EOL>for i in range(const.STRUCTURE_INFO_MAX_SIZE):<EOL><INDENT>chars = chr(<NUM_LIT:255>) * <NUM_LIT:3><EOL>delim = self._fp.read(<NUM_LIT:3>)<EOL>if PY3 and type(delim) is bytes:<EOL><INDENT>delim = delim.decode(ENCODING)<EOL><DEDENT>if PY2:<EOL><INDENT>chars = chars.decode(ENCODING)<EOL>if type(delim) is str:<EOL><INDENT>delim = delim.decode(ENCODING)<EOL><DEDENT><DEDENT>if delim == chars:<EOL><INDENT>byte = self._fp.read(<NUM_LIT:1>)<EOL>self._databaseType = ord(byte)<EOL>if self._databaseType >= <NUM_LIT>:<EOL><INDENT>self._databaseType -= <NUM_LIT><EOL><DEDENT>if self._databaseType == const.REGION_EDITION_REV0:<EOL><INDENT>self._databaseSegments = const.STATE_BEGIN_REV0<EOL><DEDENT>elif self._databaseType == const.REGION_EDITION_REV1:<EOL><INDENT>self._databaseSegments = const.STATE_BEGIN_REV1<EOL><DEDENT>elif self._databaseType in (const.CITY_EDITION_REV0,<EOL>const.CITY_EDITION_REV1,<EOL>const.CITY_EDITION_REV1_V6,<EOL>const.ORG_EDITION,<EOL>const.ISP_EDITION,<EOL>const.NETSPEED_EDITION_REV1,<EOL>const.NETSPEED_EDITION_REV1_V6,<EOL>const.ASNUM_EDITION,<EOL>const.ASNUM_EDITION_V6):<EOL><INDENT>self._databaseSegments = <NUM_LIT:0><EOL>buf = self._fp.read(const.SEGMENT_RECORD_LENGTH)<EOL>if PY3 and type(buf) is bytes:<EOL><INDENT>buf = buf.decode(ENCODING)<EOL><DEDENT>for j in range(const.SEGMENT_RECORD_LENGTH):<EOL><INDENT>self._databaseSegments += (ord(buf[j]) << (j * <NUM_LIT:8>))<EOL><DEDENT>LONG_RECORDS = (const.ORG_EDITION, const.ISP_EDITION)<EOL>if self._databaseType in LONG_RECORDS:<EOL><INDENT>self._recordLength = const.ORG_RECORD_LENGTH<EOL><DEDENT><DEDENT>break<EOL><DEDENT>else:<EOL><INDENT>self._fp.seek(-<NUM_LIT:4>, os.SEEK_CUR)<EOL><DEDENT><DEDENT>self._fp.seek(filepos, os.SEEK_SET)<EOL>

Parses the database file to determine what kind of database is
being used and setup segment sizes and start points that will
be used by the seek*() methods later.


try:<EOL><INDENT>offset = <NUM_LIT:0><EOL>seek_depth = <NUM_LIT> if len(str(ipnum)) > <NUM_LIT:10> else <NUM_LIT><EOL>for depth in range(seek_depth, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>if self._flags & const.MEMORY_CACHE:<EOL><INDENT>startIndex = <NUM_LIT:2> * self._recordLength * offset<EOL>endIndex = startIndex + (<NUM_LIT:2> * self._recordLength)<EOL>buf = self._memory[startIndex:endIndex]<EOL><DEDENT>else:<EOL><INDENT>startIndex = <NUM_LIT:2> * self._recordLength * offset<EOL>readLength = <NUM_LIT:2> * self._recordLength<EOL>try:<EOL><INDENT>self._lock.acquire()<EOL>self._fp.seek(startIndex, os.SEEK_SET)<EOL>buf = self._fp.read(readLength)<EOL><DEDENT>finally:<EOL><INDENT>self._lock.release()<EOL><DEDENT><DEDENT>if PY3 and type(buf) is bytes:<EOL><INDENT>buf = buf.decode(ENCODING)<EOL><DEDENT>x = [<NUM_LIT:0>, <NUM_LIT:0>]<EOL>for i in range(<NUM_LIT:2>):<EOL><INDENT>for j in range(self._recordLength):<EOL><INDENT>byte = buf[self._recordLength * i + j]<EOL>x[i] += ord(byte) << (j * <NUM_LIT:8>)<EOL><DEDENT><DEDENT>if ipnum & (<NUM_LIT:1> << depth):<EOL><INDENT>if x[<NUM_LIT:1>] >= self._databaseSegments:<EOL><INDENT>self._netmask = seek_depth - depth + <NUM_LIT:1><EOL>return x[<NUM_LIT:1>]<EOL><DEDENT>offset = x[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>if x[<NUM_LIT:0>] >= self._databaseSegments:<EOL><INDENT>self._netmask = seek_depth - depth + <NUM_LIT:1><EOL>return x[<NUM_LIT:0>]<EOL><DEDENT>offset = x[<NUM_LIT:0>]<EOL><DEDENT><DEDENT><DEDENT>except (IndexError, UnicodeDecodeError):<EOL><INDENT>pass<EOL><DEDENT>raise GeoIPError('<STR_LIT>')<EOL>

Using the record length and appropriate start points, seek to the
country that corresponds to the converted IP address integer.
Return offset of record.

:arg ipnum: Result of ip2long conversion


seek_org = self._seek_country(ipnum)<EOL>if seek_org == self._databaseSegments:<EOL><INDENT>return None<EOL><DEDENT>read_length = (<NUM_LIT:2> * self._recordLength - <NUM_LIT:1>) * self._databaseSegments<EOL>try:<EOL><INDENT>self._lock.acquire()<EOL>self._fp.seek(seek_org + read_length, os.SEEK_SET)<EOL>buf = self._fp.read(const.MAX_ORG_RECORD_LENGTH)<EOL><DEDENT>finally:<EOL><INDENT>self._lock.release()<EOL><DEDENT>if PY3 and type(buf) is bytes:<EOL><INDENT>buf = buf.decode(ENCODING)<EOL><DEDENT>return buf[:buf.index(chr(<NUM_LIT:0>))]<EOL>

Seek and return organization or ISP name for ipnum.
Return org/isp name.

:arg ipnum: Result of ip2long conversion


region_code = None<EOL>country_code = None<EOL>seek_country = self._seek_country(ipnum)<EOL>def get_region_code(offset):<EOL><INDENT>region1 = chr(offset // <NUM_LIT> + <NUM_LIT>)<EOL>region2 = chr(offset % <NUM_LIT> + <NUM_LIT>)<EOL>return '<STR_LIT>'.join([region1, region2])<EOL><DEDENT>if self._databaseType == const.REGION_EDITION_REV0:<EOL><INDENT>seek_region = seek_country - const.STATE_BEGIN_REV0<EOL>if seek_region >= <NUM_LIT:1000>:<EOL><INDENT>country_code = '<STR_LIT>'<EOL>region_code = get_region_code(seek_region - <NUM_LIT:1000>)<EOL><DEDENT>else:<EOL><INDENT>country_code = const.COUNTRY_CODES[seek_region]<EOL><DEDENT><DEDENT>elif self._databaseType == const.REGION_EDITION_REV1:<EOL><INDENT>seek_region = seek_country - const.STATE_BEGIN_REV1<EOL>if seek_region < const.US_OFFSET:<EOL><INDENT>pass<EOL><DEDENT>elif seek_region < const.CANADA_OFFSET:<EOL><INDENT>country_code = '<STR_LIT>'<EOL>region_code = get_region_code(seek_region - const.US_OFFSET)<EOL><DEDENT>elif seek_region < const.WORLD_OFFSET:<EOL><INDENT>country_code = '<STR_LIT>'<EOL>region_code = get_region_code(seek_region - const.CANADA_OFFSET)<EOL><DEDENT>else:<EOL><INDENT>index = (seek_region - const.WORLD_OFFSET) // const.FIPS_RANGE<EOL>if index < len(const.COUNTRY_CODES):<EOL><INDENT>country_code = const.COUNTRY_CODES[index]<EOL><DEDENT><DEDENT><DEDENT>elif self._databaseType in const.CITY_EDITIONS:<EOL><INDENT>rec = self._get_record(ipnum)<EOL>region_code = rec.get('<STR_LIT>')<EOL>country_code = rec.get('<STR_LIT>')<EOL><DEDENT>return {'<STR_LIT>': country_code, '<STR_LIT>': region_code}<EOL>

Seek and return the region information.
Returns dict containing country_code and region_code.

:arg ipnum: Result of ip2long conversion


seek_country = self._seek_country(ipnum)<EOL>if seek_country == self._databaseSegments:<EOL><INDENT>return {}<EOL><DEDENT>read_length = (<NUM_LIT:2> * self._recordLength - <NUM_LIT:1>) * self._databaseSegments<EOL>try:<EOL><INDENT>self._lock.acquire()<EOL>self._fp.seek(seek_country + read_length, os.SEEK_SET)<EOL>buf = self._fp.read(const.FULL_RECORD_LENGTH)<EOL><DEDENT>finally:<EOL><INDENT>self._lock.release()<EOL><DEDENT>if PY3 and type(buf) is bytes:<EOL><INDENT>buf = buf.decode(ENCODING)<EOL><DEDENT>record = {<EOL>'<STR_LIT>': <NUM_LIT:0>,<EOL>'<STR_LIT>': <NUM_LIT:0>,<EOL>'<STR_LIT>': None,<EOL>'<STR_LIT>': None<EOL>}<EOL>latitude = <NUM_LIT:0><EOL>longitude = <NUM_LIT:0><EOL>char = ord(buf[<NUM_LIT:0>])<EOL>record['<STR_LIT>'] = const.COUNTRY_CODES[char]<EOL>record['<STR_LIT>'] = const.COUNTRY_CODES3[char]<EOL>record['<STR_LIT>'] = const.COUNTRY_NAMES[char]<EOL>record['<STR_LIT>'] = const.CONTINENT_NAMES[char]<EOL>def read_data(buf, pos):<EOL><INDENT>cur = pos<EOL>while buf[cur] != '<STR_LIT>':<EOL><INDENT>cur += <NUM_LIT:1><EOL><DEDENT>return cur, buf[pos:cur] if cur > pos else None<EOL><DEDENT>offset, record['<STR_LIT>'] = read_data(buf, <NUM_LIT:1>)<EOL>offset, record['<STR_LIT>'] = read_data(buf, offset + <NUM_LIT:1>)<EOL>offset, record['<STR_LIT>'] = read_data(buf, offset + <NUM_LIT:1>)<EOL>offset = offset + <NUM_LIT:1><EOL>for j in range(<NUM_LIT:3>):<EOL><INDENT>latitude += (ord(buf[offset + j]) << (j * <NUM_LIT:8>))<EOL><DEDENT>for j in range(<NUM_LIT:3>):<EOL><INDENT>longitude += (ord(buf[offset + j + <NUM_LIT:3>]) << (j * <NUM_LIT:8>))<EOL><DEDENT>record['<STR_LIT>'] = (latitude / <NUM_LIT>) - <NUM_LIT><EOL>record['<STR_LIT>'] = (longitude / <NUM_LIT>) - <NUM_LIT><EOL>if self._databaseType in (const.CITY_EDITION_REV1, const.CITY_EDITION_REV1_V6):<EOL><INDENT>if record['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>dma_area = <NUM_LIT:0><EOL>for j in range(<NUM_LIT:3>):<EOL><INDENT>dma_area += ord(buf[offset + j + <NUM_LIT:6>]) << (j * <NUM_LIT:8>)<EOL><DEDENT>record['<STR_LIT>'] = int(floor(dma_area / <NUM_LIT:1000>))<EOL>record['<STR_LIT>'] = dma_area % <NUM_LIT:1000><EOL>record['<STR_LIT>'] = const.DMA_MAP.get(record['<STR_LIT>'])<EOL><DEDENT><DEDENT>params = (record['<STR_LIT>'], record['<STR_LIT>'])<EOL>record['<STR_LIT>'] = time_zone_by_country_and_region(*params)<EOL>return record<EOL>

Populate location dict for converted IP.
Returns dict with numerous location properties.

:arg ipnum: Result of ip2long conversion


if self._databaseType in const.IPV6_EDITIONS:<EOL><INDENT>response = socket.getaddrinfo(hostname, <NUM_LIT:0>, socket.AF_INET6)<EOL>family, socktype, proto, canonname, sockaddr = response[<NUM_LIT:0>]<EOL>address, port, flow, scope = sockaddr<EOL>return address<EOL><DEDENT>else:<EOL><INDENT>return socket.gethostbyname(hostname)<EOL><DEDENT>

Hostname lookup method, supports both IPv4 and IPv6.


addr = self._gethostbyname(hostname)<EOL>return self.id_by_addr(addr)<EOL>

Returns the database ID for specified hostname.
The id might be useful as array index. 0 is unknown.

:arg hostname: Hostname to get ID from.


if self._databaseType in (const.PROXY_EDITION, const.NETSPEED_EDITION_REV1, const.NETSPEED_EDITION_REV1_V6):<EOL><INDENT>raise GeoIPError('<STR_LIT>')<EOL><DEDENT>ipv = <NUM_LIT:6> if addr.find('<STR_LIT::>') >= <NUM_LIT:0> else <NUM_LIT:4><EOL>if ipv == <NUM_LIT:4> and self._databaseType not in (const.COUNTRY_EDITION, const.NETSPEED_EDITION):<EOL><INDENT>raise GeoIPError('<STR_LIT>')<EOL><DEDENT>if ipv == <NUM_LIT:6> and self._databaseType != const.COUNTRY_EDITION_V6:<EOL><INDENT>raise GeoIPError('<STR_LIT>')<EOL><DEDENT>ipnum = util.ip2long(addr)<EOL>return self._seek_country(ipnum) - const.COUNTRY_BEGIN<EOL>

Returns the database ID for specified address.
The ID might be useful as array index. 0 is unknown.

:arg addr: IPv4 or IPv6 address (eg. 203.0.113.30)


return self._netmask<EOL>

Returns the netmask depth of the last lookup.


VALID_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6)<EOL>if self._databaseType in VALID_EDITIONS:<EOL><INDENT>country_id = self.id_by_addr(addr)<EOL>return const.COUNTRY_CODES[country_id]<EOL><DEDENT>elif self._databaseType in const.REGION_CITY_EDITIONS:<EOL><INDENT>return self.region_by_addr(addr).get('<STR_LIT>')<EOL><DEDENT>raise GeoIPError('<STR_LIT>')<EOL>

Returns 2-letter country code (e.g. US) from IP address.

:arg addr: IP address (e.g. 203.0.113.30)


addr = self._gethostbyname(hostname)<EOL>return self.country_code_by_addr(addr)<EOL>

Returns 2-letter country code (e.g. US) from hostname.

:arg hostname: Hostname (e.g. example.com)


if self._databaseType == const.NETSPEED_EDITION:<EOL><INDENT>return const.NETSPEED_NAMES[self.id_by_addr(addr)]<EOL><DEDENT>elif self._databaseType in (const.NETSPEED_EDITION_REV1,<EOL>const.NETSPEED_EDITION_REV1_V6):<EOL><INDENT>ipnum = util.ip2long(addr)<EOL>return self._get_org(ipnum)<EOL><DEDENT>raise GeoIPError(<EOL>'<STR_LIT>')<EOL>

Returns NetSpeed name from address.

:arg addr: IP address (e.g. 203.0.113.30)


addr = self._gethostbyname(hostname)<EOL>return self.netspeed_by_addr(addr)<EOL>

Returns NetSpeed name from hostname. Can be Unknown, Dial-up,
Cable, or Corporate.

:arg hostname: Hostname (e.g. example.com)


VALID_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6)<EOL>if self._databaseType in VALID_EDITIONS:<EOL><INDENT>country_id = self.id_by_addr(addr)<EOL>return const.COUNTRY_NAMES[country_id]<EOL><DEDENT>elif self._databaseType in const.CITY_EDITIONS:<EOL><INDENT>return self.record_by_addr(addr).get('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>message = '<STR_LIT>'<EOL>raise GeoIPError(message)<EOL><DEDENT>

Returns full country name for specified IP address.

:arg addr: IP address (e.g. 203.0.113.30)


addr = self._gethostbyname(hostname)<EOL>return self.country_name_by_addr(addr)<EOL>

Returns full country name for specified hostname.

:arg hostname: Hostname (e.g. example.com)


valid = (const.ORG_EDITION, const.ISP_EDITION,<EOL>const.ASNUM_EDITION, const.ASNUM_EDITION_V6)<EOL>if self._databaseType not in valid:<EOL><INDENT>message = '<STR_LIT>'<EOL>raise GeoIPError(message)<EOL><DEDENT>ipnum = util.ip2long(addr)<EOL>return self._get_org(ipnum)<EOL>

Returns Organization, ISP, or ASNum name for given IP address.

:arg addr: IP address (e.g. 203.0.113.30)


addr = self._gethostbyname(hostname)<EOL>return self.org_by_addr(addr)<EOL>

Returns Organization, ISP, or ASNum name for given hostname.

:arg hostname: Hostname (e.g. example.com)


if self._databaseType not in const.CITY_EDITIONS:<EOL><INDENT>message = '<STR_LIT>'<EOL>raise GeoIPError(message)<EOL><DEDENT>ipnum = util.ip2long(addr)<EOL>rec = self._get_record(ipnum)<EOL>if not rec:<EOL><INDENT>return None<EOL><DEDENT>return rec<EOL>

Returns dictionary with city data containing `country_code`, `country_name`,
`region`, `city`, `postal_code`, `latitude`, `longitude`, `dma_code`,
`metro_code`, `area_code`, `region_code` and `time_zone`.

:arg addr: IP address (e.g. 203.0.113.30)


addr = self._gethostbyname(hostname)<EOL>return self.record_by_addr(addr)<EOL>

Returns dictionary with city data containing `country_code`, `country_name`,
`region`, `city`, `postal_code`, `latitude`, `longitude`, `dma_code`,
`metro_code`, `area_code`, `region_code` and `time_zone`.

:arg hostname: Hostname (e.g. example.com)


if self._databaseType not in const.REGION_CITY_EDITIONS:<EOL><INDENT>message = '<STR_LIT>'<EOL>raise GeoIPError(message)<EOL><DEDENT>ipnum = util.ip2long(addr)<EOL>return self._get_region(ipnum)<EOL>

Returns dictionary containing `country_code` and `region_code`.

:arg addr: IP address (e.g. 203.0.113.30)


addr = self._gethostbyname(hostname)<EOL>return self.region_by_addr(addr)<EOL>

Returns dictionary containing `country_code` and `region_code`.

:arg hostname: Hostname (e.g. example.com)


if self._databaseType not in const.CITY_EDITIONS:<EOL><INDENT>message = '<STR_LIT>'<EOL>raise GeoIPError(message)<EOL><DEDENT>ipnum = util.ip2long(addr)<EOL>return self._get_record(ipnum).get('<STR_LIT>')<EOL>

Returns time zone in tzdata format (e.g. America/New_York or Europe/Paris)

:arg addr: IP address (e.g. 203.0.113.30)


addr = self._gethostbyname(hostname)<EOL>return self.time_zone_by_addr(addr)<EOL>

Returns time zone in tzdata format (e.g. America/New_York or Europe/Paris)

:arg hostname: Hostname (e.g. example.com)


return ((c >= '<STR_LIT:a>' and c <= '<STR_LIT:z>') or (c >= '<STR_LIT:0>' and c <= '<STR_LIT>') or<EOL>(c >= '<STR_LIT:A>' and c <= '<STR_LIT>') or c == '<STR_LIT:_>' or c == '<STR_LIT:$>' or c == '<STR_LIT:\\>' or (c is not None and ord(c) > <NUM_LIT>))<EOL>

return true if the character is a letter, digit, underscore,
           dollar sign, or non-ASCII character.


c = self.theLookahead<EOL>self.theLookahead = None<EOL>if c == None:<EOL><INDENT>c = self.instream.read(<NUM_LIT:1>)<EOL><DEDENT>if c >= '<STR_LIT:U+0020>' or c == '<STR_LIT:\n>':<EOL><INDENT>return c<EOL><DEDENT>if c == '<STR_LIT>': <EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>if c == '<STR_LIT:\r>':<EOL><INDENT>return '<STR_LIT:\n>'<EOL><DEDENT>return '<STR_LIT:U+0020>'<EOL>

return the next character from stdin. Watch out for lookahead. If
           the character is a control character, translate it to a space or
           linefeed.


c = self._get()<EOL>if c == '<STR_LIT:/>' and self.theA != '<STR_LIT:\\>':<EOL><INDENT>p = self._peek()<EOL>if p == '<STR_LIT:/>':<EOL><INDENT>c = self._get()<EOL>while c > '<STR_LIT:\n>':<EOL><INDENT>c = self._get()<EOL><DEDENT>return c<EOL><DEDENT>if p == '<STR_LIT:*>':<EOL><INDENT>c = self._get()<EOL>while <NUM_LIT:1>:<EOL><INDENT>c = self._get()<EOL>if c == '<STR_LIT:*>':<EOL><INDENT>if self._peek() == '<STR_LIT:/>':<EOL><INDENT>self._get()<EOL>return '<STR_LIT:U+0020>'<EOL><DEDENT><DEDENT>if c == '<STR_LIT>':<EOL><INDENT>raise UnterminatedComment()<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return c<EOL>

get the next character, excluding comments. peek() is used to see
           if an unescaped '/' is followed by a '/' or '*'.


if action <= <NUM_LIT:1>:<EOL><INDENT>self._outA()<EOL><DEDENT>if action <= <NUM_LIT:2>:<EOL><INDENT>self.theA = self.theB<EOL>if self.theA == "<STR_LIT:'>" or self.theA == '<STR_LIT:">':<EOL><INDENT>while <NUM_LIT:1>:<EOL><INDENT>self._outA()<EOL>self.theA = self._get()<EOL>if self.theA == self.theB:<EOL><INDENT>break<EOL><DEDENT>if self.theA <= '<STR_LIT:\n>':<EOL><INDENT>raise UnterminatedStringLiteral()<EOL><DEDENT>if self.theA == '<STR_LIT:\\>':<EOL><INDENT>self._outA()<EOL>self.theA = self._get()<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if action <= <NUM_LIT:3>:<EOL><INDENT>self.theB = self._next()<EOL>if self.theB == '<STR_LIT:/>' and (self.theA == '<STR_LIT:(>' or self.theA == '<STR_LIT:U+002C>' or<EOL>self.theA == '<STR_LIT:=>' or self.theA == '<STR_LIT::>' or<EOL>self.theA == '<STR_LIT:[>' or self.theA == '<STR_LIT:?>' or<EOL>self.theA == '<STR_LIT:!>' or self.theA == '<STR_LIT:&>' or<EOL>self.theA == '<STR_LIT:|>' or self.theA == '<STR_LIT:;>' or<EOL>self.theA == '<STR_LIT:{>' or self.theA == '<STR_LIT:}>' or<EOL>self.theA == '<STR_LIT:\n>'):<EOL><INDENT>self._outA()<EOL>self._outB()<EOL>while <NUM_LIT:1>:<EOL><INDENT>self.theA = self._get()<EOL>if self.theA == '<STR_LIT:/>':<EOL><INDENT>break<EOL><DEDENT>elif self.theA == '<STR_LIT:\\>':<EOL><INDENT>self._outA()<EOL>self.theA = self._get()<EOL><DEDENT>elif self.theA <= '<STR_LIT:\n>':<EOL><INDENT>raise UnterminatedRegularExpression()<EOL><DEDENT>self._outA()<EOL><DEDENT>self.theB = self._next()<EOL><DEDENT><DEDENT>

do something! What you do is determined by the argument:
           1   Output A. Copy B to A. Get the next B.
           2   Copy B to A. Get the next B. (Delete A).
           3   Get the next B. (Delete B).
           action treats a string as a single character. Wow!
           action recognizes a regular expression if it is preceded by ( or , or =.


self.theA = '<STR_LIT:\n>'<EOL>self._action(<NUM_LIT:3>)<EOL>while self.theA != '<STR_LIT>':<EOL><INDENT>if self.theA == '<STR_LIT:U+0020>':<EOL><INDENT>if isAlphanum(self.theB):<EOL><INDENT>self._action(<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>self._action(<NUM_LIT:2>)<EOL><DEDENT><DEDENT>elif self.theA == '<STR_LIT:\n>':<EOL><INDENT>if self.theB in ['<STR_LIT:{>', '<STR_LIT:[>', '<STR_LIT:(>', '<STR_LIT:+>', '<STR_LIT:->']:<EOL><INDENT>self._action(<NUM_LIT:1>)<EOL><DEDENT>elif self.theB == '<STR_LIT:U+0020>':<EOL><INDENT>self._action(<NUM_LIT:3>)<EOL><DEDENT>else:<EOL><INDENT>if isAlphanum(self.theB):<EOL><INDENT>self._action(<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>self._action(<NUM_LIT:2>)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if self.theB == '<STR_LIT:U+0020>':<EOL><INDENT>if isAlphanum(self.theA):<EOL><INDENT>self._action(<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>self._action(<NUM_LIT:3>)<EOL><DEDENT><DEDENT>elif self.theB == '<STR_LIT:\n>':<EOL><INDENT>if self.theA in ['<STR_LIT:}>', '<STR_LIT:]>', '<STR_LIT:)>', '<STR_LIT:+>', '<STR_LIT:->', '<STR_LIT:">', '<STR_LIT>']:<EOL><INDENT>self._action(<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>if isAlphanum(self.theA):<EOL><INDENT>self._action(<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>self._action(<NUM_LIT:3>)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>self._action(<NUM_LIT:1>)<EOL><DEDENT><DEDENT><DEDENT>

Copy the input to the output, deleting the characters which are
           insignificant to JavaScript. Comments will be removed. Tabs will be
           replaced with spaces. Carriage returns will be replaced with linefeeds.
           Most spaces and linefeeds will be removed.


return CssMinifier(level).minify(css)<EOL>

Compress css using level method and return new css as a string.


css = css.replace("<STR_LIT:\r\n>", "<STR_LIT:\n>") <EOL>for rule in _REPLACERS[self.level]:<EOL><INDENT>css = re.compile(rule[<NUM_LIT:0>], re.MULTILINE|re.UNICODE|re.DOTALL).sub(rule[<NUM_LIT:1>], css)<EOL><DEDENT>return css<EOL>

Tries to minimize the length of CSS code passed as parameter. Returns string.


<EOL>paths = []<EOL>for pth in sys_path:<EOL><INDENT>pth = path.abspath(pth)<EOL>if (pth != main_file_dir<EOL>and pth == path.commonprefix((pth, main_file_dir))):<EOL><INDENT>paths.append(pth)<EOL><DEDENT><DEDENT>if paths:<EOL><INDENT>paths.sort()<EOL>return paths[-<NUM_LIT:1>]<EOL><DEDENT>

Find the parent python path that contains the __main__'s file directory

:param main_file_dir: __main__'s file directory
:param sys_path: paths list to match directory against (like sys.path)


msg = ('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>_log_error(msg % e)<EOL>

Log exception as error.
:param e: exception to be logged.


<EOL>fl = main_globals['<STR_LIT>']<EOL>search_path = None<EOL>if not path.isabs(fl) and os.getenv('<STR_LIT>'):<EOL><INDENT>cwd_fl = path.abspath(path.join(os.getenv('<STR_LIT>'), fl))<EOL>main_dir = path.dirname(cwd_fl)<EOL>search_path = _get_search_path(main_dir, sys.path)<EOL><DEDENT>if not search_path:<EOL><INDENT>main_dir = path.dirname(path.abspath(fl))<EOL>search_path = _get_search_path(main_dir, sys.path)<EOL><DEDENT>if not search_path:<EOL><INDENT>main_dir = path.dirname(path.realpath(fl))<EOL>sys_path = [path.realpath(p) for p in sys.path]<EOL>search_path = _get_search_path(main_dir, sys_path)<EOL><DEDENT>return main_dir, search_path<EOL>

Try different strategies to found the path containing the __main__'s file.
Will try strategies, in the following order:
    1. Building file's path with PWD env var.
    2. Building file's path from absolute file's path.
    3. Buidling file's path from real file's path.

:param main_globals: globals dictionary in __main__


<EOL>main_dir, search_path = _try_search_paths(main_globals)<EOL>if not search_path:<EOL><INDENT>_log_debug('<STR_LIT>' % main_dir)<EOL>return<EOL><DEDENT>pkg_str = path.relpath(main_dir, search_path).replace(path.sep, '<STR_LIT:.>')<EOL>site_pkgs = '<STR_LIT>'<EOL>if pkg_str.startswith(site_pkgs):<EOL><INDENT>pkg_str = pkg_str[len(site_pkgs):]<EOL><DEDENT>assert pkg_str<EOL>_log_debug('<STR_LIT>' % pkg_str)<EOL>try:<EOL><INDENT>if '<STR_LIT>' in main_globals['<STR_LIT>']:<EOL><INDENT>_log_debug('<STR_LIT>')<EOL>sys.modules[pkg_str] = sys.modules['<STR_LIT:__main__>']<EOL>sys.modules[pkg_str].__path__ = [main_dir]<EOL>parent_pkg_str = '<STR_LIT:.>'.join(pkg_str.split('<STR_LIT:.>')[:-<NUM_LIT:1>])<EOL>if parent_pkg_str:<EOL><INDENT>importlib.import_module(parent_pkg_str)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>_log_debug('<STR_LIT>' % pkg_str)<EOL>importlib.import_module(pkg_str)<EOL><DEDENT>main_globals['<STR_LIT>'] = pkg_str<EOL>return pkg_str<EOL><DEDENT>except ImportError as e:<EOL><INDENT>_print_exc(e)<EOL><DEDENT>

Find parent python path of __main__. From there solve the package
containing __main__, import it and set __package__ variable.

:param main_globals: globals dictionary in __main__


sys.stderr.write(msg + '<STR_LIT:\n>')<EOL>sys.stderr.flush()<EOL>

Central log function (all levels)
:param msg: message to log


if _log_level <= DEBUG:<EOL><INDENT>if _log_level == TRACE:<EOL><INDENT>traceback.print_stack()<EOL><DEDENT>_log(msg)<EOL><DEDENT>

Log at debug level
:param msg: message to log


if _log_level <= ERROR:<EOL><INDENT>_log(msg)<EOL><DEDENT>

Log at error level
:param msg: message to log


global _initialized<EOL>if _initialized:<EOL><INDENT>return<EOL><DEDENT>else:<EOL><INDENT>_initialized = True<EOL><DEDENT>frame = currentframe()<EOL>frame = frame.f_back<EOL>_init(frame, log_level)<EOL>

Enables explicit relative import in sub-modules when ran as __main__
:param log_level: module's inner logger level (equivalent to logging pkg)


global _log_level<EOL>_log_level = log_level<EOL>main_globals = frame.f_globals<EOL>pkg = main_globals.get('<STR_LIT>')<EOL>file_ = main_globals.get('<STR_LIT>')<EOL>if pkg or not file_:<EOL><INDENT>_log_debug('<STR_LIT>'<EOL>'<STR_LIT>' % (pkg, file_))<EOL>return<EOL><DEDENT>try:<EOL><INDENT>_solve_pkg(main_globals)<EOL><DEDENT>except Exception as e:<EOL><INDENT>_print_exc(e)<EOL><DEDENT>


def __init__(<EOL>self, access_token=DEMO_TOKEN, timeout=DEFAULT_TIMEOUT, websession=None<EOL>):

if websession is None:<EOL><INDENT>async def _create_session():<EOL><INDENT>return aiohttp.ClientSession()<EOL><DEDENT>loop = asyncio.get_event_loop()<EOL>self.websession = loop.run_until_complete(_create_session())<EOL><DEDENT>else:<EOL><INDENT>self.websession = websession<EOL><DEDENT>self._timeout = timeout<EOL>self._access_token = access_token<EOL>self._name = None<EOL>self._home_ids = []<EOL>self._all_home_ids = []<EOL>self._homes = {}<EOL>self.sub_manager = None<EOL>


await self.websession.close()<EOL>


loop = asyncio.get_event_loop()<EOL>task = loop.create_task(self.close_connection())<EOL>loop.run_until_complete(task)<EOL>


if self.sub_manager is not None:<EOL><INDENT>return<EOL><DEDENT>self.sub_manager = SubscriptionManager(<EOL>loop, "<STR_LIT>".format(self._access_token), SUB_ENDPOINT<EOL>)<EOL>self.sub_manager.start()<EOL>

Start subscription manager for real time data.


if self.sub_manager is None:<EOL><INDENT>return<EOL><DEDENT>await self.sub_manager.stop()<EOL>


async def execute(self, document, variable_values=None):

res = await self._execute(document, variable_values)<EOL>if res is None:<EOL><INDENT>return None<EOL><DEDENT>return res.get("<STR_LIT:data>")<EOL>


async def _execute(self, document, variable_values=None, retry=<NUM_LIT:2>):

query_str = print_ast(document)<EOL>payload = {"<STR_LIT>": query_str, "<STR_LIT>": variable_values or {}}<EOL>post_args = {<EOL>"<STR_LIT>": {"<STR_LIT>": "<STR_LIT>" + self._access_token},<EOL>"<STR_LIT:data>": payload,<EOL>}<EOL>try:<EOL><INDENT>with async_timeout.timeout(self._timeout):<EOL><INDENT>resp = await self.websession.post(API_ENDPOINT, **post_args)<EOL><DEDENT>if resp.status != <NUM_LIT:200>:<EOL><INDENT>_LOGGER.error("<STR_LIT>", resp.status)<EOL>return None<EOL><DEDENT>result = await resp.json()<EOL><DEDENT>except aiohttp.ClientError as err:<EOL><INDENT>_LOGGER.error("<STR_LIT>", err, exc_info=True)<EOL>if retry > <NUM_LIT:0>:<EOL><INDENT>return await self._execute(document, variable_values, retry - <NUM_LIT:1>)<EOL><DEDENT>raise<EOL><DEDENT>except asyncio.TimeoutError as err:<EOL><INDENT>_LOGGER.error(<EOL>"<STR_LIT>", err, exc_info=True<EOL>)<EOL>if retry > <NUM_LIT:0>:<EOL><INDENT>return await self._execute(document, variable_values, retry - <NUM_LIT:1>)<EOL><DEDENT>raise<EOL><DEDENT>errors = result.get("<STR_LIT>")<EOL>if errors:<EOL><INDENT>_LOGGER.error("<STR_LIT>", errors)<EOL><DEDENT>return result<EOL>


loop = asyncio.get_event_loop()<EOL>task = loop.create_task(self.update_info())<EOL>loop.run_until_complete(task)<EOL>


query = gql(<EOL>"""<STR_LIT>"""<EOL>)<EOL>res = await self._execute(query)<EOL>if res is None:<EOL><INDENT>return<EOL><DEDENT>errors = res.get("<STR_LIT>", [])<EOL>if errors:<EOL><INDENT>msg = errors[<NUM_LIT:0>].get("<STR_LIT:message>", "<STR_LIT>")<EOL>_LOGGER.error(msg)<EOL>raise InvalidLogin(msg)<EOL><DEDENT>data = res.get("<STR_LIT:data>")<EOL>if not data:<EOL><INDENT>return<EOL><DEDENT>viewer = data.get("<STR_LIT>")<EOL>if not viewer:<EOL><INDENT>return<EOL><DEDENT>self._name = viewer.get("<STR_LIT:name>")<EOL>homes = viewer.get("<STR_LIT>", [])<EOL>self._home_ids = []<EOL>for _home in homes:<EOL><INDENT>home_id = _home.get("<STR_LIT:id>")<EOL>self._all_home_ids += [home_id]<EOL>subs = _home.get("<STR_LIT>")<EOL>if subs:<EOL><INDENT>status = subs[<NUM_LIT:0>].get("<STR_LIT:status>", "<STR_LIT>").lower()<EOL>if not home_id or status != "<STR_LIT>":<EOL><INDENT>continue<EOL><DEDENT><DEDENT>self._home_ids += [home_id]<EOL><DEDENT>


return self._name<EOL>


return self.get_home_ids(only_active=True)<EOL>


if only_active:<EOL><INDENT>return self._home_ids<EOL><DEDENT>return self._all_home_ids<EOL>


return [self.get_home(home_id) for home_id in self.get_home_ids(only_active)]<EOL>


if home_id not in self._all_home_ids:<EOL><INDENT>_LOGGER.error("<STR_LIT>", home_id)<EOL>return None<EOL><DEDENT>if home_id not in self._homes.keys():<EOL><INDENT>self._homes[home_id] = TibberHome(home_id, self)<EOL><DEDENT>return self._homes[home_id]<EOL>

Retun an instance of TibberHome for given home id.


async def send_notification(self, title, message):

query = gql(<EOL>"""<STR_LIT>"""<EOL>% (title, message)<EOL>)<EOL>res = await self.execute(query)<EOL>if not res:<EOL><INDENT>return False<EOL><DEDENT>noti = res.get("<STR_LIT>", {})<EOL>successful = noti.get("<STR_LIT>", False)<EOL>pushed_to_number_of_devices = noti.get("<STR_LIT>", <NUM_LIT:0>)<EOL>_LOGGER.debug(<EOL>"<STR_LIT>",<EOL>successful,<EOL>pushed_to_number_of_devices,<EOL>)<EOL>return successful<EOL>


def __init__(self, home_id, tibber_control):

self._tibber_control = tibber_control<EOL>self._home_id = home_id<EOL>self._current_price_total = None<EOL>self._current_price_info = {}<EOL>self._price_info = {}<EOL>self._level_info = {}<EOL>self.sub_manager = None<EOL>self.info = {}<EOL>self._subscription_id = None<EOL>self._data = None<EOL>


query = gql(<EOL>"""<STR_LIT>"""<EOL>% self._home_id<EOL>)<EOL>self.info = await self._tibber_control.execute(query)<EOL>


loop = asyncio.get_event_loop()<EOL>task = loop.create_task(self.update_current_price_info())<EOL>loop.run_until_complete(task)<EOL>


query = gql(<EOL>"""<STR_LIT>"""<EOL>% self.home_id<EOL>)<EOL>price_info_temp = await self._tibber_control.execute(query)<EOL>if not price_info_temp:<EOL><INDENT>_LOGGER.error("<STR_LIT>")<EOL>return<EOL><DEDENT>try:<EOL><INDENT>home = price_info_temp["<STR_LIT>"]["<STR_LIT>"]<EOL>current_subscription = home["<STR_LIT>"]<EOL>price_info = current_subscription["<STR_LIT>"]["<STR_LIT>"]<EOL><DEDENT>except (KeyError, TypeError):<EOL><INDENT>_LOGGER.error("<STR_LIT>")<EOL>return<EOL><DEDENT>if price_info:<EOL><INDENT>self._current_price_info = price_info<EOL><DEDENT>


loop = asyncio.get_event_loop()<EOL>task = loop.create_task(self.update_price_info())<EOL>loop.run_until_complete(task)<EOL>


query = gql(<EOL>"""<STR_LIT>"""<EOL>% self.home_id<EOL>)<EOL>price_info_temp = await self._tibber_control.execute(query)<EOL>if not price_info_temp:<EOL><INDENT>_LOGGER.error("<STR_LIT>")<EOL>return<EOL><DEDENT>self._price_info = {}<EOL>self._level_info = {}<EOL>for key in ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>try:<EOL><INDENT>home = price_info_temp["<STR_LIT>"]["<STR_LIT>"]<EOL>current_subscription = home["<STR_LIT>"]<EOL>price_info = current_subscription["<STR_LIT>"][key]<EOL><DEDENT>except (KeyError, TypeError):<EOL><INDENT>_LOGGER.error("<STR_LIT>", key)<EOL>continue<EOL><DEDENT>if key == "<STR_LIT>":<EOL><INDENT>self._current_price_info = price_info<EOL>continue<EOL><DEDENT>for data in price_info:<EOL><INDENT>self._price_info[data.get("<STR_LIT>")] = data.get("<STR_LIT>")<EOL>self._level_info[data.get("<STR_LIT>")] = data.get("<STR_LIT>")<EOL><DEDENT><DEDENT>


if not self._current_price_info:<EOL><INDENT>return None<EOL><DEDENT>return self._current_price_info.get("<STR_LIT>")<EOL>


return self._current_price_info<EOL>


return self._price_info<EOL>

Get dictionary with price total, key is date-time.


return self._level_info<EOL>

Get dictionary with price level, key is date-time.


return self._home_id<EOL>


try:<EOL><INDENT>sub = self.info["<STR_LIT>"]["<STR_LIT>"]["<STR_LIT>"]["<STR_LIT:status>"]<EOL><DEDENT>except (KeyError, TypeError):<EOL><INDENT>return False<EOL><DEDENT>return sub == "<STR_LIT>"<EOL>


try:<EOL><INDENT>return self.info["<STR_LIT>"]["<STR_LIT>"]["<STR_LIT>"]["<STR_LIT>"]<EOL><DEDENT>except (KeyError, TypeError):<EOL><INDENT>return False<EOL><DEDENT>


try:<EOL><INDENT>return self.info["<STR_LIT>"]["<STR_LIT>"]["<STR_LIT:address>"]["<STR_LIT>"]<EOL><DEDENT>except (KeyError, TypeError):<EOL><INDENT>_LOGGER.error("<STR_LIT>")<EOL><DEDENT>return "<STR_LIT>"<EOL>


return "<STR_LIT>"<EOL>


try:<EOL><INDENT>current_subscription = self.info["<STR_LIT>"]["<STR_LIT>"]["<STR_LIT>"]<EOL>return current_subscription["<STR_LIT>"]["<STR_LIT>"]["<STR_LIT>"]<EOL><DEDENT>except (KeyError, TypeError, IndexError):<EOL><INDENT>_LOGGER.error("<STR_LIT>")<EOL><DEDENT>return "<STR_LIT>"<EOL>


currency = self.currency<EOL>consumption_unit = self.consumption_unit<EOL>if not currency or not consumption_unit:<EOL><INDENT>_LOGGER.error("<STR_LIT>")<EOL>return "<STR_LIT:U+0020>"<EOL><DEDENT>return currency + "<STR_LIT:/>" + consumption_unit<EOL>


async def rt_subscribe(self, loop, async_callback):

if self._subscription_id is not None:<EOL><INDENT>_LOGGER.error("<STR_LIT>")<EOL>return<EOL><DEDENT>await self._tibber_control.rt_connect(loop)<EOL>document = gql(<EOL>"""<STR_LIT>"""<EOL>% self.home_id<EOL>)<EOL>sub_query = print_ast(document)<EOL>self._subscription_id = await self._tibber_control.sub_manager.subscribe(<EOL>sub_query, async_callback<EOL>)<EOL>

Connect to Tibber and subscribe to Tibber rt subscription.


if self._subscription_id is None:<EOL><INDENT>_LOGGER.error("<STR_LIT>")<EOL>return<EOL><DEDENT>await self._tibber_control.sub_manager.unsubscribe(self._subscription_id)<EOL>


return (<EOL>self._tibber_control.sub_manager is not None<EOL>and self._tibber_control.sub_manager.is_running<EOL>and self._subscription_id is not None<EOL>)<EOL>


async def get_historic_data(self, n_data):

query = gql(<EOL>"""<STR_LIT>"""<EOL>% (self.home_id, n_data)<EOL>)<EOL>data = await self._tibber_control.execute(query)<EOL>if not data:<EOL><INDENT>_LOGGER.error("<STR_LIT>")<EOL>return<EOL><DEDENT>data = data["<STR_LIT>"]["<STR_LIT>"]["<STR_LIT>"]<EOL>if data is None:<EOL><INDENT>self._data = []<EOL>return<EOL><DEDENT>self._data = data["<STR_LIT>"]<EOL>


loop = asyncio.get_event_loop()<EOL>task = loop.create_task(self.get_historic_data(n_data))<EOL>loop.run_until_complete(task)<EOL>return self._data<EOL>


self.tibber = tibber.Tibber()<EOL>self.tibber.sync_update_info()<EOL>

things to be run when tests are started.


self.tibber.sync_close_connection()<EOL>


async def _create_session():<EOL><INDENT>return aiohttp.ClientSession()<EOL><DEDENT>loop = asyncio.get_event_loop()<EOL>self.websession = loop.run_until_complete(_create_session())<EOL>self.tibber = tibber.Tibber(websession=self.websession)<EOL>self.tibber.sync_update_info()<EOL>


self.tibber = tibber.Tibber(access_token='<STR_LIT>')        <EOL>self.assertRaises(tibber.InvalidLogin, self.tibber.sync_update_info)<EOL>


self.tibber = tibber.Tibber(access_token='<STR_LIT>')<EOL>self.tibber.sync_update_info()<EOL>


if len(self.geometries) == <NUM_LIT:0>:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>return server.post(<EOL>'<STR_LIT>',<EOL>self.as_payload(),<EOL>replacements={<EOL>'<STR_LIT>': self.__challenge__.slug,<EOL>'<STR_LIT>': self.identifier})<EOL>


return server.put(<EOL>'<STR_LIT>',<EOL>self.as_payload(),<EOL>replacements={<EOL>'<STR_LIT>': self.__challenge__.slug,<EOL>'<STR_LIT>': self.identifier})<EOL>


try:<EOL><INDENT>server.get(<EOL>'<STR_LIT>',<EOL>replacements={<EOL>'<STR_LIT>': self.__challenge__.slug,<EOL>'<STR_LIT>': self.identifier})<EOL><DEDENT>except Exception:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>


@classmethod<EOL><INDENT>def from_server(cls, server, slug, identifier):<DEDENT>

task = server.get(<EOL>'<STR_LIT>',<EOL>replacements={<EOL>'<STR_LIT>': slug,<EOL>'<STR_LIT>': identifier})<EOL>return cls(**task)<EOL>


return cls(**payload)<EOL>


return server.post(<EOL>'<STR_LIT>',<EOL>self.as_payload(),<EOL>replacements={'<STR_LIT>': self.slug})<EOL>


return server.put(<EOL>'<STR_LIT>',<EOL>self.as_payload(),<EOL>replacements={'<STR_LIT>': self.slug})<EOL>


try:<EOL><INDENT>server.get(<EOL>'<STR_LIT>',<EOL>replacements={'<STR_LIT>': self.slug})<EOL><DEDENT>except Exception:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

Check if a challenge exists on the server


@classmethod<EOL><INDENT>def from_server(cls, server, slug):<DEDENT>

challenge = server.get(<EOL>'<STR_LIT>',<EOL>replacements={'<STR_LIT>': slug})<EOL>return cls(<EOL>**challenge)<EOL>

Retrieve a challenge from the MapRoulette server
        :type server


for chunk in self.__cut_to_size():<EOL><INDENT>server.post(<EOL>'<STR_LIT>',<EOL>chunk.as_payload(),<EOL>replacements={<EOL>'<STR_LIT>': chunk.challenge.slug})<EOL><DEDENT>


for chunk in self.__cut_to_size():<EOL><INDENT>server.put(<EOL>'<STR_LIT>',<EOL>chunk.as_payload(),<EOL>replacements={<EOL>'<STR_LIT>': chunk.challenge.slug})<EOL><DEDENT>


if not self.challenge.exists(server):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>existing = MapRouletteTaskCollection.from_server(server, self.challenge)<EOL>same = []<EOL>new = []<EOL>changed = []<EOL>deleted = []<EOL>for task in self.tasks:<EOL><INDENT>if task.identifier in [existing_task.identifier for existing_task in existing.tasks]:<EOL><INDENT>if task == existing.get_by_identifier(task.identifier):<EOL><INDENT>same.append(task)<EOL><DEDENT>else:<EOL><INDENT>changed.append(task)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>new.append(task)<EOL><DEDENT><DEDENT>for task in existing.tasks:<EOL><INDENT>if task.identifier not in [task.identifier for task in self.tasks]:<EOL><INDENT>deleted.append(task)<EOL><DEDENT><DEDENT>if new:<EOL><INDENT>newCollection = MapRouletteTaskCollection(self.challenge, tasks=new)<EOL>newCollection.create(server)<EOL><DEDENT>if changed:<EOL><INDENT>changedCollection = MapRouletteTaskCollection(self.challenge, tasks=changed)<EOL>changedCollection.update(server)<EOL><DEDENT>if deleted:<EOL><INDENT>deletedCollection = MapRouletteTaskCollection(self.challenge, tasks=deleted)<EOL>for task in deletedCollection.tasks:<EOL><INDENT>task.status = '<STR_LIT>'<EOL><DEDENT>deletedCollection.update(server)<EOL><DEDENT>return {'<STR_LIT>': same, '<STR_LIT>': new, '<STR_LIT>': changed, '<STR_LIT>': deleted}<EOL>

Reconcile this collection with the server.


self.tasks.append(task)<EOL>


task_collection = MapRouletteTaskCollection(challenge)<EOL>i = <NUM_LIT:0><EOL>while i < self.A_TON:<EOL><INDENT>i += <NUM_LIT:1><EOL>task_collection.tasks.append(<EOL>MapRouletteTask(<EOL>challenge=challenge,<EOL>identifier='<STR_LIT>'.format(uuid.uuid4()),<EOL>geometries=self.__random_point()))<EOL><DEDENT>return task_collection<EOL>

Return a collection of A_TON of tasks with random Point geometries


def add(self, addend_mat, axis=<NUM_LIT:1>):

if self.finalized:<EOL><INDENT>if axis == <NUM_LIT:0>:<EOL><INDENT>raise NotImplementedError('<STR_LIT>')<EOL><DEDENT>elif axis == <NUM_LIT:1>:<EOL><INDENT>for hid in xrange(self.shape[<NUM_LIT:1>]):<EOL><INDENT>self.data[hid] = self.data[hid] + addend_mat<EOL><DEDENT><DEDENT>elif axis == <NUM_LIT:2>:<EOL><INDENT>raise NotImplementedError('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>

In-place addition

:param addend_mat: A matrix to be added on the Sparse3DMatrix object
:param axis: The dimension along the addend_mat is added
:return: Nothing (as it performs in-place operations)


def multiply(self, multiplier, axis=None):

if self.finalized:<EOL><INDENT>if multiplier.ndim == <NUM_LIT:1>:<EOL><INDENT>if axis == <NUM_LIT:0>:  <EOL><INDENT>raise NotImplementedError('<STR_LIT>')<EOL><DEDENT>elif axis == <NUM_LIT:1>:  <EOL><INDENT>sz = len(multiplier)<EOL>multiplier_mat = lil_matrix((sz, sz))<EOL>multiplier_mat.setdiag(multiplier)<EOL>for hid in xrange(self.shape[<NUM_LIT:1>]):<EOL><INDENT>self.data[hid] = self.data[hid] * multiplier_mat<EOL><DEDENT><DEDENT>elif axis == <NUM_LIT:2>:  <EOL><INDENT>for hid in xrange(self.shape[<NUM_LIT:1>]):<EOL><INDENT>self.data[hid].data *= multiplier[self.data[hid].indices]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT><DEDENT>elif multiplier.ndim == <NUM_LIT:2>:<EOL><INDENT>if axis == <NUM_LIT:0>:  <EOL><INDENT>for hid in xrange(self.shape[<NUM_LIT:1>]):<EOL><INDENT>self.data[hid].data *= multiplier[self.data[hid].indices, hid]<EOL><DEDENT><DEDENT>elif axis == <NUM_LIT:1>:  <EOL><INDENT>for hid in xrange(self.shape[<NUM_LIT:1>]):<EOL><INDENT>self.data[hid] = self.data[hid].multiply(multiplier)<EOL><DEDENT><DEDENT>elif axis == <NUM_LIT:2>:  <EOL><INDENT>for hid in xrange(self.shape[<NUM_LIT:1>]):<EOL><INDENT>multiplier_vec = multiplier[hid, :]<EOL>multiplier_vec = multiplier_vec.ravel()<EOL>self.data[hid].data *= multiplier_vec.repeat(np.diff(self.data[hid].indptr))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT><DEDENT>elif isinstance(multiplier, Sparse3DMatrix):  <EOL><INDENT>for hid in xrange(self.shape[<NUM_LIT:1>]):<EOL><INDENT>self.data[hid] = self.data[hid].multiply(multiplier.data[hid])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>

In-place multiplication

:param multiplier: A matrix or vector to be multiplied
:param axis: The dim along which 'multiplier' is multiplied
:return: Nothing (as it performs in-place operations)


def prepare(self, pseudocount=<NUM_LIT:0.0>, lenfile=None, read_length=<NUM_LIT:100>):

if self.probability.num_groups > <NUM_LIT:0>:<EOL><INDENT>self.grp_conv_mat = lil_matrix((self.probability.num_loci, self.probability.num_groups))<EOL>for i in range(self.probability.num_groups):<EOL><INDENT>self.grp_conv_mat[self.probability.groups[i], i] = <NUM_LIT:1.0><EOL><DEDENT>self.grp_conv_mat = self.grp_conv_mat.tocsc()<EOL>self.t2t_mat = eye(self.probability.num_loci, self.probability.num_loci)<EOL>self.t2t_mat = self.t2t_mat.tolil()<EOL>for tid_list in self.probability.groups:<EOL><INDENT>for ii in range(len(tid_list)):<EOL><INDENT>for jj in range(ii):<EOL><INDENT>i = tid_list[ii]<EOL>j = tid_list[jj]<EOL>self.t2t_mat[i, j] = <NUM_LIT:1><EOL>self.t2t_mat[j, i] = <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>self.t2t_mat = self.t2t_mat.tocsc()<EOL><DEDENT>if lenfile is not None:<EOL><INDENT>hid = dict(list(zip(self.probability.hname, np.arange(len(self.probability.hname)))))<EOL>self.target_lengths = np.zeros((self.probability.num_loci, self.probability.num_haplotypes))<EOL>if self.probability.num_haplotypes > <NUM_LIT:1>:<EOL><INDENT>with open(lenfile) as fh:<EOL><INDENT>for curline in fh:<EOL><INDENT>item = curline.rstrip().split("<STR_LIT:\t>")<EOL>locus, hap = item[<NUM_LIT:0>].split("<STR_LIT:_>")<EOL>self.target_lengths[self.probability.lid[locus], hid[hap]] = max(float(item[<NUM_LIT:1>]) - read_length + <NUM_LIT:1.0>, <NUM_LIT:1.0>)<EOL><DEDENT><DEDENT><DEDENT>elif self.probability.num_haplotypes > <NUM_LIT:0>:<EOL><INDENT>with open(lenfile) as fh:<EOL><INDENT>for curline in fh:<EOL><INDENT>item = curline.rstrip().split("<STR_LIT:\t>")<EOL>self.target_lengths[self.probability.lid[item[<NUM_LIT:0>]], <NUM_LIT:0>] = max(float(item[<NUM_LIT:1>]) - read_length + <NUM_LIT:1.0>, <NUM_LIT:1.0>)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>self.target_lengths = self.target_lengths.transpose()<EOL>if not np.all(self.target_lengths > <NUM_LIT:0.0>):<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT><DEDENT>self.probability.normalize_reads(axis=APM.Axis.READ)  <EOL>self.allelic_expression = self.probability.sum(axis=APM.Axis.READ)<EOL>if self.target_lengths is not None:  <EOL><INDENT>self.allelic_expression = np.divide(self.allelic_expression, self.target_lengths)<EOL><DEDENT>if pseudocount > <NUM_LIT:0.0>:  <EOL><INDENT>orig_allelic_expression_sum = self.allelic_expression.sum()<EOL>nzloci = np.nonzero(self.allelic_expression)[<NUM_LIT:1>]<EOL>self.allelic_expression[:, nzloci] += pseudocount<EOL>self.allelic_expression *= (orig_allelic_expression_sum / self.allelic_expression.sum())<EOL><DEDENT>

Initializes the probability of read origin according to the alignment profile

:param pseudocount: Uniform prior for allele specificity estimation
:return: Nothing (as it performs an in-place operations)


self.probability.reset()<EOL>self.probability.normalize_reads(axis=APM.Axis.READ)  <EOL>self.allelic_expression = self.probability.sum(axis=APM.Axis.READ)<EOL>if self.target_lengths is not None:  <EOL><INDENT>self.allelic_expression = np.divide(self.allelic_expression, self.target_lengths)<EOL><DEDENT>if pseudocount > <NUM_LIT:0.0>:  <EOL><INDENT>orig_allelic_expression_sum = self.allelic_expression.sum()<EOL>nzloci = np.nonzero(self.allelic_expression)[<NUM_LIT:1>]<EOL>self.allelic_expression[:, nzloci] += pseudocount<EOL>self.allelic_expression *= (orig_allelic_expression_sum / self.allelic_expression.sum())<EOL><DEDENT>


self.probability.reset()  <EOL>if model == <NUM_LIT:1>:<EOL><INDENT>self.probability.multiply(self.allelic_expression, axis=APM.Axis.READ)<EOL>self.probability.normalize_reads(axis=APM.Axis.HAPLOGROUP, grouping_mat=self.t2t_mat)<EOL>haplogroup_sum_mat = self.allelic_expression * self.t2t_mat<EOL>self.probability.multiply(haplogroup_sum_mat, axis=APM.Axis.READ)<EOL>self.probability.normalize_reads(axis=APM.Axis.GROUP, grouping_mat=self.t2t_mat)<EOL>self.probability.multiply(haplogroup_sum_mat.sum(axis=<NUM_LIT:0>), axis=APM.Axis.HAPLOTYPE)<EOL>self.probability.normalize_reads(axis=APM.Axis.READ)<EOL><DEDENT>elif model == <NUM_LIT:2>:<EOL><INDENT>self.probability.multiply(self.allelic_expression, axis=APM.Axis.READ)<EOL>self.probability.normalize_reads(axis=APM.Axis.LOCUS)<EOL>self.probability.multiply(self.allelic_expression.sum(axis=<NUM_LIT:0>), axis=APM.Axis.HAPLOTYPE)<EOL>self.probability.normalize_reads(axis=APM.Axis.GROUP, grouping_mat=self.t2t_mat)<EOL>self.probability.multiply((self.allelic_expression * self.t2t_mat).sum(axis=<NUM_LIT:0>), axis=APM.Axis.HAPLOTYPE)<EOL>self.probability.normalize_reads(axis=APM.Axis.READ)<EOL><DEDENT>elif model == <NUM_LIT:3>:<EOL><INDENT>self.probability.multiply(self.allelic_expression, axis=APM.Axis.READ)<EOL>self.probability.normalize_reads(axis=APM.Axis.GROUP, grouping_mat=self.t2t_mat)<EOL>self.probability.multiply((self.allelic_expression * self.t2t_mat).sum(axis=<NUM_LIT:0>), axis=APM.Axis.HAPLOTYPE)<EOL>self.probability.normalize_reads(axis=APM.Axis.READ)<EOL><DEDENT>elif model == <NUM_LIT:4>:<EOL><INDENT>self.probability.multiply(self.allelic_expression, axis=APM.Axis.READ)<EOL>self.probability.normalize_reads(axis=APM.Axis.READ)<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>

Updates the probability of read origin at read level

:param model: Normalization model (1: Gene->Allele->Isoform, 2: Gene->Isoform->Allele, 3: Gene->Isoform*Allele, 4: Gene*Isoform*Allele)
:return: Nothing (as it performs in-place operations)


self.update_probability_at_read_level(model)<EOL>self.allelic_expression = self.probability.sum(axis=APM.Axis.READ)<EOL>if self.target_lengths is not None:<EOL><INDENT>self.allelic_expression = np.divide(self.allelic_expression, self.target_lengths)<EOL><DEDENT>

A single EM step: Update probability at read level and then re-estimate allelic specific expression

:param model: Normalization model (1: Gene->Allele->Isoform, 2: Gene->Isoform->Allele, 3: Gene->Isoform*Allele, 4: Gene*Isoform*Allele)
:return: Nothing (as it performs in-place operations)


def run(self, model, tol=<NUM_LIT>, max_iters=<NUM_LIT>, verbose=True):

orig_err_states = np.seterr(all='<STR_LIT>')<EOL>np.seterr(under='<STR_LIT:ignore>')<EOL>if verbose:<EOL><INDENT>print()<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL><DEDENT>num_iters = <NUM_LIT:0><EOL>err_sum = <NUM_LIT><EOL>time0 = time.time()<EOL>target_err = <NUM_LIT> * tol<EOL>while err_sum > target_err and num_iters < max_iters:<EOL><INDENT>prev_isoform_expression = self.get_allelic_expression().sum(axis=<NUM_LIT:0>)<EOL>prev_isoform_expression *= (<NUM_LIT> / prev_isoform_expression.sum())<EOL>self.update_allelic_expression(model=model)<EOL>curr_isoform_expression = self.get_allelic_expression().sum(axis=<NUM_LIT:0>)<EOL>curr_isoform_expression *= (<NUM_LIT> / curr_isoform_expression.sum())<EOL>err = np.abs(curr_isoform_expression - prev_isoform_expression)<EOL>err_sum = err.sum()<EOL>num_iters += <NUM_LIT:1><EOL>if verbose:<EOL><INDENT>time1 = time.time()<EOL>delmin, s = divmod(int(time1 - time0), <NUM_LIT>)<EOL>h, m = divmod(delmin, <NUM_LIT>)<EOL>print("<STR_LIT>" % (num_iters, h, m, s, err_sum))<EOL><DEDENT><DEDENT>

Runs EM iterations

:param model: Normalization model (1: Gene->Allele->Isoform, 2: Gene->Isoform->Allele, 3: Gene->Isoform*Allele, 4: Gene*Isoform*Allele)
:param tol: Tolerance for termination
:param max_iters: Maximum number of iterations until termination
:param verbose: Display information on how EM is running
:return: Nothing (as it performs in-place operations)


def report_read_counts(self, filename, grp_wise=False, reorder='<STR_LIT>', notes=None):

expected_read_counts = self.probability.sum(axis=APM.Axis.READ)<EOL>if grp_wise:<EOL><INDENT>lname = self.probability.gname<EOL>expected_read_counts = expected_read_counts * self.grp_conv_mat<EOL><DEDENT>else:<EOL><INDENT>lname = self.probability.lname<EOL><DEDENT>total_read_counts = expected_read_counts.sum(axis=<NUM_LIT:0>)<EOL>if reorder == '<STR_LIT>':<EOL><INDENT>report_order = np.argsort(total_read_counts.flatten())<EOL>report_order = report_order[::-<NUM_LIT:1>]<EOL><DEDENT>elif reorder == '<STR_LIT>':<EOL><INDENT>report_order = np.argsort(total_read_counts.flatten())<EOL><DEDENT>elif reorder == '<STR_LIT>':<EOL><INDENT>report_order = np.arange(len(lname))  <EOL><DEDENT>cntdata = np.vstack((expected_read_counts, total_read_counts))<EOL>fhout = open(filename, '<STR_LIT:w>')<EOL>fhout.write("<STR_LIT>" + "<STR_LIT:\t>".join(self.probability.hname) + "<STR_LIT>")<EOL>if notes is not None:<EOL><INDENT>fhout.write("<STR_LIT>")<EOL><DEDENT>fhout.write("<STR_LIT:\n>")<EOL>for locus_id in report_order:<EOL><INDENT>lname_cur = lname[locus_id]<EOL>fhout.write("<STR_LIT:\t>".join([lname_cur] + list(map(str, cntdata[:, locus_id].ravel()))))<EOL>if notes is not None:<EOL><INDENT>fhout.write("<STR_LIT>" % notes[lname_cur])<EOL><DEDENT>fhout.write("<STR_LIT:\n>")<EOL><DEDENT>fhout.close()<EOL>

Exports expected read counts

:param filename: File name for output
:param grp_wise: whether the report is at isoform level or gene level
:param reorder: whether the report should be either 'decreasing' or 'increasing' order or just 'as-is'
:return: Nothing but the method writes a file


def report_depths(self, filename, tpm=True, grp_wise=False, reorder='<STR_LIT>', notes=None):

if grp_wise:<EOL><INDENT>lname = self.probability.gname<EOL>depths = self.allelic_expression * self.grp_conv_mat<EOL><DEDENT>else:<EOL><INDENT>lname = self.probability.lname<EOL>depths = self.allelic_expression<EOL><DEDENT>if tpm:<EOL><INDENT>depths *= (<NUM_LIT> / depths.sum())<EOL><DEDENT>total_depths = depths.sum(axis=<NUM_LIT:0>)<EOL>if reorder == '<STR_LIT>':<EOL><INDENT>report_order = np.argsort(total_depths.flatten())<EOL>report_order = report_order[::-<NUM_LIT:1>]<EOL><DEDENT>elif reorder == '<STR_LIT>':<EOL><INDENT>report_order = np.argsort(total_depths.flatten())<EOL><DEDENT>elif reorder == '<STR_LIT>':<EOL><INDENT>report_order = np.arange(len(lname))  <EOL><DEDENT>cntdata = np.vstack((depths, total_depths))<EOL>fhout = open(filename, '<STR_LIT:w>')<EOL>fhout.write("<STR_LIT>" + "<STR_LIT:\t>".join(self.probability.hname) + "<STR_LIT>")<EOL>if notes is not None:<EOL><INDENT>fhout.write("<STR_LIT>")<EOL><DEDENT>fhout.write("<STR_LIT:\n>")<EOL>for locus_id in report_order:<EOL><INDENT>lname_cur = lname[locus_id]<EOL>fhout.write("<STR_LIT:\t>".join([lname_cur] + list(map(str, cntdata[:, locus_id].ravel()))))<EOL>if notes is not None:<EOL><INDENT>fhout.write("<STR_LIT>" % notes[lname_cur])<EOL><DEDENT>fhout.write("<STR_LIT:\n>")<EOL><DEDENT>fhout.close()<EOL>

Exports expected depths

:param filename: File name for output
:param grp_wise: whether the report is at isoform level or gene level
:param reorder: whether the report should be either 'decreasing' or 'increasing' order or just 'as-is'
:return: Nothing but the method writes a file


def export_posterior_probability(self, filename, title="<STR_LIT>"):

self.probability.save(h5file=filename, title=title)<EOL>

Writes the posterior probability of read origin

:param filename: File name for output
:param title: The title of the posterior probability matrix
:return: Nothing but the method writes a file in EMASE format (PyTables)


def bundle(self, reset=False, shallow=False):

if self.finalized:<EOL><INDENT>if self.groups is not None and self.gname is not None:<EOL><INDENT>grp_conv_mat = lil_matrix((self.num_loci, self.num_groups))<EOL>for i in range(self.num_groups):<EOL><INDENT>grp_conv_mat[self.groups[i], i] = <NUM_LIT:1.0><EOL><DEDENT>grp_align = Sparse3DMatrix.__mul__(self, grp_conv_mat) <EOL>grp_align.num_loci = self.num_groups<EOL>grp_align.num_haplotypes = self.num_haplotypes<EOL>grp_align.num_reads = self.num_reads<EOL>grp_align.shape = (grp_align.num_loci, grp_align.num_haplotypes, grp_align.num_reads)<EOL>if not shallow:<EOL><INDENT>grp_align.lname = copy.copy(self.gname)<EOL>grp_align.hname = self.hname<EOL>grp_align.rname = copy.copy(self.rname)<EOL>grp_align.lid   = dict(list(zip(grp_align.lname, np.arange(grp_align.num_loci))))<EOL>grp_align.rid   = copy.copy(self.rid)<EOL><DEDENT>if reset:<EOL><INDENT>grp_align.reset()<EOL><DEDENT>return grp_align<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>

Returns ``AlignmentPropertyMatrix`` object in which loci are bundled using grouping information.

:param reset: whether to reset the values at the loci
:param shallow: whether to copy all the meta data


def normalize_reads(self, axis, grouping_mat=None):

if self.finalized:<EOL><INDENT>if axis == self.Axis.LOCUS:  <EOL><INDENT>normalizer = self.sum(axis=self.Axis.HAPLOTYPE)  <EOL>normalizer.eliminate_zeros()<EOL>for hid in range(self.num_haplotypes):<EOL><INDENT>self.data[hid].eliminate_zeros()  <EOL>self.data[hid] = np.divide(self.data[hid], normalizer)  <EOL><DEDENT><DEDENT>elif axis == self.Axis.HAPLOTYPE:  <EOL><INDENT>for hid in range(self.num_haplotypes):<EOL><INDENT>normalizer = self.data[hid].sum(axis=self.Axis.HAPLOTYPE)  <EOL>normalizer = normalizer.A.flatten()<EOL>self.data[hid].data /= normalizer[self.data[hid].indices]<EOL><DEDENT><DEDENT>elif axis == self.Axis.READ:  <EOL><INDENT>sum_mat = self.sum(axis=self.Axis.LOCUS)<EOL>normalizer = sum_mat.sum(axis=self.Axis.HAPLOTYPE)<EOL>normalizer = normalizer.ravel()<EOL>for hid in range(self.num_haplotypes):<EOL><INDENT>self.data[hid].data /= normalizer[self.data[hid].indices]<EOL><DEDENT><DEDENT>elif axis == self.Axis.GROUP:  <EOL><INDENT>if grouping_mat is None:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>normalizer = self.sum(axis=self.Axis.HAPLOTYPE) * grouping_mat<EOL>for hid in range(self.num_haplotypes):<EOL><INDENT>self.data[hid].eliminate_zeros()  <EOL>self.data[hid] = np.divide(self.data[hid], normalizer)<EOL><DEDENT><DEDENT>elif axis == self.Axis.HAPLOGROUP:  <EOL><INDENT>if grouping_mat is None:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>for hid in range(self.num_haplotypes):  <EOL><INDENT>normalizer = self.data[hid] * grouping_mat  <EOL>self.data[hid].eliminate_zeros()  <EOL>self.data[hid] = np.divide(self.data[hid], normalizer)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>

Read-wise normalization

:param axis: The dimension along which we want to normalize values
:param grouping_mat: An incidence matrix that specifies which isoforms are from a same gene
:return: Nothing (as the method performs in-place operations)
:rtype: None


def pull_alignments_from(self, reads_to_use, shallow=False):

new_alnmat = self.copy(shallow=shallow)<EOL>for hid in range(self.num_haplotypes):<EOL><INDENT>hdata = new_alnmat.data[hid]<EOL>hdata.data *= reads_to_use[hdata.indices]<EOL>hdata.eliminate_zeros()<EOL><DEDENT>if new_alnmat.count is not None:<EOL><INDENT>new_alnmat.count[np.logical_not(reads_to_use)] = <NUM_LIT:0><EOL><DEDENT>return new_alnmat<EOL>

Pull out alignments of certain reads

:param reads_to_use: numpy array of dtype=bool specifying which reads to use
:param shallow: whether to copy sparse 3D matrix only or not
:return: a new AlignmentPropertyMatrix object that particular reads are


def get_unique_reads(self, ignore_haplotype=False, shallow=False):

if self.finalized:<EOL><INDENT>if ignore_haplotype:<EOL><INDENT>summat = self.sum(axis=self.Axis.HAPLOTYPE)<EOL>nnz_per_read = np.diff(summat.tocsr().indptr)<EOL>unique_reads = np.logical_and(nnz_per_read > <NUM_LIT:0>, nnz_per_read < <NUM_LIT:2>)<EOL><DEDENT>else:  <EOL><INDENT>alncnt_per_read = self.sum(axis=self.Axis.LOCUS).sum(axis=self.Axis.HAPLOTYPE)<EOL>unique_reads = np.logical_and(alncnt_per_read > <NUM_LIT:0>, alncnt_per_read < <NUM_LIT:2>)<EOL><DEDENT>return self.pull_alignments_from(unique_reads, shallow=shallow)<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>

Pull out alignments of uniquely-aligning reads

:param ignore_haplotype: whether to regard allelic multiread as uniquely-aligning read
:param shallow: whether to copy sparse 3D matrix only or not
:return: a new AlignmentPropertyMatrix object that particular reads are


if self.rname is not None:<EOL><INDENT>print(self.rname[rid])<EOL>print('<STR_LIT>')<EOL><DEDENT>r = self.get_read_data(rid)<EOL>aligned_loci = np.unique(r.nonzero()[<NUM_LIT:1>])<EOL>for locus in aligned_loci:<EOL><INDENT>nzvec = r[:, locus].todense().transpose()[<NUM_LIT:0>].A.flatten()<EOL>if self.lname is not None:<EOL><INDENT>print(self.lname[locus], end='<STR_LIT:U+0020>')<EOL><DEDENT>else:<EOL><INDENT>print(locus, end='<STR_LIT:U+0020>')<EOL><DEDENT>print(nzvec)<EOL><DEDENT>


def __getattr__(self, name, *args, **kwargs):

def function(*args, **kwargs):<EOL><INDENT>if name not in StrictRedisCluster._loop_keys:<EOL><INDENT>tag_start = None<EOL>key_type = hash_tag = '<STR_LIT>'<EOL>list_ht = []<EOL>if isinstance(args[<NUM_LIT:0>], (basestring, bytes)):<EOL><INDENT>key_type = '<STR_LIT:string>'<EOL>list_ht.append(args[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>if isinstance(args[<NUM_LIT:0>], list):<EOL><INDENT>key_type = '<STR_LIT:list>'<EOL>list_ht.append(args[<NUM_LIT:0>][<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>key_type = '<STR_LIT>'<EOL>list_ht = iterkeys(args[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>for k in list_ht:<EOL><INDENT>try:<EOL><INDENT>tag_start = k.index('<STR_LIT:{>')<EOL>hash_tag = k<EOL>break<EOL><DEDENT>except Exception as e:<EOL><INDENT>tag_start = None<EOL><DEDENT><DEDENT>if name in StrictRedisCluster._tag_keys and not tag_start:<EOL><INDENT>try:<EOL><INDENT>return getattr(self, '<STR_LIT>' + name)(*args, **kwargs)<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise redis.DataError("<STR_LIT>" % name)<EOL><DEDENT><DEDENT>hkey = args[<NUM_LIT:0>]<EOL>if tag_start is not None:<EOL><INDENT>L = list(args)<EOL>if key_type != '<STR_LIT:string>':<EOL><INDENT>if key_type == '<STR_LIT:list>':<EOL><INDENT>hkey = L[<NUM_LIT:0>][<NUM_LIT:0>][tag_start + <NUM_LIT:1>:-<NUM_LIT:1>]<EOL>L[<NUM_LIT:0>][<NUM_LIT:0>] = L[<NUM_LIT:0>][<NUM_LIT:0>][<NUM_LIT:0>:tag_start]<EOL><DEDENT>else:<EOL><INDENT>hkey = hash_tag[tag_start + <NUM_LIT:1>:-<NUM_LIT:1>]<EOL>L[<NUM_LIT:0>][hash_tag[<NUM_LIT:0>:tag_start]] = L[<NUM_LIT:0>][hash_tag]<EOL>del L[<NUM_LIT:0>][hash_tag]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>hkey = L[<NUM_LIT:0>][tag_start + <NUM_LIT:1>:-<NUM_LIT:1>]<EOL>L[<NUM_LIT:0>] = L[<NUM_LIT:0>][<NUM_LIT:0>:tag_start]<EOL><DEDENT>args = tuple(L)<EOL><DEDENT>node = self._getnodenamefor(hkey)<EOL>if name in StrictRedisCluster._write_keys:<EOL><INDENT>redisent = self.redises[node]<EOL><DEDENT>elif name in StrictRedisCluster._read_keys:<EOL><INDENT>redisent = self.redises[node + '<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>raise redis.DataError("<STR_LIT>" % name)<EOL><DEDENT>return getattr(redisent, name)(*args, **kwargs)<EOL><DEDENT>else:<EOL><INDENT>if name not in self._loop_keys_admin:<EOL><INDENT>try:<EOL><INDENT>return getattr(self, '<STR_LIT>' + name)(*args, **kwargs)<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise redis.DataError("<STR_LIT>" % name)<EOL><DEDENT><DEDENT>result = {}<EOL>for alias, redisent in iteritems(self.redises):<EOL><INDENT>if (name in StrictRedisCluster._write_keys and alias.find('<STR_LIT>') >= <NUM_LIT:0>) or (name in StrictRedisCluster._read_keys and alias.find('<STR_LIT>') == -<NUM_LIT:1>):<EOL><INDENT>res = None<EOL><DEDENT>else:<EOL><INDENT>res = getattr(redisent, name)(*args, **kwargs)<EOL><DEDENT>result[alias] = res<EOL><DEDENT>return result<EOL><DEDENT><DEDENT>return function<EOL>

Magic method to handle all redis commands
- string name The name of the command called.
- tuple args of supplied arguments to the command.


return '<STR_LIT>' + str(<EOL>(abs(binascii.crc32(b(name)) & <NUM_LIT>) % self.no_servers) + <NUM_LIT:1>)<EOL>

Return the node name where the ``name`` would land to


node = self._getnodenamefor(name)<EOL>return {node: self.cluster['<STR_LIT>'][node]}<EOL>

Return the node where the ``name`` would land to


return self.set(name, value)<EOL>

Set the value at key ``name`` to ``value``


value = self.get(name)<EOL>if value:<EOL><INDENT>return value<EOL><DEDENT>raise KeyError(name)<EOL>

Return the value at key ``name``, raises a KeyError if the key
doesn't exist.


return self.delete(*names)<EOL>

Delete one or more keys specified by ``names``


redisent = self.redises[self._getnodenamefor(key) + '<STR_LIT>']<EOL>return getattr(redisent, '<STR_LIT:object>')(infotype, key)<EOL>

Return the encoding, idletime, or refcount about the key