Skip to content

Instantly share code, notes, and snippets.

View chelseatroy's full-sized avatar

Chelsea Troy chelseatroy

View GitHub Profile
@chelseatroy
chelseatroy / WeeklySurveyFragment.java
Created July 26, 2020 15:45
Extracting Responses
...
private String extractResponseFor(View view) {
RadioGroup options = (RadioGroup) view.findViewById(R.id.radio_group);
int selection = options.getCheckedRadioButtonId();
if (selection == -1) {
options.setBackgroundColor(getActivity().getResources().getColor(R.color.form_validation));
isFormValid = false;
return getString(R.string.unknown);
} else {
public class WeeklySurveyFragment extends SmartFragment implements WeeklySurveyView, OnWebserviceResponse {
...
@Override
public void onResume() {
super.onResume();
appetiteDecreaseSeverityResponse = getView().findViewById(R.id.appetite_decrease_severity_response);
appetiteDecreaseInterferenceResponse = getView().findViewById(R.id.appetite_decrease_interference_response);
nauseaFrequencyResponse = getView().findViewById(R.id.nausea_frequency_response);
nauseaSeverityResponse = getView().findViewById(R.id.nausea_severity_response);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
@chelseatroy
chelseatroy / fragment_weekly_survey.xml
Created July 26, 2020 15:23
Including a Custom layout in an Existing Layout
...
<include android:id="@+id/appetite_decrease_severity_response"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/severity_question"/>
...
@chelseatroy
chelseatroy / server.py
Created July 26, 2020 03:43
Committing the Entry as the Leader
...
else:
if self.leader:
self.current_operation = string_operation
if self.current_operation.split(" ")[0] in ["set", "delete"]:
key_value_store.write_to_log(string_operation, term_absent=True)
broadcast(self, with_return_address(self, "append_entries ['" + self.current_operation + "']"))
while not self.current_operation_committed:
@chelseatroy
chelseatroy / server.py
Created July 26, 2020 03:37
Conditional Statement—Committing for Followers
...
elif string_operation.split(" ")[0] == "commit_entries":
# followers do this to update their logs.
stringified_logs_to_append = string_operation.replace("commit_entries ", "")
print("Preparing to commit: " + stringified_logs_to_append)
logs_to_append = ast.literal_eval(stringified_logs_to_append)
[key_value_store.write_to_state_machine(command, term_absent=True) for command in logs_to_append]
response = "Commit entries call successful!"
@chelseatroy
chelseatroy / server.py
Created July 26, 2020 03:27
Marking Follower Servers Updated
...
def mark_updated(self, server_name):
self.followers_with_update_status[server_name] = True
trues = len(list(filter(lambda x: x is True, self.followers_with_update_status.values())))
falses = len(list(filter(lambda x: x is False, self.followers_with_update_status.values())))
if trues >= falses:
print("Committing entry: " + self.current_operation)
self.current_operation_committed = True
@chelseatroy
chelseatroy / server.py
Created July 26, 2020 03:21
Conditional Statement—Responding to Successful Log Replication Requests
...
elif string_operation == "Append entries call successful!":
if self.leader:
self.mark_updated(server_name)
send_pending = False
@chelseatroy
chelseatroy / server.py
Last active July 26, 2020 03:41
Keeping Track of Followers
class Server:
...
self.followers_with_update_status = {}
self.current_operation = ''
self.current_operation_committed = False
for server_name in other_server_names(name):
self.followers_with_update_status[server_name] = False
@chelseatroy
chelseatroy / server.py
Last active July 26, 2020 03:38
Conditional Statement—Responding to append_entries Requests
...
if string_operation.split(" ")[0] == "append_entries":
# followers do this to update their logs.
stringified_logs_to_append = string_operation.replace("append_entries ", "")
print("Preparing to append: " + stringified_logs_to_append)
logs_to_append = ast.literal_eval(stringified_logs_to_append)
[key_value_store.write_to_log(log, term_absent=True) for log in logs_to_append]
print("State machine after appending: " + str(key_value_store.data))
response = "Append entries call successful!"