Skip to content

Sourcery refactored main branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Aug 12, 2023

Branch main refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the main branch, then run:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from Frischifrisch August 12, 2023 04:22
Comment on lines -12 to +13
else:
print(f"Request failed with status code {response.status_code}")
return None
print(f"Request failed with status code {response.status_code}")
return None
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function make_request refactored with the following changes:

Comment on lines -118 to +117
if len(model_list) == 0:
if not model_list:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

if (env in os.environ):
return os.environ[env]
return default
return os.environ.get(env, default)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function env_or_def refactored with the following changes:

Comment on lines -17 to +15
today = datetime.datetime.today()
today = datetime.datetime.now()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 17-17 refactored with the following changes:

if (env in os.environ):
return os.environ[env]
return default
return os.environ.get(env, default)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function env_or_def refactored with the following changes:

with open(file) as f:
grammar = f.read()
grammar = Path(file).read_text()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LlamaGrammar.from_file refactored with the following changes:

  • Simplify basic file reads with pathlib (path-read)

else:
self[key] = value
return self.iterator(self, key), True
self[key] = value
return self.iterator(self, key), True
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function std.map.insert refactored with the following changes:

Comment on lines -365 to +363
if key in self:
return self.iterator(self, key)
else:
return self.end()
return self.iterator(self, key) if key in self else self.end()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function std.map.find refactored with the following changes:

Comment on lines -503 to +498
state.symbol_ids[base_name + "_" + str(next_id)] = next_id
state.symbol_ids[f"{base_name}_{str(next_id)}"] = next_id
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function generate_symbol_id refactored with the following changes:

Comment on lines -605 to +600
raise RuntimeError("expecting " + str(size) + " hex chars at " + str(src))
raise RuntimeError(f"expecting {size} hex chars at {str(src)}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_hex refactored with the following changes:

Comment on lines -649 to +644
raise RuntimeError("unknown escape at " + str(src))
raise RuntimeError(f"unknown escape at {str(src)}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_char refactored with the following changes:

Comment on lines -671 to +666
raise RuntimeError("expecting name at " + str(src))
raise RuntimeError(f"expecting name at {str(src)}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_name refactored with the following changes:

Comment on lines -738 to -740
# } else if (*pos == '[') { // char range(s)
# pos++;
# enum llama_gretype start_type = LLAMA_GRETYPE_CHAR;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_sequence refactored with the following changes:

This removes the following comments ( why? ):

#     }
# } else if (*pos == '(') { // grouping
#     const char * name_end    = parse_name(pos);
#     last_sym_start = out_elements.size();
# } else if (*pos == '[') { // char range(s)
#     break;
#     enum llama_gretype start_type = LLAMA_GRETYPE_CHAR;
#     out_elements.push_back({LLAMA_GRETYPE_RULE_REF, ref_rule_id});
# } else if (is_word_char(*pos)) { // rule reference
#     pos = parse_space(pos + 1, is_nested);
# } else {
#     // output reference to synthesized rule
#     uint32_t sub_rule_id = generate_symbol_id(state, rule_name);
#     if (last_sym_start == out_elements.size()) {
#         throw std::runtime_error(std::string("expecting preceeding item to */+/? at ") + pos);
#         throw std::runtime_error(std::string("expecting ')' at ") + pos);
#     out_elements.push_back({LLAMA_GRETYPE_RULE_REF, sub_rule_id});
#     if (*pos != ')') {
#     uint32_t     ref_rule_id = get_symbol_id(state, pos, name_end - pos);
#     pos++;
# } else if (*pos == '*' || *pos == '+' || *pos == '?') { // repetition operator
#     pos = parse_space(name_end, is_nested);
#     pos = parse_space(pos + 1, true);
# }
#     // parse nested alternates into synthesized rule
#     pos = parse_alternates(state, pos, rule_name, sub_rule_id, true);

Comment on lines -967 to +935
if not (pos[0] == ":" and pos[1] == ":" and pos[2] == "="):
raise RuntimeError("expecting ::= at " + str(pos))
if pos[0] != ":" or pos[1] != ":" or pos[2] != "=":
raise RuntimeError(f"expecting ::= at {str(pos)}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_rule refactored with the following changes:

Comment on lines -1016 to +983
if 0x20 <= c and c <= 0x7F:
if 0x20 <= c <= 0x7F:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function print_grammar_char refactored with the following changes:

  • Combine two compares on same value into a chained compare (chain-compares)

Comment on lines -1059 to +1026
"malformed rule, does not end with LLAMA_GRETYPE_END: " + str(rule_id)
f"malformed rule, does not end with LLAMA_GRETYPE_END: {rule_id}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function print_rule refactored with the following changes:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants