text stringlengths 1 2.12k | source dict |
|---|---|
homework, database, cobol
The grammar here is not great.
Always remember: the goal of a diagnostic is to tell the
user what to do to Win, not to advise him that he is Losing.
Here, the verb "ensure" is too passive.
Be direct, tell the user what to do!
Please input a proper....
Or better, please specify, though perhaps... | {
"domain": "codereview.stackexchange",
"id": 45412,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "homework, database, cobol",
"url": null
} |
homework, database, cobol
automated tests
I have no idea what the current state-of-the-art is
for COBOL unit tests.
But certainly no such tests were offered in the OP submission.
A suite of passing tests improves our level of confidence that
code works correctly for the regular and for the corner cases.
It gives us th... | {
"domain": "codereview.stackexchange",
"id": 45412,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "homework, database, cobol",
"url": null
} |
python, strings
Title: One Time Pad encryption in Python
Question: I made this code to encrypt a string with the One Time Pad method. Is this pythonic code, and are there obvious ways I can improve?
The program accepts a plaintext and can either accept an inputted key or generate one itself. It adds the key and plain... | {
"domain": "codereview.stackexchange",
"id": 45413,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, strings",
"url": null
} |
python, strings
encoded_str = insert_invalid(encoded_str, invalid_chars)
return encoded_str, code_str
def decode(decode_string: str, code_string: str) -> str:
decode_string, invalid_chars = clean_str(decode_string, legal_chars)
string_nums: list[int] = [legal_chars.index(char) for char in decode_string]
... | {
"domain": "codereview.stackexchange",
"id": 45413,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, strings",
"url": null
} |
python, strings
decoded_str = insert_invalid(decoded_str, invalid_chars)
return decoded_str
def run(runMode: chr) -> None:
if runMode == "e":
chars = "".join(legal_chars)
print(f"Only the following characters are accepted: {chars}\n")
text: str = input("Enter the message to be encoded:... | {
"domain": "codereview.stackexchange",
"id": 45413,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, strings",
"url": null
} |
python, strings
Answer: true random
random_generator = secrets.SystemRandom()
Kudos, thank you for using the appropriate interface.
(No Mersenne Twister or other PRNG.)
DRY
mode = input("Do you ...
while ...
mode = input("Do you ...
It's worth
drying
this up a bit.
Even if you change noth... | {
"domain": "codereview.stackexchange",
"id": 45413,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, strings",
"url": null
} |
python, strings
I don't understand that.
You meant to explain the key must be at least as long as the message, right?
Also, wouldn't you rather call the key key or otp or pad, instead of code?
I would expect a code variable to maybe contain ciphertext.
I guess in fairness, the trouble started with the
misleading "Ente... | {
"domain": "codereview.stackexchange",
"id": 45413,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, strings",
"url": null
} |
python, strings
This is a moderately expensive lookup.
You're asking the cPython interpreter to repeatedly do a linear scan of that string.
Now, it is C code doing the looping, not interpreted bytecode, but still.
Consider building a dict beforehand,
so within the loop we'll see just O(1) constant time lookups.
Simila... | {
"domain": "codereview.stackexchange",
"id": 45413,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, strings",
"url": null
} |
python, strings
nit: If you insert just a handful of SPACE characters after : colons,
then it should be visually apparent to the user that cleartext,
ciphertext, and OTP all have the identical length.
(Or it will be apparent how many characters from end of pad went unused.)
tricky code
character: chr = legal_c... | {
"domain": "codereview.stackexchange",
"id": 45413,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, strings",
"url": null
} |
python, strings
Thank you for that.
It would have been even nicer if wrapped up within a
test suite.
summary
You clearly went to a lot of trouble to come up with well-considered identifiers,
and to break out
single responsibility
helpers where needed.
That is all for the good, keep it up!
And in some cases those optio... | {
"domain": "codereview.stackexchange",
"id": 45413,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, strings",
"url": null
} |
python, strings
portions of the pad, to help prevent accidental reuse.
A pure CLI approach might be more convenient, and more composable,
than the current interactive prompting.
So there's room for feature requests, if you want to keep playing with this. | {
"domain": "codereview.stackexchange",
"id": 45413,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, strings",
"url": null
} |
python, strings
This codebase achieves its design goals.
I would be willing to delegate or accept maintenance tasks on it. | {
"domain": "codereview.stackexchange",
"id": 45413,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, strings",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
Title: A recursive_find_if_all Template Function Implementation in C++
Question: This is a follow-up question for recursive_any_of and recursive_none_of Template Functions Implementation in C++. I am trying to follow the suggestion of G. Sliepen's answer to impl... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
template<class...Ts1, template<class...>class Container1, typename... Ts>
struct recursive_unwrap_type<1, Container1<Ts1...>, Ts...>
{
using type = std::ranges::range_value_t<Container1<Ts1...>>;
};
template<std::size_t unwrap_level, class...Ts1, template<cl... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
template<std::size_t unwrap_level, typename F, class...Ts1, template<class...>class Container1, typename... Ts>
requires ( std::ranges::input_range<Container1<Ts1...>> &&
requires { typename recursive_variadic_invoke_result<
... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
template< std::size_t unwrap_level,
typename F,
template<class, std::size_t> class Container,
typename T,
std::size_t N>
requires ( std::ranges::input_range<Container<T, N>> &&
requires { typename re... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
template<std::size_t unwrap_level, template<class, std::size_t> class Container,
typename T,
std::size_t N>
requires ( std::ranges::input_range<Container<T, N>> &&
requires { typename recursive_array_unwrap_type<
... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
// recursive_depth function implementation with target type
template<typename T_Base, typename T>
constexpr std::size_t recursive_depth()
{
return std::size_t{0};
}
template<typename T_Base, std::ranges::input_range Range>
requires (!std::same_as<Range, T_B... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
template<class T, class Proj = std::identity, class F>
constexpr auto recursive_foreach_all(T& inputRange, F f, Proj proj = {})
{
impl::recursive_for_each_state state(std::move(f), std::move(proj));
impl::recursive_foreach_all(inputRange, state);
retu... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
template<std::size_t unwrap_level, class T, class Pred>
requires(unwrap_level <= recursive_depth<T>())
constexpr auto recursive_count_if(const T& input, const Pred& predicate)
{
if constexpr (unwrap_level > 0)
{
return std::transform_reduce(std::r... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
// recursive_any_of template function implementation
template<class T, class Proj = std::identity, class UnaryPredicate>
constexpr auto recursive_any_of(T&& value, UnaryPredicate&& p, Proj&& proj = {})
{
return recursive_find_if_all(value, p, proj);
}
// r... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
auto test_vectors_1 = n_dim_container_generator<std::vector, 4, int>(1, 3);
test_vectors_1[1][0][0][0] = 3;
std::cout << "Play with test_vectors_1:\n";
if(recursive_find_if_all(test_vectors_1, [](int i) { return i == 1; }))
std::cout << "1 is ... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
std::cout << "Projection Tests\n";
if(recursive_any_of(test_vectors_1,
[](int i) { return i == 1; },
[](int i) { return i + 1; }))
std::cout << "1 is one of the elements in test_vectors_1... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
int main()
{
auto start = std::chrono::system_clock::now();
recursive_find_if_all_tests();
recursive_any_of_tests();
recursive_none_of_tests();
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = en... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
Answer: The problem seems so simple, and at first glance the implementation looks OK. However, looks can be deceiving; there are actually lots of issues with this code.
Never std::forward() the same object multiple times
You are calling std::forward() multiple ti... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
However, the predicate will not be applied directly to a T, but rather to the projection of a T. So you should instead do something like:
requires(std::invocable<UnaryPredicate, std::invoke_result_t<Proj, T>>)
However, that is not the only issue. Consider that t... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
c++, algorithm, recursion, c++20, constrained-templates
Your recursing version of recursive_find_if_all() takes inputRange by lvalue reference, whereas the non-recursing one takes it by forwarding reference. They should all take the range by forwarding reference (just like the STL algorithms do).
The predicate does no... | {
"domain": "codereview.stackexchange",
"id": 45414,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, recursion, c++20, constrained-templates",
"url": null
} |
javascript, mysql, node.js, express.js
Title: Consuming sharded database using node.js
Question: I think the only big improvement that can be made is to check which shard to query based on the userIds of the followed users. One easy way is to check the last number of each userId (0,1,2,3,4,5,6,7,8,9) and then find wh... | {
"domain": "codereview.stackexchange",
"id": 45415,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "javascript, mysql, node.js, express.js",
"url": null
} |
javascript, mysql, node.js, express.js
// Step 3: Retrieve Tweets from Followed Users
const tweets = [];
for (const shardConnection of shardConnections) {
const tweetsQuery = 'SELECT * FROM tweets WHERE user_id IN (?) ORDER BY timestamp DESC';
const [shardTweets] = await shardConnection.execute(twe... | {
"domain": "codereview.stackexchange",
"id": 45415,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "javascript, mysql, node.js, express.js",
"url": null
} |
javascript, mysql, node.js, express.js
Querying each shard in a loop is not the best way, as it does not utilize the potential for parallel queries. A better approach is to initiate all shard queries simultaneously using Promise.all and then combine the results thus your queries will run in parallel, reducing the over... | {
"domain": "codereview.stackexchange",
"id": 45415,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "javascript, mysql, node.js, express.js",
"url": null
} |
c++
Title: Code for functionality like "valid pointer or default" and "true or default"
Question: Relatively new to C++, not sure if I'm doing everything right, any advice would be appreciated. The site does not allow me to post this question with that little actual text, so I am artificially extending it with this d... | {
"domain": "codereview.stackexchange",
"id": 45416,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++",
"url": null
} |
c++
Checking for null pointers and zero values:
In Default, you check if (!(bool)value). This works for numeric types where 0 is implicitly convertible to false, but it might not be clear or applicable for all types T. Consider if there's a more explicit or type-safe way to handle this, depending on what types you ex... | {
"domain": "codereview.stackexchange",
"id": 45416,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++",
"url": null
} |
c, file, io
Title: C- Reading and Parsing textfile
Question: I'm relatively new to C programming and currently tackling exercises on Advent of Code. The challenge I'm working on involves calculating the sum of integers within each group from a file and identifying the top 3 groups based on their sums.
Here's an examp... | {
"domain": "codereview.stackexchange",
"id": 45417,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c, file, io",
"url": null
} |
c, file, io
closeFile(file);
(*buffer)[fileSize] = '\0';
return bytesRead;
}
int main(int argc, char* argv[]) {
char *buffer = NULL;
size_t fsize = readFile(argv[1], &buffer);
bool line_changed = false;
int max1 = 0;
int max2 = 0;
int max3 = 0;
int cum = 0;
int line... | {
"domain": "codereview.stackexchange",
"id": 45417,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c, file, io",
"url": null
} |
c, file, io
There is no reason to read the entire file at once. Even if you insist on handling one character at a time, buffer[i] is no better than fgetc. The performance gain is infinitesimal, and is likely overshadowed by the overhead of memory allocation and three system calls in file size computation.
And of cour... | {
"domain": "codereview.stackexchange",
"id": 45417,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c, file, io",
"url": null
} |
php, random, security
Title: Cryptographically secure version of the core array_rand() function
Question: I want a cryptographically secure version of array_rand(). Is this it?
/**
* retrieve a random key from an array, using a cryptographically secure rng.
* - it does the same as array_rand(), except that this one... | {
"domain": "codereview.stackexchange",
"id": 45418,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "php, random, security",
"url": null
} |
php, random, security
Significant edit: I figured I can use array_is_list() to avoid copying the keys when given a list.
Answer:
array_is_list() is available from PHP8.1. You can check the PHP version major&minor version with version_compare() (version_compare(PHP_VERSION, '8.1', '>=')) or function_exists() -- the l... | {
"domain": "codereview.stackexchange",
"id": 45418,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "php, random, security",
"url": null
} |
php, random, security
And finally, the most compact and perhaps hardest to read because of all of the nested function calls: (Demo)
function array_rand_cryptographically_secure(array $array): int|string {
if (!$array) {
throw new ValueError ('Argument #1 ($array) cannot be empty');
}
return key(arr... | {
"domain": "codereview.stackexchange",
"id": 45418,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "php, random, security",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
Title: Bit vector in Java supporting O(1) rank() and O(log n) select()
Question: Introduction
I have this GitHub repository (version 1.0.0.). It implements a rank(i) operation in \$\Theta(1)\$ time, and select(i) operation in \$\Theta(\log n)\$ time.
(This post has an continu... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
/**
* This class defines a packed bit vector that supports {@code rank()} operation
* in {@code O(1)} time, and {@code select()} in {@code O(log n)} time.
*
* @version 1.0.0
* @since 1.0.0
*/
public final class RankSelectBitVector {
/**
* Indicates whether... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
(numberOfRequestedBits % Byte.SIZE != 0 ? 1 : 0);
numberOfBytes++; // Padding tail byte in order to simplify the last
// rank/select.
bytes = new byte[numberOfBytes];
// Set the rightmost, valid index... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
//// Deal with the 'second'.
this.k = (int) Math.ceil(log2(n) / 2.0);
this.second = new int[n / k + 1];
for (int i = k; i < n; i++) {
if (i % k == 0) {
second[i/k] = bruteForceRank(ell * (i / ell), i - 1);
... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
}
/**
* Writes the {@code index}th bit to {@code on}.
*
* @param index the index of the target bit.
* @param on the selector of the bit: if {@code true}, the bit will be
* set to one, otherwise set zero.
*/
public vo... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
*
* @param index the target index.
* @return the rank of the input index.
*/
public int rankThird(int index) {
checkBitIndexForRank(index);
makeSureStateIsCompiled();
int f = first[index / ell];
int s = second[index ... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
* @return the index of the {@code index}th 1-bit.
*/
public int selectThird(int bitIndex) {
checkBitIndexForSelect(bitIndex);
return selectImplThird(bitIndex, 0, getNumberOfSupportedBits());
}
private int selectImplFirst(int bitIndex,
... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
if (rangeLength == 1) {
return rangeStartIndex;
}
int halfRangeLength = rangeLength / 2;
int r = rankThird(halfRangeLength + rangeStartIndex);
if (r >= bitIndex) {
return selectImplThird(bitIndex,
... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
buildIndices();
hasDirtyState = false;
}
}
/**
* Turns the {@code index}th bit on. Indexation is zero-based.
*
* @param index the target bit index.
*/
private void turnBitOn(int index) {
int byteIndex = index / ... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
}
}
private void checkBitIndexForRank(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException(
String.format("Negative bit index: %d.", index));
}
if (index > numberOfRequestedBits) {
... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
if (bit == true) {
integer |= 1 << i;
}
}
return integer;
}
private RankSelectBitVector extractBitVector(int i) {
int startIndex = k * (i / k);
int endIndex = Math.min(k * (i / k + 1) - 2, maximu... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
com.github.coderodde.util.RankSelectBitVectorBenchmark.java:
package com.github.coderodde.util.benchmark;
import com.github.coderodde.util.RankSelectBitVector;
import java.util.Random; | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
public final class RankSelectBitVectorBenchmark {
/**
* The number of bits in the benchmark bit vector.
*/
private static final int BIT_VECTOR_LENGTH = 4_000_000;
public static void main(String[] args) {
long seed = parseSeed(args);
... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
throw new IllegalArgumentException("Rank array length mismatch.");
}
int n = Math.max(rankArray1.length, rankArray2.length);
for (int i = 0; i != n; i++) {
int rank1 = rankArray1[i];
int rank2 = rankArray2[i];
... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
answers3[i] = rankSelectBitVector.rankThird(i);
}
long answersDuration3 = System.currentTimeMillis() - st;
System.out.printf(
"rankThird() ran for %d milliseconds.\n",
answersDuration3);
... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
}
long answersDuration3 = System.currentTimeMillis() - st;
System.out.printf(
"selectThird() ran for %d milliseconds.\n",
answersDuration3);
if (!rankArraysEqual(answers1, answers2)) {
... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
com.github.coderodde.util.RankSelectBitVectorTest.java:
package com.github.coderodde.util;
import java.util.Random;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Asse... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
public final class RankSelectBitVectorTest {
@Test
public void lastBitRank() {
RankSelectBitVector bv = new RankSelectBitVector(8);
bv.writeBitOn(2);
bv.writeBitOn(6);
bv.writeBitOn(7);
assertEquals(3, bv.r... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
bv.writeBitOn(4);
bv.writeBitOn(5);
bv.writeBitOn(7);
bv.writeBitOn(8);
bv.writeBitOn(10);
bv.writeBitOn(12);
bv.writeBitOn(13);
bv.writeBitOn(15);
assertEquals(0, bv.rankThird(0));
asser... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
assertEquals(0, bv.rankThird(1));
assertEquals(0, bv.rankThird(2));
assertEquals(1, bv.rankThird(3));
assertEquals(1, bv.rankThird(4));
assertEquals(2, bv.rankThird(5));
assertEquals(3, bv.rankThird(6));
assertEquals(3, bv.rankTh... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
BruteForceBitVector referenceBv = copy(bv);
bv.buildIndices();
int numberOfOneBits = bv.rankThird(bv.getNumberOfSupportedBits());
for (int i = 0; i < bv.getNumberOfSupportedBits(); i++) {
int actualRank = referenceB... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
if (rank3 != actualRank) {
System.out.printf(
"ERROR: i = %d, actual rank = %d, rank1 = %d, " +
"rank2 = %d, rank3 = %d.\n",
i,
actualRank,
... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
RankSelectBitVector bitVector = new RankSelectBitVector(30);
bitVector.writeBit(12, true);
assertTrue(bitVector.readBit(12));
bitVector.writeBit(12, false);
assertFalse(bitVector.readBit(12));
assertFalse(bitVector.readBit(13));
}
... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
java, algorithm, bitwise, vectors, bitset
(The missing class BruteForceBitVector is here.)
Typical benchmark demo output
Seed = 1706175245835
Built the bit vector in 74 milliseconds.
Preprocessed the bit vector in 117 milliseconds.
--- Benchmarking rank operation ---
rankFirst() ran for 623 milliseconds.
rankSecond() ... | {
"domain": "codereview.stackexchange",
"id": 45419,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, algorithm, bitwise, vectors, bitset",
"url": null
} |
python, performance
Title: Tokenize a file
Question: Objective:
The goal of this script is to tokenize and print out all the words from the provided bible.txt file.
Constraints:
It should differentiate between 'U.K' and 'UK.' when removing extra symbols from the word.
It should exclude duplicates.
It should exclude ... | {
"domain": "codereview.stackexchange",
"id": 45420,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, performance",
"url": null
} |
python, performance
Answer: right tool for the job
You called nlp(line.strip()) to tokenize the input text.
Using the power of spacy here is not appropriate.
It can do many things, and you're barely taking advantage of any of them.
No need to pay the cost for what we don't use.
Please see the enclosed code below.
I do... | {
"domain": "codereview.stackexchange",
"id": 45420,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, performance",
"url": null
} |
python, performance
some other corpus.
verification
This code produces a pair of debugging log files.
They are convenient for monitoring progress.
But even better, they are formatted for convenient diffing,
so we can verify the simple tokenizer's results are nearly
identical to the reference results produced by spacy.... | {
"domain": "codereview.stackexchange",
"id": 45420,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, performance",
"url": null
} |
python, performance
lines are typically smaller than sentences,
and after we've seen a few hundred unique words
it will be commonly the case that a given line
is comprised entirely of seen words.
So we might do a cheap parse to identify each "boring" line,
and only do an expensive spacy call on lines containing novel ... | {
"domain": "codereview.stackexchange",
"id": 45420,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, performance",
"url": null
} |
python, performance
#! /usr/bin/env _TYPER_STANDARD_TRACEBACK=1 python
from collections import Counter
from operator import itemgetter
from pathlib import Path
from pprint import pp
from typing import Generator, TextIO
from spacy.language import Language
from spacy.tokens.token import Token
import spacy
import spacy.... | {
"domain": "codereview.stackexchange",
"id": 45420,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, performance",
"url": null
} |
python, performance
def main(in_file: Path) -> None:
with open(in_file) as fin:
temp = Path("/tmp")
simp_txt = temp / "bible_simple.txt"
spcy_txt = temp / "bible_spacy.txt"
with open(simp_txt, "w") as simp_out, open(spcy_txt, "w") as spcy_out:
spacy_wordlist(fin, simp_ou... | {
"domain": "codereview.stackexchange",
"id": 45420,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, performance",
"url": null
} |
go
Title: A very simple secrets cache
Question: This package is a part of a web application which is an internal company tool. This web application may need access to a number of secretes stored in hashicorp vault. The secrets rarely change, although in case they occasionally do there is a UI button to invalidate the... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
go
// May ocasionally be requested from web UI
func ResetCache() {
cacheLock.Lock()
defer cacheLock.Unlock()
cache = make(map[string]map[string]interface{})
}
token.go:
package vault
import (
"fmt"
"sync"
vault "github.com/hashicorp/vault/api"
)
var token string
var tokenLock sync.RWMutex
... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
go
func readFromVault(path string) (map[string]interface{}, error) {
client, err := vault.NewClient(&vault.Config{
Address: config.Url,
})
if err != nil {
return nil, fmt.Errorf("error creating vault client for %s: %v", config.Url, err)
}
c := client.Logical()
client.SetToken(ge... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
go
The first thing that looks odd to me is that your vault package relies on 2 global variables, that are otherwise not linked. You seem to be storing some arbitrary set of cached values under a key, all stored as a map[string]interface{}. Your functions aren't exported (except for the ResetCache one), which means all... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
go
Now imagine rmu is not a pointer. The Get call will receive a copy of the mutex, and a copy of the map, but a map is a reference type, so effecitvely it is shared should the Get function and Reset be called concurrently. Just like that, you'd have a data race, hence: the mutex has to be a pointer: a copy to a memor... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
go
Overall, this makes the cache type a lot more flexible, and allows it to be used with any type of key, and any type of data. If the flexibility in terms of data you want to cache is not that important to you, I still maintain that supporting different key types makes a lot of sense. Clearly, you're wanting to have ... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
go
Your cache implementation either treats all values as valid, or none. I'd argue that, if you're caching data in a map[string]map[string]any, you really would want to allow specific keys in the cache to be cleared/reset without it impacting the rest of the cache. Again: looking at the example of using this type of c... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
go
will instead look more like this:
// assume the token is stored like this:
var tokenV atomic.Value
tokenV.Store(tokenStr)
token := tokenV.Load().(string) // get the token value
client.SetToken(token)
When refreshing the token, I'd probably pass in the expired token as an argument, so you can (atomically) check if... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
go
You may have noticed that I've renamed the functions from ResetCache to Reset[All|Keys], GetCached to Get, and similarly I renamed GetToken and RefreshToken to Get and Refresh. The reason for this is to avoid something referred to as stutter. As I explained above: the cache component is a "general purpose" package ... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
go
It's quite important to not loose track of the way your packages will be used. We write packages to encapsulate or abstract away the intricacies that we don't want to deal with when writing logic. Should the user care that, when refreshing the token, they need to acquire a lock on said token, or that they should ac... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
go
func NumMin[T Numeric](a, b T) T {
if a < b {
return a
}
return b
}
Clearly, the last 2 functions can replace the first 2, so why did someone add their own max function? It's possible that it was just a convenience thing (the programmer didn't know about the NumMax being a thing), but in this p... | {
"domain": "codereview.stackexchange",
"id": 45421,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "go",
"url": null
} |
python, python-3.x, programming-challenge, community-challenge
Title: Finding the position in a triangle for the given challenge
Question: The LAMBCHOP doomsday device takes up much of the interior of Commander Lambda's space station, and as a result
the prison blocks have an unusual layout. They are stacked in a tr... | {
"domain": "codereview.stackexchange",
"id": 45422,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, python-3.x, programming-challenge, community-challenge",
"url": null
... |
python, python-3.x, programming-challenge, community-challenge
This method takes advantage of basic algebra. It uses the sum of the arithmetic progression of x to calculate the id of the bottom right corner, and then subtracts the difference of the input coordinate from the bottom right corner. The reason it take the ... | {
"domain": "codereview.stackexchange",
"id": 45422,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, python-3.x, programming-challenge, community-challenge",
"url": null
... |
python, file, serialization, tkinter, text-editor
Title: A little Python hex editor
Question: First off
I'm quite new to Python, there will be a lot of messy/overcomplicated code, that's why I'm posting on this site.
This code is written in Python (2.7) using the Tkinter library.
Questions
To allow for viewing/editin... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
def resize(self, event = None):
"""called when the window is resized. Re-calculates
the chars on each row"""
self.width = self.mainText.winfo_width() / 8
self.height = self.mainText.winfo_height() / 16
if not self.width / 3 ... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
if data is None:
data = "".join(self.lines)
with open(filename, "wb") as f:
f.write(self.binascii.unhexlify(data))
def saveAll(self, event = None):
"""saves a file (for binding a key to)"""
self.setBlock(... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
def saveAsWindow(self, event = None):
"""Opens the 'save as' popup"""
f = self.tkFileDialog.asksaveasfilename(filetypes = self.defaultFiles)
if f is None or f is "":
return
else:
self.saveFile(f)
self.... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
def q(self, event = None):
"""quits (for binding a key to"""
self.root.destroy()
def neatify(self,data):
"""adds a space every 2 chars (splitss
into bytes)"""
out = ""
for line in data:
count = 0
... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
self.lineNumber += 1
self.mainText.delete("1.0","end")
self.mainText.insert("1.0", self.getBlock(self.lineNumber)) | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
def scroll(self, event = None, direction = None):
"""calls the correct scroll function"""
if self.mainText.index("insert").split(".")[0] == str(self.height + 1):
self.scrollTextDown()
elif self.mainText.index("insert").split(".")[0] ... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
self.root.config(menu = self.menu)
#up and down bound to the scroll function to check if the text should scroll
self.root.bind("<Down>", self.scroll)
self.root.bind("<Up>", self.scroll)
self.root.bind("<Control-s>", self.saveAll)
... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
Code organization
You have several place where code is duplicated and could benefit from refactoring, such as opening a file — resizing the window, scrolling up — scrolling down, saving the current content of mainText into memory…
You also have the defineWidgets and in... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
Pad the last tuple with '' if need be.
>>> list(character_grouper('This is a test', 3))
[('T', 'h', 'i'), ('s', ' ', 'i'), ('s', ' ', 'a'), (' ', 't', 'e'), ('s', 't', '')]
"""
args = [iter(iterable)] * n
return izip_longest(*args, fillvalue='')
c... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
self.root.config(menu=self.menu)
self.root.bind("<Down>", self.scroll)
self.root.bind("<Up>", self.scroll)
self.root.bind("<Control-s>", self.save_file)
self.root.bind("<Control-o>", self.open_window)
self.root.bind("<Control-S>"... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
def close(self, event=None):
self.root.destroy()
def saveas_window(self, event=None):
"""Open the 'save as' popup"""
f = tk_file_dialog.asksaveasfilename(filetypes=DEFAULT_FILE_TYPES)
if f:
self.filename = f
... | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
python, file, serialization, tkinter, text-editor
if __name__ == '__main__':
Window().run()
Side note
If you are new to Python, then I highly recommend to use Python 3 instead of Python 2 whose support is reaching end of life. You will benefit from the latest modules and features. | {
"domain": "codereview.stackexchange",
"id": 45423,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, file, serialization, tkinter, text-editor",
"url": null
} |
c#, entity-framework
Title: EF save method that has three conditions
Question: I created this save method that has two conditions. One condition is checking if coordinatorsId is not 0 and another one is checking if Subject is not empty string. Is there a better way to do the two conditions?
I have come up with the be... | {
"domain": "codereview.stackexchange",
"id": 45424,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#, entity-framework",
"url": null
} |
c#, entity-framework
Answer: Let's start with proper names. Parameters and local variables must have camelCase style, properties must have PascalCase.
When working with a database, the most important thing is to reduce the number of queries to it.
Therefore, it may make sense to do some checks at the beginning of the ... | {
"domain": "codereview.stackexchange",
"id": 45424,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#, entity-framework",
"url": null
} |
java, beginner, game, console
Title: Text based Java game "Battle Arena"
Question: This is my first java program. I'm coming from a python background. This is a text based combat arena game.
Are there any ways I could better implement the overall code structure?
How might I improve the math of the attack() function?... | {
"domain": "codereview.stackexchange",
"id": 45425,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, beginner, game, console",
"url": null
} |
java, beginner, game, console
static void printStats(Character c) {
Console cnsl = System.console();
String fmt = "%1$-10s %2$-1s%n";
System.out.println("\n" + c.cname + "\'s Stats:\n---------------");
cnsl.format(fmt, "Health:", c.health);
cnsl.format(fmt, "Defense:", c.defense... | {
"domain": "codereview.stackexchange",
"id": 45425,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, beginner, game, console",
"url": null
} |
java, beginner, game, console
static int getOption() {
Scanner scanObj = new Scanner(System.in);
System.out.println("\nEnter option: (1 to battle, 2 to escape!)");
int option = scanObj.nextInt();
return option;
}
public static void main(String[] args) {
Game myGame = ne... | {
"domain": "codereview.stackexchange",
"id": 45425,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, beginner, game, console",
"url": null
} |
java, beginner, game, console
Answer: Make as many of the properties of Character to be final as possible. init (which should be named initiative) should not be a property at all.
Delete getRandom. Pass in an instance of Random for testability, and call its nextInt. Make Character a static inner class - it won't be ab... | {
"domain": "codereview.stackexchange",
"id": 45425,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, beginner, game, console",
"url": null
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.