Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary generator
#1
I need ideas how to implement such a dictionary, where the program will request data and ultimately get this kind of dictionary?
For example:

ac_voltage_multimeter = {(10, V): {(50, Hz): ((9, 'V'),), (60, Hz): ((9, 'V'),), (400, Hz):((9, 'V'),)},
(100, V): {(50, Hz): ((90, 'V'),), (60, Hz): ((90, 'V'),), (400, Hz):((90, 'V'),)},
(1100, V): {(50, Hz): ((200, 'V'),), (60, Hz): ((200, 'V'),(400, 'V'),(600, 'V'),(800, 'V'),(1000, 'V')), (400, Hz):((200, 'V'),)}}
Reply
#2
That depends on you and what you want. The code you have above needs some tweaks (Some of the "V" are not quoted and it looks like none of the "Hz" are quoted). But otherwise it creates a dictionary from static data.

If you want to create a dictionary from dynamic data, you need to figure out where the data will come from. A file? CLI input? Database? GUI?
Reply
#3
What does your input look like? Is it something like this?
10 V 50 Hz 9 V
10 V 60 Hz 9 V
10 V 4000 Hz 9 V
...
1100 V 400 Hz 200 V

Are you forced to use a dictionary? I think a classes would work better.
Reply
#4
(Jul-20-2021, 04:30 PM)bowlofred Wrote: That depends on you and what you want. The code you have above needs some tweaks (Some of the "V" are not quoted and it looks like none of the "Hz" are quoted). But otherwise it creates a dictionary from static data.

If you want to create a dictionary from dynamic data, you need to figure out where the data will come from. A file? CLI input? Database? GUI?
Yes, "V" need to quote (its my mistake)
all data come from CLI input
(Jul-20-2021, 04:36 PM)deanhystad Wrote: What does your input look like? Is it something like this?
10 V 50 Hz 9 V
10 V 60 Hz 9 V
10 V 4000 Hz 9 V
...
1100 V 400 Hz 200 V

Are you forced to use a dictionary? I think a classes would work better.

In fact, I don't know how it is better to arrange, as an option I use the dictionary at the moment.

The essence of the problem: I manage the electronic devices - the calibrator and the multimeter. The multimeter is set by the range (10, "V"), then the calibrator is set to the frequency (50, "Hz"), and the voltage (9, "V"), the multimeter measures this voltage, then the frequency (60, "Hz") , and voltage (9, "V") and so on as in the example.

Maybe you will advise how it is more correct or better to arrange, since I have such a fluent.
(Jul-20-2021, 04:30 PM)bowlofred Wrote: That depends on you and what you want. The code you have above needs some tweaks (Some of the "V" are not quoted and it looks like none of the "Hz" are quoted). But otherwise it creates a dictionary from static data.

If you want to create a dictionary from dynamic data, you need to figure out where the data will come from. A file? CLI input? Database? GUI?
or may be the best decision - its read data from file, which I will prepare in advance?
Reply
#5
It is not very clear what your intentions are.
Here are a few question that spring to mind:
  1. Where does the data come from.
  2. In what format is the incoming data.
  3. Are you able to show a sample of incoming data.
  4. What do you need to do with the data.
  5. How would you like the output of the data to be.
  6. What is done with the output data.
Reply
#6
class Multimeter:
    def __init__(self):
        someInfo = {}

acVoltageMultimeter = {'10V': Multimeter(), '100V': Multimeter(), '1100V': Multimeter()}
acVoltageMultimeter['10V'].someInfo = {'50Hz': '9V', '60Hz': '9V', '400Hz': '9V'}
acVoltageMultimeter['100V'].someInfo = {'50Hz': '90V', '60Hz': '90V', '400Hz': '90V'}
acVoltageMultimeter['1100V'].someInfo = {'50Hz': '200V', '60Hz': ['200V', '400V', '600V', '800V', '1000V'], '400Hz': '200V'}

key = acVoltageMultimeter.keys()
for k in key:
    print(k, acVoltageMultimeter[k].someInfo)
Output:
10V {'50Hz': '9V', '60Hz': '9V', '400Hz': '9V'} 100V {'50Hz': '90V', '60Hz': '90V', '400Hz': '90V'} 1100V {'50Hz': '200V', '60Hz': ['200V', '400V', '600V', '800V', '1000V'], '400Hz': '200V'}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  receive from a generator, send to a generator Skaperen 9 5,422 Feb-05-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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