Husoski 60 Newbie Poster

Yes, you can do that. The normal way to do that is to create a source file with the variable definitions that you then import into other source files that want to use them. A simple example:

# This is "my_app_globals.py"
MY_APP_NAME    = "My Application"
MY_APP_VERSION = "1.02"
MY_APP_NVERSION = (1, 2)

Then you can ues that file in another Python source file with something like:

# This is "main.py"
import my_app_globals

print("App Name:", my_app_globals.MY_APP_NAME)
print("Version: ", my_app_globals.MY_APP_VERSION)

The longer story is that import reads and compiles all of a single Python source file and puts of of the definitions into a module with a name that is (by default) the source file name without the .py suffix. (The compilation part will be skipped if an up-to-date precompiled file is found, but that's a run-time optimization. The principle is the same.) Any top-level code (not in a class or function definition) will be run once at import time to "initialize the module". All global names in that source file are stored in the module, available to other modules import it.

Those names are pretty long. You can shorten them in a couple of ways. One way that's pretty safe is to simply rename the module on import:

import my_app_globals as gbls
print("App Name:", gbls.MY_APP_NAME)

That makes "gbls" a name your program can use to access the "my_app_globals" module. The original module is only imported once per execution of your program. Another way is to use another form of the import …

Husoski 60 Newbie Poster

Your comments on lines 41-43 say why you get an error on line 46. The data variable is a list, so data['hours'] is invalid. The error message says what you should already know...lists are indexed with numbers, not strings.

I don't have an authorization key to use for that API, so I can't look at any real data. If you wanted to sort a list of dictionaries, based on the 'hours' field in each dictionary, you don't need an index at all. Use sorted(data, key=lambda i:i['hours']) without the index.

Looking closer, it looks like you are using the variable data for two different purposes. You read the previously cached data into it on line 16, and then replace that with the API response data. That discards the cache file data before you even get a chance to use it. Think about using two different variable names here, like cached_data and response_data or something. Even if you meant to append the response data to the cached file data, it's probably a good idea to first read the response into a separate variable. That gives you an opportunity to check the response data for validity, non-duplication, etc. before adding it to the cached data.

If you know that the the response is going to be a list of dictionaries that's compatible with the list of dictionaries from the cache file, then you can simply append the response data with: data += response.json(). You could try that as a quick-fix, but I still …

Xioto commented: Thank You! It works now, I am embarrassed that I didn't notice that sooner... +0
Husoski 60 Newbie Poster

Your sample code doesn't say what "add" is, but I'll guess it's the full sized ArrayList with 150 elements.

Do you really need to create 30 more ArrayLists to hold them in groups of 5? You can get a view of 5 consecutive elements from that list, starting at any given index with:

    List<Double> five = add.subList(i, Math.min(i+5, add.size());

The syntax is similar to .substring() on a String object. Calling .sublist(i,j) returns all element from index i (inclusive) to index j (exclusive). If you need that to specifically be an ArrayList, you can pass that to ArrayList's Constructor:

    ArrayList<Double> five = new ArrayList<>(add.subList(i, Math.min(i+5, add.size())));

The business with Math.min() simply insures that j is never more than the size of the original list. You can leave that out and just write .sublist(i,i+5) if both i and the length of the large list are always multiples of 5.