<commit_before>"""
Survey response summary calculation

Calculation builds up a nested dict structure which makes referencing columns
and specific values easy. This is converted to an array structure for Django
Template Language to be able to put it on the screen.

The results nested dict looks something like
{u'demographics_group/age': {
     'label': u'How old are you?',
     'options': {
         u'under_25': {'count': {'female': 53,
                            'male': 11},
                       'idx': 0,
                       'key': ,
                       'label': u'Under 25 years old',
                       'pct': {'female': 26.903553299492383,
                               'male': 10.2803738317757}},
                       {'count': {'female': 101,
                                  'male': 56},
                        'idx': 1,
                        'key': u'26_40',
                        'label': u'26 - 40 years old',
                        'pct': {'female': 51.26903553299492,
                                'male': 52.336448598130836}},
where the keys are group path names then the q name separated by /
and options are under the options field keyed on option name.

The results array looks something like
[{'key': u'demographics_group/age',
  'label': u'How old are you?',
  'options': [{'count': {'female': 53,
                         'male': 11},
               'idx': 0,
               'key': u'under_25',
               'label': u'Under 25 years old',
               'pct': {'female': 26.903553299492383,
                       'male': 10.2803738317757}},
where questions are in an array and each question's options are
in an array under the question's options field.
"""

from logging import getLogger

# These are used for grouping. Trying to count them and group by them
# at the same time doesn't work.
SKIP_QUESTIONS = [['_uuid'], ['demographics_group', 'gender']]
GENDER_COLUMN = 'demographics_group/gender'

log = getLogger(__name__)

class Element(object):
    def __init__(self, element, path):
        self.label = element['label']
        self.name = element['name']
        self.path = path + [element['name']]
        self.pathstr = pathstr(self.path)

    def __str__(self):
        return "%s %s" % (self.__class__, self.pathstr)

class MultipleChoice(Element):
    def __init__(self, question, path, group_labels):
        super(MultipleChoice, self).__init__(question, path)
        self.options = [Option(o, self.path) for o in question['children']]
        self.group_labels = group_labels

class Option(Element):
    pass

class SelectOne(MultipleChoice):
    pass

class SelectAllThatApply(MultipleChoice):
    pass

def count_submissions(submissions):
    results = {}
    # per-gender counts
    cols = ['_uuid', GENDER_COLUMN]
    question_table = submissions.loc[:, cols]
    gender_counts = question_table.groupby(
        [GENDER_COLUMN]
    ).count()
    results['female'] = int(gender_counts.loc['female'])
    results['male'] = int(gender_counts.loc['male'])
    results['total'] = int(submissions.loc[:, ['_uuid']].count())

    return results

def count_options(submissions, children, path=None, group_labels=None, results=None):
    """
    returns nested dicts where the keys are the names of the XForm element
    branches to each question and each option of a question. Only multiple
    choice questions are supported.
    """
    path = path or []  # list of names in structure leading to current element
    group_labels = group_labels or []  # list of labels in groups in path
    results = results or {}  # results nested dict under construction
    for child in children:
        deeper_path = path + [child['name']]
        if deeper_path in SKIP_QUESTIONS:
            pass
        elif child.get('type') == 'group' and child['name'] == 'meta':
            pass
        elif child.get('type') == 'group':
            deeper_group_labels = group_labels + [child['label']]
            results = count_options(submissions, child['children'],
                                    deeper_path, deeper_group_labels, results)
        elif child.get('type') == 'select one':
            question = SelectOne(child, path, group_labels)
            results = count_select_one(submissions, question, results)
        elif child.get('type') == 'select all that apply':
            question = SelectAllThatApply(child, path, group_labels)
            results = count_select_all_that_apply(submissions, question, results)
        else:
            pass
    return results

def count_select_one(submissions, q, results):
    option_counts = count_select_one_options(submissions, q.path)
    results = deep_set(results, [q.pathstr, 'label'], q.label)
    results = deep_set(results, [q.pathstr, 'group_labels'], q.group_labels)
    for idx in range(len(q.options)):
        opt = q.options[idx]
        results = deep_set(results, [q.pathstr, 'options', opt.name, 'label'], opt.label)
        results = deep_set(results, [q.pathstr, 'options', opt.name, 'idx'], idx)
        results = set_select_one_option_counts(q.path, opt.name, results, option_counts)
    return results

def count_select_all_that_apply(submissions, q, results):
    results = deep_set(results, [q.pathstr, 'label'], q.label)
    results = deep_set(results, [q.pathstr, 'group_labels'], q.group_labels)
    for idx in range(len(q.options)):
        opt = q.options[idx]
        option_counts = count_select_all_that_apply_options(submissions, opt.path)
        results = deep_set(results, [q.pathstr, 'options', opt.name, 'label'], opt.label)
        results = deep_set(results, [q.pathstr, 'options', opt.name, 'idx'], idx)
        results = set_select_all_that_apply_option_counts(q.path, opt.name, results, option_counts)
    return results

def count_select_one_options(submissions, questionpath):
    cols = ['_uuid', GENDER_COLUMN, pathstr(questionpath)]
    question_table = submissions.loc[:, cols]
    question_counts = question_table.groupby(
        [GENDER_COLUMN, pathstr(questionpath)]
    ).count()
    return question_counts

def count_select_all_that_apply_options(submissions, optionpath):
    option_col = pathstr(optionpath)
    cols = [GENDER_COLUMN, option_col]
    option_table = submissions.loc[:, cols]
    option_chosen_table = option_table.where(submissions[option_col] == 'True')
    option_counts = option_chosen_table.groupby([GENDER_COLUMN]).count()
    return option_counts

def set_select_all_that_apply_option_counts(path, option_name, results, option_table):
    for gender in ['male', 'female']:
        val = int(option_table.loc[gender])
        results = deep_set(results, [pathstr(path), 'options', option_name, 'count', gender], val)
    return results

def set_select_one_option_counts(path, option_name, results, option_table):
    for gender in ['male', 'female']:
        try:
            val = int(option_table.loc[gender, option_name])
        except KeyError:
            # values that aren't counted because they don't occur in the
            # results for this question won't be indexes in the counts
            log.debug("Question %s option %s %s not found in counts DataFrame %s",
                      pathstr(path), gender, option_name, option_table)
            val = 0

        results = deep_set(results, [pathstr(path), 'options', option_name, 'count', gender], val)
    return results

def pathstr(path):
    """ / separated path from array of strings"""
    return '/'.join(path)

def deep_set(deep_dict, path, value):
    key = path[0]
    if path[1:]:
        if key in deep_dict:
            deep_dict[key] = deep_set(deep_dict[key], path[1:], value)
        else:
            deep_dict[key] = deep_set({}, path[1:], value)
    else:
        deep_dict[key] = value

    return deep_dict

def questions_dict_to_array(question_dict):
    """
    Turn the question-name-keyed dict into an array of questions and options
    """
    questions = []
    for q_key, question in question_dict.iteritems():
        options_dict = question['options']
        options = [None] * len(options_dict)
        for o_key, option in options_dict.iteritems():
            option = options_dict[o_key]
            option['key'] = o_key
            options[option['idx']] = option
        question['options'] = options  # overwrite
        question['key'] = q_key
        questions.append(question)
    return questions

def calc_q_percents(questions, site_totals):
    """
    updates and returns a questions dict with percentages for option counts
    """
    for q_key, question in questions.iteritems():
        for o_key, option in question['options'].iteritems():
            for gender in ['female', 'male']:
                o_count = option['count'][gender]
                g_count = site_totals[gender]
                pct = (float(o_count)/float(g_count))*100
                deep_set(option, ['pct', gender], pct)
    return questions
<commit_msg>Count responses to each question<commit_after>"""
Survey response summary calculation

Calculation builds up a nested dict structure which makes referencing columns
and specific values easy. This is converted to an array structure for Django
Template Language to be able to put it on the screen.

The results nested dict looks something like
{u'demographics_group/age': {
     'label': u'How old are you?',
     'options': {
         u'under_25': {'count': {'female': 53,
                            'male': 11},
                       'idx': 0,
                       'key': ,
                       'label': u'Under 25 years old',
                       'pct': {'female': 26.903553299492383,
                               'male': 10.2803738317757}},
                       {'count': {'female': 101,
                                  'male': 56},
                        'idx': 1,
                        'key': u'26_40',
                        'label': u'26 - 40 years old',
                        'pct': {'female': 51.26903553299492,
                                'male': 52.336448598130836}},
where the keys are group path names then the q name separated by /
and options are under the options field keyed on option name.

The results array looks something like
[{'key': u'demographics_group/age',
  'label': u'How old are you?',
  'options': [{'count': {'female': 53,
                         'male': 11},
               'idx': 0,
               'key': u'under_25',
               'label': u'Under 25 years old',
               'pct': {'female': 26.903553299492383,
                       'male': 10.2803738317757}},
where questions are in an array and each question's options are
in an array under the question's options field.

Assumptions:
 - that the gender column is always called 'demographics_group/gender'
    and its values are always 'female' or 'male'
 - that the _uuid column always exists and is required
"""

from logging import getLogger

# These are used for grouping. Trying to count them and group by them
# at the same time doesn't work.
SKIP_QUESTIONS = [['_uuid'], ['demographics_group', 'gender']]
SKIP_GROUP_NAMES = ['meta', 'formhub']
GENDER_COLUMN = 'demographics_group/gender'

log = getLogger(__name__)

class Element(object):
    def __init__(self, element, path):
        self.label = element['label']
        self.name = element['name']
        self.path = path + [element['name']]
        self.pathstr = pathstr(self.path)

    def __str__(self):
        return "%s %s" % (self.__class__, self.pathstr)

class MultipleChoice(Element):
    def __init__(self, question, path, group_labels):
        super(MultipleChoice, self).__init__(question, path)
        self.options = [Option(o, self.path) for o in question['children']]
        self.group_labels = group_labels

class Option(Element):
    pass

class SelectOne(MultipleChoice):
    pass

class SelectAllThatApply(MultipleChoice):
    pass

def count_submissions(submissions):
    results = {}
    # per-gender counts
    cols = ['_uuid', GENDER_COLUMN]
    question_table = submissions.loc[:, cols]
    gender_counts = question_table.groupby(
        [GENDER_COLUMN]
    ).count()
    results['female'] = int(gender_counts.loc['female'])
    results['male'] = int(gender_counts.loc['male'])
    results['total'] = int(submissions.loc[:, ['_uuid']].count())

    return results

def count_options(submissions, children, path=None, group_labels=None, results=None):
    """
    returns nested dicts where the keys are the names of the XForm element
    branches to each question and each option of a question. Only multiple
    choice questions are supported.
    """
    path = path or []  # list of names in structure leading to current element
    group_labels = group_labels or []  # list of labels in groups in path
    results = results or {}  # results nested dict under construction
    for child in children:
        deeper_path = path + [child['name']]
        if deeper_path in SKIP_QUESTIONS:
            pass
        elif child.get('type') == 'group' and child['name'] in SKIP_GROUP_NAMES:
            pass
        elif child.get('type') == 'group':
            deeper_group_labels = group_labels + [child['label']]
            results = count_options(submissions, child['children'],
                                    deeper_path, deeper_group_labels, results)
        elif child.get('type') == 'select one':
            question = SelectOne(child, path, group_labels)
            results = count_select_one(submissions, question, results)
        elif child.get('type') == 'select all that apply':
            question = SelectAllThatApply(child, path, group_labels)
            results = count_select_all_that_apply(submissions, question, results)
        else:
            pass
    return results

def count_select_one(submissions, q, results):
    """
    assumes distinct values in question column as per formhub csvwriter
    """
    select_counts = count_select_one_selections(submissions, q)
    results = deep_set(results, [q.pathstr, 'label'], q.label)
    results = deep_set(results, [q.pathstr, 'group_labels'], q.group_labels)
    response_counts = count_select_one_responses(submissions, q)
    results = set_response_counts(q, results, response_counts)
    for idx in range(len(q.options)):
        opt = q.options[idx]
        results = deep_set(results, [q.pathstr, 'options', opt.name, 'label'], opt.label)
        results = deep_set(results, [q.pathstr, 'options', opt.name, 'idx'], idx)
        results = set_select_one_selection_counts(q, opt, results, select_counts)
    return results

def count_select_all_that_apply(submissions, q, results):
    """
    assumes column per option as per formhub csvwriter
    """
    results = deep_set(results, [q.pathstr, 'label'], q.label)
    results = deep_set(results, [q.pathstr, 'group_labels'], q.group_labels)
    response_counts = count_select_all_that_apply_responses(submissions, q)
    results = set_response_counts(q, results, response_counts)
    for idx in range(len(q.options)):
        opt = q.options[idx]
        select_counts = count_select_all_that_apply_selections(submissions, opt)
        results = deep_set(results, [q.pathstr, 'options', opt.name, 'label'], opt.label)
        results = deep_set(results, [q.pathstr, 'options', opt.name, 'idx'], idx)
        results = set_select_all_that_apply_selection_counts(q, opt, results, select_counts)
    return results

def count_select_one_selections(submissions, question):
    cols = ['_uuid', GENDER_COLUMN, question.pathstr]
    question_table = submissions.loc[:, cols]
    question_counts = question_table.groupby([GENDER_COLUMN, question.pathstr]).count()
    return question_counts

def count_select_all_that_apply_selections(submissions, option):
    """
    assumes selecting this option gives value 'True' as per formhub csvwriter
    """
    option_col = option.pathstr
    cols = [GENDER_COLUMN, option_col]
    option_table = submissions.loc[:, cols]
    option_chosen_table = option_table.where(submissions[option_col] == 'True')
    option_counts = option_chosen_table.groupby([GENDER_COLUMN]).count()
    return option_counts

def count_select_one_responses(submissions, q):
    """
    assumes that an un-answered 'select one' question column is
    set to 'n/a' as per formhub csvwriter
    """
    question_table = submissions.loc[:, [GENDER_COLUMN, q.pathstr]]
    return question_table.where(submissions[q.pathstr] != 'n/a').groupby([GENDER_COLUMN]).count()

def count_select_all_that_apply_responses(submissions, q):
    """
    assumes that an un-answered 'select all that apply' question
    has all option columns set to 'n/a' as per formhub csvwriter
    """
    some_option = q.options[0]
    question_table = submissions.loc[:, [GENDER_COLUMN, some_option.pathstr]]
    return question_table.where(
        submissions[some_option.pathstr] != 'n/a'
    ).groupby([GENDER_COLUMN]).count()

def set_select_all_that_apply_selection_counts(q, opt, results, option_table):
    for gender in ['male', 'female']:
        try:
            val = int(option_table.loc[gender])
        except KeyError:
            # values that aren't counted because they don't occur in the
            # results for this question won't be indexes in the counts
            log.debug("Question %s option %s %s not found in counts DataFrame %s",
                      q.pathstr, gender, opt.name, option_table)
            val = 0
        results = deep_set(results, [q.pathstr, 'options', opt.name, 'count', gender], val)
    return results

def set_select_one_selection_counts(q, option, results, option_table):
    for gender in ['male', 'female']:
        try:
            val = int(option_table.loc[gender, option.name])
        except KeyError:
            # values that aren't counted because they don't occur in the
            # results for this question won't be indexes in the counts
            log.debug("Question %s option %s %s not found in counts DataFrame %s",
                      q.pathstr, gender, option.name, option_table)
            val = 0

        results = deep_set(results, [q.pathstr, 'options', option.name, 'count', gender], val)
    return results

def set_response_counts(q, results, counts_table):
    total = 0
    for gender in ['male', 'female']:
        val = int(counts_table.loc[gender])
        total += val
        results = deep_set(results, [q.pathstr, 'response_count', gender], val)
    results = deep_set(results, [q.pathstr, 'response_count', 'total'], total)
    return results

def pathstr(path):
    """ / separated path from array of strings"""
    return '/'.join(path)

def deep_set(deep_dict, path, value):
    key = path[0]
    if path[1:]:
        if key in deep_dict:
            deep_dict[key] = deep_set(deep_dict[key], path[1:], value)
        else:
            deep_dict[key] = deep_set({}, path[1:], value)
    else:
        deep_dict[key] = value

    return deep_dict

def questions_dict_to_array(question_dict):
    """
    Turn the question-name-keyed dict into an array of questions and options
    """
    questions = []
    for q_key, question in question_dict.iteritems():
        options_dict = question['options']
        options = [None] * len(options_dict)
        for o_key, option in options_dict.iteritems():
            option = options_dict[o_key]
            option['key'] = o_key
            options[option['idx']] = option
        question['options'] = options  # overwrite
        question['key'] = q_key
        questions.append(question)
    return questions

def calc_q_percents(questions, site_totals):
    """
    updates and returns a questions dict with percentages for option counts
    """
    for q_key, question in questions.iteritems():
        for o_key, option in question['options'].iteritems():
            for gender in ['female', 'male']:
                o_count = option['count'][gender]
                g_count = site_totals[gender]
                pct = (float(o_count)/float(g_count))*100
                deep_set(option, ['pct', gender], pct)
    return questions
<|endoftext|>


<commit_before>import csv
import pandas as pd

f = open('cycle1/Health_citizen_survey_all.csv')
r = csv.DictReader(f, delimiter=',', quotechar='"')
df = pd.DataFrame(list(r))

# change column names
df.columns = ['waiting_group/medicine_time', 'device_id', 'yes_no_group/examined_private', 'performance_group/respect_admin', 'performance_group/queues', 'performance_group/respect_professionals', 'yes_no_group/consent', 'yes_no_group/safety', 'yes_no_group/all_medication', 'yes_no_group/complaint', 'yes_no_group/complaint_response', 'performance_group/equipment', 'waiting_group/register_time', 'performance_group/ambulance', 'tracking_no', 'personal_comment1', 'personal_comment2', 'clinic_committee', 'clinic_committee_function', 'clinic_feedback', 'improvements_comment', 'demographics_group/gender', 'facility', 'demographics_group/age', 'demographics_group/disability', 'distance', 'demographics_group/income', 'town_village', 'district', 'province', 'surveyor', 'today', 'visit_reason', 'travel_distance', '_uuid', 'waiting_group/professional_time', 'submitted_date', 'performance_group/clean']

# change values

# method
# for val in pd.unique(df.where(df['device_id']=='MAVCEC1')['facility'].ravel()):
#    print val
#
# deviceid doesn't seem to be fixed to a site
#
# df.where(df['town_village']=='Folweni').replace(inplace=True, to_replace={'facility':{'Clinic':'notclinic'}})
# doesn't seem to work

replacements_all = {
    'town_village': {},
    'district': {},
    'province': {},
    'surveyor': {},
    'today': {},
    'visit_reason': {},
    'travel_distance': {},
    'waiting_group/register_time': {},
    'waiting_group/professional_time': {},
    'waiting_group/medicine_time': {},
    'yes_no_group/safety': {},
    'yes_no_group/examined_private': {},
    'yes_no_group/consent': {},
    'yes_no_group/all_medication': {},
    'yes_no_group/complaint': {},
    'yes_no_group/complaint_response': {},
    'personal_comment1': {},
    'performance_group/clean': {},
    'performance_group/queues': {},
    'performance_group/respect_admin': {},
    'performance_group/respect_professionals': {},
    'performance_group/ambulance': {},
    'performance_group/equipment': {},
    'personal_comment2': {},
    'clinic_committee': {},
    'clinic_committee_function': {},
    'clinic_feedback': {},
    'improvements_comment': {},
    'demographics_group/gender': {},
    'demographics_group/age': {},
    'demographics_group/disability': {},
    'distance': {},
    'demographics_group/income': {}
}

replacements_MAVCEC1 = {
    'facility': {
        'Thabong Clinic': 'thabong',
        'Thabong': 'thabong',
    },
    'town_village': {},
    'district': {},
    'province': {},
    'surveyor': {},
    'today': {},
    'visit_reason': {},
    'travel_distance': {},
    'waiting_group/register_time': {},
    'waiting_group/professional_time': {},
    'waiting_group/medicine_time': {},
    'yes_no_group/safety': {},
    'yes_no_group/examined_private': {},
    'yes_no_group/consent': {},
    'yes_no_group/all_medication': {},
    'yes_no_group/complaint': {},
    'yes_no_group/complaint_response': {},
    'personal_comment1': {},
    'performance_group/clean': {},
    'performance_group/queues': {},
    'performance_group/respect_admin': {},
    'performance_group/respect_professionals': {},
    'performance_group/ambulance': {},
    'performance_group/equipment': {},
    'personal_comment2': {},
    'clinic_committee': {},
    'clinic_committee_function': {},
    'clinic_feedback': {},
    'improvements_comment': {},
    'demographics_group/gender': {},
    'demographics_group/age': {},
    'demographics_group/disability': {},
    'distance': {},
    'demographics_group/income': {}
}

replacements_MAVCEC4 = {
    'facility': {
        'Port St Johns': 'port_st_johns',
    },
    'town_village': {},
    'district': {},
    'province': {},
    'surveyor': {},
    'today': {},
    'visit_reason': {},
    'travel_distance': {},
    'waiting_group/register_time': {},
    'waiting_group/professional_time': {},
    'waiting_group/medicine_time': {},
    'yes_no_group/safety': {},
    'yes_no_group/examined_private': {},
    'yes_no_group/consent': {},
    'yes_no_group/all_medication': {},
    'yes_no_group/complaint': {},
    'yes_no_group/complaint_response': {},
    'personal_comment1': {},
    'performance_group/clean': {},
    'performance_group/queues': {},
    'performance_group/respect_admin': {},
    'performance_group/respect_professionals': {},
    'performance_group/ambulance': {},
    'performance_group/equipment': {},
    'personal_comment2': {},
    'clinic_committee': {},
    'clinic_committee_function': {},
    'clinic_feedback': {},
    'improvements_comment': {},
    'demographics_group/gender': {},
    'demographics_group/age': {},
    'demographics_group/disability': {},
    'distance': {},
    'demographics_group/income': {}
}

replacements_MAVCJHB5 = {
    'facility': {
        'Soshoanguve Block X Clinic': 'tshwane',
        'Clinic': 'tswane',
        'ufvh': 'tswane',
        'ju5huh': 'tswane',
        'tshwane': 'tswane',
        'Soshoanguve': 'tswane',
    },
    'town_village': {},
    'district': {},
    'province': {},
    'surveyor': {},
    'today': {},
    'visit_reason': {},
    'travel_distance': {},
    'waiting_group/register_time': {},
    'waiting_group/professional_time': {},
    'waiting_group/medicine_time': {},
    'yes_no_group/safety': {},
    'yes_no_group/examined_private': {},
    'yes_no_group/consent': {},
    'yes_no_group/all_medication': {},
    'yes_no_group/complaint': {},
    'yes_no_group/complaint_response': {},
    'personal_comment1': {},
    'performance_group/clean': {},
    'performance_group/queues': {},
    'performance_group/respect_admin': {},
    'performance_group/respect_professionals': {},
    'performance_group/ambulance': {},
    'performance_group/equipment': {},
    'personal_comment2': {},
    'clinic_committee': {},
    'clinic_committee_function': {},
    'clinic_feedback': {},
    'improvements_comment': {},
    'demographics_group/gender': {},
    'demographics_group/age': {},
    'demographics_group/disability': {},
    'distance': {},
    'demographics_group/income': {}
}

replacements_MAVCKZN2 = {
    'facility': {
        'siphosethu nzoyi': 'umlazi',
        'Q clinic': 'umlazi',
        'D': 'umlazi',
    },
    'town_village': {},
    'district': {},
    'province': {},
    'surveyor': {},
    'today': {},
    'visit_reason': {},
    'travel_distance': {},
    'waiting_group/register_time': {},
    'waiting_group/professional_time': {},
    'waiting_group/medicine_time': {},
    'yes_no_group/safety': {},
    'yes_no_group/examined_private': {},
    'yes_no_group/consent': {},
    'yes_no_group/all_medication': {},
    'yes_no_group/complaint': {},
    'yes_no_group/complaint_response': {},
    'personal_comment1': {},
    'performance_group/clean': {},
    'performance_group/queues': {},
    'performance_group/respect_admin': {},
    'performance_group/respect_professionals': {},
    'performance_group/ambulance': {},
    'performance_group/equipment': {},
    'personal_comment2': {},
    'clinic_committee': {},
    'clinic_committee_function': {},
    'clinic_feedback': {},
    'improvements_comment': {},
    'demographics_group/gender': {},
    'demographics_group/age': {},
    'demographics_group/disability': {},
    'distance': {},
    'demographics_group/income': {}
}

replacements_MAVCKZN5 = {
    'facility': {
        'Clinic': 'folweni',
    },
    'town_village': {},
    'district': {},
    'province': {},
    'surveyor': {},
    'today': {},
    'visit_reason': {},
    'travel_distance': {},
    'waiting_group/register_time': {},
    'waiting_group/professional_time': {},
    'waiting_group/medicine_time': {},
    'yes_no_group/safety': {},
    'yes_no_group/examined_private': {},
    'yes_no_group/consent': {},
    'yes_no_group/all_medication': {},
    'yes_no_group/complaint': {},
    'yes_no_group/complaint_response': {},
    'personal_comment1': {},
    'performance_group/clean': {},
    'performance_group/queues': {},
    'performance_group/respect_admin': {},
    'performance_group/respect_professionals': {},
    'performance_group/ambulance': {},
    'performance_group/equipment': {},
    'personal_comment2': {},
    'clinic_committee': {},
    'clinic_committee_function': {},
    'clinic_feedback': {},
    'improvements_comment': {},
    'demographics_group/gender': {},
    'demographics_group/age': {},
    'demographics_group/disability': {},
    'distance': {},
    'demographics_group/income': {}
}
<commit_msg>translation skeleton done<commit_after>"""
  _    _ ______          _   _______ _    _
 | |  | |  ____|   /\   | | |__   __| |  | |
 | |__| | |__     /  \  | |    | |  | |__| |
 |  __  |  __|   / /\ \ | |    | |  |  __  |
 | |  | | |____ / ____ \| |____| |  | |  | |
 |_|  |_|______/_/    \_\______|_|  |_|  |_|

 - no optional questions - no n/a in cycle1 data
 - one "select all that apply" - visit_reason
   - map valid answers to new columns val True/False
 - remaining columns
   - map known responses to cycle2 values
   - map known responses to 'other' if in cycle2, otherwise n/a
"""

import csv
import pandas as pd

f = open('cycle1/Health_citizen_survey_all.csv')
r = csv.DictReader(f, delimiter=',', quotechar='"')
df = pd.DataFrame(list(r))

# change column names
df.columns = ['waiting_group/medicine_time', 'device_id', 'yes_no_group/examined_private', 'performance_group/respect_admin', 'performance_group/queues', 'performance_group/respect_professionals', 'yes_no_group/consent', 'yes_no_group/safety', 'yes_no_group/all_medication', 'yes_no_group/complaint', 'yes_no_group/complaint_response', 'performance_group/equipment', 'waiting_group/register_time', 'performance_group/ambulance', 'tracking_no', 'personal_comment1', 'personal_comment2', 'clinic_committee', 'clinic_committee_function', 'clinic_feedback', 'improvements_comment', 'demographics_group/gender', 'facility', 'demographics_group/age', 'demographics_group/disability', 'distance', 'demographics_group/income', 'town_village', 'district', 'province', 'surveyor', 'today', 'visit_reason', 'travel_distance', '_uuid', 'waiting_group/professional_time', 'submitted_date', 'performance_group/clean']

# change values

# method
# for val in pd.unique(df.where(df['device_id']=='MAVCEC1')['facility'].ravel()):
#    print val
#
# deviceid doesn't seem to be fixed to a site
#
# df.where(df['town_village']=='Folweni').replace(inplace=True, to_replace={'facility':{'Clinic':'notclinic'}})
# doesn't seem to work

for c in df.columns:
    if c.startswith('waiting_group/'):
        print("### %s ###" % c)
        for val in pd.unique(df[c].ravel()):
            print("'%s': ''," % val)

replacements_all = {
    'town_village': {},
    'district': {},
    'province': {},
    'surveyor': {},
    'today': {},
    'visit_reason': {},
    'travel_distance': {
        '2 km or less': '',
        'More than 8 km': '',
        '3 - 5 km': '',
        '6 - 8 km': '',
    },
    'waiting_group/register_time': {
        'More than 4 hours': '',
        '1 - 2 hours': '',
        'Less than 1 hour': '',
        '2 - 3 hours': '',
        '3 - 4 hours': '',
        'More than 4 hours3 - 4 hours': '',
        'More than 4 hoursLess than 1 hour': '',
    },
    'waiting_group/professional_time': {
        'More than 4 hours': '',
        '2 - 3 hours': '',
        '1 - 2 hours': '',
        '3 - 4 hours': '',
        'Less than 1 hour': '',
        '2 - 3 hours1 - 2 hours': '',
        '3 - 4 hours2 - 3 hours1 - 2 hours': '',
        '2 - 3 hoursLess than 1 hour': '',
    },
    'waiting_group/medicine_time': {
        'More than 4 hours': '',
        '1 - 2 hours': '',
        'Less than 1 hour': '',
        '2 - 3 hours': '',
        '3 - 4 hours': '',
        'More than 4 hours3 - 4 hours': '',
        'More than 4 hoursLess than 1 hour': '',
    },
    'yes_no_group/safety': {
        'Yes': '',
        'No': '',
        'YesNo': '',
    },
    'yes_no_group/examined_private': {
        'Yes': '',
        'No': '',
        'YesNo': '',
    },
    'yes_no_group/consent': {
        'Yes': '',
        'No': '',
        'YesNo': '',
    },
    'yes_no_group/all_medication': {
        'Yes': '',
        'No': '',
        'YesNo': '',
    },
    'yes_no_group/complaint': {
        'Yes': '',
        'No': '',
        'YesNo': '',
    },
    'yes_no_group/complaint_response': {
        'Yes': '',
        'No': '',
        'YesNo': '',
    },
    'personal_comment1': {},
    'performance_group/clean': {
        'Very poor': '',
        'Not good.not bad': '',
        'Poor': '',
        'Good': '',
        'Excellent': '',
        'Not good.not badGood': '',
        'PoorGood': '',
    },
    'performance_group/queues': {
        'Poor': '',
        'Very poor': '',
        'Not good.not bad': '',
        'Good': '',
        'Excellent': '',
        'Very poorExcellent': '',
        'PoorGood': '',
        'Very poorGood': '',
        'Very poorPoor': '',
        'GoodExcellent': '',
    },
    'performance_group/respect_admin': {
        'Poor': '',
        'Very poor': '',
        'Good': '',
        'Not good.not bad': '',
        'Excellent': '',
        'Not good.not badExcellent': '',
        'PoorGood': '',
        'Very poorGood': '',
    },
    'performance_group/respect_professionals': {
        'Very poor': '',
        'Good': '',
        'Poor': '',
        'Excellent': '',
        'Not good.not bad': '',
        'Not good.not badExcellent': '',
        'Very poorExcellent': '',
        'Very poorNot good.not bad': '',
        'GoodExcellent': '',
        'Very poorGood': '',
    },
    'performance_group/ambulance': {
        'Very poor': '',
        'Poor': '',
        'Not good.not bad': '',
        'Good': '',
        'Excellent': '',
        'Very poorExcellent': '',
        'PoorGood': '',
        'Very poorPoorExcellent': '',
        'Not good.not badExcellent': '',
    },
    'performance_group/equipment': {
        'Very poor': '',
        'Poor': '',
        'Not good.not bad': '',
        'Good': '',
        'Excellent': '',
        'PoorExcellent': '',
        'Very poorExcellent': '',
    },
    'personal_comment2': {},
    'clinic_committee': {
        'Yes': '',
        'No': '',
        'Don\'t know': '',
    },
    'clinic_committee_function': {
        'Yes': '',
        'No': '',
        'Don\'t know': '',
    },
    'clinic_feedback': {},
    'improvements_comment': {},
    'demographics_group/gender': {
        'Male': '',
        'Female': '',
    },
    'demographics_group/age': {
        '19 - 40 years old': '',
        '41 - 60 years old': '',
        'Older than 60 years': '',
        'Under 18 years old': '',
    },
    'demographics_group/disability': {
        'No': '',
        'Yes': '',
    },
    'demographics_group/income': {
        'Government grant': '',
        'I do not earn income': '',
        'Permanent employment': '',
        'Temporary employment': '',
        'Small scale vendor (no employees)': '',
        'Own business (with employees)': '',
    },
}

replacements_MAVCEC1 = {
    'facility': {
        'Thabong Clinic': 'thabong',
        'Thabong': 'thabong',
    },
}

replacements_MAVCEC4 = {
    'facility': {
        'Port St Johns': 'port_st_johns',
    },
}

replacements_MAVCJHB5 = {
    'facility': {
        'Soshoanguve Block X Clinic': 'tshwane',
        'Clinic': 'tswane',
        'ufvh': 'tswane',
        'ju5huh': 'tswane',
        'tshwane': 'tswane',
        'Soshoanguve': 'tswane',
    },
}

replacements_MAVCKZN2 = {
    'facility': {
        'siphosethu nzoyi': 'umlazi',
        'Q clinic': 'umlazi',
        'D': 'umlazi',
    },
}

replacements_MAVCKZN5 = {
    'facility': {
        'Clinic': 'folweni',
    },
}

# [c for c in df2.columns if c.startswith("visit_reason")]
# Out[251]:
# ['visit_reason/accompanying',
#  'visit_reason/emergency',
#  'visit_reason/not_well',
#  'visit_reason/other',
#  'visit_reason/pregnant',
#  'visit_reason/regular_collection']
<|endoftext|>


<commit_before># Copyright 2017 The Forseti Security Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Forseti Server utilities."""

from itertools import izip
import logging

# pylint: disable=protected-access

def autoclose_stream(f):
    """Decorator to close gRPC stream.

    Args:
        f (func): The function to decorate

    Returns:
        wrapper: wrapper of the decorator
    """

    def wrapper(*args):
        """Wrapper function, checks context state to close stream.

        Args:
            *args (list): All arguments provided to the wrapped function.

        Yields:
            object: Whatever the wrapped function yields to the stream.
        """

        def closed(context):
            """Returns true iff the connection is closed.

            Args:
                context (object): the connection to check

            Returns:
                bool: whether the connection is closed
            """

            return context._state.client == 'closed'
        context = args[-1]
        for result in f(*args):
            yield result
            if closed(context):
                return
    return wrapper

def logcall(f, level=logging.CRITICAL):
    """Call logging decorator.

    Args:
        f (func): The function to decorate
        level (str): the level of logging

    Returns:
        wrapper: wrapper of the decorator
    """

    def wrapper(*args, **kwargs):
        """Implements the log wrapper including parameters and result.

        Args:
            *args: All args provided to the wrapped function.
            **kwargs: All kwargs provided to the wrapped function.

        Returns:
            object: the f execution result
        """
        logging.log(level, 'enter %s(%s)', f.__name__, args)
        result = f(*args, **kwargs)
        logging.log(level, 'exit %s(%s) -> %s', f.__name__, args, result)
        return result
    return wrapper

def mutual_exclusive(lock):
    """ Mutex decorator.

    Args:
        lock (object): The lock to lock out exclusive method

    Returns:
        object: decorator generator
    """

    def wrap(f):
        """Decorator generator.

        Args:
            f (func): the function to decorate

        Returns:
            func: the decorated function
        """
        def function(*args, **kw):
            """Decorated functionality, mutexing wrapped function.

            Args:
                *args: All args provided to the wrapped function
                **kw: All kw provided to the wrapped function

            Returns:
                object: the execution results of f
            """
            lock.acquire()
            try:
                return f(*args, **kw)
            finally:
                lock.release()
        return function
    return wrap

def oneof(*args):
    """Returns true iff one of the parameters is true.

    Args:
        *args: arguments to check

    Returns:
        bool: true iff one of the parameters is true.
    """

    return len([x for x in args if x]) == 1

def full_to_type_name(full_resource_name):
    """Creates a type/name format from full resource name.

    Args:
        full_resource_name (str): the full_resource_name of the resource

    Returns:
        str: type_name of that resource
    """

    return '/'.join(full_resource_name.split('/')[-2:])

def to_full_resource_name(full_parent_name, resource_type_name):
    """Creates a full resource name by parent full name and type name.

    Args:
        full_parent_name (str): the full_resource_name of the parent
        resource_type_name (str): the full_resource_name of the child

    Returns:
        str: full_resource_name of the child
    """

    return '{}{}/'.format(full_parent_name, resource_type_name)

def to_type_name(resource_type, resource_name):
    """Creates a type/name from type and name.

    Args:
        resource_type (str): the resource type
        resource_name (str): the resource name

    Returns:
        str: type_name of the resource
    """

    return '{}/{}'.format(resource_type, resource_name)

def split_type_name(resource_type_name):
    """Split the type name of the resource

    Args:
        resource_type_name (str): the type_name of the resource

    Returns:
        tuples: type and name of the resource
    """

    return resource_type_name.split('/')

def resource_to_type_name(resource):
    """Creates a type/name format from a resource dbo.

    Args:
        resource (object): the resource to get the the type_name

    Returns:
        str: type_name of the resource
    """

    return resource.type_name

def get_sql_dialect(session):
    """Return the active SqlAlchemy dialect.

    Args:
        session (object): the session to check for SqlAlchemy dialect

    Returns:
        str: name of the SqlAlchemy dialect
    """

    return session.bind.dialect.name

def get_resources_from_full_name(full_name):
    """Parse resource info from full name.

    Args:
        full_name (str): Full name of the resource in hierarchical format.
            Example of a full_name:
            organization/88888/project/myproject/firewall/99999/
            full_name has a trailing / that needs to be removed.

    Yields:
        iterator: strings of resource_type and resource_id
    """

    full_name_parts = full_name.split('/')[:-1]
    full_name_parts.reverse()
    resource_iter = iter(full_name_parts)
    for resource_id, resource_type in izip(resource_iter, resource_iter):
        yield resource_type, resource_id

def get_key_from_type_name(type_name):
    """Returns the key from type_name.

    Returns:
        str: Resource key.
    """
    if '/' in type_name:
        return type_name.split('/')[-1]
    return type_name<commit_msg>pylint fixes<commit_after># Copyright 2017 The Forseti Security Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Forseti Server utilities."""

from itertools import izip
import logging

# pylint: disable=protected-access

def autoclose_stream(f):
    """Decorator to close gRPC stream.

    Args:
        f (func): The function to decorate

    Returns:
        wrapper: wrapper of the decorator
    """

    def wrapper(*args):
        """Wrapper function, checks context state to close stream.

        Args:
            *args (list): All arguments provided to the wrapped function.

        Yields:
            object: Whatever the wrapped function yields to the stream.
        """

        def closed(context):
            """Returns true iff the connection is closed.

            Args:
                context (object): the connection to check

            Returns:
                bool: whether the connection is closed
            """

            return context._state.client == 'closed'
        context = args[-1]
        for result in f(*args):
            yield result
            if closed(context):
                return
    return wrapper

def logcall(f, level=logging.CRITICAL):
    """Call logging decorator.

    Args:
        f (func): The function to decorate
        level (str): the level of logging

    Returns:
        wrapper: wrapper of the decorator
    """

    def wrapper(*args, **kwargs):
        """Implements the log wrapper including parameters and result.

        Args:
            *args: All args provided to the wrapped function.
            **kwargs: All kwargs provided to the wrapped function.

        Returns:
            object: the f execution result
        """
        logging.log(level, 'enter %s(%s)', f.__name__, args)
        result = f(*args, **kwargs)
        logging.log(level, 'exit %s(%s) -> %s', f.__name__, args, result)
        return result
    return wrapper

def mutual_exclusive(lock):
    """ Mutex decorator.

    Args:
        lock (object): The lock to lock out exclusive method

    Returns:
        object: decorator generator
    """

    def wrap(f):
        """Decorator generator.

        Args:
            f (func): the function to decorate

        Returns:
            func: the decorated function
        """
        def function(*args, **kw):
            """Decorated functionality, mutexing wrapped function.

            Args:
                *args: All args provided to the wrapped function
                **kw: All kw provided to the wrapped function

            Returns:
                object: the execution results of f
            """
            lock.acquire()
            try:
                return f(*args, **kw)
            finally:
                lock.release()
        return function
    return wrap

def oneof(*args):
    """Returns true iff one of the parameters is true.

    Args:
        *args: arguments to check

    Returns:
        bool: true iff one of the parameters is true.
    """

    return len([x for x in args if x]) == 1

def full_to_type_name(full_resource_name):
    """Creates a type/name format from full resource name.

    Args:
        full_resource_name (str): the full_resource_name of the resource

    Returns:
        str: type_name of that resource
    """

    return '/'.join(full_resource_name.split('/')[-2:])

def to_full_resource_name(full_parent_name, resource_type_name):
    """Creates a full resource name by parent full name and type name.

    Args:
        full_parent_name (str): the full_resource_name of the parent
        resource_type_name (str): the full_resource_name of the child

    Returns:
        str: full_resource_name of the child
    """

    return '{}{}/'.format(full_parent_name, resource_type_name)

def to_type_name(resource_type, resource_name):
    """Creates a type/name from type and name.

    Args:
        resource_type (str): the resource type
        resource_name (str): the resource name

    Returns:
        str: type_name of the resource
    """

    return '{}/{}'.format(resource_type, resource_name)

def split_type_name(resource_type_name):
    """Split the type name of the resource

    Args:
        resource_type_name (str): the type_name of the resource

    Returns:
        tuples: type and name of the resource
    """

    return resource_type_name.split('/')

def resource_to_type_name(resource):
    """Creates a type/name format from a resource dbo.

    Args:
        resource (object): the resource to get the the type_name

    Returns:
        str: type_name of the resource
    """

    return resource.type_name

def get_sql_dialect(session):
    """Return the active SqlAlchemy dialect.

    Args:
        session (object): the session to check for SqlAlchemy dialect

    Returns:
        str: name of the SqlAlchemy dialect
    """

    return session.bind.dialect.name

def get_resources_from_full_name(full_name):
    """Parse resource info from full name.

    Args:
        full_name (str): Full name of the resource in hierarchical format.
            Example of a full_name:
            organization/88888/project/myproject/firewall/99999/
            full_name has a trailing / that needs to be removed.

    Yields:
        iterator: strings of resource_type and resource_id
    """

    full_name_parts = full_name.split('/')[:-1]
    full_name_parts.reverse()
    resource_iter = iter(full_name_parts)
    for resource_id, resource_type in izip(resource_iter, resource_iter):
        yield resource_type, resource_id

def get_key_from_type_name(type_name):
    """Returns the key from type_name.

    Args:
        type_name (str): Type name.

    Returns:
        str: Resource key.
    """
    if '/' in type_name:
        return type_name.split('/')[-1]
    return type_name
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# Copyright 2009, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""fuse_gmock_files.py v0.1.0.

Fuses Google Mock and Google Test source code into two .h files and a .cc file.

SYNOPSIS
       fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR

       Scans GMOCK_ROOT_DIR for Google Mock and Google Test source
       code, assuming Google Test is in the GMOCK_ROOT_DIR/../googletest
       directory, and generates three files:
       OUTPUT_DIR/gtest/gtest.h, OUTPUT_DIR/gmock/gmock.h, and
       OUTPUT_DIR/gmock-gtest-all.cc.  Then you can build your tests
       by adding OUTPUT_DIR to the include search path and linking
       with OUTPUT_DIR/gmock-gtest-all.cc.  These three files contain
       everything you need to use Google Mock.  Hence you can
       "install" Google Mock by copying them to wherever you want.

       GMOCK_ROOT_DIR can be omitted and defaults to the parent
       directory of the directory holding this script.

EXAMPLES
       ./fuse_gmock_files.py fused_gmock
       ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock

This tool is experimental.  In particular, it assumes that there is no
conditional inclusion of Google Mock or Google Test headers.  Please
report any problems to googlemock@googlegroups.com.  You can read
https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md
for more
information.
"""

from __future__ import print_function

import os
import re
import sys

__author__ = 'wan@google.com (Zhanyong Wan)'

# We assume that this file is in the scripts/ directory in the Google
# Mock root directory.
DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')

# We need to call into googletest/scripts/fuse_gtest_files.py.
sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, '../googletest/scripts'))
import fuse_gtest_files as gtest  # pylint:disable=g-import-not-at-top

# Regex for matching
# '#include "gmock/..."'.
INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')

# Where to find the source seed files.
GMOCK_H_SEED = 'include/gmock/gmock.h'
GMOCK_ALL_CC_SEED = 'src/gmock-all.cc'

# Where to put the generated files.
GTEST_H_OUTPUT = 'gtest/gtest.h'
GMOCK_H_OUTPUT = 'gmock/gmock.h'
GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'

def GetGTestRootDir(gmock_root):
  """Returns the root directory of Google Test."""

  return os.path.join(gmock_root, '../googletest')

def ValidateGMockRootDir(gmock_root):
  """Makes sure gmock_root points to a valid gmock root directory.

  The function aborts the program on failure.

  Args:
    gmock_root: A string with the mock root directory.
  """

  gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
  gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
  gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)

def ValidateOutputDir(output_dir):
  """Makes sure output_dir points to a valid output directory.

  The function aborts the program on failure.

  Args:
    output_dir: A string representing the output directory.
  """

  gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
  gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
  gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)

def FuseGMockH(gmock_root, output_dir):
  """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""

  output_file = open(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
  processed_files = set()  # Holds all gmock headers we've processed.

  def ProcessFile(gmock_header_path):
    """Processes the given gmock header file."""

    # We don't process the same header twice.
    if gmock_header_path in processed_files:
      return

    processed_files.add(gmock_header_path)

    # Reads each line in the given gmock header.

    with open(os.path.join(gmock_root, gmock_header_path), 'r') as fh:
      for line in fh:
        m = INCLUDE_GMOCK_FILE_REGEX.match(line)
        if m:
          # '#include "gmock/..."'
          # - let's process it recursively.
          ProcessFile('include/' + m.group(1))
        else:
          m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
          if m:
            # '#include "third_party/googletest/googletest/
            # include/gtest/foo.h"'.
            # We translate it to "gtest/gtest.h", regardless of what foo is,
            # since all gtest headers are fused into gtest/gtest.h.

            # There is no need to #include gtest.h twice.
            if gtest.GTEST_H_SEED not in processed_files:
              processed_files.add(gtest.GTEST_H_SEED)
              output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
          else:
            # Otherwise we copy the line unchanged to the output file.
            output_file.write(line)

  ProcessFile(GMOCK_H_SEED)
  output_file.close()

def FuseGMockAllCcToFile(gmock_root, output_file):
  """Scans folder gmock_root to fuse gmock-all.cc into output_file."""

  processed_files = set()

  def ProcessFile(gmock_source_file):
    """Processes the given gmock source file."""

    # We don't process the same #included file twice.
    if gmock_source_file in processed_files:
      return

    processed_files.add(gmock_source_file)

    # Reads each line in the given gmock source file.

    with open(os.path.join(gmock_root, gmock_source_file), 'r') as fh:
      for line in fh:
        m = INCLUDE_GMOCK_FILE_REGEX.match(line)
        if m:
          # '#include "gmock/foo.h"'.
          # We treat it as '#include  "gmock/gmock.h"', as all other gmock
          # headers are being fused into gmock.h and cannot be
          # included directly.  No need to #include
          # "third_party/googletest/googlemock/include/gmock/gmock.h"
          # more than once.

          if GMOCK_H_SEED not in processed_files:
            processed_files.add(GMOCK_H_SEED)
            output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
        else:
          m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
          if m:
            # '#include "gtest/..."'.
            # There is no need to #include gtest.h as it has been
            # #included by gtest-all.cc.

            pass
          else:
            m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
            if m:
              # It's '#include "src/foo"' - let's process it recursively.
              ProcessFile(m.group(1))
            else:
              # Otherwise we copy the line unchanged to the output file.
              output_file.write(line)

  ProcessFile(GMOCK_ALL_CC_SEED)

def FuseGMockGTestAllCc(gmock_root, output_dir):
  """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""

  with open(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT),
            'w') as output_file:
    # First, fuse gtest-all.cc into gmock-gtest-all.cc.
    gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
    # Next, append fused gmock-all.cc to gmock-gtest-all.cc.
    FuseGMockAllCcToFile(gmock_root, output_file)

def FuseGMock(gmock_root, output_dir):
  """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""

  ValidateGMockRootDir(gmock_root)
  ValidateOutputDir(output_dir)

  gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
  FuseGMockH(gmock_root, output_dir)
  FuseGMockGTestAllCc(gmock_root, output_dir)

def main():
  argc = len(sys.argv)
  if argc == 2:
    # fuse_gmock_files.py OUTPUT_DIR
    FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
  elif argc == 3:
    # fuse_gmock_files.py GMOCK_ROOT_DIR OUTPUT_DIR
    FuseGMock(sys.argv[1], sys.argv[2])
  else:
    print(__doc__)
    sys.exit(1)

if __name__ == '__main__':
  main()
<commit_msg>Googletest export<commit_after>#!/usr/bin/env python
#
# Copyright 2009, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""fuse_gmock_files.py v0.1.0.

Fuses Google Mock and Google Test source code into two .h files and a .cc file.

SYNOPSIS
       fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR

       Scans GMOCK_ROOT_DIR for Google Mock and Google Test source
       code, assuming Google Test is in the GMOCK_ROOT_DIR/../googletest
       directory, and generates three files:
       OUTPUT_DIR/gtest/gtest.h, OUTPUT_DIR/gmock/gmock.h, and
       OUTPUT_DIR/gmock-gtest-all.cc.  Then you can build your tests
       by adding OUTPUT_DIR to the include search path and linking
       with OUTPUT_DIR/gmock-gtest-all.cc.  These three files contain
       everything you need to use Google Mock.  Hence you can
       "install" Google Mock by copying them to wherever you want.

       GMOCK_ROOT_DIR can be omitted and defaults to the parent
       directory of the directory holding this script.

EXAMPLES
       ./fuse_gmock_files.py fused_gmock
       ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock

This tool is experimental.  In particular, it assumes that there is no
conditional inclusion of Google Mock or Google Test headers.  Please
report any problems to googlemock@googlegroups.com.  You can read
https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md
for more
information.
"""

from __future__ import print_function

import os
import re
import sys

__author__ = 'wan@google.com (Zhanyong Wan)'

# We assume that this file is in the scripts/ directory in the Google
# Mock root directory.
DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')

# We need to call into googletest/scripts/fuse_gtest_files.py.
sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, '../googletest/scripts'))
import fuse_gtest_files as gtest  # pylint:disable=g-import-not-at-top

# Regex for matching
# '#include "gmock/..."'.
INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')

# Where to find the source seed files.
GMOCK_H_SEED = 'include/gmock/gmock.h'
GMOCK_ALL_CC_SEED = 'src/gmock-all.cc'

# Where to put the generated files.
GTEST_H_OUTPUT = 'gtest/gtest.h'
GMOCK_H_OUTPUT = 'gmock/gmock.h'
GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'

def GetGTestRootDir(gmock_root):
  """Returns the root directory of Google Test."""

  return os.path.join(gmock_root, '../googletest')

def ValidateGMockRootDir(gmock_root):
  """Makes sure gmock_root points to a valid gmock root directory.

  The function aborts the program on failure.

  Args:
    gmock_root: A string with the mock root directory.
  """

  gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
  gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
  gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)

def ValidateOutputDir(output_dir):
  """Makes sure output_dir points to a valid output directory.

  The function aborts the program on failure.

  Args:
    output_dir: A string representing the output directory.
  """

  gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
  gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
  gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)

def FuseGMockH(gmock_root, output_dir):
  """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""

  output_file = open(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
  processed_files = set()  # Holds all gmock headers we've processed.

  def ProcessFile(gmock_header_path):
    """Processes the given gmock header file."""

    # We don't process the same header twice.
    if gmock_header_path in processed_files:
      return

    processed_files.add(gmock_header_path)

    # Reads each line in the given gmock header.

    with open(os.path.join(gmock_root, gmock_header_path), 'r') as fh:
      for line in fh:
        m = INCLUDE_GMOCK_FILE_REGEX.match(line)
        if m:
          # '#include "gmock/..."'
          # - let's process it recursively.
          ProcessFile('include/' + m.group(1))
        else:
          m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
          if m:
            # '#include "gtest/foo.h"'
            # We translate it to "gtest/gtest.h", regardless of what foo is,
            # since all gtest headers are fused into gtest/gtest.h.

            # There is no need to #include gtest.h twice.
            if gtest.GTEST_H_SEED not in processed_files:
              processed_files.add(gtest.GTEST_H_SEED)
              output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
          else:
            # Otherwise we copy the line unchanged to the output file.
            output_file.write(line)

  ProcessFile(GMOCK_H_SEED)
  output_file.close()

def FuseGMockAllCcToFile(gmock_root, output_file):
  """Scans folder gmock_root to fuse gmock-all.cc into output_file."""

  processed_files = set()

  def ProcessFile(gmock_source_file):
    """Processes the given gmock source file."""

    # We don't process the same #included file twice.
    if gmock_source_file in processed_files:
      return

    processed_files.add(gmock_source_file)

    # Reads each line in the given gmock source file.

    with open(os.path.join(gmock_root, gmock_source_file), 'r') as fh:
      for line in fh:
        m = INCLUDE_GMOCK_FILE_REGEX.match(line)
        if m:
          # '#include "gmock/foo.h"'
          # We treat it as '#include  "gmock/gmock.h"', as all other gmock
          # headers are being fused into gmock.h and cannot be
          # included directly.  No need to
          # #include "gmock/gmock.h"
          # more than once.

          if GMOCK_H_SEED not in processed_files:
            processed_files.add(GMOCK_H_SEED)
            output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
        else:
          m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
          if m:
            # '#include "gtest/..."'
            # There is no need to #include gtest.h as it has been
            # #included by gtest-all.cc.

            pass
          else:
            m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
            if m:
              # It's '#include "src/foo"' - let's process it recursively.
              ProcessFile(m.group(1))
            else:
              # Otherwise we copy the line unchanged to the output file.
              output_file.write(line)

  ProcessFile(GMOCK_ALL_CC_SEED)

def FuseGMockGTestAllCc(gmock_root, output_dir):
  """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""

  with open(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT),
            'w') as output_file:
    # First, fuse gtest-all.cc into gmock-gtest-all.cc.
    gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
    # Next, append fused gmock-all.cc to gmock-gtest-all.cc.
    FuseGMockAllCcToFile(gmock_root, output_file)

def FuseGMock(gmock_root, output_dir):
  """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""

  ValidateGMockRootDir(gmock_root)
  ValidateOutputDir(output_dir)

  gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
  FuseGMockH(gmock_root, output_dir)
  FuseGMockGTestAllCc(gmock_root, output_dir)

def main():
  argc = len(sys.argv)
  if argc == 2:
    # fuse_gmock_files.py OUTPUT_DIR
    FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
  elif argc == 3:
    # fuse_gmock_files.py GMOCK_ROOT_DIR OUTPUT_DIR
    FuseGMock(sys.argv[1], sys.argv[2])
  else:
    print(__doc__)
    sys.exit(1)

if __name__ == '__main__':
  main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on Mon Feb 13 20:25:03 2017

@author: Jonah Haefner

Title: master_example.py

The purpose of this script is to ensure the four functions included in this
package are functioning properly
dP_1 = 27.12 kPa
dP 2 = 25.67 kPa
Nu1 = 142.52
Nu2 = 147.31
"""

import TORCHE
dP_1 = TORCHE.dP_Zu(1.25,1.25,.5,1940,.0056,10,.0254,22000,'inline')
print('The Pressure Drop calculated by Zukauskas is',dP_1/1000,'kPa')
dP_2 = TORCHE.dP_GG(1.25,1.25,.5,1940,22000,10,'inline')
print('The Pressure Drop calculated by Gaddis-Gnielinski is',dP_2/1000,'kPa')
Nu_1 = TORCHE.HT_Zu(1.25,1.25,.5,1940,10,1,1,.0254,1.787e-6,'inline',22000)
print('The Nusselt Number calculated by Zukauskas is', Nu_1)
Nu_2 = TORCHE.HT_GG(1.25,1.25,.0254,.5,1940,22000,10,'inline',1)
print('The Nusselt Number calculated by Gnielinski is', Nu_2)<commit_msg>More changes to the master_example.py file. Just removed some lines but otherwise no change. There is an issue with the dP_Zu giving a different answer than expected.<commit_after>"""
Created on Mon Feb 13 20:25:03 2017

@author: Jonah Haefner

Title: master_example.py

The purpose of this script is to ensure the four functions included in this
package are functioning properly
dP_1 = 27.12 kPa   - This needs to be investigated. I think it is due to the angle of attack.
dP 2 = 25.67 kPa
Nu1 = 142.52
Nu2 = 147.31
"""

import TORCHE
dP_1 = TORCHE.dP_Zu(1.25,1.25,.5,1940,.0056,10,.0254,22000,'inline')
print('The Pressure Drop calculated by Zukauskas is',dP_1/1000,'kPa')
dP_2 = TORCHE.dP_GG(1.25,1.25,.5,1940,22000,10,'inline')
print('The Pressure Drop calculated by Gaddis-Gnielinski is',dP_2/1000,'kPa')
Nu_1 = TORCHE.HT_Zu(1.25,1.25,.5,1940,10,1,1,.0254,1.787e-6,'inline',22000)
print('The Nusselt Number calculated by Zukauskas is', Nu_1)
Nu_2 = TORCHE.HT_GG(1.25,1.25,.0254,.5,1940,22000,10,'inline',1)
print('The Nusselt Number calculated by Gnielinski is', Nu_2)<|endoftext|>


<commit_before># coding: utf-8

from __future__ import unicode_literals
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from onpay import settings
from onpay.signals import (order_created, order_updated,
                           order_success, order_failure)
import hashlib
import urllib

@python_2_unicode_compatible
class Order(models.Model):
    MODE_LIVE = 0
    MODE_TEST = 1

    STATE_SUCCESS = 0
    STATE_FAILURE = 1
    STATE_WAITING = 3
    STATE_EXPIRED = 2

    amount = models.IntegerField(_('amount'))
    currency = models.CharField(_('currency'),
                                max_length=3,
                                default=settings.ONPAY_CURRENCY,
                                choices=settings.ONPAY_CURRENCIES)
    comment = models.TextField(_('order notes'), default='')
    email = models.EmailField(_('buyer e-mail'))
    date_created = models.DateTimeField(_('created at'), auto_now_add=True)
    state = models.IntegerField(_('order status'),
                                default=STATE_WAITING, choices=(
                                    (STATE_SUCCESS, _('Success')),
                                    (STATE_FAILURE, _('Failed')),
                                    (STATE_WAITING, _('Waiting')),
                                    (STATE_EXPIRED, _('Expired')),
                                ))
    mode = models.IntegerField(_('mode'), default=MODE_LIVE, choices=(
                                   (MODE_LIVE, _('live')),
                                   (MODE_TEST, _('text')),
                               ))

    def mark_as_success(self):
        self._mark(state=self.STATE_SUCCESS, signal=order_success)

    def mark_as_failure(self):
        self._mark(state=self.STATE_FAILURE, signal=order_failure)

    def can_be_payed(self):
        return self.state == self.STATE_WAITING

    def is_successfully_paid(self):
        return self.state == self.STATE_SUCCESS

    def is_pay_failed(self):
        return self.state in [self.STATE_EXPIRED, self.STATE_FAILURE]

    def get_pay_url(self, currency=settings.ONPAY_CURRENCY,
                    success_url=settings.ONPAY_SUCCESS_URL,
                    failure_url=settings.ONPAY_SUCCESS_URL):
        pattern = 'fix;{amount:.1f};{currency};{order_id:d};no;{secret}'
        md5_str = pattern.format(amount=self.amount,
                                 currency=settings.ONPAY_CURRENCY,
                                 secret=settings.ONPAY_SECRET,
                                 order_id=self.id)
        md5 = hashlib.md5(md5_str).hexdigest()

        return settings.ONPAY_GATE_URL + '?' + urllib.urlencode({
            'pay_mode': 'fix',
            'f': 7,
            'price': self.amount,
            'pay_for': self.id,
            'convert': 'no',
            'price_final': 'true',
            'user_email': self.email,
            'md5': md5,
            'currency': currency,
            'url_success_enc': success_url,
            'url_fail_enc': failure_url,
        })

    pay_url = property(get_pay_url)

    def _mark(self, state, signal):
        self.state = state
        self.save(update_fields=['state'])
        signal.send(sender=self.__class__, order=self)

    def _check(self, flag=''):
        pattern = 'check;{order_id};{amount:.1f};{currency};{flag}{secret}'
        raw_string = pattern.format(order_id=self.id,
                                    amount=self.amount,
                                    currency=settings.ONPAY_CURRENCY,
                                    secret=settings.ONPAY_SECRET,
                                    flag=flag)
        hashed_string = hashlib.md5(raw_string).hexdigest().upper()
        return hashed_string

    def crc_check_correct(self, crc):
        return crc == self.crc_check_get()

    def crc_check_get(self):
        return self._check(flag='')

    def crc_check_create(self):
        return self._check(flag='0;')

    def crc_pay_correct(self, crc, onpay_id, order_amount):
        actual_crc = self.crc_pay_get(onpay_id, order_amount)
        # print("expected crc: %s, actual: %s" % (crc, actual_crc))
        return crc == actual_crc

    def crc_pay_get(self, onpay_id, order_amount):
        pattern = ('pay;{order_id};{onpay_id};{order_amount};'
                   '{currency};{secret}')
        md5source = pattern.format(order_id=self.id,
                                   onpay_id=onpay_id,
                                   order_amount=order_amount,
                                   currency=settings.ONPAY_CURRENCY,
                                   secret=settings.ONPAY_SECRET)
        # print('crc get =',md5source)
        md5hash = hashlib.md5(md5source).hexdigest().upper()
        return md5hash

    def crc_pay_create(self, onpay_id, order_amount):
        pattern = ('pay;{po:d};{onpay_id};{order_id:d};{order_amount};'
                   '{currency};0;{secret}')
        result_m5source = pattern.format(po=self.id,
                                         onpay_id=onpay_id,
                                         order_id=self.id,
                                         order_amount=order_amount,
                                         currency=settings.ONPAY_CURRENCY,
                                         secret=settings.ONPAY_SECRET)
        result_m5hash = hashlib.md5(result_m5source).hexdigest().upper()
        return result_m5hash

    def __str__(self):
        return '#%s' % self.pk

    class Meta(object):
        verbose_name = _('payment')
        verbose_name_plural = _('payments')

def order_trigger(**kwargs):
    instance = kwargs['instance']
    created = kwargs['created']
    sender = kwargs['sender']

    if created:
        order_created.send(sender=sender, order=instance)
    else:
        order_updated.send(sender=sender, order=instance)

def mode_trigger(**kwargs):
    instance = kwargs['instance']
    if instance.pk:
        return
    if instance.currency == 'TST':
        instance.mode = instance.MODE_TEST

models.signals.pre_save.connect(mode_trigger, sender=Order,
                                dispatch_uid='onpay.models')
models.signals.post_save.connect(order_trigger, sender=Order,
                                 dispatch_uid='onpay.models')
<commit_msg>decode<commit_after># coding: utf-8

from __future__ import unicode_literals
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.utils import six
from onpay import settings
from onpay.signals import (order_created, order_updated,
                           order_success, order_failure)
import hashlib
import urllib

@python_2_unicode_compatible
class Order(models.Model):
    MODE_LIVE = 0
    MODE_TEST = 1

    STATE_SUCCESS = 0
    STATE_FAILURE = 1
    STATE_WAITING = 3
    STATE_EXPIRED = 2

    amount = models.IntegerField(_('amount'))
    currency = models.CharField(_('currency'),
                                max_length=3,
                                default=settings.ONPAY_CURRENCY,
                                choices=settings.ONPAY_CURRENCIES)
    comment = models.TextField(_('order notes'), default='')
    email = models.EmailField(_('buyer e-mail'))
    date_created = models.DateTimeField(_('created at'), auto_now_add=True)
    state = models.IntegerField(_('order status'),
                                default=STATE_WAITING, choices=(
                                    (STATE_SUCCESS, _('Success')),
                                    (STATE_FAILURE, _('Failed')),
                                    (STATE_WAITING, _('Waiting')),
                                    (STATE_EXPIRED, _('Expired')),
                                ))
    mode = models.IntegerField(_('mode'), default=MODE_LIVE, choices=(
                                   (MODE_LIVE, _('live')),
                                   (MODE_TEST, _('text')),
                               ))

    def mark_as_success(self):
        self._mark(state=self.STATE_SUCCESS, signal=order_success)

    def mark_as_failure(self):
        self._mark(state=self.STATE_FAILURE, signal=order_failure)

    def can_be_payed(self):
        return self.state == self.STATE_WAITING

    def is_successfully_paid(self):
        return self.state == self.STATE_SUCCESS

    def is_pay_failed(self):
        return self.state in [self.STATE_EXPIRED, self.STATE_FAILURE]

    def get_pay_url(self, currency=settings.ONPAY_CURRENCY,
                    success_url=settings.ONPAY_SUCCESS_URL,
                    failure_url=settings.ONPAY_SUCCESS_URL):
        pattern = 'fix;{amount:.1f};{currency};{order_id:d};no;{secret}'
        md5_str = pattern.format(amount=self.amount,
                                 currency=settings.ONPAY_CURRENCY,
                                 secret=settings.ONPAY_SECRET,
                                 order_id=self.id)
        if six.PY3:
            md5_str = md5_str.encode('utf-8')
        md5 = hashlib.md5(md5_str).hexdigest()

        return settings.ONPAY_GATE_URL + '?' + urllib.urlencode({
            'pay_mode': 'fix',
            'f': 7,
            'price': self.amount,
            'pay_for': self.id,
            'convert': 'no',
            'price_final': 'true',
            'user_email': self.email,
            'md5': md5,
            'currency': currency,
            'url_success_enc': success_url,
            'url_fail_enc': failure_url,
        })

    pay_url = property(get_pay_url)

    def _mark(self, state, signal):
        self.state = state
        self.save(update_fields=['state'])
        signal.send(sender=self.__class__, order=self)

    def _check(self, flag=''):
        pattern = 'check;{order_id};{amount:.1f};{currency};{flag}{secret}'
        raw_string = pattern.format(order_id=self.id,
                                    amount=self.amount,
                                    currency=settings.ONPAY_CURRENCY,
                                    secret=settings.ONPAY_SECRET,
                                    flag=flag)
        if six.PY3:
            raw_string = raw_string.encode('utf-8')
        hashed_string = hashlib.md5(raw_string).hexdigest().upper()
        return hashed_string

    def crc_check_correct(self, crc):
        return crc == self.crc_check_get()

    def crc_check_get(self):
        return self._check(flag='')

    def crc_check_create(self):
        return self._check(flag='0;')

    def crc_pay_correct(self, crc, onpay_id, order_amount):
        actual_crc = self.crc_pay_get(onpay_id, order_amount)
        # print("expected crc: %s, actual: %s" % (crc, actual_crc))
        return crc == actual_crc

    def crc_pay_get(self, onpay_id, order_amount):
        pattern = ('pay;{order_id};{onpay_id};{order_amount};'
                   '{currency};{secret}')
        md5source = pattern.format(order_id=self.id,
                                   onpay_id=onpay_id,
                                   order_amount=order_amount,
                                   currency=settings.ONPAY_CURRENCY,
                                   secret=settings.ONPAY_SECRET)
        if six.PY3:
            md5source = md5source.encode('utf-8')
        md5hash = hashlib.md5(md5source).hexdigest().upper()
        return md5hash

    def crc_pay_create(self, onpay_id, order_amount):
        pattern = ('pay;{po:d};{onpay_id};{order_id:d};{order_amount};'
                   '{currency};0;{secret}')
        result_m5source = pattern.format(po=self.id,
                                         onpay_id=onpay_id,
                                         order_id=self.id,
                                         order_amount=order_amount,
                                         currency=settings.ONPAY_CURRENCY,
                                         secret=settings.ONPAY_SECRET)
        if six.PY3:
            result_m5source = result_m5source.encode('utf-8')
        result_m5hash = hashlib.md5(result_m5source).hexdigest().upper()
        return result_m5hash

    def __str__(self):
        return '#%s' % self.pk

    class Meta(object):
        verbose_name = _('payment')
        verbose_name_plural = _('payments')

def order_trigger(**kwargs):
    instance = kwargs['instance']
    created = kwargs['created']
    sender = kwargs['sender']

    if created:
        order_created.send(sender=sender, order=instance)
    else:
        order_updated.send(sender=sender, order=instance)

def mode_trigger(**kwargs):
    instance = kwargs['instance']
    if instance.pk:
        return
    if instance.currency == 'TST':
        instance.mode = instance.MODE_TEST

models.signals.pre_save.connect(mode_trigger, sender=Order,
                                dispatch_uid='onpay.models')
models.signals.post_save.connect(order_trigger, sender=Order,
                                 dispatch_uid='onpay.models')
<|endoftext|>


<commit_before>from django.db import models
from fields import Base64Field

class Queue(models.Model):
    queue = models.CharField(max_length=50)

    class Meta:
        verbose_name = ('Queue Type')
        verbose_name_plural = ('Queue Types')

    def __unicode__(self):
        return(self.queue)

class Email(models.Model):
    """ Queued up Emails to be sent out. """
    m_to = models.TextField('To')
    m_from = models.TextField('From')
    m_cc = models.TextField('CC', null=True, blank=True)
    m_bcc = models.TextField('BCC', null=True, blank=True)

    # Subjects can be be longer if they're split byt CRLF, but this is a good
    # default
    subject = models.CharField(max_length=78)
    body = models.TextField()
    queue = models.ForeignKey(Queue)
    deferred = models.BooleanField(default=False)
    sent = models.BooleanField(default=False)

    class Meta:
        verbose_name = ('Message Queue')
        verbose_name_plural = ('Message Queue')

    def __unicode__(self):
        return('[ %s ] - %s' % (self.queue, self.subject))

STATUS = (
    ('1', 'success'),
    ('2', 'blocked'),
    ('3', 'failure')
)

class Alternative(models.Model):
    email = models.ForeignKey(Email, related_name='alternatives')
    content = models.TextField()
    mimetype = models.CharField(max_length=255)

class Attachment(models.Model):
    email = models.ForeignKey(Email, related_name='attachments')
    filename = models.CharField(max_length=255, null=True, blank=True, default=None)
    content = Base64Field(null=True, blank=True, default=None)
    mimetype = models.CharField(max_length=255, null=True, blank=True, default=None)

class Header(models.Model):
    email = models.ForeignKey(Email, related_name='headers')
    key = models.CharField(max_length=255)
    value = models.CharField(max_length=255)

class Unsubscribe(models.Model):
    """ Any email addresses in this table we will not send mail to.  """
    address = models.EmailField()
    added = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = ('Usubscribe')
        verbose_name_plural = ('Unsubscribe')

    def __unicode__(self):
        pass
<commit_msg>Fixing a stupid unsubscribe error<commit_after>from django.db import models
from fields import Base64Field

class Queue(models.Model):
    queue = models.CharField(max_length=50)

    class Meta:
        verbose_name = ('Queue Type')
        verbose_name_plural = ('Queue Types')

    def __unicode__(self):
        return(self.queue)

class Email(models.Model):
    """ Queued up Emails to be sent out. """
    m_to = models.TextField('To')
    m_from = models.TextField('From')
    m_cc = models.TextField('CC', null=True, blank=True)
    m_bcc = models.TextField('BCC', null=True, blank=True)

    # Subjects can be be longer if they're split byt CRLF, but this is a good
    # default
    subject = models.CharField(max_length=78)
    body = models.TextField()
    queue = models.ForeignKey(Queue)
    deferred = models.BooleanField(default=False)
    sent = models.BooleanField(default=False)

    class Meta:
        verbose_name = ('Message Queue')
        verbose_name_plural = ('Message Queue')

    def __unicode__(self):
        return('[ %s ] - %s' % (self.queue, self.subject))

STATUS = (
    ('1', 'success'),
    ('2', 'blocked'),
    ('3', 'failure')
)

class Alternative(models.Model):
    email = models.ForeignKey(Email, related_name='alternatives')
    content = models.TextField()
    mimetype = models.CharField(max_length=255)

class Attachment(models.Model):
    email = models.ForeignKey(Email, related_name='attachments')
    filename = models.CharField(max_length=255, null=True, blank=True, default=None)
    content = Base64Field(null=True, blank=True, default=None)
    mimetype = models.CharField(max_length=255, null=True, blank=True, default=None)

class Header(models.Model):
    email = models.ForeignKey(Email, related_name='headers')
    key = models.CharField(max_length=255)
    value = models.CharField(max_length=255)

class Unsubscribe(models.Model):
    """ Any email addresses in this table we will not send mail to.  """
    address = models.EmailField()
    added = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = ('Usubscribe')
        verbose_name_plural = ('Unsubscribe')

    def __unicode__(self):
        return(self.address)
<|endoftext|>