Python Forum
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Statement has no effect"
#11
(Jan-25-2017, 08:38 PM)birdieman Wrote: I would have to refer to my variables like xyz["StartYear"] and I do not want to do that -- too long and cumbersome

Do you have another suggestion?
I suggest you listen to those who have programmed longer. You should at the bare minimum be using dictionaries.
You shouldn't have a million variables in your name space just because you think it is easier.
Reply
#12
Quote:I would have to refer to my variables like xyz["StartYear"] and I do not want to do that -- too long and cumbersome.
and your initial code is not?
Recommended Tutorials:
Reply
#13
Metulburr

I have played around with your code and have three questions

1. Why do you create/change a directory called "save" (at lease I think that is what the code does)? It changed the directory that I was using to 'Save'.

2. After the code reads the file, is there a way to refer to my variables directly, like StartYear, rather than the typical way to refer to an element in a dictionary like data['StartYear']?

3. What is the name of the file that the data is stored in?

By the way -- I really do appreciate your comments -- and I do listen -- just trying to learn more with my questions
Reply
#14
(Jan-26-2017, 12:37 AM)birdieman Wrote: 1. Why do you create/change a directory called "save" (at lease I think that is what the code does)? It changed the directory that I was using to 'Save'.
Because dictionaries is very useful data structure,some even bully Python of the use of dict anywhere.
(Jan-26-2017, 12:37 AM)birdieman Wrote: 2. After the code reads the file, is there a way to refer to my variables directly, like StartYear, rather than the typical way to refer to an element in a dictionary like data['StartYear']?
There are data['StartYear'] or data.get.('StartYear'),
you can make it a variable StartYear = data['StartYear'] but then you move from your dictionary to Python's build in dictionary Huh
So take a look a this:
>>> date = {}
>>> date['StartYear'] = 2016
>>> date['YourStartAge'] = 25
>>> date['LivExp'] = 80
>>> date
{'LivExp': 80, 'StartYear': 2016, 'YourStartAge': 25}

>>> # Accsess
>>> date['YourStartAge']
25
>>> date.get('YourStartAge')
25
>>> # get() can also be useful to catch stuff that are not in dict.
>>> date.get('hello', 'No in date dict')
'No in date dict'
So to this:
>>> YourStartAge = date['YourStartAge']
>>> YourStartAge
25
>>> # Now you have a varible YourStartAge
>>> # You have really moved it from your dict to Python build in dict(globals())
>>> globals()['YourStartAge']
25
Quote:3. What is the name of the file that the data is stored in?
It's up to you to figure out a name Wink
The dictionary i just made in and out to disk with json.
import json

date = {'LivExp': 25, 'StartYear': 2016, 'YourStartAge': 25}
with open("my_file.json", "w") as j_in:
   json.dump(date, j_in)
with open("my_file.json") as j_out:
   saved_date = json.load(j_out)

print(saved_date) # {'LivExp': 80, 'YourStartAge': 25, 'StartYear': 2016}
So on disk it will be a json file,json.load() take it from disk an it will again be the original dict.
Reply
#15
#1 I actually just copied it from my game, which i saved the JSON in a directory called save. And i didnt bother to change it.

#2 no. Once you load the json data, that is basically a dictionary. Im not sure why you have such as problem with using global variables VS dictionary keys? It does not pollute your namespace, and is much simpler to load/save. You also dont need to change the save/loading code if you add or remove a key, whereas you have to if you use variables as in your initial post. Less modification of code is less chance of bugs.

#3 in my example it is named "database{}".format(sys.version.split()[0]) which if your using python2.7.12 then the database name will be "database2.7.12", if your using python3.6.0 then your database name would be "database3.6.0".
Recommended Tutorials:
Reply
#16
Use a named tuple. In many uses (in particular, reading and writing) you won't even need to use the member names explicitly...
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#17
I would use a namedtuple if i didnt have control over how the file was structured in the first place. It sounds like the OP does though.
Recommended Tutorials:
Reply
#18
I will look up Named Tuples -- sounds interesting

Metulburr -- what did you mean by "if it didn't have control over how the file was structured"?

Also, what is OP?
Reply
#19
Quote:Metulburr -- what did you mean by "if it didn't have control over how the file was structured"?
If you were not the one writing to the file. You are just reading someone files or grabbing it off the net, and have no control on how they write it there. Like webscraping. You have not control over the HTML of sites, but you read it. Sometimes it can contain errors, but you have no control over writing it. 

Quote:Also, what is OP?
original post(er)
The person or their post that created a thread
Recommended Tutorials:
Reply
#20
I learned how to program 40 years ago (Fortran, cobol, etc -- you probably never heard of them), when the languages were not as powerful as python and others -- so my experience was with using many variables. I have only recently (6 months) started programming again.

I am reading and writing a file that I create -- no outside reading and writing -- I have total control. My program will read a bunch of variables from a file, the variables will be displayed in a GUI (not written yet), the user will change one or more of the variables, which will trigger some processing, and the variables will be saved when user is finished.

given that, which way -- dictionary described by you, or the named tuple suggested by Ofnuts, or something else -- should I proceed with? Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing effect sizes for variables in an anova eyavuz21 2 939 Feb-01-2023, 02:12 PM
Last Post: eyavuz21
  Help with TypeWriter Effect in Python Rich Extra 0 1,129 May-23-2022, 09:44 PM
Last Post: Extra
  Rotation Effect on live Webcam Feed Leziiy 0 1,576 Sep-12-2020, 04:25 PM
Last Post: Leziiy
  How to fix statement seems to have no effect hernancrespo89 0 6,739 Jan-23-2020, 09:23 PM
Last Post: hernancrespo89
  strange effect from duplicating a file descriptor Skaperen 1 2,018 Feb-18-2019, 08:20 PM
Last Post: Skaperen
  Algorithm effect on the CPU Faruk 3 2,570 Dec-19-2018, 08:57 AM
Last Post: Faruk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020