store dynamic data in variable python - python

i my code i have created one function for generate pdf. it can be called more then 2 times in code but at 1st time it can be fetch my data but after second time they give me none value. how can i store my data in list.
i am using list.append but when method call 2nd time then it can be give me null value [that is stored default in database]
my code is:-
def GeneratePdf(request, type):
# data for testing
# global data
template = get_template("employeereport/appointment.html")
# real data
empid = request.GET.get('empid')
Date = request.GET.get('date')
a = []
a.append(Date)
print(a)
output:-
['07/13/2020']
[None]
expected output-
['07/13/2020',None]

declare the list before using it
a = []
def function():
a.append(1)
function()
function()
print(a)

Related

Insert data in nested data structure

I have a variable like this:
self.playlist = {
"property_1":value_1,
"property_2":value_2,
...
"items":[]
}
where items array will contain objects of type self.playlist.
How can I insert data in self.playlist object? Let's have as input the playlist_items_sequence which i want to put data, the property_n and the value_n.
If playlist_items_sequence = [0,2,7] (zero will always be the first index, something like root) i want to put data to self.playlist["items"][2]["items"][7]["propery_n"] = value_n.
Another example: if playlist_items_sequence = [0,1,4,0] I want to execute self.playlist["items][1]["items"][4]["items"][0]["property_n"] = value_n
The simplest solution I thought is to use eval() and exec() for this data structure. But I am sure there will be a more clever solution.
Edit
If I do something like:
sub_playlist = self.playlist
for index_number in playlist_items_sequence[1:]:
sub_playlist = sub_playlist["items"][index_number]
sub_playlist["property_n"] = value_n
the self.playlist variable will not be updated.
But if I do:
exec_str = "self.playlist"
for index_number in playlist_items_sequence[1:]:
exec_str += "[\"items\"]["+str(index_number)+"]"
exec_str += "[\"property_n\"] = \""+str(value_n)"\""
exec(exec_str)
self.playlist variable will be updated.

Get a dictionary from a class?

I want to:
Take a list of lists
Make a frequency table in a dictionary
Do things with the resulting dictionary
The class works, the code works, the frequency table is correct.
I want to get a class that returns a dictionary, but I actually get a class that returns a class type.
I can see that it has the right content in there, but I just can't get it out.
Can someone show me how to turn the output of the class to a dictionary type?
I am working with HN post data. Columns, a few thousand rows.
freq_pph = {}
freq_cph = {}
freq_uph = {}
# Creates a binned frequency table:
# - key is bin_minutes (size of bin in minutes).
# - value is freq_value which sums/counts the number of things in that column.
class BinFreq:
def __init__(self, dataset, bin_minutes, freq_value, dict_name):
self.dataset = dataset
self.bin_minutes = bin_minutes
self.freq_value = freq_value
self.dict_name = dict_name
def make_table(self):
# Sets bin size
# Counts how of posts in that timedelta
if (self.bin_minutes == 60) and (self.freq_value == "None"):
for post in self.dataset:
hour_dt = post[-1]
hour_str = hour_dt.strftime("%H")
if hour_str in self.dict_name:
self.dict_name[hour_str] += 1
else:
self.dict_name[hour_str] = 1
# Sets bins size
# Sums the values of a given index/column
if (self.bin_minutes == 60) and (self.freq_value != "None"):
for post in self.dataset:
hour_dt = post[-1]
hour_str = hour_dt.strftime("%H")
if hour_str in self.dict_name:
self.dict_name[hour_str] += int(row[self.freq_value])
else:
self.dict_name[hour_str] = int(row[self.freq_value])
Instantiate:
pph = BinFreq(ask_posts, 60, "None", freq_pph)
pph.make_table()
How can pph be turned into a real dictionary?
If you want the make_table function to return a dictionary, then you have to add a return statement at the end of it, for example: return self.dict_name.
If you then want to use it outside of the class, you have to assign it to a variable, so in the second snipped do: my_dict = pph.make_table().
Classes can't return things – functions in classes could. However, the function in your class doesn't; it just modifies self.dict_name (which is a misnomer; it's really just a reference to a dict, not a name (which one might imagine is a string)), which the caller then reads (or should, anyway).
In addition, there seems to be a bug; the second if block (which is never reached anyway) refers to row, an undefined name.
Anyway, your class doesn't need to be a class at all, and is easiest implemented with the built-in collections.Counter() class:
from collections import Counter
def bin_by_hour(dataset, value_key=None):
counter = Counter()
for post in dataset:
hour = post[-1].hour # assuming it's a `datetime` object
if value_key: # count using `post[value_key]`
counter[hour] += post[value_key]
else: # just count
counter[hour] += 1
return dict(counter.items()) # make the Counter a regular dict
freq_pph = bin_by_hour(ask_posts)
freq_cph = bin_by_hour(ask_posts, value_key="num_comments") # or whatever

Unable to retun multiple dicitonary sets in python

I'm querying a REST API url & I'm trying to return all the dictionary sets, but only able to return one key pair.
Dictionary Output in the print statement inside for loop is the expected output, when when returned only one set of key pair is appearing.
Expected Dictionary looks like:
{'IncidentID': 'IM10265'}
{'IncidentID': 'IM10266'}
{'IncidentID': 'IM10267'}
{'IncidentID': 'IM10268'}
Code:
import json , requests
sm1 = requests.get('http://Rest Url', auth=('XX','YY'))
z = json.loads(sm1.text)
def get_im_list():
incidentlist_access = z['content']
for im_data in incidentlist_access:
Access_imslist = im_data['Incident']
print(Access_imslist)
#print(type(Access_imslist))
#return Access_imslist
data = get_im_list()
#print(data)
So when when I'm un-commentating
return Access_imslist & print(data)
I'm only receiving the output as:
{'IncidentID': 'IM10265'}
not the complete dictionary.
Every time you loop through the data, Access_imslist gets overwritten, so when you (presumably) return Access_Imlist it's only returning the last value.
You need to create a data structure outside of the for loop, add each bit of data to it, then return that instead. Something like:
def get_im_list():
incident_data = []
incidentlist_access = z['content']
for im_data in incidentlist_access:
incident_data.append(im_data['Incident'])
return incident_data
hope that helps!
you need to define a variable list , and then append the Access_imslist values to that variable.
like this :
data = []
def get_im_list():
incidentlist_access = z['content']
for im_data in incidentlist_access:
Access_imslist = im_data['Incident']
data.append(Access_imslist)
print(data)

Python issue value of property changes when falling out of loop scope

I am having trouble with a python class i wrote, the particular problem is in the parseData() function.
at the end of the function i have commented where the problem is. The problem is that when i finish the inner loop of the code towards the bottom of the parseData() function is run the value of self.listOfParsedWeatherData is correct, however once the scope falls out of the second loop the value of each element in self.listOfParsedWeatherData is a copy of the previous last element.
Basically when i check the value of the list within the scope of the second loop it would be something like [0, 1, 2, 3, 4, 5] but when i check the value of the same list outside of the second loop it it comes up as
[5, 5, 5, 5, 5, 5].
I am sure it has something to do with scopes or deep and shallow copies but I'm still fairly new to the python language and I'm not sure of some of the intricacies. Though i doubt the problem is something really complex.
Any help would be greatly appreciated the class is shown below and the object I'm inserting is a simple custom class that i wrote.
# imports
import pywapi # used to grab weather data from NOAA in a JSON format
import copy
from WeatherData import WeatherData # import custom weatherdata object
import pprint
pp = pprint.PrettyPrinter(indent=4) # set up pretty printer object
##
## Class used for grabing and parsing weather data from
## NOAA
##
class NOAAParser:
# initliazer for the NOAAParser class
def __init__(self):
# stores the list of json city data
self.listOfCityData = []
# A list of weatherData
self.listOfParsedWeatherData = []
##
## function to grab the weather and returns a JSON object of the weather data
##
## Parameters: self, dictionary of cities and their codes
##
def retrieveNOAAWeatherJson(self, cityCodes):
# loop through city codes and append them to a list of
cityData = []
# weather objects returned from pywapi
for key, value in cityCodes.iteritems():
cityData.append(pywapi.get_weather_from_noaa(value))
# set the list of city json data
self.listOfCityData = cityData
##
## This function parses the listOfCityData and returns a list
## of weatherData objects which can be displayed to the user
##
##
def parseData(self):
# check if the listOfCities has been populated
if not self.listOfCityData:
return False
else:
# create a new object of weather data
newWeatherData = WeatherData()
# loop over list and parse the data
for index in range(len(self.listOfCityData)):
# loop over the dictionary values in the list
for key, value in self.listOfCityData[index].iteritems():
# grab weather Description key: "weather" (String)
if key == "weather":
# print value
newWeatherData.weatherCondition = value
# grab the location key: "location" (String)
if key == "location":
# print value
newWeatherData.location = value
# grab temp key: "temperature_string" (String)
if key == "temperature_string":
# print value
newWeatherData.currentTemp = value
# grab Humidity key: "relative_humidity" (Int)
if key == "relative_humidity":
# print value
newWeatherData.humidity = value
# grab wind direction key: "wind_dir" (String)
if key == "wind_dir":
# print value
newWeatherData.windDirection = value
# grab last updated time key: "observation_time" (String)
if key == "observation_time":
# print value
newWeatherData.lastUpdated = value
# append the new weather data object to the list
self.listOfParsedWeatherData.append(newWeatherData)
## VALUE OF self.listOrParsedWeatherData is correct
## VALUE OF self.listOfParsedWeatherData is only the value of the last element
## of the previous self.listOfParsedWeatherData (incorrect)
# return success from the function
return True
This is happening because you only ever create a single instance of the WeatherData class, so you just keep updating that same one over and over again.
You just need to move the line newWeatherData = WeatherData() inside of your first for loop.

what should I change in my function

list_1 =[1, 2, 3, 4 ]
def fun(list_1):
for each value in list1:
# perform some operation and create a new data frame(pandas) for each value in the list
# so in total I should get 4 data frames.
print each_value
return new_data_frame
When I run fun(list_1) I should get 4 data frames:
1
data_frame_1
2
data_frame_2
3
data_frame_3
4
data_frame_4
but I am getting an ouput only with first value.
1
data_frame_1
so what should I change in my function.
When you return from the function, it's over.
You're out of the function and that's it.
You could use a generator to achieve what you want or you may return a tuple or a list.
Using a generator :
def fun(list_1):
for each_value in list_1:
# perform some operation and create a new data frame(pandas) named "new_data_frame" for each value in the list
print each_value
yield new_data_frame
for data_frame in fun(list_1):
# Do something with the data frame "data_frame"
This will print the value each time, and return your data frame.
But it returns a generator, so you have to call the function multiple times. What you cannot really do is get your data frames in a loop without wanting to store them in an list or assimilated or call the function only once.
A simpler way with a list could be:
def fun(list_1):
data_frames = []
for each_value in list1:
# perform some operation and create a new data frame(pandas) named "new_data_frame" for each value in the list
print each_value
data_frames.append(new_data_frame)
return data_frames
For reference, I'd suggest you have a look at What does the "yield" keyword do in Python?
which may be interesting for you.

Categories

Resources